3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * const struct berval *passwd,
9 * const struct berval *cred,
10 * const char **schemes )
12 * Returns true if user supplied credentials (cred) matches
13 * the stored password (passwd).
15 * Due to the use of the crypt(3) function
16 * this routine is NOT thread-safe.
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
27 # ifdef HAVE_SASL_SASL_H
28 # include <sasl/sasl.h>
39 /* KPASSWD/krb.h brings in a conflicting des.h so don't use both.
40 * configure currently requires OpenSSL to enable LMHASH. Obviously
41 * this requirement can be fulfilled by the KRB DES library as well.
43 #if defined(SLAPD_LMHASH) && !defined(DES_ENCRYPT)
44 # include <openssl/des.h>
45 #endif /* SLAPD_LMHASH */
50 # include <ac/crypt.h>
52 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
59 # ifdef HAVE_AIX_SECURITY
70 #include "lutil_md5.h"
71 #include "lutil_sha1.h"
74 static const unsigned char crypt64[] =
75 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
78 static char *salt_format = NULL;
83 LUTIL_PASSWD_CHK_FUNC *chk_fn;
84 LUTIL_PASSWD_HASH_FUNC *hash_fn;
88 struct pw_slist *next;
92 /* password check routines */
94 static LUTIL_PASSWD_CHK_FUNC chk_md5;
95 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
96 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
97 static LUTIL_PASSWD_HASH_FUNC hash_md5;
100 #ifdef LUTIL_SHA1_BYTES
101 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
102 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
103 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
104 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
108 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
109 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
112 #ifdef SLAPD_NS_MTA_MD5
113 static LUTIL_PASSWD_CHK_FUNC chk_ns_mta_md5;
117 static LUTIL_PASSWD_CHK_FUNC chk_sasl;
121 static LUTIL_PASSWD_CHK_FUNC chk_kerberos;
125 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
126 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
128 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
129 static LUTIL_PASSWD_CHK_FUNC chk_unix;
133 /* password hash routines */
135 #ifdef SLAPD_CLEARTEXT
136 static LUTIL_PASSWD_HASH_FUNC hash_clear;
139 static struct pw_slist *pw_schemes;
141 static const struct pw_scheme pw_schemes_default[] =
143 #ifdef LUTIL_SHA1_BYTES
144 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
145 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
148 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
149 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
152 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
153 #endif /* SLAPD_LMHASH */
155 #ifdef SLAPD_NS_MTA_MD5
156 { BER_BVC("{NS-MTA-MD5}"), chk_ns_mta_md5, NULL },
157 #endif /* SLAPD_NS_MTA_MD5 */
160 { BER_BVC("{SASL}"), chk_sasl, NULL },
164 { BER_BVC("{KERBEROS}"), chk_kerberos, NULL },
168 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
169 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
170 { BER_BVC("{UNIX}"), chk_unix, NULL },
174 #ifdef SLAPD_CLEARTEXT
176 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
179 { BER_BVNULL, NULL, NULL }
182 int lutil_passwd_add(
183 struct berval *scheme,
184 LUTIL_PASSWD_CHK_FUNC *chk,
185 LUTIL_PASSWD_HASH_FUNC *hash )
187 struct pw_slist *ptr;
189 ptr = ber_memalloc( sizeof( struct pw_slist ));
191 ptr->next = pw_schemes;
192 ptr->s.name = *scheme;
194 ptr->s.hash_fn = hash;
199 void lutil_passwd_init()
201 struct pw_slist *ptr;
204 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
205 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn )) break;
209 void lutil_passwd_destroy()
211 struct pw_slist *ptr, *next;
213 for( ptr=pw_schemes; ptr; ptr=next ) {
219 static const struct pw_scheme *get_scheme(
222 struct pw_slist *pws;
224 if (!pw_schemes) lutil_passwd_init();
226 for( pws=pw_schemes; pws; pws=pws->next ) {
227 if( strcasecmp(scheme, pws->s.name.bv_val ) == 0 ) {
235 int lutil_passwd_scheme(
238 if( scheme == NULL ) {
242 return get_scheme(scheme) != NULL;
246 static int is_allowed_scheme(
248 const char** schemes )
252 if( schemes == NULL ) return 1;
254 for( i=0; schemes[i] != NULL; i++ ) {
255 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
262 static struct berval *passwd_scheme(
263 const struct pw_scheme *scheme,
264 const struct berval * passwd,
266 const char** allowed )
268 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
272 if( passwd->bv_len >= scheme->name.bv_len ) {
273 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
274 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
275 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
285 * Return 0 if creds are good.
289 const struct berval *passwd, /* stored passwd */
290 const struct berval *cred, /* user cred */
291 const char **schemes,
294 struct pw_slist *pws;
296 if ( text ) *text = NULL;
298 if (cred == NULL || cred->bv_len == 0 ||
299 passwd == NULL || passwd->bv_len == 0 )
304 if (!pw_schemes) lutil_passwd_init();
306 for( pws=pw_schemes; pws; pws=pws->next ) {
307 if( pws->s.chk_fn ) {
309 struct berval *p = passwd_scheme( &(pws->s),
310 passwd, &x, schemes );
313 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
318 #ifdef SLAPD_CLEARTEXT
319 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
320 return (( passwd->bv_len == cred->bv_len ) &&
321 ( passwd->bv_val[0] != '{' /*'}'*/ ))
322 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
329 struct berval * lutil_passwd_generate( ber_len_t len )
333 if( len < 1 ) return NULL;
335 pw = ber_memalloc( sizeof( struct berval ) );
336 if( pw == NULL ) return NULL;
339 pw->bv_val = ber_memalloc( len + 1 );
341 if( pw->bv_val == NULL ) {
346 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
351 for( len = 0; len < pw->bv_len; len++ ) {
352 pw->bv_val[len] = crypt64[
353 pw->bv_val[len] % (sizeof(crypt64)-1) ];
356 pw->bv_val[len] = '\0';
361 struct berval * lutil_passwd_hash(
362 const struct berval * passwd,
366 const struct pw_scheme *sc = get_scheme( method );
368 if( text ) *text = NULL;
369 if( sc == NULL ) return NULL;
370 if( ! sc->hash_fn ) return NULL;
372 return (sc->hash_fn)( &sc->name, passwd, text );
375 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
376 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
377 static struct berval * pw_string(
378 const struct berval *sc,
379 const struct berval *passwd )
381 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
382 if( pw == NULL ) return NULL;
384 pw->bv_len = sc->bv_len + passwd->bv_len;
385 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
387 if( pw->bv_val == NULL ) {
392 AC_MEMCPY( pw->bv_val, sc->bv_val, sc->bv_len );
393 AC_MEMCPY( &pw->bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
395 pw->bv_val[pw->bv_len] = '\0';
398 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
400 static struct berval * pw_string64(
401 const struct berval *sc,
402 const struct berval *hash,
403 const struct berval *salt )
406 struct berval string;
407 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
410 if( b64 == NULL ) return NULL;
413 /* need to base64 combined string */
414 string.bv_len = hash->bv_len + salt->bv_len;
415 string.bv_val = ber_memalloc( string.bv_len + 1 );
417 if( string.bv_val == NULL ) {
422 AC_MEMCPY( string.bv_val, hash->bv_val,
424 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
426 string.bv_val[string.bv_len] = '\0';
432 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
433 b64->bv_len = b64len + sc->bv_len;
434 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
436 if( b64->bv_val == NULL ) {
437 if( salt ) ber_memfree( string.bv_val );
442 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
445 (unsigned char *) string.bv_val, string.bv_len,
446 &b64->bv_val[sc->bv_len], b64len );
448 if( salt ) ber_memfree( string.bv_val );
455 /* recompute length */
456 b64->bv_len = sc->bv_len + rc;
457 assert( strlen(b64->bv_val) == b64->bv_len );
461 /* PASSWORD CHECK ROUTINES */
463 #ifdef LUTIL_SHA1_BYTES
464 static int chk_ssha1(
465 const struct berval *sc,
466 const struct berval * passwd,
467 const struct berval * cred,
470 lutil_SHA1_CTX SHA1context;
471 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
473 unsigned char *orig_pass = NULL;
476 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
480 /* decode base64 password */
481 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
482 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
484 if( orig_pass == NULL ) return -1;
486 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
488 if (rc <= sizeof(SHA1digest)) {
489 ber_memfree(orig_pass);
493 /* hash credentials with salt */
494 lutil_SHA1Init(&SHA1context);
495 lutil_SHA1Update(&SHA1context,
496 (const unsigned char *) cred->bv_val, cred->bv_len);
497 lutil_SHA1Update(&SHA1context,
498 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
499 rc - sizeof(SHA1digest));
500 lutil_SHA1Final(SHA1digest, &SHA1context);
503 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
504 ber_memfree(orig_pass);
509 const struct berval *sc,
510 const struct berval * passwd,
511 const struct berval * cred,
514 lutil_SHA1_CTX SHA1context;
515 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
517 unsigned char *orig_pass = NULL;
519 /* base64 un-encode password */
520 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
521 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
523 if( orig_pass == NULL ) return -1;
525 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
527 if( rc != sizeof(SHA1digest) ) {
528 ber_memfree(orig_pass);
532 /* hash credentials with salt */
533 lutil_SHA1Init(&SHA1context);
534 lutil_SHA1Update(&SHA1context,
535 (const unsigned char *) cred->bv_val, cred->bv_len);
536 lutil_SHA1Final(SHA1digest, &SHA1context);
539 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
540 ber_memfree(orig_pass);
546 const struct berval *sc,
547 const struct berval * passwd,
548 const struct berval * cred,
551 lutil_MD5_CTX MD5context;
552 unsigned char MD5digest[LUTIL_MD5_BYTES];
554 unsigned char *orig_pass = NULL;
557 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
561 /* base64 un-encode password */
562 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
563 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
565 if( orig_pass == NULL ) return -1;
567 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
569 if (rc <= sizeof(MD5digest)) {
570 ber_memfree(orig_pass);
574 /* hash credentials with salt */
575 lutil_MD5Init(&MD5context);
576 lutil_MD5Update(&MD5context,
577 (const unsigned char *) cred->bv_val,
579 lutil_MD5Update(&MD5context,
580 &orig_pass[sizeof(MD5digest)],
581 rc - sizeof(MD5digest));
582 lutil_MD5Final(MD5digest, &MD5context);
585 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
586 ber_memfree(orig_pass);
591 const struct berval *sc,
592 const struct berval * passwd,
593 const struct berval * cred,
596 lutil_MD5_CTX MD5context;
597 unsigned char MD5digest[LUTIL_MD5_BYTES];
599 unsigned char *orig_pass = NULL;
601 /* base64 un-encode password */
602 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
603 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
605 if( orig_pass == NULL ) return -1;
607 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
608 if ( rc != sizeof(MD5digest) ) {
609 ber_memfree(orig_pass);
613 /* hash credentials with salt */
614 lutil_MD5Init(&MD5context);
615 lutil_MD5Update(&MD5context,
616 (const unsigned char *) cred->bv_val,
618 lutil_MD5Final(MD5digest, &MD5context);
621 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
622 ber_memfree(orig_pass);
627 /* pseudocode from RFC2433
628 * A.2 LmPasswordHash()
631 * IN 0-to-14-oem-char Password,
632 * OUT 16-octet PasswordHash )
634 * Set UcasePassword to the uppercased Password
635 * Zero pad UcasePassword to 14 characters
637 * DesHash( 1st 7-octets of UcasePassword,
638 * giving 1st 8-octets of PasswordHash )
640 * DesHash( 2nd 7-octets of UcasePassword,
641 * giving 2nd 8-octets of PasswordHash )
649 * OUT 8-octet Cypher )
652 * * Make Cypher an irreversibly encrypted form of Clear by
653 * * encrypting known text using Clear as the secret key.
654 * * The known text consists of the string
659 * Set StdText to "KGS!@#$%"
660 * DesEncrypt( StdText, Clear, giving Cypher )
669 * OUT 8-octet Cypher )
672 * * Use the DES encryption algorithm [4] in ECB mode [9]
673 * * to encrypt Clear into Cypher such that Cypher can
674 * * only be decrypted back to Clear by providing Key.
675 * * Note that the DES algorithm takes as input a 64-bit
676 * * stream where the 8th, 16th, 24th, etc. bits are
677 * * parity bits ignored by the encrypting algorithm.
678 * * Unless you write your own DES to accept 56-bit input
679 * * without parity, you will need to insert the parity bits
685 static void lmPasswd_to_key(
686 const unsigned char *lmPasswd,
689 /* make room for parity bits */
690 ((char *)key)[0] = lmPasswd[0];
691 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
692 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
693 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
694 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
695 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
696 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
697 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
699 des_set_odd_parity( key );
702 static int chk_lanman(
703 const struct berval *scheme,
704 const struct berval *passwd,
705 const struct berval *cred,
709 char UcasePassword[15];
711 des_key_schedule schedule;
712 des_cblock StdText = "KGS!@#$%";
713 des_cblock PasswordHash1, PasswordHash2;
714 char PasswordHash[33], storedPasswordHash[33];
716 for( i=0; i<cred->bv_len; i++) {
717 if(cred->bv_val[i] == '\0') {
718 return -1; /* NUL character in password */
722 if( cred->bv_val[i] != '\0' ) {
723 return -1; /* passwd must behave like a string */
726 strncpy( UcasePassword, cred->bv_val, 14 );
727 UcasePassword[14] = '\0';
728 ldap_pvt_str2upper( UcasePassword );
730 lmPasswd_to_key( UcasePassword, &key );
731 des_set_key_unchecked( &key, schedule );
732 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
734 lmPasswd_to_key( &UcasePassword[7], &key );
735 des_set_key_unchecked( &key, schedule );
736 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
738 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
739 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
740 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
741 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
742 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
744 /* as a precaution convert stored password hash to lower case */
745 strncpy( storedPasswordHash, passwd->bv_val, 32 );
746 storedPasswordHash[32] = '\0';
747 ldap_pvt_str2lower( storedPasswordHash );
749 return memcmp( PasswordHash, storedPasswordHash, 32) ? 1 : 0;
751 #endif /* SLAPD_LMHASH */
753 #ifdef SLAPD_NS_MTA_MD5
754 static int chk_ns_mta_md5(
755 const struct berval *scheme,
756 const struct berval *passwd,
757 const struct berval *cred,
760 lutil_MD5_CTX MD5context;
761 unsigned char MD5digest[LUTIL_MD5_BYTES], c;
762 char buffer[LUTIL_MD5_BYTES*2];
765 if( passwd.bv_len != LUTIL_MD5_BYTES*2 ) {
769 /* hash credentials with salt */
770 lutil_MD5Init(&MD5context);
771 lutil_MD5Update(&MD5context,
772 (const unsigned char *) &passwd->bv_val[32],
776 lutil_MD5Update(&MD5context,
777 (const unsigned char *) &c,
780 lutil_MD5Update(&MD5context,
781 (const unsigned char *) cred->bv_val,
785 lutil_MD5Update(&MD5context,
786 (const unsigned char *) &c,
789 lutil_MD5Update(&MD5context,
790 (const unsigned char *) &passwd->bv_val[32],
793 lutil_MD5Final(MD5digest, &MD5context);
795 for( i=0; i < sizeof( MD5digest ); i++ ) {
796 buffer[i+i] = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F];
797 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F];
801 return memcmp((char *)passwd->bv_val,
802 (char *)buffer, sizeof(buffer)) ? 1 : 0;
807 #ifdef HAVE_CYRUS_SASL
808 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
812 const struct berval *sc,
813 const struct berval * passwd,
814 const struct berval * cred,
820 for( i=0; i<cred->bv_len; i++) {
821 if(cred->bv_val[i] == '\0') {
822 return 1; /* NUL character in password */
826 if( cred->bv_val[i] != '\0' ) {
827 return 1; /* cred must behave like a string */
830 for( i=0; i<passwd->bv_len; i++) {
831 if(passwd->bv_val[i] == '\0') {
832 return 1; /* NUL character in password */
836 if( passwd->bv_val[i] != '\0' ) {
837 return 1; /* passwd must behave like a string */
842 #ifdef HAVE_CYRUS_SASL
843 if( lutil_passwd_sasl_conn != NULL ) {
845 # if SASL_VERSION_MAJOR < 2
846 sc = sasl_checkpass( lutil_passwd_sasl_conn,
847 passwd->bv_val, passwd->bv_len,
848 cred->bv_val, cred->bv_len,
851 sc = sasl_checkpass( lutil_passwd_sasl_conn,
852 passwd->bv_val, passwd->bv_len,
853 cred->bv_val, cred->bv_len );
855 rtn = ( sc != SASL_OK );
864 static int chk_kerberos(
865 const struct berval *sc,
866 const struct berval * passwd,
867 const struct berval * cred,
873 for( i=0; i<cred->bv_len; i++) {
874 if(cred->bv_val[i] == '\0') {
875 return 1; /* NUL character in password */
879 if( cred->bv_val[i] != '\0' ) {
880 return 1; /* cred must behave like a string */
883 for( i=0; i<passwd->bv_len; i++) {
884 if(passwd->bv_val[i] == '\0') {
885 return 1; /* NUL character in password */
889 if( passwd->bv_val[i] != '\0' ) {
890 return 1; /* passwd must behave like a string */
895 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
898 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
899 * (Royal Institute of Technology, Stockholm, Sweden).
900 * All rights reserved.
902 * Redistribution and use in source and binary forms, with or without
903 * modification, are permitted provided that the following conditions
906 * 1. Redistributions of source code must retain the above copyright
907 * notice, this list of conditions and the following disclaimer.
909 * 2. Redistributions in binary form must reproduce the above copyright
910 * notice, this list of conditions and the following disclaimer in the
911 * documentation and/or other materials provided with the distribution.
913 * 3. Neither the name of the Institute nor the names of its contributors
914 * may be used to endorse or promote products derived from this software
915 * without specific prior written permission.
917 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
918 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
919 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
920 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
921 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
922 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
923 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
924 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
925 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
926 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
930 krb5_context context;
933 krb5_get_init_creds_opt get_options;
934 krb5_verify_init_creds_opt verify_options;
935 krb5_principal client, server;
937 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
940 ret = krb5_init_context( &context );
946 krb5_get_init_creds_opt_set_preauth_list(&get_options,
950 krb5_get_init_creds_opt_init( &get_options );
952 krb5_verify_init_creds_opt_init( &verify_options );
954 ret = krb5_parse_name( context, passwd->bv_val, &client );
957 krb5_free_context( context );
961 ret = krb5_get_init_creds_password( context,
962 &creds, client, cred->bv_val, NULL,
963 NULL, 0, NULL, &get_options );
966 krb5_free_principal( context, client );
967 krb5_free_context( context );
972 char *host = ldap_pvt_get_fqdn( NULL );
975 krb5_free_principal( context, client );
976 krb5_free_context( context );
980 ret = krb5_sname_to_principal( context,
981 host, "ldap", KRB5_NT_SRV_HST, &server );
987 krb5_free_principal( context, client );
988 krb5_free_context( context );
992 ret = krb5_verify_init_creds( context,
993 &creds, server, NULL, NULL, &verify_options );
995 krb5_free_principal( context, client );
996 krb5_free_principal( context, server );
997 krb5_free_cred_contents( context, &creds );
998 krb5_free_context( context );
1002 #elif defined(HAVE_KRB4)
1004 /* Borrowed from Heimdal kpopper */
1006 * Copyright (c) 1989 Regents of the University of California.
1007 * All rights reserved. The Berkeley software License Agreement
1008 * specifies the terms and conditions for redistribution.
1012 char lrealm[REALM_SZ];
1013 char tkt[MAXHOSTNAMELEN];
1015 status = krb_get_lrealm(lrealm,1);
1016 if (status == KFAILURE) {
1020 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
1021 TKT_ROOT, (unsigned)getpid());
1022 krb_set_tkt_string (tkt);
1024 status = krb_verify_user( passwd->bv_val, "", lrealm,
1025 cred->bv_val, 1, "ldap");
1027 dest_tkt(); /* no point in keeping the tickets */
1029 return status == KFAILURE;
1035 #endif /* SLAPD_KPASSWD */
1038 static int chk_crypt(
1039 const struct berval *sc,
1040 const struct berval * passwd,
1041 const struct berval * cred,
1047 for( i=0; i<cred->bv_len; i++) {
1048 if(cred->bv_val[i] == '\0') {
1049 return 1; /* NUL character in password */
1053 if( cred->bv_val[i] != '\0' ) {
1054 return -1; /* cred must behave like a string */
1057 if( passwd->bv_len < 2 ) {
1058 return -1; /* passwd must be at least two characters long */
1061 for( i=0; i<passwd->bv_len; i++) {
1062 if(passwd->bv_val[i] == '\0') {
1063 return -1; /* NUL character in password */
1067 if( passwd->bv_val[i] != '\0' ) {
1068 return -1; /* passwd must behave like a string */
1071 cr = crypt( cred->bv_val, passwd->bv_val );
1073 if( cr == NULL || cr[0] == '\0' ) {
1074 /* salt must have been invalid */
1078 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
1081 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
1082 static int chk_unix(
1083 const struct berval *sc,
1084 const struct berval * passwd,
1085 const struct berval * cred,
1091 for( i=0; i<cred->bv_len; i++) {
1092 if(cred->bv_val[i] == '\0') {
1093 return -1; /* NUL character in password */
1096 if( cred->bv_val[i] != '\0' ) {
1097 return -1; /* cred must behave like a string */
1100 for( i=0; i<passwd->bv_len; i++) {
1101 if(passwd->bv_val[i] == '\0') {
1102 return -1; /* NUL character in password */
1106 if( passwd->bv_val[i] != '\0' ) {
1107 return -1; /* passwd must behave like a string */
1111 struct passwd *pwd = getpwnam(passwd->bv_val);
1114 return -1; /* not found */
1117 pw = pwd->pw_passwd;
1119 # ifdef HAVE_GETSPNAM
1121 struct spwd *spwd = getspnam(passwd->bv_val);
1128 # ifdef HAVE_AIX_SECURITY
1130 struct userpw *upw = getuserpw(passwd->bv_val);
1133 pw = upw->upw_passwd;
1138 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1139 /* password must must be at least two characters long */
1143 cr = crypt(cred->bv_val, pw);
1145 if( cr == NULL || cr[0] == '\0' ) {
1146 /* salt must have been invalid */
1150 return strcmp(pw, cr) ? 1 : 0;
1156 /* PASSWORD GENERATION ROUTINES */
1158 #ifdef LUTIL_SHA1_BYTES
1159 static struct berval *hash_ssha1(
1160 const struct berval *scheme,
1161 const struct berval *passwd,
1164 lutil_SHA1_CTX SHA1context;
1165 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1167 struct berval digest;
1170 digest.bv_val = (char *) SHA1digest;
1171 digest.bv_len = sizeof(SHA1digest);
1172 salt.bv_val = saltdata;
1173 salt.bv_len = sizeof(saltdata);
1175 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1179 lutil_SHA1Init( &SHA1context );
1180 lutil_SHA1Update( &SHA1context,
1181 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1182 lutil_SHA1Update( &SHA1context,
1183 (const unsigned char *)salt.bv_val, salt.bv_len );
1184 lutil_SHA1Final( SHA1digest, &SHA1context );
1186 return pw_string64( scheme, &digest, &salt);
1189 static struct berval *hash_sha1(
1190 const struct berval *scheme,
1191 const struct berval *passwd,
1194 lutil_SHA1_CTX SHA1context;
1195 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1196 struct berval digest;
1197 digest.bv_val = (char *) SHA1digest;
1198 digest.bv_len = sizeof(SHA1digest);
1200 lutil_SHA1Init( &SHA1context );
1201 lutil_SHA1Update( &SHA1context,
1202 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1203 lutil_SHA1Final( SHA1digest, &SHA1context );
1205 return pw_string64( scheme, &digest, NULL);
1209 static struct berval *hash_smd5(
1210 const struct berval *scheme,
1211 const struct berval *passwd,
1214 lutil_MD5_CTX MD5context;
1215 unsigned char MD5digest[LUTIL_MD5_BYTES];
1217 struct berval digest;
1220 digest.bv_val = (char *) MD5digest;
1221 digest.bv_len = sizeof(MD5digest);
1222 salt.bv_val = saltdata;
1223 salt.bv_len = sizeof(saltdata);
1225 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1229 lutil_MD5Init( &MD5context );
1230 lutil_MD5Update( &MD5context,
1231 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1232 lutil_MD5Update( &MD5context,
1233 (const unsigned char *) salt.bv_val, salt.bv_len );
1234 lutil_MD5Final( MD5digest, &MD5context );
1236 return pw_string64( scheme, &digest, &salt );
1239 static struct berval *hash_md5(
1240 const struct berval *scheme,
1241 const struct berval *passwd,
1244 lutil_MD5_CTX MD5context;
1245 unsigned char MD5digest[LUTIL_MD5_BYTES];
1247 struct berval digest;
1249 digest.bv_val = (char *) MD5digest;
1250 digest.bv_len = sizeof(MD5digest);
1252 lutil_MD5Init( &MD5context );
1253 lutil_MD5Update( &MD5context,
1254 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1255 lutil_MD5Final( MD5digest, &MD5context );
1257 return pw_string64( scheme, &digest, NULL );
1262 static struct berval *hash_lanman(
1263 const struct berval *scheme,
1264 const struct berval *passwd,
1269 char UcasePassword[15];
1271 des_key_schedule schedule;
1272 des_cblock StdText = "KGS!@#$%";
1273 des_cblock PasswordHash1, PasswordHash2;
1274 char PasswordHash[33];
1277 for( i=0; i<passwd->bv_len; i++) {
1278 if(passwd->bv_val[i] == '\0') {
1279 return NULL; /* NUL character in password */
1283 if( passwd->bv_val[i] != '\0' ) {
1284 return NULL; /* passwd must behave like a string */
1287 strncpy( UcasePassword, passwd->bv_val, 14 );
1288 UcasePassword[14] = '\0';
1289 ldap_pvt_str2upper( UcasePassword );
1291 lmPasswd_to_key( UcasePassword, &key );
1292 des_set_key_unchecked( &key, schedule );
1293 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1295 lmPasswd_to_key( &UcasePassword[7], &key );
1296 des_set_key_unchecked( &key, schedule );
1297 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1299 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1300 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1301 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1302 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1303 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1305 hash.bv_val = PasswordHash;
1308 return pw_string( scheme, &hash );
1310 #endif /* SLAPD_LMHASH */
1313 static struct berval *hash_crypt(
1314 const struct berval *scheme,
1315 const struct berval *passwd,
1319 unsigned char salt[32]; /* salt suitable for most anything */
1322 for( i=0; i<passwd->bv_len; i++) {
1323 if(passwd->bv_val[i] == '\0') {
1324 return NULL; /* NUL character in password */
1328 if( passwd->bv_val[i] != '\0' ) {
1329 return NULL; /* passwd must behave like a string */
1332 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1336 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1337 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1339 salt[sizeof( salt ) - 1 ] = '\0';
1341 if( salt_format != NULL ) {
1342 /* copy the salt we made into entropy before snprintfing
1343 it back into the salt */
1344 char entropy[sizeof(salt)];
1345 strcpy( entropy, (char *) salt );
1346 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1349 hash.bv_val = crypt( passwd->bv_val, (char *) salt );
1351 if( hash.bv_val == NULL ) return NULL;
1353 hash.bv_len = strlen( hash.bv_val );
1355 if( hash.bv_len == 0 ) {
1359 return pw_string( scheme, &hash );
1363 int lutil_salt_format(const char *format)
1366 free( salt_format );
1368 salt_format = format != NULL ? strdup( format ) : NULL;
1374 #ifdef SLAPD_CLEARTEXT
1375 static struct berval *hash_clear(
1376 const struct berval *scheme,
1377 const struct berval *passwd,
1380 return ber_bvdup( (struct berval *) passwd );