blob: 8cb78db115436014a101d200ffe432c94de32f45 [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 Tuckerc58c2ee2003-09-13 22:02:05 +100034RCSID("$Id: auth-pam.c,v 1.71 2003/09/13 12:02:05 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 Miller1f499fd2003-08-25 13:08:49 +100052#include "auth-options.h"
Damien Millere72b7af1999-12-30 15:08:44 +110053
Damien Miller4e448a32003-05-14 15:11:48 +100054extern ServerOptions options;
55
Damien Miller4f9f42a2003-05-10 19:28:02 +100056#define __unused
Kevin Steves85ecbe72001-04-20 17:43:47 +000057
Damien Miller4f9f42a2003-05-10 19:28:02 +100058#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;
Kevin Steves63007d42002-07-21 17:57:01 +000071
Damien Miller4f9f42a2003-05-10 19:28:02 +100072static void
73pthread_exit(void *value __unused)
74{
75 _exit(0);
76}
Damien Miller2f6a0ad2000-05-31 11:20:11 +100077
Damien Miller4f9f42a2003-05-10 19:28:02 +100078static int
79pthread_create(sp_pthread_t *thread, const void *attr __unused,
80 void *(*thread_start)(void *), void *arg)
81{
82 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +110083
Damien Miller4f9f42a2003-05-10 19:28:02 +100084 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}
Damien Millere72b7af1999-12-30 15:08:44 +110096
Damien Miller4f9f42a2003-05-10 19:28:02 +100097static int
98pthread_cancel(sp_pthread_t thread)
99{
100 return (kill(thread, SIGTERM));
101}
102
103static int
104pthread_join(sp_pthread_t thread, void **value __unused)
105{
106 int status;
107
108 waitpid(thread, &status, 0);
109 return (status);
110}
111#endif
112
113
114static pam_handle_t *sshpam_handle;
115static int sshpam_err;
116static int sshpam_authenticated;
117static int sshpam_new_authtok_reqd;
118static int sshpam_session_open;
119static int sshpam_cred_established;
120
121struct pam_ctxt {
122 sp_pthread_t pam_thread;
123 int pam_psock;
124 int pam_csock;
125 int pam_done;
Damien Millere72b7af1999-12-30 15:08:44 +1100126};
Damien Millere72b7af1999-12-30 15:08:44 +1100127
Damien Miller4f9f42a2003-05-10 19:28:02 +1000128static void sshpam_free_ctx(void *);
Damien Miller9d5705a2000-09-16 16:09:27 +1100129
130/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000131 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100132 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000133static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000134sshpam_thread_conv(int n, const struct pam_message **msg,
135 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100136{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000137 Buffer buffer;
138 struct pam_ctxt *ctxt;
Kevin Steves38b050a2002-07-23 00:44:07 +0000139 int i;
140
Damien Miller4f9f42a2003-05-10 19:28:02 +1000141 ctxt = data;
142 if (n <= 0 || n > PAM_MAX_NUM_MSG)
143 return (PAM_CONV_ERR);
144 *resp = xmalloc(n * sizeof **resp);
145 buffer_init(&buffer);
146 for (i = 0; i < n; ++i) {
147 resp[i]->resp_retcode = 0;
148 resp[i]->resp = NULL;
149 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
150 case PAM_PROMPT_ECHO_OFF:
151 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
152 ssh_msg_send(ctxt->pam_csock,
153 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
154 ssh_msg_recv(ctxt->pam_csock, &buffer);
155 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
156 goto fail;
157 resp[i]->resp = buffer_get_string(&buffer, NULL);
158 break;
159 case PAM_PROMPT_ECHO_ON:
160 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
161 ssh_msg_send(ctxt->pam_csock,
162 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
163 ssh_msg_recv(ctxt->pam_csock, &buffer);
164 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
165 goto fail;
166 resp[i]->resp = buffer_get_string(&buffer, NULL);
167 break;
168 case PAM_ERROR_MSG:
169 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
170 ssh_msg_send(ctxt->pam_csock,
171 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
172 break;
173 case PAM_TEXT_INFO:
174 buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg));
175 ssh_msg_send(ctxt->pam_csock,
176 PAM_MSG_MEMBER(msg, i, msg_style), &buffer);
177 break;
178 default:
179 goto fail;
180 }
181 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000182 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000183 buffer_free(&buffer);
184 return (PAM_SUCCESS);
185 fail:
186 while (i)
187 xfree(resp[--i]);
188 xfree(*resp);
189 *resp = NULL;
190 buffer_free(&buffer);
191 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000192}
193
Damien Miller4f9f42a2003-05-10 19:28:02 +1000194/*
195 * Authentication thread.
196 */
197static void *
198sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100199{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000200 struct pam_ctxt *ctxt = ctxtp;
201 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000202 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000203#ifndef USE_POSIX_THREADS
204 const char *pam_user;
205
206 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
207 setproctitle("%s [pam]", pam_user);
208#endif
209
Damien Millerf4b6f102003-09-02 23:12:06 +1000210 sshpam_conv.conv = sshpam_thread_conv;
211 sshpam_conv.appdata_ptr = ctxt;
212
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213 buffer_init(&buffer);
214 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
215 (const void *)&sshpam_conv);
216 if (sshpam_err != PAM_SUCCESS)
217 goto auth_fail;
218 sshpam_err = pam_authenticate(sshpam_handle, 0);
219 if (sshpam_err != PAM_SUCCESS)
220 goto auth_fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000221 buffer_put_cstring(&buffer, "OK");
222 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
223 buffer_free(&buffer);
224 pthread_exit(NULL);
225
226 auth_fail:
227 buffer_put_cstring(&buffer,
228 pam_strerror(sshpam_handle, sshpam_err));
229 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
230 buffer_free(&buffer);
231 pthread_exit(NULL);
232
233 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000234}
235
Damien Miller4f9f42a2003-05-10 19:28:02 +1000236static void
237sshpam_thread_cleanup(void *ctxtp)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000238{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000239 struct pam_ctxt *ctxt = ctxtp;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000240
Damien Miller4f9f42a2003-05-10 19:28:02 +1000241 pthread_cancel(ctxt->pam_thread);
242 pthread_join(ctxt->pam_thread, NULL);
243 close(ctxt->pam_psock);
244 close(ctxt->pam_csock);
245}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000246
Damien Miller4f9f42a2003-05-10 19:28:02 +1000247static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000248sshpam_null_conv(int n, const struct pam_message **msg,
249 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000250{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000251 return (PAM_CONV_ERR);
252}
Damien Miller63dc3e92001-02-07 12:58:33 +1100253
Damien Miller4f9f42a2003-05-10 19:28:02 +1000254static struct pam_conv null_conv = { sshpam_null_conv, NULL };
255
256static void
257sshpam_cleanup(void *arg)
258{
259 (void)arg;
260 debug("PAM: cleanup");
261 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
262 if (sshpam_cred_established) {
263 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
264 sshpam_cred_established = 0;
265 }
266 if (sshpam_session_open) {
267 pam_close_session(sshpam_handle, PAM_SILENT);
268 sshpam_session_open = 0;
269 }
270 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
271 pam_end(sshpam_handle, sshpam_err);
272 sshpam_handle = NULL;
273}
274
275static int
276sshpam_init(const char *user)
277{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000278 extern u_int utmp_len;
279 const char *pam_rhost, *pam_user;
280
281 if (sshpam_handle != NULL) {
282 /* We already have a PAM context; check if the user matches */
283 sshpam_err = pam_get_item(sshpam_handle,
284 PAM_USER, (const void **)&pam_user);
285 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
286 return (0);
287 fatal_remove_cleanup(sshpam_cleanup, NULL);
288 pam_end(sshpam_handle, sshpam_err);
289 sshpam_handle = NULL;
290 }
291 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000292 sshpam_err =
293 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000294 if (sshpam_err != PAM_SUCCESS) {
295 pam_end(sshpam_handle, sshpam_err);
296 sshpam_handle = NULL;
297 return (-1);
298 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000299 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000300 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000301 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
302 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000303 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000304 sshpam_handle = NULL;
305 return (-1);
306 }
307#ifdef PAM_TTY_KLUDGE
308 /*
309 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
310 * sshd doesn't set the tty until too late in the auth process and
311 * may not even set one (for tty-less connections)
312 */
313 debug("PAM: setting PAM_TTY to \"ssh\"");
314 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
315 if (sshpam_err != PAM_SUCCESS) {
316 pam_end(sshpam_handle, sshpam_err);
317 sshpam_handle = NULL;
318 return (-1);
319 }
320#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000321 fatal_add_cleanup(sshpam_cleanup, NULL);
322 return (0);
323}
324
325static void *
326sshpam_init_ctx(Authctxt *authctxt)
327{
328 struct pam_ctxt *ctxt;
329 int socks[2];
330
Damien Miller4e448a32003-05-14 15:11:48 +1000331 /* Refuse to start if we don't have PAM enabled */
332 if (!options.use_pam)
333 return NULL;
334
Damien Miller4f9f42a2003-05-10 19:28:02 +1000335 /* Initialize PAM */
336 if (sshpam_init(authctxt->user) == -1) {
337 error("PAM: initialization failed");
338 return (NULL);
339 }
340
341 ctxt = xmalloc(sizeof *ctxt);
342 ctxt->pam_done = 0;
343
344 /* Start the authentication thread */
345 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
346 error("PAM: failed create sockets: %s", strerror(errno));
347 xfree(ctxt);
348 return (NULL);
349 }
350 ctxt->pam_psock = socks[0];
351 ctxt->pam_csock = socks[1];
352 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
353 error("PAM: failed to start authentication thread: %s",
354 strerror(errno));
355 close(socks[0]);
356 close(socks[1]);
357 xfree(ctxt);
358 return (NULL);
359 }
360 fatal_add_cleanup(sshpam_thread_cleanup, ctxt);
361 return (ctxt);
362}
363
364static int
365sshpam_query(void *ctx, char **name, char **info,
366 u_int *num, char ***prompts, u_int **echo_on)
367{
368 Buffer buffer;
369 struct pam_ctxt *ctxt = ctx;
370 size_t plen;
371 u_char type;
372 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000373 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000374
375 buffer_init(&buffer);
376 *name = xstrdup("");
377 *info = xstrdup("");
378 *prompts = xmalloc(sizeof(char *));
379 **prompts = NULL;
380 plen = 0;
381 *echo_on = xmalloc(sizeof(u_int));
382 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
383 type = buffer_get_char(&buffer);
384 msg = buffer_get_string(&buffer, NULL);
385 switch (type) {
386 case PAM_PROMPT_ECHO_ON:
387 case PAM_PROMPT_ECHO_OFF:
388 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000389 len = plen + strlen(msg) + 1;
390 **prompts = xrealloc(**prompts, len);
391 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000392 **echo_on = (type == PAM_PROMPT_ECHO_ON);
393 xfree(msg);
394 return (0);
395 case PAM_ERROR_MSG:
396 case PAM_TEXT_INFO:
397 /* accumulate messages */
Damien Miller7f2d7952003-07-30 14:53:11 +1000398 len = plen + strlen(msg) + 1;
399 **prompts = xrealloc(**prompts, len);
400 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000401 xfree(msg);
402 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000403 case PAM_SUCCESS:
404 case PAM_AUTH_ERR:
405 if (**prompts != NULL) {
406 /* drain any accumulated messages */
407#if 0 /* XXX - not compatible with privsep */
408 packet_start(SSH2_MSG_USERAUTH_BANNER);
409 packet_put_cstring(**prompts);
410 packet_put_cstring("");
411 packet_send();
412 packet_write_wait();
413#endif
414 xfree(**prompts);
415 **prompts = NULL;
416 }
417 if (type == PAM_SUCCESS) {
418 *num = 0;
419 **echo_on = 0;
420 ctxt->pam_done = 1;
421 xfree(msg);
422 return (0);
423 }
424 error("PAM: %s", msg);
425 default:
426 *num = 0;
427 **echo_on = 0;
428 xfree(msg);
429 ctxt->pam_done = -1;
430 return (-1);
431 }
432 }
433 return (-1);
434}
435
436/* XXX - see also comment in auth-chall.c:verify_response */
437static int
438sshpam_respond(void *ctx, u_int num, char **resp)
439{
440 Buffer buffer;
441 struct pam_ctxt *ctxt = ctx;
442
443 debug2("PAM: %s", __func__);
444 switch (ctxt->pam_done) {
445 case 1:
446 sshpam_authenticated = 1;
447 return (0);
448 case 0:
449 break;
450 default:
451 return (-1);
452 }
453 if (num != 1) {
454 error("PAM: expected one response, got %u", num);
455 return (-1);
456 }
457 buffer_init(&buffer);
458 buffer_put_cstring(&buffer, *resp);
459 ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer);
460 buffer_free(&buffer);
461 return (1);
462}
463
464static void
465sshpam_free_ctx(void *ctxtp)
466{
467 struct pam_ctxt *ctxt = ctxtp;
468
469 fatal_remove_cleanup(sshpam_thread_cleanup, ctxt);
470 sshpam_thread_cleanup(ctxtp);
471 xfree(ctxt);
472 /*
473 * We don't call sshpam_cleanup() here because we may need the PAM
474 * handle at a later stage, e.g. when setting up a session. It's
475 * still on the cleanup list, so pam_end() *will* be called before
476 * the server process terminates.
477 */
478}
479
480KbdintDevice sshpam_device = {
481 "pam",
482 sshpam_init_ctx,
483 sshpam_query,
484 sshpam_respond,
485 sshpam_free_ctx
486};
487
488KbdintDevice mm_sshpam_device = {
489 "pam",
490 mm_sshpam_init_ctx,
491 mm_sshpam_query,
492 mm_sshpam_respond,
493 mm_sshpam_free_ctx
494};
495
496/*
497 * This replaces auth-pam.c
498 */
499void
500start_pam(const char *user)
501{
Damien Miller9d507da2003-05-14 15:31:12 +1000502 if (!options.use_pam)
503 fatal("PAM: initialisation requested when UsePAM=no");
504
Damien Miller4f9f42a2003-05-10 19:28:02 +1000505 if (sshpam_init(user) == -1)
506 fatal("PAM: initialisation failed");
507}
508
509void
510finish_pam(void)
511{
512 fatal_remove_cleanup(sshpam_cleanup, NULL);
513 sshpam_cleanup(NULL);
514}
515
Damien Miller1f499fd2003-08-25 13:08:49 +1000516u_int
517do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000518{
Damien Miller1f499fd2003-08-25 13:08:49 +1000519 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
520 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
521
522 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD)
523 return (0);
524
525 if (sshpam_err == PAM_NEW_AUTHTOK_REQD) {
526 sshpam_new_authtok_reqd = 1;
527
528 /* Prevent forwardings until password changed */
529 no_port_forwarding_flag |= 2;
530 no_agent_forwarding_flag |= 2;
531 no_x11_forwarding_flag |= 2;
532 }
533
Damien Miller4f9f42a2003-05-10 19:28:02 +1000534 return (1);
535}
536
537void
Damien Miller341c6e62003-09-02 23:18:52 +1000538do_pam_session(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000539{
540 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
541 (const void *)&null_conv);
542 if (sshpam_err != PAM_SUCCESS)
543 fatal("PAM: failed to set PAM_CONV: %s",
544 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller341c6e62003-09-02 23:18:52 +1000545 sshpam_err = pam_open_session(sshpam_handle, 0);
546 if (sshpam_err != PAM_SUCCESS)
547 fatal("PAM: pam_open_session(): %s",
548 pam_strerror(sshpam_handle, sshpam_err));
549 sshpam_session_open = 1;
550}
551
552void
553do_pam_set_tty(const char *tty)
554{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000555 if (tty != NULL) {
556 debug("PAM: setting PAM_TTY to \"%s\"", tty);
557 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
558 if (sshpam_err != PAM_SUCCESS)
559 fatal("PAM: failed to set PAM_TTY: %s",
560 pam_strerror(sshpam_handle, sshpam_err));
561 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000562}
563
564void
565do_pam_setcred(int init)
566{
567 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
568 (const void *)&null_conv);
569 if (sshpam_err != PAM_SUCCESS)
570 fatal("PAM: failed to set PAM_CONV: %s",
571 pam_strerror(sshpam_handle, sshpam_err));
572 if (init) {
573 debug("PAM: establishing credentials");
574 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
575 } else {
576 debug("PAM: reinitializing credentials");
577 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
578 }
579 if (sshpam_err == PAM_SUCCESS) {
580 sshpam_cred_established = 1;
581 return;
582 }
583 if (sshpam_authenticated)
584 fatal("PAM: pam_setcred(): %s",
585 pam_strerror(sshpam_handle, sshpam_err));
586 else
587 debug("PAM: pam_setcred(): %s",
588 pam_strerror(sshpam_handle, sshpam_err));
589}
590
591int
592is_pam_password_change_required(void)
593{
594 return (sshpam_new_authtok_reqd);
595}
596
597static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000598pam_chauthtok_conv(int n, const struct pam_message **msg,
599 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000600{
601 char input[PAM_MAX_MSG_SIZE];
602 int i;
603
604 if (n <= 0 || n > PAM_MAX_NUM_MSG)
605 return (PAM_CONV_ERR);
606 *resp = xmalloc(n * sizeof **resp);
607 for (i = 0; i < n; ++i) {
608 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
609 case PAM_PROMPT_ECHO_OFF:
610 resp[i]->resp =
611 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
612 RP_ALLOW_STDIN);
613 resp[i]->resp_retcode = PAM_SUCCESS;
614 break;
615 case PAM_PROMPT_ECHO_ON:
616 fputs(PAM_MSG_MEMBER(msg, i, msg), stderr);
617 fgets(input, sizeof input, stdin);
618 resp[i]->resp = xstrdup(input);
619 resp[i]->resp_retcode = PAM_SUCCESS;
620 break;
621 case PAM_ERROR_MSG:
622 case PAM_TEXT_INFO:
623 fputs(PAM_MSG_MEMBER(msg, i, msg), stderr);
624 resp[i]->resp_retcode = PAM_SUCCESS;
625 break;
626 default:
627 goto fail;
628 }
629 }
630 return (PAM_SUCCESS);
631 fail:
632 while (i)
633 xfree(resp[--i]);
634 xfree(*resp);
635 *resp = NULL;
636 return (PAM_CONV_ERR);
637}
638
639/*
640 * XXX this should be done in the authentication phase, but ssh1 doesn't
641 * support that
642 */
643void
644do_pam_chauthtok(void)
645{
Damien Millerf4b6f102003-09-02 23:12:06 +1000646 struct pam_conv pam_conv;
647
648 pam_conv.conv = pam_chauthtok_conv;
649 pam_conv.appdata_ptr = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000650
651 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000652 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000653 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
654 (const void *)&pam_conv);
655 if (sshpam_err != PAM_SUCCESS)
656 fatal("PAM: failed to set PAM_CONV: %s",
657 pam_strerror(sshpam_handle, sshpam_err));
658 debug("PAM: changing password");
659 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
660 if (sshpam_err != PAM_SUCCESS)
661 fatal("PAM: pam_chauthtok(): %s",
662 pam_strerror(sshpam_handle, sshpam_err));
663}
664
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000665/*
666 * Set a PAM environment string. We need to do this so that the session
667 * modules can handle things like Kerberos/GSI credentials that appear
668 * during the ssh authentication process.
669 */
670
671int
672do_pam_putenv(char *name, char *value)
673{
674 char *compound;
675 int ret = 1;
676
677#ifdef HAVE_PAM_PUTENV
678 compound = xmalloc(strlen(name)+strlen(value)+2);
679 if (compound) {
680 sprintf(compound,"%s=%s",name,value);
681 ret = pam_putenv(sshpam_handle,compound);
682 xfree(compound);
683 }
684#endif
685 return (ret);
686}
687
Damien Miller4f9f42a2003-05-10 19:28:02 +1000688void
689print_pam_messages(void)
690{
691 /* XXX */
692}
693
694char **
695fetch_pam_environment(void)
696{
697#ifdef HAVE_PAM_GETENVLIST
698 debug("PAM: retrieving environment");
699 return (pam_getenvlist(sshpam_handle));
700#else
701 return (NULL);
702#endif
703}
704
705void
706free_pam_environment(char **env)
707{
708 char **envp;
709
Damien Millere27c6cc2003-05-16 18:21:01 +1000710 if (env == NULL)
711 return;
712
Damien Miller4f9f42a2003-05-10 19:28:02 +1000713 for (envp = env; *envp; envp++)
714 xfree(*envp);
715 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100716}
717
718#endif /* USE_PAM */