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