2 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
33 * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au>
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.
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.
48 /* Based on FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des */
52 #include <sys/types.h>
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>
69 #if !defined(SSHD_PAM_SERVICE)
70 extern char *__progname
;
71 # define SSHD_PAM_SERVICE __progname
74 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
75 #ifdef PAM_SUN_CODEBASE
76 # define sshpam_const /* Solaris, HP-UX, SunOS */
78 # define sshpam_const const /* LinuxPAM, OpenPAM, AIX */
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)
85 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
101 #include "auth-options.h"
105 #include "monitor_wrap.h"
107 extern ServerOptions options
;
108 extern Buffer loginmsg
;
109 extern u_int utmp_len
;
111 /* so we don't silently change behaviour */
112 #ifdef USE_POSIX_THREADS
113 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
117 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
118 * and generally a bad idea. Use at own risk and do not expect support if
121 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
124 * Avoid namespace clash when *not* using pthreads for systems *with*
125 * pthreads, which unconditionally define pthread_t via sys/types.h
128 typedef pthread_t sp_pthread_t
;
130 typedef pid_t sp_pthread_t
;
134 sp_pthread_t pam_thread
;
140 static void sshpam_free_ctx(void *);
141 static struct pam_ctxt
*cleanup_ctxt
;
143 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
145 * Simulate threads with processes.
148 static int sshpam_thread_status
= -1;
149 static mysig_t sshpam_oldsig
;
152 sshpam_sigchld_handler(int sig
)
154 signal(SIGCHLD
, SIG_DFL
);
155 if (cleanup_ctxt
== NULL
)
156 return; /* handler called after PAM cleanup, shouldn't happen */
157 if (waitpid(cleanup_ctxt
->pam_thread
, &sshpam_thread_status
, WNOHANG
)
159 /* PAM thread has not exitted, privsep slave must have */
160 kill(cleanup_ctxt
->pam_thread
, SIGTERM
);
161 while (waitpid(cleanup_ctxt
->pam_thread
,
162 &sshpam_thread_status
, 0) == -1) {
168 if (WIFSIGNALED(sshpam_thread_status
) &&
169 WTERMSIG(sshpam_thread_status
) == SIGTERM
)
170 return; /* terminated by pthread_cancel */
171 if (!WIFEXITED(sshpam_thread_status
))
172 sigdie("PAM: authentication thread exited unexpectedly");
173 if (WEXITSTATUS(sshpam_thread_status
) != 0)
174 sigdie("PAM: authentication thread exited uncleanly");
179 pthread_exit(void *value
)
186 pthread_create(sp_pthread_t
*thread
, const void *attr
,
187 void *(*thread_start
)(void *), void *arg
)
190 struct pam_ctxt
*ctx
= arg
;
192 sshpam_thread_status
= -1;
193 switch ((pid
= fork())) {
195 error("fork(): %s", strerror(errno
));
198 close(ctx
->pam_psock
);
204 close(ctx
->pam_csock
);
206 sshpam_oldsig
= signal(SIGCHLD
, sshpam_sigchld_handler
);
212 pthread_cancel(sp_pthread_t thread
)
214 signal(SIGCHLD
, sshpam_oldsig
);
215 return (kill(thread
, SIGTERM
));
220 pthread_join(sp_pthread_t thread
, void **value
)
224 if (sshpam_thread_status
!= -1)
225 return (sshpam_thread_status
);
226 signal(SIGCHLD
, sshpam_oldsig
);
227 while (waitpid(thread
, &status
, 0) == -1) {
230 fatal("%s: waitpid: %s", __func__
, strerror(errno
));
237 static pam_handle_t
*sshpam_handle
= NULL
;
238 static int sshpam_err
= 0;
239 static int sshpam_authenticated
= 0;
240 static int sshpam_session_open
= 0;
241 static int sshpam_cred_established
= 0;
242 static int sshpam_account_status
= -1;
243 static int sshpam_maxtries_reached
= 0;
244 static char **sshpam_env
= NULL
;
245 static Authctxt
*sshpam_authctxt
= NULL
;
246 static const char *sshpam_password
= NULL
;
248 /* Some PAM implementations don't implement this */
249 #ifndef HAVE_PAM_GETENVLIST
251 pam_getenvlist(pam_handle_t
*pamh
)
254 * XXX - If necessary, we can still support envrionment passing
255 * for platforms without pam_getenvlist by searching for known
256 * env vars (e.g. KRB5CCNAME) from the PAM environment.
263 * Some platforms, notably Solaris, do not enforce password complexity
264 * rules during pam_chauthtok() if the real uid of the calling process
265 * is 0, on the assumption that it's being called by "passwd" run by root.
266 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
269 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
271 sshpam_chauthtok_ruid(pam_handle_t
*pamh
, int flags
)
275 if (sshpam_authctxt
== NULL
)
276 fatal("PAM: sshpam_authctxt not initialized");
277 if (setreuid(sshpam_authctxt
->pw
->pw_uid
, -1) == -1)
278 fatal("%s: setreuid failed: %s", __func__
, strerror(errno
));
279 result
= pam_chauthtok(pamh
, flags
);
280 if (setreuid(0, -1) == -1)
281 fatal("%s: setreuid failed: %s", __func__
, strerror(errno
));
284 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
288 sshpam_password_change_required(int reqd
)
290 debug3("%s %d", __func__
, reqd
);
291 if (sshpam_authctxt
== NULL
)
292 fatal("%s: PAM authctxt not initialized", __func__
);
293 sshpam_authctxt
->force_pwchange
= reqd
;
295 no_port_forwarding_flag
|= 2;
296 no_agent_forwarding_flag
|= 2;
297 no_x11_forwarding_flag
|= 2;
299 no_port_forwarding_flag
&= ~2;
300 no_agent_forwarding_flag
&= ~2;
301 no_x11_forwarding_flag
&= ~2;
305 /* Import regular and PAM environment from subprocess */
307 import_environments(Buffer
*b
)
313 debug3("PAM: %s entering", __func__
);
315 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
316 /* Import variables set by do_pam_account */
317 sshpam_account_status
= buffer_get_int(b
);
318 sshpam_password_change_required(buffer_get_int(b
));
320 /* Import environment from subprocess */
321 num_env
= buffer_get_int(b
);
323 fatal("%s: received %u environment variables, expected <= 1024",
325 sshpam_env
= xcalloc(num_env
+ 1, sizeof(*sshpam_env
));
326 debug3("PAM: num env strings %d", num_env
);
327 for(i
= 0; i
< num_env
; i
++)
328 sshpam_env
[i
] = buffer_get_string(b
, NULL
);
330 sshpam_env
[num_env
] = NULL
;
332 /* Import PAM environment from subprocess */
333 num_env
= buffer_get_int(b
);
334 debug("PAM: num PAM env strings %d", num_env
);
335 for(i
= 0; i
< num_env
; i
++) {
336 env
= buffer_get_string(b
, NULL
);
338 #ifdef HAVE_PAM_PUTENV
339 /* Errors are not fatal here */
340 if ((err
= pam_putenv(sshpam_handle
, env
)) != PAM_SUCCESS
) {
341 error("PAM: pam_putenv: %s",
342 pam_strerror(sshpam_handle
, sshpam_err
));
350 * Conversation function for authentication thread.
353 sshpam_thread_conv(int n
, sshpam_const
struct pam_message
**msg
,
354 struct pam_response
**resp
, void *data
)
357 struct pam_ctxt
*ctxt
;
358 struct pam_response
*reply
;
361 debug3("PAM: %s entering, %d messages", __func__
, n
);
365 error("PAM: conversation function passed a null context");
366 return (PAM_CONV_ERR
);
369 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
370 return (PAM_CONV_ERR
);
372 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
373 return (PAM_CONV_ERR
);
375 buffer_init(&buffer
);
376 for (i
= 0; i
< n
; ++i
) {
377 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
378 case PAM_PROMPT_ECHO_OFF
:
379 case PAM_PROMPT_ECHO_ON
:
380 buffer_put_cstring(&buffer
,
381 PAM_MSG_MEMBER(msg
, i
, msg
));
382 if (ssh_msg_send(ctxt
->pam_csock
,
383 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
385 if (ssh_msg_recv(ctxt
->pam_csock
, &buffer
) == -1)
387 if (buffer_get_char(&buffer
) != PAM_AUTHTOK
)
389 reply
[i
].resp
= buffer_get_string(&buffer
, NULL
);
393 buffer_put_cstring(&buffer
,
394 PAM_MSG_MEMBER(msg
, i
, msg
));
395 if (ssh_msg_send(ctxt
->pam_csock
,
396 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
402 buffer_clear(&buffer
);
404 buffer_free(&buffer
);
406 return (PAM_SUCCESS
);
409 for(i
= 0; i
< n
; i
++) {
413 buffer_free(&buffer
);
414 return (PAM_CONV_ERR
);
418 * Authentication thread.
421 sshpam_thread(void *ctxtp
)
423 struct pam_ctxt
*ctxt
= ctxtp
;
425 struct pam_conv sshpam_conv
;
426 int flags
= (options
.permit_empty_passwd
== 0 ?
427 PAM_DISALLOW_NULL_AUTHTOK
: 0);
428 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
429 extern char **environ
;
432 const char *pam_user
;
433 const char **ptr_pam_user
= &pam_user
;
434 char *tz
= getenv("TZ");
436 sshpam_err
= pam_get_item(sshpam_handle
, PAM_USER
,
437 (sshpam_const
void **)ptr_pam_user
);
438 if (sshpam_err
!= PAM_SUCCESS
)
443 if (setenv("TZ", tz
, 1) == -1)
444 error("PAM: could not set TZ environment: %s",
447 if (sshpam_authctxt
!= NULL
) {
448 setproctitle("%s [pam]",
449 sshpam_authctxt
->valid
? pam_user
: "unknown");
453 sshpam_conv
.conv
= sshpam_thread_conv
;
454 sshpam_conv
.appdata_ptr
= ctxt
;
456 if (sshpam_authctxt
== NULL
)
457 fatal("%s: PAM authctxt not initialized", __func__
);
459 buffer_init(&buffer
);
460 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
461 (const void *)&sshpam_conv
);
462 if (sshpam_err
!= PAM_SUCCESS
)
464 sshpam_err
= pam_authenticate(sshpam_handle
, flags
);
465 if (sshpam_err
== PAM_MAXTRIES
)
466 sshpam_set_maxtries_reached(1);
467 if (sshpam_err
!= PAM_SUCCESS
)
470 if (!do_pam_account()) {
471 sshpam_err
= PAM_ACCT_EXPIRED
;
474 if (sshpam_authctxt
->force_pwchange
) {
475 sshpam_err
= pam_chauthtok(sshpam_handle
,
476 PAM_CHANGE_EXPIRED_AUTHTOK
);
477 if (sshpam_err
!= PAM_SUCCESS
)
479 sshpam_password_change_required(0);
482 buffer_put_cstring(&buffer
, "OK");
484 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
485 /* Export variables set by do_pam_account */
486 buffer_put_int(&buffer
, sshpam_account_status
);
487 buffer_put_int(&buffer
, sshpam_authctxt
->force_pwchange
);
489 /* Export any environment strings set in child */
490 for(i
= 0; environ
[i
] != NULL
; i
++)
492 buffer_put_int(&buffer
, i
);
493 for(i
= 0; environ
[i
] != NULL
; i
++)
494 buffer_put_cstring(&buffer
, environ
[i
]);
496 /* Export any environment strings set by PAM in child */
497 env_from_pam
= pam_getenvlist(sshpam_handle
);
498 for(i
= 0; env_from_pam
!= NULL
&& env_from_pam
[i
] != NULL
; i
++)
500 buffer_put_int(&buffer
, i
);
501 for(i
= 0; env_from_pam
!= NULL
&& env_from_pam
[i
] != NULL
; i
++)
502 buffer_put_cstring(&buffer
, env_from_pam
[i
]);
503 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
505 /* XXX - can't do much about an error here */
506 ssh_msg_send(ctxt
->pam_csock
, sshpam_err
, &buffer
);
507 buffer_free(&buffer
);
511 buffer_put_cstring(&buffer
,
512 pam_strerror(sshpam_handle
, sshpam_err
));
513 /* XXX - can't do much about an error here */
514 if (sshpam_err
== PAM_ACCT_EXPIRED
)
515 ssh_msg_send(ctxt
->pam_csock
, PAM_ACCT_EXPIRED
, &buffer
);
516 else if (sshpam_maxtries_reached
)
517 ssh_msg_send(ctxt
->pam_csock
, PAM_MAXTRIES
, &buffer
);
519 ssh_msg_send(ctxt
->pam_csock
, PAM_AUTH_ERR
, &buffer
);
520 buffer_free(&buffer
);
523 return (NULL
); /* Avoid warning for non-pthread case */
527 sshpam_thread_cleanup(void)
529 struct pam_ctxt
*ctxt
= cleanup_ctxt
;
531 debug3("PAM: %s entering", __func__
);
532 if (ctxt
!= NULL
&& ctxt
->pam_thread
!= 0) {
533 pthread_cancel(ctxt
->pam_thread
);
534 pthread_join(ctxt
->pam_thread
, NULL
);
535 close(ctxt
->pam_psock
);
536 close(ctxt
->pam_csock
);
537 memset(ctxt
, 0, sizeof(*ctxt
));
543 sshpam_null_conv(int n
, sshpam_const
struct pam_message
**msg
,
544 struct pam_response
**resp
, void *data
)
546 debug3("PAM: %s entering, %d messages", __func__
, n
);
547 return (PAM_CONV_ERR
);
550 static struct pam_conv null_conv
= { sshpam_null_conv
, NULL
};
553 sshpam_store_conv(int n
, sshpam_const
struct pam_message
**msg
,
554 struct pam_response
**resp
, void *data
)
556 struct pam_response
*reply
;
560 debug3("PAM: %s called with %d messages", __func__
, n
);
563 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
564 return (PAM_CONV_ERR
);
566 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
567 return (PAM_CONV_ERR
);
569 for (i
= 0; i
< n
; ++i
) {
570 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
573 len
= strlen(PAM_MSG_MEMBER(msg
, i
, msg
));
574 buffer_append(&loginmsg
, PAM_MSG_MEMBER(msg
, i
, msg
), len
);
575 buffer_append(&loginmsg
, "\n", 1 );
576 reply
[i
].resp_retcode
= PAM_SUCCESS
;
583 return (PAM_SUCCESS
);
586 for(i
= 0; i
< n
; i
++) {
590 return (PAM_CONV_ERR
);
593 static struct pam_conv store_conv
= { sshpam_store_conv
, NULL
};
598 if (sshpam_handle
== NULL
|| (use_privsep
&& !mm_is_monitor()))
600 debug("PAM: cleanup");
601 pam_set_item(sshpam_handle
, PAM_CONV
, (const void *)&null_conv
);
602 if (sshpam_session_open
) {
603 debug("PAM: closing session");
604 pam_close_session(sshpam_handle
, PAM_SILENT
);
605 sshpam_session_open
= 0;
607 if (sshpam_cred_established
) {
608 debug("PAM: deleting credentials");
609 pam_setcred(sshpam_handle
, PAM_DELETE_CRED
);
610 sshpam_cred_established
= 0;
612 sshpam_authenticated
= 0;
613 pam_end(sshpam_handle
, sshpam_err
);
614 sshpam_handle
= NULL
;
618 sshpam_init(Authctxt
*authctxt
)
620 const char *pam_rhost
, *pam_user
, *user
= authctxt
->user
;
621 const char **ptr_pam_user
= &pam_user
;
622 struct ssh
*ssh
= active_state
; /* XXX */
624 if (sshpam_handle
!= NULL
) {
625 /* We already have a PAM context; check if the user matches */
626 sshpam_err
= pam_get_item(sshpam_handle
,
627 PAM_USER
, (sshpam_const
void **)ptr_pam_user
);
628 if (sshpam_err
== PAM_SUCCESS
&& strcmp(user
, pam_user
) == 0)
630 pam_end(sshpam_handle
, sshpam_err
);
631 sshpam_handle
= NULL
;
633 debug("PAM: initializing for \"%s\"", user
);
635 pam_start(SSHD_PAM_SERVICE
, user
, &store_conv
, &sshpam_handle
);
636 sshpam_authctxt
= authctxt
;
638 if (sshpam_err
!= PAM_SUCCESS
) {
639 pam_end(sshpam_handle
, sshpam_err
);
640 sshpam_handle
= NULL
;
643 pam_rhost
= auth_get_canonical_hostname(ssh
, options
.use_dns
);
644 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost
);
645 sshpam_err
= pam_set_item(sshpam_handle
, PAM_RHOST
, pam_rhost
);
646 if (sshpam_err
!= PAM_SUCCESS
) {
647 pam_end(sshpam_handle
, sshpam_err
);
648 sshpam_handle
= NULL
;
651 #ifdef PAM_TTY_KLUDGE
653 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
654 * sshd doesn't set the tty until too late in the auth process and
655 * may not even set one (for tty-less connections)
657 debug("PAM: setting PAM_TTY to \"ssh\"");
658 sshpam_err
= pam_set_item(sshpam_handle
, PAM_TTY
, "ssh");
659 if (sshpam_err
!= PAM_SUCCESS
) {
660 pam_end(sshpam_handle
, sshpam_err
);
661 sshpam_handle
= NULL
;
669 sshpam_init_ctx(Authctxt
*authctxt
)
671 struct pam_ctxt
*ctxt
;
674 debug3("PAM: %s entering", __func__
);
676 * Refuse to start if we don't have PAM enabled or do_pam_account
677 * has previously failed.
679 if (!options
.use_pam
|| sshpam_account_status
== 0)
683 if (sshpam_init(authctxt
) == -1) {
684 error("PAM: initialization failed");
688 ctxt
= xcalloc(1, sizeof *ctxt
);
690 /* Start the authentication thread */
691 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, socks
) == -1) {
692 error("PAM: failed create sockets: %s", strerror(errno
));
696 ctxt
->pam_psock
= socks
[0];
697 ctxt
->pam_csock
= socks
[1];
698 if (pthread_create(&ctxt
->pam_thread
, NULL
, sshpam_thread
, ctxt
) == -1) {
699 error("PAM: failed to start authentication thread: %s",
711 sshpam_query(void *ctx
, char **name
, char **info
,
712 u_int
*num
, char ***prompts
, u_int
**echo_on
)
714 struct ssh
*ssh
= active_state
; /* XXX */
716 struct pam_ctxt
*ctxt
= ctx
;
722 debug3("PAM: %s entering", __func__
);
723 buffer_init(&buffer
);
726 *prompts
= xmalloc(sizeof(char *));
729 *echo_on
= xmalloc(sizeof(u_int
));
730 while (ssh_msg_recv(ctxt
->pam_psock
, &buffer
) == 0) {
731 type
= buffer_get_char(&buffer
);
732 msg
= buffer_get_string(&buffer
, NULL
);
735 case PAM_PROMPT_ECHO_ON
:
736 case PAM_PROMPT_ECHO_OFF
:
738 len
= plen
+ mlen
+ 1;
739 **prompts
= xreallocarray(**prompts
, 1, len
);
740 strlcpy(**prompts
+ plen
, msg
, len
- plen
);
742 **echo_on
= (type
== PAM_PROMPT_ECHO_ON
);
747 /* accumulate messages */
748 len
= plen
+ mlen
+ 2;
749 **prompts
= xreallocarray(**prompts
, 1, len
);
750 strlcpy(**prompts
+ plen
, msg
, len
- plen
);
752 strlcat(**prompts
+ plen
, "\n", len
- plen
);
756 case PAM_ACCT_EXPIRED
:
758 if (type
== PAM_ACCT_EXPIRED
)
759 sshpam_account_status
= 0;
760 if (type
== PAM_MAXTRIES
)
761 sshpam_set_maxtries_reached(1);
764 debug3("PAM: %s", pam_strerror(sshpam_handle
, type
));
765 if (**prompts
!= NULL
&& strlen(**prompts
) != 0) {
776 if (**prompts
!= NULL
) {
777 /* drain any accumulated messages */
778 debug("PAM: %s", **prompts
);
779 buffer_append(&loginmsg
, **prompts
,
784 if (type
== PAM_SUCCESS
) {
785 if (!sshpam_authctxt
->valid
||
786 (sshpam_authctxt
->pw
->pw_uid
== 0 &&
787 options
.permit_root_login
!= PERMIT_YES
))
788 fatal("Internal error: PAM auth "
789 "succeeded when it should have "
791 import_environments(&buffer
);
798 error("PAM: %s for %s%.100s from %.100s", msg
,
799 sshpam_authctxt
->valid
? "" : "illegal user ",
800 sshpam_authctxt
->user
,
801 auth_get_canonical_hostname(ssh
, options
.use_dns
));
815 * Returns a junk password of identical length to that the user supplied.
816 * Used to mitigate timing attacks against crypt(3)/PAM stacks that
817 * vary processing time in proportion to password length.
820 fake_password(const char *wire_password
)
822 const char junk
[] = "\b\n\r\177INCORRECT";
824 size_t i
, l
= wire_password
!= NULL
? strlen(wire_password
) : 0;
827 fatal("%s: password length too long: %zu", __func__
, l
);
832 for (i
= 0; i
< l
; i
++)
833 ret
[i
] = junk
[i
% (sizeof(junk
) - 1)];
838 /* XXX - see also comment in auth-chall.c:verify_response */
840 sshpam_respond(void *ctx
, u_int num
, char **resp
)
843 struct pam_ctxt
*ctxt
= ctx
;
846 debug2("PAM: %s entering, %u responses", __func__
, num
);
847 switch (ctxt
->pam_done
) {
849 sshpam_authenticated
= 1;
857 error("PAM: expected one response, got %u", num
);
860 buffer_init(&buffer
);
861 if (sshpam_authctxt
->valid
&&
862 (sshpam_authctxt
->pw
->pw_uid
!= 0 ||
863 options
.permit_root_login
== PERMIT_YES
))
864 buffer_put_cstring(&buffer
, *resp
);
866 fake
= fake_password(*resp
);
867 buffer_put_cstring(&buffer
, fake
);
870 if (ssh_msg_send(ctxt
->pam_psock
, PAM_AUTHTOK
, &buffer
) == -1) {
871 buffer_free(&buffer
);
874 buffer_free(&buffer
);
879 sshpam_free_ctx(void *ctxtp
)
881 struct pam_ctxt
*ctxt
= ctxtp
;
883 debug3("PAM: %s entering", __func__
);
884 sshpam_thread_cleanup();
887 * We don't call sshpam_cleanup() here because we may need the PAM
888 * handle at a later stage, e.g. when setting up a session. It's
889 * still on the cleanup list, so pam_end() *will* be called before
890 * the server process terminates.
894 KbdintDevice sshpam_device
= {
902 KbdintDevice mm_sshpam_device
= {
911 * This replaces auth-pam.c
914 start_pam(Authctxt
*authctxt
)
916 if (!options
.use_pam
)
917 fatal("PAM: initialisation requested when UsePAM=no");
919 if (sshpam_init(authctxt
) == -1)
920 fatal("PAM: initialisation failed");
930 expose_authinfo(const char *caller
)
935 * Expose authentication information to PAM.
936 * The enviornment variable is versioned. Please increment the
937 * version suffix if the format of session_info changes.
939 if (sshpam_authctxt
->session_info
== NULL
)
940 auth_info
= xstrdup("");
941 else if ((auth_info
= sshbuf_dup_string(
942 sshpam_authctxt
->session_info
)) == NULL
)
943 fatal("%s: sshbuf_dup_string failed", __func__
);
945 debug2("%s: auth information in SSH_AUTH_INFO_0", caller
);
946 do_pam_putenv("SSH_AUTH_INFO_0", auth_info
);
953 debug("%s: called", __func__
);
954 if (sshpam_account_status
!= -1)
955 return (sshpam_account_status
);
957 expose_authinfo(__func__
);
959 sshpam_err
= pam_acct_mgmt(sshpam_handle
, 0);
960 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__
, sshpam_err
,
961 pam_strerror(sshpam_handle
, sshpam_err
));
963 if (sshpam_err
!= PAM_SUCCESS
&& sshpam_err
!= PAM_NEW_AUTHTOK_REQD
) {
964 sshpam_account_status
= 0;
965 return (sshpam_account_status
);
968 if (sshpam_err
== PAM_NEW_AUTHTOK_REQD
)
969 sshpam_password_change_required(1);
971 sshpam_account_status
= 1;
972 return (sshpam_account_status
);
976 do_pam_setcred(int init
)
978 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
979 (const void *)&store_conv
);
980 if (sshpam_err
!= PAM_SUCCESS
)
981 fatal("PAM: failed to set PAM_CONV: %s",
982 pam_strerror(sshpam_handle
, sshpam_err
));
984 debug("PAM: establishing credentials");
985 sshpam_err
= pam_setcred(sshpam_handle
, PAM_ESTABLISH_CRED
);
987 debug("PAM: reinitializing credentials");
988 sshpam_err
= pam_setcred(sshpam_handle
, PAM_REINITIALIZE_CRED
);
990 if (sshpam_err
== PAM_SUCCESS
) {
991 sshpam_cred_established
= 1;
994 if (sshpam_authenticated
)
995 fatal("PAM: pam_setcred(): %s",
996 pam_strerror(sshpam_handle
, sshpam_err
));
998 debug("PAM: pam_setcred(): %s",
999 pam_strerror(sshpam_handle
, sshpam_err
));
1003 sshpam_tty_conv(int n
, sshpam_const
struct pam_message
**msg
,
1004 struct pam_response
**resp
, void *data
)
1006 char input
[PAM_MAX_MSG_SIZE
];
1007 struct pam_response
*reply
;
1010 debug3("PAM: %s called with %d messages", __func__
, n
);
1014 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
|| !isatty(STDIN_FILENO
))
1015 return (PAM_CONV_ERR
);
1017 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
1018 return (PAM_CONV_ERR
);
1020 for (i
= 0; i
< n
; ++i
) {
1021 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
1022 case PAM_PROMPT_ECHO_OFF
:
1024 read_passphrase(PAM_MSG_MEMBER(msg
, i
, msg
),
1026 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1028 case PAM_PROMPT_ECHO_ON
:
1029 fprintf(stderr
, "%s\n", PAM_MSG_MEMBER(msg
, i
, msg
));
1030 if (fgets(input
, sizeof input
, stdin
) == NULL
)
1032 if ((reply
[i
].resp
= strdup(input
)) == NULL
)
1034 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1038 fprintf(stderr
, "%s\n", PAM_MSG_MEMBER(msg
, i
, msg
));
1039 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1046 return (PAM_SUCCESS
);
1049 for(i
= 0; i
< n
; i
++) {
1050 free(reply
[i
].resp
);
1053 return (PAM_CONV_ERR
);
1056 static struct pam_conv tty_conv
= { sshpam_tty_conv
, NULL
};
1059 * XXX this should be done in the authentication phase, but ssh1 doesn't
1063 do_pam_chauthtok(void)
1066 fatal("Password expired (unable to change with privsep)");
1067 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1068 (const void *)&tty_conv
);
1069 if (sshpam_err
!= PAM_SUCCESS
)
1070 fatal("PAM: failed to set PAM_CONV: %s",
1071 pam_strerror(sshpam_handle
, sshpam_err
));
1072 debug("PAM: changing password");
1073 sshpam_err
= pam_chauthtok(sshpam_handle
, PAM_CHANGE_EXPIRED_AUTHTOK
);
1074 if (sshpam_err
!= PAM_SUCCESS
)
1075 fatal("PAM: pam_chauthtok(): %s",
1076 pam_strerror(sshpam_handle
, sshpam_err
));
1080 do_pam_session(void)
1082 debug3("PAM: opening session");
1084 expose_authinfo(__func__
);
1086 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1087 (const void *)&store_conv
);
1088 if (sshpam_err
!= PAM_SUCCESS
)
1089 fatal("PAM: failed to set PAM_CONV: %s",
1090 pam_strerror(sshpam_handle
, sshpam_err
));
1091 sshpam_err
= pam_open_session(sshpam_handle
, 0);
1092 if (sshpam_err
== PAM_SUCCESS
)
1093 sshpam_session_open
= 1;
1095 sshpam_session_open
= 0;
1096 disable_forwarding();
1097 error("PAM: pam_open_session(): %s",
1098 pam_strerror(sshpam_handle
, sshpam_err
));
1104 is_pam_session_open(void)
1106 return sshpam_session_open
;
1110 * Set a PAM environment string. We need to do this so that the session
1111 * modules can handle things like Kerberos/GSI credentials that appear
1112 * during the ssh authentication process.
1115 do_pam_putenv(char *name
, char *value
)
1118 #ifdef HAVE_PAM_PUTENV
1122 len
= strlen(name
) + strlen(value
) + 2;
1123 compound
= xmalloc(len
);
1125 snprintf(compound
, len
, "%s=%s", name
, value
);
1126 ret
= pam_putenv(sshpam_handle
, compound
);
1134 fetch_pam_child_environment(void)
1140 fetch_pam_environment(void)
1142 return (pam_getenvlist(sshpam_handle
));
1146 free_pam_environment(char **env
)
1153 for (envp
= env
; *envp
; envp
++)
1159 * "Blind" conversation function for password authentication. Assumes that
1160 * echo-off prompts are for the password and stores messages for later
1164 sshpam_passwd_conv(int n
, sshpam_const
struct pam_message
**msg
,
1165 struct pam_response
**resp
, void *data
)
1167 struct pam_response
*reply
;
1171 debug3("PAM: %s called with %d messages", __func__
, n
);
1175 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
1176 return (PAM_CONV_ERR
);
1178 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
1179 return (PAM_CONV_ERR
);
1181 for (i
= 0; i
< n
; ++i
) {
1182 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
1183 case PAM_PROMPT_ECHO_OFF
:
1184 if (sshpam_password
== NULL
)
1186 if ((reply
[i
].resp
= strdup(sshpam_password
)) == NULL
)
1188 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1192 len
= strlen(PAM_MSG_MEMBER(msg
, i
, msg
));
1194 buffer_append(&loginmsg
,
1195 PAM_MSG_MEMBER(msg
, i
, msg
), len
);
1196 buffer_append(&loginmsg
, "\n", 1);
1198 if ((reply
[i
].resp
= strdup("")) == NULL
)
1200 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1207 return (PAM_SUCCESS
);
1210 for(i
= 0; i
< n
; i
++) {
1211 free(reply
[i
].resp
);
1214 return (PAM_CONV_ERR
);
1217 static struct pam_conv passwd_conv
= { sshpam_passwd_conv
, NULL
};
1220 * Attempt password authentication via PAM
1223 sshpam_auth_passwd(Authctxt
*authctxt
, const char *password
)
1225 int flags
= (options
.permit_empty_passwd
== 0 ?
1226 PAM_DISALLOW_NULL_AUTHTOK
: 0);
1229 if (!options
.use_pam
|| sshpam_handle
== NULL
)
1230 fatal("PAM: %s called when PAM disabled or failed to "
1231 "initialise.", __func__
);
1233 sshpam_password
= password
;
1234 sshpam_authctxt
= authctxt
;
1237 * If the user logging in is invalid, or is root but is not permitted
1238 * by PermitRootLogin, use an invalid password to prevent leaking
1239 * information via timing (eg if the PAM config has a delay on fail).
1241 if (!authctxt
->valid
|| (authctxt
->pw
->pw_uid
== 0 &&
1242 options
.permit_root_login
!= PERMIT_YES
))
1243 sshpam_password
= fake
= fake_password(password
);
1245 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1246 (const void *)&passwd_conv
);
1247 if (sshpam_err
!= PAM_SUCCESS
)
1248 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__
,
1249 pam_strerror(sshpam_handle
, sshpam_err
));
1251 sshpam_err
= pam_authenticate(sshpam_handle
, flags
);
1252 sshpam_password
= NULL
;
1254 if (sshpam_err
== PAM_MAXTRIES
)
1255 sshpam_set_maxtries_reached(1);
1256 if (sshpam_err
== PAM_SUCCESS
&& authctxt
->valid
) {
1257 debug("PAM: password authentication accepted for %.100s",
1261 debug("PAM: password authentication failed for %.100s: %s",
1262 authctxt
->valid
? authctxt
->user
: "an illegal user",
1263 pam_strerror(sshpam_handle
, sshpam_err
));
1269 sshpam_get_maxtries_reached(void)
1271 return sshpam_maxtries_reached
;
1275 sshpam_set_maxtries_reached(int reached
)
1277 if (reached
== 0 || sshpam_maxtries_reached
)
1279 sshpam_maxtries_reached
= 1;
1280 options
.password_authentication
= 0;
1281 options
.kbd_interactive_authentication
= 0;
1282 options
.challenge_response_authentication
= 0;
1284 #endif /* USE_PAM */