static int fldap_read(const char* path, char* buf, size_t size, off_t offset, struct fuse_file_info* fi) { static char* attrs[] = {"description", NULL}; LDAPMessage* msg = NULL; LDAPMessage* entry = NULL; BerElement* ber = NULL; char* dn; char** vals; char* attr; dn = path_to_dn(path, "cn="); ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "(ObjectClass=*)", attrs, 0, &msg); entry = ldap_first_entry(ld, msg); if (!entry) { ldap_msgfree(msg); free(dn); return -ENOENT; } for (entry = ldap_first_entry(ld, msg); entry != NULL; entry = ldap_next_entry(ld, entry)) { for (attr = ldap_first_attribute(ld, entry, &ber); attr != NULL; attr = ldap_next_attribute(ld, entry, ber)) { if (!strcmp(attr, "description") && ((vals = ldap_get_values(ld, entry, attr)) != NULL)) { strncpy(buf, vals[0] + offset, size); ldap_value_free(vals); } ldap_memfree(attr); } ber_free(ber,0); } ldap_msgfree(msg); free(dn); return strlen(buf); }
static void set_attributes(lua_State *L, LDAP *ld, LDAPMessage *entry, int tab) { BerElement *ber = NULL; BerValue **values; char *attr; int i, n; attr = ldap_first_attribute(ld, entry, &ber); while (attr != NULL) { values = ldap_get_values_len(ld, entry, attr); n = ldap_count_values_len(values); if (n == 0) /* no values */ lua_pushboolean(L, 1); else if (n == 1) /* just one value */ lua_pushlstring(L, values[0]->bv_val, values[0]->bv_len); else { /* multiple values */ lua_newtable(L); for (i = 0; i < n; i++) { lua_pushlstring(L, values[i]->bv_val, values[i]->bv_len); lua_rawseti(L, -2, i+1); } } lua_setfield(L, tab, attr); ldap_value_free_len(values); ldap_memfree(attr); attr = ldap_next_attribute(ld, entry, ber); } ber_free(ber, 0); }
unsigned int GetUserLevel(LDAP *ld,char *user) { LDAPMessage *res, *e; char *a; BerElement *ptr; char **vals; unsigned int ret; char *filter = GetClientFilter(user); if (ldap_search_s(ld, CLIENT_PATH, LDAP_SCOPE_SUBTREE,filter, NULL, 0, &res)!= LDAP_SUCCESS) { printf("%s\n",filter); ldap_perror(ld, "ldap_search_s"); exit(1); } if( (e=ldap_first_entry(ld, res)) == NULL ) fprintf(stderr, "LDAP error: user not found"); a = ldap_first_attribute(ld, e, &ptr); while( strcmp(a,"internationaliSDNNumber") != 0 ) a = ldap_next_attribute(ld, e, ptr); vals = ldap_get_values(ld, e, a); ret = atoi(vals[0]); /* free the search results */ ldap_msgfree(res); return ret; }
static void ldap_dump(LDAP *ld,LDAPMessage *res) { LDAPMessage *e; for (e = ldap_first_entry(ld, res); e; e = ldap_next_entry(ld, e)) { BerElement *b; char *attr; char *dn = ldap_get_dn(ld, res); if (dn) printf("dn: %s\n", dn); ldap_memfree(dn); for (attr = ldap_first_attribute(ld, e, &b); attr; attr = ldap_next_attribute(ld, e, b)) { char **values, **p; values = ldap_get_values(ld, e, attr); for (p = values; *p; p++) { printf("%s: %s\n", attr, *p); } ldap_value_free(values); ldap_memfree(attr); } ber_free(b, 1); printf("\n"); } }
static void dump_ldap_entry (LDAP *ld, LDAPMessage *res) { BerElement *pos; gchar **values; gchar **v; char *t; t = ldap_get_dn (ld, res); g_debug ("dn: %s\n", t); ldap_memfree (t); for (t = ldap_first_attribute (ld, res, &pos); t; t = ldap_next_attribute (ld, res, pos)) { values = get_ldap_values (ld, res, t); for (v = values; *v; v++) g_debug ("%s: %s\n", t, *v); g_strfreev (values); ldap_memfree (t); } ber_free (pos, 0); }
/** * Attempt to discover the base DN for a server using LDAP version 3. * \param ld LDAP handle for a connected server. * \param tov Timeout value (seconds), or 0 for none, default 30 secs. * \return List of Base DN's, or NULL if could not read. List should be * g_free() when done. */ static GList *ldaputil_test_v3( LDAP *ld, gint tov ) { GList *baseDN = NULL; gint rc, i; LDAPMessage *result, *e; gchar *attribs[2]; BerElement *ber; gchar *attribute; gchar **vals; struct timeval timeout; /* Set timeout */ timeout.tv_usec = 0L; if( tov > 0 ) { timeout.tv_sec = tov; } else { timeout.tv_sec = 30L; } /* Test for LDAP version 3 */ attribs[0] = SYLDAP_V3_TEST_ATTR; attribs[1] = NULL; rc = ldap_search_ext_s( ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER, attribs, 0, NULL, NULL, &timeout, 0, &result ); if( rc == LDAP_SUCCESS ) { /* Process entries */ for( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) { /* Process attributes */ for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL; attribute = ldap_next_attribute( ld, e, ber ) ) { if( strcasecmp( attribute, SYLDAP_V3_TEST_ATTR ) == 0 ) { vals = ldap_get_values( ld, e, attribute ); if( vals != NULL ) { for( i = 0; vals[i] != NULL; i++ ) { baseDN = g_list_append( baseDN, g_strdup( vals[i] ) ); } } ldap_value_free( vals ); } ldap_memfree( attribute ); } if( ber != NULL ) { ber_free( ber, 0 ); } ber = NULL; } } ldap_msgfree( result ); return baseDN; }
/* dump a record from LDAP on stdout used for debugging */ void ads_dump(ADS_STRUCT *ads, LDAPMessage *res) { char *field; LDAPMessage *msg; BerElement *b; char *this_dn; for (msg = ldap_first_entry(ads->ld, res); msg; msg = ldap_next_entry(ads->ld, msg)) { this_dn = ldap_get_dn(ads->ld, res); if (this_dn) { printf("Dumping: %s\n", this_dn); } ldap_memfree(this_dn); for (field = ldap_first_attribute(ads->ld, msg, &b); field; field = ldap_next_attribute(ads->ld, msg, b)) { char **values, **p; values = ldap_get_values(ads->ld, msg, field); for (p = values; *p; p++) { printf("%s: %s\n", field, *p); } ldap_value_free(values); ldap_memfree(field); } ber_free(b, 1); printf("\n"); } }
/* * call-seq: * entry.get_attributes => Array of String * entry.attrs => Array of String * * Return an array of all the attributes belonging to the entry. */ VALUE rb_ldap_entry_get_attributes (VALUE self) { RB_LDAPENTRY_DATA *edata; VALUE vals; char *attr; BerElement *ber = NULL; GET_LDAPENTRY_DATA (self, edata); vals = rb_ary_new (); for (attr = ldap_first_attribute (edata->ldap, edata->msg, &ber); attr != NULL; attr = ldap_next_attribute (edata->ldap, edata->msg, ber)) { rb_ary_push (vals, rb_tainted_str_new2 (attr)); ldap_memfree(attr); } #if !defined(USE_OPENLDAP1) if( ber != NULL ){ ber_free(ber, 0); } #endif return vals; }
static ATTR* get_attrs( LDAP *ld, LDAPMessage *e ) { char *a; ATTR *head, *tail, *tmp; BerElement *ber; head=tail=tmp=NULL; for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL; a = ldap_next_attribute( ld, e, ber ) ) { tmp = (ATTR*)malloc(sizeof(ATTR)); if(head == NULL) head = tail = tmp; else { tail->next = tmp; tail = tmp; } tmp->name = a; tmp->vals = ldap_get_values( ld, e, a ); tmp->next = NULL; } if ( ber != NULL ) { ber_free( ber, 0 ); } /* used for debugging tmp=head; while(tmp!= NULL) { printf("\n%s :", tmp->name); for(i=0; tmp->vals[i] != NULL; i++) printf("\n\t%d %s", i, tmp->vals[i]); tmp = tmp->next; } */ return(head); }
void AD::AddGroups(LDAPMessage *search) { DWORD i; DWORD j; LDAPMessage *entry = NULL; PWCHAR attribute; PWCHAR *values; BerElement *berElement; for(i = 0; i < ldap_count_entries(ldap, search); i++) { Group *g = new Group(); if(!i) { entry = ldap_first_entry(ldap, search); } else { entry = ldap_next_entry(ldap, entry); } attribute = ldap_first_attribute(ldap, entry, &berElement); while(attribute != NULL) { values = ldap_get_values(ldap, entry, attribute); if(lstrcmpi(attribute, L"samaccountname") == 0) { g->name = values[0]; } if(lstrcmpi(attribute, L"member") == 0) { for(j = 0; j < ldap_count_values(values); j++) { std::wstring *ret = new std::wstring(); CNToAccountName(values[j], ret); g->users.push_back(ret); } } ldap_value_free(values); ldap_memfree(attribute); attribute = ldap_next_attribute(ldap, entry, berElement); } ber_free(berElement, 0); if(g->name.length() > 0) { GetGroupManager()->groups.push_back(g); g->PrettyPrint(); } else { delete(g); } } }
static int fldap_readlink(const char* path, char* buf, size_t size) { static char* attrs[] = {"description", NULL}; char* dn; LDAPMessage* msg = NULL; LDAPMessage* entry = NULL; BerElement* ber = NULL; char** vals; char* attr; if ((dn = is_dn_exist(path))) { ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "(ObjectClass=*)", attrs, 0, &msg); if ((entry = ldap_first_entry(ld, msg))) { for (attr = ldap_first_attribute(ld, entry, &ber); attr != NULL; attr = ldap_next_attribute(ld, entry, ber)) { if ((vals = ldap_get_values(ld, entry, attr)) != NULL) { if (!strcmp("description", attr)) strncpy(buf, vals[0], size); ldap_value_free(vals); } ldap_memfree(attr); } ber_free(ber, 0); } ldap_msgfree(msg); } free(dn); return 0; }
/** int check_ldap_passwd(LDAP* ld, JBXL_LDAP_Dn user) LDAP を使用してユーザ認証を行う. @param ld LDAPサーバへのセッションハンドラ @param user ユーザ情報が格納された JBXL_LDAP_Dn @retval 0 正常終了. @retval 1 ユーザ認証失敗(ユーザは存在するが,パスワードが一致しない) @retval 2 ユーザ認証失敗(ユーザが存在しない) @retval -1 LDAPサーバへのセッションハンドラが NULL @retval -2 BASE名が不明 */ int check_ldap_passwd(LDAP* ld, JBXL_LDAP_Dn user) { int ret; char* dn_attr[] = {_tochar("distinguishedName"), NULL}; if (ld==NULL) return -1; if (user.base.buf==NULL) user.base = dup_Buffer(JBXLdapDnBind->base); if (user.base.buf==NULL) return -2; // if (user.dnbind.buf==NULL) return 2; else { Buffer tmp = erase_sBuffer(user.dnbind, "*"); copy_Buffer(&tmp, &user.dnbind); free_Buffer(&tmp); } if (user.dnbind.buf[0]=='\0') return 2; Buffer cond = make_Buffer_bystr("uid="); cat_Buffer(&user.dnbind, &cond); LDAPMessage* res = NULL; ret = ldap_search_s(ld, (char*)user.base.buf, LDAP_SCOPE_SUBTREE, (char*)cond.buf, dn_attr, 0, &res); if (res==NULL) return 2; LDAPMessage* ent = ldap_first_entry(ld, res); if (ent==NULL) { ldap_msgfree(res); return 2; } BerElement* ber = NULL; char* attr = ldap_first_attribute(ld, ent, &ber); if (attr==NULL) { ldap_msgfree(res); return 2; } char** dn = ldap_get_values(ld, ent, attr); ldap_memfree(attr); ldap_msgfree(res); if (dn==NULL || *dn==NULL) return 2; // ユーザチェック Password "" is OK!! Ohhh GeroGero!! if (user.passwd.buf==NULL || user.passwd.buf[0]=='\0') return 1; // パスワード確認 ret = ldap_simple_bind_s(ld, *dn, (char*)user.passwd.buf); if (ret!=LDAP_SUCCESS) return 1; // 念のため,セッションを確認 //ret = ldap_compare_s(ld, *dn, "name", (char*)user.dnbind.buf); //if (ret!=LDAP_COMPARE_TRUE) return 1; return 0; }
/* XXX - this is for a stopgap implementation of gfs_opendir() */ char * gfarm_generic_info_get_foreach( char *dn, int scope, /* LDAP_SCOPE_ONELEVEL or LDAP_SCOPE_SUBTREE */ char *query, void *tmp_info, /* just used as a work area */ void (*callback)(void *, void *), void *closure, const struct gfarm_generic_info_ops *ops) { LDAPMessage *res, *e; int i, rv; char *a; BerElement *ptr; char **vals; /* search for entries, return all attrs */ rv = ldap_search_s(gfarm_ldap_server, dn, scope, query, NULL, 0, &res); if (rv != LDAP_SUCCESS) { if (rv == LDAP_NO_SUCH_OBJECT) return (GFARM_ERR_NO_SUCH_OBJECT); return (ldap_err2string(rv)); } /* step through each entry returned */ for (i = 0, e = ldap_first_entry(gfarm_ldap_server, res); e != NULL; e = ldap_next_entry(gfarm_ldap_server, e)) { ops->clear(tmp_info); for (a = ldap_first_attribute(gfarm_ldap_server, e, &ptr); a != NULL; a = ldap_next_attribute(gfarm_ldap_server, e, ptr)) { vals = ldap_get_values(gfarm_ldap_server, e, a); if (vals[0] == NULL) { ldap_value_free(vals); continue; } ops->set_field(tmp_info, a, vals); ldap_value_free(vals); } if (!ops->validate(tmp_info)) { /* invalid record */ ops->free(tmp_info); continue; } (*callback)(closure, tmp_info); ops->free(tmp_info); i++; } /* free the search results */ ldap_msgfree(res); if (i == 0) return (GFARM_ERR_NO_SUCH_OBJECT); return (NULL); }
char * gfarm_generic_info_get( void *key, void *info, const struct gfarm_generic_info_ops *ops) { LDAPMessage *res, *e; int n, rv; char *a; BerElement *ber; char **vals; char *dn = ops->make_dn(key); char *error; if (dn == NULL) return (GFARM_ERR_NO_MEMORY); if ((error = gfarm_ldap_check()) != NULL) return (error); rv = ldap_search_s(gfarm_ldap_server, dn, LDAP_SCOPE_BASE, ops->query_type, NULL, 0, &res); free(dn); if (rv != LDAP_SUCCESS) { if (rv == LDAP_NO_SUCH_OBJECT) return (GFARM_ERR_NO_SUCH_OBJECT); return (ldap_err2string(rv)); } n = ldap_count_entries(gfarm_ldap_server, res); if (n == 0) { /* free the search results */ ldap_msgfree(res); return (GFARM_ERR_NO_SUCH_OBJECT); } ops->clear(info); e = ldap_first_entry(gfarm_ldap_server, res); ber = NULL; for (a = ldap_first_attribute(gfarm_ldap_server, e, &ber); a != NULL; a = ldap_next_attribute(gfarm_ldap_server, e, ber)) { vals = ldap_get_values(gfarm_ldap_server, e, a); if (vals[0] != NULL) ops->set_field(info, a, vals); ldap_value_free(vals); ldap_memfree(a); } if (ber != NULL) ber_free(ber, 0); /* free the search results */ ldap_msgfree(res); /* should check all fields are filled */ if (!ops->validate(info)) { ops->free(info); /* XXX - different error code is better ? */ return (GFARM_ERR_NO_SUCH_OBJECT); } return (NULL); /* success */ }
static int check_ldap(const char *host, unsigned short port, int timeout, int *value_int) { LDAP *ldap = NULL; LDAPMessage *res = NULL; LDAPMessage *msg = NULL; BerElement *ber = NULL; char *attrs[2] = {"namingContexts", NULL }; char *attr = NULL; char **valRes = NULL; int ldapErr = 0; zbx_alarm_on(timeout); *value_int = 0; if (NULL == (ldap = ldap_init(host, port))) { zabbix_log(LOG_LEVEL_DEBUG, "LDAP - initialization failed [%s:%hu]", host, port); goto lbl_ret; } if (LDAP_SUCCESS != (ldapErr = ldap_search_s(ldap, "", LDAP_SCOPE_BASE, "(objectClass=*)", attrs, 0, &res))) { zabbix_log(LOG_LEVEL_DEBUG, "LDAP - searching failed [%s] [%s]", host, ldap_err2string(ldapErr)); goto lbl_ret; } if (NULL == (msg = ldap_first_entry(ldap, res))) { zabbix_log(LOG_LEVEL_DEBUG, "LDAP - empty sort result. [%s] [%s]", host, ldap_err2string(ldapErr)); goto lbl_ret; } if (NULL == (attr = ldap_first_attribute(ldap, msg, &ber))) { zabbix_log(LOG_LEVEL_DEBUG, "LDAP - empty first entry result. [%s] [%s]", host, ldap_err2string(ldapErr)); goto lbl_ret; } valRes = ldap_get_values(ldap, msg, attr); *value_int = 1; lbl_ret: zbx_alarm_off(); if (NULL != valRes) ldap_value_free(valRes); if (NULL != attr) ldap_memfree(attr); if (NULL != ber) ber_free(ber, 0); if (NULL != res) ldap_msgfree(res); if (NULL != ldap) ldap_unbind(ldap); return SYSINFO_RET_OK; }
ADDRESS * address_from_ldap(LDAP_CHOOSE_S *winning_e) { ADDRESS *ret_a = NULL; if(winning_e){ char *a; BerElement *ber; ret_a = mail_newaddr(); for(a = ldap_first_attribute(winning_e->ld, winning_e->selected_entry, &ber); a != NULL; a = ldap_next_attribute(winning_e->ld, winning_e->selected_entry, ber)){ int i; char *p; char **vals; dprint((9, "attribute: %s\n", a ? a : "?")); if(!ret_a->personal && strcmp(a, winning_e->info_used->cnattr) == 0){ dprint((9, "Got cnattr:")); vals = ldap_get_values(winning_e->ld, winning_e->selected_entry, a); for(i = 0; vals[i] != NULL; i++) dprint((9, " %s\n", vals[i] ? vals[i] : "?")); if(vals && vals[0]) ret_a->personal = cpystr(vals[0]); ldap_value_free(vals); } else if(!ret_a->mailbox && strcmp(a, winning_e->info_used->mailattr) == 0){ dprint((9, "Got mailattr:")); vals = ldap_get_values(winning_e->ld, winning_e->selected_entry, a); for(i = 0; vals[i] != NULL; i++) dprint((9, " %s\n", vals[i] ? vals[i] : "?")); /* use first one */ if(vals && vals[0]){ if((p = strindex(vals[0], '@')) != NULL){ ret_a->host = cpystr(p+1); *p = '\0'; } ret_a->mailbox = cpystr(vals[0]); } ldap_value_free(vals); } our_ldap_memfree(a); } } return(ret_a); }
static VALUE ldapmessage2obj(LDAP *ld, LDAPMessage *msg) { VALUE obj; char *dn, *attr; BerElement *ber; BerValue **values; BerValue *value; VALUE rdn, attrs, ary, str; int length, i; obj = rb_class_new_instance(0, NULL, cLDAP_Message); // Set the DN dn = ldap_get_dn(ld, msg); rdn = rb_str_new2(dn); ldap_memfree(dn); rb_iv_set(obj, "@dn", rdn); // Set the attributes attrs = rb_hash_new(); attr = ldap_first_attribute(ld, msg, &ber); do { values = ldap_get_values_len(ld, msg, attr); if (values == NULL) { rldap_raise(rldap_errno_c(obj)); } ary = rb_ary_new(); length = ldap_count_values_len(values); for (i=0; i<length; i++) { value = values[i]; if (value->bv_len == 4 && strncmp(value->bv_val, "TRUE", value->bv_len) == 0) rb_ary_push(ary, Qtrue); else if (value->bv_len == 5 && strncmp(value->bv_val, "FALSE", value->bv_len) == 0) rb_ary_push(ary, Qfalse); else { str = rb_str_new(value->bv_val, value->bv_len); rb_ary_push(ary, str); } } rb_hash_aset(attrs, rb_str_new2(attr), ary); ldap_value_free_len(values); ldap_memfree(attr); } while (attr = ldap_next_attribute(ld, msg, ber)); ber_free(ber, 0); rb_iv_set(obj, "@attrs", attrs); return obj; }
int main( int argc, char **argv ) { LDAP *ld; LDAPMessage *result, *e; BerElement *ber; char *a, *dn; char **vals; int i; /* get a handle to an LDAP connection */ if ( (ld = ldap_init( MY_HOST, MY_PORT )) == NULL ) { perror( "ldap_init" ); return( 1 ); } /* authenticate to the directory as nobody */ if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_simple_bind_s" ); ldap_unbind( ld ); return( 1 ); } /* search for all entries with surname of Jensen */ if ( ldap_search_s( ld, MY_SEARCHBASE, LDAP_SCOPE_SUBTREE, MY_FILTER, NULL, 0, &result ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_search_s" ); if ( result == NULL ) { ldap_unbind( ld ); return( 1 ); } } /* for each entry print out name + all attrs and values */ for ( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) { if ( (dn = ldap_get_dn( ld, e )) != NULL ) { printf( "dn: %s\n", dn ); ldap_memfree( dn ); } for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL; a = ldap_next_attribute( ld, e, ber ) ) { if ((vals = ldap_get_values( ld, e, a)) != NULL ) { for ( i = 0; vals[i] != NULL; i++ ) { printf( "%s: %s\n", a, vals[i] ); } ldap_value_free( vals ); } ldap_memfree( a ); } if ( ber != NULL ) { ber_free( ber, 0 ); } printf( "\n" ); } ldap_msgfree( result ); ldap_unbind( ld ); return( 0 ); }
// Fetch all attributes of an entry, and display them in a dialog void LdapView::showProperties( LDAP *ld, char *dn ) { PropDlg dlg; if ( ldap_search( ld, dn, LDAP_SCOPE_BASE, "objectclass=*", NULL, FALSE ) == -1 ) { AfxMessageBox( "Failed to start asynchronous search" ); return; } LDAPMessage *res; int rc; // Process results as they come in while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res )) == LDAP_RES_SEARCH_ENTRY ) { LDAPMessage *e = ldap_first_entry( ld, res ); BerElement *ber; // Loop over attributes in this entry for ( char *a = ldap_first_attribute( ld, e, &ber ); a != NULL; a = ldap_next_attribute( ld, e, ber ) ) { struct berval **bvals; if ( (bvals = ldap_get_values_len( ld, e, a )) != NULL ) { dlg.AddLine( a ); // Loop over values for this attribute for ( int i = 0; bvals[i] != NULL; i++ ) { CString val; val.Format( " %s", bvals[ i ]->bv_val ); dlg.AddLine( val ); } ber_bvecfree( bvals ); } } if ( ber != NULL ) ber_free( ber, 0 ); ldap_msgfree( res ); } if ( rc == -1 ) { AfxMessageBox( "Error on ldap_result" ); return; } else if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) { char *errString = ldap_err2string( rc ); AfxMessageBox( errString ); } ldap_msgfree( res ); // Set the title of the dialog to the distinguished name, and display it dlg.SetTitle( dn ); dlg.DoModal(); }
/* * parses the result returned by an ldap query */ static err_t parse_ldap_result(LDAP * ldap, LDAPMessage *result, chunk_t *blob) { err_t ugh = NULL; LDAPMessage * entry = ldap_first_entry(ldap, result); if (entry != NULL) { BerElement *ber = NULL; char *attr; attr = ldap_first_attribute(ldap, entry, &ber); if (attr != NULL) { struct berval **values = ldap_get_values_len(ldap, entry, attr); if (values != NULL) { if (values[0] != NULL) { blob->len = values[0]->bv_len; blob->ptr = alloc_bytes(blob->len, "ldap blob"); memcpy(blob->ptr, values[0]->bv_val, blob->len); if (values[1] != NULL) { plog("warning: more than one value was fetched from LDAP URL"); } } else { ugh = "no values in attribute"; } ldap_value_free_len(values); } else { ugh = ldap_err2string(ldap_result2error(ldap, entry, 0)); } ldap_memfree(attr); } else { ugh = ldap_err2string(ldap_result2error(ldap, entry, 0)); } ber_free(ber, 0); } else { ugh = ldap_err2string(ldap_result2error(ldap, result, 0)); } return ugh; }
int ldap_get_attr_vals(str *_attr_name, struct berval ***_vals) { BerElement *ber; char *a; /* * check for last_ldap_result */ if (last_ldap_result == NULL) { LM_ERR("last_ldap_result == NULL\n"); return -1; } if (last_ldap_handle == NULL) { LM_ERR("last_ldap_handle == NULL\n"); return -1; } /* * search for attribute named _attr_name */ *_vals = NULL; for (a = ldap_first_attribute(last_ldap_handle, last_ldap_result, &ber); a != NULL; a = ldap_next_attribute(last_ldap_handle, last_ldap_result, ber)) { if (strlen(a) == _attr_name->len && strncmp(a, _attr_name->s, _attr_name->len) == 0) { *_vals = ldap_get_values_len( last_ldap_handle, last_ldap_result, a); ldap_memfree(a); break; } ldap_memfree(a); } if (ber != NULL) { ber_free(ber, 0); } if (*_vals != NULL) { return 0; } else { return 1; } }
void BuildReply(int res, LDAPRequest *req) { LDAPResult *ldap_result = req->result = new LDAPResult(); req->result->type = req->type; if (res != LDAP_SUCCESS) { ldap_result->error = ldap_err2string(res); return; } if (req->message == NULL) { return; } /* a search result */ for (LDAPMessage *cur = ldap_first_message(this->con, req->message); cur; cur = ldap_next_message(this->con, cur)) { LDAPAttributes attributes; char *dn = ldap_get_dn(this->con, cur); if (dn != NULL) { attributes["dn"].push_back(dn); ldap_memfree(dn); dn = NULL; } BerElement *ber = NULL; for (char *attr = ldap_first_attribute(this->con, cur, &ber); attr; attr = ldap_next_attribute(this->con, cur, ber)) { berval **vals = ldap_get_values_len(this->con, cur, attr); int count = ldap_count_values_len(vals); std::vector<Anope::string> attrs; for (int j = 0; j < count; ++j) attrs.push_back(vals[j]->bv_val); attributes[attr] = attrs; ldap_value_free_len(vals); ldap_memfree(attr); } if (ber != NULL) ber_free(ber, 0); ldap_result->messages.push_back(attributes); } }
// Sorry that this is kind of crappy, but passing the point to wstring // is basically necessary unless you want thrashed memory for strings. void AD::CNToAccountName(LPWSTR s, std::wstring *d) { LDAPMessage *search = NULL; LDAPMessage *entry = NULL; PWCHAR attribute; PWCHAR *values; BerElement *berElement = NULL; DWORD i; DWORD j; LPWSTR filters[2]; filters[0] = L"samAccountName"; filters[1] = NULL; d->clear(); if(ldap_search_s(ldap, s, LDAP_SCOPE_SUBTREE, NULL, filters, 0, &search) != LDAP_SUCCESS) { Util::Error(LdapGetLastError(), L"ldap_search_s()"); } entry = ldap_first_entry(ldap, search); for(i = 0; i < ldap_count_entries(ldap, search); i++) { if(!i) { entry = ldap_first_entry(ldap, search); } else { entry = ldap_next_entry(ldap, entry); } attribute = ldap_first_attribute(ldap, entry, &berElement); while(attribute != NULL) { values = ldap_get_values(ldap, entry, attribute); if(lstrcmpi(attribute, L"samaccountname") == 0) { d->append(values[0]); } ldap_value_free(values); ldap_memfree(attribute); attribute = ldap_next_attribute(ldap, entry, berElement); } } ber_free(berElement, 0); ldap_msgfree(search); }
/** * Should be called on an event requiring an ldap search, * will return an slack_ldap_event_t struct. The event * struct will provide all the necessary information * and pointers to relevant information. The function * itself uses the ldap_query_t tree to make a series * of ldap searches to find out information about the * nick provided, or the lack of it. */ void slack_ldap_search(const char *qval) { slack_ldap_query_t *shead = &head; //Local searches event head while(shead->next != NULL) { LDAPMessage *result; syslog(LOG_INFO, "Performing ldap search for %s", qval); char newfilter[strlen(shead->filterstr) + strlen(qval)]; sprintf(newfilter, shead->filterstr, qval); int search_status = ldap_search_ext_s( ldap, shead->basednstr, LDAP_SCOPE_SUBTREE, newfilter, NULL, 0, NULL, NULL, &timeout, 1, &result); syslog(LOG_INFO, "Search Status: %s", ldap_err2string(search_status)); //if(search_status != LDAP_SUCCESS) { return; } // We should probably die syslog(LOG_INFO, "Parsing ldap search result"); LDAPMessage *entry = ldap_first_entry(ldap, result); char *attribute; BerElement *ber; char **vals; int i; if(entry != NULL) { syslog(LOG_INFO, "Found LDAP Entry..."); /* For each attribute of the entry that has been parsed */ for(attribute = ldap_first_attribute(ldap, entry, &ber); attribute != NULL; // if NULL then we are at the end attribute = ldap_next_attribute(ldap, entry, ber)) { /* For each attribute print the attribute name and values */ if ((vals = ldap_get_values(ldap, entry, attribute)) != NULL) { for(i=0; vals[i] != NULL; i++) { syslog(LOG_INFO, "FOUND VALUE %s: %s", attribute, vals[i]); /* We now have to call the ldap event, which allows the * custom handling from the configuration. */ } ldap_value_free(vals); // MY } ldap_memfree(attribute); // GOD } if(ber != NULL) { ber_free(ber, 0); // MEMLEAK } } ldap_msgfree(result); // AVERTED /* Iterate through to the next ldap search event */ shead = shead->next; } // End While }
void AD::AddServers(LDAPMessage *search) { DWORD i; DWORD j; LDAPMessage *entry = NULL; BerElement *berElement = NULL; PWCHAR attribute; PWCHAR *values; for(i = 0; i < ldap_count_entries(ldap, search); i++) { Server *s = new Server(); if(!i) { entry = ldap_first_entry(ldap, search); } else { entry = ldap_next_entry(ldap, entry); } attribute = ldap_first_attribute(ldap, entry, &berElement); while(attribute != NULL) { values = ldap_get_values(ldap, entry, attribute); if(lstrcmpi(attribute, L"samaccountname") == 0) { s->name = values[0]; s->name.resize(s->name.length()-1); } ldap_value_free(values); ldap_memfree(attribute); attribute = ldap_next_attribute(ldap, entry, berElement); } if(s->name.length() > 0) { GetServerManager()->servers.push_back(s); s->PrettyPrint(); } else { delete(s); } } ber_free(berElement, 0); }
/* ** Store entry's attributes and values at the given table. ** @param entry Current entry. ** @param tab Absolute stack index of the table. */ static void set_attribs (lua_State *L, LDAP *ld, LDAPMessage *entry, int tab) { char *attr; BerElement *ber = NULL; for (attr = ldap_first_attribute (ld, entry, &ber); attr != NULL; attr = ldap_next_attribute (ld, entry, ber)) { lua_pushstring (L, attr); push_values (L, ld, entry, attr); lua_rawset (L, tab); /* tab[attr] = vals */ ldap_memfree (attr); } ber_free (ber, 0); /* don't need to test if (ber == NULL) */ }
static char * get_variable (const char *varname, LDAP *connection, LDAPMessage *entry) { BerElement *berptr; const char *attr; char *retval; if (strcmp (varname, "USER") == 0) return g_strdup (g_get_user_name ()); if (strcmp (varname, "EVOLUTION_UID") == 0) return get_evolution_uid (); if (connection == NULL || entry == NULL) return g_strdup (""); if (strncmp (varname, "LDAP_ATTR_", 10) != 0) return g_strdup (""); varname += 10; retval = NULL; berptr = NULL; attr = ldap_first_attribute (connection, entry, &berptr); while (attr != NULL && retval == NULL) { struct berval **values; if (strcmp (attr, varname) == 0) { values = ldap_get_values_len (connection, entry, attr); if (values != NULL && values[0] != NULL) retval = g_strdup (values[0]->bv_val); ldap_value_free_len (values); } attr = ldap_next_attribute (connection, entry, berptr); } ber_free (berptr, 0); return retval ? retval : g_strdup (""); }
static int fldap_readdir(const char* path, void* buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi) { static char* attrs[] = {"filename", NULL}; LDAPMessage* msg = NULL; LDAPMessage* entry = NULL; BerElement* ber = NULL; char* dn; char** vals; char* attr; /* The path returned should look like that "/path/to/the/folder". * After the split, we should have something like that: * ou=folder,ou=the,ou=to,ou=path,dc=example,dc=com * This is the dn of the folder. */ filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); dn = path_to_dn(path, "ou="); if (ldap_search_s(ld, dn, LDAP_SCOPE_ONE, "(ObjectClass=*)", attrs, 0, &msg) != LDAP_SUCCESS) { return -ENOENT; free(dn); } entry = ldap_first_entry(ld, msg); if (!entry) { ldap_msgfree(msg); free(dn); return 0; } for (entry = ldap_first_entry(ld, msg); entry != NULL; entry = ldap_next_entry(ld, entry)) { for( attr = ldap_first_attribute(ld, entry, &ber); attr != NULL; attr = ldap_next_attribute(ld, entry, ber)) { if ((vals = ldap_get_values(ld, entry, attr)) != NULL) { if (!strcmp("filename", attr)) filler(buf, vals[0], NULL, 0); ldap_value_free(vals); } ldap_memfree(attr); } ber_free(ber,0); } ldap_msgfree(msg); free(dn); return 0; }
/*********************************************************************** * ldap_first_attributeW (WLDAP32.@) * * Get the first attribute for a given entry. * * PARAMS * ld [I] Pointer to an LDAP context. * entry [I] Entry to retrieve attribute for. * ptr [O] Position pointer. * * RETURNS * Success: Name of the first attribute. * Failure: NULL * * NOTES * Use ldap_memfree to free the returned string. */ PWCHAR CDECL ldap_first_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, WLDAP32_BerElement** ptr ) { PWCHAR ret = NULL; #ifdef HAVE_LDAP char *retU; TRACE( "(%p, %p, %p)\n", ld, entry, ptr ); if (!ld || !entry) return NULL; retU = ldap_first_attribute( ld, entry, ptr ); ret = strUtoW( retU ); ldap_memfree( retU ); #endif return ret; }
static int fldap_write(const char* path, const char* data, size_t size, off_t offset, struct fuse_file_info* fi) { static char* attrs[] = {"description", NULL}; LDAPMessage* msg = NULL; LDAPMessage* entry = NULL; BerElement* ber = NULL; char* buf = NULL; char strsize[32]; char* dn; char** vals; char* attr; dn = path_to_dn(path, "cn="); ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "(ObjectClass=*)", attrs, 0, &msg); entry = ldap_first_entry(ld, msg); if (!entry) { ldap_msgfree(msg); free(dn); return -ENOENT; } for (entry = ldap_first_entry(ld, msg); entry != NULL; entry = ldap_next_entry(ld, entry)) { for (attr = ldap_first_attribute(ld, entry, &ber); attr != NULL; attr = ldap_next_attribute(ld, entry, ber)) { if (!strcmp(attr, "description") && ((vals = ldap_get_values(ld, entry, attr)) != NULL)) { buf = calloc(strlen(vals[0]) + size + 1, sizeof(char)); strncpy(buf, vals[0], offset); strncat(buf, data, size); ldap_value_free(vals); } ldap_memfree(attr); } ber_free(ber, 0); } ldap_msgfree(msg); if (!buf) { buf = calloc(size + 1, sizeof(char)); strncpy(buf, data, size); } snprintf(strsize, 32, "%"PRIi32, (int)strlen(buf)); modify_attr(path, LDAP_MOD_REPLACE, "description", buf); modify_attr(path, LDAP_MOD_REPLACE, "stsize", strsize); free(dn); free(buf); return size; }