• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
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.
9  *
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.
18  *
19  * 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.
30  */
31 /*
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  */
47 
48 /* Based on FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des */
49 
50 #include "includes.h"
51 
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <sys/wait.h>
55 
56 #include <errno.h>
57 #include <signal.h>
58 #include <stdarg.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #ifdef USE_PAM
63 #if defined(HAVE_SECURITY_PAM_APPL_H)
64 #include <security/pam_appl.h>
65 #elif defined (HAVE_PAM_PAM_APPL_H)
66 #include <pam/pam_appl.h>
67 #endif
68 
69 #if !defined(SSHD_PAM_SERVICE)
70 extern char *__progname;
71 # define SSHD_PAM_SERVICE		__progname
72 #endif
73 
74 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
75 #ifdef PAM_SUN_CODEBASE
76 # define sshpam_const		/* Solaris, HP-UX, SunOS */
77 #else
78 # define sshpam_const	const	/* LinuxPAM, OpenPAM, AIX */
79 #endif
80 
81 /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
82 #ifdef PAM_SUN_CODEBASE
83 # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
84 #else
85 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
86 #endif
87 
88 #include "xmalloc.h"
89 #include "buffer.h"
90 #include "key.h"
91 #include "hostfile.h"
92 #include "auth.h"
93 #include "auth-pam.h"
94 #include "canohost.h"
95 #include "log.h"
96 #include "msg.h"
97 #include "packet.h"
98 #include "misc.h"
99 #include "servconf.h"
100 #include "ssh2.h"
101 #include "auth-options.h"
102 #ifdef GSSAPI
103 #include "ssh-gss.h"
104 #endif
105 #include "monitor_wrap.h"
106 
107 extern ServerOptions options;
108 extern Buffer loginmsg;
109 extern int compat20;
110 extern u_int utmp_len;
111 
112 /* so we don't silently change behaviour */
113 #ifdef USE_POSIX_THREADS
114 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
115 #endif
116 
117 /*
118  * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
119  * and generally a bad idea.  Use at own risk and do not expect support if
120  * this breaks.
121  */
122 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
123 #include <pthread.h>
124 /*
125  * Avoid namespace clash when *not* using pthreads for systems *with*
126  * pthreads, which unconditionally define pthread_t via sys/types.h
127  * (e.g. Linux)
128  */
129 typedef pthread_t sp_pthread_t;
130 #else
131 typedef pid_t sp_pthread_t;
132 #endif
133 
134 struct pam_ctxt {
135 	sp_pthread_t	 pam_thread;
136 	int		 pam_psock;
137 	int		 pam_csock;
138 	int		 pam_done;
139 };
140 
141 static void sshpam_free_ctx(void *);
142 static struct pam_ctxt *cleanup_ctxt;
143 
144 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
145 /*
146  * Simulate threads with processes.
147  */
148 
149 static int sshpam_thread_status = -1;
150 static mysig_t sshpam_oldsig;
151 
152 static void
sshpam_sigchld_handler(int sig)153 sshpam_sigchld_handler(int sig)
154 {
155 	signal(SIGCHLD, SIG_DFL);
156 	if (cleanup_ctxt == NULL)
157 		return;	/* handler called after PAM cleanup, shouldn't happen */
158 	if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
159 	    <= 0) {
160 		/* PAM thread has not exitted, privsep slave must have */
161 		kill(cleanup_ctxt->pam_thread, SIGTERM);
162 		while (waitpid(cleanup_ctxt->pam_thread,
163 		    &sshpam_thread_status, 0) == -1) {
164 			if (errno == EINTR)
165 				continue;
166 			return;
167 		}
168 	}
169 	if (WIFSIGNALED(sshpam_thread_status) &&
170 	    WTERMSIG(sshpam_thread_status) == SIGTERM)
171 		return;	/* terminated by pthread_cancel */
172 	if (!WIFEXITED(sshpam_thread_status))
173 		sigdie("PAM: authentication thread exited unexpectedly");
174 	if (WEXITSTATUS(sshpam_thread_status) != 0)
175 		sigdie("PAM: authentication thread exited uncleanly");
176 }
177 
178 /* ARGSUSED */
179 static void
pthread_exit(void * value)180 pthread_exit(void *value)
181 {
182 	_exit(0);
183 }
184 
185 /* ARGSUSED */
186 static int
pthread_create(sp_pthread_t * thread,const void * attr,void * (* thread_start)(void *),void * arg)187 pthread_create(sp_pthread_t *thread, const void *attr,
188     void *(*thread_start)(void *), void *arg)
189 {
190 	pid_t pid;
191 	struct pam_ctxt *ctx = arg;
192 
193 	sshpam_thread_status = -1;
194 	switch ((pid = fork())) {
195 	case -1:
196 		error("fork(): %s", strerror(errno));
197 		return (-1);
198 	case 0:
199 		close(ctx->pam_psock);
200 		ctx->pam_psock = -1;
201 		thread_start(arg);
202 		_exit(1);
203 	default:
204 		*thread = pid;
205 		close(ctx->pam_csock);
206 		ctx->pam_csock = -1;
207 		sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
208 		return (0);
209 	}
210 }
211 
212 static int
pthread_cancel(sp_pthread_t thread)213 pthread_cancel(sp_pthread_t thread)
214 {
215 	signal(SIGCHLD, sshpam_oldsig);
216 	return (kill(thread, SIGTERM));
217 }
218 
219 /* ARGSUSED */
220 static int
pthread_join(sp_pthread_t thread,void ** value)221 pthread_join(sp_pthread_t thread, void **value)
222 {
223 	int status;
224 
225 	if (sshpam_thread_status != -1)
226 		return (sshpam_thread_status);
227 	signal(SIGCHLD, sshpam_oldsig);
228 	while (waitpid(thread, &status, 0) == -1) {
229 		if (errno == EINTR)
230 			continue;
231 		fatal("%s: waitpid: %s", __func__, strerror(errno));
232 	}
233 	return (status);
234 }
235 #endif
236 
237 
238 static pam_handle_t *sshpam_handle = NULL;
239 static int sshpam_err = 0;
240 static int sshpam_authenticated = 0;
241 static int sshpam_session_open = 0;
242 static int sshpam_cred_established = 0;
243 static int sshpam_account_status = -1;
244 static int sshpam_maxtries_reached = 0;
245 static char **sshpam_env = NULL;
246 static Authctxt *sshpam_authctxt = NULL;
247 static const char *sshpam_password = NULL;
248 
249 /* Some PAM implementations don't implement this */
250 #ifndef HAVE_PAM_GETENVLIST
251 static char **
pam_getenvlist(pam_handle_t * pamh)252 pam_getenvlist(pam_handle_t *pamh)
253 {
254 	/*
255 	 * XXX - If necessary, we can still support envrionment passing
256 	 * for platforms without pam_getenvlist by searching for known
257 	 * env vars (e.g. KRB5CCNAME) from the PAM environment.
258 	 */
259 	 return NULL;
260 }
261 #endif
262 
263 /*
264  * Some platforms, notably Solaris, do not enforce password complexity
265  * rules during pam_chauthtok() if the real uid of the calling process
266  * is 0, on the assumption that it's being called by "passwd" run by root.
267  * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
268  * the right thing.
269  */
270 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
271 static int
sshpam_chauthtok_ruid(pam_handle_t * pamh,int flags)272 sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
273 {
274 	int result;
275 
276 	if (sshpam_authctxt == NULL)
277 		fatal("PAM: sshpam_authctxt not initialized");
278 	if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
279 		fatal("%s: setreuid failed: %s", __func__, strerror(errno));
280 	result = pam_chauthtok(pamh, flags);
281 	if (setreuid(0, -1) == -1)
282 		fatal("%s: setreuid failed: %s", __func__, strerror(errno));
283 	return result;
284 }
285 # define pam_chauthtok(a,b)	(sshpam_chauthtok_ruid((a), (b)))
286 #endif
287 
288 void
sshpam_password_change_required(int reqd)289 sshpam_password_change_required(int reqd)
290 {
291 	debug3("%s %d", __func__, reqd);
292 	if (sshpam_authctxt == NULL)
293 		fatal("%s: PAM authctxt not initialized", __func__);
294 	sshpam_authctxt->force_pwchange = reqd;
295 	if (reqd) {
296 		no_port_forwarding_flag |= 2;
297 		no_agent_forwarding_flag |= 2;
298 		no_x11_forwarding_flag |= 2;
299 	} else {
300 		no_port_forwarding_flag &= ~2;
301 		no_agent_forwarding_flag &= ~2;
302 		no_x11_forwarding_flag &= ~2;
303 	}
304 }
305 
306 /* Import regular and PAM environment from subprocess */
307 static void
import_environments(Buffer * b)308 import_environments(Buffer *b)
309 {
310 	char *env;
311 	u_int i, num_env;
312 	int err;
313 
314 	debug3("PAM: %s entering", __func__);
315 
316 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
317 	/* Import variables set by do_pam_account */
318 	sshpam_account_status = buffer_get_int(b);
319 	sshpam_password_change_required(buffer_get_int(b));
320 
321 	/* Import environment from subprocess */
322 	num_env = buffer_get_int(b);
323 	if (num_env > 1024)
324 		fatal("%s: received %u environment variables, expected <= 1024",
325 		    __func__, num_env);
326 	sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
327 	debug3("PAM: num env strings %d", num_env);
328 	for(i = 0; i < num_env; i++)
329 		sshpam_env[i] = buffer_get_string(b, NULL);
330 
331 	sshpam_env[num_env] = NULL;
332 
333 	/* Import PAM environment from subprocess */
334 	num_env = buffer_get_int(b);
335 	debug("PAM: num PAM env strings %d", num_env);
336 	for(i = 0; i < num_env; i++) {
337 		env = buffer_get_string(b, NULL);
338 
339 #ifdef HAVE_PAM_PUTENV
340 		/* Errors are not fatal here */
341 		if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
342 			error("PAM: pam_putenv: %s",
343 			    pam_strerror(sshpam_handle, sshpam_err));
344 		}
345 #endif
346 	}
347 #endif
348 }
349 
350 /*
351  * Conversation function for authentication thread.
352  */
353 static int
sshpam_thread_conv(int n,sshpam_const struct pam_message ** msg,struct pam_response ** resp,void * data)354 sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
355     struct pam_response **resp, void *data)
356 {
357 	Buffer buffer;
358 	struct pam_ctxt *ctxt;
359 	struct pam_response *reply;
360 	int i;
361 
362 	debug3("PAM: %s entering, %d messages", __func__, n);
363 	*resp = NULL;
364 
365 	if (data == NULL) {
366 		error("PAM: conversation function passed a null context");
367 		return (PAM_CONV_ERR);
368 	}
369 	ctxt = data;
370 	if (n <= 0 || n > PAM_MAX_NUM_MSG)
371 		return (PAM_CONV_ERR);
372 
373 	if ((reply = calloc(n, sizeof(*reply))) == NULL)
374 		return (PAM_CONV_ERR);
375 
376 	buffer_init(&buffer);
377 	for (i = 0; i < n; ++i) {
378 		switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
379 		case PAM_PROMPT_ECHO_OFF:
380 		case PAM_PROMPT_ECHO_ON:
381 			buffer_put_cstring(&buffer,
382 			    PAM_MSG_MEMBER(msg, i, msg));
383 			if (ssh_msg_send(ctxt->pam_csock,
384 			    PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
385 				goto fail;
386 			if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
387 				goto fail;
388 			if (buffer_get_char(&buffer) != PAM_AUTHTOK)
389 				goto fail;
390 			reply[i].resp = buffer_get_string(&buffer, NULL);
391 			break;
392 		case PAM_ERROR_MSG:
393 		case PAM_TEXT_INFO:
394 			buffer_put_cstring(&buffer,
395 			    PAM_MSG_MEMBER(msg, i, msg));
396 			if (ssh_msg_send(ctxt->pam_csock,
397 			    PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
398 				goto fail;
399 			break;
400 		default:
401 			goto fail;
402 		}
403 		buffer_clear(&buffer);
404 	}
405 	buffer_free(&buffer);
406 	*resp = reply;
407 	return (PAM_SUCCESS);
408 
409  fail:
410 	for(i = 0; i < n; i++) {
411 		free(reply[i].resp);
412 	}
413 	free(reply);
414 	buffer_free(&buffer);
415 	return (PAM_CONV_ERR);
416 }
417 
418 /*
419  * Authentication thread.
420  */
421 static void *
sshpam_thread(void * ctxtp)422 sshpam_thread(void *ctxtp)
423 {
424 	struct pam_ctxt *ctxt = ctxtp;
425 	Buffer buffer;
426 	struct pam_conv sshpam_conv;
427 	int flags = (options.permit_empty_passwd == 0 ?
428 	    PAM_DISALLOW_NULL_AUTHTOK : 0);
429 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
430 	extern char **environ;
431 	char **env_from_pam;
432 	u_int i;
433 	const char *pam_user;
434 	const char **ptr_pam_user = &pam_user;
435 	char *tz = getenv("TZ");
436 
437 	sshpam_err = pam_get_item(sshpam_handle, PAM_USER,
438 	    (sshpam_const void **)ptr_pam_user);
439 	if (sshpam_err != PAM_SUCCESS)
440 		goto auth_fail;
441 
442 	environ[0] = NULL;
443 	if (tz != NULL)
444 		if (setenv("TZ", tz, 1) == -1)
445 			error("PAM: could not set TZ environment: %s",
446 			    strerror(errno));
447 
448 	if (sshpam_authctxt != NULL) {
449 		setproctitle("%s [pam]",
450 		    sshpam_authctxt->valid ? pam_user : "unknown");
451 	}
452 #endif
453 
454 	sshpam_conv.conv = sshpam_thread_conv;
455 	sshpam_conv.appdata_ptr = ctxt;
456 
457 	if (sshpam_authctxt == NULL)
458 		fatal("%s: PAM authctxt not initialized", __func__);
459 
460 	buffer_init(&buffer);
461 	sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
462 	    (const void *)&sshpam_conv);
463 	if (sshpam_err != PAM_SUCCESS)
464 		goto auth_fail;
465 	sshpam_err = pam_authenticate(sshpam_handle, flags);
466 	if (sshpam_err == PAM_MAXTRIES)
467 		sshpam_set_maxtries_reached(1);
468 	if (sshpam_err != PAM_SUCCESS)
469 		goto auth_fail;
470 
471 	if (compat20) {
472 		if (!do_pam_account()) {
473 			sshpam_err = PAM_ACCT_EXPIRED;
474 			goto auth_fail;
475 		}
476 		if (sshpam_authctxt->force_pwchange) {
477 			sshpam_err = pam_chauthtok(sshpam_handle,
478 			    PAM_CHANGE_EXPIRED_AUTHTOK);
479 			if (sshpam_err != PAM_SUCCESS)
480 				goto auth_fail;
481 			sshpam_password_change_required(0);
482 		}
483 	}
484 
485 	buffer_put_cstring(&buffer, "OK");
486 
487 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
488 	/* Export variables set by do_pam_account */
489 	buffer_put_int(&buffer, sshpam_account_status);
490 	buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
491 
492 	/* Export any environment strings set in child */
493 	for(i = 0; environ[i] != NULL; i++)
494 		; /* Count */
495 	buffer_put_int(&buffer, i);
496 	for(i = 0; environ[i] != NULL; i++)
497 		buffer_put_cstring(&buffer, environ[i]);
498 
499 	/* Export any environment strings set by PAM in child */
500 	env_from_pam = pam_getenvlist(sshpam_handle);
501 	for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
502 		; /* Count */
503 	buffer_put_int(&buffer, i);
504 	for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
505 		buffer_put_cstring(&buffer, env_from_pam[i]);
506 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
507 
508 	/* XXX - can't do much about an error here */
509 	ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
510 	buffer_free(&buffer);
511 	pthread_exit(NULL);
512 
513  auth_fail:
514 	buffer_put_cstring(&buffer,
515 	    pam_strerror(sshpam_handle, sshpam_err));
516 	/* XXX - can't do much about an error here */
517 	if (sshpam_err == PAM_ACCT_EXPIRED)
518 		ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
519 	else if (sshpam_maxtries_reached)
520 		ssh_msg_send(ctxt->pam_csock, PAM_MAXTRIES, &buffer);
521 	else
522 		ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
523 	buffer_free(&buffer);
524 	pthread_exit(NULL);
525 
526 	return (NULL); /* Avoid warning for non-pthread case */
527 }
528 
529 void
sshpam_thread_cleanup(void)530 sshpam_thread_cleanup(void)
531 {
532 	struct pam_ctxt *ctxt = cleanup_ctxt;
533 
534 	debug3("PAM: %s entering", __func__);
535 	if (ctxt != NULL && ctxt->pam_thread != 0) {
536 		pthread_cancel(ctxt->pam_thread);
537 		pthread_join(ctxt->pam_thread, NULL);
538 		close(ctxt->pam_psock);
539 		close(ctxt->pam_csock);
540 		memset(ctxt, 0, sizeof(*ctxt));
541 		cleanup_ctxt = NULL;
542 	}
543 }
544 
545 static int
sshpam_null_conv(int n,sshpam_const struct pam_message ** msg,struct pam_response ** resp,void * data)546 sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
547     struct pam_response **resp, void *data)
548 {
549 	debug3("PAM: %s entering, %d messages", __func__, n);
550 	return (PAM_CONV_ERR);
551 }
552 
553 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
554 
555 static int
sshpam_store_conv(int n,sshpam_const struct pam_message ** msg,struct pam_response ** resp,void * data)556 sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
557     struct pam_response **resp, void *data)
558 {
559 	struct pam_response *reply;
560 	int i;
561 	size_t len;
562 
563 	debug3("PAM: %s called with %d messages", __func__, n);
564 	*resp = NULL;
565 
566 	if (n <= 0 || n > PAM_MAX_NUM_MSG)
567 		return (PAM_CONV_ERR);
568 
569 	if ((reply = calloc(n, sizeof(*reply))) == NULL)
570 		return (PAM_CONV_ERR);
571 
572 	for (i = 0; i < n; ++i) {
573 		switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
574 		case PAM_ERROR_MSG:
575 		case PAM_TEXT_INFO:
576 			len = strlen(PAM_MSG_MEMBER(msg, i, msg));
577 			buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
578 			buffer_append(&loginmsg, "\n", 1 );
579 			reply[i].resp_retcode = PAM_SUCCESS;
580 			break;
581 		default:
582 			goto fail;
583 		}
584 	}
585 	*resp = reply;
586 	return (PAM_SUCCESS);
587 
588  fail:
589 	for(i = 0; i < n; i++) {
590 		free(reply[i].resp);
591 	}
592 	free(reply);
593 	return (PAM_CONV_ERR);
594 }
595 
596 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
597 
598 void
sshpam_cleanup(void)599 sshpam_cleanup(void)
600 {
601 	if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
602 		return;
603 	debug("PAM: cleanup");
604 	pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
605 	if (sshpam_session_open) {
606 		debug("PAM: closing session");
607 		pam_close_session(sshpam_handle, PAM_SILENT);
608 		sshpam_session_open = 0;
609 	}
610 	if (sshpam_cred_established) {
611 		debug("PAM: deleting credentials");
612 		pam_setcred(sshpam_handle, PAM_DELETE_CRED);
613 		sshpam_cred_established = 0;
614 	}
615 	sshpam_authenticated = 0;
616 	pam_end(sshpam_handle, sshpam_err);
617 	sshpam_handle = NULL;
618 }
619 
620 static int
sshpam_init(Authctxt * authctxt)621 sshpam_init(Authctxt *authctxt)
622 {
623 	const char *pam_rhost, *pam_user, *user = authctxt->user;
624 	const char **ptr_pam_user = &pam_user;
625 	struct ssh *ssh = active_state; /* XXX */
626 
627 	if (sshpam_handle != NULL) {
628 		/* We already have a PAM context; check if the user matches */
629 		sshpam_err = pam_get_item(sshpam_handle,
630 		    PAM_USER, (sshpam_const void **)ptr_pam_user);
631 		if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
632 			return (0);
633 		pam_end(sshpam_handle, sshpam_err);
634 		sshpam_handle = NULL;
635 	}
636 	debug("PAM: initializing for \"%s\"", user);
637 	sshpam_err =
638 	    pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
639 	sshpam_authctxt = authctxt;
640 
641 	if (sshpam_err != PAM_SUCCESS) {
642 		pam_end(sshpam_handle, sshpam_err);
643 		sshpam_handle = NULL;
644 		return (-1);
645 	}
646 	pam_rhost = auth_get_canonical_hostname(ssh, options.use_dns);
647 	debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
648 	sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
649 	if (sshpam_err != PAM_SUCCESS) {
650 		pam_end(sshpam_handle, sshpam_err);
651 		sshpam_handle = NULL;
652 		return (-1);
653 	}
654 #ifdef PAM_TTY_KLUDGE
655 	/*
656 	 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
657 	 * sshd doesn't set the tty until too late in the auth process and
658 	 * may not even set one (for tty-less connections)
659 	 */
660 	debug("PAM: setting PAM_TTY to \"ssh\"");
661 	sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
662 	if (sshpam_err != PAM_SUCCESS) {
663 		pam_end(sshpam_handle, sshpam_err);
664 		sshpam_handle = NULL;
665 		return (-1);
666 	}
667 #endif
668 	return (0);
669 }
670 
671 static void *
sshpam_init_ctx(Authctxt * authctxt)672 sshpam_init_ctx(Authctxt *authctxt)
673 {
674 	struct pam_ctxt *ctxt;
675 	int socks[2];
676 
677 	debug3("PAM: %s entering", __func__);
678 	/*
679 	 * Refuse to start if we don't have PAM enabled or do_pam_account
680 	 * has previously failed.
681 	 */
682 	if (!options.use_pam || sshpam_account_status == 0)
683 		return NULL;
684 
685 	/* Initialize PAM */
686 	if (sshpam_init(authctxt) == -1) {
687 		error("PAM: initialization failed");
688 		return (NULL);
689 	}
690 
691 	ctxt = xcalloc(1, sizeof *ctxt);
692 
693 	/* Start the authentication thread */
694 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
695 		error("PAM: failed create sockets: %s", strerror(errno));
696 		free(ctxt);
697 		return (NULL);
698 	}
699 	ctxt->pam_psock = socks[0];
700 	ctxt->pam_csock = socks[1];
701 	if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
702 		error("PAM: failed to start authentication thread: %s",
703 		    strerror(errno));
704 		close(socks[0]);
705 		close(socks[1]);
706 		free(ctxt);
707 		return (NULL);
708 	}
709 	cleanup_ctxt = ctxt;
710 	return (ctxt);
711 }
712 
713 static int
sshpam_query(void * ctx,char ** name,char ** info,u_int * num,char *** prompts,u_int ** echo_on)714 sshpam_query(void *ctx, char **name, char **info,
715     u_int *num, char ***prompts, u_int **echo_on)
716 {
717 	struct ssh *ssh = active_state; /* XXX */
718 	Buffer buffer;
719 	struct pam_ctxt *ctxt = ctx;
720 	size_t plen;
721 	u_char type;
722 	char *msg;
723 	size_t len, mlen;
724 
725 	debug3("PAM: %s entering", __func__);
726 	buffer_init(&buffer);
727 	*name = xstrdup("");
728 	*info = xstrdup("");
729 	*prompts = xmalloc(sizeof(char *));
730 	**prompts = NULL;
731 	plen = 0;
732 	*echo_on = xmalloc(sizeof(u_int));
733 	while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
734 		type = buffer_get_char(&buffer);
735 		msg = buffer_get_string(&buffer, NULL);
736 		mlen = strlen(msg);
737 		switch (type) {
738 		case PAM_PROMPT_ECHO_ON:
739 		case PAM_PROMPT_ECHO_OFF:
740 			*num = 1;
741 			len = plen + mlen + 1;
742 			**prompts = xreallocarray(**prompts, 1, len);
743 			strlcpy(**prompts + plen, msg, len - plen);
744 			plen += mlen;
745 			**echo_on = (type == PAM_PROMPT_ECHO_ON);
746 			free(msg);
747 			return (0);
748 		case PAM_ERROR_MSG:
749 		case PAM_TEXT_INFO:
750 			/* accumulate messages */
751 			len = plen + mlen + 2;
752 			**prompts = xreallocarray(**prompts, 1, len);
753 			strlcpy(**prompts + plen, msg, len - plen);
754 			plen += mlen;
755 			strlcat(**prompts + plen, "\n", len - plen);
756 			plen++;
757 			free(msg);
758 			break;
759 		case PAM_ACCT_EXPIRED:
760 		case PAM_MAXTRIES:
761 			if (type == PAM_ACCT_EXPIRED)
762 				sshpam_account_status = 0;
763 			if (type == PAM_MAXTRIES)
764 				sshpam_set_maxtries_reached(1);
765 			/* FALLTHROUGH */
766 		case PAM_AUTH_ERR:
767 			debug3("PAM: %s", pam_strerror(sshpam_handle, type));
768 			if (**prompts != NULL && strlen(**prompts) != 0) {
769 				*info = **prompts;
770 				**prompts = NULL;
771 				*num = 0;
772 				**echo_on = 0;
773 				ctxt->pam_done = -1;
774 				free(msg);
775 				return 0;
776 			}
777 			/* FALLTHROUGH */
778 		case PAM_SUCCESS:
779 			if (**prompts != NULL) {
780 				/* drain any accumulated messages */
781 				debug("PAM: %s", **prompts);
782 				buffer_append(&loginmsg, **prompts,
783 				    strlen(**prompts));
784 				free(**prompts);
785 				**prompts = NULL;
786 			}
787 			if (type == PAM_SUCCESS) {
788 				if (!sshpam_authctxt->valid ||
789 				    (sshpam_authctxt->pw->pw_uid == 0 &&
790 				    options.permit_root_login != PERMIT_YES))
791 					fatal("Internal error: PAM auth "
792 					    "succeeded when it should have "
793 					    "failed");
794 				import_environments(&buffer);
795 				*num = 0;
796 				**echo_on = 0;
797 				ctxt->pam_done = 1;
798 				free(msg);
799 				return (0);
800 			}
801 			error("PAM: %s for %s%.100s from %.100s", msg,
802 			    sshpam_authctxt->valid ? "" : "illegal user ",
803 			    sshpam_authctxt->user,
804 			    auth_get_canonical_hostname(ssh, options.use_dns));
805 			/* FALLTHROUGH */
806 		default:
807 			*num = 0;
808 			**echo_on = 0;
809 			free(msg);
810 			ctxt->pam_done = -1;
811 			return (-1);
812 		}
813 	}
814 	return (-1);
815 }
816 
817 /*
818  * Returns a junk password of identical length to that the user supplied.
819  * Used to mitigate timing attacks against crypt(3)/PAM stacks that
820  * vary processing time in proportion to password length.
821  */
822 static char *
fake_password(const char * wire_password)823 fake_password(const char *wire_password)
824 {
825 	const char junk[] = "\b\n\r\177INCORRECT";
826 	char *ret = NULL;
827 	size_t i, l = wire_password != NULL ? strlen(wire_password) : 0;
828 
829 	if (l >= INT_MAX)
830 		fatal("%s: password length too long: %zu", __func__, l);
831 
832 	ret = malloc(l + 1);
833 	if (ret == NULL)
834 		return NULL;
835 	for (i = 0; i < l; i++)
836 		ret[i] = junk[i % (sizeof(junk) - 1)];
837 	ret[i] = '\0';
838 	return ret;
839 }
840 
841 /* XXX - see also comment in auth-chall.c:verify_response */
842 static int
sshpam_respond(void * ctx,u_int num,char ** resp)843 sshpam_respond(void *ctx, u_int num, char **resp)
844 {
845 	Buffer buffer;
846 	struct pam_ctxt *ctxt = ctx;
847 	char *fake;
848 
849 	debug2("PAM: %s entering, %u responses", __func__, num);
850 	switch (ctxt->pam_done) {
851 	case 1:
852 		sshpam_authenticated = 1;
853 		return (0);
854 	case 0:
855 		break;
856 	default:
857 		return (-1);
858 	}
859 	if (num != 1) {
860 		error("PAM: expected one response, got %u", num);
861 		return (-1);
862 	}
863 	buffer_init(&buffer);
864 	if (sshpam_authctxt->valid &&
865 	    (sshpam_authctxt->pw->pw_uid != 0 ||
866 	    options.permit_root_login == PERMIT_YES))
867 		buffer_put_cstring(&buffer, *resp);
868 	else {
869 		fake = fake_password(*resp);
870 		buffer_put_cstring(&buffer, fake);
871 		free(fake);
872 	}
873 	if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
874 		buffer_free(&buffer);
875 		return (-1);
876 	}
877 	buffer_free(&buffer);
878 	return (1);
879 }
880 
881 static void
sshpam_free_ctx(void * ctxtp)882 sshpam_free_ctx(void *ctxtp)
883 {
884 	struct pam_ctxt *ctxt = ctxtp;
885 
886 	debug3("PAM: %s entering", __func__);
887 	sshpam_thread_cleanup();
888 	free(ctxt);
889 	/*
890 	 * We don't call sshpam_cleanup() here because we may need the PAM
891 	 * handle at a later stage, e.g. when setting up a session.  It's
892 	 * still on the cleanup list, so pam_end() *will* be called before
893 	 * the server process terminates.
894 	 */
895 }
896 
897 KbdintDevice sshpam_device = {
898 	"pam",
899 	sshpam_init_ctx,
900 	sshpam_query,
901 	sshpam_respond,
902 	sshpam_free_ctx
903 };
904 
905 KbdintDevice mm_sshpam_device = {
906 	"pam",
907 	mm_sshpam_init_ctx,
908 	mm_sshpam_query,
909 	mm_sshpam_respond,
910 	mm_sshpam_free_ctx
911 };
912 
913 /*
914  * This replaces auth-pam.c
915  */
916 void
start_pam(Authctxt * authctxt)917 start_pam(Authctxt *authctxt)
918 {
919 	if (!options.use_pam)
920 		fatal("PAM: initialisation requested when UsePAM=no");
921 
922 	if (sshpam_init(authctxt) == -1)
923 		fatal("PAM: initialisation failed");
924 }
925 
926 void
finish_pam(void)927 finish_pam(void)
928 {
929 	sshpam_cleanup();
930 }
931 
932 u_int
do_pam_account(void)933 do_pam_account(void)
934 {
935 	debug("%s: called", __func__);
936 	if (sshpam_account_status != -1)
937 		return (sshpam_account_status);
938 
939 	sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
940 	debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
941 	    pam_strerror(sshpam_handle, sshpam_err));
942 
943 	if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
944 		sshpam_account_status = 0;
945 		return (sshpam_account_status);
946 	}
947 
948 	if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
949 		sshpam_password_change_required(1);
950 
951 	sshpam_account_status = 1;
952 	return (sshpam_account_status);
953 }
954 
955 void
do_pam_setcred(int init)956 do_pam_setcred(int init)
957 {
958 	sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
959 	    (const void *)&store_conv);
960 	if (sshpam_err != PAM_SUCCESS)
961 		fatal("PAM: failed to set PAM_CONV: %s",
962 		    pam_strerror(sshpam_handle, sshpam_err));
963 	if (init) {
964 		debug("PAM: establishing credentials");
965 		sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
966 	} else {
967 		debug("PAM: reinitializing credentials");
968 		sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
969 	}
970 	if (sshpam_err == PAM_SUCCESS) {
971 		sshpam_cred_established = 1;
972 		return;
973 	}
974 	if (sshpam_authenticated)
975 		fatal("PAM: pam_setcred(): %s",
976 		    pam_strerror(sshpam_handle, sshpam_err));
977 	else
978 		debug("PAM: pam_setcred(): %s",
979 		    pam_strerror(sshpam_handle, sshpam_err));
980 }
981 
982 static int
sshpam_tty_conv(int n,sshpam_const struct pam_message ** msg,struct pam_response ** resp,void * data)983 sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
984     struct pam_response **resp, void *data)
985 {
986 	char input[PAM_MAX_MSG_SIZE];
987 	struct pam_response *reply;
988 	int i;
989 
990 	debug3("PAM: %s called with %d messages", __func__, n);
991 
992 	*resp = NULL;
993 
994 	if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
995 		return (PAM_CONV_ERR);
996 
997 	if ((reply = calloc(n, sizeof(*reply))) == NULL)
998 		return (PAM_CONV_ERR);
999 
1000 	for (i = 0; i < n; ++i) {
1001 		switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1002 		case PAM_PROMPT_ECHO_OFF:
1003 			reply[i].resp =
1004 			    read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
1005 			    RP_ALLOW_STDIN);
1006 			reply[i].resp_retcode = PAM_SUCCESS;
1007 			break;
1008 		case PAM_PROMPT_ECHO_ON:
1009 			fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1010 			if (fgets(input, sizeof input, stdin) == NULL)
1011 				input[0] = '\0';
1012 			if ((reply[i].resp = strdup(input)) == NULL)
1013 				goto fail;
1014 			reply[i].resp_retcode = PAM_SUCCESS;
1015 			break;
1016 		case PAM_ERROR_MSG:
1017 		case PAM_TEXT_INFO:
1018 			fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1019 			reply[i].resp_retcode = PAM_SUCCESS;
1020 			break;
1021 		default:
1022 			goto fail;
1023 		}
1024 	}
1025 	*resp = reply;
1026 	return (PAM_SUCCESS);
1027 
1028  fail:
1029 	for(i = 0; i < n; i++) {
1030 		free(reply[i].resp);
1031 	}
1032 	free(reply);
1033 	return (PAM_CONV_ERR);
1034 }
1035 
1036 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
1037 
1038 /*
1039  * XXX this should be done in the authentication phase, but ssh1 doesn't
1040  * support that
1041  */
1042 void
do_pam_chauthtok(void)1043 do_pam_chauthtok(void)
1044 {
1045 	if (use_privsep)
1046 		fatal("Password expired (unable to change with privsep)");
1047 	sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1048 	    (const void *)&tty_conv);
1049 	if (sshpam_err != PAM_SUCCESS)
1050 		fatal("PAM: failed to set PAM_CONV: %s",
1051 		    pam_strerror(sshpam_handle, sshpam_err));
1052 	debug("PAM: changing password");
1053 	sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1054 	if (sshpam_err != PAM_SUCCESS)
1055 		fatal("PAM: pam_chauthtok(): %s",
1056 		    pam_strerror(sshpam_handle, sshpam_err));
1057 }
1058 
1059 void
do_pam_session(void)1060 do_pam_session(void)
1061 {
1062 	debug3("PAM: opening session");
1063 	sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1064 	    (const void *)&store_conv);
1065 	if (sshpam_err != PAM_SUCCESS)
1066 		fatal("PAM: failed to set PAM_CONV: %s",
1067 		    pam_strerror(sshpam_handle, sshpam_err));
1068 	sshpam_err = pam_open_session(sshpam_handle, 0);
1069 	if (sshpam_err == PAM_SUCCESS)
1070 		sshpam_session_open = 1;
1071 	else {
1072 		sshpam_session_open = 0;
1073 		disable_forwarding();
1074 		error("PAM: pam_open_session(): %s",
1075 		    pam_strerror(sshpam_handle, sshpam_err));
1076 	}
1077 
1078 }
1079 
1080 int
is_pam_session_open(void)1081 is_pam_session_open(void)
1082 {
1083 	return sshpam_session_open;
1084 }
1085 
1086 /*
1087  * Set a PAM environment string. We need to do this so that the session
1088  * modules can handle things like Kerberos/GSI credentials that appear
1089  * during the ssh authentication process.
1090  */
1091 int
do_pam_putenv(char * name,char * value)1092 do_pam_putenv(char *name, char *value)
1093 {
1094 	int ret = 1;
1095 #ifdef HAVE_PAM_PUTENV
1096 	char *compound;
1097 	size_t len;
1098 
1099 	len = strlen(name) + strlen(value) + 2;
1100 	compound = xmalloc(len);
1101 
1102 	snprintf(compound, len, "%s=%s", name, value);
1103 	ret = pam_putenv(sshpam_handle, compound);
1104 	free(compound);
1105 #endif
1106 
1107 	return (ret);
1108 }
1109 
1110 char **
fetch_pam_child_environment(void)1111 fetch_pam_child_environment(void)
1112 {
1113 	return sshpam_env;
1114 }
1115 
1116 char **
fetch_pam_environment(void)1117 fetch_pam_environment(void)
1118 {
1119 	return (pam_getenvlist(sshpam_handle));
1120 }
1121 
1122 void
free_pam_environment(char ** env)1123 free_pam_environment(char **env)
1124 {
1125 	char **envp;
1126 
1127 	if (env == NULL)
1128 		return;
1129 
1130 	for (envp = env; *envp; envp++)
1131 		free(*envp);
1132 	free(env);
1133 }
1134 
1135 /*
1136  * "Blind" conversation function for password authentication.  Assumes that
1137  * echo-off prompts are for the password and stores messages for later
1138  * display.
1139  */
1140 static int
sshpam_passwd_conv(int n,sshpam_const struct pam_message ** msg,struct pam_response ** resp,void * data)1141 sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1142     struct pam_response **resp, void *data)
1143 {
1144 	struct pam_response *reply;
1145 	int i;
1146 	size_t len;
1147 
1148 	debug3("PAM: %s called with %d messages", __func__, n);
1149 
1150 	*resp = NULL;
1151 
1152 	if (n <= 0 || n > PAM_MAX_NUM_MSG)
1153 		return (PAM_CONV_ERR);
1154 
1155 	if ((reply = calloc(n, sizeof(*reply))) == NULL)
1156 		return (PAM_CONV_ERR);
1157 
1158 	for (i = 0; i < n; ++i) {
1159 		switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1160 		case PAM_PROMPT_ECHO_OFF:
1161 			if (sshpam_password == NULL)
1162 				goto fail;
1163 			if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1164 				goto fail;
1165 			reply[i].resp_retcode = PAM_SUCCESS;
1166 			break;
1167 		case PAM_ERROR_MSG:
1168 		case PAM_TEXT_INFO:
1169 			len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1170 			if (len > 0) {
1171 				buffer_append(&loginmsg,
1172 				    PAM_MSG_MEMBER(msg, i, msg), len);
1173 				buffer_append(&loginmsg, "\n", 1);
1174 			}
1175 			if ((reply[i].resp = strdup("")) == NULL)
1176 				goto fail;
1177 			reply[i].resp_retcode = PAM_SUCCESS;
1178 			break;
1179 		default:
1180 			goto fail;
1181 		}
1182 	}
1183 	*resp = reply;
1184 	return (PAM_SUCCESS);
1185 
1186  fail:
1187 	for(i = 0; i < n; i++) {
1188 		free(reply[i].resp);
1189 	}
1190 	free(reply);
1191 	return (PAM_CONV_ERR);
1192 }
1193 
1194 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1195 
1196 /*
1197  * Attempt password authentication via PAM
1198  */
1199 int
sshpam_auth_passwd(Authctxt * authctxt,const char * password)1200 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1201 {
1202 	int flags = (options.permit_empty_passwd == 0 ?
1203 	    PAM_DISALLOW_NULL_AUTHTOK : 0);
1204 	char *fake = NULL;
1205 
1206 	if (!options.use_pam || sshpam_handle == NULL)
1207 		fatal("PAM: %s called when PAM disabled or failed to "
1208 		    "initialise.", __func__);
1209 
1210 	sshpam_password = password;
1211 	sshpam_authctxt = authctxt;
1212 
1213 	/*
1214 	 * If the user logging in is invalid, or is root but is not permitted
1215 	 * by PermitRootLogin, use an invalid password to prevent leaking
1216 	 * information via timing (eg if the PAM config has a delay on fail).
1217 	 */
1218 	if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1219 	    options.permit_root_login != PERMIT_YES))
1220 		sshpam_password = fake = fake_password(password);
1221 
1222 	sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1223 	    (const void *)&passwd_conv);
1224 	if (sshpam_err != PAM_SUCCESS)
1225 		fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1226 		    pam_strerror(sshpam_handle, sshpam_err));
1227 
1228 	sshpam_err = pam_authenticate(sshpam_handle, flags);
1229 	sshpam_password = NULL;
1230 	free(fake);
1231 	if (sshpam_err == PAM_MAXTRIES)
1232 		sshpam_set_maxtries_reached(1);
1233 	if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1234 		debug("PAM: password authentication accepted for %.100s",
1235 		    authctxt->user);
1236 		return 1;
1237 	} else {
1238 		debug("PAM: password authentication failed for %.100s: %s",
1239 		    authctxt->valid ? authctxt->user : "an illegal user",
1240 		    pam_strerror(sshpam_handle, sshpam_err));
1241 		return 0;
1242 	}
1243 }
1244 
1245 int
sshpam_get_maxtries_reached(void)1246 sshpam_get_maxtries_reached(void)
1247 {
1248 	return sshpam_maxtries_reached;
1249 }
1250 
1251 void
sshpam_set_maxtries_reached(int reached)1252 sshpam_set_maxtries_reached(int reached)
1253 {
1254 	if (reached == 0 || sshpam_maxtries_reached)
1255 		return;
1256 	sshpam_maxtries_reached = 1;
1257 	options.password_authentication = 0;
1258 	options.kbd_interactive_authentication = 0;
1259 	options.challenge_response_authentication = 0;
1260 }
1261 #endif /* USE_PAM */
1262