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