blob: c99890b253f9c083a5d3bd7b6e7f2bf33bac6390 [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 Miller26314f62004-06-01 11:28:20 +100050RCSID("$Id: auth-pam.c,v 1.105 2004/06/01 01:28:20 djm Exp $");
Damien Millere72b7af1999-12-30 15:08:44 +110051
52#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110053#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100054#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110055#elif defined (HAVE_PAM_PAM_APPL_H)
56#include <pam/pam_appl.h>
57#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100058
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
205pam_password_change_required(int reqd)
206{
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);
235 pam_password_change_required(buffer_get_int(b));
236
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
Damien Miller1f499fd2003-08-25 13:08:49 +1000267sshpam_thread_conv(int n, const struct pam_message **msg,
268 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
Damien Miller4f9f42a2003-05-10 19:28:02 +1000278 ctxt = data;
279 if (n <= 0 || n > PAM_MAX_NUM_MSG)
280 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000281
282 if ((reply = malloc(n * sizeof(*reply))) == NULL)
283 return (PAM_CONV_ERR);
284 memset(reply, 0, n * sizeof(*reply));
285
Damien Miller4f9f42a2003-05-10 19:28:02 +1000286 buffer_init(&buffer);
287 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000288 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
289 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100290 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000291 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100292 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100293 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
294 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100295 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100296 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000297 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
298 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000299 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000300 break;
301 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100302 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000303 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100304 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100305 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
306 goto fail;
307 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
308 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000309 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
310 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000311 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000312 break;
313 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100314 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000315 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100316 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100317 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
318 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000319 break;
320 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100321 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000322 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100323 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100324 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
325 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000326 break;
327 default:
328 goto fail;
329 }
330 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000331 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000332 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000333 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000334 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000335
Damien Miller4f9f42a2003-05-10 19:28:02 +1000336 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000337 for(i = 0; i < n; i++) {
338 if (reply[i].resp != NULL)
339 xfree(reply[i].resp);
340 }
341 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000342 buffer_free(&buffer);
343 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000344}
345
Damien Miller4f9f42a2003-05-10 19:28:02 +1000346/*
347 * Authentication thread.
348 */
349static void *
350sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100351{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000352 struct pam_ctxt *ctxt = ctxtp;
353 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000354 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000355#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100356 extern char **environ;
357 char **env_from_pam;
358 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000359 const char *pam_user;
360
361 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
362 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100363 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000364#endif
365
Damien Millerf4b6f102003-09-02 23:12:06 +1000366 sshpam_conv.conv = sshpam_thread_conv;
367 sshpam_conv.appdata_ptr = ctxt;
368
Darren Tucker17addf02004-03-30 20:57:57 +1000369 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100370 fatal("%s: PAM authctxt not initialized", __func__);
371
Damien Miller4f9f42a2003-05-10 19:28:02 +1000372 buffer_init(&buffer);
373 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
374 (const void *)&sshpam_conv);
375 if (sshpam_err != PAM_SUCCESS)
376 goto auth_fail;
377 sshpam_err = pam_authenticate(sshpam_handle, 0);
378 if (sshpam_err != PAM_SUCCESS)
379 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100380
Darren Tuckerc376c862003-12-18 16:08:59 +1100381 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100382 if (!do_pam_account())
383 goto auth_fail;
Darren Tucker17addf02004-03-30 20:57:57 +1000384 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100385 sshpam_err = pam_chauthtok(sshpam_handle,
386 PAM_CHANGE_EXPIRED_AUTHTOK);
387 if (sshpam_err != PAM_SUCCESS)
388 goto auth_fail;
389 pam_password_change_required(0);
390 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100391 }
Darren Tucker07705c72003-12-18 15:34:31 +1100392
Damien Miller4f9f42a2003-05-10 19:28:02 +1000393 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100394
395#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100396 /* Export variables set by do_pam_account */
397 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000398 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100399
Damien Millerc756e9b2003-11-17 21:41:42 +1100400 /* Export any environment strings set in child */
401 for(i = 0; environ[i] != NULL; i++)
402 ; /* Count */
403 buffer_put_int(&buffer, i);
404 for(i = 0; environ[i] != NULL; i++)
405 buffer_put_cstring(&buffer, environ[i]);
406
407 /* Export any environment strings set by PAM in child */
408 env_from_pam = pam_getenvlist(sshpam_handle);
409 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
410 ; /* Count */
411 buffer_put_int(&buffer, i);
412 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
413 buffer_put_cstring(&buffer, env_from_pam[i]);
414#endif /* USE_POSIX_THREADS */
415
Damien Miller9bdba702003-11-17 21:27:55 +1100416 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000417 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
418 buffer_free(&buffer);
419 pthread_exit(NULL);
420
421 auth_fail:
422 buffer_put_cstring(&buffer,
423 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100424 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000425 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
426 buffer_free(&buffer);
427 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100428
Damien Miller4f9f42a2003-05-10 19:28:02 +1000429 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000430}
431
Darren Tucker8846a072003-10-07 11:30:15 +1000432void
433sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000434{
Darren Tucker8846a072003-10-07 11:30:15 +1000435 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000436
Darren Tuckera8df9242004-01-15 00:15:07 +1100437 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000438 if (ctxt != NULL && ctxt->pam_thread != 0) {
439 pthread_cancel(ctxt->pam_thread);
440 pthread_join(ctxt->pam_thread, NULL);
441 close(ctxt->pam_psock);
442 close(ctxt->pam_csock);
443 memset(ctxt, 0, sizeof(*ctxt));
444 cleanup_ctxt = NULL;
445 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000446}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000447
Damien Miller4f9f42a2003-05-10 19:28:02 +1000448static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000449sshpam_null_conv(int n, const struct pam_message **msg,
450 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000451{
Darren Tuckerba53b832004-02-17 20:46:59 +1100452 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000453 return (PAM_CONV_ERR);
454}
Damien Miller63dc3e92001-02-07 12:58:33 +1100455
Damien Miller4f9f42a2003-05-10 19:28:02 +1000456static struct pam_conv null_conv = { sshpam_null_conv, NULL };
457
Darren Tucker8846a072003-10-07 11:30:15 +1000458void
459sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000460{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000461 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000462 if (sshpam_handle == NULL)
463 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000464 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
465 if (sshpam_cred_established) {
466 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
467 sshpam_cred_established = 0;
468 }
469 if (sshpam_session_open) {
470 pam_close_session(sshpam_handle, PAM_SILENT);
471 sshpam_session_open = 0;
472 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100473 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000474 pam_end(sshpam_handle, sshpam_err);
475 sshpam_handle = NULL;
476}
477
478static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100479sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000480{
Darren Tucker455813b2003-09-13 22:12:11 +1000481 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100482 const char *pam_rhost, *pam_user, *user = authctxt->user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000483
484 if (sshpam_handle != NULL) {
485 /* We already have a PAM context; check if the user matches */
486 sshpam_err = pam_get_item(sshpam_handle,
487 PAM_USER, (const void **)&pam_user);
488 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
489 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000490 pam_end(sshpam_handle, sshpam_err);
491 sshpam_handle = NULL;
492 }
493 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000494 sshpam_err =
495 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000496 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100497
Damien Miller4f9f42a2003-05-10 19:28:02 +1000498 if (sshpam_err != PAM_SUCCESS) {
499 pam_end(sshpam_handle, sshpam_err);
500 sshpam_handle = NULL;
501 return (-1);
502 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000503 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000504 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000505 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
506 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000507 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000508 sshpam_handle = NULL;
509 return (-1);
510 }
511#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100512 /*
513 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
514 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000515 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100516 */
Damien Miller25d93422003-05-18 20:45:47 +1000517 debug("PAM: setting PAM_TTY to \"ssh\"");
518 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
519 if (sshpam_err != PAM_SUCCESS) {
520 pam_end(sshpam_handle, sshpam_err);
521 sshpam_handle = NULL;
522 return (-1);
523 }
524#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000525 return (0);
526}
527
528static void *
529sshpam_init_ctx(Authctxt *authctxt)
530{
531 struct pam_ctxt *ctxt;
532 int socks[2];
533
Darren Tuckera8df9242004-01-15 00:15:07 +1100534 debug3("PAM: %s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000535 /* Refuse to start if we don't have PAM enabled */
536 if (!options.use_pam)
537 return NULL;
538
Damien Miller4f9f42a2003-05-10 19:28:02 +1000539 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100540 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000541 error("PAM: initialization failed");
542 return (NULL);
543 }
544
545 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000546 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000547
548 /* Start the authentication thread */
549 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
550 error("PAM: failed create sockets: %s", strerror(errno));
551 xfree(ctxt);
552 return (NULL);
553 }
554 ctxt->pam_psock = socks[0];
555 ctxt->pam_csock = socks[1];
556 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
557 error("PAM: failed to start authentication thread: %s",
558 strerror(errno));
559 close(socks[0]);
560 close(socks[1]);
561 xfree(ctxt);
562 return (NULL);
563 }
Darren Tucker8846a072003-10-07 11:30:15 +1000564 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000565 return (ctxt);
566}
567
568static int
569sshpam_query(void *ctx, char **name, char **info,
570 u_int *num, char ***prompts, u_int **echo_on)
571{
572 Buffer buffer;
573 struct pam_ctxt *ctxt = ctx;
574 size_t plen;
575 u_char type;
576 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000577 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000578
Darren Tuckera8df9242004-01-15 00:15:07 +1100579 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000580 buffer_init(&buffer);
581 *name = xstrdup("");
582 *info = xstrdup("");
583 *prompts = xmalloc(sizeof(char *));
584 **prompts = NULL;
585 plen = 0;
586 *echo_on = xmalloc(sizeof(u_int));
587 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
588 type = buffer_get_char(&buffer);
589 msg = buffer_get_string(&buffer, NULL);
590 switch (type) {
591 case PAM_PROMPT_ECHO_ON:
592 case PAM_PROMPT_ECHO_OFF:
593 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000594 len = plen + strlen(msg) + 1;
595 **prompts = xrealloc(**prompts, len);
596 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000597 **echo_on = (type == PAM_PROMPT_ECHO_ON);
598 xfree(msg);
599 return (0);
600 case PAM_ERROR_MSG:
601 case PAM_TEXT_INFO:
602 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100603 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000604 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100605 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000606 xfree(msg);
607 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000608 case PAM_SUCCESS:
609 case PAM_AUTH_ERR:
610 if (**prompts != NULL) {
611 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100612 debug("PAM: %s", **prompts);
613 buffer_append(&loginmsg, **prompts,
614 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000615 xfree(**prompts);
616 **prompts = NULL;
617 }
618 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100619 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000620 *num = 0;
621 **echo_on = 0;
622 ctxt->pam_done = 1;
623 xfree(msg);
624 return (0);
625 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000626 error("PAM: %s for %s%.100s from %.100s", msg,
627 sshpam_authctxt->valid ? "" : "illegal user ",
628 sshpam_authctxt->user,
629 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000630 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000631 default:
632 *num = 0;
633 **echo_on = 0;
634 xfree(msg);
635 ctxt->pam_done = -1;
636 return (-1);
637 }
638 }
639 return (-1);
640}
641
642/* XXX - see also comment in auth-chall.c:verify_response */
643static int
644sshpam_respond(void *ctx, u_int num, char **resp)
645{
646 Buffer buffer;
647 struct pam_ctxt *ctxt = ctx;
648
Darren Tuckera8df9242004-01-15 00:15:07 +1100649 debug2("PAM: %s entering, %d responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000650 switch (ctxt->pam_done) {
651 case 1:
652 sshpam_authenticated = 1;
653 return (0);
654 case 0:
655 break;
656 default:
657 return (-1);
658 }
659 if (num != 1) {
660 error("PAM: expected one response, got %u", num);
661 return (-1);
662 }
663 buffer_init(&buffer);
664 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100665 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
666 buffer_free(&buffer);
667 return (-1);
668 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000669 buffer_free(&buffer);
670 return (1);
671}
672
673static void
674sshpam_free_ctx(void *ctxtp)
675{
676 struct pam_ctxt *ctxt = ctxtp;
677
Darren Tuckera8df9242004-01-15 00:15:07 +1100678 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000679 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000680 xfree(ctxt);
681 /*
682 * We don't call sshpam_cleanup() here because we may need the PAM
683 * handle at a later stage, e.g. when setting up a session. It's
684 * still on the cleanup list, so pam_end() *will* be called before
685 * the server process terminates.
686 */
687}
688
689KbdintDevice sshpam_device = {
690 "pam",
691 sshpam_init_ctx,
692 sshpam_query,
693 sshpam_respond,
694 sshpam_free_ctx
695};
696
697KbdintDevice mm_sshpam_device = {
698 "pam",
699 mm_sshpam_init_ctx,
700 mm_sshpam_query,
701 mm_sshpam_respond,
702 mm_sshpam_free_ctx
703};
704
705/*
706 * This replaces auth-pam.c
707 */
708void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100709start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000710{
Damien Miller9d507da2003-05-14 15:31:12 +1000711 if (!options.use_pam)
712 fatal("PAM: initialisation requested when UsePAM=no");
713
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100714 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000715 fatal("PAM: initialisation failed");
716}
717
718void
719finish_pam(void)
720{
Darren Tucker8846a072003-10-07 11:30:15 +1000721 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000722}
723
Damien Miller1f499fd2003-08-25 13:08:49 +1000724u_int
725do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000726{
Darren Tucker07705c72003-12-18 15:34:31 +1100727 if (sshpam_account_status != -1)
728 return (sshpam_account_status);
729
Damien Miller1f499fd2003-08-25 13:08:49 +1000730 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tuckera8df9242004-01-15 00:15:07 +1100731 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100732
733 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
734 sshpam_account_status = 0;
735 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000736 }
737
Darren Tucker07705c72003-12-18 15:34:31 +1100738 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
739 pam_password_change_required(1);
740
741 sshpam_account_status = 1;
742 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000743}
744
745void
Damien Miller341c6e62003-09-02 23:18:52 +1000746do_pam_set_tty(const char *tty)
747{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000748 if (tty != NULL) {
749 debug("PAM: setting PAM_TTY to \"%s\"", tty);
750 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
751 if (sshpam_err != PAM_SUCCESS)
752 fatal("PAM: failed to set PAM_TTY: %s",
753 pam_strerror(sshpam_handle, sshpam_err));
754 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000755}
756
757void
758do_pam_setcred(int init)
759{
760 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
761 (const void *)&null_conv);
762 if (sshpam_err != PAM_SUCCESS)
763 fatal("PAM: failed to set PAM_CONV: %s",
764 pam_strerror(sshpam_handle, sshpam_err));
765 if (init) {
766 debug("PAM: establishing credentials");
767 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
768 } else {
769 debug("PAM: reinitializing credentials");
770 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
771 }
772 if (sshpam_err == PAM_SUCCESS) {
773 sshpam_cred_established = 1;
774 return;
775 }
776 if (sshpam_authenticated)
777 fatal("PAM: pam_setcred(): %s",
778 pam_strerror(sshpam_handle, sshpam_err));
779 else
780 debug("PAM: pam_setcred(): %s",
781 pam_strerror(sshpam_handle, sshpam_err));
782}
783
Damien Miller4f9f42a2003-05-10 19:28:02 +1000784static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100785pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000786 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000787{
788 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000789 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000790 int i;
791
Darren Tuckerba53b832004-02-17 20:46:59 +1100792 debug3("PAM: %s called with %d messages", __func__, n);
793
Damien Miller5c3a5582003-09-23 22:12:38 +1000794 *resp = NULL;
795
Darren Tucker18df00c2003-11-18 12:42:07 +1100796 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000797 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000798
799 if ((reply = malloc(n * sizeof(*reply))) == NULL)
800 return (PAM_CONV_ERR);
801 memset(reply, 0, n * sizeof(*reply));
802
Damien Miller4f9f42a2003-05-10 19:28:02 +1000803 for (i = 0; i < n; ++i) {
804 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
805 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000806 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100807 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000808 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000809 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000810 break;
811 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100812 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000813 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000814 reply[i].resp = xstrdup(input);
815 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000816 break;
817 case PAM_ERROR_MSG:
818 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100819 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000820 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000821 break;
822 default:
823 goto fail;
824 }
825 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000826 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000827 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000828
Damien Miller4f9f42a2003-05-10 19:28:02 +1000829 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000830 for(i = 0; i < n; i++) {
831 if (reply[i].resp != NULL)
832 xfree(reply[i].resp);
833 }
834 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000835 return (PAM_CONV_ERR);
836}
837
Darren Tucker18df00c2003-11-18 12:42:07 +1100838static struct pam_conv tty_conv = { pam_tty_conv, NULL };
839
Damien Miller4f9f42a2003-05-10 19:28:02 +1000840/*
841 * XXX this should be done in the authentication phase, but ssh1 doesn't
842 * support that
843 */
844void
845do_pam_chauthtok(void)
846{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000847 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000848 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000849 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100850 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000851 if (sshpam_err != PAM_SUCCESS)
852 fatal("PAM: failed to set PAM_CONV: %s",
853 pam_strerror(sshpam_handle, sshpam_err));
854 debug("PAM: changing password");
855 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
856 if (sshpam_err != PAM_SUCCESS)
857 fatal("PAM: pam_chauthtok(): %s",
858 pam_strerror(sshpam_handle, sshpam_err));
859}
860
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100861static int
862pam_store_conv(int n, const struct pam_message **msg,
863 struct pam_response **resp, void *data)
864{
865 struct pam_response *reply;
866 int i;
867 size_t len;
868
869 debug3("PAM: %s called with %d messages", __func__, n);
870 *resp = NULL;
871
872 if (n <= 0 || n > PAM_MAX_NUM_MSG)
873 return (PAM_CONV_ERR);
874
875 if ((reply = malloc(n * sizeof(*reply))) == NULL)
876 return (PAM_CONV_ERR);
877 memset(reply, 0, n * sizeof(*reply));
878
879 for (i = 0; i < n; ++i) {
880 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
881 case PAM_ERROR_MSG:
882 case PAM_TEXT_INFO:
883 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
884 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
885 buffer_append(&loginmsg, "\n", 1 );
886 reply[i].resp_retcode = PAM_SUCCESS;
887 break;
888 default:
889 goto fail;
890 }
891 }
892 *resp = reply;
893 return (PAM_SUCCESS);
894
895 fail:
896 for(i = 0; i < n; i++) {
897 if (reply[i].resp != NULL)
898 xfree(reply[i].resp);
899 }
900 xfree(reply);
901 return (PAM_CONV_ERR);
902}
903
904static struct pam_conv store_conv = { pam_store_conv, NULL };
905
Darren Tucker18df00c2003-11-18 12:42:07 +1100906void
907do_pam_session(void)
908{
Darren Tucker1921ed92004-02-10 13:23:28 +1100909 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +1100910 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100911 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +1100912 if (sshpam_err != PAM_SUCCESS)
913 fatal("PAM: failed to set PAM_CONV: %s",
914 pam_strerror(sshpam_handle, sshpam_err));
915 sshpam_err = pam_open_session(sshpam_handle, 0);
916 if (sshpam_err != PAM_SUCCESS)
917 fatal("PAM: pam_open_session(): %s",
918 pam_strerror(sshpam_handle, sshpam_err));
919 sshpam_session_open = 1;
920}
921
Damien Millera8e06ce2003-11-21 23:48:55 +1100922/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000923 * Set a PAM environment string. We need to do this so that the session
924 * modules can handle things like Kerberos/GSI credentials that appear
925 * during the ssh authentication process.
926 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000927int
Damien Millera8e06ce2003-11-21 23:48:55 +1100928do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000929{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000930 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100931#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000932 char *compound;
933 size_t len;
934
935 len = strlen(name) + strlen(value) + 2;
936 compound = xmalloc(len);
937
938 snprintf(compound, len, "%s=%s", name, value);
939 ret = pam_putenv(sshpam_handle, compound);
940 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000941#endif
Damien Millerf2728092003-09-17 07:24:25 +1000942
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000943 return (ret);
944}
945
Damien Miller4f9f42a2003-05-10 19:28:02 +1000946char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100947fetch_pam_child_environment(void)
948{
949 return sshpam_env;
950}
951
952char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000953fetch_pam_environment(void)
954{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000955 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000956}
957
958void
959free_pam_environment(char **env)
960{
961 char **envp;
962
Damien Millere27c6cc2003-05-16 18:21:01 +1000963 if (env == NULL)
964 return;
965
Damien Miller4f9f42a2003-05-10 19:28:02 +1000966 for (envp = env; *envp; envp++)
967 xfree(*envp);
968 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100969}
970
Darren Tucker450a1582004-05-30 20:43:59 +1000971/*
972 * "Blind" conversation function for password authentication. Assumes that
973 * echo-off prompts are for the password and stores messages for later
974 * display.
975 */
976static int
977sshpam_passwd_conv(int n, const struct pam_message **msg,
978 struct pam_response **resp, void *data)
979{
980 struct pam_response *reply;
981 int i;
982 size_t len;
983
984 debug3("PAM: %s called with %d messages", __func__, n);
985
986 *resp = NULL;
987
988 if (n <= 0 || n > PAM_MAX_NUM_MSG)
989 return (PAM_CONV_ERR);
990
991 if ((reply = malloc(n * sizeof(*reply))) == NULL)
992 return (PAM_CONV_ERR);
993 memset(reply, 0, n * sizeof(*reply));
994
995 for (i = 0; i < n; ++i) {
996 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
997 case PAM_PROMPT_ECHO_OFF:
998 if (sshpam_password == NULL)
999 goto fail;
1000 reply[i].resp = xstrdup(sshpam_password);
1001 reply[i].resp_retcode = PAM_SUCCESS;
1002 break;
1003 case PAM_ERROR_MSG:
1004 case PAM_TEXT_INFO:
1005 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1006 if (len > 0) {
1007 buffer_append(&loginmsg,
1008 PAM_MSG_MEMBER(msg, i, msg), len);
1009 buffer_append(&loginmsg, "\n", 1);
1010 }
1011 reply[i].resp = xstrdup("");
1012 reply[i].resp_retcode = PAM_SUCCESS;
1013 break;
1014 default:
1015 goto fail;
1016 }
1017 }
1018 *resp = reply;
1019 return (PAM_SUCCESS);
1020
1021 fail:
1022 for(i = 0; i < n; i++) {
1023 if (reply[i].resp != NULL)
1024 xfree(reply[i].resp);
1025 }
1026 xfree(reply);
1027 return (PAM_CONV_ERR);
1028}
1029
1030static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1031
1032/*
1033 * Attempt password authentication via PAM
1034 */
1035int
1036sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1037{
1038 int flags = (options.permit_empty_passwd == 0 ?
1039 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tuckere061b152004-05-30 22:04:56 +10001040 static char badpw[] = "\b\n\r\177INCORRECT";
Darren Tucker450a1582004-05-30 20:43:59 +10001041
1042 if (!options.use_pam || sshpam_handle == NULL)
1043 fatal("PAM: %s called when PAM disabled or failed to "
1044 "initialise.", __func__);
1045
1046 sshpam_password = password;
1047 sshpam_authctxt = authctxt;
1048
Darren Tuckere061b152004-05-30 22:04:56 +10001049 /*
1050 * If the user logging in is invalid, or is root but is not permitted
1051 * by PermitRootLogin, use an invalid password to prevent leaking
1052 * information via timing (eg if the PAM config has a delay on fail).
1053 */
1054 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1055 options.permit_root_login != PERMIT_YES))
1056 sshpam_password = badpw;
1057
Darren Tucker450a1582004-05-30 20:43:59 +10001058 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1059 (const void *)&passwd_conv);
1060 if (sshpam_err != PAM_SUCCESS)
1061 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1062 pam_strerror(sshpam_handle, sshpam_err));
1063
1064 sshpam_err = pam_authenticate(sshpam_handle, flags);
1065 sshpam_password = NULL;
1066 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1067 debug("PAM: password authentication accepted for %.100s",
1068 authctxt->user);
1069 return 1;
1070 } else {
1071 debug("PAM: password authentication failed for %.100s: %s",
1072 authctxt->valid ? authctxt->user : "an illegal user",
1073 pam_strerror(sshpam_handle, sshpam_err));
1074 return 0;
1075 }
1076}
Damien Millere72b7af1999-12-30 15:08:44 +11001077#endif /* USE_PAM */