blob: a67eaa309dc4daeb88a4034bfd926b32b649f329 [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
Kevin Stevese683e762002-04-04 19:02:28 +000081#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110082#include "auth-pam.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100083#include "buffer.h"
84#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100086#include "log.h"
87#include "monitor_wrap.h"
88#include "msg.h"
89#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100090#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100091#include "servconf.h"
92#include "ssh2.h"
93#include "xmalloc.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100094#include "auth-options.h"
Damien Millere72b7af1999-12-30 15:08:44 +110095
Damien Miller4e448a32003-05-14 15:11:48 +100096extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +110097extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +110098extern int compat20;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +100099extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +1000100
Darren Tucker328118a2005-05-25 16:18:09 +1000101/* so we don't silently change behaviour */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000102#ifdef USE_POSIX_THREADS
Darren Tucker328118a2005-05-25 16:18:09 +1000103# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
104#endif
105
106/*
107 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
108 * and generally a bad idea. Use at own risk and do not expect support if
109 * this breaks.
110 */
111#ifdef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000112#include <pthread.h>
113/*
Damien Millera8e06ce2003-11-21 23:48:55 +1100114 * Avoid namespace clash when *not* using pthreads for systems *with*
115 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +1000116 * (e.g. Linux)
117 */
Damien Millera8e06ce2003-11-21 23:48:55 +1100118typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000119#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100120typedef pid_t sp_pthread_t;
121#endif
122
123struct pam_ctxt {
124 sp_pthread_t pam_thread;
125 int pam_psock;
126 int pam_csock;
127 int pam_done;
128};
129
130static void sshpam_free_ctx(void *);
131static struct pam_ctxt *cleanup_ctxt;
132
Darren Tucker328118a2005-05-25 16:18:09 +1000133#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000134/*
135 * Simulate threads with processes.
136 */
Kevin Steves63007d42002-07-21 17:57:01 +0000137
Darren Tucker749bc952004-01-14 22:14:04 +1100138static int sshpam_thread_status = -1;
139static mysig_t sshpam_oldsig;
140
Damien Miller94cf4c82005-07-17 17:04:47 +1000141static void
Darren Tucker749bc952004-01-14 22:14:04 +1100142sshpam_sigchld_handler(int sig)
143{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000144 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100145 if (cleanup_ctxt == NULL)
146 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000147 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000148 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000149 /* PAM thread has not exitted, privsep slave must have */
150 kill(cleanup_ctxt->pam_thread, SIGTERM);
151 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
Darren Tucker5d423f42004-07-11 16:54:08 +1000152 <= 0)
Darren Tuckerb53355e2004-05-24 11:55:36 +1000153 return; /* could not wait */
154 }
Darren Tucker749bc952004-01-14 22:14:04 +1100155 if (WIFSIGNALED(sshpam_thread_status) &&
156 WTERMSIG(sshpam_thread_status) == SIGTERM)
157 return; /* terminated by pthread_cancel */
158 if (!WIFEXITED(sshpam_thread_status))
159 fatal("PAM: authentication thread exited unexpectedly");
160 if (WEXITSTATUS(sshpam_thread_status) != 0)
161 fatal("PAM: authentication thread exited uncleanly");
162}
163
Damien Millerb8fe89c2006-07-24 14:51:00 +1000164/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000165static void
Damien Millerb8fe89c2006-07-24 14:51:00 +1000166pthread_exit(void *value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000167{
168 _exit(0);
169}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000170
Damien Millerb8fe89c2006-07-24 14:51:00 +1000171/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000172static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000173pthread_create(sp_pthread_t *thread, const void *attr,
Damien Miller4f9f42a2003-05-10 19:28:02 +1000174 void *(*thread_start)(void *), void *arg)
175{
176 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000177 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100178
Darren Tuckerb9b60212004-03-04 20:03:54 +1100179 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000180 switch ((pid = fork())) {
181 case -1:
182 error("fork(): %s", strerror(errno));
183 return (-1);
184 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000185 close(ctx->pam_psock);
186 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000187 thread_start(arg);
188 _exit(1);
189 default:
190 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000191 close(ctx->pam_csock);
192 ctx->pam_csock = -1;
Darren Tucker749bc952004-01-14 22:14:04 +1100193 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000194 return (0);
195 }
196}
Damien Millere72b7af1999-12-30 15:08:44 +1100197
Damien Miller4f9f42a2003-05-10 19:28:02 +1000198static int
199pthread_cancel(sp_pthread_t thread)
200{
Darren Tucker7ae09622004-01-14 23:07:56 +1100201 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000202 return (kill(thread, SIGTERM));
203}
204
Damien Millerb8fe89c2006-07-24 14:51:00 +1000205/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000206static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000207pthread_join(sp_pthread_t thread, void **value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000208{
209 int status;
210
Darren Tucker749bc952004-01-14 22:14:04 +1100211 if (sshpam_thread_status != -1)
212 return (sshpam_thread_status);
213 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000214 waitpid(thread, &status, 0);
215 return (status);
216}
217#endif
218
219
Damien Miller5c3a5582003-09-23 22:12:38 +1000220static pam_handle_t *sshpam_handle = NULL;
221static int sshpam_err = 0;
222static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000223static int sshpam_session_open = 0;
224static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100225static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100226static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000227static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000228static const char *sshpam_password = NULL;
Darren Tucker36a3d602005-01-20 12:43:38 +1100229static char badpw[] = "\b\n\r\177INCORRECT";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000230
Damien Millerc756e9b2003-11-17 21:41:42 +1100231/* Some PAM implementations don't implement this */
232#ifndef HAVE_PAM_GETENVLIST
233static char **
234pam_getenvlist(pam_handle_t *pamh)
235{
236 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100237 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100238 * for platforms without pam_getenvlist by searching for known
239 * env vars (e.g. KRB5CCNAME) from the PAM environment.
240 */
241 return NULL;
242}
243#endif
244
Darren Tucker21dd0892004-08-16 23:12:05 +1000245/*
246 * Some platforms, notably Solaris, do not enforce password complexity
247 * rules during pam_chauthtok() if the real uid of the calling process
248 * is 0, on the assumption that it's being called by "passwd" run by root.
249 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
250 * the right thing.
251 */
252#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
253static int
254sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
255{
256 int result;
257
258 if (sshpam_authctxt == NULL)
259 fatal("PAM: sshpam_authctxt not initialized");
260 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
261 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
262 result = pam_chauthtok(pamh, flags);
263 if (setreuid(0, -1) == -1)
264 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
265 return result;
266}
267# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
268#endif
269
Darren Tucker07705c72003-12-18 15:34:31 +1100270void
Darren Tucker17db1c42004-06-19 12:54:38 +1000271sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100272{
Darren Tuckera8df9242004-01-15 00:15:07 +1100273 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000274 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100275 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000276 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100277 if (reqd) {
278 no_port_forwarding_flag |= 2;
279 no_agent_forwarding_flag |= 2;
280 no_x11_forwarding_flag |= 2;
281 } else {
282 no_port_forwarding_flag &= ~2;
283 no_agent_forwarding_flag &= ~2;
284 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100285 }
286}
Darren Tucker1921ed92004-02-10 13:23:28 +1100287
Damien Millerc756e9b2003-11-17 21:41:42 +1100288/* Import regular and PAM environment from subprocess */
289static void
290import_environments(Buffer *b)
291{
292 char *env;
293 u_int i, num_env;
294 int err;
295
Darren Tuckera8df9242004-01-15 00:15:07 +1100296 debug3("PAM: %s entering", __func__);
297
Darren Tucker328118a2005-05-25 16:18:09 +1000298#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100299 /* Import variables set by do_pam_account */
300 sshpam_account_status = buffer_get_int(b);
Darren Tucker17db1c42004-06-19 12:54:38 +1000301 sshpam_password_change_required(buffer_get_int(b));
Darren Tucker07705c72003-12-18 15:34:31 +1100302
Damien Millerc756e9b2003-11-17 21:41:42 +1100303 /* Import environment from subprocess */
304 num_env = buffer_get_int(b);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000305 if (num_env > 1024)
306 fatal("%s: received %u environment variables, expected <= 1024",
307 __func__, num_env);
308 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
Damien Millerc756e9b2003-11-17 21:41:42 +1100309 debug3("PAM: num env strings %d", num_env);
310 for(i = 0; i < num_env; i++)
311 sshpam_env[i] = buffer_get_string(b, NULL);
312
313 sshpam_env[num_env] = NULL;
314
315 /* Import PAM environment from subprocess */
316 num_env = buffer_get_int(b);
317 debug("PAM: num PAM env strings %d", num_env);
318 for(i = 0; i < num_env; i++) {
319 env = buffer_get_string(b, NULL);
320
Darren Tucker8a1624c2003-11-18 12:45:35 +1100321#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100322 /* Errors are not fatal here */
323 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
324 error("PAM: pam_putenv: %s",
325 pam_strerror(sshpam_handle, sshpam_err));
326 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100327#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100328 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100329#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100330}
331
Damien Miller9d5705a2000-09-16 16:09:27 +1100332/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000333 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100334 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000335static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000336sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000337 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100338{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000339 Buffer buffer;
340 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000341 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000342 int i;
343
Darren Tuckerba53b832004-02-17 20:46:59 +1100344 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000345 *resp = NULL;
346
Darren Tucker59e06022004-06-30 20:34:31 +1000347 if (data == NULL) {
348 error("PAM: conversation function passed a null context");
349 return (PAM_CONV_ERR);
350 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000351 ctxt = data;
352 if (n <= 0 || n > PAM_MAX_NUM_MSG)
353 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000354
Darren Tuckerd8093e42006-05-04 16:24:34 +1000355 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000356 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000357
Damien Miller4f9f42a2003-05-10 19:28:02 +1000358 buffer_init(&buffer);
359 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000360 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
361 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100362 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000363 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100364 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100365 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
366 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100367 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100368 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000369 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
370 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000371 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000372 break;
373 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100374 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000375 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100376 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100377 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
378 goto fail;
379 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
380 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000381 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
382 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000383 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000384 break;
385 case PAM_ERROR_MSG:
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 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100393 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000394 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100395 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100396 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
397 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000398 break;
399 default:
400 goto fail;
401 }
402 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000403 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000404 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000405 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000406 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000407
Damien Miller4f9f42a2003-05-10 19:28:02 +1000408 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000409 for(i = 0; i < n; i++) {
410 if (reply[i].resp != NULL)
411 xfree(reply[i].resp);
412 }
413 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000414 buffer_free(&buffer);
415 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000416}
417
Damien Miller4f9f42a2003-05-10 19:28:02 +1000418/*
419 * Authentication thread.
420 */
421static void *
422sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100423{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000424 struct pam_ctxt *ctxt = ctxtp;
425 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000426 struct pam_conv sshpam_conv;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000427 int flags = (options.permit_empty_passwd == 0 ?
428 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000429#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100430 extern char **environ;
431 char **env_from_pam;
432 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000433 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000434 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000435
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000436 pam_get_item(sshpam_handle, PAM_USER,
437 (sshpam_const void **)ptr_pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100438 environ[0] = NULL;
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000439
440 if (sshpam_authctxt != NULL) {
441 setproctitle("%s [pam]",
442 sshpam_authctxt->valid ? pam_user : "unknown");
443 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000444#endif
445
Damien Millerf4b6f102003-09-02 23:12:06 +1000446 sshpam_conv.conv = sshpam_thread_conv;
447 sshpam_conv.appdata_ptr = ctxt;
448
Darren Tucker17addf02004-03-30 20:57:57 +1000449 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100450 fatal("%s: PAM authctxt not initialized", __func__);
451
Damien Miller4f9f42a2003-05-10 19:28:02 +1000452 buffer_init(&buffer);
453 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
454 (const void *)&sshpam_conv);
455 if (sshpam_err != PAM_SUCCESS)
456 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000457 sshpam_err = pam_authenticate(sshpam_handle, flags);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000458 if (sshpam_err != PAM_SUCCESS)
459 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100460
Darren Tuckerc376c862003-12-18 16:08:59 +1100461 if (compat20) {
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000462 if (!do_pam_account()) {
463 sshpam_err = PAM_ACCT_EXPIRED;
Darren Tucker07705c72003-12-18 15:34:31 +1100464 goto auth_fail;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000465 }
Darren Tucker17addf02004-03-30 20:57:57 +1000466 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100467 sshpam_err = pam_chauthtok(sshpam_handle,
468 PAM_CHANGE_EXPIRED_AUTHTOK);
469 if (sshpam_err != PAM_SUCCESS)
470 goto auth_fail;
Darren Tucker17db1c42004-06-19 12:54:38 +1000471 sshpam_password_change_required(0);
Darren Tucker07705c72003-12-18 15:34:31 +1100472 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100473 }
Darren Tucker07705c72003-12-18 15:34:31 +1100474
Damien Miller4f9f42a2003-05-10 19:28:02 +1000475 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100476
Darren Tucker328118a2005-05-25 16:18:09 +1000477#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100478 /* Export variables set by do_pam_account */
479 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000480 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100481
Damien Millerc756e9b2003-11-17 21:41:42 +1100482 /* Export any environment strings set in child */
483 for(i = 0; environ[i] != NULL; i++)
484 ; /* Count */
485 buffer_put_int(&buffer, i);
486 for(i = 0; environ[i] != NULL; i++)
487 buffer_put_cstring(&buffer, environ[i]);
488
489 /* Export any environment strings set by PAM in child */
490 env_from_pam = pam_getenvlist(sshpam_handle);
491 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
492 ; /* Count */
493 buffer_put_int(&buffer, i);
494 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
495 buffer_put_cstring(&buffer, env_from_pam[i]);
Darren Tucker328118a2005-05-25 16:18:09 +1000496#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100497
Damien Miller9bdba702003-11-17 21:27:55 +1100498 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000499 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
500 buffer_free(&buffer);
501 pthread_exit(NULL);
502
503 auth_fail:
504 buffer_put_cstring(&buffer,
505 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100506 /* XXX - can't do much about an error here */
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000507 if (sshpam_err == PAM_ACCT_EXPIRED)
508 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
509 else
510 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000511 buffer_free(&buffer);
512 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100513
Damien Miller4f9f42a2003-05-10 19:28:02 +1000514 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000515}
516
Darren Tucker8846a072003-10-07 11:30:15 +1000517void
518sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000519{
Darren Tucker8846a072003-10-07 11:30:15 +1000520 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000521
Darren Tuckera8df9242004-01-15 00:15:07 +1100522 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000523 if (ctxt != NULL && ctxt->pam_thread != 0) {
524 pthread_cancel(ctxt->pam_thread);
525 pthread_join(ctxt->pam_thread, NULL);
526 close(ctxt->pam_psock);
527 close(ctxt->pam_csock);
528 memset(ctxt, 0, sizeof(*ctxt));
529 cleanup_ctxt = NULL;
530 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000531}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000532
Damien Miller4f9f42a2003-05-10 19:28:02 +1000533static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000534sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000535 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000536{
Darren Tuckerba53b832004-02-17 20:46:59 +1100537 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000538 return (PAM_CONV_ERR);
539}
Damien Miller63dc3e92001-02-07 12:58:33 +1100540
Damien Miller4f9f42a2003-05-10 19:28:02 +1000541static struct pam_conv null_conv = { sshpam_null_conv, NULL };
542
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000543static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000544sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000545 struct pam_response **resp, void *data)
546{
547 struct pam_response *reply;
548 int i;
549 size_t len;
550
551 debug3("PAM: %s called with %d messages", __func__, n);
552 *resp = NULL;
553
554 if (n <= 0 || n > PAM_MAX_NUM_MSG)
555 return (PAM_CONV_ERR);
556
Darren Tuckerd8093e42006-05-04 16:24:34 +1000557 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000558 return (PAM_CONV_ERR);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000559
560 for (i = 0; i < n; ++i) {
561 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
562 case PAM_ERROR_MSG:
563 case PAM_TEXT_INFO:
564 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
565 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
566 buffer_append(&loginmsg, "\n", 1 );
567 reply[i].resp_retcode = PAM_SUCCESS;
568 break;
569 default:
570 goto fail;
571 }
572 }
573 *resp = reply;
574 return (PAM_SUCCESS);
575
576 fail:
577 for(i = 0; i < n; i++) {
578 if (reply[i].resp != NULL)
579 xfree(reply[i].resp);
580 }
581 xfree(reply);
582 return (PAM_CONV_ERR);
583}
584
585static struct pam_conv store_conv = { sshpam_store_conv, NULL };
586
Darren Tucker8846a072003-10-07 11:30:15 +1000587void
588sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000589{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000590 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000591 if (sshpam_handle == NULL)
592 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000593 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
594 if (sshpam_cred_established) {
595 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
596 sshpam_cred_established = 0;
597 }
598 if (sshpam_session_open) {
599 pam_close_session(sshpam_handle, PAM_SILENT);
600 sshpam_session_open = 0;
601 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100602 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000603 pam_end(sshpam_handle, sshpam_err);
604 sshpam_handle = NULL;
605}
606
607static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100608sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000609{
Darren Tucker455813b2003-09-13 22:12:11 +1000610 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100611 const char *pam_rhost, *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000612 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000613
614 if (sshpam_handle != NULL) {
615 /* We already have a PAM context; check if the user matches */
616 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000617 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000618 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
619 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000620 pam_end(sshpam_handle, sshpam_err);
621 sshpam_handle = NULL;
622 }
623 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000624 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000625 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000626 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100627
Damien Miller4f9f42a2003-05-10 19:28:02 +1000628 if (sshpam_err != PAM_SUCCESS) {
629 pam_end(sshpam_handle, sshpam_err);
630 sshpam_handle = NULL;
631 return (-1);
632 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000633 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000634 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000635 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
636 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000637 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000638 sshpam_handle = NULL;
639 return (-1);
640 }
641#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100642 /*
643 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
644 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000645 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100646 */
Damien Miller25d93422003-05-18 20:45:47 +1000647 debug("PAM: setting PAM_TTY to \"ssh\"");
648 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
649 if (sshpam_err != PAM_SUCCESS) {
650 pam_end(sshpam_handle, sshpam_err);
651 sshpam_handle = NULL;
652 return (-1);
653 }
654#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000655 return (0);
656}
657
658static void *
659sshpam_init_ctx(Authctxt *authctxt)
660{
661 struct pam_ctxt *ctxt;
662 int socks[2];
663
Darren Tuckera8df9242004-01-15 00:15:07 +1100664 debug3("PAM: %s entering", __func__);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000665 /*
666 * Refuse to start if we don't have PAM enabled or do_pam_account
667 * has previously failed.
668 */
669 if (!options.use_pam || sshpam_account_status == 0)
Damien Miller4e448a32003-05-14 15:11:48 +1000670 return NULL;
671
Damien Miller4f9f42a2003-05-10 19:28:02 +1000672 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100673 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000674 error("PAM: initialization failed");
675 return (NULL);
676 }
677
678 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000679 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000680
681 /* Start the authentication thread */
682 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
683 error("PAM: failed create sockets: %s", strerror(errno));
684 xfree(ctxt);
685 return (NULL);
686 }
687 ctxt->pam_psock = socks[0];
688 ctxt->pam_csock = socks[1];
689 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
690 error("PAM: failed to start authentication thread: %s",
691 strerror(errno));
692 close(socks[0]);
693 close(socks[1]);
694 xfree(ctxt);
695 return (NULL);
696 }
Darren Tucker8846a072003-10-07 11:30:15 +1000697 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000698 return (ctxt);
699}
700
701static int
702sshpam_query(void *ctx, char **name, char **info,
703 u_int *num, char ***prompts, u_int **echo_on)
704{
705 Buffer buffer;
706 struct pam_ctxt *ctxt = ctx;
707 size_t plen;
708 u_char type;
709 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000710 size_t len, mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000711
Darren Tuckera8df9242004-01-15 00:15:07 +1100712 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000713 buffer_init(&buffer);
714 *name = xstrdup("");
715 *info = xstrdup("");
716 *prompts = xmalloc(sizeof(char *));
717 **prompts = NULL;
718 plen = 0;
719 *echo_on = xmalloc(sizeof(u_int));
720 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
721 type = buffer_get_char(&buffer);
722 msg = buffer_get_string(&buffer, NULL);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000723 mlen = strlen(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000724 switch (type) {
725 case PAM_PROMPT_ECHO_ON:
726 case PAM_PROMPT_ECHO_OFF:
727 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000728 len = plen + mlen + 1;
Damien Miller36812092006-03-26 14:22:47 +1100729 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000730 strlcpy(**prompts + plen, msg, len - plen);
731 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000732 **echo_on = (type == PAM_PROMPT_ECHO_ON);
733 xfree(msg);
734 return (0);
735 case PAM_ERROR_MSG:
736 case PAM_TEXT_INFO:
737 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000738 len = plen + mlen + 2;
Damien Miller36812092006-03-26 14:22:47 +1100739 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000740 strlcpy(**prompts + plen, msg, len - plen);
741 plen += mlen;
742 strlcat(**prompts + plen, "\n", len - plen);
743 plen++;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000744 xfree(msg);
745 break;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000746 case PAM_ACCT_EXPIRED:
747 sshpam_account_status = 0;
748 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000749 case PAM_AUTH_ERR:
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000750 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
Darren Tucker7b1e6952005-09-28 22:33:27 +1000751 if (**prompts != NULL && strlen(**prompts) != 0) {
752 *info = **prompts;
753 **prompts = NULL;
754 *num = 0;
755 **echo_on = 0;
756 ctxt->pam_done = -1;
Damien Miller66f9eb62006-03-18 23:04:49 +1100757 xfree(msg);
Darren Tucker7b1e6952005-09-28 22:33:27 +1000758 return 0;
759 }
760 /* FALLTHROUGH */
761 case PAM_SUCCESS:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000762 if (**prompts != NULL) {
763 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100764 debug("PAM: %s", **prompts);
765 buffer_append(&loginmsg, **prompts,
766 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000767 xfree(**prompts);
768 **prompts = NULL;
769 }
770 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100771 if (!sshpam_authctxt->valid ||
772 (sshpam_authctxt->pw->pw_uid == 0 &&
773 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100774 fatal("Internal error: PAM auth "
775 "succeeded when it should have "
776 "failed");
Damien Millerc756e9b2003-11-17 21:41:42 +1100777 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000778 *num = 0;
779 **echo_on = 0;
780 ctxt->pam_done = 1;
781 xfree(msg);
782 return (0);
783 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000784 error("PAM: %s for %s%.100s from %.100s", msg,
785 sshpam_authctxt->valid ? "" : "illegal user ",
786 sshpam_authctxt->user,
787 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000788 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000789 default:
790 *num = 0;
791 **echo_on = 0;
792 xfree(msg);
793 ctxt->pam_done = -1;
794 return (-1);
795 }
796 }
797 return (-1);
798}
799
800/* XXX - see also comment in auth-chall.c:verify_response */
801static int
802sshpam_respond(void *ctx, u_int num, char **resp)
803{
804 Buffer buffer;
805 struct pam_ctxt *ctxt = ctx;
806
Darren Tucker1d4ebbf2006-01-29 16:46:13 +1100807 debug2("PAM: %s entering, %u responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000808 switch (ctxt->pam_done) {
809 case 1:
810 sshpam_authenticated = 1;
811 return (0);
812 case 0:
813 break;
814 default:
815 return (-1);
816 }
817 if (num != 1) {
818 error("PAM: expected one response, got %u", num);
819 return (-1);
820 }
821 buffer_init(&buffer);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100822 if (sshpam_authctxt->valid &&
823 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller37294fb2005-07-17 17:18:49 +1000824 options.permit_root_login == PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100825 buffer_put_cstring(&buffer, *resp);
826 else
827 buffer_put_cstring(&buffer, badpw);
Damien Miller9bdba702003-11-17 21:27:55 +1100828 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
829 buffer_free(&buffer);
830 return (-1);
831 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000832 buffer_free(&buffer);
833 return (1);
834}
835
836static void
837sshpam_free_ctx(void *ctxtp)
838{
839 struct pam_ctxt *ctxt = ctxtp;
840
Darren Tuckera8df9242004-01-15 00:15:07 +1100841 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000842 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000843 xfree(ctxt);
844 /*
845 * We don't call sshpam_cleanup() here because we may need the PAM
846 * handle at a later stage, e.g. when setting up a session. It's
847 * still on the cleanup list, so pam_end() *will* be called before
848 * the server process terminates.
849 */
850}
851
852KbdintDevice sshpam_device = {
853 "pam",
854 sshpam_init_ctx,
855 sshpam_query,
856 sshpam_respond,
857 sshpam_free_ctx
858};
859
860KbdintDevice mm_sshpam_device = {
861 "pam",
862 mm_sshpam_init_ctx,
863 mm_sshpam_query,
864 mm_sshpam_respond,
865 mm_sshpam_free_ctx
866};
867
868/*
869 * This replaces auth-pam.c
870 */
871void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100872start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000873{
Damien Miller9d507da2003-05-14 15:31:12 +1000874 if (!options.use_pam)
875 fatal("PAM: initialisation requested when UsePAM=no");
876
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100877 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000878 fatal("PAM: initialisation failed");
879}
880
881void
882finish_pam(void)
883{
Darren Tucker8846a072003-10-07 11:30:15 +1000884 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000885}
886
Damien Miller1f499fd2003-08-25 13:08:49 +1000887u_int
888do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000889{
Darren Tucker77fc29e2004-09-11 23:07:03 +1000890 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +1100891 if (sshpam_account_status != -1)
892 return (sshpam_account_status);
893
Damien Miller1f499fd2003-08-25 13:08:49 +1000894 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000895 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
896 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +1000897
Darren Tucker07705c72003-12-18 15:34:31 +1100898 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
899 sshpam_account_status = 0;
900 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000901 }
902
Darren Tucker07705c72003-12-18 15:34:31 +1100903 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +1000904 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +1100905
906 sshpam_account_status = 1;
907 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000908}
909
910void
Damien Miller341c6e62003-09-02 23:18:52 +1000911do_pam_set_tty(const char *tty)
912{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000913 if (tty != NULL) {
914 debug("PAM: setting PAM_TTY to \"%s\"", tty);
915 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
916 if (sshpam_err != PAM_SUCCESS)
917 fatal("PAM: failed to set PAM_TTY: %s",
918 pam_strerror(sshpam_handle, sshpam_err));
919 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000920}
921
922void
923do_pam_setcred(int init)
924{
925 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +1000926 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000927 if (sshpam_err != PAM_SUCCESS)
928 fatal("PAM: failed to set PAM_CONV: %s",
929 pam_strerror(sshpam_handle, sshpam_err));
930 if (init) {
931 debug("PAM: establishing credentials");
932 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
933 } else {
934 debug("PAM: reinitializing credentials");
935 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
936 }
937 if (sshpam_err == PAM_SUCCESS) {
938 sshpam_cred_established = 1;
939 return;
940 }
941 if (sshpam_authenticated)
942 fatal("PAM: pam_setcred(): %s",
943 pam_strerror(sshpam_handle, sshpam_err));
944 else
945 debug("PAM: pam_setcred(): %s",
946 pam_strerror(sshpam_handle, sshpam_err));
947}
948
Damien Miller4f9f42a2003-05-10 19:28:02 +1000949static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000950sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000951 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000952{
953 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000954 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000955 int i;
956
Darren Tuckerba53b832004-02-17 20:46:59 +1100957 debug3("PAM: %s called with %d messages", __func__, n);
958
Damien Miller5c3a5582003-09-23 22:12:38 +1000959 *resp = NULL;
960
Darren Tucker18df00c2003-11-18 12:42:07 +1100961 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000962 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000963
Darren Tuckerd8093e42006-05-04 16:24:34 +1000964 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000965 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000966
Damien Miller4f9f42a2003-05-10 19:28:02 +1000967 for (i = 0; i < n; ++i) {
968 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
969 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000970 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100971 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000972 RP_ALLOW_STDIN);
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_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100976 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000977 fgets(input, sizeof input, stdin);
Damien Millera6fb77f2004-07-19 09:39:11 +1000978 if ((reply[i].resp = strdup(input)) == NULL)
979 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000980 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000981 break;
982 case PAM_ERROR_MSG:
983 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100984 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000985 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000986 break;
987 default:
988 goto fail;
989 }
990 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000991 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000992 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000993
Damien Miller4f9f42a2003-05-10 19:28:02 +1000994 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000995 for(i = 0; i < n; i++) {
996 if (reply[i].resp != NULL)
997 xfree(reply[i].resp);
998 }
999 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001000 return (PAM_CONV_ERR);
1001}
1002
Darren Tucker94befab2004-06-03 14:53:12 +10001003static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +11001004
Damien Miller4f9f42a2003-05-10 19:28:02 +10001005/*
1006 * XXX this should be done in the authentication phase, but ssh1 doesn't
1007 * support that
1008 */
1009void
1010do_pam_chauthtok(void)
1011{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001012 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +10001013 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +10001014 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +11001015 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001016 if (sshpam_err != PAM_SUCCESS)
1017 fatal("PAM: failed to set PAM_CONV: %s",
1018 pam_strerror(sshpam_handle, sshpam_err));
1019 debug("PAM: changing password");
1020 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1021 if (sshpam_err != PAM_SUCCESS)
1022 fatal("PAM: pam_chauthtok(): %s",
1023 pam_strerror(sshpam_handle, sshpam_err));
1024}
1025
Darren Tucker18df00c2003-11-18 12:42:07 +11001026void
1027do_pam_session(void)
1028{
Darren Tucker1921ed92004-02-10 13:23:28 +11001029 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +11001030 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +11001031 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +11001032 if (sshpam_err != PAM_SUCCESS)
1033 fatal("PAM: failed to set PAM_CONV: %s",
1034 pam_strerror(sshpam_handle, sshpam_err));
1035 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +10001036 if (sshpam_err == PAM_SUCCESS)
1037 sshpam_session_open = 1;
1038 else {
1039 sshpam_session_open = 0;
1040 disable_forwarding();
1041 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001042 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001043 }
1044
1045}
1046
1047int
1048is_pam_session_open(void)
1049{
1050 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001051}
1052
Damien Millera8e06ce2003-11-21 23:48:55 +11001053/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001054 * Set a PAM environment string. We need to do this so that the session
1055 * modules can handle things like Kerberos/GSI credentials that appear
1056 * during the ssh authentication process.
1057 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001058int
Damien Millera8e06ce2003-11-21 23:48:55 +11001059do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001060{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001061 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +11001062#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +10001063 char *compound;
1064 size_t len;
1065
1066 len = strlen(name) + strlen(value) + 2;
1067 compound = xmalloc(len);
1068
1069 snprintf(compound, len, "%s=%s", name, value);
1070 ret = pam_putenv(sshpam_handle, compound);
1071 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001072#endif
Damien Millerf2728092003-09-17 07:24:25 +10001073
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001074 return (ret);
1075}
1076
Damien Miller4f9f42a2003-05-10 19:28:02 +10001077char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001078fetch_pam_child_environment(void)
1079{
1080 return sshpam_env;
1081}
1082
1083char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001084fetch_pam_environment(void)
1085{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001087}
1088
1089void
1090free_pam_environment(char **env)
1091{
1092 char **envp;
1093
Damien Millere27c6cc2003-05-16 18:21:01 +10001094 if (env == NULL)
1095 return;
1096
Damien Miller4f9f42a2003-05-10 19:28:02 +10001097 for (envp = env; *envp; envp++)
1098 xfree(*envp);
1099 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001100}
1101
Darren Tucker450a1582004-05-30 20:43:59 +10001102/*
1103 * "Blind" conversation function for password authentication. Assumes that
1104 * echo-off prompts are for the password and stores messages for later
1105 * display.
1106 */
1107static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001108sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001109 struct pam_response **resp, void *data)
1110{
1111 struct pam_response *reply;
1112 int i;
1113 size_t len;
1114
1115 debug3("PAM: %s called with %d messages", __func__, n);
1116
1117 *resp = NULL;
1118
1119 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1120 return (PAM_CONV_ERR);
1121
1122 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1123 return (PAM_CONV_ERR);
1124 memset(reply, 0, n * sizeof(*reply));
1125
1126 for (i = 0; i < n; ++i) {
1127 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1128 case PAM_PROMPT_ECHO_OFF:
1129 if (sshpam_password == NULL)
1130 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001131 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1132 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001133 reply[i].resp_retcode = PAM_SUCCESS;
1134 break;
1135 case PAM_ERROR_MSG:
1136 case PAM_TEXT_INFO:
1137 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1138 if (len > 0) {
1139 buffer_append(&loginmsg,
1140 PAM_MSG_MEMBER(msg, i, msg), len);
1141 buffer_append(&loginmsg, "\n", 1);
1142 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001143 if ((reply[i].resp = strdup("")) == NULL)
1144 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001145 reply[i].resp_retcode = PAM_SUCCESS;
1146 break;
1147 default:
1148 goto fail;
1149 }
1150 }
1151 *resp = reply;
1152 return (PAM_SUCCESS);
1153
Damien Miller94cf4c82005-07-17 17:04:47 +10001154 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001155 for(i = 0; i < n; i++) {
1156 if (reply[i].resp != NULL)
1157 xfree(reply[i].resp);
1158 }
1159 xfree(reply);
1160 return (PAM_CONV_ERR);
1161}
1162
1163static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1164
1165/*
1166 * Attempt password authentication via PAM
1167 */
1168int
1169sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1170{
1171 int flags = (options.permit_empty_passwd == 0 ?
1172 PAM_DISALLOW_NULL_AUTHTOK : 0);
1173
1174 if (!options.use_pam || sshpam_handle == NULL)
1175 fatal("PAM: %s called when PAM disabled or failed to "
1176 "initialise.", __func__);
1177
1178 sshpam_password = password;
1179 sshpam_authctxt = authctxt;
1180
Darren Tuckere061b152004-05-30 22:04:56 +10001181 /*
1182 * If the user logging in is invalid, or is root but is not permitted
1183 * by PermitRootLogin, use an invalid password to prevent leaking
1184 * information via timing (eg if the PAM config has a delay on fail).
1185 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001186 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001187 options.permit_root_login != PERMIT_YES))
Darren Tuckere061b152004-05-30 22:04:56 +10001188 sshpam_password = badpw;
1189
Darren Tucker450a1582004-05-30 20:43:59 +10001190 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1191 (const void *)&passwd_conv);
1192 if (sshpam_err != PAM_SUCCESS)
1193 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1194 pam_strerror(sshpam_handle, sshpam_err));
1195
1196 sshpam_err = pam_authenticate(sshpam_handle, flags);
1197 sshpam_password = NULL;
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001198 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001199 debug("PAM: password authentication accepted for %.100s",
1200 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001201 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001202 } else {
1203 debug("PAM: password authentication failed for %.100s: %s",
1204 authctxt->valid ? authctxt->user : "an illegal user",
1205 pam_strerror(sshpam_handle, sshpam_err));
1206 return 0;
1207 }
1208}
Damien Millere72b7af1999-12-30 15:08:44 +11001209#endif /* USE_PAM */