]> git.sur5r.net Git - openldap/blob - servers/slurpd/ldap_op.c
Fixup bugs created by merge.
[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         lderr = ri->ri_ldp->ld_errno;
198     } else {
199         *errmsg = "No modifications to do";
200         Debug( LDAP_DEBUG_ANY,
201                 "Error: op_ldap_add: no mods to do (%s)!", 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, *nldm, **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         state = 0;
366     int         drdnflag = -1;
367     char        *newrdn;
368
369     if ( re->re_mods == NULL ) {
370         *errmsg = "No arguments given";
371         Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
372                 0, 0, 0 );
373             return -1;
374     }
375
376     /*
377      * Get the arguments: should see newrdn: and deleteoldrdn: args.
378      */
379     for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
380         if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
381             newrdn = mi[ i ].mi_val;
382             state |= GOT_NEWRDN;
383         } else if ( !strcmp( mi[ i ].mi_type, T_DRDNFLAGSTR )) {
384             state |= GOT_DRDNFLAGSTR;
385             if ( !strcmp( mi[ i ].mi_val, "0" )) {
386                 drdnflag = 0;
387             } else if ( !strcmp( mi[ i ].mi_val, "1" )) {
388                 drdnflag = 1;
389             } else {
390                 Debug( LDAP_DEBUG_ANY,
391                         "Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
392                         mi[ i ].mi_val, 0, 0 );
393                 *errmsg = "Incorrect argument to deleteoldrdn";
394                 return -1;
395             }
396         } else {
397             Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
398                     mi[ i ].mi_type, 0, 0 );
399             *errmsg = "Bad value in replication log entry";
400             return -1;
401         }
402     }
403
404     /*
405      * Punt if we don't have all the args.
406      */
407     if ( state != GOT_ALLNEWRDNFLAGS ) {
408         Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
409                 0, 0, 0 );
410         *errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
411         return -1;
412     }
413
414 #ifdef LDAP_DEBUG
415     if ( ldap_debug & LDAP_DEBUG_ARGS ) {
416         char buf[ 256 ];
417         char *buf2;
418         sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
419         buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
420                 + 10 );
421         sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
422         Debug( LDAP_DEBUG_ARGS,
423                 "replica %s - modify rdn %s (flag: %d)\n",
424                 buf, buf2, drdnflag );
425         free( buf2 );
426     }
427 #endif /* LDAP_DEBUG */
428
429     /* Do the modrdn */
430     rc = ldap_modrdn2_s( ri->ri_ldp, re->re_dn, mi->mi_val, drdnflag );
431
432     return( ri->ri_ldp->ld_errno );
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         rc;
583     int         ldrc;
584     char        msgbuf[ 1024];
585 #ifdef HAVE_KERBEROS
586     int retval = 0;
587     int kni, got_tgt;
588     char **krbnames;
589     char *skrbnames[ 2 ];
590     char realm[ REALM_SZ ];
591     char name[ ANAME_SZ ];
592     char instance[ INST_SZ ];
593 #endif /* HAVE_KERBEROS */
594
595     *lderr = 0;
596
597     if ( ri == NULL ) {
598         Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
599         return( BIND_ERR_BADRI );
600     }
601
602     if ( ri->ri_ldp != NULL ) {
603         ldrc = ldap_unbind( ri->ri_ldp );
604         if ( ldrc != LDAP_SUCCESS ) {
605             Debug( LDAP_DEBUG_ANY,
606                     "Error: do_bind: ldap_unbind failed: %s\n",
607                     ldap_err2string( ldrc ), 0, 0 );
608         }
609         ri->ri_ldp = NULL;
610     }
611
612     Debug( LDAP_DEBUG_ARGS, "Open connection to %s:%d\n",
613             ri->ri_hostname, ri->ri_port, 0 );
614     ri->ri_ldp = ldap_open( ri->ri_hostname, ri->ri_port );
615     if ( ri->ri_ldp == NULL ) {
616         Debug( LDAP_DEBUG_ANY, "Error: ldap_open(%s, %d) failed: %s\n",
617                 ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
618         return( BIND_ERR_OPEN );
619     }
620
621     /*
622      * Set ldap library options to (1) not follow referrals, and 
623      * (2) restart the select() system call.
624      */
625 #ifdef LDAP_REFERRALS
626     ri->ri_ldp->ld_options &= ~LDAP_OPT_REFERRALS;
627 #endif /* LDAP_REFERRALS */
628     ri->ri_ldp->ld_options |= LDAP_OPT_RESTART;
629
630     switch ( ri->ri_bind_method ) {
631     case AUTH_KERBEROS:
632 #ifndef HAVE_KERBEROS
633         Debug( LDAP_DEBUG_ANY,
634             "Error: Kerberos bind for %s:%d, but not compiled w/kerberos\n",
635             ri->ri_hostname, ri->ri_port, 0 );
636         return( BIND_ERR_KERBEROS_FAILED );
637 #else /* HAVE_KERBEROS */
638         /*
639          * Bind using kerberos.
640          * If "bindprincipal" was given in the config file, then attempt
641          * to get a TGT for that principal (via the srvtab file).  If only
642          * a binddn was given, then we need to read that entry to get
643          * the kerberosName attributes, and try to get a TGT for one
644          * of them.  All are tried.  The first one which succeeds is
645          * returned.  XXX It might be a good idea to just require a
646          * bindprincipal.  Reading the entry every time might be a significant
647          * amount of overhead, if the connection is closed between most
648          * updates.
649          */
650
651         if ( ri->ri_principal != NULL ) {
652             skrbnames[ 0 ] = ri->ri_principal;
653             skrbnames[ 1 ] = NULL;
654             krbnames = skrbnames;
655         } else {
656             krbnames = read_krbnames( ri );
657         }       
658             
659         if (( krbnames == NULL ) || ( krbnames[ 0 ] == NULL )) {
660             Debug( LDAP_DEBUG_ANY,
661                     "Error: Can't find krbname for binddn \"%s\"\n",
662                     ri->ri_bind_dn, 0, 0 );
663             retval = BIND_ERR_KERBEROS_FAILED;
664             goto kexit;
665         }
666         /*
667          * Now we've got one or more kerberos principals.  See if any
668          * of them are in the srvtab file.
669          */
670         got_tgt = 0;
671         for ( kni = 0; krbnames[ kni ] != NULL; kni++ ) {
672             rc = kname_parse( name, instance, realm, krbnames[ kni ]);
673             if ( rc != KSUCCESS ) {
674                 continue;
675             }
676             upcase( realm );
677             rc = krb_get_svc_in_tkt( name, instance, realm, "krbtgt", realm,
678                     1, ri->ri_srvtab );
679             if ( rc != KSUCCESS) {
680                 Debug( LDAP_DEBUG_ANY, "Error: Can't get TGT for %s: %s\n",
681                         krbnames[ kni ], krb_err_txt[ rc ], 0 );
682             } else {
683                 got_tgt = 1;
684                 break;
685             }
686         }
687         if (!got_tgt) {
688             Debug( LDAP_DEBUG_ANY,
689                     "Error: Could not obtain TGT for DN \"%s\"\n", 
690                     ri->ri_bind_dn, 0, 0 );
691             retval = BIND_ERR_KERBEROS_FAILED;
692             goto kexit;
693         }
694         /*
695          * We've got a TGT.  Do a kerberos bind.
696          */
697         Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (kerberos)\n",
698                 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
699         ldrc = ldap_kerberos_bind_s( ri->ri_ldp, ri->ri_bind_dn );
700         ri->ri_principal = strdup( krbnames[ kni ] );
701         if ( ldrc != LDAP_SUCCESS ) {
702             Debug( LDAP_DEBUG_ANY, "Error: kerberos bind for %s:%dfailed: %s\n",
703                 ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
704             *lderr = ldrc;
705             retval = BIND_ERR_KERBEROS_FAILED;
706             goto kexit;
707         }
708 kexit:  if ( krbnames != NULL ) {
709             ldap_value_free( krbnames );
710         }
711         return( retval);
712         break;
713 #endif /* HAVE_KERBEROS */
714     case AUTH_SIMPLE:
715         /*
716          * Bind with a plaintext password.
717          */
718         Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
719                 ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
720         ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
721                 ri->ri_password );
722         if ( ldrc != LDAP_SUCCESS ) {
723             Debug( LDAP_DEBUG_ANY,
724                     "Error: ldap_simple_bind_s for %s:%d failed: %s\n",
725                     ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
726             *lderr = ldrc;
727             return( BIND_ERR_SIMPLE_FAILED );
728         } else {
729             return( BIND_OK );
730         }
731         break;
732     default:
733         Debug(  LDAP_DEBUG_ANY,
734                 "Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
735                 ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
736         return( BIND_ERR_BAD_ATYPE );
737     }
738 }
739
740
741
742
743
744 /*
745  * For debugging.  Print the contents of an ldmarr array.
746  */
747 static void
748 dump_ldm_array(
749 LDAPMod **ldmarr )
750 {
751     int                  i, j;
752     LDAPMod             *ldm;
753     struct berval       *b;
754     char                *msgbuf;
755
756     for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
757         ldm = ldmarr[ i ];
758         Debug( LDAP_DEBUG_TRACE,
759                 "Trace (%d): *** ldmarr[ %d ] contents:\n",
760                 getpid(), i, 0 );
761         Debug( LDAP_DEBUG_TRACE,
762                 "Trace (%d): *** ldm->mod_op: %d\n",
763                 getpid(), ldm->mod_op, 0 );
764         Debug( LDAP_DEBUG_TRACE,
765                 "Trace (%d): *** ldm->mod_type: %s\n",
766                 getpid(), ldm->mod_type, 0 );
767         if ( ldm->mod_bvalues != NULL ) {
768             for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
769                 msgbuf = ch_malloc( b->bv_len + 512 );
770                 sprintf( msgbuf, "***** bv[ %d ] len = %d, val = <%s>",
771                         j, b->bv_len, b->bv_val );
772                 Debug( LDAP_DEBUG_TRACE,
773                         "Trace (%d):%s\n", getpid(), msgbuf, 0 );
774                 free( msgbuf );
775             }
776         }
777     }
778 }
779
780
781 /*
782  * Get the kerberos names from the binddn for "replica" via an ldap search.
783  * Returns a null-terminated array of char *, or NULL if the entry could
784  * not be found or there were no kerberosName attributes.  The caller is
785  * responsible for freeing the returned array and strings it points to.
786  */
787 static char **
788 read_krbnames(
789     Ri  *ri
790 )
791 {
792     int rc;
793     char **krbnames;
794     int ne;
795     LDAPMessage *result, *entry;
796
797     /* First need to bind as NULL */
798     rc = ldap_simple_bind_s( ri->ri_ldp, NULL, NULL );
799     if ( rc != LDAP_SUCCESS ) {
800         Debug( LDAP_DEBUG_ANY,
801                 "Error: null bind failed getting krbnames for %s:%d: %s\n",
802                 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
803         return( NULL );
804     }
805     rc = ldap_search_st( ri->ri_ldp, ri->ri_bind_dn, LDAP_SCOPE_BASE,
806             "objectclass=*", kattrs, 0, &kst, &result );
807     if ( rc != LDAP_SUCCESS ) {
808         Debug( LDAP_DEBUG_ANY,
809                 "Error: search failed getting krbnames for %s:%d: %s\n",
810                 ri->ri_hostname, ri->ri_port, ldap_err2string( rc ));
811         return( NULL );
812     }
813     ne = ldap_count_entries( ri->ri_ldp, result );
814     if ( ne == 0 ) {
815         Debug( LDAP_DEBUG_ANY,
816                 "Error: Can't find entry \"%s\" for %s:%d kerberos bind\n",
817                 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
818             return( NULL );
819     }
820     if ( ne > 1 ) {
821         Debug( LDAP_DEBUG_ANY,
822                 "Error: Kerberos binddn \"%s\" for %s:%dis ambiguous\n",
823                 ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
824             return( NULL );
825     }
826     entry = ldap_first_entry( ri->ri_ldp, result );
827     if ( entry == NULL ) {
828         Debug( LDAP_DEBUG_ANY,
829                 "Error: Can't find \"%s\" for kerberos binddn for %s:%d\n",
830                     ri->ri_bind_dn, ri->ri_hostname, ri->ri_port );
831         return( NULL );
832     }
833     krbnames = ldap_get_values( ri->ri_ldp, entry, "kerberosName" );
834     ldap_msgfree( result );
835     return( krbnames );
836 }
837
838
839
840 /*
841  * upcase a string
842  */
843 static void
844 upcase(
845 char *s )
846 {
847     char *p;
848
849     for ( p = s; ( p != NULL ) && ( *p != '\0' ); p++ ) {
850         if ( islower( *p )) {
851             *p = toupper( *p );
852         }
853     }
854 }