2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 The OpenLDAP Foundation.
5 * Portions Copyright 2003 Mark Benson.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP). Additional significant contributors
34 * ldap_op.c - routines to perform LDAP operations
41 #include <ac/stdlib.h>
44 #include <ac/string.h>
47 #include <ac/unistd.h>
49 #define LDAP_DEPRECATED 1
51 #include "lutil_ldap.h"
54 /* Forward references */
55 static struct berval **make_singlevalued_berval LDAP_P(( char *, int ));
56 static int op_ldap_add LDAP_P(( Ri *, Re *, char **, int * ));
57 static int op_ldap_modify LDAP_P(( Ri *, Re *, char **, int * ));
58 static int op_ldap_delete LDAP_P(( Ri *, Re *, char **, int * ));
59 static int op_ldap_modrdn LDAP_P(( Ri *, Re *, char **, int * ));
60 static LDAPMod *alloc_ldapmod LDAP_P(( void ));
61 static void free_ldapmod LDAP_P(( LDAPMod * ));
62 static void free_ldmarr LDAP_P(( LDAPMod ** ));
63 static int getmodtype LDAP_P(( char * ));
64 static void dump_ldm_array LDAP_P(( LDAPMod ** ));
65 static int do_bind LDAP_P(( Ri *, int * ));
66 static int do_unbind LDAP_P(( Ri * ));
70 * Determine the type of ldap operation being performed and call the
71 * appropriate routine.
72 * - If successful, returns DO_LDAP_OK
73 * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
74 * The caller should wait a while and retry the operation.
75 * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned. The caller
76 * should reject the operation and continue with the next replication
93 if ( ri->ri_ldp == NULL ) {
94 lderr = do_bind( ri, &lderr );
96 if ( lderr != BIND_OK ) {
97 return DO_LDAP_ERR_RETRYABLE;
101 switch ( re->re_changetype ) {
103 lderr = op_ldap_add( ri, re, errmsg, errfree );
104 if ( lderr != LDAP_SUCCESS ) {
106 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
107 "Error: ldap_add_s failed adding \"%s\": %s\n",
108 *errmsg ? *errmsg : ldap_err2string( lderr ),
111 Debug( LDAP_DEBUG_ANY,
112 "Error: ldap_add_s failed adding \"%s\": %s\n",
113 *errmsg ? *errmsg : ldap_err2string( lderr ),
120 lderr = op_ldap_modify( ri, re, errmsg, errfree );
121 if ( lderr != LDAP_SUCCESS ) {
123 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
124 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
125 *errmsg ? *errmsg : ldap_err2string( lderr ),
128 Debug( LDAP_DEBUG_ANY,
129 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
130 *errmsg ? *errmsg : ldap_err2string( lderr ),
137 lderr = op_ldap_delete( ri, re, errmsg, errfree );
138 if ( lderr != LDAP_SUCCESS ) {
140 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
141 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
142 *errmsg ? *errmsg : ldap_err2string( lderr ),
145 Debug( LDAP_DEBUG_ANY,
146 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
147 *errmsg ? *errmsg : ldap_err2string( lderr ),
154 lderr = op_ldap_modrdn( ri, re, errmsg, errfree );
155 if ( lderr != LDAP_SUCCESS ) {
157 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
158 "Error: ldap_modrdn_s failed modifying %s: %s\n",
159 *errmsg ? *errmsg : ldap_err2string( lderr ),
162 Debug( LDAP_DEBUG_ANY,
163 "Error: ldap_modrdn_s failed modifying %s: %s\n",
164 *errmsg ? *errmsg : ldap_err2string( lderr ),
172 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
173 "Error: bad op \"%d\", dn = \"%s\"\n",
174 re->re_changetype, re->re_dn, 0 );
176 Debug( LDAP_DEBUG_ANY,
177 "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
178 re->re_changetype, re->re_dn, 0 );
180 return DO_LDAP_ERR_FATAL;
184 * Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
185 * we may have been idle long enough that the remote slapd timed
186 * us out. Rebind and try again.
193 return DO_LDAP_ERR_FATAL;
195 case LDAP_SERVER_DOWN: /* server went down */
196 (void) do_unbind( ri );
199 } while ( retry > 0 );
201 return DO_LDAP_ERR_RETRYABLE;
207 * Perform an ldap add operation.
218 int nattrs, rc = 0, i;
219 LDAPMod *ldm, **ldmarr;
226 * Construct a null-terminated array of LDAPMod structs.
229 while ( mi[ i ].mi_type != NULL ) {
230 ldm = alloc_ldapmod();
231 ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
232 ( nattrs + 2 ) * sizeof( LDAPMod * ));
233 ldmarr[ nattrs ] = ldm;
234 ldm->mod_op = LDAP_MOD_BVALUES;
235 ldm->mod_type = mi[ i ].mi_type;
237 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
242 if ( ldmarr != NULL ) {
243 ldmarr[ nattrs ] = NULL;
245 /* Perform the operation */
247 LDAP_LOG ( OPERATION, ARGS,
248 "op_ldap_add: replica %s:%d - add dn \"%s\"\n",
249 ri->ri_hostname, ri->ri_port, re->re_dn );
251 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
252 ri->ri_hostname, ri->ri_port, re->re_dn );
254 rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
256 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
257 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_STRING, errmsg);
261 *errmsg = "No modifications to do";
263 LDAP_LOG ( OPERATION, ERR,
264 "op_ldap_add: Error: no mods to do (%s)!\n", re->re_dn, 0, 0 );
266 Debug( LDAP_DEBUG_ANY,
267 "Error: op_ldap_add: no mods to do (%s)!\n", re->re_dn, 0, 0 );
270 free_ldmarr( ldmarr );
278 * Perform an ldap modify operation.
280 #define AWAITING_OP -1
290 int state; /* This code is a simple-minded state machine */
291 int nvals; /* Number of values we're modifying */
292 int nops; /* Number of LDAPMod structs in ldmarr */
293 LDAPMod *ldm = NULL, **ldmarr;
303 if ( re->re_mods == NULL ) {
304 *errmsg = "No arguments given";
306 LDAP_LOG ( OPERATION, ERR,
307 "op_ldap_modify: Error: no arguments\n" , 0, 0, 0 );
309 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
316 * Construct a null-terminated array of LDAPMod structs.
318 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
319 type = mi[ i ].mi_type;
320 value = mi[ i ].mi_val;
321 len = mi[ i ].mi_len;
322 switch ( getmodtype( type )) {
324 state = T_MODSEP; /* Got a separator line "-\n" */
328 ldmarr = ( LDAPMod ** )
329 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
330 ldmarr[ nops ] = ldm = alloc_ldapmod();
331 ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
332 ldm->mod_type = value;
337 state = T_MODOPREPLACE;
338 ldmarr = ( LDAPMod ** )
339 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
340 ldmarr[ nops ] = ldm = alloc_ldapmod();
341 ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
342 ldm->mod_type = value;
347 state = T_MODOPDELETE;
348 ldmarr = ( LDAPMod ** )
349 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
350 ldmarr[ nops ] = ldm = alloc_ldapmod();
351 ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
352 ldm->mod_type = value;
356 #ifdef LDAP_MOD_INCREMENT
357 case T_MODOPINCREMENT:
358 state = T_MODOPINCREMENT;
359 ldmarr = ( LDAPMod ** )
360 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
361 ldmarr[ nops ] = ldm = alloc_ldapmod();
362 ldm->mod_op = LDAP_MOD_INCREMENT | LDAP_MOD_BVALUES;
363 ldm->mod_type = value;
369 if ( state == AWAITING_OP ) {
371 LDAP_LOG ( OPERATION, ERR,
372 "op_ldap_modify: Error: unknown mod type \"%s\"\n", type, 0, 0 );
374 Debug( LDAP_DEBUG_ANY,
375 "Error: op_ldap_modify: unknown mod type \"%s\"\n",
384 * We should have an attribute: value pair here.
385 * Construct the mod_bvalues part of the ldapmod struct.
387 if ( strcasecmp( type, ldm->mod_type )) {
389 LDAP_LOG ( OPERATION, ERR,
390 "op_ldap_modify: Error: "
391 "malformed modify op, %s: %s (expecting \"%s\")\n",
392 type, value, ldm->mod_type );
394 Debug( LDAP_DEBUG_ANY,
395 "Error: malformed modify op, %s: %s (expecting %s:)\n",
396 type, value, ldm->mod_type );
400 ldm->mod_bvalues = ( struct berval ** )
401 ch_realloc( ldm->mod_bvalues,
402 ( nvals + 2 ) * sizeof( struct berval * ));
403 ldm->mod_bvalues[ nvals + 1 ] = NULL;
404 ldm->mod_bvalues[ nvals ] = ( struct berval * )
405 ch_malloc( sizeof( struct berval ));
406 ldm->mod_bvalues[ nvals ]->bv_val = value;
407 ldm->mod_bvalues[ nvals ]->bv_len = len;
411 ldmarr[ nops ] = NULL;
414 /* Actually perform the LDAP operation */
416 LDAP_LOG ( OPERATION, DETAIL1,
417 "op_ldap_modify: replica %s:%d - modify dn \"%s\"\n",
418 ri->ri_hostname, ri->ri_port, re->re_dn );
420 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
421 ri->ri_hostname, ri->ri_port, re->re_dn );
423 rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
424 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_STRING, errmsg);
427 free_ldmarr( ldmarr );
435 * Perform an ldap delete operation.
448 LDAP_LOG ( OPERATION, ARGS,
449 "op_ldap_delete: replica %s:%d - delete dn \"%s\"\n",
450 ri->ri_hostname, ri->ri_port, re->re_dn );
452 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
453 ri->ri_hostname, ri->ri_port, re->re_dn );
455 rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
456 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_STRING, errmsg);
466 * Perform an ldap modrdn operation.
468 #define GOT_NEWRDN 0x1
469 #define GOT_DELOLDRDN 0x2
470 #define GOT_NEWSUP 0x4
472 #define GOT_MODDN_REQ (GOT_NEWRDN|GOT_DELOLDRDN)
473 #define GOT_ALL_MODDN(f) (((f) & GOT_MODDN_REQ) == GOT_MODDN_REQ)
491 if ( re->re_mods == NULL ) {
492 *errmsg = "No arguments given";
494 LDAP_LOG ( OPERATION, ERR,
495 "op_ldap_modrdn: Error: no arguments\n" , 0, 0, 0 );
497 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
504 * Get the arguments: should see newrdn: and deleteoldrdn: args.
506 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
507 if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
508 if( state & GOT_NEWRDN ) {
510 LDAP_LOG ( OPERATION, ERR,
511 "op_ldap_modrdn: Error: multiple newrdn arg \"%s\"\n",
512 mi[ i ].mi_val, 0, 0 );
514 Debug( LDAP_DEBUG_ANY,
515 "Error: op_ldap_modrdn: multiple newrdn arg \"%s\"\n",
516 mi[ i ].mi_val, 0, 0 );
518 *errmsg = "Multiple newrdn argument";
522 newrdn = mi[ i ].mi_val;
525 } else if ( !strcmp( mi[ i ].mi_type, T_DELOLDRDNSTR )) {
526 if( state & GOT_DELOLDRDN ) {
528 LDAP_LOG ( OPERATION, ERR,
529 "op_ldap_modrdn: Error: multiple deleteoldrdn arg \"%s\"\n",
530 mi[ i ].mi_val, 0, 0 );
532 Debug( LDAP_DEBUG_ANY,
533 "Error: op_ldap_modrdn: multiple deleteoldrdn arg \"%s\"\n",
534 mi[ i ].mi_val, 0, 0 );
536 *errmsg = "Multiple newrdn argument";
540 state |= GOT_DELOLDRDN;
541 if ( !strcmp( mi[ i ].mi_val, "0" )) {
543 } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
547 LDAP_LOG ( OPERATION, ERR,
548 "op_ldap_modrdn: Error: bad deleteoldrdn arg \"%s\"\n",
549 mi[ i ].mi_val, 0, 0 );
551 Debug( LDAP_DEBUG_ANY,
552 "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
553 mi[ i ].mi_val, 0, 0 );
555 *errmsg = "Incorrect argument to deleteoldrdn";
559 } else if ( !strcmp( mi[ i ].mi_type, T_NEWSUPSTR )) {
560 if( state & GOT_NEWSUP ) {
562 LDAP_LOG ( OPERATION, ERR,
563 "op_ldap_modrdn: Error: multiple newsuperior arg \"%s\"\n",
564 mi[ i ].mi_val, 0, 0 );
566 Debug( LDAP_DEBUG_ANY,
567 "Error: op_ldap_modrdn: multiple newsuperior arg \"%s\"\n",
568 mi[ i ].mi_val, 0, 0 );
570 *errmsg = "Multiple newsuperior argument";
574 newsup = mi[ i ].mi_val;
579 LDAP_LOG ( OPERATION, ERR,
580 "op_ldap_modrdn: Error: bad type \"%s\"\n",
581 mi[ i ].mi_type, 0, 0 );
583 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
584 mi[ i ].mi_type, 0, 0 );
586 *errmsg = "Bad value in replication log entry";
592 * Punt if we don't have all the args.
594 if ( !GOT_ALL_MODDN(state) ) {
596 LDAP_LOG ( OPERATION, ERR,
597 "op_ldap_modrdn: Error: missing arguments\n" , 0, 0, 0 );
599 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
602 *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
607 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
610 int buf2len = strlen( re->re_dn ) + strlen( mi->mi_val ) + 11;
612 snprintf( buf, sizeof(buf), "%s:%d", ri->ri_hostname, ri->ri_port );
614 buf2 = (char *) ch_malloc( buf2len );
615 snprintf( buf2, buf2len, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
618 LDAP_LOG ( OPERATION, ARGS,
619 "op_ldap_modrdn: replica %s - modify rdn %s (flag: %d)\n",
620 buf, buf2, drdnflag );
622 Debug( LDAP_DEBUG_ARGS,
623 "replica %s - modify rdn %s (flag: %d)\n",
624 buf, buf2, drdnflag );
628 #endif /* LDAP_DEBUG */
633 rc = ldap_rename2_s( ri->ri_ldp, re->re_dn, newrdn, newsup, drdnflag );
635 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
636 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_STRING, errmsg);
644 * Allocate and initialize an ldapmod struct.
647 alloc_ldapmod( void )
651 ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
652 ldm->mod_type = NULL;
653 ldm->mod_bvalues = ( struct berval ** ) NULL;
660 * Free an ldapmod struct associated mod_bvalues. NOTE - it is assumed
661 * that mod_bvalues and mod_type contain pointers to the same block of memory
662 * pointed to by the repl struct. Therefore, it's not freed here.
673 if ( ldm->mod_bvalues != NULL ) {
674 for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
675 free( ldm->mod_bvalues[ i ] );
677 free( ldm->mod_bvalues );
685 * Free an an array of LDAPMod pointers and the LDAPMod structs they point
694 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
695 free_ldapmod( ldmarr[ i ] );
702 * Create a berval with a single value.
704 static struct berval **
705 make_singlevalued_berval(
711 p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
712 p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
714 p[ 0 ]->bv_val = value;
715 p[ 0 ]->bv_len = len;
721 * Given a modification type (string), return an enumerated type.
722 * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
729 if ( !strcmp( type, T_MODSEPSTR )) {
732 if ( !strcmp( type, T_MODOPADDSTR )) {
733 return( T_MODOPADD );
735 if ( !strcmp( type, T_MODOPREPLACESTR )) {
736 return( T_MODOPREPLACE );
738 if ( !strcmp( type, T_MODOPDELETESTR )) {
739 return( T_MODOPDELETE );
741 if ( !strcmp( type, T_MODOPINCREMENTSTR )) {
742 return( T_MODOPINCREMENT );
749 * Perform an LDAP unbind operation. If replica is NULL, or the
750 * repl_ldp is NULL, just return LDAP_SUCCESS. Otherwise, unbind,
751 * set the ldp to NULL, and return the result of the unbind call.
758 int rc = LDAP_SUCCESS;
760 if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
761 rc = ldap_unbind( ri->ri_ldp );
762 if ( rc != LDAP_SUCCESS ) {
764 LDAP_LOG ( OPERATION, ERR,
765 "do_unbind: ldap_unbind failed for %s:%d: %s\n",
766 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
768 Debug( LDAP_DEBUG_ANY,
769 "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
770 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
781 * Perform an LDAP bind operation to the replication site given
782 * by replica. If replica->repl_ldp is non-NULL, then we unbind
783 * from the replica before rebinding. It should be safe to call
784 * this to re-connect if the replica's connection goes away
787 * Returns 0 on success, -1 if an LDAP error occurred, and a return
788 * code > 0 if some other error occurred, e.g. invalid bind method.
789 * If an LDAP error occurs, the LDAP error is returned in lderr.
798 int do_tls = ri->ri_tls;
804 LDAP_LOG ( OPERATION, ERR, "do_bind: null ri ptr\n" , 0, 0, 0 );
806 Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
808 return( BIND_ERR_BADRI );
812 if ( ri->ri_ldp != NULL ) {
813 ldrc = ldap_unbind( ri->ri_ldp );
814 if ( ldrc != LDAP_SUCCESS ) {
816 LDAP_LOG ( OPERATION, ERR,
817 "do_bind: ldap_unbind failed: %s\n", ldap_err2string( ldrc ), 0, 0 );
819 Debug( LDAP_DEBUG_ANY,
820 "Error: do_bind: ldap_unbind failed: %s\n",
821 ldap_err2string( ldrc ), 0, 0 );
827 if ( ri->ri_uri != NULL ) { /* new URI style */
829 LDAP_LOG ( OPERATION, ARGS,
830 "do_bind: Initializing session to %s\n",
833 Debug( LDAP_DEBUG_ARGS, "Initializing session to %s\n",
837 ldrc = ldap_initialize( &(ri->ri_ldp), ri->ri_uri);
839 if (ldrc != LDAP_SUCCESS) {
841 LDAP_LOG ( OPERATION, ERR,
842 "do_bind: ldap_initalize (0, %s) failed: %s\n",
843 ri->ri_uri, ldap_err2string(ldrc), 0 );
845 Debug( LDAP_DEBUG_ANY, "Error: ldap_initialize(0, %s) failed: %s\n",
846 ri->ri_uri, ldap_err2string(ldrc), 0 );
848 return( BIND_ERR_OPEN );
850 } else { /* old HOST style */
852 LDAP_LOG ( OPERATION, ARGS,
853 "do_bind: Initializing session to %s:%d\n",
854 ri->ri_hostname, ri->ri_port, 0 );
856 Debug( LDAP_DEBUG_ARGS, "Initializing session to %s:%d\n",
857 ri->ri_hostname, ri->ri_port, 0 );
860 ri->ri_ldp = ldap_init( ri->ri_hostname, ri->ri_port );
861 if ( ri->ri_ldp == NULL ) {
863 LDAP_LOG ( OPERATION, ERR,
864 "do_bind: ldap_init (%s, %d) failed: %s\n",
865 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
867 Debug( LDAP_DEBUG_ANY, "Error: ldap_init(%s, %d) failed: %s\n",
868 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
870 return( BIND_ERR_OPEN );
874 { /* set version 3 */
875 int err, version = 3;
876 err = ldap_set_option(ri->ri_ldp,
877 LDAP_OPT_PROTOCOL_VERSION, &version);
879 if( err != LDAP_OPT_SUCCESS ) {
881 LDAP_LOG ( OPERATION, ERR, "do_bind: "
882 "Error: ldap_set_option(%s, LDAP_OPT_VERSION, 3) failed!\n",
883 ri->ri_hostname, 0, 0 );
885 Debug( LDAP_DEBUG_ANY,
886 "Error: ldap_set_option(%s, LDAP_OPT_VERSION, 3) failed!\n",
887 ri->ri_hostname, NULL, NULL );
890 ldap_unbind( ri->ri_ldp );
892 return BIND_ERR_VERSION;
897 * Set ldap library options to (1) not follow referrals, and
898 * (2) restart the select() system call.
902 err = ldap_set_option(ri->ri_ldp, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
904 if( err != LDAP_OPT_SUCCESS ) {
906 LDAP_LOG ( OPERATION, ERR, "do_bind: "
907 "Error: ldap_set_option(%s, REFERRALS, OFF) failed!\n",
908 ri->ri_hostname, 0, 0 );
910 Debug( LDAP_DEBUG_ANY,
911 "Error: ldap_set_option(%s,REFERRALS, OFF) failed!\n",
912 ri->ri_hostname, NULL, NULL );
914 ldap_unbind( ri->ri_ldp );
916 return BIND_ERR_REFERRALS;
919 ldap_set_option(ri->ri_ldp, LDAP_OPT_RESTART, LDAP_OPT_ON);
922 int err = ldap_start_tls_s(ri->ri_ldp, NULL, NULL);
924 if( err != LDAP_SUCCESS ) {
926 LDAP_LOG ( OPERATION, ERR, "do_bind: "
927 "%s: ldap_start_tls failed: %s (%d)\n",
928 ri->ri_tls == TLS_CRITICAL ? "Error" : "Warning",
929 ldap_err2string( err ), err );
931 Debug( LDAP_DEBUG_ANY,
932 "%s: ldap_start_tls failed: %s (%d)\n",
933 ri->ri_tls == TLS_CRITICAL ? "Error" : "Warning",
934 ldap_err2string( err ), err );
937 if( ri->ri_tls == TLS_CRITICAL ) {
939 ldap_unbind( ri->ri_ldp );
941 return BIND_ERR_TLS_FAILED;
948 switch ( ri->ri_bind_method ) {
949 case LDAP_AUTH_SIMPLE:
951 * Bind with a plaintext password.
954 LDAP_LOG ( OPERATION, ARGS,
955 "do_bind: bind to %s:%d as %s (simple)\n",
956 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
958 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
959 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
961 ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
963 if ( ldrc != LDAP_SUCCESS ) {
965 LDAP_LOG ( OPERATION, ERR, "do_bind: "
966 "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
967 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ) );
969 Debug( LDAP_DEBUG_ANY,
970 "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
971 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
974 ldap_unbind( ri->ri_ldp );
976 return( BIND_ERR_SIMPLE_FAILED );
982 LDAP_LOG ( OPERATION, ARGS,
983 "do_bind: bind to %s as %s via %s (SASL)\n",
985 ri->ri_authcId ? ri->ri_authcId : "-",
988 Debug( LDAP_DEBUG_ARGS, "bind to %s as %s via %s (SASL)\n",
990 ri->ri_authcId ? ri->ri_authcId : "-",
994 #ifdef HAVE_CYRUS_SASL
995 if( ri->ri_secprops != NULL ) {
996 int err = ldap_set_option(ri->ri_ldp,
997 LDAP_OPT_X_SASL_SECPROPS, ri->ri_secprops);
999 if( err != LDAP_OPT_SUCCESS ) {
1001 LDAP_LOG ( OPERATION, ERR, "do_bind: "
1002 "Error: ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
1003 ri->ri_hostname, ri->ri_secprops, 0 );
1005 Debug( LDAP_DEBUG_ANY,
1006 "Error: ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
1007 ri->ri_hostname, ri->ri_secprops, NULL );
1009 ldap_unbind( ri->ri_ldp );
1011 return BIND_ERR_SASL_FAILED;
1016 void *defaults = lutil_sasl_defaults( ri->ri_ldp, ri->ri_saslmech,
1017 ri->ri_realm, ri->ri_authcId, ri->ri_password, ri->ri_authzId );
1019 ldrc = ldap_sasl_interactive_bind_s( ri->ri_ldp, ri->ri_bind_dn,
1020 ri->ri_saslmech, NULL, NULL,
1021 LDAP_SASL_QUIET, lutil_sasl_interact, defaults );
1023 lutil_sasl_freedefs( defaults );
1024 if ( ldrc != LDAP_SUCCESS ) {
1026 LDAP_LOG ( OPERATION, ERR, "do_bind: "
1027 "Error: LDAP SASL for %s:%d failed: %s\n",
1028 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ) );
1030 Debug( LDAP_DEBUG_ANY, "Error: LDAP SASL for %s:%d failed: %s\n",
1031 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
1034 ldap_unbind( ri->ri_ldp );
1036 return( BIND_ERR_SASL_FAILED );
1042 LDAP_LOG ( OPERATION, ERR, "do_bind: "
1043 "Error: do_bind: SASL not supported %s:%d\n",
1044 ri->ri_hostname, ri->ri_port, 0 );
1046 Debug( LDAP_DEBUG_ANY,
1047 "Error: do_bind: SASL not supported %s:%d\n",
1048 ri->ri_hostname, ri->ri_port, NULL );
1050 ldap_unbind( ri->ri_ldp );
1052 return( BIND_ERR_BAD_ATYPE );
1057 LDAP_LOG ( OPERATION, ERR, "do_bind: "
1058 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
1059 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
1061 Debug( LDAP_DEBUG_ANY,
1062 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
1063 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
1065 ldap_unbind( ri->ri_ldp );
1067 return( BIND_ERR_BAD_ATYPE );
1073 LDAPControl *ctrls[2];
1077 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1078 c.ldctl_value.bv_val = NULL;
1079 c.ldctl_value.bv_len = 0;
1080 c.ldctl_iscritical = 0;
1082 err = ldap_set_option(ri->ri_ldp, LDAP_OPT_SERVER_CONTROLS, &ctrls);
1084 if( err != LDAP_OPT_SUCCESS ) {
1086 LDAP_LOG ( OPERATION, ERR, "do_bind: "
1087 "ldap_set_option(%s, SERVER_CONTROLS, ManageDSAit) failed!\n",
1088 ri->ri_hostname, 0, 0 );
1090 Debug( LDAP_DEBUG_ANY, "Error: "
1091 "ldap_set_option(%s, SERVER_CONTROLS, ManageDSAit) failed!\n",
1092 ri->ri_hostname, NULL, NULL );
1094 ldap_unbind( ri->ri_ldp );
1096 return BIND_ERR_MANAGEDSAIT;
1108 * For debugging. Print the contents of an ldmarr array.
1120 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
1123 LDAP_LOG ( OPERATION, INFO, "dump_ldm_array: "
1124 "Trace (%ld): *** ldmarr[ %d ] contents:\n",
1125 (long) getpid(), i, 0 );
1126 LDAP_LOG ( OPERATION, INFO, "dump_ldm_array: "
1127 "Trace (%ld): *** ldm->mod_op: %d\n",
1128 (long) getpid(), ldm->mod_op, 0 );
1129 LDAP_LOG ( OPERATION, INFO, "dump_ldm_array: "
1130 "Trace (%ld): *** ldm->mod_type: %s\n",
1131 (long) getpid(), ldm->mod_type, 0 );
1133 Debug( LDAP_DEBUG_TRACE,
1134 "Trace (%ld): *** ldmarr[ %d ] contents:\n",
1135 (long) getpid(), i, 0 );
1136 Debug( LDAP_DEBUG_TRACE,
1137 "Trace (%ld): *** ldm->mod_op: %d\n",
1138 (long) getpid(), ldm->mod_op, 0 );
1139 Debug( LDAP_DEBUG_TRACE,
1140 "Trace (%ld): *** ldm->mod_type: %s\n",
1141 (long) getpid(), ldm->mod_type, 0 );
1143 if ( ldm->mod_bvalues != NULL ) {
1144 for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
1145 msgbuf = ch_malloc( b->bv_len + 512 );
1146 sprintf( msgbuf, "***** bv[ %d ] len = %ld, val = <%s>",
1147 j, b->bv_len, b->bv_val );
1149 LDAP_LOG ( OPERATION, INFO, "dump_ldm_array: "
1150 "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );
1152 Debug( LDAP_DEBUG_TRACE,
1153 "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );