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