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 typedef int (*PASSWD_CHK_FUNC)(
84 const struct pw_scheme *scheme,
85 const struct berval *passwd,
86 const struct berval *cred );
88 typedef struct berval * (*PASSWD_HASH_FUNC) (
89 const struct pw_scheme *scheme,
90 const struct berval *passwd );
94 PASSWD_CHK_FUNC chk_fn;
95 PASSWD_HASH_FUNC hash_fn;
98 /* password check routines */
100 const struct pw_scheme *scheme,
101 const struct berval *passwd,
102 const struct berval *cred );
105 const struct pw_scheme *scheme,
106 const struct berval *passwd,
107 const struct berval *cred );
109 #ifdef LUTIL_SHA1_BYTES
110 static int chk_ssha1(
111 const struct pw_scheme *scheme,
112 const struct berval *passwd,
113 const struct berval *cred );
116 const struct pw_scheme *scheme,
117 const struct berval *passwd,
118 const struct berval *cred );
122 static int chk_lanman(
123 const struct pw_scheme *scheme,
124 const struct berval *passwd,
125 const struct berval *cred );
128 #ifdef SLAPD_NS_MTA_MD5
129 static int chk_ns_mta_md5(
130 const struct pw_scheme *scheme,
131 const struct berval *passwd,
132 const struct berval *cred );
137 const struct pw_scheme *scheme,
138 const struct berval *passwd,
139 const struct berval *cred );
143 static int chk_kerberos(
144 const struct pw_scheme *scheme,
145 const struct berval *passwd,
146 const struct berval *cred );
150 static int chk_crypt(
151 const struct pw_scheme *scheme,
152 const struct berval *passwd,
153 const struct berval *cred );
155 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
157 const struct pw_scheme *scheme,
158 const struct berval *passwd,
159 const struct berval *cred );
164 #ifdef LUTIL_SHA1_BYTES
165 /* password hash routines */
166 static struct berval *hash_sha1(
167 const struct pw_scheme *scheme,
168 const struct berval *passwd );
170 static struct berval *hash_ssha1(
171 const struct pw_scheme *scheme,
172 const struct berval *passwd );
175 static struct berval *hash_smd5(
176 const struct pw_scheme *scheme,
177 const struct berval *passwd );
179 static struct berval *hash_md5(
180 const struct pw_scheme *scheme,
181 const struct berval *passwd );
184 static struct berval *hash_lanman(
185 const struct pw_scheme *scheme,
186 const struct berval *passwd );
190 static struct berval *hash_crypt(
191 const struct pw_scheme *scheme,
192 const struct berval *passwd );
195 #ifdef SLAPD_CLEARTEXT
196 static struct berval *hash_clear(
197 const struct pw_scheme *scheme,
198 const struct berval *passwd );
201 static const struct pw_scheme pw_schemes[] =
203 #ifdef LUTIL_SHA1_BYTES
204 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
205 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
208 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
209 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
212 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
213 #endif /* SLAPD_LMHASH */
215 #ifdef SLAPD_NS_MTA_MD5
216 { BER_BVC("{NS-MTA-MD5}"), chk_ns_mta_md5, NULL },
217 #endif /* SLAPD_NS_MTA_MD5 */
220 { BER_BVC("{SASL}"), chk_sasl, NULL },
224 { BER_BVC("{KERBEROS}"), chk_kerberos, NULL },
228 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
229 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
230 { BER_BVC("{UNIX}"), chk_unix, NULL },
234 #ifdef SLAPD_CLEARTEXT
236 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
239 { BER_BVNULL, NULL, NULL }
242 static const struct pw_scheme *get_scheme(
247 for( i=0; pw_schemes[i].name.bv_val; i++) {
248 if( pw_schemes[i].name.bv_val == NULL ) continue;
250 if( strcasecmp(scheme, pw_schemes[i].name.bv_val ) == 0 ) {
251 return &pw_schemes[i];
258 int lutil_passwd_scheme(
261 if( scheme == NULL ) {
265 return get_scheme(scheme) != NULL;
269 static int is_allowed_scheme(
271 const char** schemes )
275 if( schemes == NULL ) return 1;
277 for( i=0; schemes[i] != NULL; i++ ) {
278 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
285 static struct berval *passwd_scheme(
286 const struct pw_scheme *scheme,
287 const struct berval * passwd,
289 const char** allowed )
291 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
295 if( passwd->bv_len >= scheme->name.bv_len ) {
296 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
297 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
298 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
308 * Return 0 if creds are good.
312 const struct berval *passwd, /* stored passwd */
313 const struct berval *cred, /* user cred */
314 const char **schemes )
318 if (cred == NULL || cred->bv_len == 0 ||
319 passwd == NULL || passwd->bv_len == 0 )
324 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
325 if( pw_schemes[i].chk_fn ) {
327 struct berval *p = passwd_scheme( &pw_schemes[i],
328 passwd, &x, schemes );
331 return (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
336 #ifdef SLAPD_CLEARTEXT
337 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
338 return (( passwd->bv_len == cred->bv_len ) &&
339 ( passwd->bv_val[0] != '{' /*'}'*/ ))
340 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
347 struct berval * lutil_passwd_generate( ber_len_t len )
351 if( len < 1 ) return NULL;
353 pw = ber_memalloc( sizeof( struct berval ) );
354 if( pw == NULL ) return NULL;
357 pw->bv_val = ber_memalloc( len + 1 );
359 if( pw->bv_val == NULL ) {
364 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
369 for( len = 0; len < pw->bv_len; len++ ) {
370 pw->bv_val[len] = crypt64[
371 pw->bv_val[len] % (sizeof(crypt64)-1) ];
374 pw->bv_val[len] = '\0';
379 struct berval * lutil_passwd_hash(
380 const struct berval * passwd,
381 const char * method )
383 const struct pw_scheme *sc = get_scheme( method );
385 if( sc == NULL ) return NULL;
386 if( ! sc->hash_fn ) return NULL;
388 return (sc->hash_fn)( sc, passwd );
391 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
392 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
393 static struct berval * pw_string(
394 const struct pw_scheme *sc,
395 const struct berval *passwd )
397 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
398 if( pw == NULL ) return NULL;
400 pw->bv_len = sc->name.bv_len + passwd->bv_len;
401 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
403 if( pw->bv_val == NULL ) {
408 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
409 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
411 pw->bv_val[pw->bv_len] = '\0';
414 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
416 static struct berval * pw_string64(
417 const struct pw_scheme *sc,
418 const struct berval *hash,
419 const struct berval *salt )
422 struct berval string;
423 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
426 if( b64 == NULL ) return NULL;
429 /* need to base64 combined string */
430 string.bv_len = hash->bv_len + salt->bv_len;
431 string.bv_val = ber_memalloc( string.bv_len + 1 );
433 if( string.bv_val == NULL ) {
438 AC_MEMCPY( string.bv_val, hash->bv_val,
440 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
442 string.bv_val[string.bv_len] = '\0';
448 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
449 b64->bv_len = b64len + sc->name.bv_len;
450 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
452 if( b64->bv_val == NULL ) {
453 if( salt ) ber_memfree( string.bv_val );
458 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
461 string.bv_val, string.bv_len,
462 &b64->bv_val[sc->name.bv_len], b64len );
464 if( salt ) ber_memfree( string.bv_val );
471 /* recompute length */
472 b64->bv_len = sc->name.bv_len + rc;
473 assert( strlen(b64->bv_val) == b64->bv_len );
477 /* PASSWORD CHECK ROUTINES */
479 #ifdef LUTIL_SHA1_BYTES
480 static int chk_ssha1(
481 const struct pw_scheme *sc,
482 const struct berval * passwd,
483 const struct berval * cred )
485 lutil_SHA1_CTX SHA1context;
486 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
488 unsigned char *orig_pass = NULL;
491 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
495 /* decode base64 password */
496 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
497 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
499 if( orig_pass == NULL ) return -1;
501 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
503 if (rc <= sizeof(SHA1digest)) {
504 ber_memfree(orig_pass);
508 /* hash credentials with salt */
509 lutil_SHA1Init(&SHA1context);
510 lutil_SHA1Update(&SHA1context,
511 (const unsigned char *) cred->bv_val, cred->bv_len);
512 lutil_SHA1Update(&SHA1context,
513 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
514 rc - sizeof(SHA1digest));
515 lutil_SHA1Final(SHA1digest, &SHA1context);
518 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
519 ber_memfree(orig_pass);
524 const struct pw_scheme *sc,
525 const struct berval * passwd,
526 const struct berval * cred )
528 lutil_SHA1_CTX SHA1context;
529 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
531 unsigned char *orig_pass = NULL;
533 /* base64 un-encode password */
534 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
535 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
537 if( orig_pass == NULL ) return -1;
539 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
541 if( rc != sizeof(SHA1digest) ) {
542 ber_memfree(orig_pass);
546 /* hash credentials with salt */
547 lutil_SHA1Init(&SHA1context);
548 lutil_SHA1Update(&SHA1context,
549 (const unsigned char *) cred->bv_val, cred->bv_len);
550 lutil_SHA1Final(SHA1digest, &SHA1context);
553 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
554 ber_memfree(orig_pass);
560 const struct pw_scheme *sc,
561 const struct berval * passwd,
562 const struct berval * cred )
564 lutil_MD5_CTX MD5context;
565 unsigned char MD5digest[LUTIL_MD5_BYTES];
567 unsigned char *orig_pass = NULL;
570 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
574 /* base64 un-encode password */
575 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
576 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
578 if( orig_pass == NULL ) return -1;
580 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
582 if (rc <= sizeof(MD5digest)) {
583 ber_memfree(orig_pass);
587 /* hash credentials with salt */
588 lutil_MD5Init(&MD5context);
589 lutil_MD5Update(&MD5context,
590 (const unsigned char *) cred->bv_val,
592 lutil_MD5Update(&MD5context,
593 &orig_pass[sizeof(MD5digest)],
594 rc - sizeof(MD5digest));
595 lutil_MD5Final(MD5digest, &MD5context);
598 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
599 ber_memfree(orig_pass);
604 const struct pw_scheme *sc,
605 const struct berval * passwd,
606 const struct berval * cred )
608 lutil_MD5_CTX MD5context;
609 unsigned char MD5digest[LUTIL_MD5_BYTES];
611 unsigned char *orig_pass = NULL;
613 /* base64 un-encode password */
614 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
615 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
617 if( orig_pass == NULL ) return -1;
619 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
620 if ( rc != sizeof(MD5digest) ) {
621 ber_memfree(orig_pass);
625 /* hash credentials with salt */
626 lutil_MD5Init(&MD5context);
627 lutil_MD5Update(&MD5context,
628 (const unsigned char *) cred->bv_val,
630 lutil_MD5Final(MD5digest, &MD5context);
633 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
634 ber_memfree(orig_pass);
639 static int chk_lanman(
640 const struct pw_scheme *scheme,
641 const struct berval *passwd,
642 const struct berval *cred )
646 hash = hash_lanman( scheme, cred );
647 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
649 #endif /* SLAPD_LMHASH */
651 #ifdef SLAPD_NS_MTA_MD5
652 static int chk_ns_mta_md5(
653 const struct pw_scheme *scheme,
654 const struct berval *passwd,
655 const struct berval *cred )
657 lutil_MD5_CTX MD5context;
658 unsigned char MD5digest[LUTIL_MD5_BYTES], c;
659 char buffer[LUTIL_MD5_BYTES + LUTIL_MD5_BYTES + 1];
662 /* hash credentials with salt */
663 lutil_MD5Init(&MD5context);
664 lutil_MD5Update(&MD5context,
665 (const unsigned char *) &passwd->bv_val[32],
669 lutil_MD5Update(&MD5context,
670 (const unsigned char *) &c,
673 lutil_MD5Update(&MD5context,
674 (const unsigned char *) cred->bv_val,
678 lutil_MD5Update(&MD5context,
679 (const unsigned char *) &c,
682 lutil_MD5Update(&MD5context,
683 (const unsigned char *) &passwd->bv_val[32],
686 lutil_MD5Final(MD5digest, &MD5context);
688 for( i=0; i < sizeof( MD5digest ); i++ ) {
689 buffer[i+i] = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F];
690 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F];
694 return memcmp((char *)passwd->bv_val, (char *)buffer, sizeof(buffer))
700 #ifdef HAVE_CYRUS_SASL
701 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
705 const struct pw_scheme *sc,
706 const struct berval * passwd,
707 const struct berval * cred )
712 for( i=0; i<cred->bv_len; i++) {
713 if(cred->bv_val[i] == '\0') {
714 return 1; /* NUL character in password */
718 if( cred->bv_val[i] != '\0' ) {
719 return 1; /* cred must behave like a string */
722 for( i=0; i<passwd->bv_len; i++) {
723 if(passwd->bv_val[i] == '\0') {
724 return 1; /* NUL character in password */
728 if( passwd->bv_val[i] != '\0' ) {
729 return 1; /* passwd must behave like a string */
734 #ifdef HAVE_CYRUS_SASL
735 if( lutil_passwd_sasl_conn != NULL ) {
737 # if SASL_VERSION_MAJOR < 2
738 const char *errstr = NULL;
739 sc = sasl_checkpass( lutil_passwd_sasl_conn,
740 passwd->bv_val, passwd->bv_len,
741 cred->bv_val, cred->bv_len,
744 sc = sasl_checkpass( lutil_passwd_sasl_conn,
745 passwd->bv_val, passwd->bv_len,
746 cred->bv_val, cred->bv_len );
748 rtn = ( sc != SASL_OK );
757 static int chk_kerberos(
758 const struct pw_scheme *sc,
759 const struct berval * passwd,
760 const struct berval * cred )
765 for( i=0; i<cred->bv_len; i++) {
766 if(cred->bv_val[i] == '\0') {
767 return 1; /* NUL character in password */
771 if( cred->bv_val[i] != '\0' ) {
772 return 1; /* cred must behave like a string */
775 for( i=0; i<passwd->bv_len; i++) {
776 if(passwd->bv_val[i] == '\0') {
777 return 1; /* NUL character in password */
781 if( passwd->bv_val[i] != '\0' ) {
782 return 1; /* passwd must behave like a string */
787 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
790 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
791 * (Royal Institute of Technology, Stockholm, Sweden).
792 * All rights reserved.
794 * Redistribution and use in source and binary forms, with or without
795 * modification, are permitted provided that the following conditions
798 * 1. Redistributions of source code must retain the above copyright
799 * notice, this list of conditions and the following disclaimer.
801 * 2. Redistributions in binary form must reproduce the above copyright
802 * notice, this list of conditions and the following disclaimer in the
803 * documentation and/or other materials provided with the distribution.
805 * 3. Neither the name of the Institute nor the names of its contributors
806 * may be used to endorse or promote products derived from this software
807 * without specific prior written permission.
809 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
810 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
811 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
812 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
813 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
814 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
815 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
816 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
817 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
818 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
822 krb5_context context;
825 krb5_get_init_creds_opt get_options;
826 krb5_verify_init_creds_opt verify_options;
827 krb5_principal client, server;
829 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
832 ret = krb5_init_context( &context );
838 krb5_get_init_creds_opt_set_preauth_list(&get_options,
842 krb5_get_init_creds_opt_init( &get_options );
844 krb5_verify_init_creds_opt_init( &verify_options );
846 ret = krb5_parse_name( context, passwd->bv_val, &client );
849 krb5_free_context( context );
853 ret = krb5_get_init_creds_password( context,
854 &creds, client, cred->bv_val, NULL,
855 NULL, 0, NULL, &get_options );
858 krb5_free_principal( context, client );
859 krb5_free_context( context );
864 char *host = ldap_pvt_get_fqdn( NULL );
867 krb5_free_principal( context, client );
868 krb5_free_context( context );
872 ret = krb5_sname_to_principal( context,
873 host, "ldap", KRB5_NT_SRV_HST, &server );
879 krb5_free_principal( context, client );
880 krb5_free_context( context );
884 ret = krb5_verify_init_creds( context,
885 &creds, server, NULL, NULL, &verify_options );
887 krb5_free_principal( context, client );
888 krb5_free_principal( context, server );
889 krb5_free_cred_contents( context, &creds );
890 krb5_free_context( context );
894 #elif defined(HAVE_KRB4)
896 /* Borrowed from Heimdal kpopper */
898 * Copyright (c) 1989 Regents of the University of California.
899 * All rights reserved. The Berkeley software License Agreement
900 * specifies the terms and conditions for redistribution.
904 char lrealm[REALM_SZ];
905 char tkt[MAXHOSTNAMELEN];
907 status = krb_get_lrealm(lrealm,1);
908 if (status == KFAILURE) {
912 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
913 TKT_ROOT, (unsigned)getpid());
914 krb_set_tkt_string (tkt);
916 status = krb_verify_user( passwd->bv_val, "", lrealm,
917 cred->bv_val, 1, "ldap");
919 dest_tkt(); /* no point in keeping the tickets */
921 return status == KFAILURE;
927 #endif /* SLAPD_KPASSWD */
930 static int chk_crypt(
931 const struct pw_scheme *sc,
932 const struct berval * passwd,
933 const struct berval * cred )
938 for( i=0; i<cred->bv_len; i++) {
939 if(cred->bv_val[i] == '\0') {
940 return 1; /* NUL character in password */
944 if( cred->bv_val[i] != '\0' ) {
945 return -1; /* cred must behave like a string */
948 if( passwd->bv_len < 2 ) {
949 return -1; /* passwd must be at least two characters long */
952 for( i=0; i<passwd->bv_len; i++) {
953 if(passwd->bv_val[i] == '\0') {
954 return -1; /* NUL character in password */
958 if( passwd->bv_val[i] != '\0' ) {
959 return -1; /* passwd must behave like a string */
962 cr = crypt( cred->bv_val, passwd->bv_val );
964 if( cr == NULL || cr[0] == '\0' ) {
965 /* salt must have been invalid */
969 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
972 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
974 const struct pw_scheme *sc,
975 const struct berval * passwd,
976 const struct berval * cred )
981 for( i=0; i<cred->bv_len; i++) {
982 if(cred->bv_val[i] == '\0') {
983 return -1; /* NUL character in password */
986 if( cred->bv_val[i] != '\0' ) {
987 return -1; /* cred must behave like a string */
990 for( i=0; i<passwd->bv_len; i++) {
991 if(passwd->bv_val[i] == '\0') {
992 return -1; /* NUL character in password */
996 if( passwd->bv_val[i] != '\0' ) {
997 return -1; /* passwd must behave like a string */
1001 struct passwd *pwd = getpwnam(passwd->bv_val);
1004 return -1; /* not found */
1007 pw = pwd->pw_passwd;
1009 # ifdef HAVE_GETSPNAM
1011 struct spwd *spwd = getspnam(passwd->bv_val);
1018 # ifdef HAVE_AIX_SECURITY
1020 struct userpw *upw = getuserpw(passwd->bv_val);
1023 pw = upw->upw_passwd;
1028 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1029 /* password must must be at least two characters long */
1033 cr = crypt(cred->bv_val, pw);
1035 if( cr == NULL || cr[0] == '\0' ) {
1036 /* salt must have been invalid */
1040 return strcmp(pw, cr) ? 1 : 0;
1046 /* PASSWORD GENERATION ROUTINES */
1048 #ifdef LUTIL_SHA1_BYTES
1049 static struct berval *hash_ssha1(
1050 const struct pw_scheme *scheme,
1051 const struct berval *passwd )
1053 lutil_SHA1_CTX SHA1context;
1054 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1055 unsigned char saltdata[4];
1056 struct berval digest;
1059 digest.bv_val = SHA1digest;
1060 digest.bv_len = sizeof(SHA1digest);
1061 salt.bv_val = saltdata;
1062 salt.bv_len = sizeof(saltdata);
1064 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1068 lutil_SHA1Init( &SHA1context );
1069 lutil_SHA1Update( &SHA1context,
1070 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1071 lutil_SHA1Update( &SHA1context,
1072 (const unsigned char *)salt.bv_val, salt.bv_len );
1073 lutil_SHA1Final( SHA1digest, &SHA1context );
1075 return pw_string64( scheme, &digest, &salt);
1078 static struct berval *hash_sha1(
1079 const struct pw_scheme *scheme,
1080 const struct berval *passwd )
1082 lutil_SHA1_CTX SHA1context;
1083 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1084 struct berval digest;
1085 digest.bv_val = SHA1digest;
1086 digest.bv_len = sizeof(SHA1digest);
1088 lutil_SHA1Init( &SHA1context );
1089 lutil_SHA1Update( &SHA1context,
1090 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1091 lutil_SHA1Final( SHA1digest, &SHA1context );
1093 return pw_string64( scheme, &digest, NULL);
1097 static struct berval *hash_smd5(
1098 const struct pw_scheme *scheme,
1099 const struct berval *passwd )
1101 lutil_MD5_CTX MD5context;
1102 unsigned char MD5digest[LUTIL_MD5_BYTES];
1103 unsigned char saltdata[4];
1104 struct berval digest;
1107 digest.bv_val = MD5digest;
1108 digest.bv_len = sizeof(MD5digest);
1109 salt.bv_val = saltdata;
1110 salt.bv_len = sizeof(saltdata);
1112 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1116 lutil_MD5Init( &MD5context );
1117 lutil_MD5Update( &MD5context,
1118 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1119 lutil_MD5Update( &MD5context,
1120 (const unsigned char *) salt.bv_val, salt.bv_len );
1121 lutil_MD5Final( MD5digest, &MD5context );
1123 return pw_string64( scheme, &digest, &salt );
1126 static struct berval *hash_md5(
1127 const struct pw_scheme *scheme,
1128 const struct berval *passwd )
1130 lutil_MD5_CTX MD5context;
1131 unsigned char MD5digest[LUTIL_MD5_BYTES];
1133 struct berval digest;
1135 digest.bv_val = MD5digest;
1136 digest.bv_len = sizeof(MD5digest);
1138 lutil_MD5Init( &MD5context );
1139 lutil_MD5Update( &MD5context,
1140 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1141 lutil_MD5Final( MD5digest, &MD5context );
1143 return pw_string64( scheme, &digest, NULL );
1148 /* pseudocode from RFC2433
1149 * A.2 LmPasswordHash()
1152 * IN 0-to-14-oem-char Password,
1153 * OUT 16-octet PasswordHash )
1155 * Set UcasePassword to the uppercased Password
1156 * Zero pad UcasePassword to 14 characters
1158 * DesHash( 1st 7-octets of UcasePassword,
1159 * giving 1st 8-octets of PasswordHash )
1161 * DesHash( 2nd 7-octets of UcasePassword,
1162 * giving 2nd 8-octets of PasswordHash )
1170 * OUT 8-octet Cypher )
1173 * * Make Cypher an irreversibly encrypted form of Clear by
1174 * * encrypting known text using Clear as the secret key.
1175 * * The known text consists of the string
1180 * Set StdText to "KGS!@#$%"
1181 * DesEncrypt( StdText, Clear, giving Cypher )
1190 * OUT 8-octet Cypher )
1193 * * Use the DES encryption algorithm [4] in ECB mode [9]
1194 * * to encrypt Clear into Cypher such that Cypher can
1195 * * only be decrypted back to Clear by providing Key.
1196 * * Note that the DES algorithm takes as input a 64-bit
1197 * * stream where the 8th, 16th, 24th, etc. bits are
1198 * * parity bits ignored by the encrypting algorithm.
1199 * * Unless you write your own DES to accept 56-bit input
1200 * * without parity, you will need to insert the parity bits
1206 static void lmPasswd_to_key(
1207 const unsigned char *lmPasswd,
1210 /* make room for parity bits */
1211 ((char *)key)[0] = lmPasswd[0];
1212 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1213 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1214 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1215 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1216 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1217 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1218 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1220 des_set_odd_parity( key );
1223 static struct berval *hash_lanman(
1224 const struct pw_scheme *scheme,
1225 const struct berval *passwd )
1229 char UcasePassword[15];
1231 des_key_schedule schedule;
1232 des_cblock StdText = "KGS!@#$%";
1233 des_cblock hash1, hash2;
1237 for( i=0; i<passwd->bv_len; i++) {
1238 if(passwd->bv_val[i] == '\0') {
1239 return NULL; /* NUL character in password */
1243 if( passwd->bv_val[i] != '\0' ) {
1244 return NULL; /* passwd must behave like a string */
1247 strncpy( UcasePassword, passwd->bv_val, 14 );
1248 UcasePassword[14] = '\0';
1249 ldap_pvt_str2upper( UcasePassword );
1251 lmPasswd_to_key( UcasePassword, &key );
1252 des_set_key_unchecked( &key, schedule );
1253 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1255 lmPasswd_to_key( &UcasePassword[7], &key );
1256 des_set_key_unchecked( &key, schedule );
1257 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1259 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1260 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1261 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1263 hash.bv_val = lmhash;
1266 return pw_string( scheme, &hash );
1268 #endif /* SLAPD_LMHASH */
1271 static struct berval *hash_crypt(
1272 const struct pw_scheme *scheme,
1273 const struct berval *passwd )
1276 unsigned char salt[32]; /* salt suitable for most anything */
1279 for( i=0; i<passwd->bv_len; i++) {
1280 if(passwd->bv_val[i] == '\0') {
1281 return NULL; /* NUL character in password */
1285 if( passwd->bv_val[i] != '\0' ) {
1286 return NULL; /* passwd must behave like a string */
1289 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1293 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1294 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1296 salt[sizeof( salt ) - 1 ] = '\0';
1298 if( salt_format != NULL ) {
1299 /* copy the salt we made into entropy before snprintfing
1300 it back into the salt */
1301 char entropy[sizeof(salt)];
1302 strcpy( entropy, salt );
1303 snprintf( salt, sizeof(entropy), salt_format, entropy );
1306 hash.bv_val = crypt( passwd->bv_val, salt );
1308 if( hash.bv_val == NULL ) return NULL;
1310 hash.bv_len = strlen( hash.bv_val );
1312 if( hash.bv_len == 0 ) {
1316 return pw_string( scheme, &hash );
1320 int lutil_salt_format(const char *format)
1323 free( salt_format );
1325 salt_format = format != NULL ? strdup( format ) : NULL;
1331 #ifdef SLAPD_CLEARTEXT
1332 static struct berval *hash_clear(
1333 const struct pw_scheme *scheme,
1334 const struct berval *passwd )
1336 return ber_bvdup( (struct berval *) passwd );