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
21 #include <ac/stdlib.h>
24 #include <ac/string.h>
27 #include <ac/unistd.h>
36 /* Forward references */
37 static struct berval **make_singlevalued_berval LDAP_P(( char *, int ));
38 static int op_ldap_add LDAP_P(( Ri *, Re *, char ** ));
39 static int op_ldap_modify LDAP_P(( Ri *, Re *, char ** ));
40 static int op_ldap_delete LDAP_P(( Ri *, Re *, char ** ));
41 static int op_ldap_modrdn LDAP_P(( Ri *, Re *, char ** ));
42 static LDAPMod *alloc_ldapmod LDAP_P(( void ));
43 static void free_ldapmod LDAP_P(( LDAPMod * ));
44 static void free_ldmarr LDAP_P(( LDAPMod ** ));
45 static int getmodtype LDAP_P(( char * ));
46 static void dump_ldm_array LDAP_P(( LDAPMod ** ));
47 static char **read_krbnames LDAP_P(( Ri * ));
48 static void upcase LDAP_P(( char * ));
49 static int do_bind LDAP_P(( Ri *, int * ));
50 static int do_unbind LDAP_P(( Ri * ));
53 static char *kattrs[] = {"kerberosName", NULL };
54 static struct timeval kst = {30L, 0L};
59 * Determine the type of ldap operation being performed and call the
60 * appropriate routine.
61 * - If successful, returns ERR_DO_LDAP_OK
62 * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
63 * The caller should wait a while and retry the operation.
64 * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned. The caller
65 * should reject the operation and continue with the next replication
76 int lderr = LDAP_SUCCESS;
82 if ( ri->ri_ldp == NULL ) {
83 rc = do_bind( ri, &lderr );
84 if ( rc != BIND_OK ) {
85 return DO_LDAP_ERR_RETRYABLE;
89 switch ( re->re_changetype ) {
91 lderr = op_ldap_add( ri, re, errmsg );
92 if ( lderr != LDAP_SUCCESS ) {
93 Debug( LDAP_DEBUG_ANY,
94 "Error: ldap_add_s failed adding \"%s\": %s\n",
95 *errmsg ? *errmsg : ldap_err2string( lderr ),
100 lderr = op_ldap_modify( ri, re, errmsg );
101 if ( lderr != LDAP_SUCCESS ) {
102 Debug( LDAP_DEBUG_ANY,
103 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
104 *errmsg ? *errmsg : ldap_err2string( lderr ),
109 lderr = op_ldap_delete( ri, re, errmsg );
110 if ( lderr != LDAP_SUCCESS ) {
111 Debug( LDAP_DEBUG_ANY,
112 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
113 *errmsg ? *errmsg : ldap_err2string( lderr ),
118 lderr = op_ldap_modrdn( ri, re, errmsg );
119 if ( lderr != LDAP_SUCCESS ) {
120 Debug( LDAP_DEBUG_ANY,
121 "Error: ldap_modrdn_s failed modifying %s: %s\n",
122 *errmsg ? *errmsg : ldap_err2string( lderr ),
127 Debug( LDAP_DEBUG_ANY,
128 "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
129 re->re_changetype, re->re_dn, 0 );
130 return DO_LDAP_ERR_FATAL;
134 * Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
135 * we may have been idle long enough that the remote slapd timed
136 * us out. Rebind and try again.
138 if ( lderr == LDAP_SUCCESS ) {
140 } else if ( lderr == LDAP_SERVER_DOWN ) {
141 /* The LDAP server may have timed us out - rebind and try again */
142 (void) do_unbind( ri );
145 return DO_LDAP_ERR_FATAL;
148 return DO_LDAP_ERR_FATAL;
155 * Perform an ldap add operation.
165 int nattrs, rc = 0, i;
166 LDAPMod *ldm, **ldmarr;
173 * Construct a null-terminated array of LDAPMod structs.
176 while ( mi[ i ].mi_type != NULL ) {
177 ldm = alloc_ldapmod();
178 ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
179 ( nattrs + 2 ) * sizeof( LDAPMod * ));
180 ldmarr[ nattrs ] = ldm;
181 ldm->mod_op = LDAP_MOD_BVALUES;
182 ldm->mod_type = mi[ i ].mi_type;
184 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
189 if ( ldmarr != NULL ) {
190 ldmarr[ nattrs ] = NULL;
192 /* Perform the operation */
193 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
194 ri->ri_hostname, ri->ri_port, re->re_dn );
195 rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
197 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
200 *errmsg = "No modifications to do";
201 Debug( LDAP_DEBUG_ANY,
202 "Error: op_ldap_add: no mods to do (%s)!\n", re->re_dn, 0, 0 );
204 free_ldmarr( ldmarr );
212 * Perform an ldap modify operation.
214 #define AWAITING_OP -1
223 int state; /* This code is a simple-minded state machine */
224 int nvals; /* Number of values we're modifying */
225 int nops; /* Number of LDAPMod structs in ldmarr */
226 LDAPMod *ldm, **ldmarr;
236 if ( re->re_mods == NULL ) {
237 *errmsg = "No arguments given";
238 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
244 * Construct a null-terminated array of LDAPMod structs.
246 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
247 type = mi[ i ].mi_type;
248 value = mi[ i ].mi_val;
249 len = mi[ i ].mi_len;
250 switch ( getmodtype( type )) {
252 state = T_MODSEP; /* Got a separator line "-\n" */
256 ldmarr = ( LDAPMod ** )
257 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
258 ldmarr[ nops ] = ldm = alloc_ldapmod();
259 ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
260 ldm->mod_type = value;
265 state = T_MODOPREPLACE;
266 ldmarr = ( LDAPMod ** )
267 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
268 ldmarr[ nops ] = ldm = alloc_ldapmod();
269 ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
270 ldm->mod_type = value;
275 state = T_MODOPDELETE;
276 ldmarr = ( LDAPMod ** )
277 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
278 ldmarr[ nops ] = ldm = alloc_ldapmod();
279 ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
280 ldm->mod_type = value;
285 if ( state == AWAITING_OP ) {
286 Debug( LDAP_DEBUG_ANY,
287 "Error: op_ldap_modify: unknown mod type \"%s\"\n",
293 * We should have an attribute: value pair here.
294 * Construct the mod_bvalues part of the ldapmod struct.
296 if ( strcasecmp( type, ldm->mod_type )) {
297 Debug( LDAP_DEBUG_ANY,
298 "Error: malformed modify op, %s: %s (expecting %s:)\n",
299 type, value, ldm->mod_type );
302 ldm->mod_bvalues = ( struct berval ** )
303 ch_realloc( ldm->mod_bvalues,
304 ( nvals + 2 ) * sizeof( struct berval * ));
305 ldm->mod_bvalues[ nvals + 1 ] = NULL;
306 ldm->mod_bvalues[ nvals ] = ( struct berval * )
307 ch_malloc( sizeof( struct berval ));
308 ldm->mod_bvalues[ nvals ]->bv_val = value;
309 ldm->mod_bvalues[ nvals ]->bv_len = len;
313 ldmarr[ nops ] = NULL;
316 /* Actually perform the LDAP operation */
317 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
318 ri->ri_hostname, ri->ri_port, re->re_dn );
319 rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
321 free_ldmarr( ldmarr );
329 * Perform an ldap delete operation.
340 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
341 ri->ri_hostname, ri->ri_port, re->re_dn );
342 rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
351 * Perform an ldap modrdn operation.
354 #define GOT_DRDNFLAGSTR 2
355 #define GOT_ALLNEWRDNFLAGS ( GOT_NEWRDN | GOT_DRDNFLAGSTR )
371 if ( re->re_mods == NULL ) {
372 *errmsg = "No arguments given";
373 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
379 * Get the arguments: should see newrdn: and deleteoldrdn: args.
381 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
382 if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
383 newrdn = mi[ i ].mi_val;
385 } else if ( !strcmp( mi[ i ].mi_type, T_DRDNFLAGSTR )) {
386 state |= GOT_DRDNFLAGSTR;
387 if ( !strcmp( mi[ i ].mi_val, "0" )) {
389 } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
392 Debug( LDAP_DEBUG_ANY,
393 "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
394 mi[ i ].mi_val, 0, 0 );
395 *errmsg = "Incorrect argument to deleteoldrdn";
399 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
400 mi[ i ].mi_type, 0, 0 );
401 *errmsg = "Bad value in replication log entry";
407 * Punt if we don't have all the args.
409 if ( state != GOT_ALLNEWRDNFLAGS ) {
410 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
412 *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
417 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
420 sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
421 buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
423 sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
424 Debug( LDAP_DEBUG_ARGS,
425 "replica %s - modify rdn %s (flag: %d)\n",
426 buf, buf2, drdnflag );
429 #endif /* LDAP_DEBUG */
432 rc = ldap_modrdn2_s( ri->ri_ldp, re->re_dn, mi->mi_val, drdnflag );
434 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
441 * Allocate and initialize an ldapmod struct.
444 alloc_ldapmod( void )
448 ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
449 ldm->mod_type = NULL;
450 ldm->mod_bvalues = ( struct berval ** ) NULL;
457 * Free an ldapmod struct associated mod_bvalues. NOTE - it is assumed
458 * that mod_bvalues and mod_type contain pointers to the same block of memory
459 * pointed to by the repl struct. Therefore, it's not freed here.
470 if ( ldm->mod_bvalues != NULL ) {
471 for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
472 free( ldm->mod_bvalues[ i ] );
474 free( ldm->mod_bvalues );
482 * Free an an array of LDAPMod pointers and the LDAPMod structs they point
491 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
492 free_ldapmod( ldmarr[ i ] );
499 * Create a berval with a single value.
501 static struct berval **
502 make_singlevalued_berval(
508 p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
509 p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
511 p[ 0 ]->bv_val = value;
512 p[ 0 ]->bv_len = len;
518 * Given a modification type (string), return an enumerated type.
519 * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
526 if ( !strcmp( type, T_MODSEPSTR )) {
529 if ( !strcmp( type, T_MODOPADDSTR )) {
530 return( T_MODOPADD );
532 if ( !strcmp( type, T_MODOPREPLACESTR )) {
533 return( T_MODOPREPLACE );
535 if ( !strcmp( type, T_MODOPDELETESTR )) {
536 return( T_MODOPDELETE );
543 * Perform an LDAP unbind operation. If replica is NULL, or the
544 * repl_ldp is NULL, just return LDAP_SUCCESS. Otherwise, unbind,
545 * set the ldp to NULL, and return the result of the unbind call.
552 int rc = LDAP_SUCCESS;
554 if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
555 rc = ldap_unbind( ri->ri_ldp );
556 if ( rc != LDAP_SUCCESS ) {
557 Debug( LDAP_DEBUG_ANY,
558 "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
559 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
569 * Perform an LDAP bind operation to the replication site given
570 * by replica. If replica->repl_ldp is non-NULL, then we unbind
571 * from the replica before rebinding. It should be safe to call
572 * this to re-connect if the replica's connection goes away
575 * Returns 0 on success, -1 if an LDAP error occurred, and a return
576 * code > 0 if some other error occurred, e.g. invalid bind method.
577 * If an LDAP error occurs, the LDAP error is returned in lderr.
591 char *skrbnames[ 2 ];
592 char realm[ REALM_SZ ];
593 char name[ ANAME_SZ ];
594 char instance[ INST_SZ ];
595 #endif /* HAVE_KERBEROS */
600 Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
601 return( BIND_ERR_BADRI );
604 if ( ri->ri_ldp != NULL ) {
605 ldrc = ldap_unbind( ri->ri_ldp );
606 if ( ldrc != LDAP_SUCCESS ) {
607 Debug( LDAP_DEBUG_ANY,
608 "Error: do_bind: ldap_unbind failed: %s\n",
609 ldap_err2string( ldrc ), 0, 0 );
614 Debug( LDAP_DEBUG_ARGS, "Open connection to %s:%d\n",
615 ri->ri_hostname, ri->ri_port, 0 );
616 ri->ri_ldp = ldap_open( ri->ri_hostname, ri->ri_port );
617 if ( ri->ri_ldp == NULL ) {
618 Debug( LDAP_DEBUG_ANY, "Error: ldap_open(%s, %d) failed: %s\n",
619 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
620 return( BIND_ERR_OPEN );
624 * Disable string translation if enabled by default.
625 * The replication log is written in the internal format,
626 * so this would do another translation, breaking havoc.
628 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
629 ri->ri_ldp->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
630 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
633 * Set ldap library options to (1) not follow referrals, and
634 * (2) restart the select() system call.
636 ldap_set_option(ri->ri_ldp, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
637 ldap_set_option(ri->ri_ldp, LDAP_OPT_RESTART, LDAP_OPT_ON);
639 switch ( ri->ri_bind_method ) {
641 #ifndef HAVE_KERBEROS
642 Debug( LDAP_DEBUG_ANY,
643 "Error: Kerberos bind for %s:%d, but not compiled w/kerberos\n",
644 ri->ri_hostname, ri->ri_port, 0 );
645 return( BIND_ERR_KERBEROS_FAILED );
646 #else /* HAVE_KERBEROS */
648 * Bind using kerberos.
649 * If "bindprincipal" was given in the config file, then attempt
650 * to get a TGT for that principal (via the srvtab file). If only
651 * a binddn was given, then we need to read that entry to get
652 * the kerberosName attributes, and try to get a TGT for one
653 * of them. All are tried. The first one which succeeds is
654 * returned. XXX It might be a good idea to just require a
655 * bindprincipal. Reading the entry every time might be a significant
656 * amount of overhead, if the connection is closed between most
660 if ( ri->ri_principal != NULL ) {
661 skrbnames[ 0 ] = ri->ri_principal;
662 skrbnames[ 1 ] = NULL;
663 krbnames = skrbnames;
665 krbnames = read_krbnames( ri );
668 if (( krbnames == NULL ) || ( krbnames[ 0 ] == NULL )) {
669 Debug( LDAP_DEBUG_ANY,
670 "Error: Can't find krbname for binddn \"%s\"\n",
671 ri->ri_bind_dn, 0, 0 );
672 retval = BIND_ERR_KERBEROS_FAILED;
676 * Now we've got one or more kerberos principals. See if any
677 * of them are in the srvtab file.
680 for ( kni = 0; krbnames[ kni ] != NULL; kni++ ) {
681 rc = kname_parse( name, instance, realm, krbnames[ kni ]);
682 if ( rc != KSUCCESS ) {
686 rc = krb_get_svc_in_tkt( name, instance, realm, "krbtgt", realm,
688 if ( rc != KSUCCESS) {
689 Debug( LDAP_DEBUG_ANY, "Error: Can't get TGT for %s: %s\n",
690 krbnames[ kni ], krb_err_txt[ rc ], 0 );
697 Debug( LDAP_DEBUG_ANY,
698 "Error: Could not obtain TGT for DN \"%s\"\n",
699 ri->ri_bind_dn, 0, 0 );
700 retval = BIND_ERR_KERBEROS_FAILED;
704 * We've got a TGT. Do a kerberos bind.
706 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (kerberos)\n",
707 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
708 ldrc = ldap_kerberos_bind_s( ri->ri_ldp, ri->ri_bind_dn );
709 ri->ri_principal = strdup( krbnames[ kni ] );
710 if ( ldrc != LDAP_SUCCESS ) {
711 Debug( LDAP_DEBUG_ANY, "Error: kerberos bind for %s:%dfailed: %s\n",
712 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
714 retval = BIND_ERR_KERBEROS_FAILED;
717 kexit: if ( krbnames != NULL ) {
718 ldap_value_free( krbnames );
722 #endif /* HAVE_KERBEROS */
725 * Bind with a plaintext password.
727 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
728 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
729 ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
731 if ( ldrc != LDAP_SUCCESS ) {
732 Debug( LDAP_DEBUG_ANY,
733 "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
734 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
736 return( BIND_ERR_SIMPLE_FAILED );
742 Debug( LDAP_DEBUG_ANY,
743 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
744 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
745 return( BIND_ERR_BAD_ATYPE );
754 * For debugging. Print the contents of an ldmarr array.
766 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
768 Debug( LDAP_DEBUG_TRACE,
769 "Trace (%ld): *** ldmarr[ %d ] contents:\n",
770 (long) getpid(), i, 0 );
771 Debug( LDAP_DEBUG_TRACE,
772 "Trace (%ld): *** ldm->mod_op: %d\n",
773 (long) getpid(), ldm->mod_op, 0 );
774 Debug( LDAP_DEBUG_TRACE,
775 "Trace (%ld): *** ldm->mod_type: %s\n",
776 (long) getpid(), ldm->mod_type, 0 );
777 if ( ldm->mod_bvalues != NULL ) {
778 for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
779 msgbuf = ch_malloc( b->bv_len + 512 );
780 sprintf( msgbuf, "***** bv[ %d ] len = %ld, val = <%s>",
781 j, b->bv_len, b->bv_val );
782 Debug( LDAP_DEBUG_TRACE,
783 "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );
792 * Get the kerberos names from the binddn for "replica" via an ldap search.
793 * Returns a null-terminated array of char *, or NULL if the entry could
794 * not be found or there were no kerberosName attributes. The caller is
795 * responsible for freeing the returned array and strings it points to.
805 LDAPMessage *result, *entry;
807 /* First need to bind as NULL */
808 rc = ldap_simple_bind_s( ri->ri_ldp, NULL, NULL );
809 if ( rc != LDAP_SUCCESS ) {
810 Debug( LDAP_DEBUG_ANY,
811 "Error: null bind failed getting krbnames for %s:%d: %s\n",
812 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
815 rc = ldap_search_st( ri->ri_ldp, ri->ri_bind_dn, LDAP_SCOPE_BASE,
816 "objectclass=*", kattrs, 0, &kst, &result );
817 if ( rc != LDAP_SUCCESS ) {
818 Debug( LDAP_DEBUG_ANY,
819 "Error: search failed getting krbnames for %s:%d: %s\n",
820 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
823 ne = ldap_count_entries( ri->ri_ldp, result );
825 Debug( LDAP_DEBUG_ANY,
826 "Error: Can't find entry \"%s\" for %s:%d kerberos bind\n",
827 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
831 Debug( LDAP_DEBUG_ANY,
832 "Error: Kerberos binddn \"%s\" for %s:%dis ambiguous\n",
833 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
836 entry = ldap_first_entry( ri->ri_ldp, result );
837 if ( entry == NULL ) {
838 Debug( LDAP_DEBUG_ANY,
839 "Error: Can't find \"%s\" for kerberos binddn for %s:%d\n",
840 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
843 krbnames = ldap_get_values( ri->ri_ldp, entry, "kerberosName" );
844 ldap_msgfree( result );
860 for ( p = s; ( p != NULL ) && ( *p != '\0' ); p++ ) {
861 *p = TOUPPER( (unsigned char) *p );