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>
32 /* Forward references */
33 static int get_changetype( char * );
34 static struct berval **make_singlevalued_berval( char *, int );
35 static int op_ldap_add( Ri *, Re *, char ** );
36 static int op_ldap_modify( Ri *, Re *, char ** );
37 static int op_ldap_delete( Ri *, Re *, char ** );
38 static int op_ldap_modrdn( Ri *, Re *, char ** );
39 static LDAPMod *alloc_ldapmod();
40 static void free_ldapmod( LDAPMod * );
41 static void free_ldmarr( LDAPMod ** );
42 static int getmodtype( char * );
43 static void dump_ldm_array( LDAPMod ** );
44 static char **read_krbnames( Ri * );
45 static void upcase( char * );
46 static int do_bind( Ri *, int * );
47 static int do_unbind( Ri * );
50 /* External references */
51 #ifndef SYSERRLIST_IN_STDIO
52 extern char *sys_errlist[];
53 #endif /* SYSERRLIST_IN_STDIO */
55 extern char *ch_malloc( unsigned long );
57 static char *kattrs[] = {"kerberosName", NULL };
58 static struct timeval kst = {30L, 0L};
63 * Determine the type of ldap operation being performed and call the
64 * appropriate routine.
65 * - If successful, returns ERR_DO_LDAP_OK
66 * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
67 * The caller should wait a while and retry the operation.
68 * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned. The caller
69 * should reject the operation and continue with the next replication
80 int lderr = LDAP_SUCCESS;
87 if ( ri->ri_ldp == NULL ) {
88 rc = do_bind( ri, &lderr );
89 if ( rc != BIND_OK ) {
90 return DO_LDAP_ERR_RETRYABLE;
94 switch ( re->re_changetype ) {
96 lderr = op_ldap_add( ri, re, errmsg );
97 if ( lderr != LDAP_SUCCESS ) {
98 Debug( LDAP_DEBUG_ANY,
99 "Error: ldap_add_s failed adding \"%s\": %s\n",
100 *errmsg ? *errmsg : ldap_err2string( lderr ),
105 lderr = op_ldap_modify( ri, re, errmsg );
106 if ( lderr != LDAP_SUCCESS ) {
107 Debug( LDAP_DEBUG_ANY,
108 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
109 *errmsg ? *errmsg : ldap_err2string( lderr ),
114 lderr = op_ldap_delete( ri, re, errmsg );
115 if ( lderr != LDAP_SUCCESS ) {
116 Debug( LDAP_DEBUG_ANY,
117 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
118 *errmsg ? *errmsg : ldap_err2string( lderr ),
123 lderr = op_ldap_modrdn( ri, re, errmsg );
124 if ( lderr != LDAP_SUCCESS ) {
125 Debug( LDAP_DEBUG_ANY,
126 "Error: ldap_modrdn_s failed modifying %s: %s\n",
127 *errmsg ? *errmsg : ldap_err2string( lderr ),
132 Debug( LDAP_DEBUG_ANY,
133 "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
134 re->re_changetype, re->re_dn, 0 );
135 return DO_LDAP_ERR_FATAL;
139 * Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
140 * we may have been idle long enough that the remote slapd timed
141 * us out. Rebind and try again.
143 if ( lderr == LDAP_SUCCESS ) {
145 } else if ( lderr == LDAP_SERVER_DOWN ) {
146 /* The LDAP server may have timed us out - rebind and try again */
147 (void) do_unbind( ri );
150 return DO_LDAP_ERR_FATAL;
153 return DO_LDAP_ERR_FATAL;
160 * Perform an ldap add operation.
170 int nattrs, rc = 0, i;
171 LDAPMod *ldm, **ldmarr;
178 * Construct a null-terminated array of LDAPMod structs.
181 while ( mi[ i ].mi_type != NULL ) {
182 ldm = alloc_ldapmod();
183 ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
184 ( nattrs + 2 ) * sizeof( LDAPMod * ));
185 ldmarr[ nattrs ] = ldm;
186 ldm->mod_op = LDAP_MOD_BVALUES;
187 ldm->mod_type = mi[ i ].mi_type;
189 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
194 if ( ldmarr != NULL ) {
195 ldmarr[ nattrs ] = NULL;
197 /* Perform the operation */
198 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
199 ri->ri_hostname, ri->ri_port, re->re_dn );
200 rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
201 lderr = ri->ri_ldp->ld_errno;
203 *errmsg = "No modifications to do";
204 Debug( LDAP_DEBUG_ANY,
205 "Error: op_ldap_add: no mods to do (%s)!", re->re_dn, 0, 0 );
207 free_ldmarr( ldmarr );
215 * Perform an ldap modify operation.
217 #define AWAITING_OP -1
226 int state; /* This code is a simple-minded state machine */
227 int nvals; /* Number of values we're modifying */
228 int nops; /* Number of LDAPMod structs in ldmarr */
229 LDAPMod *ldm, *nldm, **ldmarr;
239 if ( re->re_mods == NULL ) {
240 *errmsg = "No arguments given";
241 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
247 * Construct a null-terminated array of LDAPMod structs.
249 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
250 type = mi[ i ].mi_type;
251 value = mi[ i ].mi_val;
252 len = mi[ i ].mi_len;
253 switch ( getmodtype( type )) {
255 state = T_MODSEP; /* Got a separator line "-\n" */
259 ldmarr = ( LDAPMod ** )
260 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
261 ldmarr[ nops ] = ldm = alloc_ldapmod();
262 ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
263 ldm->mod_type = value;
268 state = T_MODOPREPLACE;
269 ldmarr = ( LDAPMod ** )
270 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
271 ldmarr[ nops ] = ldm = alloc_ldapmod();
272 ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
273 ldm->mod_type = value;
278 state = T_MODOPDELETE;
279 ldmarr = ( LDAPMod ** )
280 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
281 ldmarr[ nops ] = ldm = alloc_ldapmod();
282 ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
283 ldm->mod_type = value;
288 if ( state == AWAITING_OP ) {
289 Debug( LDAP_DEBUG_ANY,
290 "Error: op_ldap_modify: unknown mod type \"%s\"\n",
296 * We should have an attribute: value pair here.
297 * Construct the mod_bvalues part of the ldapmod struct.
299 if ( strcasecmp( type, ldm->mod_type )) {
300 Debug( LDAP_DEBUG_ANY,
301 "Error: malformed modify op, %s: %s (expecting %s:)\n",
302 type, value, ldm->mod_type );
305 ldm->mod_bvalues = ( struct berval ** )
306 ch_realloc( ldm->mod_bvalues,
307 ( nvals + 2 ) * sizeof( struct berval * ));
308 ldm->mod_bvalues[ nvals + 1 ] = NULL;
309 ldm->mod_bvalues[ nvals ] = ( struct berval * )
310 ch_malloc( sizeof( struct berval ));
311 ldm->mod_bvalues[ nvals ]->bv_val = value;
312 ldm->mod_bvalues[ nvals ]->bv_len = len;
316 ldmarr[ nops ] = NULL;
319 /* Actually perform the LDAP operation */
320 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
321 ri->ri_hostname, ri->ri_port, re->re_dn );
322 rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
324 free_ldmarr( ldmarr );
332 * Perform an ldap delete operation.
343 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
344 ri->ri_hostname, ri->ri_port, re->re_dn );
345 rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
354 * Perform an ldap modrdn operation.
357 #define GOT_DRDNFLAGSTR 2
358 #define GOT_ALLNEWRDNFLAGS ( GOT_NEWRDN | GOT_DRDNFLAGSTR )
373 if ( re->re_mods == NULL ) {
374 *errmsg = "No arguments given";
375 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
381 * Get the arguments: should see newrdn: and deleteoldrdn: args.
383 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
384 if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
385 newrdn = mi[ i ].mi_val;
387 } else if ( !strcmp( mi[ i ].mi_type, T_DRDNFLAGSTR )) {
388 state |= GOT_DRDNFLAGSTR;
389 if ( !strcmp( mi[ i ].mi_val, "0" )) {
391 } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
394 Debug( LDAP_DEBUG_ANY,
395 "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
396 mi[ i ].mi_val, 0, 0 );
397 *errmsg = "Incorrect argument to deleteoldrdn";
401 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
402 mi[ i ].mi_type, 0, 0 );
403 *errmsg = "Bad value in replication log entry";
409 * Punt if we don't have all the args.
411 if ( state != GOT_ALLNEWRDNFLAGS ) {
412 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
414 *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
419 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
422 sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
423 buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
425 sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
426 Debug( LDAP_DEBUG_ARGS,
427 "replica %s - modify rdn %s (flag: %d)\n",
428 buf, buf2, drdnflag );
431 #endif /* LDAP_DEBUG */
434 rc = ldap_modrdn2_s( ri->ri_ldp, re->re_dn, mi->mi_val, drdnflag );
436 return( ri->ri_ldp->ld_errno );
442 * Allocate and initialize an ldapmod struct.
449 ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
450 ldm->mod_type = NULL;
451 ldm->mod_bvalues = ( struct berval ** ) NULL;
458 * Free an ldapmod struct associated mod_bvalues. NOTE - it is assumed
459 * that mod_bvalues and mod_type contain pointers to the same block of memory
460 * pointed to by the repl struct. Therefore, it's not freed here.
471 if ( ldm->mod_bvalues != NULL ) {
472 for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
473 free( ldm->mod_bvalues[ i ] );
475 free( ldm->mod_bvalues );
483 * Free an an array of LDAPMod pointers and the LDAPMod structs they point
492 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
493 free_ldapmod( ldmarr[ i ] );
500 * Create a berval with a single value.
502 static struct berval **
503 make_singlevalued_berval(
509 p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
510 p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
512 p[ 0 ]->bv_val = value;
513 p[ 0 ]->bv_len = len;
519 * Given a modification type (string), return an enumerated type.
520 * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
527 if ( !strcmp( type, T_MODSEPSTR )) {
530 if ( !strcmp( type, T_MODOPADDSTR )) {
531 return( T_MODOPADD );
533 if ( !strcmp( type, T_MODOPREPLACESTR )) {
534 return( T_MODOPREPLACE );
536 if ( !strcmp( type, T_MODOPDELETESTR )) {
537 return( T_MODOPDELETE );
544 * Perform an LDAP unbind operation. If replica is NULL, or the
545 * repl_ldp is NULL, just return LDAP_SUCCESS. Otherwise, unbind,
546 * set the ldp to NULL, and return the result of the unbind call.
553 int rc = LDAP_SUCCESS;
555 if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
556 rc = ldap_unbind( ri->ri_ldp );
557 if ( rc != LDAP_SUCCESS ) {
558 Debug( LDAP_DEBUG_ANY,
559 "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
560 ldap_err2string( rc ), ri->ri_hostname, ri->ri_port );
570 * Perform an LDAP bind operation to the replication site given
571 * by replica. If replica->repl_ldp is non-NULL, then we unbind
572 * from the replica before rebinding. It should be safe to call
573 * this to re-connect if the replica's connection goes away
576 * Returns 0 on success, -1 if an LDAP error occurred, and a return
577 * code > 0 if some other error occurred, e.g. invalid bind method.
578 * If an LDAP error occurs, the LDAP error is returned in lderr.
593 char *skrbnames[ 2 ];
594 char realm[ REALM_SZ ];
595 char name[ ANAME_SZ ];
596 char instance[ INST_SZ ];
597 #endif /* KERBEROS */
602 Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
603 return( BIND_ERR_BADRI );
606 if ( ri->ri_ldp != NULL ) {
607 ldrc = ldap_unbind( ri->ri_ldp );
608 if ( ldrc != LDAP_SUCCESS ) {
609 Debug( LDAP_DEBUG_ANY,
610 "Error: do_bind: ldap_unbind failed: %s\n",
611 ldap_err2string( ldrc ), 0, 0 );
616 Debug( LDAP_DEBUG_ARGS, "Open connection to %s:%d\n",
617 ri->ri_hostname, ri->ri_port, 0 );
618 ri->ri_ldp = ldap_open( ri->ri_hostname, ri->ri_port );
619 if ( ri->ri_ldp == NULL ) {
620 Debug( LDAP_DEBUG_ANY, "Error: ldap_open(%s, %d) failed: %s\n",
621 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
622 return( BIND_ERR_OPEN );
626 * Set ldap library options to (1) not follow referrals, and
627 * (2) restart the select() system call.
629 #ifdef LDAP_REFERRALS
630 ri->ri_ldp->ld_options &= ~LDAP_OPT_REFERRALS;
631 #endif /* LDAP_REFERRALS */
632 ri->ri_ldp->ld_options |= LDAP_OPT_RESTART;
634 switch ( ri->ri_bind_method ) {
637 Debug( LDAP_DEBUG_ANY,
638 "Error: Kerberos bind for %s:%d, but not compiled w/kerberos\n",
639 ri->ri_hostname, ri->ri_port, 0 );
640 return( BIND_ERR_KERBEROS_FAILED );
643 * Bind using kerberos.
644 * If "bindprincipal" was given in the config file, then attempt
645 * to get a TGT for that principal (via the srvtab file). If only
646 * a binddn was given, then we need to read that entry to get
647 * the kerberosName attributes, and try to get a TGT for one
648 * of them. All are tried. The first one which succeeds is
649 * returned. XXX It might be a good idea to just require a
650 * bindprincipal. Reading the entry every time might be a significant
651 * amount of overhead, if the connection is closed between most
655 if ( ri->ri_principal != NULL ) {
656 skrbnames[ 0 ] = ri->ri_principal;
657 skrbnames[ 1 ] = NULL;
658 krbnames = skrbnames;
660 krbnames = read_krbnames( ri );
663 if (( krbnames == NULL ) || ( krbnames[ 0 ] == NULL )) {
664 Debug( LDAP_DEBUG_ANY,
665 "Error: Can't find krbname for binddn \"%s\"\n",
666 ri->ri_bind_dn, 0, 0 );
667 retval = BIND_ERR_KERBEROS_FAILED;
671 * Now we've got one or more kerberos principals. See if any
672 * of them are in the srvtab file.
675 for ( kni = 0; krbnames[ kni ] != NULL; kni++ ) {
676 rc = kname_parse( name, instance, realm, krbnames[ kni ]);
677 if ( rc != KSUCCESS ) {
681 rc = krb_get_svc_in_tkt( name, instance, realm, "krbtgt", realm,
683 if ( rc != KSUCCESS) {
684 Debug( LDAP_DEBUG_ANY, "Error: Can't get TGT for %s: %s\n",
685 krbnames[ kni ], krb_err_txt[ rc ], 0 );
692 Debug( LDAP_DEBUG_ANY,
693 "Error: Could not obtain TGT for DN \"%s\"\n",
694 ri->ri_bind_dn, 0, 0 );
695 retval = BIND_ERR_KERBEROS_FAILED;
699 * We've got a TGT. Do a kerberos bind.
701 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (kerberos)\n",
702 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
703 ldrc = ldap_kerberos_bind_s( ri->ri_ldp, ri->ri_bind_dn );
704 ri->ri_principal = strdup( krbnames[ kni ] );
705 if ( ldrc != LDAP_SUCCESS ) {
706 Debug( LDAP_DEBUG_ANY, "Error: kerberos bind for %s:%dfailed: %s\n",
707 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
709 retval = BIND_ERR_KERBEROS_FAILED;
712 kexit: if ( krbnames != NULL ) {
713 ldap_value_free( krbnames );
717 #endif /* KERBEROS */
720 * Bind with a plaintext password.
722 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
723 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
724 ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
726 if ( ldrc != LDAP_SUCCESS ) {
727 Debug( LDAP_DEBUG_ANY,
728 "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
729 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
731 return( BIND_ERR_SIMPLE_FAILED );
737 Debug( LDAP_DEBUG_ANY,
738 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
739 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
740 return( BIND_ERR_BAD_ATYPE );
749 * For debugging. Print the contents of an ldmarr array.
760 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
762 Debug( LDAP_DEBUG_TRACE,
763 "Trace (%d): *** ldmarr[ %d ] contents:\n",
765 Debug( LDAP_DEBUG_TRACE,
766 "Trace (%d): *** ldm->mod_op: %d\n",
767 getpid(), ldm->mod_op, 0 );
768 Debug( LDAP_DEBUG_TRACE,
769 "Trace (%d): *** ldm->mod_type: %s\n",
770 getpid(), ldm->mod_type, 0 );
771 if ( ldm->mod_bvalues != NULL ) {
772 for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
773 msgbuf = ch_malloc( b->bv_len + 512 );
774 sprintf( msgbuf, "***** bv[ %d ] len = %d, val = <%s>",
775 j, b->bv_len, b->bv_val );
776 Debug( LDAP_DEBUG_TRACE,
777 "Trace (%d):%s\n", getpid(), msgbuf, 0 );
786 * Get the kerberos names from the binddn for "replica" via an ldap search.
787 * Returns a null-terminated array of char *, or NULL if the entry could
788 * not be found or there were no kerberosName attributes. The caller is
789 * responsible for freeing the returned array and strings it points to.
799 LDAPMessage *result, *entry;
801 /* First need to bind as NULL */
802 rc = ldap_simple_bind_s( ri->ri_ldp, NULL, NULL );
803 if ( rc != LDAP_SUCCESS ) {
804 Debug( LDAP_DEBUG_ANY,
805 "Error: null bind failed getting krbnames for %s:%d: %s\n",
806 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
809 rc = ldap_search_st( ri->ri_ldp, ri->ri_bind_dn, LDAP_SCOPE_BASE,
810 "objectclass=*", kattrs, 0, &kst, &result );
811 if ( rc != LDAP_SUCCESS ) {
812 Debug( LDAP_DEBUG_ANY,
813 "Error: search failed getting krbnames for %s:%d: %s\n",
814 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
817 ne = ldap_count_entries( ri->ri_ldp, result );
819 Debug( LDAP_DEBUG_ANY,
820 "Error: Can't find entry \"%s\" for %s:%d kerberos bind\n",
821 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
825 Debug( LDAP_DEBUG_ANY,
826 "Error: Kerberos binddn \"%s\" for %s:%dis ambiguous\n",
827 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
830 entry = ldap_first_entry( ri->ri_ldp, result );
831 if ( entry == NULL ) {
832 Debug( LDAP_DEBUG_ANY,
833 "Error: Can't find \"%s\" for kerberos binddn for %s:%d\n",
834 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
837 krbnames = ldap_get_values( ri->ri_ldp, entry, "kerberosName" );
838 ldap_msgfree( result );
853 for ( p = s; ( p != NULL ) && ( *p != '\0' ); p++ ) {
854 if ( islower( *p )) {