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;
82 struct pw_slist *next;
83 struct lutil_pw_scheme s;
86 /* password check routines */
88 const struct lutil_pw_scheme *scheme,
89 const struct berval *passwd,
90 const struct berval *cred );
93 const struct lutil_pw_scheme *scheme,
94 const struct berval *passwd,
95 const struct berval *cred );
97 #ifdef LUTIL_SHA1_BYTES
99 const struct lutil_pw_scheme *scheme,
100 const struct berval *passwd,
101 const struct berval *cred );
104 const struct lutil_pw_scheme *scheme,
105 const struct berval *passwd,
106 const struct berval *cred );
110 static int chk_lanman(
111 const struct lutil_pw_scheme *scheme,
112 const struct berval *passwd,
113 const struct berval *cred );
116 #ifdef SLAPD_NS_MTA_MD5
117 static int chk_ns_mta_md5(
118 const struct lutil_pw_scheme *scheme,
119 const struct berval *passwd,
120 const struct berval *cred );
125 const struct lutil_pw_scheme *scheme,
126 const struct berval *passwd,
127 const struct berval *cred );
131 static int chk_kerberos(
132 const struct lutil_pw_scheme *scheme,
133 const struct berval *passwd,
134 const struct berval *cred );
138 static int chk_crypt(
139 const struct lutil_pw_scheme *scheme,
140 const struct berval *passwd,
141 const struct berval *cred );
143 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
145 const struct lutil_pw_scheme *scheme,
146 const struct berval *passwd,
147 const struct berval *cred );
152 #ifdef LUTIL_SHA1_BYTES
153 /* password hash routines */
154 static struct berval *hash_sha1(
155 const struct lutil_pw_scheme *scheme,
156 const struct berval *passwd );
158 static struct berval *hash_ssha1(
159 const struct lutil_pw_scheme *scheme,
160 const struct berval *passwd );
163 static struct berval *hash_smd5(
164 const struct lutil_pw_scheme *scheme,
165 const struct berval *passwd );
167 static struct berval *hash_md5(
168 const struct lutil_pw_scheme *scheme,
169 const struct berval *passwd );
172 static struct berval *hash_lanman(
173 const struct lutil_pw_scheme *scheme,
174 const struct berval *passwd );
178 static struct berval *hash_crypt(
179 const struct lutil_pw_scheme *scheme,
180 const struct berval *passwd );
183 #ifdef SLAPD_CLEARTEXT
184 static struct berval *hash_clear(
185 const struct lutil_pw_scheme *scheme,
186 const struct berval *passwd );
189 static struct pw_slist *pw_schemes;
191 static const struct lutil_pw_scheme pw_schemes_default[] =
193 #ifdef LUTIL_SHA1_BYTES
194 { BER_BVC("{SSHA}"), chk_ssha1, hash_ssha1 },
195 { BER_BVC("{SHA}"), chk_sha1, hash_sha1 },
198 { BER_BVC("{SMD5}"), chk_smd5, hash_smd5 },
199 { BER_BVC("{MD5}"), chk_md5, hash_md5 },
202 { BER_BVC("{LANMAN}"), chk_lanman, hash_lanman },
203 #endif /* SLAPD_LMHASH */
205 #ifdef SLAPD_NS_MTA_MD5
206 { BER_BVC("{NS-MTA-MD5}"), chk_ns_mta_md5, NULL },
207 #endif /* SLAPD_NS_MTA_MD5 */
210 { BER_BVC("{SASL}"), chk_sasl, NULL },
214 { BER_BVC("{KERBEROS}"), chk_kerberos, NULL },
218 { BER_BVC("{CRYPT}"), chk_crypt, hash_crypt },
219 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
220 { BER_BVC("{UNIX}"), chk_unix, NULL },
224 #ifdef SLAPD_CLEARTEXT
226 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
229 { BER_BVNULL, NULL, NULL }
232 int lutil_passwd_add(struct lutil_pw_scheme *s)
234 struct pw_slist *ptr;
236 ptr = ber_memalloc( sizeof( struct pw_slist ));
238 ptr->next = pw_schemes;
244 void lutil_passwd_init()
246 struct pw_slist *ptr;
247 struct lutil_pw_scheme *s;
249 for( s=(struct lutil_pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
250 if ( lutil_passwd_add( s )) break;
254 void lutil_passwd_destroy()
256 struct pw_slist *ptr, *next;
258 for( ptr=pw_schemes; ptr; ptr=next ) {
264 static const struct lutil_pw_scheme *get_scheme(
267 struct pw_slist *pws;
269 if (!pw_schemes) lutil_passwd_init();
271 for( pws=pw_schemes; pws; pws=pws->next ) {
272 if( strcasecmp(scheme, pws->s.name.bv_val ) == 0 ) {
280 int lutil_passwd_scheme(
283 if( scheme == NULL ) {
287 return get_scheme(scheme) != NULL;
291 static int is_allowed_scheme(
293 const char** schemes )
297 if( schemes == NULL ) return 1;
299 for( i=0; schemes[i] != NULL; i++ ) {
300 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
307 static struct berval *passwd_scheme(
308 const struct lutil_pw_scheme *scheme,
309 const struct berval * passwd,
311 const char** allowed )
313 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
317 if( passwd->bv_len >= scheme->name.bv_len ) {
318 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
319 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
320 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
330 * Return 0 if creds are good.
334 const struct berval *passwd, /* stored passwd */
335 const struct berval *cred, /* user cred */
336 const char **schemes )
338 struct pw_slist *pws;
340 if (cred == NULL || cred->bv_len == 0 ||
341 passwd == NULL || passwd->bv_len == 0 )
346 if (!pw_schemes) lutil_passwd_init();
348 for( pws=pw_schemes; pws; pws=pws->next ) {
349 if( pws->s.chk_fn ) {
351 struct berval *p = passwd_scheme( &(pws->s),
352 passwd, &x, schemes );
355 return (pws->s.chk_fn)( &(pws->s), p, cred );
360 #ifdef SLAPD_CLEARTEXT
361 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
362 return (( passwd->bv_len == cred->bv_len ) &&
363 ( passwd->bv_val[0] != '{' /*'}'*/ ))
364 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
371 struct berval * lutil_passwd_generate( ber_len_t len )
375 if( len < 1 ) return NULL;
377 pw = ber_memalloc( sizeof( struct berval ) );
378 if( pw == NULL ) return NULL;
381 pw->bv_val = ber_memalloc( len + 1 );
383 if( pw->bv_val == NULL ) {
388 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
393 for( len = 0; len < pw->bv_len; len++ ) {
394 pw->bv_val[len] = crypt64[
395 pw->bv_val[len] % (sizeof(crypt64)-1) ];
398 pw->bv_val[len] = '\0';
403 struct berval * lutil_passwd_hash(
404 const struct berval * passwd,
405 const char * method )
407 const struct lutil_pw_scheme *sc = get_scheme( method );
409 if( sc == NULL ) return NULL;
410 if( ! sc->hash_fn ) return NULL;
412 return (sc->hash_fn)( sc, passwd );
415 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
416 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
417 static struct berval * pw_string(
418 const struct lutil_pw_scheme *sc,
419 const struct berval *passwd )
421 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
422 if( pw == NULL ) return NULL;
424 pw->bv_len = sc->name.bv_len + passwd->bv_len;
425 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
427 if( pw->bv_val == NULL ) {
432 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
433 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
435 pw->bv_val[pw->bv_len] = '\0';
438 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
440 static struct berval * pw_string64(
441 const struct lutil_pw_scheme *sc,
442 const struct berval *hash,
443 const struct berval *salt )
446 struct berval string;
447 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
450 if( b64 == NULL ) return NULL;
453 /* need to base64 combined string */
454 string.bv_len = hash->bv_len + salt->bv_len;
455 string.bv_val = ber_memalloc( string.bv_len + 1 );
457 if( string.bv_val == NULL ) {
462 AC_MEMCPY( string.bv_val, hash->bv_val,
464 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
466 string.bv_val[string.bv_len] = '\0';
472 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
473 b64->bv_len = b64len + sc->name.bv_len;
474 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
476 if( b64->bv_val == NULL ) {
477 if( salt ) ber_memfree( string.bv_val );
482 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
485 string.bv_val, string.bv_len,
486 &b64->bv_val[sc->name.bv_len], b64len );
488 if( salt ) ber_memfree( string.bv_val );
495 /* recompute length */
496 b64->bv_len = sc->name.bv_len + rc;
497 assert( strlen(b64->bv_val) == b64->bv_len );
501 /* PASSWORD CHECK ROUTINES */
503 #ifdef LUTIL_SHA1_BYTES
504 static int chk_ssha1(
505 const struct lutil_pw_scheme *sc,
506 const struct berval * passwd,
507 const struct berval * cred )
509 lutil_SHA1_CTX SHA1context;
510 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
512 unsigned char *orig_pass = NULL;
515 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
519 /* decode base64 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_SHA1Update(&SHA1context,
537 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
538 rc - sizeof(SHA1digest));
539 lutil_SHA1Final(SHA1digest, &SHA1context);
542 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
543 ber_memfree(orig_pass);
548 const struct lutil_pw_scheme *sc,
549 const struct berval * passwd,
550 const struct berval * cred )
552 lutil_SHA1_CTX SHA1context;
553 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
555 unsigned char *orig_pass = NULL;
557 /* base64 un-encode password */
558 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
559 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
561 if( orig_pass == NULL ) return -1;
563 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
565 if( rc != sizeof(SHA1digest) ) {
566 ber_memfree(orig_pass);
570 /* hash credentials with salt */
571 lutil_SHA1Init(&SHA1context);
572 lutil_SHA1Update(&SHA1context,
573 (const unsigned char *) cred->bv_val, cred->bv_len);
574 lutil_SHA1Final(SHA1digest, &SHA1context);
577 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
578 ber_memfree(orig_pass);
584 const struct lutil_pw_scheme *sc,
585 const struct berval * passwd,
586 const struct berval * cred )
588 lutil_MD5_CTX MD5context;
589 unsigned char MD5digest[LUTIL_MD5_BYTES];
591 unsigned char *orig_pass = NULL;
594 if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
598 /* base64 un-encode password */
599 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
600 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
602 if( orig_pass == NULL ) return -1;
604 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
606 if (rc <= sizeof(MD5digest)) {
607 ber_memfree(orig_pass);
611 /* hash credentials with salt */
612 lutil_MD5Init(&MD5context);
613 lutil_MD5Update(&MD5context,
614 (const unsigned char *) cred->bv_val,
616 lutil_MD5Update(&MD5context,
617 &orig_pass[sizeof(MD5digest)],
618 rc - sizeof(MD5digest));
619 lutil_MD5Final(MD5digest, &MD5context);
622 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
623 ber_memfree(orig_pass);
628 const struct lutil_pw_scheme *sc,
629 const struct berval * passwd,
630 const struct berval * cred )
632 lutil_MD5_CTX MD5context;
633 unsigned char MD5digest[LUTIL_MD5_BYTES];
635 unsigned char *orig_pass = NULL;
637 /* base64 un-encode password */
638 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
639 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
641 if( orig_pass == NULL ) return -1;
643 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
644 if ( rc != sizeof(MD5digest) ) {
645 ber_memfree(orig_pass);
649 /* hash credentials with salt */
650 lutil_MD5Init(&MD5context);
651 lutil_MD5Update(&MD5context,
652 (const unsigned char *) cred->bv_val,
654 lutil_MD5Final(MD5digest, &MD5context);
657 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
658 ber_memfree(orig_pass);
663 static int chk_lanman(
664 const struct lutil_pw_scheme *scheme,
665 const struct berval *passwd,
666 const struct berval *cred )
670 hash = hash_lanman( scheme, cred );
671 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
673 #endif /* SLAPD_LMHASH */
675 #ifdef SLAPD_NS_MTA_MD5
676 static int chk_ns_mta_md5(
677 const struct lutil_pw_scheme *scheme,
678 const struct berval *passwd,
679 const struct berval *cred )
681 lutil_MD5_CTX MD5context;
682 unsigned char MD5digest[LUTIL_MD5_BYTES], c;
683 char buffer[LUTIL_MD5_BYTES + LUTIL_MD5_BYTES + 1];
686 /* hash credentials with salt */
687 lutil_MD5Init(&MD5context);
688 lutil_MD5Update(&MD5context,
689 (const unsigned char *) &passwd->bv_val[32],
693 lutil_MD5Update(&MD5context,
694 (const unsigned char *) &c,
697 lutil_MD5Update(&MD5context,
698 (const unsigned char *) cred->bv_val,
702 lutil_MD5Update(&MD5context,
703 (const unsigned char *) &c,
706 lutil_MD5Update(&MD5context,
707 (const unsigned char *) &passwd->bv_val[32],
710 lutil_MD5Final(MD5digest, &MD5context);
712 for( i=0; i < sizeof( MD5digest ); i++ ) {
713 buffer[i+i] = "0123456789abcdef"[(MD5digest[i]>>4) & 0x0F];
714 buffer[i+i+1] = "0123456789abcdef"[ MD5digest[i] & 0x0F];
718 return memcmp((char *)passwd->bv_val, (char *)buffer, sizeof(buffer))
724 #ifdef HAVE_CYRUS_SASL
725 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
729 const struct lutil_pw_scheme *sc,
730 const struct berval * passwd,
731 const struct berval * cred )
736 for( i=0; i<cred->bv_len; i++) {
737 if(cred->bv_val[i] == '\0') {
738 return 1; /* NUL character in password */
742 if( cred->bv_val[i] != '\0' ) {
743 return 1; /* cred must behave like a string */
746 for( i=0; i<passwd->bv_len; i++) {
747 if(passwd->bv_val[i] == '\0') {
748 return 1; /* NUL character in password */
752 if( passwd->bv_val[i] != '\0' ) {
753 return 1; /* passwd must behave like a string */
758 #ifdef HAVE_CYRUS_SASL
759 if( lutil_passwd_sasl_conn != NULL ) {
761 # if SASL_VERSION_MAJOR < 2
762 const char *errstr = NULL;
763 sc = sasl_checkpass( lutil_passwd_sasl_conn,
764 passwd->bv_val, passwd->bv_len,
765 cred->bv_val, cred->bv_len,
768 sc = sasl_checkpass( lutil_passwd_sasl_conn,
769 passwd->bv_val, passwd->bv_len,
770 cred->bv_val, cred->bv_len );
772 rtn = ( sc != SASL_OK );
781 static int chk_kerberos(
782 const struct lutil_pw_scheme *sc,
783 const struct berval * passwd,
784 const struct berval * cred )
789 for( i=0; i<cred->bv_len; i++) {
790 if(cred->bv_val[i] == '\0') {
791 return 1; /* NUL character in password */
795 if( cred->bv_val[i] != '\0' ) {
796 return 1; /* cred must behave like a string */
799 for( i=0; i<passwd->bv_len; i++) {
800 if(passwd->bv_val[i] == '\0') {
801 return 1; /* NUL character in password */
805 if( passwd->bv_val[i] != '\0' ) {
806 return 1; /* passwd must behave like a string */
811 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
814 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
815 * (Royal Institute of Technology, Stockholm, Sweden).
816 * All rights reserved.
818 * Redistribution and use in source and binary forms, with or without
819 * modification, are permitted provided that the following conditions
822 * 1. Redistributions of source code must retain the above copyright
823 * notice, this list of conditions and the following disclaimer.
825 * 2. Redistributions in binary form must reproduce the above copyright
826 * notice, this list of conditions and the following disclaimer in the
827 * documentation and/or other materials provided with the distribution.
829 * 3. Neither the name of the Institute nor the names of its contributors
830 * may be used to endorse or promote products derived from this software
831 * without specific prior written permission.
833 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
834 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
835 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
836 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
837 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
838 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
839 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
840 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
841 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
842 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
846 krb5_context context;
849 krb5_get_init_creds_opt get_options;
850 krb5_verify_init_creds_opt verify_options;
851 krb5_principal client, server;
853 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
856 ret = krb5_init_context( &context );
862 krb5_get_init_creds_opt_set_preauth_list(&get_options,
866 krb5_get_init_creds_opt_init( &get_options );
868 krb5_verify_init_creds_opt_init( &verify_options );
870 ret = krb5_parse_name( context, passwd->bv_val, &client );
873 krb5_free_context( context );
877 ret = krb5_get_init_creds_password( context,
878 &creds, client, cred->bv_val, NULL,
879 NULL, 0, NULL, &get_options );
882 krb5_free_principal( context, client );
883 krb5_free_context( context );
888 char *host = ldap_pvt_get_fqdn( NULL );
891 krb5_free_principal( context, client );
892 krb5_free_context( context );
896 ret = krb5_sname_to_principal( context,
897 host, "ldap", KRB5_NT_SRV_HST, &server );
903 krb5_free_principal( context, client );
904 krb5_free_context( context );
908 ret = krb5_verify_init_creds( context,
909 &creds, server, NULL, NULL, &verify_options );
911 krb5_free_principal( context, client );
912 krb5_free_principal( context, server );
913 krb5_free_cred_contents( context, &creds );
914 krb5_free_context( context );
918 #elif defined(HAVE_KRB4)
920 /* Borrowed from Heimdal kpopper */
922 * Copyright (c) 1989 Regents of the University of California.
923 * All rights reserved. The Berkeley software License Agreement
924 * specifies the terms and conditions for redistribution.
928 char lrealm[REALM_SZ];
929 char tkt[MAXHOSTNAMELEN];
931 status = krb_get_lrealm(lrealm,1);
932 if (status == KFAILURE) {
936 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
937 TKT_ROOT, (unsigned)getpid());
938 krb_set_tkt_string (tkt);
940 status = krb_verify_user( passwd->bv_val, "", lrealm,
941 cred->bv_val, 1, "ldap");
943 dest_tkt(); /* no point in keeping the tickets */
945 return status == KFAILURE;
951 #endif /* SLAPD_KPASSWD */
954 static int chk_crypt(
955 const struct lutil_pw_scheme *sc,
956 const struct berval * passwd,
957 const struct berval * cred )
962 for( i=0; i<cred->bv_len; i++) {
963 if(cred->bv_val[i] == '\0') {
964 return 1; /* NUL character in password */
968 if( cred->bv_val[i] != '\0' ) {
969 return -1; /* cred must behave like a string */
972 if( passwd->bv_len < 2 ) {
973 return -1; /* passwd must be at least two characters long */
976 for( i=0; i<passwd->bv_len; i++) {
977 if(passwd->bv_val[i] == '\0') {
978 return -1; /* NUL character in password */
982 if( passwd->bv_val[i] != '\0' ) {
983 return -1; /* passwd must behave like a string */
986 cr = crypt( cred->bv_val, passwd->bv_val );
988 if( cr == NULL || cr[0] == '\0' ) {
989 /* salt must have been invalid */
993 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
996 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
998 const struct lutil_pw_scheme *sc,
999 const struct berval * passwd,
1000 const struct berval * cred )
1005 for( i=0; i<cred->bv_len; i++) {
1006 if(cred->bv_val[i] == '\0') {
1007 return -1; /* NUL character in password */
1010 if( cred->bv_val[i] != '\0' ) {
1011 return -1; /* cred must behave like a string */
1014 for( i=0; i<passwd->bv_len; i++) {
1015 if(passwd->bv_val[i] == '\0') {
1016 return -1; /* NUL character in password */
1020 if( passwd->bv_val[i] != '\0' ) {
1021 return -1; /* passwd must behave like a string */
1025 struct passwd *pwd = getpwnam(passwd->bv_val);
1028 return -1; /* not found */
1031 pw = pwd->pw_passwd;
1033 # ifdef HAVE_GETSPNAM
1035 struct spwd *spwd = getspnam(passwd->bv_val);
1042 # ifdef HAVE_AIX_SECURITY
1044 struct userpw *upw = getuserpw(passwd->bv_val);
1047 pw = upw->upw_passwd;
1052 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
1053 /* password must must be at least two characters long */
1057 cr = crypt(cred->bv_val, pw);
1059 if( cr == NULL || cr[0] == '\0' ) {
1060 /* salt must have been invalid */
1064 return strcmp(pw, cr) ? 1 : 0;
1070 /* PASSWORD GENERATION ROUTINES */
1072 #ifdef LUTIL_SHA1_BYTES
1073 static struct berval *hash_ssha1(
1074 const struct lutil_pw_scheme *scheme,
1075 const struct berval *passwd )
1077 lutil_SHA1_CTX SHA1context;
1078 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1079 unsigned char saltdata[4];
1080 struct berval digest;
1083 digest.bv_val = SHA1digest;
1084 digest.bv_len = sizeof(SHA1digest);
1085 salt.bv_val = saltdata;
1086 salt.bv_len = sizeof(saltdata);
1088 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1092 lutil_SHA1Init( &SHA1context );
1093 lutil_SHA1Update( &SHA1context,
1094 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1095 lutil_SHA1Update( &SHA1context,
1096 (const unsigned char *)salt.bv_val, salt.bv_len );
1097 lutil_SHA1Final( SHA1digest, &SHA1context );
1099 return pw_string64( scheme, &digest, &salt);
1102 static struct berval *hash_sha1(
1103 const struct lutil_pw_scheme *scheme,
1104 const struct berval *passwd )
1106 lutil_SHA1_CTX SHA1context;
1107 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1108 struct berval digest;
1109 digest.bv_val = SHA1digest;
1110 digest.bv_len = sizeof(SHA1digest);
1112 lutil_SHA1Init( &SHA1context );
1113 lutil_SHA1Update( &SHA1context,
1114 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1115 lutil_SHA1Final( SHA1digest, &SHA1context );
1117 return pw_string64( scheme, &digest, NULL);
1121 static struct berval *hash_smd5(
1122 const struct lutil_pw_scheme *scheme,
1123 const struct berval *passwd )
1125 lutil_MD5_CTX MD5context;
1126 unsigned char MD5digest[LUTIL_MD5_BYTES];
1127 unsigned char saltdata[4];
1128 struct berval digest;
1131 digest.bv_val = MD5digest;
1132 digest.bv_len = sizeof(MD5digest);
1133 salt.bv_val = saltdata;
1134 salt.bv_len = sizeof(saltdata);
1136 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1140 lutil_MD5Init( &MD5context );
1141 lutil_MD5Update( &MD5context,
1142 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1143 lutil_MD5Update( &MD5context,
1144 (const unsigned char *) salt.bv_val, salt.bv_len );
1145 lutil_MD5Final( MD5digest, &MD5context );
1147 return pw_string64( scheme, &digest, &salt );
1150 static struct berval *hash_md5(
1151 const struct lutil_pw_scheme *scheme,
1152 const struct berval *passwd )
1154 lutil_MD5_CTX MD5context;
1155 unsigned char MD5digest[LUTIL_MD5_BYTES];
1157 struct berval digest;
1159 digest.bv_val = MD5digest;
1160 digest.bv_len = sizeof(MD5digest);
1162 lutil_MD5Init( &MD5context );
1163 lutil_MD5Update( &MD5context,
1164 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1165 lutil_MD5Final( MD5digest, &MD5context );
1167 return pw_string64( scheme, &digest, NULL );
1172 /* pseudocode from RFC2433
1173 * A.2 LmPasswordHash()
1176 * IN 0-to-14-oem-char Password,
1177 * OUT 16-octet PasswordHash )
1179 * Set UcasePassword to the uppercased Password
1180 * Zero pad UcasePassword to 14 characters
1182 * DesHash( 1st 7-octets of UcasePassword,
1183 * giving 1st 8-octets of PasswordHash )
1185 * DesHash( 2nd 7-octets of UcasePassword,
1186 * giving 2nd 8-octets of PasswordHash )
1194 * OUT 8-octet Cypher )
1197 * * Make Cypher an irreversibly encrypted form of Clear by
1198 * * encrypting known text using Clear as the secret key.
1199 * * The known text consists of the string
1204 * Set StdText to "KGS!@#$%"
1205 * DesEncrypt( StdText, Clear, giving Cypher )
1214 * OUT 8-octet Cypher )
1217 * * Use the DES encryption algorithm [4] in ECB mode [9]
1218 * * to encrypt Clear into Cypher such that Cypher can
1219 * * only be decrypted back to Clear by providing Key.
1220 * * Note that the DES algorithm takes as input a 64-bit
1221 * * stream where the 8th, 16th, 24th, etc. bits are
1222 * * parity bits ignored by the encrypting algorithm.
1223 * * Unless you write your own DES to accept 56-bit input
1224 * * without parity, you will need to insert the parity bits
1230 static void lmPasswd_to_key(
1231 const unsigned char *lmPasswd,
1234 /* make room for parity bits */
1235 ((char *)key)[0] = lmPasswd[0];
1236 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1237 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1238 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1239 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1240 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1241 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1242 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1244 des_set_odd_parity( key );
1247 static struct berval *hash_lanman(
1248 const struct lutil_pw_scheme *scheme,
1249 const struct berval *passwd )
1253 char UcasePassword[15];
1255 des_key_schedule schedule;
1256 des_cblock StdText = "KGS!@#$%";
1257 des_cblock hash1, hash2;
1261 for( i=0; i<passwd->bv_len; i++) {
1262 if(passwd->bv_val[i] == '\0') {
1263 return NULL; /* NUL character in password */
1267 if( passwd->bv_val[i] != '\0' ) {
1268 return NULL; /* passwd must behave like a string */
1271 strncpy( UcasePassword, passwd->bv_val, 14 );
1272 UcasePassword[14] = '\0';
1273 ldap_pvt_str2upper( UcasePassword );
1275 lmPasswd_to_key( UcasePassword, &key );
1276 des_set_key_unchecked( &key, schedule );
1277 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1279 lmPasswd_to_key( &UcasePassword[7], &key );
1280 des_set_key_unchecked( &key, schedule );
1281 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1283 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1284 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1285 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1287 hash.bv_val = lmhash;
1290 return pw_string( scheme, &hash );
1292 #endif /* SLAPD_LMHASH */
1295 static struct berval *hash_crypt(
1296 const struct lutil_pw_scheme *scheme,
1297 const struct berval *passwd )
1300 unsigned char salt[32]; /* salt suitable for most anything */
1303 for( i=0; i<passwd->bv_len; i++) {
1304 if(passwd->bv_val[i] == '\0') {
1305 return NULL; /* NUL character in password */
1309 if( passwd->bv_val[i] != '\0' ) {
1310 return NULL; /* passwd must behave like a string */
1313 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1317 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1318 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1320 salt[sizeof( salt ) - 1 ] = '\0';
1322 if( salt_format != NULL ) {
1323 /* copy the salt we made into entropy before snprintfing
1324 it back into the salt */
1325 char entropy[sizeof(salt)];
1326 strcpy( entropy, salt );
1327 snprintf( salt, sizeof(entropy), salt_format, entropy );
1330 hash.bv_val = crypt( passwd->bv_val, salt );
1332 if( hash.bv_val == NULL ) return NULL;
1334 hash.bv_len = strlen( hash.bv_val );
1336 if( hash.bv_len == 0 ) {
1340 return pw_string( scheme, &hash );
1344 int lutil_salt_format(const char *format)
1347 free( salt_format );
1349 salt_format = format != NULL ? strdup( format ) : NULL;
1355 #ifdef SLAPD_CLEARTEXT
1356 static struct berval *hash_clear(
1357 const struct lutil_pw_scheme *scheme,
1358 const struct berval *passwd )
1360 return ber_bvdup( (struct berval *) passwd );