2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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;
135 static int pw_inited;
137 static const struct pw_scheme pw_schemes_default[] =
139 #ifdef LUTIL_SHA1_BYTES
140 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
141 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
144 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
145 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
148 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
149 #endif /* SLAPD_LMHASH */
152 { BER_BVC("{SASL}"), chk_sasl, NULL },
156 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
157 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
158 { BER_BVC("{UNIX}"), chk_unix, NULL },
162 #ifdef SLAPD_CLEARTEXT
164 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
167 { BER_BVNULL, NULL, NULL }
170 int lutil_passwd_add(
171 struct berval *scheme,
172 LUTIL_PASSWD_CHK_FUNC *chk,
173 LUTIL_PASSWD_HASH_FUNC *hash )
175 struct pw_slist *ptr;
177 if (!pw_inited) lutil_passwd_init();
179 ptr = ber_memalloc( sizeof( struct pw_slist ));
181 ptr->next = pw_schemes;
182 ptr->s.name = *scheme;
184 ptr->s.hash_fn = hash;
189 void lutil_passwd_init()
195 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
196 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn ) ) break;
200 void lutil_passwd_destroy()
202 struct pw_slist *ptr, *next;
204 for( ptr=pw_schemes; ptr; ptr=next ) {
210 static const struct pw_scheme *get_scheme(
213 struct pw_slist *pws;
216 if (!pw_inited) lutil_passwd_init();
218 bv.bv_val = strchr( scheme, '}' );
222 bv.bv_len = bv.bv_val - scheme + 1;
223 bv.bv_val = (char *) scheme;
225 for( pws=pw_schemes; pws; pws=pws->next ) {
226 if( bv.bv_len != pws->s.name.bv_len )
228 if( strncasecmp(bv.bv_val, pws->s.name.bv_val, bv.bv_len ) == 0 ) {
236 int lutil_passwd_scheme(
239 if( scheme == NULL ) {
243 return get_scheme(scheme) != NULL;
247 static int is_allowed_scheme(
249 const char** schemes )
253 if( schemes == NULL ) return 1;
255 for( i=0; schemes[i] != NULL; i++ ) {
256 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
263 static struct berval *passwd_scheme(
264 const struct pw_scheme *scheme,
265 const struct berval * passwd,
267 const char** allowed )
269 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
273 if( passwd->bv_len >= scheme->name.bv_len ) {
274 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
275 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
276 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
286 * Return 0 if creds are good.
290 const struct berval *passwd, /* stored passwd */
291 const struct berval *cred, /* user cred */
292 const char **schemes,
295 struct pw_slist *pws;
297 if ( text ) *text = NULL;
299 if (cred == NULL || cred->bv_len == 0 ||
300 passwd == NULL || passwd->bv_len == 0 )
305 if (!pw_inited) lutil_passwd_init();
307 for( pws=pw_schemes; pws; pws=pws->next ) {
308 if( pws->s.chk_fn ) {
310 struct berval *p = passwd_scheme( &(pws->s),
311 passwd, &x, schemes );
314 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
319 #ifdef SLAPD_CLEARTEXT
320 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
321 return (( passwd->bv_len == cred->bv_len ) &&
322 ( passwd->bv_val[0] != '{' /*'}'*/ ))
323 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
330 int lutil_passwd_generate( struct berval *pw, ber_len_t len )
333 if( len < 1 ) return -1;
336 pw->bv_val = ber_memalloc( len + 1 );
338 if( pw->bv_val == NULL ) {
342 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
346 for( len = 0; len < pw->bv_len; len++ ) {
347 pw->bv_val[len] = crypt64[
348 pw->bv_val[len] % (sizeof(crypt64)-1) ];
351 pw->bv_val[len] = '\0';
356 int lutil_passwd_hash(
357 const struct berval * passwd,
362 const struct pw_scheme *sc = get_scheme( method );
368 if( text ) *text = "scheme not recognized";
372 if( ! sc->hash_fn ) {
373 if( text ) *text = "scheme provided no hash function";
377 if( text ) *text = NULL;
379 return (sc->hash_fn)( &sc->name, passwd, hash, text );
382 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
383 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
384 static int pw_string(
385 const struct berval *sc,
386 struct berval *passwd )
390 pw.bv_len = sc->bv_len + passwd->bv_len;
391 pw.bv_val = ber_memalloc( pw.bv_len + 1 );
393 if( pw.bv_val == NULL ) {
394 return LUTIL_PASSWD_ERR;
397 AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
398 AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
400 pw.bv_val[pw.bv_len] = '\0';
403 return LUTIL_PASSWD_OK;
405 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
407 static int pw_string64(
408 const struct berval *sc,
409 const struct berval *hash,
411 const struct berval *salt )
414 struct berval string;
418 /* need to base64 combined string */
419 string.bv_len = hash->bv_len + salt->bv_len;
420 string.bv_val = ber_memalloc( string.bv_len + 1 );
422 if( string.bv_val == NULL ) {
423 return LUTIL_PASSWD_ERR;
426 AC_MEMCPY( string.bv_val, hash->bv_val,
428 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
430 string.bv_val[string.bv_len] = '\0';
436 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
437 b64->bv_len = b64len + sc->bv_len;
438 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
440 if( b64->bv_val == NULL ) {
441 if( salt ) ber_memfree( string.bv_val );
442 return LUTIL_PASSWD_ERR;
445 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
448 (unsigned char *) string.bv_val, string.bv_len,
449 &b64->bv_val[sc->bv_len], b64len );
451 if( salt ) ber_memfree( string.bv_val );
454 return LUTIL_PASSWD_ERR;
457 /* recompute length */
458 b64->bv_len = sc->bv_len + rc;
459 assert( strlen(b64->bv_val) == b64->bv_len );
460 return LUTIL_PASSWD_OK;
463 /* PASSWORD CHECK ROUTINES */
465 #ifdef LUTIL_SHA1_BYTES
466 static int chk_ssha1(
467 const struct berval *sc,
468 const struct berval * passwd,
469 const struct berval * cred,
472 lutil_SHA1_CTX SHA1context;
473 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
475 unsigned char *orig_pass = NULL;
477 /* safety check -- must have some salt */
478 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
479 return LUTIL_PASSWD_ERR;
482 /* decode base64 password */
483 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
484 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
486 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
488 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
490 /* safety check -- must have some salt */
491 if (rc <= (int)(sizeof(SHA1digest))) {
492 ber_memfree(orig_pass);
493 return LUTIL_PASSWD_ERR;
496 /* hash credentials with salt */
497 lutil_SHA1Init(&SHA1context);
498 lutil_SHA1Update(&SHA1context,
499 (const unsigned char *) cred->bv_val, cred->bv_len);
500 lutil_SHA1Update(&SHA1context,
501 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
502 rc - sizeof(SHA1digest));
503 lutil_SHA1Final(SHA1digest, &SHA1context);
506 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
507 ber_memfree(orig_pass);
508 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
512 const struct berval *sc,
513 const struct berval * passwd,
514 const struct berval * cred,
517 lutil_SHA1_CTX SHA1context;
518 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
520 unsigned char *orig_pass = NULL;
523 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
524 return LUTIL_PASSWD_ERR;
527 /* base64 un-encode password */
528 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
529 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
531 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
533 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
535 if( rc != sizeof(SHA1digest) ) {
536 ber_memfree(orig_pass);
537 return LUTIL_PASSWD_ERR;
540 /* hash credentials with salt */
541 lutil_SHA1Init(&SHA1context);
542 lutil_SHA1Update(&SHA1context,
543 (const unsigned char *) cred->bv_val, cred->bv_len);
544 lutil_SHA1Final(SHA1digest, &SHA1context);
547 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
548 ber_memfree(orig_pass);
549 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
554 const struct berval *sc,
555 const struct berval * passwd,
556 const struct berval * cred,
559 lutil_MD5_CTX MD5context;
560 unsigned char MD5digest[LUTIL_MD5_BYTES];
562 unsigned char *orig_pass = NULL;
565 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
566 return LUTIL_PASSWD_ERR;
569 /* base64 un-encode password */
570 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
571 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
573 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
575 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
577 if (rc <= (int)(sizeof(MD5digest))) {
578 ber_memfree(orig_pass);
579 return LUTIL_PASSWD_ERR;
582 /* hash credentials with salt */
583 lutil_MD5Init(&MD5context);
584 lutil_MD5Update(&MD5context,
585 (const unsigned char *) cred->bv_val,
587 lutil_MD5Update(&MD5context,
588 &orig_pass[sizeof(MD5digest)],
589 rc - sizeof(MD5digest));
590 lutil_MD5Final(MD5digest, &MD5context);
593 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
594 ber_memfree(orig_pass);
595 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
599 const struct berval *sc,
600 const struct berval * passwd,
601 const struct berval * cred,
604 lutil_MD5_CTX MD5context;
605 unsigned char MD5digest[LUTIL_MD5_BYTES];
607 unsigned char *orig_pass = NULL;
610 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
611 return LUTIL_PASSWD_ERR;
614 /* base64 un-encode password */
615 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
616 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
618 if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
620 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
621 if ( rc != sizeof(MD5digest) ) {
622 ber_memfree(orig_pass);
623 return LUTIL_PASSWD_ERR;
626 /* hash credentials with salt */
627 lutil_MD5Init(&MD5context);
628 lutil_MD5Update(&MD5context,
629 (const unsigned char *) cred->bv_val,
631 lutil_MD5Final(MD5digest, &MD5context);
634 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
635 ber_memfree(orig_pass);
636 return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
640 /* pseudocode from RFC2433
641 * A.2 LmPasswordHash()
644 * IN 0-to-14-oem-char Password,
645 * OUT 16-octet PasswordHash )
647 * Set UcasePassword to the uppercased Password
648 * Zero pad UcasePassword to 14 characters
650 * DesHash( 1st 7-octets of UcasePassword,
651 * giving 1st 8-octets of PasswordHash )
653 * DesHash( 2nd 7-octets of UcasePassword,
654 * giving 2nd 8-octets of PasswordHash )
662 * OUT 8-octet Cypher )
665 * * Make Cypher an irreversibly encrypted form of Clear by
666 * * encrypting known text using Clear as the secret key.
667 * * The known text consists of the string
672 * Set StdText to "KGS!@#$%"
673 * DesEncrypt( StdText, Clear, giving Cypher )
682 * OUT 8-octet Cypher )
685 * * Use the DES encryption algorithm [4] in ECB mode [9]
686 * * to encrypt Clear into Cypher such that Cypher can
687 * * only be decrypted back to Clear by providing Key.
688 * * Note that the DES algorithm takes as input a 64-bit
689 * * stream where the 8th, 16th, 24th, etc. bits are
690 * * parity bits ignored by the encrypting algorithm.
691 * * Unless you write your own DES to accept 56-bit input
692 * * without parity, you will need to insert the parity bits
698 static void lmPasswd_to_key(
699 const char *lmPasswd,
702 const unsigned char *lpw = (const unsigned char *) lmPasswd;
703 unsigned char *k = (unsigned char *) key;
705 /* make room for parity bits */
707 k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
708 k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
709 k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
710 k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
711 k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
712 k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
713 k[7] = ((lpw[6] & 0x7F) << 1);
715 des_set_odd_parity( key );
718 static int chk_lanman(
719 const struct berval *scheme,
720 const struct berval *passwd,
721 const struct berval *cred,
725 char UcasePassword[15];
727 des_key_schedule schedule;
728 des_cblock StdText = "KGS!@#$%";
729 des_cblock PasswordHash1, PasswordHash2;
730 char PasswordHash[33], storedPasswordHash[33];
732 for( i=0; i<cred->bv_len; i++) {
733 if(cred->bv_val[i] == '\0') {
734 return LUTIL_PASSWD_ERR; /* NUL character in password */
738 if( cred->bv_val[i] != '\0' ) {
739 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
742 strncpy( UcasePassword, cred->bv_val, 14 );
743 UcasePassword[14] = '\0';
744 ldap_pvt_str2upper( UcasePassword );
746 lmPasswd_to_key( UcasePassword, &key );
747 des_set_key_unchecked( &key, schedule );
748 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
750 lmPasswd_to_key( &UcasePassword[7], &key );
751 des_set_key_unchecked( &key, schedule );
752 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
754 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
755 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
756 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
757 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
758 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
760 /* as a precaution convert stored password hash to lower case */
761 strncpy( storedPasswordHash, passwd->bv_val, 32 );
762 storedPasswordHash[32] = '\0';
763 ldap_pvt_str2lower( storedPasswordHash );
765 return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
767 #endif /* SLAPD_LMHASH */
770 #ifdef HAVE_CYRUS_SASL
771 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
775 const struct berval *sc,
776 const struct berval * passwd,
777 const struct berval * cred,
783 for( i=0; i<cred->bv_len; i++) {
784 if(cred->bv_val[i] == '\0') {
785 return LUTIL_PASSWD_ERR; /* NUL character in password */
789 if( cred->bv_val[i] != '\0' ) {
790 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
793 for( i=0; i<passwd->bv_len; i++) {
794 if(passwd->bv_val[i] == '\0') {
795 return LUTIL_PASSWD_ERR; /* NUL character in password */
799 if( passwd->bv_val[i] != '\0' ) {
800 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
803 rtn = LUTIL_PASSWD_ERR;
805 #ifdef HAVE_CYRUS_SASL
806 if( lutil_passwd_sasl_conn != NULL ) {
808 # if SASL_VERSION_MAJOR < 2
809 sc = sasl_checkpass( lutil_passwd_sasl_conn,
810 passwd->bv_val, passwd->bv_len,
811 cred->bv_val, cred->bv_len,
814 sc = sasl_checkpass( lutil_passwd_sasl_conn,
815 passwd->bv_val, passwd->bv_len,
816 cred->bv_val, cred->bv_len );
818 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
827 static int chk_crypt(
828 const struct berval *sc,
829 const struct berval * passwd,
830 const struct berval * cred,
836 for( i=0; i<cred->bv_len; i++) {
837 if(cred->bv_val[i] == '\0') {
838 return LUTIL_PASSWD_ERR; /* NUL character in password */
842 if( cred->bv_val[i] != '\0' ) {
843 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
846 if( passwd->bv_len < 2 ) {
847 return LUTIL_PASSWD_ERR; /* passwd must be at least two characters long */
850 for( i=0; i<passwd->bv_len; i++) {
851 if(passwd->bv_val[i] == '\0') {
852 return LUTIL_PASSWD_ERR; /* NUL character in password */
856 if( passwd->bv_val[i] != '\0' ) {
857 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
860 cr = crypt( cred->bv_val, passwd->bv_val );
862 if( cr == NULL || cr[0] == '\0' ) {
863 /* salt must have been invalid */
864 return LUTIL_PASSWD_ERR;
867 return strcmp( passwd->bv_val, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
870 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
872 const struct berval *sc,
873 const struct berval * passwd,
874 const struct berval * cred,
880 for( i=0; i<cred->bv_len; i++) {
881 if(cred->bv_val[i] == '\0') {
882 return LUTIL_PASSWD_ERR; /* NUL character in password */
885 if( cred->bv_val[i] != '\0' ) {
886 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
889 for( i=0; i<passwd->bv_len; i++) {
890 if(passwd->bv_val[i] == '\0') {
891 return LUTIL_PASSWD_ERR; /* NUL character in password */
895 if( passwd->bv_val[i] != '\0' ) {
896 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
900 struct passwd *pwd = getpwnam(passwd->bv_val);
903 return LUTIL_PASSWD_ERR; /* not found */
908 # ifdef HAVE_GETSPNAM
910 struct spwd *spwd = getspnam(passwd->bv_val);
917 # ifdef HAVE_AIX_SECURITY
919 struct userpw *upw = getuserpw(passwd->bv_val);
922 pw = upw->upw_passwd;
927 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
928 /* password must must be at least two characters long */
929 return LUTIL_PASSWD_ERR;
932 cr = crypt(cred->bv_val, pw);
934 if( cr == NULL || cr[0] == '\0' ) {
935 /* salt must have been invalid */
936 return LUTIL_PASSWD_ERR;
939 return strcmp(pw, cr) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
945 /* PASSWORD GENERATION ROUTINES */
947 #ifdef LUTIL_SHA1_BYTES
948 static int hash_ssha1(
949 const struct berval *scheme,
950 const struct berval *passwd,
954 lutil_SHA1_CTX SHA1context;
955 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
956 char saltdata[SALT_SIZE];
957 struct berval digest;
960 digest.bv_val = (char *) SHA1digest;
961 digest.bv_len = sizeof(SHA1digest);
962 salt.bv_val = saltdata;
963 salt.bv_len = sizeof(saltdata);
965 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
966 return LUTIL_PASSWD_ERR;
969 lutil_SHA1Init( &SHA1context );
970 lutil_SHA1Update( &SHA1context,
971 (const unsigned char *)passwd->bv_val, passwd->bv_len );
972 lutil_SHA1Update( &SHA1context,
973 (const unsigned char *)salt.bv_val, salt.bv_len );
974 lutil_SHA1Final( SHA1digest, &SHA1context );
976 return pw_string64( scheme, &digest, hash, &salt);
979 static int hash_sha1(
980 const struct berval *scheme,
981 const struct berval *passwd,
985 lutil_SHA1_CTX SHA1context;
986 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
987 struct berval digest;
988 digest.bv_val = (char *) SHA1digest;
989 digest.bv_len = sizeof(SHA1digest);
991 lutil_SHA1Init( &SHA1context );
992 lutil_SHA1Update( &SHA1context,
993 (const unsigned char *)passwd->bv_val, passwd->bv_len );
994 lutil_SHA1Final( SHA1digest, &SHA1context );
996 return pw_string64( scheme, &digest, hash, NULL);
1000 static int hash_smd5(
1001 const struct berval *scheme,
1002 const struct berval *passwd,
1003 struct berval *hash,
1006 lutil_MD5_CTX MD5context;
1007 unsigned char MD5digest[LUTIL_MD5_BYTES];
1008 char saltdata[SALT_SIZE];
1009 struct berval digest;
1012 digest.bv_val = (char *) MD5digest;
1013 digest.bv_len = sizeof(MD5digest);
1014 salt.bv_val = saltdata;
1015 salt.bv_len = sizeof(saltdata);
1017 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1018 return LUTIL_PASSWD_ERR;
1021 lutil_MD5Init( &MD5context );
1022 lutil_MD5Update( &MD5context,
1023 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1024 lutil_MD5Update( &MD5context,
1025 (const unsigned char *) salt.bv_val, salt.bv_len );
1026 lutil_MD5Final( MD5digest, &MD5context );
1028 return pw_string64( scheme, &digest, hash, &salt );
1031 static int hash_md5(
1032 const struct berval *scheme,
1033 const struct berval *passwd,
1034 struct berval *hash,
1037 lutil_MD5_CTX MD5context;
1038 unsigned char MD5digest[LUTIL_MD5_BYTES];
1040 struct berval digest;
1042 digest.bv_val = (char *) MD5digest;
1043 digest.bv_len = sizeof(MD5digest);
1045 lutil_MD5Init( &MD5context );
1046 lutil_MD5Update( &MD5context,
1047 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1048 lutil_MD5Final( MD5digest, &MD5context );
1050 return pw_string64( scheme, &digest, hash, NULL );
1055 static int hash_lanman(
1056 const struct berval *scheme,
1057 const struct berval *passwd,
1058 struct berval *hash,
1063 char UcasePassword[15];
1065 des_key_schedule schedule;
1066 des_cblock StdText = "KGS!@#$%";
1067 des_cblock PasswordHash1, PasswordHash2;
1068 char PasswordHash[33];
1070 for( i=0; i<passwd->bv_len; i++) {
1071 if(passwd->bv_val[i] == '\0') {
1072 return LUTIL_PASSWD_ERR; /* NUL character in password */
1076 if( passwd->bv_val[i] != '\0' ) {
1077 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1080 strncpy( UcasePassword, passwd->bv_val, 14 );
1081 UcasePassword[14] = '\0';
1082 ldap_pvt_str2upper( UcasePassword );
1084 lmPasswd_to_key( UcasePassword, &key );
1085 des_set_key_unchecked( &key, schedule );
1086 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1088 lmPasswd_to_key( &UcasePassword[7], &key );
1089 des_set_key_unchecked( &key, schedule );
1090 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1092 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1093 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1094 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1095 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1096 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1098 hash->bv_val = PasswordHash;
1101 return pw_string( scheme, hash );
1103 #endif /* SLAPD_LMHASH */
1106 static int hash_crypt(
1107 const struct berval *scheme,
1108 const struct berval *passwd,
1109 struct berval *hash,
1112 unsigned char salt[32]; /* salt suitable for most anything */
1115 for( i=0; i<passwd->bv_len; i++) {
1116 if(passwd->bv_val[i] == '\0') {
1117 return LUTIL_PASSWD_ERR; /* NUL character in password */
1121 if( passwd->bv_val[i] != '\0' ) {
1122 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
1125 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1126 return LUTIL_PASSWD_ERR;
1129 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1130 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1132 salt[sizeof( salt ) - 1 ] = '\0';
1134 if( salt_format != NULL ) {
1135 /* copy the salt we made into entropy before snprintfing
1136 it back into the salt */
1137 char entropy[sizeof(salt)];
1138 strcpy( entropy, (char *) salt );
1139 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1142 hash->bv_val = crypt( passwd->bv_val, (char *) salt );
1144 if( hash->bv_val == NULL ) return -1;
1146 hash->bv_len = strlen( hash->bv_val );
1148 if( hash->bv_len == 0 ) {
1149 return LUTIL_PASSWD_ERR;
1152 return pw_string( scheme, hash );
1156 int lutil_salt_format(const char *format)
1159 free( salt_format );
1161 salt_format = format != NULL ? strdup( format ) : NULL;
1167 #ifdef SLAPD_CLEARTEXT
1168 static int hash_clear(
1169 const struct berval *scheme,
1170 const struct berval *passwd,
1171 struct berval *hash,
1174 ber_dupbv( hash, (struct berval *)passwd );
1175 return LUTIL_PASSWD_OK;