2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
14 * ldap_op.c - routines to perform LDAP operations
19 #include <sys/types.h>
24 #include <kerberosIV/krb.h>
27 #endif /* KERBEROS_V */
36 /* Forward references */
37 static int get_changetype( char * );
38 static struct berval **make_singlevalued_berval( char *, int );
39 static int op_ldap_add( Ri *, Re *, char ** );
40 static int op_ldap_modify( Ri *, Re *, char ** );
41 static int op_ldap_delete( Ri *, Re *, char ** );
42 static int op_ldap_modrdn( Ri *, Re *, char ** );
43 static LDAPMod *alloc_ldapmod();
44 static void free_ldapmod( LDAPMod * );
45 static void free_ldmarr( LDAPMod ** );
46 static int getmodtype( char * );
47 static void dump_ldm_array( LDAPMod ** );
48 static char **read_krbnames( Ri * );
49 static void upcase( char * );
50 static int do_bind( Ri *, int * );
51 static int do_unbind( Ri * );
54 /* External references */
55 #ifndef SYSERRLIST_IN_STDIO
56 extern char *sys_errlist[];
57 #endif /* SYSERRLIST_IN_STDIO */
59 extern char *ch_malloc( unsigned long );
61 static char *kattrs[] = {"kerberosName", NULL };
62 static struct timeval kst = {30L, 0L};
67 * Determine the type of ldap operation being performed and call the
68 * appropriate routine.
69 * - If successful, returns ERR_DO_LDAP_OK
70 * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
71 * The caller should wait a while and retry the operation.
72 * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned. The caller
73 * should reject the operation and continue with the next replication
84 int lderr = LDAP_SUCCESS;
91 if ( ri->ri_ldp == NULL ) {
92 rc = do_bind( ri, &lderr );
93 if ( rc != BIND_OK ) {
94 return DO_LDAP_ERR_RETRYABLE;
98 switch ( re->re_changetype ) {
100 lderr = op_ldap_add( ri, re, errmsg );
101 if ( lderr != LDAP_SUCCESS ) {
102 Debug( LDAP_DEBUG_ANY,
103 "Error: ldap_add_s failed adding \"%s\": %s\n",
104 *errmsg ? *errmsg : ldap_err2string( lderr ),
109 lderr = op_ldap_modify( ri, re, errmsg );
110 if ( lderr != LDAP_SUCCESS ) {
111 Debug( LDAP_DEBUG_ANY,
112 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
113 *errmsg ? *errmsg : ldap_err2string( lderr ),
118 lderr = op_ldap_delete( ri, re, errmsg );
119 if ( lderr != LDAP_SUCCESS ) {
120 Debug( LDAP_DEBUG_ANY,
121 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
122 *errmsg ? *errmsg : ldap_err2string( lderr ),
127 lderr = op_ldap_modrdn( ri, re, errmsg );
128 if ( lderr != LDAP_SUCCESS ) {
129 Debug( LDAP_DEBUG_ANY,
130 "Error: ldap_modrdn_s failed modifying %s: %s\n",
131 *errmsg ? *errmsg : ldap_err2string( lderr ),
136 Debug( LDAP_DEBUG_ANY,
137 "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
138 re->re_changetype, re->re_dn, 0 );
139 return DO_LDAP_ERR_FATAL;
143 * Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
144 * we may have been idle long enough that the remote slapd timed
145 * us out. Rebind and try again.
147 if ( lderr == LDAP_SUCCESS ) {
149 } else if ( lderr == LDAP_SERVER_DOWN ) {
150 /* The LDAP server may have timed us out - rebind and try again */
151 (void) do_unbind( ri );
154 return DO_LDAP_ERR_FATAL;
157 return DO_LDAP_ERR_FATAL;
164 * Perform an ldap add operation.
174 int nattrs, rc = 0, i;
175 LDAPMod *ldm, **ldmarr;
182 * Construct a null-terminated array of LDAPMod structs.
185 while ( mi[ i ].mi_type != NULL ) {
186 ldm = alloc_ldapmod();
187 ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
188 ( nattrs + 2 ) * sizeof( LDAPMod * ));
189 ldmarr[ nattrs ] = ldm;
190 ldm->mod_op = LDAP_MOD_BVALUES;
191 ldm->mod_type = mi[ i ].mi_type;
193 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
198 if ( ldmarr != NULL ) {
199 ldmarr[ nattrs ] = NULL;
201 /* Perform the operation */
202 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
203 ri->ri_hostname, ri->ri_port, re->re_dn );
204 rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
205 lderr = ri->ri_ldp->ld_errno;
207 *errmsg = "No modifications to do";
208 Debug( LDAP_DEBUG_ANY,
209 "Error: op_ldap_add: no mods to do (%s)!", re->re_dn, 0, 0 );
211 free_ldmarr( ldmarr );
219 * Perform an ldap modify operation.
221 #define AWAITING_OP -1
230 int state; /* This code is a simple-minded state machine */
231 int nvals; /* Number of values we're modifying */
232 int nops; /* Number of LDAPMod structs in ldmarr */
233 LDAPMod *ldm, *nldm, **ldmarr;
243 if ( re->re_mods == NULL ) {
244 *errmsg = "No arguments given";
245 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
251 * Construct a null-terminated array of LDAPMod structs.
253 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
254 type = mi[ i ].mi_type;
255 value = mi[ i ].mi_val;
256 len = mi[ i ].mi_len;
257 switch ( getmodtype( type )) {
259 state = T_MODSEP; /* Got a separator line "-\n" */
263 ldmarr = ( LDAPMod ** )
264 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
265 ldmarr[ nops ] = ldm = alloc_ldapmod();
266 ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
267 ldm->mod_type = value;
272 state = T_MODOPREPLACE;
273 ldmarr = ( LDAPMod ** )
274 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
275 ldmarr[ nops ] = ldm = alloc_ldapmod();
276 ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
277 ldm->mod_type = value;
282 state = T_MODOPDELETE;
283 ldmarr = ( LDAPMod ** )
284 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
285 ldmarr[ nops ] = ldm = alloc_ldapmod();
286 ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
287 ldm->mod_type = value;
292 if ( state == AWAITING_OP ) {
293 Debug( LDAP_DEBUG_ANY,
294 "Error: op_ldap_modify: unknown mod type \"%s\"\n",
300 * We should have an attribute: value pair here.
301 * Construct the mod_bvalues part of the ldapmod struct.
303 if ( strcasecmp( type, ldm->mod_type )) {
304 Debug( LDAP_DEBUG_ANY,
305 "Error: malformed modify op, %s: %s (expecting %s:)\n",
306 type, value, ldm->mod_type );
309 ldm->mod_bvalues = ( struct berval ** )
310 ch_realloc( ldm->mod_bvalues,
311 ( nvals + 2 ) * sizeof( struct berval * ));
312 ldm->mod_bvalues[ nvals + 1 ] = NULL;
313 ldm->mod_bvalues[ nvals ] = ( struct berval * )
314 ch_malloc( sizeof( struct berval ));
315 ldm->mod_bvalues[ nvals ]->bv_val = value;
316 ldm->mod_bvalues[ nvals ]->bv_len = len;
320 ldmarr[ nops ] = NULL;
323 /* Actually perform the LDAP operation */
324 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
325 ri->ri_hostname, ri->ri_port, re->re_dn );
326 rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
328 free_ldmarr( ldmarr );
336 * Perform an ldap delete operation.
347 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
348 ri->ri_hostname, ri->ri_port, re->re_dn );
349 rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
358 * Perform an ldap modrdn operation.
361 #define GOT_DRDNFLAGSTR 2
362 #define GOT_ALLNEWRDNFLAGS ( GOT_NEWRDN | GOT_DRDNFLAGSTR )
377 if ( re->re_mods == NULL ) {
378 *errmsg = "No arguments given";
379 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
385 * Get the arguments: should see newrdn: and deleteoldrdn: args.
387 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
388 if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
389 newrdn = mi[ i ].mi_val;
391 } else if ( !strcmp( mi[ i ].mi_type, T_DRDNFLAGSTR )) {
392 state |= GOT_DRDNFLAGSTR;
393 if ( !strcmp( mi[ i ].mi_val, "0" )) {
395 } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
398 Debug( LDAP_DEBUG_ANY,
399 "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
400 mi[ i ].mi_val, 0, 0 );
401 *errmsg = "Incorrect argument to deleteoldrdn";
405 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
406 mi[ i ].mi_type, 0, 0 );
407 *errmsg = "Bad value in replication log entry";
413 * Punt if we don't have all the args.
415 if ( state != GOT_ALLNEWRDNFLAGS ) {
416 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
418 *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
423 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
426 sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
427 buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
429 sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
430 Debug( LDAP_DEBUG_ARGS,
431 "replica %s - modify rdn %s (flag: %d)\n",
432 buf, buf2, drdnflag );
435 #endif /* LDAP_DEBUG */
438 rc = ldap_modrdn2_s( ri->ri_ldp, re->re_dn, mi->mi_val, drdnflag );
440 return( ri->ri_ldp->ld_errno );
446 * Allocate and initialize an ldapmod struct.
453 ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
454 ldm->mod_type = NULL;
455 ldm->mod_bvalues = ( struct berval ** ) NULL;
462 * Free an ldapmod struct associated mod_bvalues. NOTE - it is assumed
463 * that mod_bvalues and mod_type contain pointers to the same block of memory
464 * pointed to by the repl struct. Therefore, it's not freed here.
475 if ( ldm->mod_bvalues != NULL ) {
476 for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
477 free( ldm->mod_bvalues[ i ] );
479 free( ldm->mod_bvalues );
487 * Free an an array of LDAPMod pointers and the LDAPMod structs they point
496 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
497 free_ldapmod( ldmarr[ i ] );
504 * Create a berval with a single value.
506 static struct berval **
507 make_singlevalued_berval(
513 p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
514 p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
516 p[ 0 ]->bv_val = value;
517 p[ 0 ]->bv_len = len;
523 * Given a modification type (string), return an enumerated type.
524 * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
531 if ( !strcmp( type, T_MODSEPSTR )) {
534 if ( !strcmp( type, T_MODOPADDSTR )) {
535 return( T_MODOPADD );
537 if ( !strcmp( type, T_MODOPREPLACESTR )) {
538 return( T_MODOPREPLACE );
540 if ( !strcmp( type, T_MODOPDELETESTR )) {
541 return( T_MODOPDELETE );
548 * Perform an LDAP unbind operation. If replica is NULL, or the
549 * repl_ldp is NULL, just return LDAP_SUCCESS. Otherwise, unbind,
550 * set the ldp to NULL, and return the result of the unbind call.
557 int rc = LDAP_SUCCESS;
559 if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
560 rc = ldap_unbind( ri->ri_ldp );
561 if ( rc != LDAP_SUCCESS ) {
562 Debug( LDAP_DEBUG_ANY,
563 "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
564 ldap_err2string( rc ), ri->ri_hostname, ri->ri_port );
574 * Perform an LDAP bind operation to the replication site given
575 * by replica. If replica->repl_ldp is non-NULL, then we unbind
576 * from the replica before rebinding. It should be safe to call
577 * this to re-connect if the replica's connection goes away
580 * Returns 0 on success, -1 if an LDAP error occurred, and a return
581 * code > 0 if some other error occurred, e.g. invalid bind method.
582 * If an LDAP error occurs, the LDAP error is returned in lderr.
597 char *skrbnames[ 2 ];
598 char realm[ REALM_SZ ];
599 char name[ ANAME_SZ ];
600 char instance[ INST_SZ ];
601 #endif /* KERBEROS */
606 Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
607 return( BIND_ERR_BADRI );
610 if ( ri->ri_ldp != NULL ) {
611 ldrc = ldap_unbind( ri->ri_ldp );
612 if ( ldrc != LDAP_SUCCESS ) {
613 Debug( LDAP_DEBUG_ANY,
614 "Error: do_bind: ldap_unbind failed: %s\n",
615 ldap_err2string( ldrc ), 0, 0 );
620 Debug( LDAP_DEBUG_ARGS, "Open connection to %s:%d\n",
621 ri->ri_hostname, ri->ri_port, 0 );
622 ri->ri_ldp = ldap_open( ri->ri_hostname, ri->ri_port );
623 if ( ri->ri_ldp == NULL ) {
624 Debug( LDAP_DEBUG_ANY, "Error: ldap_open(%s, %d) failed: %s\n",
625 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
626 return( BIND_ERR_OPEN );
630 * Set ldap library options to (1) not follow referrals, and
631 * (2) restart the select() system call.
633 #ifdef LDAP_REFERRALS
634 ri->ri_ldp->ld_options &= ~LDAP_OPT_REFERRALS;
635 #endif /* LDAP_REFERRALS */
636 ri->ri_ldp->ld_options |= LDAP_OPT_RESTART;
638 switch ( ri->ri_bind_method ) {
641 Debug( LDAP_DEBUG_ANY,
642 "Error: Kerberos bind for %s:%d, but not compiled w/kerberos\n",
643 ri->ri_hostname, ri->ri_port, 0 );
644 return( BIND_ERR_KERBEROS_FAILED );
647 * Bind using kerberos.
648 * If "bindprincipal" was given in the config file, then attempt
649 * to get a TGT for that principal (via the srvtab file). If only
650 * a binddn was given, then we need to read that entry to get
651 * the kerberosName attributes, and try to get a TGT for one
652 * of them. All are tried. The first one which succeeds is
653 * returned. XXX It might be a good idea to just require a
654 * bindprincipal. Reading the entry every time might be a significant
655 * amount of overhead, if the connection is closed between most
659 if ( ri->ri_principal != NULL ) {
660 skrbnames[ 0 ] = ri->ri_principal;
661 skrbnames[ 1 ] = NULL;
662 krbnames = skrbnames;
664 krbnames = read_krbnames( ri );
667 if (( krbnames == NULL ) || ( krbnames[ 0 ] == NULL )) {
668 Debug( LDAP_DEBUG_ANY,
669 "Error: Can't find krbname for binddn \"%s\"\n",
670 ri->ri_bind_dn, 0, 0 );
671 retval = BIND_ERR_KERBEROS_FAILED;
675 * Now we've got one or more kerberos principals. See if any
676 * of them are in the srvtab file.
679 for ( kni = 0; krbnames[ kni ] != NULL; kni++ ) {
680 rc = kname_parse( name, instance, realm, krbnames[ kni ]);
681 if ( rc != KSUCCESS ) {
685 rc = krb_get_svc_in_tkt( name, instance, realm, "krbtgt", realm,
687 if ( rc != KSUCCESS) {
688 Debug( LDAP_DEBUG_ANY, "Error: Can't get TGT for %s: %s\n",
689 krbnames[ kni ], krb_err_txt[ rc ], 0 );
696 Debug( LDAP_DEBUG_ANY,
697 "Error: Could not obtain TGT for DN \"%s\"\n",
698 ri->ri_bind_dn, 0, 0 );
699 retval = BIND_ERR_KERBEROS_FAILED;
703 * We've got a TGT. Do a kerberos bind.
705 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (kerberos)\n",
706 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
707 ldrc = ldap_kerberos_bind_s( ri->ri_ldp, ri->ri_bind_dn );
708 ri->ri_principal = strdup( krbnames[ kni ] );
709 if ( ldrc != LDAP_SUCCESS ) {
710 Debug( LDAP_DEBUG_ANY, "Error: kerberos bind for %s:%dfailed: %s\n",
711 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
713 retval = BIND_ERR_KERBEROS_FAILED;
716 kexit: if ( krbnames != NULL ) {
717 ldap_value_free( krbnames );
721 #endif /* KERBEROS */
724 * Bind with a plaintext password.
726 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
727 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
728 ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
730 if ( ldrc != LDAP_SUCCESS ) {
731 Debug( LDAP_DEBUG_ANY,
732 "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
733 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
735 return( BIND_ERR_SIMPLE_FAILED );
741 Debug( LDAP_DEBUG_ANY,
742 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
743 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
744 return( BIND_ERR_BAD_ATYPE );
753 * For debugging. Print the contents of an ldmarr array.
764 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
766 Debug( LDAP_DEBUG_TRACE,
767 "Trace (%d): *** ldmarr[ %d ] contents:\n",
769 Debug( LDAP_DEBUG_TRACE,
770 "Trace (%d): *** ldm->mod_op: %d\n",
771 getpid(), ldm->mod_op, 0 );
772 Debug( LDAP_DEBUG_TRACE,
773 "Trace (%d): *** ldm->mod_type: %s\n",
774 getpid(), ldm->mod_type, 0 );
775 if ( ldm->mod_bvalues != NULL ) {
776 for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
777 msgbuf = ch_malloc( b->bv_len + 512 );
778 sprintf( msgbuf, "***** bv[ %d ] len = %d, val = <%s>",
779 j, b->bv_len, b->bv_val );
780 Debug( LDAP_DEBUG_TRACE,
781 "Trace (%d):%s\n", getpid(), msgbuf, 0 );
790 * Get the kerberos names from the binddn for "replica" via an ldap search.
791 * Returns a null-terminated array of char *, or NULL if the entry could
792 * not be found or there were no kerberosName attributes. The caller is
793 * responsible for freeing the returned array and strings it points to.
803 LDAPMessage *result, *entry;
805 /* First need to bind as NULL */
806 rc = ldap_simple_bind_s( ri->ri_ldp, NULL, NULL );
807 if ( rc != LDAP_SUCCESS ) {
808 Debug( LDAP_DEBUG_ANY,
809 "Error: null bind failed getting krbnames for %s:%d: %s\n",
810 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
813 rc = ldap_search_st( ri->ri_ldp, ri->ri_bind_dn, LDAP_SCOPE_BASE,
814 "objectclass=*", kattrs, 0, &kst, &result );
815 if ( rc != LDAP_SUCCESS ) {
816 Debug( LDAP_DEBUG_ANY,
817 "Error: search failed getting krbnames for %s:%d: %s\n",
818 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
821 ne = ldap_count_entries( ri->ri_ldp, result );
823 Debug( LDAP_DEBUG_ANY,
824 "Error: Can't find entry \"%s\" for %s:%d kerberos bind\n",
825 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
829 Debug( LDAP_DEBUG_ANY,
830 "Error: Kerberos binddn \"%s\" for %s:%dis ambiguous\n",
831 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
834 entry = ldap_first_entry( ri->ri_ldp, result );
835 if ( entry == NULL ) {
836 Debug( LDAP_DEBUG_ANY,
837 "Error: Can't find \"%s\" for kerberos binddn for %s:%d\n",
838 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
841 krbnames = ldap_get_values( ri->ri_ldp, entry, "kerberosName" );
842 ldap_msgfree( result );
857 for ( p = s; ( p != NULL ) && ( *p != '\0' ); p++ ) {
858 if ( islower( *p )) {