FACT++  1.0
ldap.cc
Go to the documentation of this file.
1 #define LDAP_DEPRECATED 1
2 
3 #include <stdio.h>
4 #include <ldap.h> //libldap2-dev
5 
6 int main(int argc, const char *argv[])
7 {
8  /*
9  /* Adjust these setting for your own LDAP server */
10  #define HOSTNAME "localhost"
11  #define PORT_NUMBER LDAP_PORT
12  #define FIND_DN "uid=bjensen, ou=People, dc=example,dc=com"
13 /*
14  $ldaphost = "161.72.93.133:389";
15  $baseDN = "";
16  $groupDN = "cn=Operations,ou=Application Groups,".$baseDN;
17 */
18  const string host = "161.72.93.133";
19 
20  LDAP *ld;
21  LDAPMessage *result, *e;
22  BerElement *ber;
23  char *a;
24  char **vals;
25  int i, rc;
26  /* Get a handle to an LDAP connection. */
27  if (!(ld = ldap_init(host.c_str(), LDAP_PORT)))
28  {
29  perror( "ldap_init" );
30  return( 1 );
31  }
32  /*
33  $sr = @ldap_search($con, $dn, $filter, $attributes);
34  if (!$sr)
35  return "ldap_search failed for dn=".$dn.": ".ldap_error($con);
36 
37  $srData = @ldap_get_entries($con, $sr);
38  if ($srData["count"]==0)
39  return "No results returned by ldap_get_entries for dn=".$dn.".";
40 
41  $email =$srData[0]['mail'][0];
42  $userCommonName=$srData[0]['cn'][0];
43  $userDN =$srData[0]['dn'];
44 
45  //------------------ Authenticate user
46  if (!@ldap_bind($con, $userDN, $password))
47  return "ldap_bind failed: ".ldap_error($con);
48  */
49  /* Bind anonymously to the LDAP server. */
50  rc = ldap_simple_bind_s( ld, NULL, NULL );
51  if ( rc != LDAP_SUCCESS )
52  {
53  fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc));
54  return( 1 );
55  }
56  /* Search for the entry. */
57  if ( ( rc = ldap_search_ext_s( ld, "ou=People,dc=fact,dc=iac,dc=es", LDAP_SCOPE_BASE,
58  "(objectclass=*)", NULL, 0, NULL, NULL, LDAP_NO_LIMIT,
59  LDAP_NO_LIMIT, &result ) ) != LDAP_SUCCESS )
60  {
61  fprintf(stderr, "ldap_search_ext_s: %s\n", ldap_err2string(rc));
62  return( 1 );
63  }
64 
65  /* Since we are doing a base search, there should be only
66  one matching entry. */
67  e = ldap_first_entry( ld, result );
68  if ( e != NULL )
69  {
70  printf( "\nFound %s:\n\n", FIND_DN );
71  /* Iterate through each attribute in the entry. */
72  for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL; a = ldap_next_attribute( ld, e, ber ) )
73  {
74  /* For each attribute, print the attribute name and values. */
75  if ((vals = ldap_get_values( ld, e, a)) != NULL )
76  {
77  for ( i = 0; vals[i] != NULL; i++ ) {
78  printf( "%s: %s\n", a, vals[i] );
79  }
80  ldap_value_free( vals );
81  }
82  ldap_memfree( a );
83  }
84  if ( ber != NULL ) {
85  ber_free( ber, 0 );
86  }
87  }
88  ldap_msgfree( result );
89  ldap_unbind( ld );
90 
91  printf("end\n");
92 
93  return( 0 );
94 
int i
Definition: db_dim_client.c:21
int main(int argc, const char *argv[])
Definition: ldap.cc:6
#define FIND_DN