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,
294 struct pw_slist *pws;
296 if ( text ) *text = NULL;
298 if (cred == NULL || cred->bv_len == 0 ||
299 passwd == NULL || passwd->bv_len == 0 )
304 if (!pw_schemes) lutil_passwd_init();
306 for( pws=pw_schemes; pws; pws=pws->next ) {
307 if( pws->s.chk_fn ) {
309 struct berval *p = passwd_scheme( &(pws->s),
310 passwd, &x, schemes );
313 return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
318 #ifdef SLAPD_CLEARTEXT
319 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
320 return (( passwd->bv_len == cred->bv_len ) &&
321 ( passwd->bv_val[0] != '{' /*'}'*/ ))
322 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
329 struct berval * lutil_passwd_generate( ber_len_t len )
333 if( len < 1 ) return NULL;
335 pw = ber_memalloc( sizeof( struct berval ) );
336 if( pw == NULL ) return NULL;
339 pw->bv_val = ber_memalloc( len + 1 );
341 if( pw->bv_val == NULL ) {
346 if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
351 for( len = 0; len < pw->bv_len; len++ ) {
352 pw->bv_val[len] = crypt64[
353 pw->bv_val[len] % (sizeof(crypt64)-1) ];
356 pw->bv_val[len] = '\0';
361 struct berval * lutil_passwd_hash(
362 const struct berval * passwd,
366 const struct pw_scheme *sc = get_scheme( method );
368 if( text ) *text = NULL;
369 if( sc == NULL ) return NULL;
370 if( ! sc->hash_fn ) return NULL;
372 return (sc->hash_fn)( &sc->name, passwd, text );
375 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
376 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
377 static struct berval * pw_string(
378 const struct berval *sc,
379 const struct berval *passwd )
381 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
382 if( pw == NULL ) return NULL;
384 pw->bv_len = sc->bv_len + passwd->bv_len;
385 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
387 if( pw->bv_val == NULL ) {
392 AC_MEMCPY( pw->bv_val, sc->bv_val, sc->bv_len );
393 AC_MEMCPY( &pw->bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
395 pw->bv_val[pw->bv_len] = '\0';
398 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
400 static struct berval * pw_string64(
401 const struct berval *sc,
402 const struct berval *hash,
403 const struct berval *salt )
406 struct berval string;
407 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
410 if( b64 == NULL ) return NULL;
413 /* need to base64 combined string */
414 string.bv_len = hash->bv_len + salt->bv_len;
415 string.bv_val = ber_memalloc( string.bv_len + 1 );
417 if( string.bv_val == NULL ) {
422 AC_MEMCPY( string.bv_val, hash->bv_val,
424 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
426 string.bv_val[string.bv_len] = '\0';
432 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
433 b64->bv_len = b64len + sc->bv_len;
434 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
436 if( b64->bv_val == NULL ) {
437 if( salt ) ber_memfree( string.bv_val );
442 AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
445 (unsigned char *) string.bv_val, string.bv_len,
446 &b64->bv_val[sc->bv_len], b64len );
448 if( salt ) ber_memfree( string.bv_val );
455 /* recompute length */
456 b64->bv_len = sc->bv_len + rc;
457 assert( strlen(b64->bv_val) == b64->bv_len );
461 /* PASSWORD CHECK ROUTINES */
463 #ifdef LUTIL_SHA1_BYTES
464 static int chk_ssha1(
465 const struct berval *sc,
466 const struct berval * passwd,
467 const struct berval * cred,
470 lutil_SHA1_CTX SHA1context;
471 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
473 unsigned char *orig_pass = NULL;
476 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
480 /* decode base64 password */
481 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
482 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
484 if( orig_pass == NULL ) return -1;
486 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
488 if (rc <= sizeof(SHA1digest)) {
489 ber_memfree(orig_pass);
493 /* hash credentials with salt */
494 lutil_SHA1Init(&SHA1context);
495 lutil_SHA1Update(&SHA1context,
496 (const unsigned char *) cred->bv_val, cred->bv_len);
497 lutil_SHA1Update(&SHA1context,
498 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
499 rc - sizeof(SHA1digest));
500 lutil_SHA1Final(SHA1digest, &SHA1context);
503 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
504 ber_memfree(orig_pass);
509 const struct berval *sc,
510 const struct berval * passwd,
511 const struct berval * cred,
514 lutil_SHA1_CTX SHA1context;
515 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
517 unsigned char *orig_pass = NULL;
519 /* base64 un-encode password */
520 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
521 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
523 if( orig_pass == NULL ) return -1;
525 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
527 if( rc != sizeof(SHA1digest) ) {
528 ber_memfree(orig_pass);
532 /* hash credentials with salt */
533 lutil_SHA1Init(&SHA1context);
534 lutil_SHA1Update(&SHA1context,
535 (const unsigned char *) cred->bv_val, cred->bv_len);
536 lutil_SHA1Final(SHA1digest, &SHA1context);
539 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
540 ber_memfree(orig_pass);
546 const struct berval *sc,
547 const struct berval * passwd,
548 const struct berval * cred,
551 lutil_MD5_CTX MD5context;
552 unsigned char MD5digest[LUTIL_MD5_BYTES];
554 unsigned char *orig_pass = NULL;
557 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
561 /* base64 un-encode password */
562 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
563 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
565 if( orig_pass == NULL ) return -1;
567 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
569 if (rc <= sizeof(MD5digest)) {
570 ber_memfree(orig_pass);
574 /* hash credentials with salt */
575 lutil_MD5Init(&MD5context);
576 lutil_MD5Update(&MD5context,
577 (const unsigned char *) cred->bv_val,
579 lutil_MD5Update(&MD5context,
580 &orig_pass[sizeof(MD5digest)],
581 rc - sizeof(MD5digest));
582 lutil_MD5Final(MD5digest, &MD5context);
585 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
586 ber_memfree(orig_pass);
591 const struct berval *sc,
592 const struct berval * passwd,
593 const struct berval * cred,
596 lutil_MD5_CTX MD5context;
597 unsigned char MD5digest[LUTIL_MD5_BYTES];
599 unsigned char *orig_pass = NULL;
601 /* base64 un-encode password */
602 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
603 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
605 if( orig_pass == NULL ) return -1;
607 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
608 if ( rc != sizeof(MD5digest) ) {
609 ber_memfree(orig_pass);
613 /* hash credentials with salt */
614 lutil_MD5Init(&MD5context);
615 lutil_MD5Update(&MD5context,
616 (const unsigned char *) cred->bv_val,
618 lutil_MD5Final(MD5digest, &MD5context);
621 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
622 ber_memfree(orig_pass);
627 static int chk_lanman(
628 const struct berval *scheme,
629 const struct berval *passwd,
630 const struct berval *cred,
635 hash = hash_lanman( scheme, cred );
636 return memcmp( &hash->bv_val[scheme->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 berval *scheme,
643 const struct berval *passwd,
644 const struct berval *cred,
647 lutil_MD5_CTX MD5context;
648 unsigned char MD5digest[LUTIL_MD5_BYTES], c;
649 char buffer[LUTIL_MD5_BYTES + LUTIL_MD5_BYTES + 1];
652 /* hash credentials with salt */
653 lutil_MD5Init(&MD5context);
654 lutil_MD5Update(&MD5context,
655 (const unsigned char *) &passwd->bv_val[32],
659 lutil_MD5Update(&MD5context,
660 (const unsigned char *) &c,
663 lutil_MD5Update(&MD5context,
664 (const unsigned char *) cred->bv_val,
668 lutil_MD5Update(&MD5context,
669 (const unsigned char *) &c,
672 lutil_MD5Update(&MD5context,
673 (const unsigned char *) &passwd->bv_val[32],
676 lutil_MD5Final(MD5digest, &MD5context);
678 for( i=0; i < sizeof( MD5digest ); i++ ) {
679 buffer[i+i] = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F];
680 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F];
684 return memcmp((char *)passwd->bv_val, (char *)buffer, sizeof(buffer))
690 #ifdef HAVE_CYRUS_SASL
691 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
695 const struct berval *sc,
696 const struct berval * passwd,
697 const struct berval * cred,
703 for( i=0; i<cred->bv_len; i++) {
704 if(cred->bv_val[i] == '\0') {
705 return 1; /* NUL character in password */
709 if( cred->bv_val[i] != '\0' ) {
710 return 1; /* cred must behave like a string */
713 for( i=0; i<passwd->bv_len; i++) {
714 if(passwd->bv_val[i] == '\0') {
715 return 1; /* NUL character in password */
719 if( passwd->bv_val[i] != '\0' ) {
720 return 1; /* passwd must behave like a string */
725 #ifdef HAVE_CYRUS_SASL
726 if( lutil_passwd_sasl_conn != NULL ) {
728 # if SASL_VERSION_MAJOR < 2
729 sc = sasl_checkpass( lutil_passwd_sasl_conn,
730 passwd->bv_val, passwd->bv_len,
731 cred->bv_val, cred->bv_len,
734 sc = sasl_checkpass( lutil_passwd_sasl_conn,
735 passwd->bv_val, passwd->bv_len,
736 cred->bv_val, cred->bv_len );
738 rtn = ( sc != SASL_OK );
747 static int chk_kerberos(
748 const struct berval *sc,
749 const struct berval * passwd,
750 const struct berval * cred,
756 for( i=0; i<cred->bv_len; i++) {
757 if(cred->bv_val[i] == '\0') {
758 return 1; /* NUL character in password */
762 if( cred->bv_val[i] != '\0' ) {
763 return 1; /* cred must behave like a string */
766 for( i=0; i<passwd->bv_len; i++) {
767 if(passwd->bv_val[i] == '\0') {
768 return 1; /* NUL character in password */
772 if( passwd->bv_val[i] != '\0' ) {
773 return 1; /* passwd must behave like a string */
778 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
781 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
782 * (Royal Institute of Technology, Stockholm, Sweden).
783 * All rights reserved.
785 * Redistribution and use in source and binary forms, with or without
786 * modification, are permitted provided that the following conditions
789 * 1. Redistributions of source code must retain the above copyright
790 * notice, this list of conditions and the following disclaimer.
792 * 2. Redistributions in binary form must reproduce the above copyright
793 * notice, this list of conditions and the following disclaimer in the
794 * documentation and/or other materials provided with the distribution.
796 * 3. Neither the name of the Institute nor the names of its contributors
797 * may be used to endorse or promote products derived from this software
798 * without specific prior written permission.
800 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
801 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
802 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
803 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
804 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
805 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
806 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
807 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
808 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
809 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
813 krb5_context context;
816 krb5_get_init_creds_opt get_options;
817 krb5_verify_init_creds_opt verify_options;
818 krb5_principal client, server;
820 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
823 ret = krb5_init_context( &context );
829 krb5_get_init_creds_opt_set_preauth_list(&get_options,
833 krb5_get_init_creds_opt_init( &get_options );
835 krb5_verify_init_creds_opt_init( &verify_options );
837 ret = krb5_parse_name( context, passwd->bv_val, &client );
840 krb5_free_context( context );
844 ret = krb5_get_init_creds_password( context,
845 &creds, client, cred->bv_val, NULL,
846 NULL, 0, NULL, &get_options );
849 krb5_free_principal( context, client );
850 krb5_free_context( context );
855 char *host = ldap_pvt_get_fqdn( NULL );
858 krb5_free_principal( context, client );
859 krb5_free_context( context );
863 ret = krb5_sname_to_principal( context,
864 host, "ldap", KRB5_NT_SRV_HST, &server );
870 krb5_free_principal( context, client );
871 krb5_free_context( context );
875 ret = krb5_verify_init_creds( context,
876 &creds, server, NULL, NULL, &verify_options );
878 krb5_free_principal( context, client );
879 krb5_free_principal( context, server );
880 krb5_free_cred_contents( context, &creds );
881 krb5_free_context( context );
885 #elif defined(HAVE_KRB4)
887 /* Borrowed from Heimdal kpopper */
889 * Copyright (c) 1989 Regents of the University of California.
890 * All rights reserved. The Berkeley software License Agreement
891 * specifies the terms and conditions for redistribution.
895 char lrealm[REALM_SZ];
896 char tkt[MAXHOSTNAMELEN];
898 status = krb_get_lrealm(lrealm,1);
899 if (status == KFAILURE) {
903 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
904 TKT_ROOT, (unsigned)getpid());
905 krb_set_tkt_string (tkt);
907 status = krb_verify_user( passwd->bv_val, "", lrealm,
908 cred->bv_val, 1, "ldap");
910 dest_tkt(); /* no point in keeping the tickets */
912 return status == KFAILURE;
918 #endif /* SLAPD_KPASSWD */
921 static int chk_crypt(
922 const struct berval *sc,
923 const struct berval * passwd,
924 const struct berval * cred,
930 for( i=0; i<cred->bv_len; i++) {
931 if(cred->bv_val[i] == '\0') {
932 return 1; /* NUL character in password */
936 if( cred->bv_val[i] != '\0' ) {
937 return -1; /* cred must behave like a string */
940 if( passwd->bv_len < 2 ) {
941 return -1; /* passwd must be at least two characters long */
944 for( i=0; i<passwd->bv_len; i++) {
945 if(passwd->bv_val[i] == '\0') {
946 return -1; /* NUL character in password */
950 if( passwd->bv_val[i] != '\0' ) {
951 return -1; /* passwd must behave like a string */
954 cr = crypt( cred->bv_val, passwd->bv_val );
956 if( cr == NULL || cr[0] == '\0' ) {
957 /* salt must have been invalid */
961 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
964 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
966 const struct berval *sc,
967 const struct berval * passwd,
968 const struct berval * cred,
974 for( i=0; i<cred->bv_len; i++) {
975 if(cred->bv_val[i] == '\0') {
976 return -1; /* NUL character in password */
979 if( cred->bv_val[i] != '\0' ) {
980 return -1; /* cred must behave like a string */
983 for( i=0; i<passwd->bv_len; i++) {
984 if(passwd->bv_val[i] == '\0') {
985 return -1; /* NUL character in password */
989 if( passwd->bv_val[i] != '\0' ) {
990 return -1; /* passwd must behave like a string */
994 struct passwd *pwd = getpwnam(passwd->bv_val);
997 return -1; /* not found */
1000 pw = pwd->pw_passwd;
1002 # ifdef HAVE_GETSPNAM
1004 struct spwd *spwd = getspnam(passwd->bv_val);
1011 # ifdef HAVE_AIX_SECURITY
1013 struct userpw *upw = getuserpw(passwd->bv_val);
1016 pw = upw->upw_passwd;
1021 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1022 /* password must must be at least two characters long */
1026 cr = crypt(cred->bv_val, pw);
1028 if( cr == NULL || cr[0] == '\0' ) {
1029 /* salt must have been invalid */
1033 return strcmp(pw, cr) ? 1 : 0;
1039 /* PASSWORD GENERATION ROUTINES */
1041 #ifdef LUTIL_SHA1_BYTES
1042 static struct berval *hash_ssha1(
1043 const struct berval *scheme,
1044 const struct berval *passwd,
1047 lutil_SHA1_CTX SHA1context;
1048 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1050 struct berval digest;
1053 digest.bv_val = (char *) SHA1digest;
1054 digest.bv_len = sizeof(SHA1digest);
1055 salt.bv_val = saltdata;
1056 salt.bv_len = sizeof(saltdata);
1058 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1062 lutil_SHA1Init( &SHA1context );
1063 lutil_SHA1Update( &SHA1context,
1064 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1065 lutil_SHA1Update( &SHA1context,
1066 (const unsigned char *)salt.bv_val, salt.bv_len );
1067 lutil_SHA1Final( SHA1digest, &SHA1context );
1069 return pw_string64( scheme, &digest, &salt);
1072 static struct berval *hash_sha1(
1073 const struct berval *scheme,
1074 const struct berval *passwd,
1077 lutil_SHA1_CTX SHA1context;
1078 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1079 struct berval digest;
1080 digest.bv_val = (char *) SHA1digest;
1081 digest.bv_len = sizeof(SHA1digest);
1083 lutil_SHA1Init( &SHA1context );
1084 lutil_SHA1Update( &SHA1context,
1085 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1086 lutil_SHA1Final( SHA1digest, &SHA1context );
1088 return pw_string64( scheme, &digest, NULL);
1092 static struct berval *hash_smd5(
1093 const struct berval *scheme,
1094 const struct berval *passwd,
1097 lutil_MD5_CTX MD5context;
1098 unsigned char MD5digest[LUTIL_MD5_BYTES];
1100 struct berval digest;
1103 digest.bv_val = (char *) MD5digest;
1104 digest.bv_len = sizeof(MD5digest);
1105 salt.bv_val = saltdata;
1106 salt.bv_len = sizeof(saltdata);
1108 if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
1112 lutil_MD5Init( &MD5context );
1113 lutil_MD5Update( &MD5context,
1114 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1115 lutil_MD5Update( &MD5context,
1116 (const unsigned char *) salt.bv_val, salt.bv_len );
1117 lutil_MD5Final( MD5digest, &MD5context );
1119 return pw_string64( scheme, &digest, &salt );
1122 static struct berval *hash_md5(
1123 const struct berval *scheme,
1124 const struct berval *passwd,
1127 lutil_MD5_CTX MD5context;
1128 unsigned char MD5digest[LUTIL_MD5_BYTES];
1130 struct berval digest;
1132 digest.bv_val = (char *) MD5digest;
1133 digest.bv_len = sizeof(MD5digest);
1135 lutil_MD5Init( &MD5context );
1136 lutil_MD5Update( &MD5context,
1137 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1138 lutil_MD5Final( MD5digest, &MD5context );
1140 return pw_string64( scheme, &digest, NULL );
1145 /* pseudocode from RFC2433
1146 * A.2 LmPasswordHash()
1149 * IN 0-to-14-oem-char Password,
1150 * OUT 16-octet PasswordHash )
1152 * Set UcasePassword to the uppercased Password
1153 * Zero pad UcasePassword to 14 characters
1155 * DesHash( 1st 7-octets of UcasePassword,
1156 * giving 1st 8-octets of PasswordHash )
1158 * DesHash( 2nd 7-octets of UcasePassword,
1159 * giving 2nd 8-octets of PasswordHash )
1167 * OUT 8-octet Cypher )
1170 * * Make Cypher an irreversibly encrypted form of Clear by
1171 * * encrypting known text using Clear as the secret key.
1172 * * The known text consists of the string
1177 * Set StdText to "KGS!@#$%"
1178 * DesEncrypt( StdText, Clear, giving Cypher )
1187 * OUT 8-octet Cypher )
1190 * * Use the DES encryption algorithm [4] in ECB mode [9]
1191 * * to encrypt Clear into Cypher such that Cypher can
1192 * * only be decrypted back to Clear by providing Key.
1193 * * Note that the DES algorithm takes as input a 64-bit
1194 * * stream where the 8th, 16th, 24th, etc. bits are
1195 * * parity bits ignored by the encrypting algorithm.
1196 * * Unless you write your own DES to accept 56-bit input
1197 * * without parity, you will need to insert the parity bits
1203 static void lmPasswd_to_key(
1204 const unsigned char *lmPasswd,
1207 /* make room for parity bits */
1208 ((char *)key)[0] = lmPasswd[0];
1209 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1210 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1211 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1212 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1213 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1214 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1215 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1217 des_set_odd_parity( key );
1220 static struct berval *hash_lanman(
1221 const struct berval *scheme,
1222 const struct berval *passwd,
1227 char UcasePassword[15];
1229 des_key_schedule schedule;
1230 des_cblock StdText = "KGS!@#$%";
1231 des_cblock hash1, hash2;
1235 for( i=0; i<passwd->bv_len; i++) {
1236 if(passwd->bv_val[i] == '\0') {
1237 return NULL; /* NUL character in password */
1241 if( passwd->bv_val[i] != '\0' ) {
1242 return NULL; /* passwd must behave like a string */
1245 strncpy( UcasePassword, passwd->bv_val, 14 );
1246 UcasePassword[14] = '\0';
1247 ldap_pvt_str2upper( UcasePassword );
1249 lmPasswd_to_key( UcasePassword, &key );
1250 des_set_key_unchecked( &key, schedule );
1251 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1253 lmPasswd_to_key( &UcasePassword[7], &key );
1254 des_set_key_unchecked( &key, schedule );
1255 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1257 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1258 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1259 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1261 hash.bv_val = lmhash;
1264 return pw_string( scheme, &hash );
1266 #endif /* SLAPD_LMHASH */
1269 static struct berval *hash_crypt(
1270 const struct berval *scheme,
1271 const struct berval *passwd,
1275 unsigned char salt[32]; /* salt suitable for most anything */
1278 for( i=0; i<passwd->bv_len; i++) {
1279 if(passwd->bv_val[i] == '\0') {
1280 return NULL; /* NUL character in password */
1284 if( passwd->bv_val[i] != '\0' ) {
1285 return NULL; /* passwd must behave like a string */
1288 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1292 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1293 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1295 salt[sizeof( salt ) - 1 ] = '\0';
1297 if( salt_format != NULL ) {
1298 /* copy the salt we made into entropy before snprintfing
1299 it back into the salt */
1300 char entropy[sizeof(salt)];
1301 strcpy( entropy, (char *) salt );
1302 snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
1305 hash.bv_val = crypt( passwd->bv_val, (char *) salt );
1307 if( hash.bv_val == NULL ) return NULL;
1309 hash.bv_len = strlen( hash.bv_val );
1311 if( hash.bv_len == 0 ) {
1315 return pw_string( scheme, &hash );
1319 int lutil_salt_format(const char *format)
1322 free( salt_format );
1324 salt_format = format != NULL ? strdup( format ) : NULL;
1330 #ifdef SLAPD_CLEARTEXT
1331 static struct berval *hash_clear(
1332 const struct berval *scheme,
1333 const struct berval *passwd,
1336 return ber_bvdup( (struct berval *) passwd );