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