2003-07-24 06:52:13 +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-07-10 11:33:04 +00:00
|
|
|
#include <sys/types.h>
|
2016-07-15 03:32:45 +00:00
|
|
|
#include <string.h>
|
2006-08-30 07:52:03 +00:00
|
|
|
#include <unistd.h>
|
2006-07-10 11:33:04 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
|
2009-01-07 18:04:12 +00:00
|
|
|
# if defined(HAVE_CRYPT_H) && !defined(HAVE_SECUREWARE)
|
2003-07-24 06:52:13 +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>
|
2016-08-01 23:44:25 +00:00
|
|
|
# endif
|
2003-07-24 06:52:13 +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-06-01 20:28:03 +00:00
|
|
|
# endif
|
|
|
|
|
2015-01-14 15:21:31 +00:00
|
|
|
# if defined(WITH_OPENSSL) && !defined(HAVE_CRYPT) && defined(HAVE_DES_CRYPT)
|
2013-06-01 20:28:03 +00:00
|
|
|
# include <openssl/des.h>
|
|
|
|
# define crypt DES_crypt
|
|
|
|
# endif
|
2003-07-24 06:52:13 +00:00
|
|
|
|
2016-07-15 03:32:45 +00:00
|
|
|
/*
|
|
|
|
* Pick an appropriate password encryption type and salt for the running
|
2016-07-21 04:17:31 +00:00
|
|
|
* 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.
|
2016-07-15 03:32:45 +00:00
|
|
|
*/
|
|
|
|
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));
|
2016-07-21 04:17:31 +00:00
|
|
|
setpwent();
|
|
|
|
while ((pw = getpwent()) != NULL) {
|
2018-10-10 03:57:00 +00:00
|
|
|
if ((passwd = shadow_pw(pw)) == NULL)
|
|
|
|
continue;
|
2016-07-21 04:17:31 +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();
|
2016-07-15 03:32:45 +00:00
|
|
|
return salt;
|
|
|
|
}
|
|
|
|
|
2003-07-24 06:52:13 +00:00
|
|
|
char *
|
|
|
|
xcrypt(const char *password, const char *salt)
|
|
|
|
{
|
|
|
|
char *crypted;
|
|
|
|
|
2016-07-15 03:32:45 +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();
|
|
|
|
|
2003-07-24 06:52:13 +00:00
|
|
|
# ifdef HAVE_MD5_PASSWORDS
|
2016-08-02 00:01:52 +00:00
|
|
|
if (is_md5_salt(salt))
|
|
|
|
crypted = md5_crypt(password, salt);
|
|
|
|
else
|
|
|
|
crypted = crypt(password, salt);
|
2003-09-06 23:43:42 +00:00
|
|
|
# elif defined(__hpux) && !defined(HAVE_SECUREWARE)
|
2003-07-24 06:52:13 +00:00
|
|
|
if (iscomsec())
|
2016-08-02 00:01:52 +00:00
|
|
|
crypted = bigcrypt(password, salt);
|
|
|
|
else
|
|
|
|
crypted = crypt(password, salt);
|
2003-09-06 23:43:42 +00:00
|
|
|
# elif defined(HAVE_SECUREWARE)
|
2016-08-02 00:01:52 +00:00
|
|
|
crypted = bigcrypt(password, salt);
|
2003-07-24 06:52:13 +00:00
|
|
|
# else
|
2016-08-02 00:01:52 +00:00
|
|
|
crypted = crypt(password, salt);
|
2016-08-01 23:44:25 +00:00
|
|
|
# endif
|
2003-07-24 06:52:13 +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;
|
|
|
|
|
2003-07-26 09:38:30 +00:00
|
|
|
# if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
|
2003-07-24 06:52:13 +00:00
|
|
|
struct spwd *spw = getspnam(pw->pw_name);
|
|
|
|
|
|
|
|
if (spw != NULL)
|
|
|
|
pw_password = spw->sp_pwdp;
|
|
|
|
# endif
|
2005-08-31 16:59:49 +00:00
|
|
|
|
2007-03-26 16:35:28 +00:00
|
|
|
#ifdef USE_LIBIAF
|
2005-08-31 16:59:49 +00:00
|
|
|
return(get_iaf_password(pw));
|
|
|
|
#endif
|
|
|
|
|
2003-07-24 06:52:13 +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;
|
2003-09-06 23:43:42 +00:00
|
|
|
# elif defined(HAVE_SECUREWARE)
|
2003-07-24 06:52:13 +00:00
|
|
|
struct pr_passwd *spw = getprpwnam(pw->pw_name);
|
|
|
|
|
|
|
|
if (spw != NULL)
|
|
|
|
pw_password = spw->ufld.fd_encrypt;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
return pw_password;
|
|
|
|
}
|