auth-passwd.c revision 240075
1210008Srdivacky/* $OpenBSD: auth-passwd.c,v 1.43 2007/09/21 08:15:29 djm Exp $ */ 2210008Srdivacky/* 3210008Srdivacky * Author: Tatu Ylonen <ylo@cs.hut.fi> 4210008Srdivacky * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5210008Srdivacky * All rights reserved 6210008Srdivacky * Password authentication. This file contains the functions to check whether 7210008Srdivacky * the password is valid for the user. 8210008Srdivacky * 9210008Srdivacky * As far as I am concerned, the code I have written for this software 10210008Srdivacky * can be used freely for any purpose. Any derived versions of this 11210008Srdivacky * software must be clearly marked as such, and if the derived work is 12249423Sdim * incompatible with the protocol description in the RFC file, it must be 13210008Srdivacky * called by a name other than "ssh" or "Secure Shell". 14210008Srdivacky * 15210008Srdivacky * Copyright (c) 1999 Dug Song. All rights reserved. 16226633Sdim * Copyright (c) 2000 Markus Friedl. All rights reserved. 17210008Srdivacky * 18210008Srdivacky * Redistribution and use in source and binary forms, with or without 19210008Srdivacky * modification, are permitted provided that the following conditions 20210008Srdivacky * are met: 21249423Sdim * 1. Redistributions of source code must retain the above copyright 22249423Sdim * notice, this list of conditions and the following disclaimer. 23224145Sdim * 2. Redistributions in binary form must reproduce the above copyright 24249423Sdim * notice, this list of conditions and the following disclaimer in the 25210008Srdivacky * documentation and/or other materials provided with the distribution. 26210008Srdivacky * 27210008Srdivacky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 28226633Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 29210008Srdivacky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30210008Srdivacky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 31226633Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 32210008Srdivacky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33210008Srdivacky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34226633Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35226633Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 36249423Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37249423Sdim */ 38226633Sdim 39210008Srdivacky#include "includes.h" 40210008Srdivacky 41210008Srdivacky#include <sys/types.h> 42210008Srdivacky 43210008Srdivacky#include <pwd.h> 44210008Srdivacky#include <stdio.h> 45226633Sdim#include <string.h> 46210008Srdivacky#include <stdarg.h> 47234353Sdim 48224145Sdim#include "packet.h" 49210008Srdivacky#include "buffer.h" 50210008Srdivacky#include "log.h" 51210008Srdivacky#include "servconf.h" 52210008Srdivacky#include "key.h" 53218893Sdim#include "hostfile.h" 54210008Srdivacky#include "auth.h" 55210008Srdivacky#include "auth-options.h" 56210008Srdivacky 57210008Srdivackyextern Buffer loginmsg; 58243830Sdimextern ServerOptions options; 59210008Srdivacky 60218893Sdim#ifdef HAVE_LOGIN_CAP 61243830Sdimextern login_cap_t *lc; 62249423Sdim#endif 63249423Sdim 64210008Srdivacky 65210008Srdivacky#define DAY (24L * 60 * 60) /* 1 day in seconds */ 66210008Srdivacky#define TWO_WEEKS (2L * 7 * DAY) /* 2 weeks in seconds */ 67210008Srdivacky 68243830Sdimvoid 69210008Srdivackydisable_forwarding(void) 70210008Srdivacky{ 71243830Sdim no_port_forwarding_flag = 1; 72249423Sdim no_agent_forwarding_flag = 1; 73249423Sdim no_x11_forwarding_flag = 1; 74210008Srdivacky} 75210008Srdivacky 76210008Srdivacky/* 77210008Srdivacky * Tries to authenticate the user using password. Returns true if 78243830Sdim * authentication succeeds. 79210008Srdivacky */ 80210008Srdivackyint 81243830Sdimauth_password(Authctxt *authctxt, const char *password) 82249423Sdim{ 83249423Sdim struct passwd * pw = authctxt->pw; 84210008Srdivacky int result, ok = authctxt->valid; 85210008Srdivacky#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE) 86210008Srdivacky static int expire_checked = 0; 87210008Srdivacky#endif 88210008Srdivacky 89243830Sdim#ifndef HAVE_CYGWIN 90243830Sdim if (pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES) 91243830Sdim ok = 0; 92243830Sdim#endif 93243830Sdim if (*password == '\0' && options.permit_empty_passwd == 0) 94243830Sdim return 0; 95243830Sdim 96243830Sdim#ifdef KRB5 97243830Sdim if (options.kerberos_authentication == 1) { 98243830Sdim int ret = auth_krb5_password(authctxt, password); 99243830Sdim if (ret == 1 || ret == 0) 100243830Sdim return ret && ok; 101210008Srdivacky /* Fall back to ordinary passwd authentication. */ 102210008Srdivacky } 103210008Srdivacky#endif 104243830Sdim#ifdef HAVE_CYGWIN 105243830Sdim { 106210008Srdivacky HANDLE hToken = cygwin_logon_user(pw, password); 107210008Srdivacky 108226633Sdim if (hToken == INVALID_HANDLE_VALUE) 109234353Sdim return 0; 110234353Sdim cygwin_set_impersonation_token(hToken); 111224145Sdim return ok; 112210008Srdivacky } 113224145Sdim#endif 114210008Srdivacky#ifdef USE_PAM 115210008Srdivacky if (options.use_pam) 116210008Srdivacky return (sshpam_auth_passwd(authctxt, password) && ok); 117210008Srdivacky#endif 118210008Srdivacky#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE) 119210008Srdivacky if (!expire_checked) { 120210008Srdivacky expire_checked = 1; 121210008Srdivacky if (auth_shadow_pwexpired(authctxt)) 122210008Srdivacky authctxt->force_pwchange = 1; 123210008Srdivacky } 124210008Srdivacky#endif 125210008Srdivacky result = sys_auth_passwd(authctxt, password); 126249423Sdim if (authctxt->force_pwchange) 127249423Sdim disable_forwarding(); 128249423Sdim return (result && ok); 129249423Sdim} 130249423Sdim 131249423Sdim#ifdef BSD_AUTH 132249423Sdimstatic void 133249423Sdimwarn_expiry(Authctxt *authctxt, auth_session_t *as) 134249423Sdim{ 135249423Sdim char buf[256]; 136249423Sdim quad_t pwtimeleft, actimeleft, daysleft, pwwarntime, acwarntime; 137249423Sdim 138249423Sdim pwwarntime = acwarntime = TWO_WEEKS; 139249423Sdim 140210008Srdivacky pwtimeleft = auth_check_change(as); 141210008Srdivacky actimeleft = auth_check_expire(as); 142234353Sdim#ifdef HAVE_LOGIN_CAP 143234353Sdim if (authctxt->valid) { 144234353Sdim pwwarntime = login_getcaptime(lc, "password-warn", TWO_WEEKS, 145234353Sdim TWO_WEEKS); 146234353Sdim acwarntime = login_getcaptime(lc, "expire-warn", TWO_WEEKS, 147224145Sdim TWO_WEEKS); 148224145Sdim } 149224145Sdim#endif 150224145Sdim if (pwtimeleft != 0 && pwtimeleft < pwwarntime) { 151224145Sdim daysleft = pwtimeleft / DAY + 1; 152224145Sdim snprintf(buf, sizeof(buf), 153224145Sdim "Your password will expire in %lld day%s.\n", 154224145Sdim daysleft, daysleft == 1 ? "" : "s"); 155224145Sdim buffer_append(&loginmsg, buf, strlen(buf)); 156224145Sdim } 157239462Sdim if (actimeleft != 0 && actimeleft < acwarntime) { 158239462Sdim daysleft = actimeleft / DAY + 1; 159249423Sdim snprintf(buf, sizeof(buf), 160239462Sdim "Your account will expire in %lld day%s.\n", 161239462Sdim daysleft, daysleft == 1 ? "" : "s"); 162249423Sdim buffer_append(&loginmsg, buf, strlen(buf)); 163249423Sdim } 164249423Sdim} 165249423Sdim 166249423Sdimint 167249423Sdimsys_auth_passwd(Authctxt *authctxt, const char *password) 168249423Sdim{ 169249423Sdim struct passwd *pw = authctxt->pw; 170249423Sdim auth_session_t *as; 171249423Sdim static int expire_checked = 0; 172249423Sdim 173249423Sdim as = auth_usercheck(pw->pw_name, authctxt->style, "auth-ssh", 174249423Sdim (char *)password); 175249423Sdim if (as == NULL) 176249423Sdim return (0); 177249423Sdim if (auth_getstate(as) & AUTH_PWEXPIRED) { 178234353Sdim auth_close(as); 179234353Sdim disable_forwarding(); 180249423Sdim authctxt->force_pwchange = 1; 181249423Sdim return (1); 182249423Sdim } else { 183249423Sdim if (!expire_checked) { 184249423Sdim expire_checked = 1; 185249423Sdim warn_expiry(authctxt, as); 186249423Sdim } 187249423Sdim return (auth_close(as)); 188249423Sdim } 189249423Sdim} 190249423Sdim#elif !defined(CUSTOM_SYS_AUTH_PASSWD) 191249423Sdimint 192249423Sdimsys_auth_passwd(Authctxt *authctxt, const char *password) 193249423Sdim{ 194249423Sdim struct passwd *pw = authctxt->pw; 195249423Sdim char *encrypted_password; 196249423Sdim 197249423Sdim /* Just use the supplied fake password if authctxt is invalid */ 198249423Sdim char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd; 199249423Sdim 200249423Sdim /* Check for users with no password. */ 201234353Sdim if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0) 202234353Sdim return (1); 203249423Sdim 204249423Sdim /* Encrypt the candidate password using the proper salt. */ 205249423Sdim encrypted_password = xcrypt(password, 206249423Sdim (pw_password[0] && pw_password[1]) ? pw_password : "xx"); 207234353Sdim 208234353Sdim /* 209243830Sdim * Authentication is accepted if the encrypted passwords 210210008Srdivacky * are identical. 211243830Sdim */ 212210008Srdivacky return encrypted_password != NULL && 213210008Srdivacky strcmp(encrypted_password, pw_password) == 0; 214210008Srdivacky} 215210008Srdivacky#endif 216210008Srdivacky