]> andersk Git - gssapi-openssh.git/blame - openssh/auth-pam.c
Initial revision
[gssapi-openssh.git] / openssh / auth-pam.c
CommitLineData
7cac2b65 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.
3c0ef626 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.
18 *
7cac2b65 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.
3c0ef626 30 */
31
7cac2b65 32/* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
3c0ef626 33#include "includes.h"
7cac2b65 34RCSID("$Id$");
3c0ef626 35
36#ifdef USE_PAM
540d72c3 37#if defined(HAVE_SECURITY_PAM_APPL_H)
7cac2b65 38#include <security/pam_appl.h>
540d72c3 39#elif defined (HAVE_PAM_PAM_APPL_H)
40#include <pam/pam_appl.h>
41#endif
7cac2b65 42
350391c5 43#include "auth.h"
3c0ef626 44#include "auth-pam.h"
7cac2b65 45#include "buffer.h"
46#include "bufaux.h"
3c0ef626 47#include "canohost.h"
7cac2b65 48#include "log.h"
49#include "monitor_wrap.h"
50#include "msg.h"
51#include "packet.h"
3c0ef626 52#include "readpass.h"
7cac2b65 53#include "servconf.h"
54#include "ssh2.h"
55#include "xmalloc.h"
56#include "auth-options.h"
3c0ef626 57
7cac2b65 58extern ServerOptions options;
540d72c3 59extern Buffer loginmsg;
60extern int compat20;
d03f4262 61
7cac2b65 62#ifdef USE_POSIX_THREADS
63#include <pthread.h>
64/*
540d72c3 65 * Avoid namespace clash when *not* using pthreads for systems *with*
66 * pthreads, which unconditionally define pthread_t via sys/types.h
7cac2b65 67 * (e.g. Linux)
68 */
540d72c3 69typedef pthread_t sp_pthread_t;
7cac2b65 70#else
540d72c3 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
7cac2b65 85/*
86 * Simulate threads with processes.
87 */
540d72c3 88
89static int sshpam_thread_status = -1;
90static mysig_t sshpam_oldsig;
91
92static void
93sshpam_sigchld_handler(int sig)
94{
95 if (cleanup_ctxt == NULL)
96 return; /* handler called after PAM cleanup, shouldn't happen */
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}
3c0ef626 107
7cac2b65 108static void
109pthread_exit(void *value __unused)
110{
111 _exit(0);
112}
3c0ef626 113
7cac2b65 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;
540d72c3 129 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
7cac2b65 130 return (0);
131 }
132}
3c0ef626 133
7cac2b65 134static int
135pthread_cancel(sp_pthread_t thread)
3c0ef626 136{
540d72c3 137 signal(SIGCHLD, sshpam_oldsig);
7cac2b65 138 return (kill(thread, SIGTERM));
3c0ef626 139}
140
7cac2b65 141static int
142pthread_join(sp_pthread_t thread, void **value __unused)
3c0ef626 143{
7cac2b65 144 int status;
145
540d72c3 146 if (sshpam_thread_status != -1)
147 return (sshpam_thread_status);
148 signal(SIGCHLD, sshpam_oldsig);
7cac2b65 149 waitpid(thread, &status, 0);
150 return (status);
3c0ef626 151}
7cac2b65 152#endif
153
154
29d88157 155static pam_handle_t *sshpam_handle = NULL;
156static int sshpam_err = 0;
157static int sshpam_authenticated = 0;
29d88157 158static int sshpam_session_open = 0;
159static int sshpam_cred_established = 0;
540d72c3 160static int sshpam_account_status = -1;
161static char **sshpam_env = NULL;
162static int *force_pwchange;
163
164/* Some PAM implementations don't implement this */
165#ifndef HAVE_PAM_GETENVLIST
166static char **
167pam_getenvlist(pam_handle_t *pamh)
168{
169 /*
170 * XXX - If necessary, we can still support envrionment passing
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
7cac2b65 177
540d72c3 178void
179pam_password_change_required(int reqd)
180{
181 debug3("%s %d", __func__, reqd);
182 *force_pwchange = 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}
7cac2b65 193
540d72c3 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
202 debug3("PAM: %s entering", __func__);
203
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
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
223#ifdef HAVE_PAM_PUTENV
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 }
229#endif
230 }
231}
3c0ef626 232
233/*
7cac2b65 234 * Conversation function for authentication thread.
3c0ef626 235 */
7cac2b65 236static int
237sshpam_thread_conv(int n, const struct pam_message **msg,
238 struct pam_response **resp, void *data)
3c0ef626 239{
7cac2b65 240 Buffer buffer;
241 struct pam_ctxt *ctxt;
29d88157 242 struct pam_response *reply;
7cac2b65 243 int i;
244
540d72c3 245 debug3("PAM: %s entering, %d messages", __func__, n);
29d88157 246 *resp = NULL;
247
7cac2b65 248 ctxt = data;
249 if (n <= 0 || n > PAM_MAX_NUM_MSG)
250 return (PAM_CONV_ERR);
29d88157 251
252 if ((reply = malloc(n * sizeof(*reply))) == NULL)
253 return (PAM_CONV_ERR);
254 memset(reply, 0, n * sizeof(*reply));
255
7cac2b65 256 buffer_init(&buffer);
257 for (i = 0; i < n; ++i) {
7cac2b65 258 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
259 case PAM_PROMPT_ECHO_OFF:
540d72c3 260 buffer_put_cstring(&buffer,
29d88157 261 PAM_MSG_MEMBER(msg, i, msg));
540d72c3 262 if (ssh_msg_send(ctxt->pam_csock,
263 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
264 goto fail;
265 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
266 goto fail;
7cac2b65 267 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
268 goto fail;
29d88157 269 reply[i].resp = buffer_get_string(&buffer, NULL);
7cac2b65 270 break;
271 case PAM_PROMPT_ECHO_ON:
540d72c3 272 buffer_put_cstring(&buffer,
29d88157 273 PAM_MSG_MEMBER(msg, i, msg));
540d72c3 274 if (ssh_msg_send(ctxt->pam_csock,
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;
7cac2b65 279 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
280 goto fail;
29d88157 281 reply[i].resp = buffer_get_string(&buffer, NULL);
7cac2b65 282 break;
283 case PAM_ERROR_MSG:
540d72c3 284 buffer_put_cstring(&buffer,
29d88157 285 PAM_MSG_MEMBER(msg, i, msg));
540d72c3 286 if (ssh_msg_send(ctxt->pam_csock,
287 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
288 goto fail;
7cac2b65 289 break;
290 case PAM_TEXT_INFO:
540d72c3 291 buffer_put_cstring(&buffer,
29d88157 292 PAM_MSG_MEMBER(msg, i, msg));
540d72c3 293 if (ssh_msg_send(ctxt->pam_csock,
294 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
295 goto fail;
7cac2b65 296 break;
297 default:
298 goto fail;
3c0ef626 299 }
7cac2b65 300 buffer_clear(&buffer);
3c0ef626 301 }
7cac2b65 302 buffer_free(&buffer);
29d88157 303 *resp = reply;
7cac2b65 304 return (PAM_SUCCESS);
29d88157 305
7cac2b65 306 fail:
29d88157 307 for(i = 0; i < n; i++) {
308 if (reply[i].resp != NULL)
309 xfree(reply[i].resp);
310 }
311 xfree(reply);
7cac2b65 312 buffer_free(&buffer);
313 return (PAM_CONV_ERR);
3c0ef626 314}
315
7cac2b65 316/*
317 * Authentication thread.
318 */
319static void *
320sshpam_thread(void *ctxtp)
3c0ef626 321{
7cac2b65 322 struct pam_ctxt *ctxt = ctxtp;
323 Buffer buffer;
324 struct pam_conv sshpam_conv;
325#ifndef USE_POSIX_THREADS
540d72c3 326 extern char **environ;
327 char **env_from_pam;
328 u_int i;
7cac2b65 329 const char *pam_user;
330
331 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
332 setproctitle("%s [pam]", pam_user);
540d72c3 333 environ[0] = NULL;
7cac2b65 334#endif
3c0ef626 335
7cac2b65 336 sshpam_conv.conv = sshpam_thread_conv;
337 sshpam_conv.appdata_ptr = ctxt;
338
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;
540d72c3 347
348 if (compat20) {
349 if (!do_pam_account())
350 goto auth_fail;
351 if (*force_pwchange) {
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 }
358 }
359
7cac2b65 360 buffer_put_cstring(&buffer, "OK");
540d72c3 361
362#ifndef USE_POSIX_THREADS
363 /* Export variables set by do_pam_account */
364 buffer_put_int(&buffer, sshpam_account_status);
365 buffer_put_int(&buffer, *force_pwchange);
366
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
383 /* XXX - can't do much about an error here */
7cac2b65 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));
540d72c3 391 /* XXX - can't do much about an error here */
7cac2b65 392 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
393 buffer_free(&buffer);
394 pthread_exit(NULL);
540d72c3 395
7cac2b65 396 return (NULL); /* Avoid warning for non-pthread case */
397}
3c0ef626 398
540d72c3 399void
400sshpam_thread_cleanup(void)
7cac2b65 401{
540d72c3 402 struct pam_ctxt *ctxt = cleanup_ctxt;
403
404 debug3("PAM: %s entering", __func__);
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 }
3c0ef626 413}
414
7cac2b65 415static int
416sshpam_null_conv(int n, const struct pam_message **msg,
417 struct pam_response **resp, void *data)
3c0ef626 418{
540d72c3 419 debug3("PAM: %s entering, %d messages", __func__, n);
7cac2b65 420 return (PAM_CONV_ERR);
421}
3c0ef626 422
7cac2b65 423static struct pam_conv null_conv = { sshpam_null_conv, NULL };
3c0ef626 424
540d72c3 425void
426sshpam_cleanup(void)
7cac2b65 427{
7cac2b65 428 debug("PAM: cleanup");
29d88157 429 if (sshpam_handle == NULL)
430 return;
7cac2b65 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;
439 }
540d72c3 440 sshpam_authenticated = 0;
7cac2b65 441 pam_end(sshpam_handle, sshpam_err);
442 sshpam_handle = NULL;
443}
3c0ef626 444
7cac2b65 445static int
446sshpam_init(const char *user)
447{
448 extern u_int utmp_len;
449 extern char *__progname;
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);
7cac2b65 458 pam_end(sshpam_handle, sshpam_err);
459 sshpam_handle = NULL;
460 }
461 debug("PAM: initializing for \"%s\"", user);
462 sshpam_err =
463 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
464 if (sshpam_err != PAM_SUCCESS) {
465 pam_end(sshpam_handle, sshpam_err);
466 sshpam_handle = NULL;
467 return (-1);
3c0ef626 468 }
7cac2b65 469 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
470 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
471 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
472 if (sshpam_err != PAM_SUCCESS) {
473 pam_end(sshpam_handle, sshpam_err);
474 sshpam_handle = NULL;
475 return (-1);
476 }
477#ifdef PAM_TTY_KLUDGE
540d72c3 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
7cac2b65 481 * may not even set one (for tty-less connections)
540d72c3 482 */
7cac2b65 483 debug("PAM: setting PAM_TTY to \"ssh\"");
484 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
485 if (sshpam_err != PAM_SUCCESS) {
486 pam_end(sshpam_handle, sshpam_err);
487 sshpam_handle = NULL;
488 return (-1);
489 }
490#endif
7cac2b65 491 return (0);
3c0ef626 492}
493
7cac2b65 494static void *
495sshpam_init_ctx(Authctxt *authctxt)
3c0ef626 496{
7cac2b65 497 struct pam_ctxt *ctxt;
498 int socks[2];
3c0ef626 499
540d72c3 500 debug3("PAM: %s entering", __func__);
7cac2b65 501 /* Refuse to start if we don't have PAM enabled */
502 if (!options.use_pam)
503 return NULL;
3c0ef626 504
7cac2b65 505 /* Initialize PAM */
506 if (sshpam_init(authctxt->user) == -1) {
507 error("PAM: initialization failed");
508 return (NULL);
3c0ef626 509 }
510
7cac2b65 511 ctxt = xmalloc(sizeof *ctxt);
540d72c3 512 memset(ctxt, 0, sizeof(*ctxt));
513
514 force_pwchange = &(authctxt->force_pwchange);
7cac2b65 515
516 /* Start the authentication thread */
517 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
518 error("PAM: failed create sockets: %s", strerror(errno));
519 xfree(ctxt);
520 return (NULL);
521 }
522 ctxt->pam_psock = socks[0];
523 ctxt->pam_csock = socks[1];
524 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
525 error("PAM: failed to start authentication thread: %s",
526 strerror(errno));
527 close(socks[0]);
528 close(socks[1]);
529 xfree(ctxt);
530 return (NULL);
531 }
540d72c3 532 cleanup_ctxt = ctxt;
7cac2b65 533 return (ctxt);
534}
535
536static int
537sshpam_query(void *ctx, char **name, char **info,
538 u_int *num, char ***prompts, u_int **echo_on)
539{
540 Buffer buffer;
541 struct pam_ctxt *ctxt = ctx;
542 size_t plen;
543 u_char type;
544 char *msg;
545 size_t len;
546
540d72c3 547 debug3("PAM: %s entering", __func__);
7cac2b65 548 buffer_init(&buffer);
549 *name = xstrdup("");
550 *info = xstrdup("");
551 *prompts = xmalloc(sizeof(char *));
552 **prompts = NULL;
553 plen = 0;
554 *echo_on = xmalloc(sizeof(u_int));
555 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
556 type = buffer_get_char(&buffer);
557 msg = buffer_get_string(&buffer, NULL);
558 switch (type) {
559 case PAM_PROMPT_ECHO_ON:
560 case PAM_PROMPT_ECHO_OFF:
561 *num = 1;
562 len = plen + strlen(msg) + 1;
563 **prompts = xrealloc(**prompts, len);
564 plen += snprintf(**prompts + plen, len, "%s", msg);
565 **echo_on = (type == PAM_PROMPT_ECHO_ON);
566 xfree(msg);
567 return (0);
568 case PAM_ERROR_MSG:
569 case PAM_TEXT_INFO:
570 /* accumulate messages */
540d72c3 571 len = plen + strlen(msg) + 2;
7cac2b65 572 **prompts = xrealloc(**prompts, len);
540d72c3 573 plen += snprintf(**prompts + plen, len, "%s\n", msg);
7cac2b65 574 xfree(msg);
3c0ef626 575 break;
7cac2b65 576 case PAM_SUCCESS:
577 case PAM_AUTH_ERR:
578 if (**prompts != NULL) {
579 /* drain any accumulated messages */
540d72c3 580 debug("PAM: %s", **prompts);
581 buffer_append(&loginmsg, **prompts,
582 strlen(**prompts));
7cac2b65 583 xfree(**prompts);
584 **prompts = NULL;
585 }
586 if (type == PAM_SUCCESS) {
540d72c3 587 import_environments(&buffer);
7cac2b65 588 *num = 0;
589 **echo_on = 0;
590 ctxt->pam_done = 1;
591 xfree(msg);
592 return (0);
593 }
594 error("PAM: %s", msg);
540d72c3 595 /* FALLTHROUGH */
3c0ef626 596 default:
7cac2b65 597 *num = 0;
598 **echo_on = 0;
599 xfree(msg);
600 ctxt->pam_done = -1;
601 return (-1);
602 }
3c0ef626 603 }
7cac2b65 604 return (-1);
3c0ef626 605}
606
7cac2b65 607/* XXX - see also comment in auth-chall.c:verify_response */
608static int
609sshpam_respond(void *ctx, u_int num, char **resp)
3c0ef626 610{
7cac2b65 611 Buffer buffer;
612 struct pam_ctxt *ctxt = ctx;
613
540d72c3 614 debug2("PAM: %s entering, %d responses", __func__, num);
7cac2b65 615 switch (ctxt->pam_done) {
616 case 1:
617 sshpam_authenticated = 1;
618 return (0);
619 case 0:
620 break;
621 default:
622 return (-1);
3c0ef626 623 }
7cac2b65 624 if (num != 1) {
625 error("PAM: expected one response, got %u", num);
626 return (-1);
627 }
628 buffer_init(&buffer);
629 buffer_put_cstring(&buffer, *resp);
540d72c3 630 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
631 buffer_free(&buffer);
632 return (-1);
633 }
7cac2b65 634 buffer_free(&buffer);
635 return (1);
3c0ef626 636}
637
7cac2b65 638static void
639sshpam_free_ctx(void *ctxtp)
3c0ef626 640{
7cac2b65 641 struct pam_ctxt *ctxt = ctxtp;
350391c5 642
540d72c3 643 debug3("PAM: %s entering", __func__);
644 sshpam_thread_cleanup();
7cac2b65 645 xfree(ctxt);
646 /*
647 * We don't call sshpam_cleanup() here because we may need the PAM
648 * handle at a later stage, e.g. when setting up a session. It's
649 * still on the cleanup list, so pam_end() *will* be called before
650 * the server process terminates.
651 */
3c0ef626 652}
653
7cac2b65 654KbdintDevice sshpam_device = {
655 "pam",
656 sshpam_init_ctx,
657 sshpam_query,
658 sshpam_respond,
659 sshpam_free_ctx
660};
661
662KbdintDevice mm_sshpam_device = {
663 "pam",
664 mm_sshpam_init_ctx,
665 mm_sshpam_query,
666 mm_sshpam_respond,
667 mm_sshpam_free_ctx
668};
3c0ef626 669
670/*
7cac2b65 671 * This replaces auth-pam.c
3c0ef626 672 */
7cac2b65 673void
674start_pam(const char *user)
3c0ef626 675{
7cac2b65 676 if (!options.use_pam)
677 fatal("PAM: initialisation requested when UsePAM=no");
678
679 if (sshpam_init(user) == -1)
680 fatal("PAM: initialisation failed");
3c0ef626 681}
682
7cac2b65 683void
684finish_pam(void)
3c0ef626 685{
540d72c3 686 sshpam_cleanup();
3c0ef626 687}
688
7cac2b65 689u_int
690do_pam_account(void)
3c0ef626 691{
540d72c3 692 if (sshpam_account_status != -1)
693 return (sshpam_account_status);
694
7cac2b65 695 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
540d72c3 696 debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err);
7cac2b65 697
540d72c3 698 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
699 sshpam_account_status = 0;
700 return (sshpam_account_status);
7cac2b65 701 }
3c0ef626 702
540d72c3 703 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
704 pam_password_change_required(1);
3c0ef626 705
540d72c3 706 sshpam_account_status = 1;
707 return (sshpam_account_status);
7cac2b65 708}
3c0ef626 709
7cac2b65 710void
711do_pam_set_tty(const char *tty)
712{
713 if (tty != NULL) {
714 debug("PAM: setting PAM_TTY to \"%s\"", tty);
715 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
716 if (sshpam_err != PAM_SUCCESS)
717 fatal("PAM: failed to set PAM_TTY: %s",
718 pam_strerror(sshpam_handle, sshpam_err));
719 }
720}
3c0ef626 721
7cac2b65 722void
723do_pam_setcred(int init)
724{
725 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
726 (const void *)&null_conv);
727 if (sshpam_err != PAM_SUCCESS)
728 fatal("PAM: failed to set PAM_CONV: %s",
729 pam_strerror(sshpam_handle, sshpam_err));
730 if (init) {
731 debug("PAM: establishing credentials");
732 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
733 } else {
734 debug("PAM: reinitializing credentials");
735 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
736 }
737 if (sshpam_err == PAM_SUCCESS) {
738 sshpam_cred_established = 1;
739 return;
740 }
741 if (sshpam_authenticated)
742 fatal("PAM: pam_setcred(): %s",
743 pam_strerror(sshpam_handle, sshpam_err));
744 else
745 debug("PAM: pam_setcred(): %s",
746 pam_strerror(sshpam_handle, sshpam_err));
3c0ef626 747}
748
7cac2b65 749static int
540d72c3 750pam_tty_conv(int n, const struct pam_message **msg,
7cac2b65 751 struct pam_response **resp, void *data)
23987cb8 752{
7cac2b65 753 char input[PAM_MAX_MSG_SIZE];
29d88157 754 struct pam_response *reply;
23987cb8 755 int i;
756
540d72c3 757 debug3("PAM: %s called with %d messages", __func__, n);
758
29d88157 759 *resp = NULL;
760
540d72c3 761 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
7cac2b65 762 return (PAM_CONV_ERR);
29d88157 763
764 if ((reply = malloc(n * sizeof(*reply))) == NULL)
765 return (PAM_CONV_ERR);
766 memset(reply, 0, n * sizeof(*reply));
767
7cac2b65 768 for (i = 0; i < n; ++i) {
769 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
770 case PAM_PROMPT_ECHO_OFF:
29d88157 771 reply[i].resp =
540d72c3 772 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
7cac2b65 773 RP_ALLOW_STDIN);
29d88157 774 reply[i].resp_retcode = PAM_SUCCESS;
7cac2b65 775 break;
776 case PAM_PROMPT_ECHO_ON:
540d72c3 777 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
7cac2b65 778 fgets(input, sizeof input, stdin);
29d88157 779 reply[i].resp = xstrdup(input);
780 reply[i].resp_retcode = PAM_SUCCESS;
7cac2b65 781 break;
782 case PAM_ERROR_MSG:
783 case PAM_TEXT_INFO:
540d72c3 784 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
29d88157 785 reply[i].resp_retcode = PAM_SUCCESS;
7cac2b65 786 break;
787 default:
788 goto fail;
789 }
23987cb8 790 }
29d88157 791 *resp = reply;
7cac2b65 792 return (PAM_SUCCESS);
29d88157 793
7cac2b65 794 fail:
29d88157 795 for(i = 0; i < n; i++) {
796 if (reply[i].resp != NULL)
797 xfree(reply[i].resp);
798 }
799 xfree(reply);
7cac2b65 800 return (PAM_CONV_ERR);
23987cb8 801}
802
540d72c3 803static struct pam_conv tty_conv = { pam_tty_conv, NULL };
804
7cac2b65 805/*
806 * XXX this should be done in the authentication phase, but ssh1 doesn't
807 * support that
808 */
809void
810do_pam_chauthtok(void)
811{
7cac2b65 812 if (use_privsep)
813 fatal("Password expired (unable to change with privsep)");
814 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
540d72c3 815 (const void *)&tty_conv);
7cac2b65 816 if (sshpam_err != PAM_SUCCESS)
817 fatal("PAM: failed to set PAM_CONV: %s",
818 pam_strerror(sshpam_handle, sshpam_err));
819 debug("PAM: changing password");
820 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
821 if (sshpam_err != PAM_SUCCESS)
822 fatal("PAM: pam_chauthtok(): %s",
823 pam_strerror(sshpam_handle, sshpam_err));
824}
825
540d72c3 826static int
827pam_store_conv(int n, const struct pam_message **msg,
828 struct pam_response **resp, void *data)
829{
830 struct pam_response *reply;
831 int i;
832 size_t len;
833
834 debug3("PAM: %s called with %d messages", __func__, n);
835 *resp = NULL;
836
837 if (n <= 0 || n > PAM_MAX_NUM_MSG)
838 return (PAM_CONV_ERR);
839
840 if ((reply = malloc(n * sizeof(*reply))) == NULL)
841 return (PAM_CONV_ERR);
842 memset(reply, 0, n * sizeof(*reply));
843
844 for (i = 0; i < n; ++i) {
845 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
846 case PAM_ERROR_MSG:
847 case PAM_TEXT_INFO:
848 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
849 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
850 buffer_append(&loginmsg, "\n", 1 );
851 reply[i].resp_retcode = PAM_SUCCESS;
852 break;
853 default:
854 goto fail;
855 }
856 }
857 *resp = reply;
858 return (PAM_SUCCESS);
859
860 fail:
861 for(i = 0; i < n; i++) {
862 if (reply[i].resp != NULL)
863 xfree(reply[i].resp);
864 }
865 xfree(reply);
866 return (PAM_CONV_ERR);
867}
868
869static struct pam_conv store_conv = { pam_store_conv, NULL };
870
871void
872do_pam_session(void)
873{
874 debug3("PAM: opening session");
875 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
876 (const void *)&store_conv);
877 if (sshpam_err != PAM_SUCCESS)
878 fatal("PAM: failed to set PAM_CONV: %s",
879 pam_strerror(sshpam_handle, sshpam_err));
880 sshpam_err = pam_open_session(sshpam_handle, 0);
881 if (sshpam_err != PAM_SUCCESS)
882 fatal("PAM: pam_open_session(): %s",
883 pam_strerror(sshpam_handle, sshpam_err));
884 sshpam_session_open = 1;
885}
886
887/*
7cac2b65 888 * Set a PAM environment string. We need to do this so that the session
5598e598 889 * modules can handle things like Kerberos/GSI credentials that appear
890 * during the ssh authentication process.
891 */
7cac2b65 892int
540d72c3 893do_pam_putenv(char *name, char *value)
7cac2b65 894{
895 int ret = 1;
540d72c3 896#ifdef HAVE_PAM_PUTENV
5598e598 897 char *compound;
7cac2b65 898 size_t len;
905081a4 899
7cac2b65 900 len = strlen(name) + strlen(value) + 2;
901 compound = xmalloc(len);
902
903 snprintf(compound, len, "%s=%s", name, value);
904 ret = pam_putenv(sshpam_handle, compound);
905 xfree(compound);
905081a4 906#endif
7cac2b65 907
908 return (ret);
5598e598 909}
910
540d72c3 911char **
912fetch_pam_child_environment(void)
3c0ef626 913{
540d72c3 914 return sshpam_env;
3c0ef626 915}
916
7cac2b65 917char **
918fetch_pam_environment(void)
3c0ef626 919{
7cac2b65 920 return (pam_getenvlist(sshpam_handle));
7cac2b65 921}
3c0ef626 922
7cac2b65 923void
924free_pam_environment(char **env)
925{
926 char **envp;
3c0ef626 927
7cac2b65 928 if (env == NULL)
929 return;
3c0ef626 930
7cac2b65 931 for (envp = env; *envp; envp++)
932 xfree(*envp);
933 xfree(env);
3c0ef626 934}
935
936#endif /* USE_PAM */
This page took 0.372204 seconds and 5 git commands to generate.