]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passwd.c
7f133a8367a2d3e63057124f3663e2bb328f92a1
[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 */
463         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
464                 sizeof(SHA1digest)+SALT_SIZE) {
465                 return LUTIL_PASSWD_ERR;
466         }
467
468         /* decode base64 password */
469         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
470                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
471
472         if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
473
474         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
475
476         if (rc < (int)(sizeof(SHA1digest)+SALT_SIZE)) {
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) <
551                 sizeof(MD5digest)+SALT_SIZE) {
552                 return LUTIL_PASSWD_ERR;
553         }
554
555         /* base64 un-encode password */
556         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
557                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
558
559         if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
560
561         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
562
563         if (rc < (int)(sizeof(MD5digest)+SALT_SIZE)) {
564                 ber_memfree(orig_pass);
565                 return LUTIL_PASSWD_ERR;
566         }
567
568         /* hash credentials with salt */
569         lutil_MD5Init(&MD5context);
570         lutil_MD5Update(&MD5context,
571                 (const unsigned char *) cred->bv_val,
572                 cred->bv_len );
573         lutil_MD5Update(&MD5context,
574                 &orig_pass[sizeof(MD5digest)],
575                 rc - sizeof(MD5digest));
576         lutil_MD5Final(MD5digest, &MD5context);
577
578         /* compare */
579         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
580         ber_memfree(orig_pass);
581         return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
582 }
583
584 static int chk_md5(
585         const struct berval *sc,
586         const struct berval * passwd,
587         const struct berval * cred,
588         const char **text )
589 {
590         lutil_MD5_CTX MD5context;
591         unsigned char MD5digest[LUTIL_MD5_BYTES];
592         int rc;
593         unsigned char *orig_pass = NULL;
594
595         /* safety check */
596         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
597                 return LUTIL_PASSWD_ERR;
598         }
599
600         /* base64 un-encode password */
601         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
602                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
603
604         if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
605
606         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
607         if ( rc != sizeof(MD5digest) ) {
608                 ber_memfree(orig_pass);
609                 return LUTIL_PASSWD_ERR;
610         }
611
612         /* hash credentials with salt */
613         lutil_MD5Init(&MD5context);
614         lutil_MD5Update(&MD5context,
615                 (const unsigned char *) cred->bv_val,
616                 cred->bv_len );
617         lutil_MD5Final(MD5digest, &MD5context);
618
619         /* compare */
620         rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
621         ber_memfree(orig_pass);
622         return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
623 }
624
625 #ifdef SLAPD_LMHASH
626 /* pseudocode from RFC2433
627  * A.2 LmPasswordHash()
628  * 
629  *    LmPasswordHash(
630  *    IN  0-to-14-oem-char Password,
631  *    OUT 16-octet         PasswordHash )
632  *    {
633  *       Set UcasePassword to the uppercased Password
634  *       Zero pad UcasePassword to 14 characters
635  * 
636  *       DesHash( 1st 7-octets of UcasePassword,
637  *                giving 1st 8-octets of PasswordHash )
638  * 
639  *       DesHash( 2nd 7-octets of UcasePassword,
640  *                giving 2nd 8-octets of PasswordHash )
641  *    }
642  * 
643  * 
644  * A.3 DesHash()
645  * 
646  *    DesHash(
647  *    IN  7-octet Clear,
648  *    OUT 8-octet Cypher )
649  *    {
650  *        *
651  *        * Make Cypher an irreversibly encrypted form of Clear by
652  *        * encrypting known text using Clear as the secret key.
653  *        * The known text consists of the string
654  *        *
655  *        *              KGS!@#$%
656  *        *
657  * 
658  *       Set StdText to "KGS!@#$%"
659  *       DesEncrypt( StdText, Clear, giving Cypher )
660  *    }
661  * 
662  * 
663  * A.4 DesEncrypt()
664  * 
665  *    DesEncrypt(
666  *    IN  8-octet Clear,
667  *    IN  7-octet Key,
668  *    OUT 8-octet Cypher )
669  *    {
670  *        *
671  *        * Use the DES encryption algorithm [4] in ECB mode [9]
672  *        * to encrypt Clear into Cypher such that Cypher can
673  *        * only be decrypted back to Clear by providing Key.
674  *        * Note that the DES algorithm takes as input a 64-bit
675  *        * stream where the 8th, 16th, 24th, etc.  bits are
676  *        * parity bits ignored by the encrypting algorithm.
677  *        * Unless you write your own DES to accept 56-bit input
678  *        * without parity, you will need to insert the parity bits
679  *        * yourself.
680  *        *
681  *    }
682  */
683
684 static void lmPasswd_to_key(
685         const unsigned char *lmPasswd,
686         des_cblock *key)
687 {
688         /* make room for parity bits */
689         ((char *)key)[0] = lmPasswd[0];
690         ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
691         ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
692         ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
693         ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
694         ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
695         ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
696         ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
697                 
698         des_set_odd_parity( key );
699 }       
700
701 static int chk_lanman(
702         const struct berval *scheme,
703         const struct berval *passwd,
704         const struct berval *cred,
705         const char **text )
706 {
707         int i;
708         char UcasePassword[15];
709         des_cblock key;
710         des_key_schedule schedule;
711         des_cblock StdText = "KGS!@#$%";
712         des_cblock PasswordHash1, PasswordHash2;
713         char PasswordHash[33], storedPasswordHash[33];
714         
715         for( i=0; i<cred->bv_len; i++) {
716                 if(cred->bv_val[i] == '\0') {
717                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
718                 }
719         }
720         
721         if( cred->bv_val[i] != '\0' ) {
722                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
723         }
724         
725         strncpy( UcasePassword, cred->bv_val, 14 );
726         UcasePassword[14] = '\0';
727         ldap_pvt_str2upper( UcasePassword );
728         
729         lmPasswd_to_key( UcasePassword, &key );
730         des_set_key_unchecked( &key, schedule );
731         des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
732         
733         lmPasswd_to_key( &UcasePassword[7], &key );
734         des_set_key_unchecked( &key, schedule );
735         des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
736         
737         sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
738                 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
739                 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
740                 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
741                 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
742         
743         /* as a precaution convert stored password hash to lower case */
744         strncpy( storedPasswordHash, passwd->bv_val, 32 );
745         storedPasswordHash[32] = '\0';
746         ldap_pvt_str2lower( storedPasswordHash );
747         
748         return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
749 }
750 #endif /* SLAPD_LMHASH */
751
752 #ifdef SLAPD_SPASSWD
753 #ifdef HAVE_CYRUS_SASL
754 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
755 #endif
756
757 static int chk_sasl(
758         const struct berval *sc,
759         const struct berval * passwd,
760         const struct berval * cred,
761         const char **text )
762 {
763         unsigned int i;
764         int rtn;
765
766         for( i=0; i<cred->bv_len; i++) {
767                 if(cred->bv_val[i] == '\0') {
768                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
769                 }
770         }
771
772         if( cred->bv_val[i] != '\0' ) {
773                 return LUTIL_PASSWD_ERR;        /* cred must behave like a string */
774         }
775
776         for( i=0; i<passwd->bv_len; i++) {
777                 if(passwd->bv_val[i] == '\0') {
778                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
779                 }
780         }
781
782         if( passwd->bv_val[i] != '\0' ) {
783                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
784         }
785
786         rtn = LUTIL_PASSWD_ERR;
787
788 #ifdef HAVE_CYRUS_SASL
789         if( lutil_passwd_sasl_conn != NULL ) {
790                 int sc;
791 # if SASL_VERSION_MAJOR < 2
792                 sc = sasl_checkpass( lutil_passwd_sasl_conn,
793                         passwd->bv_val, passwd->bv_len,
794                         cred->bv_val, cred->bv_len,
795                         text );
796 # else
797                 sc = sasl_checkpass( lutil_passwd_sasl_conn,
798                         passwd->bv_val, passwd->bv_len,
799                         cred->bv_val, cred->bv_len );
800 # endif
801                 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
802         }
803 #endif
804
805         return rtn;
806 }
807 #endif
808
809 #ifdef SLAPD_CRYPT
810 static int chk_crypt(
811         const struct berval *sc,
812         const struct berval * passwd,
813         const struct berval * cred,
814         const char **text )
815 {
816         char *cr;
817         unsigned int i;
818
819         for( i=0; i<cred->bv_len; i++) {
820                 if(cred->bv_val[i] == '\0') {
821                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
822                 }
823         }
824
825         if( cred->bv_val[i] != '\0' ) {
826                 return LUTIL_PASSWD_ERR;        /* cred must behave like a string */
827         }
828
829         if( passwd->bv_len < 2 ) {
830                 return LUTIL_PASSWD_ERR;        /* passwd must be at least two characters long */
831         }
832
833         for( i=0; i<passwd->bv_len; i++) {
834                 if(passwd->bv_val[i] == '\0') {
835                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
836                 }
837         }
838
839         if( passwd->bv_val[i] != '\0' ) {
840                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
841         }
842
843         cr = crypt( cred->bv_val, passwd->bv_val );
844
845         if( cr == NULL || cr[0] == '\0' ) {
846                 /* salt must have been invalid */
847                 return LUTIL_PASSWD_ERR;
848         }
849
850         return strcmp( passwd->bv_val, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
851 }
852
853 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
854 static int chk_unix(
855         const struct berval *sc,
856         const struct berval * passwd,
857         const struct berval * cred,
858         const char **text )
859 {
860         unsigned int i;
861         char *pw,*cr;
862
863         for( i=0; i<cred->bv_len; i++) {
864                 if(cred->bv_val[i] == '\0') {
865                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
866                 }
867         }
868         if( cred->bv_val[i] != '\0' ) {
869                 return LUTIL_PASSWD_ERR;        /* cred must behave like a string */
870         }
871
872         for( i=0; i<passwd->bv_len; i++) {
873                 if(passwd->bv_val[i] == '\0') {
874                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
875                 }
876         }
877
878         if( passwd->bv_val[i] != '\0' ) {
879                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
880         }
881
882         {
883                 struct passwd *pwd = getpwnam(passwd->bv_val);
884
885                 if(pwd == NULL) {
886                         return LUTIL_PASSWD_ERR;        /* not found */
887                 }
888
889                 pw = pwd->pw_passwd;
890         }
891 #  ifdef HAVE_GETSPNAM
892         {
893                 struct spwd *spwd = getspnam(passwd->bv_val);
894
895                 if(spwd != NULL) {
896                         pw = spwd->sp_pwdp;
897                 }
898         }
899 #  endif
900 #  ifdef HAVE_AIX_SECURITY
901         {
902                 struct userpw *upw = getuserpw(passwd->bv_val);
903
904                 if (upw != NULL) {
905                         pw = upw->upw_passwd;
906                 }
907         }
908 #  endif
909
910         if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
911                 /* password must must be at least two characters long */
912                 return LUTIL_PASSWD_ERR;
913         }
914
915         cr = crypt(cred->bv_val, pw);
916
917         if( cr == NULL || cr[0] == '\0' ) {
918                 /* salt must have been invalid */
919                 return LUTIL_PASSWD_ERR;
920         }
921
922         return strcmp(pw, cr) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
923
924 }
925 # endif
926 #endif
927
928 /* PASSWORD GENERATION ROUTINES */
929
930 #ifdef LUTIL_SHA1_BYTES
931 static int hash_ssha1(
932         const struct berval *scheme,
933         const struct berval  *passwd,
934         struct berval *hash,
935         const char **text )
936 {
937         lutil_SHA1_CTX  SHA1context;
938         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
939         char            saltdata[SALT_SIZE];
940         struct berval digest;
941         struct berval salt;
942
943         digest.bv_val = (char *) SHA1digest;
944         digest.bv_len = sizeof(SHA1digest);
945         salt.bv_val = saltdata;
946         salt.bv_len = sizeof(saltdata);
947
948         if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
949                 return LUTIL_PASSWD_ERR; 
950         }
951
952         lutil_SHA1Init( &SHA1context );
953         lutil_SHA1Update( &SHA1context,
954                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
955         lutil_SHA1Update( &SHA1context,
956                 (const unsigned char *)salt.bv_val, salt.bv_len );
957         lutil_SHA1Final( SHA1digest, &SHA1context );
958
959         return pw_string64( scheme, &digest, hash, &salt);
960 }
961
962 static int hash_sha1(
963         const struct berval *scheme,
964         const struct berval  *passwd,
965         struct berval *hash,
966         const char **text )
967 {
968         lutil_SHA1_CTX  SHA1context;
969         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
970         struct berval digest;
971         digest.bv_val = (char *) SHA1digest;
972         digest.bv_len = sizeof(SHA1digest);
973      
974         lutil_SHA1Init( &SHA1context );
975         lutil_SHA1Update( &SHA1context,
976                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
977         lutil_SHA1Final( SHA1digest, &SHA1context );
978             
979         return pw_string64( scheme, &digest, hash, NULL);
980 }
981 #endif
982
983 static int hash_smd5(
984         const struct berval *scheme,
985         const struct berval  *passwd,
986         struct berval *hash,
987         const char **text )
988 {
989         lutil_MD5_CTX   MD5context;
990         unsigned char   MD5digest[LUTIL_MD5_BYTES];
991         char            saltdata[SALT_SIZE];
992         struct berval digest;
993         struct berval salt;
994
995         digest.bv_val = (char *) MD5digest;
996         digest.bv_len = sizeof(MD5digest);
997         salt.bv_val = saltdata;
998         salt.bv_len = sizeof(saltdata);
999
1000         if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1001                 return LUTIL_PASSWD_ERR; 
1002         }
1003
1004         lutil_MD5Init( &MD5context );
1005         lutil_MD5Update( &MD5context,
1006                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1007         lutil_MD5Update( &MD5context,
1008                 (const unsigned char *) salt.bv_val, salt.bv_len );
1009         lutil_MD5Final( MD5digest, &MD5context );
1010
1011         return pw_string64( scheme, &digest, hash, &salt );
1012 }
1013
1014 static int hash_md5(
1015         const struct berval *scheme,
1016         const struct berval  *passwd,
1017         struct berval *hash,
1018         const char **text )
1019 {
1020         lutil_MD5_CTX   MD5context;
1021         unsigned char   MD5digest[LUTIL_MD5_BYTES];
1022
1023         struct berval digest;
1024
1025         digest.bv_val = (char *) MD5digest;
1026         digest.bv_len = sizeof(MD5digest);
1027
1028         lutil_MD5Init( &MD5context );
1029         lutil_MD5Update( &MD5context,
1030                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1031         lutil_MD5Final( MD5digest, &MD5context );
1032
1033         return pw_string64( scheme, &digest, hash, NULL );
1034 ;
1035 }
1036
1037 #ifdef SLAPD_LMHASH 
1038 static int hash_lanman(
1039         const struct berval *scheme,
1040         const struct berval *passwd,
1041         struct berval *hash,
1042         const char **text )
1043 {
1044
1045         int i;
1046         char UcasePassword[15];
1047         des_cblock key;
1048         des_key_schedule schedule;
1049         des_cblock StdText = "KGS!@#$%";
1050         des_cblock PasswordHash1, PasswordHash2;
1051         char PasswordHash[33];
1052         
1053         for( i=0; i<passwd->bv_len; i++) {
1054                 if(passwd->bv_val[i] == '\0') {
1055                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
1056                 }
1057         }
1058         
1059         if( passwd->bv_val[i] != '\0' ) {
1060                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
1061         }
1062         
1063         strncpy( UcasePassword, passwd->bv_val, 14 );
1064         UcasePassword[14] = '\0';
1065         ldap_pvt_str2upper( UcasePassword );
1066         
1067         lmPasswd_to_key( UcasePassword, &key );
1068         des_set_key_unchecked( &key, schedule );
1069         des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1070         
1071         lmPasswd_to_key( &UcasePassword[7], &key );
1072         des_set_key_unchecked( &key, schedule );
1073         des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1074         
1075         sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
1076                 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1077                 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1078                 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1079                 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1080         
1081         hash->bv_val = PasswordHash;
1082         hash->bv_len = 32;
1083         
1084         return pw_string( scheme, hash );
1085 }
1086 #endif /* SLAPD_LMHASH */
1087
1088 #ifdef SLAPD_CRYPT
1089 static int hash_crypt(
1090         const struct berval *scheme,
1091         const struct berval *passwd,
1092         struct berval *hash,
1093         const char **text )
1094 {
1095         unsigned char salt[32]; /* salt suitable for most anything */
1096         unsigned int i;
1097
1098         for( i=0; i<passwd->bv_len; i++) {
1099                 if(passwd->bv_val[i] == '\0') {
1100                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
1101                 }
1102         }
1103
1104         if( passwd->bv_val[i] != '\0' ) {
1105                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
1106         }
1107
1108         if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1109                 return LUTIL_PASSWD_ERR; 
1110         }
1111
1112         for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1113                 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1114         }
1115         salt[sizeof( salt ) - 1 ] = '\0';
1116
1117         if( salt_format != NULL ) {
1118                 /* copy the salt we made into entropy before snprintfing
1119                    it back into the salt */
1120                 char entropy[sizeof(salt)];
1121                 strcpy( entropy, (char *) salt );
1122                 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1123         }
1124
1125         hash->bv_val = crypt( passwd->bv_val, (char *) salt );
1126
1127         if( hash->bv_val == NULL ) return -1;
1128
1129         hash->bv_len = strlen( hash->bv_val );
1130
1131         if( hash->bv_len == 0 ) {
1132                 return LUTIL_PASSWD_ERR;
1133         }
1134
1135         return pw_string( scheme, hash );
1136 }
1137 #endif
1138
1139 int lutil_salt_format(const char *format)
1140 {
1141 #ifdef SLAPD_CRYPT
1142         free( salt_format );
1143
1144         salt_format = format != NULL ? strdup( format ) : NULL;
1145 #endif
1146
1147         return 0;
1148 }
1149
1150 #ifdef SLAPD_CLEARTEXT
1151 static int hash_clear(
1152         const struct berval *scheme,
1153         const struct berval  *passwd,
1154         struct berval *hash,
1155         const char **text )
1156 {
1157         ber_dupbv( hash, (struct berval *)passwd );
1158         return LUTIL_PASSWD_OK;
1159 }
1160 #endif
1161