blob: 36a719fbb1e0298634be5d42dbc18860b6a8ad39 [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"
Darren Tucker1f7e4082004-07-01 14:00:14 +100050RCSID("$Id: auth-pam.c,v 1.110 2004/07/01 04:00:15 dtucker 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
Kevin Stevese683e762002-04-04 19:02:28 +000059#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110060#include "auth-pam.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100061#include "buffer.h"
62#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100064#include "log.h"
65#include "monitor_wrap.h"
66#include "msg.h"
67#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100068#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100069#include "servconf.h"
70#include "ssh2.h"
71#include "xmalloc.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100072#include "auth-options.h"
Damien Millere72b7af1999-12-30 15:08:44 +110073
Damien Miller4e448a32003-05-14 15:11:48 +100074extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +110075extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +110076extern int compat20;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +100077extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +100078
Damien Miller4f9f42a2003-05-10 19:28:02 +100079#ifdef USE_POSIX_THREADS
80#include <pthread.h>
81/*
Damien Millera8e06ce2003-11-21 23:48:55 +110082 * Avoid namespace clash when *not* using pthreads for systems *with*
83 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +100084 * (e.g. Linux)
85 */
Damien Millera8e06ce2003-11-21 23:48:55 +110086typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +100087#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +110088typedef pid_t sp_pthread_t;
89#endif
90
91struct pam_ctxt {
92 sp_pthread_t pam_thread;
93 int pam_psock;
94 int pam_csock;
95 int pam_done;
96};
97
98static void sshpam_free_ctx(void *);
99static struct pam_ctxt *cleanup_ctxt;
100
101#ifndef USE_POSIX_THREADS
Damien Miller4f9f42a2003-05-10 19:28:02 +1000102/*
103 * Simulate threads with processes.
104 */
Kevin Steves63007d42002-07-21 17:57:01 +0000105
Darren Tucker749bc952004-01-14 22:14:04 +1100106static int sshpam_thread_status = -1;
107static mysig_t sshpam_oldsig;
108
109static void
110sshpam_sigchld_handler(int sig)
111{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000112 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100113 if (cleanup_ctxt == NULL)
114 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000115 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
116 == -1) {
117 /* PAM thread has not exitted, privsep slave must have */
118 kill(cleanup_ctxt->pam_thread, SIGTERM);
119 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
120 == -1)
121 return; /* could not wait */
122 }
Darren Tucker749bc952004-01-14 22:14:04 +1100123 if (WIFSIGNALED(sshpam_thread_status) &&
124 WTERMSIG(sshpam_thread_status) == SIGTERM)
125 return; /* terminated by pthread_cancel */
126 if (!WIFEXITED(sshpam_thread_status))
127 fatal("PAM: authentication thread exited unexpectedly");
128 if (WEXITSTATUS(sshpam_thread_status) != 0)
129 fatal("PAM: authentication thread exited uncleanly");
130}
131
Damien Miller4f9f42a2003-05-10 19:28:02 +1000132static void
133pthread_exit(void *value __unused)
134{
135 _exit(0);
136}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000137
Damien Miller4f9f42a2003-05-10 19:28:02 +1000138static int
139pthread_create(sp_pthread_t *thread, const void *attr __unused,
140 void *(*thread_start)(void *), void *arg)
141{
142 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +1100143
Darren Tuckerb9b60212004-03-04 20:03:54 +1100144 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000145 switch ((pid = fork())) {
146 case -1:
147 error("fork(): %s", strerror(errno));
148 return (-1);
149 case 0:
150 thread_start(arg);
151 _exit(1);
152 default:
153 *thread = pid;
Darren Tucker749bc952004-01-14 22:14:04 +1100154 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000155 return (0);
156 }
157}
Damien Millere72b7af1999-12-30 15:08:44 +1100158
Damien Miller4f9f42a2003-05-10 19:28:02 +1000159static int
160pthread_cancel(sp_pthread_t thread)
161{
Darren Tucker7ae09622004-01-14 23:07:56 +1100162 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000163 return (kill(thread, SIGTERM));
164}
165
166static int
167pthread_join(sp_pthread_t thread, void **value __unused)
168{
169 int status;
170
Darren Tucker749bc952004-01-14 22:14:04 +1100171 if (sshpam_thread_status != -1)
172 return (sshpam_thread_status);
173 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000174 waitpid(thread, &status, 0);
175 return (status);
176}
177#endif
178
179
Damien Miller5c3a5582003-09-23 22:12:38 +1000180static pam_handle_t *sshpam_handle = NULL;
181static int sshpam_err = 0;
182static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000183static int sshpam_session_open = 0;
184static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100185static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100186static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000187static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000188static const char *sshpam_password = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000189
Damien Millerc756e9b2003-11-17 21:41:42 +1100190/* Some PAM implementations don't implement this */
191#ifndef HAVE_PAM_GETENVLIST
192static char **
193pam_getenvlist(pam_handle_t *pamh)
194{
195 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100196 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100197 * for platforms without pam_getenvlist by searching for known
198 * env vars (e.g. KRB5CCNAME) from the PAM environment.
199 */
200 return NULL;
201}
202#endif
203
Darren Tucker07705c72003-12-18 15:34:31 +1100204void
Darren Tucker17db1c42004-06-19 12:54:38 +1000205sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100206{
Darren Tuckera8df9242004-01-15 00:15:07 +1100207 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000208 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100209 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000210 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100211 if (reqd) {
212 no_port_forwarding_flag |= 2;
213 no_agent_forwarding_flag |= 2;
214 no_x11_forwarding_flag |= 2;
215 } else {
216 no_port_forwarding_flag &= ~2;
217 no_agent_forwarding_flag &= ~2;
218 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100219 }
220}
Darren Tucker1921ed92004-02-10 13:23:28 +1100221
Damien Millerc756e9b2003-11-17 21:41:42 +1100222/* Import regular and PAM environment from subprocess */
223static void
224import_environments(Buffer *b)
225{
226 char *env;
227 u_int i, num_env;
228 int err;
229
Darren Tuckera8df9242004-01-15 00:15:07 +1100230 debug3("PAM: %s entering", __func__);
231
Darren Tucker4b385d42004-03-04 19:54:10 +1100232#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100233 /* Import variables set by do_pam_account */
234 sshpam_account_status = buffer_get_int(b);
Darren Tucker17db1c42004-06-19 12:54:38 +1000235 sshpam_password_change_required(buffer_get_int(b));
Darren Tucker07705c72003-12-18 15:34:31 +1100236
Damien Millerc756e9b2003-11-17 21:41:42 +1100237 /* Import environment from subprocess */
238 num_env = buffer_get_int(b);
239 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
240 debug3("PAM: num env strings %d", num_env);
241 for(i = 0; i < num_env; i++)
242 sshpam_env[i] = buffer_get_string(b, NULL);
243
244 sshpam_env[num_env] = NULL;
245
246 /* Import PAM environment from subprocess */
247 num_env = buffer_get_int(b);
248 debug("PAM: num PAM env strings %d", num_env);
249 for(i = 0; i < num_env; i++) {
250 env = buffer_get_string(b, NULL);
251
Darren Tucker8a1624c2003-11-18 12:45:35 +1100252#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100253 /* Errors are not fatal here */
254 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
255 error("PAM: pam_putenv: %s",
256 pam_strerror(sshpam_handle, sshpam_err));
257 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100258#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100259 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100260#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100261}
262
Damien Miller9d5705a2000-09-16 16:09:27 +1100263/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000264 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100265 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000266static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000267sshpam_thread_conv(int n, struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000268 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100269{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000270 Buffer buffer;
271 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000272 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000273 int i;
274
Darren Tuckerba53b832004-02-17 20:46:59 +1100275 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000276 *resp = NULL;
277
Darren Tucker59e06022004-06-30 20:34:31 +1000278 if (data == NULL) {
279 error("PAM: conversation function passed a null context");
280 return (PAM_CONV_ERR);
281 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000282 ctxt = data;
283 if (n <= 0 || n > PAM_MAX_NUM_MSG)
284 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000285
286 if ((reply = malloc(n * sizeof(*reply))) == NULL)
287 return (PAM_CONV_ERR);
288 memset(reply, 0, n * sizeof(*reply));
289
Damien Miller4f9f42a2003-05-10 19:28:02 +1000290 buffer_init(&buffer);
291 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000292 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
293 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100294 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000295 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100296 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100297 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
298 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100299 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100300 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000301 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
302 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000303 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000304 break;
305 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100306 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000307 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100308 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100309 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
310 goto fail;
311 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
312 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000313 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
314 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000315 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000316 break;
317 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100318 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000319 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100320 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100321 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
322 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000323 break;
324 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100325 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000326 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100327 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100328 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
329 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000330 break;
331 default:
332 goto fail;
333 }
334 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000335 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000336 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000337 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000338 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000339
Damien Miller4f9f42a2003-05-10 19:28:02 +1000340 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000341 for(i = 0; i < n; i++) {
342 if (reply[i].resp != NULL)
343 xfree(reply[i].resp);
344 }
345 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000346 buffer_free(&buffer);
347 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000348}
349
Damien Miller4f9f42a2003-05-10 19:28:02 +1000350/*
351 * Authentication thread.
352 */
353static void *
354sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100355{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000356 struct pam_ctxt *ctxt = ctxtp;
357 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000358 struct pam_conv sshpam_conv;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000359 int flags = (options.permit_empty_passwd == 0 ?
360 PAM_DISALLOW_NULL_AUTHTOK : 0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000361#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100362 extern char **environ;
363 char **env_from_pam;
364 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000365 const char *pam_user;
366
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000367 pam_get_item(sshpam_handle, PAM_USER, (void **)&pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000368 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100369 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000370#endif
371
Damien Millerf4b6f102003-09-02 23:12:06 +1000372 sshpam_conv.conv = sshpam_thread_conv;
373 sshpam_conv.appdata_ptr = ctxt;
374
Darren Tucker17addf02004-03-30 20:57:57 +1000375 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100376 fatal("%s: PAM authctxt not initialized", __func__);
377
Damien Miller4f9f42a2003-05-10 19:28:02 +1000378 buffer_init(&buffer);
379 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
380 (const void *)&sshpam_conv);
381 if (sshpam_err != PAM_SUCCESS)
382 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000383 sshpam_err = pam_authenticate(sshpam_handle, flags);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000384 if (sshpam_err != PAM_SUCCESS)
385 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100386
Darren Tuckerc376c862003-12-18 16:08:59 +1100387 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100388 if (!do_pam_account())
389 goto auth_fail;
Darren Tucker17addf02004-03-30 20:57:57 +1000390 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100391 sshpam_err = pam_chauthtok(sshpam_handle,
392 PAM_CHANGE_EXPIRED_AUTHTOK);
393 if (sshpam_err != PAM_SUCCESS)
394 goto auth_fail;
Darren Tucker17db1c42004-06-19 12:54:38 +1000395 sshpam_password_change_required(0);
Darren Tucker07705c72003-12-18 15:34:31 +1100396 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100397 }
Darren Tucker07705c72003-12-18 15:34:31 +1100398
Damien Miller4f9f42a2003-05-10 19:28:02 +1000399 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100400
401#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100402 /* Export variables set by do_pam_account */
403 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000404 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100405
Damien Millerc756e9b2003-11-17 21:41:42 +1100406 /* Export any environment strings set in child */
407 for(i = 0; environ[i] != NULL; i++)
408 ; /* Count */
409 buffer_put_int(&buffer, i);
410 for(i = 0; environ[i] != NULL; i++)
411 buffer_put_cstring(&buffer, environ[i]);
412
413 /* Export any environment strings set by PAM in child */
414 env_from_pam = pam_getenvlist(sshpam_handle);
415 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
416 ; /* Count */
417 buffer_put_int(&buffer, i);
418 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
419 buffer_put_cstring(&buffer, env_from_pam[i]);
420#endif /* USE_POSIX_THREADS */
421
Damien Miller9bdba702003-11-17 21:27:55 +1100422 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000423 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
424 buffer_free(&buffer);
425 pthread_exit(NULL);
426
427 auth_fail:
428 buffer_put_cstring(&buffer,
429 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100430 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000431 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
432 buffer_free(&buffer);
433 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100434
Damien Miller4f9f42a2003-05-10 19:28:02 +1000435 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000436}
437
Darren Tucker8846a072003-10-07 11:30:15 +1000438void
439sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000440{
Darren Tucker8846a072003-10-07 11:30:15 +1000441 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000442
Darren Tuckera8df9242004-01-15 00:15:07 +1100443 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000444 if (ctxt != NULL && ctxt->pam_thread != 0) {
445 pthread_cancel(ctxt->pam_thread);
446 pthread_join(ctxt->pam_thread, NULL);
447 close(ctxt->pam_psock);
448 close(ctxt->pam_csock);
449 memset(ctxt, 0, sizeof(*ctxt));
450 cleanup_ctxt = NULL;
451 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000452}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000453
Damien Miller4f9f42a2003-05-10 19:28:02 +1000454static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000455sshpam_null_conv(int n, struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000456 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000457{
Darren Tuckerba53b832004-02-17 20:46:59 +1100458 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000459 return (PAM_CONV_ERR);
460}
Damien Miller63dc3e92001-02-07 12:58:33 +1100461
Damien Miller4f9f42a2003-05-10 19:28:02 +1000462static struct pam_conv null_conv = { sshpam_null_conv, NULL };
463
Darren Tucker8846a072003-10-07 11:30:15 +1000464void
465sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000466{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000467 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000468 if (sshpam_handle == NULL)
469 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000470 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
471 if (sshpam_cred_established) {
472 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
473 sshpam_cred_established = 0;
474 }
475 if (sshpam_session_open) {
476 pam_close_session(sshpam_handle, PAM_SILENT);
477 sshpam_session_open = 0;
478 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100479 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000480 pam_end(sshpam_handle, sshpam_err);
481 sshpam_handle = NULL;
482}
483
484static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100485sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000486{
Darren Tucker455813b2003-09-13 22:12:11 +1000487 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100488 const char *pam_rhost, *pam_user, *user = authctxt->user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000489
490 if (sshpam_handle != NULL) {
491 /* We already have a PAM context; check if the user matches */
492 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000493 PAM_USER, (void **)&pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000494 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
495 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000496 pam_end(sshpam_handle, sshpam_err);
497 sshpam_handle = NULL;
498 }
499 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000500 sshpam_err =
501 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000502 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100503
Damien Miller4f9f42a2003-05-10 19:28:02 +1000504 if (sshpam_err != PAM_SUCCESS) {
505 pam_end(sshpam_handle, sshpam_err);
506 sshpam_handle = NULL;
507 return (-1);
508 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000509 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000510 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000511 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
512 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000513 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000514 sshpam_handle = NULL;
515 return (-1);
516 }
517#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100518 /*
519 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
520 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000521 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100522 */
Damien Miller25d93422003-05-18 20:45:47 +1000523 debug("PAM: setting PAM_TTY to \"ssh\"");
524 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
525 if (sshpam_err != PAM_SUCCESS) {
526 pam_end(sshpam_handle, sshpam_err);
527 sshpam_handle = NULL;
528 return (-1);
529 }
530#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000531 return (0);
532}
533
534static void *
535sshpam_init_ctx(Authctxt *authctxt)
536{
537 struct pam_ctxt *ctxt;
538 int socks[2];
539
Darren Tuckera8df9242004-01-15 00:15:07 +1100540 debug3("PAM: %s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000541 /* Refuse to start if we don't have PAM enabled */
542 if (!options.use_pam)
543 return NULL;
544
Damien Miller4f9f42a2003-05-10 19:28:02 +1000545 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100546 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000547 error("PAM: initialization failed");
548 return (NULL);
549 }
550
551 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000552 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000553
554 /* Start the authentication thread */
555 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
556 error("PAM: failed create sockets: %s", strerror(errno));
557 xfree(ctxt);
558 return (NULL);
559 }
560 ctxt->pam_psock = socks[0];
561 ctxt->pam_csock = socks[1];
562 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
563 error("PAM: failed to start authentication thread: %s",
564 strerror(errno));
565 close(socks[0]);
566 close(socks[1]);
567 xfree(ctxt);
568 return (NULL);
569 }
Darren Tucker8846a072003-10-07 11:30:15 +1000570 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000571 return (ctxt);
572}
573
574static int
575sshpam_query(void *ctx, char **name, char **info,
576 u_int *num, char ***prompts, u_int **echo_on)
577{
578 Buffer buffer;
579 struct pam_ctxt *ctxt = ctx;
580 size_t plen;
581 u_char type;
582 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000583 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000584
Darren Tuckera8df9242004-01-15 00:15:07 +1100585 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000586 buffer_init(&buffer);
587 *name = xstrdup("");
588 *info = xstrdup("");
589 *prompts = xmalloc(sizeof(char *));
590 **prompts = NULL;
591 plen = 0;
592 *echo_on = xmalloc(sizeof(u_int));
593 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
594 type = buffer_get_char(&buffer);
595 msg = buffer_get_string(&buffer, NULL);
596 switch (type) {
597 case PAM_PROMPT_ECHO_ON:
598 case PAM_PROMPT_ECHO_OFF:
599 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000600 len = plen + strlen(msg) + 1;
601 **prompts = xrealloc(**prompts, len);
602 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000603 **echo_on = (type == PAM_PROMPT_ECHO_ON);
604 xfree(msg);
605 return (0);
606 case PAM_ERROR_MSG:
607 case PAM_TEXT_INFO:
608 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100609 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000610 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100611 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000612 xfree(msg);
613 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000614 case PAM_SUCCESS:
615 case PAM_AUTH_ERR:
616 if (**prompts != NULL) {
617 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100618 debug("PAM: %s", **prompts);
619 buffer_append(&loginmsg, **prompts,
620 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000621 xfree(**prompts);
622 **prompts = NULL;
623 }
624 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100625 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000626 *num = 0;
627 **echo_on = 0;
628 ctxt->pam_done = 1;
629 xfree(msg);
630 return (0);
631 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000632 error("PAM: %s for %s%.100s from %.100s", msg,
633 sshpam_authctxt->valid ? "" : "illegal user ",
634 sshpam_authctxt->user,
635 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000636 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000637 default:
638 *num = 0;
639 **echo_on = 0;
640 xfree(msg);
641 ctxt->pam_done = -1;
642 return (-1);
643 }
644 }
645 return (-1);
646}
647
648/* XXX - see also comment in auth-chall.c:verify_response */
649static int
650sshpam_respond(void *ctx, u_int num, char **resp)
651{
652 Buffer buffer;
653 struct pam_ctxt *ctxt = ctx;
654
Darren Tuckera8df9242004-01-15 00:15:07 +1100655 debug2("PAM: %s entering, %d responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000656 switch (ctxt->pam_done) {
657 case 1:
658 sshpam_authenticated = 1;
659 return (0);
660 case 0:
661 break;
662 default:
663 return (-1);
664 }
665 if (num != 1) {
666 error("PAM: expected one response, got %u", num);
667 return (-1);
668 }
669 buffer_init(&buffer);
670 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100671 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
672 buffer_free(&buffer);
673 return (-1);
674 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000675 buffer_free(&buffer);
676 return (1);
677}
678
679static void
680sshpam_free_ctx(void *ctxtp)
681{
682 struct pam_ctxt *ctxt = ctxtp;
683
Darren Tuckera8df9242004-01-15 00:15:07 +1100684 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000685 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000686 xfree(ctxt);
687 /*
688 * We don't call sshpam_cleanup() here because we may need the PAM
689 * handle at a later stage, e.g. when setting up a session. It's
690 * still on the cleanup list, so pam_end() *will* be called before
691 * the server process terminates.
692 */
693}
694
695KbdintDevice sshpam_device = {
696 "pam",
697 sshpam_init_ctx,
698 sshpam_query,
699 sshpam_respond,
700 sshpam_free_ctx
701};
702
703KbdintDevice mm_sshpam_device = {
704 "pam",
705 mm_sshpam_init_ctx,
706 mm_sshpam_query,
707 mm_sshpam_respond,
708 mm_sshpam_free_ctx
709};
710
711/*
712 * This replaces auth-pam.c
713 */
714void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100715start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000716{
Damien Miller9d507da2003-05-14 15:31:12 +1000717 if (!options.use_pam)
718 fatal("PAM: initialisation requested when UsePAM=no");
719
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100720 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000721 fatal("PAM: initialisation failed");
722}
723
724void
725finish_pam(void)
726{
Darren Tucker8846a072003-10-07 11:30:15 +1000727 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000728}
729
Damien Miller1f499fd2003-08-25 13:08:49 +1000730u_int
731do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000732{
Darren Tucker07705c72003-12-18 15:34:31 +1100733 if (sshpam_account_status != -1)
734 return (sshpam_account_status);
735
Damien Miller1f499fd2003-08-25 13:08:49 +1000736 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tuckera8df9242004-01-15 00:15:07 +1100737 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100738
739 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
740 sshpam_account_status = 0;
741 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000742 }
743
Darren Tucker07705c72003-12-18 15:34:31 +1100744 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +1000745 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +1100746
747 sshpam_account_status = 1;
748 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000749}
750
751void
Damien Miller341c6e62003-09-02 23:18:52 +1000752do_pam_set_tty(const char *tty)
753{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000754 if (tty != NULL) {
755 debug("PAM: setting PAM_TTY to \"%s\"", tty);
756 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
757 if (sshpam_err != PAM_SUCCESS)
758 fatal("PAM: failed to set PAM_TTY: %s",
759 pam_strerror(sshpam_handle, sshpam_err));
760 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000761}
762
763void
764do_pam_setcred(int init)
765{
766 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
767 (const void *)&null_conv);
768 if (sshpam_err != PAM_SUCCESS)
769 fatal("PAM: failed to set PAM_CONV: %s",
770 pam_strerror(sshpam_handle, sshpam_err));
771 if (init) {
772 debug("PAM: establishing credentials");
773 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
774 } else {
775 debug("PAM: reinitializing credentials");
776 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
777 }
778 if (sshpam_err == PAM_SUCCESS) {
779 sshpam_cred_established = 1;
780 return;
781 }
782 if (sshpam_authenticated)
783 fatal("PAM: pam_setcred(): %s",
784 pam_strerror(sshpam_handle, sshpam_err));
785 else
786 debug("PAM: pam_setcred(): %s",
787 pam_strerror(sshpam_handle, sshpam_err));
788}
789
Damien Miller4f9f42a2003-05-10 19:28:02 +1000790static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000791sshpam_tty_conv(int n, struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000792 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000793{
794 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000795 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000796 int i;
797
Darren Tuckerba53b832004-02-17 20:46:59 +1100798 debug3("PAM: %s called with %d messages", __func__, n);
799
Damien Miller5c3a5582003-09-23 22:12:38 +1000800 *resp = NULL;
801
Darren Tucker18df00c2003-11-18 12:42:07 +1100802 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000803 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000804
805 if ((reply = malloc(n * sizeof(*reply))) == NULL)
806 return (PAM_CONV_ERR);
807 memset(reply, 0, n * sizeof(*reply));
808
Damien Miller4f9f42a2003-05-10 19:28:02 +1000809 for (i = 0; i < n; ++i) {
810 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
811 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000812 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100813 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000814 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000815 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000816 break;
817 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100818 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000819 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000820 reply[i].resp = xstrdup(input);
821 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000822 break;
823 case PAM_ERROR_MSG:
824 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100825 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000826 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000827 break;
828 default:
829 goto fail;
830 }
831 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000832 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000833 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000834
Damien Miller4f9f42a2003-05-10 19:28:02 +1000835 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000836 for(i = 0; i < n; i++) {
837 if (reply[i].resp != NULL)
838 xfree(reply[i].resp);
839 }
840 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000841 return (PAM_CONV_ERR);
842}
843
Darren Tucker94befab2004-06-03 14:53:12 +1000844static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +1100845
Damien Miller4f9f42a2003-05-10 19:28:02 +1000846/*
847 * XXX this should be done in the authentication phase, but ssh1 doesn't
848 * support that
849 */
850void
851do_pam_chauthtok(void)
852{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000853 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000854 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000855 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100856 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000857 if (sshpam_err != PAM_SUCCESS)
858 fatal("PAM: failed to set PAM_CONV: %s",
859 pam_strerror(sshpam_handle, sshpam_err));
860 debug("PAM: changing password");
861 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
862 if (sshpam_err != PAM_SUCCESS)
863 fatal("PAM: pam_chauthtok(): %s",
864 pam_strerror(sshpam_handle, sshpam_err));
865}
866
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100867static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000868sshpam_store_conv(int n, struct pam_message **msg,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100869 struct pam_response **resp, void *data)
870{
871 struct pam_response *reply;
872 int i;
873 size_t len;
874
875 debug3("PAM: %s called with %d messages", __func__, n);
876 *resp = NULL;
877
878 if (n <= 0 || n > PAM_MAX_NUM_MSG)
879 return (PAM_CONV_ERR);
880
881 if ((reply = malloc(n * sizeof(*reply))) == NULL)
882 return (PAM_CONV_ERR);
883 memset(reply, 0, n * sizeof(*reply));
884
885 for (i = 0; i < n; ++i) {
886 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
887 case PAM_ERROR_MSG:
888 case PAM_TEXT_INFO:
889 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
890 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
891 buffer_append(&loginmsg, "\n", 1 );
892 reply[i].resp_retcode = PAM_SUCCESS;
893 break;
894 default:
895 goto fail;
896 }
897 }
898 *resp = reply;
899 return (PAM_SUCCESS);
900
901 fail:
902 for(i = 0; i < n; i++) {
903 if (reply[i].resp != NULL)
904 xfree(reply[i].resp);
905 }
906 xfree(reply);
907 return (PAM_CONV_ERR);
908}
909
Darren Tucker94befab2004-06-03 14:53:12 +1000910static struct pam_conv store_conv = { sshpam_store_conv, NULL };
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100911
Darren Tucker18df00c2003-11-18 12:42:07 +1100912void
913do_pam_session(void)
914{
Darren Tucker1921ed92004-02-10 13:23:28 +1100915 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +1100916 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100917 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +1100918 if (sshpam_err != PAM_SUCCESS)
919 fatal("PAM: failed to set PAM_CONV: %s",
920 pam_strerror(sshpam_handle, sshpam_err));
921 sshpam_err = pam_open_session(sshpam_handle, 0);
922 if (sshpam_err != PAM_SUCCESS)
923 fatal("PAM: pam_open_session(): %s",
924 pam_strerror(sshpam_handle, sshpam_err));
925 sshpam_session_open = 1;
926}
927
Damien Millera8e06ce2003-11-21 23:48:55 +1100928/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000929 * Set a PAM environment string. We need to do this so that the session
930 * modules can handle things like Kerberos/GSI credentials that appear
931 * during the ssh authentication process.
932 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000933int
Damien Millera8e06ce2003-11-21 23:48:55 +1100934do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000935{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000936 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100937#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000938 char *compound;
939 size_t len;
940
941 len = strlen(name) + strlen(value) + 2;
942 compound = xmalloc(len);
943
944 snprintf(compound, len, "%s=%s", name, value);
945 ret = pam_putenv(sshpam_handle, compound);
946 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000947#endif
Damien Millerf2728092003-09-17 07:24:25 +1000948
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000949 return (ret);
950}
951
Damien Miller4f9f42a2003-05-10 19:28:02 +1000952char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100953fetch_pam_child_environment(void)
954{
955 return sshpam_env;
956}
957
958char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000959fetch_pam_environment(void)
960{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000961 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000962}
963
964void
965free_pam_environment(char **env)
966{
967 char **envp;
968
Damien Millere27c6cc2003-05-16 18:21:01 +1000969 if (env == NULL)
970 return;
971
Damien Miller4f9f42a2003-05-10 19:28:02 +1000972 for (envp = env; *envp; envp++)
973 xfree(*envp);
974 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100975}
976
Darren Tucker450a1582004-05-30 20:43:59 +1000977/*
978 * "Blind" conversation function for password authentication. Assumes that
979 * echo-off prompts are for the password and stores messages for later
980 * display.
981 */
982static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000983sshpam_passwd_conv(int n, struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +1000984 struct pam_response **resp, void *data)
985{
986 struct pam_response *reply;
987 int i;
988 size_t len;
989
990 debug3("PAM: %s called with %d messages", __func__, n);
991
992 *resp = NULL;
993
994 if (n <= 0 || n > PAM_MAX_NUM_MSG)
995 return (PAM_CONV_ERR);
996
997 if ((reply = malloc(n * sizeof(*reply))) == NULL)
998 return (PAM_CONV_ERR);
999 memset(reply, 0, n * sizeof(*reply));
1000
1001 for (i = 0; i < n; ++i) {
1002 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1003 case PAM_PROMPT_ECHO_OFF:
1004 if (sshpam_password == NULL)
1005 goto fail;
1006 reply[i].resp = xstrdup(sshpam_password);
1007 reply[i].resp_retcode = PAM_SUCCESS;
1008 break;
1009 case PAM_ERROR_MSG:
1010 case PAM_TEXT_INFO:
1011 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1012 if (len > 0) {
1013 buffer_append(&loginmsg,
1014 PAM_MSG_MEMBER(msg, i, msg), len);
1015 buffer_append(&loginmsg, "\n", 1);
1016 }
1017 reply[i].resp = xstrdup("");
1018 reply[i].resp_retcode = PAM_SUCCESS;
1019 break;
1020 default:
1021 goto fail;
1022 }
1023 }
1024 *resp = reply;
1025 return (PAM_SUCCESS);
1026
1027 fail:
1028 for(i = 0; i < n; i++) {
1029 if (reply[i].resp != NULL)
1030 xfree(reply[i].resp);
1031 }
1032 xfree(reply);
1033 return (PAM_CONV_ERR);
1034}
1035
1036static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1037
1038/*
1039 * Attempt password authentication via PAM
1040 */
1041int
1042sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1043{
1044 int flags = (options.permit_empty_passwd == 0 ?
1045 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tuckere061b152004-05-30 22:04:56 +10001046 static char badpw[] = "\b\n\r\177INCORRECT";
Darren Tucker450a1582004-05-30 20:43:59 +10001047
1048 if (!options.use_pam || sshpam_handle == NULL)
1049 fatal("PAM: %s called when PAM disabled or failed to "
1050 "initialise.", __func__);
1051
1052 sshpam_password = password;
1053 sshpam_authctxt = authctxt;
1054
Darren Tuckere061b152004-05-30 22:04:56 +10001055 /*
1056 * If the user logging in is invalid, or is root but is not permitted
1057 * by PermitRootLogin, use an invalid password to prevent leaking
1058 * information via timing (eg if the PAM config has a delay on fail).
1059 */
1060 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1061 options.permit_root_login != PERMIT_YES))
1062 sshpam_password = badpw;
1063
Darren Tucker450a1582004-05-30 20:43:59 +10001064 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1065 (const void *)&passwd_conv);
1066 if (sshpam_err != PAM_SUCCESS)
1067 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1068 pam_strerror(sshpam_handle, sshpam_err));
1069
1070 sshpam_err = pam_authenticate(sshpam_handle, flags);
1071 sshpam_password = NULL;
1072 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1073 debug("PAM: password authentication accepted for %.100s",
1074 authctxt->user);
1075 return 1;
1076 } else {
1077 debug("PAM: password authentication failed for %.100s: %s",
1078 authctxt->valid ? authctxt->user : "an illegal user",
1079 pam_strerror(sshpam_handle, sshpam_err));
1080 return 0;
1081 }
1082}
Damien Millere72b7af1999-12-30 15:08:44 +11001083#endif /* USE_PAM */