blob: af8d37fab820fb638aef3c3c09eaefc118bd06ed [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 Tucker18df00c2003-11-18 12:42:07 +110034RCSID("$Id: auth-pam.c,v 1.81 2003/11/18 01:42:08 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;
Darren Tucker18df00c2003-11-18 12:42:07 +110055extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100056
Damien Miller4f9f42a2003-05-10 19:28:02 +100057#define __unused
Kevin Steves85ecbe72001-04-20 17:43:47 +000058
Damien Miller4f9f42a2003-05-10 19:28:02 +100059#ifdef USE_POSIX_THREADS
60#include <pthread.h>
61/*
62 * Avoid namespace clash when *not* using pthreads for systems *with*
63 * pthreads, which unconditionally define pthread_t via sys/types.h
64 * (e.g. Linux)
65 */
66typedef pthread_t sp_pthread_t;
67#else
68/*
69 * Simulate threads with processes.
70 */
71typedef pid_t sp_pthread_t;
Kevin Steves63007d42002-07-21 17:57:01 +000072
Damien Miller4f9f42a2003-05-10 19:28:02 +100073static void
74pthread_exit(void *value __unused)
75{
76 _exit(0);
77}
Damien Miller2f6a0ad2000-05-31 11:20:11 +100078
Damien Miller4f9f42a2003-05-10 19:28:02 +100079static int
80pthread_create(sp_pthread_t *thread, const void *attr __unused,
81 void *(*thread_start)(void *), void *arg)
82{
83 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +110084
Damien Miller4f9f42a2003-05-10 19:28:02 +100085 switch ((pid = fork())) {
86 case -1:
87 error("fork(): %s", strerror(errno));
88 return (-1);
89 case 0:
90 thread_start(arg);
91 _exit(1);
92 default:
93 *thread = pid;
94 return (0);
95 }
96}
Damien Millere72b7af1999-12-30 15:08:44 +110097
Damien Miller4f9f42a2003-05-10 19:28:02 +100098static int
99pthread_cancel(sp_pthread_t thread)
100{
101 return (kill(thread, SIGTERM));
102}
103
104static int
105pthread_join(sp_pthread_t thread, void **value __unused)
106{
107 int status;
108
109 waitpid(thread, &status, 0);
110 return (status);
111}
112#endif
113
114
Damien Miller5c3a5582003-09-23 22:12:38 +1000115static pam_handle_t *sshpam_handle = NULL;
116static int sshpam_err = 0;
117static int sshpam_authenticated = 0;
118static int sshpam_new_authtok_reqd = 0;
119static int sshpam_session_open = 0;
120static int sshpam_cred_established = 0;
Damien Millerc756e9b2003-11-17 21:41:42 +1100121static char **sshpam_env = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000122
123struct pam_ctxt {
124 sp_pthread_t pam_thread;
125 int pam_psock;
126 int pam_csock;
127 int pam_done;
Damien Millere72b7af1999-12-30 15:08:44 +1100128};
Damien Millere72b7af1999-12-30 15:08:44 +1100129
Damien Miller4f9f42a2003-05-10 19:28:02 +1000130static void sshpam_free_ctx(void *);
Darren Tucker8846a072003-10-07 11:30:15 +1000131static struct pam_ctxt *cleanup_ctxt;
Damien Miller9d5705a2000-09-16 16:09:27 +1100132
Damien Millerc756e9b2003-11-17 21:41:42 +1100133/* Some PAM implementations don't implement this */
134#ifndef HAVE_PAM_GETENVLIST
135static char **
136pam_getenvlist(pam_handle_t *pamh)
137{
138 /*
139 * XXX - If necessary, we can still support envrionment passing
140 * for platforms without pam_getenvlist by searching for known
141 * env vars (e.g. KRB5CCNAME) from the PAM environment.
142 */
143 return NULL;
144}
145#endif
146
147/* Import regular and PAM environment from subprocess */
148static void
149import_environments(Buffer *b)
150{
151 char *env;
152 u_int i, num_env;
153 int err;
154
155 /* Import environment from subprocess */
156 num_env = buffer_get_int(b);
157 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
158 debug3("PAM: num env strings %d", num_env);
159 for(i = 0; i < num_env; i++)
160 sshpam_env[i] = buffer_get_string(b, NULL);
161
162 sshpam_env[num_env] = NULL;
163
164 /* Import PAM environment from subprocess */
165 num_env = buffer_get_int(b);
166 debug("PAM: num PAM env strings %d", num_env);
167 for(i = 0; i < num_env; i++) {
168 env = buffer_get_string(b, NULL);
169
170 /* Errors are not fatal here */
171 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
172 error("PAM: pam_putenv: %s",
173 pam_strerror(sshpam_handle, sshpam_err));
174 }
175 }
176}
177
Damien Miller9d5705a2000-09-16 16:09:27 +1100178/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000179 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100180 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000181static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000182sshpam_thread_conv(int n, const struct pam_message **msg,
183 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100184{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000185 Buffer buffer;
186 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000187 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000188 int i;
189
Damien Miller5c3a5582003-09-23 22:12:38 +1000190 *resp = NULL;
191
Damien Miller4f9f42a2003-05-10 19:28:02 +1000192 ctxt = data;
193 if (n <= 0 || n > PAM_MAX_NUM_MSG)
194 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000195
196 if ((reply = malloc(n * sizeof(*reply))) == NULL)
197 return (PAM_CONV_ERR);
198 memset(reply, 0, n * sizeof(*reply));
199
Damien Miller4f9f42a2003-05-10 19:28:02 +1000200 buffer_init(&buffer);
201 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000202 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
203 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000204 buffer_put_cstring(&buffer,
205 PAM_MSG_MEMBER(msg, i, msg));
Damien Miller9bdba702003-11-17 21:27:55 +1100206 if (ssh_msg_send(ctxt->pam_csock,
207 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
208 goto fail;
209 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
210 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000211 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
212 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000213 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000214 break;
215 case PAM_PROMPT_ECHO_ON:
Damien Miller5c3a5582003-09-23 22:12:38 +1000216 buffer_put_cstring(&buffer,
217 PAM_MSG_MEMBER(msg, i, msg));
Damien Miller9bdba702003-11-17 21:27:55 +1100218 if (ssh_msg_send(ctxt->pam_csock,
219 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
220 goto fail;
221 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
222 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000223 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
224 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000225 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000226 break;
227 case PAM_ERROR_MSG:
Damien Miller5c3a5582003-09-23 22:12:38 +1000228 buffer_put_cstring(&buffer,
229 PAM_MSG_MEMBER(msg, i, msg));
Damien Miller9bdba702003-11-17 21:27:55 +1100230 if (ssh_msg_send(ctxt->pam_csock,
231 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
232 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000233 break;
234 case PAM_TEXT_INFO:
Damien Miller5c3a5582003-09-23 22:12:38 +1000235 buffer_put_cstring(&buffer,
236 PAM_MSG_MEMBER(msg, i, msg));
Damien Miller9bdba702003-11-17 21:27:55 +1100237 if (ssh_msg_send(ctxt->pam_csock,
238 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
239 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000240 break;
241 default:
242 goto fail;
243 }
244 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000245 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000246 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000247 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000248 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000249
Damien Miller4f9f42a2003-05-10 19:28:02 +1000250 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000251 for(i = 0; i < n; i++) {
252 if (reply[i].resp != NULL)
253 xfree(reply[i].resp);
254 }
255 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000256 buffer_free(&buffer);
257 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000258}
259
Damien Miller4f9f42a2003-05-10 19:28:02 +1000260/*
261 * Authentication thread.
262 */
263static void *
264sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100265{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000266 struct pam_ctxt *ctxt = ctxtp;
267 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000268 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000269#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100270 extern char **environ;
271 char **env_from_pam;
272 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000273 const char *pam_user;
274
275 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
276 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100277 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000278#endif
279
Damien Millerf4b6f102003-09-02 23:12:06 +1000280 sshpam_conv.conv = sshpam_thread_conv;
281 sshpam_conv.appdata_ptr = ctxt;
282
Damien Miller4f9f42a2003-05-10 19:28:02 +1000283 buffer_init(&buffer);
284 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
285 (const void *)&sshpam_conv);
286 if (sshpam_err != PAM_SUCCESS)
287 goto auth_fail;
288 sshpam_err = pam_authenticate(sshpam_handle, 0);
289 if (sshpam_err != PAM_SUCCESS)
290 goto auth_fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000291 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100292
293#ifndef USE_POSIX_THREADS
294 /* Export any environment strings set in child */
295 for(i = 0; environ[i] != NULL; i++)
296 ; /* Count */
297 buffer_put_int(&buffer, i);
298 for(i = 0; environ[i] != NULL; i++)
299 buffer_put_cstring(&buffer, environ[i]);
300
301 /* Export any environment strings set by PAM in child */
302 env_from_pam = pam_getenvlist(sshpam_handle);
303 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
304 ; /* Count */
305 buffer_put_int(&buffer, i);
306 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
307 buffer_put_cstring(&buffer, env_from_pam[i]);
308#endif /* USE_POSIX_THREADS */
309
Damien Miller9bdba702003-11-17 21:27:55 +1100310 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000311 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
312 buffer_free(&buffer);
313 pthread_exit(NULL);
314
315 auth_fail:
316 buffer_put_cstring(&buffer,
317 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100318 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000319 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
320 buffer_free(&buffer);
321 pthread_exit(NULL);
322
323 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000324}
325
Darren Tucker8846a072003-10-07 11:30:15 +1000326void
327sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000328{
Darren Tucker8846a072003-10-07 11:30:15 +1000329 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000330
Darren Tucker8846a072003-10-07 11:30:15 +1000331 if (ctxt != NULL && ctxt->pam_thread != 0) {
332 pthread_cancel(ctxt->pam_thread);
333 pthread_join(ctxt->pam_thread, NULL);
334 close(ctxt->pam_psock);
335 close(ctxt->pam_csock);
336 memset(ctxt, 0, sizeof(*ctxt));
337 cleanup_ctxt = NULL;
338 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000339}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000340
Damien Miller4f9f42a2003-05-10 19:28:02 +1000341static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000342sshpam_null_conv(int n, const struct pam_message **msg,
343 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000344{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000345 return (PAM_CONV_ERR);
346}
Damien Miller63dc3e92001-02-07 12:58:33 +1100347
Damien Miller4f9f42a2003-05-10 19:28:02 +1000348static struct pam_conv null_conv = { sshpam_null_conv, NULL };
349
Darren Tucker8846a072003-10-07 11:30:15 +1000350void
351sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000352{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000353 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000354 if (sshpam_handle == NULL)
355 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000356 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
357 if (sshpam_cred_established) {
358 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
359 sshpam_cred_established = 0;
360 }
361 if (sshpam_session_open) {
362 pam_close_session(sshpam_handle, PAM_SILENT);
363 sshpam_session_open = 0;
364 }
365 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
366 pam_end(sshpam_handle, sshpam_err);
367 sshpam_handle = NULL;
368}
369
370static int
371sshpam_init(const char *user)
372{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000373 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000374 extern char *__progname;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000375 const char *pam_rhost, *pam_user;
376
377 if (sshpam_handle != NULL) {
378 /* We already have a PAM context; check if the user matches */
379 sshpam_err = pam_get_item(sshpam_handle,
380 PAM_USER, (const void **)&pam_user);
381 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
382 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000383 pam_end(sshpam_handle, sshpam_err);
384 sshpam_handle = NULL;
385 }
386 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000387 sshpam_err =
388 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000389 if (sshpam_err != PAM_SUCCESS) {
390 pam_end(sshpam_handle, sshpam_err);
391 sshpam_handle = NULL;
392 return (-1);
393 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000394 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000395 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000396 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
397 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000398 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000399 sshpam_handle = NULL;
400 return (-1);
401 }
402#ifdef PAM_TTY_KLUDGE
403 /*
404 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
405 * sshd doesn't set the tty until too late in the auth process and
406 * may not even set one (for tty-less connections)
407 */
408 debug("PAM: setting PAM_TTY to \"ssh\"");
409 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
410 if (sshpam_err != PAM_SUCCESS) {
411 pam_end(sshpam_handle, sshpam_err);
412 sshpam_handle = NULL;
413 return (-1);
414 }
415#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000416 return (0);
417}
418
419static void *
420sshpam_init_ctx(Authctxt *authctxt)
421{
422 struct pam_ctxt *ctxt;
423 int socks[2];
424
Damien Miller4e448a32003-05-14 15:11:48 +1000425 /* Refuse to start if we don't have PAM enabled */
426 if (!options.use_pam)
427 return NULL;
428
Damien Miller4f9f42a2003-05-10 19:28:02 +1000429 /* Initialize PAM */
430 if (sshpam_init(authctxt->user) == -1) {
431 error("PAM: initialization failed");
432 return (NULL);
433 }
434
435 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000436 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000437
438 /* Start the authentication thread */
439 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
440 error("PAM: failed create sockets: %s", strerror(errno));
441 xfree(ctxt);
442 return (NULL);
443 }
444 ctxt->pam_psock = socks[0];
445 ctxt->pam_csock = socks[1];
446 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
447 error("PAM: failed to start authentication thread: %s",
448 strerror(errno));
449 close(socks[0]);
450 close(socks[1]);
451 xfree(ctxt);
452 return (NULL);
453 }
Darren Tucker8846a072003-10-07 11:30:15 +1000454 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000455 return (ctxt);
456}
457
458static int
459sshpam_query(void *ctx, char **name, char **info,
460 u_int *num, char ***prompts, u_int **echo_on)
461{
462 Buffer buffer;
463 struct pam_ctxt *ctxt = ctx;
464 size_t plen;
465 u_char type;
466 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000467 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000468
469 buffer_init(&buffer);
470 *name = xstrdup("");
471 *info = xstrdup("");
472 *prompts = xmalloc(sizeof(char *));
473 **prompts = NULL;
474 plen = 0;
475 *echo_on = xmalloc(sizeof(u_int));
476 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
477 type = buffer_get_char(&buffer);
478 msg = buffer_get_string(&buffer, NULL);
479 switch (type) {
480 case PAM_PROMPT_ECHO_ON:
481 case PAM_PROMPT_ECHO_OFF:
482 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000483 len = plen + strlen(msg) + 1;
484 **prompts = xrealloc(**prompts, len);
485 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000486 **echo_on = (type == PAM_PROMPT_ECHO_ON);
487 xfree(msg);
488 return (0);
489 case PAM_ERROR_MSG:
490 case PAM_TEXT_INFO:
491 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100492 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000493 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100494 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000495 xfree(msg);
496 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000497 case PAM_SUCCESS:
498 case PAM_AUTH_ERR:
499 if (**prompts != NULL) {
500 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100501 debug("PAM: %s", **prompts);
502 buffer_append(&loginmsg, **prompts,
503 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000504 xfree(**prompts);
505 **prompts = NULL;
506 }
507 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100508 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000509 *num = 0;
510 **echo_on = 0;
511 ctxt->pam_done = 1;
512 xfree(msg);
513 return (0);
514 }
515 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000516 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000517 default:
518 *num = 0;
519 **echo_on = 0;
520 xfree(msg);
521 ctxt->pam_done = -1;
522 return (-1);
523 }
524 }
525 return (-1);
526}
527
528/* XXX - see also comment in auth-chall.c:verify_response */
529static int
530sshpam_respond(void *ctx, u_int num, char **resp)
531{
532 Buffer buffer;
533 struct pam_ctxt *ctxt = ctx;
534
535 debug2("PAM: %s", __func__);
536 switch (ctxt->pam_done) {
537 case 1:
538 sshpam_authenticated = 1;
539 return (0);
540 case 0:
541 break;
542 default:
543 return (-1);
544 }
545 if (num != 1) {
546 error("PAM: expected one response, got %u", num);
547 return (-1);
548 }
549 buffer_init(&buffer);
550 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100551 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
552 buffer_free(&buffer);
553 return (-1);
554 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000555 buffer_free(&buffer);
556 return (1);
557}
558
559static void
560sshpam_free_ctx(void *ctxtp)
561{
562 struct pam_ctxt *ctxt = ctxtp;
563
Darren Tucker8846a072003-10-07 11:30:15 +1000564 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000565 xfree(ctxt);
566 /*
567 * We don't call sshpam_cleanup() here because we may need the PAM
568 * handle at a later stage, e.g. when setting up a session. It's
569 * still on the cleanup list, so pam_end() *will* be called before
570 * the server process terminates.
571 */
572}
573
574KbdintDevice sshpam_device = {
575 "pam",
576 sshpam_init_ctx,
577 sshpam_query,
578 sshpam_respond,
579 sshpam_free_ctx
580};
581
582KbdintDevice mm_sshpam_device = {
583 "pam",
584 mm_sshpam_init_ctx,
585 mm_sshpam_query,
586 mm_sshpam_respond,
587 mm_sshpam_free_ctx
588};
589
590/*
591 * This replaces auth-pam.c
592 */
593void
594start_pam(const char *user)
595{
Damien Miller9d507da2003-05-14 15:31:12 +1000596 if (!options.use_pam)
597 fatal("PAM: initialisation requested when UsePAM=no");
598
Damien Miller4f9f42a2003-05-10 19:28:02 +1000599 if (sshpam_init(user) == -1)
600 fatal("PAM: initialisation failed");
601}
602
603void
604finish_pam(void)
605{
Darren Tucker8846a072003-10-07 11:30:15 +1000606 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000607}
608
Damien Miller1f499fd2003-08-25 13:08:49 +1000609u_int
610do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000611{
Damien Miller1f499fd2003-08-25 13:08:49 +1000612 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
613 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
614
615 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD)
616 return (0);
617
618 if (sshpam_err == PAM_NEW_AUTHTOK_REQD) {
619 sshpam_new_authtok_reqd = 1;
620
621 /* Prevent forwardings until password changed */
622 no_port_forwarding_flag |= 2;
623 no_agent_forwarding_flag |= 2;
624 no_x11_forwarding_flag |= 2;
625 }
626
Damien Miller4f9f42a2003-05-10 19:28:02 +1000627 return (1);
628}
629
630void
Damien Miller341c6e62003-09-02 23:18:52 +1000631do_pam_set_tty(const char *tty)
632{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000633 if (tty != NULL) {
634 debug("PAM: setting PAM_TTY to \"%s\"", tty);
635 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
636 if (sshpam_err != PAM_SUCCESS)
637 fatal("PAM: failed to set PAM_TTY: %s",
638 pam_strerror(sshpam_handle, sshpam_err));
639 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000640}
641
642void
643do_pam_setcred(int init)
644{
645 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
646 (const void *)&null_conv);
647 if (sshpam_err != PAM_SUCCESS)
648 fatal("PAM: failed to set PAM_CONV: %s",
649 pam_strerror(sshpam_handle, sshpam_err));
650 if (init) {
651 debug("PAM: establishing credentials");
652 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
653 } else {
654 debug("PAM: reinitializing credentials");
655 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
656 }
657 if (sshpam_err == PAM_SUCCESS) {
658 sshpam_cred_established = 1;
659 return;
660 }
661 if (sshpam_authenticated)
662 fatal("PAM: pam_setcred(): %s",
663 pam_strerror(sshpam_handle, sshpam_err));
664 else
665 debug("PAM: pam_setcred(): %s",
666 pam_strerror(sshpam_handle, sshpam_err));
667}
668
669int
670is_pam_password_change_required(void)
671{
672 return (sshpam_new_authtok_reqd);
673}
674
675static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100676pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000677 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000678{
679 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000680 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000681 int i;
682
Damien Miller5c3a5582003-09-23 22:12:38 +1000683 *resp = NULL;
684
Darren Tucker18df00c2003-11-18 12:42:07 +1100685 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000686 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000687
688 if ((reply = malloc(n * sizeof(*reply))) == NULL)
689 return (PAM_CONV_ERR);
690 memset(reply, 0, n * sizeof(*reply));
691
Damien Miller4f9f42a2003-05-10 19:28:02 +1000692 for (i = 0; i < n; ++i) {
693 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
694 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000695 reply[i].resp =
Damien Miller4f9f42a2003-05-10 19:28:02 +1000696 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
697 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000698 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000699 break;
700 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100701 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000702 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000703 reply[i].resp = xstrdup(input);
704 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000705 break;
706 case PAM_ERROR_MSG:
707 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100708 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000709 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000710 break;
711 default:
712 goto fail;
713 }
714 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000715 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000716 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000717
Damien Miller4f9f42a2003-05-10 19:28:02 +1000718 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000719 for(i = 0; i < n; i++) {
720 if (reply[i].resp != NULL)
721 xfree(reply[i].resp);
722 }
723 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000724 return (PAM_CONV_ERR);
725}
726
Darren Tucker18df00c2003-11-18 12:42:07 +1100727static struct pam_conv tty_conv = { pam_tty_conv, NULL };
728
Damien Miller4f9f42a2003-05-10 19:28:02 +1000729/*
730 * XXX this should be done in the authentication phase, but ssh1 doesn't
731 * support that
732 */
733void
734do_pam_chauthtok(void)
735{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000736 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000737 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000738 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100739 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000740 if (sshpam_err != PAM_SUCCESS)
741 fatal("PAM: failed to set PAM_CONV: %s",
742 pam_strerror(sshpam_handle, sshpam_err));
743 debug("PAM: changing password");
744 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
745 if (sshpam_err != PAM_SUCCESS)
746 fatal("PAM: pam_chauthtok(): %s",
747 pam_strerror(sshpam_handle, sshpam_err));
748}
749
Darren Tucker18df00c2003-11-18 12:42:07 +1100750void
751do_pam_session(void)
752{
753 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
754 (const void *)&tty_conv);
755 if (sshpam_err != PAM_SUCCESS)
756 fatal("PAM: failed to set PAM_CONV: %s",
757 pam_strerror(sshpam_handle, sshpam_err));
758 sshpam_err = pam_open_session(sshpam_handle, 0);
759 if (sshpam_err != PAM_SUCCESS)
760 fatal("PAM: pam_open_session(): %s",
761 pam_strerror(sshpam_handle, sshpam_err));
762 sshpam_session_open = 1;
763}
764
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000765/*
766 * Set a PAM environment string. We need to do this so that the session
767 * modules can handle things like Kerberos/GSI credentials that appear
768 * during the ssh authentication process.
769 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000770int
771do_pam_putenv(char *name, char *value)
772{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000773 int ret = 1;
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000774#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000775 char *compound;
776 size_t len;
777
778 len = strlen(name) + strlen(value) + 2;
779 compound = xmalloc(len);
780
781 snprintf(compound, len, "%s=%s", name, value);
782 ret = pam_putenv(sshpam_handle, compound);
783 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000784#endif
Damien Millerf2728092003-09-17 07:24:25 +1000785
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000786 return (ret);
787}
788
Damien Miller4f9f42a2003-05-10 19:28:02 +1000789void
790print_pam_messages(void)
791{
792 /* XXX */
793}
794
795char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100796fetch_pam_child_environment(void)
797{
798 return sshpam_env;
799}
800
801char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000802fetch_pam_environment(void)
803{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000804 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000805}
806
807void
808free_pam_environment(char **env)
809{
810 char **envp;
811
Damien Millere27c6cc2003-05-16 18:21:01 +1000812 if (env == NULL)
813 return;
814
Damien Miller4f9f42a2003-05-10 19:28:02 +1000815 for (envp = env; *envp; envp++)
816 xfree(*envp);
817 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100818}
819
820#endif /* USE_PAM */