]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/ldap.c
New LDAP search options for scope
[netatalk.git] / libatalk / acl / ldap.c
1 /*
2   $Id: ldap.c,v 1.3 2009-11-28 10:03:01 franklahm Exp $
3   Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif /* HAVE_CONFIG_H */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <ldap.h>
26
27 #include <atalk/logger.h>
28 #include <atalk/afp.h>
29 #include <atalk/uuid.h>
30 #include <atalk/ldapconfig.h>   /* For struct ldap_pref */
31
32 typedef enum {
33     KEEPALIVE = 1
34 } ldapcon_t;
35
36 /********************************************************
37  * LDAP config stuff. Filled by etc/afpd/acl_config.c
38  ********************************************************/
39 int ldap_config_valid;
40
41 char *ldap_server;
42 int  ldap_auth_method;
43 char *ldap_auth_dn;
44 char *ldap_auth_pw;
45 char *ldap_userbase;
46 int  ldap_userscope;
47 char *ldap_groupbase;
48 int  ldap_groupscope;
49 char *ldap_uuid_attr;
50 char *ldap_name_attr;
51 char *ldap_group_attr;
52 char *ldap_uid_attr;
53
54 struct ldap_pref ldap_prefs[] = {
55     {&ldap_server,     "ldap_server",      0, 0, -1},
56     {&ldap_auth_method,"ldap_auth_method", 1, 1, -1},
57     {&ldap_auth_dn,    "ldap_auth_dn",     0, 0,  0},
58     {&ldap_auth_pw,    "ldap_auth_pw",     0, 0,  0},
59     {&ldap_userbase,   "ldap_userbase",    0, 0, -1},
60     {&ldap_userscope}, "ldap_userscope",   1 ,1, -1},
61     {&ldap_groupbase,  "ldap_groupbase",   0, 0, -1},
62     {&ldap_groupscope},"ldap_groupscope",  1 ,1, -1},
63     {&ldap_uuid_attr,  "ldap_uuid_attr",   0, 0, -1},
64     {&ldap_name_attr,  "ldap_name_attr",   0, 0, -1},
65     {&ldap_group_attr, "ldap_group_attr",  0, 0, -1},
66     {&ldap_uid_attr,   "ldap_uid_attr",    0, 0,  0},
67     {NULL,             NULL,               0, 0, -1}
68 };
69
70 struct pref_array prefs_array[] = {
71     {"ldap_auth_method", "none",   LDAP_AUTH_NONE},
72     {"ldap_auth_method", "simple", LDAP_AUTH_SIMPLE},
73     {"ldap_auth_method", "sasl",   LDAP_AUTH_SASL},
74     {"ldap_userscope",   "base",   LDAP_SCOPE_BASE},
75     {"ldap_userscope",   "one",    LDAP_SCOPE_ONELEVEL},
76     {"ldap_userscope",   "sub",    LDAP_SCOPE_SUBTREE},
77     {NULL,               NULL,     0}
78 };
79
80 /********************************************************
81  * Static helper function
82  ********************************************************/
83
84 /*
85  * ldap_getattr_fromfilter_withbase_scope():
86  *   conflags: KEEPALIVE
87  *   scope: LDAP_SCOPE_BASE, LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
88  *   result: return unique search result here, allocated here, caller must free
89  *
90  * All connection managment to the LDAP server is done here. Just set KEEPALIVE if you know
91  * you will be dispatching more than one search in a row, then don't set it with the last search.
92  * You MUST dispatch the queries timely, otherwise the LDAP handle might timeout.
93  */
94 static int ldap_getattr_fromfilter_withbase_scope( const char *searchbase,
95                                                    const char *filter,
96                                                    char *attributes[],
97                                                    int scope,
98                                                    ldapcon_t conflags,
99                                                    char **result) {
100     int ret = 0;
101     int ldaperr;
102     int desired_version  = LDAP_VERSION3;
103     static int ldapconnected = 0;
104     static LDAP *ld     = NULL;
105     LDAPMessage* msg    = NULL;
106     LDAPMessage* entry  = NULL;
107
108     char **attribute_values;
109     struct timeval timeout;
110
111 //    LOG(log_debug, logtype_afpd,"ldap_getattr_fromfilter_withbase_scope: BEGIN");
112
113     timeout.tv_sec = 3;
114     timeout.tv_usec = 0;
115
116     /* init LDAP if necessary */
117     if (!ldapconnected) {
118 //  LOG(log_debug, logtype_default, "ldap_getattr_fromfilter_withbase_scope: LDAP server: \'%s\'", ldap_server);
119         if ((ld = ldap_init(ldap_server, LDAP_PORT)) == NULL ) {
120             LOG(log_error, logtype_default, "ldap_getattr_fromfilter_withbase_scope: ldap_init error");
121             return -1;
122         }
123         if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version) != 0) {
124             /* LDAP_OPT_SUCCESS is not in the proposed standard, so we check for 0
125                http://tools.ietf.org/id/draft-ietf-ldapext-ldap-c-api-05.txt */
126             LOG(log_error, logtype_default, "ldap_getattr_fromfilter_withbase_scope: ldap_set_option failed!");
127             ret = -1;
128             goto cleanup;
129         }
130     }
131
132     /* connect */
133     if (!ldapconnected) {
134         if (LDAP_AUTH_NONE == ldap_auth_method) {
135             if (ldap_bind_s(ld, "", "", LDAP_AUTH_SIMPLE) != LDAP_SUCCESS ) {
136                 LOG(log_error, logtype_default, "ldap_getattr_fromfilter_withbase_scope: ldap_bind failed!");
137                 LOG(log_error, logtype_default, "ldap_auth_method: \'%d\'", ldap_auth_method);
138                 return -1;
139             }
140             ldapconnected = 1;
141
142         } else if (LDAP_AUTH_SIMPLE == ldap_auth_method) {
143             if (ldap_bind_s(ld, ldap_auth_dn, ldap_auth_pw, ldap_auth_method) != LDAP_SUCCESS ) {
144                 LOG(log_error, logtype_default, "ldap_getattr_fromfilter_withbase_scope: ldap_bind failed!");
145                 LOG(log_error, logtype_default, "ldap_auth_dn: \'%s\', ldap_auth_pw: \'%s\', ldap_auth_method: \'%d\'",
146                     ldap_auth_dn, ldap_auth_pw, ldap_auth_method);
147                 return -1;
148             }
149             ldapconnected = 1;
150         }
151     }
152
153 //    LOG(log_debug, logtype_afpd,"LDAP start search: base: %s, filter: %s, attr: %s", searchbase, filter, attributes[0]);
154
155     /* start LDAP search */
156     ldaperr = ldap_search_st(ld, searchbase, scope, filter, attributes, 0, &timeout, &msg);
157     if (ldaperr != LDAP_SUCCESS) {
158         LOG(log_error, logtype_default, "ldap_getattr_fromfilter_withbase_scope: ldap_search_st failed: %s", ldap_err2string(ldaperr));
159         ret = -1;
160         goto cleanup;
161     }
162
163     /* parse search result */
164 //    LOG(log_debug, logtype_default, "ldap_getuuidfromname: got %d entries from ldap search", ldap_count_entries(ld, msg));
165     if (ldap_count_entries(ld, msg) != 1) {
166         ret = -1;
167         goto cleanup;
168     }
169     entry = ldap_first_entry(ld, msg);
170     if (entry == NULL) {
171         LOG(log_error, logtype_default, "ldap_getattr_fromfilter_withbase_scope: error in ldap_first_entry");
172         ret = -1;
173         goto cleanup;
174     }
175     attribute_values = ldap_get_values(ld, entry, attributes[0]);
176     if (attribute_values == NULL) {
177         LOG(log_error, logtype_default, "ldap_getattr_fromfilter_withbase_scope: error in ldap_get_values");
178         ret = -1;
179         goto cleanup;
180     }
181
182 //    LOG(log_debug, logtype_afpd,"LDAP Search result: %s: %s", attributes[0], attribute_values[0]);
183
184     /* allocate place for uuid as string */
185     *result = calloc( 1, strlen(attribute_values[0]) + 1);
186     if (*result == NULL) {
187         LOG(log_error, logtype_default, "ldap_getattr_fromfilter_withbase_scope: %s: error calloc'ing",strerror(errno));
188         ret = -1;
189         goto cleanup;
190     }
191     /* get value */
192     strcpy( *result, attribute_values[0]);
193     ldap_value_free(attribute_values);
194
195     /* FIXME: is there another way to free entry ? */
196     while (entry != NULL)
197         entry = ldap_next_entry(ld, entry);
198
199 cleanup:
200     if(msg)
201         ldap_msgfree(msg);
202     if(ld) {
203         if ((ldapconnected && !(conflags & KEEPALIVE)) || (*result != NULL)) {
204             ldapconnected = 0;  /* regardless of unbind errors */
205 //      LOG(log_debug, logtype_default,"LDAP unbind!");
206             if (ldap_unbind_s(ld) != 0) {
207                 LOG(log_error, logtype_default, "ldap_unbind_s: %s\n", ldap_err2string(ldaperr));
208                 return -1;
209             }
210         }
211     }
212     return ret;
213 }
214
215 /********************************************************
216  * Interface
217  ********************************************************/
218
219 int ldap_getuuidfromname( const char *name, uuidtype_t type, char **uuid_string) {
220     int ret;
221     int len;
222     char filter[256];           /* this should really be enough. we dont want to malloc everything! */
223     char *attributes[]  = { ldap_uuid_attr, NULL};
224     char *ldap_attr;
225
226     /* make filter */
227     if (type == UUID_GROUP)
228         ldap_attr = ldap_group_attr;
229     else /* type hopefully == UUID_USER */
230         ldap_attr = ldap_name_attr;
231     len = snprintf( filter, 256, "%s=%s", ldap_attr, name);
232     if (len >= 256 || len == -1) {
233         LOG(log_error, logtype_default, "ldap_getnamefromuuid: filter error:%d, \"%s\"", len, filter);
234         return -1;
235     }
236
237     if (type == UUID_GROUP) {
238         ret = ldap_getattr_fromfilter_withbase_scope( ldap_groupbase, filter, attributes, ldap_userscope, KEEPALIVE, uuid_string);
239     } else  { /* type hopefully == UUID_USER */
240         ret = ldap_getattr_fromfilter_withbase_scope( ldap_userbase, filter, attributes, ldap_groupscope, 0, uuid_string);
241     }
242     return ret;
243 }
244
245 int ldap_getnamefromuuid( char *uuidstr, char **name, uuidtype_t *type) {
246     int ret;
247     int len;
248     char filter[256];       /* this should really be enough. we dont want to malloc everything! */
249     char *attributes[]  = { NULL, NULL};
250
251     /* make filter */
252     len = snprintf( filter, 256, "%s=%s", ldap_uuid_attr, uuidstr);
253     if (len >= 256 || len == -1) {
254         LOG(log_error, logtype_default, "ldap_getnamefromuuid: filter overflow:%d, \"%s\"", len, filter);
255         return -1;
256     }
257     /* search groups first. group acls are probably used more often */
258     attributes[0] = ldap_group_attr;
259     ret = ldap_getattr_fromfilter_withbase_scope( ldap_groupbase, filter, attributes, ldap_groupscope, KEEPALIVE, name);
260     if (ret == 0) {
261         *type = UUID_GROUP;
262         return 0;
263     }
264     attributes[0] = ldap_name_attr;
265     ret = ldap_getattr_fromfilter_withbase_scope( ldap_userbase, filter, attributes, ldap_userscope, 0, name);
266     if (ret == 0) {
267         *type = UUID_USER;
268         return 0;
269     }
270
271     return ret;
272 }
273