blob: 7cf9cf5fcc2da54459777c12d579c9a42d54a5ff [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 Miller5ef4b0f2016-01-27 17:45:56 +110048/* Based on FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des */
49
Damien Millere72b7af1999-12-30 15:08:44 +110050#include "includes.h"
Damien Miller6645e7a2006-03-15 14:42:54 +110051
52#include <sys/types.h>
53#include <sys/stat.h>
54#include <sys/wait.h>
Darren Tucker341dae52006-07-13 08:45:14 +100055
56#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110057#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100058#include <stdarg.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100059#include <string.h>
60#include <unistd.h>
Damien Millere72b7af1999-12-30 15:08:44 +110061
62#ifdef USE_PAM
Damien Miller0f47c532004-01-02 18:01:30 +110063#if defined(HAVE_SECURITY_PAM_APPL_H)
Damien Miller4f9f42a2003-05-10 19:28:02 +100064#include <security/pam_appl.h>
Damien Miller0f47c532004-01-02 18:01:30 +110065#elif defined (HAVE_PAM_PAM_APPL_H)
66#include <pam/pam_appl.h>
67#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +100068
Darren Tuckerf08bdb52005-05-26 19:59:48 +100069/* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
70#ifdef PAM_SUN_CODEBASE
71# define sshpam_const /* Solaris, HP-UX, AIX */
72#else
73# define sshpam_const const /* LinuxPAM, OpenPAM */
74#endif
75
Damien Miller2ab323e2006-08-05 12:43:32 +100076/* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
77#ifdef PAM_SUN_CODEBASE
78# define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
79#else
80# define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
81#endif
82
Damien Miller75bb6642006-08-05 14:07:20 +100083#include "xmalloc.h"
84#include "buffer.h"
85#include "key.h"
86#include "hostfile.h"
Kevin Stevese683e762002-04-04 19:02:28 +000087#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110088#include "auth-pam.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100090#include "log.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100091#include "msg.h"
92#include "packet.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100093#include "misc.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100094#include "servconf.h"
95#include "ssh2.h"
Damien Miller1f499fd2003-08-25 13:08:49 +100096#include "auth-options.h"
Damien Miller75bb6642006-08-05 14:07:20 +100097#ifdef GSSAPI
98#include "ssh-gss.h"
99#endif
100#include "monitor_wrap.h"
Damien Millere72b7af1999-12-30 15:08:44 +1100101
Damien Miller4e448a32003-05-14 15:11:48 +1000102extern ServerOptions options;
Darren Tucker18df00c2003-11-18 12:42:07 +1100103extern Buffer loginmsg;
Darren Tucker07705c72003-12-18 15:34:31 +1100104extern int compat20;
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000105extern u_int utmp_len;
Damien Miller4e448a32003-05-14 15:11:48 +1000106
Darren Tucker328118a2005-05-25 16:18:09 +1000107/* so we don't silently change behaviour */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000108#ifdef USE_POSIX_THREADS
Darren Tucker328118a2005-05-25 16:18:09 +1000109# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
110#endif
111
112/*
113 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
114 * and generally a bad idea. Use at own risk and do not expect support if
115 * this breaks.
116 */
117#ifdef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000118#include <pthread.h>
119/*
Damien Millera8e06ce2003-11-21 23:48:55 +1100120 * Avoid namespace clash when *not* using pthreads for systems *with*
121 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +1000122 * (e.g. Linux)
123 */
Damien Millera8e06ce2003-11-21 23:48:55 +1100124typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000125#else
Darren Tucker1b27c8f2004-01-13 22:35:58 +1100126typedef pid_t sp_pthread_t;
127#endif
128
129struct pam_ctxt {
130 sp_pthread_t pam_thread;
131 int pam_psock;
132 int pam_csock;
133 int pam_done;
134};
135
136static void sshpam_free_ctx(void *);
137static struct pam_ctxt *cleanup_ctxt;
138
Darren Tucker328118a2005-05-25 16:18:09 +1000139#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Miller4f9f42a2003-05-10 19:28:02 +1000140/*
141 * Simulate threads with processes.
142 */
Kevin Steves63007d42002-07-21 17:57:01 +0000143
Darren Tucker749bc952004-01-14 22:14:04 +1100144static int sshpam_thread_status = -1;
145static mysig_t sshpam_oldsig;
146
Damien Miller94cf4c82005-07-17 17:04:47 +1000147static void
Darren Tucker749bc952004-01-14 22:14:04 +1100148sshpam_sigchld_handler(int sig)
149{
Darren Tuckerb53355e2004-05-24 11:55:36 +1000150 signal(SIGCHLD, SIG_DFL);
Darren Tucker7ae09622004-01-14 23:07:56 +1100151 if (cleanup_ctxt == NULL)
152 return; /* handler called after PAM cleanup, shouldn't happen */
Darren Tuckerb53355e2004-05-24 11:55:36 +1000153 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
Damien Miller37294fb2005-07-17 17:18:49 +1000154 <= 0) {
Darren Tuckerb53355e2004-05-24 11:55:36 +1000155 /* PAM thread has not exitted, privsep slave must have */
156 kill(cleanup_ctxt->pam_thread, SIGTERM);
157 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
Darren Tucker5d423f42004-07-11 16:54:08 +1000158 <= 0)
Darren Tuckerb53355e2004-05-24 11:55:36 +1000159 return; /* could not wait */
160 }
Darren Tucker749bc952004-01-14 22:14:04 +1100161 if (WIFSIGNALED(sshpam_thread_status) &&
162 WTERMSIG(sshpam_thread_status) == SIGTERM)
163 return; /* terminated by pthread_cancel */
164 if (!WIFEXITED(sshpam_thread_status))
Darren Tucker57d4ca92007-08-10 14:32:34 +1000165 sigdie("PAM: authentication thread exited unexpectedly");
Darren Tucker749bc952004-01-14 22:14:04 +1100166 if (WEXITSTATUS(sshpam_thread_status) != 0)
Darren Tucker57d4ca92007-08-10 14:32:34 +1000167 sigdie("PAM: authentication thread exited uncleanly");
Darren Tucker749bc952004-01-14 22:14:04 +1100168}
169
Damien Millerb8fe89c2006-07-24 14:51:00 +1000170/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000171static void
Damien Millerb8fe89c2006-07-24 14:51:00 +1000172pthread_exit(void *value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000173{
174 _exit(0);
175}
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000176
Damien Millerb8fe89c2006-07-24 14:51:00 +1000177/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000178static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000179pthread_create(sp_pthread_t *thread, const void *attr,
Damien Miller4f9f42a2003-05-10 19:28:02 +1000180 void *(*thread_start)(void *), void *arg)
181{
182 pid_t pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000183 struct pam_ctxt *ctx = arg;
Damien Millere72b7af1999-12-30 15:08:44 +1100184
Darren Tuckerb9b60212004-03-04 20:03:54 +1100185 sshpam_thread_status = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000186 switch ((pid = fork())) {
187 case -1:
188 error("fork(): %s", strerror(errno));
189 return (-1);
190 case 0:
Darren Tucker4f1adad2005-07-16 11:33:06 +1000191 close(ctx->pam_psock);
192 ctx->pam_psock = -1;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000193 thread_start(arg);
194 _exit(1);
195 default:
196 *thread = pid;
Darren Tucker4f1adad2005-07-16 11:33:06 +1000197 close(ctx->pam_csock);
198 ctx->pam_csock = -1;
Darren Tucker749bc952004-01-14 22:14:04 +1100199 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000200 return (0);
201 }
202}
Damien Millere72b7af1999-12-30 15:08:44 +1100203
Damien Miller4f9f42a2003-05-10 19:28:02 +1000204static int
205pthread_cancel(sp_pthread_t thread)
206{
Darren Tucker7ae09622004-01-14 23:07:56 +1100207 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000208 return (kill(thread, SIGTERM));
209}
210
Damien Millerb8fe89c2006-07-24 14:51:00 +1000211/* ARGSUSED */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000212static int
Damien Millerb8fe89c2006-07-24 14:51:00 +1000213pthread_join(sp_pthread_t thread, void **value)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000214{
215 int status;
216
Darren Tucker749bc952004-01-14 22:14:04 +1100217 if (sshpam_thread_status != -1)
218 return (sshpam_thread_status);
219 signal(SIGCHLD, sshpam_oldsig);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000220 waitpid(thread, &status, 0);
221 return (status);
222}
223#endif
224
225
Damien Miller5c3a5582003-09-23 22:12:38 +1000226static pam_handle_t *sshpam_handle = NULL;
227static int sshpam_err = 0;
228static int sshpam_authenticated = 0;
Damien Miller5c3a5582003-09-23 22:12:38 +1000229static int sshpam_session_open = 0;
230static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100231static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100232static char **sshpam_env = NULL;
Darren Tucker17addf02004-03-30 20:57:57 +1000233static Authctxt *sshpam_authctxt = NULL;
Darren Tucker450a1582004-05-30 20:43:59 +1000234static const char *sshpam_password = NULL;
Darren Tucker36a3d602005-01-20 12:43:38 +1100235static char badpw[] = "\b\n\r\177INCORRECT";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000236
Damien Millerc756e9b2003-11-17 21:41:42 +1100237/* Some PAM implementations don't implement this */
238#ifndef HAVE_PAM_GETENVLIST
239static char **
240pam_getenvlist(pam_handle_t *pamh)
241{
242 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100243 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100244 * for platforms without pam_getenvlist by searching for known
245 * env vars (e.g. KRB5CCNAME) from the PAM environment.
246 */
247 return NULL;
248}
249#endif
250
Darren Tucker21dd0892004-08-16 23:12:05 +1000251/*
252 * Some platforms, notably Solaris, do not enforce password complexity
253 * rules during pam_chauthtok() if the real uid of the calling process
254 * is 0, on the assumption that it's being called by "passwd" run by root.
255 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
256 * the right thing.
257 */
258#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
259static int
260sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
261{
262 int result;
263
264 if (sshpam_authctxt == NULL)
265 fatal("PAM: sshpam_authctxt not initialized");
266 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
267 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
268 result = pam_chauthtok(pamh, flags);
269 if (setreuid(0, -1) == -1)
270 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
271 return result;
272}
273# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
274#endif
275
Darren Tucker07705c72003-12-18 15:34:31 +1100276void
Darren Tucker17db1c42004-06-19 12:54:38 +1000277sshpam_password_change_required(int reqd)
Darren Tucker07705c72003-12-18 15:34:31 +1100278{
Darren Tuckera8df9242004-01-15 00:15:07 +1100279 debug3("%s %d", __func__, reqd);
Darren Tucker17addf02004-03-30 20:57:57 +1000280 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100281 fatal("%s: PAM authctxt not initialized", __func__);
Darren Tucker17addf02004-03-30 20:57:57 +1000282 sshpam_authctxt->force_pwchange = reqd;
Darren Tucker07705c72003-12-18 15:34:31 +1100283 if (reqd) {
284 no_port_forwarding_flag |= 2;
285 no_agent_forwarding_flag |= 2;
286 no_x11_forwarding_flag |= 2;
287 } else {
288 no_port_forwarding_flag &= ~2;
289 no_agent_forwarding_flag &= ~2;
290 no_x11_forwarding_flag &= ~2;
Darren Tucker07705c72003-12-18 15:34:31 +1100291 }
292}
Darren Tucker1921ed92004-02-10 13:23:28 +1100293
Damien Millerc756e9b2003-11-17 21:41:42 +1100294/* Import regular and PAM environment from subprocess */
295static void
296import_environments(Buffer *b)
297{
298 char *env;
299 u_int i, num_env;
300 int err;
301
Darren Tuckera8df9242004-01-15 00:15:07 +1100302 debug3("PAM: %s entering", __func__);
303
Darren Tucker328118a2005-05-25 16:18:09 +1000304#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100305 /* Import variables set by do_pam_account */
306 sshpam_account_status = buffer_get_int(b);
Darren Tucker17db1c42004-06-19 12:54:38 +1000307 sshpam_password_change_required(buffer_get_int(b));
Darren Tucker07705c72003-12-18 15:34:31 +1100308
Damien Millerc756e9b2003-11-17 21:41:42 +1100309 /* Import environment from subprocess */
310 num_env = buffer_get_int(b);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000311 if (num_env > 1024)
312 fatal("%s: received %u environment variables, expected <= 1024",
313 __func__, num_env);
314 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
Damien Millerc756e9b2003-11-17 21:41:42 +1100315 debug3("PAM: num env strings %d", num_env);
316 for(i = 0; i < num_env; i++)
317 sshpam_env[i] = buffer_get_string(b, NULL);
318
319 sshpam_env[num_env] = NULL;
320
321 /* Import PAM environment from subprocess */
322 num_env = buffer_get_int(b);
323 debug("PAM: num PAM env strings %d", num_env);
324 for(i = 0; i < num_env; i++) {
325 env = buffer_get_string(b, NULL);
326
Darren Tucker8a1624c2003-11-18 12:45:35 +1100327#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100328 /* Errors are not fatal here */
329 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
330 error("PAM: pam_putenv: %s",
331 pam_strerror(sshpam_handle, sshpam_err));
332 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100333#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100334 }
Darren Tucker4b385d42004-03-04 19:54:10 +1100335#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100336}
337
Damien Miller9d5705a2000-09-16 16:09:27 +1100338/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000339 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100340 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000341static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000342sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000343 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100344{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000345 Buffer buffer;
346 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000347 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000348 int i;
349
Darren Tuckerba53b832004-02-17 20:46:59 +1100350 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller5c3a5582003-09-23 22:12:38 +1000351 *resp = NULL;
352
Darren Tucker59e06022004-06-30 20:34:31 +1000353 if (data == NULL) {
354 error("PAM: conversation function passed a null context");
355 return (PAM_CONV_ERR);
356 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000357 ctxt = data;
358 if (n <= 0 || n > PAM_MAX_NUM_MSG)
359 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000360
Darren Tuckerd8093e42006-05-04 16:24:34 +1000361 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000362 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000363
Damien Miller4f9f42a2003-05-10 19:28:02 +1000364 buffer_init(&buffer);
365 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000366 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
367 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100368 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000369 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100370 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100371 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
372 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100373 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100374 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000375 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
376 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000377 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000378 break;
379 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100380 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000381 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100382 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100383 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
384 goto fail;
385 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
386 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000387 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
388 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000389 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000390 break;
391 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100392 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000393 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100394 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100395 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
396 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000397 break;
398 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100399 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000400 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100401 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100402 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
403 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000404 break;
405 default:
406 goto fail;
407 }
408 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000409 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000410 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000411 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000412 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000413
Damien Miller4f9f42a2003-05-10 19:28:02 +1000414 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000415 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000416 free(reply[i].resp);
Damien Miller5c3a5582003-09-23 22:12:38 +1000417 }
Darren Tuckerf60845f2013-06-02 08:07:31 +1000418 free(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000419 buffer_free(&buffer);
420 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000421}
422
Damien Miller4f9f42a2003-05-10 19:28:02 +1000423/*
424 * Authentication thread.
425 */
426static void *
427sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100428{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000429 struct pam_ctxt *ctxt = ctxtp;
430 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000431 struct pam_conv sshpam_conv;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000432 int flags = (options.permit_empty_passwd == 0 ?
433 PAM_DISALLOW_NULL_AUTHTOK : 0);
Darren Tucker328118a2005-05-25 16:18:09 +1000434#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Damien Millerc756e9b2003-11-17 21:41:42 +1100435 extern char **environ;
436 char **env_from_pam;
437 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000438 const char *pam_user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000439 const char **ptr_pam_user = &pam_user;
Darren Tucker54e1b222006-09-17 11:57:46 +1000440 char *tz = getenv("TZ");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000441
Darren Tucker53f8e782013-12-19 11:31:44 +1100442 sshpam_err = pam_get_item(sshpam_handle, PAM_USER,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000443 (sshpam_const void **)ptr_pam_user);
Darren Tucker53f8e782013-12-19 11:31:44 +1100444 if (sshpam_err != PAM_SUCCESS)
445 goto auth_fail;
Darren Tucker54e1b222006-09-17 11:57:46 +1000446
Damien Millerc756e9b2003-11-17 21:41:42 +1100447 environ[0] = NULL;
Darren Tucker54e1b222006-09-17 11:57:46 +1000448 if (tz != NULL)
449 if (setenv("TZ", tz, 1) == -1)
450 error("PAM: could not set TZ environment: %s",
451 strerror(errno));
Damien Miller2d2ed3d2004-07-21 20:54:47 +1000452
453 if (sshpam_authctxt != NULL) {
454 setproctitle("%s [pam]",
455 sshpam_authctxt->valid ? pam_user : "unknown");
456 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000457#endif
458
Damien Millerf4b6f102003-09-02 23:12:06 +1000459 sshpam_conv.conv = sshpam_thread_conv;
460 sshpam_conv.appdata_ptr = ctxt;
461
Darren Tucker17addf02004-03-30 20:57:57 +1000462 if (sshpam_authctxt == NULL)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100463 fatal("%s: PAM authctxt not initialized", __func__);
464
Damien Miller4f9f42a2003-05-10 19:28:02 +1000465 buffer_init(&buffer);
466 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
467 (const void *)&sshpam_conv);
468 if (sshpam_err != PAM_SUCCESS)
469 goto auth_fail;
Darren Tucker1f7e4082004-07-01 14:00:14 +1000470 sshpam_err = pam_authenticate(sshpam_handle, flags);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000471 if (sshpam_err != PAM_SUCCESS)
472 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100473
Darren Tuckerc376c862003-12-18 16:08:59 +1100474 if (compat20) {
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000475 if (!do_pam_account()) {
476 sshpam_err = PAM_ACCT_EXPIRED;
Darren Tucker07705c72003-12-18 15:34:31 +1100477 goto auth_fail;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000478 }
Darren Tucker17addf02004-03-30 20:57:57 +1000479 if (sshpam_authctxt->force_pwchange) {
Darren Tucker07705c72003-12-18 15:34:31 +1100480 sshpam_err = pam_chauthtok(sshpam_handle,
481 PAM_CHANGE_EXPIRED_AUTHTOK);
482 if (sshpam_err != PAM_SUCCESS)
483 goto auth_fail;
Darren Tucker17db1c42004-06-19 12:54:38 +1000484 sshpam_password_change_required(0);
Darren Tucker07705c72003-12-18 15:34:31 +1100485 }
Darren Tuckerc376c862003-12-18 16:08:59 +1100486 }
Darren Tucker07705c72003-12-18 15:34:31 +1100487
Damien Miller4f9f42a2003-05-10 19:28:02 +1000488 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100489
Darren Tucker328118a2005-05-25 16:18:09 +1000490#ifndef UNSUPPORTED_POSIX_THREADS_HACK
Darren Tucker07705c72003-12-18 15:34:31 +1100491 /* Export variables set by do_pam_account */
492 buffer_put_int(&buffer, sshpam_account_status);
Darren Tucker17addf02004-03-30 20:57:57 +1000493 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
Darren Tucker07705c72003-12-18 15:34:31 +1100494
Damien Millerc756e9b2003-11-17 21:41:42 +1100495 /* Export any environment strings set in child */
496 for(i = 0; environ[i] != NULL; i++)
497 ; /* Count */
498 buffer_put_int(&buffer, i);
499 for(i = 0; environ[i] != NULL; i++)
500 buffer_put_cstring(&buffer, environ[i]);
501
502 /* Export any environment strings set by PAM in child */
503 env_from_pam = pam_getenvlist(sshpam_handle);
504 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
505 ; /* Count */
506 buffer_put_int(&buffer, i);
507 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
508 buffer_put_cstring(&buffer, env_from_pam[i]);
Darren Tucker328118a2005-05-25 16:18:09 +1000509#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
Damien Millerc756e9b2003-11-17 21:41:42 +1100510
Damien Miller9bdba702003-11-17 21:27:55 +1100511 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000512 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
513 buffer_free(&buffer);
514 pthread_exit(NULL);
515
516 auth_fail:
517 buffer_put_cstring(&buffer,
518 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100519 /* XXX - can't do much about an error here */
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000520 if (sshpam_err == PAM_ACCT_EXPIRED)
521 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
522 else
523 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000524 buffer_free(&buffer);
525 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100526
Damien Miller4f9f42a2003-05-10 19:28:02 +1000527 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000528}
529
Darren Tucker8846a072003-10-07 11:30:15 +1000530void
531sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000532{
Darren Tucker8846a072003-10-07 11:30:15 +1000533 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000534
Darren Tuckera8df9242004-01-15 00:15:07 +1100535 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000536 if (ctxt != NULL && ctxt->pam_thread != 0) {
537 pthread_cancel(ctxt->pam_thread);
538 pthread_join(ctxt->pam_thread, NULL);
539 close(ctxt->pam_psock);
540 close(ctxt->pam_csock);
541 memset(ctxt, 0, sizeof(*ctxt));
542 cleanup_ctxt = NULL;
543 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000544}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000545
Damien Miller4f9f42a2003-05-10 19:28:02 +1000546static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000547sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000548 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000549{
Darren Tuckerba53b832004-02-17 20:46:59 +1100550 debug3("PAM: %s entering, %d messages", __func__, n);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000551 return (PAM_CONV_ERR);
552}
Damien Miller63dc3e92001-02-07 12:58:33 +1100553
Damien Miller4f9f42a2003-05-10 19:28:02 +1000554static struct pam_conv null_conv = { sshpam_null_conv, NULL };
555
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000556static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000557sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000558 struct pam_response **resp, void *data)
559{
560 struct pam_response *reply;
561 int i;
562 size_t len;
563
564 debug3("PAM: %s called with %d messages", __func__, n);
565 *resp = NULL;
566
567 if (n <= 0 || n > PAM_MAX_NUM_MSG)
568 return (PAM_CONV_ERR);
569
Darren Tuckerd8093e42006-05-04 16:24:34 +1000570 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000571 return (PAM_CONV_ERR);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000572
573 for (i = 0; i < n; ++i) {
574 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
575 case PAM_ERROR_MSG:
576 case PAM_TEXT_INFO:
577 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
578 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
579 buffer_append(&loginmsg, "\n", 1 );
580 reply[i].resp_retcode = PAM_SUCCESS;
581 break;
582 default:
583 goto fail;
584 }
585 }
586 *resp = reply;
587 return (PAM_SUCCESS);
588
589 fail:
590 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +1000591 free(reply[i].resp);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000592 }
Darren Tuckerf60845f2013-06-02 08:07:31 +1000593 free(reply);
Darren Tucker0a7e3c62004-09-11 22:28:01 +1000594 return (PAM_CONV_ERR);
595}
596
597static struct pam_conv store_conv = { sshpam_store_conv, NULL };
598
Darren Tucker8846a072003-10-07 11:30:15 +1000599void
600sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000601{
Darren Tucker52358d62008-03-11 22:58:25 +1100602 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
Damien Miller5c3a5582003-09-23 22:12:38 +1000603 return;
Darren Tucker52358d62008-03-11 22:58:25 +1100604 debug("PAM: cleanup");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000605 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000606 if (sshpam_session_open) {
Darren Tucker52358d62008-03-11 22:58:25 +1100607 debug("PAM: closing session");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000608 pam_close_session(sshpam_handle, PAM_SILENT);
609 sshpam_session_open = 0;
610 }
Darren Tucker622d5c52009-07-12 22:07:21 +1000611 if (sshpam_cred_established) {
612 debug("PAM: deleting credentials");
613 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
614 sshpam_cred_established = 0;
615 }
Darren Tucker1921ed92004-02-10 13:23:28 +1100616 sshpam_authenticated = 0;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000617 pam_end(sshpam_handle, sshpam_err);
618 sshpam_handle = NULL;
619}
620
621static int
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100622sshpam_init(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000623{
Darren Tucker455813b2003-09-13 22:12:11 +1000624 extern char *__progname;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100625 const char *pam_rhost, *pam_user, *user = authctxt->user;
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000626 const char **ptr_pam_user = &pam_user;
Damien Miller3ed92182016-03-08 14:01:29 -0800627 struct ssh *ssh = active_state; /* XXX */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000628
629 if (sshpam_handle != NULL) {
630 /* We already have a PAM context; check if the user matches */
631 sshpam_err = pam_get_item(sshpam_handle,
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000632 PAM_USER, (sshpam_const void **)ptr_pam_user);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000633 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
634 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000635 pam_end(sshpam_handle, sshpam_err);
636 sshpam_handle = NULL;
637 }
638 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000639 sshpam_err =
Darren Tucker77fc29e2004-09-11 23:07:03 +1000640 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
Darren Tucker17addf02004-03-30 20:57:57 +1000641 sshpam_authctxt = authctxt;
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100642
Damien Miller4f9f42a2003-05-10 19:28:02 +1000643 if (sshpam_err != PAM_SUCCESS) {
644 pam_end(sshpam_handle, sshpam_err);
645 sshpam_handle = NULL;
646 return (-1);
647 }
Damien Miller3ed92182016-03-08 14:01:29 -0800648 pam_rhost = auth_get_canonical_hostname(ssh, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000649 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000650 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
651 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000652 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000653 sshpam_handle = NULL;
654 return (-1);
655 }
656#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100657 /*
658 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
659 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000660 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100661 */
Damien Miller25d93422003-05-18 20:45:47 +1000662 debug("PAM: setting PAM_TTY to \"ssh\"");
663 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
664 if (sshpam_err != PAM_SUCCESS) {
665 pam_end(sshpam_handle, sshpam_err);
666 sshpam_handle = NULL;
667 return (-1);
668 }
669#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000670 return (0);
671}
672
673static void *
674sshpam_init_ctx(Authctxt *authctxt)
675{
676 struct pam_ctxt *ctxt;
677 int socks[2];
678
Darren Tuckera8df9242004-01-15 00:15:07 +1100679 debug3("PAM: %s entering", __func__);
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000680 /*
681 * Refuse to start if we don't have PAM enabled or do_pam_account
682 * has previously failed.
683 */
684 if (!options.use_pam || sshpam_account_status == 0)
Damien Miller4e448a32003-05-14 15:11:48 +1000685 return NULL;
686
Damien Miller4f9f42a2003-05-10 19:28:02 +1000687 /* Initialize PAM */
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100688 if (sshpam_init(authctxt) == -1) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000689 error("PAM: initialization failed");
690 return (NULL);
691 }
692
Darren Tucker29171e92007-05-20 15:20:08 +1000693 ctxt = xcalloc(1, sizeof *ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000694
695 /* Start the authentication thread */
696 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
697 error("PAM: failed create sockets: %s", strerror(errno));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000698 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000699 return (NULL);
700 }
701 ctxt->pam_psock = socks[0];
702 ctxt->pam_csock = socks[1];
703 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
704 error("PAM: failed to start authentication thread: %s",
705 strerror(errno));
706 close(socks[0]);
707 close(socks[1]);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000708 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000709 return (NULL);
710 }
Darren Tucker8846a072003-10-07 11:30:15 +1000711 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000712 return (ctxt);
713}
714
715static int
716sshpam_query(void *ctx, char **name, char **info,
717 u_int *num, char ***prompts, u_int **echo_on)
718{
Damien Miller3ed92182016-03-08 14:01:29 -0800719 struct ssh *ssh = active_state; /* XXX */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000720 Buffer buffer;
721 struct pam_ctxt *ctxt = ctx;
722 size_t plen;
723 u_char type;
724 char *msg;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000725 size_t len, mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000726
Darren Tuckera8df9242004-01-15 00:15:07 +1100727 debug3("PAM: %s entering", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000728 buffer_init(&buffer);
729 *name = xstrdup("");
730 *info = xstrdup("");
731 *prompts = xmalloc(sizeof(char *));
732 **prompts = NULL;
733 plen = 0;
734 *echo_on = xmalloc(sizeof(u_int));
735 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
736 type = buffer_get_char(&buffer);
737 msg = buffer_get_string(&buffer, NULL);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000738 mlen = strlen(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000739 switch (type) {
740 case PAM_PROMPT_ECHO_ON:
741 case PAM_PROMPT_ECHO_OFF:
742 *num = 1;
Damien Millerdaffc6a2004-10-16 18:52:44 +1000743 len = plen + mlen + 1;
Darren Tuckerd1680d32015-04-30 09:18:11 +1000744 **prompts = xreallocarray(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000745 strlcpy(**prompts + plen, msg, len - plen);
746 plen += mlen;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000747 **echo_on = (type == PAM_PROMPT_ECHO_ON);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000748 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000749 return (0);
750 case PAM_ERROR_MSG:
751 case PAM_TEXT_INFO:
752 /* accumulate messages */
Damien Millerdaffc6a2004-10-16 18:52:44 +1000753 len = plen + mlen + 2;
Darren Tuckerd1680d32015-04-30 09:18:11 +1000754 **prompts = xreallocarray(**prompts, 1, len);
Damien Millerdaffc6a2004-10-16 18:52:44 +1000755 strlcpy(**prompts + plen, msg, len - plen);
756 plen += mlen;
757 strlcat(**prompts + plen, "\n", len - plen);
758 plen++;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000759 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000760 break;
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000761 case PAM_ACCT_EXPIRED:
762 sshpam_account_status = 0;
763 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000764 case PAM_AUTH_ERR:
Darren Tucker2c77b7f2006-05-15 17:22:33 +1000765 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
Darren Tucker7b1e6952005-09-28 22:33:27 +1000766 if (**prompts != NULL && strlen(**prompts) != 0) {
767 *info = **prompts;
768 **prompts = NULL;
769 *num = 0;
770 **echo_on = 0;
771 ctxt->pam_done = -1;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000772 free(msg);
Darren Tucker7b1e6952005-09-28 22:33:27 +1000773 return 0;
774 }
775 /* FALLTHROUGH */
776 case PAM_SUCCESS:
Damien Miller4f9f42a2003-05-10 19:28:02 +1000777 if (**prompts != NULL) {
778 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100779 debug("PAM: %s", **prompts);
780 buffer_append(&loginmsg, **prompts,
781 strlen(**prompts));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000782 free(**prompts);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000783 **prompts = NULL;
784 }
785 if (type == PAM_SUCCESS) {
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100786 if (!sshpam_authctxt->valid ||
787 (sshpam_authctxt->pw->pw_uid == 0 &&
788 options.permit_root_login != PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100789 fatal("Internal error: PAM auth "
790 "succeeded when it should have "
791 "failed");
Damien Millerc756e9b2003-11-17 21:41:42 +1100792 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000793 *num = 0;
794 **echo_on = 0;
795 ctxt->pam_done = 1;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000796 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000797 return (0);
798 }
Darren Tucker2a9bf4b2004-04-18 11:00:26 +1000799 error("PAM: %s for %s%.100s from %.100s", msg,
800 sshpam_authctxt->valid ? "" : "illegal user ",
801 sshpam_authctxt->user,
Damien Miller3ed92182016-03-08 14:01:29 -0800802 auth_get_canonical_hostname(ssh, options.use_dns));
Darren Tucker439ce0d2003-10-09 14:20:15 +1000803 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000804 default:
805 *num = 0;
806 **echo_on = 0;
Darren Tuckerf60845f2013-06-02 08:07:31 +1000807 free(msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000808 ctxt->pam_done = -1;
809 return (-1);
810 }
811 }
812 return (-1);
813}
814
815/* XXX - see also comment in auth-chall.c:verify_response */
816static int
817sshpam_respond(void *ctx, u_int num, char **resp)
818{
819 Buffer buffer;
820 struct pam_ctxt *ctxt = ctx;
821
Darren Tucker1d4ebbf2006-01-29 16:46:13 +1100822 debug2("PAM: %s entering, %u responses", __func__, num);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000823 switch (ctxt->pam_done) {
824 case 1:
825 sshpam_authenticated = 1;
826 return (0);
827 case 0:
828 break;
829 default:
830 return (-1);
831 }
832 if (num != 1) {
833 error("PAM: expected one response, got %u", num);
834 return (-1);
835 }
836 buffer_init(&buffer);
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +1100837 if (sshpam_authctxt->valid &&
838 (sshpam_authctxt->pw->pw_uid != 0 ||
Damien Miller37294fb2005-07-17 17:18:49 +1000839 options.permit_root_login == PERMIT_YES))
Darren Tucker36a3d602005-01-20 12:43:38 +1100840 buffer_put_cstring(&buffer, *resp);
841 else
842 buffer_put_cstring(&buffer, badpw);
Damien Miller9bdba702003-11-17 21:27:55 +1100843 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
844 buffer_free(&buffer);
845 return (-1);
846 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000847 buffer_free(&buffer);
848 return (1);
849}
850
851static void
852sshpam_free_ctx(void *ctxtp)
853{
854 struct pam_ctxt *ctxt = ctxtp;
855
Darren Tuckera8df9242004-01-15 00:15:07 +1100856 debug3("PAM: %s entering", __func__);
Darren Tucker8846a072003-10-07 11:30:15 +1000857 sshpam_thread_cleanup();
Darren Tuckerf60845f2013-06-02 08:07:31 +1000858 free(ctxt);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000859 /*
860 * We don't call sshpam_cleanup() here because we may need the PAM
861 * handle at a later stage, e.g. when setting up a session. It's
862 * still on the cleanup list, so pam_end() *will* be called before
863 * the server process terminates.
864 */
865}
866
867KbdintDevice sshpam_device = {
868 "pam",
869 sshpam_init_ctx,
870 sshpam_query,
871 sshpam_respond,
872 sshpam_free_ctx
873};
874
875KbdintDevice mm_sshpam_device = {
876 "pam",
877 mm_sshpam_init_ctx,
878 mm_sshpam_query,
879 mm_sshpam_respond,
880 mm_sshpam_free_ctx
881};
882
883/*
884 * This replaces auth-pam.c
885 */
886void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100887start_pam(Authctxt *authctxt)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000888{
Damien Miller9d507da2003-05-14 15:31:12 +1000889 if (!options.use_pam)
890 fatal("PAM: initialisation requested when UsePAM=no");
891
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100892 if (sshpam_init(authctxt) == -1)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000893 fatal("PAM: initialisation failed");
894}
895
896void
897finish_pam(void)
898{
Darren Tucker8846a072003-10-07 11:30:15 +1000899 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000900}
901
Damien Miller1f499fd2003-08-25 13:08:49 +1000902u_int
903do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000904{
Darren Tucker77fc29e2004-09-11 23:07:03 +1000905 debug("%s: called", __func__);
Darren Tucker07705c72003-12-18 15:34:31 +1100906 if (sshpam_account_status != -1)
907 return (sshpam_account_status);
908
Damien Miller1f499fd2003-08-25 13:08:49 +1000909 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000910 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
911 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller94cf4c82005-07-17 17:04:47 +1000912
Darren Tucker07705c72003-12-18 15:34:31 +1100913 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
914 sshpam_account_status = 0;
915 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000916 }
917
Darren Tucker07705c72003-12-18 15:34:31 +1100918 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
Darren Tucker17db1c42004-06-19 12:54:38 +1000919 sshpam_password_change_required(1);
Darren Tucker07705c72003-12-18 15:34:31 +1100920
921 sshpam_account_status = 1;
922 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000923}
924
925void
Damien Miller341c6e62003-09-02 23:18:52 +1000926do_pam_set_tty(const char *tty)
927{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000928 if (tty != NULL) {
929 debug("PAM: setting PAM_TTY to \"%s\"", tty);
930 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
931 if (sshpam_err != PAM_SUCCESS)
932 fatal("PAM: failed to set PAM_TTY: %s",
933 pam_strerror(sshpam_handle, sshpam_err));
934 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000935}
936
937void
938do_pam_setcred(int init)
939{
940 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker77fc29e2004-09-11 23:07:03 +1000941 (const void *)&store_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000942 if (sshpam_err != PAM_SUCCESS)
943 fatal("PAM: failed to set PAM_CONV: %s",
944 pam_strerror(sshpam_handle, sshpam_err));
945 if (init) {
946 debug("PAM: establishing credentials");
947 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
948 } else {
949 debug("PAM: reinitializing credentials");
950 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
951 }
952 if (sshpam_err == PAM_SUCCESS) {
953 sshpam_cred_established = 1;
954 return;
955 }
956 if (sshpam_authenticated)
957 fatal("PAM: pam_setcred(): %s",
958 pam_strerror(sshpam_handle, sshpam_err));
959 else
960 debug("PAM: pam_setcred(): %s",
961 pam_strerror(sshpam_handle, sshpam_err));
962}
963
Damien Miller4f9f42a2003-05-10 19:28:02 +1000964static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +1000965sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000966 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000967{
968 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000969 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000970 int i;
971
Darren Tuckerba53b832004-02-17 20:46:59 +1100972 debug3("PAM: %s called with %d messages", __func__, n);
973
Damien Miller5c3a5582003-09-23 22:12:38 +1000974 *resp = NULL;
975
Darren Tucker18df00c2003-11-18 12:42:07 +1100976 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000977 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000978
Darren Tuckerd8093e42006-05-04 16:24:34 +1000979 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Damien Miller5c3a5582003-09-23 22:12:38 +1000980 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000981
Damien Miller4f9f42a2003-05-10 19:28:02 +1000982 for (i = 0; i < n; ++i) {
983 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
984 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000985 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100986 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000987 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000988 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000989 break;
990 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100991 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Darren Tucker22164712007-05-20 15:26:07 +1000992 if (fgets(input, sizeof input, stdin) == NULL)
993 input[0] = '\0';
Damien Millera6fb77f2004-07-19 09:39:11 +1000994 if ((reply[i].resp = strdup(input)) == NULL)
995 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000996 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000997 break;
998 case PAM_ERROR_MSG:
999 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +11001000 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +10001001 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001002 break;
1003 default:
1004 goto fail;
1005 }
1006 }
Damien Miller5c3a5582003-09-23 22:12:38 +10001007 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001008 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +10001009
Damien Miller4f9f42a2003-05-10 19:28:02 +10001010 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +10001011 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001012 free(reply[i].resp);
Damien Miller5c3a5582003-09-23 22:12:38 +10001013 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001014 free(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001015 return (PAM_CONV_ERR);
1016}
1017
Darren Tucker94befab2004-06-03 14:53:12 +10001018static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
Darren Tucker18df00c2003-11-18 12:42:07 +11001019
Damien Miller4f9f42a2003-05-10 19:28:02 +10001020/*
1021 * XXX this should be done in the authentication phase, but ssh1 doesn't
1022 * support that
1023 */
1024void
1025do_pam_chauthtok(void)
1026{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001027 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +10001028 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +10001029 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +11001030 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001031 if (sshpam_err != PAM_SUCCESS)
1032 fatal("PAM: failed to set PAM_CONV: %s",
1033 pam_strerror(sshpam_handle, sshpam_err));
1034 debug("PAM: changing password");
1035 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1036 if (sshpam_err != PAM_SUCCESS)
1037 fatal("PAM: pam_chauthtok(): %s",
1038 pam_strerror(sshpam_handle, sshpam_err));
1039}
1040
Darren Tucker18df00c2003-11-18 12:42:07 +11001041void
1042do_pam_session(void)
1043{
Darren Tucker1921ed92004-02-10 13:23:28 +11001044 debug3("PAM: opening session");
Damien Millera8e06ce2003-11-21 23:48:55 +11001045 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker5cf8ef72004-02-17 23:20:07 +11001046 (const void *)&store_conv);
Darren Tucker18df00c2003-11-18 12:42:07 +11001047 if (sshpam_err != PAM_SUCCESS)
1048 fatal("PAM: failed to set PAM_CONV: %s",
1049 pam_strerror(sshpam_handle, sshpam_err));
1050 sshpam_err = pam_open_session(sshpam_handle, 0);
Darren Tucker69687f42004-09-11 22:17:26 +10001051 if (sshpam_err == PAM_SUCCESS)
1052 sshpam_session_open = 1;
1053 else {
1054 sshpam_session_open = 0;
1055 disable_forwarding();
1056 error("PAM: pam_open_session(): %s",
Darren Tucker18df00c2003-11-18 12:42:07 +11001057 pam_strerror(sshpam_handle, sshpam_err));
Darren Tucker69687f42004-09-11 22:17:26 +10001058 }
1059
1060}
1061
1062int
1063is_pam_session_open(void)
1064{
1065 return sshpam_session_open;
Darren Tucker18df00c2003-11-18 12:42:07 +11001066}
1067
Damien Millera8e06ce2003-11-21 23:48:55 +11001068/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001069 * Set a PAM environment string. We need to do this so that the session
1070 * modules can handle things like Kerberos/GSI credentials that appear
1071 * during the ssh authentication process.
1072 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001073int
Damien Millera8e06ce2003-11-21 23:48:55 +11001074do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001075{
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001076 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +11001077#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +10001078 char *compound;
1079 size_t len;
1080
1081 len = strlen(name) + strlen(value) + 2;
1082 compound = xmalloc(len);
1083
1084 snprintf(compound, len, "%s=%s", name, value);
1085 ret = pam_putenv(sshpam_handle, compound);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001086 free(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001087#endif
Damien Millerf2728092003-09-17 07:24:25 +10001088
Darren Tucker49aaf4a2003-08-26 11:58:16 +10001089 return (ret);
1090}
1091
Damien Miller4f9f42a2003-05-10 19:28:02 +10001092char **
Damien Millerc756e9b2003-11-17 21:41:42 +11001093fetch_pam_child_environment(void)
1094{
1095 return sshpam_env;
1096}
1097
1098char **
Damien Miller4f9f42a2003-05-10 19:28:02 +10001099fetch_pam_environment(void)
1100{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001101 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102}
1103
1104void
1105free_pam_environment(char **env)
1106{
1107 char **envp;
1108
Damien Millere27c6cc2003-05-16 18:21:01 +10001109 if (env == NULL)
1110 return;
1111
Damien Miller4f9f42a2003-05-10 19:28:02 +10001112 for (envp = env; *envp; envp++)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001113 free(*envp);
1114 free(env);
Damien Millere72b7af1999-12-30 15:08:44 +11001115}
1116
Darren Tucker450a1582004-05-30 20:43:59 +10001117/*
1118 * "Blind" conversation function for password authentication. Assumes that
1119 * echo-off prompts are for the password and stores messages for later
1120 * display.
1121 */
1122static int
Darren Tuckerf08bdb52005-05-26 19:59:48 +10001123sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
Darren Tucker450a1582004-05-30 20:43:59 +10001124 struct pam_response **resp, void *data)
1125{
1126 struct pam_response *reply;
1127 int i;
1128 size_t len;
1129
1130 debug3("PAM: %s called with %d messages", __func__, n);
1131
1132 *resp = NULL;
1133
1134 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1135 return (PAM_CONV_ERR);
1136
Darren Tucker29171e92007-05-20 15:20:08 +10001137 if ((reply = calloc(n, sizeof(*reply))) == NULL)
Darren Tucker450a1582004-05-30 20:43:59 +10001138 return (PAM_CONV_ERR);
Darren Tucker450a1582004-05-30 20:43:59 +10001139
1140 for (i = 0; i < n; ++i) {
1141 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1142 case PAM_PROMPT_ECHO_OFF:
1143 if (sshpam_password == NULL)
1144 goto fail;
Damien Millera6fb77f2004-07-19 09:39:11 +10001145 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1146 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001147 reply[i].resp_retcode = PAM_SUCCESS;
1148 break;
1149 case PAM_ERROR_MSG:
1150 case PAM_TEXT_INFO:
1151 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1152 if (len > 0) {
1153 buffer_append(&loginmsg,
1154 PAM_MSG_MEMBER(msg, i, msg), len);
1155 buffer_append(&loginmsg, "\n", 1);
1156 }
Damien Millera6fb77f2004-07-19 09:39:11 +10001157 if ((reply[i].resp = strdup("")) == NULL)
1158 goto fail;
Darren Tucker450a1582004-05-30 20:43:59 +10001159 reply[i].resp_retcode = PAM_SUCCESS;
1160 break;
1161 default:
1162 goto fail;
1163 }
1164 }
1165 *resp = reply;
1166 return (PAM_SUCCESS);
1167
Damien Miller94cf4c82005-07-17 17:04:47 +10001168 fail:
Darren Tucker450a1582004-05-30 20:43:59 +10001169 for(i = 0; i < n; i++) {
Darren Tuckerf60845f2013-06-02 08:07:31 +10001170 free(reply[i].resp);
Darren Tucker450a1582004-05-30 20:43:59 +10001171 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001172 free(reply);
Darren Tucker450a1582004-05-30 20:43:59 +10001173 return (PAM_CONV_ERR);
1174}
1175
1176static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1177
1178/*
1179 * Attempt password authentication via PAM
1180 */
1181int
1182sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1183{
1184 int flags = (options.permit_empty_passwd == 0 ?
1185 PAM_DISALLOW_NULL_AUTHTOK : 0);
1186
1187 if (!options.use_pam || sshpam_handle == NULL)
1188 fatal("PAM: %s called when PAM disabled or failed to "
1189 "initialise.", __func__);
1190
1191 sshpam_password = password;
1192 sshpam_authctxt = authctxt;
1193
Darren Tuckere061b152004-05-30 22:04:56 +10001194 /*
1195 * If the user logging in is invalid, or is root but is not permitted
1196 * by PermitRootLogin, use an invalid password to prevent leaking
1197 * information via timing (eg if the PAM config has a delay on fail).
1198 */
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001199 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
Damien Miller37294fb2005-07-17 17:18:49 +10001200 options.permit_root_login != PERMIT_YES))
Darren Tuckere061b152004-05-30 22:04:56 +10001201 sshpam_password = badpw;
1202
Darren Tucker450a1582004-05-30 20:43:59 +10001203 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1204 (const void *)&passwd_conv);
1205 if (sshpam_err != PAM_SUCCESS)
1206 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1207 pam_strerror(sshpam_handle, sshpam_err));
1208
1209 sshpam_err = pam_authenticate(sshpam_handle, flags);
1210 sshpam_password = NULL;
Darren Tuckerd5bfa8f2005-01-20 13:29:51 +11001211 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
Darren Tucker450a1582004-05-30 20:43:59 +10001212 debug("PAM: password authentication accepted for %.100s",
1213 authctxt->user);
Damien Miller37294fb2005-07-17 17:18:49 +10001214 return 1;
Darren Tucker450a1582004-05-30 20:43:59 +10001215 } else {
1216 debug("PAM: password authentication failed for %.100s: %s",
1217 authctxt->valid ? authctxt->user : "an illegal user",
1218 pam_strerror(sshpam_handle, sshpam_err));
1219 return 0;
1220 }
1221}
Damien Millere72b7af1999-12-30 15:08:44 +11001222#endif /* USE_PAM */