/* * Creates an LDAP search URL given a comma-separated list of attributes. * Returns a list of key=values separated by '\n' */ char * pref_get_ldap_attributes(char* host, char* base, char* filter, char* attrs, char** return_error) { char *value = NULL; LDAP* ld; int err, i; char *url; LDAPMessage *result; LDAPMessage *e; char *a; BerElement *ber; char **vals; ld = ldap_init(host, LDAP_PORT); if (!ld) return value; url = (char*) malloc(sizeof(char) * (strlen(host) + strlen(base) + strlen(filter) + strlen(attrs) + 20)); if (!url) return value; XP_SPRINTF(url, "ldap://%s/%s?%s?sub?%s", host, base, attrs, filter); err = ldap_url_search_s( ld, url, 0, &result ); XP_FREE(url); if (err != LDAP_SUCCESS) { *return_error = ldap_err2string(err); return value; } e = ldap_first_entry( ld, result ); if (e) { a = ldap_first_attribute( ld, e, &ber ); if (a) { int total_buf_size = 200; int remaining_buf_size = total_buf_size; value = (char*) malloc(sizeof(char*) * total_buf_size); if (!value) return NULL; value[0] = '\0'; for ( ; a != NULL; a = ldap_next_attribute( ld, e, ber )) { vals = ldap_get_values( ld, e, a ); if (vals && vals[0]) { remaining_buf_size -= (strlen(a) + strlen(vals[0]) + 2); if (remaining_buf_size < 1) { remaining_buf_size += 2 * total_buf_size; total_buf_size += 2 * total_buf_size; value = (char*) realloc(value, sizeof(char*) * total_buf_size); if (!value) return NULL; } strcat(value, "\n"); strcat(value, a); strcat(value, "="); strcat(value, vals[0]); ldap_value_free( vals ); } } ldap_memfree(a); } if (ber) ber_free(ber, 0); } ldap_msgfree(result); ldap_unbind(ld); return value; }
CURLcode Curl_ldap(struct connectdata *conn) { CURLcode status = CURLE_OK; int rc; void *(*ldap_open)(char *, int); int (*ldap_simple_bind_s)(void *, char *, char *); int (*ldap_unbind_s)(void *); int (*ldap_url_search_s)(void *, char *, int, void **); void *(*ldap_first_entry)(void *, void *); void *(*ldap_next_entry)(void *, void *); char *(*ldap_err2string)(int); int (*ldap_entry2text)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long); int (*ldap_entry2html)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long, char *, char *); void *server; void *result; void *entryIterator; int ldaptext; struct SessionHandle *data=conn->data; infof(data, "LDAP: %s\n", data->change.url); DynaOpen(); if (libldap == NULL) { failf(data, "The needed LDAP library/libraries couldn't be opened"); return CURLE_LIBRARY_NOT_FOUND; } ldaptext = data->set.ftp_ascii; /* This is a dirty hack */ /* The types are needed because ANSI C distinguishes between * pointer-to-object (data) and pointer-to-function. */ DYNA_GET_FUNCTION(void *(*)(char *, int), ldap_open); DYNA_GET_FUNCTION(int (*)(void *, char *, char *), ldap_simple_bind_s); DYNA_GET_FUNCTION(int (*)(void *), ldap_unbind_s); DYNA_GET_FUNCTION(int (*)(void *, char *, int, void **), ldap_url_search_s); DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_first_entry); DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_next_entry); DYNA_GET_FUNCTION(char *(*)(int), ldap_err2string); DYNA_GET_FUNCTION(int (*)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long), ldap_entry2text); DYNA_GET_FUNCTION(int (*)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long, char *, char *), ldap_entry2html); server = ldap_open(conn->hostname, conn->port); if (server == NULL) { failf(data, "LDAP: Cannot connect to %s:%d", conn->hostname, conn->port); status = CURLE_COULDNT_CONNECT; } else { rc = ldap_simple_bind_s(server, conn->bits.user_passwd?data->state.user:NULL, conn->bits.user_passwd?data->state.passwd:NULL); if (rc != 0) { failf(data, "LDAP: %s", ldap_err2string(rc)); status = CURLE_LDAP_CANNOT_BIND; } else { rc = ldap_url_search_s(server, data->change.url, 0, &result); if (rc != 0) { failf(data, "LDAP: %s", ldap_err2string(rc)); status = CURLE_LDAP_SEARCH_FAILED; } else { for (entryIterator = ldap_first_entry(server, result); entryIterator; entryIterator = ldap_next_entry(server, entryIterator)) { if (ldaptext) { rc = ldap_entry2text(server, NULL, entryIterator, NULL, NULL, NULL, WriteProc, data, (char *)"", 0, 0); if (rc != 0) { failf(data, "LDAP: %s", ldap_err2string(rc)); status = CURLE_LDAP_SEARCH_FAILED; } } else { rc = ldap_entry2html(server, NULL, entryIterator, NULL, NULL, NULL, WriteProc, data, (char *)"", 0, 0, NULL, NULL); if (rc != 0) { failf(data, "LDAP: %s", ldap_err2string(rc)); status = CURLE_LDAP_SEARCH_FAILED; } } } } ldap_unbind_s(server); } } DynaClose(); /* no data to transfer */ Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL); return status; }