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 Exp $ */
51 #include <sys/types.h>
57 #if defined(HAVE_SECURITY_PAM_APPL_H)
58 #include <security/pam_appl.h>
59 #elif defined (HAVE_PAM_PAM_APPL_H)
60 #include <pam/pam_appl.h>
63 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
64 #ifdef PAM_SUN_CODEBASE
65 # define sshpam_const /* Solaris, HP-UX, AIX */
67 # define sshpam_const const /* LinuxPAM, OpenPAM */
76 #include "monitor_wrap.h"
83 #include "auth-options.h"
85 extern ServerOptions options
;
86 extern Buffer loginmsg
;
88 extern u_int utmp_len
;
90 /* so we don't silently change behaviour */
91 #ifdef USE_POSIX_THREADS
92 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
96 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
97 * and generally a bad idea. Use at own risk and do not expect support if
100 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
103 * Avoid namespace clash when *not* using pthreads for systems *with*
104 * pthreads, which unconditionally define pthread_t via sys/types.h
107 typedef pthread_t sp_pthread_t
;
109 typedef pid_t sp_pthread_t
;
113 sp_pthread_t pam_thread
;
119 static void sshpam_free_ctx(void *);
120 static struct pam_ctxt
*cleanup_ctxt
;
122 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
124 * Simulate threads with processes.
127 static int sshpam_thread_status
= -1;
128 static mysig_t sshpam_oldsig
;
131 sshpam_sigchld_handler(int sig
)
133 signal(SIGCHLD
, SIG_DFL
);
134 if (cleanup_ctxt
== NULL
)
135 return; /* handler called after PAM cleanup, shouldn't happen */
136 if (waitpid(cleanup_ctxt
->pam_thread
, &sshpam_thread_status
, WNOHANG
)
138 /* PAM thread has not exitted, privsep slave must have */
139 kill(cleanup_ctxt
->pam_thread
, SIGTERM
);
140 if (waitpid(cleanup_ctxt
->pam_thread
, &sshpam_thread_status
, 0)
142 return; /* could not wait */
144 if (WIFSIGNALED(sshpam_thread_status
) &&
145 WTERMSIG(sshpam_thread_status
) == SIGTERM
)
146 return; /* terminated by pthread_cancel */
147 if (!WIFEXITED(sshpam_thread_status
))
148 fatal("PAM: authentication thread exited unexpectedly");
149 if (WEXITSTATUS(sshpam_thread_status
) != 0)
150 fatal("PAM: authentication thread exited uncleanly");
154 pthread_exit(void *value __unused
)
160 pthread_create(sp_pthread_t
*thread
, const void *attr __unused
,
161 void *(*thread_start
)(void *), void *arg
)
164 struct pam_ctxt
*ctx
= arg
;
166 sshpam_thread_status
= -1;
167 switch ((pid
= fork())) {
169 error("fork(): %s", strerror(errno
));
172 close(ctx
->pam_psock
);
178 close(ctx
->pam_csock
);
180 sshpam_oldsig
= signal(SIGCHLD
, sshpam_sigchld_handler
);
186 pthread_cancel(sp_pthread_t thread
)
188 signal(SIGCHLD
, sshpam_oldsig
);
189 return (kill(thread
, SIGTERM
));
193 pthread_join(sp_pthread_t thread
, void **value __unused
)
197 if (sshpam_thread_status
!= -1)
198 return (sshpam_thread_status
);
199 signal(SIGCHLD
, sshpam_oldsig
);
200 waitpid(thread
, &status
, 0);
206 static pam_handle_t
*sshpam_handle
= NULL
;
207 static int sshpam_err
= 0;
208 static int sshpam_authenticated
= 0;
209 static int sshpam_session_open
= 0;
210 static int sshpam_cred_established
= 0;
211 static int sshpam_account_status
= -1;
212 static char **sshpam_env
= NULL
;
213 static Authctxt
*sshpam_authctxt
= NULL
;
214 static const char *sshpam_password
= NULL
;
215 static char badpw
[] = "\b\n\r\177INCORRECT";
217 /* Some PAM implementations don't implement this */
218 #ifndef HAVE_PAM_GETENVLIST
220 pam_getenvlist(pam_handle_t
*pamh
)
223 * XXX - If necessary, we can still support envrionment passing
224 * for platforms without pam_getenvlist by searching for known
225 * env vars (e.g. KRB5CCNAME) from the PAM environment.
232 * Some platforms, notably Solaris, do not enforce password complexity
233 * rules during pam_chauthtok() if the real uid of the calling process
234 * is 0, on the assumption that it's being called by "passwd" run by root.
235 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
238 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
240 sshpam_chauthtok_ruid(pam_handle_t
*pamh
, int flags
)
244 if (sshpam_authctxt
== NULL
)
245 fatal("PAM: sshpam_authctxt not initialized");
246 if (setreuid(sshpam_authctxt
->pw
->pw_uid
, -1) == -1)
247 fatal("%s: setreuid failed: %s", __func__
, strerror(errno
));
248 result
= pam_chauthtok(pamh
, flags
);
249 if (setreuid(0, -1) == -1)
250 fatal("%s: setreuid failed: %s", __func__
, strerror(errno
));
253 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
257 sshpam_password_change_required(int reqd
)
259 debug3("%s %d", __func__
, reqd
);
260 if (sshpam_authctxt
== NULL
)
261 fatal("%s: PAM authctxt not initialized", __func__
);
262 sshpam_authctxt
->force_pwchange
= reqd
;
264 no_port_forwarding_flag
|= 2;
265 no_agent_forwarding_flag
|= 2;
266 no_x11_forwarding_flag
|= 2;
268 no_port_forwarding_flag
&= ~2;
269 no_agent_forwarding_flag
&= ~2;
270 no_x11_forwarding_flag
&= ~2;
274 /* Import regular and PAM environment from subprocess */
276 import_environments(Buffer
*b
)
282 debug3("PAM: %s entering", __func__
);
284 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
285 /* Import variables set by do_pam_account */
286 sshpam_account_status
= buffer_get_int(b
);
287 sshpam_password_change_required(buffer_get_int(b
));
289 /* Import environment from subprocess */
290 num_env
= buffer_get_int(b
);
292 fatal("%s: received %u environment variables, expected <= 1024",
294 sshpam_env
= xcalloc(num_env
+ 1, sizeof(*sshpam_env
));
295 debug3("PAM: num env strings %d", num_env
);
296 for(i
= 0; i
< num_env
; i
++)
297 sshpam_env
[i
] = buffer_get_string(b
, NULL
);
299 sshpam_env
[num_env
] = NULL
;
301 /* Import PAM environment from subprocess */
302 num_env
= buffer_get_int(b
);
303 debug("PAM: num PAM env strings %d", num_env
);
304 for(i
= 0; i
< num_env
; i
++) {
305 env
= buffer_get_string(b
, NULL
);
307 #ifdef HAVE_PAM_PUTENV
308 /* Errors are not fatal here */
309 if ((err
= pam_putenv(sshpam_handle
, env
)) != PAM_SUCCESS
) {
310 error("PAM: pam_putenv: %s",
311 pam_strerror(sshpam_handle
, sshpam_err
));
319 * Conversation function for authentication thread.
322 sshpam_thread_conv(int n
, sshpam_const
struct pam_message
**msg
,
323 struct pam_response
**resp
, void *data
)
326 struct pam_ctxt
*ctxt
;
327 struct pam_response
*reply
;
330 debug3("PAM: %s entering, %d messages", __func__
, n
);
334 error("PAM: conversation function passed a null context");
335 return (PAM_CONV_ERR
);
338 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
339 return (PAM_CONV_ERR
);
341 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
342 return (PAM_CONV_ERR
);
344 buffer_init(&buffer
);
345 for (i
= 0; i
< n
; ++i
) {
346 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
347 case PAM_PROMPT_ECHO_OFF
:
348 buffer_put_cstring(&buffer
,
349 PAM_MSG_MEMBER(msg
, i
, msg
));
350 if (ssh_msg_send(ctxt
->pam_csock
,
351 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
353 if (ssh_msg_recv(ctxt
->pam_csock
, &buffer
) == -1)
355 if (buffer_get_char(&buffer
) != PAM_AUTHTOK
)
357 reply
[i
].resp
= buffer_get_string(&buffer
, NULL
);
359 case PAM_PROMPT_ECHO_ON
:
360 buffer_put_cstring(&buffer
,
361 PAM_MSG_MEMBER(msg
, i
, msg
));
362 if (ssh_msg_send(ctxt
->pam_csock
,
363 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
365 if (ssh_msg_recv(ctxt
->pam_csock
, &buffer
) == -1)
367 if (buffer_get_char(&buffer
) != PAM_AUTHTOK
)
369 reply
[i
].resp
= buffer_get_string(&buffer
, NULL
);
372 buffer_put_cstring(&buffer
,
373 PAM_MSG_MEMBER(msg
, i
, msg
));
374 if (ssh_msg_send(ctxt
->pam_csock
,
375 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
379 buffer_put_cstring(&buffer
,
380 PAM_MSG_MEMBER(msg
, i
, msg
));
381 if (ssh_msg_send(ctxt
->pam_csock
,
382 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
388 buffer_clear(&buffer
);
390 buffer_free(&buffer
);
392 return (PAM_SUCCESS
);
395 for(i
= 0; i
< n
; i
++) {
396 if (reply
[i
].resp
!= NULL
)
397 xfree(reply
[i
].resp
);
400 buffer_free(&buffer
);
401 return (PAM_CONV_ERR
);
405 * Authentication thread.
408 sshpam_thread(void *ctxtp
)
410 struct pam_ctxt
*ctxt
= ctxtp
;
412 struct pam_conv sshpam_conv
;
413 int flags
= (options
.permit_empty_passwd
== 0 ?
414 PAM_DISALLOW_NULL_AUTHTOK
: 0);
415 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
416 extern char **environ
;
419 const char *pam_user
;
420 const char **ptr_pam_user
= &pam_user
;
422 pam_get_item(sshpam_handle
, PAM_USER
,
423 (sshpam_const
void **)ptr_pam_user
);
426 if (sshpam_authctxt
!= NULL
) {
427 setproctitle("%s [pam]",
428 sshpam_authctxt
->valid
? pam_user
: "unknown");
432 sshpam_conv
.conv
= sshpam_thread_conv
;
433 sshpam_conv
.appdata_ptr
= ctxt
;
435 if (sshpam_authctxt
== NULL
)
436 fatal("%s: PAM authctxt not initialized", __func__
);
438 buffer_init(&buffer
);
439 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
440 (const void *)&sshpam_conv
);
441 if (sshpam_err
!= PAM_SUCCESS
)
443 sshpam_err
= pam_authenticate(sshpam_handle
, flags
);
444 if (sshpam_err
!= PAM_SUCCESS
)
448 if (!do_pam_account()) {
449 sshpam_err
= PAM_ACCT_EXPIRED
;
452 if (sshpam_authctxt
->force_pwchange
) {
453 sshpam_err
= pam_chauthtok(sshpam_handle
,
454 PAM_CHANGE_EXPIRED_AUTHTOK
);
455 if (sshpam_err
!= PAM_SUCCESS
)
457 sshpam_password_change_required(0);
461 buffer_put_cstring(&buffer
, "OK");
463 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
464 /* Export variables set by do_pam_account */
465 buffer_put_int(&buffer
, sshpam_account_status
);
466 buffer_put_int(&buffer
, sshpam_authctxt
->force_pwchange
);
468 /* Export any environment strings set in child */
469 for(i
= 0; environ
[i
] != NULL
; i
++)
471 buffer_put_int(&buffer
, i
);
472 for(i
= 0; environ
[i
] != NULL
; i
++)
473 buffer_put_cstring(&buffer
, environ
[i
]);
475 /* Export any environment strings set by PAM in child */
476 env_from_pam
= pam_getenvlist(sshpam_handle
);
477 for(i
= 0; env_from_pam
!= NULL
&& env_from_pam
[i
] != NULL
; i
++)
479 buffer_put_int(&buffer
, i
);
480 for(i
= 0; env_from_pam
!= NULL
&& env_from_pam
[i
] != NULL
; i
++)
481 buffer_put_cstring(&buffer
, env_from_pam
[i
]);
482 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
484 /* XXX - can't do much about an error here */
485 ssh_msg_send(ctxt
->pam_csock
, sshpam_err
, &buffer
);
486 buffer_free(&buffer
);
490 buffer_put_cstring(&buffer
,
491 pam_strerror(sshpam_handle
, sshpam_err
));
492 /* XXX - can't do much about an error here */
493 if (sshpam_err
== PAM_ACCT_EXPIRED
)
494 ssh_msg_send(ctxt
->pam_csock
, PAM_ACCT_EXPIRED
, &buffer
);
496 ssh_msg_send(ctxt
->pam_csock
, PAM_AUTH_ERR
, &buffer
);
497 buffer_free(&buffer
);
500 return (NULL
); /* Avoid warning for non-pthread case */
504 sshpam_thread_cleanup(void)
506 struct pam_ctxt
*ctxt
= cleanup_ctxt
;
508 debug3("PAM: %s entering", __func__
);
509 if (ctxt
!= NULL
&& ctxt
->pam_thread
!= 0) {
510 pthread_cancel(ctxt
->pam_thread
);
511 pthread_join(ctxt
->pam_thread
, NULL
);
512 close(ctxt
->pam_psock
);
513 close(ctxt
->pam_csock
);
514 memset(ctxt
, 0, sizeof(*ctxt
));
520 sshpam_null_conv(int n
, sshpam_const
struct pam_message
**msg
,
521 struct pam_response
**resp
, void *data
)
523 debug3("PAM: %s entering, %d messages", __func__
, n
);
524 return (PAM_CONV_ERR
);
527 static struct pam_conv null_conv
= { sshpam_null_conv
, NULL
};
530 sshpam_store_conv(int n
, sshpam_const
struct pam_message
**msg
,
531 struct pam_response
**resp
, void *data
)
533 struct pam_response
*reply
;
537 debug3("PAM: %s called with %d messages", __func__
, n
);
540 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
541 return (PAM_CONV_ERR
);
543 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
544 return (PAM_CONV_ERR
);
546 for (i
= 0; i
< n
; ++i
) {
547 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
550 len
= strlen(PAM_MSG_MEMBER(msg
, i
, msg
));
551 buffer_append(&loginmsg
, PAM_MSG_MEMBER(msg
, i
, msg
), len
);
552 buffer_append(&loginmsg
, "\n", 1 );
553 reply
[i
].resp_retcode
= PAM_SUCCESS
;
560 return (PAM_SUCCESS
);
563 for(i
= 0; i
< n
; i
++) {
564 if (reply
[i
].resp
!= NULL
)
565 xfree(reply
[i
].resp
);
568 return (PAM_CONV_ERR
);
571 static struct pam_conv store_conv
= { sshpam_store_conv
, NULL
};
576 debug("PAM: cleanup");
577 if (sshpam_handle
== NULL
)
579 pam_set_item(sshpam_handle
, PAM_CONV
, (const void *)&null_conv
);
580 if (sshpam_cred_established
) {
581 pam_setcred(sshpam_handle
, PAM_DELETE_CRED
);
582 sshpam_cred_established
= 0;
584 if (sshpam_session_open
) {
585 pam_close_session(sshpam_handle
, PAM_SILENT
);
586 sshpam_session_open
= 0;
588 sshpam_authenticated
= 0;
589 pam_end(sshpam_handle
, sshpam_err
);
590 sshpam_handle
= NULL
;
594 sshpam_init(Authctxt
*authctxt
)
596 extern char *__progname
;
597 const char *pam_rhost
, *pam_user
, *user
= authctxt
->user
;
598 const char **ptr_pam_user
= &pam_user
;
600 if (sshpam_handle
!= NULL
) {
601 /* We already have a PAM context; check if the user matches */
602 sshpam_err
= pam_get_item(sshpam_handle
,
603 PAM_USER
, (sshpam_const
void **)ptr_pam_user
);
604 if (sshpam_err
== PAM_SUCCESS
&& strcmp(user
, pam_user
) == 0)
606 pam_end(sshpam_handle
, sshpam_err
);
607 sshpam_handle
= NULL
;
609 debug("PAM: initializing for \"%s\"", user
);
611 pam_start(SSHD_PAM_SERVICE
, user
, &store_conv
, &sshpam_handle
);
612 sshpam_authctxt
= authctxt
;
614 if (sshpam_err
!= PAM_SUCCESS
) {
615 pam_end(sshpam_handle
, sshpam_err
);
616 sshpam_handle
= NULL
;
619 pam_rhost
= get_remote_name_or_ip(utmp_len
, options
.use_dns
);
620 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost
);
621 sshpam_err
= pam_set_item(sshpam_handle
, PAM_RHOST
, pam_rhost
);
622 if (sshpam_err
!= PAM_SUCCESS
) {
623 pam_end(sshpam_handle
, sshpam_err
);
624 sshpam_handle
= NULL
;
627 #ifdef PAM_TTY_KLUDGE
629 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
630 * sshd doesn't set the tty until too late in the auth process and
631 * may not even set one (for tty-less connections)
633 debug("PAM: setting PAM_TTY to \"ssh\"");
634 sshpam_err
= pam_set_item(sshpam_handle
, PAM_TTY
, "ssh");
635 if (sshpam_err
!= PAM_SUCCESS
) {
636 pam_end(sshpam_handle
, sshpam_err
);
637 sshpam_handle
= NULL
;
645 sshpam_init_ctx(Authctxt
*authctxt
)
647 struct pam_ctxt
*ctxt
;
650 debug3("PAM: %s entering", __func__
);
652 * Refuse to start if we don't have PAM enabled or do_pam_account
653 * has previously failed.
655 if (!options
.use_pam
|| sshpam_account_status
== 0)
659 if (sshpam_init(authctxt
) == -1) {
660 error("PAM: initialization failed");
664 ctxt
= xmalloc(sizeof *ctxt
);
665 memset(ctxt
, 0, sizeof(*ctxt
));
667 /* Start the authentication thread */
668 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, socks
) == -1) {
669 error("PAM: failed create sockets: %s", strerror(errno
));
673 ctxt
->pam_psock
= socks
[0];
674 ctxt
->pam_csock
= socks
[1];
675 if (pthread_create(&ctxt
->pam_thread
, NULL
, sshpam_thread
, ctxt
) == -1) {
676 error("PAM: failed to start authentication thread: %s",
688 sshpam_query(void *ctx
, char **name
, char **info
,
689 u_int
*num
, char ***prompts
, u_int
**echo_on
)
692 struct pam_ctxt
*ctxt
= ctx
;
698 debug3("PAM: %s entering", __func__
);
699 buffer_init(&buffer
);
702 *prompts
= xmalloc(sizeof(char *));
705 *echo_on
= xmalloc(sizeof(u_int
));
706 while (ssh_msg_recv(ctxt
->pam_psock
, &buffer
) == 0) {
707 type
= buffer_get_char(&buffer
);
708 msg
= buffer_get_string(&buffer
, NULL
);
711 case PAM_PROMPT_ECHO_ON
:
712 case PAM_PROMPT_ECHO_OFF
:
714 len
= plen
+ mlen
+ 1;
715 **prompts
= xrealloc(**prompts
, 1, len
);
716 strlcpy(**prompts
+ plen
, msg
, len
- plen
);
718 **echo_on
= (type
== PAM_PROMPT_ECHO_ON
);
723 /* accumulate messages */
724 len
= plen
+ mlen
+ 2;
725 **prompts
= xrealloc(**prompts
, 1, len
);
726 strlcpy(**prompts
+ plen
, msg
, len
- plen
);
728 strlcat(**prompts
+ plen
, "\n", len
- plen
);
732 case PAM_ACCT_EXPIRED
:
733 sshpam_account_status
= 0;
736 debug3("PAM: %s", pam_strerror(sshpam_handle
, type
));
737 if (**prompts
!= NULL
&& strlen(**prompts
) != 0) {
748 if (**prompts
!= NULL
) {
749 /* drain any accumulated messages */
750 debug("PAM: %s", **prompts
);
751 buffer_append(&loginmsg
, **prompts
,
756 if (type
== PAM_SUCCESS
) {
757 if (!sshpam_authctxt
->valid
||
758 (sshpam_authctxt
->pw
->pw_uid
== 0 &&
759 options
.permit_root_login
!= PERMIT_YES
))
760 fatal("Internal error: PAM auth "
761 "succeeded when it should have "
763 import_environments(&buffer
);
770 error("PAM: %s for %s%.100s from %.100s", msg
,
771 sshpam_authctxt
->valid
? "" : "illegal user ",
772 sshpam_authctxt
->user
,
773 get_remote_name_or_ip(utmp_len
, options
.use_dns
));
786 /* XXX - see also comment in auth-chall.c:verify_response */
788 sshpam_respond(void *ctx
, u_int num
, char **resp
)
791 struct pam_ctxt
*ctxt
= ctx
;
793 debug2("PAM: %s entering, %u responses", __func__
, num
);
794 switch (ctxt
->pam_done
) {
796 sshpam_authenticated
= 1;
804 error("PAM: expected one response, got %u", num
);
807 buffer_init(&buffer
);
808 if (sshpam_authctxt
->valid
&&
809 (sshpam_authctxt
->pw
->pw_uid
!= 0 ||
810 options
.permit_root_login
== PERMIT_YES
))
811 buffer_put_cstring(&buffer
, *resp
);
813 buffer_put_cstring(&buffer
, badpw
);
814 if (ssh_msg_send(ctxt
->pam_psock
, PAM_AUTHTOK
, &buffer
) == -1) {
815 buffer_free(&buffer
);
818 buffer_free(&buffer
);
823 sshpam_free_ctx(void *ctxtp
)
825 struct pam_ctxt
*ctxt
= ctxtp
;
827 debug3("PAM: %s entering", __func__
);
828 sshpam_thread_cleanup();
831 * We don't call sshpam_cleanup() here because we may need the PAM
832 * handle at a later stage, e.g. when setting up a session. It's
833 * still on the cleanup list, so pam_end() *will* be called before
834 * the server process terminates.
838 KbdintDevice sshpam_device
= {
846 KbdintDevice mm_sshpam_device
= {
855 * This replaces auth-pam.c
858 start_pam(Authctxt
*authctxt
)
860 if (!options
.use_pam
)
861 fatal("PAM: initialisation requested when UsePAM=no");
863 if (sshpam_init(authctxt
) == -1)
864 fatal("PAM: initialisation failed");
876 debug("%s: called", __func__
);
877 if (sshpam_account_status
!= -1)
878 return (sshpam_account_status
);
880 sshpam_err
= pam_acct_mgmt(sshpam_handle
, 0);
881 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__
, sshpam_err
,
882 pam_strerror(sshpam_handle
, sshpam_err
));
884 if (sshpam_err
!= PAM_SUCCESS
&& sshpam_err
!= PAM_NEW_AUTHTOK_REQD
) {
885 sshpam_account_status
= 0;
886 return (sshpam_account_status
);
889 if (sshpam_err
== PAM_NEW_AUTHTOK_REQD
)
890 sshpam_password_change_required(1);
892 sshpam_account_status
= 1;
893 return (sshpam_account_status
);
897 do_pam_set_tty(const char *tty
)
900 debug("PAM: setting PAM_TTY to \"%s\"", tty
);
901 sshpam_err
= pam_set_item(sshpam_handle
, PAM_TTY
, tty
);
902 if (sshpam_err
!= PAM_SUCCESS
)
903 fatal("PAM: failed to set PAM_TTY: %s",
904 pam_strerror(sshpam_handle
, sshpam_err
));
909 do_pam_setcred(int init
)
911 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
912 (const void *)&store_conv
);
913 if (sshpam_err
!= PAM_SUCCESS
)
914 fatal("PAM: failed to set PAM_CONV: %s",
915 pam_strerror(sshpam_handle
, sshpam_err
));
917 debug("PAM: establishing credentials");
918 sshpam_err
= pam_setcred(sshpam_handle
, PAM_ESTABLISH_CRED
);
920 debug("PAM: reinitializing credentials");
921 sshpam_err
= pam_setcred(sshpam_handle
, PAM_REINITIALIZE_CRED
);
923 if (sshpam_err
== PAM_SUCCESS
) {
924 sshpam_cred_established
= 1;
927 if (sshpam_authenticated
)
928 fatal("PAM: pam_setcred(): %s",
929 pam_strerror(sshpam_handle
, sshpam_err
));
931 debug("PAM: pam_setcred(): %s",
932 pam_strerror(sshpam_handle
, sshpam_err
));
936 sshpam_tty_conv(int n
, sshpam_const
struct pam_message
**msg
,
937 struct pam_response
**resp
, void *data
)
939 char input
[PAM_MAX_MSG_SIZE
];
940 struct pam_response
*reply
;
943 debug3("PAM: %s called with %d messages", __func__
, n
);
947 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
|| !isatty(STDIN_FILENO
))
948 return (PAM_CONV_ERR
);
950 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
951 return (PAM_CONV_ERR
);
953 for (i
= 0; i
< n
; ++i
) {
954 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
955 case PAM_PROMPT_ECHO_OFF
:
957 read_passphrase(PAM_MSG_MEMBER(msg
, i
, msg
),
959 reply
[i
].resp_retcode
= PAM_SUCCESS
;
961 case PAM_PROMPT_ECHO_ON
:
962 fprintf(stderr
, "%s\n", PAM_MSG_MEMBER(msg
, i
, msg
));
963 fgets(input
, sizeof input
, stdin
);
964 if ((reply
[i
].resp
= strdup(input
)) == NULL
)
966 reply
[i
].resp_retcode
= PAM_SUCCESS
;
970 fprintf(stderr
, "%s\n", PAM_MSG_MEMBER(msg
, i
, msg
));
971 reply
[i
].resp_retcode
= PAM_SUCCESS
;
978 return (PAM_SUCCESS
);
981 for(i
= 0; i
< n
; i
++) {
982 if (reply
[i
].resp
!= NULL
)
983 xfree(reply
[i
].resp
);
986 return (PAM_CONV_ERR
);
989 static struct pam_conv tty_conv
= { sshpam_tty_conv
, NULL
};
992 * XXX this should be done in the authentication phase, but ssh1 doesn't
996 do_pam_chauthtok(void)
999 fatal("Password expired (unable to change with privsep)");
1000 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1001 (const void *)&tty_conv
);
1002 if (sshpam_err
!= PAM_SUCCESS
)
1003 fatal("PAM: failed to set PAM_CONV: %s",
1004 pam_strerror(sshpam_handle
, sshpam_err
));
1005 debug("PAM: changing password");
1006 sshpam_err
= pam_chauthtok(sshpam_handle
, PAM_CHANGE_EXPIRED_AUTHTOK
);
1007 if (sshpam_err
!= PAM_SUCCESS
)
1008 fatal("PAM: pam_chauthtok(): %s",
1009 pam_strerror(sshpam_handle
, sshpam_err
));
1013 do_pam_session(void)
1015 debug3("PAM: opening session");
1016 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1017 (const void *)&store_conv
);
1018 if (sshpam_err
!= PAM_SUCCESS
)
1019 fatal("PAM: failed to set PAM_CONV: %s",
1020 pam_strerror(sshpam_handle
, sshpam_err
));
1021 sshpam_err
= pam_open_session(sshpam_handle
, 0);
1022 if (sshpam_err
== PAM_SUCCESS
)
1023 sshpam_session_open
= 1;
1025 sshpam_session_open
= 0;
1026 disable_forwarding();
1027 error("PAM: pam_open_session(): %s",
1028 pam_strerror(sshpam_handle
, sshpam_err
));
1034 is_pam_session_open(void)
1036 return sshpam_session_open
;
1040 * Set a PAM environment string. We need to do this so that the session
1041 * modules can handle things like Kerberos/GSI credentials that appear
1042 * during the ssh authentication process.
1045 do_pam_putenv(char *name
, char *value
)
1048 #ifdef HAVE_PAM_PUTENV
1052 len
= strlen(name
) + strlen(value
) + 2;
1053 compound
= xmalloc(len
);
1055 snprintf(compound
, len
, "%s=%s", name
, value
);
1056 ret
= pam_putenv(sshpam_handle
, compound
);
1064 fetch_pam_child_environment(void)
1070 fetch_pam_environment(void)
1072 return (pam_getenvlist(sshpam_handle
));
1076 free_pam_environment(char **env
)
1083 for (envp
= env
; *envp
; envp
++)
1089 * "Blind" conversation function for password authentication. Assumes that
1090 * echo-off prompts are for the password and stores messages for later
1094 sshpam_passwd_conv(int n
, sshpam_const
struct pam_message
**msg
,
1095 struct pam_response
**resp
, void *data
)
1097 struct pam_response
*reply
;
1101 debug3("PAM: %s called with %d messages", __func__
, n
);
1105 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
1106 return (PAM_CONV_ERR
);
1108 if ((reply
= malloc(n
* sizeof(*reply
))) == NULL
)
1109 return (PAM_CONV_ERR
);
1110 memset(reply
, 0, n
* sizeof(*reply
));
1112 for (i
= 0; i
< n
; ++i
) {
1113 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
1114 case PAM_PROMPT_ECHO_OFF
:
1115 if (sshpam_password
== NULL
)
1117 if ((reply
[i
].resp
= strdup(sshpam_password
)) == NULL
)
1119 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1123 len
= strlen(PAM_MSG_MEMBER(msg
, i
, msg
));
1125 buffer_append(&loginmsg
,
1126 PAM_MSG_MEMBER(msg
, i
, msg
), len
);
1127 buffer_append(&loginmsg
, "\n", 1);
1129 if ((reply
[i
].resp
= strdup("")) == NULL
)
1131 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1138 return (PAM_SUCCESS
);
1141 for(i
= 0; i
< n
; i
++) {
1142 if (reply
[i
].resp
!= NULL
)
1143 xfree(reply
[i
].resp
);
1146 return (PAM_CONV_ERR
);
1149 static struct pam_conv passwd_conv
= { sshpam_passwd_conv
, NULL
};
1152 * Attempt password authentication via PAM
1155 sshpam_auth_passwd(Authctxt
*authctxt
, const char *password
)
1157 int flags
= (options
.permit_empty_passwd
== 0 ?
1158 PAM_DISALLOW_NULL_AUTHTOK
: 0);
1160 if (!options
.use_pam
|| sshpam_handle
== NULL
)
1161 fatal("PAM: %s called when PAM disabled or failed to "
1162 "initialise.", __func__
);
1164 sshpam_password
= password
;
1165 sshpam_authctxt
= authctxt
;
1168 * If the user logging in is invalid, or is root but is not permitted
1169 * by PermitRootLogin, use an invalid password to prevent leaking
1170 * information via timing (eg if the PAM config has a delay on fail).
1172 if (!authctxt
->valid
|| (authctxt
->pw
->pw_uid
== 0 &&
1173 options
.permit_root_login
!= PERMIT_YES
))
1174 sshpam_password
= badpw
;
1176 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1177 (const void *)&passwd_conv
);
1178 if (sshpam_err
!= PAM_SUCCESS
)
1179 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__
,
1180 pam_strerror(sshpam_handle
, sshpam_err
));
1182 sshpam_err
= pam_authenticate(sshpam_handle
, flags
);
1183 sshpam_password
= NULL
;
1184 if (sshpam_err
== PAM_SUCCESS
&& authctxt
->valid
) {
1185 debug("PAM: password authentication accepted for %.100s",
1189 debug("PAM: password authentication failed for %.100s: %s",
1190 authctxt
->valid
? authctxt
->user
: "an illegal user",
1191 pam_strerror(sshpam_handle
, sshpam_err
));
1195 #endif /* USE_PAM */