blob: 970ff61cb703ec3af1fa5df2a341b1f59e54c6fd [file] [log] [blame]
Damien Miller4f9f42a2003-05-10 19:28:02 +10001/*-
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.
Damien Millere69f18c2000-06-12 16:38:54 +10009 *
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.
Damien Millere69f18c2000-06-12 16:38:54 +100018 *
Damien Miller4f9f42a2003-05-10 19:28:02 +100019 * 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.
Damien Millere72b7af1999-12-30 15:08:44 +110030 */
31
Damien Miller25d93422003-05-18 20:45:47 +100032/* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
Damien Millere72b7af1999-12-30 15:08:44 +110033#include "includes.h"
Darren Tuckerf38db7f2003-08-08 13:43:37 +100034RCSID("$Id: auth-pam.c,v 1.66 2003/08/08 03:43:37 dtucker Exp $");
Damien Millere72b7af1999-12-30 15:08:44 +110035
36#ifdef USE_PAM
Damien Miller4f9f42a2003-05-10 19:28:02 +100037#include <security/pam_appl.h>
38
Kevin Stevese683e762002-04-04 19:02:28 +000039#include "auth.h"
Damien Miller63dc3e92001-02-07 12:58:33 +110040#include "auth-pam.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100041#include "buffer.h"
42#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "canohost.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100044#include "log.h"
45#include "monitor_wrap.h"
46#include "msg.h"
47#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "readpass.h"
Damien Miller4f9f42a2003-05-10 19:28:02 +100049#include "servconf.h"
50#include "ssh2.h"
51#include "xmalloc.h"
Damien Millere72b7af1999-12-30 15:08:44 +110052
Damien Miller4e448a32003-05-14 15:11:48 +100053extern ServerOptions options;
54
Damien Miller4f9f42a2003-05-10 19:28:02 +100055#define __unused
Kevin Steves85ecbe72001-04-20 17:43:47 +000056
Damien Miller4f9f42a2003-05-10 19:28:02 +100057#ifdef USE_POSIX_THREADS
58#include <pthread.h>
59/*
60 * Avoid namespace clash when *not* using pthreads for systems *with*
61 * pthreads, which unconditionally define pthread_t via sys/types.h
62 * (e.g. Linux)
63 */
64typedef pthread_t sp_pthread_t;
65#else
66/*
67 * Simulate threads with processes.
68 */
69typedef pid_t sp_pthread_t;
Kevin Steves63007d42002-07-21 17:57:01 +000070
Damien Miller4f9f42a2003-05-10 19:28:02 +100071static void
72pthread_exit(void *value __unused)
73{
74 _exit(0);
75}
Damien Miller2f6a0ad2000-05-31 11:20:11 +100076
Damien Miller4f9f42a2003-05-10 19:28:02 +100077static int
78pthread_create(sp_pthread_t *thread, const void *attr __unused,
79 void *(*thread_start)(void *), void *arg)
80{
81 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +110082
Damien Miller4f9f42a2003-05-10 19:28:02 +100083 switch ((pid = fork())) {
84 case -1:
85 error("fork(): %s", strerror(errno));
86 return (-1);
87 case 0:
88 thread_start(arg);
89 _exit(1);
90 default:
91 *thread = pid;
92 return (0);
93 }
94}
Damien Millere72b7af1999-12-30 15:08:44 +110095
Damien Miller4f9f42a2003-05-10 19:28:02 +100096static int
97pthread_cancel(sp_pthread_t thread)
98{
99 return (kill(thread, SIGTERM));
100}
101
102static int
103pthread_join(sp_pthread_t thread, void **value __unused)
104{
105 int status;
106
107 waitpid(thread, &status, 0);
108 return (status);
109}
110#endif
111
112
113static pam_handle_t *sshpam_handle;
114static int sshpam_err;
115static int sshpam_authenticated;
116static int sshpam_new_authtok_reqd;
117static int sshpam_session_open;
118static int sshpam_cred_established;
119
120struct pam_ctxt {
121 sp_pthread_t pam_thread;
122 int pam_psock;
123 int pam_csock;
124 int pam_done;
Damien Millere72b7af1999-12-30 15:08:44 +1100125};
Damien Millere72b7af1999-12-30 15:08:44 +1100126
Damien Miller4f9f42a2003-05-10 19:28:02 +1000127static void sshpam_free_ctx(void *);
Damien Miller9d5705a2000-09-16 16:09:27 +1100128
129/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000130 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100131 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000132static int
133sshpam_thread_conv(int n,
134 const struct pam_message **msg,
135 struct pam_response **resp,
136 void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100137{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000138 Buffer buffer;
139 struct pam_ctxt *ctxt;
Kevin Steves38b050a2002-07-23 00:44:07 +0000140 int i;
141
Damien Miller4f9f42a2003-05-10 19:28:02 +1000142 ctxt = data;
143 if (n <= 0 || n > PAM_MAX_NUM_MSG)
144 return (PAM_CONV_ERR);
145 *resp = xmalloc(n * sizeof **resp);
146 buffer_init(&buffer);
147 for (i = 0; i < n; ++i) {
148 resp[i]->resp_retcode = 0;
149 resp[i]->resp = NULL;
150 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
151 case PAM_PROMPT_ECHO_OFF:
152 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
153 ssh_msg_send(ctxt->pam_csock,
154 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
155 ssh_msg_recv(ctxt->pam_csock, &buffer);
156 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
157 goto fail;
158 resp[i]->resp = buffer_get_string(&buffer, NULL);
159 break;
160 case PAM_PROMPT_ECHO_ON:
161 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
162 ssh_msg_send(ctxt->pam_csock,
163 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
164 ssh_msg_recv(ctxt->pam_csock, &buffer);
165 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
166 goto fail;
167 resp[i]->resp = buffer_get_string(&buffer, NULL);
168 break;
169 case PAM_ERROR_MSG:
170 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
171 ssh_msg_send(ctxt->pam_csock,
172 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
173 break;
174 case PAM_TEXT_INFO:
175 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
176 ssh_msg_send(ctxt->pam_csock,
177 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
178 break;
179 default:
180 goto fail;
181 }
182 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000183 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000184 buffer_free(&buffer);
185 return (PAM_SUCCESS);
186 fail:
187 while (i)
188 xfree(resp[--i]);
189 xfree(*resp);
190 *resp = NULL;
191 buffer_free(&buffer);
192 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000193}
194
Damien Miller4f9f42a2003-05-10 19:28:02 +1000195/*
196 * Authentication thread.
197 */
198static void *
199sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100200{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000201 struct pam_ctxt *ctxt = ctxtp;
202 Buffer buffer;
203 struct pam_conv sshpam_conv = { sshpam_thread_conv, ctxt };
204#ifndef USE_POSIX_THREADS
205 const char *pam_user;
206
207 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
208 setproctitle("%s [pam]", pam_user);
209#endif
210
211 buffer_init(&buffer);
212 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
213 (const void *)&sshpam_conv);
214 if (sshpam_err != PAM_SUCCESS)
215 goto auth_fail;
216 sshpam_err = pam_authenticate(sshpam_handle, 0);
217 if (sshpam_err != PAM_SUCCESS)
218 goto auth_fail;
219 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
220 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD)
221 goto auth_fail;
222 buffer_put_cstring(&buffer, "OK");
223 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
224 buffer_free(&buffer);
225 pthread_exit(NULL);
226
227 auth_fail:
228 buffer_put_cstring(&buffer,
229 pam_strerror(sshpam_handle, sshpam_err));
230 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
231 buffer_free(&buffer);
232 pthread_exit(NULL);
233
234 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000235}
236
Damien Miller4f9f42a2003-05-10 19:28:02 +1000237static void
238sshpam_thread_cleanup(void *ctxtp)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000239{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000240 struct pam_ctxt *ctxt = ctxtp;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000241
Damien Miller4f9f42a2003-05-10 19:28:02 +1000242 pthread_cancel(ctxt->pam_thread);
243 pthread_join(ctxt->pam_thread, NULL);
244 close(ctxt->pam_psock);
245 close(ctxt->pam_csock);
246}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000247
Damien Miller4f9f42a2003-05-10 19:28:02 +1000248static int
249sshpam_null_conv(int n,
250 const struct pam_message **msg,
251 struct pam_response **resp,
252 void *data)
253{
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000254
Damien Miller4f9f42a2003-05-10 19:28:02 +1000255 return (PAM_CONV_ERR);
256}
Damien Miller63dc3e92001-02-07 12:58:33 +1100257
Damien Miller4f9f42a2003-05-10 19:28:02 +1000258static struct pam_conv null_conv = { sshpam_null_conv, NULL };
259
260static void
261sshpam_cleanup(void *arg)
262{
263 (void)arg;
264 debug("PAM: cleanup");
265 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
266 if (sshpam_cred_established) {
267 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
268 sshpam_cred_established = 0;
269 }
270 if (sshpam_session_open) {
271 pam_close_session(sshpam_handle, PAM_SILENT);
272 sshpam_session_open = 0;
273 }
274 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
275 pam_end(sshpam_handle, sshpam_err);
276 sshpam_handle = NULL;
277}
278
279static int
280sshpam_init(const char *user)
281{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000282 extern u_int utmp_len;
283 const char *pam_rhost, *pam_user;
284
285 if (sshpam_handle != NULL) {
286 /* We already have a PAM context; check if the user matches */
287 sshpam_err = pam_get_item(sshpam_handle,
288 PAM_USER, (const void **)&pam_user);
289 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
290 return (0);
291 fatal_remove_cleanup(sshpam_cleanup, NULL);
292 pam_end(sshpam_handle, sshpam_err);
293 sshpam_handle = NULL;
294 }
295 debug("PAM: initializing for \"%s\"", user);
296 sshpam_err = pam_start("sshd", user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000297 if (sshpam_err != PAM_SUCCESS) {
298 pam_end(sshpam_handle, sshpam_err);
299 sshpam_handle = NULL;
300 return (-1);
301 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000302 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000303 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000304 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
305 if (sshpam_err != PAM_SUCCESS) {
306 pam_end(sshpam_handle, sshpam_err);
307 sshpam_handle = NULL;
308 return (-1);
309 }
310#ifdef PAM_TTY_KLUDGE
311 /*
312 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
313 * sshd doesn't set the tty until too late in the auth process and
314 * may not even set one (for tty-less connections)
315 */
316 debug("PAM: setting PAM_TTY to \"ssh\"");
317 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
318 if (sshpam_err != PAM_SUCCESS) {
319 pam_end(sshpam_handle, sshpam_err);
320 sshpam_handle = NULL;
321 return (-1);
322 }
323#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000324 fatal_add_cleanup(sshpam_cleanup, NULL);
325 return (0);
326}
327
328static void *
329sshpam_init_ctx(Authctxt *authctxt)
330{
331 struct pam_ctxt *ctxt;
332 int socks[2];
333
Damien Miller4e448a32003-05-14 15:11:48 +1000334 /* Refuse to start if we don't have PAM enabled */
335 if (!options.use_pam)
336 return NULL;
337
Damien Miller4f9f42a2003-05-10 19:28:02 +1000338 /* Initialize PAM */
339 if (sshpam_init(authctxt->user) == -1) {
340 error("PAM: initialization failed");
341 return (NULL);
342 }
343
344 ctxt = xmalloc(sizeof *ctxt);
345 ctxt->pam_done = 0;
346
347 /* Start the authentication thread */
348 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
349 error("PAM: failed create sockets: %s", strerror(errno));
350 xfree(ctxt);
351 return (NULL);
352 }
353 ctxt->pam_psock = socks[0];
354 ctxt->pam_csock = socks[1];
355 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
356 error("PAM: failed to start authentication thread: %s",
357 strerror(errno));
358 close(socks[0]);
359 close(socks[1]);
360 xfree(ctxt);
361 return (NULL);
362 }
363 fatal_add_cleanup(sshpam_thread_cleanup, ctxt);
364 return (ctxt);
365}
366
367static int
368sshpam_query(void *ctx, char **name, char **info,
369 u_int *num, char ***prompts, u_int **echo_on)
370{
371 Buffer buffer;
372 struct pam_ctxt *ctxt = ctx;
373 size_t plen;
374 u_char type;
375 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000376 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000377
378 buffer_init(&buffer);
379 *name = xstrdup("");
380 *info = xstrdup("");
381 *prompts = xmalloc(sizeof(char *));
382 **prompts = NULL;
383 plen = 0;
384 *echo_on = xmalloc(sizeof(u_int));
385 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
386 type = buffer_get_char(&buffer);
387 msg = buffer_get_string(&buffer, NULL);
388 switch (type) {
389 case PAM_PROMPT_ECHO_ON:
390 case PAM_PROMPT_ECHO_OFF:
391 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000392 len = plen + strlen(msg) + 1;
393 **prompts = xrealloc(**prompts, len);
394 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000395 **echo_on = (type == PAM_PROMPT_ECHO_ON);
396 xfree(msg);
397 return (0);
398 case PAM_ERROR_MSG:
399 case PAM_TEXT_INFO:
400 /* accumulate messages */
Damien Miller7f2d7952003-07-30 14:53:11 +1000401 len = plen + strlen(msg) + 1;
402 **prompts = xrealloc(**prompts, len);
403 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000404 xfree(msg);
405 break;
406 case PAM_NEW_AUTHTOK_REQD:
407 sshpam_new_authtok_reqd = 1;
408 /* FALLTHROUGH */
409 case PAM_SUCCESS:
410 case PAM_AUTH_ERR:
411 if (**prompts != NULL) {
412 /* drain any accumulated messages */
413#if 0 /* XXX - not compatible with privsep */
414 packet_start(SSH2_MSG_USERAUTH_BANNER);
415 packet_put_cstring(**prompts);
416 packet_put_cstring("");
417 packet_send();
418 packet_write_wait();
419#endif
420 xfree(**prompts);
421 **prompts = NULL;
422 }
423 if (type == PAM_SUCCESS) {
424 *num = 0;
425 **echo_on = 0;
426 ctxt->pam_done = 1;
427 xfree(msg);
428 return (0);
429 }
430 error("PAM: %s", msg);
431 default:
432 *num = 0;
433 **echo_on = 0;
434 xfree(msg);
435 ctxt->pam_done = -1;
436 return (-1);
437 }
438 }
439 return (-1);
440}
441
442/* XXX - see also comment in auth-chall.c:verify_response */
443static int
444sshpam_respond(void *ctx, u_int num, char **resp)
445{
446 Buffer buffer;
447 struct pam_ctxt *ctxt = ctx;
448
449 debug2("PAM: %s", __func__);
450 switch (ctxt->pam_done) {
451 case 1:
452 sshpam_authenticated = 1;
453 return (0);
454 case 0:
455 break;
456 default:
457 return (-1);
458 }
459 if (num != 1) {
460 error("PAM: expected one response, got %u", num);
461 return (-1);
462 }
463 buffer_init(&buffer);
464 buffer_put_cstring(&buffer, *resp);
465 ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer);
466 buffer_free(&buffer);
467 return (1);
468}
469
470static void
471sshpam_free_ctx(void *ctxtp)
472{
473 struct pam_ctxt *ctxt = ctxtp;
474
475 fatal_remove_cleanup(sshpam_thread_cleanup, ctxt);
476 sshpam_thread_cleanup(ctxtp);
477 xfree(ctxt);
478 /*
479 * We don't call sshpam_cleanup() here because we may need the PAM
480 * handle at a later stage, e.g. when setting up a session. It's
481 * still on the cleanup list, so pam_end() *will* be called before
482 * the server process terminates.
483 */
484}
485
486KbdintDevice sshpam_device = {
487 "pam",
488 sshpam_init_ctx,
489 sshpam_query,
490 sshpam_respond,
491 sshpam_free_ctx
492};
493
494KbdintDevice mm_sshpam_device = {
495 "pam",
496 mm_sshpam_init_ctx,
497 mm_sshpam_query,
498 mm_sshpam_respond,
499 mm_sshpam_free_ctx
500};
501
502/*
503 * This replaces auth-pam.c
504 */
505void
506start_pam(const char *user)
507{
Damien Miller9d507da2003-05-14 15:31:12 +1000508 if (!options.use_pam)
509 fatal("PAM: initialisation requested when UsePAM=no");
510
Damien Miller4f9f42a2003-05-10 19:28:02 +1000511 if (sshpam_init(user) == -1)
512 fatal("PAM: initialisation failed");
513}
514
515void
516finish_pam(void)
517{
518 fatal_remove_cleanup(sshpam_cleanup, NULL);
519 sshpam_cleanup(NULL);
520}
521
522int
523do_pam_account(const char *user, const char *ruser)
524{
525 /* XXX */
526 return (1);
527}
528
529void
530do_pam_session(const char *user, const char *tty)
531{
532 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
533 (const void *)&null_conv);
534 if (sshpam_err != PAM_SUCCESS)
535 fatal("PAM: failed to set PAM_CONV: %s",
536 pam_strerror(sshpam_handle, sshpam_err));
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000537 if (tty != NULL) {
538 debug("PAM: setting PAM_TTY to \"%s\"", tty);
539 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
540 if (sshpam_err != PAM_SUCCESS)
541 fatal("PAM: failed to set PAM_TTY: %s",
542 pam_strerror(sshpam_handle, sshpam_err));
543 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000544 sshpam_err = pam_open_session(sshpam_handle, 0);
545 if (sshpam_err != PAM_SUCCESS)
546 fatal("PAM: pam_open_session(): %s",
547 pam_strerror(sshpam_handle, sshpam_err));
548 sshpam_session_open = 1;
549}
550
551void
552do_pam_setcred(int init)
553{
554 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
555 (const void *)&null_conv);
556 if (sshpam_err != PAM_SUCCESS)
557 fatal("PAM: failed to set PAM_CONV: %s",
558 pam_strerror(sshpam_handle, sshpam_err));
559 if (init) {
560 debug("PAM: establishing credentials");
561 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
562 } else {
563 debug("PAM: reinitializing credentials");
564 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
565 }
566 if (sshpam_err == PAM_SUCCESS) {
567 sshpam_cred_established = 1;
568 return;
569 }
570 if (sshpam_authenticated)
571 fatal("PAM: pam_setcred(): %s",
572 pam_strerror(sshpam_handle, sshpam_err));
573 else
574 debug("PAM: pam_setcred(): %s",
575 pam_strerror(sshpam_handle, sshpam_err));
576}
577
578int
579is_pam_password_change_required(void)
580{
581 return (sshpam_new_authtok_reqd);
582}
583
584static int
585pam_chauthtok_conv(int n,
586 const struct pam_message **msg,
587 struct pam_response **resp,
588 void *data)
589{
590 char input[PAM_MAX_MSG_SIZE];
591 int i;
592
593 if (n <= 0 || n > PAM_MAX_NUM_MSG)
594 return (PAM_CONV_ERR);
595 *resp = xmalloc(n * sizeof **resp);
596 for (i = 0; i < n; ++i) {
597 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
598 case PAM_PROMPT_ECHO_OFF:
599 resp[i]->resp =
600 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
601 RP_ALLOW_STDIN);
602 resp[i]->resp_retcode = PAM_SUCCESS;
603 break;
604 case PAM_PROMPT_ECHO_ON:
605 fputs(PAM_MSG_MEMBER(msg, i, msg), stderr);
606 fgets(input, sizeof input, stdin);
607 resp[i]->resp = xstrdup(input);
608 resp[i]->resp_retcode = PAM_SUCCESS;
609 break;
610 case PAM_ERROR_MSG:
611 case PAM_TEXT_INFO:
612 fputs(PAM_MSG_MEMBER(msg, i, msg), stderr);
613 resp[i]->resp_retcode = PAM_SUCCESS;
614 break;
615 default:
616 goto fail;
617 }
618 }
619 return (PAM_SUCCESS);
620 fail:
621 while (i)
622 xfree(resp[--i]);
623 xfree(*resp);
624 *resp = NULL;
625 return (PAM_CONV_ERR);
626}
627
628/*
629 * XXX this should be done in the authentication phase, but ssh1 doesn't
630 * support that
631 */
632void
633do_pam_chauthtok(void)
634{
635 struct pam_conv pam_conv = { pam_chauthtok_conv, NULL };
636
637 if (use_privsep)
638 fatal("PAM: chauthtok not supprted with privsep");
639 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
640 (const void *)&pam_conv);
641 if (sshpam_err != PAM_SUCCESS)
642 fatal("PAM: failed to set PAM_CONV: %s",
643 pam_strerror(sshpam_handle, sshpam_err));
644 debug("PAM: changing password");
645 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
646 if (sshpam_err != PAM_SUCCESS)
647 fatal("PAM: pam_chauthtok(): %s",
648 pam_strerror(sshpam_handle, sshpam_err));
649}
650
651void
652print_pam_messages(void)
653{
654 /* XXX */
655}
656
657char **
658fetch_pam_environment(void)
659{
660#ifdef HAVE_PAM_GETENVLIST
661 debug("PAM: retrieving environment");
662 return (pam_getenvlist(sshpam_handle));
663#else
664 return (NULL);
665#endif
666}
667
668void
669free_pam_environment(char **env)
670{
671 char **envp;
672
Damien Millere27c6cc2003-05-16 18:21:01 +1000673 if (env == NULL)
674 return;
675
Damien Miller4f9f42a2003-05-10 19:28:02 +1000676 for (envp = env; *envp; envp++)
677 xfree(*envp);
678 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100679}
680
681#endif /* USE_PAM */