blob: 8e9361caa796375de9e3aceeb437cdf4da90db75 [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
Damien Miller2ab323e2006-08-05 12:43:32 +100074/* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
75#ifdef PAM_SUN_CODEBASE
76# define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
77#else
78# define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
79#endif
80
Damien Miller75bb6642006-08-05 14:07:20 +100081#include "xmalloc.h"
82#include "buffer.h"
83#include "key.h"
84#include "hostfile.h"
Kevin Stevese683e762002-04-04 19:02:28 +000085#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110086#include "auth-pam.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000087#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100088#include "log.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100089#include "msg.h"
90#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100091#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100092#include "servconf.h"
93#include "ssh2.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100094#include "auth-options.h"
Damien Miller75bb6642006-08-05 14:07:20 +100095#ifdef GSSAPI
96#include "ssh-gss.h"
97#endif
98#include "monitor_wrap.h"
Damien Millere72b7af1999-12-30 15:08:44 +110099
Damien Miller4e448a32003-05-14 15:11:48 +1000100extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +1100101extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +1100102extern int compat20;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000103extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +1000104
Darren Tucker328118a2005-05-25 16:18:09 +1000105/* so we don't silently change behaviour */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000106#ifdef USE_POSIX_THREADS
Darren Tucker328118a2005-05-25 16:18:09 +1000107# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
108#endif
109
110/*
111 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
112 * and generally a bad idea. Use at own risk and do not expect support if
113 * this breaks.
114 */
115#ifdef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000116#include <pthread.h>
117/*
Damien Millera8e06ce2003-11-21 23:48:55 +1100118 * Avoid namespace clash when *not* using pthreads for systems *with*
119 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +1000120 * (e.g. Linux)
121 */
Damien Millera8e06ce2003-11-21 23:48:55 +1100122typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000123#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100124typedef pid_t sp_pthread_t;
125#endif
126
127struct pam_ctxt {
128 sp_pthread_t pam_thread;
129 int pam_psock;
130 int pam_csock;
131 int pam_done;
132};
133
134static void sshpam_free_ctx(void *);
135static struct pam_ctxt *cleanup_ctxt;
136
Darren Tucker328118a2005-05-25 16:18:09 +1000137#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000138/*
139 * Simulate threads with processes.
140 */
Kevin Steves63007d42002-07-21 17:57:01 +0000141
Darren Tucker749bc952004-01-14 22:14:04 +1100142static int sshpam_thread_status = -1;
143static mysig_t sshpam_oldsig;
144
Damien Miller94cf4c82005-07-17 17:04:47 +1000145static void
Darren Tucker749bc952004-01-14 22:14:04 +1100146sshpam_sigchld_handler(int sig)
147{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000148 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100149 if (cleanup_ctxt == NULL)
150 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000151 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000152 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000153 /* PAM thread has not exitted, privsep slave must have */
154 kill(cleanup_ctxt->pam_thread, SIGTERM);
155 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
Darren Tucker5d423f42004-07-11 16:54:08 +1000156 <= 0)
Darren Tuckerb53355e2004-05-24 11:55:36 +1000157 return; /* could not wait */
158 }
Darren Tucker749bc952004-01-14 22:14:04 +1100159 if (WIFSIGNALED(sshpam_thread_status) &&
160 WTERMSIG(sshpam_thread_status) == SIGTERM)
161 return; /* terminated by pthread_cancel */
162 if (!WIFEXITED(sshpam_thread_status))
163 fatal("PAM: authentication thread exited unexpectedly");
164 if (WEXITSTATUS(sshpam_thread_status) != 0)
165 fatal("PAM: authentication thread exited uncleanly");
166}
167
Damien Millerb8fe89c2006-07-24 14:51:00 +1000168/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000169static void
Damien Millerb8fe89c2006-07-24 14:51:00 +1000170pthread_exit(void *value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000171{
172 _exit(0);
173}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000174
Damien Millerb8fe89c2006-07-24 14:51:00 +1000175/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000176static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000177pthread_create(sp_pthread_t *thread, const void *attr,
Damien Miller4f9f42a2003-05-10 19:28:02 +1000178 void *(*thread_start)(void *), void *arg)
179{
180 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000181 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100182
Darren Tuckerb9b60212004-03-04 20:03:54 +1100183 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000184 switch ((pid = fork())) {
185 case -1:
186 error("fork(): %s", strerror(errno));
187 return (-1);
188 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000189 close(ctx->pam_psock);
190 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000191 thread_start(arg);
192 _exit(1);
193 default:
194 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000195 close(ctx->pam_csock);
196 ctx->pam_csock = -1;
Darren Tucker749bc952004-01-14 22:14:04 +1100197 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000198 return (0);
199 }
200}
Damien Millere72b7af1999-12-30 15:08:44 +1100201
Damien Miller4f9f42a2003-05-10 19:28:02 +1000202static int
203pthread_cancel(sp_pthread_t thread)
204{
Darren Tucker7ae09622004-01-14 23:07:56 +1100205 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000206 return (kill(thread, SIGTERM));
207}
208
Damien Millerb8fe89c2006-07-24 14:51:00 +1000209/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000210static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000211pthread_join(sp_pthread_t thread, void **value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000212{
213 int status;
214
Darren Tucker749bc952004-01-14 22:14:04 +1100215 if (sshpam_thread_status != -1)
216 return (sshpam_thread_status);
217 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000218 waitpid(thread, &status, 0);
219 return (status);
220}
221#endif
222
223
Damien Miller5c3a5582003-09-23 22:12:38 +1000224static pam_handle_t *sshpam_handle = NULL;
225static int sshpam_err = 0;
226static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000227static int sshpam_session_open = 0;
228static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100229static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100230static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000231static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000232static const char *sshpam_password = NULL;
Darren Tucker36a3d602005-01-20 12:43:38 +1100233static char badpw[] = "\b\n\r\177INCORRECT";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000234
Damien Millerc756e9b2003-11-17 21:41:42 +1100235/* Some PAM implementations don't implement this */
236#ifndef HAVE_PAM_GETENVLIST
237static char **
238pam_getenvlist(pam_handle_t *pamh)
239{
240 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100241 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100242 * for platforms without pam_getenvlist by searching for known
243 * env vars (e.g. KRB5CCNAME) from the PAM environment.
244 */
245 return NULL;
246}
247#endif
248
Darren Tucker21dd0892004-08-16 23:12:05 +1000249/*
250 * Some platforms, notably Solaris, do not enforce password complexity
251 * rules during pam_chauthtok() if the real uid of the calling process
252 * is 0, on the assumption that it's being called by "passwd" run by root.
253 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
254 * the right thing.
255 */
256#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
257static int
258sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
259{
260 int result;
261
262 if (sshpam_authctxt == NULL)
263 fatal("PAM: sshpam_authctxt not initialized");
264 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
265 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
266 result = pam_chauthtok(pamh, flags);
267 if (setreuid(0, -1) == -1)
268 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
269 return result;
270}
271# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
272#endif
273
Darren Tucker07705c72003-12-18 15:34:31 +1100274void
Darren Tucker17db1c42004-06-19 12:54:38 +1000275sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100276{
Darren Tuckera8df9242004-01-15 00:15:07 +1100277 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000278 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100279 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000280 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100281 if (reqd) {
282 no_port_forwarding_flag |= 2;
283 no_agent_forwarding_flag |= 2;
284 no_x11_forwarding_flag |= 2;
285 } else {
286 no_port_forwarding_flag &= ~2;
287 no_agent_forwarding_flag &= ~2;
288 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100289 }
290}
Darren Tucker1921ed92004-02-10 13:23:28 +1100291
Damien Millerc756e9b2003-11-17 21:41:42 +1100292/* Import regular and PAM environment from subprocess */
293static void
294import_environments(Buffer *b)
295{
296 char *env;
297 u_int i, num_env;
298 int err;
299
Darren Tuckera8df9242004-01-15 00:15:07 +1100300 debug3("PAM: %s entering", __func__);
301
Darren Tucker328118a2005-05-25 16:18:09 +1000302#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100303 /* Import variables set by do_pam_account */
304 sshpam_account_status = buffer_get_int(b);
Darren Tucker17db1c42004-06-19 12:54:38 +1000305 sshpam_password_change_required(buffer_get_int(b));
Darren Tucker07705c72003-12-18 15:34:31 +1100306
Damien Millerc756e9b2003-11-17 21:41:42 +1100307 /* Import environment from subprocess */
308 num_env = buffer_get_int(b);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000309 if (num_env > 1024)
310 fatal("%s: received %u environment variables, expected <= 1024",
311 __func__, num_env);
312 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
Damien Millerc756e9b2003-11-17 21:41:42 +1100313 debug3("PAM: num env strings %d", num_env);
314 for(i = 0; i < num_env; i++)
315 sshpam_env[i] = buffer_get_string(b, NULL);
316
317 sshpam_env[num_env] = NULL;
318
319 /* Import PAM environment from subprocess */
320 num_env = buffer_get_int(b);
321 debug("PAM: num PAM env strings %d", num_env);
322 for(i = 0; i < num_env; i++) {
323 env = buffer_get_string(b, NULL);
324
Darren Tucker8a1624c2003-11-18 12:45:35 +1100325#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100326 /* Errors are not fatal here */
327 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
328 error("PAM: pam_putenv: %s",
329 pam_strerror(sshpam_handle, sshpam_err));
330 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100331#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100332 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100333#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100334}
335
Damien Miller9d5705a2000-09-16 16:09:27 +1100336/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000337 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100338 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000339static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000340sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000341 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100342{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000343 Buffer buffer;
344 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000345 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000346 int i;
347
Darren Tuckerba53b832004-02-17 20:46:59 +1100348 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000349 *resp = NULL;
350
Darren Tucker59e06022004-06-30 20:34:31 +1000351 if (data == NULL) {
352 error("PAM: conversation function passed a null context");
353 return (PAM_CONV_ERR);
354 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000355 ctxt = data;
356 if (n <= 0 || n > PAM_MAX_NUM_MSG)
357 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000358
Darren Tuckerd8093e42006-05-04 16:24:34 +1000359 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000360 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000361
Damien Miller4f9f42a2003-05-10 19:28:02 +1000362 buffer_init(&buffer);
363 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000364 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
365 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100366 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000367 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100368 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100369 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
370 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100371 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100372 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000373 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
374 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000375 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000376 break;
377 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100378 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000379 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100380 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100381 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
382 goto fail;
383 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
384 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000385 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
386 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000387 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000388 break;
389 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100390 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000391 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100392 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100393 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
394 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000395 break;
396 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100397 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000398 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100399 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100400 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
401 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000402 break;
403 default:
404 goto fail;
405 }
406 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000407 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000408 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000409 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000410 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000411
Damien Miller4f9f42a2003-05-10 19:28:02 +1000412 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000413 for(i = 0; i < n; i++) {
414 if (reply[i].resp != NULL)
415 xfree(reply[i].resp);
416 }
417 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000418 buffer_free(&buffer);
419 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000420}
421
Damien Miller4f9f42a2003-05-10 19:28:02 +1000422/*
423 * Authentication thread.
424 */
425static void *
426sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100427{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000428 struct pam_ctxt *ctxt = ctxtp;
429 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000430 struct pam_conv sshpam_conv;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000431 int flags = (options.permit_empty_passwd == 0 ?
432 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000433#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100434 extern char **environ;
435 char **env_from_pam;
436 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000437 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000438 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000439
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000440 pam_get_item(sshpam_handle, PAM_USER,
441 (sshpam_const void **)ptr_pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100442 environ[0] = NULL;
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000443
444 if (sshpam_authctxt != NULL) {
445 setproctitle("%s [pam]",
446 sshpam_authctxt->valid ? pam_user : "unknown");
447 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000448#endif
449
Damien Millerf4b6f102003-09-02 23:12:06 +1000450 sshpam_conv.conv = sshpam_thread_conv;
451 sshpam_conv.appdata_ptr = ctxt;
452
Darren Tucker17addf02004-03-30 20:57:57 +1000453 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100454 fatal("%s: PAM authctxt not initialized", __func__);
455
Damien Miller4f9f42a2003-05-10 19:28:02 +1000456 buffer_init(&buffer);
457 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
458 (const void *)&sshpam_conv);
459 if (sshpam_err != PAM_SUCCESS)
460 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000461 sshpam_err = pam_authenticate(sshpam_handle, flags);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000462 if (sshpam_err != PAM_SUCCESS)
463 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100464
Darren Tuckerc376c862003-12-18 16:08:59 +1100465 if (compat20) {
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000466 if (!do_pam_account()) {
467 sshpam_err = PAM_ACCT_EXPIRED;
Darren Tucker07705c72003-12-18 15:34:31 +1100468 goto auth_fail;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000469 }
Darren Tucker17addf02004-03-30 20:57:57 +1000470 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100471 sshpam_err = pam_chauthtok(sshpam_handle,
472 PAM_CHANGE_EXPIRED_AUTHTOK);
473 if (sshpam_err != PAM_SUCCESS)
474 goto auth_fail;
Darren Tucker17db1c42004-06-19 12:54:38 +1000475 sshpam_password_change_required(0);
Darren Tucker07705c72003-12-18 15:34:31 +1100476 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100477 }
Darren Tucker07705c72003-12-18 15:34:31 +1100478
Damien Miller4f9f42a2003-05-10 19:28:02 +1000479 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100480
Darren Tucker328118a2005-05-25 16:18:09 +1000481#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100482 /* Export variables set by do_pam_account */
483 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000484 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100485
Damien Millerc756e9b2003-11-17 21:41:42 +1100486 /* Export any environment strings set in child */
487 for(i = 0; environ[i] != NULL; i++)
488 ; /* Count */
489 buffer_put_int(&buffer, i);
490 for(i = 0; environ[i] != NULL; i++)
491 buffer_put_cstring(&buffer, environ[i]);
492
493 /* Export any environment strings set by PAM in child */
494 env_from_pam = pam_getenvlist(sshpam_handle);
495 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
496 ; /* Count */
497 buffer_put_int(&buffer, i);
498 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
499 buffer_put_cstring(&buffer, env_from_pam[i]);
Darren Tucker328118a2005-05-25 16:18:09 +1000500#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100501
Damien Miller9bdba702003-11-17 21:27:55 +1100502 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000503 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
504 buffer_free(&buffer);
505 pthread_exit(NULL);
506
507 auth_fail:
508 buffer_put_cstring(&buffer,
509 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100510 /* XXX - can't do much about an error here */
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000511 if (sshpam_err == PAM_ACCT_EXPIRED)
512 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
513 else
514 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000515 buffer_free(&buffer);
516 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100517
Damien Miller4f9f42a2003-05-10 19:28:02 +1000518 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000519}
520
Darren Tucker8846a072003-10-07 11:30:15 +1000521void
522sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000523{
Darren Tucker8846a072003-10-07 11:30:15 +1000524 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000525
Darren Tuckera8df9242004-01-15 00:15:07 +1100526 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000527 if (ctxt != NULL && ctxt->pam_thread != 0) {
528 pthread_cancel(ctxt->pam_thread);
529 pthread_join(ctxt->pam_thread, NULL);
530 close(ctxt->pam_psock);
531 close(ctxt->pam_csock);
532 memset(ctxt, 0, sizeof(*ctxt));
533 cleanup_ctxt = NULL;
534 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000535}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000536
Damien Miller4f9f42a2003-05-10 19:28:02 +1000537static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000538sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000539 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000540{
Darren Tuckerba53b832004-02-17 20:46:59 +1100541 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000542 return (PAM_CONV_ERR);
543}
Damien Miller63dc3e92001-02-07 12:58:33 +1100544
Damien Miller4f9f42a2003-05-10 19:28:02 +1000545static struct pam_conv null_conv = { sshpam_null_conv, NULL };
546
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000547static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000548sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000549 struct pam_response **resp, void *data)
550{
551 struct pam_response *reply;
552 int i;
553 size_t len;
554
555 debug3("PAM: %s called with %d messages", __func__, n);
556 *resp = NULL;
557
558 if (n <= 0 || n > PAM_MAX_NUM_MSG)
559 return (PAM_CONV_ERR);
560
Darren Tuckerd8093e42006-05-04 16:24:34 +1000561 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000562 return (PAM_CONV_ERR);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000563
564 for (i = 0; i < n; ++i) {
565 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
566 case PAM_ERROR_MSG:
567 case PAM_TEXT_INFO:
568 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
569 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
570 buffer_append(&loginmsg, "\n", 1 );
571 reply[i].resp_retcode = PAM_SUCCESS;
572 break;
573 default:
574 goto fail;
575 }
576 }
577 *resp = reply;
578 return (PAM_SUCCESS);
579
580 fail:
581 for(i = 0; i < n; i++) {
582 if (reply[i].resp != NULL)
583 xfree(reply[i].resp);
584 }
585 xfree(reply);
586 return (PAM_CONV_ERR);
587}
588
589static struct pam_conv store_conv = { sshpam_store_conv, NULL };
590
Darren Tucker8846a072003-10-07 11:30:15 +1000591void
592sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000593{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000594 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000595 if (sshpam_handle == NULL)
596 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000597 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
598 if (sshpam_cred_established) {
599 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
600 sshpam_cred_established = 0;
601 }
602 if (sshpam_session_open) {
603 pam_close_session(sshpam_handle, PAM_SILENT);
604 sshpam_session_open = 0;
605 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100606 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000607 pam_end(sshpam_handle, sshpam_err);
608 sshpam_handle = NULL;
609}
610
611static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100612sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000613{
Darren Tucker455813b2003-09-13 22:12:11 +1000614 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100615 const char *pam_rhost, *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000616 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000617
618 if (sshpam_handle != NULL) {
619 /* We already have a PAM context; check if the user matches */
620 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000621 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000622 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
623 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000624 pam_end(sshpam_handle, sshpam_err);
625 sshpam_handle = NULL;
626 }
627 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000628 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000629 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000630 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100631
Damien Miller4f9f42a2003-05-10 19:28:02 +1000632 if (sshpam_err != PAM_SUCCESS) {
633 pam_end(sshpam_handle, sshpam_err);
634 sshpam_handle = NULL;
635 return (-1);
636 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000637 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000638 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000639 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
640 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000641 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000642 sshpam_handle = NULL;
643 return (-1);
644 }
645#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100646 /*
647 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
648 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000649 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100650 */
Damien Miller25d93422003-05-18 20:45:47 +1000651 debug("PAM: setting PAM_TTY to \"ssh\"");
652 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
653 if (sshpam_err != PAM_SUCCESS) {
654 pam_end(sshpam_handle, sshpam_err);
655 sshpam_handle = NULL;
656 return (-1);
657 }
658#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000659 return (0);
660}
661
662static void *
663sshpam_init_ctx(Authctxt *authctxt)
664{
665 struct pam_ctxt *ctxt;
666 int socks[2];
667
Darren Tuckera8df9242004-01-15 00:15:07 +1100668 debug3("PAM: %s entering", __func__);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000669 /*
670 * Refuse to start if we don't have PAM enabled or do_pam_account
671 * has previously failed.
672 */
673 if (!options.use_pam || sshpam_account_status == 0)
Damien Miller4e448a32003-05-14 15:11:48 +1000674 return NULL;
675
Damien Miller4f9f42a2003-05-10 19:28:02 +1000676 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100677 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000678 error("PAM: initialization failed");
679 return (NULL);
680 }
681
682 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000683 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000684
685 /* Start the authentication thread */
686 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
687 error("PAM: failed create sockets: %s", strerror(errno));
688 xfree(ctxt);
689 return (NULL);
690 }
691 ctxt->pam_psock = socks[0];
692 ctxt->pam_csock = socks[1];
693 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
694 error("PAM: failed to start authentication thread: %s",
695 strerror(errno));
696 close(socks[0]);
697 close(socks[1]);
698 xfree(ctxt);
699 return (NULL);
700 }
Darren Tucker8846a072003-10-07 11:30:15 +1000701 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000702 return (ctxt);
703}
704
705static int
706sshpam_query(void *ctx, char **name, char **info,
707 u_int *num, char ***prompts, u_int **echo_on)
708{
709 Buffer buffer;
710 struct pam_ctxt *ctxt = ctx;
711 size_t plen;
712 u_char type;
713 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000714 size_t len, mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000715
Darren Tuckera8df9242004-01-15 00:15:07 +1100716 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000717 buffer_init(&buffer);
718 *name = xstrdup("");
719 *info = xstrdup("");
720 *prompts = xmalloc(sizeof(char *));
721 **prompts = NULL;
722 plen = 0;
723 *echo_on = xmalloc(sizeof(u_int));
724 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
725 type = buffer_get_char(&buffer);
726 msg = buffer_get_string(&buffer, NULL);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000727 mlen = strlen(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000728 switch (type) {
729 case PAM_PROMPT_ECHO_ON:
730 case PAM_PROMPT_ECHO_OFF:
731 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000732 len = plen + mlen + 1;
Damien Miller36812092006-03-26 14:22:47 +1100733 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000734 strlcpy(**prompts + plen, msg, len - plen);
735 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000736 **echo_on = (type == PAM_PROMPT_ECHO_ON);
737 xfree(msg);
738 return (0);
739 case PAM_ERROR_MSG:
740 case PAM_TEXT_INFO:
741 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000742 len = plen + mlen + 2;
Damien Miller36812092006-03-26 14:22:47 +1100743 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000744 strlcpy(**prompts + plen, msg, len - plen);
745 plen += mlen;
746 strlcat(**prompts + plen, "\n", len - plen);
747 plen++;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000748 xfree(msg);
749 break;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000750 case PAM_ACCT_EXPIRED:
751 sshpam_account_status = 0;
752 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000753 case PAM_AUTH_ERR:
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000754 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
Darren Tucker7b1e6952005-09-28 22:33:27 +1000755 if (**prompts != NULL && strlen(**prompts) != 0) {
756 *info = **prompts;
757 **prompts = NULL;
758 *num = 0;
759 **echo_on = 0;
760 ctxt->pam_done = -1;
Damien Miller66f9eb62006-03-18 23:04:49 +1100761 xfree(msg);
Darren Tucker7b1e6952005-09-28 22:33:27 +1000762 return 0;
763 }
764 /* FALLTHROUGH */
765 case PAM_SUCCESS:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000766 if (**prompts != NULL) {
767 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100768 debug("PAM: %s", **prompts);
769 buffer_append(&loginmsg, **prompts,
770 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000771 xfree(**prompts);
772 **prompts = NULL;
773 }
774 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100775 if (!sshpam_authctxt->valid ||
776 (sshpam_authctxt->pw->pw_uid == 0 &&
777 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100778 fatal("Internal error: PAM auth "
779 "succeeded when it should have "
780 "failed");
Damien Millerc756e9b2003-11-17 21:41:42 +1100781 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000782 *num = 0;
783 **echo_on = 0;
784 ctxt->pam_done = 1;
785 xfree(msg);
786 return (0);
787 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000788 error("PAM: %s for %s%.100s from %.100s", msg,
789 sshpam_authctxt->valid ? "" : "illegal user ",
790 sshpam_authctxt->user,
791 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000792 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000793 default:
794 *num = 0;
795 **echo_on = 0;
796 xfree(msg);
797 ctxt->pam_done = -1;
798 return (-1);
799 }
800 }
801 return (-1);
802}
803
804/* XXX - see also comment in auth-chall.c:verify_response */
805static int
806sshpam_respond(void *ctx, u_int num, char **resp)
807{
808 Buffer buffer;
809 struct pam_ctxt *ctxt = ctx;
810
Darren Tucker1d4ebbf2006-01-29 16:46:13 +1100811 debug2("PAM: %s entering, %u responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000812 switch (ctxt->pam_done) {
813 case 1:
814 sshpam_authenticated = 1;
815 return (0);
816 case 0:
817 break;
818 default:
819 return (-1);
820 }
821 if (num != 1) {
822 error("PAM: expected one response, got %u", num);
823 return (-1);
824 }
825 buffer_init(&buffer);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100826 if (sshpam_authctxt->valid &&
827 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller37294fb2005-07-17 17:18:49 +1000828 options.permit_root_login == PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100829 buffer_put_cstring(&buffer, *resp);
830 else
831 buffer_put_cstring(&buffer, badpw);
Damien Miller9bdba702003-11-17 21:27:55 +1100832 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
833 buffer_free(&buffer);
834 return (-1);
835 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000836 buffer_free(&buffer);
837 return (1);
838}
839
840static void
841sshpam_free_ctx(void *ctxtp)
842{
843 struct pam_ctxt *ctxt = ctxtp;
844
Darren Tuckera8df9242004-01-15 00:15:07 +1100845 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000846 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000847 xfree(ctxt);
848 /*
849 * We don't call sshpam_cleanup() here because we may need the PAM
850 * handle at a later stage, e.g. when setting up a session. It's
851 * still on the cleanup list, so pam_end() *will* be called before
852 * the server process terminates.
853 */
854}
855
856KbdintDevice sshpam_device = {
857 "pam",
858 sshpam_init_ctx,
859 sshpam_query,
860 sshpam_respond,
861 sshpam_free_ctx
862};
863
864KbdintDevice mm_sshpam_device = {
865 "pam",
866 mm_sshpam_init_ctx,
867 mm_sshpam_query,
868 mm_sshpam_respond,
869 mm_sshpam_free_ctx
870};
871
872/*
873 * This replaces auth-pam.c
874 */
875void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100876start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000877{
Damien Miller9d507da2003-05-14 15:31:12 +1000878 if (!options.use_pam)
879 fatal("PAM: initialisation requested when UsePAM=no");
880
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100881 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000882 fatal("PAM: initialisation failed");
883}
884
885void
886finish_pam(void)
887{
Darren Tucker8846a072003-10-07 11:30:15 +1000888 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000889}
890
Damien Miller1f499fd2003-08-25 13:08:49 +1000891u_int
892do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000893{
Darren Tucker77fc29e2004-09-11 23:07:03 +1000894 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +1100895 if (sshpam_account_status != -1)
896 return (sshpam_account_status);
897
Damien Miller1f499fd2003-08-25 13:08:49 +1000898 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000899 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
900 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +1000901
Darren Tucker07705c72003-12-18 15:34:31 +1100902 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
903 sshpam_account_status = 0;
904 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000905 }
906
Darren Tucker07705c72003-12-18 15:34:31 +1100907 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +1000908 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +1100909
910 sshpam_account_status = 1;
911 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000912}
913
914void
Damien Miller341c6e62003-09-02 23:18:52 +1000915do_pam_set_tty(const char *tty)
916{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000917 if (tty != NULL) {
918 debug("PAM: setting PAM_TTY to \"%s\"", tty);
919 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
920 if (sshpam_err != PAM_SUCCESS)
921 fatal("PAM: failed to set PAM_TTY: %s",
922 pam_strerror(sshpam_handle, sshpam_err));
923 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000924}
925
926void
927do_pam_setcred(int init)
928{
929 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +1000930 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000931 if (sshpam_err != PAM_SUCCESS)
932 fatal("PAM: failed to set PAM_CONV: %s",
933 pam_strerror(sshpam_handle, sshpam_err));
934 if (init) {
935 debug("PAM: establishing credentials");
936 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
937 } else {
938 debug("PAM: reinitializing credentials");
939 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
940 }
941 if (sshpam_err == PAM_SUCCESS) {
942 sshpam_cred_established = 1;
943 return;
944 }
945 if (sshpam_authenticated)
946 fatal("PAM: pam_setcred(): %s",
947 pam_strerror(sshpam_handle, sshpam_err));
948 else
949 debug("PAM: pam_setcred(): %s",
950 pam_strerror(sshpam_handle, sshpam_err));
951}
952
Damien Miller4f9f42a2003-05-10 19:28:02 +1000953static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000954sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000955 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000956{
957 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000958 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000959 int i;
960
Darren Tuckerba53b832004-02-17 20:46:59 +1100961 debug3("PAM: %s called with %d messages", __func__, n);
962
Damien Miller5c3a5582003-09-23 22:12:38 +1000963 *resp = NULL;
964
Darren Tucker18df00c2003-11-18 12:42:07 +1100965 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000966 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000967
Darren Tuckerd8093e42006-05-04 16:24:34 +1000968 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000969 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000970
Damien Miller4f9f42a2003-05-10 19:28:02 +1000971 for (i = 0; i < n; ++i) {
972 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
973 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000974 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100975 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000976 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000977 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000978 break;
979 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100980 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000981 fgets(input, sizeof input, stdin);
Damien Millera6fb77f2004-07-19 09:39:11 +1000982 if ((reply[i].resp = strdup(input)) == NULL)
983 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000984 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000985 break;
986 case PAM_ERROR_MSG:
987 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100988 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000989 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000990 break;
991 default:
992 goto fail;
993 }
994 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000995 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000996 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000997
Damien Miller4f9f42a2003-05-10 19:28:02 +1000998 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000999 for(i = 0; i < n; i++) {
1000 if (reply[i].resp != NULL)
1001 xfree(reply[i].resp);
1002 }
1003 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001004 return (PAM_CONV_ERR);
1005}
1006
Darren Tucker94befab2004-06-03 14:53:12 +10001007static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +11001008
Damien Miller4f9f42a2003-05-10 19:28:02 +10001009/*
1010 * XXX this should be done in the authentication phase, but ssh1 doesn't
1011 * support that
1012 */
1013void
1014do_pam_chauthtok(void)
1015{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001016 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +10001017 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +10001018 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +11001019 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001020 if (sshpam_err != PAM_SUCCESS)
1021 fatal("PAM: failed to set PAM_CONV: %s",
1022 pam_strerror(sshpam_handle, sshpam_err));
1023 debug("PAM: changing password");
1024 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1025 if (sshpam_err != PAM_SUCCESS)
1026 fatal("PAM: pam_chauthtok(): %s",
1027 pam_strerror(sshpam_handle, sshpam_err));
1028}
1029
Darren Tucker18df00c2003-11-18 12:42:07 +11001030void
1031do_pam_session(void)
1032{
Darren Tucker1921ed92004-02-10 13:23:28 +11001033 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +11001034 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +11001035 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +11001036 if (sshpam_err != PAM_SUCCESS)
1037 fatal("PAM: failed to set PAM_CONV: %s",
1038 pam_strerror(sshpam_handle, sshpam_err));
1039 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +10001040 if (sshpam_err == PAM_SUCCESS)
1041 sshpam_session_open = 1;
1042 else {
1043 sshpam_session_open = 0;
1044 disable_forwarding();
1045 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001046 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001047 }
1048
1049}
1050
1051int
1052is_pam_session_open(void)
1053{
1054 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001055}
1056
Damien Millera8e06ce2003-11-21 23:48:55 +11001057/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001058 * Set a PAM environment string. We need to do this so that the session
1059 * modules can handle things like Kerberos/GSI credentials that appear
1060 * during the ssh authentication process.
1061 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001062int
Damien Millera8e06ce2003-11-21 23:48:55 +11001063do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001064{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001065 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +11001066#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +10001067 char *compound;
1068 size_t len;
1069
1070 len = strlen(name) + strlen(value) + 2;
1071 compound = xmalloc(len);
1072
1073 snprintf(compound, len, "%s=%s", name, value);
1074 ret = pam_putenv(sshpam_handle, compound);
1075 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001076#endif
Damien Millerf2728092003-09-17 07:24:25 +10001077
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001078 return (ret);
1079}
1080
Damien Miller4f9f42a2003-05-10 19:28:02 +10001081char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001082fetch_pam_child_environment(void)
1083{
1084 return sshpam_env;
1085}
1086
1087char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001088fetch_pam_environment(void)
1089{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001090 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001091}
1092
1093void
1094free_pam_environment(char **env)
1095{
1096 char **envp;
1097
Damien Millere27c6cc2003-05-16 18:21:01 +10001098 if (env == NULL)
1099 return;
1100
Damien Miller4f9f42a2003-05-10 19:28:02 +10001101 for (envp = env; *envp; envp++)
1102 xfree(*envp);
1103 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001104}
1105
Darren Tucker450a1582004-05-30 20:43:59 +10001106/*
1107 * "Blind" conversation function for password authentication. Assumes that
1108 * echo-off prompts are for the password and stores messages for later
1109 * display.
1110 */
1111static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001112sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001113 struct pam_response **resp, void *data)
1114{
1115 struct pam_response *reply;
1116 int i;
1117 size_t len;
1118
1119 debug3("PAM: %s called with %d messages", __func__, n);
1120
1121 *resp = NULL;
1122
1123 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1124 return (PAM_CONV_ERR);
1125
1126 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1127 return (PAM_CONV_ERR);
1128 memset(reply, 0, n * sizeof(*reply));
1129
1130 for (i = 0; i < n; ++i) {
1131 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1132 case PAM_PROMPT_ECHO_OFF:
1133 if (sshpam_password == NULL)
1134 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001135 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1136 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001137 reply[i].resp_retcode = PAM_SUCCESS;
1138 break;
1139 case PAM_ERROR_MSG:
1140 case PAM_TEXT_INFO:
1141 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1142 if (len > 0) {
1143 buffer_append(&loginmsg,
1144 PAM_MSG_MEMBER(msg, i, msg), len);
1145 buffer_append(&loginmsg, "\n", 1);
1146 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001147 if ((reply[i].resp = strdup("")) == NULL)
1148 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001149 reply[i].resp_retcode = PAM_SUCCESS;
1150 break;
1151 default:
1152 goto fail;
1153 }
1154 }
1155 *resp = reply;
1156 return (PAM_SUCCESS);
1157
Damien Miller94cf4c82005-07-17 17:04:47 +10001158 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001159 for(i = 0; i < n; i++) {
1160 if (reply[i].resp != NULL)
1161 xfree(reply[i].resp);
1162 }
1163 xfree(reply);
1164 return (PAM_CONV_ERR);
1165}
1166
1167static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1168
1169/*
1170 * Attempt password authentication via PAM
1171 */
1172int
1173sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1174{
1175 int flags = (options.permit_empty_passwd == 0 ?
1176 PAM_DISALLOW_NULL_AUTHTOK : 0);
1177
1178 if (!options.use_pam || sshpam_handle == NULL)
1179 fatal("PAM: %s called when PAM disabled or failed to "
1180 "initialise.", __func__);
1181
1182 sshpam_password = password;
1183 sshpam_authctxt = authctxt;
1184
Darren Tuckere061b152004-05-30 22:04:56 +10001185 /*
1186 * If the user logging in is invalid, or is root but is not permitted
1187 * by PermitRootLogin, use an invalid password to prevent leaking
1188 * information via timing (eg if the PAM config has a delay on fail).
1189 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001190 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001191 options.permit_root_login != PERMIT_YES))
Darren Tuckere061b152004-05-30 22:04:56 +10001192 sshpam_password = badpw;
1193
Darren Tucker450a1582004-05-30 20:43:59 +10001194 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1195 (const void *)&passwd_conv);
1196 if (sshpam_err != PAM_SUCCESS)
1197 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1198 pam_strerror(sshpam_handle, sshpam_err));
1199
1200 sshpam_err = pam_authenticate(sshpam_handle, flags);
1201 sshpam_password = NULL;
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001202 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001203 debug("PAM: password authentication accepted for %.100s",
1204 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001205 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001206 } else {
1207 debug("PAM: password authentication failed for %.100s: %s",
1208 authctxt->valid ? authctxt->user : "an illegal user",
1209 pam_strerror(sshpam_handle, sshpam_err));
1210 return 0;
1211 }
1212}
Damien Millere72b7af1999-12-30 15:08:44 +11001213#endif /* USE_PAM */