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>
61 #if defined(HAVE_SECURITY_PAM_APPL_H)
62 #include <security/pam_appl.h>
63 #elif defined (HAVE_PAM_PAM_APPL_H)
64 #include <pam/pam_appl.h>
67 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
68 #ifdef PAM_SUN_CODEBASE
69 # define sshpam_const /* Solaris, HP-UX, AIX */
71 # define sshpam_const const /* LinuxPAM, OpenPAM */
74 /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
75 #ifdef PAM_SUN_CODEBASE
76 # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
78 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
94 #include "auth-options.h"
98 #include "monitor_wrap.h"
100 extern ServerOptions options
;
101 extern Buffer loginmsg
;
103 extern u_int utmp_len
;
105 /* so we don't silently change behaviour */
106 #ifdef USE_POSIX_THREADS
107 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
111 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
112 * and generally a bad idea. Use at own risk and do not expect support if
115 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
118 * Avoid namespace clash when *not* using pthreads for systems *with*
119 * pthreads, which unconditionally define pthread_t via sys/types.h
122 typedef pthread_t sp_pthread_t
;
124 typedef pid_t sp_pthread_t
;
128 sp_pthread_t pam_thread
;
134 static void sshpam_free_ctx(void *);
135 static struct pam_ctxt
*cleanup_ctxt
;
137 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
139 * Simulate threads with processes.
142 static int sshpam_thread_status
= -1;
143 static mysig_t sshpam_oldsig
;
146 sshpam_sigchld_handler(int sig
)
148 signal(SIGCHLD
, SIG_DFL
);
149 if (cleanup_ctxt
== NULL
)
150 return; /* handler called after PAM cleanup, shouldn't happen */
151 if (waitpid(cleanup_ctxt
->pam_thread
, &sshpam_thread_status
, WNOHANG
)
153 /* PAM thread has not exitted, privsep slave must have */
154 kill(cleanup_ctxt
->pam_thread
, SIGTERM
);
155 if (waitpid(cleanup_ctxt
->pam_thread
, &sshpam_thread_status
, 0)
157 return; /* could not wait */
159 if (WIFSIGNALED(sshpam_thread_status
) &&
160 WTERMSIG(sshpam_thread_status
) == SIGTERM
)
161 return; /* terminated by pthread_cancel */
162 if (!WIFEXITED(sshpam_thread_status
))
163 fatal("PAM: authentication thread exited unexpectedly");
164 if (WEXITSTATUS(sshpam_thread_status
) != 0)
165 fatal("PAM: authentication thread exited uncleanly");
170 pthread_exit(void *value
)
177 pthread_create(sp_pthread_t
*thread
, const void *attr
,
178 void *(*thread_start
)(void *), void *arg
)
181 struct pam_ctxt
*ctx
= arg
;
183 sshpam_thread_status
= -1;
184 switch ((pid
= fork())) {
186 error("fork(): %s", strerror(errno
));
189 close(ctx
->pam_psock
);
195 close(ctx
->pam_csock
);
197 sshpam_oldsig
= signal(SIGCHLD
, sshpam_sigchld_handler
);
203 pthread_cancel(sp_pthread_t thread
)
205 signal(SIGCHLD
, sshpam_oldsig
);
206 return (kill(thread
, SIGTERM
));
211 pthread_join(sp_pthread_t thread
, void **value
)
215 if (sshpam_thread_status
!= -1)
216 return (sshpam_thread_status
);
217 signal(SIGCHLD
, sshpam_oldsig
);
218 waitpid(thread
, &status
, 0);
224 static pam_handle_t
*sshpam_handle
= NULL
;
225 static int sshpam_err
= 0;
226 static int sshpam_authenticated
= 0;
227 static int sshpam_session_open
= 0;
228 static int sshpam_cred_established
= 0;
229 static int sshpam_account_status
= -1;
230 static char **sshpam_env
= NULL
;
231 static Authctxt
*sshpam_authctxt
= NULL
;
232 static const char *sshpam_password
= NULL
;
233 static char badpw
[] = "\b\n\r\177INCORRECT";
235 /* Some PAM implementations don't implement this */
236 #ifndef HAVE_PAM_GETENVLIST
238 pam_getenvlist(pam_handle_t
*pamh
)
241 * XXX - If necessary, we can still support envrionment passing
242 * for platforms without pam_getenvlist by searching for known
243 * env vars (e.g. KRB5CCNAME) from the PAM environment.
250 * Some platforms, notably Solaris, do not enforce password complexity
251 * rules during pam_chauthtok() if the real uid of the calling process
252 * is 0, on the assumption that it's being called by "passwd" run by root.
253 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
256 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
258 sshpam_chauthtok_ruid(pam_handle_t
*pamh
, int flags
)
262 if (sshpam_authctxt
== NULL
)
263 fatal("PAM: sshpam_authctxt not initialized");
264 if (setreuid(sshpam_authctxt
->pw
->pw_uid
, -1) == -1)
265 fatal("%s: setreuid failed: %s", __func__
, strerror(errno
));
266 result
= pam_chauthtok(pamh
, flags
);
267 if (setreuid(0, -1) == -1)
268 fatal("%s: setreuid failed: %s", __func__
, strerror(errno
));
271 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
275 sshpam_password_change_required(int reqd
)
277 debug3("%s %d", __func__
, reqd
);
278 if (sshpam_authctxt
== NULL
)
279 fatal("%s: PAM authctxt not initialized", __func__
);
280 sshpam_authctxt
->force_pwchange
= reqd
;
282 no_port_forwarding_flag
|= 2;
283 no_agent_forwarding_flag
|= 2;
284 no_x11_forwarding_flag
|= 2;
286 no_port_forwarding_flag
&= ~2;
287 no_agent_forwarding_flag
&= ~2;
288 no_x11_forwarding_flag
&= ~2;
292 /* Import regular and PAM environment from subprocess */
294 import_environments(Buffer
*b
)
300 debug3("PAM: %s entering", __func__
);
302 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
303 /* Import variables set by do_pam_account */
304 sshpam_account_status
= buffer_get_int(b
);
305 sshpam_password_change_required(buffer_get_int(b
));
307 /* Import environment from subprocess */
308 num_env
= buffer_get_int(b
);
310 fatal("%s: received %u environment variables, expected <= 1024",
312 sshpam_env
= xcalloc(num_env
+ 1, sizeof(*sshpam_env
));
313 debug3("PAM: num env strings %d", num_env
);
314 for(i
= 0; i
< num_env
; i
++)
315 sshpam_env
[i
] = buffer_get_string(b
, NULL
);
317 sshpam_env
[num_env
] = NULL
;
319 /* Import PAM environment from subprocess */
320 num_env
= buffer_get_int(b
);
321 debug("PAM: num PAM env strings %d", num_env
);
322 for(i
= 0; i
< num_env
; i
++) {
323 env
= buffer_get_string(b
, NULL
);
325 #ifdef HAVE_PAM_PUTENV
326 /* Errors are not fatal here */
327 if ((err
= pam_putenv(sshpam_handle
, env
)) != PAM_SUCCESS
) {
328 error("PAM: pam_putenv: %s",
329 pam_strerror(sshpam_handle
, sshpam_err
));
337 * Conversation function for authentication thread.
340 sshpam_thread_conv(int n
, sshpam_const
struct pam_message
**msg
,
341 struct pam_response
**resp
, void *data
)
344 struct pam_ctxt
*ctxt
;
345 struct pam_response
*reply
;
348 debug3("PAM: %s entering, %d messages", __func__
, n
);
352 error("PAM: conversation function passed a null context");
353 return (PAM_CONV_ERR
);
356 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
357 return (PAM_CONV_ERR
);
359 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
360 return (PAM_CONV_ERR
);
362 buffer_init(&buffer
);
363 for (i
= 0; i
< n
; ++i
) {
364 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
365 case PAM_PROMPT_ECHO_OFF
:
366 buffer_put_cstring(&buffer
,
367 PAM_MSG_MEMBER(msg
, i
, msg
));
368 if (ssh_msg_send(ctxt
->pam_csock
,
369 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
371 if (ssh_msg_recv(ctxt
->pam_csock
, &buffer
) == -1)
373 if (buffer_get_char(&buffer
) != PAM_AUTHTOK
)
375 reply
[i
].resp
= buffer_get_string(&buffer
, NULL
);
377 case PAM_PROMPT_ECHO_ON
:
378 buffer_put_cstring(&buffer
,
379 PAM_MSG_MEMBER(msg
, i
, msg
));
380 if (ssh_msg_send(ctxt
->pam_csock
,
381 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
383 if (ssh_msg_recv(ctxt
->pam_csock
, &buffer
) == -1)
385 if (buffer_get_char(&buffer
) != PAM_AUTHTOK
)
387 reply
[i
].resp
= buffer_get_string(&buffer
, NULL
);
390 buffer_put_cstring(&buffer
,
391 PAM_MSG_MEMBER(msg
, i
, msg
));
392 if (ssh_msg_send(ctxt
->pam_csock
,
393 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
397 buffer_put_cstring(&buffer
,
398 PAM_MSG_MEMBER(msg
, i
, msg
));
399 if (ssh_msg_send(ctxt
->pam_csock
,
400 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
406 buffer_clear(&buffer
);
408 buffer_free(&buffer
);
410 return (PAM_SUCCESS
);
413 for(i
= 0; i
< n
; i
++) {
414 if (reply
[i
].resp
!= NULL
)
415 xfree(reply
[i
].resp
);
418 buffer_free(&buffer
);
419 return (PAM_CONV_ERR
);
423 * Authentication thread.
426 sshpam_thread(void *ctxtp
)
428 struct pam_ctxt
*ctxt
= ctxtp
;
430 struct pam_conv sshpam_conv
;
431 int flags
= (options
.permit_empty_passwd
== 0 ?
432 PAM_DISALLOW_NULL_AUTHTOK
: 0);
433 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
434 extern char **environ
;
437 const char *pam_user
;
438 const char **ptr_pam_user
= &pam_user
;
440 pam_get_item(sshpam_handle
, PAM_USER
,
441 (sshpam_const
void **)ptr_pam_user
);
444 if (sshpam_authctxt
!= NULL
) {
445 setproctitle("%s [pam]",
446 sshpam_authctxt
->valid
? pam_user
: "unknown");
450 sshpam_conv
.conv
= sshpam_thread_conv
;
451 sshpam_conv
.appdata_ptr
= ctxt
;
453 if (sshpam_authctxt
== NULL
)
454 fatal("%s: PAM authctxt not initialized", __func__
);
456 buffer_init(&buffer
);
457 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
458 (const void *)&sshpam_conv
);
459 if (sshpam_err
!= PAM_SUCCESS
)
461 sshpam_err
= pam_authenticate(sshpam_handle
, flags
);
462 if (sshpam_err
!= PAM_SUCCESS
)
466 if (!do_pam_account()) {
467 sshpam_err
= PAM_ACCT_EXPIRED
;
470 if (sshpam_authctxt
->force_pwchange
) {
471 sshpam_err
= pam_chauthtok(sshpam_handle
,
472 PAM_CHANGE_EXPIRED_AUTHTOK
);
473 if (sshpam_err
!= PAM_SUCCESS
)
475 sshpam_password_change_required(0);
479 buffer_put_cstring(&buffer
, "OK");
481 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
482 /* Export variables set by do_pam_account */
483 buffer_put_int(&buffer
, sshpam_account_status
);
484 buffer_put_int(&buffer
, sshpam_authctxt
->force_pwchange
);
486 /* Export any environment strings set in child */
487 for(i
= 0; environ
[i
] != NULL
; i
++)
489 buffer_put_int(&buffer
, i
);
490 for(i
= 0; environ
[i
] != NULL
; i
++)
491 buffer_put_cstring(&buffer
, environ
[i
]);
493 /* Export any environment strings set by PAM in child */
494 env_from_pam
= pam_getenvlist(sshpam_handle
);
495 for(i
= 0; env_from_pam
!= NULL
&& env_from_pam
[i
] != NULL
; i
++)
497 buffer_put_int(&buffer
, i
);
498 for(i
= 0; env_from_pam
!= NULL
&& env_from_pam
[i
] != NULL
; i
++)
499 buffer_put_cstring(&buffer
, env_from_pam
[i
]);
500 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
502 /* XXX - can't do much about an error here */
503 ssh_msg_send(ctxt
->pam_csock
, sshpam_err
, &buffer
);
504 buffer_free(&buffer
);
508 buffer_put_cstring(&buffer
,
509 pam_strerror(sshpam_handle
, sshpam_err
));
510 /* XXX - can't do much about an error here */
511 if (sshpam_err
== PAM_ACCT_EXPIRED
)
512 ssh_msg_send(ctxt
->pam_csock
, PAM_ACCT_EXPIRED
, &buffer
);
514 ssh_msg_send(ctxt
->pam_csock
, PAM_AUTH_ERR
, &buffer
);
515 buffer_free(&buffer
);
518 return (NULL
); /* Avoid warning for non-pthread case */
522 sshpam_thread_cleanup(void)
524 struct pam_ctxt
*ctxt
= cleanup_ctxt
;
526 debug3("PAM: %s entering", __func__
);
527 if (ctxt
!= NULL
&& ctxt
->pam_thread
!= 0) {
528 pthread_cancel(ctxt
->pam_thread
);
529 pthread_join(ctxt
->pam_thread
, NULL
);
530 close(ctxt
->pam_psock
);
531 close(ctxt
->pam_csock
);
532 memset(ctxt
, 0, sizeof(*ctxt
));
538 sshpam_null_conv(int n
, sshpam_const
struct pam_message
**msg
,
539 struct pam_response
**resp
, void *data
)
541 debug3("PAM: %s entering, %d messages", __func__
, n
);
542 return (PAM_CONV_ERR
);
545 static struct pam_conv null_conv
= { sshpam_null_conv
, NULL
};
548 sshpam_store_conv(int n
, sshpam_const
struct pam_message
**msg
,
549 struct pam_response
**resp
, void *data
)
551 struct pam_response
*reply
;
555 debug3("PAM: %s called with %d messages", __func__
, n
);
558 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
559 return (PAM_CONV_ERR
);
561 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
562 return (PAM_CONV_ERR
);
564 for (i
= 0; i
< n
; ++i
) {
565 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
568 len
= strlen(PAM_MSG_MEMBER(msg
, i
, msg
));
569 buffer_append(&loginmsg
, PAM_MSG_MEMBER(msg
, i
, msg
), len
);
570 buffer_append(&loginmsg
, "\n", 1 );
571 reply
[i
].resp_retcode
= PAM_SUCCESS
;
578 return (PAM_SUCCESS
);
581 for(i
= 0; i
< n
; i
++) {
582 if (reply
[i
].resp
!= NULL
)
583 xfree(reply
[i
].resp
);
586 return (PAM_CONV_ERR
);
589 static struct pam_conv store_conv
= { sshpam_store_conv
, NULL
};
594 debug("PAM: cleanup");
595 if (sshpam_handle
== NULL
)
597 pam_set_item(sshpam_handle
, PAM_CONV
, (const void *)&null_conv
);
598 if (sshpam_cred_established
) {
599 pam_setcred(sshpam_handle
, PAM_DELETE_CRED
);
600 sshpam_cred_established
= 0;
602 if (sshpam_session_open
) {
603 pam_close_session(sshpam_handle
, PAM_SILENT
);
604 sshpam_session_open
= 0;
606 sshpam_authenticated
= 0;
607 pam_end(sshpam_handle
, sshpam_err
);
608 sshpam_handle
= NULL
;
612 sshpam_init(Authctxt
*authctxt
)
614 extern char *__progname
;
615 const char *pam_rhost
, *pam_user
, *user
= authctxt
->user
;
616 const char **ptr_pam_user
= &pam_user
;
618 if (sshpam_handle
!= NULL
) {
619 /* We already have a PAM context; check if the user matches */
620 sshpam_err
= pam_get_item(sshpam_handle
,
621 PAM_USER
, (sshpam_const
void **)ptr_pam_user
);
622 if (sshpam_err
== PAM_SUCCESS
&& strcmp(user
, pam_user
) == 0)
624 pam_end(sshpam_handle
, sshpam_err
);
625 sshpam_handle
= NULL
;
627 debug("PAM: initializing for \"%s\"", user
);
629 pam_start(SSHD_PAM_SERVICE
, user
, &store_conv
, &sshpam_handle
);
630 sshpam_authctxt
= authctxt
;
632 if (sshpam_err
!= PAM_SUCCESS
) {
633 pam_end(sshpam_handle
, sshpam_err
);
634 sshpam_handle
= NULL
;
637 pam_rhost
= get_remote_name_or_ip(utmp_len
, options
.use_dns
);
638 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost
);
639 sshpam_err
= pam_set_item(sshpam_handle
, PAM_RHOST
, pam_rhost
);
640 if (sshpam_err
!= PAM_SUCCESS
) {
641 pam_end(sshpam_handle
, sshpam_err
);
642 sshpam_handle
= NULL
;
645 #ifdef PAM_TTY_KLUDGE
647 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
648 * sshd doesn't set the tty until too late in the auth process and
649 * may not even set one (for tty-less connections)
651 debug("PAM: setting PAM_TTY to \"ssh\"");
652 sshpam_err
= pam_set_item(sshpam_handle
, PAM_TTY
, "ssh");
653 if (sshpam_err
!= PAM_SUCCESS
) {
654 pam_end(sshpam_handle
, sshpam_err
);
655 sshpam_handle
= NULL
;
663 sshpam_init_ctx(Authctxt
*authctxt
)
665 struct pam_ctxt
*ctxt
;
668 debug3("PAM: %s entering", __func__
);
670 * Refuse to start if we don't have PAM enabled or do_pam_account
671 * has previously failed.
673 if (!options
.use_pam
|| sshpam_account_status
== 0)
677 if (sshpam_init(authctxt
) == -1) {
678 error("PAM: initialization failed");
682 ctxt
= xmalloc(sizeof *ctxt
);
683 memset(ctxt
, 0, sizeof(*ctxt
));
685 /* Start the authentication thread */
686 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, socks
) == -1) {
687 error("PAM: failed create sockets: %s", strerror(errno
));
691 ctxt
->pam_psock
= socks
[0];
692 ctxt
->pam_csock
= socks
[1];
693 if (pthread_create(&ctxt
->pam_thread
, NULL
, sshpam_thread
, ctxt
) == -1) {
694 error("PAM: failed to start authentication thread: %s",
706 sshpam_query(void *ctx
, char **name
, char **info
,
707 u_int
*num
, char ***prompts
, u_int
**echo_on
)
710 struct pam_ctxt
*ctxt
= ctx
;
716 debug3("PAM: %s entering", __func__
);
717 buffer_init(&buffer
);
720 *prompts
= xmalloc(sizeof(char *));
723 *echo_on
= xmalloc(sizeof(u_int
));
724 while (ssh_msg_recv(ctxt
->pam_psock
, &buffer
) == 0) {
725 type
= buffer_get_char(&buffer
);
726 msg
= buffer_get_string(&buffer
, NULL
);
729 case PAM_PROMPT_ECHO_ON
:
730 case PAM_PROMPT_ECHO_OFF
:
732 len
= plen
+ mlen
+ 1;
733 **prompts
= xrealloc(**prompts
, 1, len
);
734 strlcpy(**prompts
+ plen
, msg
, len
- plen
);
736 **echo_on
= (type
== PAM_PROMPT_ECHO_ON
);
741 /* accumulate messages */
742 len
= plen
+ mlen
+ 2;
743 **prompts
= xrealloc(**prompts
, 1, len
);
744 strlcpy(**prompts
+ plen
, msg
, len
- plen
);
746 strlcat(**prompts
+ plen
, "\n", len
- plen
);
750 case PAM_ACCT_EXPIRED
:
751 sshpam_account_status
= 0;
754 debug3("PAM: %s", pam_strerror(sshpam_handle
, type
));
755 if (**prompts
!= NULL
&& strlen(**prompts
) != 0) {
766 if (**prompts
!= NULL
) {
767 /* drain any accumulated messages */
768 debug("PAM: %s", **prompts
);
769 buffer_append(&loginmsg
, **prompts
,
774 if (type
== PAM_SUCCESS
) {
775 if (!sshpam_authctxt
->valid
||
776 (sshpam_authctxt
->pw
->pw_uid
== 0 &&
777 options
.permit_root_login
!= PERMIT_YES
))
778 fatal("Internal error: PAM auth "
779 "succeeded when it should have "
781 import_environments(&buffer
);
788 error("PAM: %s for %s%.100s from %.100s", msg
,
789 sshpam_authctxt
->valid
? "" : "illegal user ",
790 sshpam_authctxt
->user
,
791 get_remote_name_or_ip(utmp_len
, options
.use_dns
));
804 /* XXX - see also comment in auth-chall.c:verify_response */
806 sshpam_respond(void *ctx
, u_int num
, char **resp
)
809 struct pam_ctxt
*ctxt
= ctx
;
811 debug2("PAM: %s entering, %u responses", __func__
, num
);
812 switch (ctxt
->pam_done
) {
814 sshpam_authenticated
= 1;
822 error("PAM: expected one response, got %u", num
);
825 buffer_init(&buffer
);
826 if (sshpam_authctxt
->valid
&&
827 (sshpam_authctxt
->pw
->pw_uid
!= 0 ||
828 options
.permit_root_login
== PERMIT_YES
))
829 buffer_put_cstring(&buffer
, *resp
);
831 buffer_put_cstring(&buffer
, badpw
);
832 if (ssh_msg_send(ctxt
->pam_psock
, PAM_AUTHTOK
, &buffer
) == -1) {
833 buffer_free(&buffer
);
836 buffer_free(&buffer
);
841 sshpam_free_ctx(void *ctxtp
)
843 struct pam_ctxt
*ctxt
= ctxtp
;
845 debug3("PAM: %s entering", __func__
);
846 sshpam_thread_cleanup();
849 * We don't call sshpam_cleanup() here because we may need the PAM
850 * handle at a later stage, e.g. when setting up a session. It's
851 * still on the cleanup list, so pam_end() *will* be called before
852 * the server process terminates.
856 KbdintDevice sshpam_device
= {
864 KbdintDevice mm_sshpam_device
= {
873 * This replaces auth-pam.c
876 start_pam(Authctxt
*authctxt
)
878 if (!options
.use_pam
)
879 fatal("PAM: initialisation requested when UsePAM=no");
881 if (sshpam_init(authctxt
) == -1)
882 fatal("PAM: initialisation failed");
894 debug("%s: called", __func__
);
895 if (sshpam_account_status
!= -1)
896 return (sshpam_account_status
);
898 sshpam_err
= pam_acct_mgmt(sshpam_handle
, 0);
899 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__
, sshpam_err
,
900 pam_strerror(sshpam_handle
, sshpam_err
));
902 if (sshpam_err
!= PAM_SUCCESS
&& sshpam_err
!= PAM_NEW_AUTHTOK_REQD
) {
903 sshpam_account_status
= 0;
904 return (sshpam_account_status
);
907 if (sshpam_err
== PAM_NEW_AUTHTOK_REQD
)
908 sshpam_password_change_required(1);
910 sshpam_account_status
= 1;
911 return (sshpam_account_status
);
915 do_pam_set_tty(const char *tty
)
918 debug("PAM: setting PAM_TTY to \"%s\"", tty
);
919 sshpam_err
= pam_set_item(sshpam_handle
, PAM_TTY
, tty
);
920 if (sshpam_err
!= PAM_SUCCESS
)
921 fatal("PAM: failed to set PAM_TTY: %s",
922 pam_strerror(sshpam_handle
, sshpam_err
));
927 do_pam_setcred(int init
)
929 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
930 (const void *)&store_conv
);
931 if (sshpam_err
!= PAM_SUCCESS
)
932 fatal("PAM: failed to set PAM_CONV: %s",
933 pam_strerror(sshpam_handle
, sshpam_err
));
935 debug("PAM: establishing credentials");
936 sshpam_err
= pam_setcred(sshpam_handle
, PAM_ESTABLISH_CRED
);
938 debug("PAM: reinitializing credentials");
939 sshpam_err
= pam_setcred(sshpam_handle
, PAM_REINITIALIZE_CRED
);
941 if (sshpam_err
== PAM_SUCCESS
) {
942 sshpam_cred_established
= 1;
945 if (sshpam_authenticated
)
946 fatal("PAM: pam_setcred(): %s",
947 pam_strerror(sshpam_handle
, sshpam_err
));
949 debug("PAM: pam_setcred(): %s",
950 pam_strerror(sshpam_handle
, sshpam_err
));
954 sshpam_tty_conv(int n
, sshpam_const
struct pam_message
**msg
,
955 struct pam_response
**resp
, void *data
)
957 char input
[PAM_MAX_MSG_SIZE
];
958 struct pam_response
*reply
;
961 debug3("PAM: %s called with %d messages", __func__
, n
);
965 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
|| !isatty(STDIN_FILENO
))
966 return (PAM_CONV_ERR
);
968 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
969 return (PAM_CONV_ERR
);
971 for (i
= 0; i
< n
; ++i
) {
972 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
973 case PAM_PROMPT_ECHO_OFF
:
975 read_passphrase(PAM_MSG_MEMBER(msg
, i
, msg
),
977 reply
[i
].resp_retcode
= PAM_SUCCESS
;
979 case PAM_PROMPT_ECHO_ON
:
980 fprintf(stderr
, "%s\n", PAM_MSG_MEMBER(msg
, i
, msg
));
981 fgets(input
, sizeof input
, stdin
);
982 if ((reply
[i
].resp
= strdup(input
)) == NULL
)
984 reply
[i
].resp_retcode
= PAM_SUCCESS
;
988 fprintf(stderr
, "%s\n", PAM_MSG_MEMBER(msg
, i
, msg
));
989 reply
[i
].resp_retcode
= PAM_SUCCESS
;
996 return (PAM_SUCCESS
);
999 for(i
= 0; i
< n
; i
++) {
1000 if (reply
[i
].resp
!= NULL
)
1001 xfree(reply
[i
].resp
);
1004 return (PAM_CONV_ERR
);
1007 static struct pam_conv tty_conv
= { sshpam_tty_conv
, NULL
};
1010 * XXX this should be done in the authentication phase, but ssh1 doesn't
1014 do_pam_chauthtok(void)
1017 fatal("Password expired (unable to change with privsep)");
1018 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1019 (const void *)&tty_conv
);
1020 if (sshpam_err
!= PAM_SUCCESS
)
1021 fatal("PAM: failed to set PAM_CONV: %s",
1022 pam_strerror(sshpam_handle
, sshpam_err
));
1023 debug("PAM: changing password");
1024 sshpam_err
= pam_chauthtok(sshpam_handle
, PAM_CHANGE_EXPIRED_AUTHTOK
);
1025 if (sshpam_err
!= PAM_SUCCESS
)
1026 fatal("PAM: pam_chauthtok(): %s",
1027 pam_strerror(sshpam_handle
, sshpam_err
));
1031 do_pam_session(void)
1033 debug3("PAM: opening session");
1034 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1035 (const void *)&store_conv
);
1036 if (sshpam_err
!= PAM_SUCCESS
)
1037 fatal("PAM: failed to set PAM_CONV: %s",
1038 pam_strerror(sshpam_handle
, sshpam_err
));
1039 sshpam_err
= pam_open_session(sshpam_handle
, 0);
1040 if (sshpam_err
== PAM_SUCCESS
)
1041 sshpam_session_open
= 1;
1043 sshpam_session_open
= 0;
1044 disable_forwarding();
1045 error("PAM: pam_open_session(): %s",
1046 pam_strerror(sshpam_handle
, sshpam_err
));
1052 is_pam_session_open(void)
1054 return sshpam_session_open
;
1058 * Set a PAM environment string. We need to do this so that the session
1059 * modules can handle things like Kerberos/GSI credentials that appear
1060 * during the ssh authentication process.
1063 do_pam_putenv(char *name
, char *value
)
1066 #ifdef HAVE_PAM_PUTENV
1070 len
= strlen(name
) + strlen(value
) + 2;
1071 compound
= xmalloc(len
);
1073 snprintf(compound
, len
, "%s=%s", name
, value
);
1074 ret
= pam_putenv(sshpam_handle
, compound
);
1082 fetch_pam_child_environment(void)
1088 fetch_pam_environment(void)
1090 return (pam_getenvlist(sshpam_handle
));
1094 free_pam_environment(char **env
)
1101 for (envp
= env
; *envp
; envp
++)
1107 * "Blind" conversation function for password authentication. Assumes that
1108 * echo-off prompts are for the password and stores messages for later
1112 sshpam_passwd_conv(int n
, sshpam_const
struct pam_message
**msg
,
1113 struct pam_response
**resp
, void *data
)
1115 struct pam_response
*reply
;
1119 debug3("PAM: %s called with %d messages", __func__
, n
);
1123 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
1124 return (PAM_CONV_ERR
);
1126 if ((reply
= malloc(n
* sizeof(*reply
))) == NULL
)
1127 return (PAM_CONV_ERR
);
1128 memset(reply
, 0, n
* sizeof(*reply
));
1130 for (i
= 0; i
< n
; ++i
) {
1131 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
1132 case PAM_PROMPT_ECHO_OFF
:
1133 if (sshpam_password
== NULL
)
1135 if ((reply
[i
].resp
= strdup(sshpam_password
)) == NULL
)
1137 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1141 len
= strlen(PAM_MSG_MEMBER(msg
, i
, msg
));
1143 buffer_append(&loginmsg
,
1144 PAM_MSG_MEMBER(msg
, i
, msg
), len
);
1145 buffer_append(&loginmsg
, "\n", 1);
1147 if ((reply
[i
].resp
= strdup("")) == NULL
)
1149 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1156 return (PAM_SUCCESS
);
1159 for(i
= 0; i
< n
; i
++) {
1160 if (reply
[i
].resp
!= NULL
)
1161 xfree(reply
[i
].resp
);
1164 return (PAM_CONV_ERR
);
1167 static struct pam_conv passwd_conv
= { sshpam_passwd_conv
, NULL
};
1170 * Attempt password authentication via PAM
1173 sshpam_auth_passwd(Authctxt
*authctxt
, const char *password
)
1175 int flags
= (options
.permit_empty_passwd
== 0 ?
1176 PAM_DISALLOW_NULL_AUTHTOK
: 0);
1178 if (!options
.use_pam
|| sshpam_handle
== NULL
)
1179 fatal("PAM: %s called when PAM disabled or failed to "
1180 "initialise.", __func__
);
1182 sshpam_password
= password
;
1183 sshpam_authctxt
= authctxt
;
1186 * If the user logging in is invalid, or is root but is not permitted
1187 * by PermitRootLogin, use an invalid password to prevent leaking
1188 * information via timing (eg if the PAM config has a delay on fail).
1190 if (!authctxt
->valid
|| (authctxt
->pw
->pw_uid
== 0 &&
1191 options
.permit_root_login
!= PERMIT_YES
))
1192 sshpam_password
= badpw
;
1194 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1195 (const void *)&passwd_conv
);
1196 if (sshpam_err
!= PAM_SUCCESS
)
1197 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__
,
1198 pam_strerror(sshpam_handle
, sshpam_err
));
1200 sshpam_err
= pam_authenticate(sshpam_handle
, flags
);
1201 sshpam_password
= NULL
;
1202 if (sshpam_err
== PAM_SUCCESS
&& authctxt
->valid
) {
1203 debug("PAM: password authentication accepted for %.100s",
1207 debug("PAM: password authentication failed for %.100s: %s",
1208 authctxt
->valid
? authctxt
->user
: "an illegal user",
1209 pam_strerror(sshpam_handle
, sshpam_err
));
1213 #endif /* USE_PAM */