blob: 493993a10d1c6699dd3709c0b44f7386016640c4 [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 Miller25d93422003-05-18 20:45:47 +100048/* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
Damien Millere72b7af1999-12-30 15:08:44 +110049#include "includes.h"
Damien Miller6645e7a2006-03-15 14:42:54 +110050
51#include <sys/types.h>
52#include <sys/stat.h>
53#include <sys/wait.h>
Darren Tucker341dae52006-07-13 08:45:14 +100054
55#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110056#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100057#include <stdarg.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100058#include <string.h>
59#include <unistd.h>
Damien Millere72b7af1999-12-30 15:08:44 +110060
61#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110062#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100063#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110064#elif defined (HAVE_PAM_PAM_APPL_H)
65#include <pam/pam_appl.h>
66#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100067
Darren Tuckerf08bdb52005-05-26 19:59:48 +100068/* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
69#ifdef PAM_SUN_CODEBASE
70# define sshpam_const /* Solaris, HP-UX, AIX */
71#else
72# define sshpam_const const /* LinuxPAM, OpenPAM */
73#endif
74
Damien Miller2ab323e2006-08-05 12:43:32 +100075/* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
76#ifdef PAM_SUN_CODEBASE
77# define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
78#else
79# define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
80#endif
81
Damien Miller75bb6642006-08-05 14:07:20 +100082#include "xmalloc.h"
83#include "buffer.h"
84#include "key.h"
85#include "hostfile.h"
Kevin Stevese683e762002-04-04 19:02:28 +000086#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110087#include "auth-pam.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000088#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100089#include "log.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100090#include "msg.h"
91#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100092#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100093#include "servconf.h"
94#include "ssh2.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100095#include "auth-options.h"
Damien Miller75bb6642006-08-05 14:07:20 +100096#ifdef GSSAPI
97#include "ssh-gss.h"
98#endif
99#include "monitor_wrap.h"
Damien Millere72b7af1999-12-30 15:08:44 +1100100
Damien Miller4e448a32003-05-14 15:11:48 +1000101extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +1100102extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +1100103extern int compat20;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000104extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +1000105
Darren Tucker328118a2005-05-25 16:18:09 +1000106/* so we don't silently change behaviour */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000107#ifdef USE_POSIX_THREADS
Darren Tucker328118a2005-05-25 16:18:09 +1000108# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
109#endif
110
111/*
112 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
113 * and generally a bad idea. Use at own risk and do not expect support if
114 * this breaks.
115 */
116#ifdef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000117#include <pthread.h>
118/*
Damien Millera8e06ce2003-11-21 23:48:55 +1100119 * Avoid namespace clash when *not* using pthreads for systems *with*
120 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +1000121 * (e.g. Linux)
122 */
Damien Millera8e06ce2003-11-21 23:48:55 +1100123typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000124#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100125typedef pid_t sp_pthread_t;
126#endif
127
128struct pam_ctxt {
129 sp_pthread_t pam_thread;
130 int pam_psock;
131 int pam_csock;
132 int pam_done;
133};
134
135static void sshpam_free_ctx(void *);
136static struct pam_ctxt *cleanup_ctxt;
137
Darren Tucker328118a2005-05-25 16:18:09 +1000138#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000139/*
140 * Simulate threads with processes.
141 */
Kevin Steves63007d42002-07-21 17:57:01 +0000142
Darren Tucker749bc952004-01-14 22:14:04 +1100143static int sshpam_thread_status = -1;
144static mysig_t sshpam_oldsig;
145
Damien Miller94cf4c82005-07-17 17:04:47 +1000146static void
Darren Tucker749bc952004-01-14 22:14:04 +1100147sshpam_sigchld_handler(int sig)
148{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000149 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100150 if (cleanup_ctxt == NULL)
151 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000152 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000153 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000154 /* PAM thread has not exitted, privsep slave must have */
155 kill(cleanup_ctxt->pam_thread, SIGTERM);
156 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
Darren Tucker5d423f42004-07-11 16:54:08 +1000157 <= 0)
Darren Tuckerb53355e2004-05-24 11:55:36 +1000158 return; /* could not wait */
159 }
Darren Tucker749bc952004-01-14 22:14:04 +1100160 if (WIFSIGNALED(sshpam_thread_status) &&
161 WTERMSIG(sshpam_thread_status) == SIGTERM)
162 return; /* terminated by pthread_cancel */
163 if (!WIFEXITED(sshpam_thread_status))
164 fatal("PAM: authentication thread exited unexpectedly");
165 if (WEXITSTATUS(sshpam_thread_status) != 0)
166 fatal("PAM: authentication thread exited uncleanly");
167}
168
Damien Millerb8fe89c2006-07-24 14:51:00 +1000169/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000170static void
Damien Millerb8fe89c2006-07-24 14:51:00 +1000171pthread_exit(void *value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000172{
173 _exit(0);
174}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000175
Damien Millerb8fe89c2006-07-24 14:51:00 +1000176/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000177static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000178pthread_create(sp_pthread_t *thread, const void *attr,
Damien Miller4f9f42a2003-05-10 19:28:02 +1000179 void *(*thread_start)(void *), void *arg)
180{
181 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000182 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100183
Darren Tuckerb9b60212004-03-04 20:03:54 +1100184 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000185 switch ((pid = fork())) {
186 case -1:
187 error("fork(): %s", strerror(errno));
188 return (-1);
189 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000190 close(ctx->pam_psock);
191 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000192 thread_start(arg);
193 _exit(1);
194 default:
195 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000196 close(ctx->pam_csock);
197 ctx->pam_csock = -1;
Darren Tucker749bc952004-01-14 22:14:04 +1100198 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000199 return (0);
200 }
201}
Damien Millere72b7af1999-12-30 15:08:44 +1100202
Damien Miller4f9f42a2003-05-10 19:28:02 +1000203static int
204pthread_cancel(sp_pthread_t thread)
205{
Darren Tucker7ae09622004-01-14 23:07:56 +1100206 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000207 return (kill(thread, SIGTERM));
208}
209
Damien Millerb8fe89c2006-07-24 14:51:00 +1000210/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000211static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000212pthread_join(sp_pthread_t thread, void **value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213{
214 int status;
215
Darren Tucker749bc952004-01-14 22:14:04 +1100216 if (sshpam_thread_status != -1)
217 return (sshpam_thread_status);
218 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000219 waitpid(thread, &status, 0);
220 return (status);
221}
222#endif
223
224
Damien Miller5c3a5582003-09-23 22:12:38 +1000225static pam_handle_t *sshpam_handle = NULL;
226static int sshpam_err = 0;
227static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000228static int sshpam_session_open = 0;
229static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100230static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100231static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000232static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000233static const char *sshpam_password = NULL;
Darren Tucker36a3d602005-01-20 12:43:38 +1100234static char badpw[] = "\b\n\r\177INCORRECT";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000235
Damien Millerc756e9b2003-11-17 21:41:42 +1100236/* Some PAM implementations don't implement this */
237#ifndef HAVE_PAM_GETENVLIST
238static char **
239pam_getenvlist(pam_handle_t *pamh)
240{
241 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100242 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100243 * for platforms without pam_getenvlist by searching for known
244 * env vars (e.g. KRB5CCNAME) from the PAM environment.
245 */
246 return NULL;
247}
248#endif
249
Darren Tucker21dd0892004-08-16 23:12:05 +1000250/*
251 * Some platforms, notably Solaris, do not enforce password complexity
252 * rules during pam_chauthtok() if the real uid of the calling process
253 * is 0, on the assumption that it's being called by "passwd" run by root.
254 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
255 * the right thing.
256 */
257#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
258static int
259sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
260{
261 int result;
262
263 if (sshpam_authctxt == NULL)
264 fatal("PAM: sshpam_authctxt not initialized");
265 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
266 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
267 result = pam_chauthtok(pamh, flags);
268 if (setreuid(0, -1) == -1)
269 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
270 return result;
271}
272# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
273#endif
274
Darren Tucker07705c72003-12-18 15:34:31 +1100275void
Darren Tucker17db1c42004-06-19 12:54:38 +1000276sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100277{
Darren Tuckera8df9242004-01-15 00:15:07 +1100278 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000279 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100280 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000281 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100282 if (reqd) {
283 no_port_forwarding_flag |= 2;
284 no_agent_forwarding_flag |= 2;
285 no_x11_forwarding_flag |= 2;
286 } else {
287 no_port_forwarding_flag &= ~2;
288 no_agent_forwarding_flag &= ~2;
289 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100290 }
291}
Darren Tucker1921ed92004-02-10 13:23:28 +1100292
Damien Millerc756e9b2003-11-17 21:41:42 +1100293/* Import regular and PAM environment from subprocess */
294static void
295import_environments(Buffer *b)
296{
297 char *env;
298 u_int i, num_env;
299 int err;
300
Darren Tuckera8df9242004-01-15 00:15:07 +1100301 debug3("PAM: %s entering", __func__);
302
Darren Tucker328118a2005-05-25 16:18:09 +1000303#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100304 /* Import variables set by do_pam_account */
305 sshpam_account_status = buffer_get_int(b);
Darren Tucker17db1c42004-06-19 12:54:38 +1000306 sshpam_password_change_required(buffer_get_int(b));
Darren Tucker07705c72003-12-18 15:34:31 +1100307
Damien Millerc756e9b2003-11-17 21:41:42 +1100308 /* Import environment from subprocess */
309 num_env = buffer_get_int(b);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000310 if (num_env > 1024)
311 fatal("%s: received %u environment variables, expected <= 1024",
312 __func__, num_env);
313 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
Damien Millerc756e9b2003-11-17 21:41:42 +1100314 debug3("PAM: num env strings %d", num_env);
315 for(i = 0; i < num_env; i++)
316 sshpam_env[i] = buffer_get_string(b, NULL);
317
318 sshpam_env[num_env] = NULL;
319
320 /* Import PAM environment from subprocess */
321 num_env = buffer_get_int(b);
322 debug("PAM: num PAM env strings %d", num_env);
323 for(i = 0; i < num_env; i++) {
324 env = buffer_get_string(b, NULL);
325
Darren Tucker8a1624c2003-11-18 12:45:35 +1100326#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100327 /* Errors are not fatal here */
328 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
329 error("PAM: pam_putenv: %s",
330 pam_strerror(sshpam_handle, sshpam_err));
331 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100332#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100333 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100334#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100335}
336
Damien Miller9d5705a2000-09-16 16:09:27 +1100337/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000338 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100339 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000340static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000341sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000342 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100343{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000344 Buffer buffer;
345 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000346 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000347 int i;
348
Darren Tuckerba53b832004-02-17 20:46:59 +1100349 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000350 *resp = NULL;
351
Darren Tucker59e06022004-06-30 20:34:31 +1000352 if (data == NULL) {
353 error("PAM: conversation function passed a null context");
354 return (PAM_CONV_ERR);
355 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000356 ctxt = data;
357 if (n <= 0 || n > PAM_MAX_NUM_MSG)
358 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000359
Darren Tuckerd8093e42006-05-04 16:24:34 +1000360 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000361 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000362
Damien Miller4f9f42a2003-05-10 19:28:02 +1000363 buffer_init(&buffer);
364 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000365 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
366 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100367 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000368 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100369 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100370 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
371 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100372 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100373 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000374 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
375 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000376 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000377 break;
378 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100379 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000380 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100381 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100382 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
383 goto fail;
384 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
385 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000386 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
387 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000388 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000389 break;
390 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100391 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000392 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100393 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100394 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
395 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000396 break;
397 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100398 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000399 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100400 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100401 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
402 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000403 break;
404 default:
405 goto fail;
406 }
407 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000408 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000409 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000410 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000411 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000412
Damien Miller4f9f42a2003-05-10 19:28:02 +1000413 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000414 for(i = 0; i < n; i++) {
415 if (reply[i].resp != NULL)
416 xfree(reply[i].resp);
417 }
418 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000419 buffer_free(&buffer);
420 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000421}
422
Damien Miller4f9f42a2003-05-10 19:28:02 +1000423/*
424 * Authentication thread.
425 */
426static void *
427sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100428{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000429 struct pam_ctxt *ctxt = ctxtp;
430 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000431 struct pam_conv sshpam_conv;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000432 int flags = (options.permit_empty_passwd == 0 ?
433 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000434#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100435 extern char **environ;
436 char **env_from_pam;
437 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000438 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000439 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000440
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000441 pam_get_item(sshpam_handle, PAM_USER,
442 (sshpam_const void **)ptr_pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100443 environ[0] = NULL;
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000444
445 if (sshpam_authctxt != NULL) {
446 setproctitle("%s [pam]",
447 sshpam_authctxt->valid ? pam_user : "unknown");
448 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000449#endif
450
Damien Millerf4b6f102003-09-02 23:12:06 +1000451 sshpam_conv.conv = sshpam_thread_conv;
452 sshpam_conv.appdata_ptr = ctxt;
453
Darren Tucker17addf02004-03-30 20:57:57 +1000454 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100455 fatal("%s: PAM authctxt not initialized", __func__);
456
Damien Miller4f9f42a2003-05-10 19:28:02 +1000457 buffer_init(&buffer);
458 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
459 (const void *)&sshpam_conv);
460 if (sshpam_err != PAM_SUCCESS)
461 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000462 sshpam_err = pam_authenticate(sshpam_handle, flags);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000463 if (sshpam_err != PAM_SUCCESS)
464 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100465
Darren Tuckerc376c862003-12-18 16:08:59 +1100466 if (compat20) {
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000467 if (!do_pam_account()) {
468 sshpam_err = PAM_ACCT_EXPIRED;
Darren Tucker07705c72003-12-18 15:34:31 +1100469 goto auth_fail;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000470 }
Darren Tucker17addf02004-03-30 20:57:57 +1000471 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100472 sshpam_err = pam_chauthtok(sshpam_handle,
473 PAM_CHANGE_EXPIRED_AUTHTOK);
474 if (sshpam_err != PAM_SUCCESS)
475 goto auth_fail;
Darren Tucker17db1c42004-06-19 12:54:38 +1000476 sshpam_password_change_required(0);
Darren Tucker07705c72003-12-18 15:34:31 +1100477 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100478 }
Darren Tucker07705c72003-12-18 15:34:31 +1100479
Damien Miller4f9f42a2003-05-10 19:28:02 +1000480 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100481
Darren Tucker328118a2005-05-25 16:18:09 +1000482#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100483 /* Export variables set by do_pam_account */
484 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000485 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100486
Damien Millerc756e9b2003-11-17 21:41:42 +1100487 /* Export any environment strings set in child */
488 for(i = 0; environ[i] != NULL; i++)
489 ; /* Count */
490 buffer_put_int(&buffer, i);
491 for(i = 0; environ[i] != NULL; i++)
492 buffer_put_cstring(&buffer, environ[i]);
493
494 /* Export any environment strings set by PAM in child */
495 env_from_pam = pam_getenvlist(sshpam_handle);
496 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
497 ; /* Count */
498 buffer_put_int(&buffer, i);
499 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
500 buffer_put_cstring(&buffer, env_from_pam[i]);
Darren Tucker328118a2005-05-25 16:18:09 +1000501#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100502
Damien Miller9bdba702003-11-17 21:27:55 +1100503 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000504 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
505 buffer_free(&buffer);
506 pthread_exit(NULL);
507
508 auth_fail:
509 buffer_put_cstring(&buffer,
510 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100511 /* XXX - can't do much about an error here */
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000512 if (sshpam_err == PAM_ACCT_EXPIRED)
513 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
514 else
515 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000516 buffer_free(&buffer);
517 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100518
Damien Miller4f9f42a2003-05-10 19:28:02 +1000519 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000520}
521
Darren Tucker8846a072003-10-07 11:30:15 +1000522void
523sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000524{
Darren Tucker8846a072003-10-07 11:30:15 +1000525 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000526
Darren Tuckera8df9242004-01-15 00:15:07 +1100527 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000528 if (ctxt != NULL && ctxt->pam_thread != 0) {
529 pthread_cancel(ctxt->pam_thread);
530 pthread_join(ctxt->pam_thread, NULL);
531 close(ctxt->pam_psock);
532 close(ctxt->pam_csock);
533 memset(ctxt, 0, sizeof(*ctxt));
534 cleanup_ctxt = NULL;
535 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000536}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000537
Damien Miller4f9f42a2003-05-10 19:28:02 +1000538static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000539sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000540 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000541{
Darren Tuckerba53b832004-02-17 20:46:59 +1100542 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000543 return (PAM_CONV_ERR);
544}
Damien Miller63dc3e92001-02-07 12:58:33 +1100545
Damien Miller4f9f42a2003-05-10 19:28:02 +1000546static struct pam_conv null_conv = { sshpam_null_conv, NULL };
547
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000548static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000549sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000550 struct pam_response **resp, void *data)
551{
552 struct pam_response *reply;
553 int i;
554 size_t len;
555
556 debug3("PAM: %s called with %d messages", __func__, n);
557 *resp = NULL;
558
559 if (n <= 0 || n > PAM_MAX_NUM_MSG)
560 return (PAM_CONV_ERR);
561
Darren Tuckerd8093e42006-05-04 16:24:34 +1000562 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000563 return (PAM_CONV_ERR);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000564
565 for (i = 0; i < n; ++i) {
566 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
567 case PAM_ERROR_MSG:
568 case PAM_TEXT_INFO:
569 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
570 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
571 buffer_append(&loginmsg, "\n", 1 );
572 reply[i].resp_retcode = PAM_SUCCESS;
573 break;
574 default:
575 goto fail;
576 }
577 }
578 *resp = reply;
579 return (PAM_SUCCESS);
580
581 fail:
582 for(i = 0; i < n; i++) {
583 if (reply[i].resp != NULL)
584 xfree(reply[i].resp);
585 }
586 xfree(reply);
587 return (PAM_CONV_ERR);
588}
589
590static struct pam_conv store_conv = { sshpam_store_conv, NULL };
591
Darren Tucker8846a072003-10-07 11:30:15 +1000592void
593sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000594{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000595 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000596 if (sshpam_handle == NULL)
597 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000598 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
599 if (sshpam_cred_established) {
600 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
601 sshpam_cred_established = 0;
602 }
603 if (sshpam_session_open) {
604 pam_close_session(sshpam_handle, PAM_SILENT);
605 sshpam_session_open = 0;
606 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100607 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000608 pam_end(sshpam_handle, sshpam_err);
609 sshpam_handle = NULL;
610}
611
612static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100613sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000614{
Darren Tucker455813b2003-09-13 22:12:11 +1000615 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100616 const char *pam_rhost, *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000617 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000618
619 if (sshpam_handle != NULL) {
620 /* We already have a PAM context; check if the user matches */
621 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000622 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000623 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
624 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000625 pam_end(sshpam_handle, sshpam_err);
626 sshpam_handle = NULL;
627 }
628 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000629 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000630 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000631 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100632
Damien Miller4f9f42a2003-05-10 19:28:02 +1000633 if (sshpam_err != PAM_SUCCESS) {
634 pam_end(sshpam_handle, sshpam_err);
635 sshpam_handle = NULL;
636 return (-1);
637 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000638 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000639 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000640 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
641 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000642 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000643 sshpam_handle = NULL;
644 return (-1);
645 }
646#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100647 /*
648 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
649 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000650 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100651 */
Damien Miller25d93422003-05-18 20:45:47 +1000652 debug("PAM: setting PAM_TTY to \"ssh\"");
653 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
654 if (sshpam_err != PAM_SUCCESS) {
655 pam_end(sshpam_handle, sshpam_err);
656 sshpam_handle = NULL;
657 return (-1);
658 }
659#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000660 return (0);
661}
662
663static void *
664sshpam_init_ctx(Authctxt *authctxt)
665{
666 struct pam_ctxt *ctxt;
667 int socks[2];
668
Darren Tuckera8df9242004-01-15 00:15:07 +1100669 debug3("PAM: %s entering", __func__);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000670 /*
671 * Refuse to start if we don't have PAM enabled or do_pam_account
672 * has previously failed.
673 */
674 if (!options.use_pam || sshpam_account_status == 0)
Damien Miller4e448a32003-05-14 15:11:48 +1000675 return NULL;
676
Damien Miller4f9f42a2003-05-10 19:28:02 +1000677 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100678 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000679 error("PAM: initialization failed");
680 return (NULL);
681 }
682
683 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000684 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000685
686 /* Start the authentication thread */
687 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
688 error("PAM: failed create sockets: %s", strerror(errno));
689 xfree(ctxt);
690 return (NULL);
691 }
692 ctxt->pam_psock = socks[0];
693 ctxt->pam_csock = socks[1];
694 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
695 error("PAM: failed to start authentication thread: %s",
696 strerror(errno));
697 close(socks[0]);
698 close(socks[1]);
699 xfree(ctxt);
700 return (NULL);
701 }
Darren Tucker8846a072003-10-07 11:30:15 +1000702 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000703 return (ctxt);
704}
705
706static int
707sshpam_query(void *ctx, char **name, char **info,
708 u_int *num, char ***prompts, u_int **echo_on)
709{
710 Buffer buffer;
711 struct pam_ctxt *ctxt = ctx;
712 size_t plen;
713 u_char type;
714 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000715 size_t len, mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000716
Darren Tuckera8df9242004-01-15 00:15:07 +1100717 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000718 buffer_init(&buffer);
719 *name = xstrdup("");
720 *info = xstrdup("");
721 *prompts = xmalloc(sizeof(char *));
722 **prompts = NULL;
723 plen = 0;
724 *echo_on = xmalloc(sizeof(u_int));
725 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
726 type = buffer_get_char(&buffer);
727 msg = buffer_get_string(&buffer, NULL);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000728 mlen = strlen(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000729 switch (type) {
730 case PAM_PROMPT_ECHO_ON:
731 case PAM_PROMPT_ECHO_OFF:
732 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000733 len = plen + mlen + 1;
Damien Miller36812092006-03-26 14:22:47 +1100734 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000735 strlcpy(**prompts + plen, msg, len - plen);
736 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000737 **echo_on = (type == PAM_PROMPT_ECHO_ON);
738 xfree(msg);
739 return (0);
740 case PAM_ERROR_MSG:
741 case PAM_TEXT_INFO:
742 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000743 len = plen + mlen + 2;
Damien Miller36812092006-03-26 14:22:47 +1100744 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000745 strlcpy(**prompts + plen, msg, len - plen);
746 plen += mlen;
747 strlcat(**prompts + plen, "\n", len - plen);
748 plen++;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000749 xfree(msg);
750 break;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000751 case PAM_ACCT_EXPIRED:
752 sshpam_account_status = 0;
753 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000754 case PAM_AUTH_ERR:
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000755 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
Darren Tucker7b1e6952005-09-28 22:33:27 +1000756 if (**prompts != NULL && strlen(**prompts) != 0) {
757 *info = **prompts;
758 **prompts = NULL;
759 *num = 0;
760 **echo_on = 0;
761 ctxt->pam_done = -1;
Damien Miller66f9eb62006-03-18 23:04:49 +1100762 xfree(msg);
Darren Tucker7b1e6952005-09-28 22:33:27 +1000763 return 0;
764 }
765 /* FALLTHROUGH */
766 case PAM_SUCCESS:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000767 if (**prompts != NULL) {
768 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100769 debug("PAM: %s", **prompts);
770 buffer_append(&loginmsg, **prompts,
771 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000772 xfree(**prompts);
773 **prompts = NULL;
774 }
775 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100776 if (!sshpam_authctxt->valid ||
777 (sshpam_authctxt->pw->pw_uid == 0 &&
778 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100779 fatal("Internal error: PAM auth "
780 "succeeded when it should have "
781 "failed");
Damien Millerc756e9b2003-11-17 21:41:42 +1100782 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000783 *num = 0;
784 **echo_on = 0;
785 ctxt->pam_done = 1;
786 xfree(msg);
787 return (0);
788 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000789 error("PAM: %s for %s%.100s from %.100s", msg,
790 sshpam_authctxt->valid ? "" : "illegal user ",
791 sshpam_authctxt->user,
792 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000793 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000794 default:
795 *num = 0;
796 **echo_on = 0;
797 xfree(msg);
798 ctxt->pam_done = -1;
799 return (-1);
800 }
801 }
802 return (-1);
803}
804
805/* XXX - see also comment in auth-chall.c:verify_response */
806static int
807sshpam_respond(void *ctx, u_int num, char **resp)
808{
809 Buffer buffer;
810 struct pam_ctxt *ctxt = ctx;
811
Darren Tucker1d4ebbf2006-01-29 16:46:13 +1100812 debug2("PAM: %s entering, %u responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000813 switch (ctxt->pam_done) {
814 case 1:
815 sshpam_authenticated = 1;
816 return (0);
817 case 0:
818 break;
819 default:
820 return (-1);
821 }
822 if (num != 1) {
823 error("PAM: expected one response, got %u", num);
824 return (-1);
825 }
826 buffer_init(&buffer);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100827 if (sshpam_authctxt->valid &&
828 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller37294fb2005-07-17 17:18:49 +1000829 options.permit_root_login == PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100830 buffer_put_cstring(&buffer, *resp);
831 else
832 buffer_put_cstring(&buffer, badpw);
Damien Miller9bdba702003-11-17 21:27:55 +1100833 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
834 buffer_free(&buffer);
835 return (-1);
836 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000837 buffer_free(&buffer);
838 return (1);
839}
840
841static void
842sshpam_free_ctx(void *ctxtp)
843{
844 struct pam_ctxt *ctxt = ctxtp;
845
Darren Tuckera8df9242004-01-15 00:15:07 +1100846 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000847 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000848 xfree(ctxt);
849 /*
850 * We don't call sshpam_cleanup() here because we may need the PAM
851 * handle at a later stage, e.g. when setting up a session. It's
852 * still on the cleanup list, so pam_end() *will* be called before
853 * the server process terminates.
854 */
855}
856
857KbdintDevice sshpam_device = {
858 "pam",
859 sshpam_init_ctx,
860 sshpam_query,
861 sshpam_respond,
862 sshpam_free_ctx
863};
864
865KbdintDevice mm_sshpam_device = {
866 "pam",
867 mm_sshpam_init_ctx,
868 mm_sshpam_query,
869 mm_sshpam_respond,
870 mm_sshpam_free_ctx
871};
872
873/*
874 * This replaces auth-pam.c
875 */
876void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100877start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000878{
Damien Miller9d507da2003-05-14 15:31:12 +1000879 if (!options.use_pam)
880 fatal("PAM: initialisation requested when UsePAM=no");
881
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100882 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000883 fatal("PAM: initialisation failed");
884}
885
886void
887finish_pam(void)
888{
Darren Tucker8846a072003-10-07 11:30:15 +1000889 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000890}
891
Damien Miller1f499fd2003-08-25 13:08:49 +1000892u_int
893do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000894{
Darren Tucker77fc29e2004-09-11 23:07:03 +1000895 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +1100896 if (sshpam_account_status != -1)
897 return (sshpam_account_status);
898
Damien Miller1f499fd2003-08-25 13:08:49 +1000899 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000900 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
901 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +1000902
Darren Tucker07705c72003-12-18 15:34:31 +1100903 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
904 sshpam_account_status = 0;
905 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000906 }
907
Darren Tucker07705c72003-12-18 15:34:31 +1100908 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +1000909 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +1100910
911 sshpam_account_status = 1;
912 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000913}
914
915void
Damien Miller341c6e62003-09-02 23:18:52 +1000916do_pam_set_tty(const char *tty)
917{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000918 if (tty != NULL) {
919 debug("PAM: setting PAM_TTY to \"%s\"", tty);
920 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
921 if (sshpam_err != PAM_SUCCESS)
922 fatal("PAM: failed to set PAM_TTY: %s",
923 pam_strerror(sshpam_handle, sshpam_err));
924 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000925}
926
927void
928do_pam_setcred(int init)
929{
930 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +1000931 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000932 if (sshpam_err != PAM_SUCCESS)
933 fatal("PAM: failed to set PAM_CONV: %s",
934 pam_strerror(sshpam_handle, sshpam_err));
935 if (init) {
936 debug("PAM: establishing credentials");
937 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
938 } else {
939 debug("PAM: reinitializing credentials");
940 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
941 }
942 if (sshpam_err == PAM_SUCCESS) {
943 sshpam_cred_established = 1;
944 return;
945 }
946 if (sshpam_authenticated)
947 fatal("PAM: pam_setcred(): %s",
948 pam_strerror(sshpam_handle, sshpam_err));
949 else
950 debug("PAM: pam_setcred(): %s",
951 pam_strerror(sshpam_handle, sshpam_err));
952}
953
Damien Miller4f9f42a2003-05-10 19:28:02 +1000954static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000955sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000956 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000957{
958 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000959 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000960 int i;
961
Darren Tuckerba53b832004-02-17 20:46:59 +1100962 debug3("PAM: %s called with %d messages", __func__, n);
963
Damien Miller5c3a5582003-09-23 22:12:38 +1000964 *resp = NULL;
965
Darren Tucker18df00c2003-11-18 12:42:07 +1100966 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000967 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000968
Darren Tuckerd8093e42006-05-04 16:24:34 +1000969 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000970 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000971
Damien Miller4f9f42a2003-05-10 19:28:02 +1000972 for (i = 0; i < n; ++i) {
973 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
974 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000975 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100976 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000977 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000978 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000979 break;
980 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100981 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000982 fgets(input, sizeof input, stdin);
Damien Millera6fb77f2004-07-19 09:39:11 +1000983 if ((reply[i].resp = strdup(input)) == NULL)
984 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000985 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000986 break;
987 case PAM_ERROR_MSG:
988 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100989 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000990 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000991 break;
992 default:
993 goto fail;
994 }
995 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000996 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000997 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000998
Damien Miller4f9f42a2003-05-10 19:28:02 +1000999 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +10001000 for(i = 0; i < n; i++) {
1001 if (reply[i].resp != NULL)
1002 xfree(reply[i].resp);
1003 }
1004 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001005 return (PAM_CONV_ERR);
1006}
1007
Darren Tucker94befab2004-06-03 14:53:12 +10001008static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +11001009
Damien Miller4f9f42a2003-05-10 19:28:02 +10001010/*
1011 * XXX this should be done in the authentication phase, but ssh1 doesn't
1012 * support that
1013 */
1014void
1015do_pam_chauthtok(void)
1016{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001017 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +10001018 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +10001019 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +11001020 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001021 if (sshpam_err != PAM_SUCCESS)
1022 fatal("PAM: failed to set PAM_CONV: %s",
1023 pam_strerror(sshpam_handle, sshpam_err));
1024 debug("PAM: changing password");
1025 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1026 if (sshpam_err != PAM_SUCCESS)
1027 fatal("PAM: pam_chauthtok(): %s",
1028 pam_strerror(sshpam_handle, sshpam_err));
1029}
1030
Darren Tucker18df00c2003-11-18 12:42:07 +11001031void
1032do_pam_session(void)
1033{
Darren Tucker1921ed92004-02-10 13:23:28 +11001034 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +11001035 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +11001036 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +11001037 if (sshpam_err != PAM_SUCCESS)
1038 fatal("PAM: failed to set PAM_CONV: %s",
1039 pam_strerror(sshpam_handle, sshpam_err));
1040 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +10001041 if (sshpam_err == PAM_SUCCESS)
1042 sshpam_session_open = 1;
1043 else {
1044 sshpam_session_open = 0;
1045 disable_forwarding();
1046 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001047 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001048 }
1049
1050}
1051
1052int
1053is_pam_session_open(void)
1054{
1055 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001056}
1057
Damien Millera8e06ce2003-11-21 23:48:55 +11001058/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001059 * Set a PAM environment string. We need to do this so that the session
1060 * modules can handle things like Kerberos/GSI credentials that appear
1061 * during the ssh authentication process.
1062 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001063int
Damien Millera8e06ce2003-11-21 23:48:55 +11001064do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001065{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001066 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +11001067#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +10001068 char *compound;
1069 size_t len;
1070
1071 len = strlen(name) + strlen(value) + 2;
1072 compound = xmalloc(len);
1073
1074 snprintf(compound, len, "%s=%s", name, value);
1075 ret = pam_putenv(sshpam_handle, compound);
1076 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001077#endif
Damien Millerf2728092003-09-17 07:24:25 +10001078
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001079 return (ret);
1080}
1081
Damien Miller4f9f42a2003-05-10 19:28:02 +10001082char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001083fetch_pam_child_environment(void)
1084{
1085 return sshpam_env;
1086}
1087
1088char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001089fetch_pam_environment(void)
1090{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001091 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001092}
1093
1094void
1095free_pam_environment(char **env)
1096{
1097 char **envp;
1098
Damien Millere27c6cc2003-05-16 18:21:01 +10001099 if (env == NULL)
1100 return;
1101
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102 for (envp = env; *envp; envp++)
1103 xfree(*envp);
1104 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001105}
1106
Darren Tucker450a1582004-05-30 20:43:59 +10001107/*
1108 * "Blind" conversation function for password authentication. Assumes that
1109 * echo-off prompts are for the password and stores messages for later
1110 * display.
1111 */
1112static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001113sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001114 struct pam_response **resp, void *data)
1115{
1116 struct pam_response *reply;
1117 int i;
1118 size_t len;
1119
1120 debug3("PAM: %s called with %d messages", __func__, n);
1121
1122 *resp = NULL;
1123
1124 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1125 return (PAM_CONV_ERR);
1126
1127 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1128 return (PAM_CONV_ERR);
1129 memset(reply, 0, n * sizeof(*reply));
1130
1131 for (i = 0; i < n; ++i) {
1132 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1133 case PAM_PROMPT_ECHO_OFF:
1134 if (sshpam_password == NULL)
1135 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001136 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1137 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001138 reply[i].resp_retcode = PAM_SUCCESS;
1139 break;
1140 case PAM_ERROR_MSG:
1141 case PAM_TEXT_INFO:
1142 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1143 if (len > 0) {
1144 buffer_append(&loginmsg,
1145 PAM_MSG_MEMBER(msg, i, msg), len);
1146 buffer_append(&loginmsg, "\n", 1);
1147 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001148 if ((reply[i].resp = strdup("")) == NULL)
1149 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001150 reply[i].resp_retcode = PAM_SUCCESS;
1151 break;
1152 default:
1153 goto fail;
1154 }
1155 }
1156 *resp = reply;
1157 return (PAM_SUCCESS);
1158
Damien Miller94cf4c82005-07-17 17:04:47 +10001159 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001160 for(i = 0; i < n; i++) {
1161 if (reply[i].resp != NULL)
1162 xfree(reply[i].resp);
1163 }
1164 xfree(reply);
1165 return (PAM_CONV_ERR);
1166}
1167
1168static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1169
1170/*
1171 * Attempt password authentication via PAM
1172 */
1173int
1174sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1175{
1176 int flags = (options.permit_empty_passwd == 0 ?
1177 PAM_DISALLOW_NULL_AUTHTOK : 0);
1178
1179 if (!options.use_pam || sshpam_handle == NULL)
1180 fatal("PAM: %s called when PAM disabled or failed to "
1181 "initialise.", __func__);
1182
1183 sshpam_password = password;
1184 sshpam_authctxt = authctxt;
1185
Darren Tuckere061b152004-05-30 22:04:56 +10001186 /*
1187 * If the user logging in is invalid, or is root but is not permitted
1188 * by PermitRootLogin, use an invalid password to prevent leaking
1189 * information via timing (eg if the PAM config has a delay on fail).
1190 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001191 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001192 options.permit_root_login != PERMIT_YES))
Darren Tuckere061b152004-05-30 22:04:56 +10001193 sshpam_password = badpw;
1194
Darren Tucker450a1582004-05-30 20:43:59 +10001195 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1196 (const void *)&passwd_conv);
1197 if (sshpam_err != PAM_SUCCESS)
1198 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1199 pam_strerror(sshpam_handle, sshpam_err));
1200
1201 sshpam_err = pam_authenticate(sshpam_handle, flags);
1202 sshpam_password = NULL;
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001203 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001204 debug("PAM: password authentication accepted for %.100s",
1205 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001206 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001207 } else {
1208 debug("PAM: password authentication failed for %.100s: %s",
1209 authctxt->valid ? authctxt->user : "an illegal user",
1210 pam_strerror(sshpam_handle, sshpam_err));
1211 return 0;
1212 }
1213}
Damien Millere72b7af1999-12-30 15:08:44 +11001214#endif /* USE_PAM */