3 * Copyright (c) 1996 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
15 * ldap_op.c - routines to perform LDAP operations
22 #include <ac/stdlib.h>
25 #include <ac/string.h>
28 #include <ac/unistd.h>
31 #include "lutil_ldap.h"
34 /* Forward references */
35 static struct berval **make_singlevalued_berval LDAP_P(( char *, int ));
36 static int op_ldap_add LDAP_P(( Ri *, Re *, char ** ));
37 static int op_ldap_modify LDAP_P(( Ri *, Re *, char ** ));
38 static int op_ldap_delete LDAP_P(( Ri *, Re *, char ** ));
39 static int op_ldap_modrdn LDAP_P(( Ri *, Re *, char ** ));
40 static LDAPMod *alloc_ldapmod LDAP_P(( void ));
41 static void free_ldapmod LDAP_P(( LDAPMod * ));
42 static void free_ldmarr LDAP_P(( LDAPMod ** ));
43 static int getmodtype LDAP_P(( char * ));
44 static void dump_ldm_array LDAP_P(( LDAPMod ** ));
45 static int do_bind LDAP_P(( Ri *, int * ));
46 static int do_unbind LDAP_P(( Ri * ));
50 * Determine the type of ldap operation being performed and call the
51 * appropriate routine.
52 * - If successful, returns ERR_DO_LDAP_OK
53 * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
54 * The caller should wait a while and retry the operation.
55 * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned. The caller
56 * should reject the operation and continue with the next replication
67 int lderr = LDAP_SUCCESS;
73 if ( ri->ri_ldp == NULL ) {
74 rc = do_bind( ri, &lderr );
76 if ( rc != BIND_OK ) {
77 return DO_LDAP_ERR_RETRYABLE;
81 switch ( re->re_changetype ) {
83 lderr = op_ldap_add( ri, re, errmsg );
84 if ( lderr != LDAP_SUCCESS ) {
85 Debug( LDAP_DEBUG_ANY,
86 "Error: ldap_add_s failed adding \"%s\": %s\n",
87 *errmsg ? *errmsg : ldap_err2string( lderr ),
92 lderr = op_ldap_modify( ri, re, errmsg );
93 if ( lderr != LDAP_SUCCESS ) {
94 Debug( LDAP_DEBUG_ANY,
95 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
96 *errmsg ? *errmsg : ldap_err2string( lderr ),
101 lderr = op_ldap_delete( ri, re, errmsg );
102 if ( lderr != LDAP_SUCCESS ) {
103 Debug( LDAP_DEBUG_ANY,
104 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
105 *errmsg ? *errmsg : ldap_err2string( lderr ),
110 lderr = op_ldap_modrdn( ri, re, errmsg );
111 if ( lderr != LDAP_SUCCESS ) {
112 Debug( LDAP_DEBUG_ANY,
113 "Error: ldap_modrdn_s failed modifying %s: %s\n",
114 *errmsg ? *errmsg : ldap_err2string( lderr ),
119 Debug( LDAP_DEBUG_ANY,
120 "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
121 re->re_changetype, re->re_dn, 0 );
122 return DO_LDAP_ERR_FATAL;
126 * Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
127 * we may have been idle long enough that the remote slapd timed
128 * us out. Rebind and try again.
130 if ( lderr == LDAP_SUCCESS ) {
132 } else if ( lderr == LDAP_SERVER_DOWN ) {
133 /* The LDAP server may have timed us out - rebind and try again */
134 (void) do_unbind( ri );
137 return DO_LDAP_ERR_FATAL;
140 return DO_LDAP_ERR_FATAL;
147 * Perform an ldap add operation.
157 int nattrs, rc = 0, i;
158 LDAPMod *ldm, **ldmarr;
165 * Construct a null-terminated array of LDAPMod structs.
168 while ( mi[ i ].mi_type != NULL ) {
169 ldm = alloc_ldapmod();
170 ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
171 ( nattrs + 2 ) * sizeof( LDAPMod * ));
172 ldmarr[ nattrs ] = ldm;
173 ldm->mod_op = LDAP_MOD_BVALUES;
174 ldm->mod_type = mi[ i ].mi_type;
176 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
181 if ( ldmarr != NULL ) {
182 ldmarr[ nattrs ] = NULL;
184 /* Perform the operation */
185 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
186 ri->ri_hostname, ri->ri_port, re->re_dn );
187 rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
189 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
192 *errmsg = "No modifications to do";
193 Debug( LDAP_DEBUG_ANY,
194 "Error: op_ldap_add: no mods to do (%s)!\n", re->re_dn, 0, 0 );
196 free_ldmarr( ldmarr );
204 * Perform an ldap modify operation.
206 #define AWAITING_OP -1
215 int state; /* This code is a simple-minded state machine */
216 int nvals; /* Number of values we're modifying */
217 int nops; /* Number of LDAPMod structs in ldmarr */
218 LDAPMod *ldm, **ldmarr;
228 if ( re->re_mods == NULL ) {
229 *errmsg = "No arguments given";
230 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
236 * Construct a null-terminated array of LDAPMod structs.
238 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
239 type = mi[ i ].mi_type;
240 value = mi[ i ].mi_val;
241 len = mi[ i ].mi_len;
242 switch ( getmodtype( type )) {
244 state = T_MODSEP; /* Got a separator line "-\n" */
248 ldmarr = ( LDAPMod ** )
249 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
250 ldmarr[ nops ] = ldm = alloc_ldapmod();
251 ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
252 ldm->mod_type = value;
257 state = T_MODOPREPLACE;
258 ldmarr = ( LDAPMod ** )
259 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
260 ldmarr[ nops ] = ldm = alloc_ldapmod();
261 ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
262 ldm->mod_type = value;
267 state = T_MODOPDELETE;
268 ldmarr = ( LDAPMod ** )
269 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
270 ldmarr[ nops ] = ldm = alloc_ldapmod();
271 ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
272 ldm->mod_type = value;
277 if ( state == AWAITING_OP ) {
278 Debug( LDAP_DEBUG_ANY,
279 "Error: op_ldap_modify: unknown mod type \"%s\"\n",
285 * We should have an attribute: value pair here.
286 * Construct the mod_bvalues part of the ldapmod struct.
288 if ( strcasecmp( type, ldm->mod_type )) {
289 Debug( LDAP_DEBUG_ANY,
290 "Error: malformed modify op, %s: %s (expecting %s:)\n",
291 type, value, ldm->mod_type );
294 ldm->mod_bvalues = ( struct berval ** )
295 ch_realloc( ldm->mod_bvalues,
296 ( nvals + 2 ) * sizeof( struct berval * ));
297 ldm->mod_bvalues[ nvals + 1 ] = NULL;
298 ldm->mod_bvalues[ nvals ] = ( struct berval * )
299 ch_malloc( sizeof( struct berval ));
300 ldm->mod_bvalues[ nvals ]->bv_val = value;
301 ldm->mod_bvalues[ nvals ]->bv_len = len;
305 ldmarr[ nops ] = NULL;
308 /* Actually perform the LDAP operation */
309 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
310 ri->ri_hostname, ri->ri_port, re->re_dn );
311 rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
313 free_ldmarr( ldmarr );
321 * Perform an ldap delete operation.
332 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
333 ri->ri_hostname, ri->ri_port, re->re_dn );
334 rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
343 * Perform an ldap modrdn operation.
345 #define GOT_NEWRDN 0x1
346 #define GOT_DELOLDRDN 0x2
347 #define GOT_NEWSUP 0x4
349 #define GOT_MODDN_REQ (GOT_NEWRDN|GOT_DELOLDRDN)
350 #define GOT_ALL_MODDN(f) (((f) & GOT_MODDN_REQ) == GOT_MODDN_REQ)
367 if ( re->re_mods == NULL ) {
368 *errmsg = "No arguments given";
369 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
375 * Get the arguments: should see newrdn: and deleteoldrdn: args.
377 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
378 if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
379 if( state & GOT_NEWRDN ) {
380 Debug( LDAP_DEBUG_ANY,
381 "Error: op_ldap_modrdn: multiple newrdn arg \"%s\"\n",
382 mi[ i ].mi_val, 0, 0 );
383 *errmsg = "Multiple newrdn argument";
387 newrdn = mi[ i ].mi_val;
390 } else if ( !strcmp( mi[ i ].mi_type, T_DELOLDRDNSTR )) {
391 if( state & GOT_DELOLDRDN ) {
392 Debug( LDAP_DEBUG_ANY,
393 "Error: op_ldap_modrdn: multiple deleteoldrdn arg \"%s\"\n",
394 mi[ i ].mi_val, 0, 0 );
395 *errmsg = "Multiple newrdn argument";
399 state |= GOT_DELOLDRDN;
400 if ( !strcmp( mi[ i ].mi_val, "0" )) {
402 } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
405 Debug( LDAP_DEBUG_ANY,
406 "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
407 mi[ i ].mi_val, 0, 0 );
408 *errmsg = "Incorrect argument to deleteoldrdn";
412 } else if ( !strcmp( mi[ i ].mi_type, T_NEWSUPSTR )) {
413 if( state & GOT_NEWSUP ) {
414 Debug( LDAP_DEBUG_ANY,
415 "Error: op_ldap_modrdn: multiple newsuperior arg \"%s\"\n",
416 mi[ i ].mi_val, 0, 0 );
417 *errmsg = "Multiple newsuperior argument";
421 newrdn = mi[ i ].mi_val;
425 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
426 mi[ i ].mi_type, 0, 0 );
427 *errmsg = "Bad value in replication log entry";
433 * Punt if we don't have all the args.
435 if ( GOT_ALL_MODDN(state) ) {
436 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
438 *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
443 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
446 sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
447 buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
449 sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
450 Debug( LDAP_DEBUG_ARGS,
451 "replica %s - modify rdn %s (flag: %d)\n",
452 buf, buf2, drdnflag );
455 #endif /* LDAP_DEBUG */
458 rc = ldap_rename2_s( ri->ri_ldp, re->re_dn, mi->mi_val, newsup, drdnflag );
460 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
467 * Allocate and initialize an ldapmod struct.
470 alloc_ldapmod( void )
474 ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
475 ldm->mod_type = NULL;
476 ldm->mod_bvalues = ( struct berval ** ) NULL;
483 * Free an ldapmod struct associated mod_bvalues. NOTE - it is assumed
484 * that mod_bvalues and mod_type contain pointers to the same block of memory
485 * pointed to by the repl struct. Therefore, it's not freed here.
496 if ( ldm->mod_bvalues != NULL ) {
497 for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
498 free( ldm->mod_bvalues[ i ] );
500 free( ldm->mod_bvalues );
508 * Free an an array of LDAPMod pointers and the LDAPMod structs they point
517 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
518 free_ldapmod( ldmarr[ i ] );
525 * Create a berval with a single value.
527 static struct berval **
528 make_singlevalued_berval(
534 p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
535 p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
537 p[ 0 ]->bv_val = value;
538 p[ 0 ]->bv_len = len;
544 * Given a modification type (string), return an enumerated type.
545 * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
552 if ( !strcmp( type, T_MODSEPSTR )) {
555 if ( !strcmp( type, T_MODOPADDSTR )) {
556 return( T_MODOPADD );
558 if ( !strcmp( type, T_MODOPREPLACESTR )) {
559 return( T_MODOPREPLACE );
561 if ( !strcmp( type, T_MODOPDELETESTR )) {
562 return( T_MODOPDELETE );
569 * Perform an LDAP unbind operation. If replica is NULL, or the
570 * repl_ldp is NULL, just return LDAP_SUCCESS. Otherwise, unbind,
571 * set the ldp to NULL, and return the result of the unbind call.
578 int rc = LDAP_SUCCESS;
580 if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
581 rc = ldap_unbind( ri->ri_ldp );
582 if ( rc != LDAP_SUCCESS ) {
583 Debug( LDAP_DEBUG_ANY,
584 "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
585 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
595 * Perform an LDAP bind operation to the replication site given
596 * by replica. If replica->repl_ldp is non-NULL, then we unbind
597 * from the replica before rebinding. It should be safe to call
598 * this to re-connect if the replica's connection goes away
601 * Returns 0 on success, -1 if an LDAP error occurred, and a return
602 * code > 0 if some other error occurred, e.g. invalid bind method.
603 * If an LDAP error occurs, the LDAP error is returned in lderr.
612 #ifdef HAVE_CYRUS_SASL
620 Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
621 return( BIND_ERR_BADRI );
624 if ( ri->ri_ldp != NULL ) {
625 ldrc = ldap_unbind( ri->ri_ldp );
626 if ( ldrc != LDAP_SUCCESS ) {
627 Debug( LDAP_DEBUG_ANY,
628 "Error: do_bind: ldap_unbind failed: %s\n",
629 ldap_err2string( ldrc ), 0, 0 );
634 Debug( LDAP_DEBUG_ARGS, "Initializing session to %s:%d\n",
635 ri->ri_hostname, ri->ri_port, 0 );
637 ri->ri_ldp = ldap_init( ri->ri_hostname, ri->ri_port );
638 if ( ri->ri_ldp == NULL ) {
639 Debug( LDAP_DEBUG_ANY, "Error: ldap_init(%s, %d) failed: %s\n",
640 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
641 return( BIND_ERR_OPEN );
644 { /* set version 3 */
645 int err, version = 3;
646 err = ldap_set_option(ri->ri_ldp,
647 LDAP_OPT_PROTOCOL_VERSION, &version);
649 if( err != LDAP_OPT_SUCCESS ) {
650 Debug( LDAP_DEBUG_ANY,
651 "Error: ldap_set_option(%s, LDAP_OPT_VERSION, 3) failed!\n",
652 ri->ri_hostname, NULL, NULL );
654 ldap_unbind( ri->ri_ldp );
656 return BIND_ERR_VERSION;
661 * Set ldap library options to (1) not follow referrals, and
662 * (2) restart the select() system call.
666 err = ldap_set_option(ri->ri_ldp, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
668 if( err != LDAP_OPT_SUCCESS ) {
669 Debug( LDAP_DEBUG_ANY,
670 "Error: ldap_set_option(%s,REFERRALS, OFF) failed!\n",
671 ri->ri_hostname, NULL, NULL );
672 ldap_unbind( ri->ri_ldp );
674 return BIND_ERR_REFERRALS;
677 ldap_set_option(ri->ri_ldp, LDAP_OPT_RESTART, LDAP_OPT_ON);
679 switch ( ri->ri_bind_method ) {
682 * Bind with a plaintext password.
684 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
685 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
686 ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
688 if ( ldrc != LDAP_SUCCESS ) {
689 Debug( LDAP_DEBUG_ANY,
690 "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
691 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
693 ldap_unbind( ri->ri_ldp );
695 return( BIND_ERR_SIMPLE_FAILED );
700 Debug( LDAP_DEBUG_ARGS, "bind to %s as %s via %s (SASL)\n",
701 ri->ri_hostname, ri->ri_authcId, ri->ri_saslmech );
703 #ifdef HAVE_CYRUS_SASL
704 defaults = lutil_sasl_defaults( ri->ri_ldp, ri->ri_saslmech,
705 NULL, ri->ri_authcId, NULL, NULL );
706 ldrc = ldap_sasl_interactive_bind_s( ri->ri_ldp, ri->ri_bind_dn,
707 ri->ri_saslmech, NULL, NULL,
708 LDAP_SASL_QUIET, lutil_sasl_interact, defaults );
709 if ( ldrc != LDAP_SUCCESS ) {
710 Debug( LDAP_DEBUG_ANY, "Error: LDAP SASL for %s:%d failed: %s\n",
711 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
713 ldap_unbind( ri->ri_ldp );
715 return( BIND_ERR_SASL_FAILED );
719 Debug( LDAP_DEBUG_ANY,
720 "Error: do_bind: SASL not supported %s:%d\n",
721 ri->ri_hostname, ri->ri_port, NULL );
722 ldap_unbind( ri->ri_ldp );
724 return( BIND_ERR_BAD_ATYPE );
728 Debug( LDAP_DEBUG_ANY,
729 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
730 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
731 ldap_unbind( ri->ri_ldp );
733 return( BIND_ERR_BAD_ATYPE );
739 LDAPControl *ctrls[2];
743 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
744 c.ldctl_value.bv_val = NULL;
745 c.ldctl_value.bv_len = 0;
746 c.ldctl_iscritical = 1;
748 err = ldap_set_option(ri->ri_ldp, LDAP_OPT_SERVER_CONTROLS, &ctrls);
750 if( err != LDAP_OPT_SUCCESS ) {
751 Debug( LDAP_DEBUG_ANY,
752 "Error: ldap_set_option(%s, SERVER_CONTROLS, ManageDSAit) failed!\n",
753 ri->ri_hostname, NULL, NULL );
754 ldap_unbind( ri->ri_ldp );
756 return BIND_ERR_MANAGEDSAIT;
768 * For debugging. Print the contents of an ldmarr array.
780 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
782 Debug( LDAP_DEBUG_TRACE,
783 "Trace (%ld): *** ldmarr[ %d ] contents:\n",
784 (long) getpid(), i, 0 );
785 Debug( LDAP_DEBUG_TRACE,
786 "Trace (%ld): *** ldm->mod_op: %d\n",
787 (long) getpid(), ldm->mod_op, 0 );
788 Debug( LDAP_DEBUG_TRACE,
789 "Trace (%ld): *** ldm->mod_type: %s\n",
790 (long) getpid(), ldm->mod_type, 0 );
791 if ( ldm->mod_bvalues != NULL ) {
792 for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
793 msgbuf = ch_malloc( b->bv_len + 512 );
794 sprintf( msgbuf, "***** bv[ %d ] len = %ld, val = <%s>",
795 j, b->bv_len, b->bv_val );
796 Debug( LDAP_DEBUG_TRACE,
797 "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );