/* Client Server Praktikum Universität Tübingen
*
* Aufgabe 3, BLATT1
* Thema: LDAP
* Sebastian Seyrich und Judith Bentele
*
*/


#define HOST   "localhost"
#define PORT   1091
#define BASE   "dc=uni-tuebingen,dc=de"
#define PASSWD "secret"
#define ROOTDN "cn=admin,dc=uni-tuebingen,dc=de"

#include <lber.h>
#include <ldap.h>
#include <stdio.h>




main() {
  LDAP *ld;
  LDAPMessage    *res, *e;
  int i;
  char *a, *dn;
  void *ptr;
  char **vals;


  /*  open a connection   */
  if ( (ld = ldap_open(HOST,PORT)) == 0)
    exit(1);
  printf("connecting to ldap server ...\n");

  /*  authentication  */
  if (ldap_simple_bind_s(ld, ROOTDN, PASSWD) != LDAP_SUCCESS)
    exit(1);
  printf("bind ...\n");

  printf("search ...");

  /*  do something */
  if (ldap_search_s( ld, BASE,LDAP_SCOPE_SUBTREE, "(&(postalcode=7*)(description=*Bad*))", NULL, 0, &res) != LDAP_SUCCESS){
    ldap_perror(ld, "ldap_search_s");
    exit(1);
  }
  printf("ready\n\n");
  printf("Output:\n ");
/* step through each entry returned */
  for (e = ldap_first_entry(ld, res); e != NULL; e = ldap_next_entry(ld, e)){
    /* print its name */
    dn = ldap_get_dn(ld, e);
    printf("dn: %s\n", dn );
    free(dn);
    
    /* print each attribute */
    for (a = ldap_first_attribute(ld, e, &ptr); a != NULL; a = ldap_next_attribute(ld, e, ptr)){
      printf("\tattribute: %s / ", a );
      
      /* print each value */
      vals = ldap_get_values( ld, e, a );
      for (i = 0; vals[i] != NULL; i++) {
	printf("value: %s\n", vals[i]);
      }
      ldap_value_free(vals);
    }
    printf("\n");
  }
  
  ldap_msgfree( res);

  ldap_unbind(ld);
}



