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>
49 /* KPASSWD/krb.h brings in a conflicting des.h so don't use both.
50 * configure currently requires OpenSSL to enable LMHASH. Obviously
51 * this requirement can be fulfilled by the KRB DES library as well.
53 #if defined(SLAPD_LMHASH) && !defined(DES_ENCRYPT)
54 # include <openssl/des.h>
55 #endif /* SLAPD_LMHASH */
60 # include <ac/crypt.h>
62 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
69 # ifdef HAVE_AIX_SECURITY
80 #include "lutil_md5.h"
81 #include "lutil_sha1.h"
84 static const unsigned char crypt64[] =
85 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
88 static char *salt_format = NULL;
93 LUTIL_PASSWD_CHK_FUNC *chk_fn;
94 LUTIL_PASSWD_HASH_FUNC *hash_fn;
98 struct pw_slist *next;
102 /* password check routines */
106 static LUTIL_PASSWD_CHK_FUNC chk_md5;
107 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
108 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
109 static LUTIL_PASSWD_HASH_FUNC hash_md5;
112 #ifdef LUTIL_SHA1_BYTES
113 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
114 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
115 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
116 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
120 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
121 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
124 #ifdef SLAPD_NS_MTA_MD5
125 static LUTIL_PASSWD_CHK_FUNC chk_ns_mta_md5;
129 static LUTIL_PASSWD_CHK_FUNC chk_sasl;
133 static LUTIL_PASSWD_CHK_FUNC chk_kerberos;
137 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
138 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
140 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
141 static LUTIL_PASSWD_CHK_FUNC chk_unix;
145 /* password hash routines */
147 #ifdef SLAPD_CLEARTEXT
148 static LUTIL_PASSWD_HASH_FUNC hash_clear;
151 static struct pw_slist *pw_schemes;
153 static const struct pw_scheme pw_schemes_default[] =
155 #ifdef LUTIL_SHA1_BYTES
156 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
157 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
160 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
161 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
164 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
165 #endif /* SLAPD_LMHASH */
167 #ifdef SLAPD_NS_MTA_MD5
168 { BER_BVC("{NS-MTA-MD5}"), chk_ns_mta_md5, NULL },
169 #endif /* SLAPD_NS_MTA_MD5 */
172 { BER_BVC("{SASL}"), chk_sasl, NULL },
176 { BER_BVC("{KERBEROS}"), chk_kerberos, NULL },
180 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
181 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
182 { BER_BVC("{UNIX}"), chk_unix, NULL },
186 #ifdef SLAPD_CLEARTEXT
188 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
191 { BER_BVNULL, NULL, NULL }
194 int lutil_passwd_add(
195 struct berval *scheme,
196 LUTIL_PASSWD_CHK_FUNC *chk,
197 LUTIL_PASSWD_HASH_FUNC *hash )
199 struct pw_slist *ptr;
201 ptr = ber_memalloc( sizeof( struct pw_slist ));
203 ptr->next = pw_schemes;
204 ptr->s.name = *scheme;
206 ptr->s.hash_fn = hash;
211 void lutil_passwd_init()
213 struct pw_slist *ptr;
216 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
217 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn )) break;
221 void lutil_passwd_destroy()
223 struct pw_slist *ptr, *next;
225 for( ptr=pw_schemes; ptr; ptr=next ) {
231 static const struct pw_scheme *get_scheme(
234 struct pw_slist *pws;
236 if (!pw_schemes) lutil_passwd_init();
238 for( pws=pw_schemes; pws; pws=pws->next ) {
239 if( strcasecmp(scheme, pws->s.name.bv_val ) == 0 ) {
247 int lutil_passwd_scheme(
250 if( scheme == NULL ) {
254 return get_scheme(scheme) != NULL;
258 static int is_allowed_scheme(
260 const char** schemes )
264 if( schemes == NULL ) return 1;
266 for( i=0; schemes[i] != NULL; i++ ) {
267 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
274 static struct berval *passwd_scheme(
275 const struct pw_scheme *scheme,
276 const struct berval * passwd,
278 const char** allowed )
280 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
284 if( passwd->bv_len >= scheme->name.bv_len ) {
285 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
286 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
287 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
297 * Return 0 if creds are good.
301 const struct berval *passwd, /* stored passwd */
302 const struct berval *cred, /* user cred */
303 const char **schemes,
306 struct pw_slist *pws;
308 if ( text ) *text = NULL;
310 if (cred == NULL || cred->bv_len == 0 ||
311 passwd == NULL || passwd->bv_len == 0 )
316 if (!pw_schemes) lutil_passwd_init();
318 for( pws=pw_schemes; pws; pws=pws->next ) {
319 if( pws->s.chk_fn ) {
321 struct berval *p = passwd_scheme( &(pws->s),
322 passwd, &x, schemes );
325 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
330 #ifdef SLAPD_CLEARTEXT
331 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
332 return (( passwd->bv_len == cred->bv_len ) &&
333 ( passwd->bv_val[0] != '{' /*'}'*/ ))
334 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
341 struct berval * lutil_passwd_generate( ber_len_t len )
345 if( len < 1 ) return NULL;
347 pw = ber_memalloc( sizeof( struct berval ) );
348 if( pw == NULL ) return NULL;
351 pw->bv_val = ber_memalloc( len + 1 );
353 if( pw->bv_val == NULL ) {
358 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
363 for( len = 0; len < pw->bv_len; len++ ) {
364 pw->bv_val[len] = crypt64[
365 pw->bv_val[len] % (sizeof(crypt64)-1) ];
368 pw->bv_val[len] = '\0';
373 struct berval * lutil_passwd_hash(
374 const struct berval * passwd,
378 const struct pw_scheme *sc = get_scheme( method );
380 if( text ) *text = NULL;
381 if( sc == NULL ) return NULL;
382 if( ! sc->hash_fn ) return NULL;
384 return (sc->hash_fn)( &sc->name, passwd, text );
387 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
388 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
389 static struct berval * pw_string(
390 const struct berval *sc,
391 const struct berval *passwd )
393 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
394 if( pw == NULL ) return NULL;
396 pw->bv_len = sc->bv_len + passwd->bv_len;
397 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
399 if( pw->bv_val == NULL ) {
404 AC_MEMCPY( pw->bv_val, sc->bv_val, sc->bv_len );
405 AC_MEMCPY( &pw->bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
407 pw->bv_val[pw->bv_len] = '\0';
410 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
412 static struct berval * pw_string64(
413 const struct berval *sc,
414 const struct berval *hash,
415 const struct berval *salt )
418 struct berval string;
419 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
422 if( b64 == NULL ) return NULL;
425 /* need to base64 combined string */
426 string.bv_len = hash->bv_len + salt->bv_len;
427 string.bv_val = ber_memalloc( string.bv_len + 1 );
429 if( string.bv_val == NULL ) {
434 AC_MEMCPY( string.bv_val, hash->bv_val,
436 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
438 string.bv_val[string.bv_len] = '\0';
444 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
445 b64->bv_len = b64len + sc->bv_len;
446 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
448 if( b64->bv_val == NULL ) {
449 if( salt ) ber_memfree( string.bv_val );
454 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
457 (unsigned char *) string.bv_val, string.bv_len,
458 &b64->bv_val[sc->bv_len], b64len );
460 if( salt ) ber_memfree( string.bv_val );
467 /* recompute length */
468 b64->bv_len = sc->bv_len + rc;
469 assert( strlen(b64->bv_val) == b64->bv_len );
473 /* PASSWORD CHECK ROUTINES */
475 #ifdef LUTIL_SHA1_BYTES
476 static int chk_ssha1(
477 const struct berval *sc,
478 const struct berval * passwd,
479 const struct berval * cred,
482 lutil_SHA1_CTX SHA1context;
483 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
485 unsigned char *orig_pass = NULL;
488 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
489 sizeof(SHA1digest)+SALT_SIZE) {
493 /* decode base64 password */
494 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
495 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
497 if( orig_pass == NULL ) return -1;
499 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
501 if (rc < (int)(sizeof(SHA1digest)+SALT_SIZE)) {
502 ber_memfree(orig_pass);
506 /* hash credentials with salt */
507 lutil_SHA1Init(&SHA1context);
508 lutil_SHA1Update(&SHA1context,
509 (const unsigned char *) cred->bv_val, cred->bv_len);
510 lutil_SHA1Update(&SHA1context,
511 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
512 rc - sizeof(SHA1digest));
513 lutil_SHA1Final(SHA1digest, &SHA1context);
516 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
517 ber_memfree(orig_pass);
522 const struct berval *sc,
523 const struct berval * passwd,
524 const struct berval * cred,
527 lutil_SHA1_CTX SHA1context;
528 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
530 unsigned char *orig_pass = NULL;
533 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
537 /* base64 un-encode password */
538 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
539 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
541 if( orig_pass == NULL ) return -1;
543 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
545 if( rc != sizeof(SHA1digest) ) {
546 ber_memfree(orig_pass);
550 /* hash credentials with salt */
551 lutil_SHA1Init(&SHA1context);
552 lutil_SHA1Update(&SHA1context,
553 (const unsigned char *) cred->bv_val, cred->bv_len);
554 lutil_SHA1Final(SHA1digest, &SHA1context);
557 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
558 ber_memfree(orig_pass);
564 const struct berval *sc,
565 const struct berval * passwd,
566 const struct berval * cred,
569 lutil_MD5_CTX MD5context;
570 unsigned char MD5digest[LUTIL_MD5_BYTES];
572 unsigned char *orig_pass = NULL;
575 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
576 sizeof(MD5digest)+SALT_SIZE) {
580 /* base64 un-encode password */
581 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
582 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
584 if( orig_pass == NULL ) return -1;
586 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
588 if (rc < (int)(sizeof(MD5digest)+SALT_SIZE)) {
589 ber_memfree(orig_pass);
593 /* hash credentials with salt */
594 lutil_MD5Init(&MD5context);
595 lutil_MD5Update(&MD5context,
596 (const unsigned char *) cred->bv_val,
598 lutil_MD5Update(&MD5context,
599 &orig_pass[sizeof(MD5digest)],
600 rc - sizeof(MD5digest));
601 lutil_MD5Final(MD5digest, &MD5context);
604 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
605 ber_memfree(orig_pass);
610 const struct berval *sc,
611 const struct berval * passwd,
612 const struct berval * cred,
615 lutil_MD5_CTX MD5context;
616 unsigned char MD5digest[LUTIL_MD5_BYTES];
618 unsigned char *orig_pass = NULL;
621 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
625 /* base64 un-encode password */
626 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
627 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
629 if( orig_pass == NULL ) return -1;
631 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
632 if ( rc != sizeof(MD5digest) ) {
633 ber_memfree(orig_pass);
637 /* hash credentials with salt */
638 lutil_MD5Init(&MD5context);
639 lutil_MD5Update(&MD5context,
640 (const unsigned char *) cred->bv_val,
642 lutil_MD5Final(MD5digest, &MD5context);
645 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
646 ber_memfree(orig_pass);
651 /* pseudocode from RFC2433
652 * A.2 LmPasswordHash()
655 * IN 0-to-14-oem-char Password,
656 * OUT 16-octet PasswordHash )
658 * Set UcasePassword to the uppercased Password
659 * Zero pad UcasePassword to 14 characters
661 * DesHash( 1st 7-octets of UcasePassword,
662 * giving 1st 8-octets of PasswordHash )
664 * DesHash( 2nd 7-octets of UcasePassword,
665 * giving 2nd 8-octets of PasswordHash )
673 * OUT 8-octet Cypher )
676 * * Make Cypher an irreversibly encrypted form of Clear by
677 * * encrypting known text using Clear as the secret key.
678 * * The known text consists of the string
683 * Set StdText to "KGS!@#$%"
684 * DesEncrypt( StdText, Clear, giving Cypher )
693 * OUT 8-octet Cypher )
696 * * Use the DES encryption algorithm [4] in ECB mode [9]
697 * * to encrypt Clear into Cypher such that Cypher can
698 * * only be decrypted back to Clear by providing Key.
699 * * Note that the DES algorithm takes as input a 64-bit
700 * * stream where the 8th, 16th, 24th, etc. bits are
701 * * parity bits ignored by the encrypting algorithm.
702 * * Unless you write your own DES to accept 56-bit input
703 * * without parity, you will need to insert the parity bits
709 static void lmPasswd_to_key(
710 const unsigned char *lmPasswd,
713 /* make room for parity bits */
714 ((char *)key)[0] = lmPasswd[0];
715 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
716 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
717 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
718 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
719 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
720 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
721 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
723 des_set_odd_parity( key );
726 static int chk_lanman(
727 const struct berval *scheme,
728 const struct berval *passwd,
729 const struct berval *cred,
733 char UcasePassword[15];
735 des_key_schedule schedule;
736 des_cblock StdText = "KGS!@#$%";
737 des_cblock PasswordHash1, PasswordHash2;
738 char PasswordHash[33], storedPasswordHash[33];
740 for( i=0; i<cred->bv_len; i++) {
741 if(cred->bv_val[i] == '\0') {
742 return -1; /* NUL character in password */
746 if( cred->bv_val[i] != '\0' ) {
747 return -1; /* passwd must behave like a string */
750 strncpy( UcasePassword, cred->bv_val, 14 );
751 UcasePassword[14] = '\0';
752 ldap_pvt_str2upper( UcasePassword );
754 lmPasswd_to_key( UcasePassword, &key );
755 des_set_key_unchecked( &key, schedule );
756 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
758 lmPasswd_to_key( &UcasePassword[7], &key );
759 des_set_key_unchecked( &key, schedule );
760 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
762 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
763 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
764 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
765 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
766 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
768 /* as a precaution convert stored password hash to lower case */
769 strncpy( storedPasswordHash, passwd->bv_val, 32 );
770 storedPasswordHash[32] = '\0';
771 ldap_pvt_str2lower( storedPasswordHash );
773 return memcmp( PasswordHash, storedPasswordHash, 32) ? 1 : 0;
775 #endif /* SLAPD_LMHASH */
777 #ifdef SLAPD_NS_MTA_MD5
778 static int chk_ns_mta_md5(
779 const struct berval *scheme,
780 const struct berval *passwd,
781 const struct berval *cred,
784 lutil_MD5_CTX MD5context;
785 unsigned char MD5digest[LUTIL_MD5_BYTES], c;
786 char buffer[LUTIL_MD5_BYTES*2];
789 if( passwd->bv_len != LUTIL_MD5_BYTES*2 ) {
793 /* hash credentials with salt */
794 lutil_MD5Init(&MD5context);
795 lutil_MD5Update(&MD5context,
796 (const unsigned char *) &passwd->bv_val[32],
800 lutil_MD5Update(&MD5context,
801 (const unsigned char *) &c,
804 lutil_MD5Update(&MD5context,
805 (const unsigned char *) cred->bv_val,
809 lutil_MD5Update(&MD5context,
810 (const unsigned char *) &c,
813 lutil_MD5Update(&MD5context,
814 (const unsigned char *) &passwd->bv_val[32],
817 lutil_MD5Final(MD5digest, &MD5context);
819 for( i=0; i < sizeof( MD5digest ); i++ ) {
820 buffer[i+i] = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F];
821 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F];
825 return memcmp((char *)passwd->bv_val,
826 (char *)buffer, sizeof(buffer)) ? 1 : 0;
831 #ifdef HAVE_CYRUS_SASL
832 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
836 const struct berval *sc,
837 const struct berval * passwd,
838 const struct berval * cred,
844 for( i=0; i<cred->bv_len; i++) {
845 if(cred->bv_val[i] == '\0') {
846 return 1; /* NUL character in password */
850 if( cred->bv_val[i] != '\0' ) {
851 return 1; /* cred must behave like a string */
854 for( i=0; i<passwd->bv_len; i++) {
855 if(passwd->bv_val[i] == '\0') {
856 return 1; /* NUL character in password */
860 if( passwd->bv_val[i] != '\0' ) {
861 return 1; /* passwd must behave like a string */
866 #ifdef HAVE_CYRUS_SASL
867 if( lutil_passwd_sasl_conn != NULL ) {
869 # if SASL_VERSION_MAJOR < 2
870 sc = sasl_checkpass( lutil_passwd_sasl_conn,
871 passwd->bv_val, passwd->bv_len,
872 cred->bv_val, cred->bv_len,
875 sc = sasl_checkpass( lutil_passwd_sasl_conn,
876 passwd->bv_val, passwd->bv_len,
877 cred->bv_val, cred->bv_len );
879 rtn = ( sc != SASL_OK );
888 static int chk_kerberos(
889 const struct berval *sc,
890 const struct berval * passwd,
891 const struct berval * cred,
897 for( i=0; i<cred->bv_len; i++) {
898 if(cred->bv_val[i] == '\0') {
899 return 1; /* NUL character in password */
903 if( cred->bv_val[i] != '\0' ) {
904 return 1; /* cred must behave like a string */
907 for( i=0; i<passwd->bv_len; i++) {
908 if(passwd->bv_val[i] == '\0') {
909 return 1; /* NUL character in password */
913 if( passwd->bv_val[i] != '\0' ) {
914 return 1; /* passwd must behave like a string */
919 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
922 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
923 * (Royal Institute of Technology, Stockholm, Sweden).
924 * All rights reserved.
926 * Redistribution and use in source and binary forms, with or without
927 * modification, are permitted provided that the following conditions
930 * 1. Redistributions of source code must retain the above copyright
931 * notice, this list of conditions and the following disclaimer.
933 * 2. Redistributions in binary form must reproduce the above copyright
934 * notice, this list of conditions and the following disclaimer in the
935 * documentation and/or other materials provided with the distribution.
937 * 3. Neither the name of the Institute nor the names of its contributors
938 * may be used to endorse or promote products derived from this software
939 * without specific prior written permission.
941 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
942 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
943 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
944 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
945 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
946 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
947 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
949 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
950 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
954 krb5_context context;
957 krb5_get_init_creds_opt get_options;
958 krb5_verify_init_creds_opt verify_options;
959 krb5_principal client, server;
961 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
964 ret = krb5_init_context( &context );
970 krb5_get_init_creds_opt_set_preauth_list(&get_options,
974 krb5_get_init_creds_opt_init( &get_options );
976 krb5_verify_init_creds_opt_init( &verify_options );
978 ret = krb5_parse_name( context, passwd->bv_val, &client );
981 krb5_free_context( context );
985 ret = krb5_get_init_creds_password( context,
986 &creds, client, cred->bv_val, NULL,
987 NULL, 0, NULL, &get_options );
990 krb5_free_principal( context, client );
991 krb5_free_context( context );
996 char *host = ldap_pvt_get_fqdn( NULL );
999 krb5_free_principal( context, client );
1000 krb5_free_context( context );
1004 ret = krb5_sname_to_principal( context,
1005 host, "ldap", KRB5_NT_SRV_HST, &server );
1007 ber_memfree( host );
1011 krb5_free_principal( context, client );
1012 krb5_free_context( context );
1016 ret = krb5_verify_init_creds( context,
1017 &creds, server, NULL, NULL, &verify_options );
1019 krb5_free_principal( context, client );
1020 krb5_free_principal( context, server );
1021 krb5_free_cred_contents( context, &creds );
1022 krb5_free_context( context );
1026 #elif defined(HAVE_KRB4)
1028 /* Borrowed from Heimdal kpopper */
1030 * Copyright (c) 1989 Regents of the University of California.
1031 * All rights reserved. The Berkeley software License Agreement
1032 * specifies the terms and conditions for redistribution.
1036 char lrealm[REALM_SZ];
1037 char tkt[MAXHOSTNAMELEN];
1039 status = krb_get_lrealm(lrealm,1);
1040 if (status == KFAILURE) {
1044 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
1045 TKT_ROOT, (unsigned)getpid());
1046 krb_set_tkt_string (tkt);
1048 status = krb_verify_user( passwd->bv_val, "", lrealm,
1049 cred->bv_val, 1, "ldap");
1051 dest_tkt(); /* no point in keeping the tickets */
1053 return status == KFAILURE;
1059 #endif /* SLAPD_KPASSWD */
1062 static int chk_crypt(
1063 const struct berval *sc,
1064 const struct berval * passwd,
1065 const struct berval * cred,
1071 for( i=0; i<cred->bv_len; i++) {
1072 if(cred->bv_val[i] == '\0') {
1073 return 1; /* NUL character in password */
1077 if( cred->bv_val[i] != '\0' ) {
1078 return -1; /* cred must behave like a string */
1081 if( passwd->bv_len < 2 ) {
1082 return -1; /* passwd must be at least two characters long */
1085 for( i=0; i<passwd->bv_len; i++) {
1086 if(passwd->bv_val[i] == '\0') {
1087 return -1; /* NUL character in password */
1091 if( passwd->bv_val[i] != '\0' ) {
1092 return -1; /* passwd must behave like a string */
1095 cr = crypt( cred->bv_val, passwd->bv_val );
1097 if( cr == NULL || cr[0] == '\0' ) {
1098 /* salt must have been invalid */
1102 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
1105 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
1106 static int chk_unix(
1107 const struct berval *sc,
1108 const struct berval * passwd,
1109 const struct berval * cred,
1115 for( i=0; i<cred->bv_len; i++) {
1116 if(cred->bv_val[i] == '\0') {
1117 return -1; /* NUL character in password */
1120 if( cred->bv_val[i] != '\0' ) {
1121 return -1; /* cred must behave like a string */
1124 for( i=0; i<passwd->bv_len; i++) {
1125 if(passwd->bv_val[i] == '\0') {
1126 return -1; /* NUL character in password */
1130 if( passwd->bv_val[i] != '\0' ) {
1131 return -1; /* passwd must behave like a string */
1135 struct passwd *pwd = getpwnam(passwd->bv_val);
1138 return -1; /* not found */
1141 pw = pwd->pw_passwd;
1143 # ifdef HAVE_GETSPNAM
1145 struct spwd *spwd = getspnam(passwd->bv_val);
1152 # ifdef HAVE_AIX_SECURITY
1154 struct userpw *upw = getuserpw(passwd->bv_val);
1157 pw = upw->upw_passwd;
1162 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1163 /* password must must be at least two characters long */
1167 cr = crypt(cred->bv_val, pw);
1169 if( cr == NULL || cr[0] == '\0' ) {
1170 /* salt must have been invalid */
1174 return strcmp(pw, cr) ? 1 : 0;
1180 /* PASSWORD GENERATION ROUTINES */
1182 #ifdef LUTIL_SHA1_BYTES
1183 static struct berval *hash_ssha1(
1184 const struct berval *scheme,
1185 const struct berval *passwd,
1188 lutil_SHA1_CTX SHA1context;
1189 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1190 char saltdata[SALT_SIZE];
1191 struct berval digest;
1194 digest.bv_val = (char *) SHA1digest;
1195 digest.bv_len = sizeof(SHA1digest);
1196 salt.bv_val = saltdata;
1197 salt.bv_len = sizeof(saltdata);
1199 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1203 lutil_SHA1Init( &SHA1context );
1204 lutil_SHA1Update( &SHA1context,
1205 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1206 lutil_SHA1Update( &SHA1context,
1207 (const unsigned char *)salt.bv_val, salt.bv_len );
1208 lutil_SHA1Final( SHA1digest, &SHA1context );
1210 return pw_string64( scheme, &digest, &salt);
1213 static struct berval *hash_sha1(
1214 const struct berval *scheme,
1215 const struct berval *passwd,
1218 lutil_SHA1_CTX SHA1context;
1219 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1220 struct berval digest;
1221 digest.bv_val = (char *) SHA1digest;
1222 digest.bv_len = sizeof(SHA1digest);
1224 lutil_SHA1Init( &SHA1context );
1225 lutil_SHA1Update( &SHA1context,
1226 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1227 lutil_SHA1Final( SHA1digest, &SHA1context );
1229 return pw_string64( scheme, &digest, NULL);
1233 static struct berval *hash_smd5(
1234 const struct berval *scheme,
1235 const struct berval *passwd,
1238 lutil_MD5_CTX MD5context;
1239 unsigned char MD5digest[LUTIL_MD5_BYTES];
1240 char saltdata[SALT_SIZE];
1241 struct berval digest;
1244 digest.bv_val = (char *) MD5digest;
1245 digest.bv_len = sizeof(MD5digest);
1246 salt.bv_val = saltdata;
1247 salt.bv_len = sizeof(saltdata);
1249 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1253 lutil_MD5Init( &MD5context );
1254 lutil_MD5Update( &MD5context,
1255 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1256 lutil_MD5Update( &MD5context,
1257 (const unsigned char *) salt.bv_val, salt.bv_len );
1258 lutil_MD5Final( MD5digest, &MD5context );
1260 return pw_string64( scheme, &digest, &salt );
1263 static struct berval *hash_md5(
1264 const struct berval *scheme,
1265 const struct berval *passwd,
1268 lutil_MD5_CTX MD5context;
1269 unsigned char MD5digest[LUTIL_MD5_BYTES];
1271 struct berval digest;
1273 digest.bv_val = (char *) MD5digest;
1274 digest.bv_len = sizeof(MD5digest);
1276 lutil_MD5Init( &MD5context );
1277 lutil_MD5Update( &MD5context,
1278 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1279 lutil_MD5Final( MD5digest, &MD5context );
1281 return pw_string64( scheme, &digest, NULL );
1286 static struct berval *hash_lanman(
1287 const struct berval *scheme,
1288 const struct berval *passwd,
1293 char UcasePassword[15];
1295 des_key_schedule schedule;
1296 des_cblock StdText = "KGS!@#$%";
1297 des_cblock PasswordHash1, PasswordHash2;
1298 char PasswordHash[33];
1301 for( i=0; i<passwd->bv_len; i++) {
1302 if(passwd->bv_val[i] == '\0') {
1303 return NULL; /* NUL character in password */
1307 if( passwd->bv_val[i] != '\0' ) {
1308 return NULL; /* passwd must behave like a string */
1311 strncpy( UcasePassword, passwd->bv_val, 14 );
1312 UcasePassword[14] = '\0';
1313 ldap_pvt_str2upper( UcasePassword );
1315 lmPasswd_to_key( UcasePassword, &key );
1316 des_set_key_unchecked( &key, schedule );
1317 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1319 lmPasswd_to_key( &UcasePassword[7], &key );
1320 des_set_key_unchecked( &key, schedule );
1321 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1323 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1324 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1325 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1326 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1327 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1329 hash.bv_val = PasswordHash;
1332 return pw_string( scheme, &hash );
1334 #endif /* SLAPD_LMHASH */
1337 static struct berval *hash_crypt(
1338 const struct berval *scheme,
1339 const struct berval *passwd,
1343 unsigned char salt[32]; /* salt suitable for most anything */
1346 for( i=0; i<passwd->bv_len; i++) {
1347 if(passwd->bv_val[i] == '\0') {
1348 return NULL; /* NUL character in password */
1352 if( passwd->bv_val[i] != '\0' ) {
1353 return NULL; /* passwd must behave like a string */
1356 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1360 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1361 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1363 salt[sizeof( salt ) - 1 ] = '\0';
1365 if( salt_format != NULL ) {
1366 /* copy the salt we made into entropy before snprintfing
1367 it back into the salt */
1368 char entropy[sizeof(salt)];
1369 strcpy( entropy, (char *) salt );
1370 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1373 hash.bv_val = crypt( passwd->bv_val, (char *) salt );
1375 if( hash.bv_val == NULL ) return NULL;
1377 hash.bv_len = strlen( hash.bv_val );
1379 if( hash.bv_len == 0 ) {
1383 return pw_string( scheme, &hash );
1387 int lutil_salt_format(const char *format)
1390 free( salt_format );
1392 salt_format = format != NULL ? strdup( format ) : NULL;
1398 #ifdef SLAPD_CLEARTEXT
1399 static struct berval *hash_clear(
1400 const struct berval *scheme,
1401 const struct berval *passwd,
1404 return ber_bvdup( (struct berval *) passwd );