3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
19 * ldap_op.c - routines to perform LDAP operations
26 #include <ac/stdlib.h>
29 #include <ac/string.h>
32 #include <ac/unistd.h>
35 #include "lutil_ldap.h"
38 /* Forward references */
39 static struct berval **make_singlevalued_berval LDAP_P(( char *, int ));
40 static int op_ldap_add LDAP_P(( Ri *, Re *, char ** ));
41 static int op_ldap_modify LDAP_P(( Ri *, Re *, char ** ));
42 static int op_ldap_delete LDAP_P(( Ri *, Re *, char ** ));
43 static int op_ldap_modrdn LDAP_P(( Ri *, Re *, char ** ));
44 static LDAPMod *alloc_ldapmod LDAP_P(( void ));
45 static void free_ldapmod LDAP_P(( LDAPMod * ));
46 static void free_ldmarr LDAP_P(( LDAPMod ** ));
47 static int getmodtype LDAP_P(( char * ));
48 static void dump_ldm_array LDAP_P(( LDAPMod ** ));
49 static int do_bind LDAP_P(( Ri *, int * ));
50 static int do_unbind LDAP_P(( Ri * ));
54 * Determine the type of ldap operation being performed and call the
55 * appropriate routine.
56 * - If successful, returns DO_LDAP_OK
57 * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
58 * The caller should wait a while and retry the operation.
59 * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned. The caller
60 * should reject the operation and continue with the next replication
75 if ( ri->ri_ldp == NULL ) {
76 lderr = do_bind( ri, &lderr );
78 if ( lderr != BIND_OK ) {
79 return DO_LDAP_ERR_RETRYABLE;
83 switch ( re->re_changetype ) {
85 lderr = op_ldap_add( ri, re, errmsg );
86 if ( lderr != LDAP_SUCCESS ) {
88 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
89 "Error: ldap_add_s failed adding \"%s\": %s\n",
90 *errmsg ? *errmsg : ldap_err2string( lderr ),
93 Debug( LDAP_DEBUG_ANY,
94 "Error: ldap_add_s failed adding \"%s\": %s\n",
95 *errmsg ? *errmsg : ldap_err2string( lderr ),
102 lderr = op_ldap_modify( ri, re, errmsg );
103 if ( lderr != LDAP_SUCCESS ) {
105 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
106 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
107 *errmsg ? *errmsg : ldap_err2string( lderr ),
110 Debug( LDAP_DEBUG_ANY,
111 "Error: ldap_modify_s failed modifying \"%s\": %s\n",
112 *errmsg ? *errmsg : ldap_err2string( lderr ),
119 lderr = op_ldap_delete( ri, re, errmsg );
120 if ( lderr != LDAP_SUCCESS ) {
122 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
123 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
124 *errmsg ? *errmsg : ldap_err2string( lderr ),
127 Debug( LDAP_DEBUG_ANY,
128 "Error: ldap_delete_s failed deleting \"%s\": %s\n",
129 *errmsg ? *errmsg : ldap_err2string( lderr ),
136 lderr = op_ldap_modrdn( ri, re, errmsg );
137 if ( lderr != LDAP_SUCCESS ) {
139 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
140 "Error: ldap_modrdn_s failed modifying %s: %s\n",
141 *errmsg ? *errmsg : ldap_err2string( lderr ),
144 Debug( LDAP_DEBUG_ANY,
145 "Error: ldap_modrdn_s failed modifying %s: %s\n",
146 *errmsg ? *errmsg : ldap_err2string( lderr ),
154 LDAP_LOG ( OPERATION, ERR, "do_ldap: "
155 "Error: bad op \"%d\", dn = \"%s\"\n",
156 re->re_changetype, re->re_dn, 0 );
158 Debug( LDAP_DEBUG_ANY,
159 "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
160 re->re_changetype, re->re_dn, 0 );
162 return DO_LDAP_ERR_FATAL;
166 * Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
167 * we may have been idle long enough that the remote slapd timed
168 * us out. Rebind and try again.
175 return DO_LDAP_ERR_FATAL;
177 case LDAP_SERVER_DOWN: /* server went down */
178 (void) do_unbind( ri );
181 } while ( retry > 0 );
183 return DO_LDAP_ERR_RETRYABLE;
189 * Perform an ldap add operation.
199 int nattrs, rc = 0, i;
200 LDAPMod *ldm, **ldmarr;
207 * Construct a null-terminated array of LDAPMod structs.
210 while ( mi[ i ].mi_type != NULL ) {
211 ldm = alloc_ldapmod();
212 ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
213 ( nattrs + 2 ) * sizeof( LDAPMod * ));
214 ldmarr[ nattrs ] = ldm;
215 ldm->mod_op = LDAP_MOD_BVALUES;
216 ldm->mod_type = mi[ i ].mi_type;
218 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
223 if ( ldmarr != NULL ) {
224 ldmarr[ nattrs ] = NULL;
226 /* Perform the operation */
228 LDAP_LOG ( OPERATION, ARGS,
229 "op_ldap_add: replica %s:%d - add dn \"%s\"\n",
230 ri->ri_hostname, ri->ri_port, re->re_dn );
232 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
233 ri->ri_hostname, ri->ri_port, re->re_dn );
235 rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
237 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
240 *errmsg = "No modifications to do";
242 LDAP_LOG ( OPERATION, ERR,
243 "op_ldap_add: Error: no mods to do (%s)!\n", re->re_dn, 0, 0 );
245 Debug( LDAP_DEBUG_ANY,
246 "Error: op_ldap_add: no mods to do (%s)!\n", re->re_dn, 0, 0 );
249 free_ldmarr( ldmarr );
257 * Perform an ldap modify operation.
259 #define AWAITING_OP -1
268 int state; /* This code is a simple-minded state machine */
269 int nvals; /* Number of values we're modifying */
270 int nops; /* Number of LDAPMod structs in ldmarr */
271 LDAPMod *ldm = NULL, **ldmarr;
281 if ( re->re_mods == NULL ) {
282 *errmsg = "No arguments given";
284 LDAP_LOG ( OPERATION, ERR,
285 "op_ldap_modify: Error: no arguments\n" , 0, 0, 0 );
287 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
294 * Construct a null-terminated array of LDAPMod structs.
296 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
297 type = mi[ i ].mi_type;
298 value = mi[ i ].mi_val;
299 len = mi[ i ].mi_len;
300 switch ( getmodtype( type )) {
302 state = T_MODSEP; /* Got a separator line "-\n" */
306 ldmarr = ( LDAPMod ** )
307 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
308 ldmarr[ nops ] = ldm = alloc_ldapmod();
309 ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
310 ldm->mod_type = value;
315 state = T_MODOPREPLACE;
316 ldmarr = ( LDAPMod ** )
317 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
318 ldmarr[ nops ] = ldm = alloc_ldapmod();
319 ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
320 ldm->mod_type = value;
325 state = T_MODOPDELETE;
326 ldmarr = ( LDAPMod ** )
327 ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
328 ldmarr[ nops ] = ldm = alloc_ldapmod();
329 ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
330 ldm->mod_type = value;
335 if ( state == AWAITING_OP ) {
337 LDAP_LOG ( OPERATION, ERR,
338 "op_ldap_modify: Error: unknown mod type \"%s\"\n", type, 0, 0 );
340 Debug( LDAP_DEBUG_ANY,
341 "Error: op_ldap_modify: unknown mod type \"%s\"\n",
350 * We should have an attribute: value pair here.
351 * Construct the mod_bvalues part of the ldapmod struct.
353 if ( strcasecmp( type, ldm->mod_type )) {
355 LDAP_LOG ( OPERATION, ERR,
356 "op_ldap_modify: Error: "
357 "malformed modify op, %s: %s (expecting \"%s\")\n",
358 type, value, ldm->mod_type );
360 Debug( LDAP_DEBUG_ANY,
361 "Error: malformed modify op, %s: %s (expecting %s:)\n",
362 type, value, ldm->mod_type );
366 ldm->mod_bvalues = ( struct berval ** )
367 ch_realloc( ldm->mod_bvalues,
368 ( nvals + 2 ) * sizeof( struct berval * ));
369 ldm->mod_bvalues[ nvals + 1 ] = NULL;
370 ldm->mod_bvalues[ nvals ] = ( struct berval * )
371 ch_malloc( sizeof( struct berval ));
372 ldm->mod_bvalues[ nvals ]->bv_val = value;
373 ldm->mod_bvalues[ nvals ]->bv_len = len;
377 ldmarr[ nops ] = NULL;
380 /* Actually perform the LDAP operation */
382 LDAP_LOG ( OPERATION, DETAIL1,
383 "op_ldap_modify: replica %s:%d - modify dn \"%s\"\n",
384 ri->ri_hostname, ri->ri_port, re->re_dn );
386 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
387 ri->ri_hostname, ri->ri_port, re->re_dn );
389 rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
391 free_ldmarr( ldmarr );
399 * Perform an ldap delete operation.
411 LDAP_LOG ( OPERATION, ARGS,
412 "op_ldap_delete: replica %s:%d - delete dn \"%s\"\n",
413 ri->ri_hostname, ri->ri_port, re->re_dn );
415 Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
416 ri->ri_hostname, ri->ri_port, re->re_dn );
418 rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
427 * Perform an ldap modrdn operation.
429 #define GOT_NEWRDN 0x1
430 #define GOT_DELOLDRDN 0x2
431 #define GOT_NEWSUP 0x4
433 #define GOT_MODDN_REQ (GOT_NEWRDN|GOT_DELOLDRDN)
434 #define GOT_ALL_MODDN(f) (((f) & GOT_MODDN_REQ) == GOT_MODDN_REQ)
451 if ( re->re_mods == NULL ) {
452 *errmsg = "No arguments given";
454 LDAP_LOG ( OPERATION, ERR,
455 "op_ldap_modrdn: Error: no arguments\n" , 0, 0, 0 );
457 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
464 * Get the arguments: should see newrdn: and deleteoldrdn: args.
466 for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
467 if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
468 if( state & GOT_NEWRDN ) {
470 LDAP_LOG ( OPERATION, ERR,
471 "op_ldap_modrdn: Error: multiple newrdn arg \"%s\"\n",
472 mi[ i ].mi_val, 0, 0 );
474 Debug( LDAP_DEBUG_ANY,
475 "Error: op_ldap_modrdn: multiple newrdn arg \"%s\"\n",
476 mi[ i ].mi_val, 0, 0 );
478 *errmsg = "Multiple newrdn argument";
482 newrdn = mi[ i ].mi_val;
485 } else if ( !strcmp( mi[ i ].mi_type, T_DELOLDRDNSTR )) {
486 if( state & GOT_DELOLDRDN ) {
488 LDAP_LOG ( OPERATION, ERR,
489 "op_ldap_modrdn: Error: multiple deleteoldrdn arg \"%s\"\n",
490 mi[ i ].mi_val, 0, 0 );
492 Debug( LDAP_DEBUG_ANY,
493 "Error: op_ldap_modrdn: multiple deleteoldrdn arg \"%s\"\n",
494 mi[ i ].mi_val, 0, 0 );
496 *errmsg = "Multiple newrdn argument";
500 state |= GOT_DELOLDRDN;
501 if ( !strcmp( mi[ i ].mi_val, "0" )) {
503 } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
507 LDAP_LOG ( OPERATION, ERR,
508 "op_ldap_modrdn: Error: bad deleteoldrdn arg \"%s\"\n",
509 mi[ i ].mi_val, 0, 0 );
511 Debug( LDAP_DEBUG_ANY,
512 "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
513 mi[ i ].mi_val, 0, 0 );
515 *errmsg = "Incorrect argument to deleteoldrdn";
519 } else if ( !strcmp( mi[ i ].mi_type, T_NEWSUPSTR )) {
520 if( state & GOT_NEWSUP ) {
522 LDAP_LOG ( OPERATION, ERR,
523 "op_ldap_modrdn: Error: multiple newsuperior arg \"%s\"\n",
524 mi[ i ].mi_val, 0, 0 );
526 Debug( LDAP_DEBUG_ANY,
527 "Error: op_ldap_modrdn: multiple newsuperior arg \"%s\"\n",
528 mi[ i ].mi_val, 0, 0 );
530 *errmsg = "Multiple newsuperior argument";
534 newsup = mi[ i ].mi_val;
539 LDAP_LOG ( OPERATION, ERR,
540 "op_ldap_modrdn: Error: bad type \"%s\"\n",
541 mi[ i ].mi_type, 0, 0 );
543 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
544 mi[ i ].mi_type, 0, 0 );
546 *errmsg = "Bad value in replication log entry";
552 * Punt if we don't have all the args.
554 if ( !GOT_ALL_MODDN(state) ) {
556 LDAP_LOG ( OPERATION, ERR,
557 "op_ldap_modrdn: Error: missing arguments\n" , 0, 0, 0 );
559 Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
562 *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
567 if ( ldap_debug & LDAP_DEBUG_ARGS ) {
570 int buf2len = strlen( re->re_dn ) + strlen( mi->mi_val ) + 11;
572 snprintf( buf, sizeof(buf), "%s:%d", ri->ri_hostname, ri->ri_port );
574 buf2 = (char *) ch_malloc( buf2len );
575 snprintf( buf2, buf2len, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
578 LDAP_LOG ( OPERATION, ARGS,
579 "op_ldap_modrdn: replica %s - modify rdn %s (flag: %d)\n",
580 buf, buf2, drdnflag );
582 Debug( LDAP_DEBUG_ARGS,
583 "replica %s - modify rdn %s (flag: %d)\n",
584 buf, buf2, drdnflag );
588 #endif /* LDAP_DEBUG */
593 rc = ldap_rename2_s( ri->ri_ldp, re->re_dn, newrdn, newsup, drdnflag );
595 ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
602 * Allocate and initialize an ldapmod struct.
605 alloc_ldapmod( void )
609 ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
610 ldm->mod_type = NULL;
611 ldm->mod_bvalues = ( struct berval ** ) NULL;
618 * Free an ldapmod struct associated mod_bvalues. NOTE - it is assumed
619 * that mod_bvalues and mod_type contain pointers to the same block of memory
620 * pointed to by the repl struct. Therefore, it's not freed here.
631 if ( ldm->mod_bvalues != NULL ) {
632 for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
633 free( ldm->mod_bvalues[ i ] );
635 free( ldm->mod_bvalues );
643 * Free an an array of LDAPMod pointers and the LDAPMod structs they point
652 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
653 free_ldapmod( ldmarr[ i ] );
660 * Create a berval with a single value.
662 static struct berval **
663 make_singlevalued_berval(
669 p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
670 p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
672 p[ 0 ]->bv_val = value;
673 p[ 0 ]->bv_len = len;
679 * Given a modification type (string), return an enumerated type.
680 * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
687 if ( !strcmp( type, T_MODSEPSTR )) {
690 if ( !strcmp( type, T_MODOPADDSTR )) {
691 return( T_MODOPADD );
693 if ( !strcmp( type, T_MODOPREPLACESTR )) {
694 return( T_MODOPREPLACE );
696 if ( !strcmp( type, T_MODOPDELETESTR )) {
697 return( T_MODOPDELETE );
704 * Perform an LDAP unbind operation. If replica is NULL, or the
705 * repl_ldp is NULL, just return LDAP_SUCCESS. Otherwise, unbind,
706 * set the ldp to NULL, and return the result of the unbind call.
713 int rc = LDAP_SUCCESS;
715 if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
716 rc = ldap_unbind( ri->ri_ldp );
717 if ( rc != LDAP_SUCCESS ) {
719 LDAP_LOG ( OPERATION, ERR,
720 "do_unbind: ldap_unbind failed for %s:%d: %s\n",
721 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
723 Debug( LDAP_DEBUG_ANY,
724 "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
725 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
736 * Perform an LDAP bind operation to the replication site given
737 * by replica. If replica->repl_ldp is non-NULL, then we unbind
738 * from the replica before rebinding. It should be safe to call
739 * this to re-connect if the replica's connection goes away
742 * Returns 0 on success, -1 if an LDAP error occurred, and a return
743 * code > 0 if some other error occurred, e.g. invalid bind method.
744 * If an LDAP error occurs, the LDAP error is returned in lderr.
758 LDAP_LOG ( OPERATION, ERR, "do_bind: null ri ptr\n" , 0, 0, 0 );
760 Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
762 return( BIND_ERR_BADRI );
766 if ( ri->ri_ldp != NULL ) {
767 ldrc = ldap_unbind( ri->ri_ldp );
768 if ( ldrc != LDAP_SUCCESS ) {
770 LDAP_LOG ( OPERATION, ERR,
771 "do_bind: ldap_unbind failed: %s\n", ldap_err2string( ldrc ), 0, 0 );
773 Debug( LDAP_DEBUG_ANY,
774 "Error: do_bind: ldap_unbind failed: %s\n",
775 ldap_err2string( ldrc ), 0, 0 );
782 LDAP_LOG ( OPERATION, ARGS,
783 "do_bind: Initializing session to %s:%d\n",
784 ri->ri_hostname, ri->ri_port, 0 );
786 Debug( LDAP_DEBUG_ARGS, "Initializing session to %s:%d\n",
787 ri->ri_hostname, ri->ri_port, 0 );
790 ri->ri_ldp = ldap_init( ri->ri_hostname, ri->ri_port );
791 if ( ri->ri_ldp == NULL ) {
793 LDAP_LOG ( OPERATION, ERR,
794 "do_bind: ldap_init (%s, %d) failed: %s\n",
795 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
797 Debug( LDAP_DEBUG_ANY, "Error: ldap_init(%s, %d) failed: %s\n",
798 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
800 return( BIND_ERR_OPEN );
803 { /* set version 3 */
804 int err, version = 3;
805 err = ldap_set_option(ri->ri_ldp,
806 LDAP_OPT_PROTOCOL_VERSION, &version);
808 if( err != LDAP_OPT_SUCCESS ) {
810 LDAP_LOG ( OPERATION, ERR, "do_bind: "
811 "Error: ldap_set_option(%s, LDAP_OPT_VERSION, 3) failed!\n",
812 ri->ri_hostname, 0, 0 );
814 Debug( LDAP_DEBUG_ANY,
815 "Error: ldap_set_option(%s, LDAP_OPT_VERSION, 3) failed!\n",
816 ri->ri_hostname, NULL, NULL );
819 ldap_unbind( ri->ri_ldp );
821 return BIND_ERR_VERSION;
826 * Set ldap library options to (1) not follow referrals, and
827 * (2) restart the select() system call.
831 err = ldap_set_option(ri->ri_ldp, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
833 if( err != LDAP_OPT_SUCCESS ) {
835 LDAP_LOG ( OPERATION, ERR, "do_bind: "
836 "Error: ldap_set_option(%s, REFERRALS, OFF) failed!\n",
837 ri->ri_hostname, 0, 0 );
839 Debug( LDAP_DEBUG_ANY,
840 "Error: ldap_set_option(%s,REFERRALS, OFF) failed!\n",
841 ri->ri_hostname, NULL, NULL );
843 ldap_unbind( ri->ri_ldp );
845 return BIND_ERR_REFERRALS;
848 ldap_set_option(ri->ri_ldp, LDAP_OPT_RESTART, LDAP_OPT_ON);
851 int err = ldap_start_tls_s(ri->ri_ldp, NULL, NULL);
853 if( err != LDAP_SUCCESS ) {
855 LDAP_LOG ( OPERATION, ERR, "do_bind: "
856 "%s: ldap_start_tls failed: %s (%d)\n",
857 ri->ri_tls == TLS_CRITICAL ? "Error" : "Warning",
858 ldap_err2string( err ), err );
860 Debug( LDAP_DEBUG_ANY,
861 "%s: ldap_start_tls failed: %s (%d)\n",
862 ri->ri_tls == TLS_CRITICAL ? "Error" : "Warning",
863 ldap_err2string( err ), err );
866 if( ri->ri_tls == TLS_CRITICAL ) {
868 ldap_unbind( ri->ri_ldp );
870 return BIND_ERR_TLS_FAILED;
872 ri->ri_tls = TLS_OFF;
877 switch ( ri->ri_bind_method ) {
880 * Bind with a plaintext password.
883 LDAP_LOG ( OPERATION, ARGS,
884 "do_bind: bind to %s:%d as %s (simple)\n",
885 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
887 Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
888 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
890 ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
892 if ( ldrc != LDAP_SUCCESS ) {
894 LDAP_LOG ( OPERATION, ERR, "do_bind: "
895 "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
896 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ) );
898 Debug( LDAP_DEBUG_ANY,
899 "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
900 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
903 ldap_unbind( ri->ri_ldp );
905 return( BIND_ERR_SIMPLE_FAILED );
911 LDAP_LOG ( OPERATION, ARGS,
912 "do_bind: bind to %s as %s via %s (SASL)\n",
914 ri->ri_authcId ? ri->ri_authcId : "-",
917 Debug( LDAP_DEBUG_ARGS, "bind to %s as %s via %s (SASL)\n",
919 ri->ri_authcId ? ri->ri_authcId : "-",
923 #ifdef HAVE_CYRUS_SASL
924 if( ri->ri_secprops != NULL ) {
925 int err = ldap_set_option(ri->ri_ldp,
926 LDAP_OPT_X_SASL_SECPROPS, ri->ri_secprops);
928 if( err != LDAP_OPT_SUCCESS ) {
930 LDAP_LOG ( OPERATION, ERR, "do_bind: "
931 "Error: ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
932 ri->ri_hostname, ri->ri_secprops, 0 );
934 Debug( LDAP_DEBUG_ANY,
935 "Error: ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
936 ri->ri_hostname, ri->ri_secprops, NULL );
938 ldap_unbind( ri->ri_ldp );
940 return BIND_ERR_SASL_FAILED;
945 void *defaults = lutil_sasl_defaults( ri->ri_ldp, ri->ri_saslmech,
946 ri->ri_realm, ri->ri_authcId, ri->ri_password, ri->ri_authzId );
948 ldrc = ldap_sasl_interactive_bind_s( ri->ri_ldp, ri->ri_bind_dn,
949 ri->ri_saslmech, NULL, NULL,
950 LDAP_SASL_QUIET, lutil_sasl_interact, defaults );
952 lutil_sasl_freedefs( defaults );
953 if ( ldrc != LDAP_SUCCESS ) {
955 LDAP_LOG ( OPERATION, ERR, "do_bind: "
956 "Error: LDAP SASL for %s:%d failed: %s\n",
957 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ) );
959 Debug( LDAP_DEBUG_ANY, "Error: LDAP SASL for %s:%d failed: %s\n",
960 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
963 ldap_unbind( ri->ri_ldp );
965 return( BIND_ERR_SASL_FAILED );
971 LDAP_LOG ( OPERATION, ERR, "do_bind: "
972 "Error: do_bind: SASL not supported %s:%d\n",
973 ri->ri_hostname, ri->ri_port, 0 );
975 Debug( LDAP_DEBUG_ANY,
976 "Error: do_bind: SASL not supported %s:%d\n",
977 ri->ri_hostname, ri->ri_port, NULL );
979 ldap_unbind( ri->ri_ldp );
981 return( BIND_ERR_BAD_ATYPE );
986 LDAP_LOG ( OPERATION, ERR, "do_bind: "
987 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
988 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
990 Debug( LDAP_DEBUG_ANY,
991 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
992 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
994 ldap_unbind( ri->ri_ldp );
996 return( BIND_ERR_BAD_ATYPE );
1002 LDAPControl *ctrls[2];
1006 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1007 c.ldctl_value.bv_val = NULL;
1008 c.ldctl_value.bv_len = 0;
1009 c.ldctl_iscritical = 0;
1011 err = ldap_set_option(ri->ri_ldp, LDAP_OPT_SERVER_CONTROLS, &ctrls);
1013 if( err != LDAP_OPT_SUCCESS ) {
1015 LDAP_LOG ( OPERATION, ERR, "do_bind: "
1016 "ldap_set_option(%s, SERVER_CONTROLS, ManageDSAit) failed!\n",
1017 ri->ri_hostname, 0, 0 );
1019 Debug( LDAP_DEBUG_ANY, "Error: "
1020 "ldap_set_option(%s, SERVER_CONTROLS, ManageDSAit) failed!\n",
1021 ri->ri_hostname, NULL, NULL );
1023 ldap_unbind( ri->ri_ldp );
1025 return BIND_ERR_MANAGEDSAIT;
1037 * For debugging. Print the contents of an ldmarr array.
1049 for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
1052 LDAP_LOG ( OPERATION, INFO, "dump_ldm_array: "
1053 "Trace (%ld): *** ldmarr[ %d ] contents:\n",
1054 (long) getpid(), i, 0 );
1055 LDAP_LOG ( OPERATION, INFO, "dump_ldm_array: "
1056 "Trace (%ld): *** ldm->mod_op: %d\n",
1057 (long) getpid(), ldm->mod_op, 0 );
1058 LDAP_LOG ( OPERATION, INFO, "dump_ldm_array: "
1059 "Trace (%ld): *** ldm->mod_type: %s\n",
1060 (long) getpid(), ldm->mod_type, 0 );
1062 Debug( LDAP_DEBUG_TRACE,
1063 "Trace (%ld): *** ldmarr[ %d ] contents:\n",
1064 (long) getpid(), i, 0 );
1065 Debug( LDAP_DEBUG_TRACE,
1066 "Trace (%ld): *** ldm->mod_op: %d\n",
1067 (long) getpid(), ldm->mod_op, 0 );
1068 Debug( LDAP_DEBUG_TRACE,
1069 "Trace (%ld): *** ldm->mod_type: %s\n",
1070 (long) getpid(), ldm->mod_type, 0 );
1072 if ( ldm->mod_bvalues != NULL ) {
1073 for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
1074 msgbuf = ch_malloc( b->bv_len + 512 );
1075 sprintf( msgbuf, "***** bv[ %d ] len = %ld, val = <%s>",
1076 j, b->bv_len, b->bv_val );
1078 LDAP_LOG ( OPERATION, INFO, "dump_ldm_array: "
1079 "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );
1081 Debug( LDAP_DEBUG_TRACE,
1082 "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );