]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passwd.c
4d12404e66db9992d27f8a14a3626655deffc8cb
[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_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_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_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( bv.bv_len != pws->s.name.bv_len )
213                         continue;
214                 if( strncasecmp(bv.bv_val, pws->s.name.bv_val, bv.bv_len ) == 0 ) {
215                         return &(pws->s);
216                 }
217         }
218
219         return NULL;
220 }
221
222 int lutil_passwd_scheme(
223         const char* scheme )
224 {
225         if( scheme == NULL ) {
226                 return 0;
227         }
228
229         return get_scheme(scheme) != NULL;
230 }
231
232
233 static int is_allowed_scheme( 
234         const char* scheme,
235         const char** schemes )
236 {
237         int i;
238
239         if( schemes == NULL ) return 1;
240
241         for( i=0; schemes[i] != NULL; i++ ) {
242                 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
243                         return 1;
244                 }
245         }
246         return 0;
247 }
248
249 static struct berval *passwd_scheme(
250         const struct pw_scheme *scheme,
251         const struct berval * passwd,
252         struct berval *bv,
253         const char** allowed )
254 {
255         if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
256                 return NULL;
257         }
258
259         if( passwd->bv_len >= scheme->name.bv_len ) {
260                 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
261                         bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
262                         bv->bv_len = passwd->bv_len - scheme->name.bv_len;
263
264                         return bv;
265                 }
266         }
267
268         return NULL;
269 }
270
271 /*
272  * Return 0 if creds are good.
273  */
274 int
275 lutil_passwd(
276         const struct berval *passwd,    /* stored passwd */
277         const struct berval *cred,              /* user cred */
278         const char **schemes,
279         const char **text )
280 {
281         struct pw_slist *pws;
282
283         if ( text ) *text = NULL;
284
285         if (cred == NULL || cred->bv_len == 0 ||
286                 passwd == NULL || passwd->bv_len == 0 )
287         {
288                 return -1;
289         }
290
291         if (!pw_inited) lutil_passwd_init();
292
293         for( pws=pw_schemes; pws; pws=pws->next ) {
294                 if( pws->s.chk_fn ) {
295                         struct berval x;
296                         struct berval *p = passwd_scheme( &(pws->s),
297                                 passwd, &x, schemes );
298
299                         if( p != NULL ) {
300                                 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
301                         }
302                 }
303         }
304
305 #ifdef SLAPD_CLEARTEXT
306         if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
307                 return (( passwd->bv_len == cred->bv_len ) &&
308                                 ( passwd->bv_val[0] != '{' /*'}'*/ ))
309                         ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
310                         : 1;
311         }
312 #endif
313         return 1;
314 }
315
316 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
317 {
318
319         if( len < 1 ) return -1;
320
321         pw->bv_len = len;
322         pw->bv_val = ber_memalloc( len + 1 );
323
324         if( pw->bv_val == NULL ) {
325                 return -1;
326         }
327
328         if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
329                 return -1; 
330         }
331
332         for( len = 0; len < pw->bv_len; len++ ) {
333                 pw->bv_val[len] = crypt64[
334                         pw->bv_val[len] % (sizeof(crypt64)-1) ];
335         }
336
337         pw->bv_val[len] = '\0';
338         
339         return 0;
340 }
341
342 int lutil_passwd_hash(
343         const struct berval * passwd,
344         const char * method,
345         struct berval *hash,
346         const char **text )
347 {
348         const struct pw_scheme *sc = get_scheme( method );
349
350         hash->bv_val = NULL;
351         hash->bv_len = 0;
352
353         if( sc == NULL ) {
354                 if( text ) *text = "scheme not recognized";
355                 return -1;
356         }
357
358         if( ! sc->hash_fn ) {
359                 if( text ) *text = "scheme provided no hash function";
360                 return -1;
361         }
362
363         if( text ) *text = NULL;
364
365         return (sc->hash_fn)( &sc->name, passwd, hash, text );
366 }
367
368 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
369 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
370 static int pw_string(
371         const struct berval *sc,
372         struct berval *passwd )
373 {
374         struct berval pw;
375
376         pw.bv_len = sc->bv_len + passwd->bv_len;
377         pw.bv_val = ber_memalloc( pw.bv_len + 1 );
378
379         if( pw.bv_val == NULL ) {
380                 return LUTIL_PASSWD_ERR;
381         }
382
383         AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
384         AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
385
386         pw.bv_val[pw.bv_len] = '\0';
387         *passwd = pw;
388
389         return LUTIL_PASSWD_OK;
390 }
391 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
392
393 static int pw_string64(
394         const struct berval *sc,
395         const struct berval *hash,
396         struct berval *b64,
397         const struct berval *salt )
398 {
399         int rc;
400         struct berval string;
401         size_t b64len;
402
403         if( salt ) {
404                 /* need to base64 combined string */
405                 string.bv_len = hash->bv_len + salt->bv_len;
406                 string.bv_val = ber_memalloc( string.bv_len + 1 );
407
408                 if( string.bv_val == NULL ) {
409                         return LUTIL_PASSWD_ERR;
410                 }
411
412                 AC_MEMCPY( string.bv_val, hash->bv_val,
413                         hash->bv_len );
414                 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
415                         salt->bv_len );
416                 string.bv_val[string.bv_len] = '\0';
417
418         } else {
419                 string = *hash;
420         }
421
422         b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
423         b64->bv_len = b64len + sc->bv_len;
424         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
425
426         if( b64->bv_val == NULL ) {
427                 if( salt ) ber_memfree( string.bv_val );
428                 return LUTIL_PASSWD_ERR;
429         }
430
431         AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
432
433         rc = lutil_b64_ntop(
434                 (unsigned char *) string.bv_val, string.bv_len,
435                 &b64->bv_val[sc->bv_len], b64len );
436
437         if( salt ) ber_memfree( string.bv_val );
438         
439         if( rc < 0 ) {
440                 return LUTIL_PASSWD_ERR;
441         }
442
443         /* recompute length */
444         b64->bv_len = sc->bv_len + rc;
445         assert( strlen(b64->bv_val) == b64->bv_len );
446         return LUTIL_PASSWD_OK;
447 }
448
449 /* PASSWORD CHECK ROUTINES */
450
451 #ifdef LUTIL_SHA1_BYTES
452 static int chk_ssha1(
453         const struct berval *sc,
454         const struct berval * passwd,
455         const struct berval * cred,
456         const char **text )
457 {
458         lutil_SHA1_CTX SHA1context;
459         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
460         int rc;
461         unsigned char *orig_pass = NULL;
462
463         /* safety check -- must have some salt */
464         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
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         /* safety check -- must have some salt */
477         if (rc <= (int)(sizeof(SHA1digest))) {
478                 ber_memfree(orig_pass);
479                 return LUTIL_PASSWD_ERR;
480         }
481  
482         /* hash credentials with salt */
483         lutil_SHA1Init(&SHA1context);
484         lutil_SHA1Update(&SHA1context,
485                 (const unsigned char *) cred->bv_val, cred->bv_len);
486         lutil_SHA1Update(&SHA1context,
487                 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
488                 rc - sizeof(SHA1digest));
489         lutil_SHA1Final(SHA1digest, &SHA1context);
490  
491         /* compare */
492         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
493         ber_memfree(orig_pass);
494         return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
495 }
496
497 static int chk_sha1(
498         const struct berval *sc,
499         const struct berval * passwd,
500         const struct berval * cred,
501         const char **text )
502 {
503         lutil_SHA1_CTX SHA1context;
504         unsigned char SHA1digest[LUTIL_SHA1_BYTES];
505         int rc;
506         unsigned char *orig_pass = NULL;
507  
508         /* safety check */
509         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
510                 return LUTIL_PASSWD_ERR;
511         }
512
513         /* base64 un-encode password */
514         orig_pass = (unsigned char *) ber_memalloc( (size_t) (
515                 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
516
517         if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
518
519         rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
520
521         if( rc != sizeof(SHA1digest) ) {
522                 ber_memfree(orig_pass);
523                 return LUTIL_PASSWD_ERR;
524         }
525  
526         /* hash credentials with salt */
527         lutil_SHA1Init(&SHA1context);
528         lutil_SHA1Update(&SHA1context,
529                 (const unsigned char *) cred->bv_val, cred->bv_len);
530         lutil_SHA1Final(SHA1digest, &SHA1context);
531  
532         /* compare */
533         rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
534         ber_memfree(orig_pass);
535         return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
536 }
537 #endif
538
539 static int chk_smd5(
540         const struct berval *sc,
541         const struct berval * passwd,
542         const struct berval * cred,
543         const char **text )
544 {
545         lutil_MD5_CTX MD5context;
546         unsigned char MD5digest[LUTIL_MD5_BYTES];
547         int rc;
548         unsigned char *orig_pass = NULL;
549
550         /* safety check */
551         if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
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))) {
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 char *lmPasswd,
686         des_cblock *key)
687 {
688         const unsigned char *lpw = (const unsigned char *) lmPasswd;
689         unsigned char *k = (unsigned char *) key;
690
691         /* make room for parity bits */
692         k[0] = lpw[0];
693         k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
694         k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
695         k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
696         k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
697         k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
698         k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
699         k[7] = ((lpw[6] & 0x7F) << 1);
700                 
701         des_set_odd_parity( key );
702 }       
703
704 static int chk_lanman(
705         const struct berval *scheme,
706         const struct berval *passwd,
707         const struct berval *cred,
708         const char **text )
709 {
710         int i;
711         char UcasePassword[15];
712         des_cblock key;
713         des_key_schedule schedule;
714         des_cblock StdText = "KGS!@#$%";
715         des_cblock PasswordHash1, PasswordHash2;
716         char PasswordHash[33], storedPasswordHash[33];
717         
718         for( i=0; i<cred->bv_len; i++) {
719                 if(cred->bv_val[i] == '\0') {
720                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
721                 }
722         }
723         
724         if( cred->bv_val[i] != '\0' ) {
725                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
726         }
727         
728         strncpy( UcasePassword, cred->bv_val, 14 );
729         UcasePassword[14] = '\0';
730         ldap_pvt_str2upper( UcasePassword );
731         
732         lmPasswd_to_key( UcasePassword, &key );
733         des_set_key_unchecked( &key, schedule );
734         des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
735         
736         lmPasswd_to_key( &UcasePassword[7], &key );
737         des_set_key_unchecked( &key, schedule );
738         des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
739         
740         sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
741                 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
742                 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
743                 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
744                 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
745         
746         /* as a precaution convert stored password hash to lower case */
747         strncpy( storedPasswordHash, passwd->bv_val, 32 );
748         storedPasswordHash[32] = '\0';
749         ldap_pvt_str2lower( storedPasswordHash );
750         
751         return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
752 }
753 #endif /* SLAPD_LMHASH */
754
755 #ifdef SLAPD_CRYPT
756 static int lutil_crypt(
757         const char *key,
758         const char *salt,
759         char **hash )
760 {
761         char *cr = crypt( key, salt );
762         int rc;
763
764         if( cr == NULL || cr[0] == '\0' ) {
765                 /* salt must have been invalid */
766                 rc = LUTIL_PASSWD_ERR;
767         } else {
768                 if ( hash ) {
769                         *hash = ber_strdup( cr );
770                         rc = LUTIL_PASSWD_OK;
771                 } else {
772                         rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
773                 }
774         }
775         return rc;
776 }
777
778 static int chk_crypt(
779         const struct berval *sc,
780         const struct berval * passwd,
781         const struct berval * cred,
782         const char **text )
783 {
784         unsigned int i;
785
786         for( i=0; i<cred->bv_len; i++) {
787                 if(cred->bv_val[i] == '\0') {
788                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
789                 }
790         }
791
792         if( cred->bv_val[i] != '\0' ) {
793                 return LUTIL_PASSWD_ERR;        /* cred must behave like a string */
794         }
795
796         if( passwd->bv_len < 2 ) {
797                 return LUTIL_PASSWD_ERR;        /* passwd must be at least two characters long */
798         }
799
800         for( i=0; i<passwd->bv_len; i++) {
801                 if(passwd->bv_val[i] == '\0') {
802                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
803                 }
804         }
805
806         if( passwd->bv_val[i] != '\0' ) {
807                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
808         }
809
810         return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
811 }
812
813 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
814 static int chk_unix(
815         const struct berval *sc,
816         const struct berval * passwd,
817         const struct berval * cred,
818         const char **text )
819 {
820         unsigned int i;
821         char *pw;
822
823         for( i=0; i<cred->bv_len; i++) {
824                 if(cred->bv_val[i] == '\0') {
825                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
826                 }
827         }
828         if( cred->bv_val[i] != '\0' ) {
829                 return LUTIL_PASSWD_ERR;        /* cred must behave like a string */
830         }
831
832         for( i=0; i<passwd->bv_len; i++) {
833                 if(passwd->bv_val[i] == '\0') {
834                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
835                 }
836         }
837
838         if( passwd->bv_val[i] != '\0' ) {
839                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
840         }
841
842         {
843                 struct passwd *pwd = getpwnam(passwd->bv_val);
844
845                 if(pwd == NULL) {
846                         return LUTIL_PASSWD_ERR;        /* not found */
847                 }
848
849                 pw = pwd->pw_passwd;
850         }
851 #  ifdef HAVE_GETSPNAM
852         {
853                 struct spwd *spwd = getspnam(passwd->bv_val);
854
855                 if(spwd != NULL) {
856                         pw = spwd->sp_pwdp;
857                 }
858         }
859 #  endif
860 #  ifdef HAVE_AIX_SECURITY
861         {
862                 struct userpw *upw = getuserpw(passwd->bv_val);
863
864                 if (upw != NULL) {
865                         pw = upw->upw_passwd;
866                 }
867         }
868 #  endif
869
870         if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
871                 /* password must must be at least two characters long */
872                 return LUTIL_PASSWD_ERR;
873         }
874
875         return lutil_cryptptr( cred->bv_val, pw, NULL );
876 }
877 # endif
878 #endif
879
880 /* PASSWORD GENERATION ROUTINES */
881
882 #ifdef LUTIL_SHA1_BYTES
883 static int hash_ssha1(
884         const struct berval *scheme,
885         const struct berval  *passwd,
886         struct berval *hash,
887         const char **text )
888 {
889         lutil_SHA1_CTX  SHA1context;
890         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
891         char            saltdata[SALT_SIZE];
892         struct berval digest;
893         struct berval salt;
894
895         digest.bv_val = (char *) SHA1digest;
896         digest.bv_len = sizeof(SHA1digest);
897         salt.bv_val = saltdata;
898         salt.bv_len = sizeof(saltdata);
899
900         if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
901                 return LUTIL_PASSWD_ERR; 
902         }
903
904         lutil_SHA1Init( &SHA1context );
905         lutil_SHA1Update( &SHA1context,
906                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
907         lutil_SHA1Update( &SHA1context,
908                 (const unsigned char *)salt.bv_val, salt.bv_len );
909         lutil_SHA1Final( SHA1digest, &SHA1context );
910
911         return pw_string64( scheme, &digest, hash, &salt);
912 }
913
914 static int hash_sha1(
915         const struct berval *scheme,
916         const struct berval  *passwd,
917         struct berval *hash,
918         const char **text )
919 {
920         lutil_SHA1_CTX  SHA1context;
921         unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
922         struct berval digest;
923         digest.bv_val = (char *) SHA1digest;
924         digest.bv_len = sizeof(SHA1digest);
925      
926         lutil_SHA1Init( &SHA1context );
927         lutil_SHA1Update( &SHA1context,
928                 (const unsigned char *)passwd->bv_val, passwd->bv_len );
929         lutil_SHA1Final( SHA1digest, &SHA1context );
930             
931         return pw_string64( scheme, &digest, hash, NULL);
932 }
933 #endif
934
935 static int hash_smd5(
936         const struct berval *scheme,
937         const struct berval  *passwd,
938         struct berval *hash,
939         const char **text )
940 {
941         lutil_MD5_CTX   MD5context;
942         unsigned char   MD5digest[LUTIL_MD5_BYTES];
943         char            saltdata[SALT_SIZE];
944         struct berval digest;
945         struct berval salt;
946
947         digest.bv_val = (char *) MD5digest;
948         digest.bv_len = sizeof(MD5digest);
949         salt.bv_val = saltdata;
950         salt.bv_len = sizeof(saltdata);
951
952         if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
953                 return LUTIL_PASSWD_ERR; 
954         }
955
956         lutil_MD5Init( &MD5context );
957         lutil_MD5Update( &MD5context,
958                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
959         lutil_MD5Update( &MD5context,
960                 (const unsigned char *) salt.bv_val, salt.bv_len );
961         lutil_MD5Final( MD5digest, &MD5context );
962
963         return pw_string64( scheme, &digest, hash, &salt );
964 }
965
966 static int hash_md5(
967         const struct berval *scheme,
968         const struct berval  *passwd,
969         struct berval *hash,
970         const char **text )
971 {
972         lutil_MD5_CTX   MD5context;
973         unsigned char   MD5digest[LUTIL_MD5_BYTES];
974
975         struct berval digest;
976
977         digest.bv_val = (char *) MD5digest;
978         digest.bv_len = sizeof(MD5digest);
979
980         lutil_MD5Init( &MD5context );
981         lutil_MD5Update( &MD5context,
982                 (const unsigned char *) passwd->bv_val, passwd->bv_len );
983         lutil_MD5Final( MD5digest, &MD5context );
984
985         return pw_string64( scheme, &digest, hash, NULL );
986 ;
987 }
988
989 #ifdef SLAPD_LMHASH 
990 static int hash_lanman(
991         const struct berval *scheme,
992         const struct berval *passwd,
993         struct berval *hash,
994         const char **text )
995 {
996
997         int i;
998         char UcasePassword[15];
999         des_cblock key;
1000         des_key_schedule schedule;
1001         des_cblock StdText = "KGS!@#$%";
1002         des_cblock PasswordHash1, PasswordHash2;
1003         char PasswordHash[33];
1004         
1005         for( i=0; i<passwd->bv_len; i++) {
1006                 if(passwd->bv_val[i] == '\0') {
1007                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
1008                 }
1009         }
1010         
1011         if( passwd->bv_val[i] != '\0' ) {
1012                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
1013         }
1014         
1015         strncpy( UcasePassword, passwd->bv_val, 14 );
1016         UcasePassword[14] = '\0';
1017         ldap_pvt_str2upper( UcasePassword );
1018         
1019         lmPasswd_to_key( UcasePassword, &key );
1020         des_set_key_unchecked( &key, schedule );
1021         des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1022         
1023         lmPasswd_to_key( &UcasePassword[7], &key );
1024         des_set_key_unchecked( &key, schedule );
1025         des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1026         
1027         sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
1028                 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1029                 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1030                 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1031                 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1032         
1033         hash->bv_val = PasswordHash;
1034         hash->bv_len = 32;
1035         
1036         return pw_string( scheme, hash );
1037 }
1038 #endif /* SLAPD_LMHASH */
1039
1040 #ifdef SLAPD_CRYPT
1041 static int hash_crypt(
1042         const struct berval *scheme,
1043         const struct berval *passwd,
1044         struct berval *hash,
1045         const char **text )
1046 {
1047         unsigned char salt[32]; /* salt suitable for most anything */
1048         unsigned int i;
1049         char *save;
1050         int rc;
1051
1052         for( i=0; i<passwd->bv_len; i++) {
1053                 if(passwd->bv_val[i] == '\0') {
1054                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
1055                 }
1056         }
1057
1058         if( passwd->bv_val[i] != '\0' ) {
1059                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
1060         }
1061
1062         if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1063                 return LUTIL_PASSWD_ERR; 
1064         }
1065
1066         for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1067                 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1068         }
1069         salt[sizeof( salt ) - 1 ] = '\0';
1070
1071         if( salt_format != NULL ) {
1072                 /* copy the salt we made into entropy before snprintfing
1073                    it back into the salt */
1074                 char entropy[sizeof(salt)];
1075                 strcpy( entropy, (char *) salt );
1076                 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1077         }
1078
1079         rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
1080         if ( rc != LUTIL_PASSWD_OK ) return rc;
1081
1082         if( hash->bv_val == NULL ) return -1;
1083
1084         hash->bv_len = strlen( hash->bv_val );
1085
1086         save = hash->bv_val;
1087
1088         if( hash->bv_len == 0 ) {
1089                 rc = LUTIL_PASSWD_ERR;
1090         } else {
1091                 rc = pw_string( scheme, hash );
1092         }
1093         ber_memfree( save );
1094         return rc;
1095 }
1096 #endif
1097
1098 int lutil_salt_format(const char *format)
1099 {
1100 #ifdef SLAPD_CRYPT
1101         free( salt_format );
1102
1103         salt_format = format != NULL ? ber_strdup( format ) : NULL;
1104 #endif
1105
1106         return 0;
1107 }
1108
1109 #ifdef SLAPD_CLEARTEXT
1110 static int hash_clear(
1111         const struct berval *scheme,
1112         const struct berval  *passwd,
1113         struct berval *hash,
1114         const char **text )
1115 {
1116         ber_dupbv( hash, (struct berval *)passwd );
1117         return LUTIL_PASSWD_OK;
1118 }
1119 #endif
1120