3 * Copyright 1998-2002 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;
490 /* decode base64 password */
491 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
492 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
494 if( orig_pass == NULL ) return -1;
496 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
499 ber_memfree(orig_pass);
503 /* hash credentials with salt */
504 lutil_SHA1Init(&SHA1context);
505 lutil_SHA1Update(&SHA1context,
506 (const unsigned char *) cred->bv_val, cred->bv_len);
507 lutil_SHA1Update(&SHA1context,
508 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
509 rc - sizeof(SHA1digest));
510 lutil_SHA1Final(SHA1digest, &SHA1context);
513 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
514 ber_memfree(orig_pass);
519 const struct pw_scheme *sc,
520 const struct berval * passwd,
521 const struct berval * cred )
523 lutil_SHA1_CTX SHA1context;
524 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
526 unsigned char *orig_pass = NULL;
528 /* base64 un-encode password */
529 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
530 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
532 if( orig_pass == NULL ) return -1;
534 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
536 if( rc != sizeof(SHA1digest) ) {
537 ber_memfree(orig_pass);
541 /* hash credentials with salt */
542 lutil_SHA1Init(&SHA1context);
543 lutil_SHA1Update(&SHA1context,
544 (const unsigned char *) cred->bv_val, cred->bv_len);
545 lutil_SHA1Final(SHA1digest, &SHA1context);
548 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
549 ber_memfree(orig_pass);
555 const struct pw_scheme *sc,
556 const struct berval * passwd,
557 const struct berval * cred )
559 lutil_MD5_CTX MD5context;
560 unsigned char MD5digest[LUTIL_MD5_BYTES];
562 unsigned char *orig_pass = NULL;
564 /* base64 un-encode password */
565 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
566 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
568 if( orig_pass == NULL ) return -1;
570 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
572 ber_memfree(orig_pass);
576 /* hash credentials with salt */
577 lutil_MD5Init(&MD5context);
578 lutil_MD5Update(&MD5context,
579 (const unsigned char *) cred->bv_val,
581 lutil_MD5Update(&MD5context,
582 &orig_pass[sizeof(MD5digest)],
583 rc - sizeof(MD5digest));
584 lutil_MD5Final(MD5digest, &MD5context);
587 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
588 ber_memfree(orig_pass);
593 const struct pw_scheme *sc,
594 const struct berval * passwd,
595 const struct berval * cred )
597 lutil_MD5_CTX MD5context;
598 unsigned char MD5digest[LUTIL_MD5_BYTES];
600 unsigned char *orig_pass = NULL;
602 /* base64 un-encode password */
603 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
604 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
606 if( orig_pass == NULL ) return -1;
608 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
609 if ( rc != sizeof(MD5digest) ) {
610 ber_memfree(orig_pass);
614 /* hash credentials with salt */
615 lutil_MD5Init(&MD5context);
616 lutil_MD5Update(&MD5context,
617 (const unsigned char *) cred->bv_val,
619 lutil_MD5Final(MD5digest, &MD5context);
622 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
623 ber_memfree(orig_pass);
628 static int chk_lanman(
629 const struct pw_scheme *scheme,
630 const struct berval *passwd,
631 const struct berval *cred )
635 hash = hash_lanman( scheme, cred );
636 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
638 #endif /* SLAPD_LMHASH */
640 #ifdef SLAPD_NS_MTA_MD5
641 static int chk_ns_mta_md5(
642 const struct pw_scheme *scheme,
643 const struct berval *passwd,
644 const struct berval *cred )
646 lutil_MD5_CTX MD5context;
647 unsigned char MD5digest[LUTIL_MD5_BYTES], c;
648 char buffer[LUTIL_MD5_BYTES + LUTIL_MD5_BYTES + 1];
651 /* hash credentials with salt */
652 lutil_MD5Init(&MD5context);
653 lutil_MD5Update(&MD5context,
654 (const unsigned char *) &passwd->bv_val[32],
658 lutil_MD5Update(&MD5context,
659 (const unsigned char *) &c,
662 lutil_MD5Update(&MD5context,
663 (const unsigned char *) cred->bv_val,
667 lutil_MD5Update(&MD5context,
668 (const unsigned char *) &c,
671 lutil_MD5Update(&MD5context,
672 (const unsigned char *) &passwd->bv_val[32],
675 lutil_MD5Final(MD5digest, &MD5context);
677 for( i=0; i < sizeof( MD5digest ); i++ ) {
678 buffer[i+i] = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F];
679 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F];
683 return memcmp((char *)passwd->bv_val, (char *)buffer, sizeof(buffer))
689 #ifdef HAVE_CYRUS_SASL
690 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
694 const struct pw_scheme *sc,
695 const struct berval * passwd,
696 const struct berval * cred )
701 for( i=0; i<cred->bv_len; i++) {
702 if(cred->bv_val[i] == '\0') {
703 return 1; /* NUL character in password */
707 if( cred->bv_val[i] != '\0' ) {
708 return 1; /* cred must behave like a string */
711 for( i=0; i<passwd->bv_len; i++) {
712 if(passwd->bv_val[i] == '\0') {
713 return 1; /* NUL character in password */
717 if( passwd->bv_val[i] != '\0' ) {
718 return 1; /* passwd must behave like a string */
723 #ifdef HAVE_CYRUS_SASL
724 if( lutil_passwd_sasl_conn != NULL ) {
726 # if SASL_VERSION_MAJOR < 2
727 const char *errstr = NULL;
728 sc = sasl_checkpass( lutil_passwd_sasl_conn,
729 passwd->bv_val, passwd->bv_len,
730 cred->bv_val, cred->bv_len,
733 sc = sasl_checkpass( lutil_passwd_sasl_conn,
734 passwd->bv_val, passwd->bv_len,
735 cred->bv_val, cred->bv_len );
737 rtn = ( sc != SASL_OK );
746 static int chk_kerberos(
747 const struct pw_scheme *sc,
748 const struct berval * passwd,
749 const struct berval * cred )
754 for( i=0; i<cred->bv_len; i++) {
755 if(cred->bv_val[i] == '\0') {
756 return 1; /* NUL character in password */
760 if( cred->bv_val[i] != '\0' ) {
761 return 1; /* cred must behave like a string */
764 for( i=0; i<passwd->bv_len; i++) {
765 if(passwd->bv_val[i] == '\0') {
766 return 1; /* NUL character in password */
770 if( passwd->bv_val[i] != '\0' ) {
771 return 1; /* passwd must behave like a string */
776 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
779 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
780 * (Royal Institute of Technology, Stockholm, Sweden).
781 * All rights reserved.
783 * Redistribution and use in source and binary forms, with or without
784 * modification, are permitted provided that the following conditions
787 * 1. Redistributions of source code must retain the above copyright
788 * notice, this list of conditions and the following disclaimer.
790 * 2. Redistributions in binary form must reproduce the above copyright
791 * notice, this list of conditions and the following disclaimer in the
792 * documentation and/or other materials provided with the distribution.
794 * 3. Neither the name of the Institute nor the names of its contributors
795 * may be used to endorse or promote products derived from this software
796 * without specific prior written permission.
798 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
799 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
800 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
801 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
802 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
803 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
804 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
805 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
806 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
807 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
811 krb5_context context;
814 krb5_get_init_creds_opt get_options;
815 krb5_verify_init_creds_opt verify_options;
816 krb5_principal client, server;
818 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
821 ret = krb5_init_context( &context );
827 krb5_get_init_creds_opt_set_preauth_list(&get_options,
831 krb5_get_init_creds_opt_init( &get_options );
833 krb5_verify_init_creds_opt_init( &verify_options );
835 ret = krb5_parse_name( context, passwd->bv_val, &client );
838 krb5_free_context( context );
842 ret = krb5_get_init_creds_password( context,
843 &creds, client, cred->bv_val, NULL,
844 NULL, 0, NULL, &get_options );
847 krb5_free_principal( context, client );
848 krb5_free_context( context );
853 char *host = ldap_pvt_get_fqdn( NULL );
856 krb5_free_principal( context, client );
857 krb5_free_context( context );
861 ret = krb5_sname_to_principal( context,
862 host, "ldap", KRB5_NT_SRV_HST, &server );
868 krb5_free_principal( context, client );
869 krb5_free_context( context );
873 ret = krb5_verify_init_creds( context,
874 &creds, server, NULL, NULL, &verify_options );
876 krb5_free_principal( context, client );
877 krb5_free_principal( context, server );
878 krb5_free_cred_contents( context, &creds );
879 krb5_free_context( context );
883 #elif defined(HAVE_KRB4)
885 /* Borrowed from Heimdal kpopper */
887 * Copyright (c) 1989 Regents of the University of California.
888 * All rights reserved. The Berkeley software License Agreement
889 * specifies the terms and conditions for redistribution.
893 char lrealm[REALM_SZ];
894 char tkt[MAXHOSTNAMELEN];
896 status = krb_get_lrealm(lrealm,1);
897 if (status == KFAILURE) {
901 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
902 TKT_ROOT, (unsigned)getpid());
903 krb_set_tkt_string (tkt);
905 status = krb_verify_user( passwd->bv_val, "", lrealm,
906 cred->bv_val, 1, "ldap");
908 dest_tkt(); /* no point in keeping the tickets */
910 return status == KFAILURE;
916 #endif /* SLAPD_KPASSWD */
919 static int chk_crypt(
920 const struct pw_scheme *sc,
921 const struct berval * passwd,
922 const struct berval * cred )
927 for( i=0; i<cred->bv_len; i++) {
928 if(cred->bv_val[i] == '\0') {
929 return 1; /* NUL character in password */
933 if( cred->bv_val[i] != '\0' ) {
934 return -1; /* cred must behave like a string */
937 if( passwd->bv_len < 2 ) {
938 return -1; /* passwd must be at least two characters long */
941 for( i=0; i<passwd->bv_len; i++) {
942 if(passwd->bv_val[i] == '\0') {
943 return -1; /* NUL character in password */
947 if( passwd->bv_val[i] != '\0' ) {
948 return -1; /* passwd must behave like a string */
951 cr = crypt( cred->bv_val, passwd->bv_val );
953 if( cr == NULL || cr[0] == '\0' ) {
954 /* salt must have been invalid */
958 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
961 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
963 const struct pw_scheme *sc,
964 const struct berval * passwd,
965 const struct berval * cred )
970 for( i=0; i<cred->bv_len; i++) {
971 if(cred->bv_val[i] == '\0') {
972 return -1; /* NUL character in password */
975 if( cred->bv_val[i] != '\0' ) {
976 return -1; /* cred must behave like a string */
979 for( i=0; i<passwd->bv_len; i++) {
980 if(passwd->bv_val[i] == '\0') {
981 return -1; /* NUL character in password */
985 if( passwd->bv_val[i] != '\0' ) {
986 return -1; /* passwd must behave like a string */
990 struct passwd *pwd = getpwnam(passwd->bv_val);
993 return -1; /* not found */
998 # ifdef HAVE_GETSPNAM
1000 struct spwd *spwd = getspnam(passwd->bv_val);
1007 # ifdef HAVE_AIX_SECURITY
1009 struct userpw *upw = getuserpw(passwd->bv_val);
1012 pw = upw->upw_passwd;
1017 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1018 /* password must must be at least two characters long */
1022 cr = crypt(cred->bv_val, pw);
1024 if( cr == NULL || cr[0] == '\0' ) {
1025 /* salt must have been invalid */
1029 return strcmp(pw, cr) ? 1 : 0;
1035 /* PASSWORD GENERATION ROUTINES */
1037 #ifdef LUTIL_SHA1_BYTES
1038 static struct berval *hash_ssha1(
1039 const struct pw_scheme *scheme,
1040 const struct berval *passwd )
1042 lutil_SHA1_CTX SHA1context;
1043 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1044 unsigned char saltdata[4];
1045 struct berval digest;
1048 digest.bv_val = SHA1digest;
1049 digest.bv_len = sizeof(SHA1digest);
1050 salt.bv_val = saltdata;
1051 salt.bv_len = sizeof(saltdata);
1053 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1057 lutil_SHA1Init( &SHA1context );
1058 lutil_SHA1Update( &SHA1context,
1059 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1060 lutil_SHA1Update( &SHA1context,
1061 (const unsigned char *)salt.bv_val, salt.bv_len );
1062 lutil_SHA1Final( SHA1digest, &SHA1context );
1064 return pw_string64( scheme, &digest, &salt);
1067 static struct berval *hash_sha1(
1068 const struct pw_scheme *scheme,
1069 const struct berval *passwd )
1071 lutil_SHA1_CTX SHA1context;
1072 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1073 struct berval digest;
1074 digest.bv_val = SHA1digest;
1075 digest.bv_len = sizeof(SHA1digest);
1077 lutil_SHA1Init( &SHA1context );
1078 lutil_SHA1Update( &SHA1context,
1079 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1080 lutil_SHA1Final( SHA1digest, &SHA1context );
1082 return pw_string64( scheme, &digest, NULL);
1086 static struct berval *hash_smd5(
1087 const struct pw_scheme *scheme,
1088 const struct berval *passwd )
1090 lutil_MD5_CTX MD5context;
1091 unsigned char MD5digest[LUTIL_MD5_BYTES];
1092 unsigned char saltdata[4];
1093 struct berval digest;
1096 digest.bv_val = MD5digest;
1097 digest.bv_len = sizeof(MD5digest);
1098 salt.bv_val = saltdata;
1099 salt.bv_len = sizeof(saltdata);
1101 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1105 lutil_MD5Init( &MD5context );
1106 lutil_MD5Update( &MD5context,
1107 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1108 lutil_MD5Update( &MD5context,
1109 (const unsigned char *) salt.bv_val, salt.bv_len );
1110 lutil_MD5Final( MD5digest, &MD5context );
1112 return pw_string64( scheme, &digest, &salt );
1115 static struct berval *hash_md5(
1116 const struct pw_scheme *scheme,
1117 const struct berval *passwd )
1119 lutil_MD5_CTX MD5context;
1120 unsigned char MD5digest[LUTIL_MD5_BYTES];
1122 struct berval digest;
1124 digest.bv_val = MD5digest;
1125 digest.bv_len = sizeof(MD5digest);
1127 lutil_MD5Init( &MD5context );
1128 lutil_MD5Update( &MD5context,
1129 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1130 lutil_MD5Final( MD5digest, &MD5context );
1132 return pw_string64( scheme, &digest, NULL );
1137 /* pseudocode from RFC2433
1138 * A.2 LmPasswordHash()
1141 * IN 0-to-14-oem-char Password,
1142 * OUT 16-octet PasswordHash )
1144 * Set UcasePassword to the uppercased Password
1145 * Zero pad UcasePassword to 14 characters
1147 * DesHash( 1st 7-octets of UcasePassword,
1148 * giving 1st 8-octets of PasswordHash )
1150 * DesHash( 2nd 7-octets of UcasePassword,
1151 * giving 2nd 8-octets of PasswordHash )
1159 * OUT 8-octet Cypher )
1162 * * Make Cypher an irreversibly encrypted form of Clear by
1163 * * encrypting known text using Clear as the secret key.
1164 * * The known text consists of the string
1169 * Set StdText to "KGS!@#$%"
1170 * DesEncrypt( StdText, Clear, giving Cypher )
1179 * OUT 8-octet Cypher )
1182 * * Use the DES encryption algorithm [4] in ECB mode [9]
1183 * * to encrypt Clear into Cypher such that Cypher can
1184 * * only be decrypted back to Clear by providing Key.
1185 * * Note that the DES algorithm takes as input a 64-bit
1186 * * stream where the 8th, 16th, 24th, etc. bits are
1187 * * parity bits ignored by the encrypting algorithm.
1188 * * Unless you write your own DES to accept 56-bit input
1189 * * without parity, you will need to insert the parity bits
1195 static void lmPasswd_to_key(
1196 const unsigned char *lmPasswd,
1199 /* make room for parity bits */
1200 ((char *)key)[0] = lmPasswd[0];
1201 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1202 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1203 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1204 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1205 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1206 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1207 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1209 des_set_odd_parity( key );
1212 static struct berval *hash_lanman(
1213 const struct pw_scheme *scheme,
1214 const struct berval *passwd )
1218 char UcasePassword[15];
1220 des_key_schedule schedule;
1221 des_cblock StdText = "KGS!@#$%";
1222 des_cblock hash1, hash2;
1226 for( i=0; i<passwd->bv_len; i++) {
1227 if(passwd->bv_val[i] == '\0') {
1228 return NULL; /* NUL character in password */
1232 if( passwd->bv_val[i] != '\0' ) {
1233 return NULL; /* passwd must behave like a string */
1236 strncpy( UcasePassword, passwd->bv_val, 14 );
1237 UcasePassword[14] = '\0';
1238 ldap_pvt_str2upper( UcasePassword );
1240 lmPasswd_to_key( UcasePassword, &key );
1241 des_set_key_unchecked( &key, schedule );
1242 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1244 lmPasswd_to_key( &UcasePassword[7], &key );
1245 des_set_key_unchecked( &key, schedule );
1246 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1248 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1249 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1250 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1252 hash.bv_val = lmhash;
1255 return pw_string( scheme, &hash );
1257 #endif /* SLAPD_LMHASH */
1260 static struct berval *hash_crypt(
1261 const struct pw_scheme *scheme,
1262 const struct berval *passwd )
1265 unsigned char salt[32]; /* salt suitable for most anything */
1268 for( i=0; i<passwd->bv_len; i++) {
1269 if(passwd->bv_val[i] == '\0') {
1270 return NULL; /* NUL character in password */
1274 if( passwd->bv_val[i] != '\0' ) {
1275 return NULL; /* passwd must behave like a string */
1278 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1282 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1283 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1285 salt[sizeof( salt ) - 1 ] = '\0';
1287 if( salt_format != NULL ) {
1288 /* copy the salt we made into entropy before snprintfing
1289 it back into the salt */
1290 char entropy[sizeof(salt)];
1291 strcpy( entropy, salt );
1292 snprintf( salt, sizeof(entropy), salt_format, entropy );
1295 hash.bv_val = crypt( passwd->bv_val, salt );
1297 if( hash.bv_val == NULL ) return NULL;
1299 hash.bv_len = strlen( hash.bv_val );
1301 if( hash.bv_len == 0 ) {
1305 return pw_string( scheme, &hash );
1309 int lutil_salt_format(const char *format)
1312 free( salt_format );
1314 salt_format = format != NULL ? strdup( format ) : NULL;
1320 #ifdef SLAPD_CLEARTEXT
1321 static struct berval *hash_clear(
1322 const struct pw_scheme *scheme,
1323 const struct berval *passwd )
1325 return ber_bvdup( (struct berval *) passwd );