aboutsummaryrefslogtreecommitdiffstats
path: root/modules/lookup_ldap.c
blob: ab068a92f9b56b38a3890ff76df7dc2d17ca7950 (plain)
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
/*
 * lookup_ldap.c
 *
 * Module for Linux automountd to access automount maps in LDAP directories.
 *
 */

#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <lber.h>
#include <ldap.h>

#define MODULE_LOOKUP
#include "automount.h"

#define MAPFMT_DEFAULT "sun"

#define MODPREFIX "lookup(ldap): "

#define OBJECTCLASS "automount"
#define ATTRIBUTE "automountInformation"
#define WILDCARD "/"

struct lookup_context {
  char *server, *base;
  struct parse_mod *parser;
};

int lookup_version = AUTOFS_LOOKUP_VERSION; /* Required by protocol */

/*
 * This initializes a context (persistent non-global data) for queries to
 * this module.  Return zero if we succeed.
 */
int lookup_init(const char *mapfmt, int argc, const char * const *argv,
    void **context)
{
  struct lookup_context *ctxt = NULL;
  int rv, l;
  LDAP *ldap;

  /* If we can't build a context, bail. */
  ctxt = (struct lookup_context*) malloc(sizeof(struct lookup_context));
  *context = ctxt;
  if( ctxt == NULL ) {
    syslog(LOG_INFO, MODPREFIX "malloc: %m");
    return 1;
  }
  memset(ctxt, 0, sizeof(struct lookup_context));

  /* If a map type isn't explicitly given, parse it like sun entries. */
  if( mapfmt == NULL ) {
    mapfmt = MAPFMT_DEFAULT;
  }

  /* Now we sanity-check by binding to the server temporarily.  We have to be
   * a little strange in here, because we want to provide for use of the
   * "default" server, which is set in an ldap.conf file somewhere. */
  if(strchr(argv[0], ':') != NULL) {
    l = strchr(argv[0], ':') - argv[0];
    /* Isolate the server's name. */
    ctxt->server = malloc(l + 1);
    memset(ctxt->server, 0, l + 1);
    memcpy(ctxt->server, argv[0], l);
    /* Isolate the base DN. */
    ctxt->base = malloc(strlen(argv[0]) - l);
    memset(ctxt->base, 0, strlen(argv[0]) - l);
    memcpy(ctxt->base, argv[0] + l + 1, strlen(argv[0]) - l - 1);
  } else {
    /* Use the default server;  isolate the base DN's name. */
    l = strlen(argv[0]);
    ctxt->server = NULL;
    ctxt->base = malloc(l + 1);
    memset(ctxt->base, 0, l + 1);
    memcpy(ctxt->base, argv[0], l);
  }

  syslog(LOG_DEBUG, MODPREFIX "server = \"%s\", base dn = \"%s\"",
         ctxt->server ? ctxt->server : "(default)", ctxt->base);

  /* Initialize the LDAP context. */
  if( ( ldap = ldap_init(ctxt->server, LDAP_PORT)) == NULL ) {
    syslog(LOG_CRIT, MODPREFIX "couldn't initialize LDAP");
    return 1;
  }

  /* Connect to the server as an anonymous user. */
  rv = ldap_simple_bind_s(ldap, ctxt->base, NULL);
  if( rv != LDAP_SUCCESS ) {
    syslog(LOG_CRIT, MODPREFIX "couldn't connect to %s", ctxt->server);
    return 1;
  }

  /* Okay, we're done here. */
  ldap_unbind(ldap);

  /* Open the parser, if we can. */
  return !(ctxt->parser = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1));
}

/* Lookup by key and pass a filesystem to a parser. */
int lookup_mount(const char *root, const char *name, int name_len, void *context)
{
  struct lookup_context *ctxt = (struct lookup_context *) context;
  int rv, i, l;
  char *query;
  LDAPMessage *result, *e;
  char **values;
  char *attrs[] = {ATTRIBUTE, NULL};
  LDAP *ldap;

  chdir("/");  /* If this is not here the filesystem stays
         busy, for some reason... */

  if( ctxt == NULL ) {
    syslog(LOG_CRIT, MODPREFIX "context was NULL");
    return 0;
  }

  /* Build a query string. */
  l = name_len + strlen("(&(objectclass=" OBJECTCLASS ")(cn=))") + 2;

  query = malloc(l);
  if( query == NULL ) {
    syslog(LOG_INFO, MODPREFIX "malloc: %m");
    return 0;
  }

  memset(query, '\0', l);
  if( sprintf(query, "(&(objectclass=" OBJECTCLASS ")(cn=%s))", name) >= l ) {
    syslog(LOG_DEBUG, MODPREFIX "error forming query string");
  }
  query[l - 1] = '\0';

  /* Initialize the LDAP context. */
  if( (ldap = ldap_init(ctxt->server, LDAP_PORT) ) == NULL ) {
    syslog(LOG_CRIT, MODPREFIX "couldn't initialize LDAP connection"
           " to %s", ctxt->server ? ctxt->server : "default server");
    free(query);
    return 1;
  }

  /* Connect to the server as an anonymous user. */
  rv = ldap_simple_bind_s(ldap, ctxt->base, NULL);
  if ( rv != LDAP_SUCCESS ) {
    syslog(LOG_CRIT, MODPREFIX "couldn't bind to %s",
           ctxt->server ? ctxt->server : "default server");
    free(query);
    return 1;
  }

  /* Look around. */
  syslog(LOG_DEBUG, MODPREFIX "searching for \"%s\"", query);
  rv = ldap_search_s(ldap, ctxt->base, LDAP_SCOPE_SUBTREE,
                     query, attrs, 0, &result);

  if( ( rv != LDAP_SUCCESS) || ( result == NULL ) ) {
    syslog(LOG_INFO, MODPREFIX "query failed for %s", query);
    free(query);
    return 1;
  }

  e = ldap_first_entry(ldap, result);

  /* If we got no answers, try it with "/" instead, which makes a better
   * wildcard thatn "*" for LDAP, and also happens to be illegal for actual
   * directory names. */
  if( e == NULL ) {
    syslog(LOG_DEBUG, MODPREFIX "no entry for \"%s\" found, trying cn=\"/\"",
           name);

    sprintf(query, "(&(objectclass=" OBJECTCLASS ")(cn=" WILDCARD "))");

    syslog(LOG_DEBUG, MODPREFIX "searching for \"%s\"", query);
    rv = ldap_search_s(ldap, ctxt->base, LDAP_SCOPE_SUBTREE,
                       query, attrs, 0, &result);
    if( ( rv != LDAP_SUCCESS) || ( result == NULL ) ) {
      syslog(LOG_INFO, MODPREFIX "query failed for %s", query);
      free(query);
      return 1;
    }

    syslog(LOG_DEBUG, MODPREFIX "getting first entry for cn=\"/\"");

    e = ldap_first_entry(ldap, result);
  }

  if( e == NULL ) {
    syslog(LOG_INFO, MODPREFIX "got answer, but no first entry for %s", query);
    free(query);
    return 1;
  } else {
    syslog(LOG_DEBUG, MODPREFIX "examining first entry");
  }

  values = ldap_get_values(ldap, e, ATTRIBUTE);
  if( values == NULL ) {
    syslog(LOG_INFO, MODPREFIX "no " ATTRIBUTE " defined for %s", query);
    free(query);
    return 1;
  }

  /* Try each of the answers in sucession. */
  rv = 1;
  for( i = 0 ; ( values[i] != NULL ) && ( rv != 0 ) ; i++ ) {
    rv = ctxt->parser->parse_mount(root, name, name_len, values[0],
                 ctxt->parser->context);
  }

  /* Clean up. */
  ldap_value_free(values);
  ldap_msgfree(result);
  ldap_unbind(ldap);
  free(query);

  return rv;
}

/*
 * This destroys a context for queries to this module.  It releases the parser
 * structure (unloading the module) and frees the memory used by the context.
 */
int lookup_done(void *context)
{
  struct lookup_context *ctxt = (struct lookup_context *) context;
  int rv = close_parse(ctxt->parser);
  free(ctxt->server);
  free(ctxt->base);
  free(ctxt);
  return rv;
}