blob: 0446cd5595bf121c8b541e1bbab5881e685598c4 [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 Miller37294fb2005-07-17 17:18:49 +100050RCSID("$Id: auth-pam.c,v 1.126 2005/07/17 07:18:50 djm Exp $");
Damien Millere72b7af1999-12-30 15:08:44 +110051
52#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110053#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100054#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110055#elif defined (HAVE_PAM_PAM_APPL_H)
56#include <pam/pam_appl.h>
57#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100058
Darren Tuckerf08bdb52005-05-26 19:59:48 +100059/* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
60#ifdef PAM_SUN_CODEBASE
61# define sshpam_const /* Solaris, HP-UX, AIX */
62#else
63# define sshpam_const const /* LinuxPAM, OpenPAM */
64#endif
65
Kevin Stevese683e762002-04-04 19:02:28 +000066#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110067#include "auth-pam.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100068#include "buffer.h"
69#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100071#include "log.h"
72#include "monitor_wrap.h"
73#include "msg.h"
74#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100075#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100076#include "servconf.h"
77#include "ssh2.h"
78#include "xmalloc.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100079#include "auth-options.h"
Damien Millere72b7af1999-12-30 15:08:44 +110080
Damien Miller4e448a32003-05-14 15:11:48 +100081extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +110082extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +110083extern int compat20;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +100084extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +100085
Darren Tucker328118a2005-05-25 16:18:09 +100086/* so we don't silently change behaviour */
Damien Miller4f9f42a2003-05-10 19:28:02 +100087#ifdef USE_POSIX_THREADS
Darren Tucker328118a2005-05-25 16:18:09 +100088# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
89#endif
90
91/*
92 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
93 * and generally a bad idea. Use at own risk and do not expect support if
94 * this breaks.
95 */
96#ifdef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +100097#include <pthread.h>
98/*
Damien Millera8e06ce2003-11-21 23:48:55 +110099 * Avoid namespace clash when *not* using pthreads for systems *with*
100 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +1000101 * (e.g. Linux)
102 */
Damien Millera8e06ce2003-11-21 23:48:55 +1100103typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000104#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100105typedef pid_t sp_pthread_t;
106#endif
107
108struct pam_ctxt {
109 sp_pthread_t pam_thread;
110 int pam_psock;
111 int pam_csock;
112 int pam_done;
113};
114
115static void sshpam_free_ctx(void *);
116static struct pam_ctxt *cleanup_ctxt;
117
Darren Tucker328118a2005-05-25 16:18:09 +1000118#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000119/*
120 * Simulate threads with processes.
121 */
Kevin Steves63007d42002-07-21 17:57:01 +0000122
Darren Tucker749bc952004-01-14 22:14:04 +1100123static int sshpam_thread_status = -1;
124static mysig_t sshpam_oldsig;
125
Damien Miller94cf4c82005-07-17 17:04:47 +1000126static void
Darren Tucker749bc952004-01-14 22:14:04 +1100127sshpam_sigchld_handler(int sig)
128{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000129 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100130 if (cleanup_ctxt == NULL)
131 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000132 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000133 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000134 /* PAM thread has not exitted, privsep slave must have */
135 kill(cleanup_ctxt->pam_thread, SIGTERM);
136 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
Darren Tucker5d423f42004-07-11 16:54:08 +1000137 <= 0)
Darren Tuckerb53355e2004-05-24 11:55:36 +1000138 return; /* could not wait */
139 }
Darren Tucker749bc952004-01-14 22:14:04 +1100140 if (WIFSIGNALED(sshpam_thread_status) &&
141 WTERMSIG(sshpam_thread_status) == SIGTERM)
142 return; /* terminated by pthread_cancel */
143 if (!WIFEXITED(sshpam_thread_status))
144 fatal("PAM: authentication thread exited unexpectedly");
145 if (WEXITSTATUS(sshpam_thread_status) != 0)
146 fatal("PAM: authentication thread exited uncleanly");
147}
148
Damien Miller4f9f42a2003-05-10 19:28:02 +1000149static void
150pthread_exit(void *value __unused)
151{
152 _exit(0);
153}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000154
Damien Miller4f9f42a2003-05-10 19:28:02 +1000155static int
156pthread_create(sp_pthread_t *thread, const void *attr __unused,
157 void *(*thread_start)(void *), void *arg)
158{
159 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000160 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100161
Darren Tuckerb9b60212004-03-04 20:03:54 +1100162 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000163 switch ((pid = fork())) {
164 case -1:
165 error("fork(): %s", strerror(errno));
166 return (-1);
167 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000168 close(ctx->pam_psock);
169 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000170 thread_start(arg);
171 _exit(1);
172 default:
173 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000174 close(ctx->pam_csock);
175 ctx->pam_csock = -1;
Darren Tucker749bc952004-01-14 22:14:04 +1100176 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000177 return (0);
178 }
179}
Damien Millere72b7af1999-12-30 15:08:44 +1100180
Damien Miller4f9f42a2003-05-10 19:28:02 +1000181static int
182pthread_cancel(sp_pthread_t thread)
183{
Darren Tucker7ae09622004-01-14 23:07:56 +1100184 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000185 return (kill(thread, SIGTERM));
186}
187
188static int
189pthread_join(sp_pthread_t thread, void **value __unused)
190{
191 int status;
192
Darren Tucker749bc952004-01-14 22:14:04 +1100193 if (sshpam_thread_status != -1)
194 return (sshpam_thread_status);
195 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000196 waitpid(thread, &status, 0);
197 return (status);
198}
199#endif
200
201
Damien Miller5c3a5582003-09-23 22:12:38 +1000202static pam_handle_t *sshpam_handle = NULL;
203static int sshpam_err = 0;
204static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000205static int sshpam_session_open = 0;
206static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100207static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100208static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000209static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000210static const char *sshpam_password = NULL;
Darren Tucker36a3d602005-01-20 12:43:38 +1100211static char badpw[] = "\b\n\r\177INCORRECT";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000212
Damien Millerc756e9b2003-11-17 21:41:42 +1100213/* Some PAM implementations don't implement this */
214#ifndef HAVE_PAM_GETENVLIST
215static char **
216pam_getenvlist(pam_handle_t *pamh)
217{
218 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100219 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100220 * for platforms without pam_getenvlist by searching for known
221 * env vars (e.g. KRB5CCNAME) from the PAM environment.
222 */
223 return NULL;
224}
225#endif
226
Darren Tucker21dd0892004-08-16 23:12:05 +1000227/*
228 * Some platforms, notably Solaris, do not enforce password complexity
229 * rules during pam_chauthtok() if the real uid of the calling process
230 * is 0, on the assumption that it's being called by "passwd" run by root.
231 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
232 * the right thing.
233 */
234#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
235static int
236sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
237{
238 int result;
239
240 if (sshpam_authctxt == NULL)
241 fatal("PAM: sshpam_authctxt not initialized");
242 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
243 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
244 result = pam_chauthtok(pamh, flags);
245 if (setreuid(0, -1) == -1)
246 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
247 return result;
248}
249# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
250#endif
251
Darren Tucker07705c72003-12-18 15:34:31 +1100252void
Darren Tucker17db1c42004-06-19 12:54:38 +1000253sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100254{
Darren Tuckera8df9242004-01-15 00:15:07 +1100255 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000256 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100257 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000258 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100259 if (reqd) {
260 no_port_forwarding_flag |= 2;
261 no_agent_forwarding_flag |= 2;
262 no_x11_forwarding_flag |= 2;
263 } else {
264 no_port_forwarding_flag &= ~2;
265 no_agent_forwarding_flag &= ~2;
266 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100267 }
268}
Darren Tucker1921ed92004-02-10 13:23:28 +1100269
Damien Millerc756e9b2003-11-17 21:41:42 +1100270/* Import regular and PAM environment from subprocess */
271static void
272import_environments(Buffer *b)
273{
274 char *env;
275 u_int i, num_env;
276 int err;
277
Darren Tuckera8df9242004-01-15 00:15:07 +1100278 debug3("PAM: %s entering", __func__);
279
Darren Tucker328118a2005-05-25 16:18:09 +1000280#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100281 /* Import variables set by do_pam_account */
282 sshpam_account_status = buffer_get_int(b);
Darren Tucker17db1c42004-06-19 12:54:38 +1000283 sshpam_password_change_required(buffer_get_int(b));
Darren Tucker07705c72003-12-18 15:34:31 +1100284
Damien Millerc756e9b2003-11-17 21:41:42 +1100285 /* Import environment from subprocess */
286 num_env = buffer_get_int(b);
287 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
288 debug3("PAM: num env strings %d", num_env);
289 for(i = 0; i < num_env; i++)
290 sshpam_env[i] = buffer_get_string(b, NULL);
291
292 sshpam_env[num_env] = NULL;
293
294 /* Import PAM environment from subprocess */
295 num_env = buffer_get_int(b);
296 debug("PAM: num PAM env strings %d", num_env);
297 for(i = 0; i < num_env; i++) {
298 env = buffer_get_string(b, NULL);
299
Darren Tucker8a1624c2003-11-18 12:45:35 +1100300#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100301 /* Errors are not fatal here */
302 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
303 error("PAM: pam_putenv: %s",
304 pam_strerror(sshpam_handle, sshpam_err));
305 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100306#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100307 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100308#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100309}
310
Damien Miller9d5705a2000-09-16 16:09:27 +1100311/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000312 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100313 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000314static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000315sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000316 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100317{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000318 Buffer buffer;
319 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000320 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000321 int i;
322
Darren Tuckerba53b832004-02-17 20:46:59 +1100323 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000324 *resp = NULL;
325
Darren Tucker59e06022004-06-30 20:34:31 +1000326 if (data == NULL) {
327 error("PAM: conversation function passed a null context");
328 return (PAM_CONV_ERR);
329 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000330 ctxt = data;
331 if (n <= 0 || n > PAM_MAX_NUM_MSG)
332 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000333
334 if ((reply = malloc(n * sizeof(*reply))) == NULL)
335 return (PAM_CONV_ERR);
336 memset(reply, 0, n * sizeof(*reply));
337
Damien Miller4f9f42a2003-05-10 19:28:02 +1000338 buffer_init(&buffer);
339 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000340 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
341 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100342 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000343 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100344 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100345 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
346 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100347 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100348 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000349 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
350 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000351 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000352 break;
353 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100354 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000355 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100356 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100357 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
358 goto fail;
359 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
360 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000361 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
362 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000363 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000364 break;
365 case PAM_ERROR_MSG:
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 Miller4f9f42a2003-05-10 19:28:02 +1000371 break;
372 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100373 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000374 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100375 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100376 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
377 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000378 break;
379 default:
380 goto fail;
381 }
382 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000383 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000384 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000385 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000386 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000387
Damien Miller4f9f42a2003-05-10 19:28:02 +1000388 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000389 for(i = 0; i < n; i++) {
390 if (reply[i].resp != NULL)
391 xfree(reply[i].resp);
392 }
393 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000394 buffer_free(&buffer);
395 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000396}
397
Damien Miller4f9f42a2003-05-10 19:28:02 +1000398/*
399 * Authentication thread.
400 */
401static void *
402sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100403{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000404 struct pam_ctxt *ctxt = ctxtp;
405 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000406 struct pam_conv sshpam_conv;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000407 int flags = (options.permit_empty_passwd == 0 ?
408 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000409#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100410 extern char **environ;
411 char **env_from_pam;
412 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000413 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000414 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000415
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000416 pam_get_item(sshpam_handle, PAM_USER,
417 (sshpam_const void **)ptr_pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100418 environ[0] = NULL;
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000419
420 if (sshpam_authctxt != NULL) {
421 setproctitle("%s [pam]",
422 sshpam_authctxt->valid ? pam_user : "unknown");
423 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000424#endif
425
Damien Millerf4b6f102003-09-02 23:12:06 +1000426 sshpam_conv.conv = sshpam_thread_conv;
427 sshpam_conv.appdata_ptr = ctxt;
428
Darren Tucker17addf02004-03-30 20:57:57 +1000429 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100430 fatal("%s: PAM authctxt not initialized", __func__);
431
Damien Miller4f9f42a2003-05-10 19:28:02 +1000432 buffer_init(&buffer);
433 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
434 (const void *)&sshpam_conv);
435 if (sshpam_err != PAM_SUCCESS)
436 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000437 sshpam_err = pam_authenticate(sshpam_handle, flags);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000438 if (sshpam_err != PAM_SUCCESS)
439 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100440
Darren Tuckerc376c862003-12-18 16:08:59 +1100441 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100442 if (!do_pam_account())
443 goto auth_fail;
Darren Tucker17addf02004-03-30 20:57:57 +1000444 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100445 sshpam_err = pam_chauthtok(sshpam_handle,
446 PAM_CHANGE_EXPIRED_AUTHTOK);
447 if (sshpam_err != PAM_SUCCESS)
448 goto auth_fail;
Darren Tucker17db1c42004-06-19 12:54:38 +1000449 sshpam_password_change_required(0);
Darren Tucker07705c72003-12-18 15:34:31 +1100450 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100451 }
Darren Tucker07705c72003-12-18 15:34:31 +1100452
Damien Miller4f9f42a2003-05-10 19:28:02 +1000453 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100454
Darren Tucker328118a2005-05-25 16:18:09 +1000455#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100456 /* Export variables set by do_pam_account */
457 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000458 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100459
Damien Millerc756e9b2003-11-17 21:41:42 +1100460 /* Export any environment strings set in child */
461 for(i = 0; environ[i] != NULL; i++)
462 ; /* Count */
463 buffer_put_int(&buffer, i);
464 for(i = 0; environ[i] != NULL; i++)
465 buffer_put_cstring(&buffer, environ[i]);
466
467 /* Export any environment strings set by PAM in child */
468 env_from_pam = pam_getenvlist(sshpam_handle);
469 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
470 ; /* Count */
471 buffer_put_int(&buffer, i);
472 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
473 buffer_put_cstring(&buffer, env_from_pam[i]);
Darren Tucker328118a2005-05-25 16:18:09 +1000474#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100475
Damien Miller9bdba702003-11-17 21:27:55 +1100476 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000477 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
478 buffer_free(&buffer);
479 pthread_exit(NULL);
480
481 auth_fail:
482 buffer_put_cstring(&buffer,
483 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100484 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000485 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
486 buffer_free(&buffer);
487 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100488
Damien Miller4f9f42a2003-05-10 19:28:02 +1000489 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000490}
491
Darren Tucker8846a072003-10-07 11:30:15 +1000492void
493sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000494{
Darren Tucker8846a072003-10-07 11:30:15 +1000495 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000496
Darren Tuckera8df9242004-01-15 00:15:07 +1100497 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000498 if (ctxt != NULL && ctxt->pam_thread != 0) {
499 pthread_cancel(ctxt->pam_thread);
500 pthread_join(ctxt->pam_thread, NULL);
501 close(ctxt->pam_psock);
502 close(ctxt->pam_csock);
503 memset(ctxt, 0, sizeof(*ctxt));
504 cleanup_ctxt = NULL;
505 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000506}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000507
Damien Miller4f9f42a2003-05-10 19:28:02 +1000508static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000509sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000510 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000511{
Darren Tuckerba53b832004-02-17 20:46:59 +1100512 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000513 return (PAM_CONV_ERR);
514}
Damien Miller63dc3e92001-02-07 12:58:33 +1100515
Damien Miller4f9f42a2003-05-10 19:28:02 +1000516static struct pam_conv null_conv = { sshpam_null_conv, NULL };
517
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000518static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000519sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000520 struct pam_response **resp, void *data)
521{
522 struct pam_response *reply;
523 int i;
524 size_t len;
525
526 debug3("PAM: %s called with %d messages", __func__, n);
527 *resp = NULL;
528
529 if (n <= 0 || n > PAM_MAX_NUM_MSG)
530 return (PAM_CONV_ERR);
531
532 if ((reply = malloc(n * sizeof(*reply))) == NULL)
533 return (PAM_CONV_ERR);
534 memset(reply, 0, n * sizeof(*reply));
535
536 for (i = 0; i < n; ++i) {
537 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
538 case PAM_ERROR_MSG:
539 case PAM_TEXT_INFO:
540 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
541 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
542 buffer_append(&loginmsg, "\n", 1 );
543 reply[i].resp_retcode = PAM_SUCCESS;
544 break;
545 default:
546 goto fail;
547 }
548 }
549 *resp = reply;
550 return (PAM_SUCCESS);
551
552 fail:
553 for(i = 0; i < n; i++) {
554 if (reply[i].resp != NULL)
555 xfree(reply[i].resp);
556 }
557 xfree(reply);
558 return (PAM_CONV_ERR);
559}
560
561static struct pam_conv store_conv = { sshpam_store_conv, NULL };
562
Darren Tucker8846a072003-10-07 11:30:15 +1000563void
564sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000565{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000566 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000567 if (sshpam_handle == NULL)
568 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000569 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
570 if (sshpam_cred_established) {
571 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
572 sshpam_cred_established = 0;
573 }
574 if (sshpam_session_open) {
575 pam_close_session(sshpam_handle, PAM_SILENT);
576 sshpam_session_open = 0;
577 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100578 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000579 pam_end(sshpam_handle, sshpam_err);
580 sshpam_handle = NULL;
581}
582
583static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100584sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000585{
Darren Tucker455813b2003-09-13 22:12:11 +1000586 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100587 const char *pam_rhost, *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000588 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000589
590 if (sshpam_handle != NULL) {
591 /* We already have a PAM context; check if the user matches */
592 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000593 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000594 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
595 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000596 pam_end(sshpam_handle, sshpam_err);
597 sshpam_handle = NULL;
598 }
599 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000600 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000601 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000602 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100603
Damien Miller4f9f42a2003-05-10 19:28:02 +1000604 if (sshpam_err != PAM_SUCCESS) {
605 pam_end(sshpam_handle, sshpam_err);
606 sshpam_handle = NULL;
607 return (-1);
608 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000609 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000610 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000611 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
612 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000613 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000614 sshpam_handle = NULL;
615 return (-1);
616 }
617#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100618 /*
619 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
620 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000621 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100622 */
Damien Miller25d93422003-05-18 20:45:47 +1000623 debug("PAM: setting PAM_TTY to \"ssh\"");
624 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
625 if (sshpam_err != PAM_SUCCESS) {
626 pam_end(sshpam_handle, sshpam_err);
627 sshpam_handle = NULL;
628 return (-1);
629 }
630#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000631 return (0);
632}
633
634static void *
635sshpam_init_ctx(Authctxt *authctxt)
636{
637 struct pam_ctxt *ctxt;
638 int socks[2];
639
Darren Tuckera8df9242004-01-15 00:15:07 +1100640 debug3("PAM: %s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000641 /* Refuse to start if we don't have PAM enabled */
642 if (!options.use_pam)
643 return NULL;
644
Damien Miller4f9f42a2003-05-10 19:28:02 +1000645 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100646 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000647 error("PAM: initialization failed");
648 return (NULL);
649 }
650
651 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000652 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000653
654 /* Start the authentication thread */
655 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
656 error("PAM: failed create sockets: %s", strerror(errno));
657 xfree(ctxt);
658 return (NULL);
659 }
660 ctxt->pam_psock = socks[0];
661 ctxt->pam_csock = socks[1];
662 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
663 error("PAM: failed to start authentication thread: %s",
664 strerror(errno));
665 close(socks[0]);
666 close(socks[1]);
667 xfree(ctxt);
668 return (NULL);
669 }
Darren Tucker8846a072003-10-07 11:30:15 +1000670 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000671 return (ctxt);
672}
673
674static int
675sshpam_query(void *ctx, char **name, char **info,
676 u_int *num, char ***prompts, u_int **echo_on)
677{
678 Buffer buffer;
679 struct pam_ctxt *ctxt = ctx;
680 size_t plen;
681 u_char type;
682 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000683 size_t len, mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000684
Darren Tuckera8df9242004-01-15 00:15:07 +1100685 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000686 buffer_init(&buffer);
687 *name = xstrdup("");
688 *info = xstrdup("");
689 *prompts = xmalloc(sizeof(char *));
690 **prompts = NULL;
691 plen = 0;
692 *echo_on = xmalloc(sizeof(u_int));
693 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
694 type = buffer_get_char(&buffer);
695 msg = buffer_get_string(&buffer, NULL);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000696 mlen = strlen(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000697 switch (type) {
698 case PAM_PROMPT_ECHO_ON:
699 case PAM_PROMPT_ECHO_OFF:
700 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000701 len = plen + mlen + 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000702 **prompts = xrealloc(**prompts, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000703 strlcpy(**prompts + plen, msg, len - plen);
704 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000705 **echo_on = (type == PAM_PROMPT_ECHO_ON);
706 xfree(msg);
707 return (0);
708 case PAM_ERROR_MSG:
709 case PAM_TEXT_INFO:
710 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000711 len = plen + mlen + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000712 **prompts = xrealloc(**prompts, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000713 strlcpy(**prompts + plen, msg, len - plen);
714 plen += mlen;
715 strlcat(**prompts + plen, "\n", len - plen);
716 plen++;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000717 xfree(msg);
718 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000719 case PAM_SUCCESS:
720 case PAM_AUTH_ERR:
721 if (**prompts != NULL) {
722 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100723 debug("PAM: %s", **prompts);
724 buffer_append(&loginmsg, **prompts,
725 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000726 xfree(**prompts);
727 **prompts = NULL;
728 }
729 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100730 if (!sshpam_authctxt->valid ||
731 (sshpam_authctxt->pw->pw_uid == 0 &&
732 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100733 fatal("Internal error: PAM auth "
734 "succeeded when it should have "
735 "failed");
Damien Millerc756e9b2003-11-17 21:41:42 +1100736 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000737 *num = 0;
738 **echo_on = 0;
739 ctxt->pam_done = 1;
740 xfree(msg);
741 return (0);
742 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000743 error("PAM: %s for %s%.100s from %.100s", msg,
744 sshpam_authctxt->valid ? "" : "illegal user ",
745 sshpam_authctxt->user,
746 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000747 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000748 default:
749 *num = 0;
750 **echo_on = 0;
751 xfree(msg);
752 ctxt->pam_done = -1;
753 return (-1);
754 }
755 }
756 return (-1);
757}
758
759/* XXX - see also comment in auth-chall.c:verify_response */
760static int
761sshpam_respond(void *ctx, u_int num, char **resp)
762{
763 Buffer buffer;
764 struct pam_ctxt *ctxt = ctx;
765
Darren Tuckera8df9242004-01-15 00:15:07 +1100766 debug2("PAM: %s entering, %d responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000767 switch (ctxt->pam_done) {
768 case 1:
769 sshpam_authenticated = 1;
770 return (0);
771 case 0:
772 break;
773 default:
774 return (-1);
775 }
776 if (num != 1) {
777 error("PAM: expected one response, got %u", num);
778 return (-1);
779 }
780 buffer_init(&buffer);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100781 if (sshpam_authctxt->valid &&
782 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller37294fb2005-07-17 17:18:49 +1000783 options.permit_root_login == PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100784 buffer_put_cstring(&buffer, *resp);
785 else
786 buffer_put_cstring(&buffer, badpw);
Damien Miller9bdba702003-11-17 21:27:55 +1100787 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
788 buffer_free(&buffer);
789 return (-1);
790 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000791 buffer_free(&buffer);
792 return (1);
793}
794
795static void
796sshpam_free_ctx(void *ctxtp)
797{
798 struct pam_ctxt *ctxt = ctxtp;
799
Darren Tuckera8df9242004-01-15 00:15:07 +1100800 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000801 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000802 xfree(ctxt);
803 /*
804 * We don't call sshpam_cleanup() here because we may need the PAM
805 * handle at a later stage, e.g. when setting up a session. It's
806 * still on the cleanup list, so pam_end() *will* be called before
807 * the server process terminates.
808 */
809}
810
811KbdintDevice sshpam_device = {
812 "pam",
813 sshpam_init_ctx,
814 sshpam_query,
815 sshpam_respond,
816 sshpam_free_ctx
817};
818
819KbdintDevice mm_sshpam_device = {
820 "pam",
821 mm_sshpam_init_ctx,
822 mm_sshpam_query,
823 mm_sshpam_respond,
824 mm_sshpam_free_ctx
825};
826
827/*
828 * This replaces auth-pam.c
829 */
830void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100831start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000832{
Damien Miller9d507da2003-05-14 15:31:12 +1000833 if (!options.use_pam)
834 fatal("PAM: initialisation requested when UsePAM=no");
835
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100836 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000837 fatal("PAM: initialisation failed");
838}
839
840void
841finish_pam(void)
842{
Darren Tucker8846a072003-10-07 11:30:15 +1000843 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000844}
845
Damien Miller1f499fd2003-08-25 13:08:49 +1000846u_int
847do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000848{
Darren Tucker77fc29e2004-09-11 23:07:03 +1000849 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +1100850 if (sshpam_account_status != -1)
851 return (sshpam_account_status);
852
Damien Miller1f499fd2003-08-25 13:08:49 +1000853 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000854 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
855 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +1000856
Darren Tucker07705c72003-12-18 15:34:31 +1100857 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
858 sshpam_account_status = 0;
859 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000860 }
861
Darren Tucker07705c72003-12-18 15:34:31 +1100862 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +1000863 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +1100864
865 sshpam_account_status = 1;
866 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000867}
868
869void
Damien Miller341c6e62003-09-02 23:18:52 +1000870do_pam_set_tty(const char *tty)
871{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000872 if (tty != NULL) {
873 debug("PAM: setting PAM_TTY to \"%s\"", tty);
874 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
875 if (sshpam_err != PAM_SUCCESS)
876 fatal("PAM: failed to set PAM_TTY: %s",
877 pam_strerror(sshpam_handle, sshpam_err));
878 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000879}
880
881void
882do_pam_setcred(int init)
883{
884 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +1000885 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000886 if (sshpam_err != PAM_SUCCESS)
887 fatal("PAM: failed to set PAM_CONV: %s",
888 pam_strerror(sshpam_handle, sshpam_err));
889 if (init) {
890 debug("PAM: establishing credentials");
891 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
892 } else {
893 debug("PAM: reinitializing credentials");
894 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
895 }
896 if (sshpam_err == PAM_SUCCESS) {
897 sshpam_cred_established = 1;
898 return;
899 }
900 if (sshpam_authenticated)
901 fatal("PAM: pam_setcred(): %s",
902 pam_strerror(sshpam_handle, sshpam_err));
903 else
904 debug("PAM: pam_setcred(): %s",
905 pam_strerror(sshpam_handle, sshpam_err));
906}
907
Damien Miller4f9f42a2003-05-10 19:28:02 +1000908static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000909sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000910 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000911{
912 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000913 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000914 int i;
915
Darren Tuckerba53b832004-02-17 20:46:59 +1100916 debug3("PAM: %s called with %d messages", __func__, n);
917
Damien Miller5c3a5582003-09-23 22:12:38 +1000918 *resp = NULL;
919
Darren Tucker18df00c2003-11-18 12:42:07 +1100920 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000921 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000922
923 if ((reply = malloc(n * sizeof(*reply))) == NULL)
924 return (PAM_CONV_ERR);
925 memset(reply, 0, n * sizeof(*reply));
926
Damien Miller4f9f42a2003-05-10 19:28:02 +1000927 for (i = 0; i < n; ++i) {
928 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
929 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000930 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100931 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000932 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000933 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000934 break;
935 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100936 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000937 fgets(input, sizeof input, stdin);
Damien Millera6fb77f2004-07-19 09:39:11 +1000938 if ((reply[i].resp = strdup(input)) == NULL)
939 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000940 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000941 break;
942 case PAM_ERROR_MSG:
943 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100944 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000945 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000946 break;
947 default:
948 goto fail;
949 }
950 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000951 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000952 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000953
Damien Miller4f9f42a2003-05-10 19:28:02 +1000954 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000955 for(i = 0; i < n; i++) {
956 if (reply[i].resp != NULL)
957 xfree(reply[i].resp);
958 }
959 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000960 return (PAM_CONV_ERR);
961}
962
Darren Tucker94befab2004-06-03 14:53:12 +1000963static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +1100964
Damien Miller4f9f42a2003-05-10 19:28:02 +1000965/*
966 * XXX this should be done in the authentication phase, but ssh1 doesn't
967 * support that
968 */
969void
970do_pam_chauthtok(void)
971{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000972 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000973 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000974 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100975 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000976 if (sshpam_err != PAM_SUCCESS)
977 fatal("PAM: failed to set PAM_CONV: %s",
978 pam_strerror(sshpam_handle, sshpam_err));
979 debug("PAM: changing password");
980 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
981 if (sshpam_err != PAM_SUCCESS)
982 fatal("PAM: pam_chauthtok(): %s",
983 pam_strerror(sshpam_handle, sshpam_err));
984}
985
Darren Tucker18df00c2003-11-18 12:42:07 +1100986void
987do_pam_session(void)
988{
Darren Tucker1921ed92004-02-10 13:23:28 +1100989 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +1100990 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100991 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +1100992 if (sshpam_err != PAM_SUCCESS)
993 fatal("PAM: failed to set PAM_CONV: %s",
994 pam_strerror(sshpam_handle, sshpam_err));
995 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +1000996 if (sshpam_err == PAM_SUCCESS)
997 sshpam_session_open = 1;
998 else {
999 sshpam_session_open = 0;
1000 disable_forwarding();
1001 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001002 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001003 }
1004
1005}
1006
1007int
1008is_pam_session_open(void)
1009{
1010 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001011}
1012
Damien Millera8e06ce2003-11-21 23:48:55 +11001013/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001014 * Set a PAM environment string. We need to do this so that the session
1015 * modules can handle things like Kerberos/GSI credentials that appear
1016 * during the ssh authentication process.
1017 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001018int
Damien Millera8e06ce2003-11-21 23:48:55 +11001019do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001020{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001021 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +11001022#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +10001023 char *compound;
1024 size_t len;
1025
1026 len = strlen(name) + strlen(value) + 2;
1027 compound = xmalloc(len);
1028
1029 snprintf(compound, len, "%s=%s", name, value);
1030 ret = pam_putenv(sshpam_handle, compound);
1031 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001032#endif
Damien Millerf2728092003-09-17 07:24:25 +10001033
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001034 return (ret);
1035}
1036
Damien Miller4f9f42a2003-05-10 19:28:02 +10001037char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001038fetch_pam_child_environment(void)
1039{
1040 return sshpam_env;
1041}
1042
1043char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001044fetch_pam_environment(void)
1045{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001046 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001047}
1048
1049void
1050free_pam_environment(char **env)
1051{
1052 char **envp;
1053
Damien Millere27c6cc2003-05-16 18:21:01 +10001054 if (env == NULL)
1055 return;
1056
Damien Miller4f9f42a2003-05-10 19:28:02 +10001057 for (envp = env; *envp; envp++)
1058 xfree(*envp);
1059 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001060}
1061
Darren Tucker450a1582004-05-30 20:43:59 +10001062/*
1063 * "Blind" conversation function for password authentication. Assumes that
1064 * echo-off prompts are for the password and stores messages for later
1065 * display.
1066 */
1067static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001068sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001069 struct pam_response **resp, void *data)
1070{
1071 struct pam_response *reply;
1072 int i;
1073 size_t len;
1074
1075 debug3("PAM: %s called with %d messages", __func__, n);
1076
1077 *resp = NULL;
1078
1079 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1080 return (PAM_CONV_ERR);
1081
1082 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1083 return (PAM_CONV_ERR);
1084 memset(reply, 0, n * sizeof(*reply));
1085
1086 for (i = 0; i < n; ++i) {
1087 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1088 case PAM_PROMPT_ECHO_OFF:
1089 if (sshpam_password == NULL)
1090 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001091 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1092 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001093 reply[i].resp_retcode = PAM_SUCCESS;
1094 break;
1095 case PAM_ERROR_MSG:
1096 case PAM_TEXT_INFO:
1097 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1098 if (len > 0) {
1099 buffer_append(&loginmsg,
1100 PAM_MSG_MEMBER(msg, i, msg), len);
1101 buffer_append(&loginmsg, "\n", 1);
1102 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001103 if ((reply[i].resp = strdup("")) == NULL)
1104 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001105 reply[i].resp_retcode = PAM_SUCCESS;
1106 break;
1107 default:
1108 goto fail;
1109 }
1110 }
1111 *resp = reply;
1112 return (PAM_SUCCESS);
1113
Damien Miller94cf4c82005-07-17 17:04:47 +10001114 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001115 for(i = 0; i < n; i++) {
1116 if (reply[i].resp != NULL)
1117 xfree(reply[i].resp);
1118 }
1119 xfree(reply);
1120 return (PAM_CONV_ERR);
1121}
1122
1123static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1124
1125/*
1126 * Attempt password authentication via PAM
1127 */
1128int
1129sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1130{
1131 int flags = (options.permit_empty_passwd == 0 ?
1132 PAM_DISALLOW_NULL_AUTHTOK : 0);
1133
1134 if (!options.use_pam || sshpam_handle == NULL)
1135 fatal("PAM: %s called when PAM disabled or failed to "
1136 "initialise.", __func__);
1137
1138 sshpam_password = password;
1139 sshpam_authctxt = authctxt;
1140
Darren Tuckere061b152004-05-30 22:04:56 +10001141 /*
1142 * If the user logging in is invalid, or is root but is not permitted
1143 * by PermitRootLogin, use an invalid password to prevent leaking
1144 * information via timing (eg if the PAM config has a delay on fail).
1145 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001146 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001147 options.permit_root_login != PERMIT_YES))
Darren Tuckere061b152004-05-30 22:04:56 +10001148 sshpam_password = badpw;
1149
Darren Tucker450a1582004-05-30 20:43:59 +10001150 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1151 (const void *)&passwd_conv);
1152 if (sshpam_err != PAM_SUCCESS)
1153 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1154 pam_strerror(sshpam_handle, sshpam_err));
1155
1156 sshpam_err = pam_authenticate(sshpam_handle, flags);
1157 sshpam_password = NULL;
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001158 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001159 debug("PAM: password authentication accepted for %.100s",
1160 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001161 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001162 } else {
1163 debug("PAM: password authentication failed for %.100s: %s",
1164 authctxt->valid ? authctxt->user : "an illegal user",
1165 pam_strerror(sshpam_handle, sshpam_err));
1166 return 0;
1167 }
1168}
Damien Millere72b7af1999-12-30 15:08:44 +11001169#endif /* USE_PAM */