99 authdb->authdb.name = pool_strdup(cfg->pool, name); |
99 authdb->authdb.name = pool_strdup(cfg->pool, name); |
100 if(!authdb->authdb.name) { |
100 if(!authdb->authdb.name) { |
101 return NULL; |
101 return NULL; |
102 } |
102 } |
103 authdb->authdb.get_user = ldap_get_user; |
103 authdb->authdb.get_user = ldap_get_user; |
104 authdb->authdb.use_cache = 1; |
104 authdb->authdb.use_cache = 0; // TODO: enable caching when cache actually works |
105 |
105 |
106 // initialize default ldap config |
106 // initialize default ldap config |
107 cxstring dirtype = serverconfig_object_directive_value(node, cx_str("DirectoryType")); |
107 cxstring dirtype = serverconfig_object_directive_value(node, cx_str("DirectoryType")); |
108 LDAPConfig *default_config; |
108 LDAPConfig *default_config; |
109 if(!dirtype.ptr) { |
109 if(!dirtype.ptr) { |
131 // TODO: create resource pool |
131 // TODO: create resource pool |
132 } else { |
132 } else { |
133 authdb->config.resource = resource.ptr; |
133 authdb->config.resource = resource.ptr; |
134 } |
134 } |
135 |
135 |
|
136 if(!basedn.ptr) { |
|
137 log_ereport(LOG_FAILURE, "ldap authdb %s: basedn is required", name); |
|
138 return NULL; |
|
139 } |
|
140 authdb->config.basedn = basedn.ptr; |
|
141 |
|
142 |
136 // initialize group cache |
143 // initialize group cache |
137 authdb->groups.first = NULL; |
144 authdb->groups.first = NULL; |
138 authdb->groups.last = NULL; |
145 authdb->groups.last = NULL; |
139 authdb->groups.map = cxHashMapCreate(cfg->a, 32); |
146 authdb->groups.map = cxHashMapCreate(cfg->a, 32); |
140 if(!authdb->groups.map) { |
147 if(!authdb->groups.map) { |
141 return NULL; |
148 return NULL; |
142 } |
149 } |
|
150 |
|
151 log_ereport(LOG_INFORM, "create authdb name=%s type=ldap resource=%s", name, resource.ptr); |
143 |
152 |
144 return (AuthDB*) authdb; |
153 return (AuthDB*) authdb; |
145 } |
154 } |
146 |
155 |
147 LDAP* get_ldap_session(Session *sn, Request *rq, LDAPAuthDB *authdb) { |
156 LDAP* get_ldap_session(Session *sn, Request *rq, LDAPAuthDB *authdb) { |
178 |
187 |
179 // get the user dn |
188 // get the user dn |
180 // TODO: use config for filter |
189 // TODO: use config for filter |
181 // TODO: use asprintf |
190 // TODO: use asprintf |
182 char filter[128]; |
191 char filter[128]; |
183 int s = snprintf(filter, 127, "uid=%s", username); |
192 snprintf(filter, 128, "(uid=%s)", username); |
184 filter[s] = 0; |
|
185 |
193 |
186 LDAPMessage *result; |
194 LDAPMessage *result; |
187 struct timeval timeout; |
195 struct timeval timeout; |
188 timeout.tv_sec = 8; |
196 timeout.tv_sec = 8; |
189 timeout.tv_usec = 0; |
197 timeout.tv_usec = 0; |
198 NULL, // client controls |
206 NULL, // client controls |
199 &timeout, |
207 &timeout, |
200 1, // size limit |
208 1, // size limit |
201 &result); |
209 &result); |
202 if (r != LDAP_SUCCESS) { |
210 if (r != LDAP_SUCCESS) { |
203 ws_ldap_close(ld); |
211 //ws_ldap_close(ld); |
204 |
212 |
205 fprintf(stderr, "ldap_search_ext_s failed\n"); |
213 log_ereport(LOG_FAILURE, "ldap_get_user: search failed: %s", ldap_err2string(r)); |
206 return NULL; |
214 return NULL; |
207 } |
215 } |
208 |
216 |
209 LDAPMessage *msg = ldap_first_entry(ld, result); |
217 LDAPMessage *msg = ldap_first_entry(ld, result); |
210 if (msg) { |
218 if (msg) { |
372 |
380 |
373 void ldap_user_free(User *u) { |
381 void ldap_user_free(User *u) { |
374 LDAPUser *user = (LDAPUser*)u; |
382 LDAPUser *user = (LDAPUser*)u; |
375 ldap_memfree(user->userdn); |
383 ldap_memfree(user->userdn); |
376 // TODO: use connection pool |
384 // TODO: use connection pool |
377 ws_ldap_close(user->ldap); |
385 //ws_ldap_close(user->ldap); |
378 free(user); |
386 free(user); |
379 } |
387 } |