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