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