3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
19 * const struct berval *passwd,
20 * const struct berval *cred,
21 * const char **schemes )
23 * Returns true if user supplied credentials (cred) matches
24 * the stored password (passwd).
26 * Due to the use of the crypt(3) function
27 * this routine is NOT thread-safe.
33 #include <ac/stdlib.h>
34 #include <ac/string.h>
35 #include <ac/unistd.h>
38 # ifdef HAVE_SASL_SASL_H
39 # include <sasl/sasl.h>
50 /* KPASSWD/krb.h brings in a conflicting des.h so don't use both.
51 * configure currently requires OpenSSL to enable LMHASH. Obviously
52 * this requirement can be fulfilled by the KRB DES library as well.
54 #if defined(SLAPD_LMHASH) && !defined(DES_ENCRYPT)
55 # include <openssl/des.h>
56 #endif /* SLAPD_LMHASH */
61 # include <ac/crypt.h>
63 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
70 # ifdef HAVE_AIX_SECURITY
81 #include "lutil_md5.h"
82 #include "lutil_sha1.h"
85 static const unsigned char crypt64[] =
86 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
89 static char *salt_format = NULL;
94 LUTIL_PASSWD_CHK_FUNC *chk_fn;
95 LUTIL_PASSWD_HASH_FUNC *hash_fn;
99 struct pw_slist *next;
103 /* password check routines */
105 static LUTIL_PASSWD_CHK_FUNC chk_md5;
106 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
107 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
108 static LUTIL_PASSWD_HASH_FUNC hash_md5;
111 #ifdef LUTIL_SHA1_BYTES
112 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
113 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
114 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
115 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
119 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
120 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
123 #ifdef SLAPD_NS_MTA_MD5
124 static LUTIL_PASSWD_CHK_FUNC chk_ns_mta_md5;
128 static LUTIL_PASSWD_CHK_FUNC chk_sasl;
132 static LUTIL_PASSWD_CHK_FUNC chk_kerberos;
136 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
137 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
139 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
140 static LUTIL_PASSWD_CHK_FUNC chk_unix;
144 /* password hash routines */
146 #ifdef SLAPD_CLEARTEXT
147 static LUTIL_PASSWD_HASH_FUNC hash_clear;
150 static struct pw_slist *pw_schemes;
152 static const struct pw_scheme pw_schemes_default[] =
154 #ifdef LUTIL_SHA1_BYTES
155 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
156 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
159 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
160 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
163 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
164 #endif /* SLAPD_LMHASH */
166 #ifdef SLAPD_NS_MTA_MD5
167 { BER_BVC("{NS-MTA-MD5}"), chk_ns_mta_md5, NULL },
168 #endif /* SLAPD_NS_MTA_MD5 */
171 { BER_BVC("{SASL}"), chk_sasl, NULL },
175 { BER_BVC("{KERBEROS}"), chk_kerberos, NULL },
179 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
180 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
181 { BER_BVC("{UNIX}"), chk_unix, NULL },
185 #ifdef SLAPD_CLEARTEXT
187 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
190 { BER_BVNULL, NULL, NULL }
193 int lutil_passwd_add(
194 struct berval *scheme,
195 LUTIL_PASSWD_CHK_FUNC *chk,
196 LUTIL_PASSWD_HASH_FUNC *hash )
198 struct pw_slist *ptr;
200 ptr = ber_memalloc( sizeof( struct pw_slist ));
202 ptr->next = pw_schemes;
203 ptr->s.name = *scheme;
205 ptr->s.hash_fn = hash;
210 void lutil_passwd_init()
212 struct pw_slist *ptr;
215 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
216 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn )) break;
220 void lutil_passwd_destroy()
222 struct pw_slist *ptr, *next;
224 for( ptr=pw_schemes; ptr; ptr=next ) {
230 static const struct pw_scheme *get_scheme(
233 struct pw_slist *pws;
235 if (!pw_schemes) lutil_passwd_init();
237 for( pws=pw_schemes; pws; pws=pws->next ) {
238 if( strcasecmp(scheme, pws->s.name.bv_val ) == 0 ) {
246 int lutil_passwd_scheme(
249 if( scheme == NULL ) {
253 return get_scheme(scheme) != NULL;
257 static int is_allowed_scheme(
259 const char** schemes )
263 if( schemes == NULL ) return 1;
265 for( i=0; schemes[i] != NULL; i++ ) {
266 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
273 static struct berval *passwd_scheme(
274 const struct pw_scheme *scheme,
275 const struct berval * passwd,
277 const char** allowed )
279 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
283 if( passwd->bv_len >= scheme->name.bv_len ) {
284 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
285 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
286 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
296 * Return 0 if creds are good.
300 const struct berval *passwd, /* stored passwd */
301 const struct berval *cred, /* user cred */
302 const char **schemes,
305 struct pw_slist *pws;
307 if ( text ) *text = NULL;
309 if (cred == NULL || cred->bv_len == 0 ||
310 passwd == NULL || passwd->bv_len == 0 )
315 if (!pw_schemes) lutil_passwd_init();
317 for( pws=pw_schemes; pws; pws=pws->next ) {
318 if( pws->s.chk_fn ) {
320 struct berval *p = passwd_scheme( &(pws->s),
321 passwd, &x, schemes );
324 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
329 #ifdef SLAPD_CLEARTEXT
330 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
331 return (( passwd->bv_len == cred->bv_len ) &&
332 ( passwd->bv_val[0] != '{' /*'}'*/ ))
333 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
340 struct berval * lutil_passwd_generate( ber_len_t len )
344 if( len < 1 ) return NULL;
346 pw = ber_memalloc( sizeof( struct berval ) );
347 if( pw == NULL ) return NULL;
350 pw->bv_val = ber_memalloc( len + 1 );
352 if( pw->bv_val == NULL ) {
357 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
362 for( len = 0; len < pw->bv_len; len++ ) {
363 pw->bv_val[len] = crypt64[
364 pw->bv_val[len] % (sizeof(crypt64)-1) ];
367 pw->bv_val[len] = '\0';
372 struct berval * lutil_passwd_hash(
373 const struct berval * passwd,
377 const struct pw_scheme *sc = get_scheme( method );
379 if( text ) *text = NULL;
380 if( sc == NULL ) return NULL;
381 if( ! sc->hash_fn ) return NULL;
383 return (sc->hash_fn)( &sc->name, passwd, text );
386 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
387 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
388 static struct berval * pw_string(
389 const struct berval *sc,
390 const struct berval *passwd )
392 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
393 if( pw == NULL ) return NULL;
395 pw->bv_len = sc->bv_len + passwd->bv_len;
396 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
398 if( pw->bv_val == NULL ) {
403 AC_MEMCPY( pw->bv_val, sc->bv_val, sc->bv_len );
404 AC_MEMCPY( &pw->bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
406 pw->bv_val[pw->bv_len] = '\0';
409 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
411 static struct berval * pw_string64(
412 const struct berval *sc,
413 const struct berval *hash,
414 const struct berval *salt )
417 struct berval string;
418 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
421 if( b64 == NULL ) return NULL;
424 /* need to base64 combined string */
425 string.bv_len = hash->bv_len + salt->bv_len;
426 string.bv_val = ber_memalloc( string.bv_len + 1 );
428 if( string.bv_val == NULL ) {
433 AC_MEMCPY( string.bv_val, hash->bv_val,
435 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
437 string.bv_val[string.bv_len] = '\0';
443 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
444 b64->bv_len = b64len + sc->bv_len;
445 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
447 if( b64->bv_val == NULL ) {
448 if( salt ) ber_memfree( string.bv_val );
453 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
456 (unsigned char *) string.bv_val, string.bv_len,
457 &b64->bv_val[sc->bv_len], b64len );
459 if( salt ) ber_memfree( string.bv_val );
466 /* recompute length */
467 b64->bv_len = sc->bv_len + rc;
468 assert( strlen(b64->bv_val) == b64->bv_len );
472 /* PASSWORD CHECK ROUTINES */
474 #ifdef LUTIL_SHA1_BYTES
475 static int chk_ssha1(
476 const struct berval *sc,
477 const struct berval * passwd,
478 const struct berval * cred,
481 lutil_SHA1_CTX SHA1context;
482 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
484 unsigned char *orig_pass = NULL;
487 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
491 /* decode base64 password */
492 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
493 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
495 if( orig_pass == NULL ) return -1;
497 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
499 if (rc < 0 || (unsigned)rc <= sizeof(SHA1digest)) {
500 ber_memfree(orig_pass);
504 /* hash credentials with salt */
505 lutil_SHA1Init(&SHA1context);
506 lutil_SHA1Update(&SHA1context,
507 (const unsigned char *) cred->bv_val, cred->bv_len);
508 lutil_SHA1Update(&SHA1context,
509 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
510 rc - sizeof(SHA1digest));
511 lutil_SHA1Final(SHA1digest, &SHA1context);
514 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
515 ber_memfree(orig_pass);
520 const struct berval *sc,
521 const struct berval * passwd,
522 const struct berval * cred,
525 lutil_SHA1_CTX SHA1context;
526 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
528 unsigned char *orig_pass = NULL;
530 /* base64 un-encode password */
531 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
532 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
534 if( orig_pass == NULL ) return -1;
536 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
538 if( rc != sizeof(SHA1digest) ) {
539 ber_memfree(orig_pass);
543 /* hash credentials with salt */
544 lutil_SHA1Init(&SHA1context);
545 lutil_SHA1Update(&SHA1context,
546 (const unsigned char *) cred->bv_val, cred->bv_len);
547 lutil_SHA1Final(SHA1digest, &SHA1context);
550 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
551 ber_memfree(orig_pass);
557 const struct berval *sc,
558 const struct berval * passwd,
559 const struct berval * cred,
562 lutil_MD5_CTX MD5context;
563 unsigned char MD5digest[LUTIL_MD5_BYTES];
565 unsigned char *orig_pass = NULL;
568 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
572 /* base64 un-encode password */
573 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
574 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
576 if( orig_pass == NULL ) return -1;
578 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
580 if (rc < 0 || (unsigned)rc <= sizeof(MD5digest)) {
581 ber_memfree(orig_pass);
585 /* hash credentials with salt */
586 lutil_MD5Init(&MD5context);
587 lutil_MD5Update(&MD5context,
588 (const unsigned char *) cred->bv_val,
590 lutil_MD5Update(&MD5context,
591 &orig_pass[sizeof(MD5digest)],
592 rc - sizeof(MD5digest));
593 lutil_MD5Final(MD5digest, &MD5context);
596 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
597 ber_memfree(orig_pass);
602 const struct berval *sc,
603 const struct berval * passwd,
604 const struct berval * cred,
607 lutil_MD5_CTX MD5context;
608 unsigned char MD5digest[LUTIL_MD5_BYTES];
610 unsigned char *orig_pass = NULL;
612 /* base64 un-encode password */
613 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
614 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
616 if( orig_pass == NULL ) return -1;
618 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
619 if ( rc != sizeof(MD5digest) ) {
620 ber_memfree(orig_pass);
624 /* hash credentials with salt */
625 lutil_MD5Init(&MD5context);
626 lutil_MD5Update(&MD5context,
627 (const unsigned char *) cred->bv_val,
629 lutil_MD5Final(MD5digest, &MD5context);
632 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
633 ber_memfree(orig_pass);
638 /* pseudocode from RFC2433
639 * A.2 LmPasswordHash()
642 * IN 0-to-14-oem-char Password,
643 * OUT 16-octet PasswordHash )
645 * Set UcasePassword to the uppercased Password
646 * Zero pad UcasePassword to 14 characters
648 * DesHash( 1st 7-octets of UcasePassword,
649 * giving 1st 8-octets of PasswordHash )
651 * DesHash( 2nd 7-octets of UcasePassword,
652 * giving 2nd 8-octets of PasswordHash )
660 * OUT 8-octet Cypher )
663 * * Make Cypher an irreversibly encrypted form of Clear by
664 * * encrypting known text using Clear as the secret key.
665 * * The known text consists of the string
670 * Set StdText to "KGS!@#$%"
671 * DesEncrypt( StdText, Clear, giving Cypher )
680 * OUT 8-octet Cypher )
683 * * Use the DES encryption algorithm [4] in ECB mode [9]
684 * * to encrypt Clear into Cypher such that Cypher can
685 * * only be decrypted back to Clear by providing Key.
686 * * Note that the DES algorithm takes as input a 64-bit
687 * * stream where the 8th, 16th, 24th, etc. bits are
688 * * parity bits ignored by the encrypting algorithm.
689 * * Unless you write your own DES to accept 56-bit input
690 * * without parity, you will need to insert the parity bits
696 static void lmPasswd_to_key(
697 const unsigned char *lmPasswd,
700 /* make room for parity bits */
701 ((char *)key)[0] = lmPasswd[0];
702 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
703 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
704 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
705 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
706 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
707 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
708 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
710 des_set_odd_parity( key );
713 static int chk_lanman(
714 const struct berval *scheme,
715 const struct berval *passwd,
716 const struct berval *cred,
720 char UcasePassword[15];
722 des_key_schedule schedule;
723 des_cblock StdText = "KGS!@#$%";
724 des_cblock PasswordHash1, PasswordHash2;
725 char PasswordHash[33], storedPasswordHash[33];
727 for( i=0; i<cred->bv_len; i++) {
728 if(cred->bv_val[i] == '\0') {
729 return -1; /* NUL character in password */
733 if( cred->bv_val[i] != '\0' ) {
734 return -1; /* passwd must behave like a string */
737 strncpy( UcasePassword, cred->bv_val, 14 );
738 UcasePassword[14] = '\0';
739 ldap_pvt_str2upper( UcasePassword );
741 lmPasswd_to_key( UcasePassword, &key );
742 des_set_key_unchecked( &key, schedule );
743 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
745 lmPasswd_to_key( &UcasePassword[7], &key );
746 des_set_key_unchecked( &key, schedule );
747 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
749 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
750 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
751 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
752 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
753 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
755 /* as a precaution convert stored password hash to lower case */
756 strncpy( storedPasswordHash, passwd->bv_val, 32 );
757 storedPasswordHash[32] = '\0';
758 ldap_pvt_str2lower( storedPasswordHash );
760 return memcmp( PasswordHash, storedPasswordHash, 32) ? 1 : 0;
762 #endif /* SLAPD_LMHASH */
764 #ifdef SLAPD_NS_MTA_MD5
765 static int chk_ns_mta_md5(
766 const struct berval *scheme,
767 const struct berval *passwd,
768 const struct berval *cred,
771 lutil_MD5_CTX MD5context;
772 unsigned char MD5digest[LUTIL_MD5_BYTES], c;
773 char buffer[LUTIL_MD5_BYTES*2];
776 if( passwd->bv_len != LUTIL_MD5_BYTES*2 ) {
780 /* hash credentials with salt */
781 lutil_MD5Init(&MD5context);
782 lutil_MD5Update(&MD5context,
783 (const unsigned char *) &passwd->bv_val[32],
787 lutil_MD5Update(&MD5context,
788 (const unsigned char *) &c,
791 lutil_MD5Update(&MD5context,
792 (const unsigned char *) cred->bv_val,
796 lutil_MD5Update(&MD5context,
797 (const unsigned char *) &c,
800 lutil_MD5Update(&MD5context,
801 (const unsigned char *) &passwd->bv_val[32],
804 lutil_MD5Final(MD5digest, &MD5context);
806 for( i=0; i < sizeof( MD5digest ); i++ ) {
807 buffer[i+i] = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F];
808 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F];
812 return memcmp((char *)passwd->bv_val,
813 (char *)buffer, sizeof(buffer)) ? 1 : 0;
818 #ifdef HAVE_CYRUS_SASL
819 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
823 const struct berval *sc,
824 const struct berval * passwd,
825 const struct berval * cred,
831 for( i=0; i<cred->bv_len; i++) {
832 if(cred->bv_val[i] == '\0') {
833 return 1; /* NUL character in password */
837 if( cred->bv_val[i] != '\0' ) {
838 return 1; /* cred must behave like a string */
841 for( i=0; i<passwd->bv_len; i++) {
842 if(passwd->bv_val[i] == '\0') {
843 return 1; /* NUL character in password */
847 if( passwd->bv_val[i] != '\0' ) {
848 return 1; /* passwd must behave like a string */
853 #ifdef HAVE_CYRUS_SASL
854 if( lutil_passwd_sasl_conn != NULL ) {
856 # if SASL_VERSION_MAJOR < 2
857 sc = sasl_checkpass( lutil_passwd_sasl_conn,
858 passwd->bv_val, passwd->bv_len,
859 cred->bv_val, cred->bv_len,
862 sc = sasl_checkpass( lutil_passwd_sasl_conn,
863 passwd->bv_val, passwd->bv_len,
864 cred->bv_val, cred->bv_len );
866 rtn = ( sc != SASL_OK );
875 static int chk_kerberos(
876 const struct berval *sc,
877 const struct berval * passwd,
878 const struct berval * cred,
884 for( i=0; i<cred->bv_len; i++) {
885 if(cred->bv_val[i] == '\0') {
886 return 1; /* NUL character in password */
890 if( cred->bv_val[i] != '\0' ) {
891 return 1; /* cred must behave like a string */
894 for( i=0; i<passwd->bv_len; i++) {
895 if(passwd->bv_val[i] == '\0') {
896 return 1; /* NUL character in password */
900 if( passwd->bv_val[i] != '\0' ) {
901 return 1; /* passwd must behave like a string */
906 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
909 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
910 * (Royal Institute of Technology, Stockholm, Sweden).
911 * All rights reserved.
913 * Redistribution and use in source and binary forms, with or without
914 * modification, are permitted provided that the following conditions
917 * 1. Redistributions of source code must retain the above copyright
918 * notice, this list of conditions and the following disclaimer.
920 * 2. Redistributions in binary form must reproduce the above copyright
921 * notice, this list of conditions and the following disclaimer in the
922 * documentation and/or other materials provided with the distribution.
924 * 3. Neither the name of the Institute nor the names of its contributors
925 * may be used to endorse or promote products derived from this software
926 * without specific prior written permission.
928 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
929 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
930 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
931 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
932 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
933 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
934 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
935 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
936 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
937 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
941 krb5_context context;
944 krb5_get_init_creds_opt get_options;
945 krb5_verify_init_creds_opt verify_options;
946 krb5_principal client, server;
948 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
951 ret = krb5_init_context( &context );
957 krb5_get_init_creds_opt_set_preauth_list(&get_options,
961 krb5_get_init_creds_opt_init( &get_options );
963 krb5_verify_init_creds_opt_init( &verify_options );
965 ret = krb5_parse_name( context, passwd->bv_val, &client );
968 krb5_free_context( context );
972 ret = krb5_get_init_creds_password( context,
973 &creds, client, cred->bv_val, NULL,
974 NULL, 0, NULL, &get_options );
977 krb5_free_principal( context, client );
978 krb5_free_context( context );
983 char *host = ldap_pvt_get_fqdn( NULL );
986 krb5_free_principal( context, client );
987 krb5_free_context( context );
991 ret = krb5_sname_to_principal( context,
992 host, "ldap", KRB5_NT_SRV_HST, &server );
998 krb5_free_principal( context, client );
999 krb5_free_context( context );
1003 ret = krb5_verify_init_creds( context,
1004 &creds, server, NULL, NULL, &verify_options );
1006 krb5_free_principal( context, client );
1007 krb5_free_principal( context, server );
1008 krb5_free_cred_contents( context, &creds );
1009 krb5_free_context( context );
1013 #elif defined(HAVE_KRB4)
1015 /* Borrowed from Heimdal kpopper */
1017 * Copyright (c) 1989 Regents of the University of California.
1018 * All rights reserved. The Berkeley software License Agreement
1019 * specifies the terms and conditions for redistribution.
1023 char lrealm[REALM_SZ];
1024 char tkt[MAXHOSTNAMELEN];
1026 status = krb_get_lrealm(lrealm,1);
1027 if (status == KFAILURE) {
1031 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
1032 TKT_ROOT, (unsigned)getpid());
1033 krb_set_tkt_string (tkt);
1035 status = krb_verify_user( passwd->bv_val, "", lrealm,
1036 cred->bv_val, 1, "ldap");
1038 dest_tkt(); /* no point in keeping the tickets */
1040 return status == KFAILURE;
1046 #endif /* SLAPD_KPASSWD */
1049 static int chk_crypt(
1050 const struct berval *sc,
1051 const struct berval * passwd,
1052 const struct berval * cred,
1058 for( i=0; i<cred->bv_len; i++) {
1059 if(cred->bv_val[i] == '\0') {
1060 return 1; /* NUL character in password */
1064 if( cred->bv_val[i] != '\0' ) {
1065 return -1; /* cred must behave like a string */
1068 if( passwd->bv_len < 2 ) {
1069 return -1; /* passwd must be at least two characters long */
1072 for( i=0; i<passwd->bv_len; i++) {
1073 if(passwd->bv_val[i] == '\0') {
1074 return -1; /* NUL character in password */
1078 if( passwd->bv_val[i] != '\0' ) {
1079 return -1; /* passwd must behave like a string */
1082 cr = crypt( cred->bv_val, passwd->bv_val );
1084 if( cr == NULL || cr[0] == '\0' ) {
1085 /* salt must have been invalid */
1089 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
1092 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
1093 static int chk_unix(
1094 const struct berval *sc,
1095 const struct berval * passwd,
1096 const struct berval * cred,
1102 for( i=0; i<cred->bv_len; i++) {
1103 if(cred->bv_val[i] == '\0') {
1104 return -1; /* NUL character in password */
1107 if( cred->bv_val[i] != '\0' ) {
1108 return -1; /* cred must behave like a string */
1111 for( i=0; i<passwd->bv_len; i++) {
1112 if(passwd->bv_val[i] == '\0') {
1113 return -1; /* NUL character in password */
1117 if( passwd->bv_val[i] != '\0' ) {
1118 return -1; /* passwd must behave like a string */
1122 struct passwd *pwd = getpwnam(passwd->bv_val);
1125 return -1; /* not found */
1128 pw = pwd->pw_passwd;
1130 # ifdef HAVE_GETSPNAM
1132 struct spwd *spwd = getspnam(passwd->bv_val);
1139 # ifdef HAVE_AIX_SECURITY
1141 struct userpw *upw = getuserpw(passwd->bv_val);
1144 pw = upw->upw_passwd;
1149 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1150 /* password must must be at least two characters long */
1154 cr = crypt(cred->bv_val, pw);
1156 if( cr == NULL || cr[0] == '\0' ) {
1157 /* salt must have been invalid */
1161 return strcmp(pw, cr) ? 1 : 0;
1167 /* PASSWORD GENERATION ROUTINES */
1169 #ifdef LUTIL_SHA1_BYTES
1170 static struct berval *hash_ssha1(
1171 const struct berval *scheme,
1172 const struct berval *passwd,
1175 lutil_SHA1_CTX SHA1context;
1176 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1178 struct berval digest;
1181 digest.bv_val = (char *) SHA1digest;
1182 digest.bv_len = sizeof(SHA1digest);
1183 salt.bv_val = saltdata;
1184 salt.bv_len = sizeof(saltdata);
1186 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1190 lutil_SHA1Init( &SHA1context );
1191 lutil_SHA1Update( &SHA1context,
1192 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1193 lutil_SHA1Update( &SHA1context,
1194 (const unsigned char *)salt.bv_val, salt.bv_len );
1195 lutil_SHA1Final( SHA1digest, &SHA1context );
1197 return pw_string64( scheme, &digest, &salt);
1200 static struct berval *hash_sha1(
1201 const struct berval *scheme,
1202 const struct berval *passwd,
1205 lutil_SHA1_CTX SHA1context;
1206 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1207 struct berval digest;
1208 digest.bv_val = (char *) SHA1digest;
1209 digest.bv_len = sizeof(SHA1digest);
1211 lutil_SHA1Init( &SHA1context );
1212 lutil_SHA1Update( &SHA1context,
1213 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1214 lutil_SHA1Final( SHA1digest, &SHA1context );
1216 return pw_string64( scheme, &digest, NULL);
1220 static struct berval *hash_smd5(
1221 const struct berval *scheme,
1222 const struct berval *passwd,
1225 lutil_MD5_CTX MD5context;
1226 unsigned char MD5digest[LUTIL_MD5_BYTES];
1228 struct berval digest;
1231 digest.bv_val = (char *) MD5digest;
1232 digest.bv_len = sizeof(MD5digest);
1233 salt.bv_val = saltdata;
1234 salt.bv_len = sizeof(saltdata);
1236 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1240 lutil_MD5Init( &MD5context );
1241 lutil_MD5Update( &MD5context,
1242 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1243 lutil_MD5Update( &MD5context,
1244 (const unsigned char *) salt.bv_val, salt.bv_len );
1245 lutil_MD5Final( MD5digest, &MD5context );
1247 return pw_string64( scheme, &digest, &salt );
1250 static struct berval *hash_md5(
1251 const struct berval *scheme,
1252 const struct berval *passwd,
1255 lutil_MD5_CTX MD5context;
1256 unsigned char MD5digest[LUTIL_MD5_BYTES];
1258 struct berval digest;
1260 digest.bv_val = (char *) MD5digest;
1261 digest.bv_len = sizeof(MD5digest);
1263 lutil_MD5Init( &MD5context );
1264 lutil_MD5Update( &MD5context,
1265 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1266 lutil_MD5Final( MD5digest, &MD5context );
1268 return pw_string64( scheme, &digest, NULL );
1273 static struct berval *hash_lanman(
1274 const struct berval *scheme,
1275 const struct berval *passwd,
1280 char UcasePassword[15];
1282 des_key_schedule schedule;
1283 des_cblock StdText = "KGS!@#$%";
1284 des_cblock PasswordHash1, PasswordHash2;
1285 char PasswordHash[33];
1288 for( i=0; i<passwd->bv_len; i++) {
1289 if(passwd->bv_val[i] == '\0') {
1290 return NULL; /* NUL character in password */
1294 if( passwd->bv_val[i] != '\0' ) {
1295 return NULL; /* passwd must behave like a string */
1298 strncpy( UcasePassword, passwd->bv_val, 14 );
1299 UcasePassword[14] = '\0';
1300 ldap_pvt_str2upper( UcasePassword );
1302 lmPasswd_to_key( UcasePassword, &key );
1303 des_set_key_unchecked( &key, schedule );
1304 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1306 lmPasswd_to_key( &UcasePassword[7], &key );
1307 des_set_key_unchecked( &key, schedule );
1308 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1310 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1311 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1312 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1313 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1314 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1316 hash.bv_val = PasswordHash;
1319 return pw_string( scheme, &hash );
1321 #endif /* SLAPD_LMHASH */
1324 static struct berval *hash_crypt(
1325 const struct berval *scheme,
1326 const struct berval *passwd,
1330 unsigned char salt[32]; /* salt suitable for most anything */
1333 for( i=0; i<passwd->bv_len; i++) {
1334 if(passwd->bv_val[i] == '\0') {
1335 return NULL; /* NUL character in password */
1339 if( passwd->bv_val[i] != '\0' ) {
1340 return NULL; /* passwd must behave like a string */
1343 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1347 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1348 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1350 salt[sizeof( salt ) - 1 ] = '\0';
1352 if( salt_format != NULL ) {
1353 /* copy the salt we made into entropy before snprintfing
1354 it back into the salt */
1355 char entropy[sizeof(salt)];
1356 strcpy( entropy, (char *) salt );
1357 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1360 hash.bv_val = crypt( passwd->bv_val, (char *) salt );
1362 if( hash.bv_val == NULL ) return NULL;
1364 hash.bv_len = strlen( hash.bv_val );
1366 if( hash.bv_len == 0 ) {
1370 return pw_string( scheme, &hash );
1374 int lutil_salt_format(const char *format)
1377 free( salt_format );
1379 salt_format = format != NULL ? strdup( format ) : NULL;
1385 #ifdef SLAPD_CLEARTEXT
1386 static struct berval *hash_clear(
1387 const struct berval *scheme,
1388 const struct berval *passwd,
1391 return ber_bvdup( (struct berval *) passwd );