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 LUTIL_PASSWD_CHK_FUNC *chk_fn;
84 LUTIL_PASSWD_HASH_FUNC *hash_fn;
88 struct pw_slist *next;
92 /* password check routines */
94 static LUTIL_PASSWD_CHK_FUNC chk_md5;
95 static LUTIL_PASSWD_CHK_FUNC chk_smd5;
96 static LUTIL_PASSWD_HASH_FUNC hash_smd5;
97 static LUTIL_PASSWD_HASH_FUNC hash_md5;
100 #ifdef LUTIL_SHA1_BYTES
101 static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
102 static LUTIL_PASSWD_CHK_FUNC chk_sha1;
103 static LUTIL_PASSWD_HASH_FUNC hash_sha1;
104 static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
108 static LUTIL_PASSWD_CHK_FUNC chk_lanman;
109 static LUTIL_PASSWD_HASH_FUNC hash_lanman;
112 #ifdef SLAPD_NS_MTA_MD5
113 static LUTIL_PASSWD_CHK_FUNC chk_ns_mta_md5;
117 static LUTIL_PASSWD_CHK_FUNC chk_sasl;
121 static LUTIL_PASSWD_CHK_FUNC chk_kerberos;
125 static LUTIL_PASSWD_CHK_FUNC chk_crypt;
126 static LUTIL_PASSWD_HASH_FUNC hash_crypt;
128 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
129 static LUTIL_PASSWD_CHK_FUNC chk_unix;
133 /* password hash routines */
135 #ifdef SLAPD_CLEARTEXT
136 static LUTIL_PASSWD_HASH_FUNC hash_clear;
139 static struct pw_slist *pw_schemes;
141 static const struct pw_scheme pw_schemes_default[] =
143 #ifdef LUTIL_SHA1_BYTES
144 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
145 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
148 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
149 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
152 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
153 #endif /* SLAPD_LMHASH */
155 #ifdef SLAPD_NS_MTA_MD5
156 { BER_BVC("{NS-MTA-MD5}"), chk_ns_mta_md5, NULL },
157 #endif /* SLAPD_NS_MTA_MD5 */
160 { BER_BVC("{SASL}"), chk_sasl, NULL },
164 { BER_BVC("{KERBEROS}"), chk_kerberos, NULL },
168 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
169 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
170 { BER_BVC("{UNIX}"), chk_unix, NULL },
174 #ifdef SLAPD_CLEARTEXT
176 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
179 { BER_BVNULL, NULL, NULL }
182 int lutil_passwd_add(
183 struct berval *scheme,
184 LUTIL_PASSWD_CHK_FUNC *chk,
185 LUTIL_PASSWD_HASH_FUNC *hash )
187 struct pw_slist *ptr;
189 ptr = ber_memalloc( sizeof( struct pw_slist ));
191 ptr->next = pw_schemes;
192 ptr->s.name = *scheme;
194 ptr->s.hash_fn = hash;
199 void lutil_passwd_init()
201 struct pw_slist *ptr;
204 for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
205 if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn )) break;
209 void lutil_passwd_destroy()
211 struct pw_slist *ptr, *next;
213 for( ptr=pw_schemes; ptr; ptr=next ) {
219 static const struct pw_scheme *get_scheme(
222 struct pw_slist *pws;
224 if (!pw_schemes) lutil_passwd_init();
226 for( pws=pw_schemes; pws; pws=pws->next ) {
227 if( strcasecmp(scheme, pws->s.name.bv_val ) == 0 ) {
235 int lutil_passwd_scheme(
238 if( scheme == NULL ) {
242 return get_scheme(scheme) != NULL;
246 static int is_allowed_scheme(
248 const char** schemes )
252 if( schemes == NULL ) return 1;
254 for( i=0; schemes[i] != NULL; i++ ) {
255 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
262 static struct berval *passwd_scheme(
263 const struct pw_scheme *scheme,
264 const struct berval * passwd,
266 const char** allowed )
268 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
272 if( passwd->bv_len >= scheme->name.bv_len ) {
273 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
274 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
275 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
285 * Return 0 if creds are good.
289 const struct berval *passwd, /* stored passwd */
290 const struct berval *cred, /* user cred */
291 const char **schemes )
293 struct pw_slist *pws;
295 if (cred == NULL || cred->bv_len == 0 ||
296 passwd == NULL || passwd->bv_len == 0 )
301 if (!pw_schemes) lutil_passwd_init();
303 for( pws=pw_schemes; pws; pws=pws->next ) {
304 if( pws->s.chk_fn ) {
306 struct berval *p = passwd_scheme( &(pws->s),
307 passwd, &x, schemes );
310 return (pws->s.chk_fn)( &(pws->s.name), p, cred );
315 #ifdef SLAPD_CLEARTEXT
316 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
317 return (( passwd->bv_len == cred->bv_len ) &&
318 ( passwd->bv_val[0] != '{' /*'}'*/ ))
319 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
326 struct berval * lutil_passwd_generate( ber_len_t len )
330 if( len < 1 ) return NULL;
332 pw = ber_memalloc( sizeof( struct berval ) );
333 if( pw == NULL ) return NULL;
336 pw->bv_val = ber_memalloc( len + 1 );
338 if( pw->bv_val == NULL ) {
343 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
348 for( len = 0; len < pw->bv_len; len++ ) {
349 pw->bv_val[len] = crypt64[
350 pw->bv_val[len] % (sizeof(crypt64)-1) ];
353 pw->bv_val[len] = '\0';
358 struct berval * lutil_passwd_hash(
359 const struct berval * passwd,
360 const char * method )
362 const struct pw_scheme *sc = get_scheme( method );
364 if( sc == NULL ) return NULL;
365 if( ! sc->hash_fn ) return NULL;
367 return (sc->hash_fn)( &sc->name, passwd );
370 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
371 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
372 static struct berval * pw_string(
373 const struct berval *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->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->bv_val, sc->bv_len );
388 AC_MEMCPY( &pw->bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
390 pw->bv_val[pw->bv_len] = '\0';
393 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
395 static struct berval * pw_string64(
396 const struct berval *sc,
397 const struct berval *hash,
398 const struct berval *salt )
401 struct berval string;
402 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
405 if( b64 == NULL ) return NULL;
408 /* need to base64 combined string */
409 string.bv_len = hash->bv_len + salt->bv_len;
410 string.bv_val = ber_memalloc( string.bv_len + 1 );
412 if( string.bv_val == NULL ) {
417 AC_MEMCPY( string.bv_val, hash->bv_val,
419 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
421 string.bv_val[string.bv_len] = '\0';
427 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
428 b64->bv_len = b64len + sc->bv_len;
429 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
431 if( b64->bv_val == NULL ) {
432 if( salt ) ber_memfree( string.bv_val );
437 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
440 string.bv_val, string.bv_len,
441 &b64->bv_val[sc->bv_len], b64len );
443 if( salt ) ber_memfree( string.bv_val );
450 /* recompute length */
451 b64->bv_len = sc->bv_len + rc;
452 assert( strlen(b64->bv_val) == b64->bv_len );
456 /* PASSWORD CHECK ROUTINES */
458 #ifdef LUTIL_SHA1_BYTES
459 static int chk_ssha1(
460 const struct berval *sc,
461 const struct berval * passwd,
462 const struct berval * cred )
464 lutil_SHA1_CTX SHA1context;
465 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
467 unsigned char *orig_pass = NULL;
470 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
474 /* decode base64 password */
475 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
476 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
478 if( orig_pass == NULL ) return -1;
480 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
482 if (rc <= sizeof(SHA1digest)) {
483 ber_memfree(orig_pass);
487 /* hash credentials with salt */
488 lutil_SHA1Init(&SHA1context);
489 lutil_SHA1Update(&SHA1context,
490 (const unsigned char *) cred->bv_val, cred->bv_len);
491 lutil_SHA1Update(&SHA1context,
492 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
493 rc - sizeof(SHA1digest));
494 lutil_SHA1Final(SHA1digest, &SHA1context);
497 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
498 ber_memfree(orig_pass);
503 const struct berval *sc,
504 const struct berval * passwd,
505 const struct berval * cred )
507 lutil_SHA1_CTX SHA1context;
508 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
510 unsigned char *orig_pass = NULL;
512 /* base64 un-encode password */
513 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
514 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
516 if( orig_pass == NULL ) return -1;
518 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
520 if( rc != sizeof(SHA1digest) ) {
521 ber_memfree(orig_pass);
525 /* hash credentials with salt */
526 lutil_SHA1Init(&SHA1context);
527 lutil_SHA1Update(&SHA1context,
528 (const unsigned char *) cred->bv_val, cred->bv_len);
529 lutil_SHA1Final(SHA1digest, &SHA1context);
532 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
533 ber_memfree(orig_pass);
539 const struct berval *sc,
540 const struct berval * passwd,
541 const struct berval * cred )
543 lutil_MD5_CTX MD5context;
544 unsigned char MD5digest[LUTIL_MD5_BYTES];
546 unsigned char *orig_pass = NULL;
549 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
553 /* base64 un-encode password */
554 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
555 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
557 if( orig_pass == NULL ) return -1;
559 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
561 if (rc <= sizeof(MD5digest)) {
562 ber_memfree(orig_pass);
566 /* hash credentials with salt */
567 lutil_MD5Init(&MD5context);
568 lutil_MD5Update(&MD5context,
569 (const unsigned char *) cred->bv_val,
571 lutil_MD5Update(&MD5context,
572 &orig_pass[sizeof(MD5digest)],
573 rc - sizeof(MD5digest));
574 lutil_MD5Final(MD5digest, &MD5context);
577 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
578 ber_memfree(orig_pass);
583 const struct berval *sc,
584 const struct berval * passwd,
585 const struct berval * cred )
587 lutil_MD5_CTX MD5context;
588 unsigned char MD5digest[LUTIL_MD5_BYTES];
590 unsigned char *orig_pass = NULL;
592 /* base64 un-encode password */
593 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
594 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
596 if( orig_pass == NULL ) return -1;
598 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
599 if ( rc != sizeof(MD5digest) ) {
600 ber_memfree(orig_pass);
604 /* hash credentials with salt */
605 lutil_MD5Init(&MD5context);
606 lutil_MD5Update(&MD5context,
607 (const unsigned char *) cred->bv_val,
609 lutil_MD5Final(MD5digest, &MD5context);
612 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
613 ber_memfree(orig_pass);
618 static int chk_lanman(
619 const struct berval *scheme,
620 const struct berval *passwd,
621 const struct berval *cred )
625 hash = hash_lanman( scheme, cred );
626 return memcmp( &hash->bv_val[scheme->bv_len], passwd->bv_val, 32);
628 #endif /* SLAPD_LMHASH */
630 #ifdef SLAPD_NS_MTA_MD5
631 static int chk_ns_mta_md5(
632 const struct berval *scheme,
633 const struct berval *passwd,
634 const struct berval *cred )
636 lutil_MD5_CTX MD5context;
637 unsigned char MD5digest[LUTIL_MD5_BYTES], c;
638 char buffer[LUTIL_MD5_BYTES + LUTIL_MD5_BYTES + 1];
641 /* hash credentials with salt */
642 lutil_MD5Init(&MD5context);
643 lutil_MD5Update(&MD5context,
644 (const unsigned char *) &passwd->bv_val[32],
648 lutil_MD5Update(&MD5context,
649 (const unsigned char *) &c,
652 lutil_MD5Update(&MD5context,
653 (const unsigned char *) cred->bv_val,
657 lutil_MD5Update(&MD5context,
658 (const unsigned char *) &c,
661 lutil_MD5Update(&MD5context,
662 (const unsigned char *) &passwd->bv_val[32],
665 lutil_MD5Final(MD5digest, &MD5context);
667 for( i=0; i < sizeof( MD5digest ); i++ ) {
668 buffer[i+i] = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F];
669 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F];
673 return memcmp((char *)passwd->bv_val, (char *)buffer, sizeof(buffer))
679 #ifdef HAVE_CYRUS_SASL
680 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
684 const struct berval *sc,
685 const struct berval * passwd,
686 const struct berval * cred )
691 for( i=0; i<cred->bv_len; i++) {
692 if(cred->bv_val[i] == '\0') {
693 return 1; /* NUL character in password */
697 if( cred->bv_val[i] != '\0' ) {
698 return 1; /* cred must behave like a string */
701 for( i=0; i<passwd->bv_len; i++) {
702 if(passwd->bv_val[i] == '\0') {
703 return 1; /* NUL character in password */
707 if( passwd->bv_val[i] != '\0' ) {
708 return 1; /* passwd must behave like a string */
713 #ifdef HAVE_CYRUS_SASL
714 if( lutil_passwd_sasl_conn != NULL ) {
716 # if SASL_VERSION_MAJOR < 2
717 const char *errstr = NULL;
718 sc = sasl_checkpass( lutil_passwd_sasl_conn,
719 passwd->bv_val, passwd->bv_len,
720 cred->bv_val, cred->bv_len,
723 sc = sasl_checkpass( lutil_passwd_sasl_conn,
724 passwd->bv_val, passwd->bv_len,
725 cred->bv_val, cred->bv_len );
727 rtn = ( sc != SASL_OK );
736 static int chk_kerberos(
737 const struct berval *sc,
738 const struct berval * passwd,
739 const struct berval * cred )
744 for( i=0; i<cred->bv_len; i++) {
745 if(cred->bv_val[i] == '\0') {
746 return 1; /* NUL character in password */
750 if( cred->bv_val[i] != '\0' ) {
751 return 1; /* cred must behave like a string */
754 for( i=0; i<passwd->bv_len; i++) {
755 if(passwd->bv_val[i] == '\0') {
756 return 1; /* NUL character in password */
760 if( passwd->bv_val[i] != '\0' ) {
761 return 1; /* passwd must behave like a string */
766 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
769 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
770 * (Royal Institute of Technology, Stockholm, Sweden).
771 * All rights reserved.
773 * Redistribution and use in source and binary forms, with or without
774 * modification, are permitted provided that the following conditions
777 * 1. Redistributions of source code must retain the above copyright
778 * notice, this list of conditions and the following disclaimer.
780 * 2. Redistributions in binary form must reproduce the above copyright
781 * notice, this list of conditions and the following disclaimer in the
782 * documentation and/or other materials provided with the distribution.
784 * 3. Neither the name of the Institute nor the names of its contributors
785 * may be used to endorse or promote products derived from this software
786 * without specific prior written permission.
788 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
789 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
790 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
791 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
792 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
793 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
794 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
795 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
796 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
797 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
801 krb5_context context;
804 krb5_get_init_creds_opt get_options;
805 krb5_verify_init_creds_opt verify_options;
806 krb5_principal client, server;
808 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
811 ret = krb5_init_context( &context );
817 krb5_get_init_creds_opt_set_preauth_list(&get_options,
821 krb5_get_init_creds_opt_init( &get_options );
823 krb5_verify_init_creds_opt_init( &verify_options );
825 ret = krb5_parse_name( context, passwd->bv_val, &client );
828 krb5_free_context( context );
832 ret = krb5_get_init_creds_password( context,
833 &creds, client, cred->bv_val, NULL,
834 NULL, 0, NULL, &get_options );
837 krb5_free_principal( context, client );
838 krb5_free_context( context );
843 char *host = ldap_pvt_get_fqdn( NULL );
846 krb5_free_principal( context, client );
847 krb5_free_context( context );
851 ret = krb5_sname_to_principal( context,
852 host, "ldap", KRB5_NT_SRV_HST, &server );
858 krb5_free_principal( context, client );
859 krb5_free_context( context );
863 ret = krb5_verify_init_creds( context,
864 &creds, server, NULL, NULL, &verify_options );
866 krb5_free_principal( context, client );
867 krb5_free_principal( context, server );
868 krb5_free_cred_contents( context, &creds );
869 krb5_free_context( context );
873 #elif defined(HAVE_KRB4)
875 /* Borrowed from Heimdal kpopper */
877 * Copyright (c) 1989 Regents of the University of California.
878 * All rights reserved. The Berkeley software License Agreement
879 * specifies the terms and conditions for redistribution.
883 char lrealm[REALM_SZ];
884 char tkt[MAXHOSTNAMELEN];
886 status = krb_get_lrealm(lrealm,1);
887 if (status == KFAILURE) {
891 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
892 TKT_ROOT, (unsigned)getpid());
893 krb_set_tkt_string (tkt);
895 status = krb_verify_user( passwd->bv_val, "", lrealm,
896 cred->bv_val, 1, "ldap");
898 dest_tkt(); /* no point in keeping the tickets */
900 return status == KFAILURE;
906 #endif /* SLAPD_KPASSWD */
909 static int chk_crypt(
910 const struct berval *sc,
911 const struct berval * passwd,
912 const struct berval * cred )
917 for( i=0; i<cred->bv_len; i++) {
918 if(cred->bv_val[i] == '\0') {
919 return 1; /* NUL character in password */
923 if( cred->bv_val[i] != '\0' ) {
924 return -1; /* cred must behave like a string */
927 if( passwd->bv_len < 2 ) {
928 return -1; /* passwd must be at least two characters long */
931 for( i=0; i<passwd->bv_len; i++) {
932 if(passwd->bv_val[i] == '\0') {
933 return -1; /* NUL character in password */
937 if( passwd->bv_val[i] != '\0' ) {
938 return -1; /* passwd must behave like a string */
941 cr = crypt( cred->bv_val, passwd->bv_val );
943 if( cr == NULL || cr[0] == '\0' ) {
944 /* salt must have been invalid */
948 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
951 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
953 const struct berval *sc,
954 const struct berval * passwd,
955 const struct berval * cred )
960 for( i=0; i<cred->bv_len; i++) {
961 if(cred->bv_val[i] == '\0') {
962 return -1; /* NUL character in password */
965 if( cred->bv_val[i] != '\0' ) {
966 return -1; /* cred must behave like a string */
969 for( i=0; i<passwd->bv_len; i++) {
970 if(passwd->bv_val[i] == '\0') {
971 return -1; /* NUL character in password */
975 if( passwd->bv_val[i] != '\0' ) {
976 return -1; /* passwd must behave like a string */
980 struct passwd *pwd = getpwnam(passwd->bv_val);
983 return -1; /* not found */
988 # ifdef HAVE_GETSPNAM
990 struct spwd *spwd = getspnam(passwd->bv_val);
997 # ifdef HAVE_AIX_SECURITY
999 struct userpw *upw = getuserpw(passwd->bv_val);
1002 pw = upw->upw_passwd;
1007 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1008 /* password must must be at least two characters long */
1012 cr = crypt(cred->bv_val, pw);
1014 if( cr == NULL || cr[0] == '\0' ) {
1015 /* salt must have been invalid */
1019 return strcmp(pw, cr) ? 1 : 0;
1025 /* PASSWORD GENERATION ROUTINES */
1027 #ifdef LUTIL_SHA1_BYTES
1028 static struct berval *hash_ssha1(
1029 const struct berval *scheme,
1030 const struct berval *passwd )
1032 lutil_SHA1_CTX SHA1context;
1033 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1034 unsigned char saltdata[4];
1035 struct berval digest;
1038 digest.bv_val = SHA1digest;
1039 digest.bv_len = sizeof(SHA1digest);
1040 salt.bv_val = saltdata;
1041 salt.bv_len = sizeof(saltdata);
1043 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1047 lutil_SHA1Init( &SHA1context );
1048 lutil_SHA1Update( &SHA1context,
1049 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1050 lutil_SHA1Update( &SHA1context,
1051 (const unsigned char *)salt.bv_val, salt.bv_len );
1052 lutil_SHA1Final( SHA1digest, &SHA1context );
1054 return pw_string64( scheme, &digest, &salt);
1057 static struct berval *hash_sha1(
1058 const struct berval *scheme,
1059 const struct berval *passwd )
1061 lutil_SHA1_CTX SHA1context;
1062 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1063 struct berval digest;
1064 digest.bv_val = SHA1digest;
1065 digest.bv_len = sizeof(SHA1digest);
1067 lutil_SHA1Init( &SHA1context );
1068 lutil_SHA1Update( &SHA1context,
1069 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1070 lutil_SHA1Final( SHA1digest, &SHA1context );
1072 return pw_string64( scheme, &digest, NULL);
1076 static struct berval *hash_smd5(
1077 const struct berval *scheme,
1078 const struct berval *passwd )
1080 lutil_MD5_CTX MD5context;
1081 unsigned char MD5digest[LUTIL_MD5_BYTES];
1082 unsigned char saltdata[4];
1083 struct berval digest;
1086 digest.bv_val = MD5digest;
1087 digest.bv_len = sizeof(MD5digest);
1088 salt.bv_val = saltdata;
1089 salt.bv_len = sizeof(saltdata);
1091 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1095 lutil_MD5Init( &MD5context );
1096 lutil_MD5Update( &MD5context,
1097 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1098 lutil_MD5Update( &MD5context,
1099 (const unsigned char *) salt.bv_val, salt.bv_len );
1100 lutil_MD5Final( MD5digest, &MD5context );
1102 return pw_string64( scheme, &digest, &salt );
1105 static struct berval *hash_md5(
1106 const struct berval *scheme,
1107 const struct berval *passwd )
1109 lutil_MD5_CTX MD5context;
1110 unsigned char MD5digest[LUTIL_MD5_BYTES];
1112 struct berval digest;
1114 digest.bv_val = MD5digest;
1115 digest.bv_len = sizeof(MD5digest);
1117 lutil_MD5Init( &MD5context );
1118 lutil_MD5Update( &MD5context,
1119 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1120 lutil_MD5Final( MD5digest, &MD5context );
1122 return pw_string64( scheme, &digest, NULL );
1127 /* pseudocode from RFC2433
1128 * A.2 LmPasswordHash()
1131 * IN 0-to-14-oem-char Password,
1132 * OUT 16-octet PasswordHash )
1134 * Set UcasePassword to the uppercased Password
1135 * Zero pad UcasePassword to 14 characters
1137 * DesHash( 1st 7-octets of UcasePassword,
1138 * giving 1st 8-octets of PasswordHash )
1140 * DesHash( 2nd 7-octets of UcasePassword,
1141 * giving 2nd 8-octets of PasswordHash )
1149 * OUT 8-octet Cypher )
1152 * * Make Cypher an irreversibly encrypted form of Clear by
1153 * * encrypting known text using Clear as the secret key.
1154 * * The known text consists of the string
1159 * Set StdText to "KGS!@#$%"
1160 * DesEncrypt( StdText, Clear, giving Cypher )
1169 * OUT 8-octet Cypher )
1172 * * Use the DES encryption algorithm [4] in ECB mode [9]
1173 * * to encrypt Clear into Cypher such that Cypher can
1174 * * only be decrypted back to Clear by providing Key.
1175 * * Note that the DES algorithm takes as input a 64-bit
1176 * * stream where the 8th, 16th, 24th, etc. bits are
1177 * * parity bits ignored by the encrypting algorithm.
1178 * * Unless you write your own DES to accept 56-bit input
1179 * * without parity, you will need to insert the parity bits
1185 static void lmPasswd_to_key(
1186 const unsigned char *lmPasswd,
1189 /* make room for parity bits */
1190 ((char *)key)[0] = lmPasswd[0];
1191 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1192 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1193 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1194 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1195 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1196 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1197 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1199 des_set_odd_parity( key );
1202 static struct berval *hash_lanman(
1203 const struct berval *scheme,
1204 const struct berval *passwd )
1208 char UcasePassword[15];
1210 des_key_schedule schedule;
1211 des_cblock StdText = "KGS!@#$%";
1212 des_cblock hash1, hash2;
1216 for( i=0; i<passwd->bv_len; i++) {
1217 if(passwd->bv_val[i] == '\0') {
1218 return NULL; /* NUL character in password */
1222 if( passwd->bv_val[i] != '\0' ) {
1223 return NULL; /* passwd must behave like a string */
1226 strncpy( UcasePassword, passwd->bv_val, 14 );
1227 UcasePassword[14] = '\0';
1228 ldap_pvt_str2upper( UcasePassword );
1230 lmPasswd_to_key( UcasePassword, &key );
1231 des_set_key_unchecked( &key, schedule );
1232 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1234 lmPasswd_to_key( &UcasePassword[7], &key );
1235 des_set_key_unchecked( &key, schedule );
1236 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1238 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1239 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1240 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1242 hash.bv_val = lmhash;
1245 return pw_string( scheme, &hash );
1247 #endif /* SLAPD_LMHASH */
1250 static struct berval *hash_crypt(
1251 const struct berval *scheme,
1252 const struct berval *passwd )
1255 unsigned char salt[32]; /* salt suitable for most anything */
1258 for( i=0; i<passwd->bv_len; i++) {
1259 if(passwd->bv_val[i] == '\0') {
1260 return NULL; /* NUL character in password */
1264 if( passwd->bv_val[i] != '\0' ) {
1265 return NULL; /* passwd must behave like a string */
1268 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1272 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1273 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1275 salt[sizeof( salt ) - 1 ] = '\0';
1277 if( salt_format != NULL ) {
1278 /* copy the salt we made into entropy before snprintfing
1279 it back into the salt */
1280 char entropy[sizeof(salt)];
1281 strcpy( entropy, salt );
1282 snprintf( salt, sizeof(entropy), salt_format, entropy );
1285 hash.bv_val = crypt( passwd->bv_val, salt );
1287 if( hash.bv_val == NULL ) return NULL;
1289 hash.bv_len = strlen( hash.bv_val );
1291 if( hash.bv_len == 0 ) {
1295 return pw_string( scheme, &hash );
1299 int lutil_salt_format(const char *format)
1302 free( salt_format );
1304 salt_format = format != NULL ? strdup( format ) : NULL;
1310 #ifdef SLAPD_CLEARTEXT
1311 static struct berval *hash_clear(
1312 const struct berval *scheme,
1313 const struct berval *passwd )
1315 return ber_bvdup( (struct berval *) passwd );