2004-01-07 11:10:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2003 Ben Lindstrom. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <sys/types.h>
|
2017-01-31 12:29:48 +00:00
|
|
|
#include <string.h>
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
2009-02-24 18:49:27 +00:00
|
|
|
# if defined(HAVE_CRYPT_H) && !defined(HAVE_SECUREWARE)
|
2004-01-07 11:10:17 +00:00
|
|
|
# include <crypt.h>
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef __hpux
|
|
|
|
# include <hpsecurity.h>
|
|
|
|
# include <prot.h>
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef HAVE_SECUREWARE
|
|
|
|
# include <sys/security.h>
|
|
|
|
# include <sys/audit.h>
|
|
|
|
# include <prot.h>
|
2017-01-31 12:33:47 +00:00
|
|
|
# endif
|
2004-01-07 11:10:17 +00:00
|
|
|
|
|
|
|
# if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
|
|
|
|
# include <shadow.h>
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW)
|
|
|
|
# include <sys/label.h>
|
|
|
|
# include <sys/audit.h>
|
|
|
|
# include <pwdadj.h>
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
|
|
|
|
# include "md5crypt.h"
|
2013-09-18 17:27:38 +00:00
|
|
|
# endif
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
# if defined(WITH_OPENSSL) && !defined(HAVE_CRYPT) && defined(HAVE_DES_CRYPT)
|
2013-09-18 17:27:38 +00:00
|
|
|
# include <openssl/des.h>
|
|
|
|
# define crypt DES_crypt
|
|
|
|
# endif
|
2004-01-07 11:10:17 +00:00
|
|
|
|
2017-01-31 12:29:48 +00:00
|
|
|
/*
|
|
|
|
* Pick an appropriate password encryption type and salt for the running
|
|
|
|
* system by searching through accounts until we find one that has a valid
|
|
|
|
* salt. Usually this will be root unless the root account is locked out.
|
|
|
|
* If we don't find one we return a traditional DES-based salt.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
pick_salt(void)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
char *passwd, *p;
|
|
|
|
size_t typelen;
|
|
|
|
static char salt[32];
|
|
|
|
|
|
|
|
if (salt[0] != '\0')
|
|
|
|
return salt;
|
|
|
|
strlcpy(salt, "xx", sizeof(salt));
|
|
|
|
setpwent();
|
|
|
|
while ((pw = getpwent()) != NULL) {
|
2019-02-05 15:03:53 +00:00
|
|
|
if ((passwd = shadow_pw(pw)) == NULL)
|
|
|
|
continue;
|
2017-01-31 12:29:48 +00:00
|
|
|
if (passwd[0] == '$' && (p = strrchr(passwd+1, '$')) != NULL) {
|
|
|
|
typelen = p - passwd + 1;
|
|
|
|
strlcpy(salt, passwd, MIN(typelen, sizeof(salt)));
|
|
|
|
explicit_bzero(passwd, strlen(passwd));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
endpwent();
|
|
|
|
return salt;
|
|
|
|
}
|
|
|
|
|
2004-01-07 11:10:17 +00:00
|
|
|
char *
|
|
|
|
xcrypt(const char *password, const char *salt)
|
|
|
|
{
|
|
|
|
char *crypted;
|
|
|
|
|
2017-01-31 12:29:48 +00:00
|
|
|
/*
|
|
|
|
* If we don't have a salt we are encrypting a fake password for
|
|
|
|
* for timing purposes. Pick an appropriate salt.
|
|
|
|
*/
|
|
|
|
if (salt == NULL)
|
|
|
|
salt = pick_salt();
|
|
|
|
|
2004-01-07 11:10:17 +00:00
|
|
|
# ifdef HAVE_MD5_PASSWORDS
|
2017-01-31 12:33:47 +00:00
|
|
|
if (is_md5_salt(salt))
|
|
|
|
crypted = md5_crypt(password, salt);
|
|
|
|
else
|
|
|
|
crypted = crypt(password, salt);
|
2004-01-07 11:10:17 +00:00
|
|
|
# elif defined(__hpux) && !defined(HAVE_SECUREWARE)
|
|
|
|
if (iscomsec())
|
2017-01-31 12:33:47 +00:00
|
|
|
crypted = bigcrypt(password, salt);
|
|
|
|
else
|
|
|
|
crypted = crypt(password, salt);
|
2004-01-07 11:10:17 +00:00
|
|
|
# elif defined(HAVE_SECUREWARE)
|
2017-01-31 12:33:47 +00:00
|
|
|
crypted = bigcrypt(password, salt);
|
2004-01-07 11:10:17 +00:00
|
|
|
# else
|
2017-01-31 12:33:47 +00:00
|
|
|
crypted = crypt(password, salt);
|
|
|
|
# endif
|
2004-01-07 11:10:17 +00:00
|
|
|
|
|
|
|
return crypted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle shadowed password systems in a cleaner way for portable
|
|
|
|
* version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
shadow_pw(struct passwd *pw)
|
|
|
|
{
|
|
|
|
char *pw_password = pw->pw_passwd;
|
|
|
|
|
|
|
|
# if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
|
|
|
|
struct spwd *spw = getspnam(pw->pw_name);
|
|
|
|
|
|
|
|
if (spw != NULL)
|
|
|
|
pw_password = spw->sp_pwdp;
|
|
|
|
# endif
|
2005-09-03 06:59:33 +00:00
|
|
|
|
2008-07-23 09:23:42 +00:00
|
|
|
#ifdef USE_LIBIAF
|
2005-09-03 06:59:33 +00:00
|
|
|
return(get_iaf_password(pw));
|
|
|
|
#endif
|
|
|
|
|
2004-01-07 11:10:17 +00:00
|
|
|
# if defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW)
|
|
|
|
struct passwd_adjunct *spw;
|
|
|
|
if (issecure() && (spw = getpwanam(pw->pw_name)) != NULL)
|
|
|
|
pw_password = spw->pwa_passwd;
|
|
|
|
# elif defined(HAVE_SECUREWARE)
|
|
|
|
struct pr_passwd *spw = getprpwnam(pw->pw_name);
|
|
|
|
|
|
|
|
if (spw != NULL)
|
|
|
|
pw_password = spw->ufld.fd_encrypt;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
return pw_password;
|
|
|
|
}
|