blob: 5a3ba09b43d31907575e89877bc92462d7bb9da1 [file] [log] [blame]
Damien Miller4f9f42a2003-05-10 19:28:02 +10001/*-
2 * Copyright (c) 2002 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by ThinkSec AS and
6 * NAI Labs, the Security Research Division of Network Associates, Inc.
7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8 * DARPA CHATS research program.
Damien Millere69f18c2000-06-12 16:38:54 +10009 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
Damien Millere69f18c2000-06-12 16:38:54 +100018 *
Damien Miller4f9f42a2003-05-10 19:28:02 +100019 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
Damien Millere72b7af1999-12-30 15:08:44 +110030 */
Damien Miller26314f62004-06-01 11:28:20 +100031/*
32 * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
33 * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au>
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 */
Damien Millere72b7af1999-12-30 15:08:44 +110047
Damien Miller5ef4b0f2016-01-27 17:45:56 +110048/* Based on FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des */
49
Damien Millere72b7af1999-12-30 15:08:44 +110050#include "includes.h"
Damien Miller6645e7a2006-03-15 14:42:54 +110051
52#include <sys/types.h>
53#include <sys/stat.h>
54#include <sys/wait.h>
Darren Tucker341dae52006-07-13 08:45:14 +100055
56#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110057#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100058#include <stdarg.h>
Ruben Kerkhof335dc932020-01-20 11:09:27 +010059#include <stdlib.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100060#include <string.h>
61#include <unistd.h>
Damien Millere72b7af1999-12-30 15:08:44 +110062
63#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110064#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100065#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110066#elif defined (HAVE_PAM_PAM_APPL_H)
67#include <pam/pam_appl.h>
68#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100069
Damien Miller8bd81e12016-08-16 13:30:56 +100070#if !defined(SSHD_PAM_SERVICE)
71extern char *__progname;
72# define SSHD_PAM_SERVICE __progname
73#endif
74
Darren Tuckerf08bdb52005-05-26 19:59:48 +100075/* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
76#ifdef PAM_SUN_CODEBASE
Darren Tucker39c0cec2016-05-20 10:01:58 +100077# define sshpam_const /* Solaris, HP-UX, SunOS */
Darren Tuckerf08bdb52005-05-26 19:59:48 +100078#else
Darren Tucker39c0cec2016-05-20 10:01:58 +100079# define sshpam_const const /* LinuxPAM, OpenPAM, AIX */
Darren Tuckerf08bdb52005-05-26 19:59:48 +100080#endif
81
Damien Miller2ab323e2006-08-05 12:43:32 +100082/* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
83#ifdef PAM_SUN_CODEBASE
84# define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
85#else
86# define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
87#endif
88
Damien Miller75bb6642006-08-05 14:07:20 +100089#include "xmalloc.h"
Damien Miller120a1ec2018-07-10 19:39:52 +100090#include "sshbuf.h"
91#include "ssherr.h"
Damien Miller75bb6642006-08-05 14:07:20 +100092#include "hostfile.h"
Kevin Stevese683e762002-04-04 19:02:28 +000093#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110094#include "auth-pam.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000095#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100096#include "log.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100097#include "msg.h"
98#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100099#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +1000100#include "servconf.h"
101#include "ssh2.h"
Damien Miller1f499fd2003-08-25 13:08:49 +1000102#include "auth-options.h"
Darren Tucker945bf522020-01-23 21:06:45 +1100103#include "misc.h"
Damien Miller75bb6642006-08-05 14:07:20 +1000104#ifdef GSSAPI
105#include "ssh-gss.h"
106#endif
107#include "monitor_wrap.h"
Damien Millere72b7af1999-12-30 15:08:44 +1100108
Damien Miller4e448a32003-05-14 15:11:48 +1000109extern ServerOptions options;
Damien Miller120a1ec2018-07-10 19:39:52 +1000110extern struct sshbuf *loginmsg;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000111extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +1000112
Darren Tucker328118a2005-05-25 16:18:09 +1000113/* so we don't silently change behaviour */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000114#ifdef USE_POSIX_THREADS
Darren Tucker328118a2005-05-25 16:18:09 +1000115# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
116#endif
117
118/*
119 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
120 * and generally a bad idea. Use at own risk and do not expect support if
121 * this breaks.
122 */
123#ifdef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000124#include <pthread.h>
125/*
Damien Millera8e06ce2003-11-21 23:48:55 +1100126 * Avoid namespace clash when *not* using pthreads for systems *with*
127 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +1000128 * (e.g. Linux)
129 */
Damien Millera8e06ce2003-11-21 23:48:55 +1100130typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000131#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100132typedef pid_t sp_pthread_t;
Damien Miller48f54b92018-09-13 12:13:50 +1000133#define pthread_exit fake_pthread_exit
134#define pthread_create fake_pthread_create
135#define pthread_cancel fake_pthread_cancel
136#define pthread_join fake_pthread_join
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100137#endif
138
139struct pam_ctxt {
140 sp_pthread_t pam_thread;
141 int pam_psock;
142 int pam_csock;
143 int pam_done;
144};
145
146static void sshpam_free_ctx(void *);
147static struct pam_ctxt *cleanup_ctxt;
148
Darren Tucker328118a2005-05-25 16:18:09 +1000149#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000150/*
151 * Simulate threads with processes.
152 */
Kevin Steves63007d42002-07-21 17:57:01 +0000153
Darren Tucker749bc952004-01-14 22:14:04 +1100154static int sshpam_thread_status = -1;
Darren Tucker945bf522020-01-23 21:06:45 +1100155static sshsig_t sshpam_oldsig;
Darren Tucker749bc952004-01-14 22:14:04 +1100156
Damien Miller94cf4c82005-07-17 17:04:47 +1000157static void
Darren Tucker749bc952004-01-14 22:14:04 +1100158sshpam_sigchld_handler(int sig)
159{
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000160 ssh_signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100161 if (cleanup_ctxt == NULL)
162 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000163 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000164 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000165 /* PAM thread has not exitted, privsep slave must have */
166 kill(cleanup_ctxt->pam_thread, SIGTERM);
Damien Miller10358ab2016-07-22 14:06:36 +1000167 while (waitpid(cleanup_ctxt->pam_thread,
168 &sshpam_thread_status, 0) == -1) {
169 if (errno == EINTR)
170 continue;
171 return;
172 }
Darren Tuckerb53355e2004-05-24 11:55:36 +1000173 }
Darren Tucker749bc952004-01-14 22:14:04 +1100174 if (WIFSIGNALED(sshpam_thread_status) &&
175 WTERMSIG(sshpam_thread_status) == SIGTERM)
176 return; /* terminated by pthread_cancel */
177 if (!WIFEXITED(sshpam_thread_status))
Darren Tucker57d4ca92007-08-10 14:32:34 +1000178 sigdie("PAM: authentication thread exited unexpectedly");
Darren Tucker749bc952004-01-14 22:14:04 +1100179 if (WEXITSTATUS(sshpam_thread_status) != 0)
Darren Tucker57d4ca92007-08-10 14:32:34 +1000180 sigdie("PAM: authentication thread exited uncleanly");
Darren Tucker749bc952004-01-14 22:14:04 +1100181}
182
Damien Millerb8fe89c2006-07-24 14:51:00 +1000183/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000184static void
Damien Millerb8fe89c2006-07-24 14:51:00 +1000185pthread_exit(void *value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000186{
187 _exit(0);
188}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000189
Damien Millerb8fe89c2006-07-24 14:51:00 +1000190/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000191static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000192pthread_create(sp_pthread_t *thread, const void *attr,
Damien Miller4f9f42a2003-05-10 19:28:02 +1000193 void *(*thread_start)(void *), void *arg)
194{
195 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000196 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100197
Darren Tuckerb9b60212004-03-04 20:03:54 +1100198 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000199 switch ((pid = fork())) {
200 case -1:
201 error("fork(): %s", strerror(errno));
Darren Tuckerd220b672019-06-07 14:26:54 +1000202 return errno;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000203 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000204 close(ctx->pam_psock);
205 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000206 thread_start(arg);
207 _exit(1);
208 default:
209 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000210 close(ctx->pam_csock);
211 ctx->pam_csock = -1;
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000212 sshpam_oldsig = ssh_signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213 return (0);
214 }
215}
Damien Millere72b7af1999-12-30 15:08:44 +1100216
Damien Miller4f9f42a2003-05-10 19:28:02 +1000217static int
218pthread_cancel(sp_pthread_t thread)
219{
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000220 ssh_signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000221 return (kill(thread, SIGTERM));
222}
223
Damien Millerb8fe89c2006-07-24 14:51:00 +1000224/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000225static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000226pthread_join(sp_pthread_t thread, void **value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000227{
228 int status;
229
Darren Tucker749bc952004-01-14 22:14:04 +1100230 if (sshpam_thread_status != -1)
231 return (sshpam_thread_status);
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000232 ssh_signal(SIGCHLD, sshpam_oldsig);
Damien Miller10358ab2016-07-22 14:06:36 +1000233 while (waitpid(thread, &status, 0) == -1) {
234 if (errno == EINTR)
235 continue;
236 fatal("%s: waitpid: %s", __func__, strerror(errno));
237 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000238 return (status);
239}
240#endif
241
242
Damien Miller5c3a5582003-09-23 22:12:38 +1000243static pam_handle_t *sshpam_handle = NULL;
244static int sshpam_err = 0;
245static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000246static int sshpam_session_open = 0;
247static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100248static int sshpam_account_status = -1;
Darren Tucker01558b72016-07-18 09:33:25 +1000249static int sshpam_maxtries_reached = 0;
Damien Millerc756e9b2003-11-17 21:41:42 +1100250static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000251static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000252static const char *sshpam_password = NULL;
Damien Miller3f0786b2019-01-20 10:22:18 +1100253static char *sshpam_rhost = NULL;
254static char *sshpam_laddr = NULL;
255static char *sshpam_conninfo = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000256
Damien Millerc756e9b2003-11-17 21:41:42 +1100257/* Some PAM implementations don't implement this */
258#ifndef HAVE_PAM_GETENVLIST
259static char **
260pam_getenvlist(pam_handle_t *pamh)
261{
262 /*
Darren Tuckerfc0340f2019-06-08 00:10:59 +1000263 * XXX - If necessary, we can still support environment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100264 * for platforms without pam_getenvlist by searching for known
265 * env vars (e.g. KRB5CCNAME) from the PAM environment.
266 */
267 return NULL;
268}
269#endif
270
Darren Tucker606077e2019-05-17 13:14:12 +1000271#ifndef HAVE_PAM_PUTENV
272static int
273pam_putenv(pam_handle_t *pamh, const char *name_value)
274{
275 return PAM_SUCCESS;
276}
277#endif /* HAVE_PAM_PUTENV */
278
Darren Tucker21dd0892004-08-16 23:12:05 +1000279/*
280 * Some platforms, notably Solaris, do not enforce password complexity
281 * rules during pam_chauthtok() if the real uid of the calling process
282 * is 0, on the assumption that it's being called by "passwd" run by root.
283 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
284 * the right thing.
285 */
286#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
287static int
288sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
289{
290 int result;
291
292 if (sshpam_authctxt == NULL)
293 fatal("PAM: sshpam_authctxt not initialized");
294 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
295 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
296 result = pam_chauthtok(pamh, flags);
297 if (setreuid(0, -1) == -1)
298 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
299 return result;
300}
301# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
302#endif
303
Ruben Kerkhof6089abf2020-01-20 12:13:26 +0100304static void
Darren Tucker17db1c42004-06-19 12:54:38 +1000305sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100306{
Darren Tucker13ef4cf2018-03-03 16:21:20 +1100307 extern struct sshauthopt *auth_opts;
308 static int saved_port, saved_agent, saved_x11;
309
Darren Tuckera8df9242004-01-15 00:15:07 +1100310 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000311 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100312 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000313 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100314 if (reqd) {
Darren Tucker13ef4cf2018-03-03 16:21:20 +1100315 saved_port = auth_opts->permit_port_forwarding_flag;
316 saved_agent = auth_opts->permit_agent_forwarding_flag;
317 saved_x11 = auth_opts->permit_x11_forwarding_flag;
318 auth_opts->permit_port_forwarding_flag = 0;
319 auth_opts->permit_agent_forwarding_flag = 0;
320 auth_opts->permit_x11_forwarding_flag = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100321 } else {
Darren Tucker13ef4cf2018-03-03 16:21:20 +1100322 if (saved_port)
323 auth_opts->permit_port_forwarding_flag = saved_port;
324 if (saved_agent)
325 auth_opts->permit_agent_forwarding_flag = saved_agent;
326 if (saved_x11)
327 auth_opts->permit_x11_forwarding_flag = saved_x11;
Darren Tucker07705c72003-12-18 15:34:31 +1100328 }
329}
Darren Tucker1921ed92004-02-10 13:23:28 +1100330
Damien Millerc756e9b2003-11-17 21:41:42 +1100331/* Import regular and PAM environment from subprocess */
332static void
Damien Miller120a1ec2018-07-10 19:39:52 +1000333import_environments(struct sshbuf *b)
Damien Millerc756e9b2003-11-17 21:41:42 +1100334{
335 char *env;
Damien Miller120a1ec2018-07-10 19:39:52 +1000336 u_int n, i, num_env;
337 int r;
Damien Millerc756e9b2003-11-17 21:41:42 +1100338
Darren Tuckera8df9242004-01-15 00:15:07 +1100339 debug3("PAM: %s entering", __func__);
340
Darren Tucker328118a2005-05-25 16:18:09 +1000341#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100342 /* Import variables set by do_pam_account */
Damien Miller120a1ec2018-07-10 19:39:52 +1000343 if ((r = sshbuf_get_u32(b, &n)) != 0)
344 fatal("%s: buffer error: %s", __func__, ssh_err(r));
345 if (n > INT_MAX)
346 fatal("%s: invalid PAM account status %u", __func__, n);
347 sshpam_account_status = (int)n;
348 if ((r = sshbuf_get_u32(b, &n)) != 0)
349 fatal("%s: buffer error: %s", __func__, ssh_err(r));
350 sshpam_password_change_required(n != 0);
Darren Tucker07705c72003-12-18 15:34:31 +1100351
Damien Millerc756e9b2003-11-17 21:41:42 +1100352 /* Import environment from subprocess */
Damien Miller120a1ec2018-07-10 19:39:52 +1000353 if ((r = sshbuf_get_u32(b, &num_env)) != 0)
354 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerd8093e42006-05-04 16:24:34 +1000355 if (num_env > 1024)
356 fatal("%s: received %u environment variables, expected <= 1024",
357 __func__, num_env);
358 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
Damien Millerc756e9b2003-11-17 21:41:42 +1100359 debug3("PAM: num env strings %d", num_env);
Damien Miller120a1ec2018-07-10 19:39:52 +1000360 for(i = 0; i < num_env; i++) {
361 if ((r = sshbuf_get_cstring(b, &(sshpam_env[i]), NULL)) != 0)
362 fatal("%s: buffer error: %s", __func__, ssh_err(r));
363 }
Damien Millerc756e9b2003-11-17 21:41:42 +1100364 sshpam_env[num_env] = NULL;
365
366 /* Import PAM environment from subprocess */
Damien Miller120a1ec2018-07-10 19:39:52 +1000367 if ((r = sshbuf_get_u32(b, &num_env)) != 0)
368 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100369 debug("PAM: num PAM env strings %d", num_env);
Damien Miller120a1ec2018-07-10 19:39:52 +1000370 for (i = 0; i < num_env; i++) {
371 if ((r = sshbuf_get_cstring(b, &env, NULL)) != 0)
372 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100373 /* Errors are not fatal here */
Damien Miller120a1ec2018-07-10 19:39:52 +1000374 if ((r = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100375 error("PAM: pam_putenv: %s",
Damien Miller120a1ec2018-07-10 19:39:52 +1000376 pam_strerror(sshpam_handle, r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100377 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000378 /* XXX leak env? */
Damien Millerc756e9b2003-11-17 21:41:42 +1100379 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100380#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100381}
382
Damien Miller9d5705a2000-09-16 16:09:27 +1100383/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000384 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100385 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000386static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000387sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000388 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100389{
Damien Miller120a1ec2018-07-10 19:39:52 +1000390 struct sshbuf *buffer;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000391 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000392 struct pam_response *reply;
Damien Miller120a1ec2018-07-10 19:39:52 +1000393 int r, i;
394 u_char status;
Kevin Steves38b050a2002-07-23 00:44:07 +0000395
Darren Tuckerba53b832004-02-17 20:46:59 +1100396 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000397 *resp = NULL;
398
Darren Tucker59e06022004-06-30 20:34:31 +1000399 if (data == NULL) {
400 error("PAM: conversation function passed a null context");
401 return (PAM_CONV_ERR);
402 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000403 ctxt = data;
404 if (n <= 0 || n > PAM_MAX_NUM_MSG)
405 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000406
Darren Tuckerd8093e42006-05-04 16:24:34 +1000407 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller120a1ec2018-07-10 19:39:52 +1000408 return PAM_CONV_ERR;
409 if ((buffer = sshbuf_new()) == NULL) {
410 free(reply);
411 return PAM_CONV_ERR;
412 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000413
Damien Miller4f9f42a2003-05-10 19:28:02 +1000414 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000415 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
416 case PAM_PROMPT_ECHO_OFF:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000417 case PAM_PROMPT_ECHO_ON:
Damien Miller120a1ec2018-07-10 19:39:52 +1000418 if ((r = sshbuf_put_cstring(buffer,
419 PAM_MSG_MEMBER(msg, i, msg))) != 0)
420 fatal("%s: buffer error: %s",
421 __func__, ssh_err(r));
Damien Millera8e06ce2003-11-21 23:48:55 +1100422 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller120a1ec2018-07-10 19:39:52 +1000423 PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100424 goto fail;
Damien Miller120a1ec2018-07-10 19:39:52 +1000425
426 if (ssh_msg_recv(ctxt->pam_csock, buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100427 goto fail;
Damien Miller120a1ec2018-07-10 19:39:52 +1000428 if ((r = sshbuf_get_u8(buffer, &status)) != 0)
429 fatal("%s: buffer error: %s",
430 __func__, ssh_err(r));
431 if (status != PAM_AUTHTOK)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000432 goto fail;
Damien Miller120a1ec2018-07-10 19:39:52 +1000433 if ((r = sshbuf_get_cstring(buffer,
434 &reply[i].resp, NULL)) != 0)
435 fatal("%s: buffer error: %s",
436 __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000437 break;
438 case PAM_ERROR_MSG:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000439 case PAM_TEXT_INFO:
Damien Miller120a1ec2018-07-10 19:39:52 +1000440 if ((r = sshbuf_put_cstring(buffer,
441 PAM_MSG_MEMBER(msg, i, msg))) != 0)
442 fatal("%s: buffer error: %s",
443 __func__, ssh_err(r));
Damien Millera8e06ce2003-11-21 23:48:55 +1100444 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller120a1ec2018-07-10 19:39:52 +1000445 PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100446 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000447 break;
448 default:
449 goto fail;
450 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000451 sshbuf_reset(buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000452 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000453 sshbuf_free(buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000454 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000455 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000456
Damien Miller4f9f42a2003-05-10 19:28:02 +1000457 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000458 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000459 free(reply[i].resp);
Damien Miller5c3a5582003-09-23 22:12:38 +1000460 }
Darren Tuckerf60845f2013-06-02 08:07:31 +1000461 free(reply);
Damien Miller120a1ec2018-07-10 19:39:52 +1000462 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000463 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000464}
465
Damien Miller4f9f42a2003-05-10 19:28:02 +1000466/*
467 * Authentication thread.
468 */
469static void *
470sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100471{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000472 struct pam_ctxt *ctxt = ctxtp;
Damien Miller120a1ec2018-07-10 19:39:52 +1000473 struct sshbuf *buffer = NULL;
Damien Millerf4b6f102003-09-02 23:12:06 +1000474 struct pam_conv sshpam_conv;
Damien Miller120a1ec2018-07-10 19:39:52 +1000475 int r, flags = (options.permit_empty_passwd == 0 ?
Darren Tucker1f7e4082004-07-01 14:00:14 +1000476 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000477#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100478 extern char **environ;
479 char **env_from_pam;
480 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000481 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000482 const char **ptr_pam_user = &pam_user;
Darren Tucker54e1b222006-09-17 11:57:46 +1000483 char *tz = getenv("TZ");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000484
Darren Tucker53f8e782013-12-19 11:31:44 +1100485 sshpam_err = pam_get_item(sshpam_handle, PAM_USER,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000486 (sshpam_const void **)ptr_pam_user);
Darren Tucker53f8e782013-12-19 11:31:44 +1100487 if (sshpam_err != PAM_SUCCESS)
488 goto auth_fail;
Darren Tucker54e1b222006-09-17 11:57:46 +1000489
Damien Millerc756e9b2003-11-17 21:41:42 +1100490 environ[0] = NULL;
Darren Tucker54e1b222006-09-17 11:57:46 +1000491 if (tz != NULL)
492 if (setenv("TZ", tz, 1) == -1)
493 error("PAM: could not set TZ environment: %s",
494 strerror(errno));
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000495
496 if (sshpam_authctxt != NULL) {
497 setproctitle("%s [pam]",
498 sshpam_authctxt->valid ? pam_user : "unknown");
499 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000500#endif
501
Damien Millerf4b6f102003-09-02 23:12:06 +1000502 sshpam_conv.conv = sshpam_thread_conv;
503 sshpam_conv.appdata_ptr = ctxt;
504
Darren Tucker17addf02004-03-30 20:57:57 +1000505 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100506 fatal("%s: PAM authctxt not initialized", __func__);
507
Damien Miller120a1ec2018-07-10 19:39:52 +1000508 if ((buffer = sshbuf_new()) == NULL)
509 fatal("%s: sshbuf_new failed", __func__);
510
Damien Miller4f9f42a2003-05-10 19:28:02 +1000511 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
512 (const void *)&sshpam_conv);
513 if (sshpam_err != PAM_SUCCESS)
514 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000515 sshpam_err = pam_authenticate(sshpam_handle, flags);
Darren Tucker01558b72016-07-18 09:33:25 +1000516 if (sshpam_err == PAM_MAXTRIES)
517 sshpam_set_maxtries_reached(1);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000518 if (sshpam_err != PAM_SUCCESS)
519 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100520
Darren Tucker608ec1f2017-03-29 09:50:54 +1100521 if (!do_pam_account()) {
522 sshpam_err = PAM_ACCT_EXPIRED;
523 goto auth_fail;
524 }
525 if (sshpam_authctxt->force_pwchange) {
526 sshpam_err = pam_chauthtok(sshpam_handle,
527 PAM_CHANGE_EXPIRED_AUTHTOK);
528 if (sshpam_err != PAM_SUCCESS)
Darren Tucker07705c72003-12-18 15:34:31 +1100529 goto auth_fail;
Darren Tucker608ec1f2017-03-29 09:50:54 +1100530 sshpam_password_change_required(0);
Darren Tuckerc376c862003-12-18 16:08:59 +1100531 }
Darren Tucker07705c72003-12-18 15:34:31 +1100532
Damien Miller120a1ec2018-07-10 19:39:52 +1000533 if ((r = sshbuf_put_cstring(buffer, "OK")) != 0)
534 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100535
Darren Tucker328118a2005-05-25 16:18:09 +1000536#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100537 /* Export variables set by do_pam_account */
Damien Miller120a1ec2018-07-10 19:39:52 +1000538 if ((r = sshbuf_put_u32(buffer, sshpam_account_status)) != 0 ||
539 (r = sshbuf_put_u32(buffer, sshpam_authctxt->force_pwchange)) != 0)
540 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker07705c72003-12-18 15:34:31 +1100541
Damien Millerc756e9b2003-11-17 21:41:42 +1100542 /* Export any environment strings set in child */
Damien Miller120a1ec2018-07-10 19:39:52 +1000543 for (i = 0; environ[i] != NULL; i++) {
544 /* Count */
545 if (i > INT_MAX)
Darren Tuckerfc0340f2019-06-08 00:10:59 +1000546 fatal("%s: too many environment strings", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000547 }
548 if ((r = sshbuf_put_u32(buffer, i)) != 0)
549 fatal("%s: buffer error: %s", __func__, ssh_err(r));
550 for (i = 0; environ[i] != NULL; i++) {
551 if ((r = sshbuf_put_cstring(buffer, environ[i])) != 0)
552 fatal("%s: buffer error: %s", __func__, ssh_err(r));
553 }
Damien Millerc756e9b2003-11-17 21:41:42 +1100554 /* Export any environment strings set by PAM in child */
555 env_from_pam = pam_getenvlist(sshpam_handle);
Damien Miller120a1ec2018-07-10 19:39:52 +1000556 for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) {
557 /* Count */
558 if (i > INT_MAX)
Darren Tuckerfc0340f2019-06-08 00:10:59 +1000559 fatal("%s: too many PAM environment strings", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000560 }
561 if ((r = sshbuf_put_u32(buffer, i)) != 0)
562 fatal("%s: buffer error: %s", __func__, ssh_err(r));
563 for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) {
564 if ((r = sshbuf_put_cstring(buffer, env_from_pam[i])) != 0)
565 fatal("%s: buffer error: %s", __func__, ssh_err(r));
566 }
Darren Tucker328118a2005-05-25 16:18:09 +1000567#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100568
Damien Miller9bdba702003-11-17 21:27:55 +1100569 /* XXX - can't do much about an error here */
Damien Miller120a1ec2018-07-10 19:39:52 +1000570 ssh_msg_send(ctxt->pam_csock, sshpam_err, buffer);
571 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000572 pthread_exit(NULL);
573
574 auth_fail:
Damien Miller120a1ec2018-07-10 19:39:52 +1000575 if ((r = sshbuf_put_cstring(buffer,
576 pam_strerror(sshpam_handle, sshpam_err))) != 0)
577 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller9bdba702003-11-17 21:27:55 +1100578 /* XXX - can't do much about an error here */
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000579 if (sshpam_err == PAM_ACCT_EXPIRED)
Damien Miller120a1ec2018-07-10 19:39:52 +1000580 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, buffer);
Darren Tucker01558b72016-07-18 09:33:25 +1000581 else if (sshpam_maxtries_reached)
Damien Miller120a1ec2018-07-10 19:39:52 +1000582 ssh_msg_send(ctxt->pam_csock, PAM_MAXTRIES, buffer);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000583 else
Damien Miller120a1ec2018-07-10 19:39:52 +1000584 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, buffer);
585 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000586 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100587
Damien Miller4f9f42a2003-05-10 19:28:02 +1000588 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000589}
590
Darren Tucker8846a072003-10-07 11:30:15 +1000591void
592sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000593{
Darren Tucker8846a072003-10-07 11:30:15 +1000594 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000595
Darren Tuckera8df9242004-01-15 00:15:07 +1100596 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000597 if (ctxt != NULL && ctxt->pam_thread != 0) {
598 pthread_cancel(ctxt->pam_thread);
599 pthread_join(ctxt->pam_thread, NULL);
600 close(ctxt->pam_psock);
601 close(ctxt->pam_csock);
602 memset(ctxt, 0, sizeof(*ctxt));
603 cleanup_ctxt = NULL;
604 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000605}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000606
Damien Miller4f9f42a2003-05-10 19:28:02 +1000607static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000608sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000609 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000610{
Darren Tuckerba53b832004-02-17 20:46:59 +1100611 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000612 return (PAM_CONV_ERR);
613}
Damien Miller63dc3e92001-02-07 12:58:33 +1100614
Damien Miller4f9f42a2003-05-10 19:28:02 +1000615static struct pam_conv null_conv = { sshpam_null_conv, NULL };
616
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000617static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000618sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000619 struct pam_response **resp, void *data)
620{
621 struct pam_response *reply;
Damien Miller120a1ec2018-07-10 19:39:52 +1000622 int r, i;
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000623
624 debug3("PAM: %s called with %d messages", __func__, n);
625 *resp = NULL;
626
627 if (n <= 0 || n > PAM_MAX_NUM_MSG)
628 return (PAM_CONV_ERR);
629
Darren Tuckerd8093e42006-05-04 16:24:34 +1000630 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000631 return (PAM_CONV_ERR);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000632
633 for (i = 0; i < n; ++i) {
634 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
635 case PAM_ERROR_MSG:
636 case PAM_TEXT_INFO:
Damien Miller120a1ec2018-07-10 19:39:52 +1000637 if ((r = sshbuf_putf(loginmsg, "%s\n",
638 PAM_MSG_MEMBER(msg, i, msg))) != 0)
639 fatal("%s: buffer error: %s",
640 __func__, ssh_err(r));
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000641 reply[i].resp_retcode = PAM_SUCCESS;
642 break;
643 default:
644 goto fail;
645 }
646 }
647 *resp = reply;
648 return (PAM_SUCCESS);
649
650 fail:
651 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000652 free(reply[i].resp);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000653 }
Darren Tuckerf60845f2013-06-02 08:07:31 +1000654 free(reply);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000655 return (PAM_CONV_ERR);
656}
657
658static struct pam_conv store_conv = { sshpam_store_conv, NULL };
659
Darren Tucker8846a072003-10-07 11:30:15 +1000660void
661sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000662{
Darren Tucker52358d62008-03-11 22:58:25 +1100663 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
Damien Miller5c3a5582003-09-23 22:12:38 +1000664 return;
Darren Tucker52358d62008-03-11 22:58:25 +1100665 debug("PAM: cleanup");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000666 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000667 if (sshpam_session_open) {
Darren Tucker52358d62008-03-11 22:58:25 +1100668 debug("PAM: closing session");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000669 pam_close_session(sshpam_handle, PAM_SILENT);
670 sshpam_session_open = 0;
671 }
Darren Tucker622d5c52009-07-12 22:07:21 +1000672 if (sshpam_cred_established) {
673 debug("PAM: deleting credentials");
674 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
675 sshpam_cred_established = 0;
676 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100677 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000678 pam_end(sshpam_handle, sshpam_err);
679 sshpam_handle = NULL;
680}
681
682static int
Damien Miller3f0786b2019-01-20 10:22:18 +1100683sshpam_init(struct ssh *ssh, Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000684{
Damien Miller3f0786b2019-01-20 10:22:18 +1100685 const char *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000686 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000687
Damien Miller3f0786b2019-01-20 10:22:18 +1100688 if (sshpam_handle == NULL) {
689 if (ssh == NULL) {
690 fatal("%s: called initially with no "
691 "packet context", __func__);
692 }
693 } if (sshpam_handle != NULL) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000694 /* We already have a PAM context; check if the user matches */
695 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000696 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000697 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
698 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000699 pam_end(sshpam_handle, sshpam_err);
700 sshpam_handle = NULL;
701 }
702 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000703 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000704 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000705 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100706
Damien Miller4f9f42a2003-05-10 19:28:02 +1000707 if (sshpam_err != PAM_SUCCESS) {
708 pam_end(sshpam_handle, sshpam_err);
709 sshpam_handle = NULL;
710 return (-1);
711 }
Damien Miller8a22ffa2018-12-07 15:41:16 +1100712
Damien Miller3f0786b2019-01-20 10:22:18 +1100713 if (ssh != NULL && sshpam_rhost == NULL) {
714 /*
715 * We need to cache these as we don't have packet context
716 * during the kbdint flow.
717 */
718 sshpam_rhost = xstrdup(auth_get_canonical_hostname(ssh,
719 options.use_dns));
720 sshpam_laddr = get_local_ipaddr(
721 ssh_packet_get_connection_in(ssh));
722 xasprintf(&sshpam_conninfo, "SSH_CONNECTION=%.50s %d %.50s %d",
723 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
724 sshpam_laddr, ssh_local_port(ssh));
725 }
726 if (sshpam_rhost != NULL) {
727 debug("PAM: setting PAM_RHOST to \"%s\"", sshpam_rhost);
728 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST,
729 sshpam_rhost);
730 if (sshpam_err != PAM_SUCCESS) {
731 pam_end(sshpam_handle, sshpam_err);
732 sshpam_handle = NULL;
733 return (-1);
734 }
735 /* Put SSH_CONNECTION in the PAM environment too */
736 pam_putenv(sshpam_handle, sshpam_conninfo);
737 }
Damien Miller8a22ffa2018-12-07 15:41:16 +1100738
Damien Miller25d93422003-05-18 20:45:47 +1000739#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100740 /*
741 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
742 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000743 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100744 */
Damien Miller25d93422003-05-18 20:45:47 +1000745 debug("PAM: setting PAM_TTY to \"ssh\"");
746 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
747 if (sshpam_err != PAM_SUCCESS) {
748 pam_end(sshpam_handle, sshpam_err);
749 sshpam_handle = NULL;
750 return (-1);
751 }
752#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000753 return (0);
754}
755
Damien Millere8f47452018-04-06 14:11:44 +1000756static void
757expose_authinfo(const char *caller)
758{
759 char *auth_info;
760
761 /*
762 * Expose authentication information to PAM.
763 * The environment variable is versioned. Please increment the
764 * version suffix if the format of session_info changes.
765 */
766 if (sshpam_authctxt->session_info == NULL)
767 auth_info = xstrdup("");
768 else if ((auth_info = sshbuf_dup_string(
769 sshpam_authctxt->session_info)) == NULL)
770 fatal("%s: sshbuf_dup_string failed", __func__);
771
772 debug2("%s: auth information in SSH_AUTH_INFO_0", caller);
773 do_pam_putenv("SSH_AUTH_INFO_0", auth_info);
774 free(auth_info);
775}
776
Damien Miller4f9f42a2003-05-10 19:28:02 +1000777static void *
778sshpam_init_ctx(Authctxt *authctxt)
779{
780 struct pam_ctxt *ctxt;
Darren Tuckerd220b672019-06-07 14:26:54 +1000781 int result, socks[2];
Damien Miller4f9f42a2003-05-10 19:28:02 +1000782
Darren Tuckera8df9242004-01-15 00:15:07 +1100783 debug3("PAM: %s entering", __func__);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000784 /*
785 * Refuse to start if we don't have PAM enabled or do_pam_account
786 * has previously failed.
787 */
788 if (!options.use_pam || sshpam_account_status == 0)
Damien Miller4e448a32003-05-14 15:11:48 +1000789 return NULL;
790
Damien Miller4f9f42a2003-05-10 19:28:02 +1000791 /* Initialize PAM */
Damien Miller3f0786b2019-01-20 10:22:18 +1100792 if (sshpam_init(NULL, authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000793 error("PAM: initialization failed");
794 return (NULL);
795 }
796
Damien Millere8f47452018-04-06 14:11:44 +1000797 expose_authinfo(__func__);
Darren Tucker29171e92007-05-20 15:20:08 +1000798 ctxt = xcalloc(1, sizeof *ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000799
800 /* Start the authentication thread */
801 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
802 error("PAM: failed create sockets: %s", strerror(errno));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000803 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000804 return (NULL);
805 }
806 ctxt->pam_psock = socks[0];
807 ctxt->pam_csock = socks[1];
Elliott Hughes1bd4f7f2019-04-25 13:36:27 -0700808 result = pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt);
809 if (result != 0) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000810 error("PAM: failed to start authentication thread: %s",
Elliott Hughes1bd4f7f2019-04-25 13:36:27 -0700811 strerror(result));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000812 close(socks[0]);
813 close(socks[1]);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000814 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000815 return (NULL);
816 }
Darren Tucker8846a072003-10-07 11:30:15 +1000817 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000818 return (ctxt);
819}
820
821static int
822sshpam_query(void *ctx, char **name, char **info,
823 u_int *num, char ***prompts, u_int **echo_on)
824{
Damien Miller120a1ec2018-07-10 19:39:52 +1000825 struct sshbuf *buffer;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000826 struct pam_ctxt *ctxt = ctx;
827 size_t plen;
828 u_char type;
829 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000830 size_t len, mlen;
Damien Miller120a1ec2018-07-10 19:39:52 +1000831 int r;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000832
Darren Tuckera8df9242004-01-15 00:15:07 +1100833 debug3("PAM: %s entering", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000834 if ((buffer = sshbuf_new()) == NULL)
835 fatal("%s: sshbuf_new failed", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000836 *name = xstrdup("");
837 *info = xstrdup("");
838 *prompts = xmalloc(sizeof(char *));
839 **prompts = NULL;
840 plen = 0;
841 *echo_on = xmalloc(sizeof(u_int));
Damien Miller120a1ec2018-07-10 19:39:52 +1000842 while (ssh_msg_recv(ctxt->pam_psock, buffer) == 0) {
843 if ((r = sshbuf_get_u8(buffer, &type)) != 0 ||
844 (r = sshbuf_get_cstring(buffer, &msg, &mlen)) != 0)
845 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000846 switch (type) {
847 case PAM_PROMPT_ECHO_ON:
848 case PAM_PROMPT_ECHO_OFF:
849 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000850 len = plen + mlen + 1;
Darren Tuckerd1680d32015-04-30 09:18:11 +1000851 **prompts = xreallocarray(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000852 strlcpy(**prompts + plen, msg, len - plen);
853 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000854 **echo_on = (type == PAM_PROMPT_ECHO_ON);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000855 free(msg);
Damien Millerec0e6242019-09-13 13:14:39 +1000856 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000857 return (0);
858 case PAM_ERROR_MSG:
859 case PAM_TEXT_INFO:
860 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000861 len = plen + mlen + 2;
Darren Tuckerd1680d32015-04-30 09:18:11 +1000862 **prompts = xreallocarray(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000863 strlcpy(**prompts + plen, msg, len - plen);
864 plen += mlen;
865 strlcat(**prompts + plen, "\n", len - plen);
866 plen++;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000867 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000868 break;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000869 case PAM_ACCT_EXPIRED:
Darren Tucker01558b72016-07-18 09:33:25 +1000870 case PAM_MAXTRIES:
871 if (type == PAM_ACCT_EXPIRED)
872 sshpam_account_status = 0;
873 if (type == PAM_MAXTRIES)
874 sshpam_set_maxtries_reached(1);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000875 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000876 case PAM_AUTH_ERR:
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000877 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
Darren Tucker7b1e6952005-09-28 22:33:27 +1000878 if (**prompts != NULL && strlen(**prompts) != 0) {
879 *info = **prompts;
880 **prompts = NULL;
881 *num = 0;
882 **echo_on = 0;
883 ctxt->pam_done = -1;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000884 free(msg);
Damien Millerec0e6242019-09-13 13:14:39 +1000885 sshbuf_free(buffer);
Darren Tucker7b1e6952005-09-28 22:33:27 +1000886 return 0;
887 }
888 /* FALLTHROUGH */
889 case PAM_SUCCESS:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000890 if (**prompts != NULL) {
891 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100892 debug("PAM: %s", **prompts);
Damien Miller120a1ec2018-07-10 19:39:52 +1000893 if ((r = sshbuf_put(loginmsg, **prompts,
894 strlen(**prompts))) != 0)
895 fatal("%s: buffer error: %s",
896 __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000897 free(**prompts);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000898 **prompts = NULL;
899 }
900 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100901 if (!sshpam_authctxt->valid ||
902 (sshpam_authctxt->pw->pw_uid == 0 &&
903 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100904 fatal("Internal error: PAM auth "
905 "succeeded when it should have "
906 "failed");
Damien Miller120a1ec2018-07-10 19:39:52 +1000907 import_environments(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000908 *num = 0;
909 **echo_on = 0;
910 ctxt->pam_done = 1;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000911 free(msg);
Damien Millerec0e6242019-09-13 13:14:39 +1000912 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000913 return (0);
914 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000915 error("PAM: %s for %s%.100s from %.100s", msg,
916 sshpam_authctxt->valid ? "" : "illegal user ",
Damien Miller3f0786b2019-01-20 10:22:18 +1100917 sshpam_authctxt->user, sshpam_rhost);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000918 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000919 default:
920 *num = 0;
921 **echo_on = 0;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000922 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000923 ctxt->pam_done = -1;
Damien Millerec0e6242019-09-13 13:14:39 +1000924 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000925 return (-1);
926 }
927 }
Damien Millerec0e6242019-09-13 13:14:39 +1000928 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000929 return (-1);
930}
931
Darren Tucker283b97f2016-07-15 13:49:44 +1000932/*
933 * Returns a junk password of identical length to that the user supplied.
934 * Used to mitigate timing attacks against crypt(3)/PAM stacks that
935 * vary processing time in proportion to password length.
936 */
937static char *
938fake_password(const char *wire_password)
939{
940 const char junk[] = "\b\n\r\177INCORRECT";
941 char *ret = NULL;
942 size_t i, l = wire_password != NULL ? strlen(wire_password) : 0;
943
944 if (l >= INT_MAX)
945 fatal("%s: password length too long: %zu", __func__, l);
946
947 ret = malloc(l + 1);
Darren Tuckerbee01672017-03-10 13:40:18 +1100948 if (ret == NULL)
949 return NULL;
Darren Tucker283b97f2016-07-15 13:49:44 +1000950 for (i = 0; i < l; i++)
951 ret[i] = junk[i % (sizeof(junk) - 1)];
952 ret[i] = '\0';
953 return ret;
954}
955
Damien Miller4f9f42a2003-05-10 19:28:02 +1000956/* XXX - see also comment in auth-chall.c:verify_response */
957static int
958sshpam_respond(void *ctx, u_int num, char **resp)
959{
Damien Miller120a1ec2018-07-10 19:39:52 +1000960 struct sshbuf *buffer;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000961 struct pam_ctxt *ctxt = ctx;
Darren Tucker283b97f2016-07-15 13:49:44 +1000962 char *fake;
Damien Miller120a1ec2018-07-10 19:39:52 +1000963 int r;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000964
Darren Tucker1d4ebbf2006-01-29 16:46:13 +1100965 debug2("PAM: %s entering, %u responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000966 switch (ctxt->pam_done) {
967 case 1:
968 sshpam_authenticated = 1;
969 return (0);
970 case 0:
971 break;
972 default:
973 return (-1);
974 }
975 if (num != 1) {
976 error("PAM: expected one response, got %u", num);
977 return (-1);
978 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000979 if ((buffer = sshbuf_new()) == NULL)
980 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100981 if (sshpam_authctxt->valid &&
982 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller120a1ec2018-07-10 19:39:52 +1000983 options.permit_root_login == PERMIT_YES)) {
984 if ((r = sshbuf_put_cstring(buffer, *resp)) != 0)
985 fatal("%s: buffer error: %s", __func__, ssh_err(r));
986 } else {
Darren Tucker283b97f2016-07-15 13:49:44 +1000987 fake = fake_password(*resp);
Damien Miller120a1ec2018-07-10 19:39:52 +1000988 if ((r = sshbuf_put_cstring(buffer, fake)) != 0)
989 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker283b97f2016-07-15 13:49:44 +1000990 free(fake);
991 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000992 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, buffer) == -1) {
993 sshbuf_free(buffer);
Damien Miller9bdba702003-11-17 21:27:55 +1100994 return (-1);
995 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000996 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000997 return (1);
998}
999
1000static void
1001sshpam_free_ctx(void *ctxtp)
1002{
1003 struct pam_ctxt *ctxt = ctxtp;
1004
Darren Tuckera8df9242004-01-15 00:15:07 +11001005 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +10001006 sshpam_thread_cleanup();
Darren Tuckerf60845f2013-06-02 08:07:31 +10001007 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001008 /*
1009 * We don't call sshpam_cleanup() here because we may need the PAM
1010 * handle at a later stage, e.g. when setting up a session. It's
1011 * still on the cleanup list, so pam_end() *will* be called before
1012 * the server process terminates.
1013 */
1014}
1015
1016KbdintDevice sshpam_device = {
1017 "pam",
1018 sshpam_init_ctx,
1019 sshpam_query,
1020 sshpam_respond,
1021 sshpam_free_ctx
1022};
1023
1024KbdintDevice mm_sshpam_device = {
1025 "pam",
1026 mm_sshpam_init_ctx,
1027 mm_sshpam_query,
1028 mm_sshpam_respond,
1029 mm_sshpam_free_ctx
1030};
1031
1032/*
1033 * This replaces auth-pam.c
1034 */
1035void
Damien Miller3f0786b2019-01-20 10:22:18 +11001036start_pam(struct ssh *ssh)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001037{
Damien Miller3f0786b2019-01-20 10:22:18 +11001038 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1039
Damien Miller9d507da2003-05-14 15:31:12 +10001040 if (!options.use_pam)
1041 fatal("PAM: initialisation requested when UsePAM=no");
1042
Damien Miller3f0786b2019-01-20 10:22:18 +11001043 if (sshpam_init(ssh, authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001044 fatal("PAM: initialisation failed");
1045}
1046
1047void
1048finish_pam(void)
1049{
Darren Tucker8846a072003-10-07 11:30:15 +10001050 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +10001051}
1052
Damien Miller94bc1e72017-07-28 14:50:59 +10001053
Damien Miller1f499fd2003-08-25 13:08:49 +10001054u_int
1055do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001056{
Darren Tucker77fc29e2004-09-11 23:07:03 +10001057 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +11001058 if (sshpam_account_status != -1)
1059 return (sshpam_account_status);
1060
Damien Miller94bc1e72017-07-28 14:50:59 +10001061 expose_authinfo(__func__);
1062
Damien Miller1f499fd2003-08-25 13:08:49 +10001063 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +10001064 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
1065 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +10001066
Darren Tucker07705c72003-12-18 15:34:31 +11001067 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
1068 sshpam_account_status = 0;
1069 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +10001070 }
1071
Darren Tucker07705c72003-12-18 15:34:31 +11001072 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +10001073 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +11001074
1075 sshpam_account_status = 1;
1076 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001077}
1078
1079void
Damien Miller4f9f42a2003-05-10 19:28:02 +10001080do_pam_setcred(int init)
1081{
1082 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +10001083 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001084 if (sshpam_err != PAM_SUCCESS)
1085 fatal("PAM: failed to set PAM_CONV: %s",
1086 pam_strerror(sshpam_handle, sshpam_err));
1087 if (init) {
1088 debug("PAM: establishing credentials");
1089 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
1090 } else {
1091 debug("PAM: reinitializing credentials");
1092 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
1093 }
1094 if (sshpam_err == PAM_SUCCESS) {
1095 sshpam_cred_established = 1;
1096 return;
1097 }
1098 if (sshpam_authenticated)
1099 fatal("PAM: pam_setcred(): %s",
1100 pam_strerror(sshpam_handle, sshpam_err));
1101 else
1102 debug("PAM: pam_setcred(): %s",
1103 pam_strerror(sshpam_handle, sshpam_err));
1104}
1105
Damien Miller4f9f42a2003-05-10 19:28:02 +10001106static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001107sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +10001108 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001109{
1110 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +10001111 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001112 int i;
1113
Darren Tuckerba53b832004-02-17 20:46:59 +11001114 debug3("PAM: %s called with %d messages", __func__, n);
1115
Damien Miller5c3a5582003-09-23 22:12:38 +10001116 *resp = NULL;
1117
Darren Tucker18df00c2003-11-18 12:42:07 +11001118 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +10001119 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +10001120
Darren Tuckerd8093e42006-05-04 16:24:34 +10001121 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +10001122 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +10001123
Damien Miller4f9f42a2003-05-10 19:28:02 +10001124 for (i = 0; i < n; ++i) {
1125 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1126 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +10001127 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +11001128 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +10001129 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +10001130 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001131 break;
1132 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +11001133 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Darren Tucker22164712007-05-20 15:26:07 +10001134 if (fgets(input, sizeof input, stdin) == NULL)
1135 input[0] = '\0';
Damien Millera6fb77f2004-07-19 09:39:11 +10001136 if ((reply[i].resp = strdup(input)) == NULL)
1137 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +10001138 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001139 break;
1140 case PAM_ERROR_MSG:
1141 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +11001142 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +10001143 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001144 break;
1145 default:
1146 goto fail;
1147 }
1148 }
Damien Miller5c3a5582003-09-23 22:12:38 +10001149 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001150 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +10001151
Damien Miller4f9f42a2003-05-10 19:28:02 +10001152 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +10001153 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001154 free(reply[i].resp);
Damien Miller5c3a5582003-09-23 22:12:38 +10001155 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001156 free(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001157 return (PAM_CONV_ERR);
1158}
1159
Darren Tucker94befab2004-06-03 14:53:12 +10001160static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +11001161
Damien Miller4f9f42a2003-05-10 19:28:02 +10001162/*
1163 * XXX this should be done in the authentication phase, but ssh1 doesn't
1164 * support that
1165 */
1166void
1167do_pam_chauthtok(void)
1168{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001169 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +10001170 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +10001171 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +11001172 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001173 if (sshpam_err != PAM_SUCCESS)
1174 fatal("PAM: failed to set PAM_CONV: %s",
1175 pam_strerror(sshpam_handle, sshpam_err));
1176 debug("PAM: changing password");
1177 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1178 if (sshpam_err != PAM_SUCCESS)
1179 fatal("PAM: pam_chauthtok(): %s",
1180 pam_strerror(sshpam_handle, sshpam_err));
1181}
1182
Darren Tucker18df00c2003-11-18 12:42:07 +11001183void
djm@openbsd.org7c856852018-03-03 03:15:51 +00001184do_pam_session(struct ssh *ssh)
Darren Tucker18df00c2003-11-18 12:42:07 +11001185{
Darren Tucker1921ed92004-02-10 13:23:28 +11001186 debug3("PAM: opening session");
Damien Miller94bc1e72017-07-28 14:50:59 +10001187
1188 expose_authinfo(__func__);
1189
Damien Millera8e06ce2003-11-21 23:48:55 +11001190 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +11001191 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +11001192 if (sshpam_err != PAM_SUCCESS)
1193 fatal("PAM: failed to set PAM_CONV: %s",
1194 pam_strerror(sshpam_handle, sshpam_err));
1195 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +10001196 if (sshpam_err == PAM_SUCCESS)
1197 sshpam_session_open = 1;
1198 else {
1199 sshpam_session_open = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001200 auth_restrict_session(ssh);
Darren Tucker69687f42004-09-11 22:17:26 +10001201 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001202 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001203 }
1204
1205}
1206
1207int
1208is_pam_session_open(void)
1209{
1210 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001211}
1212
Damien Millera8e06ce2003-11-21 23:48:55 +11001213/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001214 * Set a PAM environment string. We need to do this so that the session
1215 * modules can handle things like Kerberos/GSI credentials that appear
1216 * during the ssh authentication process.
1217 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001218int
Damien Millera8e06ce2003-11-21 23:48:55 +11001219do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001220{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001221 int ret = 1;
Damien Millerf2728092003-09-17 07:24:25 +10001222 char *compound;
1223 size_t len;
1224
1225 len = strlen(name) + strlen(value) + 2;
1226 compound = xmalloc(len);
1227
1228 snprintf(compound, len, "%s=%s", name, value);
1229 ret = pam_putenv(sshpam_handle, compound);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001230 free(compound);
Damien Millerf2728092003-09-17 07:24:25 +10001231
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001232 return (ret);
1233}
1234
Damien Miller4f9f42a2003-05-10 19:28:02 +10001235char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001236fetch_pam_child_environment(void)
1237{
1238 return sshpam_env;
1239}
1240
1241char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001242fetch_pam_environment(void)
1243{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001244 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001245}
1246
1247void
1248free_pam_environment(char **env)
1249{
1250 char **envp;
1251
Damien Millere27c6cc2003-05-16 18:21:01 +10001252 if (env == NULL)
1253 return;
1254
Damien Miller4f9f42a2003-05-10 19:28:02 +10001255 for (envp = env; *envp; envp++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001256 free(*envp);
1257 free(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001258}
1259
Darren Tucker450a1582004-05-30 20:43:59 +10001260/*
1261 * "Blind" conversation function for password authentication. Assumes that
1262 * echo-off prompts are for the password and stores messages for later
1263 * display.
1264 */
1265static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001266sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001267 struct pam_response **resp, void *data)
1268{
1269 struct pam_response *reply;
Damien Miller120a1ec2018-07-10 19:39:52 +10001270 int r, i;
Darren Tucker450a1582004-05-30 20:43:59 +10001271 size_t len;
1272
1273 debug3("PAM: %s called with %d messages", __func__, n);
1274
1275 *resp = NULL;
1276
1277 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1278 return (PAM_CONV_ERR);
1279
Darren Tucker29171e92007-05-20 15:20:08 +10001280 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker450a1582004-05-30 20:43:59 +10001281 return (PAM_CONV_ERR);
Darren Tucker450a1582004-05-30 20:43:59 +10001282
1283 for (i = 0; i < n; ++i) {
1284 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1285 case PAM_PROMPT_ECHO_OFF:
1286 if (sshpam_password == NULL)
1287 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001288 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1289 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001290 reply[i].resp_retcode = PAM_SUCCESS;
1291 break;
1292 case PAM_ERROR_MSG:
1293 case PAM_TEXT_INFO:
1294 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1295 if (len > 0) {
Damien Miller120a1ec2018-07-10 19:39:52 +10001296 if ((r = sshbuf_putf(loginmsg, "%s\n",
1297 PAM_MSG_MEMBER(msg, i, msg))) != 0)
1298 fatal("%s: buffer error: %s",
1299 __func__, ssh_err(r));
Darren Tucker450a1582004-05-30 20:43:59 +10001300 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001301 if ((reply[i].resp = strdup("")) == NULL)
1302 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001303 reply[i].resp_retcode = PAM_SUCCESS;
1304 break;
1305 default:
1306 goto fail;
1307 }
1308 }
1309 *resp = reply;
1310 return (PAM_SUCCESS);
1311
Damien Miller94cf4c82005-07-17 17:04:47 +10001312 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001313 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001314 free(reply[i].resp);
Darren Tucker450a1582004-05-30 20:43:59 +10001315 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001316 free(reply);
Darren Tucker450a1582004-05-30 20:43:59 +10001317 return (PAM_CONV_ERR);
1318}
1319
1320static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1321
1322/*
1323 * Attempt password authentication via PAM
1324 */
1325int
1326sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1327{
1328 int flags = (options.permit_empty_passwd == 0 ?
1329 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker283b97f2016-07-15 13:49:44 +10001330 char *fake = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +10001331
1332 if (!options.use_pam || sshpam_handle == NULL)
1333 fatal("PAM: %s called when PAM disabled or failed to "
1334 "initialise.", __func__);
1335
1336 sshpam_password = password;
1337 sshpam_authctxt = authctxt;
1338
Darren Tuckere061b152004-05-30 22:04:56 +10001339 /*
1340 * If the user logging in is invalid, or is root but is not permitted
1341 * by PermitRootLogin, use an invalid password to prevent leaking
1342 * information via timing (eg if the PAM config has a delay on fail).
1343 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001344 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001345 options.permit_root_login != PERMIT_YES))
Darren Tucker283b97f2016-07-15 13:49:44 +10001346 sshpam_password = fake = fake_password(password);
Darren Tuckere061b152004-05-30 22:04:56 +10001347
Darren Tucker450a1582004-05-30 20:43:59 +10001348 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1349 (const void *)&passwd_conv);
1350 if (sshpam_err != PAM_SUCCESS)
1351 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1352 pam_strerror(sshpam_handle, sshpam_err));
1353
1354 sshpam_err = pam_authenticate(sshpam_handle, flags);
1355 sshpam_password = NULL;
Darren Tucker283b97f2016-07-15 13:49:44 +10001356 free(fake);
Darren Tucker01558b72016-07-18 09:33:25 +10001357 if (sshpam_err == PAM_MAXTRIES)
1358 sshpam_set_maxtries_reached(1);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001359 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001360 debug("PAM: password authentication accepted for %.100s",
1361 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001362 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001363 } else {
1364 debug("PAM: password authentication failed for %.100s: %s",
1365 authctxt->valid ? authctxt->user : "an illegal user",
1366 pam_strerror(sshpam_handle, sshpam_err));
1367 return 0;
1368 }
1369}
Darren Tucker01558b72016-07-18 09:33:25 +10001370
1371int
1372sshpam_get_maxtries_reached(void)
1373{
1374 return sshpam_maxtries_reached;
1375}
1376
1377void
1378sshpam_set_maxtries_reached(int reached)
1379{
1380 if (reached == 0 || sshpam_maxtries_reached)
1381 return;
1382 sshpam_maxtries_reached = 1;
1383 options.password_authentication = 0;
1384 options.kbd_interactive_authentication = 0;
1385 options.challenge_response_authentication = 0;
1386}
Damien Millere72b7af1999-12-30 15:08:44 +11001387#endif /* USE_PAM */