~ubuntu-branches/debian/wheezy/autofs/wheezy

« back to all changes in this revision

Viewing changes to modules/lookup_multi.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2008-03-08 01:36:09 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080308013609-cvs4f2ecoyoism02
Tags: 4.1.4+debian-2.1
* Non-maintainer upload.
* High-urgency upload for RC bugfix.
* Add -DLDAP_DEPRECATED to CFLAGS, to fix compatibility with OpenLDAP
  2.4 on 64-bit architectures.  Closes: #463419.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ident "$Id: lookup_multi.c,v 1.7 2005/02/10 12:31:29 raven Exp $"
 
2
/* ----------------------------------------------------------------------- *
 
3
 *   
 
4
 *  lookup_multi.c - module for Linux automount to seek multiple lookup
 
5
 *                   methods in succession
 
6
 *
 
7
 *   Copyright 1999 Transmeta Corporation - All Rights Reserved
 
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 <ctype.h>
 
18
#include <errno.h>
 
19
#include <limits.h>
 
20
#include <malloc.h>
 
21
#include <stdio.h>
 
22
#include <string.h>
 
23
#include <syslog.h>
 
24
#include <unistd.h>
 
25
 
 
26
#define MODULE_LOOKUP
 
27
#include "automount.h"
 
28
 
 
29
#define MODPREFIX "lookup(multi): "
 
30
 
 
31
struct module_info {
 
32
        int argc;
 
33
        const char *const *argv;
 
34
        struct lookup_mod *mod;
 
35
};
 
36
 
 
37
struct lookup_context {
 
38
        int n;
 
39
        const char **argl;
 
40
        struct module_info *m;
 
41
};
 
42
 
 
43
int lookup_version = AUTOFS_LOOKUP_VERSION;     /* Required by protocol */
 
44
 
 
45
int lookup_init(const char *my_mapfmt, int argc, const char *const *argv, void **context)
 
46
{
 
47
        struct lookup_context *ctxt;
 
48
        char *map, *mapfmt;
 
49
        int i, j, an;
 
50
 
 
51
        if (!(*context = ctxt = malloc(sizeof(struct lookup_context))))
 
52
                goto nomem;
 
53
 
 
54
        memset(ctxt, 0, sizeof(struct lookup_context));
 
55
 
 
56
        if (argc < 1) {
 
57
                crit(MODPREFIX "No map list");
 
58
                return 1;
 
59
        }
 
60
 
 
61
        ctxt->n = 1;                            /* Always at least one map */
 
62
        for (i = 0; i < argc; i++) {
 
63
                if (!strcmp(argv[i], "--"))     /* -- separates maps */
 
64
                        ctxt->n++;
 
65
        }
 
66
 
 
67
        if (!(ctxt->m = malloc(ctxt->n * sizeof(struct module_info))) ||
 
68
            !(ctxt->argl = malloc((argc + 1) * sizeof(const char *))))
 
69
                goto nomem;
 
70
 
 
71
        memset(ctxt->m, 0, ctxt->n * sizeof(struct module_info));
 
72
 
 
73
        memcpy(ctxt->argl, argv, (argc + 1) * sizeof(const char *));
 
74
 
 
75
        for (i = j = an = 0; ctxt->argl[an]; an++) {
 
76
                if (ctxt->m[i].argc == 0) {
 
77
                        ctxt->m[i].argv = &ctxt->argl[an];
 
78
                }
 
79
                if (!strcmp(ctxt->argl[an], "--")) {
 
80
                        ctxt->argl[an] = NULL;
 
81
                        i++;
 
82
                } else {
 
83
                        ctxt->m[i].argc++;
 
84
                }
 
85
        }
 
86
 
 
87
        for (i = 0; i < ctxt->n; i++) {
 
88
                if (!ctxt->m[i].argv[0]) {
 
89
                        crit(MODPREFIX "missing module name");
 
90
                        return 1;
 
91
                }
 
92
                map = strdup(ctxt->m[i].argv[0]);
 
93
                if (!map)
 
94
                        goto nomem;
 
95
 
 
96
                if ((mapfmt = strchr(map, ',')))
 
97
                        *(mapfmt++) = '\0';
 
98
 
 
99
                if (!(ctxt->m[i].mod = open_lookup(map, MODPREFIX,
 
100
                                                   mapfmt ? mapfmt : my_mapfmt,
 
101
                                                   ctxt->m[i].argc - 1,
 
102
                                                   ctxt->m[i].argv + 1)))
 
103
                        return 1;
 
104
        }
 
105
 
 
106
        *context = ctxt;
 
107
        return 0;
 
108
 
 
109
      nomem:
 
110
        crit(MODPREFIX "malloc: %m");
 
111
        return 1;
 
112
}
 
113
 
 
114
int lookup_ghost(const char *root, int ghost, time_t now, void *context)
 
115
{
 
116
        struct lookup_context *ctxt = (struct lookup_context *) context;
 
117
        int i, ret, at_least_1 = 0;
 
118
 
 
119
        for (i = 0; i < ctxt->n; i++) {
 
120
                ret = ctxt->m[i].mod->lookup_ghost(root, ghost, now,
 
121
                                                   ctxt->m[i].mod->context);
 
122
                if (ret & LKP_FAIL)
 
123
                        continue;
 
124
 
 
125
                at_least_1 = 1; 
 
126
        }
 
127
 
 
128
        if (!at_least_1)
 
129
                return LKP_FAIL;
 
130
 
 
131
        return LKP_INDIRECT;
 
132
}
 
133
 
 
134
int lookup_mount(const char *root, const char *name, int name_len, void *context)
 
135
{
 
136
        struct lookup_context *ctxt = (struct lookup_context *) context;
 
137
        int i;
 
138
 
 
139
        for (i = 0; i < ctxt->n; i++) {
 
140
                if (ctxt->m[i].mod->lookup_mount(root, name, name_len,
 
141
                                                 ctxt->m[i].mod->context) == 0)
 
142
                        return 0;
 
143
        }
 
144
        return 1;               /* No module succeeded */
 
145
}
 
146
 
 
147
int lookup_done(void *context)
 
148
{
 
149
        struct lookup_context *ctxt = (struct lookup_context *) context;
 
150
        int i, rv = 0;
 
151
 
 
152
        for (i = 0; i < ctxt->n; i++) {
 
153
                rv = rv || close_lookup(ctxt->m[i].mod);
 
154
        }
 
155
 
 
156
        free(ctxt->argl);
 
157
        free(ctxt->m);
 
158
        free(ctxt);
 
159
 
 
160
        return rv;
 
161
}