blob: 53221a02ed6895c93027327b41967589f1f20847 [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 Tucker4b385d42004-03-04 19:54:10 +110034RCSID("$Id: auth-pam.c,v 1.96 2004/03/04 08:54:10 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
Damien Miller4f9f42a2003-05-10 19:28:02 +1000120 switch ((pid = fork())) {
121 case -1:
122 error("fork(): %s", strerror(errno));
123 return (-1);
124 case 0:
125 thread_start(arg);
126 _exit(1);
127 default:
128 *thread = pid;
Darren Tucker749bc952004-01-14 22:14:04 +1100129 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000130 return (0);
131 }
132}
Damien Millere72b7af1999-12-30 15:08:44 +1100133
Damien Miller4f9f42a2003-05-10 19:28:02 +1000134static int
135pthread_cancel(sp_pthread_t thread)
136{
Darren Tucker7ae09622004-01-14 23:07:56 +1100137 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000138 return (kill(thread, SIGTERM));
139}
140
141static int
142pthread_join(sp_pthread_t thread, void **value __unused)
143{
144 int status;
145
Darren Tucker749bc952004-01-14 22:14:04 +1100146 if (sshpam_thread_status != -1)
147 return (sshpam_thread_status);
148 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000149 waitpid(thread, &status, 0);
150 return (status);
151}
152#endif
153
154
Damien Miller5c3a5582003-09-23 22:12:38 +1000155static pam_handle_t *sshpam_handle = NULL;
156static int sshpam_err = 0;
157static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000158static int sshpam_session_open = 0;
159static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100160static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100161static char **sshpam_env = NULL;
Darren Tucker1921ed92004-02-10 13:23:28 +1100162static int *force_pwchange;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000163
Damien Millerc756e9b2003-11-17 21:41:42 +1100164/* Some PAM implementations don't implement this */
165#ifndef HAVE_PAM_GETENVLIST
166static char **
167pam_getenvlist(pam_handle_t *pamh)
168{
169 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100170 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100171 * for platforms without pam_getenvlist by searching for known
172 * env vars (e.g. KRB5CCNAME) from the PAM environment.
173 */
174 return NULL;
175}
176#endif
177
Darren Tucker07705c72003-12-18 15:34:31 +1100178void
179pam_password_change_required(int reqd)
180{
Darren Tuckera8df9242004-01-15 00:15:07 +1100181 debug3("%s %d", __func__, reqd);
Darren Tucker1921ed92004-02-10 13:23:28 +1100182 *force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100183 if (reqd) {
184 no_port_forwarding_flag |= 2;
185 no_agent_forwarding_flag |= 2;
186 no_x11_forwarding_flag |= 2;
187 } else {
188 no_port_forwarding_flag &= ~2;
189 no_agent_forwarding_flag &= ~2;
190 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100191 }
192}
Darren Tucker1921ed92004-02-10 13:23:28 +1100193
Damien Millerc756e9b2003-11-17 21:41:42 +1100194/* Import regular and PAM environment from subprocess */
195static void
196import_environments(Buffer *b)
197{
198 char *env;
199 u_int i, num_env;
200 int err;
201
Darren Tuckera8df9242004-01-15 00:15:07 +1100202 debug3("PAM: %s entering", __func__);
203
Darren Tucker4b385d42004-03-04 19:54:10 +1100204#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100205 /* Import variables set by do_pam_account */
206 sshpam_account_status = buffer_get_int(b);
207 pam_password_change_required(buffer_get_int(b));
208
Damien Millerc756e9b2003-11-17 21:41:42 +1100209 /* Import environment from subprocess */
210 num_env = buffer_get_int(b);
211 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
212 debug3("PAM: num env strings %d", num_env);
213 for(i = 0; i < num_env; i++)
214 sshpam_env[i] = buffer_get_string(b, NULL);
215
216 sshpam_env[num_env] = NULL;
217
218 /* Import PAM environment from subprocess */
219 num_env = buffer_get_int(b);
220 debug("PAM: num PAM env strings %d", num_env);
221 for(i = 0; i < num_env; i++) {
222 env = buffer_get_string(b, NULL);
223
Darren Tucker8a1624c2003-11-18 12:45:35 +1100224#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100225 /* Errors are not fatal here */
226 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
227 error("PAM: pam_putenv: %s",
228 pam_strerror(sshpam_handle, sshpam_err));
229 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100230#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100231 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100232#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100233}
234
Damien Miller9d5705a2000-09-16 16:09:27 +1100235/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000236 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100237 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000238static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000239sshpam_thread_conv(int n, const struct pam_message **msg,
240 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100241{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000242 Buffer buffer;
243 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000244 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000245 int i;
246
Darren Tuckerba53b832004-02-17 20:46:59 +1100247 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000248 *resp = NULL;
249
Damien Miller4f9f42a2003-05-10 19:28:02 +1000250 ctxt = data;
251 if (n <= 0 || n > PAM_MAX_NUM_MSG)
252 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000253
254 if ((reply = malloc(n * sizeof(*reply))) == NULL)
255 return (PAM_CONV_ERR);
256 memset(reply, 0, n * sizeof(*reply));
257
Damien Miller4f9f42a2003-05-10 19:28:02 +1000258 buffer_init(&buffer);
259 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000260 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
261 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100262 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000263 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100264 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100265 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
266 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100267 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100268 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000269 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
270 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000271 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000272 break;
273 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100274 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000275 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100276 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100277 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
278 goto fail;
279 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
280 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000281 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
282 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000283 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000284 break;
285 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100286 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000287 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100288 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100289 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
290 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000291 break;
292 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100293 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000294 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100295 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100296 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
297 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000298 break;
299 default:
300 goto fail;
301 }
302 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000303 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000304 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000305 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000306 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000307
Damien Miller4f9f42a2003-05-10 19:28:02 +1000308 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000309 for(i = 0; i < n; i++) {
310 if (reply[i].resp != NULL)
311 xfree(reply[i].resp);
312 }
313 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000314 buffer_free(&buffer);
315 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000316}
317
Damien Miller4f9f42a2003-05-10 19:28:02 +1000318/*
319 * Authentication thread.
320 */
321static void *
322sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100323{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000324 struct pam_ctxt *ctxt = ctxtp;
325 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000326 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000327#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100328 extern char **environ;
329 char **env_from_pam;
330 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000331 const char *pam_user;
332
333 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
334 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100335 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000336#endif
337
Damien Millerf4b6f102003-09-02 23:12:06 +1000338 sshpam_conv.conv = sshpam_thread_conv;
339 sshpam_conv.appdata_ptr = ctxt;
340
Damien Miller4f9f42a2003-05-10 19:28:02 +1000341 buffer_init(&buffer);
342 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
343 (const void *)&sshpam_conv);
344 if (sshpam_err != PAM_SUCCESS)
345 goto auth_fail;
346 sshpam_err = pam_authenticate(sshpam_handle, 0);
347 if (sshpam_err != PAM_SUCCESS)
348 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100349
Darren Tuckerc376c862003-12-18 16:08:59 +1100350 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100351 if (!do_pam_account())
352 goto auth_fail;
Darren Tucker1921ed92004-02-10 13:23:28 +1100353 if (*force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100354 sshpam_err = pam_chauthtok(sshpam_handle,
355 PAM_CHANGE_EXPIRED_AUTHTOK);
356 if (sshpam_err != PAM_SUCCESS)
357 goto auth_fail;
358 pam_password_change_required(0);
359 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100360 }
Darren Tucker07705c72003-12-18 15:34:31 +1100361
Damien Miller4f9f42a2003-05-10 19:28:02 +1000362 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100363
364#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100365 /* Export variables set by do_pam_account */
366 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker1921ed92004-02-10 13:23:28 +1100367 buffer_put_int(&buffer, *force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100368
Damien Millerc756e9b2003-11-17 21:41:42 +1100369 /* Export any environment strings set in child */
370 for(i = 0; environ[i] != NULL; i++)
371 ; /* Count */
372 buffer_put_int(&buffer, i);
373 for(i = 0; environ[i] != NULL; i++)
374 buffer_put_cstring(&buffer, environ[i]);
375
376 /* Export any environment strings set by PAM in child */
377 env_from_pam = pam_getenvlist(sshpam_handle);
378 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
379 ; /* Count */
380 buffer_put_int(&buffer, i);
381 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
382 buffer_put_cstring(&buffer, env_from_pam[i]);
383#endif /* USE_POSIX_THREADS */
384
Damien Miller9bdba702003-11-17 21:27:55 +1100385 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000386 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
387 buffer_free(&buffer);
388 pthread_exit(NULL);
389
390 auth_fail:
391 buffer_put_cstring(&buffer,
392 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100393 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000394 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
395 buffer_free(&buffer);
396 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100397
Damien Miller4f9f42a2003-05-10 19:28:02 +1000398 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000399}
400
Darren Tucker8846a072003-10-07 11:30:15 +1000401void
402sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000403{
Darren Tucker8846a072003-10-07 11:30:15 +1000404 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000405
Darren Tuckera8df9242004-01-15 00:15:07 +1100406 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000407 if (ctxt != NULL && ctxt->pam_thread != 0) {
408 pthread_cancel(ctxt->pam_thread);
409 pthread_join(ctxt->pam_thread, NULL);
410 close(ctxt->pam_psock);
411 close(ctxt->pam_csock);
412 memset(ctxt, 0, sizeof(*ctxt));
413 cleanup_ctxt = NULL;
414 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000415}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000416
Damien Miller4f9f42a2003-05-10 19:28:02 +1000417static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000418sshpam_null_conv(int n, const struct pam_message **msg,
419 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000420{
Darren Tuckerba53b832004-02-17 20:46:59 +1100421 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000422 return (PAM_CONV_ERR);
423}
Damien Miller63dc3e92001-02-07 12:58:33 +1100424
Damien Miller4f9f42a2003-05-10 19:28:02 +1000425static struct pam_conv null_conv = { sshpam_null_conv, NULL };
426
Darren Tucker8846a072003-10-07 11:30:15 +1000427void
428sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000429{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000430 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000431 if (sshpam_handle == NULL)
432 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000433 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
434 if (sshpam_cred_established) {
435 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
436 sshpam_cred_established = 0;
437 }
438 if (sshpam_session_open) {
439 pam_close_session(sshpam_handle, PAM_SILENT);
440 sshpam_session_open = 0;
441 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100442 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000443 pam_end(sshpam_handle, sshpam_err);
444 sshpam_handle = NULL;
445}
446
447static int
448sshpam_init(const char *user)
449{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000450 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000451 extern char *__progname;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000452 const char *pam_rhost, *pam_user;
453
454 if (sshpam_handle != NULL) {
455 /* We already have a PAM context; check if the user matches */
456 sshpam_err = pam_get_item(sshpam_handle,
457 PAM_USER, (const void **)&pam_user);
458 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
459 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000460 pam_end(sshpam_handle, sshpam_err);
461 sshpam_handle = NULL;
462 }
463 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000464 sshpam_err =
465 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000466 if (sshpam_err != PAM_SUCCESS) {
467 pam_end(sshpam_handle, sshpam_err);
468 sshpam_handle = NULL;
469 return (-1);
470 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000471 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000472 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000473 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
474 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000475 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000476 sshpam_handle = NULL;
477 return (-1);
478 }
479#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100480 /*
481 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
482 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000483 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100484 */
Damien Miller25d93422003-05-18 20:45:47 +1000485 debug("PAM: setting PAM_TTY to \"ssh\"");
486 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
487 if (sshpam_err != PAM_SUCCESS) {
488 pam_end(sshpam_handle, sshpam_err);
489 sshpam_handle = NULL;
490 return (-1);
491 }
492#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000493 return (0);
494}
495
496static void *
497sshpam_init_ctx(Authctxt *authctxt)
498{
499 struct pam_ctxt *ctxt;
500 int socks[2];
501
Darren Tuckera8df9242004-01-15 00:15:07 +1100502 debug3("PAM: %s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000503 /* Refuse to start if we don't have PAM enabled */
504 if (!options.use_pam)
505 return NULL;
506
Damien Miller4f9f42a2003-05-10 19:28:02 +1000507 /* Initialize PAM */
508 if (sshpam_init(authctxt->user) == -1) {
509 error("PAM: initialization failed");
510 return (NULL);
511 }
512
513 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000514 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000515
Darren Tucker1921ed92004-02-10 13:23:28 +1100516 force_pwchange = &(authctxt->force_pwchange);
517
Damien Miller4f9f42a2003-05-10 19:28:02 +1000518 /* Start the authentication thread */
519 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
520 error("PAM: failed create sockets: %s", strerror(errno));
521 xfree(ctxt);
522 return (NULL);
523 }
524 ctxt->pam_psock = socks[0];
525 ctxt->pam_csock = socks[1];
526 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
527 error("PAM: failed to start authentication thread: %s",
528 strerror(errno));
529 close(socks[0]);
530 close(socks[1]);
531 xfree(ctxt);
532 return (NULL);
533 }
Darren Tucker8846a072003-10-07 11:30:15 +1000534 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000535 return (ctxt);
536}
537
538static int
539sshpam_query(void *ctx, char **name, char **info,
540 u_int *num, char ***prompts, u_int **echo_on)
541{
542 Buffer buffer;
543 struct pam_ctxt *ctxt = ctx;
544 size_t plen;
545 u_char type;
546 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000547 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000548
Darren Tuckera8df9242004-01-15 00:15:07 +1100549 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000550 buffer_init(&buffer);
551 *name = xstrdup("");
552 *info = xstrdup("");
553 *prompts = xmalloc(sizeof(char *));
554 **prompts = NULL;
555 plen = 0;
556 *echo_on = xmalloc(sizeof(u_int));
557 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
558 type = buffer_get_char(&buffer);
559 msg = buffer_get_string(&buffer, NULL);
560 switch (type) {
561 case PAM_PROMPT_ECHO_ON:
562 case PAM_PROMPT_ECHO_OFF:
563 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000564 len = plen + strlen(msg) + 1;
565 **prompts = xrealloc(**prompts, len);
566 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000567 **echo_on = (type == PAM_PROMPT_ECHO_ON);
568 xfree(msg);
569 return (0);
570 case PAM_ERROR_MSG:
571 case PAM_TEXT_INFO:
572 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100573 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000574 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100575 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000576 xfree(msg);
577 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000578 case PAM_SUCCESS:
579 case PAM_AUTH_ERR:
580 if (**prompts != NULL) {
581 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100582 debug("PAM: %s", **prompts);
583 buffer_append(&loginmsg, **prompts,
584 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000585 xfree(**prompts);
586 **prompts = NULL;
587 }
588 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100589 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000590 *num = 0;
591 **echo_on = 0;
592 ctxt->pam_done = 1;
593 xfree(msg);
594 return (0);
595 }
596 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000597 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000598 default:
599 *num = 0;
600 **echo_on = 0;
601 xfree(msg);
602 ctxt->pam_done = -1;
603 return (-1);
604 }
605 }
606 return (-1);
607}
608
609/* XXX - see also comment in auth-chall.c:verify_response */
610static int
611sshpam_respond(void *ctx, u_int num, char **resp)
612{
613 Buffer buffer;
614 struct pam_ctxt *ctxt = ctx;
615
Darren Tuckera8df9242004-01-15 00:15:07 +1100616 debug2("PAM: %s entering, %d responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000617 switch (ctxt->pam_done) {
618 case 1:
619 sshpam_authenticated = 1;
620 return (0);
621 case 0:
622 break;
623 default:
624 return (-1);
625 }
626 if (num != 1) {
627 error("PAM: expected one response, got %u", num);
628 return (-1);
629 }
630 buffer_init(&buffer);
631 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100632 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
633 buffer_free(&buffer);
634 return (-1);
635 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000636 buffer_free(&buffer);
637 return (1);
638}
639
640static void
641sshpam_free_ctx(void *ctxtp)
642{
643 struct pam_ctxt *ctxt = ctxtp;
644
Darren Tuckera8df9242004-01-15 00:15:07 +1100645 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000646 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000647 xfree(ctxt);
648 /*
649 * We don't call sshpam_cleanup() here because we may need the PAM
650 * handle at a later stage, e.g. when setting up a session. It's
651 * still on the cleanup list, so pam_end() *will* be called before
652 * the server process terminates.
653 */
654}
655
656KbdintDevice sshpam_device = {
657 "pam",
658 sshpam_init_ctx,
659 sshpam_query,
660 sshpam_respond,
661 sshpam_free_ctx
662};
663
664KbdintDevice mm_sshpam_device = {
665 "pam",
666 mm_sshpam_init_ctx,
667 mm_sshpam_query,
668 mm_sshpam_respond,
669 mm_sshpam_free_ctx
670};
671
672/*
673 * This replaces auth-pam.c
674 */
675void
676start_pam(const char *user)
677{
Damien Miller9d507da2003-05-14 15:31:12 +1000678 if (!options.use_pam)
679 fatal("PAM: initialisation requested when UsePAM=no");
680
Damien Miller4f9f42a2003-05-10 19:28:02 +1000681 if (sshpam_init(user) == -1)
682 fatal("PAM: initialisation failed");
683}
684
685void
686finish_pam(void)
687{
Darren Tucker8846a072003-10-07 11:30:15 +1000688 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000689}
690
Damien Miller1f499fd2003-08-25 13:08:49 +1000691u_int
692do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000693{
Darren Tucker07705c72003-12-18 15:34:31 +1100694 if (sshpam_account_status != -1)
695 return (sshpam_account_status);
696
Damien Miller1f499fd2003-08-25 13:08:49 +1000697 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tuckera8df9242004-01-15 00:15:07 +1100698 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100699
700 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
701 sshpam_account_status = 0;
702 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000703 }
704
Darren Tucker07705c72003-12-18 15:34:31 +1100705 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
706 pam_password_change_required(1);
707
708 sshpam_account_status = 1;
709 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000710}
711
712void
Damien Miller341c6e62003-09-02 23:18:52 +1000713do_pam_set_tty(const char *tty)
714{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000715 if (tty != NULL) {
716 debug("PAM: setting PAM_TTY to \"%s\"", tty);
717 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
718 if (sshpam_err != PAM_SUCCESS)
719 fatal("PAM: failed to set PAM_TTY: %s",
720 pam_strerror(sshpam_handle, sshpam_err));
721 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000722}
723
724void
725do_pam_setcred(int init)
726{
727 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
728 (const void *)&null_conv);
729 if (sshpam_err != PAM_SUCCESS)
730 fatal("PAM: failed to set PAM_CONV: %s",
731 pam_strerror(sshpam_handle, sshpam_err));
732 if (init) {
733 debug("PAM: establishing credentials");
734 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
735 } else {
736 debug("PAM: reinitializing credentials");
737 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
738 }
739 if (sshpam_err == PAM_SUCCESS) {
740 sshpam_cred_established = 1;
741 return;
742 }
743 if (sshpam_authenticated)
744 fatal("PAM: pam_setcred(): %s",
745 pam_strerror(sshpam_handle, sshpam_err));
746 else
747 debug("PAM: pam_setcred(): %s",
748 pam_strerror(sshpam_handle, sshpam_err));
749}
750
Damien Miller4f9f42a2003-05-10 19:28:02 +1000751static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100752pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000753 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000754{
755 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000756 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000757 int i;
758
Darren Tuckerba53b832004-02-17 20:46:59 +1100759 debug3("PAM: %s called with %d messages", __func__, n);
760
Damien Miller5c3a5582003-09-23 22:12:38 +1000761 *resp = NULL;
762
Darren Tucker18df00c2003-11-18 12:42:07 +1100763 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000764 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000765
766 if ((reply = malloc(n * sizeof(*reply))) == NULL)
767 return (PAM_CONV_ERR);
768 memset(reply, 0, n * sizeof(*reply));
769
Damien Miller4f9f42a2003-05-10 19:28:02 +1000770 for (i = 0; i < n; ++i) {
771 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
772 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000773 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100774 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000775 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000776 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000777 break;
778 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100779 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000780 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000781 reply[i].resp = xstrdup(input);
782 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000783 break;
784 case PAM_ERROR_MSG:
785 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100786 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000787 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000788 break;
789 default:
790 goto fail;
791 }
792 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000793 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000794 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000795
Damien Miller4f9f42a2003-05-10 19:28:02 +1000796 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000797 for(i = 0; i < n; i++) {
798 if (reply[i].resp != NULL)
799 xfree(reply[i].resp);
800 }
801 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000802 return (PAM_CONV_ERR);
803}
804
Darren Tucker18df00c2003-11-18 12:42:07 +1100805static struct pam_conv tty_conv = { pam_tty_conv, NULL };
806
Damien Miller4f9f42a2003-05-10 19:28:02 +1000807/*
808 * XXX this should be done in the authentication phase, but ssh1 doesn't
809 * support that
810 */
811void
812do_pam_chauthtok(void)
813{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000814 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000815 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000816 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100817 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000818 if (sshpam_err != PAM_SUCCESS)
819 fatal("PAM: failed to set PAM_CONV: %s",
820 pam_strerror(sshpam_handle, sshpam_err));
821 debug("PAM: changing password");
822 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
823 if (sshpam_err != PAM_SUCCESS)
824 fatal("PAM: pam_chauthtok(): %s",
825 pam_strerror(sshpam_handle, sshpam_err));
826}
827
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100828static int
829pam_store_conv(int n, const struct pam_message **msg,
830 struct pam_response **resp, void *data)
831{
832 struct pam_response *reply;
833 int i;
834 size_t len;
835
836 debug3("PAM: %s called with %d messages", __func__, n);
837 *resp = NULL;
838
839 if (n <= 0 || n > PAM_MAX_NUM_MSG)
840 return (PAM_CONV_ERR);
841
842 if ((reply = malloc(n * sizeof(*reply))) == NULL)
843 return (PAM_CONV_ERR);
844 memset(reply, 0, n * sizeof(*reply));
845
846 for (i = 0; i < n; ++i) {
847 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
848 case PAM_ERROR_MSG:
849 case PAM_TEXT_INFO:
850 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
851 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
852 buffer_append(&loginmsg, "\n", 1 );
853 reply[i].resp_retcode = PAM_SUCCESS;
854 break;
855 default:
856 goto fail;
857 }
858 }
859 *resp = reply;
860 return (PAM_SUCCESS);
861
862 fail:
863 for(i = 0; i < n; i++) {
864 if (reply[i].resp != NULL)
865 xfree(reply[i].resp);
866 }
867 xfree(reply);
868 return (PAM_CONV_ERR);
869}
870
871static struct pam_conv store_conv = { pam_store_conv, NULL };
872
Darren Tucker18df00c2003-11-18 12:42:07 +1100873void
874do_pam_session(void)
875{
Darren Tucker1921ed92004-02-10 13:23:28 +1100876 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +1100877 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100878 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +1100879 if (sshpam_err != PAM_SUCCESS)
880 fatal("PAM: failed to set PAM_CONV: %s",
881 pam_strerror(sshpam_handle, sshpam_err));
882 sshpam_err = pam_open_session(sshpam_handle, 0);
883 if (sshpam_err != PAM_SUCCESS)
884 fatal("PAM: pam_open_session(): %s",
885 pam_strerror(sshpam_handle, sshpam_err));
886 sshpam_session_open = 1;
887}
888
Damien Millera8e06ce2003-11-21 23:48:55 +1100889/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000890 * Set a PAM environment string. We need to do this so that the session
891 * modules can handle things like Kerberos/GSI credentials that appear
892 * during the ssh authentication process.
893 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000894int
Damien Millera8e06ce2003-11-21 23:48:55 +1100895do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000896{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000897 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100898#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000899 char *compound;
900 size_t len;
901
902 len = strlen(name) + strlen(value) + 2;
903 compound = xmalloc(len);
904
905 snprintf(compound, len, "%s=%s", name, value);
906 ret = pam_putenv(sshpam_handle, compound);
907 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000908#endif
Damien Millerf2728092003-09-17 07:24:25 +1000909
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000910 return (ret);
911}
912
Damien Miller4f9f42a2003-05-10 19:28:02 +1000913char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100914fetch_pam_child_environment(void)
915{
916 return sshpam_env;
917}
918
919char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000920fetch_pam_environment(void)
921{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000922 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000923}
924
925void
926free_pam_environment(char **env)
927{
928 char **envp;
929
Damien Millere27c6cc2003-05-16 18:21:01 +1000930 if (env == NULL)
931 return;
932
Damien Miller4f9f42a2003-05-10 19:28:02 +1000933 for (envp = env; *envp; envp++)
934 xfree(*envp);
935 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100936}
937
938#endif /* USE_PAM */