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