3 * Copyright 1998-1999 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>
48 #include "lutil_md5.h"
49 #include "lutil_sha1.h"
52 static const unsigned char crypt64[] =
53 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
57 typedef int (*PASSWD_CHK_FUNC)(
58 const struct pw_scheme *scheme,
59 const struct berval *passwd,
60 const struct berval *cred );
62 typedef struct berval * (*PASSWD_HASH_FUNC) (
63 const struct pw_scheme *scheme,
64 const struct berval *passwd );
68 PASSWD_CHK_FUNC chk_fn;
69 PASSWD_HASH_FUNC hash_fn;
72 /* password check routines */
74 const struct pw_scheme *scheme,
75 const struct berval *passwd,
76 const struct berval *cred );
79 const struct pw_scheme *scheme,
80 const struct berval *passwd,
81 const struct berval *cred );
84 const struct pw_scheme *scheme,
85 const struct berval *passwd,
86 const struct berval *cred );
89 const struct pw_scheme *scheme,
90 const struct berval *passwd,
91 const struct berval *cred );
95 const struct pw_scheme *scheme,
96 const struct berval *passwd,
97 const struct berval *cred );
101 static int chk_kerberos(
102 const struct pw_scheme *scheme,
103 const struct berval *passwd,
104 const struct berval *cred );
108 static int chk_crypt(
109 const struct pw_scheme *scheme,
110 const struct berval *passwd,
111 const struct berval *cred );
113 #if defined( HAVE_GETSPNAM ) \
114 || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
116 const struct pw_scheme *scheme,
117 const struct berval *passwd,
118 const struct berval *cred );
123 /* password hash routines */
124 static struct berval *hash_sha1(
125 const struct pw_scheme *scheme,
126 const struct berval *passwd );
128 static struct berval *hash_ssha1(
129 const struct pw_scheme *scheme,
130 const struct berval *passwd );
132 static struct berval *hash_smd5(
133 const struct pw_scheme *scheme,
134 const struct berval *passwd );
136 static struct berval *hash_md5(
137 const struct pw_scheme *scheme,
138 const struct berval *passwd );
141 static struct berval *hash_crypt(
142 const struct pw_scheme *scheme,
143 const struct berval *passwd );
147 static const struct pw_scheme pw_schemes[] =
149 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
150 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
152 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
153 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
156 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
160 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
164 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
165 # if defined( HAVE_GETSPNAM ) \
166 || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
167 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
171 #ifdef SLAPD_CLEARTEXT
173 { {0, "{CLEARTEXT}"}, NULL, NULL },
176 { {0, NULL}, NULL, NULL }
179 static const struct pw_scheme *get_scheme(
184 for( i=0; pw_schemes[i].name.bv_val; i++) {
185 if( pw_schemes[i].name.bv_len == 0 ) continue;
187 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
188 pw_schemes[i].name.bv_len) == 0 )
190 return &pw_schemes[i];
197 int lutil_passwd_scheme(
200 if( scheme == NULL ) {
204 return get_scheme(scheme) != NULL;
208 static int is_allowed_scheme(
210 const char** schemes )
214 if( schemes == NULL ) return 1;
216 for( i=0; schemes[i] != NULL; i++ ) {
217 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
224 static struct berval *passwd_scheme(
225 const struct pw_scheme *scheme,
226 const struct berval * passwd,
227 const char** allowed )
229 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
233 if( passwd->bv_len >= scheme->name.bv_len ) {
234 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
235 struct berval *bv = ber_memalloc( sizeof(struct berval) );
237 if( bv == NULL ) return NULL;
239 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
240 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
250 * Return 0 if creds are good.
254 const struct berval *passwd, /* stored passwd */
255 const struct berval *cred, /* user cred */
256 const char **schemes )
260 if (cred == NULL || cred->bv_len == 0 ||
261 passwd == NULL || passwd->bv_len == 0 )
266 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
267 if( pw_schemes[i].chk_fn ) {
268 struct berval *p = passwd_scheme( &pw_schemes[i],
272 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
274 /* only free the berval structure as the bv_val points
275 * into passwd->bv_val
284 #ifdef SLAPD_CLEARTEXT
285 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
286 return passwd->bv_len == cred->bv_len
287 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
296 struct berval * lutil_passwd_generate( ber_len_t len )
300 if( len < 1 ) return NULL;
302 pw = ber_memalloc( sizeof( struct berval ) );
303 if( pw == NULL ) return NULL;
306 pw->bv_val = ber_memalloc( len + 1 );
308 if( pw->bv_val == NULL ) {
313 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
318 for( len = 0; len < pw->bv_len; len++ ) {
319 pw->bv_val[len] = crypt64[
320 pw->bv_val[len] % (sizeof(crypt64)-1) ];
323 pw->bv_val[len] = '\0';
328 struct berval * lutil_passwd_hash(
329 const struct berval * passwd,
330 const char * method )
332 const struct pw_scheme *sc = get_scheme( method );
334 if( sc == NULL ) return NULL;
335 if( ! sc->hash_fn ) return NULL;
337 return (sc->hash_fn)( sc, passwd );
340 static struct berval * pw_string(
341 const struct pw_scheme *sc,
342 const struct berval *passwd )
344 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
345 if( pw == NULL ) return NULL;
347 pw->bv_len = sc->name.bv_len + passwd->bv_len;
348 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
350 if( pw->bv_val == NULL ) {
355 memcpy( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
356 memcpy( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
358 pw->bv_val[pw->bv_len] = '\0';
362 static struct berval * pw_string64(
363 const struct pw_scheme *sc,
364 const struct berval *hash,
365 const struct berval *salt )
368 struct berval string;
369 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
372 if( b64 == NULL ) return NULL;
375 /* need to base64 combined string */
376 string.bv_len = hash->bv_len + salt->bv_len;
377 string.bv_val = ber_memalloc( string.bv_len + 1 );
379 if( string.bv_val == NULL ) {
384 memcpy( string.bv_val, hash->bv_val,
386 memcpy( &string.bv_val[hash->bv_len], salt->bv_val,
388 string.bv_val[string.bv_len] = '\0';
394 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
395 b64->bv_len = b64len + sc->name.bv_len;
396 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
398 if( b64->bv_val == NULL ) {
399 if( salt ) ber_memfree( string.bv_val );
404 memcpy(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
407 string.bv_val, string.bv_len,
408 &b64->bv_val[sc->name.bv_len], b64len );
410 if( salt ) ber_memfree( string.bv_val );
417 /* recompute length */
418 b64->bv_len = sc->name.bv_len + rc;
419 assert( strlen(b64->bv_val) == b64->bv_len );
423 /* PASSWORD CHECK ROUTINES */
425 static int chk_ssha1(
426 const struct pw_scheme *sc,
427 const struct berval * passwd,
428 const struct berval * cred )
430 lutil_SHA1_CTX SHA1context;
431 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
433 unsigned char *orig_pass = NULL;
435 /* decode base64 password */
436 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
437 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
439 if( orig_pass == NULL ) return -1;
441 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
444 ber_memfree(orig_pass);
448 /* hash credentials with salt */
449 lutil_SHA1Init(&SHA1context);
450 lutil_SHA1Update(&SHA1context,
451 (const unsigned char *) cred->bv_val, cred->bv_len);
452 lutil_SHA1Update(&SHA1context,
453 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
454 rc - sizeof(SHA1digest));
455 lutil_SHA1Final(SHA1digest, &SHA1context);
458 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
459 ber_memfree(orig_pass);
464 const struct pw_scheme *sc,
465 const struct berval * passwd,
466 const struct berval * cred )
468 lutil_SHA1_CTX SHA1context;
469 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
471 unsigned char *orig_pass = NULL;
473 /* base64 un-encode password */
474 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
475 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
477 if( orig_pass == NULL ) return -1;
479 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
481 if( rc != sizeof(SHA1digest) ) {
482 ber_memfree(orig_pass);
486 /* hash credentials with salt */
487 lutil_SHA1Init(&SHA1context);
488 lutil_SHA1Update(&SHA1context,
489 (const unsigned char *) cred->bv_val, cred->bv_len);
490 lutil_SHA1Final(SHA1digest, &SHA1context);
493 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
494 ber_memfree(orig_pass);
499 const struct pw_scheme *sc,
500 const struct berval * passwd,
501 const struct berval * cred )
503 lutil_MD5_CTX MD5context;
504 unsigned char MD5digest[LUTIL_MD5_BYTES];
506 unsigned char *orig_pass = NULL;
508 /* base64 un-encode password */
509 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
510 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
512 if( orig_pass == NULL ) return -1;
514 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
516 ber_memfree(orig_pass);
520 /* hash credentials with salt */
521 lutil_MD5Init(&MD5context);
522 lutil_MD5Update(&MD5context,
523 (const unsigned char *) cred->bv_val, cred->bv_len );
524 lutil_MD5Update(&MD5context,
525 (const unsigned char *) &orig_pass[sizeof(MD5digest)],
526 rc - sizeof(MD5digest));
527 lutil_MD5Final(MD5digest, &MD5context);
530 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
531 ber_memfree(orig_pass);
536 const struct pw_scheme *sc,
537 const struct berval * passwd,
538 const struct berval * cred )
540 lutil_MD5_CTX MD5context;
541 unsigned char MD5digest[LUTIL_MD5_BYTES];
543 unsigned char *orig_pass = NULL;
545 /* base64 un-encode password */
546 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
547 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
549 if( orig_pass == NULL ) return -1;
551 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
552 if ( rc != sizeof(MD5digest) ) {
553 ber_memfree(orig_pass);
557 /* hash credentials with salt */
558 lutil_MD5Init(&MD5context);
559 lutil_MD5Update(&MD5context,
560 (const unsigned char *) cred->bv_val, cred->bv_len );
561 lutil_MD5Final(MD5digest, &MD5context);
564 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
565 ber_memfree(orig_pass);
570 #ifdef HAVE_CYRUS_SASL
571 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
575 const struct pw_scheme *sc,
576 const struct berval * passwd,
577 const struct berval * cred )
582 for( i=0; i<cred->bv_len; i++) {
583 if(cred->bv_val[i] == '\0') {
584 return 1; /* NUL character in password */
588 if( cred->bv_val[i] != '\0' ) {
589 return 1; /* cred must behave like a string */
592 for( i=0; i<passwd->bv_len; i++) {
593 if(passwd->bv_val[i] == '\0') {
594 return 1; /* NUL character in password */
598 if( passwd->bv_val[i] != '\0' ) {
599 return 1; /* passwd must behave like a string */
604 #ifdef HAVE_CYRUS_SASL
605 if( lutil_passwd_sasl_conn != NULL ) {
606 const char *errstr = NULL;
609 sc = sasl_checkpass( lutil_passwd_sasl_conn,
610 passwd->bv_val, passwd->bv_len,
611 cred->bv_val, cred->bv_len,
614 rtn = ( sc != SASL_OK );
623 static int chk_kerberos(
624 const struct pw_scheme *sc,
625 const struct berval * passwd,
626 const struct berval * cred )
631 for( i=0; i<cred->bv_len; i++) {
632 if(cred->bv_val[i] == '\0') {
633 return 1; /* NUL character in password */
637 if( cred->bv_val[i] != '\0' ) {
638 return 1; /* cred must behave like a string */
641 for( i=0; i<passwd->bv_len; i++) {
642 if(passwd->bv_val[i] == '\0') {
643 return 1; /* NUL character in password */
647 if( passwd->bv_val[i] != '\0' ) {
648 return 1; /* passwd must behave like a string */
653 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
656 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
657 * (Royal Institute of Technology, Stockholm, Sweden).
658 * All rights reserved.
660 * Redistribution and use in source and binary forms, with or without
661 * modification, are permitted provided that the following conditions
664 * 1. Redistributions of source code must retain the above copyright
665 * notice, this list of conditions and the following disclaimer.
667 * 2. Redistributions in binary form must reproduce the above copyright
668 * notice, this list of conditions and the following disclaimer in the
669 * documentation and/or other materials provided with the distribution.
671 * 3. Neither the name of the Institute nor the names of its contributors
672 * may be used to endorse or promote products derived from this software
673 * without specific prior written permission.
675 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
676 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
677 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
678 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
679 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
680 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
681 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
682 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
683 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
684 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
688 krb5_context context;
691 krb5_get_init_creds_opt get_options;
692 krb5_verify_init_creds_opt verify_options;
693 krb5_principal client, server;
695 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
698 ret = krb5_init_context( &context );
704 krb5_get_init_creds_opt_set_preauth_list(&get_options,
708 krb5_get_init_creds_opt_init( &get_options );
710 krb5_verify_init_creds_opt_init( &verify_options );
712 ret = krb5_parse_name( context, passwd->bv_val, &client );
715 krb5_free_context( context );
719 ret = krb5_get_init_creds_password( context,
720 &creds, client, cred->bv_val, NULL,
721 NULL, 0, NULL, &get_options );
724 krb5_free_principal( context, client );
725 krb5_free_context( context );
730 char host[MAXHOSTNAMELEN];
732 if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) {
733 krb5_free_principal( context, client );
734 krb5_free_context( context );
738 ret = krb5_sname_to_principal( context,
739 host, "ldap", KRB5_NT_SRV_HST, &server );
743 krb5_free_principal( context, client );
744 krb5_free_context( context );
748 ret = krb5_verify_init_creds( context,
749 &creds, server, NULL, NULL, &verify_options );
751 krb5_free_principal( context, client );
752 krb5_free_principal( context, server );
753 krb5_free_creds_contents( context, &creds );
754 krb5_free_context( context );
758 #elif defined(HAVE_KRB4)
760 /* Borrowed from Heimdal kpopper */
762 * Copyright (c) 1989 Regents of the University of California.
763 * All rights reserved. The Berkeley software License Agreement
764 * specifies the terms and conditions for redistribution.
768 char lrealm[REALM_SZ];
769 char tkt[MAXHOSTNAMELEN];
771 status = krb_get_lrealm(lrealm,1);
772 if (status == KFAILURE) {
776 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
777 TKT_ROOT, (unsigned)getpid());
778 krb_set_tkt_string (tkt);
780 status = krb_verify_user( passwd->bv_val, "", lrealm,
781 cred->bv_val, 1, "ldap");
783 dest_tkt(); /* no point in keeping the tickets */
785 return status == KFAILURE;
791 #endif /* SLAPD_KPASSWD */
794 static int chk_crypt(
795 const struct pw_scheme *sc,
796 const struct berval * passwd,
797 const struct berval * cred )
802 for( i=0; i<cred->bv_len; i++) {
803 if(cred->bv_val[i] == '\0') {
804 return 1; /* NUL character in password */
808 if( cred->bv_val[i] != '\0' ) {
809 return -1; /* cred must behave like a string */
812 if( passwd->bv_len < 2 ) {
813 return -1; /* passwd must be at least two characters long */
816 for( i=0; i<passwd->bv_len; i++) {
817 if(passwd->bv_val[i] == '\0') {
818 return -1; /* NUL character in password */
822 if( passwd->bv_val[i] != '\0' ) {
823 return -1; /* passwd must behave like a string */
826 cr = crypt( cred->bv_val, passwd->bv_val );
828 if( cr == NULL || cr[0] == '\0' ) {
829 /* salt must have been invalid */
833 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
836 # if defined( HAVE_GETSPNAM ) \
837 || ( 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 */
865 # ifdef HAVE_GETSPNAM
867 struct spwd *spwd = getspnam(passwd->bv_val);
870 return -1; /* not found */
878 struct passwd *pwd = getpwnam(passwd->bv_val);
881 return -1; /* not found */
888 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
889 /* password must must be at least two characters long */
893 cr = crypt(cred->bv_val, pw);
895 if( cr == NULL || cr[0] == '\0' ) {
896 /* salt must have been invalid */
900 return strcmp(pw, cr) ? 1 : 0;
906 /* PASSWORD GENERATION ROUTINES */
908 static struct berval *hash_ssha1(
909 const struct pw_scheme *scheme,
910 const struct berval *passwd )
912 lutil_SHA1_CTX SHA1context;
913 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
914 unsigned char saltdata[4];
915 struct berval digest;
918 digest.bv_val = SHA1digest;
919 digest.bv_len = sizeof(SHA1digest);
920 salt.bv_val = saltdata;
921 salt.bv_len = sizeof(saltdata);
923 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
927 lutil_SHA1Init( &SHA1context );
928 lutil_SHA1Update( &SHA1context,
929 (const unsigned char *)passwd->bv_val, passwd->bv_len );
930 lutil_SHA1Update( &SHA1context,
931 (const unsigned char *)salt.bv_val, salt.bv_len );
932 lutil_SHA1Final( SHA1digest, &SHA1context );
934 return pw_string64( scheme, &digest, &salt);
937 static struct berval *hash_sha1(
938 const struct pw_scheme *scheme,
939 const struct berval *passwd )
941 lutil_SHA1_CTX SHA1context;
942 unsigned char SHA1digest[20];
943 struct berval digest;
944 digest.bv_val = SHA1digest;
945 digest.bv_len = sizeof(SHA1digest);
947 lutil_SHA1Init( &SHA1context );
948 lutil_SHA1Update( &SHA1context,
949 (const unsigned char *)passwd->bv_val, passwd->bv_len );
950 lutil_SHA1Final( SHA1digest, &SHA1context );
952 return pw_string64( scheme, &digest, NULL);
955 static struct berval *hash_smd5(
956 const struct pw_scheme *scheme,
957 const struct berval *passwd )
959 lutil_MD5_CTX MD5context;
960 unsigned char MD5digest[16];
961 unsigned char saltdata[4];
962 struct berval digest;
965 digest.bv_val = MD5digest;
966 digest.bv_len = sizeof(MD5digest);
967 salt.bv_val = saltdata;
968 salt.bv_len = sizeof(saltdata);
970 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
974 lutil_MD5Init( &MD5context );
975 lutil_MD5Update( &MD5context,
976 (const unsigned char *) passwd->bv_val, passwd->bv_len );
977 lutil_MD5Update( &MD5context,
978 (const unsigned char *) salt.bv_val, salt.bv_len );
979 lutil_MD5Final( MD5digest, &MD5context );
981 return pw_string64( scheme, &digest, &salt );
984 static struct berval *hash_md5(
985 const struct pw_scheme *scheme,
986 const struct berval *passwd )
988 lutil_MD5_CTX MD5context;
989 unsigned char MD5digest[16];
991 struct berval digest;
993 digest.bv_val = MD5digest;
994 digest.bv_len = sizeof(MD5digest);
996 lutil_MD5Init( &MD5context );
997 lutil_MD5Update( &MD5context,
998 (const unsigned char *) passwd->bv_val, passwd->bv_len );
999 lutil_MD5Final( MD5digest, &MD5context );
1001 return pw_string64( scheme, &digest, NULL );
1006 static struct berval *hash_crypt(
1007 const struct pw_scheme *scheme,
1008 const struct berval *passwd )
1011 unsigned char salt[3];
1014 for( i=0; i<passwd->bv_len; i++) {
1015 if(passwd->bv_val[i] == '\0') {
1016 return NULL; /* NUL character in password */
1020 if( passwd->bv_val[i] != '\0' ) {
1021 return NULL; /* passwd must behave like a string */
1024 if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
1028 salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
1029 salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
1032 hash.bv_val = crypt( passwd->bv_val, salt );
1034 if( hash.bv_val == NULL ) return NULL;
1036 hash.bv_len = strlen( hash.bv_val );
1038 if( hash.bv_len == 0 ) {
1042 return pw_string( scheme, &hash );