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