1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
--- autofs-4.1.3/modules/lookup_nssldap.c 1970-01-01 01:00:00.000000000 +0100
+++ autofs-4.1.3/modules/lookup_nssldap.c 2005-06-28 15:13:49.000000000 +0200
@@ -0,0 +1,326 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *
+ * lookup_nss.c - module for Linux automountd to access a NSS
+ * automount map
+ *
+ * Copyright 1997 Transmeta Corporation - All Rights Reserved
+ * Copyright 2001-2003 Ian Kent <raven@themaw.net>
+ * Copyright 2005 PADL Software Pty Ltd - All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
+ * USA; either version 2 of the License, or (at your option) any later
+ * version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <time.h>
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <resolv.h>
+#include <dlfcn.h>
+#include <nss.h>
+
+#define MODULE_LOOKUP
+#include "automount.h"
+
+#define MAPFMT_DEFAULT "sun"
+
+#define NAMESERVICE "ldap"
+
+#define MODPREFIX "lookup(nss" NAMESERVICE "): "
+
+struct lookup_context {
+ char *nsname;
+ char *mapname;
+ struct parse_mod *parse;
+ void *dlhandle;
+ enum nss_status (*setautomntent)(const char *, void **);
+ enum nss_status (*getautomntent_r)(void *, const char **, const char **,
+ char *, size_t, int *);
+ enum nss_status (*endautomntent)(void **);
+ enum nss_status (*getautomntbyname_r)(void *, const char *,
+ const char **, const char **,
+ char *, size_t, int *);
+};
+
+int lookup_version = AUTOFS_LOOKUP_VERSION;
+
+int lookup_init(const char *mapfmt, int argc, const char *const *argv, void **context_p)
+{
+ struct lookup_context *context;
+ char buf[1024];
+
+ context = (struct lookup_context *)malloc(sizeof(*context));
+ if (context == NULL) {
+ crit(MODPREFIX "malloc: %m");
+ return 1;
+ }
+ memset(context, 0, sizeof(*context));
+
+ context->nsname = NULL;
+ context->parse = NULL;
+ context->dlhandle = NULL;
+ context->setautomntent = NULL;
+ context->getautomntent_r = NULL;
+ context->endautomntent = NULL;
+
+ if (mapfmt == NULL) {
+ mapfmt = MAPFMT_DEFAULT;
+ }
+
+ if (argc < 1) {
+ crit(MODPREFIX "invalid number of arguments");
+ return 1;
+ }
+
+ asprintf(&context->nsname, "nss%s", NAMESERVICE);
+ if (context->nsname == NULL) {
+ crit(MODPREFIX "strdup: %m");
+ return 1;
+ }
+
+ snprintf(buf, sizeof(buf), "/lib/libnss_%s.so.2", NAMESERVICE);
+
+ context->dlhandle = dlopen(buf, RTLD_NOW | RTLD_LOCAL);
+ if (context->dlhandle == NULL) {
+ crit(MODPREFIX "failed to load %s nameservice provider: %s", NAMESERVICE, dlerror());
+ return 1;
+ }
+
+ snprintf(buf, sizeof(buf), "_nss_%s_setautomntent", NAMESERVICE);
+ context->setautomntent = dlsym(context->dlhandle, buf);
+ if (context->setautomntent == NULL) {
+ crit(MODPREFIX "failed to load %s nameservice provider: %s", NAMESERVICE, dlerror());
+ return 1;
+ }
+
+ snprintf(buf, sizeof(buf), "_nss_%s_getautomntent_r", NAMESERVICE);
+ context->getautomntent_r = dlsym(context->dlhandle, buf);
+ if (context->getautomntent_r == NULL) {
+ crit(MODPREFIX "failed to load %s nameservice provider: %s", NAMESERVICE, dlerror());
+ return 1;
+ }
+
+ snprintf(buf, sizeof(buf), "_nss_%s_endautomntent", NAMESERVICE);
+ context->endautomntent = dlsym(context->dlhandle, buf);
+ if (context->endautomntent == NULL) {
+ crit(MODPREFIX "failed to load %s nameservice provider: %s", NAMESERVICE, dlerror());
+ return 1;
+ }
+
+ snprintf(buf, sizeof(buf), "_nss_%s_getautomntbyname_r", NAMESERVICE);
+ context->getautomntbyname_r = dlsym(context->dlhandle, buf);
+ if (context->getautomntbyname_r == NULL) {
+ crit(MODPREFIX "failed to load %s nameservice provider: %s", NAMESERVICE, dlerror());
+ return 1;
+ }
+
+ context->mapname = strdup(argv[0]);
+ if (context->mapname == NULL) {
+ crit(MODPREFIX "strdup: %m");
+ return 1;
+ }
+
+ context->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
+ if (context->parse == NULL) {
+ free(context);
+ return 1;
+ }
+
+ *context_p = context;
+ return 0;
+}
+
+static const char *nsserr_string(enum nss_status status)
+{
+ switch (status) {
+ case NSS_STATUS_TRYAGAIN:
+ return "Insufficient buffer space";
+ break;
+ case NSS_STATUS_UNAVAIL:
+ return "Name service unavailable";
+ break;
+ case NSS_STATUS_NOTFOUND:
+ return "Not found";
+ break;
+ case NSS_STATUS_SUCCESS:
+ return "Success";
+ break;
+ default:
+ break;
+ }
+
+ return "Unknown error";
+}
+
+static int read_map(const char *root, struct lookup_context *context)
+{
+ enum nss_status status;
+ void *private = NULL;
+ time_t age = time(NULL);
+ const char *key, *mapent;
+ int nss_errno;
+ char buffer[KEY_MAX_LEN + 1 + MAPENT_MAX_LEN + 1];
+
+ status = (*context->setautomntent)(context->mapname, &private);
+ if (status != NSS_STATUS_SUCCESS) {
+ warn(MODPREFIX "failed to read map %s: %s",
+ context->mapname, nsserr_string(status));
+ return 0;
+ }
+
+ for (;;) {
+ status = (*context->getautomntent_r)(private, &key, &mapent,
+ buffer, sizeof(buffer),
+ &nss_errno);
+ if (status != NSS_STATUS_SUCCESS)
+ break;
+
+ cache_update(key, mapent, age);
+ }
+
+ (*context->endautomntent)(&private);
+
+ cache_clean(root, age);
+ return 1;
+}
+
+int lookup_ghost(const char *root, int ghost, void *context)
+{
+ struct lookup_context *ctxt = (struct lookup_context *)context;
+ struct mapent_cache *me;
+ int status = 1;
+
+ if (!read_map(root, ctxt))
+ return LKP_FAIL;
+
+ status = cache_ghost(root, ghost, ctxt->mapname, ctxt->nsname, ctxt->parse);
+
+ me = cache_lookup_first();
+ if (me == NULL)
+ return LKP_FAIL;
+
+ if (*me->key == '/' && *(root + 1) != '-') {
+ me = cache_partial_match(root);
+ /* me NULL => no entries for this direct mount root or indirect map */
+ if (me == NULL)
+ return LKP_FAIL | LKP_INDIRECT;
+ }
+
+ return status;
+}
+
+int lookup_mount(const char *root, const char *name, int name_len, void *context)
+{
+ struct lookup_context *ctxt = (struct lookup_context *)context;
+ char key[KEY_MAX_LEN + 1];
+ char buffer[KEY_MAX_LEN + 1 + MAPENT_MAX_LEN + 1];
+ const char *canon_key, *mapent;
+ struct mapent_cache *me = NULL;
+ time_t age = time(NULL);
+ enum nss_status status;
+
+ debug(MODPREFIX "looking up %s", name);
+
+ snprintf(key, sizeof(key), "%s/%s", root, name);
+
+ me = cache_lookup(name);
+ if (me == NULL) {
+ me = cache_lookup(key);
+ }
+
+ if (me == NULL) {
+ /* path component, do submount */
+ me = cache_partial_match(key);
+
+ if (me) {
+ snprintf(buffer, sizeof(buffer), "-fstype=autofs %s:%s",
+ ctxt->nsname, ctxt->mapname);
+ mapent = buffer;
+ }
+ } else {
+ snprintf(buffer, sizeof(buffer), "%s", me->mapent);
+ mapent = buffer;
+ }
+
+ if (me == NULL) {
+ const char *keys[3];
+ int i;
+ int nss_errno;
+ void *private = NULL;
+
+ status = (*ctxt->setautomntent)(ctxt->mapname, &private);
+ if (status != NSS_STATUS_SUCCESS) {
+ warn(MODPREFIX "failed to read map %s: %s", ctxt->mapname, nsserr_string(status));
+ goto out_err;
+ }
+
+ keys[0] = name,
+ keys[1] = key;
+ keys[2] = "*";
+
+ for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
+ status = (*ctxt->getautomntbyname_r)(private, name,
+ &canon_key, &mapent,
+ buffer, sizeof(buffer),
+ &nss_errno);
+ if (status != NSS_STATUS_NOTFOUND)
+ break;
+ }
+
+ (*ctxt->endautomntent)(&private);
+
+ if (status != NSS_STATUS_SUCCESS) {
+ warn(MODPREFIX "failed to read map %s: %s", ctxt->mapname, nsserr_string(status));
+ goto out_err;
+ }
+
+ cache_update(keys[i], mapent, age);
+ }
+
+ debug(MODPREFIX "%s -> %s", name, mapent);
+
+ return ctxt->parse->parse_mount(root, name, name_len, mapent, ctxt->parse->context);
+
+out_err:
+ warn(MODPREFIX "lookup for %s failed: %d", name, status);
+ return 1;
+}
+
+int lookup_done(void *context)
+{
+ struct lookup_context *ctxt = (struct lookup_context *)context;
+ int ret;
+
+ if (ctxt->nsname != NULL) {
+ free(ctxt->nsname);
+ ctxt->nsname = NULL;
+ }
+
+ if (ctxt->mapname != NULL) {
+ free(ctxt->mapname);
+ ctxt->mapname = NULL;
+ }
+
+ ret = close_parse(ctxt->parse);
+
+ if (ctxt->dlhandle != NULL) {
+ dlclose(ctxt->dlhandle);
+ ctxt->dlhandle = NULL;
+ }
+
+ memset(ctxt, 0, sizeof(*ctxt));
+ free(ctxt);
+
+ return ret;
+}
+
--- autofs-4.1.3/modules/lookup_nssldap.c 2005-06-28 15:13:49.000000000 +0200
+++ autofs-4.1.3/modules/lookup_nssldap.c 2005-07-21 11:32:58.000000000 +0200
@@ -16,6 +16,8 @@
*
* ----------------------------------------------------------------------- */
+#define _GNU_SOURCE
+#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
@@ -88,7 +90,7 @@
return 1;
}
- snprintf(buf, sizeof(buf), "/lib/libnss_%s.so.2", NAMESERVICE);
+ snprintf(buf, sizeof(buf), "libnss_%s.so.2", NAMESERVICE);
context->dlhandle = dlopen(buf, RTLD_NOW | RTLD_LOCAL);
if (context->dlhandle == NULL) {
@@ -184,8 +186,11 @@
&nss_errno);
if (status != NSS_STATUS_SUCCESS)
break;
-
+#ifdef CHE_FAIL
+ cache_update(root, key, mapent, age);
+#else
cache_update(key, mapent, age);
+#endif
}
(*context->endautomntent)(&private);
@@ -194,7 +199,7 @@
return 1;
}
-int lookup_ghost(const char *root, int ghost, void *context)
+int lookup_ghost(const char *root, int ghost, time_t now, void *context)
{
struct lookup_context *ctxt = (struct lookup_context *)context;
struct mapent_cache *me;
@@ -284,7 +289,11 @@
goto out_err;
}
+#ifdef CHE_FAIL
+ cache_update(root, keys[i], mapent, age);
+#else
cache_update(keys[i], mapent, age);
+#endif
}
debug(MODPREFIX "%s -> %s", name, mapent);
--- autofs-4.1.3/modules/Makefile 2004-04-03 09:14:33.000000000 +0200
+++ autofs-4.1.3/modules/Makefile 2005-07-21 11:39:35.000000000 +0200
@@ -7,13 +7,13 @@
include ../Makefile.rules
SRCS := lookup_yp.c lookup_file.c lookup_program.c lookup_userhome.c \
- lookup_multi.c \
+ lookup_multi.c lookup_nssldap.c \
parse_sun.c \
mount_generic.c mount_nfs.c mount_afs.c mount_autofs.c \
mount_changer.c mount_bind.c
MODS := lookup_yp.so lookup_file.so lookup_program.so lookup_userhome.so \
- lookup_multi.so \
+ lookup_multi.so lookup_nssldap.so \
parse_sun.so \
mount_generic.so mount_nfs.so mount_afs.so mount_autofs.so \
mount_changer.so mount_bind.so
|