blob: 31e1999f0819fdfdbd901a1b70da4ea02a1c7b34 [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"
Damien Miller0f47c532004-01-02 18:01:30 +110034RCSID("$Id: auth-pam.c,v 1.87 2004/01/02 07:01:31 djm 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#define __unused
Kevin Steves85ecbe72001-04-20 17:43:47 +000063
Damien Miller4f9f42a2003-05-10 19:28:02 +100064#ifdef USE_POSIX_THREADS
65#include <pthread.h>
66/*
Damien Millera8e06ce2003-11-21 23:48:55 +110067 * Avoid namespace clash when *not* using pthreads for systems *with*
68 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +100069 * (e.g. Linux)
70 */
Damien Millera8e06ce2003-11-21 23:48:55 +110071typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +100072#else
73/*
74 * Simulate threads with processes.
75 */
76typedef pid_t sp_pthread_t;
Kevin Steves63007d42002-07-21 17:57:01 +000077
Damien Miller4f9f42a2003-05-10 19:28:02 +100078static void
79pthread_exit(void *value __unused)
80{
81 _exit(0);
82}
Damien Miller2f6a0ad2000-05-31 11:20:11 +100083
Damien Miller4f9f42a2003-05-10 19:28:02 +100084static int
85pthread_create(sp_pthread_t *thread, const void *attr __unused,
86 void *(*thread_start)(void *), void *arg)
87{
88 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +110089
Damien Miller4f9f42a2003-05-10 19:28:02 +100090 switch ((pid = fork())) {
91 case -1:
92 error("fork(): %s", strerror(errno));
93 return (-1);
94 case 0:
95 thread_start(arg);
96 _exit(1);
97 default:
98 *thread = pid;
99 return (0);
100 }
101}
Damien Millere72b7af1999-12-30 15:08:44 +1100102
Damien Miller4f9f42a2003-05-10 19:28:02 +1000103static int
104pthread_cancel(sp_pthread_t thread)
105{
106 return (kill(thread, SIGTERM));
107}
108
109static int
110pthread_join(sp_pthread_t thread, void **value __unused)
111{
112 int status;
113
114 waitpid(thread, &status, 0);
115 return (status);
116}
117#endif
118
119
Damien Miller5c3a5582003-09-23 22:12:38 +1000120static pam_handle_t *sshpam_handle = NULL;
121static int sshpam_err = 0;
122static int sshpam_authenticated = 0;
123static int sshpam_new_authtok_reqd = 0;
124static int sshpam_session_open = 0;
125static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100126static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100127static char **sshpam_env = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000128
129struct pam_ctxt {
130 sp_pthread_t pam_thread;
131 int pam_psock;
132 int pam_csock;
133 int pam_done;
Damien Millere72b7af1999-12-30 15:08:44 +1100134};
Damien Millere72b7af1999-12-30 15:08:44 +1100135
Damien Miller4f9f42a2003-05-10 19:28:02 +1000136static void sshpam_free_ctx(void *);
Darren Tucker8846a072003-10-07 11:30:15 +1000137static struct pam_ctxt *cleanup_ctxt;
Damien Miller9d5705a2000-09-16 16:09:27 +1100138
Damien Millerc756e9b2003-11-17 21:41:42 +1100139/* Some PAM implementations don't implement this */
140#ifndef HAVE_PAM_GETENVLIST
141static char **
142pam_getenvlist(pam_handle_t *pamh)
143{
144 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100145 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100146 * for platforms without pam_getenvlist by searching for known
147 * env vars (e.g. KRB5CCNAME) from the PAM environment.
148 */
149 return NULL;
150}
151#endif
152
Darren Tucker07705c72003-12-18 15:34:31 +1100153void
154pam_password_change_required(int reqd)
155{
156 sshpam_new_authtok_reqd = reqd;
157 if (reqd) {
158 no_port_forwarding_flag |= 2;
159 no_agent_forwarding_flag |= 2;
160 no_x11_forwarding_flag |= 2;
161 } else {
162 no_port_forwarding_flag &= ~2;
163 no_agent_forwarding_flag &= ~2;
164 no_x11_forwarding_flag &= ~2;
165
166 }
167}
Damien Millerc756e9b2003-11-17 21:41:42 +1100168/* Import regular and PAM environment from subprocess */
169static void
170import_environments(Buffer *b)
171{
172 char *env;
173 u_int i, num_env;
174 int err;
175
Darren Tucker07705c72003-12-18 15:34:31 +1100176 /* Import variables set by do_pam_account */
177 sshpam_account_status = buffer_get_int(b);
178 pam_password_change_required(buffer_get_int(b));
179
Damien Millerc756e9b2003-11-17 21:41:42 +1100180 /* Import environment from subprocess */
181 num_env = buffer_get_int(b);
182 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
183 debug3("PAM: num env strings %d", num_env);
184 for(i = 0; i < num_env; i++)
185 sshpam_env[i] = buffer_get_string(b, NULL);
186
187 sshpam_env[num_env] = NULL;
188
189 /* Import PAM environment from subprocess */
190 num_env = buffer_get_int(b);
191 debug("PAM: num PAM env strings %d", num_env);
192 for(i = 0; i < num_env; i++) {
193 env = buffer_get_string(b, NULL);
194
Darren Tucker8a1624c2003-11-18 12:45:35 +1100195#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100196 /* Errors are not fatal here */
197 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
198 error("PAM: pam_putenv: %s",
199 pam_strerror(sshpam_handle, sshpam_err));
200 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100201#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100202 }
203}
204
Damien Miller9d5705a2000-09-16 16:09:27 +1100205/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000206 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100207 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000208static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000209sshpam_thread_conv(int n, const struct pam_message **msg,
210 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100211{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000212 Buffer buffer;
213 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000214 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000215 int i;
216
Damien Miller5c3a5582003-09-23 22:12:38 +1000217 *resp = NULL;
218
Damien Miller4f9f42a2003-05-10 19:28:02 +1000219 ctxt = data;
220 if (n <= 0 || n > PAM_MAX_NUM_MSG)
221 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000222
223 if ((reply = malloc(n * sizeof(*reply))) == NULL)
224 return (PAM_CONV_ERR);
225 memset(reply, 0, n * sizeof(*reply));
226
Damien Miller4f9f42a2003-05-10 19:28:02 +1000227 buffer_init(&buffer);
228 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000229 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
230 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100231 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000232 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100233 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100234 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
235 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100236 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100237 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000238 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
239 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000240 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000241 break;
242 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100243 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000244 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100245 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100246 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
247 goto fail;
248 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
249 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000250 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
251 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000252 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000253 break;
254 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100255 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000256 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100257 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100258 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
259 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000260 break;
261 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100262 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000263 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100264 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100265 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
266 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000267 break;
268 default:
269 goto fail;
270 }
271 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000272 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000273 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000274 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000275 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000276
Damien Miller4f9f42a2003-05-10 19:28:02 +1000277 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000278 for(i = 0; i < n; i++) {
279 if (reply[i].resp != NULL)
280 xfree(reply[i].resp);
281 }
282 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000283 buffer_free(&buffer);
284 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000285}
286
Damien Miller4f9f42a2003-05-10 19:28:02 +1000287/*
288 * Authentication thread.
289 */
290static void *
291sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100292{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000293 struct pam_ctxt *ctxt = ctxtp;
294 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000295 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000296#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100297 extern char **environ;
298 char **env_from_pam;
299 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000300 const char *pam_user;
301
302 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
303 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100304 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000305#endif
306
Damien Millerf4b6f102003-09-02 23:12:06 +1000307 sshpam_conv.conv = sshpam_thread_conv;
308 sshpam_conv.appdata_ptr = ctxt;
309
Damien Miller4f9f42a2003-05-10 19:28:02 +1000310 buffer_init(&buffer);
311 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
312 (const void *)&sshpam_conv);
313 if (sshpam_err != PAM_SUCCESS)
314 goto auth_fail;
315 sshpam_err = pam_authenticate(sshpam_handle, 0);
316 if (sshpam_err != PAM_SUCCESS)
317 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100318
Darren Tuckerc376c862003-12-18 16:08:59 +1100319 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100320 if (!do_pam_account())
321 goto auth_fail;
322 if (sshpam_new_authtok_reqd) {
323 sshpam_err = pam_chauthtok(sshpam_handle,
324 PAM_CHANGE_EXPIRED_AUTHTOK);
325 if (sshpam_err != PAM_SUCCESS)
326 goto auth_fail;
327 pam_password_change_required(0);
328 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100329 }
Darren Tucker07705c72003-12-18 15:34:31 +1100330
Damien Miller4f9f42a2003-05-10 19:28:02 +1000331 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100332
333#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100334 /* Export variables set by do_pam_account */
335 buffer_put_int(&buffer, sshpam_account_status);
336 buffer_put_int(&buffer, sshpam_new_authtok_reqd);
337
Damien Millerc756e9b2003-11-17 21:41:42 +1100338 /* Export any environment strings set in child */
339 for(i = 0; environ[i] != NULL; i++)
340 ; /* Count */
341 buffer_put_int(&buffer, i);
342 for(i = 0; environ[i] != NULL; i++)
343 buffer_put_cstring(&buffer, environ[i]);
344
345 /* Export any environment strings set by PAM in child */
346 env_from_pam = pam_getenvlist(sshpam_handle);
347 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
348 ; /* Count */
349 buffer_put_int(&buffer, i);
350 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
351 buffer_put_cstring(&buffer, env_from_pam[i]);
352#endif /* USE_POSIX_THREADS */
353
Damien Miller9bdba702003-11-17 21:27:55 +1100354 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000355 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
356 buffer_free(&buffer);
357 pthread_exit(NULL);
358
359 auth_fail:
360 buffer_put_cstring(&buffer,
361 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100362 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000363 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
364 buffer_free(&buffer);
365 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100366
Damien Miller4f9f42a2003-05-10 19:28:02 +1000367 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000368}
369
Darren Tucker8846a072003-10-07 11:30:15 +1000370void
371sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000372{
Darren Tucker8846a072003-10-07 11:30:15 +1000373 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000374
Darren Tucker8846a072003-10-07 11:30:15 +1000375 if (ctxt != NULL && ctxt->pam_thread != 0) {
376 pthread_cancel(ctxt->pam_thread);
377 pthread_join(ctxt->pam_thread, NULL);
378 close(ctxt->pam_psock);
379 close(ctxt->pam_csock);
380 memset(ctxt, 0, sizeof(*ctxt));
381 cleanup_ctxt = NULL;
382 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000383}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000384
Damien Miller4f9f42a2003-05-10 19:28:02 +1000385static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000386sshpam_null_conv(int n, const struct pam_message **msg,
387 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000388{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000389 return (PAM_CONV_ERR);
390}
Damien Miller63dc3e92001-02-07 12:58:33 +1100391
Damien Miller4f9f42a2003-05-10 19:28:02 +1000392static struct pam_conv null_conv = { sshpam_null_conv, NULL };
393
Darren Tucker8846a072003-10-07 11:30:15 +1000394void
395sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000396{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000397 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000398 if (sshpam_handle == NULL)
399 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000400 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
401 if (sshpam_cred_established) {
402 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
403 sshpam_cred_established = 0;
404 }
405 if (sshpam_session_open) {
406 pam_close_session(sshpam_handle, PAM_SILENT);
407 sshpam_session_open = 0;
408 }
409 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
410 pam_end(sshpam_handle, sshpam_err);
411 sshpam_handle = NULL;
412}
413
414static int
415sshpam_init(const char *user)
416{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000417 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000418 extern char *__progname;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000419 const char *pam_rhost, *pam_user;
420
421 if (sshpam_handle != NULL) {
422 /* We already have a PAM context; check if the user matches */
423 sshpam_err = pam_get_item(sshpam_handle,
424 PAM_USER, (const void **)&pam_user);
425 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
426 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000427 pam_end(sshpam_handle, sshpam_err);
428 sshpam_handle = NULL;
429 }
430 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000431 sshpam_err =
432 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000433 if (sshpam_err != PAM_SUCCESS) {
434 pam_end(sshpam_handle, sshpam_err);
435 sshpam_handle = NULL;
436 return (-1);
437 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000438 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000439 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000440 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
441 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000442 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000443 sshpam_handle = NULL;
444 return (-1);
445 }
446#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100447 /*
448 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
449 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000450 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100451 */
Damien Miller25d93422003-05-18 20:45:47 +1000452 debug("PAM: setting PAM_TTY to \"ssh\"");
453 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
454 if (sshpam_err != PAM_SUCCESS) {
455 pam_end(sshpam_handle, sshpam_err);
456 sshpam_handle = NULL;
457 return (-1);
458 }
459#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000460 return (0);
461}
462
463static void *
464sshpam_init_ctx(Authctxt *authctxt)
465{
466 struct pam_ctxt *ctxt;
467 int socks[2];
468
Damien Miller4e448a32003-05-14 15:11:48 +1000469 /* Refuse to start if we don't have PAM enabled */
470 if (!options.use_pam)
471 return NULL;
472
Damien Miller4f9f42a2003-05-10 19:28:02 +1000473 /* Initialize PAM */
474 if (sshpam_init(authctxt->user) == -1) {
475 error("PAM: initialization failed");
476 return (NULL);
477 }
478
479 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000480 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000481
482 /* Start the authentication thread */
483 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
484 error("PAM: failed create sockets: %s", strerror(errno));
485 xfree(ctxt);
486 return (NULL);
487 }
488 ctxt->pam_psock = socks[0];
489 ctxt->pam_csock = socks[1];
490 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
491 error("PAM: failed to start authentication thread: %s",
492 strerror(errno));
493 close(socks[0]);
494 close(socks[1]);
495 xfree(ctxt);
496 return (NULL);
497 }
Darren Tucker8846a072003-10-07 11:30:15 +1000498 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000499 return (ctxt);
500}
501
502static int
503sshpam_query(void *ctx, char **name, char **info,
504 u_int *num, char ***prompts, u_int **echo_on)
505{
506 Buffer buffer;
507 struct pam_ctxt *ctxt = ctx;
508 size_t plen;
509 u_char type;
510 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000511 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000512
513 buffer_init(&buffer);
514 *name = xstrdup("");
515 *info = xstrdup("");
516 *prompts = xmalloc(sizeof(char *));
517 **prompts = NULL;
518 plen = 0;
519 *echo_on = xmalloc(sizeof(u_int));
520 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
521 type = buffer_get_char(&buffer);
522 msg = buffer_get_string(&buffer, NULL);
523 switch (type) {
524 case PAM_PROMPT_ECHO_ON:
525 case PAM_PROMPT_ECHO_OFF:
526 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000527 len = plen + strlen(msg) + 1;
528 **prompts = xrealloc(**prompts, len);
529 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000530 **echo_on = (type == PAM_PROMPT_ECHO_ON);
531 xfree(msg);
532 return (0);
533 case PAM_ERROR_MSG:
534 case PAM_TEXT_INFO:
535 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100536 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000537 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100538 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000539 xfree(msg);
540 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000541 case PAM_SUCCESS:
542 case PAM_AUTH_ERR:
543 if (**prompts != NULL) {
544 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100545 debug("PAM: %s", **prompts);
546 buffer_append(&loginmsg, **prompts,
547 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000548 xfree(**prompts);
549 **prompts = NULL;
550 }
551 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100552 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000553 *num = 0;
554 **echo_on = 0;
555 ctxt->pam_done = 1;
556 xfree(msg);
557 return (0);
558 }
559 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000560 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000561 default:
562 *num = 0;
563 **echo_on = 0;
564 xfree(msg);
565 ctxt->pam_done = -1;
566 return (-1);
567 }
568 }
569 return (-1);
570}
571
572/* XXX - see also comment in auth-chall.c:verify_response */
573static int
574sshpam_respond(void *ctx, u_int num, char **resp)
575{
576 Buffer buffer;
577 struct pam_ctxt *ctxt = ctx;
578
579 debug2("PAM: %s", __func__);
580 switch (ctxt->pam_done) {
581 case 1:
582 sshpam_authenticated = 1;
583 return (0);
584 case 0:
585 break;
586 default:
587 return (-1);
588 }
589 if (num != 1) {
590 error("PAM: expected one response, got %u", num);
591 return (-1);
592 }
593 buffer_init(&buffer);
594 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100595 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
596 buffer_free(&buffer);
597 return (-1);
598 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000599 buffer_free(&buffer);
600 return (1);
601}
602
603static void
604sshpam_free_ctx(void *ctxtp)
605{
606 struct pam_ctxt *ctxt = ctxtp;
607
Darren Tucker8846a072003-10-07 11:30:15 +1000608 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000609 xfree(ctxt);
610 /*
611 * We don't call sshpam_cleanup() here because we may need the PAM
612 * handle at a later stage, e.g. when setting up a session. It's
613 * still on the cleanup list, so pam_end() *will* be called before
614 * the server process terminates.
615 */
616}
617
618KbdintDevice sshpam_device = {
619 "pam",
620 sshpam_init_ctx,
621 sshpam_query,
622 sshpam_respond,
623 sshpam_free_ctx
624};
625
626KbdintDevice mm_sshpam_device = {
627 "pam",
628 mm_sshpam_init_ctx,
629 mm_sshpam_query,
630 mm_sshpam_respond,
631 mm_sshpam_free_ctx
632};
633
634/*
635 * This replaces auth-pam.c
636 */
637void
638start_pam(const char *user)
639{
Damien Miller9d507da2003-05-14 15:31:12 +1000640 if (!options.use_pam)
641 fatal("PAM: initialisation requested when UsePAM=no");
642
Damien Miller4f9f42a2003-05-10 19:28:02 +1000643 if (sshpam_init(user) == -1)
644 fatal("PAM: initialisation failed");
645}
646
647void
648finish_pam(void)
649{
Darren Tucker8846a072003-10-07 11:30:15 +1000650 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000651}
652
Damien Miller1f499fd2003-08-25 13:08:49 +1000653u_int
654do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000655{
Darren Tucker07705c72003-12-18 15:34:31 +1100656 if (sshpam_account_status != -1)
657 return (sshpam_account_status);
658
Damien Miller1f499fd2003-08-25 13:08:49 +1000659 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
660 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100661
662 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
663 sshpam_account_status = 0;
664 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000665 }
666
Darren Tucker07705c72003-12-18 15:34:31 +1100667 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
668 pam_password_change_required(1);
669
670 sshpam_account_status = 1;
671 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000672}
673
674void
Damien Miller341c6e62003-09-02 23:18:52 +1000675do_pam_set_tty(const char *tty)
676{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000677 if (tty != NULL) {
678 debug("PAM: setting PAM_TTY to \"%s\"", tty);
679 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
680 if (sshpam_err != PAM_SUCCESS)
681 fatal("PAM: failed to set PAM_TTY: %s",
682 pam_strerror(sshpam_handle, sshpam_err));
683 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000684}
685
686void
687do_pam_setcred(int init)
688{
689 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
690 (const void *)&null_conv);
691 if (sshpam_err != PAM_SUCCESS)
692 fatal("PAM: failed to set PAM_CONV: %s",
693 pam_strerror(sshpam_handle, sshpam_err));
694 if (init) {
695 debug("PAM: establishing credentials");
696 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
697 } else {
698 debug("PAM: reinitializing credentials");
699 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
700 }
701 if (sshpam_err == PAM_SUCCESS) {
702 sshpam_cred_established = 1;
703 return;
704 }
705 if (sshpam_authenticated)
706 fatal("PAM: pam_setcred(): %s",
707 pam_strerror(sshpam_handle, sshpam_err));
708 else
709 debug("PAM: pam_setcred(): %s",
710 pam_strerror(sshpam_handle, sshpam_err));
711}
712
713int
714is_pam_password_change_required(void)
715{
716 return (sshpam_new_authtok_reqd);
717}
718
719static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100720pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000721 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000722{
723 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000724 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000725 int i;
726
Damien Miller5c3a5582003-09-23 22:12:38 +1000727 *resp = NULL;
728
Darren Tucker18df00c2003-11-18 12:42:07 +1100729 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000730 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000731
732 if ((reply = malloc(n * sizeof(*reply))) == NULL)
733 return (PAM_CONV_ERR);
734 memset(reply, 0, n * sizeof(*reply));
735
Damien Miller4f9f42a2003-05-10 19:28:02 +1000736 for (i = 0; i < n; ++i) {
737 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
738 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000739 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100740 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000741 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000742 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000743 break;
744 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100745 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000746 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000747 reply[i].resp = xstrdup(input);
748 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000749 break;
750 case PAM_ERROR_MSG:
751 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100752 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000753 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000754 break;
755 default:
756 goto fail;
757 }
758 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000759 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000760 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000761
Damien Miller4f9f42a2003-05-10 19:28:02 +1000762 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000763 for(i = 0; i < n; i++) {
764 if (reply[i].resp != NULL)
765 xfree(reply[i].resp);
766 }
767 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000768 return (PAM_CONV_ERR);
769}
770
Darren Tucker18df00c2003-11-18 12:42:07 +1100771static struct pam_conv tty_conv = { pam_tty_conv, NULL };
772
Damien Miller4f9f42a2003-05-10 19:28:02 +1000773/*
774 * XXX this should be done in the authentication phase, but ssh1 doesn't
775 * support that
776 */
777void
778do_pam_chauthtok(void)
779{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000780 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000781 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000782 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100783 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000784 if (sshpam_err != PAM_SUCCESS)
785 fatal("PAM: failed to set PAM_CONV: %s",
786 pam_strerror(sshpam_handle, sshpam_err));
787 debug("PAM: changing password");
788 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
789 if (sshpam_err != PAM_SUCCESS)
790 fatal("PAM: pam_chauthtok(): %s",
791 pam_strerror(sshpam_handle, sshpam_err));
792}
793
Darren Tucker18df00c2003-11-18 12:42:07 +1100794void
795do_pam_session(void)
796{
Damien Millera8e06ce2003-11-21 23:48:55 +1100797 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100798 (const void *)&tty_conv);
799 if (sshpam_err != PAM_SUCCESS)
800 fatal("PAM: failed to set PAM_CONV: %s",
801 pam_strerror(sshpam_handle, sshpam_err));
802 sshpam_err = pam_open_session(sshpam_handle, 0);
803 if (sshpam_err != PAM_SUCCESS)
804 fatal("PAM: pam_open_session(): %s",
805 pam_strerror(sshpam_handle, sshpam_err));
806 sshpam_session_open = 1;
807}
808
Damien Millera8e06ce2003-11-21 23:48:55 +1100809/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000810 * Set a PAM environment string. We need to do this so that the session
811 * modules can handle things like Kerberos/GSI credentials that appear
812 * during the ssh authentication process.
813 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000814int
Damien Millera8e06ce2003-11-21 23:48:55 +1100815do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000816{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000817 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100818#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000819 char *compound;
820 size_t len;
821
822 len = strlen(name) + strlen(value) + 2;
823 compound = xmalloc(len);
824
825 snprintf(compound, len, "%s=%s", name, value);
826 ret = pam_putenv(sshpam_handle, compound);
827 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000828#endif
Damien Millerf2728092003-09-17 07:24:25 +1000829
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000830 return (ret);
831}
832
Damien Miller4f9f42a2003-05-10 19:28:02 +1000833void
834print_pam_messages(void)
835{
836 /* XXX */
837}
838
839char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100840fetch_pam_child_environment(void)
841{
842 return sshpam_env;
843}
844
845char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000846fetch_pam_environment(void)
847{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000848 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000849}
850
851void
852free_pam_environment(char **env)
853{
854 char **envp;
855
Damien Millere27c6cc2003-05-16 18:21:01 +1000856 if (env == NULL)
857 return;
858
Damien Miller4f9f42a2003-05-10 19:28:02 +1000859 for (envp = env; *envp; envp++)
860 xfree(*envp);
861 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100862}
863
864#endif /* USE_PAM */