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>
62 #if defined(HAVE_SECURITY_PAM_APPL_H)
63 #include <security/pam_appl.h>
64 #elif defined (HAVE_PAM_PAM_APPL_H)
65 #include <pam/pam_appl.h>
68 /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
69 #ifdef PAM_SUN_CODEBASE
70 # define sshpam_const /* Solaris, HP-UX, AIX */
72 # define sshpam_const const /* LinuxPAM, OpenPAM */
75 /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
76 #ifdef PAM_SUN_CODEBASE
77 # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
79 # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
95 #include "auth-options.h"
99 #include "monitor_wrap.h"
101 extern ServerOptions options
;
102 extern Buffer loginmsg
;
104 extern u_int utmp_len
;
106 /* so we don't silently change behaviour */
107 #ifdef USE_POSIX_THREADS
108 # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
112 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
113 * and generally a bad idea. Use at own risk and do not expect support if
116 #ifdef UNSUPPORTED_POSIX_THREADS_HACK
119 * Avoid namespace clash when *not* using pthreads for systems *with*
120 * pthreads, which unconditionally define pthread_t via sys/types.h
123 typedef pthread_t sp_pthread_t
;
125 typedef pid_t sp_pthread_t
;
129 sp_pthread_t pam_thread
;
135 static void sshpam_free_ctx(void *);
136 static struct pam_ctxt
*cleanup_ctxt
;
138 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
140 * Simulate threads with processes.
143 static int sshpam_thread_status
= -1;
144 static mysig_t sshpam_oldsig
;
147 sshpam_sigchld_handler(int sig
)
149 signal(SIGCHLD
, SIG_DFL
);
150 if (cleanup_ctxt
== NULL
)
151 return; /* handler called after PAM cleanup, shouldn't happen */
152 if (waitpid(cleanup_ctxt
->pam_thread
, &sshpam_thread_status
, WNOHANG
)
154 /* PAM thread has not exitted, privsep slave must have */
155 kill(cleanup_ctxt
->pam_thread
, SIGTERM
);
156 if (waitpid(cleanup_ctxt
->pam_thread
, &sshpam_thread_status
, 0)
158 return; /* could not wait */
160 if (WIFSIGNALED(sshpam_thread_status
) &&
161 WTERMSIG(sshpam_thread_status
) == SIGTERM
)
162 return; /* terminated by pthread_cancel */
163 if (!WIFEXITED(sshpam_thread_status
))
164 fatal("PAM: authentication thread exited unexpectedly");
165 if (WEXITSTATUS(sshpam_thread_status
) != 0)
166 fatal("PAM: authentication thread exited uncleanly");
171 pthread_exit(void *value
)
178 pthread_create(sp_pthread_t
*thread
, const void *attr
,
179 void *(*thread_start
)(void *), void *arg
)
182 struct pam_ctxt
*ctx
= arg
;
184 sshpam_thread_status
= -1;
185 switch ((pid
= fork())) {
187 error("fork(): %s", strerror(errno
));
190 close(ctx
->pam_psock
);
196 close(ctx
->pam_csock
);
198 sshpam_oldsig
= signal(SIGCHLD
, sshpam_sigchld_handler
);
204 pthread_cancel(sp_pthread_t thread
)
206 signal(SIGCHLD
, sshpam_oldsig
);
207 return (kill(thread
, SIGTERM
));
212 pthread_join(sp_pthread_t thread
, void **value
)
216 if (sshpam_thread_status
!= -1)
217 return (sshpam_thread_status
);
218 signal(SIGCHLD
, sshpam_oldsig
);
219 waitpid(thread
, &status
, 0);
225 static pam_handle_t
*sshpam_handle
= NULL
;
226 static int sshpam_err
= 0;
227 static int sshpam_authenticated
= 0;
228 static int sshpam_session_open
= 0;
229 static int sshpam_cred_established
= 0;
230 static int sshpam_account_status
= -1;
231 static char **sshpam_env
= NULL
;
232 static Authctxt
*sshpam_authctxt
= NULL
;
233 static const char *sshpam_password
= NULL
;
234 static char badpw
[] = "\b\n\r\177INCORRECT";
236 /* Some PAM implementations don't implement this */
237 #ifndef HAVE_PAM_GETENVLIST
239 pam_getenvlist(pam_handle_t
*pamh
)
242 * XXX - If necessary, we can still support envrionment passing
243 * for platforms without pam_getenvlist by searching for known
244 * env vars (e.g. KRB5CCNAME) from the PAM environment.
251 * Some platforms, notably Solaris, do not enforce password complexity
252 * rules during pam_chauthtok() if the real uid of the calling process
253 * is 0, on the assumption that it's being called by "passwd" run by root.
254 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
257 #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
259 sshpam_chauthtok_ruid(pam_handle_t
*pamh
, int flags
)
263 if (sshpam_authctxt
== NULL
)
264 fatal("PAM: sshpam_authctxt not initialized");
265 if (setreuid(sshpam_authctxt
->pw
->pw_uid
, -1) == -1)
266 fatal("%s: setreuid failed: %s", __func__
, strerror(errno
));
267 result
= pam_chauthtok(pamh
, flags
);
268 if (setreuid(0, -1) == -1)
269 fatal("%s: setreuid failed: %s", __func__
, strerror(errno
));
272 # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
276 sshpam_password_change_required(int reqd
)
278 debug3("%s %d", __func__
, reqd
);
279 if (sshpam_authctxt
== NULL
)
280 fatal("%s: PAM authctxt not initialized", __func__
);
281 sshpam_authctxt
->force_pwchange
= reqd
;
283 no_port_forwarding_flag
|= 2;
284 no_agent_forwarding_flag
|= 2;
285 no_x11_forwarding_flag
|= 2;
287 no_port_forwarding_flag
&= ~2;
288 no_agent_forwarding_flag
&= ~2;
289 no_x11_forwarding_flag
&= ~2;
293 /* Import regular and PAM environment from subprocess */
295 import_environments(Buffer
*b
)
301 debug3("PAM: %s entering", __func__
);
303 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
304 /* Import variables set by do_pam_account */
305 sshpam_account_status
= buffer_get_int(b
);
306 sshpam_password_change_required(buffer_get_int(b
));
308 /* Import environment from subprocess */
309 num_env
= buffer_get_int(b
);
311 fatal("%s: received %u environment variables, expected <= 1024",
313 sshpam_env
= xcalloc(num_env
+ 1, sizeof(*sshpam_env
));
314 debug3("PAM: num env strings %d", num_env
);
315 for(i
= 0; i
< num_env
; i
++)
316 sshpam_env
[i
] = buffer_get_string(b
, NULL
);
318 sshpam_env
[num_env
] = NULL
;
320 /* Import PAM environment from subprocess */
321 num_env
= buffer_get_int(b
);
322 debug("PAM: num PAM env strings %d", num_env
);
323 for(i
= 0; i
< num_env
; i
++) {
324 env
= buffer_get_string(b
, NULL
);
326 #ifdef HAVE_PAM_PUTENV
327 /* Errors are not fatal here */
328 if ((err
= pam_putenv(sshpam_handle
, env
)) != PAM_SUCCESS
) {
329 error("PAM: pam_putenv: %s",
330 pam_strerror(sshpam_handle
, sshpam_err
));
338 * Conversation function for authentication thread.
341 sshpam_thread_conv(int n
, sshpam_const
struct pam_message
**msg
,
342 struct pam_response
**resp
, void *data
)
345 struct pam_ctxt
*ctxt
;
346 struct pam_response
*reply
;
349 debug3("PAM: %s entering, %d messages", __func__
, n
);
353 error("PAM: conversation function passed a null context");
354 return (PAM_CONV_ERR
);
357 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
358 return (PAM_CONV_ERR
);
360 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
361 return (PAM_CONV_ERR
);
363 buffer_init(&buffer
);
364 for (i
= 0; i
< n
; ++i
) {
365 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
366 case PAM_PROMPT_ECHO_OFF
:
367 buffer_put_cstring(&buffer
,
368 PAM_MSG_MEMBER(msg
, i
, msg
));
369 if (ssh_msg_send(ctxt
->pam_csock
,
370 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
372 if (ssh_msg_recv(ctxt
->pam_csock
, &buffer
) == -1)
374 if (buffer_get_char(&buffer
) != PAM_AUTHTOK
)
376 reply
[i
].resp
= buffer_get_string(&buffer
, NULL
);
378 case PAM_PROMPT_ECHO_ON
:
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)
384 if (ssh_msg_recv(ctxt
->pam_csock
, &buffer
) == -1)
386 if (buffer_get_char(&buffer
) != PAM_AUTHTOK
)
388 reply
[i
].resp
= buffer_get_string(&buffer
, NULL
);
391 buffer_put_cstring(&buffer
,
392 PAM_MSG_MEMBER(msg
, i
, msg
));
393 if (ssh_msg_send(ctxt
->pam_csock
,
394 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
398 buffer_put_cstring(&buffer
,
399 PAM_MSG_MEMBER(msg
, i
, msg
));
400 if (ssh_msg_send(ctxt
->pam_csock
,
401 PAM_MSG_MEMBER(msg
, i
, msg_style
), &buffer
) == -1)
407 buffer_clear(&buffer
);
409 buffer_free(&buffer
);
411 return (PAM_SUCCESS
);
414 for(i
= 0; i
< n
; i
++) {
415 if (reply
[i
].resp
!= NULL
)
416 xfree(reply
[i
].resp
);
419 buffer_free(&buffer
);
420 return (PAM_CONV_ERR
);
424 * Authentication thread.
427 sshpam_thread(void *ctxtp
)
429 struct pam_ctxt
*ctxt
= ctxtp
;
431 struct pam_conv sshpam_conv
;
432 int flags
= (options
.permit_empty_passwd
== 0 ?
433 PAM_DISALLOW_NULL_AUTHTOK
: 0);
434 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
435 extern char **environ
;
438 const char *pam_user
;
439 const char **ptr_pam_user
= &pam_user
;
441 pam_get_item(sshpam_handle
, PAM_USER
,
442 (sshpam_const
void **)ptr_pam_user
);
445 if (sshpam_authctxt
!= NULL
) {
446 setproctitle("%s [pam]",
447 sshpam_authctxt
->valid
? pam_user
: "unknown");
451 sshpam_conv
.conv
= sshpam_thread_conv
;
452 sshpam_conv
.appdata_ptr
= ctxt
;
454 if (sshpam_authctxt
== NULL
)
455 fatal("%s: PAM authctxt not initialized", __func__
);
457 buffer_init(&buffer
);
458 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
459 (const void *)&sshpam_conv
);
460 if (sshpam_err
!= PAM_SUCCESS
)
462 sshpam_err
= pam_authenticate(sshpam_handle
, flags
);
463 if (sshpam_err
!= PAM_SUCCESS
)
467 if (!do_pam_account()) {
468 sshpam_err
= PAM_ACCT_EXPIRED
;
471 if (sshpam_authctxt
->force_pwchange
) {
472 sshpam_err
= pam_chauthtok(sshpam_handle
,
473 PAM_CHANGE_EXPIRED_AUTHTOK
);
474 if (sshpam_err
!= PAM_SUCCESS
)
476 sshpam_password_change_required(0);
480 buffer_put_cstring(&buffer
, "OK");
482 #ifndef UNSUPPORTED_POSIX_THREADS_HACK
483 /* Export variables set by do_pam_account */
484 buffer_put_int(&buffer
, sshpam_account_status
);
485 buffer_put_int(&buffer
, sshpam_authctxt
->force_pwchange
);
487 /* Export any environment strings set in child */
488 for(i
= 0; environ
[i
] != NULL
; i
++)
490 buffer_put_int(&buffer
, i
);
491 for(i
= 0; environ
[i
] != NULL
; i
++)
492 buffer_put_cstring(&buffer
, environ
[i
]);
494 /* Export any environment strings set by PAM in child */
495 env_from_pam
= pam_getenvlist(sshpam_handle
);
496 for(i
= 0; env_from_pam
!= NULL
&& env_from_pam
[i
] != NULL
; i
++)
498 buffer_put_int(&buffer
, i
);
499 for(i
= 0; env_from_pam
!= NULL
&& env_from_pam
[i
] != NULL
; i
++)
500 buffer_put_cstring(&buffer
, env_from_pam
[i
]);
501 #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
503 /* XXX - can't do much about an error here */
504 ssh_msg_send(ctxt
->pam_csock
, sshpam_err
, &buffer
);
505 buffer_free(&buffer
);
509 buffer_put_cstring(&buffer
,
510 pam_strerror(sshpam_handle
, sshpam_err
));
511 /* XXX - can't do much about an error here */
512 if (sshpam_err
== PAM_ACCT_EXPIRED
)
513 ssh_msg_send(ctxt
->pam_csock
, PAM_ACCT_EXPIRED
, &buffer
);
515 ssh_msg_send(ctxt
->pam_csock
, PAM_AUTH_ERR
, &buffer
);
516 buffer_free(&buffer
);
519 return (NULL
); /* Avoid warning for non-pthread case */
523 sshpam_thread_cleanup(void)
525 struct pam_ctxt
*ctxt
= cleanup_ctxt
;
527 debug3("PAM: %s entering", __func__
);
528 if (ctxt
!= NULL
&& ctxt
->pam_thread
!= 0) {
529 pthread_cancel(ctxt
->pam_thread
);
530 pthread_join(ctxt
->pam_thread
, NULL
);
531 close(ctxt
->pam_psock
);
532 close(ctxt
->pam_csock
);
533 memset(ctxt
, 0, sizeof(*ctxt
));
539 sshpam_null_conv(int n
, sshpam_const
struct pam_message
**msg
,
540 struct pam_response
**resp
, void *data
)
542 debug3("PAM: %s entering, %d messages", __func__
, n
);
543 return (PAM_CONV_ERR
);
546 static struct pam_conv null_conv
= { sshpam_null_conv
, NULL
};
549 sshpam_store_conv(int n
, sshpam_const
struct pam_message
**msg
,
550 struct pam_response
**resp
, void *data
)
552 struct pam_response
*reply
;
556 debug3("PAM: %s called with %d messages", __func__
, n
);
559 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
560 return (PAM_CONV_ERR
);
562 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
563 return (PAM_CONV_ERR
);
565 for (i
= 0; i
< n
; ++i
) {
566 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
569 len
= strlen(PAM_MSG_MEMBER(msg
, i
, msg
));
570 buffer_append(&loginmsg
, PAM_MSG_MEMBER(msg
, i
, msg
), len
);
571 buffer_append(&loginmsg
, "\n", 1 );
572 reply
[i
].resp_retcode
= PAM_SUCCESS
;
579 return (PAM_SUCCESS
);
582 for(i
= 0; i
< n
; i
++) {
583 if (reply
[i
].resp
!= NULL
)
584 xfree(reply
[i
].resp
);
587 return (PAM_CONV_ERR
);
590 static struct pam_conv store_conv
= { sshpam_store_conv
, NULL
};
595 debug("PAM: cleanup");
596 if (sshpam_handle
== NULL
)
598 pam_set_item(sshpam_handle
, PAM_CONV
, (const void *)&null_conv
);
599 if (sshpam_cred_established
) {
600 pam_setcred(sshpam_handle
, PAM_DELETE_CRED
);
601 sshpam_cred_established
= 0;
603 if (sshpam_session_open
) {
604 pam_close_session(sshpam_handle
, PAM_SILENT
);
605 sshpam_session_open
= 0;
607 sshpam_authenticated
= 0;
608 pam_end(sshpam_handle
, sshpam_err
);
609 sshpam_handle
= NULL
;
613 sshpam_init(Authctxt
*authctxt
)
615 extern char *__progname
;
616 const char *pam_rhost
, *pam_user
, *user
= authctxt
->user
;
617 const char **ptr_pam_user
= &pam_user
;
619 if (sshpam_handle
!= NULL
) {
620 /* We already have a PAM context; check if the user matches */
621 sshpam_err
= pam_get_item(sshpam_handle
,
622 PAM_USER
, (sshpam_const
void **)ptr_pam_user
);
623 if (sshpam_err
== PAM_SUCCESS
&& strcmp(user
, pam_user
) == 0)
625 pam_end(sshpam_handle
, sshpam_err
);
626 sshpam_handle
= NULL
;
628 debug("PAM: initializing for \"%s\"", user
);
630 pam_start(SSHD_PAM_SERVICE
, user
, &store_conv
, &sshpam_handle
);
631 sshpam_authctxt
= authctxt
;
633 if (sshpam_err
!= PAM_SUCCESS
) {
634 pam_end(sshpam_handle
, sshpam_err
);
635 sshpam_handle
= NULL
;
638 pam_rhost
= get_remote_name_or_ip(utmp_len
, options
.use_dns
);
639 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost
);
640 sshpam_err
= pam_set_item(sshpam_handle
, PAM_RHOST
, pam_rhost
);
641 if (sshpam_err
!= PAM_SUCCESS
) {
642 pam_end(sshpam_handle
, sshpam_err
);
643 sshpam_handle
= NULL
;
646 #ifdef PAM_TTY_KLUDGE
648 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
649 * sshd doesn't set the tty until too late in the auth process and
650 * may not even set one (for tty-less connections)
652 debug("PAM: setting PAM_TTY to \"ssh\"");
653 sshpam_err
= pam_set_item(sshpam_handle
, PAM_TTY
, "ssh");
654 if (sshpam_err
!= PAM_SUCCESS
) {
655 pam_end(sshpam_handle
, sshpam_err
);
656 sshpam_handle
= NULL
;
664 sshpam_init_ctx(Authctxt
*authctxt
)
666 struct pam_ctxt
*ctxt
;
669 debug3("PAM: %s entering", __func__
);
671 * Refuse to start if we don't have PAM enabled or do_pam_account
672 * has previously failed.
674 if (!options
.use_pam
|| sshpam_account_status
== 0)
678 if (sshpam_init(authctxt
) == -1) {
679 error("PAM: initialization failed");
683 ctxt
= xmalloc(sizeof *ctxt
);
684 memset(ctxt
, 0, sizeof(*ctxt
));
686 /* Start the authentication thread */
687 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, socks
) == -1) {
688 error("PAM: failed create sockets: %s", strerror(errno
));
692 ctxt
->pam_psock
= socks
[0];
693 ctxt
->pam_csock
= socks
[1];
694 if (pthread_create(&ctxt
->pam_thread
, NULL
, sshpam_thread
, ctxt
) == -1) {
695 error("PAM: failed to start authentication thread: %s",
707 sshpam_query(void *ctx
, char **name
, char **info
,
708 u_int
*num
, char ***prompts
, u_int
**echo_on
)
711 struct pam_ctxt
*ctxt
= ctx
;
717 debug3("PAM: %s entering", __func__
);
718 buffer_init(&buffer
);
721 *prompts
= xmalloc(sizeof(char *));
724 *echo_on
= xmalloc(sizeof(u_int
));
725 while (ssh_msg_recv(ctxt
->pam_psock
, &buffer
) == 0) {
726 type
= buffer_get_char(&buffer
);
727 msg
= buffer_get_string(&buffer
, NULL
);
730 case PAM_PROMPT_ECHO_ON
:
731 case PAM_PROMPT_ECHO_OFF
:
733 len
= plen
+ mlen
+ 1;
734 **prompts
= xrealloc(**prompts
, 1, len
);
735 strlcpy(**prompts
+ plen
, msg
, len
- plen
);
737 **echo_on
= (type
== PAM_PROMPT_ECHO_ON
);
742 /* accumulate messages */
743 len
= plen
+ mlen
+ 2;
744 **prompts
= xrealloc(**prompts
, 1, len
);
745 strlcpy(**prompts
+ plen
, msg
, len
- plen
);
747 strlcat(**prompts
+ plen
, "\n", len
- plen
);
751 case PAM_ACCT_EXPIRED
:
752 sshpam_account_status
= 0;
755 debug3("PAM: %s", pam_strerror(sshpam_handle
, type
));
756 if (**prompts
!= NULL
&& strlen(**prompts
) != 0) {
767 if (**prompts
!= NULL
) {
768 /* drain any accumulated messages */
769 debug("PAM: %s", **prompts
);
770 buffer_append(&loginmsg
, **prompts
,
775 if (type
== PAM_SUCCESS
) {
776 if (!sshpam_authctxt
->valid
||
777 (sshpam_authctxt
->pw
->pw_uid
== 0 &&
778 options
.permit_root_login
!= PERMIT_YES
))
779 fatal("Internal error: PAM auth "
780 "succeeded when it should have "
782 import_environments(&buffer
);
789 error("PAM: %s for %s%.100s from %.100s", msg
,
790 sshpam_authctxt
->valid
? "" : "illegal user ",
791 sshpam_authctxt
->user
,
792 get_remote_name_or_ip(utmp_len
, options
.use_dns
));
805 /* XXX - see also comment in auth-chall.c:verify_response */
807 sshpam_respond(void *ctx
, u_int num
, char **resp
)
810 struct pam_ctxt
*ctxt
= ctx
;
812 debug2("PAM: %s entering, %u responses", __func__
, num
);
813 switch (ctxt
->pam_done
) {
815 sshpam_authenticated
= 1;
823 error("PAM: expected one response, got %u", num
);
826 buffer_init(&buffer
);
827 if (sshpam_authctxt
->valid
&&
828 (sshpam_authctxt
->pw
->pw_uid
!= 0 ||
829 options
.permit_root_login
== PERMIT_YES
))
830 buffer_put_cstring(&buffer
, *resp
);
832 buffer_put_cstring(&buffer
, badpw
);
833 if (ssh_msg_send(ctxt
->pam_psock
, PAM_AUTHTOK
, &buffer
) == -1) {
834 buffer_free(&buffer
);
837 buffer_free(&buffer
);
842 sshpam_free_ctx(void *ctxtp
)
844 struct pam_ctxt
*ctxt
= ctxtp
;
846 debug3("PAM: %s entering", __func__
);
847 sshpam_thread_cleanup();
850 * We don't call sshpam_cleanup() here because we may need the PAM
851 * handle at a later stage, e.g. when setting up a session. It's
852 * still on the cleanup list, so pam_end() *will* be called before
853 * the server process terminates.
857 KbdintDevice sshpam_device
= {
865 KbdintDevice mm_sshpam_device
= {
874 * This replaces auth-pam.c
877 start_pam(Authctxt
*authctxt
)
879 if (!options
.use_pam
)
880 fatal("PAM: initialisation requested when UsePAM=no");
882 if (sshpam_init(authctxt
) == -1)
883 fatal("PAM: initialisation failed");
895 debug("%s: called", __func__
);
896 if (sshpam_account_status
!= -1)
897 return (sshpam_account_status
);
899 sshpam_err
= pam_acct_mgmt(sshpam_handle
, 0);
900 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__
, sshpam_err
,
901 pam_strerror(sshpam_handle
, sshpam_err
));
903 if (sshpam_err
!= PAM_SUCCESS
&& sshpam_err
!= PAM_NEW_AUTHTOK_REQD
) {
904 sshpam_account_status
= 0;
905 return (sshpam_account_status
);
908 if (sshpam_err
== PAM_NEW_AUTHTOK_REQD
)
909 sshpam_password_change_required(1);
911 sshpam_account_status
= 1;
912 return (sshpam_account_status
);
916 do_pam_set_tty(const char *tty
)
919 debug("PAM: setting PAM_TTY to \"%s\"", tty
);
920 sshpam_err
= pam_set_item(sshpam_handle
, PAM_TTY
, tty
);
921 if (sshpam_err
!= PAM_SUCCESS
)
922 fatal("PAM: failed to set PAM_TTY: %s",
923 pam_strerror(sshpam_handle
, sshpam_err
));
928 do_pam_setcred(int init
)
930 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
931 (const void *)&store_conv
);
932 if (sshpam_err
!= PAM_SUCCESS
)
933 fatal("PAM: failed to set PAM_CONV: %s",
934 pam_strerror(sshpam_handle
, sshpam_err
));
936 debug("PAM: establishing credentials");
937 sshpam_err
= pam_setcred(sshpam_handle
, PAM_ESTABLISH_CRED
);
939 debug("PAM: reinitializing credentials");
940 sshpam_err
= pam_setcred(sshpam_handle
, PAM_REINITIALIZE_CRED
);
942 if (sshpam_err
== PAM_SUCCESS
) {
943 sshpam_cred_established
= 1;
946 if (sshpam_authenticated
)
947 fatal("PAM: pam_setcred(): %s",
948 pam_strerror(sshpam_handle
, sshpam_err
));
950 debug("PAM: pam_setcred(): %s",
951 pam_strerror(sshpam_handle
, sshpam_err
));
955 sshpam_tty_conv(int n
, sshpam_const
struct pam_message
**msg
,
956 struct pam_response
**resp
, void *data
)
958 char input
[PAM_MAX_MSG_SIZE
];
959 struct pam_response
*reply
;
962 debug3("PAM: %s called with %d messages", __func__
, n
);
966 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
|| !isatty(STDIN_FILENO
))
967 return (PAM_CONV_ERR
);
969 if ((reply
= calloc(n
, sizeof(*reply
))) == NULL
)
970 return (PAM_CONV_ERR
);
972 for (i
= 0; i
< n
; ++i
) {
973 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
974 case PAM_PROMPT_ECHO_OFF
:
976 read_passphrase(PAM_MSG_MEMBER(msg
, i
, msg
),
978 reply
[i
].resp_retcode
= PAM_SUCCESS
;
980 case PAM_PROMPT_ECHO_ON
:
981 fprintf(stderr
, "%s\n", PAM_MSG_MEMBER(msg
, i
, msg
));
982 fgets(input
, sizeof input
, stdin
);
983 if ((reply
[i
].resp
= strdup(input
)) == NULL
)
985 reply
[i
].resp_retcode
= PAM_SUCCESS
;
989 fprintf(stderr
, "%s\n", PAM_MSG_MEMBER(msg
, i
, msg
));
990 reply
[i
].resp_retcode
= PAM_SUCCESS
;
997 return (PAM_SUCCESS
);
1000 for(i
= 0; i
< n
; i
++) {
1001 if (reply
[i
].resp
!= NULL
)
1002 xfree(reply
[i
].resp
);
1005 return (PAM_CONV_ERR
);
1008 static struct pam_conv tty_conv
= { sshpam_tty_conv
, NULL
};
1011 * XXX this should be done in the authentication phase, but ssh1 doesn't
1015 do_pam_chauthtok(void)
1018 fatal("Password expired (unable to change with privsep)");
1019 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1020 (const void *)&tty_conv
);
1021 if (sshpam_err
!= PAM_SUCCESS
)
1022 fatal("PAM: failed to set PAM_CONV: %s",
1023 pam_strerror(sshpam_handle
, sshpam_err
));
1024 debug("PAM: changing password");
1025 sshpam_err
= pam_chauthtok(sshpam_handle
, PAM_CHANGE_EXPIRED_AUTHTOK
);
1026 if (sshpam_err
!= PAM_SUCCESS
)
1027 fatal("PAM: pam_chauthtok(): %s",
1028 pam_strerror(sshpam_handle
, sshpam_err
));
1032 do_pam_session(void)
1034 debug3("PAM: opening session");
1035 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1036 (const void *)&store_conv
);
1037 if (sshpam_err
!= PAM_SUCCESS
)
1038 fatal("PAM: failed to set PAM_CONV: %s",
1039 pam_strerror(sshpam_handle
, sshpam_err
));
1040 sshpam_err
= pam_open_session(sshpam_handle
, 0);
1041 if (sshpam_err
== PAM_SUCCESS
)
1042 sshpam_session_open
= 1;
1044 sshpam_session_open
= 0;
1045 disable_forwarding();
1046 error("PAM: pam_open_session(): %s",
1047 pam_strerror(sshpam_handle
, sshpam_err
));
1053 is_pam_session_open(void)
1055 return sshpam_session_open
;
1059 * Set a PAM environment string. We need to do this so that the session
1060 * modules can handle things like Kerberos/GSI credentials that appear
1061 * during the ssh authentication process.
1064 do_pam_putenv(char *name
, char *value
)
1067 #ifdef HAVE_PAM_PUTENV
1071 len
= strlen(name
) + strlen(value
) + 2;
1072 compound
= xmalloc(len
);
1074 snprintf(compound
, len
, "%s=%s", name
, value
);
1075 ret
= pam_putenv(sshpam_handle
, compound
);
1083 fetch_pam_child_environment(void)
1089 fetch_pam_environment(void)
1091 return (pam_getenvlist(sshpam_handle
));
1095 free_pam_environment(char **env
)
1102 for (envp
= env
; *envp
; envp
++)
1108 * "Blind" conversation function for password authentication. Assumes that
1109 * echo-off prompts are for the password and stores messages for later
1113 sshpam_passwd_conv(int n
, sshpam_const
struct pam_message
**msg
,
1114 struct pam_response
**resp
, void *data
)
1116 struct pam_response
*reply
;
1120 debug3("PAM: %s called with %d messages", __func__
, n
);
1124 if (n
<= 0 || n
> PAM_MAX_NUM_MSG
)
1125 return (PAM_CONV_ERR
);
1127 if ((reply
= malloc(n
* sizeof(*reply
))) == NULL
)
1128 return (PAM_CONV_ERR
);
1129 memset(reply
, 0, n
* sizeof(*reply
));
1131 for (i
= 0; i
< n
; ++i
) {
1132 switch (PAM_MSG_MEMBER(msg
, i
, msg_style
)) {
1133 case PAM_PROMPT_ECHO_OFF
:
1134 if (sshpam_password
== NULL
)
1136 if ((reply
[i
].resp
= strdup(sshpam_password
)) == NULL
)
1138 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1142 len
= strlen(PAM_MSG_MEMBER(msg
, i
, msg
));
1144 buffer_append(&loginmsg
,
1145 PAM_MSG_MEMBER(msg
, i
, msg
), len
);
1146 buffer_append(&loginmsg
, "\n", 1);
1148 if ((reply
[i
].resp
= strdup("")) == NULL
)
1150 reply
[i
].resp_retcode
= PAM_SUCCESS
;
1157 return (PAM_SUCCESS
);
1160 for(i
= 0; i
< n
; i
++) {
1161 if (reply
[i
].resp
!= NULL
)
1162 xfree(reply
[i
].resp
);
1165 return (PAM_CONV_ERR
);
1168 static struct pam_conv passwd_conv
= { sshpam_passwd_conv
, NULL
};
1171 * Attempt password authentication via PAM
1174 sshpam_auth_passwd(Authctxt
*authctxt
, const char *password
)
1176 int flags
= (options
.permit_empty_passwd
== 0 ?
1177 PAM_DISALLOW_NULL_AUTHTOK
: 0);
1179 if (!options
.use_pam
|| sshpam_handle
== NULL
)
1180 fatal("PAM: %s called when PAM disabled or failed to "
1181 "initialise.", __func__
);
1183 sshpam_password
= password
;
1184 sshpam_authctxt
= authctxt
;
1187 * If the user logging in is invalid, or is root but is not permitted
1188 * by PermitRootLogin, use an invalid password to prevent leaking
1189 * information via timing (eg if the PAM config has a delay on fail).
1191 if (!authctxt
->valid
|| (authctxt
->pw
->pw_uid
== 0 &&
1192 options
.permit_root_login
!= PERMIT_YES
))
1193 sshpam_password
= badpw
;
1195 sshpam_err
= pam_set_item(sshpam_handle
, PAM_CONV
,
1196 (const void *)&passwd_conv
);
1197 if (sshpam_err
!= PAM_SUCCESS
)
1198 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__
,
1199 pam_strerror(sshpam_handle
, sshpam_err
));
1201 sshpam_err
= pam_authenticate(sshpam_handle
, flags
);
1202 sshpam_password
= NULL
;
1203 if (sshpam_err
== PAM_SUCCESS
&& authctxt
->valid
) {
1204 debug("PAM: password authentication accepted for %.100s",
1208 debug("PAM: password authentication failed for %.100s: %s",
1209 authctxt
->valid
? authctxt
->user
: "an illegal user",
1210 pam_strerror(sshpam_handle
, sshpam_err
));
1214 #endif /* USE_PAM */