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 );
381 if( text ) *text = "scheme not recognized";
385 if( ! sc->hash_fn ) {
386 if( text ) *text = "scheme provided no hash function";
390 if( text ) *text = NULL;
392 return (sc->hash_fn)( &sc->name, passwd, text );
395 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
396 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
397 static struct berval * pw_string(
398 const struct berval *sc,
399 const struct berval *passwd )
401 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
402 if( pw == NULL ) return NULL;
404 pw->bv_len = sc->bv_len + passwd->bv_len;
405 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
407 if( pw->bv_val == NULL ) {
412 AC_MEMCPY( pw->bv_val, sc->bv_val, sc->bv_len );
413 AC_MEMCPY( &pw->bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
415 pw->bv_val[pw->bv_len] = '\0';
418 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
420 static struct berval * pw_string64(
421 const struct berval *sc,
422 const struct berval *hash,
423 const struct berval *salt )
426 struct berval string;
427 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
430 if( b64 == NULL ) return NULL;
433 /* need to base64 combined string */
434 string.bv_len = hash->bv_len + salt->bv_len;
435 string.bv_val = ber_memalloc( string.bv_len + 1 );
437 if( string.bv_val == NULL ) {
442 AC_MEMCPY( string.bv_val, hash->bv_val,
444 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
446 string.bv_val[string.bv_len] = '\0';
452 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
453 b64->bv_len = b64len + sc->bv_len;
454 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
456 if( b64->bv_val == NULL ) {
457 if( salt ) ber_memfree( string.bv_val );
462 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
465 (unsigned char *) string.bv_val, string.bv_len,
466 &b64->bv_val[sc->bv_len], b64len );
468 if( salt ) ber_memfree( string.bv_val );
475 /* recompute length */
476 b64->bv_len = sc->bv_len + rc;
477 assert( strlen(b64->bv_val) == b64->bv_len );
481 /* PASSWORD CHECK ROUTINES */
483 #ifdef LUTIL_SHA1_BYTES
484 static int chk_ssha1(
485 const struct berval *sc,
486 const struct berval * passwd,
487 const struct berval * cred,
490 lutil_SHA1_CTX SHA1context;
491 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
493 unsigned char *orig_pass = NULL;
496 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
497 sizeof(SHA1digest)+SALT_SIZE) {
501 /* decode base64 password */
502 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
503 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
505 if( orig_pass == NULL ) return -1;
507 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
509 if (rc < (int)(sizeof(SHA1digest)+SALT_SIZE)) {
510 ber_memfree(orig_pass);
514 /* hash credentials with salt */
515 lutil_SHA1Init(&SHA1context);
516 lutil_SHA1Update(&SHA1context,
517 (const unsigned char *) cred->bv_val, cred->bv_len);
518 lutil_SHA1Update(&SHA1context,
519 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
520 rc - sizeof(SHA1digest));
521 lutil_SHA1Final(SHA1digest, &SHA1context);
524 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
525 ber_memfree(orig_pass);
530 const struct berval *sc,
531 const struct berval * passwd,
532 const struct berval * cred,
535 lutil_SHA1_CTX SHA1context;
536 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
538 unsigned char *orig_pass = NULL;
541 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
545 /* base64 un-encode password */
546 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
547 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
549 if( orig_pass == NULL ) return -1;
551 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
553 if( rc != sizeof(SHA1digest) ) {
554 ber_memfree(orig_pass);
558 /* hash credentials with salt */
559 lutil_SHA1Init(&SHA1context);
560 lutil_SHA1Update(&SHA1context,
561 (const unsigned char *) cred->bv_val, cred->bv_len);
562 lutil_SHA1Final(SHA1digest, &SHA1context);
565 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
566 ber_memfree(orig_pass);
572 const struct berval *sc,
573 const struct berval * passwd,
574 const struct berval * cred,
577 lutil_MD5_CTX MD5context;
578 unsigned char MD5digest[LUTIL_MD5_BYTES];
580 unsigned char *orig_pass = NULL;
583 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <
584 sizeof(MD5digest)+SALT_SIZE) {
588 /* base64 un-encode password */
589 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
590 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
592 if( orig_pass == NULL ) return -1;
594 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
596 if (rc < (int)(sizeof(MD5digest)+SALT_SIZE)) {
597 ber_memfree(orig_pass);
601 /* hash credentials with salt */
602 lutil_MD5Init(&MD5context);
603 lutil_MD5Update(&MD5context,
604 (const unsigned char *) cred->bv_val,
606 lutil_MD5Update(&MD5context,
607 &orig_pass[sizeof(MD5digest)],
608 rc - sizeof(MD5digest));
609 lutil_MD5Final(MD5digest, &MD5context);
612 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
613 ber_memfree(orig_pass);
618 const struct berval *sc,
619 const struct berval * passwd,
620 const struct berval * cred,
623 lutil_MD5_CTX MD5context;
624 unsigned char MD5digest[LUTIL_MD5_BYTES];
626 unsigned char *orig_pass = NULL;
629 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
633 /* base64 un-encode password */
634 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
635 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
637 if( orig_pass == NULL ) return -1;
639 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
640 if ( rc != sizeof(MD5digest) ) {
641 ber_memfree(orig_pass);
645 /* hash credentials with salt */
646 lutil_MD5Init(&MD5context);
647 lutil_MD5Update(&MD5context,
648 (const unsigned char *) cred->bv_val,
650 lutil_MD5Final(MD5digest, &MD5context);
653 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
654 ber_memfree(orig_pass);
659 /* pseudocode from RFC2433
660 * A.2 LmPasswordHash()
663 * IN 0-to-14-oem-char Password,
664 * OUT 16-octet PasswordHash )
666 * Set UcasePassword to the uppercased Password
667 * Zero pad UcasePassword to 14 characters
669 * DesHash( 1st 7-octets of UcasePassword,
670 * giving 1st 8-octets of PasswordHash )
672 * DesHash( 2nd 7-octets of UcasePassword,
673 * giving 2nd 8-octets of PasswordHash )
681 * OUT 8-octet Cypher )
684 * * Make Cypher an irreversibly encrypted form of Clear by
685 * * encrypting known text using Clear as the secret key.
686 * * The known text consists of the string
691 * Set StdText to "KGS!@#$%"
692 * DesEncrypt( StdText, Clear, giving Cypher )
701 * OUT 8-octet Cypher )
704 * * Use the DES encryption algorithm [4] in ECB mode [9]
705 * * to encrypt Clear into Cypher such that Cypher can
706 * * only be decrypted back to Clear by providing Key.
707 * * Note that the DES algorithm takes as input a 64-bit
708 * * stream where the 8th, 16th, 24th, etc. bits are
709 * * parity bits ignored by the encrypting algorithm.
710 * * Unless you write your own DES to accept 56-bit input
711 * * without parity, you will need to insert the parity bits
717 static void lmPasswd_to_key(
718 const unsigned char *lmPasswd,
721 /* make room for parity bits */
722 ((char *)key)[0] = lmPasswd[0];
723 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
724 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
725 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
726 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
727 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
728 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
729 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
731 des_set_odd_parity( key );
734 static int chk_lanman(
735 const struct berval *scheme,
736 const struct berval *passwd,
737 const struct berval *cred,
741 char UcasePassword[15];
743 des_key_schedule schedule;
744 des_cblock StdText = "KGS!@#$%";
745 des_cblock PasswordHash1, PasswordHash2;
746 char PasswordHash[33], storedPasswordHash[33];
748 for( i=0; i<cred->bv_len; i++) {
749 if(cred->bv_val[i] == '\0') {
750 return -1; /* NUL character in password */
754 if( cred->bv_val[i] != '\0' ) {
755 return -1; /* passwd must behave like a string */
758 strncpy( UcasePassword, cred->bv_val, 14 );
759 UcasePassword[14] = '\0';
760 ldap_pvt_str2upper( UcasePassword );
762 lmPasswd_to_key( UcasePassword, &key );
763 des_set_key_unchecked( &key, schedule );
764 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
766 lmPasswd_to_key( &UcasePassword[7], &key );
767 des_set_key_unchecked( &key, schedule );
768 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
770 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
771 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
772 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
773 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
774 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
776 /* as a precaution convert stored password hash to lower case */
777 strncpy( storedPasswordHash, passwd->bv_val, 32 );
778 storedPasswordHash[32] = '\0';
779 ldap_pvt_str2lower( storedPasswordHash );
781 return memcmp( PasswordHash, storedPasswordHash, 32) ? 1 : 0;
783 #endif /* SLAPD_LMHASH */
785 #ifdef SLAPD_NS_MTA_MD5
786 static int chk_ns_mta_md5(
787 const struct berval *scheme,
788 const struct berval *passwd,
789 const struct berval *cred,
792 lutil_MD5_CTX MD5context;
793 unsigned char MD5digest[LUTIL_MD5_BYTES], c;
794 char buffer[LUTIL_MD5_BYTES*2];
797 if( passwd->bv_len != LUTIL_MD5_BYTES*2 ) {
801 /* hash credentials with salt */
802 lutil_MD5Init(&MD5context);
803 lutil_MD5Update(&MD5context,
804 (const unsigned char *) &passwd->bv_val[32],
808 lutil_MD5Update(&MD5context,
809 (const unsigned char *) &c,
812 lutil_MD5Update(&MD5context,
813 (const unsigned char *) cred->bv_val,
817 lutil_MD5Update(&MD5context,
818 (const unsigned char *) &c,
821 lutil_MD5Update(&MD5context,
822 (const unsigned char *) &passwd->bv_val[32],
825 lutil_MD5Final(MD5digest, &MD5context);
827 for( i=0; i < sizeof( MD5digest ); i++ ) {
828 buffer[i+i] = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F];
829 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F];
833 return memcmp((char *)passwd->bv_val,
834 (char *)buffer, sizeof(buffer)) ? 1 : 0;
839 #ifdef HAVE_CYRUS_SASL
840 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
844 const struct berval *sc,
845 const struct berval * passwd,
846 const struct berval * cred,
852 for( i=0; i<cred->bv_len; i++) {
853 if(cred->bv_val[i] == '\0') {
854 return 1; /* NUL character in password */
858 if( cred->bv_val[i] != '\0' ) {
859 return 1; /* cred must behave like a string */
862 for( i=0; i<passwd->bv_len; i++) {
863 if(passwd->bv_val[i] == '\0') {
864 return 1; /* NUL character in password */
868 if( passwd->bv_val[i] != '\0' ) {
869 return 1; /* passwd must behave like a string */
874 #ifdef HAVE_CYRUS_SASL
875 if( lutil_passwd_sasl_conn != NULL ) {
877 # if SASL_VERSION_MAJOR < 2
878 sc = sasl_checkpass( lutil_passwd_sasl_conn,
879 passwd->bv_val, passwd->bv_len,
880 cred->bv_val, cred->bv_len,
883 sc = sasl_checkpass( lutil_passwd_sasl_conn,
884 passwd->bv_val, passwd->bv_len,
885 cred->bv_val, cred->bv_len );
887 rtn = ( sc != SASL_OK );
896 static int chk_kerberos(
897 const struct berval *sc,
898 const struct berval * passwd,
899 const struct berval * cred,
905 for( i=0; i<cred->bv_len; i++) {
906 if(cred->bv_val[i] == '\0') {
907 return 1; /* NUL character in password */
911 if( cred->bv_val[i] != '\0' ) {
912 return 1; /* cred must behave like a string */
915 for( i=0; i<passwd->bv_len; i++) {
916 if(passwd->bv_val[i] == '\0') {
917 return 1; /* NUL character in password */
921 if( passwd->bv_val[i] != '\0' ) {
922 return 1; /* passwd must behave like a string */
927 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
930 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
931 * (Royal Institute of Technology, Stockholm, Sweden).
932 * All rights reserved.
934 * Redistribution and use in source and binary forms, with or without
935 * modification, are permitted provided that the following conditions
938 * 1. Redistributions of source code must retain the above copyright
939 * notice, this list of conditions and the following disclaimer.
941 * 2. Redistributions in binary form must reproduce the above copyright
942 * notice, this list of conditions and the following disclaimer in the
943 * documentation and/or other materials provided with the distribution.
945 * 3. Neither the name of the Institute nor the names of its contributors
946 * may be used to endorse or promote products derived from this software
947 * without specific prior written permission.
949 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
950 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
951 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
952 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
953 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
954 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
955 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
956 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
957 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
958 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
962 krb5_context context;
965 krb5_get_init_creds_opt get_options;
966 krb5_verify_init_creds_opt verify_options;
967 krb5_principal client, server;
969 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
972 ret = krb5_init_context( &context );
978 krb5_get_init_creds_opt_set_preauth_list(&get_options,
982 krb5_get_init_creds_opt_init( &get_options );
984 krb5_verify_init_creds_opt_init( &verify_options );
986 ret = krb5_parse_name( context, passwd->bv_val, &client );
989 krb5_free_context( context );
993 ret = krb5_get_init_creds_password( context,
994 &creds, client, cred->bv_val, NULL,
995 NULL, 0, NULL, &get_options );
998 krb5_free_principal( context, client );
999 krb5_free_context( context );
1004 char *host = ldap_pvt_get_fqdn( NULL );
1006 if( host == NULL ) {
1007 krb5_free_principal( context, client );
1008 krb5_free_context( context );
1012 ret = krb5_sname_to_principal( context,
1013 host, "ldap", KRB5_NT_SRV_HST, &server );
1015 ber_memfree( host );
1019 krb5_free_principal( context, client );
1020 krb5_free_context( context );
1024 ret = krb5_verify_init_creds( context,
1025 &creds, server, NULL, NULL, &verify_options );
1027 krb5_free_principal( context, client );
1028 krb5_free_principal( context, server );
1029 krb5_free_cred_contents( context, &creds );
1030 krb5_free_context( context );
1034 #elif defined(HAVE_KRB4)
1036 /* Borrowed from Heimdal kpopper */
1038 * Copyright (c) 1989 Regents of the University of California.
1039 * All rights reserved. The Berkeley software License Agreement
1040 * specifies the terms and conditions for redistribution.
1044 char lrealm[REALM_SZ];
1045 char tkt[MAXHOSTNAMELEN];
1047 status = krb_get_lrealm(lrealm,1);
1048 if (status == KFAILURE) {
1052 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
1053 TKT_ROOT, (unsigned)getpid());
1054 krb_set_tkt_string (tkt);
1056 status = krb_verify_user( passwd->bv_val, "", lrealm,
1057 cred->bv_val, 1, "ldap");
1059 dest_tkt(); /* no point in keeping the tickets */
1061 return status == KFAILURE;
1067 #endif /* SLAPD_KPASSWD */
1070 static int chk_crypt(
1071 const struct berval *sc,
1072 const struct berval * passwd,
1073 const struct berval * cred,
1079 for( i=0; i<cred->bv_len; i++) {
1080 if(cred->bv_val[i] == '\0') {
1081 return 1; /* NUL character in password */
1085 if( cred->bv_val[i] != '\0' ) {
1086 return -1; /* cred must behave like a string */
1089 if( passwd->bv_len < 2 ) {
1090 return -1; /* passwd must be at least two characters long */
1093 for( i=0; i<passwd->bv_len; i++) {
1094 if(passwd->bv_val[i] == '\0') {
1095 return -1; /* NUL character in password */
1099 if( passwd->bv_val[i] != '\0' ) {
1100 return -1; /* passwd must behave like a string */
1103 cr = crypt( cred->bv_val, passwd->bv_val );
1105 if( cr == NULL || cr[0] == '\0' ) {
1106 /* salt must have been invalid */
1110 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
1113 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
1114 static int chk_unix(
1115 const struct berval *sc,
1116 const struct berval * passwd,
1117 const struct berval * cred,
1123 for( i=0; i<cred->bv_len; i++) {
1124 if(cred->bv_val[i] == '\0') {
1125 return -1; /* NUL character in password */
1128 if( cred->bv_val[i] != '\0' ) {
1129 return -1; /* cred must behave like a string */
1132 for( i=0; i<passwd->bv_len; i++) {
1133 if(passwd->bv_val[i] == '\0') {
1134 return -1; /* NUL character in password */
1138 if( passwd->bv_val[i] != '\0' ) {
1139 return -1; /* passwd must behave like a string */
1143 struct passwd *pwd = getpwnam(passwd->bv_val);
1146 return -1; /* not found */
1149 pw = pwd->pw_passwd;
1151 # ifdef HAVE_GETSPNAM
1153 struct spwd *spwd = getspnam(passwd->bv_val);
1160 # ifdef HAVE_AIX_SECURITY
1162 struct userpw *upw = getuserpw(passwd->bv_val);
1165 pw = upw->upw_passwd;
1170 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1171 /* password must must be at least two characters long */
1175 cr = crypt(cred->bv_val, pw);
1177 if( cr == NULL || cr[0] == '\0' ) {
1178 /* salt must have been invalid */
1182 return strcmp(pw, cr) ? 1 : 0;
1188 /* PASSWORD GENERATION ROUTINES */
1190 #ifdef LUTIL_SHA1_BYTES
1191 static struct berval *hash_ssha1(
1192 const struct berval *scheme,
1193 const struct berval *passwd,
1196 lutil_SHA1_CTX SHA1context;
1197 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1198 char saltdata[SALT_SIZE];
1199 struct berval digest;
1202 digest.bv_val = (char *) SHA1digest;
1203 digest.bv_len = sizeof(SHA1digest);
1204 salt.bv_val = saltdata;
1205 salt.bv_len = sizeof(saltdata);
1207 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1211 lutil_SHA1Init( &SHA1context );
1212 lutil_SHA1Update( &SHA1context,
1213 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1214 lutil_SHA1Update( &SHA1context,
1215 (const unsigned char *)salt.bv_val, salt.bv_len );
1216 lutil_SHA1Final( SHA1digest, &SHA1context );
1218 return pw_string64( scheme, &digest, &salt);
1221 static struct berval *hash_sha1(
1222 const struct berval *scheme,
1223 const struct berval *passwd,
1226 lutil_SHA1_CTX SHA1context;
1227 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1228 struct berval digest;
1229 digest.bv_val = (char *) SHA1digest;
1230 digest.bv_len = sizeof(SHA1digest);
1232 lutil_SHA1Init( &SHA1context );
1233 lutil_SHA1Update( &SHA1context,
1234 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1235 lutil_SHA1Final( SHA1digest, &SHA1context );
1237 return pw_string64( scheme, &digest, NULL);
1241 static struct berval *hash_smd5(
1242 const struct berval *scheme,
1243 const struct berval *passwd,
1246 lutil_MD5_CTX MD5context;
1247 unsigned char MD5digest[LUTIL_MD5_BYTES];
1248 char saltdata[SALT_SIZE];
1249 struct berval digest;
1252 digest.bv_val = (char *) MD5digest;
1253 digest.bv_len = sizeof(MD5digest);
1254 salt.bv_val = saltdata;
1255 salt.bv_len = sizeof(saltdata);
1257 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1261 lutil_MD5Init( &MD5context );
1262 lutil_MD5Update( &MD5context,
1263 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1264 lutil_MD5Update( &MD5context,
1265 (const unsigned char *) salt.bv_val, salt.bv_len );
1266 lutil_MD5Final( MD5digest, &MD5context );
1268 return pw_string64( scheme, &digest, &salt );
1271 static struct berval *hash_md5(
1272 const struct berval *scheme,
1273 const struct berval *passwd,
1276 lutil_MD5_CTX MD5context;
1277 unsigned char MD5digest[LUTIL_MD5_BYTES];
1279 struct berval digest;
1281 digest.bv_val = (char *) MD5digest;
1282 digest.bv_len = sizeof(MD5digest);
1284 lutil_MD5Init( &MD5context );
1285 lutil_MD5Update( &MD5context,
1286 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1287 lutil_MD5Final( MD5digest, &MD5context );
1289 return pw_string64( scheme, &digest, NULL );
1294 static struct berval *hash_lanman(
1295 const struct berval *scheme,
1296 const struct berval *passwd,
1301 char UcasePassword[15];
1303 des_key_schedule schedule;
1304 des_cblock StdText = "KGS!@#$%";
1305 des_cblock PasswordHash1, PasswordHash2;
1306 char PasswordHash[33];
1309 for( i=0; i<passwd->bv_len; i++) {
1310 if(passwd->bv_val[i] == '\0') {
1311 return NULL; /* NUL character in password */
1315 if( passwd->bv_val[i] != '\0' ) {
1316 return NULL; /* passwd must behave like a string */
1319 strncpy( UcasePassword, passwd->bv_val, 14 );
1320 UcasePassword[14] = '\0';
1321 ldap_pvt_str2upper( UcasePassword );
1323 lmPasswd_to_key( UcasePassword, &key );
1324 des_set_key_unchecked( &key, schedule );
1325 des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
1327 lmPasswd_to_key( &UcasePassword[7], &key );
1328 des_set_key_unchecked( &key, schedule );
1329 des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
1331 sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1332 PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
1333 PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
1334 PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
1335 PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
1337 hash.bv_val = PasswordHash;
1340 return pw_string( scheme, &hash );
1342 #endif /* SLAPD_LMHASH */
1345 static struct berval *hash_crypt(
1346 const struct berval *scheme,
1347 const struct berval *passwd,
1351 unsigned char salt[32]; /* salt suitable for most anything */
1354 for( i=0; i<passwd->bv_len; i++) {
1355 if(passwd->bv_val[i] == '\0') {
1356 return NULL; /* NUL character in password */
1360 if( passwd->bv_val[i] != '\0' ) {
1361 return NULL; /* passwd must behave like a string */
1364 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1368 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1369 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1371 salt[sizeof( salt ) - 1 ] = '\0';
1373 if( salt_format != NULL ) {
1374 /* copy the salt we made into entropy before snprintfing
1375 it back into the salt */
1376 char entropy[sizeof(salt)];
1377 strcpy( entropy, (char *) salt );
1378 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1381 hash.bv_val = crypt( passwd->bv_val, (char *) salt );
1383 if( hash.bv_val == NULL ) return NULL;
1385 hash.bv_len = strlen( hash.bv_val );
1387 if( hash.bv_len == 0 ) {
1391 return pw_string( scheme, &hash );
1395 int lutil_salt_format(const char *format)
1398 free( salt_format );
1400 salt_format = format != NULL ? strdup( format ) : NULL;
1406 #ifdef SLAPD_CLEARTEXT
1407 static struct berval *hash_clear(
1408 const struct berval *scheme,
1409 const struct berval *passwd,
1412 return ber_bvdup( (struct berval *) passwd );