2 Copyright (c) 2008,2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
26 #define LDAP_DEPRECATED 1
29 #include <atalk/logger.h>
30 #include <atalk/afp.h>
31 #include <atalk/uuid.h>
32 #include <atalk/ldapconfig.h> /* For struct ldap_pref */
38 /********************************************************
39 * LDAP config stuff. Filled by libatalk/acl/ldap_config.c
40 ********************************************************/
41 int ldap_config_valid;
52 char *ldap_uuid_string;
54 char *ldap_group_attr;
57 struct ldap_pref ldap_prefs[] = {
58 {&ldap_server, "ldap server", 0, 0, -1},
59 {&ldap_auth_method,"ldap auth method", 1, 1, -1},
60 {&ldap_auth_dn, "ldap auth dn", 0, 0, 0},
61 {&ldap_auth_pw, "ldap auth pw", 0, 0, 0},
62 {&ldap_userbase, "ldap userbase", 0, 0, -1},
63 {&ldap_userscope, "ldap userscope", 1 ,1, -1},
64 {&ldap_groupbase, "ldap groupbase", 0, 0, -1},
65 {&ldap_groupscope, "ldap groupscope", 1 ,1, -1},
66 {&ldap_uuid_attr, "ldap uuid attr", 0, 0, -1},
67 {&ldap_uuid_string,"ldap uuid string", 0, 0, 0},
68 {&ldap_name_attr, "ldap name attr", 0, 0, -1},
69 {&ldap_group_attr, "ldap group attr", 0, 0, -1},
70 {&ldap_uid_attr, "ldap uid attr", 0, 0, 0},
71 {NULL, NULL, 0, 0, -1}
74 struct pref_array prefs_array[] = {
75 {"ldap auth method", "none", LDAP_AUTH_NONE},
76 {"ldap auth method", "simple", LDAP_AUTH_SIMPLE},
77 {"ldap auth method", "sasl", LDAP_AUTH_SASL},
78 {"ldap userscope", "base", LDAP_SCOPE_BASE},
79 {"ldap userscope", "one", LDAP_SCOPE_ONELEVEL},
80 {"ldap userscope", "sub", LDAP_SCOPE_SUBTREE},
81 {"ldap groupscope", "base", LDAP_SCOPE_BASE},
82 {"ldap groupscope", "one", LDAP_SCOPE_ONELEVEL},
83 {"ldap groupscope", "sub", LDAP_SCOPE_SUBTREE},
87 /********************************************************
88 * Static helper function
89 ********************************************************/
92 * ldap_getattr_fromfilter_withbase_scope():
94 * scope: LDAP_SCOPE_BASE, LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
95 * result: return unique search result here, allocated here, caller must free
97 * returns: -1 on error
99 * 1 successfull search, result int 'result'
101 * All connection managment to the LDAP server is done here. Just set KEEPALIVE if you know
102 * you will be dispatching more than one search in a row, then don't set it with the last search.
103 * You MUST dispatch the queries timely, otherwise the LDAP handle might timeout.
105 static int ldap_getattr_fromfilter_withbase_scope( const char *searchbase,
114 int desired_version = LDAP_VERSION3;
115 static int ldapconnected = 0;
116 static LDAP *ld = NULL;
117 LDAPMessage* msg = NULL;
118 LDAPMessage* entry = NULL;
119 char **attribute_values = NULL;
120 struct timeval timeout;
122 LOG(log_maxdebug, logtype_afpd,"ldap: BEGIN");
127 /* init LDAP if necessary */
132 LOG(log_maxdebug, logtype_default, "ldap: server: \"%s\"",
134 if ((ld = ldap_init(ldap_server, LDAP_PORT)) == NULL ) {
135 LOG(log_error, logtype_default, "ldap: ldap_init error: %s",
139 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version) != 0) {
140 /* LDAP_OPT_SUCCESS is not in the proposed standard, so we check for 0
141 http://tools.ietf.org/id/draft-ietf-ldapext-ldap-c-api-05.txt */
142 LOG(log_error, logtype_default, "ldap: ldap_set_option failed!");
150 if (!ldapconnected) {
151 if (LDAP_AUTH_NONE == ldap_auth_method) {
152 if (ldap_bind_s(ld, "", "", LDAP_AUTH_SIMPLE) != LDAP_SUCCESS ) {
153 LOG(log_error, logtype_default, "ldap: ldap_bind failed, auth_method: \'%d\'",
161 } else if (LDAP_AUTH_SIMPLE == ldap_auth_method) {
162 if (ldap_bind_s(ld, ldap_auth_dn, ldap_auth_pw, ldap_auth_method) != LDAP_SUCCESS ) {
163 LOG(log_error, logtype_default,
164 "ldap: ldap_bind failed: ldap_auth_dn: \'%s\', ldap_auth_pw: \'%s\', ldap_auth_method: \'%d\'",
165 ldap_auth_dn, ldap_auth_pw, ldap_auth_method);
174 LOG(log_maxdebug, logtype_afpd, "ldap: start search: base: %s, filter: %s, attr: %s",
175 searchbase, filter, attributes[0]);
177 /* start LDAP search */
178 ldaperr = ldap_search_st(ld, searchbase, scope, filter, attributes, 0, &timeout, &msg);
179 LOG(log_maxdebug, logtype_default, "ldap: ldap_search_st returned: %s",
180 ldap_err2string(ldaperr));
181 if (ldaperr != LDAP_SUCCESS) {
182 LOG(log_error, logtype_default, "ldap: ldap_search_st failed: %s, retrycount: %i",
183 ldap_err2string(ldaperr), retrycount);
188 /* parse search result */
189 LOG(log_maxdebug, logtype_default, "ldap: got %d entries from ldap search",
190 ldap_count_entries(ld, msg));
191 if ((ret = ldap_count_entries(ld, msg)) != 1) {
196 entry = ldap_first_entry(ld, msg);
198 LOG(log_error, logtype_default, "ldap: ldap_first_entry error");
202 attribute_values = ldap_get_values(ld, entry, attributes[0]);
203 if (attribute_values == NULL) {
204 LOG(log_error, logtype_default, "ldap: ldap_get_values error");
209 LOG(log_maxdebug, logtype_afpd,"ldap: search result: %s: %s",
210 attributes[0], attribute_values[0]);
212 /* allocate result */
213 *result = strdup(attribute_values[0]);
214 if (*result == NULL) {
215 LOG(log_error, logtype_default, "ldap: strdup error: %s",strerror(errno));
221 if (attribute_values)
222 ldap_value_free(attribute_values);
223 /* FIXME: is there another way to free entry ? */
224 while (entry != NULL)
225 entry = ldap_next_entry(ld, entry);
230 if ((ret == -1) || !(conflags & KEEPALIVE)) {
231 LOG(log_maxdebug, logtype_default,"ldap: unbind");
232 if (ldap_unbind_s(ld) != 0) {
233 LOG(log_error, logtype_default, "ldap: unbind: %s\n", ldap_err2string(ldaperr));
239 /* In case of error we try twice */
250 /********************************************************
252 ********************************************************/
255 * Search UUID for name in LDAP
257 * Caller must free uuid_string when done with it
259 * @param name (r) name to search
260 * @param type (r) type of USER or GROUP
261 * @param uuid_string (w) result as pointer to allocated UUID-string
263 * @returns 0 on success, -1 on error or not found
265 int ldap_getuuidfromname( const char *name, uuidtype_t type, char **uuid_string) {
268 char filter[256]; /* this should really be enough. we dont want to malloc everything! */
269 char *attributes[] = { ldap_uuid_attr, NULL};
272 if (!ldap_config_valid)
276 if (type == UUID_GROUP)
277 ldap_attr = ldap_group_attr;
278 else /* type hopefully == UUID_USER */
279 ldap_attr = ldap_name_attr;
280 len = snprintf( filter, 256, "%s=%s", ldap_attr, name);
281 if (len >= 256 || len == -1) {
282 LOG(log_error, logtype_default, "ldap_getnamefromuuid: filter error:%d, \"%s\"", len, filter);
286 if (type == UUID_GROUP) {
287 ret = ldap_getattr_fromfilter_withbase_scope( ldap_groupbase, filter, attributes, ldap_groupscope, KEEPALIVE, uuid_string);
288 } else { /* type hopefully == UUID_USER */
289 ret = ldap_getattr_fromfilter_withbase_scope( ldap_userbase, filter, attributes, ldap_userscope, KEEPALIVE, uuid_string);
297 * LDAP search wrapper
298 * returns allocated storage in name, caller must free it
299 * returns 0 on success, -1 on error or not found
301 * @param uuidstr (r) uuid to search as ascii string
302 * @param name (w) return pointer to name as allocated string
303 * @param type (w) return type: USER or GROUP
305 * returns 0 on success, -1 on errror
307 int ldap_getnamefromuuid( const char *uuidstr, char **name, uuidtype_t *type) {
310 char filter[256]; /* this should really be enough. we dont want to malloc everything! */
311 char *attributes[] = { NULL, NULL};
313 if (!ldap_config_valid)
317 len = snprintf( filter, 256, "%s=%s", ldap_uuid_attr, uuidstr);
318 if (len >= 256 || len == -1) {
319 LOG(log_error, logtype_default, "ldap_getnamefromuuid: filter overflow:%d, \"%s\"", len, filter);
322 /* search groups first. group acls are probably used more often */
323 attributes[0] = ldap_group_attr;
324 ret = ldap_getattr_fromfilter_withbase_scope( ldap_groupbase, filter, attributes, ldap_groupscope, KEEPALIVE, name);
332 attributes[0] = ldap_name_attr;
333 ret = ldap_getattr_fromfilter_withbase_scope( ldap_userbase, filter, attributes, ldap_userscope, KEEPALIVE, name);
341 #endif /* HAVE_LDAP */