blob: fe2ae77116f66fab3622d5ad8fafb994b63e84a0 [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 Tucker1b27c8f2004-01-13 22:35:58 +110034RCSID("$Id: auth-pam.c,v 1.89 2004/01/13 11:35:59 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
Damien Miller4f9f42a2003-05-10 19:28:02 +100089static void
90pthread_exit(void *value __unused)
91{
92 _exit(0);
93}
Damien Miller2f6a0ad2000-05-31 11:20:11 +100094
Damien Miller4f9f42a2003-05-10 19:28:02 +100095static int
96pthread_create(sp_pthread_t *thread, const void *attr __unused,
97 void *(*thread_start)(void *), void *arg)
98{
99 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +1100100
Damien Miller4f9f42a2003-05-10 19:28:02 +1000101 switch ((pid = fork())) {
102 case -1:
103 error("fork(): %s", strerror(errno));
104 return (-1);
105 case 0:
106 thread_start(arg);
107 _exit(1);
108 default:
109 *thread = pid;
110 return (0);
111 }
112}
Damien Millere72b7af1999-12-30 15:08:44 +1100113
Damien Miller4f9f42a2003-05-10 19:28:02 +1000114static int
115pthread_cancel(sp_pthread_t thread)
116{
117 return (kill(thread, SIGTERM));
118}
119
120static int
121pthread_join(sp_pthread_t thread, void **value __unused)
122{
123 int status;
124
125 waitpid(thread, &status, 0);
126 return (status);
127}
128#endif
129
130
Damien Miller5c3a5582003-09-23 22:12:38 +1000131static pam_handle_t *sshpam_handle = NULL;
132static int sshpam_err = 0;
133static int sshpam_authenticated = 0;
134static int sshpam_new_authtok_reqd = 0;
135static int sshpam_session_open = 0;
136static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100137static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100138static char **sshpam_env = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000139
Damien Millerc756e9b2003-11-17 21:41:42 +1100140/* Some PAM implementations don't implement this */
141#ifndef HAVE_PAM_GETENVLIST
142static char **
143pam_getenvlist(pam_handle_t *pamh)
144{
145 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100146 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100147 * for platforms without pam_getenvlist by searching for known
148 * env vars (e.g. KRB5CCNAME) from the PAM environment.
149 */
150 return NULL;
151}
152#endif
153
Darren Tucker07705c72003-12-18 15:34:31 +1100154void
155pam_password_change_required(int reqd)
156{
157 sshpam_new_authtok_reqd = reqd;
158 if (reqd) {
159 no_port_forwarding_flag |= 2;
160 no_agent_forwarding_flag |= 2;
161 no_x11_forwarding_flag |= 2;
162 } else {
163 no_port_forwarding_flag &= ~2;
164 no_agent_forwarding_flag &= ~2;
165 no_x11_forwarding_flag &= ~2;
166
167 }
168}
Damien Millerc756e9b2003-11-17 21:41:42 +1100169/* Import regular and PAM environment from subprocess */
170static void
171import_environments(Buffer *b)
172{
173 char *env;
174 u_int i, num_env;
175 int err;
176
Darren Tucker07705c72003-12-18 15:34:31 +1100177 /* Import variables set by do_pam_account */
178 sshpam_account_status = buffer_get_int(b);
179 pam_password_change_required(buffer_get_int(b));
180
Damien Millerc756e9b2003-11-17 21:41:42 +1100181 /* Import environment from subprocess */
182 num_env = buffer_get_int(b);
183 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
184 debug3("PAM: num env strings %d", num_env);
185 for(i = 0; i < num_env; i++)
186 sshpam_env[i] = buffer_get_string(b, NULL);
187
188 sshpam_env[num_env] = NULL;
189
190 /* Import PAM environment from subprocess */
191 num_env = buffer_get_int(b);
192 debug("PAM: num PAM env strings %d", num_env);
193 for(i = 0; i < num_env; i++) {
194 env = buffer_get_string(b, NULL);
195
Darren Tucker8a1624c2003-11-18 12:45:35 +1100196#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100197 /* Errors are not fatal here */
198 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
199 error("PAM: pam_putenv: %s",
200 pam_strerror(sshpam_handle, sshpam_err));
201 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100202#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100203 }
204}
205
Damien Miller9d5705a2000-09-16 16:09:27 +1100206/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000207 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100208 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000209static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000210sshpam_thread_conv(int n, const struct pam_message **msg,
211 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100212{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213 Buffer buffer;
214 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000215 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000216 int i;
217
Damien Miller5c3a5582003-09-23 22:12:38 +1000218 *resp = NULL;
219
Damien Miller4f9f42a2003-05-10 19:28:02 +1000220 ctxt = data;
221 if (n <= 0 || n > PAM_MAX_NUM_MSG)
222 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000223
224 if ((reply = malloc(n * sizeof(*reply))) == NULL)
225 return (PAM_CONV_ERR);
226 memset(reply, 0, n * sizeof(*reply));
227
Damien Miller4f9f42a2003-05-10 19:28:02 +1000228 buffer_init(&buffer);
229 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000230 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
231 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100232 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000233 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100234 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100235 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
236 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100237 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100238 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000239 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
240 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000241 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000242 break;
243 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100244 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000245 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100246 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100247 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
248 goto fail;
249 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
250 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000251 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
252 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000253 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000254 break;
255 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100256 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000257 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100258 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100259 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
260 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000261 break;
262 case PAM_TEXT_INFO:
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 Miller4f9f42a2003-05-10 19:28:02 +1000268 break;
269 default:
270 goto fail;
271 }
272 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000273 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000274 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000275 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000276 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000277
Damien Miller4f9f42a2003-05-10 19:28:02 +1000278 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000279 for(i = 0; i < n; i++) {
280 if (reply[i].resp != NULL)
281 xfree(reply[i].resp);
282 }
283 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000284 buffer_free(&buffer);
285 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000286}
287
Damien Miller4f9f42a2003-05-10 19:28:02 +1000288/*
289 * Authentication thread.
290 */
291static void *
292sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100293{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000294 struct pam_ctxt *ctxt = ctxtp;
295 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000296 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000297#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100298 extern char **environ;
299 char **env_from_pam;
300 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000301 const char *pam_user;
302
303 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
304 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100305 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000306#endif
307
Damien Millerf4b6f102003-09-02 23:12:06 +1000308 sshpam_conv.conv = sshpam_thread_conv;
309 sshpam_conv.appdata_ptr = ctxt;
310
Damien Miller4f9f42a2003-05-10 19:28:02 +1000311 buffer_init(&buffer);
312 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
313 (const void *)&sshpam_conv);
314 if (sshpam_err != PAM_SUCCESS)
315 goto auth_fail;
316 sshpam_err = pam_authenticate(sshpam_handle, 0);
317 if (sshpam_err != PAM_SUCCESS)
318 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100319
Darren Tuckerc376c862003-12-18 16:08:59 +1100320 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100321 if (!do_pam_account())
322 goto auth_fail;
323 if (sshpam_new_authtok_reqd) {
324 sshpam_err = pam_chauthtok(sshpam_handle,
325 PAM_CHANGE_EXPIRED_AUTHTOK);
326 if (sshpam_err != PAM_SUCCESS)
327 goto auth_fail;
328 pam_password_change_required(0);
329 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100330 }
Darren Tucker07705c72003-12-18 15:34:31 +1100331
Damien Miller4f9f42a2003-05-10 19:28:02 +1000332 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100333
334#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100335 /* Export variables set by do_pam_account */
336 buffer_put_int(&buffer, sshpam_account_status);
337 buffer_put_int(&buffer, sshpam_new_authtok_reqd);
338
Damien Millerc756e9b2003-11-17 21:41:42 +1100339 /* Export any environment strings set in child */
340 for(i = 0; environ[i] != NULL; i++)
341 ; /* Count */
342 buffer_put_int(&buffer, i);
343 for(i = 0; environ[i] != NULL; i++)
344 buffer_put_cstring(&buffer, environ[i]);
345
346 /* Export any environment strings set by PAM in child */
347 env_from_pam = pam_getenvlist(sshpam_handle);
348 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
349 ; /* Count */
350 buffer_put_int(&buffer, i);
351 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
352 buffer_put_cstring(&buffer, env_from_pam[i]);
353#endif /* USE_POSIX_THREADS */
354
Damien Miller9bdba702003-11-17 21:27:55 +1100355 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000356 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
357 buffer_free(&buffer);
358 pthread_exit(NULL);
359
360 auth_fail:
361 buffer_put_cstring(&buffer,
362 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100363 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000364 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
365 buffer_free(&buffer);
366 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100367
Damien Miller4f9f42a2003-05-10 19:28:02 +1000368 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000369}
370
Darren Tucker8846a072003-10-07 11:30:15 +1000371void
372sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000373{
Darren Tucker8846a072003-10-07 11:30:15 +1000374 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000375
Darren Tucker8846a072003-10-07 11:30:15 +1000376 if (ctxt != NULL && ctxt->pam_thread != 0) {
377 pthread_cancel(ctxt->pam_thread);
378 pthread_join(ctxt->pam_thread, NULL);
379 close(ctxt->pam_psock);
380 close(ctxt->pam_csock);
381 memset(ctxt, 0, sizeof(*ctxt));
382 cleanup_ctxt = NULL;
383 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000384}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000385
Damien Miller4f9f42a2003-05-10 19:28:02 +1000386static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000387sshpam_null_conv(int n, const struct pam_message **msg,
388 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000389{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000390 return (PAM_CONV_ERR);
391}
Damien Miller63dc3e92001-02-07 12:58:33 +1100392
Damien Miller4f9f42a2003-05-10 19:28:02 +1000393static struct pam_conv null_conv = { sshpam_null_conv, NULL };
394
Darren Tucker8846a072003-10-07 11:30:15 +1000395void
396sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000397{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000398 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000399 if (sshpam_handle == NULL)
400 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000401 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
402 if (sshpam_cred_established) {
403 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
404 sshpam_cred_established = 0;
405 }
406 if (sshpam_session_open) {
407 pam_close_session(sshpam_handle, PAM_SILENT);
408 sshpam_session_open = 0;
409 }
410 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
411 pam_end(sshpam_handle, sshpam_err);
412 sshpam_handle = NULL;
413}
414
415static int
416sshpam_init(const char *user)
417{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000418 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000419 extern char *__progname;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000420 const char *pam_rhost, *pam_user;
421
422 if (sshpam_handle != NULL) {
423 /* We already have a PAM context; check if the user matches */
424 sshpam_err = pam_get_item(sshpam_handle,
425 PAM_USER, (const void **)&pam_user);
426 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
427 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000428 pam_end(sshpam_handle, sshpam_err);
429 sshpam_handle = NULL;
430 }
431 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000432 sshpam_err =
433 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000434 if (sshpam_err != PAM_SUCCESS) {
435 pam_end(sshpam_handle, sshpam_err);
436 sshpam_handle = NULL;
437 return (-1);
438 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000439 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000440 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000441 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
442 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000443 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000444 sshpam_handle = NULL;
445 return (-1);
446 }
447#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100448 /*
449 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
450 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000451 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100452 */
Damien Miller25d93422003-05-18 20:45:47 +1000453 debug("PAM: setting PAM_TTY to \"ssh\"");
454 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
455 if (sshpam_err != PAM_SUCCESS) {
456 pam_end(sshpam_handle, sshpam_err);
457 sshpam_handle = NULL;
458 return (-1);
459 }
460#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000461 return (0);
462}
463
464static void *
465sshpam_init_ctx(Authctxt *authctxt)
466{
467 struct pam_ctxt *ctxt;
468 int socks[2];
469
Damien Miller4e448a32003-05-14 15:11:48 +1000470 /* Refuse to start if we don't have PAM enabled */
471 if (!options.use_pam)
472 return NULL;
473
Damien Miller4f9f42a2003-05-10 19:28:02 +1000474 /* Initialize PAM */
475 if (sshpam_init(authctxt->user) == -1) {
476 error("PAM: initialization failed");
477 return (NULL);
478 }
479
480 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000481 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000482
483 /* Start the authentication thread */
484 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
485 error("PAM: failed create sockets: %s", strerror(errno));
486 xfree(ctxt);
487 return (NULL);
488 }
489 ctxt->pam_psock = socks[0];
490 ctxt->pam_csock = socks[1];
491 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
492 error("PAM: failed to start authentication thread: %s",
493 strerror(errno));
494 close(socks[0]);
495 close(socks[1]);
496 xfree(ctxt);
497 return (NULL);
498 }
Darren Tucker8846a072003-10-07 11:30:15 +1000499 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000500 return (ctxt);
501}
502
503static int
504sshpam_query(void *ctx, char **name, char **info,
505 u_int *num, char ***prompts, u_int **echo_on)
506{
507 Buffer buffer;
508 struct pam_ctxt *ctxt = ctx;
509 size_t plen;
510 u_char type;
511 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000512 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000513
514 buffer_init(&buffer);
515 *name = xstrdup("");
516 *info = xstrdup("");
517 *prompts = xmalloc(sizeof(char *));
518 **prompts = NULL;
519 plen = 0;
520 *echo_on = xmalloc(sizeof(u_int));
521 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
522 type = buffer_get_char(&buffer);
523 msg = buffer_get_string(&buffer, NULL);
524 switch (type) {
525 case PAM_PROMPT_ECHO_ON:
526 case PAM_PROMPT_ECHO_OFF:
527 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000528 len = plen + strlen(msg) + 1;
529 **prompts = xrealloc(**prompts, len);
530 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000531 **echo_on = (type == PAM_PROMPT_ECHO_ON);
532 xfree(msg);
533 return (0);
534 case PAM_ERROR_MSG:
535 case PAM_TEXT_INFO:
536 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100537 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000538 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100539 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000540 xfree(msg);
541 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000542 case PAM_SUCCESS:
543 case PAM_AUTH_ERR:
544 if (**prompts != NULL) {
545 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100546 debug("PAM: %s", **prompts);
547 buffer_append(&loginmsg, **prompts,
548 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000549 xfree(**prompts);
550 **prompts = NULL;
551 }
552 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100553 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000554 *num = 0;
555 **echo_on = 0;
556 ctxt->pam_done = 1;
557 xfree(msg);
558 return (0);
559 }
560 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000561 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000562 default:
563 *num = 0;
564 **echo_on = 0;
565 xfree(msg);
566 ctxt->pam_done = -1;
567 return (-1);
568 }
569 }
570 return (-1);
571}
572
573/* XXX - see also comment in auth-chall.c:verify_response */
574static int
575sshpam_respond(void *ctx, u_int num, char **resp)
576{
577 Buffer buffer;
578 struct pam_ctxt *ctxt = ctx;
579
580 debug2("PAM: %s", __func__);
581 switch (ctxt->pam_done) {
582 case 1:
583 sshpam_authenticated = 1;
584 return (0);
585 case 0:
586 break;
587 default:
588 return (-1);
589 }
590 if (num != 1) {
591 error("PAM: expected one response, got %u", num);
592 return (-1);
593 }
594 buffer_init(&buffer);
595 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100596 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
597 buffer_free(&buffer);
598 return (-1);
599 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000600 buffer_free(&buffer);
601 return (1);
602}
603
604static void
605sshpam_free_ctx(void *ctxtp)
606{
607 struct pam_ctxt *ctxt = ctxtp;
608
Darren Tucker8846a072003-10-07 11:30:15 +1000609 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000610 xfree(ctxt);
611 /*
612 * We don't call sshpam_cleanup() here because we may need the PAM
613 * handle at a later stage, e.g. when setting up a session. It's
614 * still on the cleanup list, so pam_end() *will* be called before
615 * the server process terminates.
616 */
617}
618
619KbdintDevice sshpam_device = {
620 "pam",
621 sshpam_init_ctx,
622 sshpam_query,
623 sshpam_respond,
624 sshpam_free_ctx
625};
626
627KbdintDevice mm_sshpam_device = {
628 "pam",
629 mm_sshpam_init_ctx,
630 mm_sshpam_query,
631 mm_sshpam_respond,
632 mm_sshpam_free_ctx
633};
634
635/*
636 * This replaces auth-pam.c
637 */
638void
639start_pam(const char *user)
640{
Damien Miller9d507da2003-05-14 15:31:12 +1000641 if (!options.use_pam)
642 fatal("PAM: initialisation requested when UsePAM=no");
643
Damien Miller4f9f42a2003-05-10 19:28:02 +1000644 if (sshpam_init(user) == -1)
645 fatal("PAM: initialisation failed");
646}
647
648void
649finish_pam(void)
650{
Darren Tucker8846a072003-10-07 11:30:15 +1000651 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000652}
653
Damien Miller1f499fd2003-08-25 13:08:49 +1000654u_int
655do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000656{
Darren Tucker07705c72003-12-18 15:34:31 +1100657 if (sshpam_account_status != -1)
658 return (sshpam_account_status);
659
Damien Miller1f499fd2003-08-25 13:08:49 +1000660 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
661 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100662
663 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
664 sshpam_account_status = 0;
665 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000666 }
667
Darren Tucker07705c72003-12-18 15:34:31 +1100668 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
669 pam_password_change_required(1);
670
671 sshpam_account_status = 1;
672 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000673}
674
675void
Damien Miller341c6e62003-09-02 23:18:52 +1000676do_pam_set_tty(const char *tty)
677{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000678 if (tty != NULL) {
679 debug("PAM: setting PAM_TTY to \"%s\"", tty);
680 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
681 if (sshpam_err != PAM_SUCCESS)
682 fatal("PAM: failed to set PAM_TTY: %s",
683 pam_strerror(sshpam_handle, sshpam_err));
684 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000685}
686
687void
688do_pam_setcred(int init)
689{
690 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
691 (const void *)&null_conv);
692 if (sshpam_err != PAM_SUCCESS)
693 fatal("PAM: failed to set PAM_CONV: %s",
694 pam_strerror(sshpam_handle, sshpam_err));
695 if (init) {
696 debug("PAM: establishing credentials");
697 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
698 } else {
699 debug("PAM: reinitializing credentials");
700 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
701 }
702 if (sshpam_err == PAM_SUCCESS) {
703 sshpam_cred_established = 1;
704 return;
705 }
706 if (sshpam_authenticated)
707 fatal("PAM: pam_setcred(): %s",
708 pam_strerror(sshpam_handle, sshpam_err));
709 else
710 debug("PAM: pam_setcred(): %s",
711 pam_strerror(sshpam_handle, sshpam_err));
712}
713
714int
715is_pam_password_change_required(void)
716{
717 return (sshpam_new_authtok_reqd);
718}
719
720static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100721pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000722 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000723{
724 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000725 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000726 int i;
727
Damien Miller5c3a5582003-09-23 22:12:38 +1000728 *resp = NULL;
729
Darren Tucker18df00c2003-11-18 12:42:07 +1100730 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000731 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000732
733 if ((reply = malloc(n * sizeof(*reply))) == NULL)
734 return (PAM_CONV_ERR);
735 memset(reply, 0, n * sizeof(*reply));
736
Damien Miller4f9f42a2003-05-10 19:28:02 +1000737 for (i = 0; i < n; ++i) {
738 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
739 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000740 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100741 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000742 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000743 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000744 break;
745 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100746 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000747 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000748 reply[i].resp = xstrdup(input);
749 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000750 break;
751 case PAM_ERROR_MSG:
752 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100753 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000754 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000755 break;
756 default:
757 goto fail;
758 }
759 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000760 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000761 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000762
Damien Miller4f9f42a2003-05-10 19:28:02 +1000763 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000764 for(i = 0; i < n; i++) {
765 if (reply[i].resp != NULL)
766 xfree(reply[i].resp);
767 }
768 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000769 return (PAM_CONV_ERR);
770}
771
Darren Tucker18df00c2003-11-18 12:42:07 +1100772static struct pam_conv tty_conv = { pam_tty_conv, NULL };
773
Damien Miller4f9f42a2003-05-10 19:28:02 +1000774/*
775 * XXX this should be done in the authentication phase, but ssh1 doesn't
776 * support that
777 */
778void
779do_pam_chauthtok(void)
780{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000781 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000782 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000783 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100784 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000785 if (sshpam_err != PAM_SUCCESS)
786 fatal("PAM: failed to set PAM_CONV: %s",
787 pam_strerror(sshpam_handle, sshpam_err));
788 debug("PAM: changing password");
789 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
790 if (sshpam_err != PAM_SUCCESS)
791 fatal("PAM: pam_chauthtok(): %s",
792 pam_strerror(sshpam_handle, sshpam_err));
793}
794
Darren Tucker18df00c2003-11-18 12:42:07 +1100795void
796do_pam_session(void)
797{
Damien Millera8e06ce2003-11-21 23:48:55 +1100798 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100799 (const void *)&tty_conv);
800 if (sshpam_err != PAM_SUCCESS)
801 fatal("PAM: failed to set PAM_CONV: %s",
802 pam_strerror(sshpam_handle, sshpam_err));
803 sshpam_err = pam_open_session(sshpam_handle, 0);
804 if (sshpam_err != PAM_SUCCESS)
805 fatal("PAM: pam_open_session(): %s",
806 pam_strerror(sshpam_handle, sshpam_err));
807 sshpam_session_open = 1;
808}
809
Damien Millera8e06ce2003-11-21 23:48:55 +1100810/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000811 * Set a PAM environment string. We need to do this so that the session
812 * modules can handle things like Kerberos/GSI credentials that appear
813 * during the ssh authentication process.
814 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000815int
Damien Millera8e06ce2003-11-21 23:48:55 +1100816do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000817{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000818 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100819#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000820 char *compound;
821 size_t len;
822
823 len = strlen(name) + strlen(value) + 2;
824 compound = xmalloc(len);
825
826 snprintf(compound, len, "%s=%s", name, value);
827 ret = pam_putenv(sshpam_handle, compound);
828 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000829#endif
Damien Millerf2728092003-09-17 07:24:25 +1000830
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000831 return (ret);
832}
833
Damien Miller4f9f42a2003-05-10 19:28:02 +1000834void
835print_pam_messages(void)
836{
837 /* XXX */
838}
839
840char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100841fetch_pam_child_environment(void)
842{
843 return sshpam_env;
844}
845
846char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000847fetch_pam_environment(void)
848{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000849 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000850}
851
852void
853free_pam_environment(char **env)
854{
855 char **envp;
856
Damien Millere27c6cc2003-05-16 18:21:01 +1000857 if (env == NULL)
858 return;
859
Damien Miller4f9f42a2003-05-10 19:28:02 +1000860 for (envp = env; *envp; envp++)
861 xfree(*envp);
862 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100863}
864
865#endif /* USE_PAM */