blob: 6ce9db12b2fa9e774b295a3c9f460d2006365711 [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 Millerb8fe89c2006-07-24 14:51:00 +100057#include <string.h>
58#include <unistd.h>
Damien Millere72b7af1999-12-30 15:08:44 +110059
60#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110061#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100062#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110063#elif defined (HAVE_PAM_PAM_APPL_H)
64#include <pam/pam_appl.h>
65#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100066
Darren Tuckerf08bdb52005-05-26 19:59:48 +100067/* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
68#ifdef PAM_SUN_CODEBASE
69# define sshpam_const /* Solaris, HP-UX, AIX */
70#else
71# define sshpam_const const /* LinuxPAM, OpenPAM */
72#endif
73
Kevin Stevese683e762002-04-04 19:02:28 +000074#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110075#include "auth-pam.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100076#include "buffer.h"
77#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000078#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100079#include "log.h"
80#include "monitor_wrap.h"
81#include "msg.h"
82#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100083#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100084#include "servconf.h"
85#include "ssh2.h"
86#include "xmalloc.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100087#include "auth-options.h"
Damien Millere72b7af1999-12-30 15:08:44 +110088
Damien Miller4e448a32003-05-14 15:11:48 +100089extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +110090extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +110091extern int compat20;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +100092extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +100093
Darren Tucker328118a2005-05-25 16:18:09 +100094/* so we don't silently change behaviour */
Damien Miller4f9f42a2003-05-10 19:28:02 +100095#ifdef USE_POSIX_THREADS
Darren Tucker328118a2005-05-25 16:18:09 +100096# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
97#endif
98
99/*
100 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
101 * and generally a bad idea. Use at own risk and do not expect support if
102 * this breaks.
103 */
104#ifdef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000105#include <pthread.h>
106/*
Damien Millera8e06ce2003-11-21 23:48:55 +1100107 * Avoid namespace clash when *not* using pthreads for systems *with*
108 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +1000109 * (e.g. Linux)
110 */
Damien Millera8e06ce2003-11-21 23:48:55 +1100111typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000112#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100113typedef pid_t sp_pthread_t;
114#endif
115
116struct pam_ctxt {
117 sp_pthread_t pam_thread;
118 int pam_psock;
119 int pam_csock;
120 int pam_done;
121};
122
123static void sshpam_free_ctx(void *);
124static struct pam_ctxt *cleanup_ctxt;
125
Darren Tucker328118a2005-05-25 16:18:09 +1000126#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000127/*
128 * Simulate threads with processes.
129 */
Kevin Steves63007d42002-07-21 17:57:01 +0000130
Darren Tucker749bc952004-01-14 22:14:04 +1100131static int sshpam_thread_status = -1;
132static mysig_t sshpam_oldsig;
133
Damien Miller94cf4c82005-07-17 17:04:47 +1000134static void
Darren Tucker749bc952004-01-14 22:14:04 +1100135sshpam_sigchld_handler(int sig)
136{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000137 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100138 if (cleanup_ctxt == NULL)
139 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000140 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000141 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000142 /* PAM thread has not exitted, privsep slave must have */
143 kill(cleanup_ctxt->pam_thread, SIGTERM);
144 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
Darren Tucker5d423f42004-07-11 16:54:08 +1000145 <= 0)
Darren Tuckerb53355e2004-05-24 11:55:36 +1000146 return; /* could not wait */
147 }
Darren Tucker749bc952004-01-14 22:14:04 +1100148 if (WIFSIGNALED(sshpam_thread_status) &&
149 WTERMSIG(sshpam_thread_status) == SIGTERM)
150 return; /* terminated by pthread_cancel */
151 if (!WIFEXITED(sshpam_thread_status))
152 fatal("PAM: authentication thread exited unexpectedly");
153 if (WEXITSTATUS(sshpam_thread_status) != 0)
154 fatal("PAM: authentication thread exited uncleanly");
155}
156
Damien Millerb8fe89c2006-07-24 14:51:00 +1000157/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000158static void
Damien Millerb8fe89c2006-07-24 14:51:00 +1000159pthread_exit(void *value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000160{
161 _exit(0);
162}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000163
Damien Millerb8fe89c2006-07-24 14:51:00 +1000164/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000165static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000166pthread_create(sp_pthread_t *thread, const void *attr,
Damien Miller4f9f42a2003-05-10 19:28:02 +1000167 void *(*thread_start)(void *), void *arg)
168{
169 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000170 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100171
Darren Tuckerb9b60212004-03-04 20:03:54 +1100172 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000173 switch ((pid = fork())) {
174 case -1:
175 error("fork(): %s", strerror(errno));
176 return (-1);
177 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000178 close(ctx->pam_psock);
179 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000180 thread_start(arg);
181 _exit(1);
182 default:
183 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000184 close(ctx->pam_csock);
185 ctx->pam_csock = -1;
Darren Tucker749bc952004-01-14 22:14:04 +1100186 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000187 return (0);
188 }
189}
Damien Millere72b7af1999-12-30 15:08:44 +1100190
Damien Miller4f9f42a2003-05-10 19:28:02 +1000191static int
192pthread_cancel(sp_pthread_t thread)
193{
Darren Tucker7ae09622004-01-14 23:07:56 +1100194 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000195 return (kill(thread, SIGTERM));
196}
197
Damien Millerb8fe89c2006-07-24 14:51:00 +1000198/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000199static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000200pthread_join(sp_pthread_t thread, void **value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000201{
202 int status;
203
Darren Tucker749bc952004-01-14 22:14:04 +1100204 if (sshpam_thread_status != -1)
205 return (sshpam_thread_status);
206 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000207 waitpid(thread, &status, 0);
208 return (status);
209}
210#endif
211
212
Damien Miller5c3a5582003-09-23 22:12:38 +1000213static pam_handle_t *sshpam_handle = NULL;
214static int sshpam_err = 0;
215static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000216static int sshpam_session_open = 0;
217static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100218static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100219static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000220static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000221static const char *sshpam_password = NULL;
Darren Tucker36a3d602005-01-20 12:43:38 +1100222static char badpw[] = "\b\n\r\177INCORRECT";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000223
Damien Millerc756e9b2003-11-17 21:41:42 +1100224/* Some PAM implementations don't implement this */
225#ifndef HAVE_PAM_GETENVLIST
226static char **
227pam_getenvlist(pam_handle_t *pamh)
228{
229 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100230 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100231 * for platforms without pam_getenvlist by searching for known
232 * env vars (e.g. KRB5CCNAME) from the PAM environment.
233 */
234 return NULL;
235}
236#endif
237
Darren Tucker21dd0892004-08-16 23:12:05 +1000238/*
239 * Some platforms, notably Solaris, do not enforce password complexity
240 * rules during pam_chauthtok() if the real uid of the calling process
241 * is 0, on the assumption that it's being called by "passwd" run by root.
242 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
243 * the right thing.
244 */
245#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
246static int
247sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
248{
249 int result;
250
251 if (sshpam_authctxt == NULL)
252 fatal("PAM: sshpam_authctxt not initialized");
253 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
254 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
255 result = pam_chauthtok(pamh, flags);
256 if (setreuid(0, -1) == -1)
257 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
258 return result;
259}
260# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
261#endif
262
Darren Tucker07705c72003-12-18 15:34:31 +1100263void
Darren Tucker17db1c42004-06-19 12:54:38 +1000264sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100265{
Darren Tuckera8df9242004-01-15 00:15:07 +1100266 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000267 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100268 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000269 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100270 if (reqd) {
271 no_port_forwarding_flag |= 2;
272 no_agent_forwarding_flag |= 2;
273 no_x11_forwarding_flag |= 2;
274 } else {
275 no_port_forwarding_flag &= ~2;
276 no_agent_forwarding_flag &= ~2;
277 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100278 }
279}
Darren Tucker1921ed92004-02-10 13:23:28 +1100280
Damien Millerc756e9b2003-11-17 21:41:42 +1100281/* Import regular and PAM environment from subprocess */
282static void
283import_environments(Buffer *b)
284{
285 char *env;
286 u_int i, num_env;
287 int err;
288
Darren Tuckera8df9242004-01-15 00:15:07 +1100289 debug3("PAM: %s entering", __func__);
290
Darren Tucker328118a2005-05-25 16:18:09 +1000291#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100292 /* Import variables set by do_pam_account */
293 sshpam_account_status = buffer_get_int(b);
Darren Tucker17db1c42004-06-19 12:54:38 +1000294 sshpam_password_change_required(buffer_get_int(b));
Darren Tucker07705c72003-12-18 15:34:31 +1100295
Damien Millerc756e9b2003-11-17 21:41:42 +1100296 /* Import environment from subprocess */
297 num_env = buffer_get_int(b);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000298 if (num_env > 1024)
299 fatal("%s: received %u environment variables, expected <= 1024",
300 __func__, num_env);
301 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
Damien Millerc756e9b2003-11-17 21:41:42 +1100302 debug3("PAM: num env strings %d", num_env);
303 for(i = 0; i < num_env; i++)
304 sshpam_env[i] = buffer_get_string(b, NULL);
305
306 sshpam_env[num_env] = NULL;
307
308 /* Import PAM environment from subprocess */
309 num_env = buffer_get_int(b);
310 debug("PAM: num PAM env strings %d", num_env);
311 for(i = 0; i < num_env; i++) {
312 env = buffer_get_string(b, NULL);
313
Darren Tucker8a1624c2003-11-18 12:45:35 +1100314#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100315 /* Errors are not fatal here */
316 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
317 error("PAM: pam_putenv: %s",
318 pam_strerror(sshpam_handle, sshpam_err));
319 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100320#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100321 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100322#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100323}
324
Damien Miller9d5705a2000-09-16 16:09:27 +1100325/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000326 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100327 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000328static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000329sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000330 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100331{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000332 Buffer buffer;
333 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000334 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000335 int i;
336
Darren Tuckerba53b832004-02-17 20:46:59 +1100337 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000338 *resp = NULL;
339
Darren Tucker59e06022004-06-30 20:34:31 +1000340 if (data == NULL) {
341 error("PAM: conversation function passed a null context");
342 return (PAM_CONV_ERR);
343 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000344 ctxt = data;
345 if (n <= 0 || n > PAM_MAX_NUM_MSG)
346 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000347
Darren Tuckerd8093e42006-05-04 16:24:34 +1000348 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000349 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000350
Damien Miller4f9f42a2003-05-10 19:28:02 +1000351 buffer_init(&buffer);
352 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000353 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
354 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100355 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000356 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100357 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100358 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
359 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100360 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100361 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000362 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
363 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000364 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000365 break;
366 case PAM_PROMPT_ECHO_ON:
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;
372 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
373 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_ERROR_MSG:
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;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000384 break;
385 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100386 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000387 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100388 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100389 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
390 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000391 break;
392 default:
393 goto fail;
394 }
395 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000396 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000397 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000398 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000399 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000400
Damien Miller4f9f42a2003-05-10 19:28:02 +1000401 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000402 for(i = 0; i < n; i++) {
403 if (reply[i].resp != NULL)
404 xfree(reply[i].resp);
405 }
406 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000407 buffer_free(&buffer);
408 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000409}
410
Damien Miller4f9f42a2003-05-10 19:28:02 +1000411/*
412 * Authentication thread.
413 */
414static void *
415sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100416{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000417 struct pam_ctxt *ctxt = ctxtp;
418 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000419 struct pam_conv sshpam_conv;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000420 int flags = (options.permit_empty_passwd == 0 ?
421 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000422#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100423 extern char **environ;
424 char **env_from_pam;
425 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000426 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000427 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000428
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000429 pam_get_item(sshpam_handle, PAM_USER,
430 (sshpam_const void **)ptr_pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100431 environ[0] = NULL;
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000432
433 if (sshpam_authctxt != NULL) {
434 setproctitle("%s [pam]",
435 sshpam_authctxt->valid ? pam_user : "unknown");
436 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000437#endif
438
Damien Millerf4b6f102003-09-02 23:12:06 +1000439 sshpam_conv.conv = sshpam_thread_conv;
440 sshpam_conv.appdata_ptr = ctxt;
441
Darren Tucker17addf02004-03-30 20:57:57 +1000442 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100443 fatal("%s: PAM authctxt not initialized", __func__);
444
Damien Miller4f9f42a2003-05-10 19:28:02 +1000445 buffer_init(&buffer);
446 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
447 (const void *)&sshpam_conv);
448 if (sshpam_err != PAM_SUCCESS)
449 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000450 sshpam_err = pam_authenticate(sshpam_handle, flags);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000451 if (sshpam_err != PAM_SUCCESS)
452 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100453
Darren Tuckerc376c862003-12-18 16:08:59 +1100454 if (compat20) {
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000455 if (!do_pam_account()) {
456 sshpam_err = PAM_ACCT_EXPIRED;
Darren Tucker07705c72003-12-18 15:34:31 +1100457 goto auth_fail;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000458 }
Darren Tucker17addf02004-03-30 20:57:57 +1000459 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100460 sshpam_err = pam_chauthtok(sshpam_handle,
461 PAM_CHANGE_EXPIRED_AUTHTOK);
462 if (sshpam_err != PAM_SUCCESS)
463 goto auth_fail;
Darren Tucker17db1c42004-06-19 12:54:38 +1000464 sshpam_password_change_required(0);
Darren Tucker07705c72003-12-18 15:34:31 +1100465 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100466 }
Darren Tucker07705c72003-12-18 15:34:31 +1100467
Damien Miller4f9f42a2003-05-10 19:28:02 +1000468 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100469
Darren Tucker328118a2005-05-25 16:18:09 +1000470#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100471 /* Export variables set by do_pam_account */
472 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000473 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100474
Damien Millerc756e9b2003-11-17 21:41:42 +1100475 /* Export any environment strings set in child */
476 for(i = 0; environ[i] != NULL; i++)
477 ; /* Count */
478 buffer_put_int(&buffer, i);
479 for(i = 0; environ[i] != NULL; i++)
480 buffer_put_cstring(&buffer, environ[i]);
481
482 /* Export any environment strings set by PAM in child */
483 env_from_pam = pam_getenvlist(sshpam_handle);
484 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
485 ; /* Count */
486 buffer_put_int(&buffer, i);
487 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
488 buffer_put_cstring(&buffer, env_from_pam[i]);
Darren Tucker328118a2005-05-25 16:18:09 +1000489#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100490
Damien Miller9bdba702003-11-17 21:27:55 +1100491 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000492 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
493 buffer_free(&buffer);
494 pthread_exit(NULL);
495
496 auth_fail:
497 buffer_put_cstring(&buffer,
498 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100499 /* XXX - can't do much about an error here */
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000500 if (sshpam_err == PAM_ACCT_EXPIRED)
501 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
502 else
503 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000504 buffer_free(&buffer);
505 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100506
Damien Miller4f9f42a2003-05-10 19:28:02 +1000507 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000508}
509
Darren Tucker8846a072003-10-07 11:30:15 +1000510void
511sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000512{
Darren Tucker8846a072003-10-07 11:30:15 +1000513 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000514
Darren Tuckera8df9242004-01-15 00:15:07 +1100515 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000516 if (ctxt != NULL && ctxt->pam_thread != 0) {
517 pthread_cancel(ctxt->pam_thread);
518 pthread_join(ctxt->pam_thread, NULL);
519 close(ctxt->pam_psock);
520 close(ctxt->pam_csock);
521 memset(ctxt, 0, sizeof(*ctxt));
522 cleanup_ctxt = NULL;
523 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000524}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000525
Damien Miller4f9f42a2003-05-10 19:28:02 +1000526static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000527sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000528 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000529{
Darren Tuckerba53b832004-02-17 20:46:59 +1100530 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000531 return (PAM_CONV_ERR);
532}
Damien Miller63dc3e92001-02-07 12:58:33 +1100533
Damien Miller4f9f42a2003-05-10 19:28:02 +1000534static struct pam_conv null_conv = { sshpam_null_conv, NULL };
535
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000536static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000537sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000538 struct pam_response **resp, void *data)
539{
540 struct pam_response *reply;
541 int i;
542 size_t len;
543
544 debug3("PAM: %s called with %d messages", __func__, n);
545 *resp = NULL;
546
547 if (n <= 0 || n > PAM_MAX_NUM_MSG)
548 return (PAM_CONV_ERR);
549
Darren Tuckerd8093e42006-05-04 16:24:34 +1000550 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000551 return (PAM_CONV_ERR);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000552
553 for (i = 0; i < n; ++i) {
554 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
555 case PAM_ERROR_MSG:
556 case PAM_TEXT_INFO:
557 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
558 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
559 buffer_append(&loginmsg, "\n", 1 );
560 reply[i].resp_retcode = PAM_SUCCESS;
561 break;
562 default:
563 goto fail;
564 }
565 }
566 *resp = reply;
567 return (PAM_SUCCESS);
568
569 fail:
570 for(i = 0; i < n; i++) {
571 if (reply[i].resp != NULL)
572 xfree(reply[i].resp);
573 }
574 xfree(reply);
575 return (PAM_CONV_ERR);
576}
577
578static struct pam_conv store_conv = { sshpam_store_conv, NULL };
579
Darren Tucker8846a072003-10-07 11:30:15 +1000580void
581sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000582{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000583 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000584 if (sshpam_handle == NULL)
585 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000586 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
587 if (sshpam_cred_established) {
588 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
589 sshpam_cred_established = 0;
590 }
591 if (sshpam_session_open) {
592 pam_close_session(sshpam_handle, PAM_SILENT);
593 sshpam_session_open = 0;
594 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100595 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000596 pam_end(sshpam_handle, sshpam_err);
597 sshpam_handle = NULL;
598}
599
600static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100601sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000602{
Darren Tucker455813b2003-09-13 22:12:11 +1000603 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100604 const char *pam_rhost, *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000605 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000606
607 if (sshpam_handle != NULL) {
608 /* We already have a PAM context; check if the user matches */
609 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000610 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000611 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
612 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000613 pam_end(sshpam_handle, sshpam_err);
614 sshpam_handle = NULL;
615 }
616 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000617 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000618 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000619 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100620
Damien Miller4f9f42a2003-05-10 19:28:02 +1000621 if (sshpam_err != PAM_SUCCESS) {
622 pam_end(sshpam_handle, sshpam_err);
623 sshpam_handle = NULL;
624 return (-1);
625 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000626 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000627 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000628 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
629 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000630 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000631 sshpam_handle = NULL;
632 return (-1);
633 }
634#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100635 /*
636 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
637 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000638 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100639 */
Damien Miller25d93422003-05-18 20:45:47 +1000640 debug("PAM: setting PAM_TTY to \"ssh\"");
641 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
642 if (sshpam_err != PAM_SUCCESS) {
643 pam_end(sshpam_handle, sshpam_err);
644 sshpam_handle = NULL;
645 return (-1);
646 }
647#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000648 return (0);
649}
650
651static void *
652sshpam_init_ctx(Authctxt *authctxt)
653{
654 struct pam_ctxt *ctxt;
655 int socks[2];
656
Darren Tuckera8df9242004-01-15 00:15:07 +1100657 debug3("PAM: %s entering", __func__);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000658 /*
659 * Refuse to start if we don't have PAM enabled or do_pam_account
660 * has previously failed.
661 */
662 if (!options.use_pam || sshpam_account_status == 0)
Damien Miller4e448a32003-05-14 15:11:48 +1000663 return NULL;
664
Damien Miller4f9f42a2003-05-10 19:28:02 +1000665 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100666 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000667 error("PAM: initialization failed");
668 return (NULL);
669 }
670
671 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000672 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000673
674 /* Start the authentication thread */
675 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
676 error("PAM: failed create sockets: %s", strerror(errno));
677 xfree(ctxt);
678 return (NULL);
679 }
680 ctxt->pam_psock = socks[0];
681 ctxt->pam_csock = socks[1];
682 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
683 error("PAM: failed to start authentication thread: %s",
684 strerror(errno));
685 close(socks[0]);
686 close(socks[1]);
687 xfree(ctxt);
688 return (NULL);
689 }
Darren Tucker8846a072003-10-07 11:30:15 +1000690 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000691 return (ctxt);
692}
693
694static int
695sshpam_query(void *ctx, char **name, char **info,
696 u_int *num, char ***prompts, u_int **echo_on)
697{
698 Buffer buffer;
699 struct pam_ctxt *ctxt = ctx;
700 size_t plen;
701 u_char type;
702 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000703 size_t len, mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000704
Darren Tuckera8df9242004-01-15 00:15:07 +1100705 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000706 buffer_init(&buffer);
707 *name = xstrdup("");
708 *info = xstrdup("");
709 *prompts = xmalloc(sizeof(char *));
710 **prompts = NULL;
711 plen = 0;
712 *echo_on = xmalloc(sizeof(u_int));
713 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
714 type = buffer_get_char(&buffer);
715 msg = buffer_get_string(&buffer, NULL);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000716 mlen = strlen(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000717 switch (type) {
718 case PAM_PROMPT_ECHO_ON:
719 case PAM_PROMPT_ECHO_OFF:
720 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000721 len = plen + mlen + 1;
Damien Miller36812092006-03-26 14:22:47 +1100722 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000723 strlcpy(**prompts + plen, msg, len - plen);
724 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000725 **echo_on = (type == PAM_PROMPT_ECHO_ON);
726 xfree(msg);
727 return (0);
728 case PAM_ERROR_MSG:
729 case PAM_TEXT_INFO:
730 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000731 len = plen + mlen + 2;
Damien Miller36812092006-03-26 14:22:47 +1100732 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000733 strlcpy(**prompts + plen, msg, len - plen);
734 plen += mlen;
735 strlcat(**prompts + plen, "\n", len - plen);
736 plen++;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000737 xfree(msg);
738 break;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000739 case PAM_ACCT_EXPIRED:
740 sshpam_account_status = 0;
741 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000742 case PAM_AUTH_ERR:
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000743 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
Darren Tucker7b1e6952005-09-28 22:33:27 +1000744 if (**prompts != NULL && strlen(**prompts) != 0) {
745 *info = **prompts;
746 **prompts = NULL;
747 *num = 0;
748 **echo_on = 0;
749 ctxt->pam_done = -1;
Damien Miller66f9eb62006-03-18 23:04:49 +1100750 xfree(msg);
Darren Tucker7b1e6952005-09-28 22:33:27 +1000751 return 0;
752 }
753 /* FALLTHROUGH */
754 case PAM_SUCCESS:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000755 if (**prompts != NULL) {
756 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100757 debug("PAM: %s", **prompts);
758 buffer_append(&loginmsg, **prompts,
759 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000760 xfree(**prompts);
761 **prompts = NULL;
762 }
763 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100764 if (!sshpam_authctxt->valid ||
765 (sshpam_authctxt->pw->pw_uid == 0 &&
766 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100767 fatal("Internal error: PAM auth "
768 "succeeded when it should have "
769 "failed");
Damien Millerc756e9b2003-11-17 21:41:42 +1100770 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000771 *num = 0;
772 **echo_on = 0;
773 ctxt->pam_done = 1;
774 xfree(msg);
775 return (0);
776 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000777 error("PAM: %s for %s%.100s from %.100s", msg,
778 sshpam_authctxt->valid ? "" : "illegal user ",
779 sshpam_authctxt->user,
780 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000781 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000782 default:
783 *num = 0;
784 **echo_on = 0;
785 xfree(msg);
786 ctxt->pam_done = -1;
787 return (-1);
788 }
789 }
790 return (-1);
791}
792
793/* XXX - see also comment in auth-chall.c:verify_response */
794static int
795sshpam_respond(void *ctx, u_int num, char **resp)
796{
797 Buffer buffer;
798 struct pam_ctxt *ctxt = ctx;
799
Darren Tucker1d4ebbf2006-01-29 16:46:13 +1100800 debug2("PAM: %s entering, %u responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000801 switch (ctxt->pam_done) {
802 case 1:
803 sshpam_authenticated = 1;
804 return (0);
805 case 0:
806 break;
807 default:
808 return (-1);
809 }
810 if (num != 1) {
811 error("PAM: expected one response, got %u", num);
812 return (-1);
813 }
814 buffer_init(&buffer);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100815 if (sshpam_authctxt->valid &&
816 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller37294fb2005-07-17 17:18:49 +1000817 options.permit_root_login == PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100818 buffer_put_cstring(&buffer, *resp);
819 else
820 buffer_put_cstring(&buffer, badpw);
Damien Miller9bdba702003-11-17 21:27:55 +1100821 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
822 buffer_free(&buffer);
823 return (-1);
824 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000825 buffer_free(&buffer);
826 return (1);
827}
828
829static void
830sshpam_free_ctx(void *ctxtp)
831{
832 struct pam_ctxt *ctxt = ctxtp;
833
Darren Tuckera8df9242004-01-15 00:15:07 +1100834 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000835 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000836 xfree(ctxt);
837 /*
838 * We don't call sshpam_cleanup() here because we may need the PAM
839 * handle at a later stage, e.g. when setting up a session. It's
840 * still on the cleanup list, so pam_end() *will* be called before
841 * the server process terminates.
842 */
843}
844
845KbdintDevice sshpam_device = {
846 "pam",
847 sshpam_init_ctx,
848 sshpam_query,
849 sshpam_respond,
850 sshpam_free_ctx
851};
852
853KbdintDevice mm_sshpam_device = {
854 "pam",
855 mm_sshpam_init_ctx,
856 mm_sshpam_query,
857 mm_sshpam_respond,
858 mm_sshpam_free_ctx
859};
860
861/*
862 * This replaces auth-pam.c
863 */
864void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100865start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000866{
Damien Miller9d507da2003-05-14 15:31:12 +1000867 if (!options.use_pam)
868 fatal("PAM: initialisation requested when UsePAM=no");
869
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100870 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000871 fatal("PAM: initialisation failed");
872}
873
874void
875finish_pam(void)
876{
Darren Tucker8846a072003-10-07 11:30:15 +1000877 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000878}
879
Damien Miller1f499fd2003-08-25 13:08:49 +1000880u_int
881do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000882{
Darren Tucker77fc29e2004-09-11 23:07:03 +1000883 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +1100884 if (sshpam_account_status != -1)
885 return (sshpam_account_status);
886
Damien Miller1f499fd2003-08-25 13:08:49 +1000887 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000888 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
889 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +1000890
Darren Tucker07705c72003-12-18 15:34:31 +1100891 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
892 sshpam_account_status = 0;
893 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000894 }
895
Darren Tucker07705c72003-12-18 15:34:31 +1100896 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +1000897 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +1100898
899 sshpam_account_status = 1;
900 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000901}
902
903void
Damien Miller341c6e62003-09-02 23:18:52 +1000904do_pam_set_tty(const char *tty)
905{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000906 if (tty != NULL) {
907 debug("PAM: setting PAM_TTY to \"%s\"", tty);
908 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
909 if (sshpam_err != PAM_SUCCESS)
910 fatal("PAM: failed to set PAM_TTY: %s",
911 pam_strerror(sshpam_handle, sshpam_err));
912 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000913}
914
915void
916do_pam_setcred(int init)
917{
918 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +1000919 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000920 if (sshpam_err != PAM_SUCCESS)
921 fatal("PAM: failed to set PAM_CONV: %s",
922 pam_strerror(sshpam_handle, sshpam_err));
923 if (init) {
924 debug("PAM: establishing credentials");
925 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
926 } else {
927 debug("PAM: reinitializing credentials");
928 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
929 }
930 if (sshpam_err == PAM_SUCCESS) {
931 sshpam_cred_established = 1;
932 return;
933 }
934 if (sshpam_authenticated)
935 fatal("PAM: pam_setcred(): %s",
936 pam_strerror(sshpam_handle, sshpam_err));
937 else
938 debug("PAM: pam_setcred(): %s",
939 pam_strerror(sshpam_handle, sshpam_err));
940}
941
Damien Miller4f9f42a2003-05-10 19:28:02 +1000942static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000943sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000944 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000945{
946 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000947 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000948 int i;
949
Darren Tuckerba53b832004-02-17 20:46:59 +1100950 debug3("PAM: %s called with %d messages", __func__, n);
951
Damien Miller5c3a5582003-09-23 22:12:38 +1000952 *resp = NULL;
953
Darren Tucker18df00c2003-11-18 12:42:07 +1100954 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000955 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000956
Darren Tuckerd8093e42006-05-04 16:24:34 +1000957 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000958 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000959
Damien Miller4f9f42a2003-05-10 19:28:02 +1000960 for (i = 0; i < n; ++i) {
961 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
962 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000963 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100964 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000965 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000966 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000967 break;
968 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100969 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000970 fgets(input, sizeof input, stdin);
Damien Millera6fb77f2004-07-19 09:39:11 +1000971 if ((reply[i].resp = strdup(input)) == NULL)
972 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000973 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000974 break;
975 case PAM_ERROR_MSG:
976 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100977 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000978 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000979 break;
980 default:
981 goto fail;
982 }
983 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000984 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000985 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000986
Damien Miller4f9f42a2003-05-10 19:28:02 +1000987 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000988 for(i = 0; i < n; i++) {
989 if (reply[i].resp != NULL)
990 xfree(reply[i].resp);
991 }
992 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000993 return (PAM_CONV_ERR);
994}
995
Darren Tucker94befab2004-06-03 14:53:12 +1000996static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +1100997
Damien Miller4f9f42a2003-05-10 19:28:02 +1000998/*
999 * XXX this should be done in the authentication phase, but ssh1 doesn't
1000 * support that
1001 */
1002void
1003do_pam_chauthtok(void)
1004{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001005 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +10001006 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +10001007 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +11001008 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001009 if (sshpam_err != PAM_SUCCESS)
1010 fatal("PAM: failed to set PAM_CONV: %s",
1011 pam_strerror(sshpam_handle, sshpam_err));
1012 debug("PAM: changing password");
1013 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1014 if (sshpam_err != PAM_SUCCESS)
1015 fatal("PAM: pam_chauthtok(): %s",
1016 pam_strerror(sshpam_handle, sshpam_err));
1017}
1018
Darren Tucker18df00c2003-11-18 12:42:07 +11001019void
1020do_pam_session(void)
1021{
Darren Tucker1921ed92004-02-10 13:23:28 +11001022 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +11001023 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +11001024 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +11001025 if (sshpam_err != PAM_SUCCESS)
1026 fatal("PAM: failed to set PAM_CONV: %s",
1027 pam_strerror(sshpam_handle, sshpam_err));
1028 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +10001029 if (sshpam_err == PAM_SUCCESS)
1030 sshpam_session_open = 1;
1031 else {
1032 sshpam_session_open = 0;
1033 disable_forwarding();
1034 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001035 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001036 }
1037
1038}
1039
1040int
1041is_pam_session_open(void)
1042{
1043 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001044}
1045
Damien Millera8e06ce2003-11-21 23:48:55 +11001046/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001047 * Set a PAM environment string. We need to do this so that the session
1048 * modules can handle things like Kerberos/GSI credentials that appear
1049 * during the ssh authentication process.
1050 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001051int
Damien Millera8e06ce2003-11-21 23:48:55 +11001052do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001053{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001054 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +11001055#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +10001056 char *compound;
1057 size_t len;
1058
1059 len = strlen(name) + strlen(value) + 2;
1060 compound = xmalloc(len);
1061
1062 snprintf(compound, len, "%s=%s", name, value);
1063 ret = pam_putenv(sshpam_handle, compound);
1064 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001065#endif
Damien Millerf2728092003-09-17 07:24:25 +10001066
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001067 return (ret);
1068}
1069
Damien Miller4f9f42a2003-05-10 19:28:02 +10001070char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001071fetch_pam_child_environment(void)
1072{
1073 return sshpam_env;
1074}
1075
1076char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001077fetch_pam_environment(void)
1078{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001079 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001080}
1081
1082void
1083free_pam_environment(char **env)
1084{
1085 char **envp;
1086
Damien Millere27c6cc2003-05-16 18:21:01 +10001087 if (env == NULL)
1088 return;
1089
Damien Miller4f9f42a2003-05-10 19:28:02 +10001090 for (envp = env; *envp; envp++)
1091 xfree(*envp);
1092 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001093}
1094
Darren Tucker450a1582004-05-30 20:43:59 +10001095/*
1096 * "Blind" conversation function for password authentication. Assumes that
1097 * echo-off prompts are for the password and stores messages for later
1098 * display.
1099 */
1100static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001101sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001102 struct pam_response **resp, void *data)
1103{
1104 struct pam_response *reply;
1105 int i;
1106 size_t len;
1107
1108 debug3("PAM: %s called with %d messages", __func__, n);
1109
1110 *resp = NULL;
1111
1112 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1113 return (PAM_CONV_ERR);
1114
1115 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1116 return (PAM_CONV_ERR);
1117 memset(reply, 0, n * sizeof(*reply));
1118
1119 for (i = 0; i < n; ++i) {
1120 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1121 case PAM_PROMPT_ECHO_OFF:
1122 if (sshpam_password == NULL)
1123 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001124 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1125 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001126 reply[i].resp_retcode = PAM_SUCCESS;
1127 break;
1128 case PAM_ERROR_MSG:
1129 case PAM_TEXT_INFO:
1130 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1131 if (len > 0) {
1132 buffer_append(&loginmsg,
1133 PAM_MSG_MEMBER(msg, i, msg), len);
1134 buffer_append(&loginmsg, "\n", 1);
1135 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001136 if ((reply[i].resp = strdup("")) == NULL)
1137 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001138 reply[i].resp_retcode = PAM_SUCCESS;
1139 break;
1140 default:
1141 goto fail;
1142 }
1143 }
1144 *resp = reply;
1145 return (PAM_SUCCESS);
1146
Damien Miller94cf4c82005-07-17 17:04:47 +10001147 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001148 for(i = 0; i < n; i++) {
1149 if (reply[i].resp != NULL)
1150 xfree(reply[i].resp);
1151 }
1152 xfree(reply);
1153 return (PAM_CONV_ERR);
1154}
1155
1156static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1157
1158/*
1159 * Attempt password authentication via PAM
1160 */
1161int
1162sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1163{
1164 int flags = (options.permit_empty_passwd == 0 ?
1165 PAM_DISALLOW_NULL_AUTHTOK : 0);
1166
1167 if (!options.use_pam || sshpam_handle == NULL)
1168 fatal("PAM: %s called when PAM disabled or failed to "
1169 "initialise.", __func__);
1170
1171 sshpam_password = password;
1172 sshpam_authctxt = authctxt;
1173
Darren Tuckere061b152004-05-30 22:04:56 +10001174 /*
1175 * If the user logging in is invalid, or is root but is not permitted
1176 * by PermitRootLogin, use an invalid password to prevent leaking
1177 * information via timing (eg if the PAM config has a delay on fail).
1178 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001179 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001180 options.permit_root_login != PERMIT_YES))
Darren Tuckere061b152004-05-30 22:04:56 +10001181 sshpam_password = badpw;
1182
Darren Tucker450a1582004-05-30 20:43:59 +10001183 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1184 (const void *)&passwd_conv);
1185 if (sshpam_err != PAM_SUCCESS)
1186 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1187 pam_strerror(sshpam_handle, sshpam_err));
1188
1189 sshpam_err = pam_authenticate(sshpam_handle, flags);
1190 sshpam_password = NULL;
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001191 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001192 debug("PAM: password authentication accepted for %.100s",
1193 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001194 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001195 } else {
1196 debug("PAM: password authentication failed for %.100s: %s",
1197 authctxt->valid ? authctxt->user : "an illegal user",
1198 pam_strerror(sshpam_handle, sshpam_err));
1199 return 0;
1200 }
1201}
Damien Millere72b7af1999-12-30 15:08:44 +11001202#endif /* USE_PAM */