Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2002 Networks Associates Technology, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 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. |
Damien Miller | e69f18c | 2000-06-12 16:38:54 +1000 | [diff] [blame] | 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 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. |
Damien Miller | e69f18c | 2000-06-12 16:38:54 +1000 | [diff] [blame] | 18 | * |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 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 |
| 29 | * SUCH DAMAGE. |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 30 | */ |
Damien Miller | 26314f6 | 2004-06-01 11:28:20 +1000 | [diff] [blame] | 31 | /* |
| 32 | * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org> |
| 33 | * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au> |
| 34 | * |
| 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. |
| 38 | * |
| 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. |
| 46 | */ |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 47 | |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 48 | /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */ |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 49 | #include "includes.h" |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 50 | |
| 51 | #include <sys/types.h> |
| 52 | #include <sys/stat.h> |
| 53 | #include <sys/wait.h> |
Darren Tucker | 341dae5 | 2006-07-13 08:45:14 +1000 | [diff] [blame] | 54 | |
| 55 | #include <errno.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 56 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 57 | #include <stdarg.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 58 | #include <string.h> |
| 59 | #include <unistd.h> |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 60 | |
| 61 | #ifdef USE_PAM |
Damien Miller | 0f47c53 | 2004-01-02 18:01:30 +1100 | [diff] [blame] | 62 | #if defined(HAVE_SECURITY_PAM_APPL_H) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 63 | #include <security/pam_appl.h> |
Damien Miller | 0f47c53 | 2004-01-02 18:01:30 +1100 | [diff] [blame] | 64 | #elif defined (HAVE_PAM_PAM_APPL_H) |
| 65 | #include <pam/pam_appl.h> |
| 66 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 67 | |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 68 | /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */ |
| 69 | #ifdef PAM_SUN_CODEBASE |
| 70 | # define sshpam_const /* Solaris, HP-UX, AIX */ |
| 71 | #else |
| 72 | # define sshpam_const const /* LinuxPAM, OpenPAM */ |
| 73 | #endif |
| 74 | |
Damien Miller | 2ab323e | 2006-08-05 12:43:32 +1000 | [diff] [blame] | 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) |
| 78 | #else |
| 79 | # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member) |
| 80 | #endif |
| 81 | |
Damien Miller | 75bb664 | 2006-08-05 14:07:20 +1000 | [diff] [blame] | 82 | #include "xmalloc.h" |
| 83 | #include "buffer.h" |
| 84 | #include "key.h" |
| 85 | #include "hostfile.h" |
Kevin Steves | e683e76 | 2002-04-04 19:02:28 +0000 | [diff] [blame] | 86 | #include "auth.h" |
Damien Miller | 63dc3e9 | 2001-02-07 12:58:33 +1100 | [diff] [blame] | 87 | #include "auth-pam.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 88 | #include "canohost.h" |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 89 | #include "log.h" |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 90 | #include "msg.h" |
| 91 | #include "packet.h" |
Darren Tucker | b6db172 | 2004-05-13 17:29:35 +1000 | [diff] [blame] | 92 | #include "misc.h" |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 93 | #include "servconf.h" |
| 94 | #include "ssh2.h" |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 95 | #include "auth-options.h" |
Damien Miller | 75bb664 | 2006-08-05 14:07:20 +1000 | [diff] [blame] | 96 | #ifdef GSSAPI |
| 97 | #include "ssh-gss.h" |
| 98 | #endif |
| 99 | #include "monitor_wrap.h" |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 100 | |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 101 | extern ServerOptions options; |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 102 | extern Buffer loginmsg; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 103 | extern int compat20; |
Darren Tucker | 2a9bf4b | 2004-04-18 11:00:26 +1000 | [diff] [blame] | 104 | extern u_int utmp_len; |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 105 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 106 | /* so we don't silently change behaviour */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 107 | #ifdef USE_POSIX_THREADS |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 108 | # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK" |
| 109 | #endif |
| 110 | |
| 111 | /* |
| 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 |
| 114 | * this breaks. |
| 115 | */ |
| 116 | #ifdef UNSUPPORTED_POSIX_THREADS_HACK |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 117 | #include <pthread.h> |
| 118 | /* |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 119 | * Avoid namespace clash when *not* using pthreads for systems *with* |
| 120 | * pthreads, which unconditionally define pthread_t via sys/types.h |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 121 | * (e.g. Linux) |
| 122 | */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 123 | typedef pthread_t sp_pthread_t; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 124 | #else |
Darren Tucker | 1b27c8f | 2004-01-13 22:35:58 +1100 | [diff] [blame] | 125 | typedef pid_t sp_pthread_t; |
| 126 | #endif |
| 127 | |
| 128 | struct pam_ctxt { |
| 129 | sp_pthread_t pam_thread; |
| 130 | int pam_psock; |
| 131 | int pam_csock; |
| 132 | int pam_done; |
| 133 | }; |
| 134 | |
| 135 | static void sshpam_free_ctx(void *); |
| 136 | static struct pam_ctxt *cleanup_ctxt; |
| 137 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 138 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 139 | /* |
| 140 | * Simulate threads with processes. |
| 141 | */ |
Kevin Steves | 63007d4 | 2002-07-21 17:57:01 +0000 | [diff] [blame] | 142 | |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 143 | static int sshpam_thread_status = -1; |
| 144 | static mysig_t sshpam_oldsig; |
| 145 | |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 146 | static void |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 147 | sshpam_sigchld_handler(int sig) |
| 148 | { |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 149 | signal(SIGCHLD, SIG_DFL); |
Darren Tucker | 7ae0962 | 2004-01-14 23:07:56 +1100 | [diff] [blame] | 150 | if (cleanup_ctxt == NULL) |
| 151 | return; /* handler called after PAM cleanup, shouldn't happen */ |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 152 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG) |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 153 | <= 0) { |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 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) |
Darren Tucker | 5d423f4 | 2004-07-11 16:54:08 +1000 | [diff] [blame] | 157 | <= 0) |
Darren Tucker | b53355e | 2004-05-24 11:55:36 +1000 | [diff] [blame] | 158 | return; /* could not wait */ |
| 159 | } |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 160 | if (WIFSIGNALED(sshpam_thread_status) && |
| 161 | WTERMSIG(sshpam_thread_status) == SIGTERM) |
| 162 | return; /* terminated by pthread_cancel */ |
| 163 | if (!WIFEXITED(sshpam_thread_status)) |
Darren Tucker | 57d4ca9 | 2007-08-10 14:32:34 +1000 | [diff] [blame] | 164 | sigdie("PAM: authentication thread exited unexpectedly"); |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 165 | if (WEXITSTATUS(sshpam_thread_status) != 0) |
Darren Tucker | 57d4ca9 | 2007-08-10 14:32:34 +1000 | [diff] [blame] | 166 | sigdie("PAM: authentication thread exited uncleanly"); |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 167 | } |
| 168 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 169 | /* ARGSUSED */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 170 | static void |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 171 | pthread_exit(void *value) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 172 | { |
| 173 | _exit(0); |
| 174 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 175 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 176 | /* ARGSUSED */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 177 | static int |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 178 | pthread_create(sp_pthread_t *thread, const void *attr, |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 179 | void *(*thread_start)(void *), void *arg) |
| 180 | { |
| 181 | pid_t pid; |
Darren Tucker | 4f1adad | 2005-07-16 11:33:06 +1000 | [diff] [blame] | 182 | struct pam_ctxt *ctx = arg; |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 183 | |
Darren Tucker | b9b6021 | 2004-03-04 20:03:54 +1100 | [diff] [blame] | 184 | sshpam_thread_status = -1; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 185 | switch ((pid = fork())) { |
| 186 | case -1: |
| 187 | error("fork(): %s", strerror(errno)); |
| 188 | return (-1); |
| 189 | case 0: |
Darren Tucker | 4f1adad | 2005-07-16 11:33:06 +1000 | [diff] [blame] | 190 | close(ctx->pam_psock); |
| 191 | ctx->pam_psock = -1; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 192 | thread_start(arg); |
| 193 | _exit(1); |
| 194 | default: |
| 195 | *thread = pid; |
Darren Tucker | 4f1adad | 2005-07-16 11:33:06 +1000 | [diff] [blame] | 196 | close(ctx->pam_csock); |
| 197 | ctx->pam_csock = -1; |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 198 | sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 199 | return (0); |
| 200 | } |
| 201 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 202 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 203 | static int |
| 204 | pthread_cancel(sp_pthread_t thread) |
| 205 | { |
Darren Tucker | 7ae0962 | 2004-01-14 23:07:56 +1100 | [diff] [blame] | 206 | signal(SIGCHLD, sshpam_oldsig); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 207 | return (kill(thread, SIGTERM)); |
| 208 | } |
| 209 | |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 210 | /* ARGSUSED */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 211 | static int |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 212 | pthread_join(sp_pthread_t thread, void **value) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 213 | { |
| 214 | int status; |
| 215 | |
Darren Tucker | 749bc95 | 2004-01-14 22:14:04 +1100 | [diff] [blame] | 216 | if (sshpam_thread_status != -1) |
| 217 | return (sshpam_thread_status); |
| 218 | signal(SIGCHLD, sshpam_oldsig); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 219 | waitpid(thread, &status, 0); |
| 220 | return (status); |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 225 | static pam_handle_t *sshpam_handle = NULL; |
| 226 | static int sshpam_err = 0; |
| 227 | static int sshpam_authenticated = 0; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 228 | static int sshpam_session_open = 0; |
| 229 | static int sshpam_cred_established = 0; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 230 | static int sshpam_account_status = -1; |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 231 | static char **sshpam_env = NULL; |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 232 | static Authctxt *sshpam_authctxt = NULL; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 233 | static const char *sshpam_password = NULL; |
Darren Tucker | 36a3d60 | 2005-01-20 12:43:38 +1100 | [diff] [blame] | 234 | static char badpw[] = "\b\n\r\177INCORRECT"; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 235 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 236 | /* Some PAM implementations don't implement this */ |
| 237 | #ifndef HAVE_PAM_GETENVLIST |
| 238 | static char ** |
| 239 | pam_getenvlist(pam_handle_t *pamh) |
| 240 | { |
| 241 | /* |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 242 | * XXX - If necessary, we can still support envrionment passing |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 243 | * for platforms without pam_getenvlist by searching for known |
| 244 | * env vars (e.g. KRB5CCNAME) from the PAM environment. |
| 245 | */ |
| 246 | return NULL; |
| 247 | } |
| 248 | #endif |
| 249 | |
Darren Tucker | 21dd089 | 2004-08-16 23:12:05 +1000 | [diff] [blame] | 250 | /* |
| 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 |
| 255 | * the right thing. |
| 256 | */ |
| 257 | #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID |
| 258 | static int |
| 259 | sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags) |
| 260 | { |
| 261 | int result; |
| 262 | |
| 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)); |
| 270 | return result; |
| 271 | } |
| 272 | # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b))) |
| 273 | #endif |
| 274 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 275 | void |
Darren Tucker | 17db1c4 | 2004-06-19 12:54:38 +1000 | [diff] [blame] | 276 | sshpam_password_change_required(int reqd) |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 277 | { |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 278 | debug3("%s %d", __func__, reqd); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 279 | if (sshpam_authctxt == NULL) |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 280 | fatal("%s: PAM authctxt not initialized", __func__); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 281 | sshpam_authctxt->force_pwchange = reqd; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 282 | if (reqd) { |
| 283 | no_port_forwarding_flag |= 2; |
| 284 | no_agent_forwarding_flag |= 2; |
| 285 | no_x11_forwarding_flag |= 2; |
| 286 | } else { |
| 287 | no_port_forwarding_flag &= ~2; |
| 288 | no_agent_forwarding_flag &= ~2; |
| 289 | no_x11_forwarding_flag &= ~2; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 290 | } |
| 291 | } |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 292 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 293 | /* Import regular and PAM environment from subprocess */ |
| 294 | static void |
| 295 | import_environments(Buffer *b) |
| 296 | { |
| 297 | char *env; |
| 298 | u_int i, num_env; |
| 299 | int err; |
| 300 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 301 | debug3("PAM: %s entering", __func__); |
| 302 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 303 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 304 | /* Import variables set by do_pam_account */ |
| 305 | sshpam_account_status = buffer_get_int(b); |
Darren Tucker | 17db1c4 | 2004-06-19 12:54:38 +1000 | [diff] [blame] | 306 | sshpam_password_change_required(buffer_get_int(b)); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 307 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 308 | /* Import environment from subprocess */ |
| 309 | num_env = buffer_get_int(b); |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 310 | if (num_env > 1024) |
| 311 | fatal("%s: received %u environment variables, expected <= 1024", |
| 312 | __func__, num_env); |
| 313 | sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env)); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 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); |
| 317 | |
| 318 | sshpam_env[num_env] = NULL; |
| 319 | |
| 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); |
| 325 | |
Darren Tucker | 8a1624c | 2003-11-18 12:45:35 +1100 | [diff] [blame] | 326 | #ifdef HAVE_PAM_PUTENV |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 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)); |
| 331 | } |
Darren Tucker | 8a1624c | 2003-11-18 12:45:35 +1100 | [diff] [blame] | 332 | #endif |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 333 | } |
Darren Tucker | 4b385d4 | 2004-03-04 19:54:10 +1100 | [diff] [blame] | 334 | #endif |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 335 | } |
| 336 | |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 337 | /* |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 338 | * Conversation function for authentication thread. |
Damien Miller | 9d5705a | 2000-09-16 16:09:27 +1100 | [diff] [blame] | 339 | */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 340 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 341 | sshpam_thread_conv(int n, sshpam_const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 342 | struct pam_response **resp, void *data) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 343 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 344 | Buffer buffer; |
| 345 | struct pam_ctxt *ctxt; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 346 | struct pam_response *reply; |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 347 | int i; |
| 348 | |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 349 | debug3("PAM: %s entering, %d messages", __func__, n); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 350 | *resp = NULL; |
| 351 | |
Darren Tucker | 59e0602 | 2004-06-30 20:34:31 +1000 | [diff] [blame] | 352 | if (data == NULL) { |
| 353 | error("PAM: conversation function passed a null context"); |
| 354 | return (PAM_CONV_ERR); |
| 355 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 356 | ctxt = data; |
| 357 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 358 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 359 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 360 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 361 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 362 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 363 | buffer_init(&buffer); |
| 364 | for (i = 0; i < n; ++i) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 365 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 366 | case PAM_PROMPT_ECHO_OFF: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 367 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 368 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 369 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 370 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 371 | goto fail; |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 372 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 373 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 374 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
| 375 | goto fail; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 376 | reply[i].resp = buffer_get_string(&buffer, NULL); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 377 | break; |
| 378 | case PAM_PROMPT_ECHO_ON: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 379 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 380 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 381 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 382 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 383 | goto fail; |
| 384 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) |
| 385 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 386 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
| 387 | goto fail; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 388 | reply[i].resp = buffer_get_string(&buffer, NULL); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 389 | break; |
| 390 | case PAM_ERROR_MSG: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 391 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 392 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 393 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 394 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 395 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 396 | break; |
| 397 | case PAM_TEXT_INFO: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 398 | buffer_put_cstring(&buffer, |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 399 | PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 400 | if (ssh_msg_send(ctxt->pam_csock, |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 401 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
| 402 | goto fail; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 403 | break; |
| 404 | default: |
| 405 | goto fail; |
| 406 | } |
| 407 | buffer_clear(&buffer); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 408 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 409 | buffer_free(&buffer); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 410 | *resp = reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 411 | return (PAM_SUCCESS); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 412 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 413 | fail: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 414 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 415 | free(reply[i].resp); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 416 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 417 | free(reply); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 418 | buffer_free(&buffer); |
| 419 | return (PAM_CONV_ERR); |
Kevin Steves | 38b050a | 2002-07-23 00:44:07 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 422 | /* |
| 423 | * Authentication thread. |
| 424 | */ |
| 425 | static void * |
| 426 | sshpam_thread(void *ctxtp) |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 427 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 428 | struct pam_ctxt *ctxt = ctxtp; |
| 429 | Buffer buffer; |
Damien Miller | f4b6f10 | 2003-09-02 23:12:06 +1000 | [diff] [blame] | 430 | struct pam_conv sshpam_conv; |
Darren Tucker | 1f7e408 | 2004-07-01 14:00:14 +1000 | [diff] [blame] | 431 | int flags = (options.permit_empty_passwd == 0 ? |
| 432 | PAM_DISALLOW_NULL_AUTHTOK : 0); |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 433 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 434 | extern char **environ; |
| 435 | char **env_from_pam; |
| 436 | u_int i; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 437 | const char *pam_user; |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 438 | const char **ptr_pam_user = &pam_user; |
Darren Tucker | 54e1b22 | 2006-09-17 11:57:46 +1000 | [diff] [blame] | 439 | char *tz = getenv("TZ"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 440 | |
Darren Tucker | 53f8e78 | 2013-12-19 11:31:44 +1100 | [diff] [blame] | 441 | sshpam_err = pam_get_item(sshpam_handle, PAM_USER, |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 442 | (sshpam_const void **)ptr_pam_user); |
Darren Tucker | 53f8e78 | 2013-12-19 11:31:44 +1100 | [diff] [blame] | 443 | if (sshpam_err != PAM_SUCCESS) |
| 444 | goto auth_fail; |
Darren Tucker | 54e1b22 | 2006-09-17 11:57:46 +1000 | [diff] [blame] | 445 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 446 | environ[0] = NULL; |
Darren Tucker | 54e1b22 | 2006-09-17 11:57:46 +1000 | [diff] [blame] | 447 | if (tz != NULL) |
| 448 | if (setenv("TZ", tz, 1) == -1) |
| 449 | error("PAM: could not set TZ environment: %s", |
| 450 | strerror(errno)); |
Damien Miller | 2d2ed3d | 2004-07-21 20:54:47 +1000 | [diff] [blame] | 451 | |
| 452 | if (sshpam_authctxt != NULL) { |
| 453 | setproctitle("%s [pam]", |
| 454 | sshpam_authctxt->valid ? pam_user : "unknown"); |
| 455 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 456 | #endif |
| 457 | |
Damien Miller | f4b6f10 | 2003-09-02 23:12:06 +1000 | [diff] [blame] | 458 | sshpam_conv.conv = sshpam_thread_conv; |
| 459 | sshpam_conv.appdata_ptr = ctxt; |
| 460 | |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 461 | if (sshpam_authctxt == NULL) |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 462 | fatal("%s: PAM authctxt not initialized", __func__); |
| 463 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 464 | buffer_init(&buffer); |
| 465 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 466 | (const void *)&sshpam_conv); |
| 467 | if (sshpam_err != PAM_SUCCESS) |
| 468 | goto auth_fail; |
Darren Tucker | 1f7e408 | 2004-07-01 14:00:14 +1000 | [diff] [blame] | 469 | sshpam_err = pam_authenticate(sshpam_handle, flags); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 470 | if (sshpam_err != PAM_SUCCESS) |
| 471 | goto auth_fail; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 472 | |
Darren Tucker | c376c86 | 2003-12-18 16:08:59 +1100 | [diff] [blame] | 473 | if (compat20) { |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 474 | if (!do_pam_account()) { |
| 475 | sshpam_err = PAM_ACCT_EXPIRED; |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 476 | goto auth_fail; |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 477 | } |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 478 | if (sshpam_authctxt->force_pwchange) { |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 479 | sshpam_err = pam_chauthtok(sshpam_handle, |
| 480 | PAM_CHANGE_EXPIRED_AUTHTOK); |
| 481 | if (sshpam_err != PAM_SUCCESS) |
| 482 | goto auth_fail; |
Darren Tucker | 17db1c4 | 2004-06-19 12:54:38 +1000 | [diff] [blame] | 483 | sshpam_password_change_required(0); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 484 | } |
Darren Tucker | c376c86 | 2003-12-18 16:08:59 +1100 | [diff] [blame] | 485 | } |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 486 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 487 | buffer_put_cstring(&buffer, "OK"); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 488 | |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 489 | #ifndef UNSUPPORTED_POSIX_THREADS_HACK |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 490 | /* Export variables set by do_pam_account */ |
| 491 | buffer_put_int(&buffer, sshpam_account_status); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 492 | buffer_put_int(&buffer, sshpam_authctxt->force_pwchange); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 493 | |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 494 | /* Export any environment strings set in child */ |
| 495 | for(i = 0; environ[i] != NULL; i++) |
| 496 | ; /* Count */ |
| 497 | buffer_put_int(&buffer, i); |
| 498 | for(i = 0; environ[i] != NULL; i++) |
| 499 | buffer_put_cstring(&buffer, environ[i]); |
| 500 | |
| 501 | /* Export any environment strings set by PAM in child */ |
| 502 | env_from_pam = pam_getenvlist(sshpam_handle); |
| 503 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) |
| 504 | ; /* Count */ |
| 505 | buffer_put_int(&buffer, i); |
| 506 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) |
| 507 | buffer_put_cstring(&buffer, env_from_pam[i]); |
Darren Tucker | 328118a | 2005-05-25 16:18:09 +1000 | [diff] [blame] | 508 | #endif /* UNSUPPORTED_POSIX_THREADS_HACK */ |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 509 | |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 510 | /* XXX - can't do much about an error here */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 511 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); |
| 512 | buffer_free(&buffer); |
| 513 | pthread_exit(NULL); |
| 514 | |
| 515 | auth_fail: |
| 516 | buffer_put_cstring(&buffer, |
| 517 | pam_strerror(sshpam_handle, sshpam_err)); |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 518 | /* XXX - can't do much about an error here */ |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 519 | if (sshpam_err == PAM_ACCT_EXPIRED) |
| 520 | ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer); |
| 521 | else |
| 522 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 523 | buffer_free(&buffer); |
| 524 | pthread_exit(NULL); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 525 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 526 | return (NULL); /* Avoid warning for non-pthread case */ |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 527 | } |
| 528 | |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 529 | void |
| 530 | sshpam_thread_cleanup(void) |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 531 | { |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 532 | struct pam_ctxt *ctxt = cleanup_ctxt; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 533 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 534 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 535 | if (ctxt != NULL && ctxt->pam_thread != 0) { |
| 536 | pthread_cancel(ctxt->pam_thread); |
| 537 | pthread_join(ctxt->pam_thread, NULL); |
| 538 | close(ctxt->pam_psock); |
| 539 | close(ctxt->pam_csock); |
| 540 | memset(ctxt, 0, sizeof(*ctxt)); |
| 541 | cleanup_ctxt = NULL; |
| 542 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 543 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 544 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 545 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 546 | sshpam_null_conv(int n, sshpam_const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 547 | struct pam_response **resp, void *data) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 548 | { |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 549 | debug3("PAM: %s entering, %d messages", __func__, n); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 550 | return (PAM_CONV_ERR); |
| 551 | } |
Damien Miller | 63dc3e9 | 2001-02-07 12:58:33 +1100 | [diff] [blame] | 552 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 553 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
| 554 | |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 555 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 556 | sshpam_store_conv(int n, sshpam_const struct pam_message **msg, |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 557 | struct pam_response **resp, void *data) |
| 558 | { |
| 559 | struct pam_response *reply; |
| 560 | int i; |
| 561 | size_t len; |
| 562 | |
| 563 | debug3("PAM: %s called with %d messages", __func__, n); |
| 564 | *resp = NULL; |
| 565 | |
| 566 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 567 | return (PAM_CONV_ERR); |
| 568 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 569 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 570 | return (PAM_CONV_ERR); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 571 | |
| 572 | for (i = 0; i < n; ++i) { |
| 573 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 574 | case PAM_ERROR_MSG: |
| 575 | case PAM_TEXT_INFO: |
| 576 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); |
| 577 | buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len); |
| 578 | buffer_append(&loginmsg, "\n", 1 ); |
| 579 | reply[i].resp_retcode = PAM_SUCCESS; |
| 580 | break; |
| 581 | default: |
| 582 | goto fail; |
| 583 | } |
| 584 | } |
| 585 | *resp = reply; |
| 586 | return (PAM_SUCCESS); |
| 587 | |
| 588 | fail: |
| 589 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 590 | free(reply[i].resp); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 591 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 592 | free(reply); |
Darren Tucker | 0a7e3c6 | 2004-09-11 22:28:01 +1000 | [diff] [blame] | 593 | return (PAM_CONV_ERR); |
| 594 | } |
| 595 | |
| 596 | static struct pam_conv store_conv = { sshpam_store_conv, NULL }; |
| 597 | |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 598 | void |
| 599 | sshpam_cleanup(void) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 600 | { |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 601 | if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor())) |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 602 | return; |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 603 | debug("PAM: cleanup"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 604 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 605 | if (sshpam_session_open) { |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 606 | debug("PAM: closing session"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 607 | pam_close_session(sshpam_handle, PAM_SILENT); |
| 608 | sshpam_session_open = 0; |
| 609 | } |
Darren Tucker | 622d5c5 | 2009-07-12 22:07:21 +1000 | [diff] [blame] | 610 | if (sshpam_cred_established) { |
| 611 | debug("PAM: deleting credentials"); |
| 612 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); |
| 613 | sshpam_cred_established = 0; |
| 614 | } |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 615 | sshpam_authenticated = 0; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 616 | pam_end(sshpam_handle, sshpam_err); |
| 617 | sshpam_handle = NULL; |
| 618 | } |
| 619 | |
| 620 | static int |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 621 | sshpam_init(Authctxt *authctxt) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 622 | { |
Darren Tucker | 455813b | 2003-09-13 22:12:11 +1000 | [diff] [blame] | 623 | extern char *__progname; |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 624 | const char *pam_rhost, *pam_user, *user = authctxt->user; |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 625 | const char **ptr_pam_user = &pam_user; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 626 | |
| 627 | if (sshpam_handle != NULL) { |
| 628 | /* We already have a PAM context; check if the user matches */ |
| 629 | sshpam_err = pam_get_item(sshpam_handle, |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 630 | PAM_USER, (sshpam_const void **)ptr_pam_user); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 631 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) |
| 632 | return (0); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 633 | pam_end(sshpam_handle, sshpam_err); |
| 634 | sshpam_handle = NULL; |
| 635 | } |
| 636 | debug("PAM: initializing for \"%s\"", user); |
Darren Tucker | c58c2ee | 2003-09-13 22:02:05 +1000 | [diff] [blame] | 637 | sshpam_err = |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 638 | pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle); |
Darren Tucker | 17addf0 | 2004-03-30 20:57:57 +1000 | [diff] [blame] | 639 | sshpam_authctxt = authctxt; |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 640 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 641 | if (sshpam_err != PAM_SUCCESS) { |
| 642 | pam_end(sshpam_handle, sshpam_err); |
| 643 | sshpam_handle = NULL; |
| 644 | return (-1); |
| 645 | } |
Damien Miller | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 646 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); |
Damien Miller | 4633720 | 2003-06-02 11:04:39 +1000 | [diff] [blame] | 647 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 648 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
| 649 | if (sshpam_err != PAM_SUCCESS) { |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 650 | pam_end(sshpam_handle, sshpam_err); |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 651 | sshpam_handle = NULL; |
| 652 | return (-1); |
| 653 | } |
| 654 | #ifdef PAM_TTY_KLUDGE |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 655 | /* |
| 656 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. |
| 657 | * sshd doesn't set the tty until too late in the auth process and |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 658 | * may not even set one (for tty-less connections) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 659 | */ |
Damien Miller | 25d9342 | 2003-05-18 20:45:47 +1000 | [diff] [blame] | 660 | debug("PAM: setting PAM_TTY to \"ssh\""); |
| 661 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); |
| 662 | if (sshpam_err != PAM_SUCCESS) { |
| 663 | pam_end(sshpam_handle, sshpam_err); |
| 664 | sshpam_handle = NULL; |
| 665 | return (-1); |
| 666 | } |
| 667 | #endif |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 668 | return (0); |
| 669 | } |
| 670 | |
| 671 | static void * |
| 672 | sshpam_init_ctx(Authctxt *authctxt) |
| 673 | { |
| 674 | struct pam_ctxt *ctxt; |
| 675 | int socks[2]; |
| 676 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 677 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 678 | /* |
| 679 | * Refuse to start if we don't have PAM enabled or do_pam_account |
| 680 | * has previously failed. |
| 681 | */ |
| 682 | if (!options.use_pam || sshpam_account_status == 0) |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 683 | return NULL; |
| 684 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 685 | /* Initialize PAM */ |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 686 | if (sshpam_init(authctxt) == -1) { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 687 | error("PAM: initialization failed"); |
| 688 | return (NULL); |
| 689 | } |
| 690 | |
Darren Tucker | 29171e9 | 2007-05-20 15:20:08 +1000 | [diff] [blame] | 691 | ctxt = xcalloc(1, sizeof *ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 692 | |
| 693 | /* Start the authentication thread */ |
| 694 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { |
| 695 | error("PAM: failed create sockets: %s", strerror(errno)); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 696 | free(ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 697 | return (NULL); |
| 698 | } |
| 699 | ctxt->pam_psock = socks[0]; |
| 700 | ctxt->pam_csock = socks[1]; |
| 701 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { |
| 702 | error("PAM: failed to start authentication thread: %s", |
| 703 | strerror(errno)); |
| 704 | close(socks[0]); |
| 705 | close(socks[1]); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 706 | free(ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 707 | return (NULL); |
| 708 | } |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 709 | cleanup_ctxt = ctxt; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 710 | return (ctxt); |
| 711 | } |
| 712 | |
| 713 | static int |
| 714 | sshpam_query(void *ctx, char **name, char **info, |
| 715 | u_int *num, char ***prompts, u_int **echo_on) |
| 716 | { |
| 717 | Buffer buffer; |
| 718 | struct pam_ctxt *ctxt = ctx; |
| 719 | size_t plen; |
| 720 | u_char type; |
| 721 | char *msg; |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 722 | size_t len, mlen; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 723 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 724 | debug3("PAM: %s entering", __func__); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 725 | buffer_init(&buffer); |
| 726 | *name = xstrdup(""); |
| 727 | *info = xstrdup(""); |
| 728 | *prompts = xmalloc(sizeof(char *)); |
| 729 | **prompts = NULL; |
| 730 | plen = 0; |
| 731 | *echo_on = xmalloc(sizeof(u_int)); |
| 732 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { |
| 733 | type = buffer_get_char(&buffer); |
| 734 | msg = buffer_get_string(&buffer, NULL); |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 735 | mlen = strlen(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 736 | switch (type) { |
| 737 | case PAM_PROMPT_ECHO_ON: |
| 738 | case PAM_PROMPT_ECHO_OFF: |
| 739 | *num = 1; |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 740 | len = plen + mlen + 1; |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 741 | **prompts = xrealloc(**prompts, 1, len); |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 742 | strlcpy(**prompts + plen, msg, len - plen); |
| 743 | plen += mlen; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 744 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 745 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 746 | return (0); |
| 747 | case PAM_ERROR_MSG: |
| 748 | case PAM_TEXT_INFO: |
| 749 | /* accumulate messages */ |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 750 | len = plen + mlen + 2; |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 751 | **prompts = xrealloc(**prompts, 1, len); |
Damien Miller | daffc6a | 2004-10-16 18:52:44 +1000 | [diff] [blame] | 752 | strlcpy(**prompts + plen, msg, len - plen); |
| 753 | plen += mlen; |
| 754 | strlcat(**prompts + plen, "\n", len - plen); |
| 755 | plen++; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 756 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 757 | break; |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 758 | case PAM_ACCT_EXPIRED: |
| 759 | sshpam_account_status = 0; |
| 760 | /* FALLTHROUGH */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 761 | case PAM_AUTH_ERR: |
Darren Tucker | 2c77b7f | 2006-05-15 17:22:33 +1000 | [diff] [blame] | 762 | debug3("PAM: %s", pam_strerror(sshpam_handle, type)); |
Darren Tucker | 7b1e695 | 2005-09-28 22:33:27 +1000 | [diff] [blame] | 763 | if (**prompts != NULL && strlen(**prompts) != 0) { |
| 764 | *info = **prompts; |
| 765 | **prompts = NULL; |
| 766 | *num = 0; |
| 767 | **echo_on = 0; |
| 768 | ctxt->pam_done = -1; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 769 | free(msg); |
Darren Tucker | 7b1e695 | 2005-09-28 22:33:27 +1000 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | /* FALLTHROUGH */ |
| 773 | case PAM_SUCCESS: |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 774 | if (**prompts != NULL) { |
| 775 | /* drain any accumulated messages */ |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 776 | debug("PAM: %s", **prompts); |
| 777 | buffer_append(&loginmsg, **prompts, |
| 778 | strlen(**prompts)); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 779 | free(**prompts); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 780 | **prompts = NULL; |
| 781 | } |
| 782 | if (type == PAM_SUCCESS) { |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 783 | if (!sshpam_authctxt->valid || |
| 784 | (sshpam_authctxt->pw->pw_uid == 0 && |
| 785 | options.permit_root_login != PERMIT_YES)) |
Darren Tucker | 36a3d60 | 2005-01-20 12:43:38 +1100 | [diff] [blame] | 786 | fatal("Internal error: PAM auth " |
| 787 | "succeeded when it should have " |
| 788 | "failed"); |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 789 | import_environments(&buffer); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 790 | *num = 0; |
| 791 | **echo_on = 0; |
| 792 | ctxt->pam_done = 1; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 793 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 794 | return (0); |
| 795 | } |
Darren Tucker | 2a9bf4b | 2004-04-18 11:00:26 +1000 | [diff] [blame] | 796 | error("PAM: %s for %s%.100s from %.100s", msg, |
| 797 | sshpam_authctxt->valid ? "" : "illegal user ", |
| 798 | sshpam_authctxt->user, |
| 799 | get_remote_name_or_ip(utmp_len, options.use_dns)); |
Darren Tucker | 439ce0d | 2003-10-09 14:20:15 +1000 | [diff] [blame] | 800 | /* FALLTHROUGH */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 801 | default: |
| 802 | *num = 0; |
| 803 | **echo_on = 0; |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 804 | free(msg); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 805 | ctxt->pam_done = -1; |
| 806 | return (-1); |
| 807 | } |
| 808 | } |
| 809 | return (-1); |
| 810 | } |
| 811 | |
| 812 | /* XXX - see also comment in auth-chall.c:verify_response */ |
| 813 | static int |
| 814 | sshpam_respond(void *ctx, u_int num, char **resp) |
| 815 | { |
| 816 | Buffer buffer; |
| 817 | struct pam_ctxt *ctxt = ctx; |
| 818 | |
Darren Tucker | 1d4ebbf | 2006-01-29 16:46:13 +1100 | [diff] [blame] | 819 | debug2("PAM: %s entering, %u responses", __func__, num); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 820 | switch (ctxt->pam_done) { |
| 821 | case 1: |
| 822 | sshpam_authenticated = 1; |
| 823 | return (0); |
| 824 | case 0: |
| 825 | break; |
| 826 | default: |
| 827 | return (-1); |
| 828 | } |
| 829 | if (num != 1) { |
| 830 | error("PAM: expected one response, got %u", num); |
| 831 | return (-1); |
| 832 | } |
| 833 | buffer_init(&buffer); |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 834 | if (sshpam_authctxt->valid && |
| 835 | (sshpam_authctxt->pw->pw_uid != 0 || |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 836 | options.permit_root_login == PERMIT_YES)) |
Darren Tucker | 36a3d60 | 2005-01-20 12:43:38 +1100 | [diff] [blame] | 837 | buffer_put_cstring(&buffer, *resp); |
| 838 | else |
| 839 | buffer_put_cstring(&buffer, badpw); |
Damien Miller | 9bdba70 | 2003-11-17 21:27:55 +1100 | [diff] [blame] | 840 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) { |
| 841 | buffer_free(&buffer); |
| 842 | return (-1); |
| 843 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 844 | buffer_free(&buffer); |
| 845 | return (1); |
| 846 | } |
| 847 | |
| 848 | static void |
| 849 | sshpam_free_ctx(void *ctxtp) |
| 850 | { |
| 851 | struct pam_ctxt *ctxt = ctxtp; |
| 852 | |
Darren Tucker | a8df924 | 2004-01-15 00:15:07 +1100 | [diff] [blame] | 853 | debug3("PAM: %s entering", __func__); |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 854 | sshpam_thread_cleanup(); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 855 | free(ctxt); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 856 | /* |
| 857 | * We don't call sshpam_cleanup() here because we may need the PAM |
| 858 | * handle at a later stage, e.g. when setting up a session. It's |
| 859 | * still on the cleanup list, so pam_end() *will* be called before |
| 860 | * the server process terminates. |
| 861 | */ |
| 862 | } |
| 863 | |
| 864 | KbdintDevice sshpam_device = { |
| 865 | "pam", |
| 866 | sshpam_init_ctx, |
| 867 | sshpam_query, |
| 868 | sshpam_respond, |
| 869 | sshpam_free_ctx |
| 870 | }; |
| 871 | |
| 872 | KbdintDevice mm_sshpam_device = { |
| 873 | "pam", |
| 874 | mm_sshpam_init_ctx, |
| 875 | mm_sshpam_query, |
| 876 | mm_sshpam_respond, |
| 877 | mm_sshpam_free_ctx |
| 878 | }; |
| 879 | |
| 880 | /* |
| 881 | * This replaces auth-pam.c |
| 882 | */ |
| 883 | void |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 884 | start_pam(Authctxt *authctxt) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 885 | { |
Damien Miller | 9d507da | 2003-05-14 15:31:12 +1000 | [diff] [blame] | 886 | if (!options.use_pam) |
| 887 | fatal("PAM: initialisation requested when UsePAM=no"); |
| 888 | |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 889 | if (sshpam_init(authctxt) == -1) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 890 | fatal("PAM: initialisation failed"); |
| 891 | } |
| 892 | |
| 893 | void |
| 894 | finish_pam(void) |
| 895 | { |
Darren Tucker | 8846a07 | 2003-10-07 11:30:15 +1000 | [diff] [blame] | 896 | sshpam_cleanup(); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 897 | } |
| 898 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 899 | u_int |
| 900 | do_pam_account(void) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 901 | { |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 902 | debug("%s: called", __func__); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 903 | if (sshpam_account_status != -1) |
| 904 | return (sshpam_account_status); |
| 905 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 906 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 907 | debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err, |
| 908 | pam_strerror(sshpam_handle, sshpam_err)); |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 909 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 910 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { |
| 911 | sshpam_account_status = 0; |
| 912 | return (sshpam_account_status); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 913 | } |
| 914 | |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 915 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) |
Darren Tucker | 17db1c4 | 2004-06-19 12:54:38 +1000 | [diff] [blame] | 916 | sshpam_password_change_required(1); |
Darren Tucker | 07705c7 | 2003-12-18 15:34:31 +1100 | [diff] [blame] | 917 | |
| 918 | sshpam_account_status = 1; |
| 919 | return (sshpam_account_status); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | void |
Damien Miller | 341c6e6 | 2003-09-02 23:18:52 +1000 | [diff] [blame] | 923 | do_pam_set_tty(const char *tty) |
| 924 | { |
Darren Tucker | f38db7f | 2003-08-08 13:43:37 +1000 | [diff] [blame] | 925 | if (tty != NULL) { |
| 926 | debug("PAM: setting PAM_TTY to \"%s\"", tty); |
| 927 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); |
| 928 | if (sshpam_err != PAM_SUCCESS) |
| 929 | fatal("PAM: failed to set PAM_TTY: %s", |
| 930 | pam_strerror(sshpam_handle, sshpam_err)); |
| 931 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void |
| 935 | do_pam_setcred(int init) |
| 936 | { |
| 937 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 938 | (const void *)&store_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 939 | if (sshpam_err != PAM_SUCCESS) |
| 940 | fatal("PAM: failed to set PAM_CONV: %s", |
| 941 | pam_strerror(sshpam_handle, sshpam_err)); |
| 942 | if (init) { |
| 943 | debug("PAM: establishing credentials"); |
| 944 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); |
| 945 | } else { |
| 946 | debug("PAM: reinitializing credentials"); |
| 947 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); |
| 948 | } |
| 949 | if (sshpam_err == PAM_SUCCESS) { |
| 950 | sshpam_cred_established = 1; |
| 951 | return; |
| 952 | } |
| 953 | if (sshpam_authenticated) |
| 954 | fatal("PAM: pam_setcred(): %s", |
| 955 | pam_strerror(sshpam_handle, sshpam_err)); |
| 956 | else |
| 957 | debug("PAM: pam_setcred(): %s", |
| 958 | pam_strerror(sshpam_handle, sshpam_err)); |
| 959 | } |
| 960 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 961 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 962 | sshpam_tty_conv(int n, sshpam_const struct pam_message **msg, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 963 | struct pam_response **resp, void *data) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 964 | { |
| 965 | char input[PAM_MAX_MSG_SIZE]; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 966 | struct pam_response *reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 967 | int i; |
| 968 | |
Darren Tucker | ba53b83 | 2004-02-17 20:46:59 +1100 | [diff] [blame] | 969 | debug3("PAM: %s called with %d messages", __func__, n); |
| 970 | |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 971 | *resp = NULL; |
| 972 | |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 973 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 974 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 975 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 976 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 977 | return (PAM_CONV_ERR); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 978 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 979 | for (i = 0; i < n; ++i) { |
| 980 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 981 | case PAM_PROMPT_ECHO_OFF: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 982 | reply[i].resp = |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 983 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 984 | RP_ALLOW_STDIN); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 985 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 986 | break; |
| 987 | case PAM_PROMPT_ECHO_ON: |
Darren Tucker | 0947ddf | 2003-11-13 11:21:31 +1100 | [diff] [blame] | 988 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
Darren Tucker | 2216471 | 2007-05-20 15:26:07 +1000 | [diff] [blame] | 989 | if (fgets(input, sizeof input, stdin) == NULL) |
| 990 | input[0] = '\0'; |
Damien Miller | a6fb77f | 2004-07-19 09:39:11 +1000 | [diff] [blame] | 991 | if ((reply[i].resp = strdup(input)) == NULL) |
| 992 | goto fail; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 993 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 994 | break; |
| 995 | case PAM_ERROR_MSG: |
| 996 | case PAM_TEXT_INFO: |
Darren Tucker | 0947ddf | 2003-11-13 11:21:31 +1100 | [diff] [blame] | 997 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 998 | reply[i].resp_retcode = PAM_SUCCESS; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 999 | break; |
| 1000 | default: |
| 1001 | goto fail; |
| 1002 | } |
| 1003 | } |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1004 | *resp = reply; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1005 | return (PAM_SUCCESS); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1006 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1007 | fail: |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1008 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1009 | free(reply[i].resp); |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 1010 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1011 | free(reply); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1012 | return (PAM_CONV_ERR); |
| 1013 | } |
| 1014 | |
Darren Tucker | 94befab | 2004-06-03 14:53:12 +1000 | [diff] [blame] | 1015 | static struct pam_conv tty_conv = { sshpam_tty_conv, NULL }; |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1016 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1017 | /* |
| 1018 | * XXX this should be done in the authentication phase, but ssh1 doesn't |
| 1019 | * support that |
| 1020 | */ |
| 1021 | void |
| 1022 | do_pam_chauthtok(void) |
| 1023 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1024 | if (use_privsep) |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1025 | fatal("Password expired (unable to change with privsep)"); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1026 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1027 | (const void *)&tty_conv); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1028 | if (sshpam_err != PAM_SUCCESS) |
| 1029 | fatal("PAM: failed to set PAM_CONV: %s", |
| 1030 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1031 | debug("PAM: changing password"); |
| 1032 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); |
| 1033 | if (sshpam_err != PAM_SUCCESS) |
| 1034 | fatal("PAM: pam_chauthtok(): %s", |
| 1035 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1036 | } |
| 1037 | |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1038 | void |
| 1039 | do_pam_session(void) |
| 1040 | { |
Darren Tucker | 1921ed9 | 2004-02-10 13:23:28 +1100 | [diff] [blame] | 1041 | debug3("PAM: opening session"); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1042 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
Darren Tucker | 5cf8ef7 | 2004-02-17 23:20:07 +1100 | [diff] [blame] | 1043 | (const void *)&store_conv); |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1044 | if (sshpam_err != PAM_SUCCESS) |
| 1045 | fatal("PAM: failed to set PAM_CONV: %s", |
| 1046 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1047 | sshpam_err = pam_open_session(sshpam_handle, 0); |
Darren Tucker | 69687f4 | 2004-09-11 22:17:26 +1000 | [diff] [blame] | 1048 | if (sshpam_err == PAM_SUCCESS) |
| 1049 | sshpam_session_open = 1; |
| 1050 | else { |
| 1051 | sshpam_session_open = 0; |
| 1052 | disable_forwarding(); |
| 1053 | error("PAM: pam_open_session(): %s", |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1054 | pam_strerror(sshpam_handle, sshpam_err)); |
Darren Tucker | 69687f4 | 2004-09-11 22:17:26 +1000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | } |
| 1058 | |
| 1059 | int |
| 1060 | is_pam_session_open(void) |
| 1061 | { |
| 1062 | return sshpam_session_open; |
Darren Tucker | 18df00c | 2003-11-18 12:42:07 +1100 | [diff] [blame] | 1063 | } |
| 1064 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1065 | /* |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1066 | * Set a PAM environment string. We need to do this so that the session |
| 1067 | * modules can handle things like Kerberos/GSI credentials that appear |
| 1068 | * during the ssh authentication process. |
| 1069 | */ |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1070 | int |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1071 | do_pam_putenv(char *name, char *value) |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1072 | { |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1073 | int ret = 1; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1074 | #ifdef HAVE_PAM_PUTENV |
Damien Miller | f272809 | 2003-09-17 07:24:25 +1000 | [diff] [blame] | 1075 | char *compound; |
| 1076 | size_t len; |
| 1077 | |
| 1078 | len = strlen(name) + strlen(value) + 2; |
| 1079 | compound = xmalloc(len); |
| 1080 | |
| 1081 | snprintf(compound, len, "%s=%s", name, value); |
| 1082 | ret = pam_putenv(sshpam_handle, compound); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1083 | free(compound); |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1084 | #endif |
Damien Miller | f272809 | 2003-09-17 07:24:25 +1000 | [diff] [blame] | 1085 | |
Darren Tucker | 49aaf4a | 2003-08-26 11:58:16 +1000 | [diff] [blame] | 1086 | return (ret); |
| 1087 | } |
| 1088 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1089 | char ** |
Damien Miller | c756e9b | 2003-11-17 21:41:42 +1100 | [diff] [blame] | 1090 | fetch_pam_child_environment(void) |
| 1091 | { |
| 1092 | return sshpam_env; |
| 1093 | } |
| 1094 | |
| 1095 | char ** |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1096 | fetch_pam_environment(void) |
| 1097 | { |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1098 | return (pam_getenvlist(sshpam_handle)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | void |
| 1102 | free_pam_environment(char **env) |
| 1103 | { |
| 1104 | char **envp; |
| 1105 | |
Damien Miller | e27c6cc | 2003-05-16 18:21:01 +1000 | [diff] [blame] | 1106 | if (env == NULL) |
| 1107 | return; |
| 1108 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1109 | for (envp = env; *envp; envp++) |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1110 | free(*envp); |
| 1111 | free(env); |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1112 | } |
| 1113 | |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1114 | /* |
| 1115 | * "Blind" conversation function for password authentication. Assumes that |
| 1116 | * echo-off prompts are for the password and stores messages for later |
| 1117 | * display. |
| 1118 | */ |
| 1119 | static int |
Darren Tucker | f08bdb5 | 2005-05-26 19:59:48 +1000 | [diff] [blame] | 1120 | sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg, |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1121 | struct pam_response **resp, void *data) |
| 1122 | { |
| 1123 | struct pam_response *reply; |
| 1124 | int i; |
| 1125 | size_t len; |
| 1126 | |
| 1127 | debug3("PAM: %s called with %d messages", __func__, n); |
| 1128 | |
| 1129 | *resp = NULL; |
| 1130 | |
| 1131 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
| 1132 | return (PAM_CONV_ERR); |
| 1133 | |
Darren Tucker | 29171e9 | 2007-05-20 15:20:08 +1000 | [diff] [blame] | 1134 | if ((reply = calloc(n, sizeof(*reply))) == NULL) |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1135 | return (PAM_CONV_ERR); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1136 | |
| 1137 | for (i = 0; i < n; ++i) { |
| 1138 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
| 1139 | case PAM_PROMPT_ECHO_OFF: |
| 1140 | if (sshpam_password == NULL) |
| 1141 | goto fail; |
Damien Miller | a6fb77f | 2004-07-19 09:39:11 +1000 | [diff] [blame] | 1142 | if ((reply[i].resp = strdup(sshpam_password)) == NULL) |
| 1143 | goto fail; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1144 | reply[i].resp_retcode = PAM_SUCCESS; |
| 1145 | break; |
| 1146 | case PAM_ERROR_MSG: |
| 1147 | case PAM_TEXT_INFO: |
| 1148 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); |
| 1149 | if (len > 0) { |
| 1150 | buffer_append(&loginmsg, |
| 1151 | PAM_MSG_MEMBER(msg, i, msg), len); |
| 1152 | buffer_append(&loginmsg, "\n", 1); |
| 1153 | } |
Damien Miller | a6fb77f | 2004-07-19 09:39:11 +1000 | [diff] [blame] | 1154 | if ((reply[i].resp = strdup("")) == NULL) |
| 1155 | goto fail; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1156 | reply[i].resp_retcode = PAM_SUCCESS; |
| 1157 | break; |
| 1158 | default: |
| 1159 | goto fail; |
| 1160 | } |
| 1161 | } |
| 1162 | *resp = reply; |
| 1163 | return (PAM_SUCCESS); |
| 1164 | |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 1165 | fail: |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1166 | for(i = 0; i < n; i++) { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1167 | free(reply[i].resp); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1168 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1169 | free(reply); |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1170 | return (PAM_CONV_ERR); |
| 1171 | } |
| 1172 | |
| 1173 | static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL }; |
| 1174 | |
| 1175 | /* |
| 1176 | * Attempt password authentication via PAM |
| 1177 | */ |
| 1178 | int |
| 1179 | sshpam_auth_passwd(Authctxt *authctxt, const char *password) |
| 1180 | { |
| 1181 | int flags = (options.permit_empty_passwd == 0 ? |
| 1182 | PAM_DISALLOW_NULL_AUTHTOK : 0); |
| 1183 | |
| 1184 | if (!options.use_pam || sshpam_handle == NULL) |
| 1185 | fatal("PAM: %s called when PAM disabled or failed to " |
| 1186 | "initialise.", __func__); |
| 1187 | |
| 1188 | sshpam_password = password; |
| 1189 | sshpam_authctxt = authctxt; |
| 1190 | |
Darren Tucker | e061b15 | 2004-05-30 22:04:56 +1000 | [diff] [blame] | 1191 | /* |
| 1192 | * If the user logging in is invalid, or is root but is not permitted |
| 1193 | * by PermitRootLogin, use an invalid password to prevent leaking |
| 1194 | * information via timing (eg if the PAM config has a delay on fail). |
| 1195 | */ |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 1196 | if (!authctxt->valid || (authctxt->pw->pw_uid == 0 && |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 1197 | options.permit_root_login != PERMIT_YES)) |
Darren Tucker | e061b15 | 2004-05-30 22:04:56 +1000 | [diff] [blame] | 1198 | sshpam_password = badpw; |
| 1199 | |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1200 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
| 1201 | (const void *)&passwd_conv); |
| 1202 | if (sshpam_err != PAM_SUCCESS) |
| 1203 | fatal("PAM: %s: failed to set PAM_CONV: %s", __func__, |
| 1204 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1205 | |
| 1206 | sshpam_err = pam_authenticate(sshpam_handle, flags); |
| 1207 | sshpam_password = NULL; |
Darren Tucker | d5bfa8f | 2005-01-20 13:29:51 +1100 | [diff] [blame] | 1208 | if (sshpam_err == PAM_SUCCESS && authctxt->valid) { |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1209 | debug("PAM: password authentication accepted for %.100s", |
| 1210 | authctxt->user); |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 1211 | return 1; |
Darren Tucker | 450a158 | 2004-05-30 20:43:59 +1000 | [diff] [blame] | 1212 | } else { |
| 1213 | debug("PAM: password authentication failed for %.100s: %s", |
| 1214 | authctxt->valid ? authctxt->user : "an illegal user", |
| 1215 | pam_strerror(sshpam_handle, sshpam_err)); |
| 1216 | return 0; |
| 1217 | } |
| 1218 | } |
Damien Miller | e72b7af | 1999-12-30 15:08:44 +1100 | [diff] [blame] | 1219 | #endif /* USE_PAM */ |