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