3 * Copyright 1998-2000 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 # include <openssl/des.h>
28 #endif /* SLAPD_LMHASH */
42 # include <ac/crypt.h>
44 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
51 # ifdef HAVE_AIX_SECURITY
61 #include "lutil_md5.h"
62 #include "lutil_sha1.h"
65 static const unsigned char crypt64[] =
66 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
70 typedef int (*PASSWD_CHK_FUNC)(
71 const struct pw_scheme *scheme,
72 const struct berval *passwd,
73 const struct berval *cred );
75 typedef struct berval * (*PASSWD_HASH_FUNC) (
76 const struct pw_scheme *scheme,
77 const struct berval *passwd );
81 PASSWD_CHK_FUNC chk_fn;
82 PASSWD_HASH_FUNC hash_fn;
85 /* password check routines */
87 const struct pw_scheme *scheme,
88 const struct berval *passwd,
89 const struct berval *cred );
92 const struct pw_scheme *scheme,
93 const struct berval *passwd,
94 const struct berval *cred );
96 #ifdef LUTIL_SHA1_BYTES
98 const struct pw_scheme *scheme,
99 const struct berval *passwd,
100 const struct berval *cred );
103 const struct pw_scheme *scheme,
104 const struct berval *passwd,
105 const struct berval *cred );
109 static int chk_lanman(
110 const struct pw_scheme *scheme,
111 const struct berval *passwd,
112 const struct berval *cred );
117 const struct pw_scheme *scheme,
118 const struct berval *passwd,
119 const struct berval *cred );
123 static int chk_kerberos(
124 const struct pw_scheme *scheme,
125 const struct berval *passwd,
126 const struct berval *cred );
130 static int chk_crypt(
131 const struct pw_scheme *scheme,
132 const struct berval *passwd,
133 const struct berval *cred );
135 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
137 const struct pw_scheme *scheme,
138 const struct berval *passwd,
139 const struct berval *cred );
144 #ifdef LUTIL_SHA1_BYTES
145 /* password hash routines */
146 static struct berval *hash_sha1(
147 const struct pw_scheme *scheme,
148 const struct berval *passwd );
150 static struct berval *hash_ssha1(
151 const struct pw_scheme *scheme,
152 const struct berval *passwd );
155 static struct berval *hash_smd5(
156 const struct pw_scheme *scheme,
157 const struct berval *passwd );
159 static struct berval *hash_md5(
160 const struct pw_scheme *scheme,
161 const struct berval *passwd );
164 static struct berval *hash_lanman(
165 const struct pw_scheme *scheme,
166 const struct berval *passwd );
170 static struct berval *hash_crypt(
171 const struct pw_scheme *scheme,
172 const struct berval *passwd );
176 static const struct pw_scheme pw_schemes[] =
178 #ifdef LUTIL_SHA1_BYTES
179 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
180 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
183 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
184 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
187 { {sizeof("{LANMAN}")-1, "{LANMAN}"}, chk_lanman, hash_lanman },
188 #endif /* SLAPD_LMHASH */
191 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
195 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
199 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
200 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
201 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
205 #ifdef SLAPD_CLEARTEXT
207 { {0, "{CLEARTEXT}"}, NULL, NULL },
210 { {0, NULL}, NULL, NULL }
213 static const struct pw_scheme *get_scheme(
218 for( i=0; pw_schemes[i].name.bv_val; i++) {
219 if( pw_schemes[i].name.bv_len == 0 ) continue;
221 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
222 pw_schemes[i].name.bv_len) == 0 )
224 return &pw_schemes[i];
231 int lutil_passwd_scheme(
234 if( scheme == NULL ) {
238 return get_scheme(scheme) != NULL;
242 static int is_allowed_scheme(
244 const char** schemes )
248 if( schemes == NULL ) return 1;
250 for( i=0; schemes[i] != NULL; i++ ) {
251 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
258 static struct berval *passwd_scheme(
259 const struct pw_scheme *scheme,
260 const struct berval * passwd,
261 const char** allowed )
263 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
267 if( passwd->bv_len >= scheme->name.bv_len ) {
268 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
269 struct berval *bv = ber_memalloc( sizeof(struct berval) );
271 if( bv == NULL ) return NULL;
273 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
274 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
284 * Return 0 if creds are good.
288 const struct berval *passwd, /* stored passwd */
289 const struct berval *cred, /* user cred */
290 const char **schemes )
294 if (cred == NULL || cred->bv_len == 0 ||
295 passwd == NULL || passwd->bv_len == 0 )
300 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
301 if( pw_schemes[i].chk_fn ) {
302 struct berval *p = passwd_scheme( &pw_schemes[i],
306 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
308 /* only free the berval structure as the bv_val points
309 * into passwd->bv_val
318 #ifdef SLAPD_CLEARTEXT
319 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
320 return passwd->bv_len == cred->bv_len
321 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
328 struct berval * lutil_passwd_generate( ber_len_t len )
332 if( len < 1 ) return NULL;
334 pw = ber_memalloc( sizeof( struct berval ) );
335 if( pw == NULL ) return NULL;
338 pw->bv_val = ber_memalloc( len + 1 );
340 if( pw->bv_val == NULL ) {
345 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
350 for( len = 0; len < pw->bv_len; len++ ) {
351 pw->bv_val[len] = crypt64[
352 pw->bv_val[len] % (sizeof(crypt64)-1) ];
355 pw->bv_val[len] = '\0';
360 struct berval * lutil_passwd_hash(
361 const struct berval * passwd,
362 const char * method )
364 const struct pw_scheme *sc = get_scheme( method );
366 if( sc == NULL ) return NULL;
367 if( ! sc->hash_fn ) return NULL;
369 return (sc->hash_fn)( sc, passwd );
372 static struct berval * pw_string(
373 const struct pw_scheme *sc,
374 const struct berval *passwd )
376 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
377 if( pw == NULL ) return NULL;
379 pw->bv_len = sc->name.bv_len + passwd->bv_len;
380 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
382 if( pw->bv_val == NULL ) {
387 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
388 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
390 pw->bv_val[pw->bv_len] = '\0';
394 static struct berval * pw_string64(
395 const struct pw_scheme *sc,
396 const struct berval *hash,
397 const struct berval *salt )
400 struct berval string;
401 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
404 if( b64 == NULL ) return NULL;
407 /* need to base64 combined string */
408 string.bv_len = hash->bv_len + salt->bv_len;
409 string.bv_val = ber_memalloc( string.bv_len + 1 );
411 if( string.bv_val == NULL ) {
416 AC_MEMCPY( string.bv_val, hash->bv_val,
418 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
420 string.bv_val[string.bv_len] = '\0';
426 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
427 b64->bv_len = b64len + sc->name.bv_len;
428 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
430 if( b64->bv_val == NULL ) {
431 if( salt ) ber_memfree( string.bv_val );
436 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
439 string.bv_val, string.bv_len,
440 &b64->bv_val[sc->name.bv_len], b64len );
442 if( salt ) ber_memfree( string.bv_val );
449 /* recompute length */
450 b64->bv_len = sc->name.bv_len + rc;
451 assert( strlen(b64->bv_val) == b64->bv_len );
455 /* PASSWORD CHECK ROUTINES */
457 #ifdef LUTIL_SHA1_BYTES
458 static int chk_ssha1(
459 const struct pw_scheme *sc,
460 const struct berval * passwd,
461 const struct berval * cred )
463 lutil_SHA1_CTX SHA1context;
464 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
466 unsigned char *orig_pass = NULL;
468 /* decode base64 password */
469 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
470 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
472 if( orig_pass == NULL ) return -1;
474 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
477 ber_memfree(orig_pass);
481 /* hash credentials with salt */
482 lutil_SHA1Init(&SHA1context);
483 lutil_SHA1Update(&SHA1context,
484 (const unsigned char *) cred->bv_val, cred->bv_len);
485 lutil_SHA1Update(&SHA1context,
486 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
487 rc - sizeof(SHA1digest));
488 lutil_SHA1Final(SHA1digest, &SHA1context);
491 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
492 ber_memfree(orig_pass);
497 const struct pw_scheme *sc,
498 const struct berval * passwd,
499 const struct berval * cred )
501 lutil_SHA1_CTX SHA1context;
502 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
504 unsigned char *orig_pass = NULL;
506 /* base64 un-encode password */
507 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
508 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
510 if( orig_pass == NULL ) return -1;
512 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
514 if( rc != sizeof(SHA1digest) ) {
515 ber_memfree(orig_pass);
519 /* hash credentials with salt */
520 lutil_SHA1Init(&SHA1context);
521 lutil_SHA1Update(&SHA1context,
522 (const unsigned char *) cred->bv_val, cred->bv_len);
523 lutil_SHA1Final(SHA1digest, &SHA1context);
526 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
527 ber_memfree(orig_pass);
533 const struct pw_scheme *sc,
534 const struct berval * passwd,
535 const struct berval * cred )
537 lutil_MD5_CTX MD5context;
538 unsigned char MD5digest[LUTIL_MD5_BYTES];
540 unsigned char *orig_pass = NULL;
542 /* base64 un-encode password */
543 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
544 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
546 if( orig_pass == NULL ) return -1;
548 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
550 ber_memfree(orig_pass);
554 /* hash credentials with salt */
555 lutil_MD5Init(&MD5context);
556 lutil_MD5Update(&MD5context,
557 (const unsigned char *) cred->bv_val,
559 lutil_MD5Update(&MD5context,
560 &orig_pass[sizeof(MD5digest)],
561 rc - sizeof(MD5digest));
562 lutil_MD5Final(MD5digest, &MD5context);
565 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
566 ber_memfree(orig_pass);
571 const struct pw_scheme *sc,
572 const struct berval * passwd,
573 const struct berval * cred )
575 lutil_MD5_CTX MD5context;
576 unsigned char MD5digest[LUTIL_MD5_BYTES];
578 unsigned char *orig_pass = NULL;
580 /* base64 un-encode password */
581 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
582 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
584 if( orig_pass == NULL ) return -1;
586 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
587 if ( rc != sizeof(MD5digest) ) {
588 ber_memfree(orig_pass);
592 /* hash credentials with salt */
593 lutil_MD5Init(&MD5context);
594 lutil_MD5Update(&MD5context,
595 (const unsigned char *) cred->bv_val,
597 lutil_MD5Final(MD5digest, &MD5context);
600 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
601 ber_memfree(orig_pass);
606 static int chk_lanman(
607 const struct pw_scheme *scheme,
608 const struct berval *passwd,
609 const struct berval *cred )
613 hash = hash_lanman( scheme, cred );
614 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
616 #endif /* SLAPD_LMHASH */
619 #ifdef HAVE_CYRUS_SASL
620 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
624 const struct pw_scheme *sc,
625 const struct berval * passwd,
626 const struct berval * cred )
631 for( i=0; i<cred->bv_len; i++) {
632 if(cred->bv_val[i] == '\0') {
633 return 1; /* NUL character in password */
637 if( cred->bv_val[i] != '\0' ) {
638 return 1; /* cred must behave like a string */
641 for( i=0; i<passwd->bv_len; i++) {
642 if(passwd->bv_val[i] == '\0') {
643 return 1; /* NUL character in password */
647 if( passwd->bv_val[i] != '\0' ) {
648 return 1; /* passwd must behave like a string */
653 #ifdef HAVE_CYRUS_SASL
654 if( lutil_passwd_sasl_conn != NULL ) {
655 const char *errstr = NULL;
658 sc = sasl_checkpass( lutil_passwd_sasl_conn,
659 passwd->bv_val, passwd->bv_len,
660 cred->bv_val, cred->bv_len,
663 rtn = ( sc != SASL_OK );
672 static int chk_kerberos(
673 const struct pw_scheme *sc,
674 const struct berval * passwd,
675 const struct berval * cred )
680 for( i=0; i<cred->bv_len; i++) {
681 if(cred->bv_val[i] == '\0') {
682 return 1; /* NUL character in password */
686 if( cred->bv_val[i] != '\0' ) {
687 return 1; /* cred must behave like a string */
690 for( i=0; i<passwd->bv_len; i++) {
691 if(passwd->bv_val[i] == '\0') {
692 return 1; /* NUL character in password */
696 if( passwd->bv_val[i] != '\0' ) {
697 return 1; /* passwd must behave like a string */
702 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
705 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
706 * (Royal Institute of Technology, Stockholm, Sweden).
707 * All rights reserved.
709 * Redistribution and use in source and binary forms, with or without
710 * modification, are permitted provided that the following conditions
713 * 1. Redistributions of source code must retain the above copyright
714 * notice, this list of conditions and the following disclaimer.
716 * 2. Redistributions in binary form must reproduce the above copyright
717 * notice, this list of conditions and the following disclaimer in the
718 * documentation and/or other materials provided with the distribution.
720 * 3. Neither the name of the Institute nor the names of its contributors
721 * may be used to endorse or promote products derived from this software
722 * without specific prior written permission.
724 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
725 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
726 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
727 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
728 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
729 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
730 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
731 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
732 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
733 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
737 krb5_context context;
740 krb5_get_init_creds_opt get_options;
741 krb5_verify_init_creds_opt verify_options;
742 krb5_principal client, server;
744 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
747 ret = krb5_init_context( &context );
753 krb5_get_init_creds_opt_set_preauth_list(&get_options,
757 krb5_get_init_creds_opt_init( &get_options );
759 krb5_verify_init_creds_opt_init( &verify_options );
761 ret = krb5_parse_name( context, passwd->bv_val, &client );
764 krb5_free_context( context );
768 ret = krb5_get_init_creds_password( context,
769 &creds, client, cred->bv_val, NULL,
770 NULL, 0, NULL, &get_options );
773 krb5_free_principal( context, client );
774 krb5_free_context( context );
779 char *host = ldap_pvt_get_fqdn( NULL );
782 krb5_free_principal( context, client );
783 krb5_free_context( context );
787 ret = krb5_sname_to_principal( context,
788 host, "ldap", KRB5_NT_SRV_HST, &server );
794 krb5_free_principal( context, client );
795 krb5_free_context( context );
799 ret = krb5_verify_init_creds( context,
800 &creds, server, NULL, NULL, &verify_options );
802 krb5_free_principal( context, client );
803 krb5_free_principal( context, server );
804 krb5_free_cred_contents( context, &creds );
805 krb5_free_context( context );
809 #elif defined(HAVE_KRB4)
811 /* Borrowed from Heimdal kpopper */
813 * Copyright (c) 1989 Regents of the University of California.
814 * All rights reserved. The Berkeley software License Agreement
815 * specifies the terms and conditions for redistribution.
819 char lrealm[REALM_SZ];
820 char tkt[MAXHOSTNAMELEN];
822 status = krb_get_lrealm(lrealm,1);
823 if (status == KFAILURE) {
827 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
828 TKT_ROOT, (unsigned)getpid());
829 krb_set_tkt_string (tkt);
831 status = krb_verify_user( passwd->bv_val, "", lrealm,
832 cred->bv_val, 1, "ldap");
834 dest_tkt(); /* no point in keeping the tickets */
836 return status == KFAILURE;
842 #endif /* SLAPD_KPASSWD */
845 static int chk_crypt(
846 const struct pw_scheme *sc,
847 const struct berval * passwd,
848 const struct berval * cred )
853 for( i=0; i<cred->bv_len; i++) {
854 if(cred->bv_val[i] == '\0') {
855 return 1; /* NUL character in password */
859 if( cred->bv_val[i] != '\0' ) {
860 return -1; /* cred must behave like a string */
863 if( passwd->bv_len < 2 ) {
864 return -1; /* passwd must be at least two characters long */
867 for( i=0; i<passwd->bv_len; i++) {
868 if(passwd->bv_val[i] == '\0') {
869 return -1; /* NUL character in password */
873 if( passwd->bv_val[i] != '\0' ) {
874 return -1; /* passwd must behave like a string */
877 cr = crypt( cred->bv_val, passwd->bv_val );
879 if( cr == NULL || cr[0] == '\0' ) {
880 /* salt must have been invalid */
884 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
887 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
889 const struct pw_scheme *sc,
890 const struct berval * passwd,
891 const struct berval * cred )
896 for( i=0; i<cred->bv_len; i++) {
897 if(cred->bv_val[i] == '\0') {
898 return -1; /* NUL character in password */
901 if( cred->bv_val[i] != '\0' ) {
902 return -1; /* cred must behave like a string */
905 for( i=0; i<passwd->bv_len; i++) {
906 if(passwd->bv_val[i] == '\0') {
907 return -1; /* NUL character in password */
911 if( passwd->bv_val[i] != '\0' ) {
912 return -1; /* passwd must behave like a string */
916 struct passwd *pwd = getpwnam(passwd->bv_val);
919 return -1; /* not found */
924 # ifdef HAVE_GETSPNAM
926 struct spwd *spwd = getspnam(passwd->bv_val);
933 # ifdef HAVE_AIX_SECURITY
935 struct userpw *upw = getuserpw(passwd->bv_val);
938 pw = upw->upw_passwd;
943 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
944 /* password must must be at least two characters long */
948 cr = crypt(cred->bv_val, pw);
950 if( cr == NULL || cr[0] == '\0' ) {
951 /* salt must have been invalid */
955 return strcmp(pw, cr) ? 1 : 0;
961 /* PASSWORD GENERATION ROUTINES */
963 #ifdef LUTIL_SHA1_BYTES
964 static struct berval *hash_ssha1(
965 const struct pw_scheme *scheme,
966 const struct berval *passwd )
968 lutil_SHA1_CTX SHA1context;
969 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
970 unsigned char saltdata[4];
971 struct berval digest;
974 digest.bv_val = SHA1digest;
975 digest.bv_len = sizeof(SHA1digest);
976 salt.bv_val = saltdata;
977 salt.bv_len = sizeof(saltdata);
979 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
983 lutil_SHA1Init( &SHA1context );
984 lutil_SHA1Update( &SHA1context,
985 (const unsigned char *)passwd->bv_val, passwd->bv_len );
986 lutil_SHA1Update( &SHA1context,
987 (const unsigned char *)salt.bv_val, salt.bv_len );
988 lutil_SHA1Final( SHA1digest, &SHA1context );
990 return pw_string64( scheme, &digest, &salt);
993 static struct berval *hash_sha1(
994 const struct pw_scheme *scheme,
995 const struct berval *passwd )
997 lutil_SHA1_CTX SHA1context;
998 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
999 struct berval digest;
1000 digest.bv_val = SHA1digest;
1001 digest.bv_len = sizeof(SHA1digest);
1003 lutil_SHA1Init( &SHA1context );
1004 lutil_SHA1Update( &SHA1context,
1005 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1006 lutil_SHA1Final( SHA1digest, &SHA1context );
1008 return pw_string64( scheme, &digest, NULL);
1012 static struct berval *hash_smd5(
1013 const struct pw_scheme *scheme,
1014 const struct berval *passwd )
1016 lutil_MD5_CTX MD5context;
1017 unsigned char MD5digest[LUTIL_MD5_BYTES];
1018 unsigned char saltdata[4];
1019 struct berval digest;
1022 digest.bv_val = MD5digest;
1023 digest.bv_len = sizeof(MD5digest);
1024 salt.bv_val = saltdata;
1025 salt.bv_len = sizeof(saltdata);
1027 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1031 lutil_MD5Init( &MD5context );
1032 lutil_MD5Update( &MD5context,
1033 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1034 lutil_MD5Update( &MD5context,
1035 (const unsigned char *) salt.bv_val, salt.bv_len );
1036 lutil_MD5Final( MD5digest, &MD5context );
1038 return pw_string64( scheme, &digest, &salt );
1041 static struct berval *hash_md5(
1042 const struct pw_scheme *scheme,
1043 const struct berval *passwd )
1045 lutil_MD5_CTX MD5context;
1046 unsigned char MD5digest[LUTIL_MD5_BYTES];
1048 struct berval digest;
1050 digest.bv_val = MD5digest;
1051 digest.bv_len = sizeof(MD5digest);
1053 lutil_MD5Init( &MD5context );
1054 lutil_MD5Update( &MD5context,
1055 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1056 lutil_MD5Final( MD5digest, &MD5context );
1058 return pw_string64( scheme, &digest, NULL );
1063 /* pseudocode from RFC2433
1064 * A.2 LmPasswordHash()
1067 * IN 0-to-14-oem-char Password,
1068 * OUT 16-octet PasswordHash )
1070 * Set UcasePassword to the uppercased Password
1071 * Zero pad UcasePassword to 14 characters
1073 * DesHash( 1st 7-octets of UcasePassword,
1074 * giving 1st 8-octets of PasswordHash )
1076 * DesHash( 2nd 7-octets of UcasePassword,
1077 * giving 2nd 8-octets of PasswordHash )
1085 * OUT 8-octet Cypher )
1088 * * Make Cypher an irreversibly encrypted form of Clear by
1089 * * encrypting known text using Clear as the secret key.
1090 * * The known text consists of the string
1095 * Set StdText to "KGS!@#$%"
1096 * DesEncrypt( StdText, Clear, giving Cypher )
1105 * OUT 8-octet Cypher )
1108 * * Use the DES encryption algorithm [4] in ECB mode [9]
1109 * * to encrypt Clear into Cypher such that Cypher can
1110 * * only be decrypted back to Clear by providing Key.
1111 * * Note that the DES algorithm takes as input a 64-bit
1112 * * stream where the 8th, 16th, 24th, etc. bits are
1113 * * parity bits ignored by the encrypting algorithm.
1114 * * Unless you write your own DES to accept 56-bit input
1115 * * without parity, you will need to insert the parity bits
1121 static void lmPasswd_to_key(
1122 const unsigned char *lmPasswd,
1125 /* make room for parity bits */
1126 ((char *)key)[0] = lmPasswd[0];
1127 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1128 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1129 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1130 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1131 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1132 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1133 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1135 des_set_odd_parity( key );
1138 static struct berval *hash_lanman(
1139 const struct pw_scheme *scheme,
1140 const struct berval *passwd )
1144 char UcasePassword[15];
1146 des_key_schedule schedule;
1147 des_cblock StdText = "KGS!@#$%";
1148 des_cblock hash1, hash2;
1152 for( i=0; i<passwd->bv_len; i++) {
1153 if(passwd->bv_val[i] == '\0') {
1154 return NULL; /* NUL character in password */
1158 if( passwd->bv_val[i] != '\0' ) {
1159 return NULL; /* passwd must behave like a string */
1162 strncpy( UcasePassword, passwd->bv_val, 14 );
1163 UcasePassword[14] = '\0';
1164 ldap_pvt_str2upper( UcasePassword );
1166 lmPasswd_to_key( UcasePassword, &key );
1167 des_set_key_unchecked( &key, schedule );
1168 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1170 lmPasswd_to_key( &UcasePassword[7], &key );
1171 des_set_key_unchecked( &key, schedule );
1172 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1174 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1175 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1176 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1178 hash.bv_val = lmhash;
1181 return pw_string( scheme, &hash );
1183 #endif /* SLAPD_LMHASH */
1186 static struct berval *hash_crypt(
1187 const struct pw_scheme *scheme,
1188 const struct berval *passwd )
1191 unsigned char salt[9]; /* salt suitable for anything */
1194 for( i=0; i<passwd->bv_len; i++) {
1195 if(passwd->bv_val[i] == '\0') {
1196 return NULL; /* NUL character in password */
1200 if( passwd->bv_val[i] != '\0' ) {
1201 return NULL; /* passwd must behave like a string */
1204 if( lutil_entropy( salt, 8) < 0 ) {
1208 for( i=0; i<8; i++ ) {
1209 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1213 hash.bv_val = crypt( passwd->bv_val, salt );
1215 if( hash.bv_val == NULL ) return NULL;
1217 hash.bv_len = strlen( hash.bv_val );
1219 if( hash.bv_len == 0 ) {
1223 return pw_string( scheme, &hash );