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;
463 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
464 sizeof(SHA1digest)+SALT_SIZE) {
465 return LUTIL_PASSWD_ERR;
468 /* decode base64 password */
469 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
470 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
472 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
474 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
476 if (rc < (int)(sizeof(SHA1digest)+SALT_SIZE)) {
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) <
551 sizeof(MD5digest)+SALT_SIZE) {
552 return LUTIL_PASSWD_ERR;
555 /* base64 un-encode password */
556 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
557 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
559 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
561 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
563 if (rc < (int)(sizeof(MD5digest)+SALT_SIZE)) {
564 ber_memfree(orig_pass);
565 return LUTIL_PASSWD_ERR;
568 /* hash credentials with salt */
569 lutil_MD5Init(&MD5context);
570 lutil_MD5Update(&MD5context,
571 (const unsigned char *) cred->bv_val,
573 lutil_MD5Update(&MD5context,
574 &orig_pass[sizeof(MD5digest)],
575 rc - sizeof(MD5digest));
576 lutil_MD5Final(MD5digest, &MD5context);
579 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
580 ber_memfree(orig_pass);
581 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
585 const struct berval *sc,
586 const struct berval * passwd,
587 const struct berval * cred,
590 lutil_MD5_CTX MD5context;
591 unsigned char MD5digest[LUTIL_MD5_BYTES];
593 unsigned char *orig_pass = NULL;
596 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
597 return LUTIL_PASSWD_ERR;
600 /* base64 un-encode password */
601 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
602 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
604 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
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;
612 /* hash credentials with salt */
613 lutil_MD5Init(&MD5context);
614 lutil_MD5Update(&MD5context,
615 (const unsigned char *) cred->bv_val,
617 lutil_MD5Final(MD5digest, &MD5context);
620 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
621 ber_memfree(orig_pass);
622 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
626 /* pseudocode from RFC2433
627 * A.2 LmPasswordHash()
630 * IN 0-to-14-oem-char Password,
631 * OUT 16-octet PasswordHash )
633 * Set UcasePassword to the uppercased Password
634 * Zero pad UcasePassword to 14 characters
636 * DesHash( 1st 7-octets of UcasePassword,
637 * giving 1st 8-octets of PasswordHash )
639 * DesHash( 2nd 7-octets of UcasePassword,
640 * giving 2nd 8-octets of PasswordHash )
648 * OUT 8-octet Cypher )
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
658 * Set StdText to "KGS!@#$%"
659 * DesEncrypt( StdText, Clear, giving Cypher )
668 * OUT 8-octet Cypher )
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
684 static void lmPasswd_to_key(
685 const unsigned char *lmPasswd,
688 /* make room for parity bits */
689 ((char *)key)[0] = lmPasswd[0];
690 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
691 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
692 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
693 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
694 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
695 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
696 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
698 des_set_odd_parity( key );
701 static int chk_lanman(
702 const struct berval *scheme,
703 const struct berval *passwd,
704 const struct berval *cred,
708 char UcasePassword[15];
710 des_key_schedule schedule;
711 des_cblock StdText = "KGS!@#$%";
712 des_cblock PasswordHash1, PasswordHash2;
713 char PasswordHash[33], storedPasswordHash[33];
715 for( i=0; i<cred->bv_len; i++) {
716 if(cred->bv_val[i] == '\0') {
717 return LUTIL_PASSWD_ERR; /* NUL character in password */
721 if( cred->bv_val[i] != '\0' ) {
722 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
725 strncpy( UcasePassword, cred->bv_val, 14 );
726 UcasePassword[14] = '\0';
727 ldap_pvt_str2upper( UcasePassword );
729 lmPasswd_to_key( UcasePassword, &key );
730 des_set_key_unchecked( &key, schedule );
731 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
733 lmPasswd_to_key( &UcasePassword[7], &key );
734 des_set_key_unchecked( &key, schedule );
735 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
737 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
738 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
739 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
740 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
741 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
743 /* as a precaution convert stored password hash to lower case */
744 strncpy( storedPasswordHash, passwd->bv_val, 32 );
745 storedPasswordHash[32] = '\0';
746 ldap_pvt_str2lower( storedPasswordHash );
748 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
750 #endif /* SLAPD_LMHASH */
753 #ifdef HAVE_CYRUS_SASL
754 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
758 const struct berval *sc,
759 const struct berval * passwd,
760 const struct berval * cred,
766 for( i=0; i<cred->bv_len; i++) {
767 if(cred->bv_val[i] == '\0') {
768 return LUTIL_PASSWD_ERR; /* NUL character in password */
772 if( cred->bv_val[i] != '\0' ) {
773 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
776 for( i=0; i<passwd->bv_len; i++) {
777 if(passwd->bv_val[i] == '\0') {
778 return LUTIL_PASSWD_ERR; /* NUL character in password */
782 if( passwd->bv_val[i] != '\0' ) {
783 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
786 rtn = LUTIL_PASSWD_ERR;
788 #ifdef HAVE_CYRUS_SASL
789 if( lutil_passwd_sasl_conn != NULL ) {
791 # if SASL_VERSION_MAJOR < 2
792 sc = sasl_checkpass( lutil_passwd_sasl_conn,
793 passwd->bv_val, passwd->bv_len,
794 cred->bv_val, cred->bv_len,
797 sc = sasl_checkpass( lutil_passwd_sasl_conn,
798 passwd->bv_val, passwd->bv_len,
799 cred->bv_val, cred->bv_len );
801 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
810 static int chk_crypt(
811 const struct berval *sc,
812 const struct berval * passwd,
813 const struct berval * cred,
819 for( i=0; i<cred->bv_len; i++) {
820 if(cred->bv_val[i] == '\0') {
821 return LUTIL_PASSWD_ERR; /* NUL character in password */
825 if( cred->bv_val[i] != '\0' ) {
826 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
829 if( passwd->bv_len < 2 ) {
830 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
833 for( i=0; i<passwd->bv_len; i++) {
834 if(passwd->bv_val[i] == '\0') {
835 return LUTIL_PASSWD_ERR; /* NUL character in password */
839 if( passwd->bv_val[i] != '\0' ) {
840 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
843 cr = crypt( cred->bv_val, passwd->bv_val );
845 if( cr == NULL || cr[0] == '\0' ) {
846 /* salt must have been invalid */
847 return LUTIL_PASSWD_ERR;
850 return strcmp( passwd->bv_val, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
853 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
855 const struct berval *sc,
856 const struct berval * passwd,
857 const struct berval * cred,
863 for( i=0; i<cred->bv_len; i++) {
864 if(cred->bv_val[i] == '\0') {
865 return LUTIL_PASSWD_ERR; /* NUL character in password */
868 if( cred->bv_val[i] != '\0' ) {
869 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
872 for( i=0; i<passwd->bv_len; i++) {
873 if(passwd->bv_val[i] == '\0') {
874 return LUTIL_PASSWD_ERR; /* NUL character in password */
878 if( passwd->bv_val[i] != '\0' ) {
879 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
883 struct passwd *pwd = getpwnam(passwd->bv_val);
886 return LUTIL_PASSWD_ERR; /* not found */
891 # ifdef HAVE_GETSPNAM
893 struct spwd *spwd = getspnam(passwd->bv_val);
900 # ifdef HAVE_AIX_SECURITY
902 struct userpw *upw = getuserpw(passwd->bv_val);
905 pw = upw->upw_passwd;
910 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
911 /* password must must be at least two characters long */
912 return LUTIL_PASSWD_ERR;
915 cr = crypt(cred->bv_val, pw);
917 if( cr == NULL || cr[0] == '\0' ) {
918 /* salt must have been invalid */
919 return LUTIL_PASSWD_ERR;
922 return strcmp(pw, cr) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
928 /* PASSWORD GENERATION ROUTINES */
930 #ifdef LUTIL_SHA1_BYTES
931 static int hash_ssha1(
932 const struct berval *scheme,
933 const struct berval *passwd,
937 lutil_SHA1_CTX SHA1context;
938 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
939 char saltdata[SALT_SIZE];
940 struct berval digest;
943 digest.bv_val = (char *) SHA1digest;
944 digest.bv_len = sizeof(SHA1digest);
945 salt.bv_val = saltdata;
946 salt.bv_len = sizeof(saltdata);
948 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
949 return LUTIL_PASSWD_ERR;
952 lutil_SHA1Init( &SHA1context );
953 lutil_SHA1Update( &SHA1context,
954 (const unsigned char *)passwd->bv_val, passwd->bv_len );
955 lutil_SHA1Update( &SHA1context,
956 (const unsigned char *)salt.bv_val, salt.bv_len );
957 lutil_SHA1Final( SHA1digest, &SHA1context );
959 return pw_string64( scheme, &digest, hash, &salt);
962 static int hash_sha1(
963 const struct berval *scheme,
964 const struct berval *passwd,
968 lutil_SHA1_CTX SHA1context;
969 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
970 struct berval digest;
971 digest.bv_val = (char *) SHA1digest;
972 digest.bv_len = sizeof(SHA1digest);
974 lutil_SHA1Init( &SHA1context );
975 lutil_SHA1Update( &SHA1context,
976 (const unsigned char *)passwd->bv_val, passwd->bv_len );
977 lutil_SHA1Final( SHA1digest, &SHA1context );
979 return pw_string64( scheme, &digest, hash, NULL);
983 static int hash_smd5(
984 const struct berval *scheme,
985 const struct berval *passwd,
989 lutil_MD5_CTX MD5context;
990 unsigned char MD5digest[LUTIL_MD5_BYTES];
991 char saltdata[SALT_SIZE];
992 struct berval digest;
995 digest.bv_val = (char *) MD5digest;
996 digest.bv_len = sizeof(MD5digest);
997 salt.bv_val = saltdata;
998 salt.bv_len = sizeof(saltdata);
1000 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1001 return LUTIL_PASSWD_ERR;
1004 lutil_MD5Init( &MD5context );
1005 lutil_MD5Update( &MD5context,
1006 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1007 lutil_MD5Update( &MD5context,
1008 (const unsigned char *) salt.bv_val, salt.bv_len );
1009 lutil_MD5Final( MD5digest, &MD5context );
1011 return pw_string64( scheme, &digest, hash, &salt );
1014 static int hash_md5(
1015 const struct berval *scheme,
1016 const struct berval *passwd,
1017 struct berval *hash,
1020 lutil_MD5_CTX MD5context;
1021 unsigned char MD5digest[LUTIL_MD5_BYTES];
1023 struct berval digest;
1025 digest.bv_val = (char *) MD5digest;
1026 digest.bv_len = sizeof(MD5digest);
1028 lutil_MD5Init( &MD5context );
1029 lutil_MD5Update( &MD5context,
1030 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1031 lutil_MD5Final( MD5digest, &MD5context );
1033 return pw_string64( scheme, &digest, hash, NULL );
1038 static int hash_lanman(
1039 const struct berval *scheme,
1040 const struct berval *passwd,
1041 struct berval *hash,
1046 char UcasePassword[15];
1048 des_key_schedule schedule;
1049 des_cblock StdText = "KGS!@#$%";
1050 des_cblock PasswordHash1, PasswordHash2;
1051 char PasswordHash[33];
1053 for( i=0; i<passwd->bv_len; i++) {
1054 if(passwd->bv_val[i] == '\0') {
1055 return LUTIL_PASSWD_ERR; /* NUL character in password */
1059 if( passwd->bv_val[i] != '\0' ) {
1060 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1063 strncpy( UcasePassword, passwd->bv_val, 14 );
1064 UcasePassword[14] = '\0';
1065 ldap_pvt_str2upper( UcasePassword );
1067 lmPasswd_to_key( UcasePassword, &key );
1068 des_set_key_unchecked( &key, schedule );
1069 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1071 lmPasswd_to_key( &UcasePassword[7], &key );
1072 des_set_key_unchecked( &key, schedule );
1073 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1075 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1076 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1077 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1078 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1079 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1081 hash->bv_val = PasswordHash;
1084 return pw_string( scheme, hash );
1086 #endif /* SLAPD_LMHASH */
1089 static int hash_crypt(
1090 const struct berval *scheme,
1091 const struct berval *passwd,
1092 struct berval *hash,
1095 unsigned char salt[32]; /* salt suitable for most anything */
1098 for( i=0; i<passwd->bv_len; i++) {
1099 if(passwd->bv_val[i] == '\0') {
1100 return LUTIL_PASSWD_ERR; /* NUL character in password */
1104 if( passwd->bv_val[i] != '\0' ) {
1105 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1108 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1109 return LUTIL_PASSWD_ERR;
1112 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1113 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1115 salt[sizeof( salt ) - 1 ] = '\0';
1117 if( salt_format != NULL ) {
1118 /* copy the salt we made into entropy before snprintfing
1119 it back into the salt */
1120 char entropy[sizeof(salt)];
1121 strcpy( entropy, (char *) salt );
1122 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1125 hash->bv_val = crypt( passwd->bv_val, (char *) salt );
1127 if( hash->bv_val == NULL ) return -1;
1129 hash->bv_len = strlen( hash->bv_val );
1131 if( hash->bv_len == 0 ) {
1132 return LUTIL_PASSWD_ERR;
1135 return pw_string( scheme, hash );
1139 int lutil_salt_format(const char *format)
1142 free( salt_format );
1144 salt_format = format != NULL ? strdup( format ) : NULL;
1150 #ifdef SLAPD_CLEARTEXT
1151 static int hash_clear(
1152 const struct berval *scheme,
1153 const struct berval *passwd,
1154 struct berval *hash,
1157 ber_dupbv( hash, (struct berval *)passwd );
1158 return LUTIL_PASSWD_OK;