1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1999-08-18 03:00:59 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1998-11-07 06:04:14 +08:00
|
|
|
/*
|
|
|
|
* lutil_password(credentials, password)
|
|
|
|
*
|
|
|
|
* Returns true if user supplied credentials matches
|
|
|
|
* the stored password.
|
|
|
|
*
|
|
|
|
* Due to the use of the crypt(3) function
|
|
|
|
* this routine is NOT thread-safe.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
1999-06-03 08:37:44 +08:00
|
|
|
#include <ac/stdlib.h>
|
1998-12-30 13:32:17 +08:00
|
|
|
|
1998-11-07 06:04:14 +08:00
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/unistd.h>
|
1999-10-01 12:48:30 +08:00
|
|
|
#include <ac/crypt.h>
|
1998-11-07 06:04:14 +08:00
|
|
|
|
|
|
|
#include "lutil_md5.h"
|
|
|
|
#include "lutil_sha1.h"
|
|
|
|
#include "lutil.h"
|
|
|
|
|
1999-06-27 04:52:59 +08:00
|
|
|
#ifdef HAVE_SHADOW_H
|
|
|
|
# include <shadow.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
# include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
static int is_allowed_scheme(
|
|
|
|
const char* scheme,
|
|
|
|
const char** schemes )
|
1999-06-30 06:24:53 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
if(schemes == NULL) {
|
1999-06-30 06:24:53 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
for(i=0; schemes[i] != NULL; i++) {
|
|
|
|
if(strcasecmp(scheme, schemes[i]) == 0) {
|
1999-06-30 06:24:53 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
const char *lutil_passwd_schemes[] = {
|
|
|
|
#ifdef SLAPD_CRYPT
|
|
|
|
"{CRYPT}",
|
|
|
|
#endif
|
|
|
|
"{MD5}", "{SMD5}",
|
|
|
|
"{SHA}", "{SSHA}",
|
|
|
|
# if defined( HAVE_GETSPNAM ) \
|
|
|
|
|| ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
|
|
|
|
"{UNIX}",
|
|
|
|
#endif
|
|
|
|
#ifdef SLAPD_CLEARTEXT
|
|
|
|
"{CLEARTEXT}", /* psuedo scheme */
|
|
|
|
#endif
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
int lutil_passwd_scheme( char *scheme ) {
|
|
|
|
return is_allowed_scheme( scheme, lutil_passwd_schemes );
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *passwd_scheme(
|
1999-06-30 06:24:53 +08:00
|
|
|
const char* passwd,
|
1999-09-26 06:13:25 +08:00
|
|
|
const char* scheme,
|
|
|
|
const char** schemes )
|
1999-06-30 06:24:53 +08:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
if( !is_allowed_scheme( scheme, schemes ) ) {
|
1999-06-30 06:24:53 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
len = strlen(scheme);
|
1999-06-30 06:24:53 +08:00
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
if( strncasecmp( passwd, scheme, len ) == 0 ) {
|
1999-06-30 06:24:53 +08:00
|
|
|
return &passwd[len];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1998-11-07 06:04:14 +08:00
|
|
|
/*
|
1998-12-01 11:36:37 +08:00
|
|
|
* Return 0 if creds are good.
|
1998-11-07 06:04:14 +08:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
lutil_passwd(
|
|
|
|
const char *cred,
|
1999-06-30 06:24:53 +08:00
|
|
|
const char *passwd,
|
1999-09-26 06:13:25 +08:00
|
|
|
const char **schemes)
|
1998-11-07 06:04:14 +08:00
|
|
|
{
|
1999-06-30 06:24:53 +08:00
|
|
|
const char *p;
|
1998-11-07 06:04:14 +08:00
|
|
|
|
|
|
|
if (cred == NULL || passwd == NULL) {
|
1998-12-01 11:36:37 +08:00
|
|
|
return -1;
|
1998-11-07 06:04:14 +08:00
|
|
|
}
|
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
if ((p = passwd_scheme( passwd, "{MD5}", schemes )) != NULL ) {
|
1998-12-01 11:36:37 +08:00
|
|
|
lutil_MD5_CTX MD5context;
|
|
|
|
unsigned char MD5digest[16];
|
|
|
|
char base64digest[25]; /* ceiling(sizeof(input)/3) * 4 + 1 */
|
1998-11-07 06:04:14 +08:00
|
|
|
|
1998-12-01 11:36:37 +08:00
|
|
|
lutil_MD5Init(&MD5context);
|
|
|
|
lutil_MD5Update(&MD5context,
|
1998-11-23 09:46:32 +08:00
|
|
|
(const unsigned char *)cred, strlen(cred));
|
1998-12-01 11:36:37 +08:00
|
|
|
lutil_MD5Final(MD5digest, &MD5context);
|
1998-11-07 06:04:14 +08:00
|
|
|
|
1998-12-01 11:36:37 +08:00
|
|
|
if ( lutil_b64_ntop(MD5digest, sizeof(MD5digest),
|
1998-11-07 06:04:14 +08:00
|
|
|
base64digest, sizeof(base64digest)) < 0)
|
|
|
|
{
|
|
|
|
return ( 1 );
|
|
|
|
}
|
|
|
|
|
1998-12-01 11:36:37 +08:00
|
|
|
return( strcmp(p, base64digest) );
|
1998-11-07 06:04:14 +08:00
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
} else if ((p = passwd_scheme( passwd, "{SHA}", schemes )) != NULL ) {
|
1998-12-01 11:36:37 +08:00
|
|
|
lutil_SHA1_CTX SHA1context;
|
1998-11-07 06:04:14 +08:00
|
|
|
unsigned char SHA1digest[20];
|
|
|
|
char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
|
|
|
|
|
1998-12-01 11:36:37 +08:00
|
|
|
lutil_SHA1Init(&SHA1context);
|
|
|
|
lutil_SHA1Update(&SHA1context,
|
1998-11-23 09:46:32 +08:00
|
|
|
(const unsigned char *) cred, strlen(cred));
|
1998-12-01 11:36:37 +08:00
|
|
|
lutil_SHA1Final(SHA1digest, &SHA1context);
|
1998-11-07 06:04:14 +08:00
|
|
|
|
1998-12-01 11:36:37 +08:00
|
|
|
if (lutil_b64_ntop(SHA1digest, sizeof(SHA1digest),
|
1998-11-07 06:04:14 +08:00
|
|
|
base64digest, sizeof(base64digest)) < 0)
|
|
|
|
{
|
1998-12-01 11:36:37 +08:00
|
|
|
return ( 1 );
|
1998-11-07 06:04:14 +08:00
|
|
|
}
|
|
|
|
|
1998-12-01 11:36:37 +08:00
|
|
|
return( strcmp(p, base64digest) );
|
1998-12-30 05:45:08 +08:00
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
} else if ((p = passwd_scheme( passwd, "{SSHA}", schemes )) != NULL ) {
|
1998-12-30 13:32:17 +08:00
|
|
|
lutil_SHA1_CTX SHA1context;
|
|
|
|
unsigned char SHA1digest[20];
|
|
|
|
int pw_len = strlen(p);
|
|
|
|
int rc;
|
|
|
|
unsigned char *orig_pass = NULL;
|
|
|
|
|
|
|
|
/* base64 un-encode password */
|
1999-04-02 04:26:09 +08:00
|
|
|
orig_pass = (unsigned char *)malloc((size_t)(pw_len * 0.75 + 1));
|
1998-12-30 13:32:17 +08:00
|
|
|
if ((rc = lutil_b64_pton(p, orig_pass, pw_len)) < 0)
|
|
|
|
{
|
|
|
|
free(orig_pass);
|
|
|
|
return ( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hash credentials with salt */
|
|
|
|
lutil_SHA1Init(&SHA1context);
|
|
|
|
lutil_SHA1Update(&SHA1context,
|
|
|
|
(const unsigned char *) cred, strlen(cred));
|
|
|
|
lutil_SHA1Update(&SHA1context,
|
|
|
|
(const unsigned char *) orig_pass + sizeof(SHA1digest),
|
|
|
|
rc - sizeof(SHA1digest));
|
|
|
|
lutil_SHA1Final(SHA1digest, &SHA1context);
|
|
|
|
|
|
|
|
/* compare */
|
1999-05-25 06:51:13 +08:00
|
|
|
rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
|
1998-12-30 13:32:17 +08:00
|
|
|
free(orig_pass);
|
|
|
|
return(rc);
|
|
|
|
|
1999-09-26 06:13:25 +08:00
|
|
|
} else if ((p = passwd_scheme( passwd, "{SMD5}", schemes )) != NULL ) {
|
1998-12-30 13:32:17 +08:00
|
|
|
lutil_MD5_CTX MD5context;
|
|
|
|
unsigned char MD5digest[16];
|
|
|
|
int pw_len = strlen(p);
|
|
|
|
int rc;
|
|
|
|
unsigned char *orig_pass = NULL;
|
|
|
|
|
|
|
|
/* base64 un-encode password */
|
1999-04-02 04:26:09 +08:00
|
|
|
orig_pass = (unsigned char *)malloc((size_t)(pw_len * 0.75 + 1));
|
1998-12-30 13:32:17 +08:00
|
|
|
if ((rc = lutil_b64_pton(p, orig_pass, pw_len)) < 0)
|
|
|
|
{
|
|
|
|
free(orig_pass);
|
|
|
|
return ( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hash credentials with salt */
|
|
|
|
lutil_MD5Init(&MD5context);
|
|
|
|
lutil_MD5Update(&MD5context,
|
|
|
|
(const unsigned char *) cred, strlen(cred));
|
|
|
|
lutil_MD5Update(&MD5context,
|
|
|
|
(const unsigned char *) orig_pass + sizeof(MD5digest),
|
|
|
|
rc - sizeof(MD5digest));
|
|
|
|
lutil_MD5Final(MD5digest, &MD5context);
|
|
|
|
|
|
|
|
/* compare */
|
1999-05-25 06:51:13 +08:00
|
|
|
rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
|
1998-12-30 13:32:17 +08:00
|
|
|
free(orig_pass);
|
|
|
|
return ( rc );
|
|
|
|
|
1998-12-30 05:45:08 +08:00
|
|
|
#ifdef SLAPD_CRYPT
|
1999-09-26 06:13:25 +08:00
|
|
|
} else if ((p = passwd_scheme( passwd, "{CRYPT}", schemes )) != NULL ) {
|
1998-12-30 05:45:08 +08:00
|
|
|
return( strcmp(p, crypt(cred, p)) );
|
|
|
|
|
1999-06-27 04:52:59 +08:00
|
|
|
# if defined( HAVE_GETSPNAM ) \
|
|
|
|
|| ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
|
1999-09-26 06:13:25 +08:00
|
|
|
} else if ((p = passwd_scheme( passwd, "{UNIX}", schemes )) != NULL ) {
|
1999-06-27 04:52:59 +08:00
|
|
|
|
|
|
|
# ifdef HAVE_GETSPNAM
|
1999-06-30 06:24:53 +08:00
|
|
|
struct spwd *spwd = getspnam(p);
|
1999-06-27 04:52:59 +08:00
|
|
|
|
|
|
|
if(spwd == NULL) {
|
|
|
|
return 1; /* not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
return strcmp(spwd->sp_pwdp, crypt(cred, spwd->sp_pwdp));
|
|
|
|
# else
|
1999-06-30 06:24:53 +08:00
|
|
|
struct passwd *pwd = getpwnam(p);
|
1999-06-27 04:52:59 +08:00
|
|
|
|
|
|
|
if(pwd == NULL) {
|
|
|
|
return 1; /* not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
return strcmp(pwd->pw_passwd, crypt(cred, pwd->pw_passwd));
|
|
|
|
# endif
|
|
|
|
# endif
|
1998-12-30 05:45:08 +08:00
|
|
|
#endif
|
1998-11-07 06:04:14 +08:00
|
|
|
}
|
|
|
|
|
1998-12-01 11:36:37 +08:00
|
|
|
#ifdef SLAPD_CLEARTEXT
|
1999-09-26 06:13:25 +08:00
|
|
|
return is_allowed_scheme("{CLEARTEXT}", schemes ) &&
|
1999-06-30 06:24:53 +08:00
|
|
|
strcmp(passwd, cred) != 0;
|
1998-12-01 11:36:37 +08:00
|
|
|
#else
|
|
|
|
return( 1 );
|
|
|
|
#endif
|
|
|
|
|
1998-11-07 06:04:14 +08:00
|
|
|
}
|