/* Netprog 2000 */ /* Sample LDAP client based on the code in RFC 1823 - LDAP API This example uses synchronous calls to add a new record to the netprog 2000 database. */ #include #include /* the encoding library */ #include /* the LDAP library */ #include #include /* strdup */ /* The name of the server is hard-coded here! In general we would want this to be specified on the command line, or in a config file somewhere! */ char *serverhost = "monte.cs.rpi.edu"; LDAPMod *makemod(char *name, char *value) { LDAPMod *m; char **vals; m = malloc(sizeof(LDAPMod)); vals = malloc(2 * sizeof(char *)); vals[0] = strdup(value); vals[1] = NULL; m->mod_type = strdup(name); m->mod_values = vals; return(m); } int main(int argc, char **argv ) { LDAP *ld; /* ldap connection handle */ LDAPMessage *res, *e; /* point to LDAP messages */ LDAPMod *attribs[10]; char *dn = argv[1]; /* open a connection to the LDAP server running on the default LDAP port on the machine serverhost */ if ( (ld = ldap_open( serverhost, LDAP_PORT )) == NULL ) { printf("Problem opening LDAP connection to %s\n",serverhost); exit( 1 ); } /* authenticate as nobody */ if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) { /* ldap_perror is like perror, but uses the ldap_errno */ ldap_perror( ld, "ldap_simple_bind_s" ); exit( 1 ); } printf("adding dn <%s>\n",dn); attribs[0] = makemod("cn","Joe Student"); attribs[1] = makemod("objectclass","student"); attribs[2] = makemod("email","joe@rpi.edu"); attribs[3] = makemod("sn","Student"); attribs[4] = NULL; /* add a new record */ if (ldap_add_s(ld,dn,attribs)!=LDAP_SUCCESS) { ldap_perror(ld,"Error adding\n"); exit(1); } /* close and free connection resources */ ldap_unbind( ld ); return(0); }