blob: ea361f17126c10e5ae34fa2dfc8694ec28e054ee [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 Tuckerb9b60212004-03-04 20:03:54 +110034RCSID("$Id: auth-pam.c,v 1.97 2004/03/04 09:03:54 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 Tucker1921ed92004-02-10 13:23:28 +1100163static int *force_pwchange;
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 Tucker1921ed92004-02-10 13:23:28 +1100183 *force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100184 if (reqd) {
185 no_port_forwarding_flag |= 2;
186 no_agent_forwarding_flag |= 2;
187 no_x11_forwarding_flag |= 2;
188 } else {
189 no_port_forwarding_flag &= ~2;
190 no_agent_forwarding_flag &= ~2;
191 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100192 }
193}
Darren Tucker1921ed92004-02-10 13:23:28 +1100194
Damien Millerc756e9b2003-11-17 21:41:42 +1100195/* Import regular and PAM environment from subprocess */
196static void
197import_environments(Buffer *b)
198{
199 char *env;
200 u_int i, num_env;
201 int err;
202
Darren Tuckera8df9242004-01-15 00:15:07 +1100203 debug3("PAM: %s entering", __func__);
204
Darren Tucker4b385d42004-03-04 19:54:10 +1100205#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100206 /* Import variables set by do_pam_account */
207 sshpam_account_status = buffer_get_int(b);
208 pam_password_change_required(buffer_get_int(b));
209
Damien Millerc756e9b2003-11-17 21:41:42 +1100210 /* Import environment from subprocess */
211 num_env = buffer_get_int(b);
212 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
213 debug3("PAM: num env strings %d", num_env);
214 for(i = 0; i < num_env; i++)
215 sshpam_env[i] = buffer_get_string(b, NULL);
216
217 sshpam_env[num_env] = NULL;
218
219 /* Import PAM environment from subprocess */
220 num_env = buffer_get_int(b);
221 debug("PAM: num PAM env strings %d", num_env);
222 for(i = 0; i < num_env; i++) {
223 env = buffer_get_string(b, NULL);
224
Darren Tucker8a1624c2003-11-18 12:45:35 +1100225#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100226 /* Errors are not fatal here */
227 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
228 error("PAM: pam_putenv: %s",
229 pam_strerror(sshpam_handle, sshpam_err));
230 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100231#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100232 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100233#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100234}
235
Damien Miller9d5705a2000-09-16 16:09:27 +1100236/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000237 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100238 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000239static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000240sshpam_thread_conv(int n, const struct pam_message **msg,
241 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100242{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000243 Buffer buffer;
244 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000245 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000246 int i;
247
Darren Tuckerba53b832004-02-17 20:46:59 +1100248 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000249 *resp = NULL;
250
Damien Miller4f9f42a2003-05-10 19:28:02 +1000251 ctxt = data;
252 if (n <= 0 || n > PAM_MAX_NUM_MSG)
253 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000254
255 if ((reply = malloc(n * sizeof(*reply))) == NULL)
256 return (PAM_CONV_ERR);
257 memset(reply, 0, n * sizeof(*reply));
258
Damien Miller4f9f42a2003-05-10 19:28:02 +1000259 buffer_init(&buffer);
260 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000261 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
262 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100263 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000264 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100265 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100266 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
267 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100268 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100269 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000270 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
271 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000272 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000273 break;
274 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100275 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000276 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100277 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100278 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
279 goto fail;
280 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
281 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000282 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
283 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000284 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000285 break;
286 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100287 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000288 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100289 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100290 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
291 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000292 break;
293 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100294 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000295 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100296 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100297 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
298 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000299 break;
300 default:
301 goto fail;
302 }
303 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000304 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000305 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000306 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000307 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000308
Damien Miller4f9f42a2003-05-10 19:28:02 +1000309 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000310 for(i = 0; i < n; i++) {
311 if (reply[i].resp != NULL)
312 xfree(reply[i].resp);
313 }
314 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000315 buffer_free(&buffer);
316 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000317}
318
Damien Miller4f9f42a2003-05-10 19:28:02 +1000319/*
320 * Authentication thread.
321 */
322static void *
323sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100324{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000325 struct pam_ctxt *ctxt = ctxtp;
326 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000327 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000328#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100329 extern char **environ;
330 char **env_from_pam;
331 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000332 const char *pam_user;
333
334 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
335 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100336 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000337#endif
338
Damien Millerf4b6f102003-09-02 23:12:06 +1000339 sshpam_conv.conv = sshpam_thread_conv;
340 sshpam_conv.appdata_ptr = ctxt;
341
Damien Miller4f9f42a2003-05-10 19:28:02 +1000342 buffer_init(&buffer);
343 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
344 (const void *)&sshpam_conv);
345 if (sshpam_err != PAM_SUCCESS)
346 goto auth_fail;
347 sshpam_err = pam_authenticate(sshpam_handle, 0);
348 if (sshpam_err != PAM_SUCCESS)
349 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100350
Darren Tuckerc376c862003-12-18 16:08:59 +1100351 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100352 if (!do_pam_account())
353 goto auth_fail;
Darren Tucker1921ed92004-02-10 13:23:28 +1100354 if (*force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100355 sshpam_err = pam_chauthtok(sshpam_handle,
356 PAM_CHANGE_EXPIRED_AUTHTOK);
357 if (sshpam_err != PAM_SUCCESS)
358 goto auth_fail;
359 pam_password_change_required(0);
360 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100361 }
Darren Tucker07705c72003-12-18 15:34:31 +1100362
Damien Miller4f9f42a2003-05-10 19:28:02 +1000363 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100364
365#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100366 /* Export variables set by do_pam_account */
367 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker1921ed92004-02-10 13:23:28 +1100368 buffer_put_int(&buffer, *force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100369
Damien Millerc756e9b2003-11-17 21:41:42 +1100370 /* Export any environment strings set in child */
371 for(i = 0; environ[i] != NULL; i++)
372 ; /* Count */
373 buffer_put_int(&buffer, i);
374 for(i = 0; environ[i] != NULL; i++)
375 buffer_put_cstring(&buffer, environ[i]);
376
377 /* Export any environment strings set by PAM in child */
378 env_from_pam = pam_getenvlist(sshpam_handle);
379 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
380 ; /* Count */
381 buffer_put_int(&buffer, i);
382 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
383 buffer_put_cstring(&buffer, env_from_pam[i]);
384#endif /* USE_POSIX_THREADS */
385
Damien Miller9bdba702003-11-17 21:27:55 +1100386 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000387 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
388 buffer_free(&buffer);
389 pthread_exit(NULL);
390
391 auth_fail:
392 buffer_put_cstring(&buffer,
393 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100394 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000395 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
396 buffer_free(&buffer);
397 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100398
Damien Miller4f9f42a2003-05-10 19:28:02 +1000399 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000400}
401
Darren Tucker8846a072003-10-07 11:30:15 +1000402void
403sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000404{
Darren Tucker8846a072003-10-07 11:30:15 +1000405 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000406
Darren Tuckera8df9242004-01-15 00:15:07 +1100407 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000408 if (ctxt != NULL && ctxt->pam_thread != 0) {
409 pthread_cancel(ctxt->pam_thread);
410 pthread_join(ctxt->pam_thread, NULL);
411 close(ctxt->pam_psock);
412 close(ctxt->pam_csock);
413 memset(ctxt, 0, sizeof(*ctxt));
414 cleanup_ctxt = NULL;
415 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000416}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000417
Damien Miller4f9f42a2003-05-10 19:28:02 +1000418static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000419sshpam_null_conv(int n, const struct pam_message **msg,
420 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000421{
Darren Tuckerba53b832004-02-17 20:46:59 +1100422 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000423 return (PAM_CONV_ERR);
424}
Damien Miller63dc3e92001-02-07 12:58:33 +1100425
Damien Miller4f9f42a2003-05-10 19:28:02 +1000426static struct pam_conv null_conv = { sshpam_null_conv, NULL };
427
Darren Tucker8846a072003-10-07 11:30:15 +1000428void
429sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000430{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000431 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000432 if (sshpam_handle == NULL)
433 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000434 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
435 if (sshpam_cred_established) {
436 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
437 sshpam_cred_established = 0;
438 }
439 if (sshpam_session_open) {
440 pam_close_session(sshpam_handle, PAM_SILENT);
441 sshpam_session_open = 0;
442 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100443 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000444 pam_end(sshpam_handle, sshpam_err);
445 sshpam_handle = NULL;
446}
447
448static int
449sshpam_init(const char *user)
450{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000451 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000452 extern char *__progname;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000453 const char *pam_rhost, *pam_user;
454
455 if (sshpam_handle != NULL) {
456 /* We already have a PAM context; check if the user matches */
457 sshpam_err = pam_get_item(sshpam_handle,
458 PAM_USER, (const void **)&pam_user);
459 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
460 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000461 pam_end(sshpam_handle, sshpam_err);
462 sshpam_handle = NULL;
463 }
464 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000465 sshpam_err =
466 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000467 if (sshpam_err != PAM_SUCCESS) {
468 pam_end(sshpam_handle, sshpam_err);
469 sshpam_handle = NULL;
470 return (-1);
471 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000472 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000473 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000474 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
475 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000476 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000477 sshpam_handle = NULL;
478 return (-1);
479 }
480#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100481 /*
482 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
483 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000484 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100485 */
Damien Miller25d93422003-05-18 20:45:47 +1000486 debug("PAM: setting PAM_TTY to \"ssh\"");
487 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
488 if (sshpam_err != PAM_SUCCESS) {
489 pam_end(sshpam_handle, sshpam_err);
490 sshpam_handle = NULL;
491 return (-1);
492 }
493#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000494 return (0);
495}
496
497static void *
498sshpam_init_ctx(Authctxt *authctxt)
499{
500 struct pam_ctxt *ctxt;
501 int socks[2];
502
Darren Tuckera8df9242004-01-15 00:15:07 +1100503 debug3("PAM: %s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000504 /* Refuse to start if we don't have PAM enabled */
505 if (!options.use_pam)
506 return NULL;
507
Damien Miller4f9f42a2003-05-10 19:28:02 +1000508 /* Initialize PAM */
509 if (sshpam_init(authctxt->user) == -1) {
510 error("PAM: initialization failed");
511 return (NULL);
512 }
513
514 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000515 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000516
Darren Tucker1921ed92004-02-10 13:23:28 +1100517 force_pwchange = &(authctxt->force_pwchange);
518
Damien Miller4f9f42a2003-05-10 19:28:02 +1000519 /* Start the authentication thread */
520 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
521 error("PAM: failed create sockets: %s", strerror(errno));
522 xfree(ctxt);
523 return (NULL);
524 }
525 ctxt->pam_psock = socks[0];
526 ctxt->pam_csock = socks[1];
527 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
528 error("PAM: failed to start authentication thread: %s",
529 strerror(errno));
530 close(socks[0]);
531 close(socks[1]);
532 xfree(ctxt);
533 return (NULL);
534 }
Darren Tucker8846a072003-10-07 11:30:15 +1000535 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000536 return (ctxt);
537}
538
539static int
540sshpam_query(void *ctx, char **name, char **info,
541 u_int *num, char ***prompts, u_int **echo_on)
542{
543 Buffer buffer;
544 struct pam_ctxt *ctxt = ctx;
545 size_t plen;
546 u_char type;
547 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000548 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000549
Darren Tuckera8df9242004-01-15 00:15:07 +1100550 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000551 buffer_init(&buffer);
552 *name = xstrdup("");
553 *info = xstrdup("");
554 *prompts = xmalloc(sizeof(char *));
555 **prompts = NULL;
556 plen = 0;
557 *echo_on = xmalloc(sizeof(u_int));
558 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
559 type = buffer_get_char(&buffer);
560 msg = buffer_get_string(&buffer, NULL);
561 switch (type) {
562 case PAM_PROMPT_ECHO_ON:
563 case PAM_PROMPT_ECHO_OFF:
564 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000565 len = plen + strlen(msg) + 1;
566 **prompts = xrealloc(**prompts, len);
567 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000568 **echo_on = (type == PAM_PROMPT_ECHO_ON);
569 xfree(msg);
570 return (0);
571 case PAM_ERROR_MSG:
572 case PAM_TEXT_INFO:
573 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100574 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000575 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100576 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000577 xfree(msg);
578 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000579 case PAM_SUCCESS:
580 case PAM_AUTH_ERR:
581 if (**prompts != NULL) {
582 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100583 debug("PAM: %s", **prompts);
584 buffer_append(&loginmsg, **prompts,
585 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000586 xfree(**prompts);
587 **prompts = NULL;
588 }
589 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100590 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000591 *num = 0;
592 **echo_on = 0;
593 ctxt->pam_done = 1;
594 xfree(msg);
595 return (0);
596 }
597 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000598 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000599 default:
600 *num = 0;
601 **echo_on = 0;
602 xfree(msg);
603 ctxt->pam_done = -1;
604 return (-1);
605 }
606 }
607 return (-1);
608}
609
610/* XXX - see also comment in auth-chall.c:verify_response */
611static int
612sshpam_respond(void *ctx, u_int num, char **resp)
613{
614 Buffer buffer;
615 struct pam_ctxt *ctxt = ctx;
616
Darren Tuckera8df9242004-01-15 00:15:07 +1100617 debug2("PAM: %s entering, %d responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000618 switch (ctxt->pam_done) {
619 case 1:
620 sshpam_authenticated = 1;
621 return (0);
622 case 0:
623 break;
624 default:
625 return (-1);
626 }
627 if (num != 1) {
628 error("PAM: expected one response, got %u", num);
629 return (-1);
630 }
631 buffer_init(&buffer);
632 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100633 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
634 buffer_free(&buffer);
635 return (-1);
636 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000637 buffer_free(&buffer);
638 return (1);
639}
640
641static void
642sshpam_free_ctx(void *ctxtp)
643{
644 struct pam_ctxt *ctxt = ctxtp;
645
Darren Tuckera8df9242004-01-15 00:15:07 +1100646 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000647 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000648 xfree(ctxt);
649 /*
650 * We don't call sshpam_cleanup() here because we may need the PAM
651 * handle at a later stage, e.g. when setting up a session. It's
652 * still on the cleanup list, so pam_end() *will* be called before
653 * the server process terminates.
654 */
655}
656
657KbdintDevice sshpam_device = {
658 "pam",
659 sshpam_init_ctx,
660 sshpam_query,
661 sshpam_respond,
662 sshpam_free_ctx
663};
664
665KbdintDevice mm_sshpam_device = {
666 "pam",
667 mm_sshpam_init_ctx,
668 mm_sshpam_query,
669 mm_sshpam_respond,
670 mm_sshpam_free_ctx
671};
672
673/*
674 * This replaces auth-pam.c
675 */
676void
677start_pam(const char *user)
678{
Damien Miller9d507da2003-05-14 15:31:12 +1000679 if (!options.use_pam)
680 fatal("PAM: initialisation requested when UsePAM=no");
681
Damien Miller4f9f42a2003-05-10 19:28:02 +1000682 if (sshpam_init(user) == -1)
683 fatal("PAM: initialisation failed");
684}
685
686void
687finish_pam(void)
688{
Darren Tucker8846a072003-10-07 11:30:15 +1000689 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000690}
691
Damien Miller1f499fd2003-08-25 13:08:49 +1000692u_int
693do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000694{
Darren Tucker07705c72003-12-18 15:34:31 +1100695 if (sshpam_account_status != -1)
696 return (sshpam_account_status);
697
Damien Miller1f499fd2003-08-25 13:08:49 +1000698 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tuckera8df9242004-01-15 00:15:07 +1100699 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100700
701 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
702 sshpam_account_status = 0;
703 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000704 }
705
Darren Tucker07705c72003-12-18 15:34:31 +1100706 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
707 pam_password_change_required(1);
708
709 sshpam_account_status = 1;
710 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000711}
712
713void
Damien Miller341c6e62003-09-02 23:18:52 +1000714do_pam_set_tty(const char *tty)
715{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000716 if (tty != NULL) {
717 debug("PAM: setting PAM_TTY to \"%s\"", tty);
718 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
719 if (sshpam_err != PAM_SUCCESS)
720 fatal("PAM: failed to set PAM_TTY: %s",
721 pam_strerror(sshpam_handle, sshpam_err));
722 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000723}
724
725void
726do_pam_setcred(int init)
727{
728 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
729 (const void *)&null_conv);
730 if (sshpam_err != PAM_SUCCESS)
731 fatal("PAM: failed to set PAM_CONV: %s",
732 pam_strerror(sshpam_handle, sshpam_err));
733 if (init) {
734 debug("PAM: establishing credentials");
735 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
736 } else {
737 debug("PAM: reinitializing credentials");
738 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
739 }
740 if (sshpam_err == PAM_SUCCESS) {
741 sshpam_cred_established = 1;
742 return;
743 }
744 if (sshpam_authenticated)
745 fatal("PAM: pam_setcred(): %s",
746 pam_strerror(sshpam_handle, sshpam_err));
747 else
748 debug("PAM: pam_setcred(): %s",
749 pam_strerror(sshpam_handle, sshpam_err));
750}
751
Damien Miller4f9f42a2003-05-10 19:28:02 +1000752static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100753pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000754 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000755{
756 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000757 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000758 int i;
759
Darren Tuckerba53b832004-02-17 20:46:59 +1100760 debug3("PAM: %s called with %d messages", __func__, n);
761
Damien Miller5c3a5582003-09-23 22:12:38 +1000762 *resp = NULL;
763
Darren Tucker18df00c2003-11-18 12:42:07 +1100764 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000765 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000766
767 if ((reply = malloc(n * sizeof(*reply))) == NULL)
768 return (PAM_CONV_ERR);
769 memset(reply, 0, n * sizeof(*reply));
770
Damien Miller4f9f42a2003-05-10 19:28:02 +1000771 for (i = 0; i < n; ++i) {
772 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
773 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000774 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100775 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000776 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000777 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000778 break;
779 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100780 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000781 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000782 reply[i].resp = xstrdup(input);
783 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000784 break;
785 case PAM_ERROR_MSG:
786 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100787 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000788 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000789 break;
790 default:
791 goto fail;
792 }
793 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000794 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000795 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000796
Damien Miller4f9f42a2003-05-10 19:28:02 +1000797 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000798 for(i = 0; i < n; i++) {
799 if (reply[i].resp != NULL)
800 xfree(reply[i].resp);
801 }
802 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000803 return (PAM_CONV_ERR);
804}
805
Darren Tucker18df00c2003-11-18 12:42:07 +1100806static struct pam_conv tty_conv = { pam_tty_conv, NULL };
807
Damien Miller4f9f42a2003-05-10 19:28:02 +1000808/*
809 * XXX this should be done in the authentication phase, but ssh1 doesn't
810 * support that
811 */
812void
813do_pam_chauthtok(void)
814{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000815 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000816 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000817 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100818 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000819 if (sshpam_err != PAM_SUCCESS)
820 fatal("PAM: failed to set PAM_CONV: %s",
821 pam_strerror(sshpam_handle, sshpam_err));
822 debug("PAM: changing password");
823 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
824 if (sshpam_err != PAM_SUCCESS)
825 fatal("PAM: pam_chauthtok(): %s",
826 pam_strerror(sshpam_handle, sshpam_err));
827}
828
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100829static int
830pam_store_conv(int n, const struct pam_message **msg,
831 struct pam_response **resp, void *data)
832{
833 struct pam_response *reply;
834 int i;
835 size_t len;
836
837 debug3("PAM: %s called with %d messages", __func__, n);
838 *resp = NULL;
839
840 if (n <= 0 || n > PAM_MAX_NUM_MSG)
841 return (PAM_CONV_ERR);
842
843 if ((reply = malloc(n * sizeof(*reply))) == NULL)
844 return (PAM_CONV_ERR);
845 memset(reply, 0, n * sizeof(*reply));
846
847 for (i = 0; i < n; ++i) {
848 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
849 case PAM_ERROR_MSG:
850 case PAM_TEXT_INFO:
851 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
852 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
853 buffer_append(&loginmsg, "\n", 1 );
854 reply[i].resp_retcode = PAM_SUCCESS;
855 break;
856 default:
857 goto fail;
858 }
859 }
860 *resp = reply;
861 return (PAM_SUCCESS);
862
863 fail:
864 for(i = 0; i < n; i++) {
865 if (reply[i].resp != NULL)
866 xfree(reply[i].resp);
867 }
868 xfree(reply);
869 return (PAM_CONV_ERR);
870}
871
872static struct pam_conv store_conv = { pam_store_conv, NULL };
873
Darren Tucker18df00c2003-11-18 12:42:07 +1100874void
875do_pam_session(void)
876{
Darren Tucker1921ed92004-02-10 13:23:28 +1100877 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +1100878 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100879 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +1100880 if (sshpam_err != PAM_SUCCESS)
881 fatal("PAM: failed to set PAM_CONV: %s",
882 pam_strerror(sshpam_handle, sshpam_err));
883 sshpam_err = pam_open_session(sshpam_handle, 0);
884 if (sshpam_err != PAM_SUCCESS)
885 fatal("PAM: pam_open_session(): %s",
886 pam_strerror(sshpam_handle, sshpam_err));
887 sshpam_session_open = 1;
888}
889
Damien Millera8e06ce2003-11-21 23:48:55 +1100890/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000891 * Set a PAM environment string. We need to do this so that the session
892 * modules can handle things like Kerberos/GSI credentials that appear
893 * during the ssh authentication process.
894 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000895int
Damien Millera8e06ce2003-11-21 23:48:55 +1100896do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000897{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000898 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100899#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000900 char *compound;
901 size_t len;
902
903 len = strlen(name) + strlen(value) + 2;
904 compound = xmalloc(len);
905
906 snprintf(compound, len, "%s=%s", name, value);
907 ret = pam_putenv(sshpam_handle, compound);
908 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000909#endif
Damien Millerf2728092003-09-17 07:24:25 +1000910
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000911 return (ret);
912}
913
Damien Miller4f9f42a2003-05-10 19:28:02 +1000914char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100915fetch_pam_child_environment(void)
916{
917 return sshpam_env;
918}
919
920char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000921fetch_pam_environment(void)
922{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000923 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000924}
925
926void
927free_pam_environment(char **env)
928{
929 char **envp;
930
Damien Millere27c6cc2003-05-16 18:21:01 +1000931 if (env == NULL)
932 return;
933
Damien Miller4f9f42a2003-05-10 19:28:02 +1000934 for (envp = env; *envp; envp++)
935 xfree(*envp);
936 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100937}
938
939#endif /* USE_PAM */