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