blob: 8c0138362a9024d9311dce3eefd3ec4e3b96e151 [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>
Damien Millerb8fe89c2006-07-24 14:51:00 +100059#include <string.h>
60#include <unistd.h>
Damien Millere72b7af1999-12-30 15:08:44 +110061
62#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110063#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100064#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110065#elif defined (HAVE_PAM_PAM_APPL_H)
66#include <pam/pam_appl.h>
67#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100068
Damien Miller8bd81e12016-08-16 13:30:56 +100069#if !defined(SSHD_PAM_SERVICE)
70extern char *__progname;
71# define SSHD_PAM_SERVICE __progname
72#endif
73
Darren Tuckerf08bdb52005-05-26 19:59:48 +100074/* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
75#ifdef PAM_SUN_CODEBASE
Darren Tucker39c0cec2016-05-20 10:01:58 +100076# define sshpam_const /* Solaris, HP-UX, SunOS */
Darren Tuckerf08bdb52005-05-26 19:59:48 +100077#else
Darren Tucker39c0cec2016-05-20 10:01:58 +100078# define sshpam_const const /* LinuxPAM, OpenPAM, AIX */
Darren Tuckerf08bdb52005-05-26 19:59:48 +100079#endif
80
Damien Miller2ab323e2006-08-05 12:43:32 +100081/* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
82#ifdef PAM_SUN_CODEBASE
83# define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
84#else
85# define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
86#endif
87
Damien Miller75bb6642006-08-05 14:07:20 +100088#include "xmalloc.h"
Damien Miller120a1ec2018-07-10 19:39:52 +100089#include "sshbuf.h"
90#include "ssherr.h"
Damien Miller75bb6642006-08-05 14:07:20 +100091#include "hostfile.h"
Kevin Stevese683e762002-04-04 19:02:28 +000092#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110093#include "auth-pam.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000094#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100095#include "log.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100096#include "msg.h"
97#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100098#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100099#include "servconf.h"
100#include "ssh2.h"
Damien Miller1f499fd2003-08-25 13:08:49 +1000101#include "auth-options.h"
Damien Miller75bb6642006-08-05 14:07:20 +1000102#ifdef GSSAPI
103#include "ssh-gss.h"
104#endif
105#include "monitor_wrap.h"
Damien Millere72b7af1999-12-30 15:08:44 +1100106
Damien Miller4e448a32003-05-14 15:11:48 +1000107extern ServerOptions options;
Damien Miller120a1ec2018-07-10 19:39:52 +1000108extern struct sshbuf *loginmsg;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000109extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +1000110
Darren Tucker328118a2005-05-25 16:18:09 +1000111/* so we don't silently change behaviour */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000112#ifdef USE_POSIX_THREADS
Darren Tucker328118a2005-05-25 16:18:09 +1000113# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
114#endif
115
116/*
117 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
118 * and generally a bad idea. Use at own risk and do not expect support if
119 * this breaks.
120 */
121#ifdef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000122#include <pthread.h>
123/*
Damien Millera8e06ce2003-11-21 23:48:55 +1100124 * Avoid namespace clash when *not* using pthreads for systems *with*
125 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +1000126 * (e.g. Linux)
127 */
Damien Millera8e06ce2003-11-21 23:48:55 +1100128typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000129#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100130typedef pid_t sp_pthread_t;
131#endif
132
133struct pam_ctxt {
134 sp_pthread_t pam_thread;
135 int pam_psock;
136 int pam_csock;
137 int pam_done;
138};
139
140static void sshpam_free_ctx(void *);
141static struct pam_ctxt *cleanup_ctxt;
142
Darren Tucker328118a2005-05-25 16:18:09 +1000143#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000144/*
145 * Simulate threads with processes.
146 */
Kevin Steves63007d42002-07-21 17:57:01 +0000147
Darren Tucker749bc952004-01-14 22:14:04 +1100148static int sshpam_thread_status = -1;
149static mysig_t sshpam_oldsig;
150
Damien Miller94cf4c82005-07-17 17:04:47 +1000151static void
Darren Tucker749bc952004-01-14 22:14:04 +1100152sshpam_sigchld_handler(int sig)
153{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000154 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100155 if (cleanup_ctxt == NULL)
156 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000157 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000158 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000159 /* PAM thread has not exitted, privsep slave must have */
160 kill(cleanup_ctxt->pam_thread, SIGTERM);
Damien Miller10358ab2016-07-22 14:06:36 +1000161 while (waitpid(cleanup_ctxt->pam_thread,
162 &sshpam_thread_status, 0) == -1) {
163 if (errno == EINTR)
164 continue;
165 return;
166 }
Darren Tuckerb53355e2004-05-24 11:55:36 +1000167 }
Darren Tucker749bc952004-01-14 22:14:04 +1100168 if (WIFSIGNALED(sshpam_thread_status) &&
169 WTERMSIG(sshpam_thread_status) == SIGTERM)
170 return; /* terminated by pthread_cancel */
171 if (!WIFEXITED(sshpam_thread_status))
Darren Tucker57d4ca92007-08-10 14:32:34 +1000172 sigdie("PAM: authentication thread exited unexpectedly");
Darren Tucker749bc952004-01-14 22:14:04 +1100173 if (WEXITSTATUS(sshpam_thread_status) != 0)
Darren Tucker57d4ca92007-08-10 14:32:34 +1000174 sigdie("PAM: authentication thread exited uncleanly");
Darren Tucker749bc952004-01-14 22:14:04 +1100175}
176
Damien Millerb8fe89c2006-07-24 14:51:00 +1000177/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000178static void
Damien Millerb8fe89c2006-07-24 14:51:00 +1000179pthread_exit(void *value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000180{
181 _exit(0);
182}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000183
Damien Millerb8fe89c2006-07-24 14:51:00 +1000184/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000185static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000186pthread_create(sp_pthread_t *thread, const void *attr,
Damien Miller4f9f42a2003-05-10 19:28:02 +1000187 void *(*thread_start)(void *), void *arg)
188{
189 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000190 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100191
Darren Tuckerb9b60212004-03-04 20:03:54 +1100192 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000193 switch ((pid = fork())) {
194 case -1:
195 error("fork(): %s", strerror(errno));
196 return (-1);
197 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000198 close(ctx->pam_psock);
199 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000200 thread_start(arg);
201 _exit(1);
202 default:
203 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000204 close(ctx->pam_csock);
205 ctx->pam_csock = -1;
Darren Tucker749bc952004-01-14 22:14:04 +1100206 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000207 return (0);
208 }
209}
Damien Millere72b7af1999-12-30 15:08:44 +1100210
Damien Miller4f9f42a2003-05-10 19:28:02 +1000211static int
212pthread_cancel(sp_pthread_t thread)
213{
Darren Tucker7ae09622004-01-14 23:07:56 +1100214 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000215 return (kill(thread, SIGTERM));
216}
217
Damien Millerb8fe89c2006-07-24 14:51:00 +1000218/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000219static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000220pthread_join(sp_pthread_t thread, void **value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000221{
222 int status;
223
Darren Tucker749bc952004-01-14 22:14:04 +1100224 if (sshpam_thread_status != -1)
225 return (sshpam_thread_status);
226 signal(SIGCHLD, sshpam_oldsig);
Damien Miller10358ab2016-07-22 14:06:36 +1000227 while (waitpid(thread, &status, 0) == -1) {
228 if (errno == EINTR)
229 continue;
230 fatal("%s: waitpid: %s", __func__, strerror(errno));
231 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000232 return (status);
233}
234#endif
235
236
Damien Miller5c3a5582003-09-23 22:12:38 +1000237static pam_handle_t *sshpam_handle = NULL;
238static int sshpam_err = 0;
239static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000240static int sshpam_session_open = 0;
241static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100242static int sshpam_account_status = -1;
Darren Tucker01558b72016-07-18 09:33:25 +1000243static int sshpam_maxtries_reached = 0;
Damien Millerc756e9b2003-11-17 21:41:42 +1100244static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000245static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000246static const char *sshpam_password = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000247
Damien Millerc756e9b2003-11-17 21:41:42 +1100248/* Some PAM implementations don't implement this */
249#ifndef HAVE_PAM_GETENVLIST
250static char **
251pam_getenvlist(pam_handle_t *pamh)
252{
253 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100254 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100255 * for platforms without pam_getenvlist by searching for known
256 * env vars (e.g. KRB5CCNAME) from the PAM environment.
257 */
258 return NULL;
259}
260#endif
261
Darren Tucker21dd0892004-08-16 23:12:05 +1000262/*
263 * Some platforms, notably Solaris, do not enforce password complexity
264 * rules during pam_chauthtok() if the real uid of the calling process
265 * is 0, on the assumption that it's being called by "passwd" run by root.
266 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
267 * the right thing.
268 */
269#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
270static int
271sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
272{
273 int result;
274
275 if (sshpam_authctxt == NULL)
276 fatal("PAM: sshpam_authctxt not initialized");
277 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
278 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
279 result = pam_chauthtok(pamh, flags);
280 if (setreuid(0, -1) == -1)
281 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
282 return result;
283}
284# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
285#endif
286
Darren Tucker07705c72003-12-18 15:34:31 +1100287void
Darren Tucker17db1c42004-06-19 12:54:38 +1000288sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100289{
Darren Tucker13ef4cf2018-03-03 16:21:20 +1100290 extern struct sshauthopt *auth_opts;
291 static int saved_port, saved_agent, saved_x11;
292
Darren Tuckera8df9242004-01-15 00:15:07 +1100293 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000294 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100295 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000296 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100297 if (reqd) {
Darren Tucker13ef4cf2018-03-03 16:21:20 +1100298 saved_port = auth_opts->permit_port_forwarding_flag;
299 saved_agent = auth_opts->permit_agent_forwarding_flag;
300 saved_x11 = auth_opts->permit_x11_forwarding_flag;
301 auth_opts->permit_port_forwarding_flag = 0;
302 auth_opts->permit_agent_forwarding_flag = 0;
303 auth_opts->permit_x11_forwarding_flag = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100304 } else {
Darren Tucker13ef4cf2018-03-03 16:21:20 +1100305 if (saved_port)
306 auth_opts->permit_port_forwarding_flag = saved_port;
307 if (saved_agent)
308 auth_opts->permit_agent_forwarding_flag = saved_agent;
309 if (saved_x11)
310 auth_opts->permit_x11_forwarding_flag = saved_x11;
Darren Tucker07705c72003-12-18 15:34:31 +1100311 }
312}
Darren Tucker1921ed92004-02-10 13:23:28 +1100313
Damien Millerc756e9b2003-11-17 21:41:42 +1100314/* Import regular and PAM environment from subprocess */
315static void
Damien Miller120a1ec2018-07-10 19:39:52 +1000316import_environments(struct sshbuf *b)
Damien Millerc756e9b2003-11-17 21:41:42 +1100317{
318 char *env;
Damien Miller120a1ec2018-07-10 19:39:52 +1000319 u_int n, i, num_env;
320 int r;
Damien Millerc756e9b2003-11-17 21:41:42 +1100321
Darren Tuckera8df9242004-01-15 00:15:07 +1100322 debug3("PAM: %s entering", __func__);
323
Darren Tucker328118a2005-05-25 16:18:09 +1000324#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100325 /* Import variables set by do_pam_account */
Damien Miller120a1ec2018-07-10 19:39:52 +1000326 if ((r = sshbuf_get_u32(b, &n)) != 0)
327 fatal("%s: buffer error: %s", __func__, ssh_err(r));
328 if (n > INT_MAX)
329 fatal("%s: invalid PAM account status %u", __func__, n);
330 sshpam_account_status = (int)n;
331 if ((r = sshbuf_get_u32(b, &n)) != 0)
332 fatal("%s: buffer error: %s", __func__, ssh_err(r));
333 sshpam_password_change_required(n != 0);
Darren Tucker07705c72003-12-18 15:34:31 +1100334
Damien Millerc756e9b2003-11-17 21:41:42 +1100335 /* Import environment from subprocess */
Damien Miller120a1ec2018-07-10 19:39:52 +1000336 if ((r = sshbuf_get_u32(b, &num_env)) != 0)
337 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerd8093e42006-05-04 16:24:34 +1000338 if (num_env > 1024)
339 fatal("%s: received %u environment variables, expected <= 1024",
340 __func__, num_env);
341 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
Damien Millerc756e9b2003-11-17 21:41:42 +1100342 debug3("PAM: num env strings %d", num_env);
Damien Miller120a1ec2018-07-10 19:39:52 +1000343 for(i = 0; i < num_env; i++) {
344 if ((r = sshbuf_get_cstring(b, &(sshpam_env[i]), NULL)) != 0)
345 fatal("%s: buffer error: %s", __func__, ssh_err(r));
346 }
Damien Millerc756e9b2003-11-17 21:41:42 +1100347 sshpam_env[num_env] = NULL;
348
349 /* Import PAM environment from subprocess */
Damien Miller120a1ec2018-07-10 19:39:52 +1000350 if ((r = sshbuf_get_u32(b, &num_env)) != 0)
351 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100352 debug("PAM: num PAM env strings %d", num_env);
Damien Miller120a1ec2018-07-10 19:39:52 +1000353 for (i = 0; i < num_env; i++) {
354 if ((r = sshbuf_get_cstring(b, &env, NULL)) != 0)
355 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker8a1624c2003-11-18 12:45:35 +1100356#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100357 /* Errors are not fatal here */
Damien Miller120a1ec2018-07-10 19:39:52 +1000358 if ((r = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100359 error("PAM: pam_putenv: %s",
Damien Miller120a1ec2018-07-10 19:39:52 +1000360 pam_strerror(sshpam_handle, r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100361 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100362#endif
Damien Miller120a1ec2018-07-10 19:39:52 +1000363 /* XXX leak env? */
Damien Millerc756e9b2003-11-17 21:41:42 +1100364 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100365#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100366}
367
Damien Miller9d5705a2000-09-16 16:09:27 +1100368/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000369 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100370 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000371static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000372sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000373 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100374{
Damien Miller120a1ec2018-07-10 19:39:52 +1000375 struct sshbuf *buffer;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000376 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000377 struct pam_response *reply;
Damien Miller120a1ec2018-07-10 19:39:52 +1000378 int r, i;
379 u_char status;
Kevin Steves38b050a2002-07-23 00:44:07 +0000380
Darren Tuckerba53b832004-02-17 20:46:59 +1100381 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000382 *resp = NULL;
383
Darren Tucker59e06022004-06-30 20:34:31 +1000384 if (data == NULL) {
385 error("PAM: conversation function passed a null context");
386 return (PAM_CONV_ERR);
387 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000388 ctxt = data;
389 if (n <= 0 || n > PAM_MAX_NUM_MSG)
390 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000391
Darren Tuckerd8093e42006-05-04 16:24:34 +1000392 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller120a1ec2018-07-10 19:39:52 +1000393 return PAM_CONV_ERR;
394 if ((buffer = sshbuf_new()) == NULL) {
395 free(reply);
396 return PAM_CONV_ERR;
397 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000398
Damien Miller4f9f42a2003-05-10 19:28:02 +1000399 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000400 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
401 case PAM_PROMPT_ECHO_OFF:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000402 case PAM_PROMPT_ECHO_ON:
Damien Miller120a1ec2018-07-10 19:39:52 +1000403 if ((r = sshbuf_put_cstring(buffer,
404 PAM_MSG_MEMBER(msg, i, msg))) != 0)
405 fatal("%s: buffer error: %s",
406 __func__, ssh_err(r));
Damien Millera8e06ce2003-11-21 23:48:55 +1100407 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller120a1ec2018-07-10 19:39:52 +1000408 PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100409 goto fail;
Damien Miller120a1ec2018-07-10 19:39:52 +1000410
411 if (ssh_msg_recv(ctxt->pam_csock, buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100412 goto fail;
Damien Miller120a1ec2018-07-10 19:39:52 +1000413 if ((r = sshbuf_get_u8(buffer, &status)) != 0)
414 fatal("%s: buffer error: %s",
415 __func__, ssh_err(r));
416 if (status != PAM_AUTHTOK)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000417 goto fail;
Damien Miller120a1ec2018-07-10 19:39:52 +1000418 if ((r = sshbuf_get_cstring(buffer,
419 &reply[i].resp, NULL)) != 0)
420 fatal("%s: buffer error: %s",
421 __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000422 break;
423 case PAM_ERROR_MSG:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000424 case PAM_TEXT_INFO:
Damien Miller120a1ec2018-07-10 19:39:52 +1000425 if ((r = sshbuf_put_cstring(buffer,
426 PAM_MSG_MEMBER(msg, i, msg))) != 0)
427 fatal("%s: buffer error: %s",
428 __func__, ssh_err(r));
Damien Millera8e06ce2003-11-21 23:48:55 +1100429 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller120a1ec2018-07-10 19:39:52 +1000430 PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100431 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000432 break;
433 default:
434 goto fail;
435 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000436 sshbuf_reset(buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000437 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000438 sshbuf_free(buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000439 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000440 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000441
Damien Miller4f9f42a2003-05-10 19:28:02 +1000442 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000443 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000444 free(reply[i].resp);
Damien Miller5c3a5582003-09-23 22:12:38 +1000445 }
Darren Tuckerf60845f2013-06-02 08:07:31 +1000446 free(reply);
Damien Miller120a1ec2018-07-10 19:39:52 +1000447 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000448 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000449}
450
Damien Miller4f9f42a2003-05-10 19:28:02 +1000451/*
452 * Authentication thread.
453 */
454static void *
455sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100456{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000457 struct pam_ctxt *ctxt = ctxtp;
Damien Miller120a1ec2018-07-10 19:39:52 +1000458 struct sshbuf *buffer = NULL;
Damien Millerf4b6f102003-09-02 23:12:06 +1000459 struct pam_conv sshpam_conv;
Damien Miller120a1ec2018-07-10 19:39:52 +1000460 int r, flags = (options.permit_empty_passwd == 0 ?
Darren Tucker1f7e4082004-07-01 14:00:14 +1000461 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000462#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100463 extern char **environ;
464 char **env_from_pam;
465 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000466 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000467 const char **ptr_pam_user = &pam_user;
Darren Tucker54e1b222006-09-17 11:57:46 +1000468 char *tz = getenv("TZ");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000469
Darren Tucker53f8e782013-12-19 11:31:44 +1100470 sshpam_err = pam_get_item(sshpam_handle, PAM_USER,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000471 (sshpam_const void **)ptr_pam_user);
Darren Tucker53f8e782013-12-19 11:31:44 +1100472 if (sshpam_err != PAM_SUCCESS)
473 goto auth_fail;
Darren Tucker54e1b222006-09-17 11:57:46 +1000474
Damien Millerc756e9b2003-11-17 21:41:42 +1100475 environ[0] = NULL;
Darren Tucker54e1b222006-09-17 11:57:46 +1000476 if (tz != NULL)
477 if (setenv("TZ", tz, 1) == -1)
478 error("PAM: could not set TZ environment: %s",
479 strerror(errno));
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000480
481 if (sshpam_authctxt != NULL) {
482 setproctitle("%s [pam]",
483 sshpam_authctxt->valid ? pam_user : "unknown");
484 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000485#endif
486
Damien Millerf4b6f102003-09-02 23:12:06 +1000487 sshpam_conv.conv = sshpam_thread_conv;
488 sshpam_conv.appdata_ptr = ctxt;
489
Darren Tucker17addf02004-03-30 20:57:57 +1000490 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100491 fatal("%s: PAM authctxt not initialized", __func__);
492
Damien Miller120a1ec2018-07-10 19:39:52 +1000493 if ((buffer = sshbuf_new()) == NULL)
494 fatal("%s: sshbuf_new failed", __func__);
495
Damien Miller4f9f42a2003-05-10 19:28:02 +1000496 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
497 (const void *)&sshpam_conv);
498 if (sshpam_err != PAM_SUCCESS)
499 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000500 sshpam_err = pam_authenticate(sshpam_handle, flags);
Darren Tucker01558b72016-07-18 09:33:25 +1000501 if (sshpam_err == PAM_MAXTRIES)
502 sshpam_set_maxtries_reached(1);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000503 if (sshpam_err != PAM_SUCCESS)
504 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100505
Darren Tucker608ec1f2017-03-29 09:50:54 +1100506 if (!do_pam_account()) {
507 sshpam_err = PAM_ACCT_EXPIRED;
508 goto auth_fail;
509 }
510 if (sshpam_authctxt->force_pwchange) {
511 sshpam_err = pam_chauthtok(sshpam_handle,
512 PAM_CHANGE_EXPIRED_AUTHTOK);
513 if (sshpam_err != PAM_SUCCESS)
Darren Tucker07705c72003-12-18 15:34:31 +1100514 goto auth_fail;
Darren Tucker608ec1f2017-03-29 09:50:54 +1100515 sshpam_password_change_required(0);
Darren Tuckerc376c862003-12-18 16:08:59 +1100516 }
Darren Tucker07705c72003-12-18 15:34:31 +1100517
Damien Miller120a1ec2018-07-10 19:39:52 +1000518 if ((r = sshbuf_put_cstring(buffer, "OK")) != 0)
519 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100520
Darren Tucker328118a2005-05-25 16:18:09 +1000521#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100522 /* Export variables set by do_pam_account */
Damien Miller120a1ec2018-07-10 19:39:52 +1000523 if ((r = sshbuf_put_u32(buffer, sshpam_account_status)) != 0 ||
524 (r = sshbuf_put_u32(buffer, sshpam_authctxt->force_pwchange)) != 0)
525 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker07705c72003-12-18 15:34:31 +1100526
Damien Millerc756e9b2003-11-17 21:41:42 +1100527 /* Export any environment strings set in child */
Damien Miller120a1ec2018-07-10 19:39:52 +1000528 for (i = 0; environ[i] != NULL; i++) {
529 /* Count */
530 if (i > INT_MAX)
531 fatal("%s: too many enviornment strings", __func__);
532 }
533 if ((r = sshbuf_put_u32(buffer, i)) != 0)
534 fatal("%s: buffer error: %s", __func__, ssh_err(r));
535 for (i = 0; environ[i] != NULL; i++) {
536 if ((r = sshbuf_put_cstring(buffer, environ[i])) != 0)
537 fatal("%s: buffer error: %s", __func__, ssh_err(r));
538 }
Damien Millerc756e9b2003-11-17 21:41:42 +1100539 /* Export any environment strings set by PAM in child */
540 env_from_pam = pam_getenvlist(sshpam_handle);
Damien Miller120a1ec2018-07-10 19:39:52 +1000541 for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) {
542 /* Count */
543 if (i > INT_MAX)
544 fatal("%s: too many PAM enviornment strings", __func__);
545 }
546 if ((r = sshbuf_put_u32(buffer, i)) != 0)
547 fatal("%s: buffer error: %s", __func__, ssh_err(r));
548 for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) {
549 if ((r = sshbuf_put_cstring(buffer, env_from_pam[i])) != 0)
550 fatal("%s: buffer error: %s", __func__, ssh_err(r));
551 }
Darren Tucker328118a2005-05-25 16:18:09 +1000552#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100553
Damien Miller9bdba702003-11-17 21:27:55 +1100554 /* XXX - can't do much about an error here */
Damien Miller120a1ec2018-07-10 19:39:52 +1000555 ssh_msg_send(ctxt->pam_csock, sshpam_err, buffer);
556 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000557 pthread_exit(NULL);
558
559 auth_fail:
Damien Miller120a1ec2018-07-10 19:39:52 +1000560 if ((r = sshbuf_put_cstring(buffer,
561 pam_strerror(sshpam_handle, sshpam_err))) != 0)
562 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller9bdba702003-11-17 21:27:55 +1100563 /* XXX - can't do much about an error here */
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000564 if (sshpam_err == PAM_ACCT_EXPIRED)
Damien Miller120a1ec2018-07-10 19:39:52 +1000565 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, buffer);
Darren Tucker01558b72016-07-18 09:33:25 +1000566 else if (sshpam_maxtries_reached)
Damien Miller120a1ec2018-07-10 19:39:52 +1000567 ssh_msg_send(ctxt->pam_csock, PAM_MAXTRIES, buffer);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000568 else
Damien Miller120a1ec2018-07-10 19:39:52 +1000569 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, buffer);
570 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000571 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100572
Damien Miller4f9f42a2003-05-10 19:28:02 +1000573 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000574}
575
Darren Tucker8846a072003-10-07 11:30:15 +1000576void
577sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000578{
Darren Tucker8846a072003-10-07 11:30:15 +1000579 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000580
Darren Tuckera8df9242004-01-15 00:15:07 +1100581 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000582 if (ctxt != NULL && ctxt->pam_thread != 0) {
583 pthread_cancel(ctxt->pam_thread);
584 pthread_join(ctxt->pam_thread, NULL);
585 close(ctxt->pam_psock);
586 close(ctxt->pam_csock);
587 memset(ctxt, 0, sizeof(*ctxt));
588 cleanup_ctxt = NULL;
589 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000590}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000591
Damien Miller4f9f42a2003-05-10 19:28:02 +1000592static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000593sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000594 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000595{
Darren Tuckerba53b832004-02-17 20:46:59 +1100596 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000597 return (PAM_CONV_ERR);
598}
Damien Miller63dc3e92001-02-07 12:58:33 +1100599
Damien Miller4f9f42a2003-05-10 19:28:02 +1000600static struct pam_conv null_conv = { sshpam_null_conv, NULL };
601
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000602static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000603sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000604 struct pam_response **resp, void *data)
605{
606 struct pam_response *reply;
Damien Miller120a1ec2018-07-10 19:39:52 +1000607 int r, i;
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000608
609 debug3("PAM: %s called with %d messages", __func__, n);
610 *resp = NULL;
611
612 if (n <= 0 || n > PAM_MAX_NUM_MSG)
613 return (PAM_CONV_ERR);
614
Darren Tuckerd8093e42006-05-04 16:24:34 +1000615 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000616 return (PAM_CONV_ERR);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000617
618 for (i = 0; i < n; ++i) {
619 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
620 case PAM_ERROR_MSG:
621 case PAM_TEXT_INFO:
Damien Miller120a1ec2018-07-10 19:39:52 +1000622 if ((r = sshbuf_putf(loginmsg, "%s\n",
623 PAM_MSG_MEMBER(msg, i, msg))) != 0)
624 fatal("%s: buffer error: %s",
625 __func__, ssh_err(r));
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000626 reply[i].resp_retcode = PAM_SUCCESS;
627 break;
628 default:
629 goto fail;
630 }
631 }
632 *resp = reply;
633 return (PAM_SUCCESS);
634
635 fail:
636 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000637 free(reply[i].resp);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000638 }
Darren Tuckerf60845f2013-06-02 08:07:31 +1000639 free(reply);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000640 return (PAM_CONV_ERR);
641}
642
643static struct pam_conv store_conv = { sshpam_store_conv, NULL };
644
Darren Tucker8846a072003-10-07 11:30:15 +1000645void
646sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000647{
Darren Tucker52358d62008-03-11 22:58:25 +1100648 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
Damien Miller5c3a5582003-09-23 22:12:38 +1000649 return;
Darren Tucker52358d62008-03-11 22:58:25 +1100650 debug("PAM: cleanup");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000651 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000652 if (sshpam_session_open) {
Darren Tucker52358d62008-03-11 22:58:25 +1100653 debug("PAM: closing session");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000654 pam_close_session(sshpam_handle, PAM_SILENT);
655 sshpam_session_open = 0;
656 }
Darren Tucker622d5c52009-07-12 22:07:21 +1000657 if (sshpam_cred_established) {
658 debug("PAM: deleting credentials");
659 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
660 sshpam_cred_established = 0;
661 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100662 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000663 pam_end(sshpam_handle, sshpam_err);
664 sshpam_handle = NULL;
665}
666
667static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100668sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000669{
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100670 const char *pam_rhost, *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000671 const char **ptr_pam_user = &pam_user;
Damien Miller3ed92182016-03-08 14:01:29 -0800672 struct ssh *ssh = active_state; /* XXX */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000673
674 if (sshpam_handle != NULL) {
675 /* We already have a PAM context; check if the user matches */
676 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000677 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000678 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
679 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000680 pam_end(sshpam_handle, sshpam_err);
681 sshpam_handle = NULL;
682 }
683 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000684 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000685 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000686 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100687
Damien Miller4f9f42a2003-05-10 19:28:02 +1000688 if (sshpam_err != PAM_SUCCESS) {
689 pam_end(sshpam_handle, sshpam_err);
690 sshpam_handle = NULL;
691 return (-1);
692 }
Damien Miller3ed92182016-03-08 14:01:29 -0800693 pam_rhost = auth_get_canonical_hostname(ssh, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000694 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000695 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
696 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000697 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000698 sshpam_handle = NULL;
699 return (-1);
700 }
701#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100702 /*
703 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
704 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000705 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100706 */
Damien Miller25d93422003-05-18 20:45:47 +1000707 debug("PAM: setting PAM_TTY to \"ssh\"");
708 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
709 if (sshpam_err != PAM_SUCCESS) {
710 pam_end(sshpam_handle, sshpam_err);
711 sshpam_handle = NULL;
712 return (-1);
713 }
714#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000715 return (0);
716}
717
Damien Millere8f47452018-04-06 14:11:44 +1000718static void
719expose_authinfo(const char *caller)
720{
721 char *auth_info;
722
723 /*
724 * Expose authentication information to PAM.
725 * The environment variable is versioned. Please increment the
726 * version suffix if the format of session_info changes.
727 */
728 if (sshpam_authctxt->session_info == NULL)
729 auth_info = xstrdup("");
730 else if ((auth_info = sshbuf_dup_string(
731 sshpam_authctxt->session_info)) == NULL)
732 fatal("%s: sshbuf_dup_string failed", __func__);
733
734 debug2("%s: auth information in SSH_AUTH_INFO_0", caller);
735 do_pam_putenv("SSH_AUTH_INFO_0", auth_info);
736 free(auth_info);
737}
738
Damien Miller4f9f42a2003-05-10 19:28:02 +1000739static void *
740sshpam_init_ctx(Authctxt *authctxt)
741{
742 struct pam_ctxt *ctxt;
743 int socks[2];
744
Darren Tuckera8df9242004-01-15 00:15:07 +1100745 debug3("PAM: %s entering", __func__);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000746 /*
747 * Refuse to start if we don't have PAM enabled or do_pam_account
748 * has previously failed.
749 */
750 if (!options.use_pam || sshpam_account_status == 0)
Damien Miller4e448a32003-05-14 15:11:48 +1000751 return NULL;
752
Damien Miller4f9f42a2003-05-10 19:28:02 +1000753 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100754 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000755 error("PAM: initialization failed");
756 return (NULL);
757 }
758
Damien Millere8f47452018-04-06 14:11:44 +1000759 expose_authinfo(__func__);
Darren Tucker29171e92007-05-20 15:20:08 +1000760 ctxt = xcalloc(1, sizeof *ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000761
762 /* Start the authentication thread */
763 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
764 error("PAM: failed create sockets: %s", strerror(errno));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000765 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000766 return (NULL);
767 }
768 ctxt->pam_psock = socks[0];
769 ctxt->pam_csock = socks[1];
770 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
771 error("PAM: failed to start authentication thread: %s",
772 strerror(errno));
773 close(socks[0]);
774 close(socks[1]);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000775 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000776 return (NULL);
777 }
Darren Tucker8846a072003-10-07 11:30:15 +1000778 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000779 return (ctxt);
780}
781
782static int
783sshpam_query(void *ctx, char **name, char **info,
784 u_int *num, char ***prompts, u_int **echo_on)
785{
Damien Miller3ed92182016-03-08 14:01:29 -0800786 struct ssh *ssh = active_state; /* XXX */
Damien Miller120a1ec2018-07-10 19:39:52 +1000787 struct sshbuf *buffer;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000788 struct pam_ctxt *ctxt = ctx;
789 size_t plen;
790 u_char type;
791 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000792 size_t len, mlen;
Damien Miller120a1ec2018-07-10 19:39:52 +1000793 int r;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000794
Darren Tuckera8df9242004-01-15 00:15:07 +1100795 debug3("PAM: %s entering", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000796 if ((buffer = sshbuf_new()) == NULL)
797 fatal("%s: sshbuf_new failed", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000798 *name = xstrdup("");
799 *info = xstrdup("");
800 *prompts = xmalloc(sizeof(char *));
801 **prompts = NULL;
802 plen = 0;
803 *echo_on = xmalloc(sizeof(u_int));
Damien Miller120a1ec2018-07-10 19:39:52 +1000804 while (ssh_msg_recv(ctxt->pam_psock, buffer) == 0) {
805 if ((r = sshbuf_get_u8(buffer, &type)) != 0 ||
806 (r = sshbuf_get_cstring(buffer, &msg, &mlen)) != 0)
807 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000808 switch (type) {
809 case PAM_PROMPT_ECHO_ON:
810 case PAM_PROMPT_ECHO_OFF:
811 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000812 len = plen + mlen + 1;
Darren Tuckerd1680d32015-04-30 09:18:11 +1000813 **prompts = xreallocarray(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000814 strlcpy(**prompts + plen, msg, len - plen);
815 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000816 **echo_on = (type == PAM_PROMPT_ECHO_ON);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000817 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000818 return (0);
819 case PAM_ERROR_MSG:
820 case PAM_TEXT_INFO:
821 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000822 len = plen + mlen + 2;
Darren Tuckerd1680d32015-04-30 09:18:11 +1000823 **prompts = xreallocarray(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000824 strlcpy(**prompts + plen, msg, len - plen);
825 plen += mlen;
826 strlcat(**prompts + plen, "\n", len - plen);
827 plen++;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000828 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000829 break;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000830 case PAM_ACCT_EXPIRED:
Darren Tucker01558b72016-07-18 09:33:25 +1000831 case PAM_MAXTRIES:
832 if (type == PAM_ACCT_EXPIRED)
833 sshpam_account_status = 0;
834 if (type == PAM_MAXTRIES)
835 sshpam_set_maxtries_reached(1);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000836 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000837 case PAM_AUTH_ERR:
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000838 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
Darren Tucker7b1e6952005-09-28 22:33:27 +1000839 if (**prompts != NULL && strlen(**prompts) != 0) {
840 *info = **prompts;
841 **prompts = NULL;
842 *num = 0;
843 **echo_on = 0;
844 ctxt->pam_done = -1;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000845 free(msg);
Darren Tucker7b1e6952005-09-28 22:33:27 +1000846 return 0;
847 }
848 /* FALLTHROUGH */
849 case PAM_SUCCESS:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000850 if (**prompts != NULL) {
851 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100852 debug("PAM: %s", **prompts);
Damien Miller120a1ec2018-07-10 19:39:52 +1000853 if ((r = sshbuf_put(loginmsg, **prompts,
854 strlen(**prompts))) != 0)
855 fatal("%s: buffer error: %s",
856 __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000857 free(**prompts);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000858 **prompts = NULL;
859 }
860 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100861 if (!sshpam_authctxt->valid ||
862 (sshpam_authctxt->pw->pw_uid == 0 &&
863 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100864 fatal("Internal error: PAM auth "
865 "succeeded when it should have "
866 "failed");
Damien Miller120a1ec2018-07-10 19:39:52 +1000867 import_environments(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000868 *num = 0;
869 **echo_on = 0;
870 ctxt->pam_done = 1;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000871 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000872 return (0);
873 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000874 error("PAM: %s for %s%.100s from %.100s", msg,
875 sshpam_authctxt->valid ? "" : "illegal user ",
876 sshpam_authctxt->user,
Damien Miller3ed92182016-03-08 14:01:29 -0800877 auth_get_canonical_hostname(ssh, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000878 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000879 default:
880 *num = 0;
881 **echo_on = 0;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000882 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000883 ctxt->pam_done = -1;
884 return (-1);
885 }
886 }
887 return (-1);
888}
889
Darren Tucker283b97f2016-07-15 13:49:44 +1000890/*
891 * Returns a junk password of identical length to that the user supplied.
892 * Used to mitigate timing attacks against crypt(3)/PAM stacks that
893 * vary processing time in proportion to password length.
894 */
895static char *
896fake_password(const char *wire_password)
897{
898 const char junk[] = "\b\n\r\177INCORRECT";
899 char *ret = NULL;
900 size_t i, l = wire_password != NULL ? strlen(wire_password) : 0;
901
902 if (l >= INT_MAX)
903 fatal("%s: password length too long: %zu", __func__, l);
904
905 ret = malloc(l + 1);
Darren Tuckerbee01672017-03-10 13:40:18 +1100906 if (ret == NULL)
907 return NULL;
Darren Tucker283b97f2016-07-15 13:49:44 +1000908 for (i = 0; i < l; i++)
909 ret[i] = junk[i % (sizeof(junk) - 1)];
910 ret[i] = '\0';
911 return ret;
912}
913
Damien Miller4f9f42a2003-05-10 19:28:02 +1000914/* XXX - see also comment in auth-chall.c:verify_response */
915static int
916sshpam_respond(void *ctx, u_int num, char **resp)
917{
Damien Miller120a1ec2018-07-10 19:39:52 +1000918 struct sshbuf *buffer;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000919 struct pam_ctxt *ctxt = ctx;
Darren Tucker283b97f2016-07-15 13:49:44 +1000920 char *fake;
Damien Miller120a1ec2018-07-10 19:39:52 +1000921 int r;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000922
Darren Tucker1d4ebbf2006-01-29 16:46:13 +1100923 debug2("PAM: %s entering, %u responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000924 switch (ctxt->pam_done) {
925 case 1:
926 sshpam_authenticated = 1;
927 return (0);
928 case 0:
929 break;
930 default:
931 return (-1);
932 }
933 if (num != 1) {
934 error("PAM: expected one response, got %u", num);
935 return (-1);
936 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000937 if ((buffer = sshbuf_new()) == NULL)
938 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100939 if (sshpam_authctxt->valid &&
940 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller120a1ec2018-07-10 19:39:52 +1000941 options.permit_root_login == PERMIT_YES)) {
942 if ((r = sshbuf_put_cstring(buffer, *resp)) != 0)
943 fatal("%s: buffer error: %s", __func__, ssh_err(r));
944 } else {
Darren Tucker283b97f2016-07-15 13:49:44 +1000945 fake = fake_password(*resp);
Damien Miller120a1ec2018-07-10 19:39:52 +1000946 if ((r = sshbuf_put_cstring(buffer, fake)) != 0)
947 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker283b97f2016-07-15 13:49:44 +1000948 free(fake);
949 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000950 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, buffer) == -1) {
951 sshbuf_free(buffer);
Damien Miller9bdba702003-11-17 21:27:55 +1100952 return (-1);
953 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000954 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000955 return (1);
956}
957
958static void
959sshpam_free_ctx(void *ctxtp)
960{
961 struct pam_ctxt *ctxt = ctxtp;
962
Darren Tuckera8df9242004-01-15 00:15:07 +1100963 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000964 sshpam_thread_cleanup();
Darren Tuckerf60845f2013-06-02 08:07:31 +1000965 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000966 /*
967 * We don't call sshpam_cleanup() here because we may need the PAM
968 * handle at a later stage, e.g. when setting up a session. It's
969 * still on the cleanup list, so pam_end() *will* be called before
970 * the server process terminates.
971 */
972}
973
974KbdintDevice sshpam_device = {
975 "pam",
976 sshpam_init_ctx,
977 sshpam_query,
978 sshpam_respond,
979 sshpam_free_ctx
980};
981
982KbdintDevice mm_sshpam_device = {
983 "pam",
984 mm_sshpam_init_ctx,
985 mm_sshpam_query,
986 mm_sshpam_respond,
987 mm_sshpam_free_ctx
988};
989
990/*
991 * This replaces auth-pam.c
992 */
993void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100994start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000995{
Damien Miller9d507da2003-05-14 15:31:12 +1000996 if (!options.use_pam)
997 fatal("PAM: initialisation requested when UsePAM=no");
998
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100999 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001000 fatal("PAM: initialisation failed");
1001}
1002
1003void
1004finish_pam(void)
1005{
Darren Tucker8846a072003-10-07 11:30:15 +10001006 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +10001007}
1008
Damien Miller94bc1e72017-07-28 14:50:59 +10001009
Damien Miller1f499fd2003-08-25 13:08:49 +10001010u_int
1011do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001012{
Darren Tucker77fc29e2004-09-11 23:07:03 +10001013 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +11001014 if (sshpam_account_status != -1)
1015 return (sshpam_account_status);
1016
Damien Miller94bc1e72017-07-28 14:50:59 +10001017 expose_authinfo(__func__);
1018
Damien Miller1f499fd2003-08-25 13:08:49 +10001019 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +10001020 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
1021 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +10001022
Darren Tucker07705c72003-12-18 15:34:31 +11001023 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
1024 sshpam_account_status = 0;
1025 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +10001026 }
1027
Darren Tucker07705c72003-12-18 15:34:31 +11001028 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +10001029 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +11001030
1031 sshpam_account_status = 1;
1032 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001033}
1034
1035void
Damien Miller4f9f42a2003-05-10 19:28:02 +10001036do_pam_setcred(int init)
1037{
1038 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +10001039 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001040 if (sshpam_err != PAM_SUCCESS)
1041 fatal("PAM: failed to set PAM_CONV: %s",
1042 pam_strerror(sshpam_handle, sshpam_err));
1043 if (init) {
1044 debug("PAM: establishing credentials");
1045 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
1046 } else {
1047 debug("PAM: reinitializing credentials");
1048 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
1049 }
1050 if (sshpam_err == PAM_SUCCESS) {
1051 sshpam_cred_established = 1;
1052 return;
1053 }
1054 if (sshpam_authenticated)
1055 fatal("PAM: pam_setcred(): %s",
1056 pam_strerror(sshpam_handle, sshpam_err));
1057 else
1058 debug("PAM: pam_setcred(): %s",
1059 pam_strerror(sshpam_handle, sshpam_err));
1060}
1061
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001063sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +10001064 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001065{
1066 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +10001067 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001068 int i;
1069
Darren Tuckerba53b832004-02-17 20:46:59 +11001070 debug3("PAM: %s called with %d messages", __func__, n);
1071
Damien Miller5c3a5582003-09-23 22:12:38 +10001072 *resp = NULL;
1073
Darren Tucker18df00c2003-11-18 12:42:07 +11001074 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +10001075 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +10001076
Darren Tuckerd8093e42006-05-04 16:24:34 +10001077 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +10001078 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +10001079
Damien Miller4f9f42a2003-05-10 19:28:02 +10001080 for (i = 0; i < n; ++i) {
1081 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1082 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +10001083 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +11001084 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +10001085 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +10001086 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001087 break;
1088 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +11001089 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Darren Tucker22164712007-05-20 15:26:07 +10001090 if (fgets(input, sizeof input, stdin) == NULL)
1091 input[0] = '\0';
Damien Millera6fb77f2004-07-19 09:39:11 +10001092 if ((reply[i].resp = strdup(input)) == NULL)
1093 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +10001094 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001095 break;
1096 case PAM_ERROR_MSG:
1097 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +11001098 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +10001099 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001100 break;
1101 default:
1102 goto fail;
1103 }
1104 }
Damien Miller5c3a5582003-09-23 22:12:38 +10001105 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001106 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +10001107
Damien Miller4f9f42a2003-05-10 19:28:02 +10001108 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +10001109 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001110 free(reply[i].resp);
Damien Miller5c3a5582003-09-23 22:12:38 +10001111 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001112 free(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001113 return (PAM_CONV_ERR);
1114}
1115
Darren Tucker94befab2004-06-03 14:53:12 +10001116static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +11001117
Damien Miller4f9f42a2003-05-10 19:28:02 +10001118/*
1119 * XXX this should be done in the authentication phase, but ssh1 doesn't
1120 * support that
1121 */
1122void
1123do_pam_chauthtok(void)
1124{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001125 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +10001126 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +10001127 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +11001128 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001129 if (sshpam_err != PAM_SUCCESS)
1130 fatal("PAM: failed to set PAM_CONV: %s",
1131 pam_strerror(sshpam_handle, sshpam_err));
1132 debug("PAM: changing password");
1133 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1134 if (sshpam_err != PAM_SUCCESS)
1135 fatal("PAM: pam_chauthtok(): %s",
1136 pam_strerror(sshpam_handle, sshpam_err));
1137}
1138
Darren Tucker18df00c2003-11-18 12:42:07 +11001139void
djm@openbsd.org7c856852018-03-03 03:15:51 +00001140do_pam_session(struct ssh *ssh)
Darren Tucker18df00c2003-11-18 12:42:07 +11001141{
Darren Tucker1921ed92004-02-10 13:23:28 +11001142 debug3("PAM: opening session");
Damien Miller94bc1e72017-07-28 14:50:59 +10001143
1144 expose_authinfo(__func__);
1145
Damien Millera8e06ce2003-11-21 23:48:55 +11001146 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +11001147 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +11001148 if (sshpam_err != PAM_SUCCESS)
1149 fatal("PAM: failed to set PAM_CONV: %s",
1150 pam_strerror(sshpam_handle, sshpam_err));
1151 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +10001152 if (sshpam_err == PAM_SUCCESS)
1153 sshpam_session_open = 1;
1154 else {
1155 sshpam_session_open = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001156 auth_restrict_session(ssh);
Darren Tucker69687f42004-09-11 22:17:26 +10001157 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001158 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001159 }
1160
1161}
1162
1163int
1164is_pam_session_open(void)
1165{
1166 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001167}
1168
Damien Millera8e06ce2003-11-21 23:48:55 +11001169/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001170 * Set a PAM environment string. We need to do this so that the session
1171 * modules can handle things like Kerberos/GSI credentials that appear
1172 * during the ssh authentication process.
1173 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001174int
Damien Millera8e06ce2003-11-21 23:48:55 +11001175do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001176{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001177 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +11001178#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +10001179 char *compound;
1180 size_t len;
1181
1182 len = strlen(name) + strlen(value) + 2;
1183 compound = xmalloc(len);
1184
1185 snprintf(compound, len, "%s=%s", name, value);
1186 ret = pam_putenv(sshpam_handle, compound);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001187 free(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001188#endif
Damien Millerf2728092003-09-17 07:24:25 +10001189
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001190 return (ret);
1191}
1192
Damien Miller4f9f42a2003-05-10 19:28:02 +10001193char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001194fetch_pam_child_environment(void)
1195{
1196 return sshpam_env;
1197}
1198
1199char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001200fetch_pam_environment(void)
1201{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001202 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001203}
1204
1205void
1206free_pam_environment(char **env)
1207{
1208 char **envp;
1209
Damien Millere27c6cc2003-05-16 18:21:01 +10001210 if (env == NULL)
1211 return;
1212
Damien Miller4f9f42a2003-05-10 19:28:02 +10001213 for (envp = env; *envp; envp++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001214 free(*envp);
1215 free(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001216}
1217
Darren Tucker450a1582004-05-30 20:43:59 +10001218/*
1219 * "Blind" conversation function for password authentication. Assumes that
1220 * echo-off prompts are for the password and stores messages for later
1221 * display.
1222 */
1223static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001224sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001225 struct pam_response **resp, void *data)
1226{
1227 struct pam_response *reply;
Damien Miller120a1ec2018-07-10 19:39:52 +10001228 int r, i;
Darren Tucker450a1582004-05-30 20:43:59 +10001229 size_t len;
1230
1231 debug3("PAM: %s called with %d messages", __func__, n);
1232
1233 *resp = NULL;
1234
1235 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1236 return (PAM_CONV_ERR);
1237
Darren Tucker29171e92007-05-20 15:20:08 +10001238 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker450a1582004-05-30 20:43:59 +10001239 return (PAM_CONV_ERR);
Darren Tucker450a1582004-05-30 20:43:59 +10001240
1241 for (i = 0; i < n; ++i) {
1242 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1243 case PAM_PROMPT_ECHO_OFF:
1244 if (sshpam_password == NULL)
1245 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001246 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1247 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001248 reply[i].resp_retcode = PAM_SUCCESS;
1249 break;
1250 case PAM_ERROR_MSG:
1251 case PAM_TEXT_INFO:
1252 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1253 if (len > 0) {
Damien Miller120a1ec2018-07-10 19:39:52 +10001254 if ((r = sshbuf_putf(loginmsg, "%s\n",
1255 PAM_MSG_MEMBER(msg, i, msg))) != 0)
1256 fatal("%s: buffer error: %s",
1257 __func__, ssh_err(r));
Darren Tucker450a1582004-05-30 20:43:59 +10001258 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001259 if ((reply[i].resp = strdup("")) == NULL)
1260 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001261 reply[i].resp_retcode = PAM_SUCCESS;
1262 break;
1263 default:
1264 goto fail;
1265 }
1266 }
1267 *resp = reply;
1268 return (PAM_SUCCESS);
1269
Damien Miller94cf4c82005-07-17 17:04:47 +10001270 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001271 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001272 free(reply[i].resp);
Darren Tucker450a1582004-05-30 20:43:59 +10001273 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001274 free(reply);
Darren Tucker450a1582004-05-30 20:43:59 +10001275 return (PAM_CONV_ERR);
1276}
1277
1278static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1279
1280/*
1281 * Attempt password authentication via PAM
1282 */
1283int
1284sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1285{
1286 int flags = (options.permit_empty_passwd == 0 ?
1287 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker283b97f2016-07-15 13:49:44 +10001288 char *fake = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +10001289
1290 if (!options.use_pam || sshpam_handle == NULL)
1291 fatal("PAM: %s called when PAM disabled or failed to "
1292 "initialise.", __func__);
1293
1294 sshpam_password = password;
1295 sshpam_authctxt = authctxt;
1296
Darren Tuckere061b152004-05-30 22:04:56 +10001297 /*
1298 * If the user logging in is invalid, or is root but is not permitted
1299 * by PermitRootLogin, use an invalid password to prevent leaking
1300 * information via timing (eg if the PAM config has a delay on fail).
1301 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001302 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001303 options.permit_root_login != PERMIT_YES))
Darren Tucker283b97f2016-07-15 13:49:44 +10001304 sshpam_password = fake = fake_password(password);
Darren Tuckere061b152004-05-30 22:04:56 +10001305
Darren Tucker450a1582004-05-30 20:43:59 +10001306 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1307 (const void *)&passwd_conv);
1308 if (sshpam_err != PAM_SUCCESS)
1309 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1310 pam_strerror(sshpam_handle, sshpam_err));
1311
1312 sshpam_err = pam_authenticate(sshpam_handle, flags);
1313 sshpam_password = NULL;
Darren Tucker283b97f2016-07-15 13:49:44 +10001314 free(fake);
Darren Tucker01558b72016-07-18 09:33:25 +10001315 if (sshpam_err == PAM_MAXTRIES)
1316 sshpam_set_maxtries_reached(1);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001317 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001318 debug("PAM: password authentication accepted for %.100s",
1319 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001320 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001321 } else {
1322 debug("PAM: password authentication failed for %.100s: %s",
1323 authctxt->valid ? authctxt->user : "an illegal user",
1324 pam_strerror(sshpam_handle, sshpam_err));
1325 return 0;
1326 }
1327}
Darren Tucker01558b72016-07-18 09:33:25 +10001328
1329int
1330sshpam_get_maxtries_reached(void)
1331{
1332 return sshpam_maxtries_reached;
1333}
1334
1335void
1336sshpam_set_maxtries_reached(int reached)
1337{
1338 if (reached == 0 || sshpam_maxtries_reached)
1339 return;
1340 sshpam_maxtries_reached = 1;
1341 options.password_authentication = 0;
1342 options.kbd_interactive_authentication = 0;
1343 options.challenge_response_authentication = 0;
1344}
Damien Millere72b7af1999-12-30 15:08:44 +11001345#endif /* USE_PAM */