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