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 $ */
53 #if defined(HAVE_SECURITY_PAM_APPL_H)
54 #include <security/pam_appl.h>
55 #elif defined (HAVE_PAM_PAM_APPL_H)
56 #include <pam/pam_appl.h>
65 #include "monitor_wrap.h"
72 #include "auth-options.h"
74 extern ServerOptions options;
75 extern Buffer loginmsg;
77 extern u_int utmp_len;
79 #ifdef USE_POSIX_THREADS
82 * Avoid namespace clash when *not* using pthreads for systems *with*
83 * pthreads, which unconditionally define pthread_t via sys/types.h
86 typedef pthread_t sp_pthread_t;
88 typedef pid_t sp_pthread_t;
92 sp_pthread_t pam_thread;
98 static void sshpam_free_ctx(void *);
99 static struct pam_ctxt *cleanup_ctxt;
101 #ifndef USE_POSIX_THREADS
103 * Simulate threads with processes.
106 static int sshpam_thread_status = -1;
107 static mysig_t sshpam_oldsig;
110 sshpam_sigchld_handler(int sig)
112 signal(SIGCHLD, SIG_DFL);
113 if (cleanup_ctxt == NULL)
114 return; /* handler called after PAM cleanup, shouldn't happen */
115 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
117 /* PAM thread has not exitted, privsep slave must have */
118 kill(cleanup_ctxt->pam_thread, SIGTERM);
119 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
121 return; /* could not wait */
123 if (WIFSIGNALED(sshpam_thread_status) &&
124 WTERMSIG(sshpam_thread_status) == SIGTERM)
125 return; /* terminated by pthread_cancel */
126 if (!WIFEXITED(sshpam_thread_status))
127 fatal("PAM: authentication thread exited unexpectedly");
128 if (WEXITSTATUS(sshpam_thread_status) != 0)
129 fatal("PAM: authentication thread exited uncleanly");
133 pthread_exit(void *value __unused)
139 pthread_create(sp_pthread_t *thread, const void *attr __unused,
140 void *(*thread_start)(void *), void *arg)
144 sshpam_thread_status = -1;
145 switch ((pid = fork())) {
147 error("fork(): %s", strerror(errno));
154 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
160 pthread_cancel(sp_pthread_t thread)
162 signal(SIGCHLD, sshpam_oldsig);
163 return (kill(thread, SIGTERM));
167 pthread_join(sp_pthread_t thread, void **value __unused)
171 if (sshpam_thread_status != -1)
172 return (sshpam_thread_status);
173 signal(SIGCHLD, sshpam_oldsig);
174 waitpid(thread, &status, 0);
180 static pam_handle_t *sshpam_handle = NULL;
181 static int sshpam_err = 0;
182 static int sshpam_authenticated = 0;
183 static int sshpam_session_open = 0;
184 static int sshpam_cred_established = 0;
185 static int sshpam_account_status = -1;
186 static char **sshpam_env = NULL;
187 static Authctxt *sshpam_authctxt = NULL;
188 static const char *sshpam_password = NULL;
190 /* Some PAM implementations don't implement this */
191 #ifndef HAVE_PAM_GETENVLIST
193 pam_getenvlist(pam_handle_t *pamh)
196 * XXX - If necessary, we can still support envrionment passing
197 * for platforms without pam_getenvlist by searching for known
198 * env vars (e.g. KRB5CCNAME) from the PAM environment.
205 sshpam_password_change_required(int reqd)
207 debug3("%s %d", __func__, reqd);
208 if (sshpam_authctxt == NULL)
209 fatal("%s: PAM authctxt not initialized", __func__);
210 sshpam_authctxt->force_pwchange = reqd;
212 no_port_forwarding_flag |= 2;
213 no_agent_forwarding_flag |= 2;
214 no_x11_forwarding_flag |= 2;
216 no_port_forwarding_flag &= ~2;
217 no_agent_forwarding_flag &= ~2;
218 no_x11_forwarding_flag &= ~2;
222 /* Import regular and PAM environment from subprocess */
224 import_environments(Buffer *b)
230 debug3("PAM: %s entering", __func__);
232 #ifndef USE_POSIX_THREADS
233 /* Import variables set by do_pam_account */
234 sshpam_account_status = buffer_get_int(b);
235 sshpam_password_change_required(buffer_get_int(b));
237 /* Import environment from subprocess */
238 num_env = buffer_get_int(b);
239 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
240 debug3("PAM: num env strings %d", num_env);
241 for(i = 0; i < num_env; i++)
242 sshpam_env[i] = buffer_get_string(b, NULL);
244 sshpam_env[num_env] = NULL;
246 /* Import PAM environment from subprocess */
247 num_env = buffer_get_int(b);
248 debug("PAM: num PAM env strings %d", num_env);
249 for(i = 0; i < num_env; i++) {
250 env = buffer_get_string(b, NULL);
252 #ifdef HAVE_PAM_PUTENV
253 /* Errors are not fatal here */
254 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
255 error("PAM: pam_putenv: %s",
256 pam_strerror(sshpam_handle, sshpam_err));
264 * Conversation function for authentication thread.
267 sshpam_thread_conv(int n, struct pam_message **msg,
268 struct pam_response **resp, void *data)
271 struct pam_ctxt *ctxt;
272 struct pam_response *reply;
275 debug3("PAM: %s entering, %d messages", __func__, n);
279 error("PAM: conversation function passed a null context");
280 return (PAM_CONV_ERR);
283 if (n <= 0 || n > PAM_MAX_NUM_MSG)
284 return (PAM_CONV_ERR);
286 if ((reply = malloc(n * sizeof(*reply))) == NULL)
287 return (PAM_CONV_ERR);
288 memset(reply, 0, n * sizeof(*reply));
290 buffer_init(&buffer);
291 for (i = 0; i < n; ++i) {
292 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
293 case PAM_PROMPT_ECHO_OFF:
294 buffer_put_cstring(&buffer,
295 PAM_MSG_MEMBER(msg, i, msg));
296 if (ssh_msg_send(ctxt->pam_csock,
297 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
299 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
301 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
303 reply[i].resp = buffer_get_string(&buffer, NULL);
305 case PAM_PROMPT_ECHO_ON:
306 buffer_put_cstring(&buffer,
307 PAM_MSG_MEMBER(msg, i, msg));
308 if (ssh_msg_send(ctxt->pam_csock,
309 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
311 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
313 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
315 reply[i].resp = buffer_get_string(&buffer, NULL);
318 buffer_put_cstring(&buffer,
319 PAM_MSG_MEMBER(msg, i, msg));
320 if (ssh_msg_send(ctxt->pam_csock,
321 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
325 buffer_put_cstring(&buffer,
326 PAM_MSG_MEMBER(msg, i, msg));
327 if (ssh_msg_send(ctxt->pam_csock,
328 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
334 buffer_clear(&buffer);
336 buffer_free(&buffer);
338 return (PAM_SUCCESS);
341 for(i = 0; i < n; i++) {
342 if (reply[i].resp != NULL)
343 xfree(reply[i].resp);
346 buffer_free(&buffer);
347 return (PAM_CONV_ERR);
351 * Authentication thread.
354 sshpam_thread(void *ctxtp)
356 struct pam_ctxt *ctxt = ctxtp;
358 struct pam_conv sshpam_conv;
359 int flags = (options.permit_empty_passwd == 0 ?
360 PAM_DISALLOW_NULL_AUTHTOK : 0);
361 #ifndef USE_POSIX_THREADS
362 extern char **environ;
365 const char *pam_user;
367 pam_get_item(sshpam_handle, PAM_USER, (void **)&pam_user);
368 setproctitle("%s [pam]", pam_user);
372 sshpam_conv.conv = sshpam_thread_conv;
373 sshpam_conv.appdata_ptr = ctxt;
375 if (sshpam_authctxt == NULL)
376 fatal("%s: PAM authctxt not initialized", __func__);
378 buffer_init(&buffer);
379 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
380 (const void *)&sshpam_conv);
381 if (sshpam_err != PAM_SUCCESS)
383 sshpam_err = pam_authenticate(sshpam_handle, flags);
384 if (sshpam_err != PAM_SUCCESS)
388 if (!do_pam_account())
390 if (sshpam_authctxt->force_pwchange) {
391 sshpam_err = pam_chauthtok(sshpam_handle,
392 PAM_CHANGE_EXPIRED_AUTHTOK);
393 if (sshpam_err != PAM_SUCCESS)
395 sshpam_password_change_required(0);
399 buffer_put_cstring(&buffer, "OK");
401 #ifndef USE_POSIX_THREADS
402 /* Export variables set by do_pam_account */
403 buffer_put_int(&buffer, sshpam_account_status);
404 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
406 /* Export any environment strings set in child */
407 for(i = 0; environ[i] != NULL; i++)
409 buffer_put_int(&buffer, i);
410 for(i = 0; environ[i] != NULL; i++)
411 buffer_put_cstring(&buffer, environ[i]);
413 /* Export any environment strings set by PAM in child */
414 env_from_pam = pam_getenvlist(sshpam_handle);
415 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
417 buffer_put_int(&buffer, i);
418 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
419 buffer_put_cstring(&buffer, env_from_pam[i]);
420 #endif /* USE_POSIX_THREADS */
422 /* XXX - can't do much about an error here */
423 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
424 buffer_free(&buffer);
428 buffer_put_cstring(&buffer,
429 pam_strerror(sshpam_handle, sshpam_err));
430 /* XXX - can't do much about an error here */
431 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
432 buffer_free(&buffer);
435 return (NULL); /* Avoid warning for non-pthread case */
439 sshpam_thread_cleanup(void)
441 struct pam_ctxt *ctxt = cleanup_ctxt;
443 debug3("PAM: %s entering", __func__);
444 if (ctxt != NULL && ctxt->pam_thread != 0) {
445 pthread_cancel(ctxt->pam_thread);
446 pthread_join(ctxt->pam_thread, NULL);
447 close(ctxt->pam_psock);
448 close(ctxt->pam_csock);
449 memset(ctxt, 0, sizeof(*ctxt));
455 sshpam_null_conv(int n, struct pam_message **msg,
456 struct pam_response **resp, void *data)
458 debug3("PAM: %s entering, %d messages", __func__, n);
459 return (PAM_CONV_ERR);
462 static struct pam_conv null_conv = { sshpam_null_conv, NULL };
467 debug("PAM: cleanup");
468 if (sshpam_handle == NULL)
470 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
471 if (sshpam_cred_established) {
472 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
473 sshpam_cred_established = 0;
475 if (sshpam_session_open) {
476 pam_close_session(sshpam_handle, PAM_SILENT);
477 sshpam_session_open = 0;
479 sshpam_authenticated = 0;
480 pam_end(sshpam_handle, sshpam_err);
481 sshpam_handle = NULL;
485 sshpam_init(Authctxt *authctxt)
487 extern char *__progname;
488 const char *pam_rhost, *pam_user, *user = authctxt->user;
490 if (sshpam_handle != NULL) {
491 /* We already have a PAM context; check if the user matches */
492 sshpam_err = pam_get_item(sshpam_handle,
493 PAM_USER, (void **)&pam_user);
494 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
496 pam_end(sshpam_handle, sshpam_err);
497 sshpam_handle = NULL;
499 debug("PAM: initializing for \"%s\"", user);
501 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
502 sshpam_authctxt = authctxt;
504 if (sshpam_err != PAM_SUCCESS) {
505 pam_end(sshpam_handle, sshpam_err);
506 sshpam_handle = NULL;
509 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
510 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
511 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
512 if (sshpam_err != PAM_SUCCESS) {
513 pam_end(sshpam_handle, sshpam_err);
514 sshpam_handle = NULL;
517 #ifdef PAM_TTY_KLUDGE
519 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
520 * sshd doesn't set the tty until too late in the auth process and
521 * may not even set one (for tty-less connections)
523 debug("PAM: setting PAM_TTY to \"ssh\"");
524 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
525 if (sshpam_err != PAM_SUCCESS) {
526 pam_end(sshpam_handle, sshpam_err);
527 sshpam_handle = NULL;
535 sshpam_init_ctx(Authctxt *authctxt)
537 struct pam_ctxt *ctxt;
540 debug3("PAM: %s entering", __func__);
541 /* Refuse to start if we don't have PAM enabled */
542 if (!options.use_pam)
546 if (sshpam_init(authctxt) == -1) {
547 error("PAM: initialization failed");
551 ctxt = xmalloc(sizeof *ctxt);
552 memset(ctxt, 0, sizeof(*ctxt));
554 /* Start the authentication thread */
555 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
556 error("PAM: failed create sockets: %s", strerror(errno));
560 ctxt->pam_psock = socks[0];
561 ctxt->pam_csock = socks[1];
562 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
563 error("PAM: failed to start authentication thread: %s",
575 sshpam_query(void *ctx, char **name, char **info,
576 u_int *num, char ***prompts, u_int **echo_on)
579 struct pam_ctxt *ctxt = ctx;
585 debug3("PAM: %s entering", __func__);
586 buffer_init(&buffer);
589 *prompts = xmalloc(sizeof(char *));
592 *echo_on = xmalloc(sizeof(u_int));
593 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
594 type = buffer_get_char(&buffer);
595 msg = buffer_get_string(&buffer, NULL);
597 case PAM_PROMPT_ECHO_ON:
598 case PAM_PROMPT_ECHO_OFF:
600 len = plen + strlen(msg) + 1;
601 **prompts = xrealloc(**prompts, len);
602 plen += snprintf(**prompts + plen, len, "%s", msg);
603 **echo_on = (type == PAM_PROMPT_ECHO_ON);
608 /* accumulate messages */
609 len = plen + strlen(msg) + 2;
610 **prompts = xrealloc(**prompts, len);
611 plen += snprintf(**prompts + plen, len, "%s\n", msg);
616 if (**prompts != NULL) {
617 /* drain any accumulated messages */
618 debug("PAM: %s", **prompts);
619 buffer_append(&loginmsg, **prompts,
624 if (type == PAM_SUCCESS) {
625 import_environments(&buffer);
632 error("PAM: %s for %s%.100s from %.100s", msg,
633 sshpam_authctxt->valid ? "" : "illegal user ",
634 sshpam_authctxt->user,
635 get_remote_name_or_ip(utmp_len, options.use_dns));
648 /* XXX - see also comment in auth-chall.c:verify_response */
650 sshpam_respond(void *ctx, u_int num, char **resp)
653 struct pam_ctxt *ctxt = ctx;
655 debug2("PAM: %s entering, %d responses", __func__, num);
656 switch (ctxt->pam_done) {
658 sshpam_authenticated = 1;
666 error("PAM: expected one response, got %u", num);
669 buffer_init(&buffer);
670 buffer_put_cstring(&buffer, *resp);
671 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
672 buffer_free(&buffer);
675 buffer_free(&buffer);
680 sshpam_free_ctx(void *ctxtp)
682 struct pam_ctxt *ctxt = ctxtp;
684 debug3("PAM: %s entering", __func__);
685 sshpam_thread_cleanup();
688 * We don't call sshpam_cleanup() here because we may need the PAM
689 * handle at a later stage, e.g. when setting up a session. It's
690 * still on the cleanup list, so pam_end() *will* be called before
691 * the server process terminates.
695 KbdintDevice sshpam_device = {
703 KbdintDevice mm_sshpam_device = {
712 * This replaces auth-pam.c
715 start_pam(Authctxt *authctxt)
717 if (!options.use_pam)
718 fatal("PAM: initialisation requested when UsePAM=no");
720 if (sshpam_init(authctxt) == -1)
721 fatal("PAM: initialisation failed");
733 if (sshpam_account_status != -1)
734 return (sshpam_account_status);
736 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
737 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
739 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
740 sshpam_account_status = 0;
741 return (sshpam_account_status);
744 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
745 sshpam_password_change_required(1);
747 sshpam_account_status = 1;
748 return (sshpam_account_status);
752 do_pam_set_tty(const char *tty)
755 debug("PAM: setting PAM_TTY to \"%s\"", tty);
756 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
757 if (sshpam_err != PAM_SUCCESS)
758 fatal("PAM: failed to set PAM_TTY: %s",
759 pam_strerror(sshpam_handle, sshpam_err));
764 do_pam_setcred(int init)
766 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
767 (const void *)&null_conv);
768 if (sshpam_err != PAM_SUCCESS)
769 fatal("PAM: failed to set PAM_CONV: %s",
770 pam_strerror(sshpam_handle, sshpam_err));
772 debug("PAM: establishing credentials");
773 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
775 debug("PAM: reinitializing credentials");
776 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
778 if (sshpam_err == PAM_SUCCESS) {
779 sshpam_cred_established = 1;
782 if (sshpam_authenticated)
783 fatal("PAM: pam_setcred(): %s",
784 pam_strerror(sshpam_handle, sshpam_err));
786 debug("PAM: pam_setcred(): %s",
787 pam_strerror(sshpam_handle, sshpam_err));
791 sshpam_tty_conv(int n, struct pam_message **msg,
792 struct pam_response **resp, void *data)
794 char input[PAM_MAX_MSG_SIZE];
795 struct pam_response *reply;
798 debug3("PAM: %s called with %d messages", __func__, n);
802 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
803 return (PAM_CONV_ERR);
805 if ((reply = malloc(n * sizeof(*reply))) == NULL)
806 return (PAM_CONV_ERR);
807 memset(reply, 0, n * sizeof(*reply));
809 for (i = 0; i < n; ++i) {
810 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
811 case PAM_PROMPT_ECHO_OFF:
813 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
815 reply[i].resp_retcode = PAM_SUCCESS;
817 case PAM_PROMPT_ECHO_ON:
818 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
819 fgets(input, sizeof input, stdin);
820 if ((reply[i].resp = strdup(input)) == NULL)
822 reply[i].resp_retcode = PAM_SUCCESS;
826 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
827 reply[i].resp_retcode = PAM_SUCCESS;
834 return (PAM_SUCCESS);
837 for(i = 0; i < n; i++) {
838 if (reply[i].resp != NULL)
839 xfree(reply[i].resp);
842 return (PAM_CONV_ERR);
845 static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
848 * XXX this should be done in the authentication phase, but ssh1 doesn't
852 do_pam_chauthtok(void)
855 fatal("Password expired (unable to change with privsep)");
856 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
857 (const void *)&tty_conv);
858 if (sshpam_err != PAM_SUCCESS)
859 fatal("PAM: failed to set PAM_CONV: %s",
860 pam_strerror(sshpam_handle, sshpam_err));
861 debug("PAM: changing password");
862 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
863 if (sshpam_err != PAM_SUCCESS)
864 fatal("PAM: pam_chauthtok(): %s",
865 pam_strerror(sshpam_handle, sshpam_err));
869 sshpam_store_conv(int n, struct pam_message **msg,
870 struct pam_response **resp, void *data)
872 struct pam_response *reply;
876 debug3("PAM: %s called with %d messages", __func__, n);
879 if (n <= 0 || n > PAM_MAX_NUM_MSG)
880 return (PAM_CONV_ERR);
882 if ((reply = malloc(n * sizeof(*reply))) == NULL)
883 return (PAM_CONV_ERR);
884 memset(reply, 0, n * sizeof(*reply));
886 for (i = 0; i < n; ++i) {
887 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
890 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
891 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
892 buffer_append(&loginmsg, "\n", 1 );
893 reply[i].resp_retcode = PAM_SUCCESS;
900 return (PAM_SUCCESS);
903 for(i = 0; i < n; i++) {
904 if (reply[i].resp != NULL)
905 xfree(reply[i].resp);
908 return (PAM_CONV_ERR);
911 static struct pam_conv store_conv = { sshpam_store_conv, NULL };
916 debug3("PAM: opening session");
917 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
918 (const void *)&store_conv);
919 if (sshpam_err != PAM_SUCCESS)
920 fatal("PAM: failed to set PAM_CONV: %s",
921 pam_strerror(sshpam_handle, sshpam_err));
922 sshpam_err = pam_open_session(sshpam_handle, 0);
923 if (sshpam_err != PAM_SUCCESS)
924 fatal("PAM: pam_open_session(): %s",
925 pam_strerror(sshpam_handle, sshpam_err));
926 sshpam_session_open = 1;
930 * Set a PAM environment string. We need to do this so that the session
931 * modules can handle things like Kerberos/GSI credentials that appear
932 * during the ssh authentication process.
935 do_pam_putenv(char *name, char *value)
938 #ifdef HAVE_PAM_PUTENV
942 len = strlen(name) + strlen(value) + 2;
943 compound = xmalloc(len);
945 snprintf(compound, len, "%s=%s", name, value);
946 ret = pam_putenv(sshpam_handle, compound);
954 fetch_pam_child_environment(void)
960 fetch_pam_environment(void)
962 return (pam_getenvlist(sshpam_handle));
966 free_pam_environment(char **env)
973 for (envp = env; *envp; envp++)
979 * "Blind" conversation function for password authentication. Assumes that
980 * echo-off prompts are for the password and stores messages for later
984 sshpam_passwd_conv(int n, struct pam_message **msg,
985 struct pam_response **resp, void *data)
987 struct pam_response *reply;
991 debug3("PAM: %s called with %d messages", __func__, n);
995 if (n <= 0 || n > PAM_MAX_NUM_MSG)
996 return (PAM_CONV_ERR);
998 if ((reply = malloc(n * sizeof(*reply))) == NULL)
999 return (PAM_CONV_ERR);
1000 memset(reply, 0, n * sizeof(*reply));
1002 for (i = 0; i < n; ++i) {
1003 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1004 case PAM_PROMPT_ECHO_OFF:
1005 if (sshpam_password == NULL)
1007 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1009 reply[i].resp_retcode = PAM_SUCCESS;
1013 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1015 buffer_append(&loginmsg,
1016 PAM_MSG_MEMBER(msg, i, msg), len);
1017 buffer_append(&loginmsg, "\n", 1);
1019 if ((reply[i].resp = strdup("")) == NULL)
1021 reply[i].resp_retcode = PAM_SUCCESS;
1028 return (PAM_SUCCESS);
1031 for(i = 0; i < n; i++) {
1032 if (reply[i].resp != NULL)
1033 xfree(reply[i].resp);
1036 return (PAM_CONV_ERR);
1039 static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1042 * Attempt password authentication via PAM
1045 sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1047 int flags = (options.permit_empty_passwd == 0 ?
1048 PAM_DISALLOW_NULL_AUTHTOK : 0);
1049 static char badpw[] = "\b\n\r\177INCORRECT";
1051 if (!options.use_pam || sshpam_handle == NULL)
1052 fatal("PAM: %s called when PAM disabled or failed to "
1053 "initialise.", __func__);
1055 sshpam_password = password;
1056 sshpam_authctxt = authctxt;
1059 * If the user logging in is invalid, or is root but is not permitted
1060 * by PermitRootLogin, use an invalid password to prevent leaking
1061 * information via timing (eg if the PAM config has a delay on fail).
1063 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1064 options.permit_root_login != PERMIT_YES))
1065 sshpam_password = badpw;
1067 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1068 (const void *)&passwd_conv);
1069 if (sshpam_err != PAM_SUCCESS)
1070 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1071 pam_strerror(sshpam_handle, sshpam_err));
1073 sshpam_err = pam_authenticate(sshpam_handle, flags);
1074 sshpam_password = NULL;
1075 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1076 debug("PAM: password authentication accepted for %.100s",
1080 debug("PAM: password authentication failed for %.100s: %s",
1081 authctxt->valid ? authctxt->user : "an illegal user",
1082 pam_strerror(sshpam_handle, sshpam_err));
1086 #endif /* USE_PAM */