blob: 27b9bab2352b960253483079e35216d2a24ddc88 [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 */
Damien Miller26314f62004-06-01 11:28:20 +100031/*
32 * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
33 * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au>
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 */
Damien Millere72b7af1999-12-30 15:08:44 +110047
Damien Miller25d93422003-05-18 20:45:47 +100048/* 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 +110049#include "includes.h"
Darren Tucker69687f42004-09-11 22:17:26 +100050RCSID("$Id: auth-pam.c,v 1.115 2004/09/11 12:17:26 dtucker Exp $");
Damien Millere72b7af1999-12-30 15:08:44 +110051
52#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110053#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100054#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110055#elif defined (HAVE_PAM_PAM_APPL_H)
56#include <pam/pam_appl.h>
57#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100058
Kevin Stevese683e762002-04-04 19:02:28 +000059#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110060#include "auth-pam.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100061#include "buffer.h"
62#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100064#include "log.h"
65#include "monitor_wrap.h"
66#include "msg.h"
67#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100068#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100069#include "servconf.h"
70#include "ssh2.h"
71#include "xmalloc.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100072#include "auth-options.h"
Damien Millere72b7af1999-12-30 15:08:44 +110073
Damien Miller4e448a32003-05-14 15:11:48 +100074extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +110075extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +110076extern int compat20;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +100077extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +100078
Damien Miller4f9f42a2003-05-10 19:28:02 +100079#ifdef USE_POSIX_THREADS
80#include <pthread.h>
81/*
Damien Millera8e06ce2003-11-21 23:48:55 +110082 * Avoid namespace clash when *not* using pthreads for systems *with*
83 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +100084 * (e.g. Linux)
85 */
Damien Millera8e06ce2003-11-21 23:48:55 +110086typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +100087#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +110088typedef pid_t sp_pthread_t;
89#endif
90
91struct pam_ctxt {
92 sp_pthread_t pam_thread;
93 int pam_psock;
94 int pam_csock;
95 int pam_done;
96};
97
98static void sshpam_free_ctx(void *);
99static struct pam_ctxt *cleanup_ctxt;
100
101#ifndef USE_POSIX_THREADS
Damien Miller4f9f42a2003-05-10 19:28:02 +1000102/*
103 * Simulate threads with processes.
104 */
Kevin Steves63007d42002-07-21 17:57:01 +0000105
Darren Tucker749bc952004-01-14 22:14:04 +1100106static int sshpam_thread_status = -1;
107static mysig_t sshpam_oldsig;
108
109static void
110sshpam_sigchld_handler(int sig)
111{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000112 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100113 if (cleanup_ctxt == NULL)
114 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000115 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Darren Tucker5d423f42004-07-11 16:54:08 +1000116 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000117 /* PAM thread has not exitted, privsep slave must have */
118 kill(cleanup_ctxt->pam_thread, SIGTERM);
119 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
Darren Tucker5d423f42004-07-11 16:54:08 +1000120 <= 0)
Darren Tuckerb53355e2004-05-24 11:55:36 +1000121 return; /* could not wait */
122 }
Darren Tucker749bc952004-01-14 22:14:04 +1100123 if (WIFSIGNALED(sshpam_thread_status) &&
124 WTERMSIG(sshpam_thread_status) == SIGTERM)
125 return; /* terminated by pthread_cancel */
126 if (!WIFEXITED(sshpam_thread_status))
127 fatal("PAM: authentication thread exited unexpectedly");
128 if (WEXITSTATUS(sshpam_thread_status) != 0)
129 fatal("PAM: authentication thread exited uncleanly");
130}
131
Damien Miller4f9f42a2003-05-10 19:28:02 +1000132static void
133pthread_exit(void *value __unused)
134{
135 _exit(0);
136}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000137
Damien Miller4f9f42a2003-05-10 19:28:02 +1000138static int
139pthread_create(sp_pthread_t *thread, const void *attr __unused,
140 void *(*thread_start)(void *), void *arg)
141{
142 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +1100143
Darren Tuckerb9b60212004-03-04 20:03:54 +1100144 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000145 switch ((pid = fork())) {
146 case -1:
147 error("fork(): %s", strerror(errno));
148 return (-1);
149 case 0:
150 thread_start(arg);
151 _exit(1);
152 default:
153 *thread = pid;
Darren Tucker749bc952004-01-14 22:14:04 +1100154 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000155 return (0);
156 }
157}
Damien Millere72b7af1999-12-30 15:08:44 +1100158
Damien Miller4f9f42a2003-05-10 19:28:02 +1000159static int
160pthread_cancel(sp_pthread_t thread)
161{
Darren Tucker7ae09622004-01-14 23:07:56 +1100162 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000163 return (kill(thread, SIGTERM));
164}
165
166static int
167pthread_join(sp_pthread_t thread, void **value __unused)
168{
169 int status;
170
Darren Tucker749bc952004-01-14 22:14:04 +1100171 if (sshpam_thread_status != -1)
172 return (sshpam_thread_status);
173 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000174 waitpid(thread, &status, 0);
175 return (status);
176}
177#endif
178
179
Damien Miller5c3a5582003-09-23 22:12:38 +1000180static pam_handle_t *sshpam_handle = NULL;
181static int sshpam_err = 0;
182static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000183static int sshpam_session_open = 0;
184static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100185static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100186static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000187static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000188static const char *sshpam_password = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000189
Damien Millerc756e9b2003-11-17 21:41:42 +1100190/* Some PAM implementations don't implement this */
191#ifndef HAVE_PAM_GETENVLIST
192static char **
193pam_getenvlist(pam_handle_t *pamh)
194{
195 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100196 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100197 * for platforms without pam_getenvlist by searching for known
198 * env vars (e.g. KRB5CCNAME) from the PAM environment.
199 */
200 return NULL;
201}
202#endif
203
Darren Tucker21dd0892004-08-16 23:12:05 +1000204/*
205 * Some platforms, notably Solaris, do not enforce password complexity
206 * rules during pam_chauthtok() if the real uid of the calling process
207 * is 0, on the assumption that it's being called by "passwd" run by root.
208 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
209 * the right thing.
210 */
211#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
212static int
213sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
214{
215 int result;
216
217 if (sshpam_authctxt == NULL)
218 fatal("PAM: sshpam_authctxt not initialized");
219 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
220 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
221 result = pam_chauthtok(pamh, flags);
222 if (setreuid(0, -1) == -1)
223 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
224 return result;
225}
226# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
227#endif
228
Darren Tucker07705c72003-12-18 15:34:31 +1100229void
Darren Tucker17db1c42004-06-19 12:54:38 +1000230sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100231{
Darren Tuckera8df9242004-01-15 00:15:07 +1100232 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000233 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100234 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000235 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100236 if (reqd) {
237 no_port_forwarding_flag |= 2;
238 no_agent_forwarding_flag |= 2;
239 no_x11_forwarding_flag |= 2;
240 } else {
241 no_port_forwarding_flag &= ~2;
242 no_agent_forwarding_flag &= ~2;
243 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100244 }
245}
Darren Tucker1921ed92004-02-10 13:23:28 +1100246
Damien Millerc756e9b2003-11-17 21:41:42 +1100247/* Import regular and PAM environment from subprocess */
248static void
249import_environments(Buffer *b)
250{
251 char *env;
252 u_int i, num_env;
253 int err;
254
Darren Tuckera8df9242004-01-15 00:15:07 +1100255 debug3("PAM: %s entering", __func__);
256
Darren Tucker4b385d42004-03-04 19:54:10 +1100257#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100258 /* Import variables set by do_pam_account */
259 sshpam_account_status = buffer_get_int(b);
Darren Tucker17db1c42004-06-19 12:54:38 +1000260 sshpam_password_change_required(buffer_get_int(b));
Darren Tucker07705c72003-12-18 15:34:31 +1100261
Damien Millerc756e9b2003-11-17 21:41:42 +1100262 /* Import environment from subprocess */
263 num_env = buffer_get_int(b);
264 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
265 debug3("PAM: num env strings %d", num_env);
266 for(i = 0; i < num_env; i++)
267 sshpam_env[i] = buffer_get_string(b, NULL);
268
269 sshpam_env[num_env] = NULL;
270
271 /* Import PAM environment from subprocess */
272 num_env = buffer_get_int(b);
273 debug("PAM: num PAM env strings %d", num_env);
274 for(i = 0; i < num_env; i++) {
275 env = buffer_get_string(b, NULL);
276
Darren Tucker8a1624c2003-11-18 12:45:35 +1100277#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100278 /* Errors are not fatal here */
279 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
280 error("PAM: pam_putenv: %s",
281 pam_strerror(sshpam_handle, sshpam_err));
282 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100283#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100284 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100285#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100286}
287
Damien Miller9d5705a2000-09-16 16:09:27 +1100288/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000289 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100290 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000291static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000292sshpam_thread_conv(int n, struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000293 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100294{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000295 Buffer buffer;
296 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000297 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000298 int i;
299
Darren Tuckerba53b832004-02-17 20:46:59 +1100300 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000301 *resp = NULL;
302
Darren Tucker59e06022004-06-30 20:34:31 +1000303 if (data == NULL) {
304 error("PAM: conversation function passed a null context");
305 return (PAM_CONV_ERR);
306 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000307 ctxt = data;
308 if (n <= 0 || n > PAM_MAX_NUM_MSG)
309 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000310
311 if ((reply = malloc(n * sizeof(*reply))) == NULL)
312 return (PAM_CONV_ERR);
313 memset(reply, 0, n * sizeof(*reply));
314
Damien Miller4f9f42a2003-05-10 19:28:02 +1000315 buffer_init(&buffer);
316 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000317 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
318 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100319 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000320 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100321 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100322 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
323 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100324 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100325 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000326 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
327 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000328 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000329 break;
330 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100331 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000332 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100333 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100334 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
335 goto fail;
336 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
337 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000338 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
339 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000340 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000341 break;
342 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100343 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000344 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100345 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100346 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
347 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000348 break;
349 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100350 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000351 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100352 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100353 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
354 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000355 break;
356 default:
357 goto fail;
358 }
359 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000360 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000361 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000362 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000363 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000364
Damien Miller4f9f42a2003-05-10 19:28:02 +1000365 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000366 for(i = 0; i < n; i++) {
367 if (reply[i].resp != NULL)
368 xfree(reply[i].resp);
369 }
370 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000371 buffer_free(&buffer);
372 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000373}
374
Damien Miller4f9f42a2003-05-10 19:28:02 +1000375/*
376 * Authentication thread.
377 */
378static void *
379sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100380{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000381 struct pam_ctxt *ctxt = ctxtp;
382 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000383 struct pam_conv sshpam_conv;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000384 int flags = (options.permit_empty_passwd == 0 ?
385 PAM_DISALLOW_NULL_AUTHTOK : 0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000386#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100387 extern char **environ;
388 char **env_from_pam;
389 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000390 const char *pam_user;
391
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000392 pam_get_item(sshpam_handle, PAM_USER, (void **)&pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100393 environ[0] = NULL;
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000394
395 if (sshpam_authctxt != NULL) {
396 setproctitle("%s [pam]",
397 sshpam_authctxt->valid ? pam_user : "unknown");
398 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000399#endif
400
Damien Millerf4b6f102003-09-02 23:12:06 +1000401 sshpam_conv.conv = sshpam_thread_conv;
402 sshpam_conv.appdata_ptr = ctxt;
403
Darren Tucker17addf02004-03-30 20:57:57 +1000404 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100405 fatal("%s: PAM authctxt not initialized", __func__);
406
Damien Miller4f9f42a2003-05-10 19:28:02 +1000407 buffer_init(&buffer);
408 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
409 (const void *)&sshpam_conv);
410 if (sshpam_err != PAM_SUCCESS)
411 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000412 sshpam_err = pam_authenticate(sshpam_handle, flags);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000413 if (sshpam_err != PAM_SUCCESS)
414 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100415
Darren Tuckerc376c862003-12-18 16:08:59 +1100416 if (compat20) {
Darren Tucker07705c72003-12-18 15:34:31 +1100417 if (!do_pam_account())
418 goto auth_fail;
Darren Tucker17addf02004-03-30 20:57:57 +1000419 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100420 sshpam_err = pam_chauthtok(sshpam_handle,
421 PAM_CHANGE_EXPIRED_AUTHTOK);
422 if (sshpam_err != PAM_SUCCESS)
423 goto auth_fail;
Darren Tucker17db1c42004-06-19 12:54:38 +1000424 sshpam_password_change_required(0);
Darren Tucker07705c72003-12-18 15:34:31 +1100425 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100426 }
Darren Tucker07705c72003-12-18 15:34:31 +1100427
Damien Miller4f9f42a2003-05-10 19:28:02 +1000428 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100429
430#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100431 /* Export variables set by do_pam_account */
432 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000433 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100434
Damien Millerc756e9b2003-11-17 21:41:42 +1100435 /* Export any environment strings set in child */
436 for(i = 0; environ[i] != NULL; i++)
437 ; /* Count */
438 buffer_put_int(&buffer, i);
439 for(i = 0; environ[i] != NULL; i++)
440 buffer_put_cstring(&buffer, environ[i]);
441
442 /* Export any environment strings set by PAM in child */
443 env_from_pam = pam_getenvlist(sshpam_handle);
444 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
445 ; /* Count */
446 buffer_put_int(&buffer, i);
447 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
448 buffer_put_cstring(&buffer, env_from_pam[i]);
449#endif /* USE_POSIX_THREADS */
450
Damien Miller9bdba702003-11-17 21:27:55 +1100451 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000452 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
453 buffer_free(&buffer);
454 pthread_exit(NULL);
455
456 auth_fail:
457 buffer_put_cstring(&buffer,
458 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100459 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000460 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
461 buffer_free(&buffer);
462 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100463
Damien Miller4f9f42a2003-05-10 19:28:02 +1000464 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000465}
466
Darren Tucker8846a072003-10-07 11:30:15 +1000467void
468sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000469{
Darren Tucker8846a072003-10-07 11:30:15 +1000470 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000471
Darren Tuckera8df9242004-01-15 00:15:07 +1100472 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000473 if (ctxt != NULL && ctxt->pam_thread != 0) {
474 pthread_cancel(ctxt->pam_thread);
475 pthread_join(ctxt->pam_thread, NULL);
476 close(ctxt->pam_psock);
477 close(ctxt->pam_csock);
478 memset(ctxt, 0, sizeof(*ctxt));
479 cleanup_ctxt = NULL;
480 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000481}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000482
Damien Miller4f9f42a2003-05-10 19:28:02 +1000483static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000484sshpam_null_conv(int n, struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000485 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000486{
Darren Tuckerba53b832004-02-17 20:46:59 +1100487 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000488 return (PAM_CONV_ERR);
489}
Damien Miller63dc3e92001-02-07 12:58:33 +1100490
Damien Miller4f9f42a2003-05-10 19:28:02 +1000491static struct pam_conv null_conv = { sshpam_null_conv, NULL };
492
Darren Tucker8846a072003-10-07 11:30:15 +1000493void
494sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000495{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000496 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000497 if (sshpam_handle == NULL)
498 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000499 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
500 if (sshpam_cred_established) {
501 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
502 sshpam_cred_established = 0;
503 }
504 if (sshpam_session_open) {
505 pam_close_session(sshpam_handle, PAM_SILENT);
506 sshpam_session_open = 0;
507 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100508 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000509 pam_end(sshpam_handle, sshpam_err);
510 sshpam_handle = NULL;
511}
512
513static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100514sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000515{
Darren Tucker455813b2003-09-13 22:12:11 +1000516 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100517 const char *pam_rhost, *pam_user, *user = authctxt->user;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000518
519 if (sshpam_handle != NULL) {
520 /* We already have a PAM context; check if the user matches */
521 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000522 PAM_USER, (void **)&pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000523 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
524 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000525 pam_end(sshpam_handle, sshpam_err);
526 sshpam_handle = NULL;
527 }
528 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000529 sshpam_err =
530 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000531 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100532
Damien Miller4f9f42a2003-05-10 19:28:02 +1000533 if (sshpam_err != PAM_SUCCESS) {
534 pam_end(sshpam_handle, sshpam_err);
535 sshpam_handle = NULL;
536 return (-1);
537 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000538 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000539 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000540 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
541 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000542 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000543 sshpam_handle = NULL;
544 return (-1);
545 }
546#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100547 /*
548 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
549 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000550 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100551 */
Damien Miller25d93422003-05-18 20:45:47 +1000552 debug("PAM: setting PAM_TTY to \"ssh\"");
553 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
554 if (sshpam_err != PAM_SUCCESS) {
555 pam_end(sshpam_handle, sshpam_err);
556 sshpam_handle = NULL;
557 return (-1);
558 }
559#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000560 return (0);
561}
562
563static void *
564sshpam_init_ctx(Authctxt *authctxt)
565{
566 struct pam_ctxt *ctxt;
567 int socks[2];
568
Darren Tuckera8df9242004-01-15 00:15:07 +1100569 debug3("PAM: %s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000570 /* Refuse to start if we don't have PAM enabled */
571 if (!options.use_pam)
572 return NULL;
573
Damien Miller4f9f42a2003-05-10 19:28:02 +1000574 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100575 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000576 error("PAM: initialization failed");
577 return (NULL);
578 }
579
580 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000581 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000582
583 /* Start the authentication thread */
584 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
585 error("PAM: failed create sockets: %s", strerror(errno));
586 xfree(ctxt);
587 return (NULL);
588 }
589 ctxt->pam_psock = socks[0];
590 ctxt->pam_csock = socks[1];
591 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
592 error("PAM: failed to start authentication thread: %s",
593 strerror(errno));
594 close(socks[0]);
595 close(socks[1]);
596 xfree(ctxt);
597 return (NULL);
598 }
Darren Tucker8846a072003-10-07 11:30:15 +1000599 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000600 return (ctxt);
601}
602
603static int
604sshpam_query(void *ctx, char **name, char **info,
605 u_int *num, char ***prompts, u_int **echo_on)
606{
607 Buffer buffer;
608 struct pam_ctxt *ctxt = ctx;
609 size_t plen;
610 u_char type;
611 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000612 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000613
Darren Tuckera8df9242004-01-15 00:15:07 +1100614 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000615 buffer_init(&buffer);
616 *name = xstrdup("");
617 *info = xstrdup("");
618 *prompts = xmalloc(sizeof(char *));
619 **prompts = NULL;
620 plen = 0;
621 *echo_on = xmalloc(sizeof(u_int));
622 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
623 type = buffer_get_char(&buffer);
624 msg = buffer_get_string(&buffer, NULL);
625 switch (type) {
626 case PAM_PROMPT_ECHO_ON:
627 case PAM_PROMPT_ECHO_OFF:
628 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000629 len = plen + strlen(msg) + 1;
630 **prompts = xrealloc(**prompts, len);
631 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000632 **echo_on = (type == PAM_PROMPT_ECHO_ON);
633 xfree(msg);
634 return (0);
635 case PAM_ERROR_MSG:
636 case PAM_TEXT_INFO:
637 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100638 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000639 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100640 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000641 xfree(msg);
642 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000643 case PAM_SUCCESS:
644 case PAM_AUTH_ERR:
645 if (**prompts != NULL) {
646 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100647 debug("PAM: %s", **prompts);
648 buffer_append(&loginmsg, **prompts,
649 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000650 xfree(**prompts);
651 **prompts = NULL;
652 }
653 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100654 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000655 *num = 0;
656 **echo_on = 0;
657 ctxt->pam_done = 1;
658 xfree(msg);
659 return (0);
660 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000661 error("PAM: %s for %s%.100s from %.100s", msg,
662 sshpam_authctxt->valid ? "" : "illegal user ",
663 sshpam_authctxt->user,
664 get_remote_name_or_ip(utmp_len, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000665 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000666 default:
667 *num = 0;
668 **echo_on = 0;
669 xfree(msg);
670 ctxt->pam_done = -1;
671 return (-1);
672 }
673 }
674 return (-1);
675}
676
677/* XXX - see also comment in auth-chall.c:verify_response */
678static int
679sshpam_respond(void *ctx, u_int num, char **resp)
680{
681 Buffer buffer;
682 struct pam_ctxt *ctxt = ctx;
683
Darren Tuckera8df9242004-01-15 00:15:07 +1100684 debug2("PAM: %s entering, %d responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000685 switch (ctxt->pam_done) {
686 case 1:
687 sshpam_authenticated = 1;
688 return (0);
689 case 0:
690 break;
691 default:
692 return (-1);
693 }
694 if (num != 1) {
695 error("PAM: expected one response, got %u", num);
696 return (-1);
697 }
698 buffer_init(&buffer);
699 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100700 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
701 buffer_free(&buffer);
702 return (-1);
703 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000704 buffer_free(&buffer);
705 return (1);
706}
707
708static void
709sshpam_free_ctx(void *ctxtp)
710{
711 struct pam_ctxt *ctxt = ctxtp;
712
Darren Tuckera8df9242004-01-15 00:15:07 +1100713 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000714 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000715 xfree(ctxt);
716 /*
717 * We don't call sshpam_cleanup() here because we may need the PAM
718 * handle at a later stage, e.g. when setting up a session. It's
719 * still on the cleanup list, so pam_end() *will* be called before
720 * the server process terminates.
721 */
722}
723
724KbdintDevice sshpam_device = {
725 "pam",
726 sshpam_init_ctx,
727 sshpam_query,
728 sshpam_respond,
729 sshpam_free_ctx
730};
731
732KbdintDevice mm_sshpam_device = {
733 "pam",
734 mm_sshpam_init_ctx,
735 mm_sshpam_query,
736 mm_sshpam_respond,
737 mm_sshpam_free_ctx
738};
739
740/*
741 * This replaces auth-pam.c
742 */
743void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100744start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000745{
Damien Miller9d507da2003-05-14 15:31:12 +1000746 if (!options.use_pam)
747 fatal("PAM: initialisation requested when UsePAM=no");
748
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100749 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000750 fatal("PAM: initialisation failed");
751}
752
753void
754finish_pam(void)
755{
Darren Tucker8846a072003-10-07 11:30:15 +1000756 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000757}
758
Damien Miller1f499fd2003-08-25 13:08:49 +1000759u_int
760do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000761{
Darren Tucker07705c72003-12-18 15:34:31 +1100762 if (sshpam_account_status != -1)
763 return (sshpam_account_status);
764
Damien Miller1f499fd2003-08-25 13:08:49 +1000765 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tuckera8df9242004-01-15 00:15:07 +1100766 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100767
768 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
769 sshpam_account_status = 0;
770 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000771 }
772
Darren Tucker07705c72003-12-18 15:34:31 +1100773 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +1000774 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +1100775
776 sshpam_account_status = 1;
777 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000778}
779
780void
Damien Miller341c6e62003-09-02 23:18:52 +1000781do_pam_set_tty(const char *tty)
782{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000783 if (tty != NULL) {
784 debug("PAM: setting PAM_TTY to \"%s\"", tty);
785 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
786 if (sshpam_err != PAM_SUCCESS)
787 fatal("PAM: failed to set PAM_TTY: %s",
788 pam_strerror(sshpam_handle, sshpam_err));
789 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000790}
791
792void
793do_pam_setcred(int init)
794{
795 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
796 (const void *)&null_conv);
797 if (sshpam_err != PAM_SUCCESS)
798 fatal("PAM: failed to set PAM_CONV: %s",
799 pam_strerror(sshpam_handle, sshpam_err));
800 if (init) {
801 debug("PAM: establishing credentials");
802 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
803 } else {
804 debug("PAM: reinitializing credentials");
805 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
806 }
807 if (sshpam_err == PAM_SUCCESS) {
808 sshpam_cred_established = 1;
809 return;
810 }
811 if (sshpam_authenticated)
812 fatal("PAM: pam_setcred(): %s",
813 pam_strerror(sshpam_handle, sshpam_err));
814 else
815 debug("PAM: pam_setcred(): %s",
816 pam_strerror(sshpam_handle, sshpam_err));
817}
818
Damien Miller4f9f42a2003-05-10 19:28:02 +1000819static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000820sshpam_tty_conv(int n, struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000821 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000822{
823 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000824 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000825 int i;
826
Darren Tuckerba53b832004-02-17 20:46:59 +1100827 debug3("PAM: %s called with %d messages", __func__, n);
828
Damien Miller5c3a5582003-09-23 22:12:38 +1000829 *resp = NULL;
830
Darren Tucker18df00c2003-11-18 12:42:07 +1100831 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000832 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000833
834 if ((reply = malloc(n * sizeof(*reply))) == NULL)
835 return (PAM_CONV_ERR);
836 memset(reply, 0, n * sizeof(*reply));
837
Damien Miller4f9f42a2003-05-10 19:28:02 +1000838 for (i = 0; i < n; ++i) {
839 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
840 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000841 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100842 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000843 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000844 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000845 break;
846 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100847 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000848 fgets(input, sizeof input, stdin);
Damien Millera6fb77f2004-07-19 09:39:11 +1000849 if ((reply[i].resp = strdup(input)) == NULL)
850 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000851 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000852 break;
853 case PAM_ERROR_MSG:
854 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100855 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000856 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000857 break;
858 default:
859 goto fail;
860 }
861 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000862 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000863 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000864
Damien Miller4f9f42a2003-05-10 19:28:02 +1000865 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000866 for(i = 0; i < n; i++) {
867 if (reply[i].resp != NULL)
868 xfree(reply[i].resp);
869 }
870 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000871 return (PAM_CONV_ERR);
872}
873
Darren Tucker94befab2004-06-03 14:53:12 +1000874static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +1100875
Damien Miller4f9f42a2003-05-10 19:28:02 +1000876/*
877 * XXX this should be done in the authentication phase, but ssh1 doesn't
878 * support that
879 */
880void
881do_pam_chauthtok(void)
882{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000883 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000884 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000885 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100886 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000887 if (sshpam_err != PAM_SUCCESS)
888 fatal("PAM: failed to set PAM_CONV: %s",
889 pam_strerror(sshpam_handle, sshpam_err));
890 debug("PAM: changing password");
891 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
892 if (sshpam_err != PAM_SUCCESS)
893 fatal("PAM: pam_chauthtok(): %s",
894 pam_strerror(sshpam_handle, sshpam_err));
895}
896
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100897static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +1000898sshpam_store_conv(int n, struct pam_message **msg,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100899 struct pam_response **resp, void *data)
900{
901 struct pam_response *reply;
902 int i;
903 size_t len;
904
905 debug3("PAM: %s called with %d messages", __func__, n);
906 *resp = NULL;
907
908 if (n <= 0 || n > PAM_MAX_NUM_MSG)
909 return (PAM_CONV_ERR);
910
911 if ((reply = malloc(n * sizeof(*reply))) == NULL)
912 return (PAM_CONV_ERR);
913 memset(reply, 0, n * sizeof(*reply));
914
915 for (i = 0; i < n; ++i) {
916 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
917 case PAM_ERROR_MSG:
918 case PAM_TEXT_INFO:
919 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
920 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
921 buffer_append(&loginmsg, "\n", 1 );
922 reply[i].resp_retcode = PAM_SUCCESS;
923 break;
924 default:
925 goto fail;
926 }
927 }
928 *resp = reply;
929 return (PAM_SUCCESS);
930
931 fail:
932 for(i = 0; i < n; i++) {
933 if (reply[i].resp != NULL)
934 xfree(reply[i].resp);
935 }
936 xfree(reply);
937 return (PAM_CONV_ERR);
938}
939
Darren Tucker94befab2004-06-03 14:53:12 +1000940static struct pam_conv store_conv = { sshpam_store_conv, NULL };
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100941
Darren Tucker18df00c2003-11-18 12:42:07 +1100942void
943do_pam_session(void)
944{
Darren Tucker1921ed92004-02-10 13:23:28 +1100945 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +1100946 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +1100947 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +1100948 if (sshpam_err != PAM_SUCCESS)
949 fatal("PAM: failed to set PAM_CONV: %s",
950 pam_strerror(sshpam_handle, sshpam_err));
951 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +1000952 if (sshpam_err == PAM_SUCCESS)
953 sshpam_session_open = 1;
954 else {
955 sshpam_session_open = 0;
956 disable_forwarding();
957 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +1100958 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +1000959 }
960
961}
962
963int
964is_pam_session_open(void)
965{
966 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +1100967}
968
Damien Millera8e06ce2003-11-21 23:48:55 +1100969/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000970 * Set a PAM environment string. We need to do this so that the session
971 * modules can handle things like Kerberos/GSI credentials that appear
972 * during the ssh authentication process.
973 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000974int
Damien Millera8e06ce2003-11-21 23:48:55 +1100975do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000976{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000977 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100978#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000979 char *compound;
980 size_t len;
981
982 len = strlen(name) + strlen(value) + 2;
983 compound = xmalloc(len);
984
985 snprintf(compound, len, "%s=%s", name, value);
986 ret = pam_putenv(sshpam_handle, compound);
987 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000988#endif
Damien Millerf2728092003-09-17 07:24:25 +1000989
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000990 return (ret);
991}
992
Damien Miller4f9f42a2003-05-10 19:28:02 +1000993char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100994fetch_pam_child_environment(void)
995{
996 return sshpam_env;
997}
998
999char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001000fetch_pam_environment(void)
1001{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001002 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001003}
1004
1005void
1006free_pam_environment(char **env)
1007{
1008 char **envp;
1009
Damien Millere27c6cc2003-05-16 18:21:01 +10001010 if (env == NULL)
1011 return;
1012
Damien Miller4f9f42a2003-05-10 19:28:02 +10001013 for (envp = env; *envp; envp++)
1014 xfree(*envp);
1015 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001016}
1017
Darren Tucker450a1582004-05-30 20:43:59 +10001018/*
1019 * "Blind" conversation function for password authentication. Assumes that
1020 * echo-off prompts are for the password and stores messages for later
1021 * display.
1022 */
1023static int
Darren Tuckere2ba9c22004-07-01 12:38:14 +10001024sshpam_passwd_conv(int n, struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001025 struct pam_response **resp, void *data)
1026{
1027 struct pam_response *reply;
1028 int i;
1029 size_t len;
1030
1031 debug3("PAM: %s called with %d messages", __func__, n);
1032
1033 *resp = NULL;
1034
1035 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1036 return (PAM_CONV_ERR);
1037
1038 if ((reply = malloc(n * sizeof(*reply))) == NULL)
1039 return (PAM_CONV_ERR);
1040 memset(reply, 0, n * sizeof(*reply));
1041
1042 for (i = 0; i < n; ++i) {
1043 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1044 case PAM_PROMPT_ECHO_OFF:
1045 if (sshpam_password == NULL)
1046 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001047 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1048 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001049 reply[i].resp_retcode = PAM_SUCCESS;
1050 break;
1051 case PAM_ERROR_MSG:
1052 case PAM_TEXT_INFO:
1053 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1054 if (len > 0) {
1055 buffer_append(&loginmsg,
1056 PAM_MSG_MEMBER(msg, i, msg), len);
1057 buffer_append(&loginmsg, "\n", 1);
1058 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001059 if ((reply[i].resp = strdup("")) == NULL)
1060 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001061 reply[i].resp_retcode = PAM_SUCCESS;
1062 break;
1063 default:
1064 goto fail;
1065 }
1066 }
1067 *resp = reply;
1068 return (PAM_SUCCESS);
1069
1070 fail:
1071 for(i = 0; i < n; i++) {
1072 if (reply[i].resp != NULL)
1073 xfree(reply[i].resp);
1074 }
1075 xfree(reply);
1076 return (PAM_CONV_ERR);
1077}
1078
1079static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1080
1081/*
1082 * Attempt password authentication via PAM
1083 */
1084int
1085sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1086{
1087 int flags = (options.permit_empty_passwd == 0 ?
1088 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tuckere061b152004-05-30 22:04:56 +10001089 static char badpw[] = "\b\n\r\177INCORRECT";
Darren Tucker450a1582004-05-30 20:43:59 +10001090
1091 if (!options.use_pam || sshpam_handle == NULL)
1092 fatal("PAM: %s called when PAM disabled or failed to "
1093 "initialise.", __func__);
1094
1095 sshpam_password = password;
1096 sshpam_authctxt = authctxt;
1097
Darren Tuckere061b152004-05-30 22:04:56 +10001098 /*
1099 * If the user logging in is invalid, or is root but is not permitted
1100 * by PermitRootLogin, use an invalid password to prevent leaking
1101 * information via timing (eg if the PAM config has a delay on fail).
1102 */
1103 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1104 options.permit_root_login != PERMIT_YES))
1105 sshpam_password = badpw;
1106
Darren Tucker450a1582004-05-30 20:43:59 +10001107 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1108 (const void *)&passwd_conv);
1109 if (sshpam_err != PAM_SUCCESS)
1110 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1111 pam_strerror(sshpam_handle, sshpam_err));
1112
1113 sshpam_err = pam_authenticate(sshpam_handle, flags);
1114 sshpam_password = NULL;
1115 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1116 debug("PAM: password authentication accepted for %.100s",
1117 authctxt->user);
1118 return 1;
1119 } else {
1120 debug("PAM: password authentication failed for %.100s: %s",
1121 authctxt->valid ? authctxt->user : "an illegal user",
1122 pam_strerror(sshpam_handle, sshpam_err));
1123 return 0;
1124 }
1125}
Damien Millere72b7af1999-12-30 15:08:44 +11001126#endif /* USE_PAM */