1*50a69bb5SSascha Wildner /* $OpenBSD: auth-passwd.c,v 1.48 2020/10/18 11:32:01 djm Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos * Author: Tatu Ylonen <ylo@cs.hut.fi>
418de8d7fSPeter Avalos * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
518de8d7fSPeter Avalos * All rights reserved
618de8d7fSPeter Avalos * Password authentication. This file contains the functions to check whether
718de8d7fSPeter Avalos * the password is valid for the user.
818de8d7fSPeter Avalos *
918de8d7fSPeter Avalos * As far as I am concerned, the code I have written for this software
1018de8d7fSPeter Avalos * can be used freely for any purpose. Any derived versions of this
1118de8d7fSPeter Avalos * software must be clearly marked as such, and if the derived work is
1218de8d7fSPeter Avalos * incompatible with the protocol description in the RFC file, it must be
1318de8d7fSPeter Avalos * called by a name other than "ssh" or "Secure Shell".
1418de8d7fSPeter Avalos *
1518de8d7fSPeter Avalos * Copyright (c) 1999 Dug Song. All rights reserved.
1618de8d7fSPeter Avalos * Copyright (c) 2000 Markus Friedl. All rights reserved.
1718de8d7fSPeter Avalos *
1818de8d7fSPeter Avalos * Redistribution and use in source and binary forms, with or without
1918de8d7fSPeter Avalos * modification, are permitted provided that the following conditions
2018de8d7fSPeter Avalos * are met:
2118de8d7fSPeter Avalos * 1. Redistributions of source code must retain the above copyright
2218de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer.
2318de8d7fSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
2418de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer in the
2518de8d7fSPeter Avalos * documentation and/or other materials provided with the distribution.
2618de8d7fSPeter Avalos *
2718de8d7fSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2818de8d7fSPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2918de8d7fSPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
3018de8d7fSPeter Avalos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3118de8d7fSPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3218de8d7fSPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3318de8d7fSPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3418de8d7fSPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3518de8d7fSPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3618de8d7fSPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3718de8d7fSPeter Avalos */
3818de8d7fSPeter Avalos
3918de8d7fSPeter Avalos #include "includes.h"
4018de8d7fSPeter Avalos
4118de8d7fSPeter Avalos #include <sys/types.h>
4218de8d7fSPeter Avalos
4318de8d7fSPeter Avalos #include <pwd.h>
4418de8d7fSPeter Avalos #include <stdio.h>
4518de8d7fSPeter Avalos #include <string.h>
4618de8d7fSPeter Avalos #include <stdarg.h>
4718de8d7fSPeter Avalos
4818de8d7fSPeter Avalos #include "packet.h"
49664f4763Szrj #include "sshbuf.h"
50664f4763Szrj #include "ssherr.h"
5118de8d7fSPeter Avalos #include "log.h"
5236e94dc5SPeter Avalos #include "misc.h"
5318de8d7fSPeter Avalos #include "servconf.h"
54664f4763Szrj #include "sshkey.h"
5518de8d7fSPeter Avalos #include "hostfile.h"
5618de8d7fSPeter Avalos #include "auth.h"
5718de8d7fSPeter Avalos #include "auth-options.h"
5818de8d7fSPeter Avalos
59664f4763Szrj extern struct sshbuf *loginmsg;
6018de8d7fSPeter Avalos extern ServerOptions options;
6118de8d7fSPeter Avalos
6218de8d7fSPeter Avalos #ifdef HAVE_LOGIN_CAP
6318de8d7fSPeter Avalos extern login_cap_t *lc;
6418de8d7fSPeter Avalos #endif
6518de8d7fSPeter Avalos
6618de8d7fSPeter Avalos
6718de8d7fSPeter Avalos #define DAY (24L * 60 * 60) /* 1 day in seconds */
6818de8d7fSPeter Avalos #define TWO_WEEKS (2L * 7 * DAY) /* 2 weeks in seconds */
6918de8d7fSPeter Avalos
70e9778795SPeter Avalos #define MAX_PASSWORD_LEN 1024
71e9778795SPeter Avalos
7218de8d7fSPeter Avalos /*
7318de8d7fSPeter Avalos * Tries to authenticate the user using password. Returns true if
7418de8d7fSPeter Avalos * authentication succeeds.
7518de8d7fSPeter Avalos */
7618de8d7fSPeter Avalos int
auth_password(struct ssh * ssh,const char * password)77664f4763Szrj auth_password(struct ssh *ssh, const char *password)
7818de8d7fSPeter Avalos {
79664f4763Szrj Authctxt *authctxt = ssh->authctxt;
8018de8d7fSPeter Avalos struct passwd *pw = authctxt->pw;
8118de8d7fSPeter Avalos int result, ok = authctxt->valid;
8218de8d7fSPeter Avalos #if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
8318de8d7fSPeter Avalos static int expire_checked = 0;
8418de8d7fSPeter Avalos #endif
8518de8d7fSPeter Avalos
86e9778795SPeter Avalos if (strlen(password) > MAX_PASSWORD_LEN)
87e9778795SPeter Avalos return 0;
88e9778795SPeter Avalos
8918de8d7fSPeter Avalos #ifndef HAVE_CYGWIN
9018de8d7fSPeter Avalos if (pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES)
9118de8d7fSPeter Avalos ok = 0;
9218de8d7fSPeter Avalos #endif
9318de8d7fSPeter Avalos if (*password == '\0' && options.permit_empty_passwd == 0)
9418de8d7fSPeter Avalos return 0;
9518de8d7fSPeter Avalos
9618de8d7fSPeter Avalos #ifdef KRB5
9718de8d7fSPeter Avalos if (options.kerberos_authentication == 1) {
9818de8d7fSPeter Avalos int ret = auth_krb5_password(authctxt, password);
9918de8d7fSPeter Avalos if (ret == 1 || ret == 0)
10018de8d7fSPeter Avalos return ret && ok;
10118de8d7fSPeter Avalos /* Fall back to ordinary passwd authentication. */
10218de8d7fSPeter Avalos }
10318de8d7fSPeter Avalos #endif
10418de8d7fSPeter Avalos #ifdef HAVE_CYGWIN
10540c002afSPeter Avalos {
10618de8d7fSPeter Avalos HANDLE hToken = cygwin_logon_user(pw, password);
10718de8d7fSPeter Avalos
10818de8d7fSPeter Avalos if (hToken == INVALID_HANDLE_VALUE)
10918de8d7fSPeter Avalos return 0;
11018de8d7fSPeter Avalos cygwin_set_impersonation_token(hToken);
11118de8d7fSPeter Avalos return ok;
11218de8d7fSPeter Avalos }
11318de8d7fSPeter Avalos #endif
11418de8d7fSPeter Avalos #ifdef USE_PAM
11518de8d7fSPeter Avalos if (options.use_pam)
11618de8d7fSPeter Avalos return (sshpam_auth_passwd(authctxt, password) && ok);
11718de8d7fSPeter Avalos #endif
11818de8d7fSPeter Avalos #if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
11918de8d7fSPeter Avalos if (!expire_checked) {
12018de8d7fSPeter Avalos expire_checked = 1;
12118de8d7fSPeter Avalos if (auth_shadow_pwexpired(authctxt))
12218de8d7fSPeter Avalos authctxt->force_pwchange = 1;
12318de8d7fSPeter Avalos }
12418de8d7fSPeter Avalos #endif
125664f4763Szrj result = sys_auth_passwd(ssh, password);
12618de8d7fSPeter Avalos if (authctxt->force_pwchange)
127664f4763Szrj auth_restrict_session(ssh);
12818de8d7fSPeter Avalos return (result && ok);
12918de8d7fSPeter Avalos }
13018de8d7fSPeter Avalos
13118de8d7fSPeter Avalos #ifdef BSD_AUTH
13218de8d7fSPeter Avalos static void
warn_expiry(Authctxt * authctxt,auth_session_t * as)13318de8d7fSPeter Avalos warn_expiry(Authctxt *authctxt, auth_session_t *as)
13418de8d7fSPeter Avalos {
135664f4763Szrj int r;
13618de8d7fSPeter Avalos quad_t pwtimeleft, actimeleft, daysleft, pwwarntime, acwarntime;
13718de8d7fSPeter Avalos
13818de8d7fSPeter Avalos pwwarntime = acwarntime = TWO_WEEKS;
13918de8d7fSPeter Avalos
14018de8d7fSPeter Avalos pwtimeleft = auth_check_change(as);
14118de8d7fSPeter Avalos actimeleft = auth_check_expire(as);
14218de8d7fSPeter Avalos #ifdef HAVE_LOGIN_CAP
14318de8d7fSPeter Avalos if (authctxt->valid) {
14418de8d7fSPeter Avalos pwwarntime = login_getcaptime(lc, "password-warn", TWO_WEEKS,
14518de8d7fSPeter Avalos TWO_WEEKS);
14618de8d7fSPeter Avalos acwarntime = login_getcaptime(lc, "expire-warn", TWO_WEEKS,
14718de8d7fSPeter Avalos TWO_WEEKS);
14818de8d7fSPeter Avalos }
14918de8d7fSPeter Avalos #endif
15018de8d7fSPeter Avalos if (pwtimeleft != 0 && pwtimeleft < pwwarntime) {
15118de8d7fSPeter Avalos daysleft = pwtimeleft / DAY + 1;
152664f4763Szrj if ((r = sshbuf_putf(loginmsg,
15318de8d7fSPeter Avalos "Your password will expire in %lld day%s.\n",
154664f4763Szrj daysleft, daysleft == 1 ? "" : "s")) != 0)
155*50a69bb5SSascha Wildner fatal_fr(r, "buffer error");
15618de8d7fSPeter Avalos }
15718de8d7fSPeter Avalos if (actimeleft != 0 && actimeleft < acwarntime) {
15818de8d7fSPeter Avalos daysleft = actimeleft / DAY + 1;
159664f4763Szrj if ((r = sshbuf_putf(loginmsg,
16018de8d7fSPeter Avalos "Your account will expire in %lld day%s.\n",
161664f4763Szrj daysleft, daysleft == 1 ? "" : "s")) != 0)
162*50a69bb5SSascha Wildner fatal_fr(r, "buffer error");
16318de8d7fSPeter Avalos }
16418de8d7fSPeter Avalos }
16518de8d7fSPeter Avalos
16618de8d7fSPeter Avalos int
sys_auth_passwd(struct ssh * ssh,const char * password)167664f4763Szrj sys_auth_passwd(struct ssh *ssh, const char *password)
16818de8d7fSPeter Avalos {
169664f4763Szrj Authctxt *authctxt = ssh->authctxt;
17018de8d7fSPeter Avalos auth_session_t *as;
17118de8d7fSPeter Avalos static int expire_checked = 0;
17218de8d7fSPeter Avalos
173664f4763Szrj as = auth_usercheck(authctxt->pw->pw_name, authctxt->style, "auth-ssh",
17418de8d7fSPeter Avalos (char *)password);
17518de8d7fSPeter Avalos if (as == NULL)
17618de8d7fSPeter Avalos return (0);
17718de8d7fSPeter Avalos if (auth_getstate(as) & AUTH_PWEXPIRED) {
17818de8d7fSPeter Avalos auth_close(as);
179664f4763Szrj auth_restrict_session(ssh);
18018de8d7fSPeter Avalos authctxt->force_pwchange = 1;
18118de8d7fSPeter Avalos return (1);
18218de8d7fSPeter Avalos } else {
18318de8d7fSPeter Avalos if (!expire_checked) {
18418de8d7fSPeter Avalos expire_checked = 1;
18518de8d7fSPeter Avalos warn_expiry(authctxt, as);
18618de8d7fSPeter Avalos }
18718de8d7fSPeter Avalos return (auth_close(as));
18818de8d7fSPeter Avalos }
18918de8d7fSPeter Avalos }
19018de8d7fSPeter Avalos #elif !defined(CUSTOM_SYS_AUTH_PASSWD)
19118de8d7fSPeter Avalos int
sys_auth_passwd(struct ssh * ssh,const char * password)192664f4763Szrj sys_auth_passwd(struct ssh *ssh, const char *password)
19318de8d7fSPeter Avalos {
194664f4763Szrj Authctxt *authctxt = ssh->authctxt;
19518de8d7fSPeter Avalos struct passwd *pw = authctxt->pw;
196e9778795SPeter Avalos char *encrypted_password, *salt = NULL;
19718de8d7fSPeter Avalos
19818de8d7fSPeter Avalos /* Just use the supplied fake password if authctxt is invalid */
19918de8d7fSPeter Avalos char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
20018de8d7fSPeter Avalos
201664f4763Szrj if (pw_password == NULL)
202664f4763Szrj return 0;
203664f4763Szrj
20418de8d7fSPeter Avalos /* Check for users with no password. */
20518de8d7fSPeter Avalos if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
20618de8d7fSPeter Avalos return (1);
20718de8d7fSPeter Avalos
208e9778795SPeter Avalos /*
209e9778795SPeter Avalos * Encrypt the candidate password using the proper salt, or pass a
210e9778795SPeter Avalos * NULL and let xcrypt pick one.
211e9778795SPeter Avalos */
212e9778795SPeter Avalos if (authctxt->valid && pw_password[0] && pw_password[1])
213e9778795SPeter Avalos salt = pw_password;
214e9778795SPeter Avalos encrypted_password = xcrypt(password, salt);
21518de8d7fSPeter Avalos
21618de8d7fSPeter Avalos /*
21718de8d7fSPeter Avalos * Authentication is accepted if the encrypted passwords
21818de8d7fSPeter Avalos * are identical.
21918de8d7fSPeter Avalos */
22099e85e0dSPeter Avalos return encrypted_password != NULL &&
22199e85e0dSPeter Avalos strcmp(encrypted_password, pw_password) == 0;
22218de8d7fSPeter Avalos }
22318de8d7fSPeter Avalos #endif
224