]> andersk Git - openssh.git/blame - auth-pam.c
- (dtucker) [openbsd-compat/fake-rfc2553.h] Bug #563: Prepend ssh_ to compat
[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 */
31
5ff453c0 32/* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
a5c9cd31 33#include "includes.h"
5ff453c0 34RCSID("$Id$");
a5c9cd31 35
36#ifdef USE_PAM
2511d104 37#if defined(HAVE_SECURITY_PAM_APPL_H)
05114c74 38#include <security/pam_appl.h>
2511d104 39#elif defined (HAVE_PAM_PAM_APPL_H)
40#include <pam/pam_appl.h>
41#endif
05114c74 42
fde58bd4 43#include "auth.h"
5c377b3b 44#include "auth-pam.h"
05114c74 45#include "buffer.h"
46#include "bufaux.h"
42f11eb2 47#include "canohost.h"
05114c74 48#include "log.h"
49#include "monitor_wrap.h"
50#include "msg.h"
51#include "packet.h"
42f11eb2 52#include "readpass.h"
05114c74 53#include "servconf.h"
54#include "ssh2.h"
55#include "xmalloc.h"
5b9e2464 56#include "auth-options.h"
a5c9cd31 57
7fceb20d 58extern ServerOptions options;
3eaf3960 59extern Buffer loginmsg;
b3ef7fb7 60extern int compat20;
7fceb20d 61
05114c74 62#ifdef USE_POSIX_THREADS
63#include <pthread.h>
64/*
aff51935 65 * Avoid namespace clash when *not* using pthreads for systems *with*
66 * pthreads, which unconditionally define pthread_t via sys/types.h
05114c74 67 * (e.g. Linux)
68 */
aff51935 69typedef pthread_t sp_pthread_t;
05114c74 70#else
e47e681f 71typedef pid_t sp_pthread_t;
72#endif
73
74struct pam_ctxt {
75 sp_pthread_t pam_thread;
76 int pam_psock;
77 int pam_csock;
78 int pam_done;
79};
80
81static void sshpam_free_ctx(void *);
82static struct pam_ctxt *cleanup_ctxt;
83
84#ifndef USE_POSIX_THREADS
05114c74 85/*
86 * Simulate threads with processes.
87 */
a5c9cd31 88
100e6910 89static int sshpam_thread_status = -1;
90static mysig_t sshpam_oldsig;
91
92static void
93sshpam_sigchld_handler(int sig)
94{
28b49ff8 95 if (cleanup_ctxt == NULL)
96 return; /* handler called after PAM cleanup, shouldn't happen */
100e6910 97 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0) == -1)
98 return; /* couldn't wait for process */
99 if (WIFSIGNALED(sshpam_thread_status) &&
100 WTERMSIG(sshpam_thread_status) == SIGTERM)
101 return; /* terminated by pthread_cancel */
102 if (!WIFEXITED(sshpam_thread_status))
103 fatal("PAM: authentication thread exited unexpectedly");
104 if (WEXITSTATUS(sshpam_thread_status) != 0)
105 fatal("PAM: authentication thread exited uncleanly");
106}
107
05114c74 108static void
109pthread_exit(void *value __unused)
110{
111 _exit(0);
112}
5daf7064 113
05114c74 114static int
115pthread_create(sp_pthread_t *thread, const void *attr __unused,
116 void *(*thread_start)(void *), void *arg)
117{
118 pid_t pid;
119
120 switch ((pid = fork())) {
121 case -1:
122 error("fork(): %s", strerror(errno));
123 return (-1);
124 case 0:
125 thread_start(arg);
126 _exit(1);
127 default:
128 *thread = pid;
100e6910 129 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
05114c74 130 return (0);
131 }
132}
a5c9cd31 133
05114c74 134static int
135pthread_cancel(sp_pthread_t thread)
8c9fe09e 136{
28b49ff8 137 signal(SIGCHLD, sshpam_oldsig);
05114c74 138 return (kill(thread, SIGTERM));
8c9fe09e 139}
140
05114c74 141static int
142pthread_join(sp_pthread_t thread, void **value __unused)
8c9fe09e 143{
05114c74 144 int status;
145
100e6910 146 if (sshpam_thread_status != -1)
147 return (sshpam_thread_status);
148 signal(SIGCHLD, sshpam_oldsig);
05114c74 149 waitpid(thread, &status, 0);
150 return (status);
8c9fe09e 151}
05114c74 152#endif
153
154
0a23d79f 155static pam_handle_t *sshpam_handle = NULL;
156static int sshpam_err = 0;
157static int sshpam_authenticated = 0;
158static int sshpam_new_authtok_reqd = 0;
159static int sshpam_session_open = 0;
160static int sshpam_cred_established = 0;
b3ef7fb7 161static int sshpam_account_status = -1;
2212fc98 162static char **sshpam_env = NULL;
05114c74 163
2212fc98 164/* Some PAM implementations don't implement this */
165#ifndef HAVE_PAM_GETENVLIST
166static char **
167pam_getenvlist(pam_handle_t *pamh)
168{
169 /*
aff51935 170 * XXX - If necessary, we can still support envrionment passing
2212fc98 171 * for platforms without pam_getenvlist by searching for known
172 * env vars (e.g. KRB5CCNAME) from the PAM environment.
173 */
174 return NULL;
175}
176#endif
177
b3ef7fb7 178void
179pam_password_change_required(int reqd)
180{
90f3c272 181 debug3("%s %d", __func__, reqd);
b3ef7fb7 182 sshpam_new_authtok_reqd = reqd;
183 if (reqd) {
184 no_port_forwarding_flag |= 2;
185 no_agent_forwarding_flag |= 2;
186 no_x11_forwarding_flag |= 2;
187 } else {
188 no_port_forwarding_flag &= ~2;
189 no_agent_forwarding_flag &= ~2;
190 no_x11_forwarding_flag &= ~2;
191
192 }
193}
2212fc98 194/* Import regular and PAM environment from subprocess */
195static void
196import_environments(Buffer *b)
197{
198 char *env;
199 u_int i, num_env;
200 int err;
201
90f3c272 202 debug3("PAM: %s entering", __func__);
203
b3ef7fb7 204 /* Import variables set by do_pam_account */
205 sshpam_account_status = buffer_get_int(b);
206 pam_password_change_required(buffer_get_int(b));
207
2212fc98 208 /* Import environment from subprocess */
209 num_env = buffer_get_int(b);
210 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
211 debug3("PAM: num env strings %d", num_env);
212 for(i = 0; i < num_env; i++)
213 sshpam_env[i] = buffer_get_string(b, NULL);
214
215 sshpam_env[num_env] = NULL;
216
217 /* Import PAM environment from subprocess */
218 num_env = buffer_get_int(b);
219 debug("PAM: num PAM env strings %d", num_env);
220 for(i = 0; i < num_env; i++) {
221 env = buffer_get_string(b, NULL);
222
f79a6165 223#ifdef HAVE_PAM_PUTENV
2212fc98 224 /* Errors are not fatal here */
225 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
226 error("PAM: pam_putenv: %s",
227 pam_strerror(sshpam_handle, sshpam_err));
228 }
f79a6165 229#endif
2212fc98 230 }
231}
232
ad55cd03 233/*
05114c74 234 * Conversation function for authentication thread.
ad55cd03 235 */
05114c74 236static int
5b9e2464 237sshpam_thread_conv(int n, const struct pam_message **msg,
238 struct pam_response **resp, void *data)
a5c9cd31 239{
05114c74 240 Buffer buffer;
241 struct pam_ctxt *ctxt;
0a23d79f 242 struct pam_response *reply;
05114c74 243 int i;
244
90f3c272 245 debug3("PAM: %s entering, %d responses", __func__, n);
0a23d79f 246 *resp = NULL;
247
05114c74 248 ctxt = data;
249 if (n <= 0 || n > PAM_MAX_NUM_MSG)
250 return (PAM_CONV_ERR);
0a23d79f 251
252 if ((reply = malloc(n * sizeof(*reply))) == NULL)
253 return (PAM_CONV_ERR);
254 memset(reply, 0, n * sizeof(*reply));
255
05114c74 256 buffer_init(&buffer);
257 for (i = 0; i < n; ++i) {
05114c74 258 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
259 case PAM_PROMPT_ECHO_OFF:
aff51935 260 buffer_put_cstring(&buffer,
0a23d79f 261 PAM_MSG_MEMBER(msg, i, msg));
aff51935 262 if (ssh_msg_send(ctxt->pam_csock,
7b2a0de3 263 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
264 goto fail;
aff51935 265 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
7b2a0de3 266 goto fail;
05114c74 267 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
268 goto fail;
0a23d79f 269 reply[i].resp = buffer_get_string(&buffer, NULL);
05114c74 270 break;
271 case PAM_PROMPT_ECHO_ON:
aff51935 272 buffer_put_cstring(&buffer,
0a23d79f 273 PAM_MSG_MEMBER(msg, i, msg));
aff51935 274 if (ssh_msg_send(ctxt->pam_csock,
7b2a0de3 275 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
276 goto fail;
277 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
278 goto fail;
05114c74 279 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
280 goto fail;
0a23d79f 281 reply[i].resp = buffer_get_string(&buffer, NULL);
05114c74 282 break;
283 case PAM_ERROR_MSG:
aff51935 284 buffer_put_cstring(&buffer,
0a23d79f 285 PAM_MSG_MEMBER(msg, i, msg));
aff51935 286 if (ssh_msg_send(ctxt->pam_csock,
7b2a0de3 287 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
288 goto fail;
05114c74 289 break;
290 case PAM_TEXT_INFO:
aff51935 291 buffer_put_cstring(&buffer,
0a23d79f 292 PAM_MSG_MEMBER(msg, i, msg));
aff51935 293 if (ssh_msg_send(ctxt->pam_csock,
7b2a0de3 294 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
295 goto fail;
05114c74 296 break;
297 default:
298 goto fail;
a5c9cd31 299 }
05114c74 300 buffer_clear(&buffer);
a5c9cd31 301 }
05114c74 302 buffer_free(&buffer);
0a23d79f 303 *resp = reply;
05114c74 304 return (PAM_SUCCESS);
0a23d79f 305
05114c74 306 fail:
0a23d79f 307 for(i = 0; i < n; i++) {
308 if (reply[i].resp != NULL)
309 xfree(reply[i].resp);
310 }
311 xfree(reply);
05114c74 312 buffer_free(&buffer);
313 return (PAM_CONV_ERR);
314}
a5c9cd31 315
05114c74 316/*
317 * Authentication thread.
318 */
319static void *
320sshpam_thread(void *ctxtp)
321{
322 struct pam_ctxt *ctxt = ctxtp;
323 Buffer buffer;
c53917a9 324 struct pam_conv sshpam_conv;
05114c74 325#ifndef USE_POSIX_THREADS
2212fc98 326 extern char **environ;
327 char **env_from_pam;
328 u_int i;
05114c74 329 const char *pam_user;
330
331 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
332 setproctitle("%s [pam]", pam_user);
2212fc98 333 environ[0] = NULL;
05114c74 334#endif
a5c9cd31 335
c53917a9 336 sshpam_conv.conv = sshpam_thread_conv;
337 sshpam_conv.appdata_ptr = ctxt;
338
05114c74 339 buffer_init(&buffer);
340 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
341 (const void *)&sshpam_conv);
342 if (sshpam_err != PAM_SUCCESS)
343 goto auth_fail;
344 sshpam_err = pam_authenticate(sshpam_handle, 0);
345 if (sshpam_err != PAM_SUCCESS)
346 goto auth_fail;
b3ef7fb7 347
2935db92 348 if (compat20) {
b3ef7fb7 349 if (!do_pam_account())
350 goto auth_fail;
351 if (sshpam_new_authtok_reqd) {
352 sshpam_err = pam_chauthtok(sshpam_handle,
353 PAM_CHANGE_EXPIRED_AUTHTOK);
354 if (sshpam_err != PAM_SUCCESS)
355 goto auth_fail;
356 pam_password_change_required(0);
357 }
2935db92 358 }
b3ef7fb7 359
05114c74 360 buffer_put_cstring(&buffer, "OK");
2212fc98 361
362#ifndef USE_POSIX_THREADS
b3ef7fb7 363 /* Export variables set by do_pam_account */
364 buffer_put_int(&buffer, sshpam_account_status);
365 buffer_put_int(&buffer, sshpam_new_authtok_reqd);
366
2212fc98 367 /* Export any environment strings set in child */
368 for(i = 0; environ[i] != NULL; i++)
369 ; /* Count */
370 buffer_put_int(&buffer, i);
371 for(i = 0; environ[i] != NULL; i++)
372 buffer_put_cstring(&buffer, environ[i]);
373
374 /* Export any environment strings set by PAM in child */
375 env_from_pam = pam_getenvlist(sshpam_handle);
376 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
377 ; /* Count */
378 buffer_put_int(&buffer, i);
379 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
380 buffer_put_cstring(&buffer, env_from_pam[i]);
381#endif /* USE_POSIX_THREADS */
382
7b2a0de3 383 /* XXX - can't do much about an error here */
05114c74 384 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
385 buffer_free(&buffer);
386 pthread_exit(NULL);
387
388 auth_fail:
389 buffer_put_cstring(&buffer,
390 pam_strerror(sshpam_handle, sshpam_err));
7b2a0de3 391 /* XXX - can't do much about an error here */
05114c74 392 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
393 buffer_free(&buffer);
394 pthread_exit(NULL);
b6453d99 395
05114c74 396 return (NULL); /* Avoid warning for non-pthread case */
a5c9cd31 397}
398
c6630044 399void
400sshpam_thread_cleanup(void)
a5c9cd31 401{
c6630044 402 struct pam_ctxt *ctxt = cleanup_ctxt;
403
90f3c272 404 debug3("PAM: %s entering", __func__);
c6630044 405 if (ctxt != NULL && ctxt->pam_thread != 0) {
406 pthread_cancel(ctxt->pam_thread);
407 pthread_join(ctxt->pam_thread, NULL);
408 close(ctxt->pam_psock);
409 close(ctxt->pam_csock);
410 memset(ctxt, 0, sizeof(*ctxt));
411 cleanup_ctxt = NULL;
412 }
05114c74 413}
a5c9cd31 414
05114c74 415static int
5b9e2464 416sshpam_null_conv(int n, const struct pam_message **msg,
417 struct pam_response **resp, void *data)
05114c74 418{
90f3c272 419 debug3("PAM: %s entering, %d responses", __func__, n);
05114c74 420 return (PAM_CONV_ERR);
421}
a5c9cd31 422
05114c74 423static struct pam_conv null_conv = { sshpam_null_conv, NULL };
424
c6630044 425void
426sshpam_cleanup(void)
05114c74 427{
05114c74 428 debug("PAM: cleanup");
0a23d79f 429 if (sshpam_handle == NULL)
430 return;
05114c74 431 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
432 if (sshpam_cred_established) {
433 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
434 sshpam_cred_established = 0;
435 }
436 if (sshpam_session_open) {
437 pam_close_session(sshpam_handle, PAM_SILENT);
438 sshpam_session_open = 0;
a5c9cd31 439 }
05114c74 440 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
441 pam_end(sshpam_handle, sshpam_err);
442 sshpam_handle = NULL;
a5c9cd31 443}
444
05114c74 445static int
446sshpam_init(const char *user)
a5c9cd31 447{
05114c74 448 extern u_int utmp_len;
74678fb9 449 extern char *__progname;
05114c74 450 const char *pam_rhost, *pam_user;
451
452 if (sshpam_handle != NULL) {
453 /* We already have a PAM context; check if the user matches */
454 sshpam_err = pam_get_item(sshpam_handle,
455 PAM_USER, (const void **)&pam_user);
456 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
457 return (0);
05114c74 458 pam_end(sshpam_handle, sshpam_err);
459 sshpam_handle = NULL;
460 }
461 debug("PAM: initializing for \"%s\"", user);
01224183 462 sshpam_err =
463 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
5ff453c0 464 if (sshpam_err != PAM_SUCCESS) {
465 pam_end(sshpam_handle, sshpam_err);
466 sshpam_handle = NULL;
05114c74 467 return (-1);
5ff453c0 468 }
c5a7d788 469 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
05d62329 470 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
05114c74 471 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
5ff453c0 472 if (sshpam_err != PAM_SUCCESS) {
5b9e2464 473 pam_end(sshpam_handle, sshpam_err);
5ff453c0 474 sshpam_handle = NULL;
475 return (-1);
476 }
477#ifdef PAM_TTY_KLUDGE
aff51935 478 /*
479 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
480 * sshd doesn't set the tty until too late in the auth process and
5ff453c0 481 * may not even set one (for tty-less connections)
aff51935 482 */
5ff453c0 483 debug("PAM: setting PAM_TTY to \"ssh\"");
484 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
05114c74 485 if (sshpam_err != PAM_SUCCESS) {
486 pam_end(sshpam_handle, sshpam_err);
487 sshpam_handle = NULL;
488 return (-1);
a5c9cd31 489 }
5ff453c0 490#endif
05114c74 491 return (0);
a5c9cd31 492}
493
05114c74 494static void *
495sshpam_init_ctx(Authctxt *authctxt)
a5c9cd31 496{
05114c74 497 struct pam_ctxt *ctxt;
498 int socks[2];
2b87da3b 499
90f3c272 500 debug3("PAM: %s entering", __func__);
7fceb20d 501 /* Refuse to start if we don't have PAM enabled */
502 if (!options.use_pam)
503 return NULL;
504
05114c74 505 /* Initialize PAM */
506 if (sshpam_init(authctxt->user) == -1) {
507 error("PAM: initialization failed");
508 return (NULL);
509 }
5c377b3b 510
05114c74 511 ctxt = xmalloc(sizeof *ctxt);
c6630044 512 memset(ctxt, 0, sizeof(*ctxt));
05114c74 513
514 /* Start the authentication thread */
515 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
516 error("PAM: failed create sockets: %s", strerror(errno));
517 xfree(ctxt);
518 return (NULL);
519 }
520 ctxt->pam_psock = socks[0];
521 ctxt->pam_csock = socks[1];
522 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
523 error("PAM: failed to start authentication thread: %s",
524 strerror(errno));
525 close(socks[0]);
526 close(socks[1]);
527 xfree(ctxt);
528 return (NULL);
a5c9cd31 529 }
c6630044 530 cleanup_ctxt = ctxt;
05114c74 531 return (ctxt);
532}
a5c9cd31 533
05114c74 534static int
535sshpam_query(void *ctx, char **name, char **info,
536 u_int *num, char ***prompts, u_int **echo_on)
537{
538 Buffer buffer;
539 struct pam_ctxt *ctxt = ctx;
540 size_t plen;
541 u_char type;
542 char *msg;
cbdeccf3 543 size_t len;
05114c74 544
90f3c272 545 debug3("PAM: %s entering", __func__);
05114c74 546 buffer_init(&buffer);
547 *name = xstrdup("");
548 *info = xstrdup("");
549 *prompts = xmalloc(sizeof(char *));
550 **prompts = NULL;
551 plen = 0;
552 *echo_on = xmalloc(sizeof(u_int));
553 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
554 type = buffer_get_char(&buffer);
555 msg = buffer_get_string(&buffer, NULL);
556 switch (type) {
557 case PAM_PROMPT_ECHO_ON:
558 case PAM_PROMPT_ECHO_OFF:
559 *num = 1;
cbdeccf3 560 len = plen + strlen(msg) + 1;
561 **prompts = xrealloc(**prompts, len);
562 plen += snprintf(**prompts + plen, len, "%s", msg);
05114c74 563 **echo_on = (type == PAM_PROMPT_ECHO_ON);
564 xfree(msg);
565 return (0);
566 case PAM_ERROR_MSG:
567 case PAM_TEXT_INFO:
568 /* accumulate messages */
4f1b45b4 569 len = plen + strlen(msg) + 2;
cbdeccf3 570 **prompts = xrealloc(**prompts, len);
4f1b45b4 571 plen += snprintf(**prompts + plen, len, "%s\n", msg);
05114c74 572 xfree(msg);
5daf7064 573 break;
05114c74 574 case PAM_SUCCESS:
575 case PAM_AUTH_ERR:
576 if (**prompts != NULL) {
577 /* drain any accumulated messages */
3eaf3960 578 debug("PAM: %s", **prompts);
579 buffer_append(&loginmsg, **prompts,
580 strlen(**prompts));
05114c74 581 xfree(**prompts);
582 **prompts = NULL;
583 }
584 if (type == PAM_SUCCESS) {
2212fc98 585 import_environments(&buffer);
05114c74 586 *num = 0;
587 **echo_on = 0;
588 ctxt->pam_done = 1;
589 xfree(msg);
590 return (0);
591 }
592 error("PAM: %s", msg);
e250770b 593 /* FALLTHROUGH */
5daf7064 594 default:
05114c74 595 *num = 0;
596 **echo_on = 0;
597 xfree(msg);
598 ctxt->pam_done = -1;
599 return (-1);
600 }
a5c9cd31 601 }
05114c74 602 return (-1);
a5c9cd31 603}
604
05114c74 605/* XXX - see also comment in auth-chall.c:verify_response */
606static int
607sshpam_respond(void *ctx, u_int num, char **resp)
a5c9cd31 608{
05114c74 609 Buffer buffer;
610 struct pam_ctxt *ctxt = ctx;
611
90f3c272 612 debug2("PAM: %s entering, %d responses", __func__, num);
05114c74 613 switch (ctxt->pam_done) {
614 case 1:
615 sshpam_authenticated = 1;
616 return (0);
617 case 0:
618 break;
619 default:
620 return (-1);
a5c9cd31 621 }
05114c74 622 if (num != 1) {
623 error("PAM: expected one response, got %u", num);
624 return (-1);
625 }
626 buffer_init(&buffer);
627 buffer_put_cstring(&buffer, *resp);
7b2a0de3 628 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
629 buffer_free(&buffer);
630 return (-1);
631 }
05114c74 632 buffer_free(&buffer);
633 return (1);
a5c9cd31 634}
635
05114c74 636static void
637sshpam_free_ctx(void *ctxtp)
a5c9cd31 638{
05114c74 639 struct pam_ctxt *ctxt = ctxtp;
39ce53de 640
90f3c272 641 debug3("PAM: %s entering", __func__);
c6630044 642 sshpam_thread_cleanup();
05114c74 643 xfree(ctxt);
644 /*
645 * We don't call sshpam_cleanup() here because we may need the PAM
646 * handle at a later stage, e.g. when setting up a session. It's
647 * still on the cleanup list, so pam_end() *will* be called before
648 * the server process terminates.
649 */
ad55cd03 650}
651
05114c74 652KbdintDevice sshpam_device = {
653 "pam",
654 sshpam_init_ctx,
655 sshpam_query,
656 sshpam_respond,
657 sshpam_free_ctx
658};
659
660KbdintDevice mm_sshpam_device = {
661 "pam",
662 mm_sshpam_init_ctx,
663 mm_sshpam_query,
664 mm_sshpam_respond,
665 mm_sshpam_free_ctx
666};
2919e060 667
2b87da3b 668/*
05114c74 669 * This replaces auth-pam.c
ad55cd03 670 */
05114c74 671void
672start_pam(const char *user)
ad55cd03 673{
817e6d38 674 if (!options.use_pam)
675 fatal("PAM: initialisation requested when UsePAM=no");
676
05114c74 677 if (sshpam_init(user) == -1)
678 fatal("PAM: initialisation failed");
a5c9cd31 679}
680
05114c74 681void
682finish_pam(void)
a5c9cd31 683{
c6630044 684 sshpam_cleanup();
a5c9cd31 685}
686
5b9e2464 687u_int
688do_pam_account(void)
a5c9cd31 689{
b3ef7fb7 690 if (sshpam_account_status != -1)
691 return (sshpam_account_status);
692
5b9e2464 693 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
90f3c272 694 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
b3ef7fb7 695
696 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
697 sshpam_account_status = 0;
698 return (sshpam_account_status);
5b9e2464 699 }
700
b3ef7fb7 701 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
702 pam_password_change_required(1);
703
704 sshpam_account_status = 1;
705 return (sshpam_account_status);
05114c74 706}
46c76c63 707
49e82bb9 708void
709do_pam_set_tty(const char *tty)
710{
26b3608b 711 if (tty != NULL) {
712 debug("PAM: setting PAM_TTY to \"%s\"", tty);
713 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
714 if (sshpam_err != PAM_SUCCESS)
715 fatal("PAM: failed to set PAM_TTY: %s",
716 pam_strerror(sshpam_handle, sshpam_err));
717 }
05114c74 718}
cbd7492e 719
05114c74 720void
721do_pam_setcred(int init)
722{
723 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
724 (const void *)&null_conv);
725 if (sshpam_err != PAM_SUCCESS)
726 fatal("PAM: failed to set PAM_CONV: %s",
727 pam_strerror(sshpam_handle, sshpam_err));
728 if (init) {
729 debug("PAM: establishing credentials");
730 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
731 } else {
732 debug("PAM: reinitializing credentials");
733 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
734 }
735 if (sshpam_err == PAM_SUCCESS) {
736 sshpam_cred_established = 1;
737 return;
738 }
739 if (sshpam_authenticated)
740 fatal("PAM: pam_setcred(): %s",
741 pam_strerror(sshpam_handle, sshpam_err));
742 else
743 debug("PAM: pam_setcred(): %s",
744 pam_strerror(sshpam_handle, sshpam_err));
a5c9cd31 745}
746
05114c74 747int
748is_pam_password_change_required(void)
a5c9cd31 749{
05114c74 750 return (sshpam_new_authtok_reqd);
a5c9cd31 751}
752
05114c74 753static int
3eaf3960 754pam_tty_conv(int n, const struct pam_message **msg,
5b9e2464 755 struct pam_response **resp, void *data)
ee48c949 756{
05114c74 757 char input[PAM_MAX_MSG_SIZE];
0a23d79f 758 struct pam_response *reply;
ee48c949 759 int i;
760
0a23d79f 761 *resp = NULL;
762
3eaf3960 763 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
05114c74 764 return (PAM_CONV_ERR);
0a23d79f 765
766 if ((reply = malloc(n * sizeof(*reply))) == NULL)
767 return (PAM_CONV_ERR);
768 memset(reply, 0, n * sizeof(*reply));
769
05114c74 770 for (i = 0; i < n; ++i) {
771 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
772 case PAM_PROMPT_ECHO_OFF:
0a23d79f 773 reply[i].resp =
aff51935 774 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
05114c74 775 RP_ALLOW_STDIN);
0a23d79f 776 reply[i].resp_retcode = PAM_SUCCESS;
05114c74 777 break;
778 case PAM_PROMPT_ECHO_ON:
74117b26 779 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
05114c74 780 fgets(input, sizeof input, stdin);
0a23d79f 781 reply[i].resp = xstrdup(input);
782 reply[i].resp_retcode = PAM_SUCCESS;
05114c74 783 break;
784 case PAM_ERROR_MSG:
785 case PAM_TEXT_INFO:
74117b26 786 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
0a23d79f 787 reply[i].resp_retcode = PAM_SUCCESS;
05114c74 788 break;
789 default:
790 goto fail;
791 }
ee48c949 792 }
0a23d79f 793 *resp = reply;
05114c74 794 return (PAM_SUCCESS);
0a23d79f 795
05114c74 796 fail:
0a23d79f 797 for(i = 0; i < n; i++) {
798 if (reply[i].resp != NULL)
799 xfree(reply[i].resp);
800 }
801 xfree(reply);
05114c74 802 return (PAM_CONV_ERR);
ee48c949 803}
804
3eaf3960 805static struct pam_conv tty_conv = { pam_tty_conv, NULL };
806
05114c74 807/*
808 * XXX this should be done in the authentication phase, but ssh1 doesn't
809 * support that
810 */
811void
812do_pam_chauthtok(void)
a5c9cd31 813{
05114c74 814 if (use_privsep)
5b9e2464 815 fatal("Password expired (unable to change with privsep)");
05114c74 816 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
3eaf3960 817 (const void *)&tty_conv);
05114c74 818 if (sshpam_err != PAM_SUCCESS)
819 fatal("PAM: failed to set PAM_CONV: %s",
820 pam_strerror(sshpam_handle, sshpam_err));
821 debug("PAM: changing password");
822 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
823 if (sshpam_err != PAM_SUCCESS)
824 fatal("PAM: pam_chauthtok(): %s",
825 pam_strerror(sshpam_handle, sshpam_err));
5daf7064 826}
827
3eaf3960 828void
829do_pam_session(void)
830{
aff51935 831 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
3eaf3960 832 (const void *)&tty_conv);
833 if (sshpam_err != PAM_SUCCESS)
834 fatal("PAM: failed to set PAM_CONV: %s",
835 pam_strerror(sshpam_handle, sshpam_err));
836 sshpam_err = pam_open_session(sshpam_handle, 0);
837 if (sshpam_err != PAM_SUCCESS)
838 fatal("PAM: pam_open_session(): %s",
839 pam_strerror(sshpam_handle, sshpam_err));
840 sshpam_session_open = 1;
841}
842
aff51935 843/*
749560dd 844 * Set a PAM environment string. We need to do this so that the session
845 * modules can handle things like Kerberos/GSI credentials that appear
846 * during the ssh authentication process.
847 */
749560dd 848int
aff51935 849do_pam_putenv(char *name, char *value)
749560dd 850{
749560dd 851 int ret = 1;
b6453d99 852#ifdef HAVE_PAM_PUTENV
263c65df 853 char *compound;
854 size_t len;
855
856 len = strlen(name) + strlen(value) + 2;
857 compound = xmalloc(len);
858
859 snprintf(compound, len, "%s=%s", name, value);
860 ret = pam_putenv(sshpam_handle, compound);
861 xfree(compound);
749560dd 862#endif
263c65df 863
749560dd 864 return (ret);
865}
866
05114c74 867void
868print_pam_messages(void)
5daf7064 869{
05114c74 870 /* XXX */
871}
2b87da3b 872
2212fc98 873char **
874fetch_pam_child_environment(void)
875{
876 return sshpam_env;
877}
878
05114c74 879char **
880fetch_pam_environment(void)
881{
05114c74 882 return (pam_getenvlist(sshpam_handle));
05114c74 883}
5c377b3b 884
05114c74 885void
886free_pam_environment(char **env)
887{
888 char **envp;
5daf7064 889
0eb6370a 890 if (env == NULL)
891 return;
892
05114c74 893 for (envp = env; *envp; envp++)
894 xfree(*envp);
895 xfree(env);
a5c9cd31 896}
897
898#endif /* USE_PAM */
This page took 0.316751 seconds and 5 git commands to generate.