blob: 5ddc8bec31aabaea564d18b24c43139fae50c582 [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>
54#include <signal.h>
Damien Millere72b7af1999-12-30 15:08:44 +110055
56#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110057#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100058#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110059#elif defined (HAVE_PAM_PAM_APPL_H)
60#include <pam/pam_appl.h>
61#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100062
Darren Tuckerf08bdb52005-05-26 19:59:48 +100063/* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
64#ifdef PAM_SUN_CODEBASE
65# define sshpam_const /* Solaris, HP-UX, AIX */
66#else
67# define sshpam_const const /* LinuxPAM, OpenPAM */
68#endif
69
Kevin Stevese683e762002-04-04 19:02:28 +000070#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110071#include "auth-pam.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100072#include "buffer.h"
73#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100075#include "log.h"
76#include "monitor_wrap.h"
77#include "msg.h"
78#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100079#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100080#include "servconf.h"
81#include "ssh2.h"
82#include "xmalloc.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100083#include "auth-options.h"
Damien Millere72b7af1999-12-30 15:08:44 +110084
Damien Miller4e448a32003-05-14 15:11:48 +100085extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +110086extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +110087extern int compat20;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +100088extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +100089
Darren Tucker328118a2005-05-25 16:18:09 +100090/* so we don't silently change behaviour */
Damien Miller4f9f42a2003-05-10 19:28:02 +100091#ifdef USE_POSIX_THREADS
Darren Tucker328118a2005-05-25 16:18:09 +100092# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
93#endif
94
95/*
96 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
97 * and generally a bad idea. Use at own risk and do not expect support if
98 * this breaks.
99 */
100#ifdef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000101#include <pthread.h>
102/*
Damien Millera8e06ce2003-11-21 23:48:55 +1100103 * Avoid namespace clash when *not* using pthreads for systems *with*
104 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +1000105 * (e.g. Linux)
106 */
Damien Millera8e06ce2003-11-21 23:48:55 +1100107typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000108#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100109typedef pid_t sp_pthread_t;
110#endif
111
112struct pam_ctxt {
113 sp_pthread_t pam_thread;
114 int pam_psock;
115 int pam_csock;
116 int pam_done;
117};
118
119static void sshpam_free_ctx(void *);
120static struct pam_ctxt *cleanup_ctxt;
121
Darren Tucker328118a2005-05-25 16:18:09 +1000122#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000123/*
124 * Simulate threads with processes.
125 */
Kevin Steves63007d42002-07-21 17:57:01 +0000126
Darren Tucker749bc952004-01-14 22:14:04 +1100127static int sshpam_thread_status = -1;
128static mysig_t sshpam_oldsig;
129
Damien Miller94cf4c82005-07-17 17:04:47 +1000130static void
Darren Tucker749bc952004-01-14 22:14:04 +1100131sshpam_sigchld_handler(int sig)
132{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000133 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100134 if (cleanup_ctxt == NULL)
135 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000136 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000137 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000138 /* PAM thread has not exitted, privsep slave must have */
139 kill(cleanup_ctxt->pam_thread, SIGTERM);
140 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
Darren Tucker5d423f42004-07-11 16:54:08 +1000141 <= 0)
Darren Tuckerb53355e2004-05-24 11:55:36 +1000142 return; /* could not wait */
143 }
Darren Tucker749bc952004-01-14 22:14:04 +1100144 if (WIFSIGNALED(sshpam_thread_status) &&
145 WTERMSIG(sshpam_thread_status) == SIGTERM)
146 return; /* terminated by pthread_cancel */
147 if (!WIFEXITED(sshpam_thread_status))
148 fatal("PAM: authentication thread exited unexpectedly");
149 if (WEXITSTATUS(sshpam_thread_status) != 0)
150 fatal("PAM: authentication thread exited uncleanly");
151}
152
Damien Miller4f9f42a2003-05-10 19:28:02 +1000153static void
154pthread_exit(void *value __unused)
155{
156 _exit(0);
157}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000158
Damien Miller4f9f42a2003-05-10 19:28:02 +1000159static int
160pthread_create(sp_pthread_t *thread, const void *attr __unused,
161 void *(*thread_start)(void *), void *arg)
162{
163 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000164 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100165
Darren Tuckerb9b60212004-03-04 20:03:54 +1100166 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000167 switch ((pid = fork())) {
168 case -1:
169 error("fork(): %s", strerror(errno));
170 return (-1);
171 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000172 close(ctx->pam_psock);
173 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000174 thread_start(arg);
175 _exit(1);
176 default:
177 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000178 close(ctx->pam_csock);
179 ctx->pam_csock = -1;
Darren Tucker749bc952004-01-14 22:14:04 +1100180 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000181 return (0);
182 }
183}
Damien Millere72b7af1999-12-30 15:08:44 +1100184
Damien Miller4f9f42a2003-05-10 19:28:02 +1000185static int
186pthread_cancel(sp_pthread_t thread)
187{
Darren Tucker7ae09622004-01-14 23:07:56 +1100188 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000189 return (kill(thread, SIGTERM));
190}
191
192static int
193pthread_join(sp_pthread_t thread, void **value __unused)
194{
195 int status;
196
Darren Tucker749bc952004-01-14 22:14:04 +1100197 if (sshpam_thread_status != -1)
198 return (sshpam_thread_status);
199 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000200 waitpid(thread, &status, 0);
201 return (status);
202}
203#endif
204
205
Damien Miller5c3a5582003-09-23 22:12:38 +1000206static pam_handle_t *sshpam_handle = NULL;
207static int sshpam_err = 0;
208static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000209static int sshpam_session_open = 0;
210static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100211static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100212static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000213static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000214static const char *sshpam_password = NULL;
Darren Tucker36a3d602005-01-20 12:43:38 +1100215static char badpw[] = "\b\n\r\177INCORRECT";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000216
Damien Millerc756e9b2003-11-17 21:41:42 +1100217/* Some PAM implementations don't implement this */
218#ifndef HAVE_PAM_GETENVLIST
219static char **
220pam_getenvlist(pam_handle_t *pamh)
221{
222 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100223 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100224 * for platforms without pam_getenvlist by searching for known
225 * env vars (e.g. KRB5CCNAME) from the PAM environment.
226 */
227 return NULL;
228}
229#endif
230
Darren Tucker21dd0892004-08-16 23:12:05 +1000231/*
232 * Some platforms, notably Solaris, do not enforce password complexity
233 * rules during pam_chauthtok() if the real uid of the calling process
234 * is 0, on the assumption that it's being called by "passwd" run by root.
235 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
236 * the right thing.
237 */
238#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
239static int
240sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
241{
242 int result;
243
244 if (sshpam_authctxt == NULL)
245 fatal("PAM: sshpam_authctxt not initialized");
246 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
247 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
248 result = pam_chauthtok(pamh, flags);
249 if (setreuid(0, -1) == -1)
250 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
251 return result;
252}
253# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
254#endif
255
Darren Tucker07705c72003-12-18 15:34:31 +1100256void
Darren Tucker17db1c42004-06-19 12:54:38 +1000257sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100258{
Darren Tuckera8df9242004-01-15 00:15:07 +1100259 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000260 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100261 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000262 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100263 if (reqd) {
264 no_port_forwarding_flag |= 2;
265 no_agent_forwarding_flag |= 2;
266 no_x11_forwarding_flag |= 2;
267 } else {
268 no_port_forwarding_flag &= ~2;
269 no_agent_forwarding_flag &= ~2;
270 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100271 }
272}
Darren Tucker1921ed92004-02-10 13:23:28 +1100273
Damien Millerc756e9b2003-11-17 21:41:42 +1100274/* Import regular and PAM environment from subprocess */
275static void
276import_environments(Buffer *b)
277{
278 char *env;
279 u_int i, num_env;
280 int err;
281
Darren Tuckera8df9242004-01-15 00:15:07 +1100282 debug3("PAM: %s entering", __func__);
283
Darren Tucker328118a2005-05-25 16:18:09 +1000284#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100285 /* Import variables set by do_pam_account */
286 sshpam_account_status = buffer_get_int(b);
Darren Tucker17db1c42004-06-19 12:54:38 +1000287 sshpam_password_change_required(buffer_get_int(b));
Darren Tucker07705c72003-12-18 15:34:31 +1100288
Damien Millerc756e9b2003-11-17 21:41:42 +1100289 /* Import environment from subprocess */
290 num_env = buffer_get_int(b);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000291 if (num_env > 1024)
292 fatal("%s: received %u environment variables, expected <= 1024",
293 __func__, num_env);
294 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
Damien Millerc756e9b2003-11-17 21:41:42 +1100295 debug3("PAM: num env strings %d", num_env);
296 for(i = 0; i < num_env; i++)
297 sshpam_env[i] = buffer_get_string(b, NULL);
298
299 sshpam_env[num_env] = NULL;
300
301 /* Import PAM environment from subprocess */
302 num_env = buffer_get_int(b);
303 debug("PAM: num PAM env strings %d", num_env);
304 for(i = 0; i < num_env; i++) {
305 env = buffer_get_string(b, NULL);
306
Darren Tucker8a1624c2003-11-18 12:45:35 +1100307#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100308 /* Errors are not fatal here */
309 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
310 error("PAM: pam_putenv: %s",
311 pam_strerror(sshpam_handle, sshpam_err));
312 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100313#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100314 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100315#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100316}
317
Damien Miller9d5705a2000-09-16 16:09:27 +1100318/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000319 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100320 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000321static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000322sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000323 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100324{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000325 Buffer buffer;
326 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000327 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000328 int i;
329
Darren Tuckerba53b832004-02-17 20:46:59 +1100330 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000331 *resp = NULL;
332
Darren Tucker59e06022004-06-30 20:34:31 +1000333 if (data == NULL) {
334 error("PAM: conversation function passed a null context");
335 return (PAM_CONV_ERR);
336 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000337 ctxt = data;
338 if (n <= 0 || n > PAM_MAX_NUM_MSG)
339 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000340
Darren Tuckerd8093e42006-05-04 16:24:34 +1000341 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000342 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000343
Damien Miller4f9f42a2003-05-10 19:28:02 +1000344 buffer_init(&buffer);
345 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000346 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
347 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100348 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000349 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100350 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100351 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
352 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100353 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100354 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000355 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
356 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000357 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000358 break;
359 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100360 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000361 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100362 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100363 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
364 goto fail;
365 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
366 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000367 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
368 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000369 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000370 break;
371 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100372 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000373 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100374 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100375 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
376 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000377 break;
378 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100379 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000380 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100381 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100382 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
383 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000384 break;
385 default:
386 goto fail;
387 }
388 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000389 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000390 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000391 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000392 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000393
Damien Miller4f9f42a2003-05-10 19:28:02 +1000394 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000395 for(i = 0; i < n; i++) {
396 if (reply[i].resp != NULL)
397 xfree(reply[i].resp);
398 }
399 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000400 buffer_free(&buffer);
401 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000402}
403
Damien Miller4f9f42a2003-05-10 19:28:02 +1000404/*
405 * Authentication thread.
406 */
407static void *
408sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100409{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000410 struct pam_ctxt *ctxt = ctxtp;
411 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000412 struct pam_conv sshpam_conv;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000413 int flags = (options.permit_empty_passwd == 0 ?
414 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000415#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100416 extern char **environ;
417 char **env_from_pam;
418 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000419 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000420 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000421
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000422 pam_get_item(sshpam_handle, PAM_USER,
423 (sshpam_const void **)ptr_pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100424 environ[0] = NULL;
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000425
426 if (sshpam_authctxt != NULL) {
427 setproctitle("%s [pam]",
428 sshpam_authctxt->valid ? pam_user : "unknown");
429 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000430#endif
431
Damien Millerf4b6f102003-09-02 23:12:06 +1000432 sshpam_conv.conv = sshpam_thread_conv;
433 sshpam_conv.appdata_ptr = ctxt;
434
Darren Tucker17addf02004-03-30 20:57:57 +1000435 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100436 fatal("%s: PAM authctxt not initialized", __func__);
437
Damien Miller4f9f42a2003-05-10 19:28:02 +1000438 buffer_init(&buffer);
439 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
440 (const void *)&sshpam_conv);
441 if (sshpam_err != PAM_SUCCESS)
442 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000443 sshpam_err = pam_authenticate(sshpam_handle, flags);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000444 if (sshpam_err != PAM_SUCCESS)
445 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100446
Darren Tuckerc376c862003-12-18 16:08:59 +1100447 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100448 if (!do_pam_account())
449 goto auth_fail;
Darren Tucker17addf02004-03-30 20:57:57 +1000450 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100451 sshpam_err = pam_chauthtok(sshpam_handle,
452 PAM_CHANGE_EXPIRED_AUTHTOK);
453 if (sshpam_err != PAM_SUCCESS)
454 goto auth_fail;
Darren Tucker17db1c42004-06-19 12:54:38 +1000455 sshpam_password_change_required(0);
Darren Tucker07705c72003-12-18 15:34:31 +1100456 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100457 }
Darren Tucker07705c72003-12-18 15:34:31 +1100458
Damien Miller4f9f42a2003-05-10 19:28:02 +1000459 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100460
Darren Tucker328118a2005-05-25 16:18:09 +1000461#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100462 /* Export variables set by do_pam_account */
463 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000464 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100465
Damien Millerc756e9b2003-11-17 21:41:42 +1100466 /* Export any environment strings set in child */
467 for(i = 0; environ[i] != NULL; i++)
468 ; /* Count */
469 buffer_put_int(&buffer, i);
470 for(i = 0; environ[i] != NULL; i++)
471 buffer_put_cstring(&buffer, environ[i]);
472
473 /* Export any environment strings set by PAM in child */
474 env_from_pam = pam_getenvlist(sshpam_handle);
475 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
476 ; /* Count */
477 buffer_put_int(&buffer, i);
478 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
479 buffer_put_cstring(&buffer, env_from_pam[i]);
Darren Tucker328118a2005-05-25 16:18:09 +1000480#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100481
Damien Miller9bdba702003-11-17 21:27:55 +1100482 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000483 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
484 buffer_free(&buffer);
485 pthread_exit(NULL);
486
487 auth_fail:
488 buffer_put_cstring(&buffer,
489 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100490 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000491 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
492 buffer_free(&buffer);
493 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100494
Damien Miller4f9f42a2003-05-10 19:28:02 +1000495 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000496}
497
Darren Tucker8846a072003-10-07 11:30:15 +1000498void
499sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000500{
Darren Tucker8846a072003-10-07 11:30:15 +1000501 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000502
Darren Tuckera8df9242004-01-15 00:15:07 +1100503 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000504 if (ctxt != NULL && ctxt->pam_thread != 0) {
505 pthread_cancel(ctxt->pam_thread);
506 pthread_join(ctxt->pam_thread, NULL);
507 close(ctxt->pam_psock);
508 close(ctxt->pam_csock);
509 memset(ctxt, 0, sizeof(*ctxt));
510 cleanup_ctxt = NULL;
511 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000512}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000513
Damien Miller4f9f42a2003-05-10 19:28:02 +1000514static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000515sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000516 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000517{
Darren Tuckerba53b832004-02-17 20:46:59 +1100518 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000519 return (PAM_CONV_ERR);
520}
Damien Miller63dc3e92001-02-07 12:58:33 +1100521
Damien Miller4f9f42a2003-05-10 19:28:02 +1000522static struct pam_conv null_conv = { sshpam_null_conv, NULL };
523
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000524static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000525sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000526 struct pam_response **resp, void *data)
527{
528 struct pam_response *reply;
529 int i;
530 size_t len;
531
532 debug3("PAM: %s called with %d messages", __func__, n);
533 *resp = NULL;
534
535 if (n <= 0 || n > PAM_MAX_NUM_MSG)
536 return (PAM_CONV_ERR);
537
Darren Tuckerd8093e42006-05-04 16:24:34 +1000538 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000539 return (PAM_CONV_ERR);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000540
541 for (i = 0; i < n; ++i) {
542 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
543 case PAM_ERROR_MSG:
544 case PAM_TEXT_INFO:
545 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
546 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
547 buffer_append(&loginmsg, "\n", 1 );
548 reply[i].resp_retcode = PAM_SUCCESS;
549 break;
550 default:
551 goto fail;
552 }
553 }
554 *resp = reply;
555 return (PAM_SUCCESS);
556
557 fail:
558 for(i = 0; i < n; i++) {
559 if (reply[i].resp != NULL)
560 xfree(reply[i].resp);
561 }
562 xfree(reply);
563 return (PAM_CONV_ERR);
564}
565
566static struct pam_conv store_conv = { sshpam_store_conv, NULL };
567
Darren Tucker8846a072003-10-07 11:30:15 +1000568void
569sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000570{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000571 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000572 if (sshpam_handle == NULL)
573 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000574 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
575 if (sshpam_cred_established) {
576 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
577 sshpam_cred_established = 0;
578 }
579 if (sshpam_session_open) {
580 pam_close_session(sshpam_handle, PAM_SILENT);
581 sshpam_session_open = 0;
582 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100583 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000584 pam_end(sshpam_handle, sshpam_err);
585 sshpam_handle = NULL;
586}
587
588static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100589sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000590{
Darren Tucker455813b2003-09-13 22:12:11 +1000591 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100592 const char *pam_rhost, *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000593 const char **ptr_pam_user = &pam_user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000594
595 if (sshpam_handle != NULL) {
596 /* We already have a PAM context; check if the user matches */
597 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000598 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000599 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
600 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000601 pam_end(sshpam_handle, sshpam_err);
602 sshpam_handle = NULL;
603 }
604 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000605 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000606 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000607 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100608
Damien Miller4f9f42a2003-05-10 19:28:02 +1000609 if (sshpam_err != PAM_SUCCESS) {
610 pam_end(sshpam_handle, sshpam_err);
611 sshpam_handle = NULL;
612 return (-1);
613 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000614 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000615 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000616 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
617 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000618 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000619 sshpam_handle = NULL;
620 return (-1);
621 }
622#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100623 /*
624 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
625 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000626 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100627 */
Damien Miller25d93422003-05-18 20:45:47 +1000628 debug("PAM: setting PAM_TTY to \"ssh\"");
629 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
630 if (sshpam_err != PAM_SUCCESS) {
631 pam_end(sshpam_handle, sshpam_err);
632 sshpam_handle = NULL;
633 return (-1);
634 }
635#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000636 return (0);
637}
638
639static void *
640sshpam_init_ctx(Authctxt *authctxt)
641{
642 struct pam_ctxt *ctxt;
643 int socks[2];
644
Darren Tuckera8df9242004-01-15 00:15:07 +1100645 debug3("PAM: %s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000646 /* Refuse to start if we don't have PAM enabled */
647 if (!options.use_pam)
648 return NULL;
649
Damien Miller4f9f42a2003-05-10 19:28:02 +1000650 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100651 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000652 error("PAM: initialization failed");
653 return (NULL);
654 }
655
656 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000657 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000658
659 /* Start the authentication thread */
660 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
661 error("PAM: failed create sockets: %s", strerror(errno));
662 xfree(ctxt);
663 return (NULL);
664 }
665 ctxt->pam_psock = socks[0];
666 ctxt->pam_csock = socks[1];
667 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
668 error("PAM: failed to start authentication thread: %s",
669 strerror(errno));
670 close(socks[0]);
671 close(socks[1]);
672 xfree(ctxt);
673 return (NULL);
674 }
Darren Tucker8846a072003-10-07 11:30:15 +1000675 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000676 return (ctxt);
677}
678
679static int
680sshpam_query(void *ctx, char **name, char **info,
681 u_int *num, char ***prompts, u_int **echo_on)
682{
683 Buffer buffer;
684 struct pam_ctxt *ctxt = ctx;
685 size_t plen;
686 u_char type;
687 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000688 size_t len, mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000689
Darren Tuckera8df9242004-01-15 00:15:07 +1100690 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000691 buffer_init(&buffer);
692 *name = xstrdup("");
693 *info = xstrdup("");
694 *prompts = xmalloc(sizeof(char *));
695 **prompts = NULL;
696 plen = 0;
697 *echo_on = xmalloc(sizeof(u_int));
698 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
699 type = buffer_get_char(&buffer);
700 msg = buffer_get_string(&buffer, NULL);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000701 mlen = strlen(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000702 switch (type) {
703 case PAM_PROMPT_ECHO_ON:
704 case PAM_PROMPT_ECHO_OFF:
705 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000706 len = plen + mlen + 1;
Damien Miller36812092006-03-26 14:22:47 +1100707 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000708 strlcpy(**prompts + plen, msg, len - plen);
709 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000710 **echo_on = (type == PAM_PROMPT_ECHO_ON);
711 xfree(msg);
712 return (0);
713 case PAM_ERROR_MSG:
714 case PAM_TEXT_INFO:
715 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000716 len = plen + mlen + 2;
Damien Miller36812092006-03-26 14:22:47 +1100717 **prompts = xrealloc(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000718 strlcpy(**prompts + plen, msg, len - plen);
719 plen += mlen;
720 strlcat(**prompts + plen, "\n", len - plen);
721 plen++;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000722 xfree(msg);
723 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000724 case PAM_AUTH_ERR:
Darren Tucker7b1e6952005-09-28 22:33:27 +1000725 debug3("PAM: PAM_AUTH_ERR");
726 if (**prompts != NULL && strlen(**prompts) != 0) {
727 *info = **prompts;
728 **prompts = NULL;
729 *num = 0;
730 **echo_on = 0;
731 ctxt->pam_done = -1;
Damien Miller66f9eb62006-03-18 23:04:49 +1100732 xfree(msg);
Darren Tucker7b1e6952005-09-28 22:33:27 +1000733 return 0;
734 }
735 /* FALLTHROUGH */
736 case PAM_SUCCESS:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000737 if (**prompts != NULL) {
738 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100739 debug("PAM: %s", **prompts);
740 buffer_append(&loginmsg, **prompts,
741 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000742 xfree(**prompts);
743 **prompts = NULL;
744 }
745 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100746 if (!sshpam_authctxt->valid ||
747 (sshpam_authctxt->pw->pw_uid == 0 &&
748 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100749 fatal("Internal error: PAM auth "
750 "succeeded when it should have "
751 "failed");
Damien Millerc756e9b2003-11-17 21:41:42 +1100752 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000753 *num = 0;
754 **echo_on = 0;
755 ctxt->pam_done = 1;
756 xfree(msg);
757 return (0);
758 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000759 error("PAM: %s for %s%.100s from %.100s", msg,
760 sshpam_authctxt->valid ? "" : "illegal user ",
761 sshpam_authctxt->user,
762 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000763 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000764 default:
765 *num = 0;
766 **echo_on = 0;
767 xfree(msg);
768 ctxt->pam_done = -1;
769 return (-1);
770 }
771 }
772 return (-1);
773}
774
775/* XXX - see also comment in auth-chall.c:verify_response */
776static int
777sshpam_respond(void *ctx, u_int num, char **resp)
778{
779 Buffer buffer;
780 struct pam_ctxt *ctxt = ctx;
781
Darren Tucker1d4ebbf2006-01-29 16:46:13 +1100782 debug2("PAM: %s entering, %u responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000783 switch (ctxt->pam_done) {
784 case 1:
785 sshpam_authenticated = 1;
786 return (0);
787 case 0:
788 break;
789 default:
790 return (-1);
791 }
792 if (num != 1) {
793 error("PAM: expected one response, got %u", num);
794 return (-1);
795 }
796 buffer_init(&buffer);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100797 if (sshpam_authctxt->valid &&
798 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller37294fb2005-07-17 17:18:49 +1000799 options.permit_root_login == PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100800 buffer_put_cstring(&buffer, *resp);
801 else
802 buffer_put_cstring(&buffer, badpw);
Damien Miller9bdba702003-11-17 21:27:55 +1100803 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
804 buffer_free(&buffer);
805 return (-1);
806 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000807 buffer_free(&buffer);
808 return (1);
809}
810
811static void
812sshpam_free_ctx(void *ctxtp)
813{
814 struct pam_ctxt *ctxt = ctxtp;
815
Darren Tuckera8df9242004-01-15 00:15:07 +1100816 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000817 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000818 xfree(ctxt);
819 /*
820 * We don't call sshpam_cleanup() here because we may need the PAM
821 * handle at a later stage, e.g. when setting up a session. It's
822 * still on the cleanup list, so pam_end() *will* be called before
823 * the server process terminates.
824 */
825}
826
827KbdintDevice sshpam_device = {
828 "pam",
829 sshpam_init_ctx,
830 sshpam_query,
831 sshpam_respond,
832 sshpam_free_ctx
833};
834
835KbdintDevice mm_sshpam_device = {
836 "pam",
837 mm_sshpam_init_ctx,
838 mm_sshpam_query,
839 mm_sshpam_respond,
840 mm_sshpam_free_ctx
841};
842
843/*
844 * This replaces auth-pam.c
845 */
846void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100847start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000848{
Damien Miller9d507da2003-05-14 15:31:12 +1000849 if (!options.use_pam)
850 fatal("PAM: initialisation requested when UsePAM=no");
851
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100852 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000853 fatal("PAM: initialisation failed");
854}
855
856void
857finish_pam(void)
858{
Darren Tucker8846a072003-10-07 11:30:15 +1000859 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000860}
861
Damien Miller1f499fd2003-08-25 13:08:49 +1000862u_int
863do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000864{
Darren Tucker77fc29e2004-09-11 23:07:03 +1000865 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +1100866 if (sshpam_account_status != -1)
867 return (sshpam_account_status);
868
Damien Miller1f499fd2003-08-25 13:08:49 +1000869 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000870 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
871 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +1000872
Darren Tucker07705c72003-12-18 15:34:31 +1100873 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
874 sshpam_account_status = 0;
875 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000876 }
877
Darren Tucker07705c72003-12-18 15:34:31 +1100878 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +1000879 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +1100880
881 sshpam_account_status = 1;
882 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000883}
884
885void
Damien Miller341c6e62003-09-02 23:18:52 +1000886do_pam_set_tty(const char *tty)
887{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000888 if (tty != NULL) {
889 debug("PAM: setting PAM_TTY to \"%s\"", tty);
890 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
891 if (sshpam_err != PAM_SUCCESS)
892 fatal("PAM: failed to set PAM_TTY: %s",
893 pam_strerror(sshpam_handle, sshpam_err));
894 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000895}
896
897void
898do_pam_setcred(int init)
899{
900 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +1000901 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000902 if (sshpam_err != PAM_SUCCESS)
903 fatal("PAM: failed to set PAM_CONV: %s",
904 pam_strerror(sshpam_handle, sshpam_err));
905 if (init) {
906 debug("PAM: establishing credentials");
907 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
908 } else {
909 debug("PAM: reinitializing credentials");
910 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
911 }
912 if (sshpam_err == PAM_SUCCESS) {
913 sshpam_cred_established = 1;
914 return;
915 }
916 if (sshpam_authenticated)
917 fatal("PAM: pam_setcred(): %s",
918 pam_strerror(sshpam_handle, sshpam_err));
919 else
920 debug("PAM: pam_setcred(): %s",
921 pam_strerror(sshpam_handle, sshpam_err));
922}
923
Damien Miller4f9f42a2003-05-10 19:28:02 +1000924static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000925sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000926 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000927{
928 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000929 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000930 int i;
931
Darren Tuckerba53b832004-02-17 20:46:59 +1100932 debug3("PAM: %s called with %d messages", __func__, n);
933
Damien Miller5c3a5582003-09-23 22:12:38 +1000934 *resp = NULL;
935
Darren Tucker18df00c2003-11-18 12:42:07 +1100936 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000937 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000938
Darren Tuckerd8093e42006-05-04 16:24:34 +1000939 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000940 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000941
Damien Miller4f9f42a2003-05-10 19:28:02 +1000942 for (i = 0; i < n; ++i) {
943 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
944 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000945 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100946 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000947 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000948 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000949 break;
950 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100951 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000952 fgets(input, sizeof input, stdin);
Damien Millera6fb77f2004-07-19 09:39:11 +1000953 if ((reply[i].resp = strdup(input)) == NULL)
954 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000955 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000956 break;
957 case PAM_ERROR_MSG:
958 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100959 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000960 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000961 break;
962 default:
963 goto fail;
964 }
965 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000966 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000967 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000968
Damien Miller4f9f42a2003-05-10 19:28:02 +1000969 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000970 for(i = 0; i < n; i++) {
971 if (reply[i].resp != NULL)
972 xfree(reply[i].resp);
973 }
974 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000975 return (PAM_CONV_ERR);
976}
977
Darren Tucker94befab2004-06-03 14:53:12 +1000978static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +1100979
Damien Miller4f9f42a2003-05-10 19:28:02 +1000980/*
981 * XXX this should be done in the authentication phase, but ssh1 doesn't
982 * support that
983 */
984void
985do_pam_chauthtok(void)
986{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000987 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000988 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000989 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100990 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000991 if (sshpam_err != PAM_SUCCESS)
992 fatal("PAM: failed to set PAM_CONV: %s",
993 pam_strerror(sshpam_handle, sshpam_err));
994 debug("PAM: changing password");
995 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
996 if (sshpam_err != PAM_SUCCESS)
997 fatal("PAM: pam_chauthtok(): %s",
998 pam_strerror(sshpam_handle, sshpam_err));
999}
1000
Darren Tucker18df00c2003-11-18 12:42:07 +11001001void
1002do_pam_session(void)
1003{
Darren Tucker1921ed92004-02-10 13:23:28 +11001004 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +11001005 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +11001006 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +11001007 if (sshpam_err != PAM_SUCCESS)
1008 fatal("PAM: failed to set PAM_CONV: %s",
1009 pam_strerror(sshpam_handle, sshpam_err));
1010 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +10001011 if (sshpam_err == PAM_SUCCESS)
1012 sshpam_session_open = 1;
1013 else {
1014 sshpam_session_open = 0;
1015 disable_forwarding();
1016 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001017 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001018 }
1019
1020}
1021
1022int
1023is_pam_session_open(void)
1024{
1025 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001026}
1027
Damien Millera8e06ce2003-11-21 23:48:55 +11001028/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001029 * Set a PAM environment string. We need to do this so that the session
1030 * modules can handle things like Kerberos/GSI credentials that appear
1031 * during the ssh authentication process.
1032 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001033int
Damien Millera8e06ce2003-11-21 23:48:55 +11001034do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001035{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001036 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +11001037#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +10001038 char *compound;
1039 size_t len;
1040
1041 len = strlen(name) + strlen(value) + 2;
1042 compound = xmalloc(len);
1043
1044 snprintf(compound, len, "%s=%s", name, value);
1045 ret = pam_putenv(sshpam_handle, compound);
1046 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001047#endif
Damien Millerf2728092003-09-17 07:24:25 +10001048
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001049 return (ret);
1050}
1051
Damien Miller4f9f42a2003-05-10 19:28:02 +10001052char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001053fetch_pam_child_environment(void)
1054{
1055 return sshpam_env;
1056}
1057
1058char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001059fetch_pam_environment(void)
1060{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001061 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062}
1063
1064void
1065free_pam_environment(char **env)
1066{
1067 char **envp;
1068
Damien Millere27c6cc2003-05-16 18:21:01 +10001069 if (env == NULL)
1070 return;
1071
Damien Miller4f9f42a2003-05-10 19:28:02 +10001072 for (envp = env; *envp; envp++)
1073 xfree(*envp);
1074 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001075}
1076
Darren Tucker450a1582004-05-30 20:43:59 +10001077/*
1078 * "Blind" conversation function for password authentication. Assumes that
1079 * echo-off prompts are for the password and stores messages for later
1080 * display.
1081 */
1082static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001083sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001084 struct pam_response **resp, void *data)
1085{
1086 struct pam_response *reply;
1087 int i;
1088 size_t len;
1089
1090 debug3("PAM: %s called with %d messages", __func__, n);
1091
1092 *resp = NULL;
1093
1094 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1095 return (PAM_CONV_ERR);
1096
1097 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1098 return (PAM_CONV_ERR);
1099 memset(reply, 0, n * sizeof(*reply));
1100
1101 for (i = 0; i < n; ++i) {
1102 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1103 case PAM_PROMPT_ECHO_OFF:
1104 if (sshpam_password == NULL)
1105 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001106 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1107 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001108 reply[i].resp_retcode = PAM_SUCCESS;
1109 break;
1110 case PAM_ERROR_MSG:
1111 case PAM_TEXT_INFO:
1112 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1113 if (len > 0) {
1114 buffer_append(&loginmsg,
1115 PAM_MSG_MEMBER(msg, i, msg), len);
1116 buffer_append(&loginmsg, "\n", 1);
1117 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001118 if ((reply[i].resp = strdup("")) == NULL)
1119 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001120 reply[i].resp_retcode = PAM_SUCCESS;
1121 break;
1122 default:
1123 goto fail;
1124 }
1125 }
1126 *resp = reply;
1127 return (PAM_SUCCESS);
1128
Damien Miller94cf4c82005-07-17 17:04:47 +10001129 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001130 for(i = 0; i < n; i++) {
1131 if (reply[i].resp != NULL)
1132 xfree(reply[i].resp);
1133 }
1134 xfree(reply);
1135 return (PAM_CONV_ERR);
1136}
1137
1138static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1139
1140/*
1141 * Attempt password authentication via PAM
1142 */
1143int
1144sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1145{
1146 int flags = (options.permit_empty_passwd == 0 ?
1147 PAM_DISALLOW_NULL_AUTHTOK : 0);
1148
1149 if (!options.use_pam || sshpam_handle == NULL)
1150 fatal("PAM: %s called when PAM disabled or failed to "
1151 "initialise.", __func__);
1152
1153 sshpam_password = password;
1154 sshpam_authctxt = authctxt;
1155
Darren Tuckere061b152004-05-30 22:04:56 +10001156 /*
1157 * If the user logging in is invalid, or is root but is not permitted
1158 * by PermitRootLogin, use an invalid password to prevent leaking
1159 * information via timing (eg if the PAM config has a delay on fail).
1160 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001161 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001162 options.permit_root_login != PERMIT_YES))
Darren Tuckere061b152004-05-30 22:04:56 +10001163 sshpam_password = badpw;
1164
Darren Tucker450a1582004-05-30 20:43:59 +10001165 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1166 (const void *)&passwd_conv);
1167 if (sshpam_err != PAM_SUCCESS)
1168 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1169 pam_strerror(sshpam_handle, sshpam_err));
1170
1171 sshpam_err = pam_authenticate(sshpam_handle, flags);
1172 sshpam_password = NULL;
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001173 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001174 debug("PAM: password authentication accepted for %.100s",
1175 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001176 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001177 } else {
1178 debug("PAM: password authentication failed for %.100s: %s",
1179 authctxt->valid ? authctxt->user : "an illegal user",
1180 pam_strerror(sshpam_handle, sshpam_err));
1181 return 0;
1182 }
1183}
Damien Millere72b7af1999-12-30 15:08:44 +11001184#endif /* USE_PAM */