blob: 833c850e7d7ad3500d2fa943b66efc88f5fcc52e [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 Tuckerb53355e2004-05-24 11:55:36 +100034RCSID("$Id: auth-pam.c,v 1.102 2004/05/24 01:55:36 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"
Darren Tuckerb6db1722004-05-13 17:29:35 +100052#include "misc.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;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +100061extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +100062
Damien Miller4f9f42a2003-05-10 19:28:02 +100063#ifdef USE_POSIX_THREADS
64#include <pthread.h>
65/*
Damien Millera8e06ce2003-11-21 23:48:55 +110066 * Avoid namespace clash when *not* using pthreads for systems *with*
67 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +100068 * (e.g. Linux)
69 */
Damien Millera8e06ce2003-11-21 23:48:55 +110070typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +100071#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +110072typedef pid_t sp_pthread_t;
73#endif
74
75struct pam_ctxt {
76 sp_pthread_t pam_thread;
77 int pam_psock;
78 int pam_csock;
79 int pam_done;
80};
81
82static void sshpam_free_ctx(void *);
83static struct pam_ctxt *cleanup_ctxt;
84
85#ifndef USE_POSIX_THREADS
Damien Miller4f9f42a2003-05-10 19:28:02 +100086/*
87 * Simulate threads with processes.
88 */
Kevin Steves63007d42002-07-21 17:57:01 +000089
Darren Tucker749bc952004-01-14 22:14:04 +110090static int sshpam_thread_status = -1;
91static mysig_t sshpam_oldsig;
92
93static void
94sshpam_sigchld_handler(int sig)
95{
Darren Tuckerb53355e2004-05-24 11:55:36 +100096 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +110097 if (cleanup_ctxt == NULL)
98 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +100099 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
100 == -1) {
101 /* PAM thread has not exitted, privsep slave must have */
102 kill(cleanup_ctxt->pam_thread, SIGTERM);
103 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
104 == -1)
105 return; /* could not wait */
106 }
Darren Tucker749bc952004-01-14 22:14:04 +1100107 if (WIFSIGNALED(sshpam_thread_status) &&
108 WTERMSIG(sshpam_thread_status) == SIGTERM)
109 return; /* terminated by pthread_cancel */
110 if (!WIFEXITED(sshpam_thread_status))
111 fatal("PAM: authentication thread exited unexpectedly");
112 if (WEXITSTATUS(sshpam_thread_status) != 0)
113 fatal("PAM: authentication thread exited uncleanly");
114}
115
Damien Miller4f9f42a2003-05-10 19:28:02 +1000116static void
117pthread_exit(void *value __unused)
118{
119 _exit(0);
120}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000121
Damien Miller4f9f42a2003-05-10 19:28:02 +1000122static int
123pthread_create(sp_pthread_t *thread, const void *attr __unused,
124 void *(*thread_start)(void *), void *arg)
125{
126 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +1100127
Darren Tuckerb9b60212004-03-04 20:03:54 +1100128 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000129 switch ((pid = fork())) {
130 case -1:
131 error("fork(): %s", strerror(errno));
132 return (-1);
133 case 0:
134 thread_start(arg);
135 _exit(1);
136 default:
137 *thread = pid;
Darren Tucker749bc952004-01-14 22:14:04 +1100138 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000139 return (0);
140 }
141}
Damien Millere72b7af1999-12-30 15:08:44 +1100142
Damien Miller4f9f42a2003-05-10 19:28:02 +1000143static int
144pthread_cancel(sp_pthread_t thread)
145{
Darren Tucker7ae09622004-01-14 23:07:56 +1100146 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000147 return (kill(thread, SIGTERM));
148}
149
150static int
151pthread_join(sp_pthread_t thread, void **value __unused)
152{
153 int status;
154
Darren Tucker749bc952004-01-14 22:14:04 +1100155 if (sshpam_thread_status != -1)
156 return (sshpam_thread_status);
157 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000158 waitpid(thread, &status, 0);
159 return (status);
160}
161#endif
162
163
Damien Miller5c3a5582003-09-23 22:12:38 +1000164static pam_handle_t *sshpam_handle = NULL;
165static int sshpam_err = 0;
166static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000167static int sshpam_session_open = 0;
168static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100169static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100170static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000171static Authctxt *sshpam_authctxt = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000172
Damien Millerc756e9b2003-11-17 21:41:42 +1100173/* Some PAM implementations don't implement this */
174#ifndef HAVE_PAM_GETENVLIST
175static char **
176pam_getenvlist(pam_handle_t *pamh)
177{
178 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100179 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100180 * for platforms without pam_getenvlist by searching for known
181 * env vars (e.g. KRB5CCNAME) from the PAM environment.
182 */
183 return NULL;
184}
185#endif
186
Darren Tucker07705c72003-12-18 15:34:31 +1100187void
188pam_password_change_required(int reqd)
189{
Darren Tuckera8df9242004-01-15 00:15:07 +1100190 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000191 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100192 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000193 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100194 if (reqd) {
195 no_port_forwarding_flag |= 2;
196 no_agent_forwarding_flag |= 2;
197 no_x11_forwarding_flag |= 2;
198 } else {
199 no_port_forwarding_flag &= ~2;
200 no_agent_forwarding_flag &= ~2;
201 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100202 }
203}
Darren Tucker1921ed92004-02-10 13:23:28 +1100204
Damien Millerc756e9b2003-11-17 21:41:42 +1100205/* Import regular and PAM environment from subprocess */
206static void
207import_environments(Buffer *b)
208{
209 char *env;
210 u_int i, num_env;
211 int err;
212
Darren Tuckera8df9242004-01-15 00:15:07 +1100213 debug3("PAM: %s entering", __func__);
214
Darren Tucker4b385d42004-03-04 19:54:10 +1100215#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100216 /* Import variables set by do_pam_account */
217 sshpam_account_status = buffer_get_int(b);
218 pam_password_change_required(buffer_get_int(b));
219
Damien Millerc756e9b2003-11-17 21:41:42 +1100220 /* Import environment from subprocess */
221 num_env = buffer_get_int(b);
222 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
223 debug3("PAM: num env strings %d", num_env);
224 for(i = 0; i < num_env; i++)
225 sshpam_env[i] = buffer_get_string(b, NULL);
226
227 sshpam_env[num_env] = NULL;
228
229 /* Import PAM environment from subprocess */
230 num_env = buffer_get_int(b);
231 debug("PAM: num PAM env strings %d", num_env);
232 for(i = 0; i < num_env; i++) {
233 env = buffer_get_string(b, NULL);
234
Darren Tucker8a1624c2003-11-18 12:45:35 +1100235#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100236 /* Errors are not fatal here */
237 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
238 error("PAM: pam_putenv: %s",
239 pam_strerror(sshpam_handle, sshpam_err));
240 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100241#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100242 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100243#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100244}
245
Damien Miller9d5705a2000-09-16 16:09:27 +1100246/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000247 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100248 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000249static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000250sshpam_thread_conv(int n, const struct pam_message **msg,
251 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100252{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000253 Buffer buffer;
254 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000255 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000256 int i;
257
Darren Tuckerba53b832004-02-17 20:46:59 +1100258 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000259 *resp = NULL;
260
Damien Miller4f9f42a2003-05-10 19:28:02 +1000261 ctxt = data;
262 if (n <= 0 || n > PAM_MAX_NUM_MSG)
263 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000264
265 if ((reply = malloc(n * sizeof(*reply))) == NULL)
266 return (PAM_CONV_ERR);
267 memset(reply, 0, n * sizeof(*reply));
268
Damien Miller4f9f42a2003-05-10 19:28:02 +1000269 buffer_init(&buffer);
270 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000271 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
272 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100273 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000274 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100275 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100276 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
277 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100278 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100279 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000280 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
281 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000282 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000283 break;
284 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100285 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000286 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100287 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100288 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
289 goto fail;
290 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
291 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000292 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
293 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000294 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000295 break;
296 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100297 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000298 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100299 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100300 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
301 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000302 break;
303 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100304 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000305 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100306 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100307 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
308 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000309 break;
310 default:
311 goto fail;
312 }
313 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000314 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000315 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000316 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000317 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000318
Damien Miller4f9f42a2003-05-10 19:28:02 +1000319 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000320 for(i = 0; i < n; i++) {
321 if (reply[i].resp != NULL)
322 xfree(reply[i].resp);
323 }
324 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000325 buffer_free(&buffer);
326 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000327}
328
Damien Miller4f9f42a2003-05-10 19:28:02 +1000329/*
330 * Authentication thread.
331 */
332static void *
333sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100334{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000335 struct pam_ctxt *ctxt = ctxtp;
336 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000337 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000338#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100339 extern char **environ;
340 char **env_from_pam;
341 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000342 const char *pam_user;
343
344 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
345 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100346 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000347#endif
348
Damien Millerf4b6f102003-09-02 23:12:06 +1000349 sshpam_conv.conv = sshpam_thread_conv;
350 sshpam_conv.appdata_ptr = ctxt;
351
Darren Tucker17addf02004-03-30 20:57:57 +1000352 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100353 fatal("%s: PAM authctxt not initialized", __func__);
354
Damien Miller4f9f42a2003-05-10 19:28:02 +1000355 buffer_init(&buffer);
356 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
357 (const void *)&sshpam_conv);
358 if (sshpam_err != PAM_SUCCESS)
359 goto auth_fail;
360 sshpam_err = pam_authenticate(sshpam_handle, 0);
361 if (sshpam_err != PAM_SUCCESS)
362 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100363
Darren Tuckerc376c862003-12-18 16:08:59 +1100364 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100365 if (!do_pam_account())
366 goto auth_fail;
Darren Tucker17addf02004-03-30 20:57:57 +1000367 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100368 sshpam_err = pam_chauthtok(sshpam_handle,
369 PAM_CHANGE_EXPIRED_AUTHTOK);
370 if (sshpam_err != PAM_SUCCESS)
371 goto auth_fail;
372 pam_password_change_required(0);
373 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100374 }
Darren Tucker07705c72003-12-18 15:34:31 +1100375
Damien Miller4f9f42a2003-05-10 19:28:02 +1000376 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100377
378#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100379 /* Export variables set by do_pam_account */
380 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000381 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100382
Damien Millerc756e9b2003-11-17 21:41:42 +1100383 /* Export any environment strings set in child */
384 for(i = 0; environ[i] != NULL; i++)
385 ; /* Count */
386 buffer_put_int(&buffer, i);
387 for(i = 0; environ[i] != NULL; i++)
388 buffer_put_cstring(&buffer, environ[i]);
389
390 /* Export any environment strings set by PAM in child */
391 env_from_pam = pam_getenvlist(sshpam_handle);
392 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
393 ; /* Count */
394 buffer_put_int(&buffer, i);
395 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
396 buffer_put_cstring(&buffer, env_from_pam[i]);
397#endif /* USE_POSIX_THREADS */
398
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, sshpam_err, &buffer);
401 buffer_free(&buffer);
402 pthread_exit(NULL);
403
404 auth_fail:
405 buffer_put_cstring(&buffer,
406 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100407 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000408 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
409 buffer_free(&buffer);
410 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100411
Damien Miller4f9f42a2003-05-10 19:28:02 +1000412 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000413}
414
Darren Tucker8846a072003-10-07 11:30:15 +1000415void
416sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000417{
Darren Tucker8846a072003-10-07 11:30:15 +1000418 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000419
Darren Tuckera8df9242004-01-15 00:15:07 +1100420 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000421 if (ctxt != NULL && ctxt->pam_thread != 0) {
422 pthread_cancel(ctxt->pam_thread);
423 pthread_join(ctxt->pam_thread, NULL);
424 close(ctxt->pam_psock);
425 close(ctxt->pam_csock);
426 memset(ctxt, 0, sizeof(*ctxt));
427 cleanup_ctxt = NULL;
428 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000429}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000430
Damien Miller4f9f42a2003-05-10 19:28:02 +1000431static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000432sshpam_null_conv(int n, const struct pam_message **msg,
433 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000434{
Darren Tuckerba53b832004-02-17 20:46:59 +1100435 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000436 return (PAM_CONV_ERR);
437}
Damien Miller63dc3e92001-02-07 12:58:33 +1100438
Damien Miller4f9f42a2003-05-10 19:28:02 +1000439static struct pam_conv null_conv = { sshpam_null_conv, NULL };
440
Darren Tucker8846a072003-10-07 11:30:15 +1000441void
442sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000443{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000444 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000445 if (sshpam_handle == NULL)
446 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000447 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
448 if (sshpam_cred_established) {
449 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
450 sshpam_cred_established = 0;
451 }
452 if (sshpam_session_open) {
453 pam_close_session(sshpam_handle, PAM_SILENT);
454 sshpam_session_open = 0;
455 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100456 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000457 pam_end(sshpam_handle, sshpam_err);
458 sshpam_handle = NULL;
459}
460
461static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100462sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000463{
Darren Tucker455813b2003-09-13 22:12:11 +1000464 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100465 const char *pam_rhost, *pam_user, *user = authctxt->user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000466
467 if (sshpam_handle != NULL) {
468 /* We already have a PAM context; check if the user matches */
469 sshpam_err = pam_get_item(sshpam_handle,
470 PAM_USER, (const void **)&pam_user);
471 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
472 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000473 pam_end(sshpam_handle, sshpam_err);
474 sshpam_handle = NULL;
475 }
476 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000477 sshpam_err =
478 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000479 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100480
Damien Miller4f9f42a2003-05-10 19:28:02 +1000481 if (sshpam_err != PAM_SUCCESS) {
482 pam_end(sshpam_handle, sshpam_err);
483 sshpam_handle = NULL;
484 return (-1);
485 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000486 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000487 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000488 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
489 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000490 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000491 sshpam_handle = NULL;
492 return (-1);
493 }
494#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100495 /*
496 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
497 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000498 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100499 */
Damien Miller25d93422003-05-18 20:45:47 +1000500 debug("PAM: setting PAM_TTY to \"ssh\"");
501 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
502 if (sshpam_err != PAM_SUCCESS) {
503 pam_end(sshpam_handle, sshpam_err);
504 sshpam_handle = NULL;
505 return (-1);
506 }
507#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000508 return (0);
509}
510
511static void *
512sshpam_init_ctx(Authctxt *authctxt)
513{
514 struct pam_ctxt *ctxt;
515 int socks[2];
516
Darren Tuckera8df9242004-01-15 00:15:07 +1100517 debug3("PAM: %s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000518 /* Refuse to start if we don't have PAM enabled */
519 if (!options.use_pam)
520 return NULL;
521
Damien Miller4f9f42a2003-05-10 19:28:02 +1000522 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100523 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000524 error("PAM: initialization failed");
525 return (NULL);
526 }
527
528 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000529 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000530
531 /* Start the authentication thread */
532 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
533 error("PAM: failed create sockets: %s", strerror(errno));
534 xfree(ctxt);
535 return (NULL);
536 }
537 ctxt->pam_psock = socks[0];
538 ctxt->pam_csock = socks[1];
539 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
540 error("PAM: failed to start authentication thread: %s",
541 strerror(errno));
542 close(socks[0]);
543 close(socks[1]);
544 xfree(ctxt);
545 return (NULL);
546 }
Darren Tucker8846a072003-10-07 11:30:15 +1000547 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000548 return (ctxt);
549}
550
551static int
552sshpam_query(void *ctx, char **name, char **info,
553 u_int *num, char ***prompts, u_int **echo_on)
554{
555 Buffer buffer;
556 struct pam_ctxt *ctxt = ctx;
557 size_t plen;
558 u_char type;
559 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000560 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000561
Darren Tuckera8df9242004-01-15 00:15:07 +1100562 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000563 buffer_init(&buffer);
564 *name = xstrdup("");
565 *info = xstrdup("");
566 *prompts = xmalloc(sizeof(char *));
567 **prompts = NULL;
568 plen = 0;
569 *echo_on = xmalloc(sizeof(u_int));
570 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
571 type = buffer_get_char(&buffer);
572 msg = buffer_get_string(&buffer, NULL);
573 switch (type) {
574 case PAM_PROMPT_ECHO_ON:
575 case PAM_PROMPT_ECHO_OFF:
576 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000577 len = plen + strlen(msg) + 1;
578 **prompts = xrealloc(**prompts, len);
579 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000580 **echo_on = (type == PAM_PROMPT_ECHO_ON);
581 xfree(msg);
582 return (0);
583 case PAM_ERROR_MSG:
584 case PAM_TEXT_INFO:
585 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100586 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000587 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100588 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000589 xfree(msg);
590 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000591 case PAM_SUCCESS:
592 case PAM_AUTH_ERR:
593 if (**prompts != NULL) {
594 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100595 debug("PAM: %s", **prompts);
596 buffer_append(&loginmsg, **prompts,
597 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000598 xfree(**prompts);
599 **prompts = NULL;
600 }
601 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100602 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000603 *num = 0;
604 **echo_on = 0;
605 ctxt->pam_done = 1;
606 xfree(msg);
607 return (0);
608 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000609 error("PAM: %s for %s%.100s from %.100s", msg,
610 sshpam_authctxt->valid ? "" : "illegal user ",
611 sshpam_authctxt->user,
612 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000613 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000614 default:
615 *num = 0;
616 **echo_on = 0;
617 xfree(msg);
618 ctxt->pam_done = -1;
619 return (-1);
620 }
621 }
622 return (-1);
623}
624
625/* XXX - see also comment in auth-chall.c:verify_response */
626static int
627sshpam_respond(void *ctx, u_int num, char **resp)
628{
629 Buffer buffer;
630 struct pam_ctxt *ctxt = ctx;
631
Darren Tuckera8df9242004-01-15 00:15:07 +1100632 debug2("PAM: %s entering, %d responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000633 switch (ctxt->pam_done) {
634 case 1:
635 sshpam_authenticated = 1;
636 return (0);
637 case 0:
638 break;
639 default:
640 return (-1);
641 }
642 if (num != 1) {
643 error("PAM: expected one response, got %u", num);
644 return (-1);
645 }
646 buffer_init(&buffer);
647 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100648 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
649 buffer_free(&buffer);
650 return (-1);
651 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000652 buffer_free(&buffer);
653 return (1);
654}
655
656static void
657sshpam_free_ctx(void *ctxtp)
658{
659 struct pam_ctxt *ctxt = ctxtp;
660
Darren Tuckera8df9242004-01-15 00:15:07 +1100661 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000662 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000663 xfree(ctxt);
664 /*
665 * We don't call sshpam_cleanup() here because we may need the PAM
666 * handle at a later stage, e.g. when setting up a session. It's
667 * still on the cleanup list, so pam_end() *will* be called before
668 * the server process terminates.
669 */
670}
671
672KbdintDevice sshpam_device = {
673 "pam",
674 sshpam_init_ctx,
675 sshpam_query,
676 sshpam_respond,
677 sshpam_free_ctx
678};
679
680KbdintDevice mm_sshpam_device = {
681 "pam",
682 mm_sshpam_init_ctx,
683 mm_sshpam_query,
684 mm_sshpam_respond,
685 mm_sshpam_free_ctx
686};
687
688/*
689 * This replaces auth-pam.c
690 */
691void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100692start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000693{
Damien Miller9d507da2003-05-14 15:31:12 +1000694 if (!options.use_pam)
695 fatal("PAM: initialisation requested when UsePAM=no");
696
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100697 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000698 fatal("PAM: initialisation failed");
699}
700
701void
702finish_pam(void)
703{
Darren Tucker8846a072003-10-07 11:30:15 +1000704 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000705}
706
Damien Miller1f499fd2003-08-25 13:08:49 +1000707u_int
708do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000709{
Darren Tucker07705c72003-12-18 15:34:31 +1100710 if (sshpam_account_status != -1)
711 return (sshpam_account_status);
712
Damien Miller1f499fd2003-08-25 13:08:49 +1000713 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tuckera8df9242004-01-15 00:15:07 +1100714 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100715
716 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
717 sshpam_account_status = 0;
718 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000719 }
720
Darren Tucker07705c72003-12-18 15:34:31 +1100721 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
722 pam_password_change_required(1);
723
724 sshpam_account_status = 1;
725 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000726}
727
728void
Damien Miller341c6e62003-09-02 23:18:52 +1000729do_pam_set_tty(const char *tty)
730{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000731 if (tty != NULL) {
732 debug("PAM: setting PAM_TTY to \"%s\"", tty);
733 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
734 if (sshpam_err != PAM_SUCCESS)
735 fatal("PAM: failed to set PAM_TTY: %s",
736 pam_strerror(sshpam_handle, sshpam_err));
737 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000738}
739
740void
741do_pam_setcred(int init)
742{
743 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
744 (const void *)&null_conv);
745 if (sshpam_err != PAM_SUCCESS)
746 fatal("PAM: failed to set PAM_CONV: %s",
747 pam_strerror(sshpam_handle, sshpam_err));
748 if (init) {
749 debug("PAM: establishing credentials");
750 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
751 } else {
752 debug("PAM: reinitializing credentials");
753 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
754 }
755 if (sshpam_err == PAM_SUCCESS) {
756 sshpam_cred_established = 1;
757 return;
758 }
759 if (sshpam_authenticated)
760 fatal("PAM: pam_setcred(): %s",
761 pam_strerror(sshpam_handle, sshpam_err));
762 else
763 debug("PAM: pam_setcred(): %s",
764 pam_strerror(sshpam_handle, sshpam_err));
765}
766
Damien Miller4f9f42a2003-05-10 19:28:02 +1000767static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100768pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000769 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000770{
771 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000772 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000773 int i;
774
Darren Tuckerba53b832004-02-17 20:46:59 +1100775 debug3("PAM: %s called with %d messages", __func__, n);
776
Damien Miller5c3a5582003-09-23 22:12:38 +1000777 *resp = NULL;
778
Darren Tucker18df00c2003-11-18 12:42:07 +1100779 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000780 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000781
782 if ((reply = malloc(n * sizeof(*reply))) == NULL)
783 return (PAM_CONV_ERR);
784 memset(reply, 0, n * sizeof(*reply));
785
Damien Miller4f9f42a2003-05-10 19:28:02 +1000786 for (i = 0; i < n; ++i) {
787 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
788 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000789 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100790 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000791 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000792 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000793 break;
794 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100795 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000796 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000797 reply[i].resp = xstrdup(input);
798 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000799 break;
800 case PAM_ERROR_MSG:
801 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100802 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000803 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000804 break;
805 default:
806 goto fail;
807 }
808 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000809 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000810 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000811
Damien Miller4f9f42a2003-05-10 19:28:02 +1000812 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000813 for(i = 0; i < n; i++) {
814 if (reply[i].resp != NULL)
815 xfree(reply[i].resp);
816 }
817 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000818 return (PAM_CONV_ERR);
819}
820
Darren Tucker18df00c2003-11-18 12:42:07 +1100821static struct pam_conv tty_conv = { pam_tty_conv, NULL };
822
Damien Miller4f9f42a2003-05-10 19:28:02 +1000823/*
824 * XXX this should be done in the authentication phase, but ssh1 doesn't
825 * support that
826 */
827void
828do_pam_chauthtok(void)
829{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000830 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000831 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000832 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100833 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000834 if (sshpam_err != PAM_SUCCESS)
835 fatal("PAM: failed to set PAM_CONV: %s",
836 pam_strerror(sshpam_handle, sshpam_err));
837 debug("PAM: changing password");
838 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
839 if (sshpam_err != PAM_SUCCESS)
840 fatal("PAM: pam_chauthtok(): %s",
841 pam_strerror(sshpam_handle, sshpam_err));
842}
843
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100844static int
845pam_store_conv(int n, const struct pam_message **msg,
846 struct pam_response **resp, void *data)
847{
848 struct pam_response *reply;
849 int i;
850 size_t len;
851
852 debug3("PAM: %s called with %d messages", __func__, n);
853 *resp = NULL;
854
855 if (n <= 0 || n > PAM_MAX_NUM_MSG)
856 return (PAM_CONV_ERR);
857
858 if ((reply = malloc(n * sizeof(*reply))) == NULL)
859 return (PAM_CONV_ERR);
860 memset(reply, 0, n * sizeof(*reply));
861
862 for (i = 0; i < n; ++i) {
863 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
864 case PAM_ERROR_MSG:
865 case PAM_TEXT_INFO:
866 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
867 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
868 buffer_append(&loginmsg, "\n", 1 );
869 reply[i].resp_retcode = PAM_SUCCESS;
870 break;
871 default:
872 goto fail;
873 }
874 }
875 *resp = reply;
876 return (PAM_SUCCESS);
877
878 fail:
879 for(i = 0; i < n; i++) {
880 if (reply[i].resp != NULL)
881 xfree(reply[i].resp);
882 }
883 xfree(reply);
884 return (PAM_CONV_ERR);
885}
886
887static struct pam_conv store_conv = { pam_store_conv, NULL };
888
Darren Tucker18df00c2003-11-18 12:42:07 +1100889void
890do_pam_session(void)
891{
Darren Tucker1921ed92004-02-10 13:23:28 +1100892 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +1100893 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100894 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +1100895 if (sshpam_err != PAM_SUCCESS)
896 fatal("PAM: failed to set PAM_CONV: %s",
897 pam_strerror(sshpam_handle, sshpam_err));
898 sshpam_err = pam_open_session(sshpam_handle, 0);
899 if (sshpam_err != PAM_SUCCESS)
900 fatal("PAM: pam_open_session(): %s",
901 pam_strerror(sshpam_handle, sshpam_err));
902 sshpam_session_open = 1;
903}
904
Damien Millera8e06ce2003-11-21 23:48:55 +1100905/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000906 * Set a PAM environment string. We need to do this so that the session
907 * modules can handle things like Kerberos/GSI credentials that appear
908 * during the ssh authentication process.
909 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000910int
Damien Millera8e06ce2003-11-21 23:48:55 +1100911do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000912{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000913 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100914#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000915 char *compound;
916 size_t len;
917
918 len = strlen(name) + strlen(value) + 2;
919 compound = xmalloc(len);
920
921 snprintf(compound, len, "%s=%s", name, value);
922 ret = pam_putenv(sshpam_handle, compound);
923 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000924#endif
Damien Millerf2728092003-09-17 07:24:25 +1000925
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000926 return (ret);
927}
928
Damien Miller4f9f42a2003-05-10 19:28:02 +1000929char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100930fetch_pam_child_environment(void)
931{
932 return sshpam_env;
933}
934
935char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000936fetch_pam_environment(void)
937{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000938 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000939}
940
941void
942free_pam_environment(char **env)
943{
944 char **envp;
945
Damien Millere27c6cc2003-05-16 18:21:01 +1000946 if (env == NULL)
947 return;
948
Damien Miller4f9f42a2003-05-10 19:28:02 +1000949 for (envp = env; *envp; envp++)
950 xfree(*envp);
951 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100952}
953
954#endif /* USE_PAM */