blob: 14d0c7b7f13fde3c240657fdb01d8776998dbadf [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 Tucker749bc952004-01-14 22:14:04 +110034RCSID("$Id: auth-pam.c,v 1.90 2004/01/14 11:14:05 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{
95 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0) == -1)
96 return; /* couldn't wait for process */
97 if (WIFSIGNALED(sshpam_thread_status) &&
98 WTERMSIG(sshpam_thread_status) == SIGTERM)
99 return; /* terminated by pthread_cancel */
100 if (!WIFEXITED(sshpam_thread_status))
101 fatal("PAM: authentication thread exited unexpectedly");
102 if (WEXITSTATUS(sshpam_thread_status) != 0)
103 fatal("PAM: authentication thread exited uncleanly");
104}
105
Damien Miller4f9f42a2003-05-10 19:28:02 +1000106static void
107pthread_exit(void *value __unused)
108{
109 _exit(0);
110}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000111
Damien Miller4f9f42a2003-05-10 19:28:02 +1000112static int
113pthread_create(sp_pthread_t *thread, const void *attr __unused,
114 void *(*thread_start)(void *), void *arg)
115{
116 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +1100117
Damien Miller4f9f42a2003-05-10 19:28:02 +1000118 switch ((pid = fork())) {
119 case -1:
120 error("fork(): %s", strerror(errno));
121 return (-1);
122 case 0:
123 thread_start(arg);
124 _exit(1);
125 default:
126 *thread = pid;
Darren Tucker749bc952004-01-14 22:14:04 +1100127 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000128 return (0);
129 }
130}
Damien Millere72b7af1999-12-30 15:08:44 +1100131
Damien Miller4f9f42a2003-05-10 19:28:02 +1000132static int
133pthread_cancel(sp_pthread_t thread)
134{
135 return (kill(thread, SIGTERM));
136}
137
138static int
139pthread_join(sp_pthread_t thread, void **value __unused)
140{
141 int status;
142
Darren Tucker749bc952004-01-14 22:14:04 +1100143 if (sshpam_thread_status != -1)
144 return (sshpam_thread_status);
145 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000146 waitpid(thread, &status, 0);
147 return (status);
148}
149#endif
150
151
Damien Miller5c3a5582003-09-23 22:12:38 +1000152static pam_handle_t *sshpam_handle = NULL;
153static int sshpam_err = 0;
154static int sshpam_authenticated = 0;
155static int sshpam_new_authtok_reqd = 0;
156static int sshpam_session_open = 0;
157static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100158static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100159static char **sshpam_env = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000160
Damien Millerc756e9b2003-11-17 21:41:42 +1100161/* Some PAM implementations don't implement this */
162#ifndef HAVE_PAM_GETENVLIST
163static char **
164pam_getenvlist(pam_handle_t *pamh)
165{
166 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100167 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100168 * for platforms without pam_getenvlist by searching for known
169 * env vars (e.g. KRB5CCNAME) from the PAM environment.
170 */
171 return NULL;
172}
173#endif
174
Darren Tucker07705c72003-12-18 15:34:31 +1100175void
176pam_password_change_required(int reqd)
177{
178 sshpam_new_authtok_reqd = reqd;
179 if (reqd) {
180 no_port_forwarding_flag |= 2;
181 no_agent_forwarding_flag |= 2;
182 no_x11_forwarding_flag |= 2;
183 } else {
184 no_port_forwarding_flag &= ~2;
185 no_agent_forwarding_flag &= ~2;
186 no_x11_forwarding_flag &= ~2;
187
188 }
189}
Damien Millerc756e9b2003-11-17 21:41:42 +1100190/* Import regular and PAM environment from subprocess */
191static void
192import_environments(Buffer *b)
193{
194 char *env;
195 u_int i, num_env;
196 int err;
197
Darren Tucker07705c72003-12-18 15:34:31 +1100198 /* Import variables set by do_pam_account */
199 sshpam_account_status = buffer_get_int(b);
200 pam_password_change_required(buffer_get_int(b));
201
Damien Millerc756e9b2003-11-17 21:41:42 +1100202 /* Import environment from subprocess */
203 num_env = buffer_get_int(b);
204 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
205 debug3("PAM: num env strings %d", num_env);
206 for(i = 0; i < num_env; i++)
207 sshpam_env[i] = buffer_get_string(b, NULL);
208
209 sshpam_env[num_env] = NULL;
210
211 /* Import PAM environment from subprocess */
212 num_env = buffer_get_int(b);
213 debug("PAM: num PAM env strings %d", num_env);
214 for(i = 0; i < num_env; i++) {
215 env = buffer_get_string(b, NULL);
216
Darren Tucker8a1624c2003-11-18 12:45:35 +1100217#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100218 /* Errors are not fatal here */
219 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
220 error("PAM: pam_putenv: %s",
221 pam_strerror(sshpam_handle, sshpam_err));
222 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100223#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100224 }
225}
226
Damien Miller9d5705a2000-09-16 16:09:27 +1100227/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000228 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100229 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000230static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000231sshpam_thread_conv(int n, const struct pam_message **msg,
232 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100233{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000234 Buffer buffer;
235 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000236 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000237 int i;
238
Damien Miller5c3a5582003-09-23 22:12:38 +1000239 *resp = NULL;
240
Damien Miller4f9f42a2003-05-10 19:28:02 +1000241 ctxt = data;
242 if (n <= 0 || n > PAM_MAX_NUM_MSG)
243 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000244
245 if ((reply = malloc(n * sizeof(*reply))) == NULL)
246 return (PAM_CONV_ERR);
247 memset(reply, 0, n * sizeof(*reply));
248
Damien Miller4f9f42a2003-05-10 19:28:02 +1000249 buffer_init(&buffer);
250 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000251 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
252 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100253 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000254 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100255 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100256 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
257 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100258 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100259 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000260 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
261 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000262 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000263 break;
264 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100265 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000266 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100267 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100268 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
269 goto fail;
270 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
271 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000272 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
273 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000274 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000275 break;
276 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100277 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000278 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100279 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100280 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
281 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000282 break;
283 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100284 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000285 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100286 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100287 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
288 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000289 break;
290 default:
291 goto fail;
292 }
293 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000294 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000295 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000296 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000297 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000298
Damien Miller4f9f42a2003-05-10 19:28:02 +1000299 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000300 for(i = 0; i < n; i++) {
301 if (reply[i].resp != NULL)
302 xfree(reply[i].resp);
303 }
304 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000305 buffer_free(&buffer);
306 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000307}
308
Damien Miller4f9f42a2003-05-10 19:28:02 +1000309/*
310 * Authentication thread.
311 */
312static void *
313sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100314{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000315 struct pam_ctxt *ctxt = ctxtp;
316 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000317 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000318#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100319 extern char **environ;
320 char **env_from_pam;
321 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000322 const char *pam_user;
323
324 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
325 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100326 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000327#endif
328
Damien Millerf4b6f102003-09-02 23:12:06 +1000329 sshpam_conv.conv = sshpam_thread_conv;
330 sshpam_conv.appdata_ptr = ctxt;
331
Damien Miller4f9f42a2003-05-10 19:28:02 +1000332 buffer_init(&buffer);
333 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
334 (const void *)&sshpam_conv);
335 if (sshpam_err != PAM_SUCCESS)
336 goto auth_fail;
337 sshpam_err = pam_authenticate(sshpam_handle, 0);
338 if (sshpam_err != PAM_SUCCESS)
339 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100340
Darren Tuckerc376c862003-12-18 16:08:59 +1100341 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100342 if (!do_pam_account())
343 goto auth_fail;
344 if (sshpam_new_authtok_reqd) {
345 sshpam_err = pam_chauthtok(sshpam_handle,
346 PAM_CHANGE_EXPIRED_AUTHTOK);
347 if (sshpam_err != PAM_SUCCESS)
348 goto auth_fail;
349 pam_password_change_required(0);
350 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100351 }
Darren Tucker07705c72003-12-18 15:34:31 +1100352
Damien Miller4f9f42a2003-05-10 19:28:02 +1000353 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100354
355#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100356 /* Export variables set by do_pam_account */
357 buffer_put_int(&buffer, sshpam_account_status);
358 buffer_put_int(&buffer, sshpam_new_authtok_reqd);
359
Damien Millerc756e9b2003-11-17 21:41:42 +1100360 /* Export any environment strings set in child */
361 for(i = 0; environ[i] != NULL; i++)
362 ; /* Count */
363 buffer_put_int(&buffer, i);
364 for(i = 0; environ[i] != NULL; i++)
365 buffer_put_cstring(&buffer, environ[i]);
366
367 /* Export any environment strings set by PAM in child */
368 env_from_pam = pam_getenvlist(sshpam_handle);
369 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
370 ; /* Count */
371 buffer_put_int(&buffer, i);
372 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
373 buffer_put_cstring(&buffer, env_from_pam[i]);
374#endif /* USE_POSIX_THREADS */
375
Damien Miller9bdba702003-11-17 21:27:55 +1100376 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000377 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
378 buffer_free(&buffer);
379 pthread_exit(NULL);
380
381 auth_fail:
382 buffer_put_cstring(&buffer,
383 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100384 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000385 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
386 buffer_free(&buffer);
387 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100388
Damien Miller4f9f42a2003-05-10 19:28:02 +1000389 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000390}
391
Darren Tucker8846a072003-10-07 11:30:15 +1000392void
393sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000394{
Darren Tucker8846a072003-10-07 11:30:15 +1000395 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000396
Darren Tucker8846a072003-10-07 11:30:15 +1000397 if (ctxt != NULL && ctxt->pam_thread != 0) {
398 pthread_cancel(ctxt->pam_thread);
399 pthread_join(ctxt->pam_thread, NULL);
400 close(ctxt->pam_psock);
401 close(ctxt->pam_csock);
402 memset(ctxt, 0, sizeof(*ctxt));
403 cleanup_ctxt = NULL;
404 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000405}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000406
Damien Miller4f9f42a2003-05-10 19:28:02 +1000407static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000408sshpam_null_conv(int n, const struct pam_message **msg,
409 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000410{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000411 return (PAM_CONV_ERR);
412}
Damien Miller63dc3e92001-02-07 12:58:33 +1100413
Damien Miller4f9f42a2003-05-10 19:28:02 +1000414static struct pam_conv null_conv = { sshpam_null_conv, NULL };
415
Darren Tucker8846a072003-10-07 11:30:15 +1000416void
417sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000418{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000419 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000420 if (sshpam_handle == NULL)
421 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000422 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
423 if (sshpam_cred_established) {
424 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
425 sshpam_cred_established = 0;
426 }
427 if (sshpam_session_open) {
428 pam_close_session(sshpam_handle, PAM_SILENT);
429 sshpam_session_open = 0;
430 }
431 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
432 pam_end(sshpam_handle, sshpam_err);
433 sshpam_handle = NULL;
434}
435
436static int
437sshpam_init(const char *user)
438{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000439 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000440 extern char *__progname;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000441 const char *pam_rhost, *pam_user;
442
443 if (sshpam_handle != NULL) {
444 /* We already have a PAM context; check if the user matches */
445 sshpam_err = pam_get_item(sshpam_handle,
446 PAM_USER, (const void **)&pam_user);
447 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
448 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000449 pam_end(sshpam_handle, sshpam_err);
450 sshpam_handle = NULL;
451 }
452 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000453 sshpam_err =
454 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000455 if (sshpam_err != PAM_SUCCESS) {
456 pam_end(sshpam_handle, sshpam_err);
457 sshpam_handle = NULL;
458 return (-1);
459 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000460 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000461 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000462 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
463 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000464 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000465 sshpam_handle = NULL;
466 return (-1);
467 }
468#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100469 /*
470 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
471 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000472 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100473 */
Damien Miller25d93422003-05-18 20:45:47 +1000474 debug("PAM: setting PAM_TTY to \"ssh\"");
475 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
476 if (sshpam_err != PAM_SUCCESS) {
477 pam_end(sshpam_handle, sshpam_err);
478 sshpam_handle = NULL;
479 return (-1);
480 }
481#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000482 return (0);
483}
484
485static void *
486sshpam_init_ctx(Authctxt *authctxt)
487{
488 struct pam_ctxt *ctxt;
489 int socks[2];
490
Damien Miller4e448a32003-05-14 15:11:48 +1000491 /* Refuse to start if we don't have PAM enabled */
492 if (!options.use_pam)
493 return NULL;
494
Damien Miller4f9f42a2003-05-10 19:28:02 +1000495 /* Initialize PAM */
496 if (sshpam_init(authctxt->user) == -1) {
497 error("PAM: initialization failed");
498 return (NULL);
499 }
500
501 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000502 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000503
504 /* Start the authentication thread */
505 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
506 error("PAM: failed create sockets: %s", strerror(errno));
507 xfree(ctxt);
508 return (NULL);
509 }
510 ctxt->pam_psock = socks[0];
511 ctxt->pam_csock = socks[1];
512 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
513 error("PAM: failed to start authentication thread: %s",
514 strerror(errno));
515 close(socks[0]);
516 close(socks[1]);
517 xfree(ctxt);
518 return (NULL);
519 }
Darren Tucker8846a072003-10-07 11:30:15 +1000520 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000521 return (ctxt);
522}
523
524static int
525sshpam_query(void *ctx, char **name, char **info,
526 u_int *num, char ***prompts, u_int **echo_on)
527{
528 Buffer buffer;
529 struct pam_ctxt *ctxt = ctx;
530 size_t plen;
531 u_char type;
532 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000533 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000534
535 buffer_init(&buffer);
536 *name = xstrdup("");
537 *info = xstrdup("");
538 *prompts = xmalloc(sizeof(char *));
539 **prompts = NULL;
540 plen = 0;
541 *echo_on = xmalloc(sizeof(u_int));
542 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
543 type = buffer_get_char(&buffer);
544 msg = buffer_get_string(&buffer, NULL);
545 switch (type) {
546 case PAM_PROMPT_ECHO_ON:
547 case PAM_PROMPT_ECHO_OFF:
548 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000549 len = plen + strlen(msg) + 1;
550 **prompts = xrealloc(**prompts, len);
551 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000552 **echo_on = (type == PAM_PROMPT_ECHO_ON);
553 xfree(msg);
554 return (0);
555 case PAM_ERROR_MSG:
556 case PAM_TEXT_INFO:
557 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100558 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000559 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100560 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000561 xfree(msg);
562 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000563 case PAM_SUCCESS:
564 case PAM_AUTH_ERR:
565 if (**prompts != NULL) {
566 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100567 debug("PAM: %s", **prompts);
568 buffer_append(&loginmsg, **prompts,
569 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000570 xfree(**prompts);
571 **prompts = NULL;
572 }
573 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100574 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000575 *num = 0;
576 **echo_on = 0;
577 ctxt->pam_done = 1;
578 xfree(msg);
579 return (0);
580 }
581 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000582 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000583 default:
584 *num = 0;
585 **echo_on = 0;
586 xfree(msg);
587 ctxt->pam_done = -1;
588 return (-1);
589 }
590 }
591 return (-1);
592}
593
594/* XXX - see also comment in auth-chall.c:verify_response */
595static int
596sshpam_respond(void *ctx, u_int num, char **resp)
597{
598 Buffer buffer;
599 struct pam_ctxt *ctxt = ctx;
600
601 debug2("PAM: %s", __func__);
602 switch (ctxt->pam_done) {
603 case 1:
604 sshpam_authenticated = 1;
605 return (0);
606 case 0:
607 break;
608 default:
609 return (-1);
610 }
611 if (num != 1) {
612 error("PAM: expected one response, got %u", num);
613 return (-1);
614 }
615 buffer_init(&buffer);
616 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100617 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
618 buffer_free(&buffer);
619 return (-1);
620 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000621 buffer_free(&buffer);
622 return (1);
623}
624
625static void
626sshpam_free_ctx(void *ctxtp)
627{
628 struct pam_ctxt *ctxt = ctxtp;
629
Darren Tucker8846a072003-10-07 11:30:15 +1000630 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000631 xfree(ctxt);
632 /*
633 * We don't call sshpam_cleanup() here because we may need the PAM
634 * handle at a later stage, e.g. when setting up a session. It's
635 * still on the cleanup list, so pam_end() *will* be called before
636 * the server process terminates.
637 */
638}
639
640KbdintDevice sshpam_device = {
641 "pam",
642 sshpam_init_ctx,
643 sshpam_query,
644 sshpam_respond,
645 sshpam_free_ctx
646};
647
648KbdintDevice mm_sshpam_device = {
649 "pam",
650 mm_sshpam_init_ctx,
651 mm_sshpam_query,
652 mm_sshpam_respond,
653 mm_sshpam_free_ctx
654};
655
656/*
657 * This replaces auth-pam.c
658 */
659void
660start_pam(const char *user)
661{
Damien Miller9d507da2003-05-14 15:31:12 +1000662 if (!options.use_pam)
663 fatal("PAM: initialisation requested when UsePAM=no");
664
Damien Miller4f9f42a2003-05-10 19:28:02 +1000665 if (sshpam_init(user) == -1)
666 fatal("PAM: initialisation failed");
667}
668
669void
670finish_pam(void)
671{
Darren Tucker8846a072003-10-07 11:30:15 +1000672 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000673}
674
Damien Miller1f499fd2003-08-25 13:08:49 +1000675u_int
676do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000677{
Darren Tucker07705c72003-12-18 15:34:31 +1100678 if (sshpam_account_status != -1)
679 return (sshpam_account_status);
680
Damien Miller1f499fd2003-08-25 13:08:49 +1000681 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
682 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100683
684 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
685 sshpam_account_status = 0;
686 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000687 }
688
Darren Tucker07705c72003-12-18 15:34:31 +1100689 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
690 pam_password_change_required(1);
691
692 sshpam_account_status = 1;
693 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000694}
695
696void
Damien Miller341c6e62003-09-02 23:18:52 +1000697do_pam_set_tty(const char *tty)
698{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000699 if (tty != NULL) {
700 debug("PAM: setting PAM_TTY to \"%s\"", tty);
701 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
702 if (sshpam_err != PAM_SUCCESS)
703 fatal("PAM: failed to set PAM_TTY: %s",
704 pam_strerror(sshpam_handle, sshpam_err));
705 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000706}
707
708void
709do_pam_setcred(int init)
710{
711 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
712 (const void *)&null_conv);
713 if (sshpam_err != PAM_SUCCESS)
714 fatal("PAM: failed to set PAM_CONV: %s",
715 pam_strerror(sshpam_handle, sshpam_err));
716 if (init) {
717 debug("PAM: establishing credentials");
718 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
719 } else {
720 debug("PAM: reinitializing credentials");
721 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
722 }
723 if (sshpam_err == PAM_SUCCESS) {
724 sshpam_cred_established = 1;
725 return;
726 }
727 if (sshpam_authenticated)
728 fatal("PAM: pam_setcred(): %s",
729 pam_strerror(sshpam_handle, sshpam_err));
730 else
731 debug("PAM: pam_setcred(): %s",
732 pam_strerror(sshpam_handle, sshpam_err));
733}
734
735int
736is_pam_password_change_required(void)
737{
738 return (sshpam_new_authtok_reqd);
739}
740
741static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100742pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000743 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000744{
745 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000746 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000747 int i;
748
Damien Miller5c3a5582003-09-23 22:12:38 +1000749 *resp = NULL;
750
Darren Tucker18df00c2003-11-18 12:42:07 +1100751 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000752 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000753
754 if ((reply = malloc(n * sizeof(*reply))) == NULL)
755 return (PAM_CONV_ERR);
756 memset(reply, 0, n * sizeof(*reply));
757
Damien Miller4f9f42a2003-05-10 19:28:02 +1000758 for (i = 0; i < n; ++i) {
759 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
760 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000761 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100762 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000763 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000764 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000765 break;
766 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100767 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000768 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000769 reply[i].resp = xstrdup(input);
770 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000771 break;
772 case PAM_ERROR_MSG:
773 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100774 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000775 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000776 break;
777 default:
778 goto fail;
779 }
780 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000781 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000782 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000783
Damien Miller4f9f42a2003-05-10 19:28:02 +1000784 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000785 for(i = 0; i < n; i++) {
786 if (reply[i].resp != NULL)
787 xfree(reply[i].resp);
788 }
789 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000790 return (PAM_CONV_ERR);
791}
792
Darren Tucker18df00c2003-11-18 12:42:07 +1100793static struct pam_conv tty_conv = { pam_tty_conv, NULL };
794
Damien Miller4f9f42a2003-05-10 19:28:02 +1000795/*
796 * XXX this should be done in the authentication phase, but ssh1 doesn't
797 * support that
798 */
799void
800do_pam_chauthtok(void)
801{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000802 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000803 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000804 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100805 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000806 if (sshpam_err != PAM_SUCCESS)
807 fatal("PAM: failed to set PAM_CONV: %s",
808 pam_strerror(sshpam_handle, sshpam_err));
809 debug("PAM: changing password");
810 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
811 if (sshpam_err != PAM_SUCCESS)
812 fatal("PAM: pam_chauthtok(): %s",
813 pam_strerror(sshpam_handle, sshpam_err));
814}
815
Darren Tucker18df00c2003-11-18 12:42:07 +1100816void
817do_pam_session(void)
818{
Damien Millera8e06ce2003-11-21 23:48:55 +1100819 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100820 (const void *)&tty_conv);
821 if (sshpam_err != PAM_SUCCESS)
822 fatal("PAM: failed to set PAM_CONV: %s",
823 pam_strerror(sshpam_handle, sshpam_err));
824 sshpam_err = pam_open_session(sshpam_handle, 0);
825 if (sshpam_err != PAM_SUCCESS)
826 fatal("PAM: pam_open_session(): %s",
827 pam_strerror(sshpam_handle, sshpam_err));
828 sshpam_session_open = 1;
829}
830
Damien Millera8e06ce2003-11-21 23:48:55 +1100831/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000832 * Set a PAM environment string. We need to do this so that the session
833 * modules can handle things like Kerberos/GSI credentials that appear
834 * during the ssh authentication process.
835 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000836int
Damien Millera8e06ce2003-11-21 23:48:55 +1100837do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000838{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000839 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100840#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000841 char *compound;
842 size_t len;
843
844 len = strlen(name) + strlen(value) + 2;
845 compound = xmalloc(len);
846
847 snprintf(compound, len, "%s=%s", name, value);
848 ret = pam_putenv(sshpam_handle, compound);
849 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000850#endif
Damien Millerf2728092003-09-17 07:24:25 +1000851
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000852 return (ret);
853}
854
Damien Miller4f9f42a2003-05-10 19:28:02 +1000855void
856print_pam_messages(void)
857{
858 /* XXX */
859}
860
861char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100862fetch_pam_child_environment(void)
863{
864 return sshpam_env;
865}
866
867char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000868fetch_pam_environment(void)
869{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000870 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000871}
872
873void
874free_pam_environment(char **env)
875{
876 char **envp;
877
Damien Millere27c6cc2003-05-16 18:21:01 +1000878 if (env == NULL)
879 return;
880
Damien Miller4f9f42a2003-05-10 19:28:02 +1000881 for (envp = env; *envp; envp++)
882 xfree(*envp);
883 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100884}
885
886#endif /* USE_PAM */