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