blob: 6f2264c5d7ced36447b549a5f7cb227ad9796011 [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 */
31
Damien Miller25d93422003-05-18 20:45:47 +100032/* 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 +110033#include "includes.h"
Darren Tuckerdbf7a742004-03-08 23:04:06 +110034RCSID("$Id: auth-pam.c,v 1.98 2004/03/08 12:04:06 dtucker Exp $");
Damien Millere72b7af1999-12-30 15:08:44 +110035
36#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110037#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100038#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110039#elif defined (HAVE_PAM_PAM_APPL_H)
40#include <pam/pam_appl.h>
41#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100042
Kevin Stevese683e762002-04-04 19:02:28 +000043#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110044#include "auth-pam.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100045#include "buffer.h"
46#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100048#include "log.h"
49#include "monitor_wrap.h"
50#include "msg.h"
51#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "readpass.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100053#include "servconf.h"
54#include "ssh2.h"
55#include "xmalloc.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100056#include "auth-options.h"
Damien Millere72b7af1999-12-30 15:08:44 +110057
Damien Miller4e448a32003-05-14 15:11:48 +100058extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +110059extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +110060extern int compat20;
Damien Miller4e448a32003-05-14 15:11:48 +100061
Damien Miller4f9f42a2003-05-10 19:28:02 +100062#ifdef USE_POSIX_THREADS
63#include <pthread.h>
64/*
Damien Millera8e06ce2003-11-21 23:48:55 +110065 * Avoid namespace clash when *not* using pthreads for systems *with*
66 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +100067 * (e.g. Linux)
68 */
Damien Millera8e06ce2003-11-21 23:48:55 +110069typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +100070#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +110071typedef pid_t sp_pthread_t;
72#endif
73
74struct pam_ctxt {
75 sp_pthread_t pam_thread;
76 int pam_psock;
77 int pam_csock;
78 int pam_done;
79};
80
81static void sshpam_free_ctx(void *);
82static struct pam_ctxt *cleanup_ctxt;
83
84#ifndef USE_POSIX_THREADS
Damien Miller4f9f42a2003-05-10 19:28:02 +100085/*
86 * Simulate threads with processes.
87 */
Kevin Steves63007d42002-07-21 17:57:01 +000088
Darren Tucker749bc952004-01-14 22:14:04 +110089static int sshpam_thread_status = -1;
90static mysig_t sshpam_oldsig;
91
92static void
93sshpam_sigchld_handler(int sig)
94{
Darren Tucker7ae09622004-01-14 23:07:56 +110095 if (cleanup_ctxt == NULL)
96 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tucker749bc952004-01-14 22:14:04 +110097 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0) == -1)
98 return; /* couldn't wait for process */
99 if (WIFSIGNALED(sshpam_thread_status) &&
100 WTERMSIG(sshpam_thread_status) == SIGTERM)
101 return; /* terminated by pthread_cancel */
102 if (!WIFEXITED(sshpam_thread_status))
103 fatal("PAM: authentication thread exited unexpectedly");
104 if (WEXITSTATUS(sshpam_thread_status) != 0)
105 fatal("PAM: authentication thread exited uncleanly");
106}
107
Damien Miller4f9f42a2003-05-10 19:28:02 +1000108static void
109pthread_exit(void *value __unused)
110{
111 _exit(0);
112}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000113
Damien Miller4f9f42a2003-05-10 19:28:02 +1000114static int
115pthread_create(sp_pthread_t *thread, const void *attr __unused,
116 void *(*thread_start)(void *), void *arg)
117{
118 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +1100119
Darren Tuckerb9b60212004-03-04 20:03:54 +1100120 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000121 switch ((pid = fork())) {
122 case -1:
123 error("fork(): %s", strerror(errno));
124 return (-1);
125 case 0:
126 thread_start(arg);
127 _exit(1);
128 default:
129 *thread = pid;
Darren Tucker749bc952004-01-14 22:14:04 +1100130 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000131 return (0);
132 }
133}
Damien Millere72b7af1999-12-30 15:08:44 +1100134
Damien Miller4f9f42a2003-05-10 19:28:02 +1000135static int
136pthread_cancel(sp_pthread_t thread)
137{
Darren Tucker7ae09622004-01-14 23:07:56 +1100138 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000139 return (kill(thread, SIGTERM));
140}
141
142static int
143pthread_join(sp_pthread_t thread, void **value __unused)
144{
145 int status;
146
Darren Tucker749bc952004-01-14 22:14:04 +1100147 if (sshpam_thread_status != -1)
148 return (sshpam_thread_status);
149 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000150 waitpid(thread, &status, 0);
151 return (status);
152}
153#endif
154
155
Damien Miller5c3a5582003-09-23 22:12:38 +1000156static pam_handle_t *sshpam_handle = NULL;
157static int sshpam_err = 0;
158static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000159static int sshpam_session_open = 0;
160static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100161static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100162static char **sshpam_env = NULL;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100163static Authctxt *the_authctxt = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000164
Damien Millerc756e9b2003-11-17 21:41:42 +1100165/* Some PAM implementations don't implement this */
166#ifndef HAVE_PAM_GETENVLIST
167static char **
168pam_getenvlist(pam_handle_t *pamh)
169{
170 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100171 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100172 * for platforms without pam_getenvlist by searching for known
173 * env vars (e.g. KRB5CCNAME) from the PAM environment.
174 */
175 return NULL;
176}
177#endif
178
Darren Tucker07705c72003-12-18 15:34:31 +1100179void
180pam_password_change_required(int reqd)
181{
Darren Tuckera8df9242004-01-15 00:15:07 +1100182 debug3("%s %d", __func__, reqd);
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100183 if (the_authctxt == NULL)
184 fatal("%s: PAM authctxt not initialized", __func__);
185 the_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100186 if (reqd) {
187 no_port_forwarding_flag |= 2;
188 no_agent_forwarding_flag |= 2;
189 no_x11_forwarding_flag |= 2;
190 } else {
191 no_port_forwarding_flag &= ~2;
192 no_agent_forwarding_flag &= ~2;
193 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100194 }
195}
Darren Tucker1921ed92004-02-10 13:23:28 +1100196
Damien Millerc756e9b2003-11-17 21:41:42 +1100197/* Import regular and PAM environment from subprocess */
198static void
199import_environments(Buffer *b)
200{
201 char *env;
202 u_int i, num_env;
203 int err;
204
Darren Tuckera8df9242004-01-15 00:15:07 +1100205 debug3("PAM: %s entering", __func__);
206
Darren Tucker4b385d42004-03-04 19:54:10 +1100207#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100208 /* Import variables set by do_pam_account */
209 sshpam_account_status = buffer_get_int(b);
210 pam_password_change_required(buffer_get_int(b));
211
Damien Millerc756e9b2003-11-17 21:41:42 +1100212 /* Import environment from subprocess */
213 num_env = buffer_get_int(b);
214 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
215 debug3("PAM: num env strings %d", num_env);
216 for(i = 0; i < num_env; i++)
217 sshpam_env[i] = buffer_get_string(b, NULL);
218
219 sshpam_env[num_env] = NULL;
220
221 /* Import PAM environment from subprocess */
222 num_env = buffer_get_int(b);
223 debug("PAM: num PAM env strings %d", num_env);
224 for(i = 0; i < num_env; i++) {
225 env = buffer_get_string(b, NULL);
226
Darren Tucker8a1624c2003-11-18 12:45:35 +1100227#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100228 /* Errors are not fatal here */
229 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
230 error("PAM: pam_putenv: %s",
231 pam_strerror(sshpam_handle, sshpam_err));
232 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100233#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100234 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100235#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100236}
237
Damien Miller9d5705a2000-09-16 16:09:27 +1100238/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000239 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100240 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000241static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000242sshpam_thread_conv(int n, const struct pam_message **msg,
243 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100244{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000245 Buffer buffer;
246 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000247 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000248 int i;
249
Darren Tuckerba53b832004-02-17 20:46:59 +1100250 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000251 *resp = NULL;
252
Damien Miller4f9f42a2003-05-10 19:28:02 +1000253 ctxt = data;
254 if (n <= 0 || n > PAM_MAX_NUM_MSG)
255 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000256
257 if ((reply = malloc(n * sizeof(*reply))) == NULL)
258 return (PAM_CONV_ERR);
259 memset(reply, 0, n * sizeof(*reply));
260
Damien Miller4f9f42a2003-05-10 19:28:02 +1000261 buffer_init(&buffer);
262 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000263 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
264 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100265 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000266 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100267 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100268 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
269 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100270 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100271 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000272 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
273 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000274 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000275 break;
276 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100277 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000278 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100279 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100280 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
281 goto fail;
282 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
283 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000284 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
285 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000286 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000287 break;
288 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100289 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000290 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100291 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100292 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
293 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000294 break;
295 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100296 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000297 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100298 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100299 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
300 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000301 break;
302 default:
303 goto fail;
304 }
305 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000306 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000307 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000308 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000309 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000310
Damien Miller4f9f42a2003-05-10 19:28:02 +1000311 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000312 for(i = 0; i < n; i++) {
313 if (reply[i].resp != NULL)
314 xfree(reply[i].resp);
315 }
316 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000317 buffer_free(&buffer);
318 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000319}
320
Damien Miller4f9f42a2003-05-10 19:28:02 +1000321/*
322 * Authentication thread.
323 */
324static void *
325sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100326{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000327 struct pam_ctxt *ctxt = ctxtp;
328 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000329 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000330#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100331 extern char **environ;
332 char **env_from_pam;
333 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000334 const char *pam_user;
335
336 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
337 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100338 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000339#endif
340
Damien Millerf4b6f102003-09-02 23:12:06 +1000341 sshpam_conv.conv = sshpam_thread_conv;
342 sshpam_conv.appdata_ptr = ctxt;
343
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100344 if (the_authctxt == NULL)
345 fatal("%s: PAM authctxt not initialized", __func__);
346
Damien Miller4f9f42a2003-05-10 19:28:02 +1000347 buffer_init(&buffer);
348 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
349 (const void *)&sshpam_conv);
350 if (sshpam_err != PAM_SUCCESS)
351 goto auth_fail;
352 sshpam_err = pam_authenticate(sshpam_handle, 0);
353 if (sshpam_err != PAM_SUCCESS)
354 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100355
Darren Tuckerc376c862003-12-18 16:08:59 +1100356 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100357 if (!do_pam_account())
358 goto auth_fail;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100359 if (the_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100360 sshpam_err = pam_chauthtok(sshpam_handle,
361 PAM_CHANGE_EXPIRED_AUTHTOK);
362 if (sshpam_err != PAM_SUCCESS)
363 goto auth_fail;
364 pam_password_change_required(0);
365 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100366 }
Darren Tucker07705c72003-12-18 15:34:31 +1100367
Damien Miller4f9f42a2003-05-10 19:28:02 +1000368 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100369
370#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100371 /* Export variables set by do_pam_account */
372 buffer_put_int(&buffer, sshpam_account_status);
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100373 buffer_put_int(&buffer, the_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100374
Damien Millerc756e9b2003-11-17 21:41:42 +1100375 /* Export any environment strings set in child */
376 for(i = 0; environ[i] != NULL; i++)
377 ; /* Count */
378 buffer_put_int(&buffer, i);
379 for(i = 0; environ[i] != NULL; i++)
380 buffer_put_cstring(&buffer, environ[i]);
381
382 /* Export any environment strings set by PAM in child */
383 env_from_pam = pam_getenvlist(sshpam_handle);
384 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
385 ; /* Count */
386 buffer_put_int(&buffer, i);
387 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
388 buffer_put_cstring(&buffer, env_from_pam[i]);
389#endif /* USE_POSIX_THREADS */
390
Damien Miller9bdba702003-11-17 21:27:55 +1100391 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000392 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
393 buffer_free(&buffer);
394 pthread_exit(NULL);
395
396 auth_fail:
397 buffer_put_cstring(&buffer,
398 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100399 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000400 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
401 buffer_free(&buffer);
402 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100403
Damien Miller4f9f42a2003-05-10 19:28:02 +1000404 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000405}
406
Darren Tucker8846a072003-10-07 11:30:15 +1000407void
408sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000409{
Darren Tucker8846a072003-10-07 11:30:15 +1000410 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000411
Darren Tuckera8df9242004-01-15 00:15:07 +1100412 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000413 if (ctxt != NULL && ctxt->pam_thread != 0) {
414 pthread_cancel(ctxt->pam_thread);
415 pthread_join(ctxt->pam_thread, NULL);
416 close(ctxt->pam_psock);
417 close(ctxt->pam_csock);
418 memset(ctxt, 0, sizeof(*ctxt));
419 cleanup_ctxt = NULL;
420 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000421}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000422
Damien Miller4f9f42a2003-05-10 19:28:02 +1000423static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000424sshpam_null_conv(int n, const struct pam_message **msg,
425 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000426{
Darren Tuckerba53b832004-02-17 20:46:59 +1100427 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000428 return (PAM_CONV_ERR);
429}
Damien Miller63dc3e92001-02-07 12:58:33 +1100430
Damien Miller4f9f42a2003-05-10 19:28:02 +1000431static struct pam_conv null_conv = { sshpam_null_conv, NULL };
432
Darren Tucker8846a072003-10-07 11:30:15 +1000433void
434sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000435{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000436 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000437 if (sshpam_handle == NULL)
438 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000439 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
440 if (sshpam_cred_established) {
441 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
442 sshpam_cred_established = 0;
443 }
444 if (sshpam_session_open) {
445 pam_close_session(sshpam_handle, PAM_SILENT);
446 sshpam_session_open = 0;
447 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100448 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000449 pam_end(sshpam_handle, sshpam_err);
450 sshpam_handle = NULL;
451}
452
453static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100454sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000455{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000456 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000457 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100458 const char *pam_rhost, *pam_user, *user = authctxt->user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000459
460 if (sshpam_handle != NULL) {
461 /* We already have a PAM context; check if the user matches */
462 sshpam_err = pam_get_item(sshpam_handle,
463 PAM_USER, (const void **)&pam_user);
464 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
465 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000466 pam_end(sshpam_handle, sshpam_err);
467 sshpam_handle = NULL;
468 }
469 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000470 sshpam_err =
471 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100472 the_authctxt = authctxt;
473
Damien Miller4f9f42a2003-05-10 19:28:02 +1000474 if (sshpam_err != PAM_SUCCESS) {
475 pam_end(sshpam_handle, sshpam_err);
476 sshpam_handle = NULL;
477 return (-1);
478 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000479 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000480 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000481 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
482 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000483 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000484 sshpam_handle = NULL;
485 return (-1);
486 }
487#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100488 /*
489 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
490 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000491 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100492 */
Damien Miller25d93422003-05-18 20:45:47 +1000493 debug("PAM: setting PAM_TTY to \"ssh\"");
494 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
495 if (sshpam_err != PAM_SUCCESS) {
496 pam_end(sshpam_handle, sshpam_err);
497 sshpam_handle = NULL;
498 return (-1);
499 }
500#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000501 return (0);
502}
503
504static void *
505sshpam_init_ctx(Authctxt *authctxt)
506{
507 struct pam_ctxt *ctxt;
508 int socks[2];
509
Darren Tuckera8df9242004-01-15 00:15:07 +1100510 debug3("PAM: %s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000511 /* Refuse to start if we don't have PAM enabled */
512 if (!options.use_pam)
513 return NULL;
514
Damien Miller4f9f42a2003-05-10 19:28:02 +1000515 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100516 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000517 error("PAM: initialization failed");
518 return (NULL);
519 }
520
521 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000522 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000523
524 /* Start the authentication thread */
525 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
526 error("PAM: failed create sockets: %s", strerror(errno));
527 xfree(ctxt);
528 return (NULL);
529 }
530 ctxt->pam_psock = socks[0];
531 ctxt->pam_csock = socks[1];
532 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
533 error("PAM: failed to start authentication thread: %s",
534 strerror(errno));
535 close(socks[0]);
536 close(socks[1]);
537 xfree(ctxt);
538 return (NULL);
539 }
Darren Tucker8846a072003-10-07 11:30:15 +1000540 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000541 return (ctxt);
542}
543
544static int
545sshpam_query(void *ctx, char **name, char **info,
546 u_int *num, char ***prompts, u_int **echo_on)
547{
548 Buffer buffer;
549 struct pam_ctxt *ctxt = ctx;
550 size_t plen;
551 u_char type;
552 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000553 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000554
Darren Tuckera8df9242004-01-15 00:15:07 +1100555 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000556 buffer_init(&buffer);
557 *name = xstrdup("");
558 *info = xstrdup("");
559 *prompts = xmalloc(sizeof(char *));
560 **prompts = NULL;
561 plen = 0;
562 *echo_on = xmalloc(sizeof(u_int));
563 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
564 type = buffer_get_char(&buffer);
565 msg = buffer_get_string(&buffer, NULL);
566 switch (type) {
567 case PAM_PROMPT_ECHO_ON:
568 case PAM_PROMPT_ECHO_OFF:
569 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000570 len = plen + strlen(msg) + 1;
571 **prompts = xrealloc(**prompts, len);
572 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000573 **echo_on = (type == PAM_PROMPT_ECHO_ON);
574 xfree(msg);
575 return (0);
576 case PAM_ERROR_MSG:
577 case PAM_TEXT_INFO:
578 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100579 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000580 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100581 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000582 xfree(msg);
583 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000584 case PAM_SUCCESS:
585 case PAM_AUTH_ERR:
586 if (**prompts != NULL) {
587 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100588 debug("PAM: %s", **prompts);
589 buffer_append(&loginmsg, **prompts,
590 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000591 xfree(**prompts);
592 **prompts = NULL;
593 }
594 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100595 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000596 *num = 0;
597 **echo_on = 0;
598 ctxt->pam_done = 1;
599 xfree(msg);
600 return (0);
601 }
602 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000603 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000604 default:
605 *num = 0;
606 **echo_on = 0;
607 xfree(msg);
608 ctxt->pam_done = -1;
609 return (-1);
610 }
611 }
612 return (-1);
613}
614
615/* XXX - see also comment in auth-chall.c:verify_response */
616static int
617sshpam_respond(void *ctx, u_int num, char **resp)
618{
619 Buffer buffer;
620 struct pam_ctxt *ctxt = ctx;
621
Darren Tuckera8df9242004-01-15 00:15:07 +1100622 debug2("PAM: %s entering, %d responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000623 switch (ctxt->pam_done) {
624 case 1:
625 sshpam_authenticated = 1;
626 return (0);
627 case 0:
628 break;
629 default:
630 return (-1);
631 }
632 if (num != 1) {
633 error("PAM: expected one response, got %u", num);
634 return (-1);
635 }
636 buffer_init(&buffer);
637 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100638 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
639 buffer_free(&buffer);
640 return (-1);
641 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000642 buffer_free(&buffer);
643 return (1);
644}
645
646static void
647sshpam_free_ctx(void *ctxtp)
648{
649 struct pam_ctxt *ctxt = ctxtp;
650
Darren Tuckera8df9242004-01-15 00:15:07 +1100651 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000652 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000653 xfree(ctxt);
654 /*
655 * We don't call sshpam_cleanup() here because we may need the PAM
656 * handle at a later stage, e.g. when setting up a session. It's
657 * still on the cleanup list, so pam_end() *will* be called before
658 * the server process terminates.
659 */
660}
661
662KbdintDevice sshpam_device = {
663 "pam",
664 sshpam_init_ctx,
665 sshpam_query,
666 sshpam_respond,
667 sshpam_free_ctx
668};
669
670KbdintDevice mm_sshpam_device = {
671 "pam",
672 mm_sshpam_init_ctx,
673 mm_sshpam_query,
674 mm_sshpam_respond,
675 mm_sshpam_free_ctx
676};
677
678/*
679 * This replaces auth-pam.c
680 */
681void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100682start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000683{
Damien Miller9d507da2003-05-14 15:31:12 +1000684 if (!options.use_pam)
685 fatal("PAM: initialisation requested when UsePAM=no");
686
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100687 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000688 fatal("PAM: initialisation failed");
689}
690
691void
692finish_pam(void)
693{
Darren Tucker8846a072003-10-07 11:30:15 +1000694 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000695}
696
Damien Miller1f499fd2003-08-25 13:08:49 +1000697u_int
698do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000699{
Darren Tucker07705c72003-12-18 15:34:31 +1100700 if (sshpam_account_status != -1)
701 return (sshpam_account_status);
702
Damien Miller1f499fd2003-08-25 13:08:49 +1000703 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tuckera8df9242004-01-15 00:15:07 +1100704 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100705
706 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
707 sshpam_account_status = 0;
708 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000709 }
710
Darren Tucker07705c72003-12-18 15:34:31 +1100711 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
712 pam_password_change_required(1);
713
714 sshpam_account_status = 1;
715 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000716}
717
718void
Damien Miller341c6e62003-09-02 23:18:52 +1000719do_pam_set_tty(const char *tty)
720{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000721 if (tty != NULL) {
722 debug("PAM: setting PAM_TTY to \"%s\"", tty);
723 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
724 if (sshpam_err != PAM_SUCCESS)
725 fatal("PAM: failed to set PAM_TTY: %s",
726 pam_strerror(sshpam_handle, sshpam_err));
727 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000728}
729
730void
731do_pam_setcred(int init)
732{
733 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
734 (const void *)&null_conv);
735 if (sshpam_err != PAM_SUCCESS)
736 fatal("PAM: failed to set PAM_CONV: %s",
737 pam_strerror(sshpam_handle, sshpam_err));
738 if (init) {
739 debug("PAM: establishing credentials");
740 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
741 } else {
742 debug("PAM: reinitializing credentials");
743 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
744 }
745 if (sshpam_err == PAM_SUCCESS) {
746 sshpam_cred_established = 1;
747 return;
748 }
749 if (sshpam_authenticated)
750 fatal("PAM: pam_setcred(): %s",
751 pam_strerror(sshpam_handle, sshpam_err));
752 else
753 debug("PAM: pam_setcred(): %s",
754 pam_strerror(sshpam_handle, sshpam_err));
755}
756
Damien Miller4f9f42a2003-05-10 19:28:02 +1000757static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100758pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000759 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000760{
761 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000762 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000763 int i;
764
Darren Tuckerba53b832004-02-17 20:46:59 +1100765 debug3("PAM: %s called with %d messages", __func__, n);
766
Damien Miller5c3a5582003-09-23 22:12:38 +1000767 *resp = NULL;
768
Darren Tucker18df00c2003-11-18 12:42:07 +1100769 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000770 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000771
772 if ((reply = malloc(n * sizeof(*reply))) == NULL)
773 return (PAM_CONV_ERR);
774 memset(reply, 0, n * sizeof(*reply));
775
Damien Miller4f9f42a2003-05-10 19:28:02 +1000776 for (i = 0; i < n; ++i) {
777 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
778 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000779 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100780 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000781 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000782 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000783 break;
784 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100785 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000786 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000787 reply[i].resp = xstrdup(input);
788 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000789 break;
790 case PAM_ERROR_MSG:
791 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100792 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000793 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000794 break;
795 default:
796 goto fail;
797 }
798 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000799 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000800 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000801
Damien Miller4f9f42a2003-05-10 19:28:02 +1000802 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000803 for(i = 0; i < n; i++) {
804 if (reply[i].resp != NULL)
805 xfree(reply[i].resp);
806 }
807 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000808 return (PAM_CONV_ERR);
809}
810
Darren Tucker18df00c2003-11-18 12:42:07 +1100811static struct pam_conv tty_conv = { pam_tty_conv, NULL };
812
Damien Miller4f9f42a2003-05-10 19:28:02 +1000813/*
814 * XXX this should be done in the authentication phase, but ssh1 doesn't
815 * support that
816 */
817void
818do_pam_chauthtok(void)
819{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000820 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000821 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000822 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100823 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000824 if (sshpam_err != PAM_SUCCESS)
825 fatal("PAM: failed to set PAM_CONV: %s",
826 pam_strerror(sshpam_handle, sshpam_err));
827 debug("PAM: changing password");
828 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
829 if (sshpam_err != PAM_SUCCESS)
830 fatal("PAM: pam_chauthtok(): %s",
831 pam_strerror(sshpam_handle, sshpam_err));
832}
833
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100834static int
835pam_store_conv(int n, const struct pam_message **msg,
836 struct pam_response **resp, void *data)
837{
838 struct pam_response *reply;
839 int i;
840 size_t len;
841
842 debug3("PAM: %s called with %d messages", __func__, n);
843 *resp = NULL;
844
845 if (n <= 0 || n > PAM_MAX_NUM_MSG)
846 return (PAM_CONV_ERR);
847
848 if ((reply = malloc(n * sizeof(*reply))) == NULL)
849 return (PAM_CONV_ERR);
850 memset(reply, 0, n * sizeof(*reply));
851
852 for (i = 0; i < n; ++i) {
853 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
854 case PAM_ERROR_MSG:
855 case PAM_TEXT_INFO:
856 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
857 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
858 buffer_append(&loginmsg, "\n", 1 );
859 reply[i].resp_retcode = PAM_SUCCESS;
860 break;
861 default:
862 goto fail;
863 }
864 }
865 *resp = reply;
866 return (PAM_SUCCESS);
867
868 fail:
869 for(i = 0; i < n; i++) {
870 if (reply[i].resp != NULL)
871 xfree(reply[i].resp);
872 }
873 xfree(reply);
874 return (PAM_CONV_ERR);
875}
876
877static struct pam_conv store_conv = { pam_store_conv, NULL };
878
Darren Tucker18df00c2003-11-18 12:42:07 +1100879void
880do_pam_session(void)
881{
Darren Tucker1921ed92004-02-10 13:23:28 +1100882 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +1100883 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100884 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +1100885 if (sshpam_err != PAM_SUCCESS)
886 fatal("PAM: failed to set PAM_CONV: %s",
887 pam_strerror(sshpam_handle, sshpam_err));
888 sshpam_err = pam_open_session(sshpam_handle, 0);
889 if (sshpam_err != PAM_SUCCESS)
890 fatal("PAM: pam_open_session(): %s",
891 pam_strerror(sshpam_handle, sshpam_err));
892 sshpam_session_open = 1;
893}
894
Damien Millera8e06ce2003-11-21 23:48:55 +1100895/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000896 * Set a PAM environment string. We need to do this so that the session
897 * modules can handle things like Kerberos/GSI credentials that appear
898 * during the ssh authentication process.
899 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000900int
Damien Millera8e06ce2003-11-21 23:48:55 +1100901do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000902{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000903 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100904#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000905 char *compound;
906 size_t len;
907
908 len = strlen(name) + strlen(value) + 2;
909 compound = xmalloc(len);
910
911 snprintf(compound, len, "%s=%s", name, value);
912 ret = pam_putenv(sshpam_handle, compound);
913 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000914#endif
Damien Millerf2728092003-09-17 07:24:25 +1000915
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000916 return (ret);
917}
918
Damien Miller4f9f42a2003-05-10 19:28:02 +1000919char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100920fetch_pam_child_environment(void)
921{
922 return sshpam_env;
923}
924
925char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000926fetch_pam_environment(void)
927{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000928 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000929}
930
931void
932free_pam_environment(char **env)
933{
934 char **envp;
935
Damien Millere27c6cc2003-05-16 18:21:01 +1000936 if (env == NULL)
937 return;
938
Damien Miller4f9f42a2003-05-10 19:28:02 +1000939 for (envp = env; *envp; envp++)
940 xfree(*envp);
941 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100942}
943
944#endif /* USE_PAM */