]> git.sur5r.net Git - openldap/blob - servers/slurpd/ldap_op.c
Remove unused vars/function
[openldap] / servers / slurpd / ldap_op.c
1 /*
2  * Copyright (c) 1996 Regents of the University of Michigan.
3  * All rights reserved.
4  *
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.
11  */
12
13 /*
14  * ldap_op.c - routines to perform LDAP operations
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/stdlib.h>
22
23 #include <ac/errno.h>
24 #include <ac/string.h>
25 #include <ac/ctype.h>
26 #include <ac/time.h>
27 #include <ac/unistd.h>
28
29 #include <ac/krb.h>
30
31 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
32 /* Get LDAP->ld_lberoptions.  Must precede slurp.h, both define ldap_debug. */
33 #include "../../libraries/libldap/ldap-int.h"
34 #endif
35
36 #include <lber.h>
37 #include <ldap.h>
38
39 #include "slurp.h"
40
41 /* Forward references */
42 static struct berval **make_singlevalued_berval LDAP_P(( char   *, int ));
43 static int op_ldap_add LDAP_P(( Ri *, Re *, char ** ));
44 static int op_ldap_modify LDAP_P(( Ri *, Re *, char ** ));
45 static int op_ldap_delete LDAP_P(( Ri *, Re *, char ** ));
46 static int op_ldap_modrdn LDAP_P(( Ri *, Re *, char ** ));
47 static LDAPMod *alloc_ldapmod LDAP_P(( void ));
48 static void free_ldapmod LDAP_P(( LDAPMod * ));
49 static void free_ldmarr LDAP_P(( LDAPMod ** ));
50 static int getmodtype LDAP_P(( char * ));
51 static void dump_ldm_array LDAP_P(( LDAPMod ** ));
52 static char **read_krbnames LDAP_P(( Ri * ));
53 #ifdef HAVE_KERBEROS
54 static void upcase LDAP_P(( char * ));
55 #endif
56 static int do_bind LDAP_P(( Ri *, int * ));
57 static int do_unbind LDAP_P(( Ri * ));
58
59
60 static char *kattrs[] = {"kerberosName", NULL };
61 static struct timeval kst = {30L, 0L};
62
63
64
65 /*
66  * Determine the type of ldap operation being performed and call the
67  * appropriate routine.
68  * - If successful, returns ERR_DO_LDAP_OK
69  * - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
70  *   The caller should wait a while and retry the operation.
71  * - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned.  The caller
72  *   should reject the operation and continue with the next replication
73  *   entry.
74  */
75 int
76 do_ldap(
77     Ri          *ri,
78     Re          *re,
79     char        **errmsg
80 )
81 {
82     int rc = 0;
83     int lderr = LDAP_SUCCESS;
84     int retry = 2;
85
86     *errmsg = NULL;
87
88     while ( retry > 0 ) {
89         if ( ri->ri_ldp == NULL ) {
90             rc = do_bind( ri, &lderr );
91             if ( rc != BIND_OK ) {
92                 return DO_LDAP_ERR_RETRYABLE;
93             }
94         }
95
96         switch ( re->re_changetype ) {
97         case T_ADDCT:
98             lderr = op_ldap_add( ri, re, errmsg );
99             if ( lderr != LDAP_SUCCESS ) {
100                 Debug( LDAP_DEBUG_ANY,
101                         "Error: ldap_add_s failed adding \"%s\": %s\n",
102                         *errmsg ? *errmsg : ldap_err2string( lderr ),
103                         re->re_dn, 0 );
104             }
105             break;
106         case T_MODIFYCT:
107             lderr = op_ldap_modify( ri, re, errmsg );
108             if ( lderr != LDAP_SUCCESS ) {
109                 Debug( LDAP_DEBUG_ANY,
110                         "Error: ldap_modify_s failed modifying \"%s\": %s\n",
111                         *errmsg ? *errmsg : ldap_err2string( lderr ),
112                         re->re_dn, 0 );
113             }
114             break;
115         case T_DELETECT:
116             lderr = op_ldap_delete( ri, re, errmsg );
117             if ( lderr != LDAP_SUCCESS ) {
118                 Debug( LDAP_DEBUG_ANY,
119                         "Error: ldap_delete_s failed deleting \"%s\": %s\n",
120                         *errmsg ? *errmsg : ldap_err2string( lderr ),
121                         re->re_dn, 0 );
122             }
123             break;
124         case T_MODRDNCT:
125             lderr = op_ldap_modrdn( ri, re, errmsg );
126             if ( lderr != LDAP_SUCCESS ) {
127                 Debug( LDAP_DEBUG_ANY,
128                         "Error: ldap_modrdn_s failed modifying %s: %s\n",
129                         *errmsg ? *errmsg : ldap_err2string( lderr ),
130                         re->re_dn, 0 );
131             }
132             break;
133         default:
134             Debug( LDAP_DEBUG_ANY,
135                     "Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
136                     re->re_changetype, re->re_dn, 0 );
137             return DO_LDAP_ERR_FATAL;
138         }
139
140         /*
141          * Analyze return code.  If ok, just return.  If LDAP_SERVER_DOWN,
142          * we may have been idle long enough that the remote slapd timed
143          * us out.  Rebind and try again.
144          */
145         if ( lderr == LDAP_SUCCESS ) {
146             return DO_LDAP_OK;
147         } else if ( lderr == LDAP_SERVER_DOWN ) {
148             /* The LDAP server may have timed us out - rebind and try again */
149             (void) do_unbind( ri );
150             retry--;
151         } else {
152             return DO_LDAP_ERR_FATAL;
153         }
154     }
155     return DO_LDAP_ERR_FATAL;
156 }
157
158
159
160
161 /*
162  * Perform an ldap add operation.
163  */
164 static int
165 op_ldap_add(
166     Ri          *ri,
167     Re          *re,
168     char        **errmsg
169 )
170 {
171     Mi          *mi;
172     int         nattrs, rc = 0, i;
173     LDAPMod     *ldm, **ldmarr;
174     int         lderr = 0;
175
176     nattrs = i = 0;
177     ldmarr = NULL;
178
179     /*
180      * Construct a null-terminated array of LDAPMod structs.
181      */
182     mi = re->re_mods;
183     while ( mi[ i ].mi_type != NULL ) {
184         ldm = alloc_ldapmod();
185         ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
186                 ( nattrs + 2 ) * sizeof( LDAPMod * ));
187         ldmarr[ nattrs ] = ldm;
188         ldm->mod_op = LDAP_MOD_BVALUES;
189         ldm->mod_type = mi[ i ].mi_type;
190         ldm->mod_bvalues =
191                 make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
192         i++;
193         nattrs++;
194     }
195
196     if ( ldmarr != NULL ) {
197         ldmarr[ nattrs ] = NULL;
198
199         /* Perform the operation */
200         Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
201                 ri->ri_hostname, ri->ri_port, re->re_dn );
202         rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
203
204         ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
205
206     } else {
207         *errmsg = "No modifications to do";
208         Debug( LDAP_DEBUG_ANY,
209                "Error: op_ldap_add: no mods to do (%s)!\n", re->re_dn, 0, 0 );
210     }
211     free_ldmarr( ldmarr );
212     return( lderr ); 
213 }
214
215
216
217
218 /*
219  * Perform an ldap modify operation.
220  */
221 #define AWAITING_OP -1
222 static int
223 op_ldap_modify(
224     Ri          *ri,
225     Re          *re,
226     char        **errmsg
227 )
228 {
229     Mi          *mi;
230     int         state;  /* This code is a simple-minded state machine */
231     int         nvals;  /* Number of values we're modifying */
232     int         nops;   /* Number of LDAPMod structs in ldmarr */
233     LDAPMod     *ldm, **ldmarr;
234     int         i, len;
235     char        *type, *value;
236     int         rc = 0;
237
238     state = AWAITING_OP;
239     nvals = 0;
240     nops = 0;
241     ldmarr = NULL;
242
243     if ( re->re_mods == NULL ) {
244         *errmsg = "No arguments given";
245         Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
246                 0, 0, 0 );
247             return -1;
248     }
249
250     /*
251      * Construct a null-terminated array of LDAPMod structs.
252      */
253     for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
254         type = mi[ i ].mi_type;
255         value = mi[ i ].mi_val;
256         len = mi[ i ].mi_len;
257         switch ( getmodtype( type )) {
258         case T_MODSEP:
259             state = T_MODSEP; /* Got a separator line "-\n" */
260             continue;
261         case T_MODOPADD:
262             state = T_MODOPADD;
263             ldmarr = ( LDAPMod ** )
264                     ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
265             ldmarr[ nops ] = ldm = alloc_ldapmod();
266             ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
267             ldm->mod_type = value;
268             nvals = 0;
269             nops++;
270             break;
271         case T_MODOPREPLACE:
272             state = T_MODOPREPLACE;
273             ldmarr = ( LDAPMod ** )
274                     ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
275             ldmarr[ nops ] = ldm = alloc_ldapmod();
276             ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
277             ldm->mod_type = value;
278             nvals = 0;
279             nops++;
280             break;
281         case T_MODOPDELETE:
282             state = T_MODOPDELETE;
283             ldmarr = ( LDAPMod ** )
284                     ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
285             ldmarr[ nops ] = ldm = alloc_ldapmod();
286             ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
287             ldm->mod_type = value;
288             nvals = 0;
289             nops++;
290             break;
291         default:
292             if ( state == AWAITING_OP ) {
293                 Debug( LDAP_DEBUG_ANY,
294                         "Error: op_ldap_modify: unknown mod type \"%s\"\n",
295                         type, 0, 0 );
296                 continue;
297             }
298
299             /*
300              * We should have an attribute: value pair here.
301              * Construct the mod_bvalues part of the ldapmod struct.
302              */
303             if ( strcasecmp( type, ldm->mod_type )) {
304                 Debug( LDAP_DEBUG_ANY,
305                         "Error: malformed modify op, %s: %s (expecting %s:)\n",
306                         type, value, ldm->mod_type );
307                 continue;
308             }
309             ldm->mod_bvalues = ( struct berval ** )
310                     ch_realloc( ldm->mod_bvalues,
311                     ( nvals + 2 ) * sizeof( struct berval * ));
312             ldm->mod_bvalues[ nvals + 1 ] = NULL;
313             ldm->mod_bvalues[ nvals ] = ( struct berval * )
314                     ch_malloc( sizeof( struct berval ));
315             ldm->mod_bvalues[ nvals ]->bv_val = value;
316             ldm->mod_bvalues[ nvals ]->bv_len = len;
317             nvals++;
318         }
319     }
320     ldmarr[ nops ] = NULL;
321
322     if ( nops > 0 ) {
323         /* Actually perform the LDAP operation */
324         Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
325                 ri->ri_hostname, ri->ri_port, re->re_dn );
326         rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
327     }
328     free_ldmarr( ldmarr );
329     return( rc );
330 }
331
332
333
334
335 /*
336  * Perform an ldap delete operation.
337  */
338 static int
339 op_ldap_delete(
340     Ri          *ri,
341     Re          *re,
342     char        **errmsg
343 )
344 {
345     int         rc;
346
347     Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
348             ri->ri_hostname, ri->ri_port, re->re_dn );
349     rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
350
351     return( rc );
352 }
353
354
355
356
357 /*
358  * Perform an ldap modrdn operation.
359  */
360 #define GOT_NEWRDN              0x1
361 #define GOT_DELOLDRDN   0x2
362 #define GOT_NEWSUP              0x4
363
364 #define GOT_MODDN_REQ   (GOT_NEWRDN|GOT_DELOLDRDN)
365 #define GOT_ALL_MODDN(f)        (((f) & GOT_MODDN_REQ) == GOT_MODDN_REQ)
366 static int
367 op_ldap_modrdn(
368     Ri          *ri,
369     Re          *re,
370     char        **errmsg
371 )
372 {
373     int         rc = 0;
374     Mi          *mi;
375     int         i;
376         int             lderr = 0;
377     int         state = 0;
378     int         drdnflag = -1;
379     char        *newrdn;
380         char    *newsup = NULL;
381
382     if ( re->re_mods == NULL ) {
383         *errmsg = "No arguments given";
384         Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
385                 0, 0, 0 );
386             return -1;
387     }
388
389     /*
390      * Get the arguments: should see newrdn: and deleteoldrdn: args.
391      */
392     for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
393         if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
394                 if( state & GOT_NEWRDN ) {
395                 Debug( LDAP_DEBUG_ANY,
396                         "Error: op_ldap_modrdn: multiple newrdn arg \"%s\"\n",
397                         mi[ i ].mi_val, 0, 0 );
398                 *errmsg = "Multiple newrdn argument";
399                 return -1;
400                 }
401
402             newrdn = mi[ i ].mi_val;
403             state |= GOT_NEWRDN;
404
405         } else if ( !strcmp( mi[ i ].mi_type, T_DELOLDRDNSTR )) {
406                 if( state & GOT_DELOLDRDN ) {
407                 Debug( LDAP_DEBUG_ANY,
408                         "Error: op_ldap_modrdn: multiple deleteoldrdn arg \"%s\"\n",
409                         mi[ i ].mi_val, 0, 0 );
410                 *errmsg = "Multiple newrdn argument";
411                 return -1;
412                 }
413
414             state |= GOT_DELOLDRDN;
415             if ( !strcmp( mi[ i ].mi_val, "0" )) {
416                 drdnflag = 0;
417             } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
418                 drdnflag = 1;
419             } else {
420                 Debug( LDAP_DEBUG_ANY,
421                         "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
422                         mi[ i ].mi_val, 0, 0 );
423                 *errmsg = "Incorrect argument to deleteoldrdn";
424                 return -1;
425             }
426
427         } else if ( !strcmp( mi[ i ].mi_type, T_NEWSUPSTR )) {
428                 if( state & GOT_NEWSUP ) {
429                 Debug( LDAP_DEBUG_ANY,
430                         "Error: op_ldap_modrdn: multiple newsuperior arg \"%s\"\n",
431                         mi[ i ].mi_val, 0, 0 );
432                 *errmsg = "Multiple newrdn argument";
433                 return -1;
434                 }
435
436                 newrdn = mi[ i ].mi_val;
437             state |= GOT_NEWSUP;
438
439         } else {
440             Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
441                     mi[ i ].mi_type, 0, 0 );
442             *errmsg = "Bad value in replication log entry";
443             return -1;
444         }
445     }
446
447     /*
448      * Punt if we don't have all the args.
449      */
450     if ( GOT_ALL_MODDN(state) ) {
451         Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
452                 0, 0, 0 );
453         *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
454         return -1;
455     }
456
457 #ifdef LDAP_DEBUG
458     if ( ldap_debug & LDAP_DEBUG_ARGS ) {
459         char buf[ 256 ];
460         char *buf2;
461         sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
462         buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
463                 + 10 );
464         sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
465         Debug( LDAP_DEBUG_ARGS,
466                 "replica %s - modify rdn %s (flag: %d)\n",
467                 buf, buf2, drdnflag );
468         free( buf2 );
469     }
470 #endif /* LDAP_DEBUG */
471
472     /* Do the modrdn */
473     rc = ldap_rename2_s( ri->ri_ldp, re->re_dn, mi->mi_val, drdnflag, newsup );
474
475         ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
476     return( lderr );
477 }
478
479
480
481 /*
482  * Allocate and initialize an ldapmod struct.
483  */
484 static LDAPMod *
485 alloc_ldapmod( void )
486 {
487     LDAPMod     *ldm;
488
489     ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
490     ldm->mod_type = NULL;
491     ldm->mod_bvalues = ( struct berval ** ) NULL;
492     return( ldm );
493 }
494
495
496
497 /*
498  * Free an ldapmod struct associated mod_bvalues.  NOTE - it is assumed
499  * that mod_bvalues and mod_type contain pointers to the same block of memory
500  * pointed to by the repl struct.  Therefore, it's not freed here.
501  */
502 static void
503 free_ldapmod(
504 LDAPMod *ldm )
505 {
506     int         i;
507
508     if ( ldm == NULL ) {
509         return;
510     }
511     if ( ldm->mod_bvalues != NULL ) {
512         for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
513             free( ldm->mod_bvalues[ i ] );
514         }
515         free( ldm->mod_bvalues );
516     }
517     free( ldm );
518     return;
519 }
520
521
522 /*
523  * Free an an array of LDAPMod pointers and the LDAPMod structs they point
524  * to.
525  */
526 static void
527 free_ldmarr(
528 LDAPMod **ldmarr )
529 {
530     int i;
531
532     for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
533         free_ldapmod( ldmarr[ i ] );
534     }
535     free( ldmarr );
536 }
537
538
539 /*
540  * Create a berval with a single value. 
541  */
542 static struct berval **
543 make_singlevalued_berval( 
544 char    *value,
545 int     len )
546 {
547     struct berval **p;
548
549     p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
550     p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
551     p[ 1 ] = NULL;
552     p[ 0 ]->bv_val = value;
553     p[ 0 ]->bv_len = len;
554     return( p );
555 }
556
557
558 /*
559  * Given a modification type (string), return an enumerated type.
560  * Avoids ugly copy in op_ldap_modify - lets us use a switch statement
561  * there.
562  */
563 static int
564 getmodtype( 
565 char *type )
566 {
567     if ( !strcmp( type, T_MODSEPSTR )) {
568         return( T_MODSEP );
569     }
570     if ( !strcmp( type, T_MODOPADDSTR )) {
571         return( T_MODOPADD );
572     }
573     if ( !strcmp( type, T_MODOPREPLACESTR )) {
574         return( T_MODOPREPLACE );
575     }
576     if ( !strcmp( type, T_MODOPDELETESTR )) {
577         return( T_MODOPDELETE );
578     }
579     return( T_ERR );
580 }
581
582
583 /*
584  * Perform an LDAP unbind operation.  If replica is NULL, or the
585  * repl_ldp is NULL, just return LDAP_SUCCESS.  Otherwise, unbind,
586  * set the ldp to NULL, and return the result of the unbind call.
587  */
588 static int
589 do_unbind(
590     Ri  *ri
591 )
592 {
593     int         rc = LDAP_SUCCESS;
594
595     if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
596         rc = ldap_unbind( ri->ri_ldp );
597         if ( rc != LDAP_SUCCESS ) {
598             Debug( LDAP_DEBUG_ANY,
599                     "Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
600                     ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
601         }
602         ri->ri_ldp = NULL;
603     }
604     return rc;
605 }
606
607
608
609 /*
610  * Perform an LDAP bind operation to the replication site given
611  * by replica.  If replica->repl_ldp is non-NULL, then we unbind
612  * from the replica before rebinding.  It should be safe to call
613  * this to re-connect if the replica's connection goes away
614  * for some reason.
615  *
616  * Returns 0 on success, -1 if an LDAP error occurred, and a return
617  * code > 0 if some other error occurred, e.g. invalid bind method.
618  * If an LDAP error occurs, the LDAP error is returned in lderr.
619  */
620 static int
621 do_bind( 
622     Ri  *ri,
623     int *lderr
624 )
625 {
626     int         ldrc;
627 #ifdef HAVE_KERBEROS
628     int rc;
629     int retval = 0;
630     int kni, got_tgt;
631     char **krbnames;
632     char *skrbnames[ 2 ];
633     char realm[ REALM_SZ ];
634     char name[ ANAME_SZ ];
635     char instance[ INST_SZ ];
636 #endif /* HAVE_KERBEROS */
637
638     *lderr = 0;
639
640     if ( ri == NULL ) {
641         Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
642         return( BIND_ERR_BADRI );
643     }
644
645     if ( ri->ri_ldp != NULL ) {
646         ldrc = ldap_unbind( ri->ri_ldp );
647         if ( ldrc != LDAP_SUCCESS ) {
648             Debug( LDAP_DEBUG_ANY,
649                     "Error: do_bind: ldap_unbind failed: %s\n",
650                     ldap_err2string( ldrc ), 0, 0 );
651         }
652         ri->ri_ldp = NULL;
653     }
654
655     Debug( LDAP_DEBUG_ARGS, "Initializing session to %s:%d\n",
656             ri->ri_hostname, ri->ri_port, 0 );
657     ri->ri_ldp = ldap_init( ri->ri_hostname, ri->ri_port );
658     if ( ri->ri_ldp == NULL ) {
659         Debug( LDAP_DEBUG_ANY, "Error: ldap_init(%s, %d) failed: %s\n",
660                 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
661         return( BIND_ERR_OPEN );
662     }
663
664     /*
665      * Disable string translation if enabled by default.
666      * The replication log is written in the internal format,
667      * so this would do another translation, breaking havoc.
668      */
669 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
670         ri->ri_ldp->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
671 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
672
673     /*
674      * Set ldap library options to (1) not follow referrals, and 
675      * (2) restart the select() system call.
676      */
677         ldap_set_option(ri->ri_ldp, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
678         ldap_set_option(ri->ri_ldp, LDAP_OPT_RESTART, LDAP_OPT_ON);
679
680     switch ( ri->ri_bind_method ) {
681     case AUTH_KERBEROS:
682 #ifndef HAVE_KERBEROS
683         Debug( LDAP_DEBUG_ANY,
684             "Error: Kerberos bind for %s:%d, but not compiled w/kerberos\n",
685             ri->ri_hostname, ri->ri_port, 0 );
686         return( BIND_ERR_KERBEROS_FAILED );
687 #else /* HAVE_KERBEROS */
688         /*
689          * Bind using kerberos.
690          * If "bindprincipal" was given in the config file, then attempt
691          * to get a TGT for that principal (via the srvtab file).  If only
692          * a binddn was given, then we need to read that entry to get
693          * the kerberosName attributes, and try to get a TGT for one
694          * of them.  All are tried.  The first one which succeeds is
695          * returned.  XXX It might be a good idea to just require a
696          * bindprincipal.  Reading the entry every time might be a significant
697          * amount of overhead, if the connection is closed between most
698          * updates.
699          */
700
701         if ( ri->ri_principal != NULL ) {
702             skrbnames[ 0 ] = ri->ri_principal;
703             skrbnames[ 1 ] = NULL;
704             krbnames = skrbnames;
705         } else {
706             krbnames = read_krbnames( ri );
707         }       
708             
709         if (( krbnames == NULL ) || ( krbnames[ 0 ] == NULL )) {
710             Debug( LDAP_DEBUG_ANY,
711                     "Error: Can't find krbname for binddn \"%s\"\n",
712                     ri->ri_bind_dn, 0, 0 );
713             retval = BIND_ERR_KERBEROS_FAILED;
714             goto kexit;
715         }
716         /*
717          * Now we've got one or more kerberos principals.  See if any
718          * of them are in the srvtab file.
719          */
720         got_tgt = 0;
721         for ( kni = 0; krbnames[ kni ] != NULL; kni++ ) {
722             rc = kname_parse( name, instance, realm, krbnames[ kni ]);
723             if ( rc != KSUCCESS ) {
724                 continue;
725             }
726             upcase( realm );
727             rc = krb_get_svc_in_tkt( name, instance, realm, "krbtgt", realm,
728                     1, ri->ri_srvtab );
729             if ( rc != KSUCCESS) {
730                 Debug( LDAP_DEBUG_ANY, "Error: Can't get TGT for %s: %s\n",
731                         krbnames[ kni ], krb_err_txt[ rc ], 0 );
732             } else {
733                 got_tgt = 1;
734                 break;
735             }
736         }
737         if (!got_tgt) {
738             Debug( LDAP_DEBUG_ANY,
739                     "Error: Could not obtain TGT for DN \"%s\"\n", 
740                     ri->ri_bind_dn, 0, 0 );
741             retval = BIND_ERR_KERBEROS_FAILED;
742             goto kexit;
743         }
744         /*
745          * We've got a TGT.  Do a kerberos bind.
746          */
747         Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (kerberos)\n",
748                 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
749         ldrc = ldap_kerberos_bind_s( ri->ri_ldp, ri->ri_bind_dn );
750         ri->ri_principal = strdup( krbnames[ kni ] );
751         if ( ldrc != LDAP_SUCCESS ) {
752             Debug( LDAP_DEBUG_ANY, "Error: kerberos bind for %s:%dfailed: %s\n",
753                 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
754             *lderr = ldrc;
755             retval = BIND_ERR_KERBEROS_FAILED;
756             goto kexit;
757         }
758 kexit:  if ( krbnames != NULL ) {
759             ldap_value_free( krbnames );
760         }
761         return( retval);
762         break;
763 #endif /* HAVE_KERBEROS */
764     case AUTH_SIMPLE:
765         /*
766          * Bind with a plaintext password.
767          */
768         Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
769                 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
770         ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
771                 ri->ri_password );
772         if ( ldrc != LDAP_SUCCESS ) {
773             Debug( LDAP_DEBUG_ANY,
774                     "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
775                     ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
776             *lderr = ldrc;
777             return( BIND_ERR_SIMPLE_FAILED );
778         } else {
779             return( BIND_OK );
780         }
781         break;
782     default:
783         Debug(  LDAP_DEBUG_ANY,
784                 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
785                 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
786         return( BIND_ERR_BAD_ATYPE );
787     }
788 }
789
790
791
792
793
794 /*
795  * For debugging.  Print the contents of an ldmarr array.
796  */
797 static void
798 dump_ldm_array(
799     LDAPMod **ldmarr
800 )
801 {
802     int                  i, j;
803     LDAPMod             *ldm;
804     struct berval       *b;
805     char                *msgbuf;
806
807     for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
808         ldm = ldmarr[ i ];
809         Debug( LDAP_DEBUG_TRACE,
810                 "Trace (%ld): *** ldmarr[ %d ] contents:\n",
811                 (long) getpid(), i, 0 );
812         Debug( LDAP_DEBUG_TRACE,
813                 "Trace (%ld): *** ldm->mod_op: %d\n",
814                 (long) getpid(), ldm->mod_op, 0 );
815         Debug( LDAP_DEBUG_TRACE,
816                 "Trace (%ld): *** ldm->mod_type: %s\n",
817                 (long) getpid(), ldm->mod_type, 0 );
818         if ( ldm->mod_bvalues != NULL ) {
819             for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
820                 msgbuf = ch_malloc( b->bv_len + 512 );
821                 sprintf( msgbuf, "***** bv[ %d ] len = %ld, val = <%s>",
822                         j, b->bv_len, b->bv_val );
823                 Debug( LDAP_DEBUG_TRACE,
824                         "Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );
825                 free( msgbuf );
826             }
827         }
828     }
829 }
830
831
832 /*
833  * Get the kerberos names from the binddn for "replica" via an ldap search.
834  * Returns a null-terminated array of char *, or NULL if the entry could
835  * not be found or there were no kerberosName attributes.  The caller is
836  * responsible for freeing the returned array and strings it points to.
837  */
838 static char **
839 read_krbnames(
840     Ri  *ri
841 )
842 {
843     int rc;
844     char **krbnames;
845     int ne;
846     LDAPMessage *result, *entry;
847
848     /* First need to bind as NULL */
849     rc = ldap_simple_bind_s( ri->ri_ldp, NULL, NULL );
850     if ( rc != LDAP_SUCCESS ) {
851         Debug( LDAP_DEBUG_ANY,
852                 "Error: null bind failed getting krbnames for %s:%d: %s\n",
853                 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
854         return( NULL );
855     }
856     rc = ldap_search_st( ri->ri_ldp, ri->ri_bind_dn, LDAP_SCOPE_BASE,
857             "objectclass=*", kattrs, 0, &kst, &result );
858     if ( rc != LDAP_SUCCESS ) {
859         Debug( LDAP_DEBUG_ANY,
860                 "Error: search failed getting krbnames for %s:%d: %s\n",
861                 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
862         return( NULL );
863     }
864     ne = ldap_count_entries( ri->ri_ldp, result );
865     if ( ne == 0 ) {
866         Debug( LDAP_DEBUG_ANY,
867                 "Error: Can't find entry \"%s\" for %s:%d kerberos bind\n",
868                 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
869             return( NULL );
870     }
871     if ( ne > 1 ) {
872         Debug( LDAP_DEBUG_ANY,
873                 "Error: Kerberos binddn \"%s\" for %s:%dis ambiguous\n",
874                 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
875             return( NULL );
876     }
877     entry = ldap_first_entry( ri->ri_ldp, result );
878     if ( entry == NULL ) {
879         Debug( LDAP_DEBUG_ANY,
880                 "Error: Can't find \"%s\" for kerberos binddn for %s:%d\n",
881                     ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
882         return( NULL );
883     }
884     krbnames = ldap_get_values( ri->ri_ldp, entry, "kerberosName" );
885     ldap_msgfree( result );
886     return( krbnames );
887 }
888
889
890 #ifdef HAVE_KERBEROS
891
892 /*
893  * upcase a string
894  */
895 static void
896 upcase(
897     char *s
898 )
899 {
900     char *p;
901
902     for ( p = s; ( p != NULL ) && ( *p != '\0' ); p++ ) {
903             *p = TOUPPER( (unsigned char) *p );
904     }
905 }
906
907 #endif /* HAVE_KERBEROS */