]> git.sur5r.net Git - openldap/blob - servers/slurpd/ldap_op.c
1673a12e785deca937496c3c015c8d04d535915b
[openldap] / servers / slurpd / ldap_op.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1996 Regents of the University of Michigan.
4  * All rights reserved.
5  *
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.
12  */
13
14 /*
15  * ldap_op.c - routines to perform LDAP operations
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/stdlib.h>
23
24 #include <ac/errno.h>
25 #include <ac/string.h>
26 #include <ac/ctype.h>
27 #include <ac/time.h>
28 #include <ac/unistd.h>
29
30 #include <ldap.h>
31
32 #include "slurp.h"
33
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 * ));
47
48
49 /*
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
57  *   entry.
58  */
59 int
60 do_ldap(
61     Ri          *ri,
62     Re          *re,
63     char        **errmsg
64 )
65 {
66     int rc = 0;
67     int lderr = LDAP_SUCCESS;
68     int retry = 2;
69
70     *errmsg = NULL;
71
72     while ( retry > 0 ) {
73         if ( ri->ri_ldp == NULL ) {
74             rc = do_bind( ri, &lderr );
75
76             if ( rc != BIND_OK ) {
77                         return DO_LDAP_ERR_RETRYABLE;
78             }
79         }
80
81         switch ( re->re_changetype ) {
82         case T_ADDCT:
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 ),
88                         re->re_dn, 0 );
89             }
90             break;
91         case T_MODIFYCT:
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 ),
97                         re->re_dn, 0 );
98             }
99             break;
100         case T_DELETECT:
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 ),
106                         re->re_dn, 0 );
107             }
108             break;
109         case T_MODRDNCT:
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 ),
115                         re->re_dn, 0 );
116             }
117             break;
118         default:
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;
123         }
124
125         /*
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.
129          */
130         if ( lderr == LDAP_SUCCESS ) {
131             return DO_LDAP_OK;
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 );
135             retry--;
136         } else {
137             return DO_LDAP_ERR_FATAL;
138         }
139     }
140     return DO_LDAP_ERR_FATAL;
141 }
142
143
144
145
146 /*
147  * Perform an ldap add operation.
148  */
149 static int
150 op_ldap_add(
151     Ri          *ri,
152     Re          *re,
153     char        **errmsg
154 )
155 {
156     Mi          *mi;
157     int         nattrs, rc = 0, i;
158     LDAPMod     *ldm, **ldmarr;
159     int         lderr = 0;
160
161     nattrs = i = 0;
162     ldmarr = NULL;
163
164     /*
165      * Construct a null-terminated array of LDAPMod structs.
166      */
167     mi = re->re_mods;
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;
175         ldm->mod_bvalues =
176                 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
177         i++;
178         nattrs++;
179     }
180
181     if ( ldmarr != NULL ) {
182         ldmarr[ nattrs ] = NULL;
183
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 );
188
189         ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
190
191     } else {
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 );
195     }
196     free_ldmarr( ldmarr );
197     return( lderr ); 
198 }
199
200
201
202
203 /*
204  * Perform an ldap modify operation.
205  */
206 #define AWAITING_OP -1
207 static int
208 op_ldap_modify(
209     Ri          *ri,
210     Re          *re,
211     char        **errmsg
212 )
213 {
214     Mi          *mi;
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;
219     int         i, len;
220     char        *type, *value;
221     int         rc = 0;
222
223     state = AWAITING_OP;
224     nvals = 0;
225     nops = 0;
226     ldmarr = NULL;
227
228     if ( re->re_mods == NULL ) {
229         *errmsg = "No arguments given";
230         Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
231                 0, 0, 0 );
232             return -1;
233     }
234
235     /*
236      * Construct a null-terminated array of LDAPMod structs.
237      */
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 )) {
243         case T_MODSEP:
244             state = T_MODSEP; /* Got a separator line "-\n" */
245             continue;
246         case T_MODOPADD:
247             state = T_MODOPADD;
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;
253             nvals = 0;
254             nops++;
255             break;
256         case T_MODOPREPLACE:
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;
263             nvals = 0;
264             nops++;
265             break;
266         case T_MODOPDELETE:
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;
273             nvals = 0;
274             nops++;
275             break;
276         default:
277             if ( state == AWAITING_OP ) {
278                 Debug( LDAP_DEBUG_ANY,
279                         "Error: op_ldap_modify: unknown mod type \"%s\"\n",
280                         type, 0, 0 );
281                 continue;
282             }
283
284             /*
285              * We should have an attribute: value pair here.
286              * Construct the mod_bvalues part of the ldapmod struct.
287              */
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 );
292                 continue;
293             }
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;
302             nvals++;
303         }
304     }
305     ldmarr[ nops ] = NULL;
306
307     if ( nops > 0 ) {
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 );
312     }
313     free_ldmarr( ldmarr );
314     return( rc );
315 }
316
317
318
319
320 /*
321  * Perform an ldap delete operation.
322  */
323 static int
324 op_ldap_delete(
325     Ri          *ri,
326     Re          *re,
327     char        **errmsg
328 )
329 {
330     int         rc;
331
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 );
335
336     return( rc );
337 }
338
339
340
341
342 /*
343  * Perform an ldap modrdn operation.
344  */
345 #define GOT_NEWRDN              0x1
346 #define GOT_DELOLDRDN   0x2
347 #define GOT_NEWSUP              0x4
348
349 #define GOT_MODDN_REQ   (GOT_NEWRDN|GOT_DELOLDRDN)
350 #define GOT_ALL_MODDN(f)        (((f) & GOT_MODDN_REQ) == GOT_MODDN_REQ)
351 static int
352 op_ldap_modrdn(
353     Ri          *ri,
354     Re          *re,
355     char        **errmsg
356 )
357 {
358     int         rc = 0;
359     Mi          *mi;
360     int         i;
361         int             lderr = 0;
362     int         state = 0;
363     int         drdnflag = -1;
364     char        *newrdn;
365         char    *newsup = NULL;
366
367     if ( re->re_mods == NULL ) {
368         *errmsg = "No arguments given";
369         Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
370                 0, 0, 0 );
371             return -1;
372     }
373
374     /*
375      * Get the arguments: should see newrdn: and deleteoldrdn: args.
376      */
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";
384                 return -1;
385                 }
386
387             newrdn = mi[ i ].mi_val;
388             state |= GOT_NEWRDN;
389
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";
396                 return -1;
397                 }
398
399             state |= GOT_DELOLDRDN;
400             if ( !strcmp( mi[ i ].mi_val, "0" )) {
401                 drdnflag = 0;
402             } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
403                 drdnflag = 1;
404             } else {
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";
409                 return -1;
410             }
411
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";
418                 return -1;
419                 }
420
421                 newrdn = mi[ i ].mi_val;
422             state |= GOT_NEWSUP;
423
424         } else {
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";
428             return -1;
429         }
430     }
431
432     /*
433      * Punt if we don't have all the args.
434      */
435     if ( GOT_ALL_MODDN(state) ) {
436         Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
437                 0, 0, 0 );
438         *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
439         return -1;
440     }
441
442 #ifdef LDAP_DEBUG
443     if ( ldap_debug & LDAP_DEBUG_ARGS ) {
444         char buf[ 256 ];
445         char *buf2;
446         sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
447         buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
448                 + 10 );
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 );
453         free( buf2 );
454     }
455 #endif /* LDAP_DEBUG */
456
457     /* Do the modrdn */
458     rc = ldap_rename2_s( ri->ri_ldp, re->re_dn, mi->mi_val, newsup, drdnflag );
459
460         ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
461     return( lderr );
462 }
463
464
465
466 /*
467  * Allocate and initialize an ldapmod struct.
468  */
469 static LDAPMod *
470 alloc_ldapmod( void )
471 {
472     LDAPMod     *ldm;
473
474     ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
475     ldm->mod_type = NULL;
476     ldm->mod_bvalues = ( struct berval ** ) NULL;
477     return( ldm );
478 }
479
480
481
482 /*
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.
486  */
487 static void
488 free_ldapmod(
489 LDAPMod *ldm )
490 {
491     int         i;
492
493     if ( ldm == NULL ) {
494         return;
495     }
496     if ( ldm->mod_bvalues != NULL ) {
497         for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
498             free( ldm->mod_bvalues[ i ] );
499         }
500         free( ldm->mod_bvalues );
501     }
502     free( ldm );
503     return;
504 }
505
506
507 /*
508  * Free an an array of LDAPMod pointers and the LDAPMod structs they point
509  * to.
510  */
511 static void
512 free_ldmarr(
513 LDAPMod **ldmarr )
514 {
515     int i;
516
517     for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
518         free_ldapmod( ldmarr[ i ] );
519     }
520     free( ldmarr );
521 }
522
523
524 /*
525  * Create a berval with a single value. 
526  */
527 static struct berval **
528 make_singlevalued_berval( 
529 char    *value,
530 int     len )
531 {
532     struct berval **p;
533
534     p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
535     p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
536     p[ 1 ] = NULL;
537     p[ 0 ]->bv_val = value;
538     p[ 0 ]->bv_len = len;
539     return( p );
540 }
541
542
543 /*
544  * Given a modification type (string), return an enumerated type.
545  * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
546  * there.
547  */
548 static int
549 getmodtype( 
550 char *type )
551 {
552     if ( !strcmp( type, T_MODSEPSTR )) {
553         return( T_MODSEP );
554     }
555     if ( !strcmp( type, T_MODOPADDSTR )) {
556         return( T_MODOPADD );
557     }
558     if ( !strcmp( type, T_MODOPREPLACESTR )) {
559         return( T_MODOPREPLACE );
560     }
561     if ( !strcmp( type, T_MODOPDELETESTR )) {
562         return( T_MODOPDELETE );
563     }
564     return( T_ERR );
565 }
566
567
568 /*
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.
572  */
573 static int
574 do_unbind(
575     Ri  *ri
576 )
577 {
578     int         rc = LDAP_SUCCESS;
579
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 ) );
586         }
587         ri->ri_ldp = NULL;
588     }
589     return rc;
590 }
591
592
593
594 /*
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
599  * for some reason.
600  *
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.
604  */
605 static int
606 do_bind( 
607     Ri  *ri,
608     int *lderr
609 )
610 {
611     int         ldrc;
612
613     *lderr = 0;
614
615     if ( ri == NULL ) {
616         Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
617         return( BIND_ERR_BADRI );
618     }
619
620     if ( ri->ri_ldp != NULL ) {
621         ldrc = ldap_unbind( ri->ri_ldp );
622         if ( ldrc != LDAP_SUCCESS ) {
623             Debug( LDAP_DEBUG_ANY,
624                     "Error: do_bind: ldap_unbind failed: %s\n",
625                     ldap_err2string( ldrc ), 0, 0 );
626         }
627         ri->ri_ldp = NULL;
628     }
629
630     Debug( LDAP_DEBUG_ARGS, "Initializing session to %s:%d\n",
631             ri->ri_hostname, ri->ri_port, 0 );
632
633     ri->ri_ldp = ldap_init( ri->ri_hostname, ri->ri_port );
634     if ( ri->ri_ldp == NULL ) {
635                 Debug( LDAP_DEBUG_ANY, "Error: ldap_init(%s, %d) failed: %s\n",
636                         ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
637                 return( BIND_ERR_OPEN );
638     }
639
640         {       /* set version 3 */
641                 int err, version = 3;
642                 err = ldap_set_option(ri->ri_ldp,
643                         LDAP_OPT_PROTOCOL_VERSION, &version);
644
645                 if( err != LDAP_OPT_SUCCESS ) {
646                         Debug( LDAP_DEBUG_ANY,
647                                 "Error: ldap_set_option(%s, LDAP_OPT_VERSION, 3) failed!\n",
648                                 ri->ri_hostname, NULL, NULL );
649
650                         ldap_unbind( ri->ri_ldp );
651                         ri->ri_ldp = NULL;
652                         return BIND_ERR_VERSION;
653                 }
654         }
655
656     /*
657      * Set ldap library options to (1) not follow referrals, and 
658      * (2) restart the select() system call.
659      */
660         {
661                 int err;
662                 err = ldap_set_option(ri->ri_ldp, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
663
664                 if( err != LDAP_OPT_SUCCESS ) {
665                         Debug( LDAP_DEBUG_ANY,
666                                 "Error: ldap_set_option(%s,REFERRALS, OFF) failed!\n",
667                                 ri->ri_hostname, NULL, NULL );
668                         ldap_unbind( ri->ri_ldp );
669                         ri->ri_ldp = NULL;
670                         return BIND_ERR_REFERRALS;
671                 }
672         }
673         ldap_set_option(ri->ri_ldp, LDAP_OPT_RESTART, LDAP_OPT_ON);
674
675     switch ( ri->ri_bind_method ) {
676     case AUTH_SIMPLE:
677         /*
678          * Bind with a plaintext password.
679          */
680         Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
681                 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
682         ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
683                 ri->ri_password );
684         if ( ldrc != LDAP_SUCCESS ) {
685             Debug( LDAP_DEBUG_ANY,
686                     "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
687                     ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
688             *lderr = ldrc;
689                 ldap_unbind( ri->ri_ldp );
690                 ri->ri_ldp = NULL;
691             return( BIND_ERR_SIMPLE_FAILED );
692         }
693         break;
694     default:
695         Debug(  LDAP_DEBUG_ANY,
696                 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
697                 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
698         ldap_unbind( ri->ri_ldp );
699         ri->ri_ldp = NULL;
700         return( BIND_ERR_BAD_ATYPE );
701     }
702
703         {
704                 int err;
705                 LDAPControl c;
706                 LDAPControl *ctrls[2];
707                 ctrls[0] = &c;
708                 ctrls[1] = NULL;
709
710                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
711                 c.ldctl_value.bv_val = NULL;
712                 c.ldctl_value.bv_len = 0;
713                 c.ldctl_iscritical = 1;
714
715                 err = ldap_set_option(ri->ri_ldp, LDAP_OPT_SERVER_CONTROLS, &ctrls);
716
717                 if( err != LDAP_OPT_SUCCESS ) {
718                         Debug( LDAP_DEBUG_ANY,
719                                 "Error: ldap_set_option(%s, SERVER_CONTROLS, ManageDSAit) failed!\n",
720                                 ri->ri_hostname, NULL, NULL );
721                         ldap_unbind( ri->ri_ldp );
722                         ri->ri_ldp = NULL;
723                         return BIND_ERR_MANAGEDSAIT;
724                 }
725         }
726
727         return( BIND_OK );
728 }
729
730
731
732
733
734 /*
735  * For debugging.  Print the contents of an ldmarr array.
736  */
737 static void
738 dump_ldm_array(
739     LDAPMod **ldmarr
740 )
741 {
742     int                  i, j;
743     LDAPMod             *ldm;
744     struct berval       *b;
745     char                *msgbuf;
746
747     for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
748         ldm = ldmarr[ i ];
749         Debug( LDAP_DEBUG_TRACE,
750                 "Trace (%ld): *** ldmarr[ %d ] contents:\n",
751                 (long) getpid(), i, 0 );
752         Debug( LDAP_DEBUG_TRACE,
753                 "Trace (%ld): *** ldm->mod_op: %d\n",
754                 (long) getpid(), ldm->mod_op, 0 );
755         Debug( LDAP_DEBUG_TRACE,
756                 "Trace (%ld): *** ldm->mod_type: %s\n",
757                 (long) getpid(), ldm->mod_type, 0 );
758         if ( ldm->mod_bvalues != NULL ) {
759             for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
760                 msgbuf = ch_malloc( b->bv_len + 512 );
761                 sprintf( msgbuf, "***** bv[ %d ] len = %ld, val = <%s>",
762                         j, b->bv_len, b->bv_val );
763                 Debug( LDAP_DEBUG_TRACE,
764                         "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );
765                 free( msgbuf );
766             }
767         }
768     }
769 }