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