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
53 #include "lutil_md5.h"
54 #include "lutil_sha1.h"
57 static const unsigned char crypt64[] =
58 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
62 typedef int (*PASSWD_CHK_FUNC)(
63 const struct pw_scheme *scheme,
64 const struct berval *passwd,
65 const struct berval *cred );
67 typedef struct berval * (*PASSWD_HASH_FUNC) (
68 const struct pw_scheme *scheme,
69 const struct berval *passwd );
73 PASSWD_CHK_FUNC chk_fn;
74 PASSWD_HASH_FUNC hash_fn;
77 /* password check routines */
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 );
94 const struct pw_scheme *scheme,
95 const struct berval *passwd,
96 const struct berval *cred );
100 const struct pw_scheme *scheme,
101 const struct berval *passwd,
102 const struct berval *cred );
106 static int chk_kerberos(
107 const struct pw_scheme *scheme,
108 const struct berval *passwd,
109 const struct berval *cred );
113 static int chk_crypt(
114 const struct pw_scheme *scheme,
115 const struct berval *passwd,
116 const struct berval *cred );
118 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
120 const struct pw_scheme *scheme,
121 const struct berval *passwd,
122 const struct berval *cred );
127 /* password hash routines */
128 static struct berval *hash_sha1(
129 const struct pw_scheme *scheme,
130 const struct berval *passwd );
132 static struct berval *hash_ssha1(
133 const struct pw_scheme *scheme,
134 const struct berval *passwd );
136 static struct berval *hash_smd5(
137 const struct pw_scheme *scheme,
138 const struct berval *passwd );
140 static struct berval *hash_md5(
141 const struct pw_scheme *scheme,
142 const struct berval *passwd );
145 static struct berval *hash_crypt(
146 const struct pw_scheme *scheme,
147 const struct berval *passwd );
151 static const struct pw_scheme pw_schemes[] =
153 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
154 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
156 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
157 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
160 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
164 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
168 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
169 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
170 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
174 #ifdef SLAPD_CLEARTEXT
176 { {0, "{CLEARTEXT}"}, NULL, NULL },
179 { {0, NULL}, NULL, NULL }
182 static const struct pw_scheme *get_scheme(
187 for( i=0; pw_schemes[i].name.bv_val; i++) {
188 if( pw_schemes[i].name.bv_len == 0 ) continue;
190 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
191 pw_schemes[i].name.bv_len) == 0 )
193 return &pw_schemes[i];
200 int lutil_passwd_scheme(
203 if( scheme == NULL ) {
207 return get_scheme(scheme) != NULL;
211 static int is_allowed_scheme(
213 const char** schemes )
217 if( schemes == NULL ) return 1;
219 for( i=0; schemes[i] != NULL; i++ ) {
220 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
227 static struct berval *passwd_scheme(
228 const struct pw_scheme *scheme,
229 const struct berval * passwd,
230 const char** allowed )
232 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
236 if( passwd->bv_len >= scheme->name.bv_len ) {
237 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
238 struct berval *bv = ber_memalloc( sizeof(struct berval) );
240 if( bv == NULL ) return NULL;
242 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
243 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
253 * Return 0 if creds are good.
257 const struct berval *passwd, /* stored passwd */
258 const struct berval *cred, /* user cred */
259 const char **schemes )
263 if (cred == NULL || cred->bv_len == 0 ||
264 passwd == NULL || passwd->bv_len == 0 )
269 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
270 if( pw_schemes[i].chk_fn ) {
271 struct berval *p = passwd_scheme( &pw_schemes[i],
275 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
277 /* only free the berval structure as the bv_val points
278 * into passwd->bv_val
287 #ifdef SLAPD_CLEARTEXT
288 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
289 return passwd->bv_len == cred->bv_len
290 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
297 struct berval * lutil_passwd_generate( ber_len_t len )
301 if( len < 1 ) return NULL;
303 pw = ber_memalloc( sizeof( struct berval ) );
304 if( pw == NULL ) return NULL;
307 pw->bv_val = ber_memalloc( len + 1 );
309 if( pw->bv_val == NULL ) {
314 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
319 for( len = 0; len < pw->bv_len; len++ ) {
320 pw->bv_val[len] = crypt64[
321 pw->bv_val[len] % (sizeof(crypt64)-1) ];
324 pw->bv_val[len] = '\0';
329 struct berval * lutil_passwd_hash(
330 const struct berval * passwd,
331 const char * method )
333 const struct pw_scheme *sc = get_scheme( method );
335 if( sc == NULL ) return NULL;
336 if( ! sc->hash_fn ) return NULL;
338 return (sc->hash_fn)( sc, passwd );
341 static struct berval * pw_string(
342 const struct pw_scheme *sc,
343 const struct berval *passwd )
345 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
346 if( pw == NULL ) return NULL;
348 pw->bv_len = sc->name.bv_len + passwd->bv_len;
349 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
351 if( pw->bv_val == NULL ) {
356 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
357 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
359 pw->bv_val[pw->bv_len] = '\0';
363 static struct berval * pw_string64(
364 const struct pw_scheme *sc,
365 const struct berval *hash,
366 const struct berval *salt )
369 struct berval string;
370 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
373 if( b64 == NULL ) return NULL;
376 /* need to base64 combined string */
377 string.bv_len = hash->bv_len + salt->bv_len;
378 string.bv_val = ber_memalloc( string.bv_len + 1 );
380 if( string.bv_val == NULL ) {
385 AC_MEMCPY( string.bv_val, hash->bv_val,
387 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
389 string.bv_val[string.bv_len] = '\0';
395 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
396 b64->bv_len = b64len + sc->name.bv_len;
397 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
399 if( b64->bv_val == NULL ) {
400 if( salt ) ber_memfree( string.bv_val );
405 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
408 string.bv_val, string.bv_len,
409 &b64->bv_val[sc->name.bv_len], b64len );
411 if( salt ) ber_memfree( string.bv_val );
418 /* recompute length */
419 b64->bv_len = sc->name.bv_len + rc;
420 assert( strlen(b64->bv_val) == b64->bv_len );
424 /* PASSWORD CHECK ROUTINES */
426 static int chk_ssha1(
427 const struct pw_scheme *sc,
428 const struct berval * passwd,
429 const struct berval * cred )
431 lutil_SHA1_CTX SHA1context;
432 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
434 unsigned char *orig_pass = NULL;
436 /* decode base64 password */
437 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
438 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
440 if( orig_pass == NULL ) return -1;
442 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
445 ber_memfree(orig_pass);
449 /* hash credentials with salt */
450 lutil_SHA1Init(&SHA1context);
451 lutil_SHA1Update(&SHA1context,
452 (const unsigned char *) cred->bv_val, cred->bv_len);
453 lutil_SHA1Update(&SHA1context,
454 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
455 rc - sizeof(SHA1digest));
456 lutil_SHA1Final(SHA1digest, &SHA1context);
459 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
460 ber_memfree(orig_pass);
465 const struct pw_scheme *sc,
466 const struct berval * passwd,
467 const struct berval * cred )
469 lutil_SHA1_CTX SHA1context;
470 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
472 unsigned char *orig_pass = NULL;
474 /* base64 un-encode password */
475 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
476 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
478 if( orig_pass == NULL ) return -1;
480 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
482 if( rc != sizeof(SHA1digest) ) {
483 ber_memfree(orig_pass);
487 /* hash credentials with salt */
488 lutil_SHA1Init(&SHA1context);
489 lutil_SHA1Update(&SHA1context,
490 (const unsigned char *) cred->bv_val, cred->bv_len);
491 lutil_SHA1Final(SHA1digest, &SHA1context);
494 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
495 ber_memfree(orig_pass);
500 const struct pw_scheme *sc,
501 const struct berval * passwd,
502 const struct berval * cred )
504 lutil_MD5_CTX MD5context;
505 unsigned char MD5digest[LUTIL_MD5_BYTES];
507 unsigned char *orig_pass = NULL;
509 /* base64 un-encode password */
510 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
511 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
513 if( orig_pass == NULL ) return -1;
515 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
517 ber_memfree(orig_pass);
521 /* hash credentials with salt */
522 lutil_MD5Init(&MD5context);
523 lutil_MD5Update(&MD5context,
524 (const unsigned char *) cred->bv_val, cred->bv_len );
525 lutil_MD5Update(&MD5context,
526 (const unsigned char *) &orig_pass[sizeof(MD5digest)],
527 rc - sizeof(MD5digest));
528 lutil_MD5Final(MD5digest, &MD5context);
531 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
532 ber_memfree(orig_pass);
537 const struct pw_scheme *sc,
538 const struct berval * passwd,
539 const struct berval * cred )
541 lutil_MD5_CTX MD5context;
542 unsigned char MD5digest[LUTIL_MD5_BYTES];
544 unsigned char *orig_pass = NULL;
546 /* base64 un-encode password */
547 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
548 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
550 if( orig_pass == NULL ) return -1;
552 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
553 if ( rc != sizeof(MD5digest) ) {
554 ber_memfree(orig_pass);
558 /* hash credentials with salt */
559 lutil_MD5Init(&MD5context);
560 lutil_MD5Update(&MD5context,
561 (const unsigned char *) cred->bv_val, cred->bv_len );
562 lutil_MD5Final(MD5digest, &MD5context);
565 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
566 ber_memfree(orig_pass);
571 #ifdef HAVE_CYRUS_SASL
572 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
576 const struct pw_scheme *sc,
577 const struct berval * passwd,
578 const struct berval * cred )
583 for( i=0; i<cred->bv_len; i++) {
584 if(cred->bv_val[i] == '\0') {
585 return 1; /* NUL character in password */
589 if( cred->bv_val[i] != '\0' ) {
590 return 1; /* cred must behave like a string */
593 for( i=0; i<passwd->bv_len; i++) {
594 if(passwd->bv_val[i] == '\0') {
595 return 1; /* NUL character in password */
599 if( passwd->bv_val[i] != '\0' ) {
600 return 1; /* passwd must behave like a string */
605 #ifdef HAVE_CYRUS_SASL
606 if( lutil_passwd_sasl_conn != NULL ) {
607 const char *errstr = NULL;
610 sc = sasl_checkpass( lutil_passwd_sasl_conn,
611 passwd->bv_val, passwd->bv_len,
612 cred->bv_val, cred->bv_len,
615 rtn = ( sc != SASL_OK );
624 static int chk_kerberos(
625 const struct pw_scheme *sc,
626 const struct berval * passwd,
627 const struct berval * cred )
632 for( i=0; i<cred->bv_len; i++) {
633 if(cred->bv_val[i] == '\0') {
634 return 1; /* NUL character in password */
638 if( cred->bv_val[i] != '\0' ) {
639 return 1; /* cred must behave like a string */
642 for( i=0; i<passwd->bv_len; i++) {
643 if(passwd->bv_val[i] == '\0') {
644 return 1; /* NUL character in password */
648 if( passwd->bv_val[i] != '\0' ) {
649 return 1; /* passwd must behave like a string */
654 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
657 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
658 * (Royal Institute of Technology, Stockholm, Sweden).
659 * All rights reserved.
661 * Redistribution and use in source and binary forms, with or without
662 * modification, are permitted provided that the following conditions
665 * 1. Redistributions of source code must retain the above copyright
666 * notice, this list of conditions and the following disclaimer.
668 * 2. Redistributions in binary form must reproduce the above copyright
669 * notice, this list of conditions and the following disclaimer in the
670 * documentation and/or other materials provided with the distribution.
672 * 3. Neither the name of the Institute nor the names of its contributors
673 * may be used to endorse or promote products derived from this software
674 * without specific prior written permission.
676 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
677 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
678 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
679 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
680 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
681 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
682 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
683 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
684 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
685 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
689 krb5_context context;
692 krb5_get_init_creds_opt get_options;
693 krb5_verify_init_creds_opt verify_options;
694 krb5_principal client, server;
696 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
699 ret = krb5_init_context( &context );
705 krb5_get_init_creds_opt_set_preauth_list(&get_options,
709 krb5_get_init_creds_opt_init( &get_options );
711 krb5_verify_init_creds_opt_init( &verify_options );
713 ret = krb5_parse_name( context, passwd->bv_val, &client );
716 krb5_free_context( context );
720 ret = krb5_get_init_creds_password( context,
721 &creds, client, cred->bv_val, NULL,
722 NULL, 0, NULL, &get_options );
725 krb5_free_principal( context, client );
726 krb5_free_context( context );
731 char *host = ldap_pvt_get_fqdn( NULL );
734 krb5_free_principal( context, client );
735 krb5_free_context( context );
739 ret = krb5_sname_to_principal( context,
740 host, "ldap", KRB5_NT_SRV_HST, &server );
746 krb5_free_principal( context, client );
747 krb5_free_context( context );
751 ret = krb5_verify_init_creds( context,
752 &creds, server, NULL, NULL, &verify_options );
754 krb5_free_principal( context, client );
755 krb5_free_principal( context, server );
756 krb5_free_cred_contents( context, &creds );
757 krb5_free_context( context );
761 #elif defined(HAVE_KRB4)
763 /* Borrowed from Heimdal kpopper */
765 * Copyright (c) 1989 Regents of the University of California.
766 * All rights reserved. The Berkeley software License Agreement
767 * specifies the terms and conditions for redistribution.
771 char lrealm[REALM_SZ];
772 char tkt[MAXHOSTNAMELEN];
774 status = krb_get_lrealm(lrealm,1);
775 if (status == KFAILURE) {
779 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
780 TKT_ROOT, (unsigned)getpid());
781 krb_set_tkt_string (tkt);
783 status = krb_verify_user( passwd->bv_val, "", lrealm,
784 cred->bv_val, 1, "ldap");
786 dest_tkt(); /* no point in keeping the tickets */
788 return status == KFAILURE;
794 #endif /* SLAPD_KPASSWD */
797 static int chk_crypt(
798 const struct pw_scheme *sc,
799 const struct berval * passwd,
800 const struct berval * cred )
805 for( i=0; i<cred->bv_len; i++) {
806 if(cred->bv_val[i] == '\0') {
807 return 1; /* NUL character in password */
811 if( cred->bv_val[i] != '\0' ) {
812 return -1; /* cred must behave like a string */
815 if( passwd->bv_len < 2 ) {
816 return -1; /* passwd must be at least two characters long */
819 for( i=0; i<passwd->bv_len; i++) {
820 if(passwd->bv_val[i] == '\0') {
821 return -1; /* NUL character in password */
825 if( passwd->bv_val[i] != '\0' ) {
826 return -1; /* passwd must behave like a string */
829 cr = crypt( cred->bv_val, passwd->bv_val );
831 if( cr == NULL || cr[0] == '\0' ) {
832 /* salt must have been invalid */
836 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
839 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
841 const struct pw_scheme *sc,
842 const struct berval * passwd,
843 const struct berval * cred )
848 for( i=0; i<cred->bv_len; i++) {
849 if(cred->bv_val[i] == '\0') {
850 return -1; /* NUL character in password */
853 if( cred->bv_val[i] != '\0' ) {
854 return -1; /* cred must behave like a string */
857 for( i=0; i<passwd->bv_len; i++) {
858 if(passwd->bv_val[i] == '\0') {
859 return -1; /* NUL character in password */
863 if( passwd->bv_val[i] != '\0' ) {
864 return -1; /* passwd must behave like a string */
868 struct passwd *pwd = getpwnam(passwd->bv_val);
871 return -1; /* not found */
876 # ifdef HAVE_GETSPNAM
878 struct spwd *spwd = getspnam(passwd->bv_val);
885 # ifdef HAVE_AIX_SECURITY
887 struct userpw *upw = getuserpw(passwd->bv_val);
890 pw = upw->upw_passwd;
895 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
896 /* password must must be at least two characters long */
900 cr = crypt(cred->bv_val, pw);
902 if( cr == NULL || cr[0] == '\0' ) {
903 /* salt must have been invalid */
907 return strcmp(pw, cr) ? 1 : 0;
913 /* PASSWORD GENERATION ROUTINES */
915 static struct berval *hash_ssha1(
916 const struct pw_scheme *scheme,
917 const struct berval *passwd )
919 lutil_SHA1_CTX SHA1context;
920 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
921 unsigned char saltdata[4];
922 struct berval digest;
925 digest.bv_val = SHA1digest;
926 digest.bv_len = sizeof(SHA1digest);
927 salt.bv_val = saltdata;
928 salt.bv_len = sizeof(saltdata);
930 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
934 lutil_SHA1Init( &SHA1context );
935 lutil_SHA1Update( &SHA1context,
936 (const unsigned char *)passwd->bv_val, passwd->bv_len );
937 lutil_SHA1Update( &SHA1context,
938 (const unsigned char *)salt.bv_val, salt.bv_len );
939 lutil_SHA1Final( SHA1digest, &SHA1context );
941 return pw_string64( scheme, &digest, &salt);
944 static struct berval *hash_sha1(
945 const struct pw_scheme *scheme,
946 const struct berval *passwd )
948 lutil_SHA1_CTX SHA1context;
949 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
950 struct berval digest;
951 digest.bv_val = SHA1digest;
952 digest.bv_len = sizeof(SHA1digest);
954 lutil_SHA1Init( &SHA1context );
955 lutil_SHA1Update( &SHA1context,
956 (const unsigned char *)passwd->bv_val, passwd->bv_len );
957 lutil_SHA1Final( SHA1digest, &SHA1context );
959 return pw_string64( scheme, &digest, NULL);
962 static struct berval *hash_smd5(
963 const struct pw_scheme *scheme,
964 const struct berval *passwd )
966 lutil_MD5_CTX MD5context;
967 unsigned char MD5digest[LUTIL_MD5_BYTES];
968 unsigned char saltdata[4];
969 struct berval digest;
972 digest.bv_val = MD5digest;
973 digest.bv_len = sizeof(MD5digest);
974 salt.bv_val = saltdata;
975 salt.bv_len = sizeof(saltdata);
977 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
981 lutil_MD5Init( &MD5context );
982 lutil_MD5Update( &MD5context,
983 (const unsigned char *) passwd->bv_val, passwd->bv_len );
984 lutil_MD5Update( &MD5context,
985 (const unsigned char *) salt.bv_val, salt.bv_len );
986 lutil_MD5Final( MD5digest, &MD5context );
988 return pw_string64( scheme, &digest, &salt );
991 static struct berval *hash_md5(
992 const struct pw_scheme *scheme,
993 const struct berval *passwd )
995 lutil_MD5_CTX MD5context;
996 unsigned char MD5digest[LUTIL_MD5_BYTES];
998 struct berval digest;
1000 digest.bv_val = MD5digest;
1001 digest.bv_len = sizeof(MD5digest);
1003 lutil_MD5Init( &MD5context );
1004 lutil_MD5Update( &MD5context,
1005 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1006 lutil_MD5Final( MD5digest, &MD5context );
1008 return pw_string64( scheme, &digest, NULL );
1013 static struct berval *hash_crypt(
1014 const struct pw_scheme *scheme,
1015 const struct berval *passwd )
1018 unsigned char salt[3];
1021 for( i=0; i<passwd->bv_len; i++) {
1022 if(passwd->bv_val[i] == '\0') {
1023 return NULL; /* NUL character in password */
1027 if( passwd->bv_val[i] != '\0' ) {
1028 return NULL; /* passwd must behave like a string */
1031 if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
1035 salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
1036 salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
1039 hash.bv_val = crypt( passwd->bv_val, salt );
1041 if( hash.bv_val == NULL ) return NULL;
1043 hash.bv_len = strlen( hash.bv_val );
1045 if( hash.bv_len == 0 ) {
1049 return pw_string( scheme, &hash );