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>
36 #include <ac/unistd.h>
45 #ifdef HAVE_AIX_SECURITY
51 #include "lutil_md5.h"
52 #include "lutil_sha1.h"
55 static const unsigned char crypt64[] =
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
60 typedef int (*PASSWD_CHK_FUNC)(
61 const struct pw_scheme *scheme,
62 const struct berval *passwd,
63 const struct berval *cred );
65 typedef struct berval * (*PASSWD_HASH_FUNC) (
66 const struct pw_scheme *scheme,
67 const struct berval *passwd );
71 PASSWD_CHK_FUNC chk_fn;
72 PASSWD_HASH_FUNC hash_fn;
75 /* password check routines */
77 const struct pw_scheme *scheme,
78 const struct berval *passwd,
79 const struct berval *cred );
82 const struct pw_scheme *scheme,
83 const struct berval *passwd,
84 const struct berval *cred );
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 );
98 const struct pw_scheme *scheme,
99 const struct berval *passwd,
100 const struct berval *cred );
104 static int chk_kerberos(
105 const struct pw_scheme *scheme,
106 const struct berval *passwd,
107 const struct berval *cred );
111 static int chk_crypt(
112 const struct pw_scheme *scheme,
113 const struct berval *passwd,
114 const struct berval *cred );
116 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
118 const struct pw_scheme *scheme,
119 const struct berval *passwd,
120 const struct berval *cred );
125 /* password hash routines */
126 static struct berval *hash_sha1(
127 const struct pw_scheme *scheme,
128 const struct berval *passwd );
130 static struct berval *hash_ssha1(
131 const struct pw_scheme *scheme,
132 const struct berval *passwd );
134 static struct berval *hash_smd5(
135 const struct pw_scheme *scheme,
136 const struct berval *passwd );
138 static struct berval *hash_md5(
139 const struct pw_scheme *scheme,
140 const struct berval *passwd );
143 static struct berval *hash_crypt(
144 const struct pw_scheme *scheme,
145 const struct berval *passwd );
149 static const struct pw_scheme pw_schemes[] =
151 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
152 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
154 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
155 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
158 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
162 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
166 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
167 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
168 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
172 #ifdef SLAPD_CLEARTEXT
174 { {0, "{CLEARTEXT}"}, NULL, NULL },
177 { {0, NULL}, NULL, NULL }
180 static const struct pw_scheme *get_scheme(
185 for( i=0; pw_schemes[i].name.bv_val; i++) {
186 if( pw_schemes[i].name.bv_len == 0 ) continue;
188 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
189 pw_schemes[i].name.bv_len) == 0 )
191 return &pw_schemes[i];
198 int lutil_passwd_scheme(
201 if( scheme == NULL ) {
205 return get_scheme(scheme) != NULL;
209 static int is_allowed_scheme(
211 const char** schemes )
215 if( schemes == NULL ) return 1;
217 for( i=0; schemes[i] != NULL; i++ ) {
218 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
225 static struct berval *passwd_scheme(
226 const struct pw_scheme *scheme,
227 const struct berval * passwd,
228 const char** allowed )
230 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
234 if( passwd->bv_len >= scheme->name.bv_len ) {
235 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
236 struct berval *bv = ber_memalloc( sizeof(struct berval) );
238 if( bv == NULL ) return NULL;
240 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
241 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
251 * Return 0 if creds are good.
255 const struct berval *passwd, /* stored passwd */
256 const struct berval *cred, /* user cred */
257 const char **schemes )
261 if (cred == NULL || cred->bv_len == 0 ||
262 passwd == NULL || passwd->bv_len == 0 )
267 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
268 if( pw_schemes[i].chk_fn ) {
269 struct berval *p = passwd_scheme( &pw_schemes[i],
273 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
275 /* only free the berval structure as the bv_val points
276 * into passwd->bv_val
285 #ifdef SLAPD_CLEARTEXT
286 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
287 return passwd->bv_len == cred->bv_len
288 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
295 struct berval * lutil_passwd_generate( ber_len_t len )
299 if( len < 1 ) return NULL;
301 pw = ber_memalloc( sizeof( struct berval ) );
302 if( pw == NULL ) return NULL;
305 pw->bv_val = ber_memalloc( len + 1 );
307 if( pw->bv_val == NULL ) {
312 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
317 for( len = 0; len < pw->bv_len; len++ ) {
318 pw->bv_val[len] = crypt64[
319 pw->bv_val[len] % (sizeof(crypt64)-1) ];
322 pw->bv_val[len] = '\0';
327 struct berval * lutil_passwd_hash(
328 const struct berval * passwd,
329 const char * method )
331 const struct pw_scheme *sc = get_scheme( method );
333 if( sc == NULL ) return NULL;
334 if( ! sc->hash_fn ) return NULL;
336 return (sc->hash_fn)( sc, passwd );
339 static struct berval * pw_string(
340 const struct pw_scheme *sc,
341 const struct berval *passwd )
343 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
344 if( pw == NULL ) return NULL;
346 pw->bv_len = sc->name.bv_len + passwd->bv_len;
347 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
349 if( pw->bv_val == NULL ) {
354 memcpy( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
355 memcpy( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
357 pw->bv_val[pw->bv_len] = '\0';
361 static struct berval * pw_string64(
362 const struct pw_scheme *sc,
363 const struct berval *hash,
364 const struct berval *salt )
367 struct berval string;
368 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
371 if( b64 == NULL ) return NULL;
374 /* need to base64 combined string */
375 string.bv_len = hash->bv_len + salt->bv_len;
376 string.bv_val = ber_memalloc( string.bv_len + 1 );
378 if( string.bv_val == NULL ) {
383 memcpy( string.bv_val, hash->bv_val,
385 memcpy( &string.bv_val[hash->bv_len], salt->bv_val,
387 string.bv_val[string.bv_len] = '\0';
393 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
394 b64->bv_len = b64len + sc->name.bv_len;
395 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
397 if( b64->bv_val == NULL ) {
398 if( salt ) ber_memfree( string.bv_val );
403 memcpy(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
406 string.bv_val, string.bv_len,
407 &b64->bv_val[sc->name.bv_len], b64len );
409 if( salt ) ber_memfree( string.bv_val );
416 /* recompute length */
417 b64->bv_len = sc->name.bv_len + rc;
418 assert( strlen(b64->bv_val) == b64->bv_len );
422 /* PASSWORD CHECK ROUTINES */
424 static int chk_ssha1(
425 const struct pw_scheme *sc,
426 const struct berval * passwd,
427 const struct berval * cred )
429 lutil_SHA1_CTX SHA1context;
430 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
432 unsigned char *orig_pass = NULL;
434 /* decode base64 password */
435 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
436 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
438 if( orig_pass == NULL ) return -1;
440 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
443 ber_memfree(orig_pass);
447 /* hash credentials with salt */
448 lutil_SHA1Init(&SHA1context);
449 lutil_SHA1Update(&SHA1context,
450 (const unsigned char *) cred->bv_val, cred->bv_len);
451 lutil_SHA1Update(&SHA1context,
452 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
453 rc - sizeof(SHA1digest));
454 lutil_SHA1Final(SHA1digest, &SHA1context);
457 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
458 ber_memfree(orig_pass);
463 const struct pw_scheme *sc,
464 const struct berval * passwd,
465 const struct berval * cred )
467 lutil_SHA1_CTX SHA1context;
468 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
470 unsigned char *orig_pass = NULL;
472 /* base64 un-encode password */
473 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
474 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
476 if( orig_pass == NULL ) return -1;
478 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
480 if( rc != sizeof(SHA1digest) ) {
481 ber_memfree(orig_pass);
485 /* hash credentials with salt */
486 lutil_SHA1Init(&SHA1context);
487 lutil_SHA1Update(&SHA1context,
488 (const unsigned char *) cred->bv_val, cred->bv_len);
489 lutil_SHA1Final(SHA1digest, &SHA1context);
492 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
493 ber_memfree(orig_pass);
498 const struct pw_scheme *sc,
499 const struct berval * passwd,
500 const struct berval * cred )
502 lutil_MD5_CTX MD5context;
503 unsigned char MD5digest[LUTIL_MD5_BYTES];
505 unsigned char *orig_pass = NULL;
507 /* base64 un-encode password */
508 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
509 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
511 if( orig_pass == NULL ) return -1;
513 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
515 ber_memfree(orig_pass);
519 /* hash credentials with salt */
520 lutil_MD5Init(&MD5context);
521 lutil_MD5Update(&MD5context,
522 (const unsigned char *) cred->bv_val, cred->bv_len );
523 lutil_MD5Update(&MD5context,
524 (const unsigned char *) &orig_pass[sizeof(MD5digest)],
525 rc - sizeof(MD5digest));
526 lutil_MD5Final(MD5digest, &MD5context);
529 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
530 ber_memfree(orig_pass);
535 const struct pw_scheme *sc,
536 const struct berval * passwd,
537 const struct berval * cred )
539 lutil_MD5_CTX MD5context;
540 unsigned char MD5digest[LUTIL_MD5_BYTES];
542 unsigned char *orig_pass = NULL;
544 /* base64 un-encode password */
545 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
546 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
548 if( orig_pass == NULL ) return -1;
550 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
551 if ( rc != sizeof(MD5digest) ) {
552 ber_memfree(orig_pass);
556 /* hash credentials with salt */
557 lutil_MD5Init(&MD5context);
558 lutil_MD5Update(&MD5context,
559 (const unsigned char *) cred->bv_val, cred->bv_len );
560 lutil_MD5Final(MD5digest, &MD5context);
563 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
564 ber_memfree(orig_pass);
569 #ifdef HAVE_CYRUS_SASL
570 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
574 const struct pw_scheme *sc,
575 const struct berval * passwd,
576 const struct berval * cred )
581 for( i=0; i<cred->bv_len; i++) {
582 if(cred->bv_val[i] == '\0') {
583 return 1; /* NUL character in password */
587 if( cred->bv_val[i] != '\0' ) {
588 return 1; /* cred must behave like a string */
591 for( i=0; i<passwd->bv_len; i++) {
592 if(passwd->bv_val[i] == '\0') {
593 return 1; /* NUL character in password */
597 if( passwd->bv_val[i] != '\0' ) {
598 return 1; /* passwd must behave like a string */
603 #ifdef HAVE_CYRUS_SASL
604 if( lutil_passwd_sasl_conn != NULL ) {
605 const char *errstr = NULL;
608 sc = sasl_checkpass( lutil_passwd_sasl_conn,
609 passwd->bv_val, passwd->bv_len,
610 cred->bv_val, cred->bv_len,
613 rtn = ( sc != SASL_OK );
622 static int chk_kerberos(
623 const struct pw_scheme *sc,
624 const struct berval * passwd,
625 const struct berval * cred )
630 for( i=0; i<cred->bv_len; i++) {
631 if(cred->bv_val[i] == '\0') {
632 return 1; /* NUL character in password */
636 if( cred->bv_val[i] != '\0' ) {
637 return 1; /* cred must behave like a string */
640 for( i=0; i<passwd->bv_len; i++) {
641 if(passwd->bv_val[i] == '\0') {
642 return 1; /* NUL character in password */
646 if( passwd->bv_val[i] != '\0' ) {
647 return 1; /* passwd must behave like a string */
652 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
655 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
656 * (Royal Institute of Technology, Stockholm, Sweden).
657 * All rights reserved.
659 * Redistribution and use in source and binary forms, with or without
660 * modification, are permitted provided that the following conditions
663 * 1. Redistributions of source code must retain the above copyright
664 * notice, this list of conditions and the following disclaimer.
666 * 2. Redistributions in binary form must reproduce the above copyright
667 * notice, this list of conditions and the following disclaimer in the
668 * documentation and/or other materials provided with the distribution.
670 * 3. Neither the name of the Institute nor the names of its contributors
671 * may be used to endorse or promote products derived from this software
672 * without specific prior written permission.
674 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
675 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
676 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
677 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
678 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
679 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
680 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
681 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
682 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
683 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
687 krb5_context context;
690 krb5_get_init_creds_opt get_options;
691 krb5_verify_init_creds_opt verify_options;
692 krb5_principal client, server;
694 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
697 ret = krb5_init_context( &context );
703 krb5_get_init_creds_opt_set_preauth_list(&get_options,
707 krb5_get_init_creds_opt_init( &get_options );
709 krb5_verify_init_creds_opt_init( &verify_options );
711 ret = krb5_parse_name( context, passwd->bv_val, &client );
714 krb5_free_context( context );
718 ret = krb5_get_init_creds_password( context,
719 &creds, client, cred->bv_val, NULL,
720 NULL, 0, NULL, &get_options );
723 krb5_free_principal( context, client );
724 krb5_free_context( context );
729 char host[MAXHOSTNAMELEN+1];
731 if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) {
732 krb5_free_principal( context, client );
733 krb5_free_context( context );
737 host[MAXHOSTNAMELEN] = '\0';
739 ret = krb5_sname_to_principal( context,
740 host, "ldap", KRB5_NT_SRV_HST, &server );
744 krb5_free_principal( context, client );
745 krb5_free_context( context );
749 ret = krb5_verify_init_creds( context,
750 &creds, server, NULL, NULL, &verify_options );
752 krb5_free_principal( context, client );
753 krb5_free_principal( context, server );
754 krb5_free_creds_contents( context, &creds );
755 krb5_free_context( context );
759 #elif defined(HAVE_KRB4)
761 /* Borrowed from Heimdal kpopper */
763 * Copyright (c) 1989 Regents of the University of California.
764 * All rights reserved. The Berkeley software License Agreement
765 * specifies the terms and conditions for redistribution.
769 char lrealm[REALM_SZ];
770 char tkt[MAXHOSTNAMELEN];
772 status = krb_get_lrealm(lrealm,1);
773 if (status == KFAILURE) {
777 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
778 TKT_ROOT, (unsigned)getpid());
779 krb_set_tkt_string (tkt);
781 status = krb_verify_user( passwd->bv_val, "", lrealm,
782 cred->bv_val, 1, "ldap");
784 dest_tkt(); /* no point in keeping the tickets */
786 return status == KFAILURE;
792 #endif /* SLAPD_KPASSWD */
795 static int chk_crypt(
796 const struct pw_scheme *sc,
797 const struct berval * passwd,
798 const struct berval * cred )
803 for( i=0; i<cred->bv_len; i++) {
804 if(cred->bv_val[i] == '\0') {
805 return 1; /* NUL character in password */
809 if( cred->bv_val[i] != '\0' ) {
810 return -1; /* cred must behave like a string */
813 if( passwd->bv_len < 2 ) {
814 return -1; /* passwd must be at least two characters long */
817 for( i=0; i<passwd->bv_len; i++) {
818 if(passwd->bv_val[i] == '\0') {
819 return -1; /* NUL character in password */
823 if( passwd->bv_val[i] != '\0' ) {
824 return -1; /* passwd must behave like a string */
827 cr = crypt( cred->bv_val, passwd->bv_val );
829 if( cr == NULL || cr[0] == '\0' ) {
830 /* salt must have been invalid */
834 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
837 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
839 const struct pw_scheme *sc,
840 const struct berval * passwd,
841 const struct berval * cred )
846 for( i=0; i<cred->bv_len; i++) {
847 if(cred->bv_val[i] == '\0') {
848 return -1; /* NUL character in password */
851 if( cred->bv_val[i] != '\0' ) {
852 return -1; /* cred must behave like a string */
855 for( i=0; i<passwd->bv_len; i++) {
856 if(passwd->bv_val[i] == '\0') {
857 return -1; /* NUL character in password */
861 if( passwd->bv_val[i] != '\0' ) {
862 return -1; /* passwd must behave like a string */
866 struct passwd *pwd = getpwnam(passwd->bv_val);
869 return -1; /* not found */
874 # ifdef HAVE_GETSPNAM
876 struct spwd *spwd = getspnam(passwd->bv_val);
883 # ifdef HAVE_AIX_SECURITY
885 struct userpw *upw = getuserpw(passwd->bv_val);
888 pw = upw->upw_passwd;
893 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
894 /* password must must be at least two characters long */
898 cr = crypt(cred->bv_val, pw);
900 if( cr == NULL || cr[0] == '\0' ) {
901 /* salt must have been invalid */
905 return strcmp(pw, cr) ? 1 : 0;
911 /* PASSWORD GENERATION ROUTINES */
913 static struct berval *hash_ssha1(
914 const struct pw_scheme *scheme,
915 const struct berval *passwd )
917 lutil_SHA1_CTX SHA1context;
918 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
919 unsigned char saltdata[4];
920 struct berval digest;
923 digest.bv_val = SHA1digest;
924 digest.bv_len = sizeof(SHA1digest);
925 salt.bv_val = saltdata;
926 salt.bv_len = sizeof(saltdata);
928 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
932 lutil_SHA1Init( &SHA1context );
933 lutil_SHA1Update( &SHA1context,
934 (const unsigned char *)passwd->bv_val, passwd->bv_len );
935 lutil_SHA1Update( &SHA1context,
936 (const unsigned char *)salt.bv_val, salt.bv_len );
937 lutil_SHA1Final( SHA1digest, &SHA1context );
939 return pw_string64( scheme, &digest, &salt);
942 static struct berval *hash_sha1(
943 const struct pw_scheme *scheme,
944 const struct berval *passwd )
946 lutil_SHA1_CTX SHA1context;
947 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
948 struct berval digest;
949 digest.bv_val = SHA1digest;
950 digest.bv_len = sizeof(SHA1digest);
952 lutil_SHA1Init( &SHA1context );
953 lutil_SHA1Update( &SHA1context,
954 (const unsigned char *)passwd->bv_val, passwd->bv_len );
955 lutil_SHA1Final( SHA1digest, &SHA1context );
957 return pw_string64( scheme, &digest, NULL);
960 static struct berval *hash_smd5(
961 const struct pw_scheme *scheme,
962 const struct berval *passwd )
964 lutil_MD5_CTX MD5context;
965 unsigned char MD5digest[LUTIL_MD5_BYTES];
966 unsigned char saltdata[4];
967 struct berval digest;
970 digest.bv_val = MD5digest;
971 digest.bv_len = sizeof(MD5digest);
972 salt.bv_val = saltdata;
973 salt.bv_len = sizeof(saltdata);
975 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
979 lutil_MD5Init( &MD5context );
980 lutil_MD5Update( &MD5context,
981 (const unsigned char *) passwd->bv_val, passwd->bv_len );
982 lutil_MD5Update( &MD5context,
983 (const unsigned char *) salt.bv_val, salt.bv_len );
984 lutil_MD5Final( MD5digest, &MD5context );
986 return pw_string64( scheme, &digest, &salt );
989 static struct berval *hash_md5(
990 const struct pw_scheme *scheme,
991 const struct berval *passwd )
993 lutil_MD5_CTX MD5context;
994 unsigned char MD5digest[LUTIL_MD5_BYTES];
996 struct berval digest;
998 digest.bv_val = MD5digest;
999 digest.bv_len = sizeof(MD5digest);
1001 lutil_MD5Init( &MD5context );
1002 lutil_MD5Update( &MD5context,
1003 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1004 lutil_MD5Final( MD5digest, &MD5context );
1006 return pw_string64( scheme, &digest, NULL );
1011 static struct berval *hash_crypt(
1012 const struct pw_scheme *scheme,
1013 const struct berval *passwd )
1016 unsigned char salt[3];
1019 for( i=0; i<passwd->bv_len; i++) {
1020 if(passwd->bv_val[i] == '\0') {
1021 return NULL; /* NUL character in password */
1025 if( passwd->bv_val[i] != '\0' ) {
1026 return NULL; /* passwd must behave like a string */
1029 if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
1033 salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
1034 salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
1037 hash.bv_val = crypt( passwd->bv_val, salt );
1039 if( hash.bv_val == NULL ) return NULL;
1041 hash.bv_len = strlen( hash.bv_val );
1043 if( hash.bv_len == 0 ) {
1047 return pw_string( scheme, &hash );