]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passwd.c
Add security checks to root DSE searches.
[openldap] / libraries / liblutil / passwd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * int lutil_passwd(
8  *      const struct berval *passwd,
9  *      const struct berval *cred,
10  *      const char **schemes )
11  *
12  * Returns true if user supplied credentials (cred) matches
13  * the stored password (passwd). 
14  *
15  * Due to the use of the crypt(3) function 
16  * this routine is NOT thread-safe.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
25
26 #ifdef SLAPD_LMHASH
27 #       include <openssl/des.h>
28 #endif /* SLAPD_LMHASH */
29
30 #ifdef SLAPD_SPASSWD
31 #       include <sasl.h>
32 #endif
33
34 #ifdef SLAPD_KPASSWD
35 #       include <ac/krb.h>
36 #       include <ac/krb5.h>
37 #endif
38
39 #include <ac/param.h>
40
41 #ifdef SLAPD_CRYPT
42 #include <ac/crypt.h>
43 #endif
44
45 #ifdef HAVE_SHADOW_H
46 #       include <shadow.h>
47 #endif
48 #ifdef HAVE_PWD_H
49 #       include <pwd.h>
50 #endif
51 #ifdef HAVE_AIX_SECURITY
52 #       include <userpw.h>
53 #endif
54
55 #include <lber.h>
56
57 #include "ldap_pvt.h"
58
59 #include "lutil_md5.h"
60 #include "lutil_sha1.h"
61 #include "lutil.h"
62
63 static const unsigned char crypt64[] =
64         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
65
66 struct pw_scheme;
67
68 typedef int (*PASSWD_CHK_FUNC)(
69         const struct pw_scheme *scheme,
70         const struct berval *passwd,
71         const struct berval *cred );
72
73 typedef struct berval * (*PASSWD_HASH_FUNC) (
74         const struct pw_scheme *scheme,
75         const struct berval *passwd );
76
77 struct pw_scheme {
78         struct berval name;
79         PASSWD_CHK_FUNC chk_fn;
80         PASSWD_HASH_FUNC hash_fn;
81 };
82
83 /* password check routines */
84 static int chk_md5(
85         const struct pw_scheme *scheme,
86         const struct berval *passwd,
87         const struct berval *cred );
88
89 static int chk_smd5(
90         const struct pw_scheme *scheme,
91         const struct berval *passwd,
92         const struct berval *cred );
93
94 static int chk_ssha1(
95         const struct pw_scheme *scheme,
96         const struct berval *passwd,
97         const struct berval *cred );
98
99 static int chk_sha1(
100         const struct pw_scheme *scheme,
101         const struct berval *passwd,
102         const struct berval *cred );
103
104 #ifdef SLAPD_LMHASH
105 static int chk_lanman(
106         const struct pw_scheme *scheme,
107         const struct berval *passwd,
108         const struct berval *cred );
109 #endif
110
111 #ifdef SLAPD_SPASSWD
112 static int chk_sasl(
113         const struct pw_scheme *scheme,
114         const struct berval *passwd,
115         const struct berval *cred );
116 #endif
117
118 #ifdef SLAPD_KPASSWD
119 static int chk_kerberos(
120         const struct pw_scheme *scheme,
121         const struct berval *passwd,
122         const struct berval *cred );
123 #endif
124
125 #ifdef SLAPD_CRYPT
126 static int chk_crypt(
127         const struct pw_scheme *scheme,
128         const struct berval *passwd,
129         const struct berval *cred );
130
131 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
132 static int chk_unix(
133         const struct pw_scheme *scheme,
134         const struct berval *passwd,
135         const struct berval *cred );
136 #endif
137 #endif
138
139
140 /* password hash routines */
141 static struct berval *hash_sha1(
142         const struct pw_scheme *scheme,
143         const struct berval *passwd );
144
145 static struct berval *hash_ssha1(
146         const struct pw_scheme *scheme,
147         const struct berval *passwd );
148
149 static struct berval *hash_smd5(
150         const struct pw_scheme *scheme,
151         const struct berval *passwd );
152
153 static struct berval *hash_md5(
154         const struct pw_scheme *scheme,
155         const struct berval *passwd );
156
157 #ifdef SLAPD_LMHASH
158 static struct berval *hash_lanman(
159         const struct pw_scheme *scheme,
160         const struct berval *passwd );
161 #endif
162
163 #ifdef SLAPD_CRYPT
164 static struct berval *hash_crypt(
165         const struct pw_scheme *scheme,
166         const struct berval *passwd );
167 #endif
168
169
170 static const struct pw_scheme pw_schemes[] =
171 {
172         { {sizeof("{SSHA}")-1, "{SSHA}"},       chk_ssha1, hash_ssha1 },
173         { {sizeof("{SHA}")-1, "{SHA}"},         chk_sha1, hash_sha1 },
174
175         { {sizeof("{SMD5}")-1, "{SMD5}"},       chk_smd5, hash_smd5 },
176         { {sizeof("{MD5}")-1, "{MD5}"},         chk_md5, hash_md5 },
177
178 #ifdef SLAPD_LMHASH
179         { {sizeof("{LANMAN}")-1, "{LANMAN}"},   chk_lanman, hash_lanman },
180 #endif /* SLAPD_LMHASH */
181
182 #ifdef SLAPD_SPASSWD
183         { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
184 #endif
185
186 #ifdef SLAPD_KPASSWD
187         { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
188 #endif
189
190 #ifdef SLAPD_CRYPT
191         { {sizeof("{CRYPT}")-1, "{CRYPT}"},     chk_crypt, hash_crypt },
192 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
193         { {sizeof("{UNIX}")-1, "{UNIX}"},       chk_unix, NULL },
194 # endif
195 #endif
196
197 #ifdef SLAPD_CLEARTEXT
198         /* psuedo scheme */
199         { {0, "{CLEARTEXT}"}, NULL, NULL },
200 #endif
201
202         { {0, NULL}, NULL, NULL }
203 };
204
205 static const struct pw_scheme *get_scheme(
206         const char* scheme )
207 {
208         int i;
209
210         for( i=0; pw_schemes[i].name.bv_val; i++) {
211                 if( pw_schemes[i].name.bv_len == 0 ) continue;
212
213                 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
214                         pw_schemes[i].name.bv_len) == 0 )
215                 {
216                         return &pw_schemes[i];
217                 }
218         }
219
220         return NULL;
221 }
222
223 int lutil_passwd_scheme(
224         const char* scheme )
225 {
226         if( scheme == NULL ) {
227                 return 0;
228         }
229
230         return get_scheme(scheme) != NULL;
231 }
232
233
234 static int is_allowed_scheme( 
235         const char* scheme,
236         const char** schemes )
237 {
238         int i;
239
240         if( schemes == NULL ) return 1;
241
242         for( i=0; schemes[i] != NULL; i++ ) {
243                 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
244                         return 1;
245                 }
246         }
247         return 0;
248 }
249
250 static struct berval *passwd_scheme(
251         const struct pw_scheme *scheme,
252         const struct berval * passwd,
253         const char** allowed )
254 {
255         if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
256                 return NULL;
257         }
258
259         if( passwd->bv_len >= scheme->name.bv_len ) {
260                 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
261                         struct berval *bv = ber_memalloc( sizeof(struct berval) );
262
263                         if( bv == NULL ) return NULL;
264
265                         bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
266                         bv->bv_len = passwd->bv_len - scheme->name.bv_len;
267
268                         return bv;
269                 }
270         }
271
272         return NULL;
273 }
274
275 /*
276  * Return 0 if creds are good.
277  */
278 int
279 lutil_passwd(
280         const struct berval *passwd,    /* stored passwd */
281         const struct berval *cred,              /* user cred */
282         const char **schemes )
283 {
284         int i;
285
286         if (cred == NULL || cred->bv_len == 0 ||
287                 passwd == NULL || passwd->bv_len == 0 )
288         {
289                 return -1;
290         }
291
292         for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
293                 if( pw_schemes[i].chk_fn ) {
294                         struct berval *p = passwd_scheme( &pw_schemes[i],
295                                 passwd, schemes );
296
297                         if( p != NULL ) {
298                                 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
299
300                                 /* only free the berval structure as the bv_val points
301                                  * into passwd->bv_val
302                                  */
303                                 ber_memfree( p );
304                                 
305                                 return rc;
306                         }
307                 }
308         }
309
310 #ifdef SLAPD_CLEARTEXT
311         if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
312                 return passwd->bv_len == cred->bv_len
313                         ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
314                         : 1;
315         }
316 #endif
317         return 1;
318 }
319
320 struct berval * lutil_passwd_generate( ber_len_t len )
321 {
322         struct berval *pw;
323
324         if( len < 1 ) return NULL;
325
326         pw = ber_memalloc( sizeof( struct berval ) );
327         if( pw == NULL ) return NULL;
328
329         pw->bv_len = len;
330         pw->bv_val = ber_memalloc( len + 1 );
331
332         if( pw->bv_val == NULL ) {
333                 ber_memfree( pw );
334                 return NULL;
335         }
336
337         if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
338                 ber_bvfree( pw );
339                 return NULL; 
340         }
341
342         for( len = 0; len < pw->bv_len; len++ ) {
343                 pw->bv_val[len] = crypt64[
344                         pw->bv_val[len] % (sizeof(crypt64)-1) ];
345         }
346
347         pw->bv_val[len] = '\0';
348         
349         return pw;
350 }
351
352 struct berval * lutil_passwd_hash(
353         const struct berval * passwd,
354         const char * method )
355 {
356         const struct pw_scheme *sc = get_scheme( method );
357
358         if( sc == NULL ) return NULL;
359         if( ! sc->hash_fn ) return NULL;
360
361         return (sc->hash_fn)( sc, passwd );
362 }
363
364 static struct berval * pw_string(
365         const struct pw_scheme *sc,
366         const struct berval *passwd )
367 {
368         struct berval *pw = ber_memalloc( sizeof( struct berval ) );
369         if( pw == NULL ) return NULL;
370
371         pw->bv_len = sc->name.bv_len + passwd->bv_len;
372         pw->bv_val = ber_memalloc( pw->bv_len + 1 );
373
374         if( pw->bv_val == NULL ) {
375                 ber_memfree( pw );
376                 return NULL;
377         }
378
379         AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
380         AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
381
382         pw->bv_val[pw->bv_len] = '\0';
383         return pw;
384 }
385
386 static struct berval * pw_string64(
387         const struct pw_scheme *sc,
388         const struct berval *hash,
389         const struct berval *salt )
390 {
391         int rc;
392         struct berval string;
393         struct berval *b64 = ber_memalloc( sizeof(struct berval) );
394         size_t b64len;
395
396         if( b64 == NULL ) return NULL;
397
398         if( salt ) {
399                 /* need to base64 combined string */
400                 string.bv_len = hash->bv_len + salt->bv_len;
401                 string.bv_val = ber_memalloc( string.bv_len + 1 );
402
403                 if( string.bv_val == NULL ) {
404                         ber_memfree( b64 );
405                         return NULL;
406                 }
407
408                 AC_MEMCPY( string.bv_val, hash->bv_val,
409                         hash->bv_len );
410                 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
411                         salt->bv_len );
412                 string.bv_val[string.bv_len] = '\0';
413
414         } else {
415                 string = *hash;
416         }
417
418         b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
419         b64->bv_len = b64len + sc->name.bv_len;
420         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
421
422         if( b64->bv_val == NULL ) {
423                 if( salt ) ber_memfree( string.bv_val );
424                 ber_memfree( b64 );
425                 return NULL;
426         }
427
428         AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
429
430         rc = lutil_b64_ntop(
431                 string.bv_val, string.bv_len,
432                 &b64->bv_val[sc->name.bv_len], b64len );
433
434         if( salt ) ber_memfree( string.bv_val );
435         
436         if( rc < 0 ) {
437                 ber_bvfree( b64 );
438                 return NULL;
439         }
440
441         /* recompute length */
442         b64->bv_len = sc->name.bv_len + rc;
443         assert( strlen(b64->bv_val) == b64->bv_len );
444         return b64;
445 }
446
447 /* PASSWORD CHECK ROUTINES */
448
449 static int chk_ssha1(
450         const struct pw_scheme *sc,
451         const struct berval * passwd,
452         const struct berval * cred )
453 {
454         lutil_SHA1_CTX SHA1context;
455         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
456         int rc;
457         unsigned char *orig_pass = NULL;
458  
459         /* decode base64 password */
460         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
461                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
462
463         if( orig_pass == NULL ) return -1;
464
465         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
466
467         if(rc < 0) {
468                 ber_memfree(orig_pass);
469                 return -1;
470         }
471  
472         /* hash credentials with salt */
473         lutil_SHA1Init(&SHA1context);
474         lutil_SHA1Update(&SHA1context,
475                 (const unsigned char *) cred->bv_val, cred->bv_len);
476         lutil_SHA1Update(&SHA1context,
477                 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
478                 rc - sizeof(SHA1digest));
479         lutil_SHA1Final(SHA1digest, &SHA1context);
480  
481         /* compare */
482         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
483         ber_memfree(orig_pass);
484         return rc ? 1 : 0;
485 }
486
487 static int chk_sha1(
488         const struct pw_scheme *sc,
489         const struct berval * passwd,
490         const struct berval * cred )
491 {
492         lutil_SHA1_CTX SHA1context;
493         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
494         int rc;
495         unsigned char *orig_pass = NULL;
496  
497         /* base64 un-encode password */
498         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
499                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
500
501         if( orig_pass == NULL ) return -1;
502
503         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
504
505         if( rc != sizeof(SHA1digest) ) {
506                 ber_memfree(orig_pass);
507                 return -1;
508         }
509  
510         /* hash credentials with salt */
511         lutil_SHA1Init(&SHA1context);
512         lutil_SHA1Update(&SHA1context,
513                 (const unsigned char *) cred->bv_val, cred->bv_len);
514         lutil_SHA1Final(SHA1digest, &SHA1context);
515  
516         /* compare */
517         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
518         ber_memfree(orig_pass);
519         return rc ? 1 : 0;
520 }
521
522 static int chk_smd5(
523         const struct pw_scheme *sc,
524         const struct berval * passwd,
525         const struct berval * cred )
526 {
527         lutil_MD5_CTX MD5context;
528         unsigned char MD5digest[LUTIL_MD5_BYTES];
529         int rc;
530         unsigned char *orig_pass = NULL;
531
532         /* base64 un-encode password */
533         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
534                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
535
536         if( orig_pass == NULL ) return -1;
537
538         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
539         if ( rc < 0 ) {
540                 ber_memfree(orig_pass);
541                 return -1;
542         }
543
544         /* hash credentials with salt */
545         lutil_MD5Init(&MD5context);
546         lutil_MD5Update(&MD5context,
547                 (const unsigned char *) cred->bv_val,
548                 cred->bv_len );
549         lutil_MD5Update(&MD5context,
550                 &orig_pass[sizeof(MD5digest)],
551                 rc - sizeof(MD5digest));
552         lutil_MD5Final(MD5digest, &MD5context);
553
554         /* compare */
555         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
556         ber_memfree(orig_pass);
557         return rc ? 1 : 0;
558 }
559
560 static int chk_md5(
561         const struct pw_scheme *sc,
562         const struct berval * passwd,
563         const struct berval * cred )
564 {
565         lutil_MD5_CTX MD5context;
566         unsigned char MD5digest[LUTIL_MD5_BYTES];
567         int rc;
568         unsigned char *orig_pass = NULL;
569
570         /* base64 un-encode password */
571         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
572                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
573
574         if( orig_pass == NULL ) return -1;
575
576         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
577         if ( rc != sizeof(MD5digest) ) {
578                 ber_memfree(orig_pass);
579                 return -1;
580         }
581
582         /* hash credentials with salt */
583         lutil_MD5Init(&MD5context);
584         lutil_MD5Update(&MD5context,
585                 (const unsigned char *) cred->bv_val,
586                 cred->bv_len );
587         lutil_MD5Final(MD5digest, &MD5context);
588
589         /* compare */
590         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
591         ber_memfree(orig_pass);
592         return rc ? 1 : 0;
593 }
594
595 #ifdef SLAPD_LMHASH
596 static int chk_lanman(
597         const struct pw_scheme *scheme,
598         const struct berval *passwd,
599         const struct berval *cred )
600 {
601         struct berval *hash;
602
603         hash = hash_lanman( scheme, cred );
604         return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
605 }
606 #endif /* SLAPD_LMHASH */
607
608 #ifdef SLAPD_SPASSWD
609 #ifdef HAVE_CYRUS_SASL
610 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
611 #endif
612
613 static int chk_sasl(
614         const struct pw_scheme *sc,
615         const struct berval * passwd,
616         const struct berval * cred )
617 {
618         int i;
619         int rtn;
620
621         for( i=0; i<cred->bv_len; i++) {
622                 if(cred->bv_val[i] == '\0') {
623                         return 1;       /* NUL character in password */
624                 }
625         }
626
627         if( cred->bv_val[i] != '\0' ) {
628                 return 1;       /* cred must behave like a string */
629         }
630
631         for( i=0; i<passwd->bv_len; i++) {
632                 if(passwd->bv_val[i] == '\0') {
633                         return 1;       /* NUL character in password */
634                 }
635         }
636
637         if( passwd->bv_val[i] != '\0' ) {
638                 return 1;       /* passwd must behave like a string */
639         }
640
641         rtn = 1;
642
643 #ifdef HAVE_CYRUS_SASL
644         if( lutil_passwd_sasl_conn != NULL ) {
645                 const char *errstr = NULL;
646                 int sc;
647
648                 sc = sasl_checkpass( lutil_passwd_sasl_conn,
649                         passwd->bv_val, passwd->bv_len,
650                         cred->bv_val, cred->bv_len,
651                         &errstr );
652
653                 rtn = ( sc != SASL_OK );
654         }
655 #endif
656
657         return rtn;
658 }
659 #endif
660
661 #ifdef SLAPD_KPASSWD
662 static int chk_kerberos(
663         const struct pw_scheme *sc,
664         const struct berval * passwd,
665         const struct berval * cred )
666 {
667         int i;
668         int rtn;
669
670         for( i=0; i<cred->bv_len; i++) {
671                 if(cred->bv_val[i] == '\0') {
672                         return 1;       /* NUL character in password */
673                 }
674         }
675
676         if( cred->bv_val[i] != '\0' ) {
677                 return 1;       /* cred must behave like a string */
678         }
679
680         for( i=0; i<passwd->bv_len; i++) {
681                 if(passwd->bv_val[i] == '\0') {
682                         return 1;       /* NUL character in password */
683                 }
684         }
685
686         if( passwd->bv_val[i] != '\0' ) {
687                 return 1;       /* passwd must behave like a string */
688         }
689
690         rtn = 1;
691
692 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
693         {
694 /* Portions:
695  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
696  * (Royal Institute of Technology, Stockholm, Sweden).
697  * All rights reserved.
698  *
699  * Redistribution and use in source and binary forms, with or without
700  * modification, are permitted provided that the following conditions
701  * are met:
702  *
703  * 1. Redistributions of source code must retain the above copyright
704  *    notice, this list of conditions and the following disclaimer.
705  *
706  * 2. Redistributions in binary form must reproduce the above copyright
707  *    notice, this list of conditions and the following disclaimer in the
708  *    documentation and/or other materials provided with the distribution.
709  *
710  * 3. Neither the name of the Institute nor the names of its contributors
711  *    may be used to endorse or promote products derived from this software
712  *    without specific prior written permission.
713  *
714  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
715  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
716  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
717  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
718  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
719  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
720  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
721  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
722  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
723  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
724  * SUCH DAMAGE.
725  */
726
727                 krb5_context context;
728                 krb5_error_code ret;
729                 krb5_creds creds;
730                 krb5_get_init_creds_opt get_options;
731                 krb5_verify_init_creds_opt verify_options;
732                 krb5_principal client, server;
733 #ifdef notdef
734                 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
735 #endif
736
737                 ret = krb5_init_context( &context );
738                 if (ret) {
739                         return 1;
740                 }
741
742 #ifdef notdef
743                 krb5_get_init_creds_opt_set_preauth_list(&get_options,
744                         pre_auth_types, 1);
745 #endif
746
747                 krb5_get_init_creds_opt_init( &get_options );
748
749                 krb5_verify_init_creds_opt_init( &verify_options );
750         
751                 ret = krb5_parse_name( context, passwd->bv_val, &client );
752
753                 if (ret) {
754                         krb5_free_context( context );
755                         return 1;
756                 }
757
758                 ret = krb5_get_init_creds_password( context,
759                         &creds, client, cred->bv_val, NULL,
760                         NULL, 0, NULL, &get_options );
761
762                 if (ret) {
763                         krb5_free_principal( context, client );
764                         krb5_free_context( context );
765                         return 1;
766                 }
767
768                 {
769                         char *host = ldap_pvt_get_fqdn( NULL );
770
771                         if( host == NULL ) {
772                                 krb5_free_principal( context, client );
773                                 krb5_free_context( context );
774                                 return 1;
775                         }
776
777                         ret = krb5_sname_to_principal( context,
778                                 host, "ldap", KRB5_NT_SRV_HST, &server );
779
780                         ber_memfree( host );
781                 }
782
783                 if (ret) {
784                         krb5_free_principal( context, client );
785                         krb5_free_context( context );
786                         return 1;
787                 }
788
789                 ret = krb5_verify_init_creds( context,
790                         &creds, server, NULL, NULL, &verify_options );
791
792                 krb5_free_principal( context, client );
793                 krb5_free_principal( context, server );
794                 krb5_free_cred_contents( context, &creds );
795                 krb5_free_context( context );
796
797                 rtn = !!ret;
798         }
799 #elif   defined(HAVE_KRB4)
800         {
801                 /* Borrowed from Heimdal kpopper */
802 /* Portions:
803  * Copyright (c) 1989 Regents of the University of California.
804  * All rights reserved.  The Berkeley software License Agreement
805  * specifies the terms and conditions for redistribution.
806  */
807
808                 int status;
809                 char lrealm[REALM_SZ];
810                 char tkt[MAXHOSTNAMELEN];
811
812                 status = krb_get_lrealm(lrealm,1);
813                 if (status == KFAILURE) {
814                         return 1;
815                 }
816
817                 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
818                         TKT_ROOT, (unsigned)getpid());
819                 krb_set_tkt_string (tkt);
820
821                 status = krb_verify_user( passwd->bv_val, "", lrealm,
822                         cred->bv_val, 1, "ldap");
823
824                 dest_tkt(); /* no point in keeping the tickets */
825
826                 return status == KFAILURE;
827         }
828 #endif
829
830         return rtn;
831 }
832 #endif /* SLAPD_KPASSWD */
833
834 #ifdef SLAPD_CRYPT
835 static int chk_crypt(
836         const struct pw_scheme *sc,
837         const struct berval * passwd,
838         const struct berval * cred )
839 {
840         char *cr;
841         int i;
842
843         for( i=0; i<cred->bv_len; i++) {
844                 if(cred->bv_val[i] == '\0') {
845                         return 1;       /* NUL character in password */
846                 }
847         }
848
849         if( cred->bv_val[i] != '\0' ) {
850                 return -1;      /* cred must behave like a string */
851         }
852
853         if( passwd->bv_len < 2 ) {
854                 return -1;      /* passwd must be at least two characters long */
855         }
856
857         for( i=0; i<passwd->bv_len; i++) {
858                 if(passwd->bv_val[i] == '\0') {
859                         return -1;      /* NUL character in password */
860                 }
861         }
862
863         if( passwd->bv_val[i] != '\0' ) {
864                 return -1;      /* passwd must behave like a string */
865         }
866
867         cr = crypt( cred->bv_val, passwd->bv_val );
868
869         if( cr == NULL || cr[0] == '\0' ) {
870                 /* salt must have been invalid */
871                 return -1;
872         }
873
874         return strcmp( passwd->bv_val, cr ) ? 1 : 0;
875 }
876
877 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
878 static int chk_unix(
879         const struct pw_scheme *sc,
880         const struct berval * passwd,
881         const struct berval * cred )
882 {
883         int i;
884         char *pw,*cr;
885
886         for( i=0; i<cred->bv_len; i++) {
887                 if(cred->bv_val[i] == '\0') {
888                         return -1;      /* NUL character in password */
889                 }
890         }
891         if( cred->bv_val[i] != '\0' ) {
892                 return -1;      /* cred must behave like a string */
893         }
894
895         for( i=0; i<passwd->bv_len; i++) {
896                 if(passwd->bv_val[i] == '\0') {
897                         return -1;      /* NUL character in password */
898                 }
899         }
900
901         if( passwd->bv_val[i] != '\0' ) {
902                 return -1;      /* passwd must behave like a string */
903         }
904
905         {
906                 struct passwd *pwd = getpwnam(passwd->bv_val);
907
908                 if(pwd == NULL) {
909                         return -1;      /* not found */
910                 }
911
912                 pw = pwd->pw_passwd;
913         }
914 #  ifdef HAVE_GETSPNAM
915         {
916                 struct spwd *spwd = getspnam(passwd->bv_val);
917
918                 if(spwd != NULL) {
919                         pw = spwd->sp_pwdp;
920                 }
921         }
922 #  endif
923 #  ifdef HAVE_AIX_SECURITY
924         {
925                 struct userpw *upw = getuserpw(passwd->bv_val);
926
927                 if (upw != NULL) {
928                         pw = upw->upw_passwd;
929                 }
930         }
931 #  endif
932
933         if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
934                 /* password must must be at least two characters long */
935                 return -1;
936         }
937
938         cr = crypt(cred->bv_val, pw);
939
940         if( cr == NULL || cr[0] == '\0' ) {
941                 /* salt must have been invalid */
942                 return -1;
943         }
944
945         return strcmp(pw, cr) ? 1 : 0;
946
947 }
948 # endif
949 #endif
950
951 /* PASSWORD GENERATION ROUTINES */
952
953 static struct berval *hash_ssha1(
954         const struct pw_scheme *scheme,
955         const struct berval  *passwd )
956 {
957         lutil_SHA1_CTX  SHA1context;
958         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
959         unsigned char   saltdata[4];
960         struct berval digest;
961         struct berval salt;
962
963         digest.bv_val = SHA1digest;
964         digest.bv_len = sizeof(SHA1digest);
965         salt.bv_val = saltdata;
966         salt.bv_len = sizeof(saltdata);
967
968         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
969                 return NULL; 
970         }
971
972         lutil_SHA1Init( &SHA1context );
973         lutil_SHA1Update( &SHA1context,
974                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
975         lutil_SHA1Update( &SHA1context,
976                 (const unsigned char *)salt.bv_val, salt.bv_len );
977         lutil_SHA1Final( SHA1digest, &SHA1context );
978
979         return pw_string64( scheme, &digest, &salt);
980 }
981
982 static struct berval *hash_sha1(
983         const struct pw_scheme *scheme,
984         const struct berval  *passwd )
985 {
986         lutil_SHA1_CTX  SHA1context;
987         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
988         struct berval digest;
989         digest.bv_val = SHA1digest;
990         digest.bv_len = sizeof(SHA1digest);
991      
992         lutil_SHA1Init( &SHA1context );
993         lutil_SHA1Update( &SHA1context,
994                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
995         lutil_SHA1Final( SHA1digest, &SHA1context );
996             
997         return pw_string64( scheme, &digest, NULL);
998 }
999
1000 static struct berval *hash_smd5(
1001         const struct pw_scheme *scheme,
1002         const struct berval  *passwd )
1003 {
1004         lutil_MD5_CTX   MD5context;
1005         unsigned char   MD5digest[LUTIL_MD5_BYTES];
1006         unsigned char   saltdata[4];
1007         struct berval digest;
1008         struct berval salt;
1009
1010         digest.bv_val = MD5digest;
1011         digest.bv_len = sizeof(MD5digest);
1012         salt.bv_val = saltdata;
1013         salt.bv_len = sizeof(saltdata);
1014
1015         if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1016                 return NULL; 
1017         }
1018
1019         lutil_MD5Init( &MD5context );
1020         lutil_MD5Update( &MD5context,
1021                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1022         lutil_MD5Update( &MD5context,
1023                 (const unsigned char *) salt.bv_val, salt.bv_len );
1024         lutil_MD5Final( MD5digest, &MD5context );
1025
1026         return pw_string64( scheme, &digest, &salt );
1027 }
1028
1029 static struct berval *hash_md5(
1030         const struct pw_scheme *scheme,
1031         const struct berval  *passwd )
1032 {
1033         lutil_MD5_CTX   MD5context;
1034         unsigned char   MD5digest[LUTIL_MD5_BYTES];
1035
1036         struct berval digest;
1037
1038         digest.bv_val = MD5digest;
1039         digest.bv_len = sizeof(MD5digest);
1040
1041         lutil_MD5Init( &MD5context );
1042         lutil_MD5Update( &MD5context,
1043                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1044         lutil_MD5Final( MD5digest, &MD5context );
1045
1046         return pw_string64( scheme, &digest, NULL );
1047 ;
1048 }
1049
1050 #ifdef SLAPD_LMHASH 
1051 /* pseudocode from RFC2433
1052  * A.2 LmPasswordHash()
1053  * 
1054  *    LmPasswordHash(
1055  *    IN  0-to-14-oem-char Password,
1056  *    OUT 16-octet         PasswordHash )
1057  *    {
1058  *       Set UcasePassword to the uppercased Password
1059  *       Zero pad UcasePassword to 14 characters
1060  * 
1061  *       DesHash( 1st 7-octets of UcasePassword,
1062  *                giving 1st 8-octets of PasswordHash )
1063  * 
1064  *       DesHash( 2nd 7-octets of UcasePassword,
1065  *                giving 2nd 8-octets of PasswordHash )
1066  *    }
1067  * 
1068  * 
1069  * A.3 DesHash()
1070  * 
1071  *    DesHash(
1072  *    IN  7-octet Clear,
1073  *    OUT 8-octet Cypher )
1074  *    {
1075  *        *
1076  *        * Make Cypher an irreversibly encrypted form of Clear by
1077  *        * encrypting known text using Clear as the secret key.
1078  *        * The known text consists of the string
1079  *        *
1080  *        *              KGS!@#$%
1081  *        *
1082  * 
1083  *       Set StdText to "KGS!@#$%"
1084  *       DesEncrypt( StdText, Clear, giving Cypher )
1085  *    }
1086  * 
1087  * 
1088  * A.4 DesEncrypt()
1089  * 
1090  *    DesEncrypt(
1091  *    IN  8-octet Clear,
1092  *    IN  7-octet Key,
1093  *    OUT 8-octet Cypher )
1094  *    {
1095  *        *
1096  *        * Use the DES encryption algorithm [4] in ECB mode [9]
1097  *        * to encrypt Clear into Cypher such that Cypher can
1098  *        * only be decrypted back to Clear by providing Key.
1099  *        * Note that the DES algorithm takes as input a 64-bit
1100  *        * stream where the 8th, 16th, 24th, etc.  bits are
1101  *        * parity bits ignored by the encrypting algorithm.
1102  *        * Unless you write your own DES to accept 56-bit input
1103  *        * without parity, you will need to insert the parity bits
1104  *        * yourself.
1105  *        *
1106  *    }
1107  */
1108
1109 static void lmPasswd_to_key(
1110         const unsigned char *lmPasswd,
1111         des_cblock *key)
1112 {
1113         /* make room for parity bits */
1114         ((char *)key)[0] = lmPasswd[0];
1115         ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1116         ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1117         ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1118         ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1119         ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1120         ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1121         ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1122                 
1123         des_set_odd_parity( key );
1124 }       
1125
1126 static struct berval *hash_lanman(
1127         const struct pw_scheme *scheme,
1128         const struct berval *passwd )
1129 {
1130
1131         int i;
1132         char UcasePassword[15];
1133         des_cblock key;
1134         des_key_schedule schedule;
1135         des_cblock StdText = "KGS!@#$%";
1136         des_cblock hash1, hash2;
1137         char lmhash[33];
1138         struct berval hash;
1139         
1140         for( i=0; i<passwd->bv_len; i++) {
1141                 if(passwd->bv_val[i] == '\0') {
1142                         return NULL;    /* NUL character in password */
1143                 }
1144         }
1145         
1146         if( passwd->bv_val[i] != '\0' ) {
1147                 return NULL;    /* passwd must behave like a string */
1148         }
1149         
1150         strncpy( UcasePassword, passwd->bv_val, 14 );
1151         UcasePassword[14] = '\0';
1152         ldap_pvt_str2upper( UcasePassword );
1153         
1154         lmPasswd_to_key( UcasePassword, &key );
1155         des_set_key_unchecked( &key, schedule );
1156         des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1157         
1158         lmPasswd_to_key( &UcasePassword[7], &key );
1159         des_set_key_unchecked( &key, schedule );
1160         des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1161         
1162         sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
1163                 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1164                 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1165         
1166         hash.bv_val = lmhash;
1167         hash.bv_len = 32;
1168         
1169         return pw_string( scheme, &hash );
1170 }
1171 #endif /* SLAPD_LMHASH */
1172
1173 #ifdef SLAPD_CRYPT
1174 static struct berval *hash_crypt(
1175         const struct pw_scheme *scheme,
1176         const struct berval *passwd )
1177 {
1178         struct berval hash;
1179         unsigned char salt[9];  /* salt suitable for anything */
1180         int i;
1181
1182         for( i=0; i<passwd->bv_len; i++) {
1183                 if(passwd->bv_val[i] == '\0') {
1184                         return NULL;    /* NUL character in password */
1185                 }
1186         }
1187
1188         if( passwd->bv_val[i] != '\0' ) {
1189                 return NULL;    /* passwd must behave like a string */
1190         }
1191
1192         if( lutil_entropy( salt, 8) < 0 ) {
1193                 return NULL; 
1194         }
1195
1196         for( i=0; i<8; i++ ) {
1197                 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1198         }
1199         salt[8] = '\0';
1200
1201         hash.bv_val = crypt( passwd->bv_val, salt );
1202
1203         if( hash.bv_val == NULL ) return NULL;
1204
1205         hash.bv_len = strlen( hash.bv_val );
1206
1207         if( hash.bv_len == 0 ) {
1208                 return NULL;
1209         }
1210
1211         return pw_string( scheme, &hash );
1212 }
1213 #endif