Home | History | Annotate | Line # | Download | only in liblutil
passwd.c revision 1.2
      1  1.2  lukem /*	$NetBSD: passwd.c,v 1.2 2010/03/08 04:58:18 lukem Exp $	*/
      2  1.2  lukem 
      3  1.2  lukem /* OpenLDAP: pkg/ldap/libraries/liblutil/passwd.c,v 1.104.2.9 2009/08/30 22:55:47 quanah Exp */
      4  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5  1.1  lukem  *
      6  1.2  lukem  * Copyright 1998-2009 The OpenLDAP Foundation.
      7  1.1  lukem  * All rights reserved.
      8  1.1  lukem  *
      9  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     10  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     11  1.1  lukem  * Public License.
     12  1.1  lukem  *
     13  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     14  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     15  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     16  1.1  lukem  */
     17  1.1  lukem 
     18  1.1  lukem /*
     19  1.1  lukem  * int lutil_passwd(
     20  1.1  lukem  *	const struct berval *passwd,
     21  1.1  lukem  *	const struct berval *cred,
     22  1.1  lukem  *	const char **schemes )
     23  1.1  lukem  *
     24  1.1  lukem  * Returns true if user supplied credentials (cred) matches
     25  1.1  lukem  * the stored password (passwd).
     26  1.1  lukem  *
     27  1.1  lukem  * Due to the use of the crypt(3) function
     28  1.1  lukem  * this routine is NOT thread-safe.
     29  1.1  lukem  */
     30  1.1  lukem 
     31  1.1  lukem #include "portable.h"
     32  1.1  lukem 
     33  1.1  lukem #include <stdio.h>
     34  1.1  lukem #include <ac/stdlib.h>
     35  1.1  lukem #include <ac/string.h>
     36  1.2  lukem #include <ac/time.h>
     37  1.1  lukem #include <ac/unistd.h>
     38  1.1  lukem 
     39  1.1  lukem #if defined(SLAPD_LMHASH)
     40  1.2  lukem #if defined(HAVE_OPENSSL)
     41  1.1  lukem #	include <openssl/des.h>
     42  1.2  lukem 
     43  1.2  lukem 
     44  1.2  lukem typedef des_cblock des_key;
     45  1.2  lukem typedef des_cblock des_data_block;
     46  1.2  lukem typedef des_key_schedule des_context;
     47  1.2  lukem #define des_failed(encrypted) 0
     48  1.2  lukem #define des_finish(key, schedule)
     49  1.2  lukem 
     50  1.2  lukem #elif defined(HAVE_MOZNSS)
     51  1.2  lukem /*
     52  1.2  lukem   hack hack hack
     53  1.2  lukem   We need to define this here so that nspr/obsolete/protypes.h will not be included
     54  1.2  lukem   if that file is included, it will create a uint32 typedef that will cause the
     55  1.2  lukem   one in lutil_sha1.h to blow up
     56  1.2  lukem */
     57  1.2  lukem #define PROTYPES_H 1
     58  1.2  lukem #	include <nss/pk11pub.h>
     59  1.2  lukem typedef PK11SymKey *des_key;
     60  1.2  lukem typedef unsigned char des_data_block[8];
     61  1.2  lukem typedef PK11Context *des_context[1];
     62  1.2  lukem #define DES_ENCRYPT CKA_ENCRYPT
     63  1.2  lukem 
     64  1.2  lukem #endif
     65  1.2  lukem 
     66  1.1  lukem #endif /* SLAPD_LMHASH */
     67  1.1  lukem 
     68  1.1  lukem #include <ac/param.h>
     69  1.1  lukem 
     70  1.1  lukem #ifdef SLAPD_CRYPT
     71  1.1  lukem # include <ac/crypt.h>
     72  1.1  lukem 
     73  1.1  lukem # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
     74  1.1  lukem #  ifdef HAVE_SHADOW_H
     75  1.1  lukem #	include <shadow.h>
     76  1.1  lukem #  endif
     77  1.1  lukem #  ifdef HAVE_PWD_H
     78  1.1  lukem #	include <pwd.h>
     79  1.1  lukem #  endif
     80  1.1  lukem #  ifdef HAVE_AIX_SECURITY
     81  1.1  lukem #	include <userpw.h>
     82  1.1  lukem #  endif
     83  1.1  lukem # endif
     84  1.1  lukem #endif
     85  1.1  lukem 
     86  1.1  lukem #include <lber.h>
     87  1.1  lukem 
     88  1.1  lukem #include "ldap_pvt.h"
     89  1.1  lukem #include "lber_pvt.h"
     90  1.1  lukem 
     91  1.1  lukem #include "lutil_md5.h"
     92  1.1  lukem #include "lutil_sha1.h"
     93  1.1  lukem #include "lutil.h"
     94  1.1  lukem 
     95  1.1  lukem static const unsigned char crypt64[] =
     96  1.1  lukem 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
     97  1.1  lukem 
     98  1.1  lukem #ifdef SLAPD_CRYPT
     99  1.1  lukem static char *salt_format = NULL;
    100  1.1  lukem static lutil_cryptfunc lutil_crypt;
    101  1.1  lukem lutil_cryptfunc *lutil_cryptptr = lutil_crypt;
    102  1.1  lukem #endif
    103  1.1  lukem 
    104  1.1  lukem /* KLUDGE:
    105  1.1  lukem  *  chk_fn is NULL iff name is {CLEARTEXT}
    106  1.1  lukem  *	otherwise, things will break
    107  1.1  lukem  */
    108  1.1  lukem struct pw_scheme {
    109  1.1  lukem 	struct berval name;
    110  1.1  lukem 	LUTIL_PASSWD_CHK_FUNC *chk_fn;
    111  1.1  lukem 	LUTIL_PASSWD_HASH_FUNC *hash_fn;
    112  1.1  lukem };
    113  1.1  lukem 
    114  1.1  lukem struct pw_slist {
    115  1.1  lukem 	struct pw_slist *next;
    116  1.1  lukem 	struct pw_scheme s;
    117  1.1  lukem };
    118  1.1  lukem 
    119  1.1  lukem /* password check routines */
    120  1.1  lukem 
    121  1.1  lukem #define	SALT_SIZE	4
    122  1.1  lukem 
    123  1.1  lukem static LUTIL_PASSWD_CHK_FUNC chk_md5;
    124  1.1  lukem static LUTIL_PASSWD_CHK_FUNC chk_smd5;
    125  1.1  lukem static LUTIL_PASSWD_HASH_FUNC hash_smd5;
    126  1.1  lukem static LUTIL_PASSWD_HASH_FUNC hash_md5;
    127  1.1  lukem 
    128  1.1  lukem 
    129  1.1  lukem #ifdef LUTIL_SHA1_BYTES
    130  1.1  lukem static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
    131  1.1  lukem static LUTIL_PASSWD_CHK_FUNC chk_sha1;
    132  1.1  lukem static LUTIL_PASSWD_HASH_FUNC hash_sha1;
    133  1.1  lukem static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
    134  1.1  lukem #endif
    135  1.1  lukem 
    136  1.1  lukem #ifdef SLAPD_LMHASH
    137  1.1  lukem static LUTIL_PASSWD_CHK_FUNC chk_lanman;
    138  1.1  lukem static LUTIL_PASSWD_HASH_FUNC hash_lanman;
    139  1.1  lukem #endif
    140  1.1  lukem 
    141  1.1  lukem #ifdef SLAPD_CRYPT
    142  1.1  lukem static LUTIL_PASSWD_CHK_FUNC chk_crypt;
    143  1.1  lukem static LUTIL_PASSWD_HASH_FUNC hash_crypt;
    144  1.1  lukem 
    145  1.1  lukem #if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
    146  1.1  lukem static LUTIL_PASSWD_CHK_FUNC chk_unix;
    147  1.1  lukem #endif
    148  1.1  lukem #endif
    149  1.1  lukem 
    150  1.1  lukem /* password hash routines */
    151  1.1  lukem 
    152  1.1  lukem #ifdef SLAPD_CLEARTEXT
    153  1.1  lukem static LUTIL_PASSWD_HASH_FUNC hash_clear;
    154  1.1  lukem #endif
    155  1.1  lukem 
    156  1.1  lukem static struct pw_slist *pw_schemes;
    157  1.1  lukem static int pw_inited;
    158  1.1  lukem 
    159  1.1  lukem static const struct pw_scheme pw_schemes_default[] =
    160  1.1  lukem {
    161  1.1  lukem #ifdef LUTIL_SHA1_BYTES
    162  1.1  lukem 	{ BER_BVC("{SSHA}"),		chk_ssha1, hash_ssha1 },
    163  1.1  lukem 	{ BER_BVC("{SHA}"),			chk_sha1, hash_sha1 },
    164  1.1  lukem #endif
    165  1.1  lukem 
    166  1.1  lukem 	{ BER_BVC("{SMD5}"),		chk_smd5, hash_smd5 },
    167  1.1  lukem 	{ BER_BVC("{MD5}"),			chk_md5, hash_md5 },
    168  1.1  lukem 
    169  1.1  lukem #ifdef SLAPD_LMHASH
    170  1.1  lukem 	{ BER_BVC("{LANMAN}"),		chk_lanman, hash_lanman },
    171  1.1  lukem #endif /* SLAPD_LMHASH */
    172  1.1  lukem 
    173  1.1  lukem #ifdef SLAPD_CRYPT
    174  1.1  lukem 	{ BER_BVC("{CRYPT}"),		chk_crypt, hash_crypt },
    175  1.1  lukem # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
    176  1.1  lukem 	{ BER_BVC("{UNIX}"),		chk_unix, NULL },
    177  1.1  lukem # endif
    178  1.1  lukem #endif
    179  1.1  lukem 
    180  1.1  lukem #ifdef SLAPD_CLEARTEXT
    181  1.1  lukem 	/* pseudo scheme */
    182  1.1  lukem 	{ BER_BVC("{CLEARTEXT}"),	NULL, hash_clear },
    183  1.1  lukem #endif
    184  1.1  lukem 
    185  1.1  lukem 	{ BER_BVNULL, NULL, NULL }
    186  1.1  lukem };
    187  1.1  lukem 
    188  1.1  lukem int lutil_passwd_add(
    189  1.1  lukem 	struct berval *scheme,
    190  1.1  lukem 	LUTIL_PASSWD_CHK_FUNC *chk,
    191  1.1  lukem 	LUTIL_PASSWD_HASH_FUNC *hash )
    192  1.1  lukem {
    193  1.1  lukem 	struct pw_slist *ptr;
    194  1.1  lukem 
    195  1.1  lukem 	if (!pw_inited) lutil_passwd_init();
    196  1.1  lukem 
    197  1.1  lukem 	ptr = ber_memalloc( sizeof( struct pw_slist ));
    198  1.1  lukem 	if (!ptr) return -1;
    199  1.1  lukem 	ptr->next = pw_schemes;
    200  1.1  lukem 	ptr->s.name = *scheme;
    201  1.1  lukem 	ptr->s.chk_fn = chk;
    202  1.1  lukem 	ptr->s.hash_fn = hash;
    203  1.1  lukem 	pw_schemes = ptr;
    204  1.1  lukem 	return 0;
    205  1.1  lukem }
    206  1.1  lukem 
    207  1.1  lukem void lutil_passwd_init()
    208  1.1  lukem {
    209  1.1  lukem 	struct pw_scheme *s;
    210  1.1  lukem 
    211  1.1  lukem 	pw_inited = 1;
    212  1.1  lukem 
    213  1.1  lukem 	for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
    214  1.1  lukem 		if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn ) ) break;
    215  1.1  lukem 	}
    216  1.1  lukem }
    217  1.1  lukem 
    218  1.1  lukem void lutil_passwd_destroy()
    219  1.1  lukem {
    220  1.1  lukem 	struct pw_slist *ptr, *next;
    221  1.1  lukem 
    222  1.1  lukem 	for( ptr=pw_schemes; ptr; ptr=next ) {
    223  1.1  lukem 		next = ptr->next;
    224  1.1  lukem 		ber_memfree( ptr );
    225  1.1  lukem 	}
    226  1.1  lukem }
    227  1.1  lukem 
    228  1.1  lukem static const struct pw_scheme *get_scheme(
    229  1.1  lukem 	const char* scheme )
    230  1.1  lukem {
    231  1.1  lukem 	struct pw_slist *pws;
    232  1.1  lukem 	struct berval bv;
    233  1.1  lukem 
    234  1.1  lukem 	if (!pw_inited) lutil_passwd_init();
    235  1.1  lukem 
    236  1.1  lukem 	bv.bv_val = strchr( scheme, '}' );
    237  1.1  lukem 	if ( !bv.bv_val )
    238  1.1  lukem 		return NULL;
    239  1.1  lukem 
    240  1.1  lukem 	bv.bv_len = bv.bv_val - scheme + 1;
    241  1.1  lukem 	bv.bv_val = (char *) scheme;
    242  1.1  lukem 
    243  1.1  lukem 	for( pws=pw_schemes; pws; pws=pws->next ) {
    244  1.1  lukem 		if ( ber_bvstrcasecmp(&bv, &pws->s.name ) == 0 ) {
    245  1.1  lukem 			return &(pws->s);
    246  1.1  lukem 		}
    247  1.1  lukem 	}
    248  1.1  lukem 
    249  1.1  lukem 	return NULL;
    250  1.1  lukem }
    251  1.1  lukem 
    252  1.1  lukem int lutil_passwd_scheme(
    253  1.1  lukem 	const char* scheme )
    254  1.1  lukem {
    255  1.1  lukem 	if( scheme == NULL ) {
    256  1.1  lukem 		return 0;
    257  1.1  lukem 	}
    258  1.1  lukem 
    259  1.1  lukem 	return get_scheme(scheme) != NULL;
    260  1.1  lukem }
    261  1.1  lukem 
    262  1.1  lukem 
    263  1.1  lukem static int is_allowed_scheme(
    264  1.1  lukem 	const char* scheme,
    265  1.1  lukem 	const char** schemes )
    266  1.1  lukem {
    267  1.1  lukem 	int i;
    268  1.1  lukem 
    269  1.1  lukem 	if( schemes == NULL ) return 1;
    270  1.1  lukem 
    271  1.1  lukem 	for( i=0; schemes[i] != NULL; i++ ) {
    272  1.1  lukem 		if( strcasecmp( scheme, schemes[i] ) == 0 ) {
    273  1.1  lukem 			return 1;
    274  1.1  lukem 		}
    275  1.1  lukem 	}
    276  1.1  lukem 	return 0;
    277  1.1  lukem }
    278  1.1  lukem 
    279  1.1  lukem static struct berval *passwd_scheme(
    280  1.1  lukem 	const struct pw_scheme *scheme,
    281  1.1  lukem 	const struct berval * passwd,
    282  1.1  lukem 	struct berval *bv,
    283  1.1  lukem 	const char** allowed )
    284  1.1  lukem {
    285  1.1  lukem 	if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
    286  1.1  lukem 		return NULL;
    287  1.1  lukem 	}
    288  1.1  lukem 
    289  1.1  lukem 	if( passwd->bv_len >= scheme->name.bv_len ) {
    290  1.1  lukem 		if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
    291  1.1  lukem 			bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
    292  1.1  lukem 			bv->bv_len = passwd->bv_len - scheme->name.bv_len;
    293  1.1  lukem 
    294  1.1  lukem 			return bv;
    295  1.1  lukem 		}
    296  1.1  lukem 	}
    297  1.1  lukem 
    298  1.1  lukem 	return NULL;
    299  1.1  lukem }
    300  1.1  lukem 
    301  1.1  lukem /*
    302  1.1  lukem  * Return 0 if creds are good.
    303  1.1  lukem  */
    304  1.1  lukem int
    305  1.1  lukem lutil_passwd(
    306  1.1  lukem 	const struct berval *passwd,	/* stored passwd */
    307  1.1  lukem 	const struct berval *cred,		/* user cred */
    308  1.1  lukem 	const char **schemes,
    309  1.1  lukem 	const char **text )
    310  1.1  lukem {
    311  1.1  lukem 	struct pw_slist *pws;
    312  1.1  lukem 
    313  1.1  lukem 	if ( text ) *text = NULL;
    314  1.1  lukem 
    315  1.1  lukem 	if (cred == NULL || cred->bv_len == 0 ||
    316  1.1  lukem 		passwd == NULL || passwd->bv_len == 0 )
    317  1.1  lukem 	{
    318  1.1  lukem 		return -1;
    319  1.1  lukem 	}
    320  1.1  lukem 
    321  1.1  lukem 	if (!pw_inited) lutil_passwd_init();
    322  1.1  lukem 
    323  1.1  lukem 	for( pws=pw_schemes; pws; pws=pws->next ) {
    324  1.1  lukem 		if( pws->s.chk_fn ) {
    325  1.1  lukem 			struct berval x;
    326  1.1  lukem 			struct berval *p = passwd_scheme( &(pws->s),
    327  1.1  lukem 				passwd, &x, schemes );
    328  1.1  lukem 
    329  1.1  lukem 			if( p != NULL ) {
    330  1.1  lukem 				return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
    331  1.1  lukem 			}
    332  1.1  lukem 		}
    333  1.1  lukem 	}
    334  1.1  lukem 
    335  1.1  lukem #ifdef SLAPD_CLEARTEXT
    336  1.1  lukem 	/* Do we think there is a scheme specifier here that we
    337  1.1  lukem 	 * didn't recognize? Assume a scheme name is at least 1 character.
    338  1.1  lukem 	 */
    339  1.1  lukem 	if (( passwd->bv_val[0] == '{' ) &&
    340  1.1  lukem 		( ber_bvchr( passwd, '}' ) > passwd->bv_val+1 ))
    341  1.1  lukem 	{
    342  1.1  lukem 		return 1;
    343  1.1  lukem 	}
    344  1.1  lukem 	if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
    345  1.1  lukem 		return ( passwd->bv_len == cred->bv_len ) ?
    346  1.1  lukem 			memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
    347  1.1  lukem 			: 1;
    348  1.1  lukem 	}
    349  1.1  lukem #endif
    350  1.1  lukem 	return 1;
    351  1.1  lukem }
    352  1.1  lukem 
    353  1.1  lukem int lutil_passwd_generate( struct berval *pw, ber_len_t len )
    354  1.1  lukem {
    355  1.1  lukem 
    356  1.1  lukem 	if( len < 1 ) return -1;
    357  1.1  lukem 
    358  1.1  lukem 	pw->bv_len = len;
    359  1.1  lukem 	pw->bv_val = ber_memalloc( len + 1 );
    360  1.1  lukem 
    361  1.1  lukem 	if( pw->bv_val == NULL ) {
    362  1.1  lukem 		return -1;
    363  1.1  lukem 	}
    364  1.1  lukem 
    365  1.1  lukem 	if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
    366  1.1  lukem 		return -1;
    367  1.1  lukem 	}
    368  1.1  lukem 
    369  1.1  lukem 	for( len = 0; len < pw->bv_len; len++ ) {
    370  1.1  lukem 		pw->bv_val[len] = crypt64[
    371  1.1  lukem 			pw->bv_val[len] % (sizeof(crypt64)-1) ];
    372  1.1  lukem 	}
    373  1.1  lukem 
    374  1.1  lukem 	pw->bv_val[len] = '\0';
    375  1.1  lukem 
    376  1.1  lukem 	return 0;
    377  1.1  lukem }
    378  1.1  lukem 
    379  1.1  lukem int lutil_passwd_hash(
    380  1.1  lukem 	const struct berval * passwd,
    381  1.1  lukem 	const char * method,
    382  1.1  lukem 	struct berval *hash,
    383  1.1  lukem 	const char **text )
    384  1.1  lukem {
    385  1.1  lukem 	const struct pw_scheme *sc = get_scheme( method );
    386  1.1  lukem 
    387  1.1  lukem 	hash->bv_val = NULL;
    388  1.1  lukem 	hash->bv_len = 0;
    389  1.1  lukem 
    390  1.1  lukem 	if( sc == NULL ) {
    391  1.1  lukem 		if( text ) *text = "scheme not recognized";
    392  1.1  lukem 		return -1;
    393  1.1  lukem 	}
    394  1.1  lukem 
    395  1.1  lukem 	if( ! sc->hash_fn ) {
    396  1.1  lukem 		if( text ) *text = "scheme provided no hash function";
    397  1.1  lukem 		return -1;
    398  1.1  lukem 	}
    399  1.1  lukem 
    400  1.1  lukem 	if( text ) *text = NULL;
    401  1.1  lukem 
    402  1.1  lukem 	return (sc->hash_fn)( &sc->name, passwd, hash, text );
    403  1.1  lukem }
    404  1.1  lukem 
    405  1.1  lukem /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
    406  1.1  lukem #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
    407  1.1  lukem static int pw_string(
    408  1.1  lukem 	const struct berval *sc,
    409  1.1  lukem 	struct berval *passwd )
    410  1.1  lukem {
    411  1.1  lukem 	struct berval pw;
    412  1.1  lukem 
    413  1.1  lukem 	pw.bv_len = sc->bv_len + passwd->bv_len;
    414  1.1  lukem 	pw.bv_val = ber_memalloc( pw.bv_len + 1 );
    415  1.1  lukem 
    416  1.1  lukem 	if( pw.bv_val == NULL ) {
    417  1.1  lukem 		return LUTIL_PASSWD_ERR;
    418  1.1  lukem 	}
    419  1.1  lukem 
    420  1.1  lukem 	AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
    421  1.1  lukem 	AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
    422  1.1  lukem 
    423  1.1  lukem 	pw.bv_val[pw.bv_len] = '\0';
    424  1.1  lukem 	*passwd = pw;
    425  1.1  lukem 
    426  1.1  lukem 	return LUTIL_PASSWD_OK;
    427  1.1  lukem }
    428  1.1  lukem #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
    429  1.1  lukem 
    430  1.1  lukem static int pw_string64(
    431  1.1  lukem 	const struct berval *sc,
    432  1.1  lukem 	const struct berval *hash,
    433  1.1  lukem 	struct berval *b64,
    434  1.1  lukem 	const struct berval *salt )
    435  1.1  lukem {
    436  1.1  lukem 	int rc;
    437  1.1  lukem 	struct berval string;
    438  1.1  lukem 	size_t b64len;
    439  1.1  lukem 
    440  1.1  lukem 	if( salt ) {
    441  1.1  lukem 		/* need to base64 combined string */
    442  1.1  lukem 		string.bv_len = hash->bv_len + salt->bv_len;
    443  1.1  lukem 		string.bv_val = ber_memalloc( string.bv_len + 1 );
    444  1.1  lukem 
    445  1.1  lukem 		if( string.bv_val == NULL ) {
    446  1.1  lukem 			return LUTIL_PASSWD_ERR;
    447  1.1  lukem 		}
    448  1.1  lukem 
    449  1.1  lukem 		AC_MEMCPY( string.bv_val, hash->bv_val,
    450  1.1  lukem 			hash->bv_len );
    451  1.1  lukem 		AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
    452  1.1  lukem 			salt->bv_len );
    453  1.1  lukem 		string.bv_val[string.bv_len] = '\0';
    454  1.1  lukem 
    455  1.1  lukem 	} else {
    456  1.1  lukem 		string = *hash;
    457  1.1  lukem 	}
    458  1.1  lukem 
    459  1.1  lukem 	b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
    460  1.1  lukem 	b64->bv_len = b64len + sc->bv_len;
    461  1.1  lukem 	b64->bv_val = ber_memalloc( b64->bv_len + 1 );
    462  1.1  lukem 
    463  1.1  lukem 	if( b64->bv_val == NULL ) {
    464  1.1  lukem 		if( salt ) ber_memfree( string.bv_val );
    465  1.1  lukem 		return LUTIL_PASSWD_ERR;
    466  1.1  lukem 	}
    467  1.1  lukem 
    468  1.1  lukem 	AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
    469  1.1  lukem 
    470  1.1  lukem 	rc = lutil_b64_ntop(
    471  1.1  lukem 		(unsigned char *) string.bv_val, string.bv_len,
    472  1.1  lukem 		&b64->bv_val[sc->bv_len], b64len );
    473  1.1  lukem 
    474  1.1  lukem 	if( salt ) ber_memfree( string.bv_val );
    475  1.1  lukem 
    476  1.1  lukem 	if( rc < 0 ) {
    477  1.1  lukem 		return LUTIL_PASSWD_ERR;
    478  1.1  lukem 	}
    479  1.1  lukem 
    480  1.1  lukem 	/* recompute length */
    481  1.1  lukem 	b64->bv_len = sc->bv_len + rc;
    482  1.1  lukem 	assert( strlen(b64->bv_val) == b64->bv_len );
    483  1.1  lukem 	return LUTIL_PASSWD_OK;
    484  1.1  lukem }
    485  1.1  lukem 
    486  1.1  lukem /* PASSWORD CHECK ROUTINES */
    487  1.1  lukem 
    488  1.1  lukem #ifdef LUTIL_SHA1_BYTES
    489  1.1  lukem static int chk_ssha1(
    490  1.1  lukem 	const struct berval *sc,
    491  1.1  lukem 	const struct berval * passwd,
    492  1.1  lukem 	const struct berval * cred,
    493  1.1  lukem 	const char **text )
    494  1.1  lukem {
    495  1.1  lukem 	lutil_SHA1_CTX SHA1context;
    496  1.1  lukem 	unsigned char SHA1digest[LUTIL_SHA1_BYTES];
    497  1.1  lukem 	int rc;
    498  1.1  lukem 	unsigned char *orig_pass = NULL;
    499  1.1  lukem 
    500  1.1  lukem 	/* safety check -- must have some salt */
    501  1.1  lukem 	if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(SHA1digest)) {
    502  1.1  lukem 		return LUTIL_PASSWD_ERR;
    503  1.1  lukem 	}
    504  1.1  lukem 
    505  1.1  lukem 	/* decode base64 password */
    506  1.1  lukem 	orig_pass = (unsigned char *) ber_memalloc( (size_t) (
    507  1.1  lukem 		LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
    508  1.1  lukem 
    509  1.1  lukem 	if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
    510  1.1  lukem 
    511  1.1  lukem 	rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
    512  1.1  lukem 
    513  1.1  lukem 	/* safety check -- must have some salt */
    514  1.1  lukem 	if (rc <= (int)(sizeof(SHA1digest))) {
    515  1.1  lukem 		ber_memfree(orig_pass);
    516  1.1  lukem 		return LUTIL_PASSWD_ERR;
    517  1.1  lukem 	}
    518  1.1  lukem 
    519  1.1  lukem 	/* hash credentials with salt */
    520  1.1  lukem 	lutil_SHA1Init(&SHA1context);
    521  1.1  lukem 	lutil_SHA1Update(&SHA1context,
    522  1.1  lukem 		(const unsigned char *) cred->bv_val, cred->bv_len);
    523  1.1  lukem 	lutil_SHA1Update(&SHA1context,
    524  1.1  lukem 		(const unsigned char *) &orig_pass[sizeof(SHA1digest)],
    525  1.1  lukem 		rc - sizeof(SHA1digest));
    526  1.1  lukem 	lutil_SHA1Final(SHA1digest, &SHA1context);
    527  1.1  lukem 
    528  1.1  lukem 	/* compare */
    529  1.1  lukem 	rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
    530  1.1  lukem 	ber_memfree(orig_pass);
    531  1.1  lukem 	return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    532  1.1  lukem }
    533  1.1  lukem 
    534  1.1  lukem static int chk_sha1(
    535  1.1  lukem 	const struct berval *sc,
    536  1.1  lukem 	const struct berval * passwd,
    537  1.1  lukem 	const struct berval * cred,
    538  1.1  lukem 	const char **text )
    539  1.1  lukem {
    540  1.1  lukem 	lutil_SHA1_CTX SHA1context;
    541  1.1  lukem 	unsigned char SHA1digest[LUTIL_SHA1_BYTES];
    542  1.1  lukem 	int rc;
    543  1.1  lukem 	unsigned char *orig_pass = NULL;
    544  1.1  lukem 
    545  1.1  lukem 	/* safety check */
    546  1.1  lukem 	if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(SHA1digest)) {
    547  1.1  lukem 		return LUTIL_PASSWD_ERR;
    548  1.1  lukem 	}
    549  1.1  lukem 
    550  1.1  lukem 	/* base64 un-encode password */
    551  1.1  lukem 	orig_pass = (unsigned char *) ber_memalloc( (size_t) (
    552  1.1  lukem 		LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
    553  1.1  lukem 
    554  1.1  lukem 	if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
    555  1.1  lukem 
    556  1.1  lukem 	rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
    557  1.1  lukem 
    558  1.1  lukem 	if( rc != sizeof(SHA1digest) ) {
    559  1.1  lukem 		ber_memfree(orig_pass);
    560  1.1  lukem 		return LUTIL_PASSWD_ERR;
    561  1.1  lukem 	}
    562  1.1  lukem 
    563  1.1  lukem 	/* hash credentials with salt */
    564  1.1  lukem 	lutil_SHA1Init(&SHA1context);
    565  1.1  lukem 	lutil_SHA1Update(&SHA1context,
    566  1.1  lukem 		(const unsigned char *) cred->bv_val, cred->bv_len);
    567  1.1  lukem 	lutil_SHA1Final(SHA1digest, &SHA1context);
    568  1.1  lukem 
    569  1.1  lukem 	/* compare */
    570  1.1  lukem 	rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
    571  1.1  lukem 	ber_memfree(orig_pass);
    572  1.1  lukem 	return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    573  1.1  lukem }
    574  1.1  lukem #endif
    575  1.1  lukem 
    576  1.1  lukem static int chk_smd5(
    577  1.1  lukem 	const struct berval *sc,
    578  1.1  lukem 	const struct berval * passwd,
    579  1.1  lukem 	const struct berval * cred,
    580  1.1  lukem 	const char **text )
    581  1.1  lukem {
    582  1.1  lukem 	lutil_MD5_CTX MD5context;
    583  1.1  lukem 	unsigned char MD5digest[LUTIL_MD5_BYTES];
    584  1.1  lukem 	int rc;
    585  1.1  lukem 	unsigned char *orig_pass = NULL;
    586  1.1  lukem 
    587  1.1  lukem 	/* safety check */
    588  1.1  lukem 	if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) <= sizeof(MD5digest)) {
    589  1.1  lukem 		return LUTIL_PASSWD_ERR;
    590  1.1  lukem 	}
    591  1.1  lukem 
    592  1.1  lukem 	/* base64 un-encode password */
    593  1.1  lukem 	orig_pass = (unsigned char *) ber_memalloc( (size_t) (
    594  1.1  lukem 		LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
    595  1.1  lukem 
    596  1.1  lukem 	if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
    597  1.1  lukem 
    598  1.1  lukem 	rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
    599  1.1  lukem 
    600  1.1  lukem 	if (rc <= (int)(sizeof(MD5digest))) {
    601  1.1  lukem 		ber_memfree(orig_pass);
    602  1.1  lukem 		return LUTIL_PASSWD_ERR;
    603  1.1  lukem 	}
    604  1.1  lukem 
    605  1.1  lukem 	/* hash credentials with salt */
    606  1.1  lukem 	lutil_MD5Init(&MD5context);
    607  1.1  lukem 	lutil_MD5Update(&MD5context,
    608  1.1  lukem 		(const unsigned char *) cred->bv_val,
    609  1.1  lukem 		cred->bv_len );
    610  1.1  lukem 	lutil_MD5Update(&MD5context,
    611  1.1  lukem 		&orig_pass[sizeof(MD5digest)],
    612  1.1  lukem 		rc - sizeof(MD5digest));
    613  1.1  lukem 	lutil_MD5Final(MD5digest, &MD5context);
    614  1.1  lukem 
    615  1.1  lukem 	/* compare */
    616  1.1  lukem 	rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
    617  1.1  lukem 	ber_memfree(orig_pass);
    618  1.1  lukem 	return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    619  1.1  lukem }
    620  1.1  lukem 
    621  1.1  lukem static int chk_md5(
    622  1.1  lukem 	const struct berval *sc,
    623  1.1  lukem 	const struct berval * passwd,
    624  1.1  lukem 	const struct berval * cred,
    625  1.1  lukem 	const char **text )
    626  1.1  lukem {
    627  1.1  lukem 	lutil_MD5_CTX MD5context;
    628  1.1  lukem 	unsigned char MD5digest[LUTIL_MD5_BYTES];
    629  1.1  lukem 	int rc;
    630  1.1  lukem 	unsigned char *orig_pass = NULL;
    631  1.1  lukem 
    632  1.1  lukem 	/* safety check */
    633  1.1  lukem 	if (LUTIL_BASE64_DECODE_LEN(passwd->bv_len) < sizeof(MD5digest)) {
    634  1.1  lukem 		return LUTIL_PASSWD_ERR;
    635  1.1  lukem 	}
    636  1.1  lukem 
    637  1.1  lukem 	/* base64 un-encode password */
    638  1.1  lukem 	orig_pass = (unsigned char *) ber_memalloc( (size_t) (
    639  1.1  lukem 		LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
    640  1.1  lukem 
    641  1.1  lukem 	if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
    642  1.1  lukem 
    643  1.1  lukem 	rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
    644  1.1  lukem 	if ( rc != sizeof(MD5digest) ) {
    645  1.1  lukem 		ber_memfree(orig_pass);
    646  1.1  lukem 		return LUTIL_PASSWD_ERR;
    647  1.1  lukem 	}
    648  1.1  lukem 
    649  1.1  lukem 	/* hash credentials with salt */
    650  1.1  lukem 	lutil_MD5Init(&MD5context);
    651  1.1  lukem 	lutil_MD5Update(&MD5context,
    652  1.1  lukem 		(const unsigned char *) cred->bv_val,
    653  1.1  lukem 		cred->bv_len );
    654  1.1  lukem 	lutil_MD5Final(MD5digest, &MD5context);
    655  1.1  lukem 
    656  1.1  lukem 	/* compare */
    657  1.1  lukem 	rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
    658  1.1  lukem 	ber_memfree(orig_pass);
    659  1.1  lukem 	return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    660  1.1  lukem }
    661  1.1  lukem 
    662  1.1  lukem #ifdef SLAPD_LMHASH
    663  1.2  lukem 
    664  1.2  lukem #if defined(HAVE_OPENSSL)
    665  1.2  lukem 
    666  1.2  lukem /*
    667  1.2  lukem  * abstract away setting the parity.
    668  1.2  lukem  */
    669  1.2  lukem static void
    670  1.2  lukem des_set_key_and_parity( des_key *key, unsigned char *keyData)
    671  1.2  lukem {
    672  1.2  lukem     memcpy(key, keyData, 8);
    673  1.2  lukem     des_set_odd_parity( key );
    674  1.2  lukem }
    675  1.2  lukem 
    676  1.2  lukem 
    677  1.2  lukem #elif defined(HAVE_MOZNSS)
    678  1.2  lukem 
    679  1.2  lukem /*
    680  1.2  lukem  * implement MozNSS wrappers for the openSSL calls
    681  1.2  lukem  */
    682  1.2  lukem static void
    683  1.2  lukem des_set_key_and_parity( des_key *key, unsigned char *keyData)
    684  1.2  lukem {
    685  1.2  lukem     SECItem keyDataItem;
    686  1.2  lukem     PK11SlotInfo *slot;
    687  1.2  lukem     *key = NULL;
    688  1.2  lukem 
    689  1.2  lukem     keyDataItem.data = keyData;
    690  1.2  lukem     keyDataItem.len = 8;
    691  1.2  lukem 
    692  1.2  lukem     slot = PK11_GetBestSlot(CKM_DES_ECB, NULL);
    693  1.2  lukem     if (slot == NULL) {
    694  1.2  lukem 	return;
    695  1.2  lukem     }
    696  1.2  lukem 
    697  1.2  lukem     /* NOTE: this will not work in FIPS mode. In order to make lmhash
    698  1.2  lukem      * work in fips mode we need to define a LMHASH pbe mechanism and
    699  1.2  lukem      * do the fulll key derivation inside the token */
    700  1.2  lukem     *key = PK11_ImportSymKey(slot, CKM_DES_ECB, PK11_OriginGenerated,
    701  1.2  lukem 		CKA_ENCRYPT, &keyDataItem, NULL);
    702  1.2  lukem }
    703  1.2  lukem 
    704  1.2  lukem static void
    705  1.2  lukem des_set_key_unchecked( des_key *key, des_context ctxt )
    706  1.2  lukem {
    707  1.2  lukem     ctxt[0] = NULL;
    708  1.2  lukem 
    709  1.2  lukem     /* handle error conditions from previous call */
    710  1.2  lukem     if (!*key) {
    711  1.2  lukem 	return;
    712  1.2  lukem     }
    713  1.2  lukem 
    714  1.2  lukem     ctxt[0] = PK11_CreateContextBySymKey(CKM_DES_ECB, CKA_ENCRYPT, *key, NULL);
    715  1.2  lukem }
    716  1.2  lukem 
    717  1.2  lukem static void
    718  1.2  lukem des_ecb_encrypt( des_data_block *plain, des_data_block *encrypted,
    719  1.2  lukem 			des_context ctxt, int op)
    720  1.2  lukem {
    721  1.2  lukem     SECStatus rv;
    722  1.2  lukem     int size;
    723  1.2  lukem 
    724  1.2  lukem     if (ctxt[0] == NULL) {
    725  1.2  lukem 	/* need to fail here...  */
    726  1.2  lukem 	memset(encrypted, 0, sizeof(des_data_block));
    727  1.2  lukem 	return;
    728  1.2  lukem     }
    729  1.2  lukem     rv = PK11_CipherOp(ctxt[0], (unsigned char *)&encrypted[0],
    730  1.2  lukem 			&size, sizeof(des_data_block),
    731  1.2  lukem 			(unsigned char *)&plain[0], sizeof(des_data_block));
    732  1.2  lukem     if (rv != SECSuccess) {
    733  1.2  lukem 	/* signal failure */
    734  1.2  lukem 	memset(encrypted, 0, sizeof(des_data_block));
    735  1.2  lukem 	return;
    736  1.2  lukem     }
    737  1.2  lukem     return;
    738  1.2  lukem }
    739  1.2  lukem 
    740  1.2  lukem static int
    741  1.2  lukem des_failed(des_data_block *encrypted)
    742  1.2  lukem {
    743  1.2  lukem    static const des_data_block zero = { 0 };
    744  1.2  lukem    return memcmp(encrypted, zero, sizeof(zero)) == 0;
    745  1.2  lukem }
    746  1.2  lukem 
    747  1.2  lukem static void
    748  1.2  lukem des_finish(des_key *key, des_context ctxt)
    749  1.2  lukem {
    750  1.2  lukem      if (*key) {
    751  1.2  lukem 	PK11_FreeSymKey(*key);
    752  1.2  lukem 	*key = NULL;
    753  1.2  lukem      }
    754  1.2  lukem      if (ctxt[0]) {
    755  1.2  lukem 	PK11_Finalize(ctxt[0]);
    756  1.2  lukem 	PK11_DestroyContext(ctxt[0], PR_TRUE);
    757  1.2  lukem 	ctxt[0] = NULL;
    758  1.2  lukem      }
    759  1.2  lukem }
    760  1.2  lukem 
    761  1.2  lukem #endif
    762  1.2  lukem 
    763  1.1  lukem /* pseudocode from RFC2433
    764  1.1  lukem  * A.2 LmPasswordHash()
    765  1.1  lukem  *
    766  1.1  lukem  *    LmPasswordHash(
    767  1.1  lukem  *    IN  0-to-14-oem-char Password,
    768  1.1  lukem  *    OUT 16-octet         PasswordHash )
    769  1.1  lukem  *    {
    770  1.1  lukem  *       Set UcasePassword to the uppercased Password
    771  1.1  lukem  *       Zero pad UcasePassword to 14 characters
    772  1.1  lukem  *
    773  1.1  lukem  *       DesHash( 1st 7-octets of UcasePassword,
    774  1.1  lukem  *                giving 1st 8-octets of PasswordHash )
    775  1.1  lukem  *
    776  1.1  lukem  *       DesHash( 2nd 7-octets of UcasePassword,
    777  1.1  lukem  *                giving 2nd 8-octets of PasswordHash )
    778  1.1  lukem  *    }
    779  1.1  lukem  *
    780  1.1  lukem  *
    781  1.1  lukem  * A.3 DesHash()
    782  1.1  lukem  *
    783  1.1  lukem  *    DesHash(
    784  1.1  lukem  *    IN  7-octet Clear,
    785  1.1  lukem  *    OUT 8-octet Cypher )
    786  1.1  lukem  *    {
    787  1.1  lukem  *        *
    788  1.1  lukem  *        * Make Cypher an irreversibly encrypted form of Clear by
    789  1.1  lukem  *        * encrypting known text using Clear as the secret key.
    790  1.1  lukem  *        * The known text consists of the string
    791  1.1  lukem  *        *
    792  1.1  lukem  *        *              KGS!@#$%
    793  1.1  lukem  *        *
    794  1.1  lukem  *
    795  1.1  lukem  *       Set StdText to "KGS!@#$%"
    796  1.1  lukem  *       DesEncrypt( StdText, Clear, giving Cypher )
    797  1.1  lukem  *    }
    798  1.1  lukem  *
    799  1.1  lukem  *
    800  1.1  lukem  * A.4 DesEncrypt()
    801  1.1  lukem  *
    802  1.1  lukem  *    DesEncrypt(
    803  1.1  lukem  *    IN  8-octet Clear,
    804  1.1  lukem  *    IN  7-octet Key,
    805  1.1  lukem  *    OUT 8-octet Cypher )
    806  1.1  lukem  *    {
    807  1.1  lukem  *        *
    808  1.1  lukem  *        * Use the DES encryption algorithm [4] in ECB mode [9]
    809  1.1  lukem  *        * to encrypt Clear into Cypher such that Cypher can
    810  1.1  lukem  *        * only be decrypted back to Clear by providing Key.
    811  1.1  lukem  *        * Note that the DES algorithm takes as input a 64-bit
    812  1.1  lukem  *        * stream where the 8th, 16th, 24th, etc.  bits are
    813  1.1  lukem  *        * parity bits ignored by the encrypting algorithm.
    814  1.1  lukem  *        * Unless you write your own DES to accept 56-bit input
    815  1.1  lukem  *        * without parity, you will need to insert the parity bits
    816  1.1  lukem  *        * yourself.
    817  1.1  lukem  *        *
    818  1.1  lukem  *    }
    819  1.1  lukem  */
    820  1.1  lukem 
    821  1.1  lukem static void lmPasswd_to_key(
    822  1.1  lukem 	const char *lmPasswd,
    823  1.2  lukem 	des_key *key)
    824  1.1  lukem {
    825  1.1  lukem 	const unsigned char *lpw = (const unsigned char *) lmPasswd;
    826  1.2  lukem 	unsigned char k[8];
    827  1.1  lukem 
    828  1.1  lukem 	/* make room for parity bits */
    829  1.1  lukem 	k[0] = lpw[0];
    830  1.1  lukem 	k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
    831  1.1  lukem 	k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
    832  1.1  lukem 	k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
    833  1.1  lukem 	k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
    834  1.1  lukem 	k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
    835  1.1  lukem 	k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
    836  1.1  lukem 	k[7] = ((lpw[6] & 0x7F) << 1);
    837  1.1  lukem 
    838  1.2  lukem 	des_set_key_and_parity( key, k );
    839  1.1  lukem }
    840  1.1  lukem 
    841  1.1  lukem static int chk_lanman(
    842  1.1  lukem 	const struct berval *scheme,
    843  1.1  lukem 	const struct berval *passwd,
    844  1.1  lukem 	const struct berval *cred,
    845  1.1  lukem 	const char **text )
    846  1.1  lukem {
    847  1.2  lukem 	ber_len_t i;
    848  1.1  lukem 	char UcasePassword[15];
    849  1.2  lukem 	des_key key;
    850  1.2  lukem 	des_context schedule;
    851  1.2  lukem 	des_data_block StdText = "KGS!@#$%";
    852  1.2  lukem 	des_data_block PasswordHash1, PasswordHash2;
    853  1.1  lukem 	char PasswordHash[33], storedPasswordHash[33];
    854  1.1  lukem 
    855  1.1  lukem 	for( i=0; i<cred->bv_len; i++) {
    856  1.1  lukem 		if(cred->bv_val[i] == '\0') {
    857  1.1  lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    858  1.1  lukem 		}
    859  1.1  lukem 	}
    860  1.1  lukem 
    861  1.1  lukem 	if( cred->bv_val[i] != '\0' ) {
    862  1.1  lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
    863  1.1  lukem 	}
    864  1.1  lukem 
    865  1.1  lukem 	strncpy( UcasePassword, cred->bv_val, 14 );
    866  1.1  lukem 	UcasePassword[14] = '\0';
    867  1.1  lukem 	ldap_pvt_str2upper( UcasePassword );
    868  1.1  lukem 
    869  1.1  lukem 	lmPasswd_to_key( UcasePassword, &key );
    870  1.1  lukem 	des_set_key_unchecked( &key, schedule );
    871  1.1  lukem 	des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
    872  1.2  lukem 
    873  1.2  lukem 	if (des_failed(&PasswordHash1)) {
    874  1.2  lukem 	    return LUTIL_PASSWD_ERR;
    875  1.2  lukem 	}
    876  1.1  lukem 
    877  1.1  lukem 	lmPasswd_to_key( &UcasePassword[7], &key );
    878  1.1  lukem 	des_set_key_unchecked( &key, schedule );
    879  1.1  lukem 	des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
    880  1.2  lukem 	if (des_failed(&PasswordHash2)) {
    881  1.2  lukem 	    return LUTIL_PASSWD_ERR;
    882  1.2  lukem 	}
    883  1.2  lukem 
    884  1.2  lukem 	des_finish( &key, schedule );
    885  1.1  lukem 
    886  1.1  lukem 	sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
    887  1.1  lukem 		PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
    888  1.1  lukem 		PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
    889  1.1  lukem 		PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
    890  1.1  lukem 		PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
    891  1.1  lukem 
    892  1.1  lukem 	/* as a precaution convert stored password hash to lower case */
    893  1.1  lukem 	strncpy( storedPasswordHash, passwd->bv_val, 32 );
    894  1.1  lukem 	storedPasswordHash[32] = '\0';
    895  1.1  lukem 	ldap_pvt_str2lower( storedPasswordHash );
    896  1.1  lukem 
    897  1.1  lukem 	return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    898  1.1  lukem }
    899  1.1  lukem #endif /* SLAPD_LMHASH */
    900  1.1  lukem 
    901  1.1  lukem #ifdef SLAPD_CRYPT
    902  1.1  lukem static int lutil_crypt(
    903  1.1  lukem 	const char *key,
    904  1.1  lukem 	const char *salt,
    905  1.1  lukem 	char **hash )
    906  1.1  lukem {
    907  1.1  lukem 	char *cr = crypt( key, salt );
    908  1.1  lukem 	int rc;
    909  1.1  lukem 
    910  1.1  lukem 	if( cr == NULL || cr[0] == '\0' ) {
    911  1.1  lukem 		/* salt must have been invalid */
    912  1.1  lukem 		rc = LUTIL_PASSWD_ERR;
    913  1.1  lukem 	} else {
    914  1.1  lukem 		if ( hash ) {
    915  1.1  lukem 			*hash = ber_strdup( cr );
    916  1.1  lukem 			rc = LUTIL_PASSWD_OK;
    917  1.1  lukem 		} else {
    918  1.1  lukem 			rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    919  1.1  lukem 		}
    920  1.1  lukem 	}
    921  1.1  lukem 	return rc;
    922  1.1  lukem }
    923  1.1  lukem 
    924  1.1  lukem static int chk_crypt(
    925  1.1  lukem 	const struct berval *sc,
    926  1.1  lukem 	const struct berval * passwd,
    927  1.1  lukem 	const struct berval * cred,
    928  1.1  lukem 	const char **text )
    929  1.1  lukem {
    930  1.1  lukem 	unsigned int i;
    931  1.1  lukem 
    932  1.1  lukem 	for( i=0; i<cred->bv_len; i++) {
    933  1.1  lukem 		if(cred->bv_val[i] == '\0') {
    934  1.1  lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    935  1.1  lukem 		}
    936  1.1  lukem 	}
    937  1.1  lukem 
    938  1.1  lukem 	if( cred->bv_val[i] != '\0' ) {
    939  1.1  lukem 		return LUTIL_PASSWD_ERR;	/* cred must behave like a string */
    940  1.1  lukem 	}
    941  1.1  lukem 
    942  1.1  lukem 	if( passwd->bv_len < 2 ) {
    943  1.1  lukem 		return LUTIL_PASSWD_ERR;	/* passwd must be at least two characters long */
    944  1.1  lukem 	}
    945  1.1  lukem 
    946  1.1  lukem 	for( i=0; i<passwd->bv_len; i++) {
    947  1.1  lukem 		if(passwd->bv_val[i] == '\0') {
    948  1.1  lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    949  1.1  lukem 		}
    950  1.1  lukem 	}
    951  1.1  lukem 
    952  1.1  lukem 	if( passwd->bv_val[i] != '\0' ) {
    953  1.1  lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
    954  1.1  lukem 	}
    955  1.1  lukem 
    956  1.1  lukem 	return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
    957  1.1  lukem }
    958  1.1  lukem 
    959  1.1  lukem # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
    960  1.1  lukem static int chk_unix(
    961  1.1  lukem 	const struct berval *sc,
    962  1.1  lukem 	const struct berval * passwd,
    963  1.1  lukem 	const struct berval * cred,
    964  1.1  lukem 	const char **text )
    965  1.1  lukem {
    966  1.1  lukem 	unsigned int i;
    967  1.1  lukem 	char *pw;
    968  1.1  lukem 
    969  1.1  lukem 	for( i=0; i<cred->bv_len; i++) {
    970  1.1  lukem 		if(cred->bv_val[i] == '\0') {
    971  1.1  lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    972  1.1  lukem 		}
    973  1.1  lukem 	}
    974  1.1  lukem 	if( cred->bv_val[i] != '\0' ) {
    975  1.1  lukem 		return LUTIL_PASSWD_ERR;	/* cred must behave like a string */
    976  1.1  lukem 	}
    977  1.1  lukem 
    978  1.1  lukem 	for( i=0; i<passwd->bv_len; i++) {
    979  1.1  lukem 		if(passwd->bv_val[i] == '\0') {
    980  1.1  lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    981  1.1  lukem 		}
    982  1.1  lukem 	}
    983  1.1  lukem 
    984  1.1  lukem 	if( passwd->bv_val[i] != '\0' ) {
    985  1.1  lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
    986  1.1  lukem 	}
    987  1.1  lukem 
    988  1.1  lukem 	{
    989  1.1  lukem 		struct passwd *pwd = getpwnam(passwd->bv_val);
    990  1.1  lukem 
    991  1.1  lukem 		if(pwd == NULL) {
    992  1.1  lukem 			return LUTIL_PASSWD_ERR;	/* not found */
    993  1.1  lukem 		}
    994  1.1  lukem 
    995  1.1  lukem 		pw = pwd->pw_passwd;
    996  1.1  lukem 	}
    997  1.1  lukem #  ifdef HAVE_GETSPNAM
    998  1.1  lukem 	{
    999  1.1  lukem 		struct spwd *spwd = getspnam(passwd->bv_val);
   1000  1.1  lukem 
   1001  1.1  lukem 		if(spwd != NULL) {
   1002  1.1  lukem 			pw = spwd->sp_pwdp;
   1003  1.1  lukem 		}
   1004  1.1  lukem 	}
   1005  1.1  lukem #  endif
   1006  1.1  lukem #  ifdef HAVE_AIX_SECURITY
   1007  1.1  lukem 	{
   1008  1.1  lukem 		struct userpw *upw = getuserpw(passwd->bv_val);
   1009  1.1  lukem 
   1010  1.1  lukem 		if (upw != NULL) {
   1011  1.1  lukem 			pw = upw->upw_passwd;
   1012  1.1  lukem 		}
   1013  1.1  lukem 	}
   1014  1.1  lukem #  endif
   1015  1.1  lukem 
   1016  1.1  lukem 	if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
   1017  1.1  lukem 		/* password must must be at least two characters long */
   1018  1.1  lukem 		return LUTIL_PASSWD_ERR;
   1019  1.1  lukem 	}
   1020  1.1  lukem 
   1021  1.1  lukem 	return lutil_cryptptr( cred->bv_val, pw, NULL );
   1022  1.1  lukem }
   1023  1.1  lukem # endif
   1024  1.1  lukem #endif
   1025  1.1  lukem 
   1026  1.1  lukem /* PASSWORD GENERATION ROUTINES */
   1027  1.1  lukem 
   1028  1.1  lukem #ifdef LUTIL_SHA1_BYTES
   1029  1.1  lukem static int hash_ssha1(
   1030  1.1  lukem 	const struct berval *scheme,
   1031  1.1  lukem 	const struct berval  *passwd,
   1032  1.1  lukem 	struct berval *hash,
   1033  1.1  lukem 	const char **text )
   1034  1.1  lukem {
   1035  1.1  lukem 	lutil_SHA1_CTX  SHA1context;
   1036  1.1  lukem 	unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
   1037  1.1  lukem 	char            saltdata[SALT_SIZE];
   1038  1.1  lukem 	struct berval digest;
   1039  1.1  lukem 	struct berval salt;
   1040  1.1  lukem 
   1041  1.1  lukem 	digest.bv_val = (char *) SHA1digest;
   1042  1.1  lukem 	digest.bv_len = sizeof(SHA1digest);
   1043  1.1  lukem 	salt.bv_val = saltdata;
   1044  1.1  lukem 	salt.bv_len = sizeof(saltdata);
   1045  1.1  lukem 
   1046  1.1  lukem 	if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
   1047  1.1  lukem 		return LUTIL_PASSWD_ERR;
   1048  1.1  lukem 	}
   1049  1.1  lukem 
   1050  1.1  lukem 	lutil_SHA1Init( &SHA1context );
   1051  1.1  lukem 	lutil_SHA1Update( &SHA1context,
   1052  1.1  lukem 		(const unsigned char *)passwd->bv_val, passwd->bv_len );
   1053  1.1  lukem 	lutil_SHA1Update( &SHA1context,
   1054  1.1  lukem 		(const unsigned char *)salt.bv_val, salt.bv_len );
   1055  1.1  lukem 	lutil_SHA1Final( SHA1digest, &SHA1context );
   1056  1.1  lukem 
   1057  1.1  lukem 	return pw_string64( scheme, &digest, hash, &salt);
   1058  1.1  lukem }
   1059  1.1  lukem 
   1060  1.1  lukem static int hash_sha1(
   1061  1.1  lukem 	const struct berval *scheme,
   1062  1.1  lukem 	const struct berval  *passwd,
   1063  1.1  lukem 	struct berval *hash,
   1064  1.1  lukem 	const char **text )
   1065  1.1  lukem {
   1066  1.1  lukem 	lutil_SHA1_CTX  SHA1context;
   1067  1.1  lukem 	unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
   1068  1.1  lukem 	struct berval digest;
   1069  1.1  lukem 	digest.bv_val = (char *) SHA1digest;
   1070  1.1  lukem 	digest.bv_len = sizeof(SHA1digest);
   1071  1.1  lukem 
   1072  1.1  lukem 	lutil_SHA1Init( &SHA1context );
   1073  1.1  lukem 	lutil_SHA1Update( &SHA1context,
   1074  1.1  lukem 		(const unsigned char *)passwd->bv_val, passwd->bv_len );
   1075  1.1  lukem 	lutil_SHA1Final( SHA1digest, &SHA1context );
   1076  1.1  lukem 
   1077  1.1  lukem 	return pw_string64( scheme, &digest, hash, NULL);
   1078  1.1  lukem }
   1079  1.1  lukem #endif
   1080  1.1  lukem 
   1081  1.1  lukem static int hash_smd5(
   1082  1.1  lukem 	const struct berval *scheme,
   1083  1.1  lukem 	const struct berval  *passwd,
   1084  1.1  lukem 	struct berval *hash,
   1085  1.1  lukem 	const char **text )
   1086  1.1  lukem {
   1087  1.1  lukem 	lutil_MD5_CTX   MD5context;
   1088  1.1  lukem 	unsigned char   MD5digest[LUTIL_MD5_BYTES];
   1089  1.1  lukem 	char            saltdata[SALT_SIZE];
   1090  1.1  lukem 	struct berval digest;
   1091  1.1  lukem 	struct berval salt;
   1092  1.1  lukem 
   1093  1.1  lukem 	digest.bv_val = (char *) MD5digest;
   1094  1.1  lukem 	digest.bv_len = sizeof(MD5digest);
   1095  1.1  lukem 	salt.bv_val = saltdata;
   1096  1.1  lukem 	salt.bv_len = sizeof(saltdata);
   1097  1.1  lukem 
   1098  1.1  lukem 	if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
   1099  1.1  lukem 		return LUTIL_PASSWD_ERR;
   1100  1.1  lukem 	}
   1101  1.1  lukem 
   1102  1.1  lukem 	lutil_MD5Init( &MD5context );
   1103  1.1  lukem 	lutil_MD5Update( &MD5context,
   1104  1.1  lukem 		(const unsigned char *) passwd->bv_val, passwd->bv_len );
   1105  1.1  lukem 	lutil_MD5Update( &MD5context,
   1106  1.1  lukem 		(const unsigned char *) salt.bv_val, salt.bv_len );
   1107  1.1  lukem 	lutil_MD5Final( MD5digest, &MD5context );
   1108  1.1  lukem 
   1109  1.1  lukem 	return pw_string64( scheme, &digest, hash, &salt );
   1110  1.1  lukem }
   1111  1.1  lukem 
   1112  1.1  lukem static int hash_md5(
   1113  1.1  lukem 	const struct berval *scheme,
   1114  1.1  lukem 	const struct berval  *passwd,
   1115  1.1  lukem 	struct berval *hash,
   1116  1.1  lukem 	const char **text )
   1117  1.1  lukem {
   1118  1.1  lukem 	lutil_MD5_CTX   MD5context;
   1119  1.1  lukem 	unsigned char   MD5digest[LUTIL_MD5_BYTES];
   1120  1.1  lukem 
   1121  1.1  lukem 	struct berval digest;
   1122  1.1  lukem 
   1123  1.1  lukem 	digest.bv_val = (char *) MD5digest;
   1124  1.1  lukem 	digest.bv_len = sizeof(MD5digest);
   1125  1.1  lukem 
   1126  1.1  lukem 	lutil_MD5Init( &MD5context );
   1127  1.1  lukem 	lutil_MD5Update( &MD5context,
   1128  1.1  lukem 		(const unsigned char *) passwd->bv_val, passwd->bv_len );
   1129  1.1  lukem 	lutil_MD5Final( MD5digest, &MD5context );
   1130  1.1  lukem 
   1131  1.1  lukem 	return pw_string64( scheme, &digest, hash, NULL );
   1132  1.1  lukem ;
   1133  1.1  lukem }
   1134  1.1  lukem 
   1135  1.1  lukem #ifdef SLAPD_LMHASH
   1136  1.1  lukem static int hash_lanman(
   1137  1.1  lukem 	const struct berval *scheme,
   1138  1.1  lukem 	const struct berval *passwd,
   1139  1.1  lukem 	struct berval *hash,
   1140  1.1  lukem 	const char **text )
   1141  1.1  lukem {
   1142  1.1  lukem 
   1143  1.2  lukem 	ber_len_t i;
   1144  1.1  lukem 	char UcasePassword[15];
   1145  1.2  lukem 	des_key key;
   1146  1.2  lukem 	des_context schedule;
   1147  1.2  lukem 	des_data_block StdText = "KGS!@#$%";
   1148  1.2  lukem 	des_data_block PasswordHash1, PasswordHash2;
   1149  1.1  lukem 	char PasswordHash[33];
   1150  1.1  lukem 
   1151  1.1  lukem 	for( i=0; i<passwd->bv_len; i++) {
   1152  1.1  lukem 		if(passwd->bv_val[i] == '\0') {
   1153  1.1  lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
   1154  1.1  lukem 		}
   1155  1.1  lukem 	}
   1156  1.1  lukem 
   1157  1.1  lukem 	if( passwd->bv_val[i] != '\0' ) {
   1158  1.1  lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
   1159  1.1  lukem 	}
   1160  1.1  lukem 
   1161  1.1  lukem 	strncpy( UcasePassword, passwd->bv_val, 14 );
   1162  1.1  lukem 	UcasePassword[14] = '\0';
   1163  1.1  lukem 	ldap_pvt_str2upper( UcasePassword );
   1164  1.1  lukem 
   1165  1.1  lukem 	lmPasswd_to_key( UcasePassword, &key );
   1166  1.1  lukem 	des_set_key_unchecked( &key, schedule );
   1167  1.1  lukem 	des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
   1168  1.1  lukem 
   1169  1.1  lukem 	lmPasswd_to_key( &UcasePassword[7], &key );
   1170  1.1  lukem 	des_set_key_unchecked( &key, schedule );
   1171  1.1  lukem 	des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
   1172  1.1  lukem 
   1173  1.1  lukem 	sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
   1174  1.1  lukem 		PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
   1175  1.1  lukem 		PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
   1176  1.1  lukem 		PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
   1177  1.1  lukem 		PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
   1178  1.1  lukem 
   1179  1.1  lukem 	hash->bv_val = PasswordHash;
   1180  1.1  lukem 	hash->bv_len = 32;
   1181  1.1  lukem 
   1182  1.1  lukem 	return pw_string( scheme, hash );
   1183  1.1  lukem }
   1184  1.1  lukem #endif /* SLAPD_LMHASH */
   1185  1.1  lukem 
   1186  1.1  lukem #ifdef SLAPD_CRYPT
   1187  1.1  lukem static int hash_crypt(
   1188  1.1  lukem 	const struct berval *scheme,
   1189  1.1  lukem 	const struct berval *passwd,
   1190  1.1  lukem 	struct berval *hash,
   1191  1.1  lukem 	const char **text )
   1192  1.1  lukem {
   1193  1.1  lukem 	unsigned char salt[32];	/* salt suitable for most anything */
   1194  1.1  lukem 	unsigned int i;
   1195  1.1  lukem 	char *save;
   1196  1.1  lukem 	int rc;
   1197  1.1  lukem 
   1198  1.1  lukem 	for( i=0; i<passwd->bv_len; i++) {
   1199  1.1  lukem 		if(passwd->bv_val[i] == '\0') {
   1200  1.1  lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
   1201  1.1  lukem 		}
   1202  1.1  lukem 	}
   1203  1.1  lukem 
   1204  1.1  lukem 	if( passwd->bv_val[i] != '\0' ) {
   1205  1.1  lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
   1206  1.1  lukem 	}
   1207  1.1  lukem 
   1208  1.1  lukem 	if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
   1209  1.1  lukem 		return LUTIL_PASSWD_ERR;
   1210  1.1  lukem 	}
   1211  1.1  lukem 
   1212  1.1  lukem 	for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
   1213  1.1  lukem 		salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
   1214  1.1  lukem 	}
   1215  1.1  lukem 	salt[sizeof( salt ) - 1 ] = '\0';
   1216  1.1  lukem 
   1217  1.1  lukem 	if( salt_format != NULL ) {
   1218  1.1  lukem 		/* copy the salt we made into entropy before snprintfing
   1219  1.1  lukem 		   it back into the salt */
   1220  1.1  lukem 		char entropy[sizeof(salt)];
   1221  1.1  lukem 		strcpy( entropy, (char *) salt );
   1222  1.1  lukem 		snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
   1223  1.1  lukem 	}
   1224  1.1  lukem 
   1225  1.1  lukem 	rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
   1226  1.1  lukem 	if ( rc != LUTIL_PASSWD_OK ) return rc;
   1227  1.1  lukem 
   1228  1.1  lukem 	if( hash->bv_val == NULL ) return -1;
   1229  1.1  lukem 
   1230  1.1  lukem 	hash->bv_len = strlen( hash->bv_val );
   1231  1.1  lukem 
   1232  1.1  lukem 	save = hash->bv_val;
   1233  1.1  lukem 
   1234  1.1  lukem 	if( hash->bv_len == 0 ) {
   1235  1.1  lukem 		rc = LUTIL_PASSWD_ERR;
   1236  1.1  lukem 	} else {
   1237  1.1  lukem 		rc = pw_string( scheme, hash );
   1238  1.1  lukem 	}
   1239  1.1  lukem 	ber_memfree( save );
   1240  1.1  lukem 	return rc;
   1241  1.1  lukem }
   1242  1.1  lukem #endif
   1243  1.1  lukem 
   1244  1.1  lukem int lutil_salt_format(const char *format)
   1245  1.1  lukem {
   1246  1.1  lukem #ifdef SLAPD_CRYPT
   1247  1.2  lukem 	ber_memfree( salt_format );
   1248  1.1  lukem 
   1249  1.1  lukem 	salt_format = format != NULL ? ber_strdup( format ) : NULL;
   1250  1.1  lukem #endif
   1251  1.1  lukem 
   1252  1.1  lukem 	return 0;
   1253  1.1  lukem }
   1254  1.1  lukem 
   1255  1.1  lukem #ifdef SLAPD_CLEARTEXT
   1256  1.1  lukem static int hash_clear(
   1257  1.1  lukem 	const struct berval *scheme,
   1258  1.1  lukem 	const struct berval  *passwd,
   1259  1.1  lukem 	struct berval *hash,
   1260  1.1  lukem 	const char **text )
   1261  1.1  lukem {
   1262  1.1  lukem 	ber_dupbv( hash, (struct berval *)passwd );
   1263  1.1  lukem 	return LUTIL_PASSWD_OK;
   1264  1.1  lukem }
   1265  1.1  lukem #endif
   1266  1.1  lukem 
   1267