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