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
17 #define DISABLE_BRIDGE
21 #include <ac/string.h>
23 #include <sys/types.h>
27 #include <kerberosIV/krb.h>
30 #endif /* KERBEROS_V */
38 /* Forward references */
39 static int get_changetype LDAP_P(( char * ));
40 static struct berval **make_singlevalued_berval LDAP_P(( char *, int ));
41 static int op_ldap_add LDAP_P(( Ri *, Re *, char ** ));
42 static int op_ldap_modify LDAP_P(( Ri *, Re *, char ** ));
43 static int op_ldap_delete LDAP_P(( Ri *, Re *, char ** ));
44 static int op_ldap_modrdn LDAP_P(( Ri *, Re *, char ** ));
45 static LDAPMod *alloc_ldapmod LDAP_P(());
46 static void free_ldapmod LDAP_P(( LDAPMod * ));
47 static void free_ldmarr LDAP_P(( LDAPMod ** ));
48 static int getmodtype LDAP_P(( char * ));
49 static void dump_ldm_array LDAP_P(( LDAPMod ** ));
50 static char **read_krbnames LDAP_P(( Ri * ));
51 static void upcase LDAP_P(( char * ));
52 static int do_bind LDAP_P(( Ri *, int * ));
53 static int do_unbind LDAP_P(( Ri * ));
56 /* External references */
57 #ifdef DECL_SYS_ERRLIST
58 extern char *sys_errlist[];
59 #endif /* DECL_SYS_ERRLIST */
61 extern char *ch_malloc( unsigned long );
63 static char *kattrs[] = {"kerberosName", NULL };
64 static struct timeval kst = {30L, 0L};
69 * Determine the type of ldap operation being performed and call the
70 * appropriate routine.
71 * - If successful, returns ERR_DO_LDAP_OK
72 * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
73 * The caller should wait a while and retry the operation.
74 * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned. The caller
75 * should reject the operation and continue with the next replication
86 int lderr = LDAP_SUCCESS;
93 if ( ri->ri_ldp == NULL ) {
94 rc = do_bind( ri, &lderr );
95 if ( rc != BIND_OK ) {
96 return DO_LDAP_ERR_RETRYABLE;
100 switch ( re->re_changetype ) {
102 lderr = op_ldap_add( ri, re, errmsg );
103 if ( lderr != LDAP_SUCCESS ) {
104 Debug( LDAP_DEBUG_ANY,
105 "Error: ldap_add_s failed adding \"%s\": %s\n",
106 *errmsg ? *errmsg : ldap_err2string( lderr ),
111 lderr = op_ldap_modify( ri, re, errmsg );
112 if ( lderr != LDAP_SUCCESS ) {
113 Debug( LDAP_DEBUG_ANY,
114 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
115 *errmsg ? *errmsg : ldap_err2string( lderr ),
120 lderr = op_ldap_delete( ri, re, errmsg );
121 if ( lderr != LDAP_SUCCESS ) {
122 Debug( LDAP_DEBUG_ANY,
123 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
124 *errmsg ? *errmsg : ldap_err2string( lderr ),
129 lderr = op_ldap_modrdn( ri, re, errmsg );
130 if ( lderr != LDAP_SUCCESS ) {
131 Debug( LDAP_DEBUG_ANY,
132 "Error: ldap_modrdn_s failed modifying %s: %s\n",
133 *errmsg ? *errmsg : ldap_err2string( lderr ),
138 Debug( LDAP_DEBUG_ANY,
139 "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
140 re->re_changetype, re->re_dn, 0 );
141 return DO_LDAP_ERR_FATAL;
145 * Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
146 * we may have been idle long enough that the remote slapd timed
147 * us out. Rebind and try again.
149 if ( lderr == LDAP_SUCCESS ) {
151 } else if ( lderr == LDAP_SERVER_DOWN ) {
152 /* The LDAP server may have timed us out - rebind and try again */
153 (void) do_unbind( ri );
156 return DO_LDAP_ERR_FATAL;
159 return DO_LDAP_ERR_FATAL;
166 * Perform an ldap add operation.
176 int nattrs, rc = 0, i;
177 LDAPMod *ldm, **ldmarr;
184 * Construct a null-terminated array of LDAPMod structs.
187 while ( mi[ i ].mi_type != NULL ) {
188 ldm = alloc_ldapmod();
189 ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
190 ( nattrs + 2 ) * sizeof( LDAPMod * ));
191 ldmarr[ nattrs ] = ldm;
192 ldm->mod_op = LDAP_MOD_BVALUES;
193 ldm->mod_type = mi[ i ].mi_type;
195 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
200 if ( ldmarr != NULL ) {
201 ldmarr[ nattrs ] = NULL;
203 /* Perform the operation */
204 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
205 ri->ri_hostname, ri->ri_port, re->re_dn );
206 rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
207 lderr = ri->ri_ldp->ld_errno;
209 *errmsg = "No modifications to do";
210 Debug( LDAP_DEBUG_ANY,
211 "Error: op_ldap_add: no mods to do (%s)!", re->re_dn, 0, 0 );
213 free_ldmarr( ldmarr );
221 * Perform an ldap modify operation.
223 #define AWAITING_OP -1
232 int state; /* This code is a simple-minded state machine */
233 int nvals; /* Number of values we're modifying */
234 int nops; /* Number of LDAPMod structs in ldmarr */
235 LDAPMod *ldm, *nldm, **ldmarr;
245 if ( re->re_mods == NULL ) {
246 *errmsg = "No arguments given";
247 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
253 * Construct a null-terminated array of LDAPMod structs.
255 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
256 type = mi[ i ].mi_type;
257 value = mi[ i ].mi_val;
258 len = mi[ i ].mi_len;
259 switch ( getmodtype( type )) {
261 state = T_MODSEP; /* Got a separator line "-\n" */
265 ldmarr = ( LDAPMod ** )
266 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
267 ldmarr[ nops ] = ldm = alloc_ldapmod();
268 ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
269 ldm->mod_type = value;
274 state = T_MODOPREPLACE;
275 ldmarr = ( LDAPMod ** )
276 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
277 ldmarr[ nops ] = ldm = alloc_ldapmod();
278 ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
279 ldm->mod_type = value;
284 state = T_MODOPDELETE;
285 ldmarr = ( LDAPMod ** )
286 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
287 ldmarr[ nops ] = ldm = alloc_ldapmod();
288 ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
289 ldm->mod_type = value;
294 if ( state == AWAITING_OP ) {
295 Debug( LDAP_DEBUG_ANY,
296 "Error: op_ldap_modify: unknown mod type \"%s\"\n",
302 * We should have an attribute: value pair here.
303 * Construct the mod_bvalues part of the ldapmod struct.
305 if ( strcasecmp( type, ldm->mod_type )) {
306 Debug( LDAP_DEBUG_ANY,
307 "Error: malformed modify op, %s: %s (expecting %s:)\n",
308 type, value, ldm->mod_type );
311 ldm->mod_bvalues = ( struct berval ** )
312 ch_realloc( ldm->mod_bvalues,
313 ( nvals + 2 ) * sizeof( struct berval * ));
314 ldm->mod_bvalues[ nvals + 1 ] = NULL;
315 ldm->mod_bvalues[ nvals ] = ( struct berval * )
316 ch_malloc( sizeof( struct berval ));
317 ldm->mod_bvalues[ nvals ]->bv_val = value;
318 ldm->mod_bvalues[ nvals ]->bv_len = len;
322 ldmarr[ nops ] = NULL;
325 /* Actually perform the LDAP operation */
326 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
327 ri->ri_hostname, ri->ri_port, re->re_dn );
328 rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
330 free_ldmarr( ldmarr );
338 * Perform an ldap delete operation.
349 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
350 ri->ri_hostname, ri->ri_port, re->re_dn );
351 rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
360 * Perform an ldap modrdn operation.
363 #define GOT_DRDNFLAGSTR 2
364 #define GOT_ALLNEWRDNFLAGS ( GOT_NEWRDN | GOT_DRDNFLAGSTR )
379 if ( re->re_mods == NULL ) {
380 *errmsg = "No arguments given";
381 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
387 * Get the arguments: should see newrdn: and deleteoldrdn: args.
389 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
390 if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
391 newrdn = mi[ i ].mi_val;
393 } else if ( !strcmp( mi[ i ].mi_type, T_DRDNFLAGSTR )) {
394 state |= GOT_DRDNFLAGSTR;
395 if ( !strcmp( mi[ i ].mi_val, "0" )) {
397 } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
400 Debug( LDAP_DEBUG_ANY,
401 "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
402 mi[ i ].mi_val, 0, 0 );
403 *errmsg = "Incorrect argument to deleteoldrdn";
407 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
408 mi[ i ].mi_type, 0, 0 );
409 *errmsg = "Bad value in replication log entry";
415 * Punt if we don't have all the args.
417 if ( state != GOT_ALLNEWRDNFLAGS ) {
418 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
420 *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
425 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
428 sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
429 buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
431 sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
432 Debug( LDAP_DEBUG_ARGS,
433 "replica %s - modify rdn %s (flag: %d)\n",
434 buf, buf2, drdnflag );
437 #endif /* LDAP_DEBUG */
440 rc = ldap_modrdn2_s( ri->ri_ldp, re->re_dn, mi->mi_val, drdnflag );
442 return( ri->ri_ldp->ld_errno );
448 * Allocate and initialize an ldapmod struct.
455 ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
456 ldm->mod_type = NULL;
457 ldm->mod_bvalues = ( struct berval ** ) NULL;
464 * Free an ldapmod struct associated mod_bvalues. NOTE - it is assumed
465 * that mod_bvalues and mod_type contain pointers to the same block of memory
466 * pointed to by the repl struct. Therefore, it's not freed here.
477 if ( ldm->mod_bvalues != NULL ) {
478 for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
479 free( ldm->mod_bvalues[ i ] );
481 free( ldm->mod_bvalues );
489 * Free an an array of LDAPMod pointers and the LDAPMod structs they point
498 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
499 free_ldapmod( ldmarr[ i ] );
506 * Create a berval with a single value.
508 static struct berval **
509 make_singlevalued_berval(
515 p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
516 p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
518 p[ 0 ]->bv_val = value;
519 p[ 0 ]->bv_len = len;
525 * Given a modification type (string), return an enumerated type.
526 * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
533 if ( !strcmp( type, T_MODSEPSTR )) {
536 if ( !strcmp( type, T_MODOPADDSTR )) {
537 return( T_MODOPADD );
539 if ( !strcmp( type, T_MODOPREPLACESTR )) {
540 return( T_MODOPREPLACE );
542 if ( !strcmp( type, T_MODOPDELETESTR )) {
543 return( T_MODOPDELETE );
550 * Perform an LDAP unbind operation. If replica is NULL, or the
551 * repl_ldp is NULL, just return LDAP_SUCCESS. Otherwise, unbind,
552 * set the ldp to NULL, and return the result of the unbind call.
559 int rc = LDAP_SUCCESS;
561 if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
562 rc = ldap_unbind( ri->ri_ldp );
563 if ( rc != LDAP_SUCCESS ) {
564 Debug( LDAP_DEBUG_ANY,
565 "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
566 ldap_err2string( rc ), ri->ri_hostname, ri->ri_port );
576 * Perform an LDAP bind operation to the replication site given
577 * by replica. If replica->repl_ldp is non-NULL, then we unbind
578 * from the replica before rebinding. It should be safe to call
579 * this to re-connect if the replica's connection goes away
582 * Returns 0 on success, -1 if an LDAP error occurred, and a return
583 * code > 0 if some other error occurred, e.g. invalid bind method.
584 * If an LDAP error occurs, the LDAP error is returned in lderr.
599 char *skrbnames[ 2 ];
600 char realm[ REALM_SZ ];
601 char name[ ANAME_SZ ];
602 char instance[ INST_SZ ];
603 #endif /* KERBEROS */
608 Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
609 return( BIND_ERR_BADRI );
612 if ( ri->ri_ldp != NULL ) {
613 ldrc = ldap_unbind( ri->ri_ldp );
614 if ( ldrc != LDAP_SUCCESS ) {
615 Debug( LDAP_DEBUG_ANY,
616 "Error: do_bind: ldap_unbind failed: %s\n",
617 ldap_err2string( ldrc ), 0, 0 );
622 Debug( LDAP_DEBUG_ARGS, "Open connection to %s:%d\n",
623 ri->ri_hostname, ri->ri_port, 0 );
624 ri->ri_ldp = ldap_open( ri->ri_hostname, ri->ri_port );
625 if ( ri->ri_ldp == NULL ) {
626 Debug( LDAP_DEBUG_ANY, "Error: ldap_open(%s, %d) failed: %s\n",
627 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
628 return( BIND_ERR_OPEN );
632 * Set ldap library options to (1) not follow referrals, and
633 * (2) restart the select() system call.
635 #ifdef LDAP_REFERRALS
636 ri->ri_ldp->ld_options &= ~LDAP_OPT_REFERRALS;
637 #endif /* LDAP_REFERRALS */
638 ri->ri_ldp->ld_options |= LDAP_OPT_RESTART;
640 switch ( ri->ri_bind_method ) {
643 Debug( LDAP_DEBUG_ANY,
644 "Error: Kerberos bind for %s:%d, but not compiled w/kerberos\n",
645 ri->ri_hostname, ri->ri_port, 0 );
646 return( BIND_ERR_KERBEROS_FAILED );
649 * Bind using kerberos.
650 * If "bindprincipal" was given in the config file, then attempt
651 * to get a TGT for that principal (via the srvtab file). If only
652 * a binddn was given, then we need to read that entry to get
653 * the kerberosName attributes, and try to get a TGT for one
654 * of them. All are tried. The first one which succeeds is
655 * returned. XXX It might be a good idea to just require a
656 * bindprincipal. Reading the entry every time might be a significant
657 * amount of overhead, if the connection is closed between most
661 if ( ri->ri_principal != NULL ) {
662 skrbnames[ 0 ] = ri->ri_principal;
663 skrbnames[ 1 ] = NULL;
664 krbnames = skrbnames;
666 krbnames = read_krbnames( ri );
669 if (( krbnames == NULL ) || ( krbnames[ 0 ] == NULL )) {
670 Debug( LDAP_DEBUG_ANY,
671 "Error: Can't find krbname for binddn \"%s\"\n",
672 ri->ri_bind_dn, 0, 0 );
673 retval = BIND_ERR_KERBEROS_FAILED;
677 * Now we've got one or more kerberos principals. See if any
678 * of them are in the srvtab file.
681 for ( kni = 0; krbnames[ kni ] != NULL; kni++ ) {
682 rc = kname_parse( name, instance, realm, krbnames[ kni ]);
683 if ( rc != KSUCCESS ) {
687 rc = krb_get_svc_in_tkt( name, instance, realm, "krbtgt", realm,
689 if ( rc != KSUCCESS) {
690 Debug( LDAP_DEBUG_ANY, "Error: Can't get TGT for %s: %s\n",
691 krbnames[ kni ], krb_err_txt[ rc ], 0 );
698 Debug( LDAP_DEBUG_ANY,
699 "Error: Could not obtain TGT for DN \"%s\"\n",
700 ri->ri_bind_dn, 0, 0 );
701 retval = BIND_ERR_KERBEROS_FAILED;
705 * We've got a TGT. Do a kerberos bind.
707 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (kerberos)\n",
708 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
709 ldrc = ldap_kerberos_bind_s( ri->ri_ldp, ri->ri_bind_dn );
710 ri->ri_principal = strdup( krbnames[ kni ] );
711 if ( ldrc != LDAP_SUCCESS ) {
712 Debug( LDAP_DEBUG_ANY, "Error: kerberos bind for %s:%dfailed: %s\n",
713 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
715 retval = BIND_ERR_KERBEROS_FAILED;
718 kexit: if ( krbnames != NULL ) {
719 ldap_value_free( krbnames );
723 #endif /* KERBEROS */
726 * Bind with a plaintext password.
728 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
729 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
730 ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
732 if ( ldrc != LDAP_SUCCESS ) {
733 Debug( LDAP_DEBUG_ANY,
734 "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
735 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
737 return( BIND_ERR_SIMPLE_FAILED );
743 Debug( LDAP_DEBUG_ANY,
744 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
745 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
746 return( BIND_ERR_BAD_ATYPE );
755 * For debugging. Print the contents of an ldmarr array.
766 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
768 Debug( LDAP_DEBUG_TRACE,
769 "Trace (%d): *** ldmarr[ %d ] contents:\n",
771 Debug( LDAP_DEBUG_TRACE,
772 "Trace (%d): *** ldm->mod_op: %d\n",
773 getpid(), ldm->mod_op, 0 );
774 Debug( LDAP_DEBUG_TRACE,
775 "Trace (%d): *** ldm->mod_type: %s\n",
776 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 = %d, val = <%s>",
781 j, b->bv_len, b->bv_val );
782 Debug( LDAP_DEBUG_TRACE,
783 "Trace (%d):%s\n", 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 );
859 for ( p = s; ( p != NULL ) && ( *p != '\0' ); p++ ) {
860 if ( islower( *p )) {