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