blob: 45c5e0a0a3c26f854f5ec26609caba39c473ab3e [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 Tucker7ae09622004-01-14 23:07:56 +110034RCSID("$Id: auth-pam.c,v 1.91 2004/01/14 12:07:56 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;
158static int sshpam_new_authtok_reqd = 0;
159static int sshpam_session_open = 0;
160static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100161static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100162static char **sshpam_env = NULL;
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{
181 sshpam_new_authtok_reqd = reqd;
182 if (reqd) {
183 no_port_forwarding_flag |= 2;
184 no_agent_forwarding_flag |= 2;
185 no_x11_forwarding_flag |= 2;
186 } else {
187 no_port_forwarding_flag &= ~2;
188 no_agent_forwarding_flag &= ~2;
189 no_x11_forwarding_flag &= ~2;
190
191 }
192}
Damien Millerc756e9b2003-11-17 21:41:42 +1100193/* Import regular and PAM environment from subprocess */
194static void
195import_environments(Buffer *b)
196{
197 char *env;
198 u_int i, num_env;
199 int err;
200
Darren Tucker07705c72003-12-18 15:34:31 +1100201 /* Import variables set by do_pam_account */
202 sshpam_account_status = buffer_get_int(b);
203 pam_password_change_required(buffer_get_int(b));
204
Damien Millerc756e9b2003-11-17 21:41:42 +1100205 /* Import environment from subprocess */
206 num_env = buffer_get_int(b);
207 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
208 debug3("PAM: num env strings %d", num_env);
209 for(i = 0; i < num_env; i++)
210 sshpam_env[i] = buffer_get_string(b, NULL);
211
212 sshpam_env[num_env] = NULL;
213
214 /* Import PAM environment from subprocess */
215 num_env = buffer_get_int(b);
216 debug("PAM: num PAM env strings %d", num_env);
217 for(i = 0; i < num_env; i++) {
218 env = buffer_get_string(b, NULL);
219
Darren Tucker8a1624c2003-11-18 12:45:35 +1100220#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100221 /* Errors are not fatal here */
222 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
223 error("PAM: pam_putenv: %s",
224 pam_strerror(sshpam_handle, sshpam_err));
225 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100226#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100227 }
228}
229
Damien Miller9d5705a2000-09-16 16:09:27 +1100230/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000231 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100232 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000233static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000234sshpam_thread_conv(int n, const struct pam_message **msg,
235 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100236{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000237 Buffer buffer;
238 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000239 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000240 int i;
241
Damien Miller5c3a5582003-09-23 22:12:38 +1000242 *resp = NULL;
243
Damien Miller4f9f42a2003-05-10 19:28:02 +1000244 ctxt = data;
245 if (n <= 0 || n > PAM_MAX_NUM_MSG)
246 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000247
248 if ((reply = malloc(n * sizeof(*reply))) == NULL)
249 return (PAM_CONV_ERR);
250 memset(reply, 0, n * sizeof(*reply));
251
Damien Miller4f9f42a2003-05-10 19:28:02 +1000252 buffer_init(&buffer);
253 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000254 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
255 case PAM_PROMPT_ECHO_OFF:
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 Millera8e06ce2003-11-21 23:48:55 +1100261 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100262 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000263 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
264 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000265 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000266 break;
267 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100268 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000269 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100270 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100271 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
272 goto fail;
273 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
274 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000275 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
276 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000277 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000278 break;
279 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100280 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000281 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100282 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100283 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
284 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000285 break;
286 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100287 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000288 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100289 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100290 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
291 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000292 break;
293 default:
294 goto fail;
295 }
296 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000297 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000298 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000299 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000300 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000301
Damien Miller4f9f42a2003-05-10 19:28:02 +1000302 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000303 for(i = 0; i < n; i++) {
304 if (reply[i].resp != NULL)
305 xfree(reply[i].resp);
306 }
307 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000308 buffer_free(&buffer);
309 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000310}
311
Damien Miller4f9f42a2003-05-10 19:28:02 +1000312/*
313 * Authentication thread.
314 */
315static void *
316sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100317{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000318 struct pam_ctxt *ctxt = ctxtp;
319 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000320 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000321#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100322 extern char **environ;
323 char **env_from_pam;
324 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000325 const char *pam_user;
326
327 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
328 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100329 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000330#endif
331
Damien Millerf4b6f102003-09-02 23:12:06 +1000332 sshpam_conv.conv = sshpam_thread_conv;
333 sshpam_conv.appdata_ptr = ctxt;
334
Damien Miller4f9f42a2003-05-10 19:28:02 +1000335 buffer_init(&buffer);
336 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
337 (const void *)&sshpam_conv);
338 if (sshpam_err != PAM_SUCCESS)
339 goto auth_fail;
340 sshpam_err = pam_authenticate(sshpam_handle, 0);
341 if (sshpam_err != PAM_SUCCESS)
342 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100343
Darren Tuckerc376c862003-12-18 16:08:59 +1100344 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100345 if (!do_pam_account())
346 goto auth_fail;
347 if (sshpam_new_authtok_reqd) {
348 sshpam_err = pam_chauthtok(sshpam_handle,
349 PAM_CHANGE_EXPIRED_AUTHTOK);
350 if (sshpam_err != PAM_SUCCESS)
351 goto auth_fail;
352 pam_password_change_required(0);
353 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100354 }
Darren Tucker07705c72003-12-18 15:34:31 +1100355
Damien Miller4f9f42a2003-05-10 19:28:02 +1000356 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100357
358#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100359 /* Export variables set by do_pam_account */
360 buffer_put_int(&buffer, sshpam_account_status);
361 buffer_put_int(&buffer, sshpam_new_authtok_reqd);
362
Damien Millerc756e9b2003-11-17 21:41:42 +1100363 /* Export any environment strings set in child */
364 for(i = 0; environ[i] != NULL; i++)
365 ; /* Count */
366 buffer_put_int(&buffer, i);
367 for(i = 0; environ[i] != NULL; i++)
368 buffer_put_cstring(&buffer, environ[i]);
369
370 /* Export any environment strings set by PAM in child */
371 env_from_pam = pam_getenvlist(sshpam_handle);
372 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
373 ; /* Count */
374 buffer_put_int(&buffer, i);
375 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
376 buffer_put_cstring(&buffer, env_from_pam[i]);
377#endif /* USE_POSIX_THREADS */
378
Damien Miller9bdba702003-11-17 21:27:55 +1100379 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000380 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
381 buffer_free(&buffer);
382 pthread_exit(NULL);
383
384 auth_fail:
385 buffer_put_cstring(&buffer,
386 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100387 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000388 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
389 buffer_free(&buffer);
390 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100391
Damien Miller4f9f42a2003-05-10 19:28:02 +1000392 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000393}
394
Darren Tucker8846a072003-10-07 11:30:15 +1000395void
396sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000397{
Darren Tucker8846a072003-10-07 11:30:15 +1000398 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000399
Darren Tucker8846a072003-10-07 11:30:15 +1000400 if (ctxt != NULL && ctxt->pam_thread != 0) {
401 pthread_cancel(ctxt->pam_thread);
402 pthread_join(ctxt->pam_thread, NULL);
403 close(ctxt->pam_psock);
404 close(ctxt->pam_csock);
405 memset(ctxt, 0, sizeof(*ctxt));
406 cleanup_ctxt = NULL;
407 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000408}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000409
Damien Miller4f9f42a2003-05-10 19:28:02 +1000410static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000411sshpam_null_conv(int n, const struct pam_message **msg,
412 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000413{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000414 return (PAM_CONV_ERR);
415}
Damien Miller63dc3e92001-02-07 12:58:33 +1100416
Damien Miller4f9f42a2003-05-10 19:28:02 +1000417static struct pam_conv null_conv = { sshpam_null_conv, NULL };
418
Darren Tucker8846a072003-10-07 11:30:15 +1000419void
420sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000421{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000422 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000423 if (sshpam_handle == NULL)
424 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000425 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
426 if (sshpam_cred_established) {
427 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
428 sshpam_cred_established = 0;
429 }
430 if (sshpam_session_open) {
431 pam_close_session(sshpam_handle, PAM_SILENT);
432 sshpam_session_open = 0;
433 }
434 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
435 pam_end(sshpam_handle, sshpam_err);
436 sshpam_handle = NULL;
437}
438
439static int
440sshpam_init(const char *user)
441{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000442 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000443 extern char *__progname;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000444 const char *pam_rhost, *pam_user;
445
446 if (sshpam_handle != NULL) {
447 /* We already have a PAM context; check if the user matches */
448 sshpam_err = pam_get_item(sshpam_handle,
449 PAM_USER, (const void **)&pam_user);
450 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
451 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000452 pam_end(sshpam_handle, sshpam_err);
453 sshpam_handle = NULL;
454 }
455 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000456 sshpam_err =
457 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000458 if (sshpam_err != PAM_SUCCESS) {
459 pam_end(sshpam_handle, sshpam_err);
460 sshpam_handle = NULL;
461 return (-1);
462 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000463 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000464 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000465 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
466 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000467 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000468 sshpam_handle = NULL;
469 return (-1);
470 }
471#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100472 /*
473 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
474 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000475 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100476 */
Damien Miller25d93422003-05-18 20:45:47 +1000477 debug("PAM: setting PAM_TTY to \"ssh\"");
478 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
479 if (sshpam_err != PAM_SUCCESS) {
480 pam_end(sshpam_handle, sshpam_err);
481 sshpam_handle = NULL;
482 return (-1);
483 }
484#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000485 return (0);
486}
487
488static void *
489sshpam_init_ctx(Authctxt *authctxt)
490{
491 struct pam_ctxt *ctxt;
492 int socks[2];
493
Damien Miller4e448a32003-05-14 15:11:48 +1000494 /* Refuse to start if we don't have PAM enabled */
495 if (!options.use_pam)
496 return NULL;
497
Damien Miller4f9f42a2003-05-10 19:28:02 +1000498 /* Initialize PAM */
499 if (sshpam_init(authctxt->user) == -1) {
500 error("PAM: initialization failed");
501 return (NULL);
502 }
503
504 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000505 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000506
507 /* Start the authentication thread */
508 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
509 error("PAM: failed create sockets: %s", strerror(errno));
510 xfree(ctxt);
511 return (NULL);
512 }
513 ctxt->pam_psock = socks[0];
514 ctxt->pam_csock = socks[1];
515 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
516 error("PAM: failed to start authentication thread: %s",
517 strerror(errno));
518 close(socks[0]);
519 close(socks[1]);
520 xfree(ctxt);
521 return (NULL);
522 }
Darren Tucker8846a072003-10-07 11:30:15 +1000523 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000524 return (ctxt);
525}
526
527static int
528sshpam_query(void *ctx, char **name, char **info,
529 u_int *num, char ***prompts, u_int **echo_on)
530{
531 Buffer buffer;
532 struct pam_ctxt *ctxt = ctx;
533 size_t plen;
534 u_char type;
535 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000536 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000537
538 buffer_init(&buffer);
539 *name = xstrdup("");
540 *info = xstrdup("");
541 *prompts = xmalloc(sizeof(char *));
542 **prompts = NULL;
543 plen = 0;
544 *echo_on = xmalloc(sizeof(u_int));
545 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
546 type = buffer_get_char(&buffer);
547 msg = buffer_get_string(&buffer, NULL);
548 switch (type) {
549 case PAM_PROMPT_ECHO_ON:
550 case PAM_PROMPT_ECHO_OFF:
551 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000552 len = plen + strlen(msg) + 1;
553 **prompts = xrealloc(**prompts, len);
554 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000555 **echo_on = (type == PAM_PROMPT_ECHO_ON);
556 xfree(msg);
557 return (0);
558 case PAM_ERROR_MSG:
559 case PAM_TEXT_INFO:
560 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100561 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000562 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100563 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000564 xfree(msg);
565 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000566 case PAM_SUCCESS:
567 case PAM_AUTH_ERR:
568 if (**prompts != NULL) {
569 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100570 debug("PAM: %s", **prompts);
571 buffer_append(&loginmsg, **prompts,
572 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000573 xfree(**prompts);
574 **prompts = NULL;
575 }
576 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100577 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000578 *num = 0;
579 **echo_on = 0;
580 ctxt->pam_done = 1;
581 xfree(msg);
582 return (0);
583 }
584 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000585 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000586 default:
587 *num = 0;
588 **echo_on = 0;
589 xfree(msg);
590 ctxt->pam_done = -1;
591 return (-1);
592 }
593 }
594 return (-1);
595}
596
597/* XXX - see also comment in auth-chall.c:verify_response */
598static int
599sshpam_respond(void *ctx, u_int num, char **resp)
600{
601 Buffer buffer;
602 struct pam_ctxt *ctxt = ctx;
603
604 debug2("PAM: %s", __func__);
605 switch (ctxt->pam_done) {
606 case 1:
607 sshpam_authenticated = 1;
608 return (0);
609 case 0:
610 break;
611 default:
612 return (-1);
613 }
614 if (num != 1) {
615 error("PAM: expected one response, got %u", num);
616 return (-1);
617 }
618 buffer_init(&buffer);
619 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100620 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
621 buffer_free(&buffer);
622 return (-1);
623 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000624 buffer_free(&buffer);
625 return (1);
626}
627
628static void
629sshpam_free_ctx(void *ctxtp)
630{
631 struct pam_ctxt *ctxt = ctxtp;
632
Darren Tucker8846a072003-10-07 11:30:15 +1000633 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000634 xfree(ctxt);
635 /*
636 * We don't call sshpam_cleanup() here because we may need the PAM
637 * handle at a later stage, e.g. when setting up a session. It's
638 * still on the cleanup list, so pam_end() *will* be called before
639 * the server process terminates.
640 */
641}
642
643KbdintDevice sshpam_device = {
644 "pam",
645 sshpam_init_ctx,
646 sshpam_query,
647 sshpam_respond,
648 sshpam_free_ctx
649};
650
651KbdintDevice mm_sshpam_device = {
652 "pam",
653 mm_sshpam_init_ctx,
654 mm_sshpam_query,
655 mm_sshpam_respond,
656 mm_sshpam_free_ctx
657};
658
659/*
660 * This replaces auth-pam.c
661 */
662void
663start_pam(const char *user)
664{
Damien Miller9d507da2003-05-14 15:31:12 +1000665 if (!options.use_pam)
666 fatal("PAM: initialisation requested when UsePAM=no");
667
Damien Miller4f9f42a2003-05-10 19:28:02 +1000668 if (sshpam_init(user) == -1)
669 fatal("PAM: initialisation failed");
670}
671
672void
673finish_pam(void)
674{
Darren Tucker8846a072003-10-07 11:30:15 +1000675 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000676}
677
Damien Miller1f499fd2003-08-25 13:08:49 +1000678u_int
679do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000680{
Darren Tucker07705c72003-12-18 15:34:31 +1100681 if (sshpam_account_status != -1)
682 return (sshpam_account_status);
683
Damien Miller1f499fd2003-08-25 13:08:49 +1000684 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
685 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100686
687 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
688 sshpam_account_status = 0;
689 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000690 }
691
Darren Tucker07705c72003-12-18 15:34:31 +1100692 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
693 pam_password_change_required(1);
694
695 sshpam_account_status = 1;
696 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000697}
698
699void
Damien Miller341c6e62003-09-02 23:18:52 +1000700do_pam_set_tty(const char *tty)
701{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000702 if (tty != NULL) {
703 debug("PAM: setting PAM_TTY to \"%s\"", tty);
704 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
705 if (sshpam_err != PAM_SUCCESS)
706 fatal("PAM: failed to set PAM_TTY: %s",
707 pam_strerror(sshpam_handle, sshpam_err));
708 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000709}
710
711void
712do_pam_setcred(int init)
713{
714 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
715 (const void *)&null_conv);
716 if (sshpam_err != PAM_SUCCESS)
717 fatal("PAM: failed to set PAM_CONV: %s",
718 pam_strerror(sshpam_handle, sshpam_err));
719 if (init) {
720 debug("PAM: establishing credentials");
721 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
722 } else {
723 debug("PAM: reinitializing credentials");
724 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
725 }
726 if (sshpam_err == PAM_SUCCESS) {
727 sshpam_cred_established = 1;
728 return;
729 }
730 if (sshpam_authenticated)
731 fatal("PAM: pam_setcred(): %s",
732 pam_strerror(sshpam_handle, sshpam_err));
733 else
734 debug("PAM: pam_setcred(): %s",
735 pam_strerror(sshpam_handle, sshpam_err));
736}
737
738int
739is_pam_password_change_required(void)
740{
741 return (sshpam_new_authtok_reqd);
742}
743
744static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100745pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000746 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000747{
748 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000749 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000750 int i;
751
Damien Miller5c3a5582003-09-23 22:12:38 +1000752 *resp = NULL;
753
Darren Tucker18df00c2003-11-18 12:42:07 +1100754 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000755 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000756
757 if ((reply = malloc(n * sizeof(*reply))) == NULL)
758 return (PAM_CONV_ERR);
759 memset(reply, 0, n * sizeof(*reply));
760
Damien Miller4f9f42a2003-05-10 19:28:02 +1000761 for (i = 0; i < n; ++i) {
762 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
763 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000764 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100765 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000766 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000767 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000768 break;
769 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100770 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000771 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000772 reply[i].resp = xstrdup(input);
773 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000774 break;
775 case PAM_ERROR_MSG:
776 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100777 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000778 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000779 break;
780 default:
781 goto fail;
782 }
783 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000784 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000785 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000786
Damien Miller4f9f42a2003-05-10 19:28:02 +1000787 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000788 for(i = 0; i < n; i++) {
789 if (reply[i].resp != NULL)
790 xfree(reply[i].resp);
791 }
792 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000793 return (PAM_CONV_ERR);
794}
795
Darren Tucker18df00c2003-11-18 12:42:07 +1100796static struct pam_conv tty_conv = { pam_tty_conv, NULL };
797
Damien Miller4f9f42a2003-05-10 19:28:02 +1000798/*
799 * XXX this should be done in the authentication phase, but ssh1 doesn't
800 * support that
801 */
802void
803do_pam_chauthtok(void)
804{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000805 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000806 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000807 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100808 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000809 if (sshpam_err != PAM_SUCCESS)
810 fatal("PAM: failed to set PAM_CONV: %s",
811 pam_strerror(sshpam_handle, sshpam_err));
812 debug("PAM: changing password");
813 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
814 if (sshpam_err != PAM_SUCCESS)
815 fatal("PAM: pam_chauthtok(): %s",
816 pam_strerror(sshpam_handle, sshpam_err));
817}
818
Darren Tucker18df00c2003-11-18 12:42:07 +1100819void
820do_pam_session(void)
821{
Damien Millera8e06ce2003-11-21 23:48:55 +1100822 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100823 (const void *)&tty_conv);
824 if (sshpam_err != PAM_SUCCESS)
825 fatal("PAM: failed to set PAM_CONV: %s",
826 pam_strerror(sshpam_handle, sshpam_err));
827 sshpam_err = pam_open_session(sshpam_handle, 0);
828 if (sshpam_err != PAM_SUCCESS)
829 fatal("PAM: pam_open_session(): %s",
830 pam_strerror(sshpam_handle, sshpam_err));
831 sshpam_session_open = 1;
832}
833
Damien Millera8e06ce2003-11-21 23:48:55 +1100834/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000835 * Set a PAM environment string. We need to do this so that the session
836 * modules can handle things like Kerberos/GSI credentials that appear
837 * during the ssh authentication process.
838 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000839int
Damien Millera8e06ce2003-11-21 23:48:55 +1100840do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000841{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000842 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100843#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000844 char *compound;
845 size_t len;
846
847 len = strlen(name) + strlen(value) + 2;
848 compound = xmalloc(len);
849
850 snprintf(compound, len, "%s=%s", name, value);
851 ret = pam_putenv(sshpam_handle, compound);
852 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000853#endif
Damien Millerf2728092003-09-17 07:24:25 +1000854
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000855 return (ret);
856}
857
Damien Miller4f9f42a2003-05-10 19:28:02 +1000858void
859print_pam_messages(void)
860{
861 /* XXX */
862}
863
864char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100865fetch_pam_child_environment(void)
866{
867 return sshpam_env;
868}
869
870char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000871fetch_pam_environment(void)
872{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000873 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000874}
875
876void
877free_pam_environment(char **env)
878{
879 char **envp;
880
Damien Millere27c6cc2003-05-16 18:21:01 +1000881 if (env == NULL)
882 return;
883
Damien Miller4f9f42a2003-05-10 19:28:02 +1000884 for (envp = env; *envp; envp++)
885 xfree(*envp);
886 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100887}
888
889#endif /* USE_PAM */