55 { |
55 { |
56 LDAP *ld = NULL; |
56 LDAP *ld = NULL; |
57 |
57 |
58 #ifdef SOLARIS |
58 #ifdef SOLARIS |
59 ld = ldap_init(config->hostname, config->port); |
59 ld = ldap_init(config->hostname, config->port); |
60 if(ld) { |
|
61 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version); |
|
62 } else { |
|
63 log_ereport( |
|
64 LOG_FAILURE, |
|
65 "ldap_resource_create_connection failed: host: %s port: %d", |
|
66 hostname, |
|
67 port); |
|
68 } |
|
69 #else |
60 #else |
70 char *ldap_uri = NULL; |
61 char *ldap_uri = NULL; |
71 asprintf(&ldap_uri, "ldap://%s:%d", hostname, port); |
62 asprintf(&ldap_uri, "ldap://%s:%d", hostname, port); |
72 ld = ws_ldap_resource_create_uri_connection(ldap_uri, ldap_version); |
63 ld = ws_ldap_resource_create_uri_connection(ldap_uri, ldap_version); |
73 free(ldap_uri); |
64 free(ldap_uri); |
74 #endif |
65 #endif |
75 if(!ld) { |
66 |
76 return NULL; |
67 if(ld) { |
77 } |
68 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version); |
78 |
69 } |
79 return NULL; |
70 |
|
71 return ld; |
80 } |
72 } |
81 |
73 |
82 LDAP* ws_ldap_resource_create_uri_connection( |
74 LDAP* ws_ldap_resource_create_uri_connection( |
83 const char *uri, |
75 const char *uri, |
84 int ldap_version) |
76 int ldap_version) |
189 } |
181 } |
190 |
182 |
191 if(!ldap) { |
183 if(!ldap) { |
192 log_ereport( |
184 log_ereport( |
193 LOG_FAILURE, |
185 LOG_FAILURE, |
194 "Resource pool %s: %s: cannot create LDAP session", |
186 "resource pool %s: %s: cannot create LDAP session", |
195 respool->name, |
187 respool->name, |
196 respool->ldap_uri ? respool->ldap_uri : respool->host); |
188 respool->ldap_uri ? respool->ldap_uri : respool->host); |
197 return NULL; |
189 return NULL; |
198 } |
190 } |
199 |
191 |
200 if(respool->bind) { |
192 if(respool->bind) { |
201 struct berval *server_cred; |
193 struct berval *server_cred; |
202 if(ldap_resource_bind(respool, ldap, &server_cred) != LDAP_SUCCESS) { |
194 int r = ldap_resource_bind(respool, ldap, &server_cred); |
203 log_ereport(LOG_FAILURE, "Resource pool %s: bind failed", respool->name); |
195 if(r != LDAP_SUCCESS) { |
|
196 log_ereport(LOG_FAILURE, "resource pool %s: bind failed: %s", respool->name, ldap_err2string(r)); |
204 ws_ldap_close(ldap); |
197 ws_ldap_close(ldap); |
205 return NULL; |
198 return NULL; |
206 } |
199 } |
207 } |
200 } |
208 |
201 |
242 |
235 |
243 int ldap_resource_bind(LDAPResourcePool *respool, LDAP *ldap, struct berval **server_cred) { |
236 int ldap_resource_bind(LDAPResourcePool *respool, LDAP *ldap, struct berval **server_cred) { |
244 if(!respool->binddn) { |
237 if(!respool->binddn) { |
245 return -1; |
238 return -1; |
246 } |
239 } |
247 |
240 return ws_ldap_bind(ldap, respool->binddn, respool->bindpw, server_cred); |
|
241 } |
|
242 |
|
243 int ws_ldap_bind(LDAP *ldap, const char *binddn, const char *bindpw, struct berval **server_cred) { |
248 struct berval cred; |
244 struct berval cred; |
249 cred.bv_val = respool->bindpw; |
245 cred.bv_val = (char*)bindpw; |
250 cred.bv_len = strlen(cred.bv_val); |
246 cred.bv_len = strlen(cred.bv_val); |
251 return ldap_sasl_bind_s( |
247 return ldap_sasl_bind_s( |
252 ldap, |
248 ldap, |
253 respool->binddn, |
249 binddn, |
254 LDAP_SASL_SIMPLE, |
250 LDAP_SASL_SIMPLE, |
255 &cred, |
251 &cred, |
256 NULL, |
252 NULL, |
257 NULL, |
253 NULL, |
258 server_cred); |
254 server_cred); |
259 } |
255 } |
|
256 |