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