1 | #include <stdio.h> |
---|
2 | #include <ldap.h> |
---|
3 | |
---|
4 | int main(void) |
---|
5 | { |
---|
6 | LDAP *ld; |
---|
7 | LDAPMessage *res, *e; |
---|
8 | int i, rc; |
---|
9 | char *a, *dn; |
---|
10 | BerElement *ptr; |
---|
11 | char **vals; |
---|
12 | |
---|
13 | /* open an LDAP session */ |
---|
14 | if ( (ld = ldap_init( "ldap.eparana.parana", 389 )) == NULL ) |
---|
15 | { |
---|
16 | fprintf( stderr, "erro ao se conectar:"); |
---|
17 | return 1; |
---|
18 | } |
---|
19 | |
---|
20 | /* authenticate as nobody */ |
---|
21 | if (( rc = ldap_simple_bind_s( ld, NULL, NULL )) != LDAP_SUCCESS ) |
---|
22 | { |
---|
23 | fprintf( stderr, "ldap_simple_bind_s: %s\n", ldap_err2string( rc )); |
---|
24 | ldap_unbind( ld ); |
---|
25 | return 1; |
---|
26 | } |
---|
27 | |
---|
28 | /* search for entries with cn of "Babs Jensen", return all attrs */ |
---|
29 | if (( rc = ldap_search_s( ld, "dc=pr, dc=gov, dc=br", LDAP_SCOPE_SUBTREE, "(uid=alexandrecorreia)", NULL, 0, &res )) != LDAP_SUCCESS ) |
---|
30 | { |
---|
31 | fprintf( stderr, "ldap_search_s: %s\n", ldap_err2string( rc )); |
---|
32 | if ( res == NULL ) |
---|
33 | { |
---|
34 | ldap_unbind( ld ); |
---|
35 | return 1; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | /* step through each entry returned */ |
---|
40 | for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) ) |
---|
41 | { |
---|
42 | /* print its name */ |
---|
43 | dn = ldap_get_dn( ld, e ); |
---|
44 | printf( "uid: %s\n", dn ); |
---|
45 | ldap_memfree( dn ); |
---|
46 | |
---|
47 | /* print each attribute */ |
---|
48 | for ( a = ldap_first_attribute( ld, e, &ptr ); a != NULL; a = ldap_next_attribute( ld, e, ptr ) ) |
---|
49 | { |
---|
50 | printf( "\tattribute: %s\n", a ); |
---|
51 | |
---|
52 | /* print each value */ |
---|
53 | vals = ldap_get_values( ld, e, a ); |
---|
54 | |
---|
55 | for ( i = 0; vals[i] != NULL; i++ ) |
---|
56 | printf( "\t\tvalue: %s\n", vals[i] ); |
---|
57 | |
---|
58 | ldap_value_free( vals ); |
---|
59 | ldap_memfree( a ); |
---|
60 | } |
---|
61 | if ( ptr != NULL ) |
---|
62 | ber_free( ptr, 0 ); |
---|
63 | } |
---|
64 | /* free the search results */ |
---|
65 | ldap_msgfree( res ); |
---|
66 | |
---|
67 | /* close and free connection resources */ |
---|
68 | ldap_unbind( ld ); |
---|
69 | |
---|
70 | return 0; |
---|
71 | } |
---|