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
23 #include <ac/string.h>
26 #include <ac/unistd.h> /* Get_t getpid() */
35 /* Forward references */
36 static struct berval **make_singlevalued_berval LDAP_P(( char *, int ));
37 static int op_ldap_add LDAP_P(( Ri *, Re *, char ** ));
38 static int op_ldap_modify LDAP_P(( Ri *, Re *, char ** ));
39 static int op_ldap_delete LDAP_P(( Ri *, Re *, char ** ));
40 static int op_ldap_modrdn LDAP_P(( Ri *, Re *, char ** ));
41 static LDAPMod *alloc_ldapmod LDAP_P(( void ));
42 static void free_ldapmod LDAP_P(( LDAPMod * ));
43 static void free_ldmarr LDAP_P(( LDAPMod ** ));
44 static int getmodtype LDAP_P(( char * ));
45 static void dump_ldm_array LDAP_P(( LDAPMod ** ));
46 static char **read_krbnames LDAP_P(( Ri * ));
47 static void upcase LDAP_P(( char * ));
48 static int do_bind LDAP_P(( Ri *, int * ));
49 static int do_unbind LDAP_P(( Ri * ));
52 static char *kattrs[] = {"kerberosName", NULL };
53 static struct timeval kst = {30L, 0L};
58 * Determine the type of ldap operation being performed and call the
59 * appropriate routine.
60 * - If successful, returns ERR_DO_LDAP_OK
61 * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
62 * The caller should wait a while and retry the operation.
63 * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned. The caller
64 * should reject the operation and continue with the next replication
75 int lderr = LDAP_SUCCESS;
81 if ( ri->ri_ldp == NULL ) {
82 rc = do_bind( ri, &lderr );
83 if ( rc != BIND_OK ) {
84 return DO_LDAP_ERR_RETRYABLE;
88 switch ( re->re_changetype ) {
90 lderr = op_ldap_add( ri, re, errmsg );
91 if ( lderr != LDAP_SUCCESS ) {
92 Debug( LDAP_DEBUG_ANY,
93 "Error: ldap_add_s failed adding \"%s\": %s\n",
94 *errmsg ? *errmsg : ldap_err2string( lderr ),
99 lderr = op_ldap_modify( ri, re, errmsg );
100 if ( lderr != LDAP_SUCCESS ) {
101 Debug( LDAP_DEBUG_ANY,
102 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
103 *errmsg ? *errmsg : ldap_err2string( lderr ),
108 lderr = op_ldap_delete( ri, re, errmsg );
109 if ( lderr != LDAP_SUCCESS ) {
110 Debug( LDAP_DEBUG_ANY,
111 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
112 *errmsg ? *errmsg : ldap_err2string( lderr ),
117 lderr = op_ldap_modrdn( ri, re, errmsg );
118 if ( lderr != LDAP_SUCCESS ) {
119 Debug( LDAP_DEBUG_ANY,
120 "Error: ldap_modrdn_s failed modifying %s: %s\n",
121 *errmsg ? *errmsg : ldap_err2string( lderr ),
126 Debug( LDAP_DEBUG_ANY,
127 "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
128 re->re_changetype, re->re_dn, 0 );
129 return DO_LDAP_ERR_FATAL;
133 * Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
134 * we may have been idle long enough that the remote slapd timed
135 * us out. Rebind and try again.
137 if ( lderr == LDAP_SUCCESS ) {
139 } else if ( lderr == LDAP_SERVER_DOWN ) {
140 /* The LDAP server may have timed us out - rebind and try again */
141 (void) do_unbind( ri );
144 return DO_LDAP_ERR_FATAL;
147 return DO_LDAP_ERR_FATAL;
154 * Perform an ldap add operation.
164 int nattrs, rc = 0, i;
165 LDAPMod *ldm, **ldmarr;
172 * Construct a null-terminated array of LDAPMod structs.
175 while ( mi[ i ].mi_type != NULL ) {
176 ldm = alloc_ldapmod();
177 ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
178 ( nattrs + 2 ) * sizeof( LDAPMod * ));
179 ldmarr[ nattrs ] = ldm;
180 ldm->mod_op = LDAP_MOD_BVALUES;
181 ldm->mod_type = mi[ i ].mi_type;
183 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
188 if ( ldmarr != NULL ) {
189 ldmarr[ nattrs ] = NULL;
191 /* Perform the operation */
192 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
193 ri->ri_hostname, ri->ri_port, re->re_dn );
194 rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
196 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
199 *errmsg = "No modifications to do";
200 Debug( LDAP_DEBUG_ANY,
201 "Error: op_ldap_add: no mods to do (%s)!", re->re_dn, 0, 0 );
203 free_ldmarr( ldmarr );
211 * Perform an ldap modify operation.
213 #define AWAITING_OP -1
222 int state; /* This code is a simple-minded state machine */
223 int nvals; /* Number of values we're modifying */
224 int nops; /* Number of LDAPMod structs in ldmarr */
225 LDAPMod *ldm, **ldmarr;
235 if ( re->re_mods == NULL ) {
236 *errmsg = "No arguments given";
237 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
243 * Construct a null-terminated array of LDAPMod structs.
245 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
246 type = mi[ i ].mi_type;
247 value = mi[ i ].mi_val;
248 len = mi[ i ].mi_len;
249 switch ( getmodtype( type )) {
251 state = T_MODSEP; /* Got a separator line "-\n" */
255 ldmarr = ( LDAPMod ** )
256 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
257 ldmarr[ nops ] = ldm = alloc_ldapmod();
258 ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
259 ldm->mod_type = value;
264 state = T_MODOPREPLACE;
265 ldmarr = ( LDAPMod ** )
266 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
267 ldmarr[ nops ] = ldm = alloc_ldapmod();
268 ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
269 ldm->mod_type = value;
274 state = T_MODOPDELETE;
275 ldmarr = ( LDAPMod ** )
276 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
277 ldmarr[ nops ] = ldm = alloc_ldapmod();
278 ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
279 ldm->mod_type = value;
284 if ( state == AWAITING_OP ) {
285 Debug( LDAP_DEBUG_ANY,
286 "Error: op_ldap_modify: unknown mod type \"%s\"\n",
292 * We should have an attribute: value pair here.
293 * Construct the mod_bvalues part of the ldapmod struct.
295 if ( strcasecmp( type, ldm->mod_type )) {
296 Debug( LDAP_DEBUG_ANY,
297 "Error: malformed modify op, %s: %s (expecting %s:)\n",
298 type, value, ldm->mod_type );
301 ldm->mod_bvalues = ( struct berval ** )
302 ch_realloc( ldm->mod_bvalues,
303 ( nvals + 2 ) * sizeof( struct berval * ));
304 ldm->mod_bvalues[ nvals + 1 ] = NULL;
305 ldm->mod_bvalues[ nvals ] = ( struct berval * )
306 ch_malloc( sizeof( struct berval ));
307 ldm->mod_bvalues[ nvals ]->bv_val = value;
308 ldm->mod_bvalues[ nvals ]->bv_len = len;
312 ldmarr[ nops ] = NULL;
315 /* Actually perform the LDAP operation */
316 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
317 ri->ri_hostname, ri->ri_port, re->re_dn );
318 rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
320 free_ldmarr( ldmarr );
328 * Perform an ldap delete operation.
339 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
340 ri->ri_hostname, ri->ri_port, re->re_dn );
341 rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
350 * Perform an ldap modrdn operation.
353 #define GOT_DRDNFLAGSTR 2
354 #define GOT_ALLNEWRDNFLAGS ( GOT_NEWRDN | GOT_DRDNFLAGSTR )
370 if ( re->re_mods == NULL ) {
371 *errmsg = "No arguments given";
372 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
378 * Get the arguments: should see newrdn: and deleteoldrdn: args.
380 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
381 if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
382 newrdn = mi[ i ].mi_val;
384 } else if ( !strcmp( mi[ i ].mi_type, T_DRDNFLAGSTR )) {
385 state |= GOT_DRDNFLAGSTR;
386 if ( !strcmp( mi[ i ].mi_val, "0" )) {
388 } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
391 Debug( LDAP_DEBUG_ANY,
392 "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
393 mi[ i ].mi_val, 0, 0 );
394 *errmsg = "Incorrect argument to deleteoldrdn";
398 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
399 mi[ i ].mi_type, 0, 0 );
400 *errmsg = "Bad value in replication log entry";
406 * Punt if we don't have all the args.
408 if ( state != GOT_ALLNEWRDNFLAGS ) {
409 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
411 *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
416 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
419 sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
420 buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
422 sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
423 Debug( LDAP_DEBUG_ARGS,
424 "replica %s - modify rdn %s (flag: %d)\n",
425 buf, buf2, drdnflag );
428 #endif /* LDAP_DEBUG */
431 rc = ldap_modrdn2_s( ri->ri_ldp, re->re_dn, mi->mi_val, drdnflag );
433 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
440 * Allocate and initialize an ldapmod struct.
443 alloc_ldapmod( void )
447 ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
448 ldm->mod_type = NULL;
449 ldm->mod_bvalues = ( struct berval ** ) NULL;
456 * Free an ldapmod struct associated mod_bvalues. NOTE - it is assumed
457 * that mod_bvalues and mod_type contain pointers to the same block of memory
458 * pointed to by the repl struct. Therefore, it's not freed here.
469 if ( ldm->mod_bvalues != NULL ) {
470 for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
471 free( ldm->mod_bvalues[ i ] );
473 free( ldm->mod_bvalues );
481 * Free an an array of LDAPMod pointers and the LDAPMod structs they point
490 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
491 free_ldapmod( ldmarr[ i ] );
498 * Create a berval with a single value.
500 static struct berval **
501 make_singlevalued_berval(
507 p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
508 p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
510 p[ 0 ]->bv_val = value;
511 p[ 0 ]->bv_len = len;
517 * Given a modification type (string), return an enumerated type.
518 * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
525 if ( !strcmp( type, T_MODSEPSTR )) {
528 if ( !strcmp( type, T_MODOPADDSTR )) {
529 return( T_MODOPADD );
531 if ( !strcmp( type, T_MODOPREPLACESTR )) {
532 return( T_MODOPREPLACE );
534 if ( !strcmp( type, T_MODOPDELETESTR )) {
535 return( T_MODOPDELETE );
542 * Perform an LDAP unbind operation. If replica is NULL, or the
543 * repl_ldp is NULL, just return LDAP_SUCCESS. Otherwise, unbind,
544 * set the ldp to NULL, and return the result of the unbind call.
551 int rc = LDAP_SUCCESS;
553 if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
554 rc = ldap_unbind( ri->ri_ldp );
555 if ( rc != LDAP_SUCCESS ) {
556 Debug( LDAP_DEBUG_ANY,
557 "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
558 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
568 * Perform an LDAP bind operation to the replication site given
569 * by replica. If replica->repl_ldp is non-NULL, then we unbind
570 * from the replica before rebinding. It should be safe to call
571 * this to re-connect if the replica's connection goes away
574 * Returns 0 on success, -1 if an LDAP error occurred, and a return
575 * code > 0 if some other error occurred, e.g. invalid bind method.
576 * If an LDAP error occurs, the LDAP error is returned in lderr.
590 char *skrbnames[ 2 ];
591 char realm[ REALM_SZ ];
592 char name[ ANAME_SZ ];
593 char instance[ INST_SZ ];
594 #endif /* HAVE_KERBEROS */
599 Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
600 return( BIND_ERR_BADRI );
603 if ( ri->ri_ldp != NULL ) {
604 ldrc = ldap_unbind( ri->ri_ldp );
605 if ( ldrc != LDAP_SUCCESS ) {
606 Debug( LDAP_DEBUG_ANY,
607 "Error: do_bind: ldap_unbind failed: %s\n",
608 ldap_err2string( ldrc ), 0, 0 );
613 Debug( LDAP_DEBUG_ARGS, "Open connection to %s:%d\n",
614 ri->ri_hostname, ri->ri_port, 0 );
615 ri->ri_ldp = ldap_open( ri->ri_hostname, ri->ri_port );
616 if ( ri->ri_ldp == NULL ) {
617 Debug( LDAP_DEBUG_ANY, "Error: ldap_open(%s, %d) failed: %s\n",
618 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
619 return( BIND_ERR_OPEN );
623 * Disable string translation if enabled by default.
624 * The replication log is written in the internal format,
625 * so this would do another translation, breaking havoc.
627 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
628 ri->ri_ldp->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
629 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
632 * Set ldap library options to (1) not follow referrals, and
633 * (2) restart the select() system call.
635 ldap_set_option(ri->ri_ldp, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
636 ldap_set_option(ri->ri_ldp, LDAP_OPT_RESTART, LDAP_OPT_ON);
638 switch ( ri->ri_bind_method ) {
640 #ifndef HAVE_KERBEROS
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 );
645 #else /* HAVE_KERBEROS */
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 /* HAVE_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.
765 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
767 Debug( LDAP_DEBUG_TRACE,
768 "Trace (%ld): *** ldmarr[ %d ] contents:\n",
769 (long) getpid(), i, 0 );
770 Debug( LDAP_DEBUG_TRACE,
771 "Trace (%ld): *** ldm->mod_op: %d\n",
772 (long) getpid(), ldm->mod_op, 0 );
773 Debug( LDAP_DEBUG_TRACE,
774 "Trace (%ld): *** ldm->mod_type: %s\n",
775 (long) getpid(), ldm->mod_type, 0 );
776 if ( ldm->mod_bvalues != NULL ) {
777 for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
778 msgbuf = ch_malloc( b->bv_len + 512 );
779 sprintf( msgbuf, "***** bv[ %d ] len = %ld, val = <%s>",
780 j, b->bv_len, b->bv_val );
781 Debug( LDAP_DEBUG_TRACE,
782 "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );
791 * Get the kerberos names from the binddn for "replica" via an ldap search.
792 * Returns a null-terminated array of char *, or NULL if the entry could
793 * not be found or there were no kerberosName attributes. The caller is
794 * responsible for freeing the returned array and strings it points to.
804 LDAPMessage *result, *entry;
806 /* First need to bind as NULL */
807 rc = ldap_simple_bind_s( ri->ri_ldp, NULL, NULL );
808 if ( rc != LDAP_SUCCESS ) {
809 Debug( LDAP_DEBUG_ANY,
810 "Error: null bind failed getting krbnames for %s:%d: %s\n",
811 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
814 rc = ldap_search_st( ri->ri_ldp, ri->ri_bind_dn, LDAP_SCOPE_BASE,
815 "objectclass=*", kattrs, 0, &kst, &result );
816 if ( rc != LDAP_SUCCESS ) {
817 Debug( LDAP_DEBUG_ANY,
818 "Error: search failed getting krbnames for %s:%d: %s\n",
819 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
822 ne = ldap_count_entries( ri->ri_ldp, result );
824 Debug( LDAP_DEBUG_ANY,
825 "Error: Can't find entry \"%s\" for %s:%d kerberos bind\n",
826 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
830 Debug( LDAP_DEBUG_ANY,
831 "Error: Kerberos binddn \"%s\" for %s:%dis ambiguous\n",
832 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
835 entry = ldap_first_entry( ri->ri_ldp, result );
836 if ( entry == NULL ) {
837 Debug( LDAP_DEBUG_ANY,
838 "Error: Can't find \"%s\" for kerberos binddn for %s:%d\n",
839 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
842 krbnames = ldap_get_values( ri->ri_ldp, entry, "kerberosName" );
843 ldap_msgfree( result );
859 for ( p = s; ( p != NULL ) && ( *p != '\0' ); p++ ) {
860 *p = TOUPPER( (unsigned char) *p );