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