2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * const struct berval *passwd,
19 * const struct berval *cred,
20 * const char **schemes )
22 * Returns true if user supplied credentials (cred) matches
23 * the stored password (passwd).
25 * Due to the use of the crypt(3) function
26 * this routine is NOT thread-safe.
32 #include <ac/stdlib.h>
33 #include <ac/string.h>
34 #include <ac/unistd.h>
37 # ifdef HAVE_SASL_SASL_H
38 # include <sasl/sasl.h>
44 #if defined(SLAPD_LMHASH)
45 # include <openssl/des.h>
46 #endif /* SLAPD_LMHASH */
51 # include <ac/crypt.h>
53 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
60 # ifdef HAVE_AIX_SECURITY
71 #include "lutil_md5.h"
72 #include "lutil_sha1.h"
75 static const unsigned char crypt64[] =
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
79 static char *salt_format = NULL;
84 LUTIL_PASSWD_CHK_FUNC *chk_fn;
85 LUTIL_PASSWD_HASH_FUNC *hash_fn;
89 struct pw_slist *next;
93 /* password check routines */
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;
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;
111 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
112 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
116 static LUTIL_PASSWD_CHK_FUNC chk_sasl;
120 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
121 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
123 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
124 static LUTIL_PASSWD_CHK_FUNC chk_unix;
128 /* password hash routines */
130 #ifdef SLAPD_CLEARTEXT
131 static LUTIL_PASSWD_HASH_FUNC hash_clear;
134 static struct pw_slist *pw_schemes;
136 static const struct pw_scheme pw_schemes_default[] =
138 #ifdef LUTIL_SHA1_BYTES
139 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
140 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
143 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
144 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
147 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
148 #endif /* SLAPD_LMHASH */
151 { BER_BVC("{SASL}"), chk_sasl, NULL },
155 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
156 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
157 { BER_BVC("{UNIX}"), chk_unix, NULL },
161 #ifdef SLAPD_CLEARTEXT
163 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
166 { BER_BVNULL, NULL, NULL }
169 int lutil_passwd_add(
170 struct berval *scheme,
171 LUTIL_PASSWD_CHK_FUNC *chk,
172 LUTIL_PASSWD_HASH_FUNC *hash )
174 struct pw_slist *ptr;
176 ptr = ber_memalloc( sizeof( struct pw_slist ));
178 ptr->next = pw_schemes;
179 ptr->s.name = *scheme;
181 ptr->s.hash_fn = hash;
186 void lutil_passwd_init()
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;
195 void lutil_passwd_destroy()
197 struct pw_slist *ptr, *next;
199 for( ptr=pw_schemes; ptr; ptr=next ) {
205 static const struct pw_scheme *get_scheme(
208 struct pw_slist *pws;
210 if (!pw_schemes) lutil_passwd_init();
212 for( pws=pw_schemes; pws; pws=pws->next ) {
213 if( strcasecmp(scheme, pws->s.name.bv_val ) == 0 ) {
221 int lutil_passwd_scheme(
224 if( scheme == NULL ) {
228 return get_scheme(scheme) != NULL;
232 static int is_allowed_scheme(
234 const char** schemes )
238 if( schemes == NULL ) return 1;
240 for( i=0; schemes[i] != NULL; i++ ) {
241 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
248 static struct berval *passwd_scheme(
249 const struct pw_scheme *scheme,
250 const struct berval * passwd,
252 const char** allowed )
254 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
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;
271 * Return 0 if creds are good.
275 const struct berval *passwd, /* stored passwd */
276 const struct berval *cred, /* user cred */
277 const char **schemes,
280 struct pw_slist *pws;
282 if ( text ) *text = NULL;
284 if (cred == NULL || cred->bv_len == 0 ||
285 passwd == NULL || passwd->bv_len == 0 )
290 if (!pw_schemes) lutil_passwd_init();
292 for( pws=pw_schemes; pws; pws=pws->next ) {
293 if( pws->s.chk_fn ) {
295 struct berval *p = passwd_scheme( &(pws->s),
296 passwd, &x, schemes );
299 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
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 )
315 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
318 if( len < 1 ) return -1;
321 pw->bv_val = ber_memalloc( len + 1 );
323 if( pw->bv_val == NULL ) {
327 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
331 for( len = 0; len < pw->bv_len; len++ ) {
332 pw->bv_val[len] = crypt64[
333 pw->bv_val[len] % (sizeof(crypt64)-1) ];
336 pw->bv_val[len] = '\0';
341 int lutil_passwd_hash(
342 const struct berval * passwd,
347 const struct pw_scheme *sc = get_scheme( method );
353 if( text ) *text = "scheme not recognized";
357 if( ! sc->hash_fn ) {
358 if( text ) *text = "scheme provided no hash function";
362 if( text ) *text = NULL;
364 return (sc->hash_fn)( &sc->name, passwd, hash, text );
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 )
375 pw.bv_len = sc->bv_len + passwd->bv_len;
376 pw.bv_val = ber_memalloc( pw.bv_len + 1 );
378 if( pw.bv_val == NULL ) {
379 return LUTIL_PASSWD_ERR;
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 );
385 pw.bv_val[pw.bv_len] = '\0';
388 return LUTIL_PASSWD_OK;
390 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
392 static int pw_string64(
393 const struct berval *sc,
394 const struct berval *hash,
396 const struct berval *salt )
399 struct berval string;
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 );
407 if( string.bv_val == NULL ) {
408 return LUTIL_PASSWD_ERR;
411 AC_MEMCPY( string.bv_val, hash->bv_val,
413 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
415 string.bv_val[string.bv_len] = '\0';
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 );
425 if( b64->bv_val == NULL ) {
426 if( salt ) ber_memfree( string.bv_val );
427 return LUTIL_PASSWD_ERR;
430 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
433 (unsigned char *) string.bv_val, string.bv_len,
434 &b64->bv_val[sc->bv_len], b64len );
436 if( salt ) ber_memfree( string.bv_val );
439 return LUTIL_PASSWD_ERR;
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;
448 /* PASSWORD CHECK ROUTINES */
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,
457 lutil_SHA1_CTX SHA1context;
458 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
460 unsigned char *orig_pass = NULL;
462 /* safety check -- must have some salt */
463 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
464 return LUTIL_PASSWD_ERR;
467 /* decode base64 password */
468 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
469 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
471 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
473 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
475 /* safety check -- must have some salt */
476 if (rc <= (int)(sizeof(SHA1digest))) {
477 ber_memfree(orig_pass);
478 return LUTIL_PASSWD_ERR;
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);
491 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
492 ber_memfree(orig_pass);
493 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
497 const struct berval *sc,
498 const struct berval * passwd,
499 const struct berval * cred,
502 lutil_SHA1_CTX SHA1context;
503 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
505 unsigned char *orig_pass = NULL;
508 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
509 return LUTIL_PASSWD_ERR;
512 /* base64 un-encode password */
513 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
514 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
516 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
518 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
520 if( rc != sizeof(SHA1digest) ) {
521 ber_memfree(orig_pass);
522 return LUTIL_PASSWD_ERR;
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);
532 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
533 ber_memfree(orig_pass);
534 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
539 const struct berval *sc,
540 const struct berval * passwd,
541 const struct berval * cred,
544 lutil_MD5_CTX MD5context;
545 unsigned char MD5digest[LUTIL_MD5_BYTES];
547 unsigned char *orig_pass = NULL;
550 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
551 return LUTIL_PASSWD_ERR;
554 /* base64 un-encode password */
555 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
556 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
558 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
560 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
562 if (rc <= (int)(sizeof(MD5digest))) {
563 ber_memfree(orig_pass);
564 return LUTIL_PASSWD_ERR;
567 /* hash credentials with salt */
568 lutil_MD5Init(&MD5context);
569 lutil_MD5Update(&MD5context,
570 (const unsigned char *) cred->bv_val,
572 lutil_MD5Update(&MD5context,
573 &orig_pass[sizeof(MD5digest)],
574 rc - sizeof(MD5digest));
575 lutil_MD5Final(MD5digest, &MD5context);
578 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
579 ber_memfree(orig_pass);
580 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
584 const struct berval *sc,
585 const struct berval * passwd,
586 const struct berval * cred,
589 lutil_MD5_CTX MD5context;
590 unsigned char MD5digest[LUTIL_MD5_BYTES];
592 unsigned char *orig_pass = NULL;
595 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
596 return LUTIL_PASSWD_ERR;
599 /* base64 un-encode password */
600 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
601 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
603 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
605 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
606 if ( rc != sizeof(MD5digest) ) {
607 ber_memfree(orig_pass);
608 return LUTIL_PASSWD_ERR;
611 /* hash credentials with salt */
612 lutil_MD5Init(&MD5context);
613 lutil_MD5Update(&MD5context,
614 (const unsigned char *) cred->bv_val,
616 lutil_MD5Final(MD5digest, &MD5context);
619 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
620 ber_memfree(orig_pass);
621 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
625 /* pseudocode from RFC2433
626 * A.2 LmPasswordHash()
629 * IN 0-to-14-oem-char Password,
630 * OUT 16-octet PasswordHash )
632 * Set UcasePassword to the uppercased Password
633 * Zero pad UcasePassword to 14 characters
635 * DesHash( 1st 7-octets of UcasePassword,
636 * giving 1st 8-octets of PasswordHash )
638 * DesHash( 2nd 7-octets of UcasePassword,
639 * giving 2nd 8-octets of PasswordHash )
647 * OUT 8-octet Cypher )
650 * * Make Cypher an irreversibly encrypted form of Clear by
651 * * encrypting known text using Clear as the secret key.
652 * * The known text consists of the string
657 * Set StdText to "KGS!@#$%"
658 * DesEncrypt( StdText, Clear, giving Cypher )
667 * OUT 8-octet Cypher )
670 * * Use the DES encryption algorithm [4] in ECB mode [9]
671 * * to encrypt Clear into Cypher such that Cypher can
672 * * only be decrypted back to Clear by providing Key.
673 * * Note that the DES algorithm takes as input a 64-bit
674 * * stream where the 8th, 16th, 24th, etc. bits are
675 * * parity bits ignored by the encrypting algorithm.
676 * * Unless you write your own DES to accept 56-bit input
677 * * without parity, you will need to insert the parity bits
683 static void lmPasswd_to_key(
684 const unsigned char *lmPasswd,
687 /* make room for parity bits */
688 ((char *)key)[0] = lmPasswd[0];
689 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
690 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
691 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
692 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
693 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
694 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
695 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
697 des_set_odd_parity( key );
700 static int chk_lanman(
701 const struct berval *scheme,
702 const struct berval *passwd,
703 const struct berval *cred,
707 char UcasePassword[15];
709 des_key_schedule schedule;
710 des_cblock StdText = "KGS!@#$%";
711 des_cblock PasswordHash1, PasswordHash2;
712 char PasswordHash[33], storedPasswordHash[33];
714 for( i=0; i<cred->bv_len; i++) {
715 if(cred->bv_val[i] == '\0') {
716 return LUTIL_PASSWD_ERR; /* NUL character in password */
720 if( cred->bv_val[i] != '\0' ) {
721 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
724 strncpy( UcasePassword, cred->bv_val, 14 );
725 UcasePassword[14] = '\0';
726 ldap_pvt_str2upper( UcasePassword );
728 lmPasswd_to_key( UcasePassword, &key );
729 des_set_key_unchecked( &key, schedule );
730 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
732 lmPasswd_to_key( &UcasePassword[7], &key );
733 des_set_key_unchecked( &key, schedule );
734 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
736 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
737 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
738 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
739 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
740 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
742 /* as a precaution convert stored password hash to lower case */
743 strncpy( storedPasswordHash, passwd->bv_val, 32 );
744 storedPasswordHash[32] = '\0';
745 ldap_pvt_str2lower( storedPasswordHash );
747 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
749 #endif /* SLAPD_LMHASH */
752 #ifdef HAVE_CYRUS_SASL
753 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
757 const struct berval *sc,
758 const struct berval * passwd,
759 const struct berval * cred,
765 for( i=0; i<cred->bv_len; i++) {
766 if(cred->bv_val[i] == '\0') {
767 return LUTIL_PASSWD_ERR; /* NUL character in password */
771 if( cred->bv_val[i] != '\0' ) {
772 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
775 for( i=0; i<passwd->bv_len; i++) {
776 if(passwd->bv_val[i] == '\0') {
777 return LUTIL_PASSWD_ERR; /* NUL character in password */
781 if( passwd->bv_val[i] != '\0' ) {
782 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
785 rtn = LUTIL_PASSWD_ERR;
787 #ifdef HAVE_CYRUS_SASL
788 if( lutil_passwd_sasl_conn != NULL ) {
790 # if SASL_VERSION_MAJOR < 2
791 sc = sasl_checkpass( lutil_passwd_sasl_conn,
792 passwd->bv_val, passwd->bv_len,
793 cred->bv_val, cred->bv_len,
796 sc = sasl_checkpass( lutil_passwd_sasl_conn,
797 passwd->bv_val, passwd->bv_len,
798 cred->bv_val, cred->bv_len );
800 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
809 static int chk_crypt(
810 const struct berval *sc,
811 const struct berval * passwd,
812 const struct berval * cred,
818 for( i=0; i<cred->bv_len; i++) {
819 if(cred->bv_val[i] == '\0') {
820 return LUTIL_PASSWD_ERR; /* NUL character in password */
824 if( cred->bv_val[i] != '\0' ) {
825 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
828 if( passwd->bv_len < 2 ) {
829 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
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 */
838 if( passwd->bv_val[i] != '\0' ) {
839 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
842 cr = crypt( cred->bv_val, passwd->bv_val );
844 if( cr == NULL || cr[0] == '\0' ) {
845 /* salt must have been invalid */
846 return LUTIL_PASSWD_ERR;
849 return strcmp( passwd->bv_val, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
852 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
854 const struct berval *sc,
855 const struct berval * passwd,
856 const struct berval * cred,
862 for( i=0; i<cred->bv_len; i++) {
863 if(cred->bv_val[i] == '\0') {
864 return LUTIL_PASSWD_ERR; /* NUL character in password */
867 if( cred->bv_val[i] != '\0' ) {
868 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
871 for( i=0; i<passwd->bv_len; i++) {
872 if(passwd->bv_val[i] == '\0') {
873 return LUTIL_PASSWD_ERR; /* NUL character in password */
877 if( passwd->bv_val[i] != '\0' ) {
878 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
882 struct passwd *pwd = getpwnam(passwd->bv_val);
885 return LUTIL_PASSWD_ERR; /* not found */
890 # ifdef HAVE_GETSPNAM
892 struct spwd *spwd = getspnam(passwd->bv_val);
899 # ifdef HAVE_AIX_SECURITY
901 struct userpw *upw = getuserpw(passwd->bv_val);
904 pw = upw->upw_passwd;
909 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
910 /* password must must be at least two characters long */
911 return LUTIL_PASSWD_ERR;
914 cr = crypt(cred->bv_val, pw);
916 if( cr == NULL || cr[0] == '\0' ) {
917 /* salt must have been invalid */
918 return LUTIL_PASSWD_ERR;
921 return strcmp(pw, cr) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
927 /* PASSWORD GENERATION ROUTINES */
929 #ifdef LUTIL_SHA1_BYTES
930 static int hash_ssha1(
931 const struct berval *scheme,
932 const struct berval *passwd,
936 lutil_SHA1_CTX SHA1context;
937 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
938 char saltdata[SALT_SIZE];
939 struct berval digest;
942 digest.bv_val = (char *) SHA1digest;
943 digest.bv_len = sizeof(SHA1digest);
944 salt.bv_val = saltdata;
945 salt.bv_len = sizeof(saltdata);
947 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
948 return LUTIL_PASSWD_ERR;
951 lutil_SHA1Init( &SHA1context );
952 lutil_SHA1Update( &SHA1context,
953 (const unsigned char *)passwd->bv_val, passwd->bv_len );
954 lutil_SHA1Update( &SHA1context,
955 (const unsigned char *)salt.bv_val, salt.bv_len );
956 lutil_SHA1Final( SHA1digest, &SHA1context );
958 return pw_string64( scheme, &digest, hash, &salt);
961 static int hash_sha1(
962 const struct berval *scheme,
963 const struct berval *passwd,
967 lutil_SHA1_CTX SHA1context;
968 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
969 struct berval digest;
970 digest.bv_val = (char *) SHA1digest;
971 digest.bv_len = sizeof(SHA1digest);
973 lutil_SHA1Init( &SHA1context );
974 lutil_SHA1Update( &SHA1context,
975 (const unsigned char *)passwd->bv_val, passwd->bv_len );
976 lutil_SHA1Final( SHA1digest, &SHA1context );
978 return pw_string64( scheme, &digest, hash, NULL);
982 static int hash_smd5(
983 const struct berval *scheme,
984 const struct berval *passwd,
988 lutil_MD5_CTX MD5context;
989 unsigned char MD5digest[LUTIL_MD5_BYTES];
990 char saltdata[SALT_SIZE];
991 struct berval digest;
994 digest.bv_val = (char *) MD5digest;
995 digest.bv_len = sizeof(MD5digest);
996 salt.bv_val = saltdata;
997 salt.bv_len = sizeof(saltdata);
999 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1000 return LUTIL_PASSWD_ERR;
1003 lutil_MD5Init( &MD5context );
1004 lutil_MD5Update( &MD5context,
1005 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1006 lutil_MD5Update( &MD5context,
1007 (const unsigned char *) salt.bv_val, salt.bv_len );
1008 lutil_MD5Final( MD5digest, &MD5context );
1010 return pw_string64( scheme, &digest, hash, &salt );
1013 static int hash_md5(
1014 const struct berval *scheme,
1015 const struct berval *passwd,
1016 struct berval *hash,
1019 lutil_MD5_CTX MD5context;
1020 unsigned char MD5digest[LUTIL_MD5_BYTES];
1022 struct berval digest;
1024 digest.bv_val = (char *) MD5digest;
1025 digest.bv_len = sizeof(MD5digest);
1027 lutil_MD5Init( &MD5context );
1028 lutil_MD5Update( &MD5context,
1029 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1030 lutil_MD5Final( MD5digest, &MD5context );
1032 return pw_string64( scheme, &digest, hash, NULL );
1037 static int hash_lanman(
1038 const struct berval *scheme,
1039 const struct berval *passwd,
1040 struct berval *hash,
1045 char UcasePassword[15];
1047 des_key_schedule schedule;
1048 des_cblock StdText = "KGS!@#$%";
1049 des_cblock PasswordHash1, PasswordHash2;
1050 char PasswordHash[33];
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 */
1058 if( passwd->bv_val[i] != '\0' ) {
1059 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1062 strncpy( UcasePassword, passwd->bv_val, 14 );
1063 UcasePassword[14] = '\0';
1064 ldap_pvt_str2upper( UcasePassword );
1066 lmPasswd_to_key( UcasePassword, &key );
1067 des_set_key_unchecked( &key, schedule );
1068 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1070 lmPasswd_to_key( &UcasePassword[7], &key );
1071 des_set_key_unchecked( &key, schedule );
1072 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1074 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1075 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1076 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1077 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1078 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1080 hash->bv_val = PasswordHash;
1083 return pw_string( scheme, hash );
1085 #endif /* SLAPD_LMHASH */
1088 static int hash_crypt(
1089 const struct berval *scheme,
1090 const struct berval *passwd,
1091 struct berval *hash,
1094 unsigned char salt[32]; /* salt suitable for most anything */
1097 for( i=0; i<passwd->bv_len; i++) {
1098 if(passwd->bv_val[i] == '\0') {
1099 return LUTIL_PASSWD_ERR; /* NUL character in password */
1103 if( passwd->bv_val[i] != '\0' ) {
1104 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1107 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1108 return LUTIL_PASSWD_ERR;
1111 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1112 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1114 salt[sizeof( salt ) - 1 ] = '\0';
1116 if( salt_format != NULL ) {
1117 /* copy the salt we made into entropy before snprintfing
1118 it back into the salt */
1119 char entropy[sizeof(salt)];
1120 strcpy( entropy, (char *) salt );
1121 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1124 hash->bv_val = crypt( passwd->bv_val, (char *) salt );
1126 if( hash->bv_val == NULL ) return -1;
1128 hash->bv_len = strlen( hash->bv_val );
1130 if( hash->bv_len == 0 ) {
1131 return LUTIL_PASSWD_ERR;
1134 return pw_string( scheme, hash );
1138 int lutil_salt_format(const char *format)
1141 free( salt_format );
1143 salt_format = format != NULL ? strdup( format ) : NULL;
1149 #ifdef SLAPD_CLEARTEXT
1150 static int hash_clear(
1151 const struct berval *scheme,
1152 const struct berval *passwd,
1153 struct berval *hash,
1156 ber_dupbv( hash, (struct berval *)passwd );
1157 return LUTIL_PASSWD_OK;