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./";
61 static char *salt_format = NULL;
66 typedef int (*PASSWD_CHK_FUNC)(
67 const struct pw_scheme *scheme,
68 const struct berval *passwd,
69 const struct berval *cred );
71 typedef struct berval * (*PASSWD_HASH_FUNC) (
72 const struct pw_scheme *scheme,
73 const struct berval *passwd );
77 PASSWD_CHK_FUNC chk_fn;
78 PASSWD_HASH_FUNC hash_fn;
81 /* password check routines */
83 const struct pw_scheme *scheme,
84 const struct berval *passwd,
85 const struct berval *cred );
88 const struct pw_scheme *scheme,
89 const struct berval *passwd,
90 const struct berval *cred );
93 const struct pw_scheme *scheme,
94 const struct berval *passwd,
95 const struct berval *cred );
98 const struct pw_scheme *scheme,
99 const struct berval *passwd,
100 const struct berval *cred );
104 const struct pw_scheme *scheme,
105 const struct berval *passwd,
106 const struct berval *cred );
110 static int chk_kerberos(
111 const struct pw_scheme *scheme,
112 const struct berval *passwd,
113 const struct berval *cred );
117 static int chk_crypt(
118 const struct pw_scheme *scheme,
119 const struct berval *passwd,
120 const struct berval *cred );
122 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
124 const struct pw_scheme *scheme,
125 const struct berval *passwd,
126 const struct berval *cred );
131 /* password hash routines */
132 static struct berval *hash_sha1(
133 const struct pw_scheme *scheme,
134 const struct berval *passwd );
136 static struct berval *hash_ssha1(
137 const struct pw_scheme *scheme,
138 const struct berval *passwd );
140 static struct berval *hash_smd5(
141 const struct pw_scheme *scheme,
142 const struct berval *passwd );
144 static struct berval *hash_md5(
145 const struct pw_scheme *scheme,
146 const struct berval *passwd );
149 static struct berval *hash_crypt(
150 const struct pw_scheme *scheme,
151 const struct berval *passwd );
155 static const struct pw_scheme pw_schemes[] =
157 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
158 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
160 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
161 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
164 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
168 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
172 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
173 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
174 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
178 #ifdef SLAPD_CLEARTEXT
180 { {0, "{CLEARTEXT}"}, NULL, NULL },
183 { {0, NULL}, NULL, NULL }
186 static const struct pw_scheme *get_scheme(
191 for( i=0; pw_schemes[i].name.bv_val; i++) {
192 if( pw_schemes[i].name.bv_len == 0 ) continue;
194 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
195 pw_schemes[i].name.bv_len) == 0 )
197 return &pw_schemes[i];
204 int lutil_passwd_scheme(
207 if( scheme == NULL ) {
211 return get_scheme(scheme) != NULL;
215 static int is_allowed_scheme(
217 const char** schemes )
221 if( schemes == NULL ) return 1;
223 for( i=0; schemes[i] != NULL; i++ ) {
224 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
231 static struct berval *passwd_scheme(
232 const struct pw_scheme *scheme,
233 const struct berval * passwd,
234 const char** allowed )
236 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
240 if( passwd->bv_len >= scheme->name.bv_len ) {
241 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
242 struct berval *bv = ber_memalloc( sizeof(struct berval) );
244 if( bv == NULL ) return NULL;
246 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
247 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
257 * Return 0 if creds are good.
261 const struct berval *passwd, /* stored passwd */
262 const struct berval *cred, /* user cred */
263 const char **schemes )
267 if (cred == NULL || cred->bv_len == 0 ||
268 passwd == NULL || passwd->bv_len == 0 )
273 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
274 if( pw_schemes[i].chk_fn ) {
275 struct berval *p = passwd_scheme( &pw_schemes[i],
279 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
281 /* only free the berval structure as the bv_val points
282 * into passwd->bv_val
291 #ifdef SLAPD_CLEARTEXT
292 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
293 return passwd->bv_len == cred->bv_len
294 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
301 struct berval * lutil_passwd_generate( ber_len_t len )
305 if( len < 1 ) return NULL;
307 pw = ber_memalloc( sizeof( struct berval ) );
308 if( pw == NULL ) return NULL;
311 pw->bv_val = ber_memalloc( len + 1 );
313 if( pw->bv_val == NULL ) {
318 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
323 for( len = 0; len < pw->bv_len; len++ ) {
324 pw->bv_val[len] = crypt64[
325 pw->bv_val[len] % (sizeof(crypt64)-1) ];
328 pw->bv_val[len] = '\0';
333 struct berval * lutil_passwd_hash(
334 const struct berval * passwd,
335 const char * method )
337 const struct pw_scheme *sc = get_scheme( method );
339 if( sc == NULL ) return NULL;
340 if( ! sc->hash_fn ) return NULL;
342 return (sc->hash_fn)( sc, passwd );
345 static struct berval * pw_string(
346 const struct pw_scheme *sc,
347 const struct berval *passwd )
349 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
350 if( pw == NULL ) return NULL;
352 pw->bv_len = sc->name.bv_len + passwd->bv_len;
353 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
355 if( pw->bv_val == NULL ) {
360 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
361 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
363 pw->bv_val[pw->bv_len] = '\0';
367 static struct berval * pw_string64(
368 const struct pw_scheme *sc,
369 const struct berval *hash,
370 const struct berval *salt )
373 struct berval string;
374 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
377 if( b64 == NULL ) return NULL;
380 /* need to base64 combined string */
381 string.bv_len = hash->bv_len + salt->bv_len;
382 string.bv_val = ber_memalloc( string.bv_len + 1 );
384 if( string.bv_val == NULL ) {
389 AC_MEMCPY( string.bv_val, hash->bv_val,
391 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
393 string.bv_val[string.bv_len] = '\0';
399 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
400 b64->bv_len = b64len + sc->name.bv_len;
401 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
403 if( b64->bv_val == NULL ) {
404 if( salt ) ber_memfree( string.bv_val );
409 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
412 string.bv_val, string.bv_len,
413 &b64->bv_val[sc->name.bv_len], b64len );
415 if( salt ) ber_memfree( string.bv_val );
422 /* recompute length */
423 b64->bv_len = sc->name.bv_len + rc;
424 assert( strlen(b64->bv_val) == b64->bv_len );
428 /* PASSWORD CHECK ROUTINES */
430 static int chk_ssha1(
431 const struct pw_scheme *sc,
432 const struct berval * passwd,
433 const struct berval * cred )
435 lutil_SHA1_CTX SHA1context;
436 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
438 unsigned char *orig_pass = NULL;
440 /* decode base64 password */
441 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
442 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
444 if( orig_pass == NULL ) return -1;
446 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
449 ber_memfree(orig_pass);
453 /* hash credentials with salt */
454 lutil_SHA1Init(&SHA1context);
455 lutil_SHA1Update(&SHA1context,
456 (const unsigned char *) cred->bv_val, cred->bv_len);
457 lutil_SHA1Update(&SHA1context,
458 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
459 rc - sizeof(SHA1digest));
460 lutil_SHA1Final(SHA1digest, &SHA1context);
463 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
464 ber_memfree(orig_pass);
469 const struct pw_scheme *sc,
470 const struct berval * passwd,
471 const struct berval * cred )
473 lutil_SHA1_CTX SHA1context;
474 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
476 unsigned char *orig_pass = NULL;
478 /* base64 un-encode password */
479 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
480 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
482 if( orig_pass == NULL ) return -1;
484 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
486 if( rc != sizeof(SHA1digest) ) {
487 ber_memfree(orig_pass);
491 /* hash credentials with salt */
492 lutil_SHA1Init(&SHA1context);
493 lutil_SHA1Update(&SHA1context,
494 (const unsigned char *) cred->bv_val, cred->bv_len);
495 lutil_SHA1Final(SHA1digest, &SHA1context);
498 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
499 ber_memfree(orig_pass);
504 const struct pw_scheme *sc,
505 const struct berval * passwd,
506 const struct berval * cred )
508 lutil_MD5_CTX MD5context;
509 unsigned char MD5digest[LUTIL_MD5_BYTES];
511 unsigned char *orig_pass = NULL;
513 /* base64 un-encode password */
514 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
515 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
517 if( orig_pass == NULL ) return -1;
519 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
521 ber_memfree(orig_pass);
525 /* hash credentials with salt */
526 lutil_MD5Init(&MD5context);
527 lutil_MD5Update(&MD5context,
528 (const unsigned char *) cred->bv_val,
530 lutil_MD5Update(&MD5context,
531 &orig_pass[sizeof(MD5digest)],
532 rc - sizeof(MD5digest));
533 lutil_MD5Final(MD5digest, &MD5context);
536 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
537 ber_memfree(orig_pass);
542 const struct pw_scheme *sc,
543 const struct berval * passwd,
544 const struct berval * cred )
546 lutil_MD5_CTX MD5context;
547 unsigned char MD5digest[LUTIL_MD5_BYTES];
549 unsigned char *orig_pass = NULL;
551 /* base64 un-encode password */
552 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
553 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
555 if( orig_pass == NULL ) return -1;
557 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
558 if ( rc != sizeof(MD5digest) ) {
559 ber_memfree(orig_pass);
563 /* hash credentials with salt */
564 lutil_MD5Init(&MD5context);
565 lutil_MD5Update(&MD5context,
566 (const unsigned char *) cred->bv_val,
568 lutil_MD5Final(MD5digest, &MD5context);
571 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
572 ber_memfree(orig_pass);
577 #ifdef HAVE_CYRUS_SASL
578 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
582 const struct pw_scheme *sc,
583 const struct berval * passwd,
584 const struct berval * cred )
589 for( i=0; i<cred->bv_len; i++) {
590 if(cred->bv_val[i] == '\0') {
591 return 1; /* NUL character in password */
595 if( cred->bv_val[i] != '\0' ) {
596 return 1; /* cred must behave like a string */
599 for( i=0; i<passwd->bv_len; i++) {
600 if(passwd->bv_val[i] == '\0') {
601 return 1; /* NUL character in password */
605 if( passwd->bv_val[i] != '\0' ) {
606 return 1; /* passwd must behave like a string */
611 #ifdef HAVE_CYRUS_SASL
612 if( lutil_passwd_sasl_conn != NULL ) {
613 const char *errstr = NULL;
616 sc = sasl_checkpass( lutil_passwd_sasl_conn,
617 passwd->bv_val, passwd->bv_len,
618 cred->bv_val, cred->bv_len,
621 rtn = ( sc != SASL_OK );
630 static int chk_kerberos(
631 const struct pw_scheme *sc,
632 const struct berval * passwd,
633 const struct berval * cred )
638 for( i=0; i<cred->bv_len; i++) {
639 if(cred->bv_val[i] == '\0') {
640 return 1; /* NUL character in password */
644 if( cred->bv_val[i] != '\0' ) {
645 return 1; /* cred must behave like a string */
648 for( i=0; i<passwd->bv_len; i++) {
649 if(passwd->bv_val[i] == '\0') {
650 return 1; /* NUL character in password */
654 if( passwd->bv_val[i] != '\0' ) {
655 return 1; /* passwd must behave like a string */
660 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
663 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
664 * (Royal Institute of Technology, Stockholm, Sweden).
665 * All rights reserved.
667 * Redistribution and use in source and binary forms, with or without
668 * modification, are permitted provided that the following conditions
671 * 1. Redistributions of source code must retain the above copyright
672 * notice, this list of conditions and the following disclaimer.
674 * 2. Redistributions in binary form must reproduce the above copyright
675 * notice, this list of conditions and the following disclaimer in the
676 * documentation and/or other materials provided with the distribution.
678 * 3. Neither the name of the Institute nor the names of its contributors
679 * may be used to endorse or promote products derived from this software
680 * without specific prior written permission.
682 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
683 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
684 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
685 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
686 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
687 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
688 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
689 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
690 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
691 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
695 krb5_context context;
698 krb5_get_init_creds_opt get_options;
699 krb5_verify_init_creds_opt verify_options;
700 krb5_principal client, server;
702 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
705 ret = krb5_init_context( &context );
711 krb5_get_init_creds_opt_set_preauth_list(&get_options,
715 krb5_get_init_creds_opt_init( &get_options );
717 krb5_verify_init_creds_opt_init( &verify_options );
719 ret = krb5_parse_name( context, passwd->bv_val, &client );
722 krb5_free_context( context );
726 ret = krb5_get_init_creds_password( context,
727 &creds, client, cred->bv_val, NULL,
728 NULL, 0, NULL, &get_options );
731 krb5_free_principal( context, client );
732 krb5_free_context( context );
737 char *host = ldap_pvt_get_fqdn( NULL );
740 krb5_free_principal( context, client );
741 krb5_free_context( context );
745 ret = krb5_sname_to_principal( context,
746 host, "ldap", KRB5_NT_SRV_HST, &server );
752 krb5_free_principal( context, client );
753 krb5_free_context( context );
757 ret = krb5_verify_init_creds( context,
758 &creds, server, NULL, NULL, &verify_options );
760 krb5_free_principal( context, client );
761 krb5_free_principal( context, server );
762 krb5_free_cred_contents( context, &creds );
763 krb5_free_context( context );
767 #elif defined(HAVE_KRB4)
769 /* Borrowed from Heimdal kpopper */
771 * Copyright (c) 1989 Regents of the University of California.
772 * All rights reserved. The Berkeley software License Agreement
773 * specifies the terms and conditions for redistribution.
777 char lrealm[REALM_SZ];
778 char tkt[MAXHOSTNAMELEN];
780 status = krb_get_lrealm(lrealm,1);
781 if (status == KFAILURE) {
785 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
786 TKT_ROOT, (unsigned)getpid());
787 krb_set_tkt_string (tkt);
789 status = krb_verify_user( passwd->bv_val, "", lrealm,
790 cred->bv_val, 1, "ldap");
792 dest_tkt(); /* no point in keeping the tickets */
794 return status == KFAILURE;
800 #endif /* SLAPD_KPASSWD */
803 static int chk_crypt(
804 const struct pw_scheme *sc,
805 const struct berval * passwd,
806 const struct berval * cred )
811 for( i=0; i<cred->bv_len; i++) {
812 if(cred->bv_val[i] == '\0') {
813 return 1; /* NUL character in password */
817 if( cred->bv_val[i] != '\0' ) {
818 return -1; /* cred must behave like a string */
821 if( passwd->bv_len < 2 ) {
822 return -1; /* passwd must be at least two characters long */
825 for( i=0; i<passwd->bv_len; i++) {
826 if(passwd->bv_val[i] == '\0') {
827 return -1; /* NUL character in password */
831 if( passwd->bv_val[i] != '\0' ) {
832 return -1; /* passwd must behave like a string */
835 cr = crypt( cred->bv_val, passwd->bv_val );
837 if( cr == NULL || cr[0] == '\0' ) {
838 /* salt must have been invalid */
842 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
845 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
847 const struct pw_scheme *sc,
848 const struct berval * passwd,
849 const struct berval * cred )
854 for( i=0; i<cred->bv_len; i++) {
855 if(cred->bv_val[i] == '\0') {
856 return -1; /* NUL character in password */
859 if( cred->bv_val[i] != '\0' ) {
860 return -1; /* cred must behave like a string */
863 for( i=0; i<passwd->bv_len; i++) {
864 if(passwd->bv_val[i] == '\0') {
865 return -1; /* NUL character in password */
869 if( passwd->bv_val[i] != '\0' ) {
870 return -1; /* passwd must behave like a string */
874 struct passwd *pwd = getpwnam(passwd->bv_val);
877 return -1; /* not found */
882 # ifdef HAVE_GETSPNAM
884 struct spwd *spwd = getspnam(passwd->bv_val);
891 # ifdef HAVE_AIX_SECURITY
893 struct userpw *upw = getuserpw(passwd->bv_val);
896 pw = upw->upw_passwd;
901 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
902 /* password must must be at least two characters long */
906 cr = crypt(cred->bv_val, pw);
908 if( cr == NULL || cr[0] == '\0' ) {
909 /* salt must have been invalid */
913 return strcmp(pw, cr) ? 1 : 0;
919 /* PASSWORD GENERATION ROUTINES */
921 static struct berval *hash_ssha1(
922 const struct pw_scheme *scheme,
923 const struct berval *passwd )
925 lutil_SHA1_CTX SHA1context;
926 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
927 unsigned char saltdata[4];
928 struct berval digest;
931 digest.bv_val = SHA1digest;
932 digest.bv_len = sizeof(SHA1digest);
933 salt.bv_val = saltdata;
934 salt.bv_len = sizeof(saltdata);
936 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
940 lutil_SHA1Init( &SHA1context );
941 lutil_SHA1Update( &SHA1context,
942 (const unsigned char *)passwd->bv_val, passwd->bv_len );
943 lutil_SHA1Update( &SHA1context,
944 (const unsigned char *)salt.bv_val, salt.bv_len );
945 lutil_SHA1Final( SHA1digest, &SHA1context );
947 return pw_string64( scheme, &digest, &salt);
950 static struct berval *hash_sha1(
951 const struct pw_scheme *scheme,
952 const struct berval *passwd )
954 lutil_SHA1_CTX SHA1context;
955 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
956 struct berval digest;
957 digest.bv_val = SHA1digest;
958 digest.bv_len = sizeof(SHA1digest);
960 lutil_SHA1Init( &SHA1context );
961 lutil_SHA1Update( &SHA1context,
962 (const unsigned char *)passwd->bv_val, passwd->bv_len );
963 lutil_SHA1Final( SHA1digest, &SHA1context );
965 return pw_string64( scheme, &digest, NULL);
968 static struct berval *hash_smd5(
969 const struct pw_scheme *scheme,
970 const struct berval *passwd )
972 lutil_MD5_CTX MD5context;
973 unsigned char MD5digest[LUTIL_MD5_BYTES];
974 unsigned char saltdata[4];
975 struct berval digest;
978 digest.bv_val = MD5digest;
979 digest.bv_len = sizeof(MD5digest);
980 salt.bv_val = saltdata;
981 salt.bv_len = sizeof(saltdata);
983 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
987 lutil_MD5Init( &MD5context );
988 lutil_MD5Update( &MD5context,
989 (const unsigned char *) passwd->bv_val, passwd->bv_len );
990 lutil_MD5Update( &MD5context,
991 (const unsigned char *) salt.bv_val, salt.bv_len );
992 lutil_MD5Final( MD5digest, &MD5context );
994 return pw_string64( scheme, &digest, &salt );
997 static struct berval *hash_md5(
998 const struct pw_scheme *scheme,
999 const struct berval *passwd )
1001 lutil_MD5_CTX MD5context;
1002 unsigned char MD5digest[LUTIL_MD5_BYTES];
1004 struct berval digest;
1006 digest.bv_val = MD5digest;
1007 digest.bv_len = sizeof(MD5digest);
1009 lutil_MD5Init( &MD5context );
1010 lutil_MD5Update( &MD5context,
1011 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1012 lutil_MD5Final( MD5digest, &MD5context );
1014 return pw_string64( scheme, &digest, NULL );
1019 static struct berval *hash_crypt(
1020 const struct pw_scheme *scheme,
1021 const struct berval *passwd )
1024 unsigned char salt[32]; /* salt suitable for most anything */
1027 for( i=0; i<passwd->bv_len; i++) {
1028 if(passwd->bv_val[i] == '\0') {
1029 return NULL; /* NUL character in password */
1033 if( passwd->bv_val[i] != '\0' ) {
1034 return NULL; /* passwd must behave like a string */
1037 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1041 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1042 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1044 salt[sizeof( salt ) - 1 ] = '\0';
1046 if( salt_format != NULL ) {
1047 /* copy the salt we made into entropy before snprintfing
1048 it back into the salt */
1049 char entropy[sizeof(salt)];
1050 strcpy( entropy, salt );
1051 snprintf( salt, sizeof(entropy), salt_format, entropy );
1054 hash.bv_val = crypt( passwd->bv_val, salt );
1056 if( hash.bv_val == NULL ) return NULL;
1058 hash.bv_len = strlen( hash.bv_val );
1060 if( hash.bv_len == 0 ) {
1064 return pw_string( scheme, &hash );
1068 int lutil_salt_format( const char *format)
1071 free( salt_format );
1073 salt_format = format != NULL ? strdup( format ) : NULL;