~ubuntu-branches/ubuntu/precise/autofs5/precise

« back to all changes in this revision

Viewing changes to .pc/autofs-5.0.5-mapent-becomes-negative-during-lookup.patch/modules/lookup_yp.c

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-03 14:35:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110703143546-nej26krjij0rf792
Tags: 5.0.6-0ubuntu1
* New upstream release:
  - Dropped upstream patches 
  - Refreshed debian/patches/17ld.patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ----------------------------------------------------------------------- *
2
 
 *   
3
 
 *  lookup_yp.c - module for Linux automountd to access a YP (NIS)
4
 
 *                automount map
5
 
 *
6
 
 *   Copyright 1997 Transmeta Corporation - All Rights Reserved
7
 
 *   Copyright 2001-2003 Ian Kent <raven@themaw.net>
8
 
 *
9
 
 *   This program is free software; you can redistribute it and/or modify
10
 
 *   it under the terms of the GNU General Public License as published by
11
 
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
12
 
 *   USA; either version 2 of the License, or (at your option) any later
13
 
 *   version; incorporated herein by reference.
14
 
 *
15
 
 * ----------------------------------------------------------------------- */
16
 
 
17
 
#include <stdio.h>
18
 
#include <malloc.h>
19
 
#include <time.h>
20
 
#include <signal.h>
21
 
#include <ctype.h>
22
 
#include <sys/param.h>
23
 
#include <sys/types.h>
24
 
#include <sys/stat.h>
25
 
#include <rpc/rpc.h>
26
 
#include <rpc/xdr.h>
27
 
#include <rpcsvc/yp_prot.h>
28
 
#include <rpcsvc/ypclnt.h>
29
 
 
30
 
#define MODULE_LOOKUP
31
 
#include "automount.h"
32
 
#include "nsswitch.h"
33
 
 
34
 
#define MAPFMT_DEFAULT "sun"
35
 
 
36
 
#define MODPREFIX "lookup(yp): "
37
 
 
38
 
struct lookup_context {
39
 
        const char *domainname;
40
 
        const char *mapname;
41
 
        unsigned long order;
42
 
        struct parse_mod *parse;
43
 
};
44
 
 
45
 
struct callback_master_data {
46
 
        unsigned timeout;
47
 
        unsigned logging;
48
 
        unsigned logopt;
49
 
        time_t age;
50
 
};
51
 
 
52
 
struct callback_data {
53
 
        struct autofs_point *ap;
54
 
        struct map_source *source;
55
 
        unsigned logopt;
56
 
        time_t age;
57
 
};
58
 
 
59
 
int lookup_version = AUTOFS_LOOKUP_VERSION;     /* Required by protocol */
60
 
 
61
 
static unsigned int get_map_order(const char *domain, const char *map)
62
 
{
63
 
        char key[] = "YP_LAST_MODIFIED";
64
 
        int key_len = strlen(key);
65
 
        char *order;
66
 
        int order_len;
67
 
        char *mapname;
68
 
        long last_changed;
69
 
        int err;
70
 
 
71
 
        mapname = alloca(strlen(map) + 1);
72
 
        if (!mapname)
73
 
                return 0;
74
 
 
75
 
        strcpy(mapname, map);
76
 
 
77
 
        err = yp_match(domain, mapname, key, key_len, &order, &order_len);
78
 
        if (err != YPERR_SUCCESS) {
79
 
                if (err == YPERR_MAP) {
80
 
                        char *usc;
81
 
 
82
 
                        while ((usc = strchr(mapname, '_')))
83
 
                                *usc = '.';
84
 
 
85
 
                        err = yp_match(domain, mapname,
86
 
                                       key, key_len, &order, &order_len);
87
 
 
88
 
                        if (err != YPERR_SUCCESS)
89
 
                                return 0;
90
 
 
91
 
                        last_changed = atol(order);
92
 
                        free(order);
93
 
 
94
 
                        return (unsigned int) last_changed;
95
 
                }
96
 
                return 0;
97
 
        }
98
 
 
99
 
        last_changed = atol(order);
100
 
        free(order);
101
 
 
102
 
        return (unsigned int) last_changed;
103
 
}
104
 
 
105
 
int lookup_init(const char *mapfmt, int argc, const char *const *argv, void **context)
106
 
{
107
 
        struct lookup_context *ctxt;
108
 
        char buf[MAX_ERR_BUF];
109
 
        int err;
110
 
 
111
 
        *context = NULL;
112
 
 
113
 
        ctxt = malloc(sizeof(struct lookup_context));
114
 
        if (!ctxt) {
115
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
116
 
                logerr(MODPREFIX "%s", estr);
117
 
                return 1;
118
 
        }
119
 
        memset(ctxt, 0, sizeof(struct lookup_context));
120
 
 
121
 
        if (argc < 1) {
122
 
                free(ctxt);
123
 
                logerr(MODPREFIX "no map name");
124
 
                return 1;
125
 
        }
126
 
        ctxt->mapname = argv[0];
127
 
 
128
 
        /* This should, but doesn't, take a const char ** */
129
 
        err = yp_get_default_domain((char **) &ctxt->domainname);
130
 
        if (err) {
131
 
                size_t len = strlen(ctxt->mapname);
132
 
                char *name = alloca(len + 1);
133
 
                memcpy(name, ctxt->mapname, len);
134
 
                name[len] = '\0';
135
 
                free(ctxt);
136
 
                logerr(MODPREFIX "map %s: %s", name, yperr_string(err));
137
 
                return 1;
138
 
        }
139
 
 
140
 
        ctxt->order = get_map_order(ctxt->domainname, ctxt->mapname);
141
 
 
142
 
        if (!mapfmt)
143
 
                mapfmt = MAPFMT_DEFAULT;
144
 
 
145
 
        ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
146
 
        if (!ctxt->parse) {
147
 
                free(ctxt);
148
 
                logmsg(MODPREFIX "failed to open parse context");
149
 
                return 1;
150
 
        }
151
 
        *context = ctxt;
152
 
 
153
 
        return 0;
154
 
}
155
 
 
156
 
int yp_all_master_callback(int status, char *ypkey, int ypkeylen,
157
 
                    char *val, int vallen, char *ypcb_data)
158
 
{
159
 
        struct callback_master_data *cbdata =
160
 
                        (struct callback_master_data *) ypcb_data;
161
 
        unsigned int timeout = cbdata->timeout;
162
 
        unsigned int logging = cbdata->logging;
163
 
        unsigned int logopt = cbdata->logopt;
164
 
        time_t age = cbdata->age;
165
 
        char *buffer;
166
 
        unsigned int len;
167
 
 
168
 
        if (status != YP_TRUE)
169
 
                return status;
170
 
 
171
 
        /* Ignore zero length and single non-printable char keys */
172
 
        if (ypkeylen == 0 || (ypkeylen == 1 && !isprint(*ypkey))) {
173
 
                warn(logopt, MODPREFIX
174
 
                     "ignoring invalid map entry, zero length or "
175
 
                     "single character non-printable key");
176
 
                return 0;
177
 
        }
178
 
 
179
 
        /*
180
 
         * Ignore keys beginning with '+' as plus map
181
 
         * inclusion is only valid in file maps.
182
 
         */
183
 
        if (*ypkey == '+')
184
 
                return 0;
185
 
 
186
 
        *(ypkey + ypkeylen) = '\0';
187
 
        *(val + vallen) = '\0';
188
 
 
189
 
        len = ypkeylen + 1 + vallen + 2;
190
 
 
191
 
        buffer = alloca(len);
192
 
        if (!buffer) {
193
 
                error(logopt, MODPREFIX "could not malloc parse buffer");
194
 
                return 0;
195
 
        }
196
 
        memset(buffer, 0, len);
197
 
 
198
 
        strcat(buffer, ypkey);
199
 
        strcat(buffer, " ");
200
 
        strcat(buffer, val);
201
 
 
202
 
        master_parse_entry(buffer, timeout, logging, age);
203
 
 
204
 
        return 0;
205
 
}
206
 
 
207
 
int lookup_read_master(struct master *master, time_t age, void *context)
208
 
{
209
 
        struct lookup_context *ctxt = (struct lookup_context *) context;
210
 
        struct ypall_callback ypcb;
211
 
        struct callback_master_data ypcb_data;
212
 
        unsigned int logging = master->default_logging;
213
 
        unsigned int logopt = master->logopt;
214
 
        char *mapname;
215
 
        int err;
216
 
 
217
 
        mapname = alloca(strlen(ctxt->mapname) + 1);
218
 
        if (!mapname)
219
 
                return 0;
220
 
 
221
 
        strcpy(mapname, ctxt->mapname);
222
 
 
223
 
        ypcb_data.timeout = master->default_timeout;
224
 
        ypcb_data.logging = logging;
225
 
        ypcb_data.logopt = logopt;
226
 
        ypcb_data.age = age;
227
 
 
228
 
        ypcb.foreach = yp_all_master_callback;
229
 
        ypcb.data = (char *) &ypcb_data;
230
 
 
231
 
        err = yp_all((char *) ctxt->domainname, mapname, &ypcb);
232
 
 
233
 
        if (err != YPERR_SUCCESS) {
234
 
                if (err == YPERR_MAP) {
235
 
                        char *usc;
236
 
 
237
 
                        while ((usc = strchr(mapname, '_')))
238
 
                                *usc = '.';
239
 
 
240
 
                        err = yp_all((char *) ctxt->domainname, mapname, &ypcb);
241
 
                }
242
 
 
243
 
                if (err == YPERR_SUCCESS)
244
 
                        return NSS_STATUS_SUCCESS;
245
 
 
246
 
                info(logopt,
247
 
                     MODPREFIX "read of master map %s failed: %s",
248
 
                     mapname, yperr_string(err));
249
 
 
250
 
                if (err == YPERR_PMAP || err == YPERR_YPSERV)
251
 
                        return NSS_STATUS_UNAVAIL;
252
 
 
253
 
                return NSS_STATUS_NOTFOUND;
254
 
        }
255
 
 
256
 
        return NSS_STATUS_SUCCESS;
257
 
}
258
 
 
259
 
int yp_all_callback(int status, char *ypkey, int ypkeylen,
260
 
                    char *val, int vallen, char *ypcb_data)
261
 
{
262
 
        struct callback_data *cbdata = (struct callback_data *) ypcb_data;
263
 
        struct autofs_point *ap = cbdata->ap;
264
 
        struct map_source *source = cbdata->source;
265
 
        struct mapent_cache *mc = source->mc;
266
 
        unsigned int logopt = cbdata->logopt;
267
 
        time_t age = cbdata->age;
268
 
        char *key, *mapent;
269
 
        int ret;
270
 
 
271
 
        if (status != YP_TRUE)
272
 
                return status;
273
 
 
274
 
        /* Ignore zero length and single non-printable char keys */
275
 
        if (ypkeylen == 0 || (ypkeylen == 1 && !isprint(*ypkey))) {
276
 
                warn(logopt, MODPREFIX
277
 
                     "ignoring invalid map entry, zero length or "
278
 
                     "single character non-printable key");
279
 
                return 0;
280
 
        }
281
 
 
282
 
        /*
283
 
         * Ignore keys beginning with '+' as plus map
284
 
         * inclusion is only valid in file maps.
285
 
         */
286
 
        if (*ypkey == '+')
287
 
                return 0;
288
 
 
289
 
        key = sanitize_path(ypkey, ypkeylen, ap->type, ap->logopt);
290
 
        if (!key) {
291
 
                error(logopt, MODPREFIX "invalid path %s", ypkey);
292
 
                return 0;
293
 
        }
294
 
 
295
 
        mapent = alloca(vallen + 1);
296
 
        strncpy(mapent, val, vallen);
297
 
        *(mapent + vallen) = '\0';
298
 
 
299
 
        cache_writelock(mc);
300
 
        ret = cache_update(mc, source, key, mapent, age);
301
 
        cache_unlock(mc);
302
 
 
303
 
        free(key);
304
 
 
305
 
        if (ret == CHE_FAIL)
306
 
                return -1;
307
 
 
308
 
        return 0;
309
 
}
310
 
 
311
 
int lookup_read_map(struct autofs_point *ap, time_t age, void *context)
312
 
{
313
 
        struct lookup_context *ctxt = (struct lookup_context *) context;
314
 
        struct ypall_callback ypcb;
315
 
        struct callback_data ypcb_data;
316
 
        unsigned int logopt = ap->logopt;
317
 
        struct map_source *source;
318
 
        char *mapname;
319
 
        int err;
320
 
 
321
 
        source = ap->entry->current;
322
 
        ap->entry->current = NULL;
323
 
        master_source_current_signal(ap->entry);
324
 
 
325
 
        /*
326
 
         * If we don't need to create directories then there's no use
327
 
         * reading the map. We always need to read the whole map for
328
 
         * direct mounts in order to mount the triggers.
329
 
         */
330
 
        if (!(ap->flags & MOUNT_FLAG_GHOST) && ap->type != LKP_DIRECT)
331
 
                return NSS_STATUS_SUCCESS;
332
 
 
333
 
        ypcb_data.ap = ap;
334
 
        ypcb_data.source = source;
335
 
        ypcb_data.logopt = logopt;
336
 
        ypcb_data.age = age;
337
 
 
338
 
        ypcb.foreach = yp_all_callback;
339
 
        ypcb.data = (char *) &ypcb_data;
340
 
 
341
 
        mapname = alloca(strlen(ctxt->mapname) + 1);
342
 
        if (!mapname)
343
 
                return NSS_STATUS_UNKNOWN;
344
 
 
345
 
        strcpy(mapname, ctxt->mapname);
346
 
 
347
 
        err = yp_all((char *) ctxt->domainname, mapname, &ypcb);
348
 
 
349
 
        if (err != YPERR_SUCCESS) {
350
 
                if (err == YPERR_MAP) {
351
 
                        char *usc;
352
 
 
353
 
                        while ((usc = strchr(mapname, '_')))
354
 
                                *usc = '.';
355
 
 
356
 
                        err = yp_all((char *) ctxt->domainname, mapname, &ypcb);
357
 
                }
358
 
 
359
 
                if (err == YPERR_SUCCESS)
360
 
                        return NSS_STATUS_SUCCESS;
361
 
 
362
 
                warn(ap->logopt,
363
 
                     MODPREFIX "read of map %s failed: %s",
364
 
                     ap->path, yperr_string(err));
365
 
 
366
 
                if (err == YPERR_PMAP || err == YPERR_YPSERV)
367
 
                        return NSS_STATUS_UNAVAIL;
368
 
 
369
 
                return NSS_STATUS_NOTFOUND;
370
 
        }
371
 
 
372
 
        source->age = age;
373
 
 
374
 
        return NSS_STATUS_SUCCESS;
375
 
}
376
 
 
377
 
static int lookup_one(struct autofs_point *ap,
378
 
                      const char *key, int key_len,
379
 
                      struct lookup_context *ctxt)
380
 
{
381
 
        struct map_source *source;
382
 
        struct mapent_cache *mc;
383
 
        char *mapname;
384
 
        char *mapent;
385
 
        int mapent_len;
386
 
        time_t age = time(NULL);
387
 
        int ret;
388
 
 
389
 
        source = ap->entry->current;
390
 
        ap->entry->current = NULL;
391
 
        master_source_current_signal(ap->entry);
392
 
 
393
 
        mc = source->mc;
394
 
 
395
 
        mapname = alloca(strlen(ctxt->mapname) + 1);
396
 
        if (!mapname)
397
 
                return 0;
398
 
 
399
 
        strcpy(mapname, ctxt->mapname);
400
 
 
401
 
        /*
402
 
         * For reasons unknown, the standard YP definitions doesn't
403
 
         * define input strings as const char *.  However, my
404
 
         * understanding is that they will not be modified by the
405
 
         * library.
406
 
         */
407
 
        ret = yp_match((char *) ctxt->domainname, mapname,
408
 
                       (char *) key, key_len, &mapent, &mapent_len);
409
 
 
410
 
        if (ret != YPERR_SUCCESS) {
411
 
                if (ret == YPERR_MAP) {
412
 
                        char *usc;
413
 
 
414
 
                        while ((usc = strchr(mapname, '_')))
415
 
                                *usc = '.';
416
 
 
417
 
                        ret = yp_match((char *) ctxt->domainname,
418
 
                                mapname, key, key_len, &mapent, &mapent_len);
419
 
                }
420
 
 
421
 
                if (ret != YPERR_SUCCESS) {
422
 
                        if (ret == YPERR_KEY)
423
 
                                return CHE_MISSING;
424
 
 
425
 
                        return -ret;
426
 
                }
427
 
        }
428
 
 
429
 
        cache_writelock(mc);
430
 
        ret = cache_update(mc, source, key, mapent, age);
431
 
        cache_unlock(mc);
432
 
        free(mapent);
433
 
 
434
 
        return ret;
435
 
}
436
 
 
437
 
static int lookup_wild(struct autofs_point *ap, struct lookup_context *ctxt)
438
 
{
439
 
        struct map_source *source;
440
 
        struct mapent_cache *mc;
441
 
        char *mapname;
442
 
        char *mapent;
443
 
        int mapent_len;
444
 
        time_t age = time(NULL);
445
 
        int ret;
446
 
 
447
 
        source = ap->entry->current;
448
 
        ap->entry->current = NULL;
449
 
        master_source_current_signal(ap->entry);
450
 
 
451
 
        mc = source->mc;
452
 
 
453
 
        mapname = alloca(strlen(ctxt->mapname) + 1);
454
 
        if (!mapname)
455
 
                return 0;
456
 
 
457
 
        strcpy(mapname, ctxt->mapname);
458
 
 
459
 
        ret = yp_match((char *) ctxt->domainname,
460
 
                       mapname, "*", 1, &mapent, &mapent_len);
461
 
 
462
 
        if (ret != YPERR_SUCCESS) {
463
 
                if (ret == YPERR_MAP) {
464
 
                        char *usc;
465
 
 
466
 
                        while ((usc = strchr(mapname, '_')))
467
 
                                *usc = '.';
468
 
 
469
 
                        ret = yp_match((char *) ctxt->domainname,
470
 
                                mapname, "*", 1, &mapent, &mapent_len);
471
 
                }
472
 
 
473
 
                if (ret != YPERR_SUCCESS) {
474
 
                        if (ret == YPERR_KEY)
475
 
                                return CHE_MISSING;
476
 
 
477
 
                        return -ret;
478
 
                }
479
 
        }
480
 
 
481
 
        cache_writelock(mc);
482
 
        ret = cache_update(mc, source, "*", mapent, age);
483
 
        cache_unlock(mc);
484
 
        free(mapent);
485
 
 
486
 
        return ret;
487
 
}
488
 
 
489
 
static int check_map_indirect(struct autofs_point *ap,
490
 
                              char *key, int key_len,
491
 
                              struct lookup_context *ctxt)
492
 
{
493
 
        struct map_source *source;
494
 
        struct mapent_cache *mc;
495
 
        struct mapent *exists;
496
 
        unsigned int map_order;
497
 
        int ret = 0;
498
 
 
499
 
        source = ap->entry->current;
500
 
        ap->entry->current = NULL;
501
 
        master_source_current_signal(ap->entry);
502
 
 
503
 
        mc = source->mc;
504
 
 
505
 
        master_source_current_wait(ap->entry);
506
 
        ap->entry->current = source;
507
 
 
508
 
        /* check map and if change is detected re-read map */
509
 
        ret = lookup_one(ap, key, key_len, ctxt);
510
 
        if (ret == CHE_FAIL)
511
 
                return NSS_STATUS_NOTFOUND;
512
 
 
513
 
        if (ret < 0) {
514
 
                /*
515
 
                 * If the server is down and the entry exists in the cache
516
 
                 * and belongs to this map return success and use the entry.
517
 
                 */
518
 
                exists = cache_lookup(mc, key);
519
 
                if (exists && exists->source == source)
520
 
                        return NSS_STATUS_SUCCESS;
521
 
 
522
 
                warn(ap->logopt,
523
 
                     MODPREFIX "lookup for %s failed: %s",
524
 
                     key, yperr_string(-ret));
525
 
 
526
 
                return NSS_STATUS_UNAVAIL;
527
 
        }
528
 
 
529
 
        /* Only read map if it has been modified */
530
 
        map_order = get_map_order(ctxt->domainname, ctxt->mapname);
531
 
        if (map_order > ctxt->order) {
532
 
                ctxt->order = map_order;
533
 
                source->stale = 1;
534
 
        }
535
 
 
536
 
        cache_writelock(mc);
537
 
        exists = cache_lookup_distinct(mc, key);
538
 
        /* Not found in the map but found in the cache */
539
 
        if (exists && exists->source == source && ret & CHE_MISSING) {
540
 
                if (exists->mapent) {
541
 
                        free(exists->mapent);
542
 
                        exists->mapent = NULL;
543
 
                        source->stale = 1;
544
 
                        exists->status = 0;
545
 
                }
546
 
        }
547
 
        cache_unlock(mc);
548
 
 
549
 
        if (ret == CHE_MISSING) {
550
 
                struct mapent *we;
551
 
                int wild = CHE_MISSING;
552
 
 
553
 
                master_source_current_wait(ap->entry);
554
 
                ap->entry->current = source;
555
 
 
556
 
                wild = lookup_wild(ap, ctxt);
557
 
                /*
558
 
                 * Check for map change and update as needed for
559
 
                 * following cache lookup.
560
 
                 */
561
 
                cache_writelock(mc);
562
 
                we = cache_lookup_distinct(mc, "*");
563
 
                if (we) {
564
 
                        /* Wildcard entry existed and is now gone */
565
 
                        if (we->source == source && (wild & CHE_MISSING)) {
566
 
                                cache_delete(mc, "*");
567
 
                                source->stale = 1;
568
 
                        }
569
 
                } else {
570
 
                        /* Wildcard not in map but now is */
571
 
                        if (wild & (CHE_OK | CHE_UPDATED))
572
 
                                source->stale = 1;
573
 
                }
574
 
                cache_unlock(mc);
575
 
 
576
 
                if (wild & (CHE_OK | CHE_UPDATED))
577
 
                        return NSS_STATUS_SUCCESS;
578
 
        }
579
 
 
580
 
        if (ret == CHE_MISSING)
581
 
                return NSS_STATUS_NOTFOUND;
582
 
 
583
 
        return NSS_STATUS_SUCCESS;
584
 
}
585
 
 
586
 
int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *context)
587
 
{
588
 
        struct lookup_context *ctxt = (struct lookup_context *) context;
589
 
        struct map_source *source;
590
 
        struct mapent_cache *mc;
591
 
        char key[KEY_MAX_LEN + 1];
592
 
        int key_len;
593
 
        char *mapent = NULL;
594
 
        int mapent_len;
595
 
        struct mapent *me;
596
 
        int status = 0;
597
 
        int ret = 1;
598
 
 
599
 
        source = ap->entry->current;
600
 
        ap->entry->current = NULL;
601
 
        master_source_current_signal(ap->entry);
602
 
 
603
 
        mc = source->mc;
604
 
 
605
 
        debug(ap->logopt, MODPREFIX "looking up %s", name);
606
 
 
607
 
        key_len = snprintf(key, KEY_MAX_LEN + 1, "%s", name);
608
 
        if (key_len > KEY_MAX_LEN)
609
 
                return NSS_STATUS_NOTFOUND;
610
 
 
611
 
        /* Check if we recorded a mount fail for this key anywhere */
612
 
        me = lookup_source_mapent(ap, key, LKP_DISTINCT);
613
 
        if (me) {
614
 
                if (me->status >= time(NULL)) {
615
 
                        cache_unlock(me->mc);
616
 
                        return NSS_STATUS_NOTFOUND;
617
 
                } else {
618
 
                        struct mapent_cache *smc = me->mc;
619
 
                        struct mapent *sme;
620
 
 
621
 
                        if (me->mapent)
622
 
                                cache_unlock(smc);
623
 
                        else {
624
 
                                cache_unlock(smc);
625
 
                                cache_writelock(smc);
626
 
                                sme = cache_lookup_distinct(smc, key);
627
 
                                /* Negative timeout expired for non-existent entry. */
628
 
                                if (sme && !sme->mapent)
629
 
                                        cache_delete(smc, key);
630
 
                                cache_unlock(smc);
631
 
                        }
632
 
                }
633
 
        }
634
 
 
635
 
         /*
636
 
          * We can't check the direct mount map as if it's not in
637
 
          * the map cache already we never get a mount lookup, so
638
 
          * we never know about it.
639
 
          */
640
 
        if (ap->type == LKP_INDIRECT && *key != '/') {
641
 
                char *lkp_key;
642
 
 
643
 
                cache_readlock(mc);
644
 
                me = cache_lookup_distinct(mc, key);
645
 
                if (me && me->multi)
646
 
                        lkp_key = strdup(me->multi->key);
647
 
                else
648
 
                        lkp_key = strdup(key);
649
 
                cache_unlock(mc);
650
 
 
651
 
                if (!lkp_key)
652
 
                        return NSS_STATUS_UNKNOWN;
653
 
 
654
 
                master_source_current_wait(ap->entry);
655
 
                ap->entry->current = source;
656
 
 
657
 
                status = check_map_indirect(ap, lkp_key, strlen(lkp_key), ctxt);
658
 
                free(lkp_key);
659
 
                if (status)
660
 
                        return status;
661
 
        }
662
 
 
663
 
        cache_readlock(mc);
664
 
        me = cache_lookup(mc, key);
665
 
        /* Stale mapent => check for entry in alternate source or wildcard */
666
 
        if (me && !me->mapent) {
667
 
                while ((me = cache_lookup_key_next(me)))
668
 
                        if (me->source == source)
669
 
                                break;
670
 
                if (!me)
671
 
                        me = cache_lookup_distinct(mc, "*");
672
 
        }
673
 
        if (me && (me->source == source || *me->key == '/')) {
674
 
                mapent_len = strlen(me->mapent);
675
 
                mapent = alloca(mapent_len + 1);
676
 
                strcpy(mapent, me->mapent);
677
 
        }
678
 
        cache_unlock(mc);
679
 
 
680
 
        if (mapent) {
681
 
                master_source_current_wait(ap->entry);
682
 
                ap->entry->current = source;
683
 
 
684
 
                debug(ap->logopt, MODPREFIX "%s -> %s", key, mapent);
685
 
                ret = ctxt->parse->parse_mount(ap, key, key_len,
686
 
                                               mapent, ctxt->parse->context);
687
 
                if (ret) {
688
 
                        time_t now = time(NULL);
689
 
                        int rv = CHE_OK;
690
 
 
691
 
                        cache_writelock(mc);
692
 
                        me = cache_lookup_distinct(mc, key);
693
 
                        if (!me)
694
 
                                rv = cache_update(mc, source, key, NULL, now);
695
 
                        if (rv != CHE_FAIL) {
696
 
                                me = cache_lookup_distinct(mc, key);
697
 
                                me->status = now + ap->negative_timeout;
698
 
                        }
699
 
                        cache_unlock(mc);
700
 
                }
701
 
         }
702
 
 
703
 
        if (ret)
704
 
                return NSS_STATUS_TRYAGAIN;
705
 
 
706
 
        return NSS_STATUS_SUCCESS;
707
 
}
708
 
 
709
 
int lookup_done(void *context)
710
 
{
711
 
        struct lookup_context *ctxt = (struct lookup_context *) context;
712
 
        int rv = close_parse(ctxt->parse);
713
 
        free(ctxt);
714
 
        return rv;
715
 
}