blob: 1dec53e929d92bd7fdceb48e1c6ccdaeaef63fb9 [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;
Damien Miller48f54b92018-09-13 12:13:50 +1000131#define pthread_exit fake_pthread_exit
132#define pthread_create fake_pthread_create
133#define pthread_cancel fake_pthread_cancel
134#define pthread_join fake_pthread_join
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100135#endif
136
137struct pam_ctxt {
138 sp_pthread_t pam_thread;
139 int pam_psock;
140 int pam_csock;
141 int pam_done;
142};
143
144static void sshpam_free_ctx(void *);
145static struct pam_ctxt *cleanup_ctxt;
146
Darren Tucker328118a2005-05-25 16:18:09 +1000147#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000148/*
149 * Simulate threads with processes.
150 */
Kevin Steves63007d42002-07-21 17:57:01 +0000151
Darren Tucker749bc952004-01-14 22:14:04 +1100152static int sshpam_thread_status = -1;
153static mysig_t sshpam_oldsig;
154
Damien Miller94cf4c82005-07-17 17:04:47 +1000155static void
Darren Tucker749bc952004-01-14 22:14:04 +1100156sshpam_sigchld_handler(int sig)
157{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000158 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100159 if (cleanup_ctxt == NULL)
160 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000161 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000162 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000163 /* PAM thread has not exitted, privsep slave must have */
164 kill(cleanup_ctxt->pam_thread, SIGTERM);
Damien Miller10358ab2016-07-22 14:06:36 +1000165 while (waitpid(cleanup_ctxt->pam_thread,
166 &sshpam_thread_status, 0) == -1) {
167 if (errno == EINTR)
168 continue;
169 return;
170 }
Darren Tuckerb53355e2004-05-24 11:55:36 +1000171 }
Darren Tucker749bc952004-01-14 22:14:04 +1100172 if (WIFSIGNALED(sshpam_thread_status) &&
173 WTERMSIG(sshpam_thread_status) == SIGTERM)
174 return; /* terminated by pthread_cancel */
175 if (!WIFEXITED(sshpam_thread_status))
Darren Tucker57d4ca92007-08-10 14:32:34 +1000176 sigdie("PAM: authentication thread exited unexpectedly");
Darren Tucker749bc952004-01-14 22:14:04 +1100177 if (WEXITSTATUS(sshpam_thread_status) != 0)
Darren Tucker57d4ca92007-08-10 14:32:34 +1000178 sigdie("PAM: authentication thread exited uncleanly");
Darren Tucker749bc952004-01-14 22:14:04 +1100179}
180
Damien Millerb8fe89c2006-07-24 14:51:00 +1000181/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000182static void
Damien Millerb8fe89c2006-07-24 14:51:00 +1000183pthread_exit(void *value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000184{
185 _exit(0);
186}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000187
Damien Millerb8fe89c2006-07-24 14:51:00 +1000188/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000189static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000190pthread_create(sp_pthread_t *thread, const void *attr,
Damien Miller4f9f42a2003-05-10 19:28:02 +1000191 void *(*thread_start)(void *), void *arg)
192{
193 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000194 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100195
Darren Tuckerb9b60212004-03-04 20:03:54 +1100196 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000197 switch ((pid = fork())) {
198 case -1:
199 error("fork(): %s", strerror(errno));
200 return (-1);
201 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000202 close(ctx->pam_psock);
203 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000204 thread_start(arg);
205 _exit(1);
206 default:
207 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000208 close(ctx->pam_csock);
209 ctx->pam_csock = -1;
Darren Tucker749bc952004-01-14 22:14:04 +1100210 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000211 return (0);
212 }
213}
Damien Millere72b7af1999-12-30 15:08:44 +1100214
Damien Miller4f9f42a2003-05-10 19:28:02 +1000215static int
216pthread_cancel(sp_pthread_t thread)
217{
Darren Tucker7ae09622004-01-14 23:07:56 +1100218 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000219 return (kill(thread, SIGTERM));
220}
221
Damien Millerb8fe89c2006-07-24 14:51:00 +1000222/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000223static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000224pthread_join(sp_pthread_t thread, void **value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000225{
226 int status;
227
Darren Tucker749bc952004-01-14 22:14:04 +1100228 if (sshpam_thread_status != -1)
229 return (sshpam_thread_status);
230 signal(SIGCHLD, sshpam_oldsig);
Damien Miller10358ab2016-07-22 14:06:36 +1000231 while (waitpid(thread, &status, 0) == -1) {
232 if (errno == EINTR)
233 continue;
234 fatal("%s: waitpid: %s", __func__, strerror(errno));
235 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000236 return (status);
237}
238#endif
239
240
Damien Miller5c3a5582003-09-23 22:12:38 +1000241static pam_handle_t *sshpam_handle = NULL;
242static int sshpam_err = 0;
243static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000244static int sshpam_session_open = 0;
245static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100246static int sshpam_account_status = -1;
Darren Tucker01558b72016-07-18 09:33:25 +1000247static int sshpam_maxtries_reached = 0;
Damien Millerc756e9b2003-11-17 21:41:42 +1100248static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000249static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000250static const char *sshpam_password = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000251
Damien Millerc756e9b2003-11-17 21:41:42 +1100252/* Some PAM implementations don't implement this */
253#ifndef HAVE_PAM_GETENVLIST
254static char **
255pam_getenvlist(pam_handle_t *pamh)
256{
257 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100258 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100259 * for platforms without pam_getenvlist by searching for known
260 * env vars (e.g. KRB5CCNAME) from the PAM environment.
261 */
262 return NULL;
263}
264#endif
265
Darren Tucker21dd0892004-08-16 23:12:05 +1000266/*
267 * Some platforms, notably Solaris, do not enforce password complexity
268 * rules during pam_chauthtok() if the real uid of the calling process
269 * is 0, on the assumption that it's being called by "passwd" run by root.
270 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
271 * the right thing.
272 */
273#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
274static int
275sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
276{
277 int result;
278
279 if (sshpam_authctxt == NULL)
280 fatal("PAM: sshpam_authctxt not initialized");
281 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
282 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
283 result = pam_chauthtok(pamh, flags);
284 if (setreuid(0, -1) == -1)
285 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
286 return result;
287}
288# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
289#endif
290
Darren Tucker07705c72003-12-18 15:34:31 +1100291void
Darren Tucker17db1c42004-06-19 12:54:38 +1000292sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100293{
Darren Tucker13ef4cf2018-03-03 16:21:20 +1100294 extern struct sshauthopt *auth_opts;
295 static int saved_port, saved_agent, saved_x11;
296
Darren Tuckera8df9242004-01-15 00:15:07 +1100297 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000298 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100299 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000300 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100301 if (reqd) {
Darren Tucker13ef4cf2018-03-03 16:21:20 +1100302 saved_port = auth_opts->permit_port_forwarding_flag;
303 saved_agent = auth_opts->permit_agent_forwarding_flag;
304 saved_x11 = auth_opts->permit_x11_forwarding_flag;
305 auth_opts->permit_port_forwarding_flag = 0;
306 auth_opts->permit_agent_forwarding_flag = 0;
307 auth_opts->permit_x11_forwarding_flag = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100308 } else {
Darren Tucker13ef4cf2018-03-03 16:21:20 +1100309 if (saved_port)
310 auth_opts->permit_port_forwarding_flag = saved_port;
311 if (saved_agent)
312 auth_opts->permit_agent_forwarding_flag = saved_agent;
313 if (saved_x11)
314 auth_opts->permit_x11_forwarding_flag = saved_x11;
Darren Tucker07705c72003-12-18 15:34:31 +1100315 }
316}
Darren Tucker1921ed92004-02-10 13:23:28 +1100317
Damien Millerc756e9b2003-11-17 21:41:42 +1100318/* Import regular and PAM environment from subprocess */
319static void
Damien Miller120a1ec2018-07-10 19:39:52 +1000320import_environments(struct sshbuf *b)
Damien Millerc756e9b2003-11-17 21:41:42 +1100321{
322 char *env;
Damien Miller120a1ec2018-07-10 19:39:52 +1000323 u_int n, i, num_env;
324 int r;
Damien Millerc756e9b2003-11-17 21:41:42 +1100325
Darren Tuckera8df9242004-01-15 00:15:07 +1100326 debug3("PAM: %s entering", __func__);
327
Darren Tucker328118a2005-05-25 16:18:09 +1000328#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100329 /* Import variables set by do_pam_account */
Damien Miller120a1ec2018-07-10 19:39:52 +1000330 if ((r = sshbuf_get_u32(b, &n)) != 0)
331 fatal("%s: buffer error: %s", __func__, ssh_err(r));
332 if (n > INT_MAX)
333 fatal("%s: invalid PAM account status %u", __func__, n);
334 sshpam_account_status = (int)n;
335 if ((r = sshbuf_get_u32(b, &n)) != 0)
336 fatal("%s: buffer error: %s", __func__, ssh_err(r));
337 sshpam_password_change_required(n != 0);
Darren Tucker07705c72003-12-18 15:34:31 +1100338
Damien Millerc756e9b2003-11-17 21:41:42 +1100339 /* Import environment from subprocess */
Damien Miller120a1ec2018-07-10 19:39:52 +1000340 if ((r = sshbuf_get_u32(b, &num_env)) != 0)
341 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerd8093e42006-05-04 16:24:34 +1000342 if (num_env > 1024)
343 fatal("%s: received %u environment variables, expected <= 1024",
344 __func__, num_env);
345 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
Damien Millerc756e9b2003-11-17 21:41:42 +1100346 debug3("PAM: num env strings %d", num_env);
Damien Miller120a1ec2018-07-10 19:39:52 +1000347 for(i = 0; i < num_env; i++) {
348 if ((r = sshbuf_get_cstring(b, &(sshpam_env[i]), NULL)) != 0)
349 fatal("%s: buffer error: %s", __func__, ssh_err(r));
350 }
Damien Millerc756e9b2003-11-17 21:41:42 +1100351 sshpam_env[num_env] = NULL;
352
353 /* Import PAM environment from subprocess */
Damien Miller120a1ec2018-07-10 19:39:52 +1000354 if ((r = sshbuf_get_u32(b, &num_env)) != 0)
355 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100356 debug("PAM: num PAM env strings %d", num_env);
Damien Miller120a1ec2018-07-10 19:39:52 +1000357 for (i = 0; i < num_env; i++) {
358 if ((r = sshbuf_get_cstring(b, &env, NULL)) != 0)
359 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker8a1624c2003-11-18 12:45:35 +1100360#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100361 /* Errors are not fatal here */
Damien Miller120a1ec2018-07-10 19:39:52 +1000362 if ((r = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100363 error("PAM: pam_putenv: %s",
Damien Miller120a1ec2018-07-10 19:39:52 +1000364 pam_strerror(sshpam_handle, r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100365 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100366#endif
Damien Miller120a1ec2018-07-10 19:39:52 +1000367 /* XXX leak env? */
Damien Millerc756e9b2003-11-17 21:41:42 +1100368 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100369#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100370}
371
Damien Miller9d5705a2000-09-16 16:09:27 +1100372/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000373 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100374 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000375static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000376sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000377 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100378{
Damien Miller120a1ec2018-07-10 19:39:52 +1000379 struct sshbuf *buffer;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000380 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000381 struct pam_response *reply;
Damien Miller120a1ec2018-07-10 19:39:52 +1000382 int r, i;
383 u_char status;
Kevin Steves38b050a2002-07-23 00:44:07 +0000384
Darren Tuckerba53b832004-02-17 20:46:59 +1100385 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000386 *resp = NULL;
387
Darren Tucker59e06022004-06-30 20:34:31 +1000388 if (data == NULL) {
389 error("PAM: conversation function passed a null context");
390 return (PAM_CONV_ERR);
391 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000392 ctxt = data;
393 if (n <= 0 || n > PAM_MAX_NUM_MSG)
394 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000395
Darren Tuckerd8093e42006-05-04 16:24:34 +1000396 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller120a1ec2018-07-10 19:39:52 +1000397 return PAM_CONV_ERR;
398 if ((buffer = sshbuf_new()) == NULL) {
399 free(reply);
400 return PAM_CONV_ERR;
401 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000402
Damien Miller4f9f42a2003-05-10 19:28:02 +1000403 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000404 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
405 case PAM_PROMPT_ECHO_OFF:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000406 case PAM_PROMPT_ECHO_ON:
Damien Miller120a1ec2018-07-10 19:39:52 +1000407 if ((r = sshbuf_put_cstring(buffer,
408 PAM_MSG_MEMBER(msg, i, msg))) != 0)
409 fatal("%s: buffer error: %s",
410 __func__, ssh_err(r));
Damien Millera8e06ce2003-11-21 23:48:55 +1100411 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller120a1ec2018-07-10 19:39:52 +1000412 PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100413 goto fail;
Damien Miller120a1ec2018-07-10 19:39:52 +1000414
415 if (ssh_msg_recv(ctxt->pam_csock, buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100416 goto fail;
Damien Miller120a1ec2018-07-10 19:39:52 +1000417 if ((r = sshbuf_get_u8(buffer, &status)) != 0)
418 fatal("%s: buffer error: %s",
419 __func__, ssh_err(r));
420 if (status != PAM_AUTHTOK)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000421 goto fail;
Damien Miller120a1ec2018-07-10 19:39:52 +1000422 if ((r = sshbuf_get_cstring(buffer,
423 &reply[i].resp, NULL)) != 0)
424 fatal("%s: buffer error: %s",
425 __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000426 break;
427 case PAM_ERROR_MSG:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000428 case PAM_TEXT_INFO:
Damien Miller120a1ec2018-07-10 19:39:52 +1000429 if ((r = sshbuf_put_cstring(buffer,
430 PAM_MSG_MEMBER(msg, i, msg))) != 0)
431 fatal("%s: buffer error: %s",
432 __func__, ssh_err(r));
Damien Millera8e06ce2003-11-21 23:48:55 +1100433 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller120a1ec2018-07-10 19:39:52 +1000434 PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100435 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000436 break;
437 default:
438 goto fail;
439 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000440 sshbuf_reset(buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000441 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000442 sshbuf_free(buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000443 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000444 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000445
Damien Miller4f9f42a2003-05-10 19:28:02 +1000446 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000447 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000448 free(reply[i].resp);
Damien Miller5c3a5582003-09-23 22:12:38 +1000449 }
Darren Tuckerf60845f2013-06-02 08:07:31 +1000450 free(reply);
Damien Miller120a1ec2018-07-10 19:39:52 +1000451 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000452 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000453}
454
Damien Miller4f9f42a2003-05-10 19:28:02 +1000455/*
456 * Authentication thread.
457 */
458static void *
459sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100460{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000461 struct pam_ctxt *ctxt = ctxtp;
Damien Miller120a1ec2018-07-10 19:39:52 +1000462 struct sshbuf *buffer = NULL;
Damien Millerf4b6f102003-09-02 23:12:06 +1000463 struct pam_conv sshpam_conv;
Damien Miller120a1ec2018-07-10 19:39:52 +1000464 int r, flags = (options.permit_empty_passwd == 0 ?
Darren Tucker1f7e4082004-07-01 14:00:14 +1000465 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000466#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100467 extern char **environ;
468 char **env_from_pam;
469 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000470 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000471 const char **ptr_pam_user = &pam_user;
Darren Tucker54e1b222006-09-17 11:57:46 +1000472 char *tz = getenv("TZ");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000473
Darren Tucker53f8e782013-12-19 11:31:44 +1100474 sshpam_err = pam_get_item(sshpam_handle, PAM_USER,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000475 (sshpam_const void **)ptr_pam_user);
Darren Tucker53f8e782013-12-19 11:31:44 +1100476 if (sshpam_err != PAM_SUCCESS)
477 goto auth_fail;
Darren Tucker54e1b222006-09-17 11:57:46 +1000478
Damien Millerc756e9b2003-11-17 21:41:42 +1100479 environ[0] = NULL;
Darren Tucker54e1b222006-09-17 11:57:46 +1000480 if (tz != NULL)
481 if (setenv("TZ", tz, 1) == -1)
482 error("PAM: could not set TZ environment: %s",
483 strerror(errno));
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000484
485 if (sshpam_authctxt != NULL) {
486 setproctitle("%s [pam]",
487 sshpam_authctxt->valid ? pam_user : "unknown");
488 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000489#endif
490
Damien Millerf4b6f102003-09-02 23:12:06 +1000491 sshpam_conv.conv = sshpam_thread_conv;
492 sshpam_conv.appdata_ptr = ctxt;
493
Darren Tucker17addf02004-03-30 20:57:57 +1000494 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100495 fatal("%s: PAM authctxt not initialized", __func__);
496
Damien Miller120a1ec2018-07-10 19:39:52 +1000497 if ((buffer = sshbuf_new()) == NULL)
498 fatal("%s: sshbuf_new failed", __func__);
499
Damien Miller4f9f42a2003-05-10 19:28:02 +1000500 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
501 (const void *)&sshpam_conv);
502 if (sshpam_err != PAM_SUCCESS)
503 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000504 sshpam_err = pam_authenticate(sshpam_handle, flags);
Darren Tucker01558b72016-07-18 09:33:25 +1000505 if (sshpam_err == PAM_MAXTRIES)
506 sshpam_set_maxtries_reached(1);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000507 if (sshpam_err != PAM_SUCCESS)
508 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100509
Darren Tucker608ec1f2017-03-29 09:50:54 +1100510 if (!do_pam_account()) {
511 sshpam_err = PAM_ACCT_EXPIRED;
512 goto auth_fail;
513 }
514 if (sshpam_authctxt->force_pwchange) {
515 sshpam_err = pam_chauthtok(sshpam_handle,
516 PAM_CHANGE_EXPIRED_AUTHTOK);
517 if (sshpam_err != PAM_SUCCESS)
Darren Tucker07705c72003-12-18 15:34:31 +1100518 goto auth_fail;
Darren Tucker608ec1f2017-03-29 09:50:54 +1100519 sshpam_password_change_required(0);
Darren Tuckerc376c862003-12-18 16:08:59 +1100520 }
Darren Tucker07705c72003-12-18 15:34:31 +1100521
Damien Miller120a1ec2018-07-10 19:39:52 +1000522 if ((r = sshbuf_put_cstring(buffer, "OK")) != 0)
523 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerc756e9b2003-11-17 21:41:42 +1100524
Darren Tucker328118a2005-05-25 16:18:09 +1000525#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100526 /* Export variables set by do_pam_account */
Damien Miller120a1ec2018-07-10 19:39:52 +1000527 if ((r = sshbuf_put_u32(buffer, sshpam_account_status)) != 0 ||
528 (r = sshbuf_put_u32(buffer, sshpam_authctxt->force_pwchange)) != 0)
529 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker07705c72003-12-18 15:34:31 +1100530
Damien Millerc756e9b2003-11-17 21:41:42 +1100531 /* Export any environment strings set in child */
Damien Miller120a1ec2018-07-10 19:39:52 +1000532 for (i = 0; environ[i] != NULL; i++) {
533 /* Count */
534 if (i > INT_MAX)
535 fatal("%s: too many enviornment strings", __func__);
536 }
537 if ((r = sshbuf_put_u32(buffer, i)) != 0)
538 fatal("%s: buffer error: %s", __func__, ssh_err(r));
539 for (i = 0; environ[i] != NULL; i++) {
540 if ((r = sshbuf_put_cstring(buffer, environ[i])) != 0)
541 fatal("%s: buffer error: %s", __func__, ssh_err(r));
542 }
Damien Millerc756e9b2003-11-17 21:41:42 +1100543 /* Export any environment strings set by PAM in child */
544 env_from_pam = pam_getenvlist(sshpam_handle);
Damien Miller120a1ec2018-07-10 19:39:52 +1000545 for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) {
546 /* Count */
547 if (i > INT_MAX)
548 fatal("%s: too many PAM enviornment strings", __func__);
549 }
550 if ((r = sshbuf_put_u32(buffer, i)) != 0)
551 fatal("%s: buffer error: %s", __func__, ssh_err(r));
552 for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) {
553 if ((r = sshbuf_put_cstring(buffer, env_from_pam[i])) != 0)
554 fatal("%s: buffer error: %s", __func__, ssh_err(r));
555 }
Darren Tucker328118a2005-05-25 16:18:09 +1000556#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100557
Damien Miller9bdba702003-11-17 21:27:55 +1100558 /* XXX - can't do much about an error here */
Damien Miller120a1ec2018-07-10 19:39:52 +1000559 ssh_msg_send(ctxt->pam_csock, sshpam_err, buffer);
560 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000561 pthread_exit(NULL);
562
563 auth_fail:
Damien Miller120a1ec2018-07-10 19:39:52 +1000564 if ((r = sshbuf_put_cstring(buffer,
565 pam_strerror(sshpam_handle, sshpam_err))) != 0)
566 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller9bdba702003-11-17 21:27:55 +1100567 /* XXX - can't do much about an error here */
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000568 if (sshpam_err == PAM_ACCT_EXPIRED)
Damien Miller120a1ec2018-07-10 19:39:52 +1000569 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, buffer);
Darren Tucker01558b72016-07-18 09:33:25 +1000570 else if (sshpam_maxtries_reached)
Damien Miller120a1ec2018-07-10 19:39:52 +1000571 ssh_msg_send(ctxt->pam_csock, PAM_MAXTRIES, buffer);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000572 else
Damien Miller120a1ec2018-07-10 19:39:52 +1000573 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, buffer);
574 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000575 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100576
Damien Miller4f9f42a2003-05-10 19:28:02 +1000577 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000578}
579
Darren Tucker8846a072003-10-07 11:30:15 +1000580void
581sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000582{
Darren Tucker8846a072003-10-07 11:30:15 +1000583 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000584
Darren Tuckera8df9242004-01-15 00:15:07 +1100585 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000586 if (ctxt != NULL && ctxt->pam_thread != 0) {
587 pthread_cancel(ctxt->pam_thread);
588 pthread_join(ctxt->pam_thread, NULL);
589 close(ctxt->pam_psock);
590 close(ctxt->pam_csock);
591 memset(ctxt, 0, sizeof(*ctxt));
592 cleanup_ctxt = NULL;
593 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000594}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000595
Damien Miller4f9f42a2003-05-10 19:28:02 +1000596static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000597sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000598 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000599{
Darren Tuckerba53b832004-02-17 20:46:59 +1100600 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000601 return (PAM_CONV_ERR);
602}
Damien Miller63dc3e92001-02-07 12:58:33 +1100603
Damien Miller4f9f42a2003-05-10 19:28:02 +1000604static struct pam_conv null_conv = { sshpam_null_conv, NULL };
605
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000606static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000607sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000608 struct pam_response **resp, void *data)
609{
610 struct pam_response *reply;
Damien Miller120a1ec2018-07-10 19:39:52 +1000611 int r, i;
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000612
613 debug3("PAM: %s called with %d messages", __func__, n);
614 *resp = NULL;
615
616 if (n <= 0 || n > PAM_MAX_NUM_MSG)
617 return (PAM_CONV_ERR);
618
Darren Tuckerd8093e42006-05-04 16:24:34 +1000619 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000620 return (PAM_CONV_ERR);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000621
622 for (i = 0; i < n; ++i) {
623 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
624 case PAM_ERROR_MSG:
625 case PAM_TEXT_INFO:
Damien Miller120a1ec2018-07-10 19:39:52 +1000626 if ((r = sshbuf_putf(loginmsg, "%s\n",
627 PAM_MSG_MEMBER(msg, i, msg))) != 0)
628 fatal("%s: buffer error: %s",
629 __func__, ssh_err(r));
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000630 reply[i].resp_retcode = PAM_SUCCESS;
631 break;
632 default:
633 goto fail;
634 }
635 }
636 *resp = reply;
637 return (PAM_SUCCESS);
638
639 fail:
640 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000641 free(reply[i].resp);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000642 }
Darren Tuckerf60845f2013-06-02 08:07:31 +1000643 free(reply);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000644 return (PAM_CONV_ERR);
645}
646
647static struct pam_conv store_conv = { sshpam_store_conv, NULL };
648
Darren Tucker8846a072003-10-07 11:30:15 +1000649void
650sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000651{
Darren Tucker52358d62008-03-11 22:58:25 +1100652 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
Damien Miller5c3a5582003-09-23 22:12:38 +1000653 return;
Darren Tucker52358d62008-03-11 22:58:25 +1100654 debug("PAM: cleanup");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000655 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000656 if (sshpam_session_open) {
Darren Tucker52358d62008-03-11 22:58:25 +1100657 debug("PAM: closing session");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000658 pam_close_session(sshpam_handle, PAM_SILENT);
659 sshpam_session_open = 0;
660 }
Darren Tucker622d5c52009-07-12 22:07:21 +1000661 if (sshpam_cred_established) {
662 debug("PAM: deleting credentials");
663 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
664 sshpam_cred_established = 0;
665 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100666 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000667 pam_end(sshpam_handle, sshpam_err);
668 sshpam_handle = NULL;
669}
670
671static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100672sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000673{
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100674 const char *pam_rhost, *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000675 const char **ptr_pam_user = &pam_user;
Damien Miller3ed92182016-03-08 14:01:29 -0800676 struct ssh *ssh = active_state; /* XXX */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000677
678 if (sshpam_handle != NULL) {
679 /* We already have a PAM context; check if the user matches */
680 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000681 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000682 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
683 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000684 pam_end(sshpam_handle, sshpam_err);
685 sshpam_handle = NULL;
686 }
687 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000688 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000689 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000690 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100691
Damien Miller4f9f42a2003-05-10 19:28:02 +1000692 if (sshpam_err != PAM_SUCCESS) {
693 pam_end(sshpam_handle, sshpam_err);
694 sshpam_handle = NULL;
695 return (-1);
696 }
Damien Miller3ed92182016-03-08 14:01:29 -0800697 pam_rhost = auth_get_canonical_hostname(ssh, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000698 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000699 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
700 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000701 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000702 sshpam_handle = NULL;
703 return (-1);
704 }
705#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100706 /*
707 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
708 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000709 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100710 */
Damien Miller25d93422003-05-18 20:45:47 +1000711 debug("PAM: setting PAM_TTY to \"ssh\"");
712 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
713 if (sshpam_err != PAM_SUCCESS) {
714 pam_end(sshpam_handle, sshpam_err);
715 sshpam_handle = NULL;
716 return (-1);
717 }
718#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000719 return (0);
720}
721
Damien Millere8f47452018-04-06 14:11:44 +1000722static void
723expose_authinfo(const char *caller)
724{
725 char *auth_info;
726
727 /*
728 * Expose authentication information to PAM.
729 * The environment variable is versioned. Please increment the
730 * version suffix if the format of session_info changes.
731 */
732 if (sshpam_authctxt->session_info == NULL)
733 auth_info = xstrdup("");
734 else if ((auth_info = sshbuf_dup_string(
735 sshpam_authctxt->session_info)) == NULL)
736 fatal("%s: sshbuf_dup_string failed", __func__);
737
738 debug2("%s: auth information in SSH_AUTH_INFO_0", caller);
739 do_pam_putenv("SSH_AUTH_INFO_0", auth_info);
740 free(auth_info);
741}
742
Damien Miller4f9f42a2003-05-10 19:28:02 +1000743static void *
744sshpam_init_ctx(Authctxt *authctxt)
745{
746 struct pam_ctxt *ctxt;
747 int socks[2];
748
Darren Tuckera8df9242004-01-15 00:15:07 +1100749 debug3("PAM: %s entering", __func__);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000750 /*
751 * Refuse to start if we don't have PAM enabled or do_pam_account
752 * has previously failed.
753 */
754 if (!options.use_pam || sshpam_account_status == 0)
Damien Miller4e448a32003-05-14 15:11:48 +1000755 return NULL;
756
Damien Miller4f9f42a2003-05-10 19:28:02 +1000757 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100758 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000759 error("PAM: initialization failed");
760 return (NULL);
761 }
762
Damien Millere8f47452018-04-06 14:11:44 +1000763 expose_authinfo(__func__);
Darren Tucker29171e92007-05-20 15:20:08 +1000764 ctxt = xcalloc(1, sizeof *ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000765
766 /* Start the authentication thread */
767 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
768 error("PAM: failed create sockets: %s", strerror(errno));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000769 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000770 return (NULL);
771 }
772 ctxt->pam_psock = socks[0];
773 ctxt->pam_csock = socks[1];
774 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
775 error("PAM: failed to start authentication thread: %s",
776 strerror(errno));
777 close(socks[0]);
778 close(socks[1]);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000779 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000780 return (NULL);
781 }
Darren Tucker8846a072003-10-07 11:30:15 +1000782 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000783 return (ctxt);
784}
785
786static int
787sshpam_query(void *ctx, char **name, char **info,
788 u_int *num, char ***prompts, u_int **echo_on)
789{
Damien Miller3ed92182016-03-08 14:01:29 -0800790 struct ssh *ssh = active_state; /* XXX */
Damien Miller120a1ec2018-07-10 19:39:52 +1000791 struct sshbuf *buffer;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000792 struct pam_ctxt *ctxt = ctx;
793 size_t plen;
794 u_char type;
795 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000796 size_t len, mlen;
Damien Miller120a1ec2018-07-10 19:39:52 +1000797 int r;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000798
Darren Tuckera8df9242004-01-15 00:15:07 +1100799 debug3("PAM: %s entering", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000800 if ((buffer = sshbuf_new()) == NULL)
801 fatal("%s: sshbuf_new failed", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000802 *name = xstrdup("");
803 *info = xstrdup("");
804 *prompts = xmalloc(sizeof(char *));
805 **prompts = NULL;
806 plen = 0;
807 *echo_on = xmalloc(sizeof(u_int));
Damien Miller120a1ec2018-07-10 19:39:52 +1000808 while (ssh_msg_recv(ctxt->pam_psock, buffer) == 0) {
809 if ((r = sshbuf_get_u8(buffer, &type)) != 0 ||
810 (r = sshbuf_get_cstring(buffer, &msg, &mlen)) != 0)
811 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000812 switch (type) {
813 case PAM_PROMPT_ECHO_ON:
814 case PAM_PROMPT_ECHO_OFF:
815 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000816 len = plen + mlen + 1;
Darren Tuckerd1680d32015-04-30 09:18:11 +1000817 **prompts = xreallocarray(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000818 strlcpy(**prompts + plen, msg, len - plen);
819 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000820 **echo_on = (type == PAM_PROMPT_ECHO_ON);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000821 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000822 return (0);
823 case PAM_ERROR_MSG:
824 case PAM_TEXT_INFO:
825 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000826 len = plen + mlen + 2;
Darren Tuckerd1680d32015-04-30 09:18:11 +1000827 **prompts = xreallocarray(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000828 strlcpy(**prompts + plen, msg, len - plen);
829 plen += mlen;
830 strlcat(**prompts + plen, "\n", len - plen);
831 plen++;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000832 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000833 break;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000834 case PAM_ACCT_EXPIRED:
Darren Tucker01558b72016-07-18 09:33:25 +1000835 case PAM_MAXTRIES:
836 if (type == PAM_ACCT_EXPIRED)
837 sshpam_account_status = 0;
838 if (type == PAM_MAXTRIES)
839 sshpam_set_maxtries_reached(1);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000840 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000841 case PAM_AUTH_ERR:
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000842 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
Darren Tucker7b1e6952005-09-28 22:33:27 +1000843 if (**prompts != NULL && strlen(**prompts) != 0) {
844 *info = **prompts;
845 **prompts = NULL;
846 *num = 0;
847 **echo_on = 0;
848 ctxt->pam_done = -1;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000849 free(msg);
Darren Tucker7b1e6952005-09-28 22:33:27 +1000850 return 0;
851 }
852 /* FALLTHROUGH */
853 case PAM_SUCCESS:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000854 if (**prompts != NULL) {
855 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100856 debug("PAM: %s", **prompts);
Damien Miller120a1ec2018-07-10 19:39:52 +1000857 if ((r = sshbuf_put(loginmsg, **prompts,
858 strlen(**prompts))) != 0)
859 fatal("%s: buffer error: %s",
860 __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000861 free(**prompts);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000862 **prompts = NULL;
863 }
864 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100865 if (!sshpam_authctxt->valid ||
866 (sshpam_authctxt->pw->pw_uid == 0 &&
867 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100868 fatal("Internal error: PAM auth "
869 "succeeded when it should have "
870 "failed");
Damien Miller120a1ec2018-07-10 19:39:52 +1000871 import_environments(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000872 *num = 0;
873 **echo_on = 0;
874 ctxt->pam_done = 1;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000875 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000876 return (0);
877 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000878 error("PAM: %s for %s%.100s from %.100s", msg,
879 sshpam_authctxt->valid ? "" : "illegal user ",
880 sshpam_authctxt->user,
Damien Miller3ed92182016-03-08 14:01:29 -0800881 auth_get_canonical_hostname(ssh, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000882 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000883 default:
884 *num = 0;
885 **echo_on = 0;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000886 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000887 ctxt->pam_done = -1;
888 return (-1);
889 }
890 }
891 return (-1);
892}
893
Darren Tucker283b97f2016-07-15 13:49:44 +1000894/*
895 * Returns a junk password of identical length to that the user supplied.
896 * Used to mitigate timing attacks against crypt(3)/PAM stacks that
897 * vary processing time in proportion to password length.
898 */
899static char *
900fake_password(const char *wire_password)
901{
902 const char junk[] = "\b\n\r\177INCORRECT";
903 char *ret = NULL;
904 size_t i, l = wire_password != NULL ? strlen(wire_password) : 0;
905
906 if (l >= INT_MAX)
907 fatal("%s: password length too long: %zu", __func__, l);
908
909 ret = malloc(l + 1);
Darren Tuckerbee01672017-03-10 13:40:18 +1100910 if (ret == NULL)
911 return NULL;
Darren Tucker283b97f2016-07-15 13:49:44 +1000912 for (i = 0; i < l; i++)
913 ret[i] = junk[i % (sizeof(junk) - 1)];
914 ret[i] = '\0';
915 return ret;
916}
917
Damien Miller4f9f42a2003-05-10 19:28:02 +1000918/* XXX - see also comment in auth-chall.c:verify_response */
919static int
920sshpam_respond(void *ctx, u_int num, char **resp)
921{
Damien Miller120a1ec2018-07-10 19:39:52 +1000922 struct sshbuf *buffer;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000923 struct pam_ctxt *ctxt = ctx;
Darren Tucker283b97f2016-07-15 13:49:44 +1000924 char *fake;
Damien Miller120a1ec2018-07-10 19:39:52 +1000925 int r;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000926
Darren Tucker1d4ebbf2006-01-29 16:46:13 +1100927 debug2("PAM: %s entering, %u responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000928 switch (ctxt->pam_done) {
929 case 1:
930 sshpam_authenticated = 1;
931 return (0);
932 case 0:
933 break;
934 default:
935 return (-1);
936 }
937 if (num != 1) {
938 error("PAM: expected one response, got %u", num);
939 return (-1);
940 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000941 if ((buffer = sshbuf_new()) == NULL)
942 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100943 if (sshpam_authctxt->valid &&
944 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller120a1ec2018-07-10 19:39:52 +1000945 options.permit_root_login == PERMIT_YES)) {
946 if ((r = sshbuf_put_cstring(buffer, *resp)) != 0)
947 fatal("%s: buffer error: %s", __func__, ssh_err(r));
948 } else {
Darren Tucker283b97f2016-07-15 13:49:44 +1000949 fake = fake_password(*resp);
Damien Miller120a1ec2018-07-10 19:39:52 +1000950 if ((r = sshbuf_put_cstring(buffer, fake)) != 0)
951 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker283b97f2016-07-15 13:49:44 +1000952 free(fake);
953 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000954 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, buffer) == -1) {
955 sshbuf_free(buffer);
Damien Miller9bdba702003-11-17 21:27:55 +1100956 return (-1);
957 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000958 sshbuf_free(buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000959 return (1);
960}
961
962static void
963sshpam_free_ctx(void *ctxtp)
964{
965 struct pam_ctxt *ctxt = ctxtp;
966
Darren Tuckera8df9242004-01-15 00:15:07 +1100967 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000968 sshpam_thread_cleanup();
Darren Tuckerf60845f2013-06-02 08:07:31 +1000969 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000970 /*
971 * We don't call sshpam_cleanup() here because we may need the PAM
972 * handle at a later stage, e.g. when setting up a session. It's
973 * still on the cleanup list, so pam_end() *will* be called before
974 * the server process terminates.
975 */
976}
977
978KbdintDevice sshpam_device = {
979 "pam",
980 sshpam_init_ctx,
981 sshpam_query,
982 sshpam_respond,
983 sshpam_free_ctx
984};
985
986KbdintDevice mm_sshpam_device = {
987 "pam",
988 mm_sshpam_init_ctx,
989 mm_sshpam_query,
990 mm_sshpam_respond,
991 mm_sshpam_free_ctx
992};
993
994/*
995 * This replaces auth-pam.c
996 */
997void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100998start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000999{
Damien Miller9d507da2003-05-14 15:31:12 +10001000 if (!options.use_pam)
1001 fatal("PAM: initialisation requested when UsePAM=no");
1002
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001003 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001004 fatal("PAM: initialisation failed");
1005}
1006
1007void
1008finish_pam(void)
1009{
Darren Tucker8846a072003-10-07 11:30:15 +10001010 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +10001011}
1012
Damien Miller94bc1e72017-07-28 14:50:59 +10001013
Damien Miller1f499fd2003-08-25 13:08:49 +10001014u_int
1015do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001016{
Darren Tucker77fc29e2004-09-11 23:07:03 +10001017 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +11001018 if (sshpam_account_status != -1)
1019 return (sshpam_account_status);
1020
Damien Miller94bc1e72017-07-28 14:50:59 +10001021 expose_authinfo(__func__);
1022
Damien Miller1f499fd2003-08-25 13:08:49 +10001023 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +10001024 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
1025 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +10001026
Darren Tucker07705c72003-12-18 15:34:31 +11001027 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
1028 sshpam_account_status = 0;
1029 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +10001030 }
1031
Darren Tucker07705c72003-12-18 15:34:31 +11001032 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +10001033 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +11001034
1035 sshpam_account_status = 1;
1036 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001037}
1038
1039void
Damien Miller4f9f42a2003-05-10 19:28:02 +10001040do_pam_setcred(int init)
1041{
1042 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +10001043 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001044 if (sshpam_err != PAM_SUCCESS)
1045 fatal("PAM: failed to set PAM_CONV: %s",
1046 pam_strerror(sshpam_handle, sshpam_err));
1047 if (init) {
1048 debug("PAM: establishing credentials");
1049 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
1050 } else {
1051 debug("PAM: reinitializing credentials");
1052 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
1053 }
1054 if (sshpam_err == PAM_SUCCESS) {
1055 sshpam_cred_established = 1;
1056 return;
1057 }
1058 if (sshpam_authenticated)
1059 fatal("PAM: pam_setcred(): %s",
1060 pam_strerror(sshpam_handle, sshpam_err));
1061 else
1062 debug("PAM: pam_setcred(): %s",
1063 pam_strerror(sshpam_handle, sshpam_err));
1064}
1065
Damien Miller4f9f42a2003-05-10 19:28:02 +10001066static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001067sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +10001068 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001069{
1070 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +10001071 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001072 int i;
1073
Darren Tuckerba53b832004-02-17 20:46:59 +11001074 debug3("PAM: %s called with %d messages", __func__, n);
1075
Damien Miller5c3a5582003-09-23 22:12:38 +10001076 *resp = NULL;
1077
Darren Tucker18df00c2003-11-18 12:42:07 +11001078 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +10001079 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +10001080
Darren Tuckerd8093e42006-05-04 16:24:34 +10001081 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +10001082 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +10001083
Damien Miller4f9f42a2003-05-10 19:28:02 +10001084 for (i = 0; i < n; ++i) {
1085 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1086 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +10001087 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +11001088 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +10001089 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +10001090 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001091 break;
1092 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +11001093 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Darren Tucker22164712007-05-20 15:26:07 +10001094 if (fgets(input, sizeof input, stdin) == NULL)
1095 input[0] = '\0';
Damien Millera6fb77f2004-07-19 09:39:11 +10001096 if ((reply[i].resp = strdup(input)) == NULL)
1097 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +10001098 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001099 break;
1100 case PAM_ERROR_MSG:
1101 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +11001102 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +10001103 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001104 break;
1105 default:
1106 goto fail;
1107 }
1108 }
Damien Miller5c3a5582003-09-23 22:12:38 +10001109 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001110 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +10001111
Damien Miller4f9f42a2003-05-10 19:28:02 +10001112 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +10001113 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001114 free(reply[i].resp);
Damien Miller5c3a5582003-09-23 22:12:38 +10001115 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001116 free(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001117 return (PAM_CONV_ERR);
1118}
1119
Darren Tucker94befab2004-06-03 14:53:12 +10001120static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +11001121
Damien Miller4f9f42a2003-05-10 19:28:02 +10001122/*
1123 * XXX this should be done in the authentication phase, but ssh1 doesn't
1124 * support that
1125 */
1126void
1127do_pam_chauthtok(void)
1128{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001129 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +10001130 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +10001131 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +11001132 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001133 if (sshpam_err != PAM_SUCCESS)
1134 fatal("PAM: failed to set PAM_CONV: %s",
1135 pam_strerror(sshpam_handle, sshpam_err));
1136 debug("PAM: changing password");
1137 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1138 if (sshpam_err != PAM_SUCCESS)
1139 fatal("PAM: pam_chauthtok(): %s",
1140 pam_strerror(sshpam_handle, sshpam_err));
1141}
1142
Darren Tucker18df00c2003-11-18 12:42:07 +11001143void
djm@openbsd.org7c856852018-03-03 03:15:51 +00001144do_pam_session(struct ssh *ssh)
Darren Tucker18df00c2003-11-18 12:42:07 +11001145{
Darren Tucker1921ed92004-02-10 13:23:28 +11001146 debug3("PAM: opening session");
Damien Miller94bc1e72017-07-28 14:50:59 +10001147
1148 expose_authinfo(__func__);
1149
Damien Millera8e06ce2003-11-21 23:48:55 +11001150 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +11001151 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +11001152 if (sshpam_err != PAM_SUCCESS)
1153 fatal("PAM: failed to set PAM_CONV: %s",
1154 pam_strerror(sshpam_handle, sshpam_err));
1155 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +10001156 if (sshpam_err == PAM_SUCCESS)
1157 sshpam_session_open = 1;
1158 else {
1159 sshpam_session_open = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001160 auth_restrict_session(ssh);
Darren Tucker69687f42004-09-11 22:17:26 +10001161 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001162 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001163 }
1164
1165}
1166
1167int
1168is_pam_session_open(void)
1169{
1170 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001171}
1172
Damien Millera8e06ce2003-11-21 23:48:55 +11001173/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001174 * Set a PAM environment string. We need to do this so that the session
1175 * modules can handle things like Kerberos/GSI credentials that appear
1176 * during the ssh authentication process.
1177 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001178int
Damien Millera8e06ce2003-11-21 23:48:55 +11001179do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001180{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001181 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +11001182#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +10001183 char *compound;
1184 size_t len;
1185
1186 len = strlen(name) + strlen(value) + 2;
1187 compound = xmalloc(len);
1188
1189 snprintf(compound, len, "%s=%s", name, value);
1190 ret = pam_putenv(sshpam_handle, compound);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001191 free(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001192#endif
Damien Millerf2728092003-09-17 07:24:25 +10001193
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001194 return (ret);
1195}
1196
Damien Miller4f9f42a2003-05-10 19:28:02 +10001197char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001198fetch_pam_child_environment(void)
1199{
1200 return sshpam_env;
1201}
1202
1203char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001204fetch_pam_environment(void)
1205{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001206 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001207}
1208
1209void
1210free_pam_environment(char **env)
1211{
1212 char **envp;
1213
Damien Millere27c6cc2003-05-16 18:21:01 +10001214 if (env == NULL)
1215 return;
1216
Damien Miller4f9f42a2003-05-10 19:28:02 +10001217 for (envp = env; *envp; envp++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001218 free(*envp);
1219 free(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001220}
1221
Darren Tucker450a1582004-05-30 20:43:59 +10001222/*
1223 * "Blind" conversation function for password authentication. Assumes that
1224 * echo-off prompts are for the password and stores messages for later
1225 * display.
1226 */
1227static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001228sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001229 struct pam_response **resp, void *data)
1230{
1231 struct pam_response *reply;
Damien Miller120a1ec2018-07-10 19:39:52 +10001232 int r, i;
Darren Tucker450a1582004-05-30 20:43:59 +10001233 size_t len;
1234
1235 debug3("PAM: %s called with %d messages", __func__, n);
1236
1237 *resp = NULL;
1238
1239 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1240 return (PAM_CONV_ERR);
1241
Darren Tucker29171e92007-05-20 15:20:08 +10001242 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker450a1582004-05-30 20:43:59 +10001243 return (PAM_CONV_ERR);
Darren Tucker450a1582004-05-30 20:43:59 +10001244
1245 for (i = 0; i < n; ++i) {
1246 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1247 case PAM_PROMPT_ECHO_OFF:
1248 if (sshpam_password == NULL)
1249 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001250 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1251 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001252 reply[i].resp_retcode = PAM_SUCCESS;
1253 break;
1254 case PAM_ERROR_MSG:
1255 case PAM_TEXT_INFO:
1256 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1257 if (len > 0) {
Damien Miller120a1ec2018-07-10 19:39:52 +10001258 if ((r = sshbuf_putf(loginmsg, "%s\n",
1259 PAM_MSG_MEMBER(msg, i, msg))) != 0)
1260 fatal("%s: buffer error: %s",
1261 __func__, ssh_err(r));
Darren Tucker450a1582004-05-30 20:43:59 +10001262 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001263 if ((reply[i].resp = strdup("")) == NULL)
1264 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001265 reply[i].resp_retcode = PAM_SUCCESS;
1266 break;
1267 default:
1268 goto fail;
1269 }
1270 }
1271 *resp = reply;
1272 return (PAM_SUCCESS);
1273
Damien Miller94cf4c82005-07-17 17:04:47 +10001274 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001275 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001276 free(reply[i].resp);
Darren Tucker450a1582004-05-30 20:43:59 +10001277 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001278 free(reply);
Darren Tucker450a1582004-05-30 20:43:59 +10001279 return (PAM_CONV_ERR);
1280}
1281
1282static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1283
1284/*
1285 * Attempt password authentication via PAM
1286 */
1287int
1288sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1289{
1290 int flags = (options.permit_empty_passwd == 0 ?
1291 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker283b97f2016-07-15 13:49:44 +10001292 char *fake = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +10001293
1294 if (!options.use_pam || sshpam_handle == NULL)
1295 fatal("PAM: %s called when PAM disabled or failed to "
1296 "initialise.", __func__);
1297
1298 sshpam_password = password;
1299 sshpam_authctxt = authctxt;
1300
Darren Tuckere061b152004-05-30 22:04:56 +10001301 /*
1302 * If the user logging in is invalid, or is root but is not permitted
1303 * by PermitRootLogin, use an invalid password to prevent leaking
1304 * information via timing (eg if the PAM config has a delay on fail).
1305 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001306 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001307 options.permit_root_login != PERMIT_YES))
Darren Tucker283b97f2016-07-15 13:49:44 +10001308 sshpam_password = fake = fake_password(password);
Darren Tuckere061b152004-05-30 22:04:56 +10001309
Darren Tucker450a1582004-05-30 20:43:59 +10001310 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1311 (const void *)&passwd_conv);
1312 if (sshpam_err != PAM_SUCCESS)
1313 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1314 pam_strerror(sshpam_handle, sshpam_err));
1315
1316 sshpam_err = pam_authenticate(sshpam_handle, flags);
1317 sshpam_password = NULL;
Darren Tucker283b97f2016-07-15 13:49:44 +10001318 free(fake);
Darren Tucker01558b72016-07-18 09:33:25 +10001319 if (sshpam_err == PAM_MAXTRIES)
1320 sshpam_set_maxtries_reached(1);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001321 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001322 debug("PAM: password authentication accepted for %.100s",
1323 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001324 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001325 } else {
1326 debug("PAM: password authentication failed for %.100s: %s",
1327 authctxt->valid ? authctxt->user : "an illegal user",
1328 pam_strerror(sshpam_handle, sshpam_err));
1329 return 0;
1330 }
1331}
Darren Tucker01558b72016-07-18 09:33:25 +10001332
1333int
1334sshpam_get_maxtries_reached(void)
1335{
1336 return sshpam_maxtries_reached;
1337}
1338
1339void
1340sshpam_set_maxtries_reached(int reached)
1341{
1342 if (reached == 0 || sshpam_maxtries_reached)
1343 return;
1344 sshpam_maxtries_reached = 1;
1345 options.password_authentication = 0;
1346 options.kbd_interactive_authentication = 0;
1347 options.challenge_response_authentication = 0;
1348}
Damien Millere72b7af1999-12-30 15:08:44 +11001349#endif /* USE_PAM */