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