xcrypt.c revision 294332
191592Smarkm/* 212099Sjoerg * Copyright (c) 2003 Ben Lindstrom. All rights reserved. 312099Sjoerg * 491592Smarkm * Redistribution and use in source and binary forms, with or without 512099Sjoerg * modification, are permitted provided that the following conditions 612099Sjoerg * are met: 712099Sjoerg * 1. Redistributions of source code must retain the above copyright 812099Sjoerg * notice, this list of conditions and the following disclaimer. 912099Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1012099Sjoerg * notice, this list of conditions and the following disclaimer in the 1112099Sjoerg * documentation and/or other materials provided with the distribution. 1212099Sjoerg * 1312099Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1412099Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1512099Sjoerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1612099Sjoerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1712099Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1812099Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1912099Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2012099Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2112099Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2212099Sjoerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2312099Sjoerg */ 2412099Sjoerg 2512099Sjoerg#include "includes.h" 2612099Sjoerg 2712099Sjoerg#include <sys/types.h> 2812099Sjoerg#include <unistd.h> 2912099Sjoerg#include <pwd.h> 3012099Sjoerg 3112099Sjoerg# if defined(HAVE_CRYPT_H) && !defined(HAVE_SECUREWARE) 3212099Sjoerg# include <crypt.h> 3312099Sjoerg# endif 3412099Sjoerg 3591592Smarkm# ifdef __hpux 3691592Smarkm# include <hpsecurity.h> 3791592Smarkm# include <prot.h> 3812099Sjoerg# endif 3991592Smarkm 4012099Sjoerg# ifdef HAVE_SECUREWARE 4112099Sjoerg# include <sys/security.h> 4212099Sjoerg# include <sys/audit.h> 4312099Sjoerg# include <prot.h> 4412099Sjoerg# endif 4512099Sjoerg 4612099Sjoerg# if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) 4712099Sjoerg# include <shadow.h> 4812099Sjoerg# endif 4912099Sjoerg 5012099Sjoerg# if defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW) 5112099Sjoerg# include <sys/label.h> 5212099Sjoerg# include <sys/audit.h> 5312099Sjoerg# include <pwdadj.h> 5412099Sjoerg# endif 5512099Sjoerg 5612099Sjoerg# if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) 5712099Sjoerg# include "md5crypt.h" 5812099Sjoerg# endif 5912099Sjoerg 6012099Sjoerg# if defined(WITH_OPENSSL) && !defined(HAVE_CRYPT) && defined(HAVE_DES_CRYPT) 6112099Sjoerg# include <openssl/des.h> 6291592Smarkm# define crypt DES_crypt 6391592Smarkm# endif 6491592Smarkm 6591592Smarkmchar * 6691592Smarkmxcrypt(const char *password, const char *salt) 6791592Smarkm{ 6891592Smarkm char *crypted; 6991592Smarkm 7091592Smarkm# ifdef HAVE_MD5_PASSWORDS 7191592Smarkm if (is_md5_salt(salt)) 7291592Smarkm crypted = md5_crypt(password, salt); 7391592Smarkm else 7491592Smarkm crypted = crypt(password, salt); 7591592Smarkm# elif defined(__hpux) && !defined(HAVE_SECUREWARE) 7691592Smarkm if (iscomsec()) 7791592Smarkm crypted = bigcrypt(password, salt); 7891592Smarkm else 7991592Smarkm crypted = crypt(password, salt); 8091592Smarkm# elif defined(HAVE_SECUREWARE) 8112099Sjoerg crypted = bigcrypt(password, salt); 8212099Sjoerg# else 8312099Sjoerg crypted = crypt(password, salt); 8412099Sjoerg# endif 8512099Sjoerg 8691592Smarkm return crypted; 8712099Sjoerg} 8891592Smarkm 8912099Sjoerg/* 9012099Sjoerg * Handle shadowed password systems in a cleaner way for portable 9180284Sobrien * version. 9280284Sobrien */ 9312099Sjoerg 9412099Sjoergchar * 9512099Sjoergshadow_pw(struct passwd *pw) 9612099Sjoerg{ 9791592Smarkm char *pw_password = pw->pw_passwd; 9891592Smarkm 9912099Sjoerg# if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) 10080284Sobrien struct spwd *spw = getspnam(pw->pw_name); 10180284Sobrien 10212099Sjoerg if (spw != NULL) 10312099Sjoerg pw_password = spw->sp_pwdp; 10412099Sjoerg# endif 10512099Sjoerg 10612099Sjoerg#ifdef USE_LIBIAF 10712099Sjoerg return(get_iaf_password(pw)); 10812099Sjoerg#endif 10912099Sjoerg 11012099Sjoerg# if defined(HAVE_GETPWANAM) && !defined(DISABLE_SHADOW) 11112099Sjoerg struct passwd_adjunct *spw; 11212099Sjoerg if (issecure() && (spw = getpwanam(pw->pw_name)) != NULL) 11312099Sjoerg pw_password = spw->pwa_passwd; 11412099Sjoerg# elif defined(HAVE_SECUREWARE) 11512099Sjoerg struct pr_passwd *spw = getprpwnam(pw->pw_name); 11612099Sjoerg 11712099Sjoerg if (spw != NULL) 11812099Sjoerg pw_password = spw->ufld.fd_encrypt; 11912099Sjoerg# endif 12012099Sjoerg 12112099Sjoerg return pw_password; 12212099Sjoerg} 12312099Sjoerg