blob: 92a3da40674d56289747755a9f9d1e9befacda47 [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"
Damien Millerc756e9b2003-11-17 21:41:42 +110034RCSID("$Id: auth-pam.c,v 1.80 2003/11/17 10:41:42 djm 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
Damien Miller5c3a5582003-09-23 22:12:38 +1000114static 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;
Damien Millerc756e9b2003-11-17 21:41:42 +1100120static char **sshpam_env = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000121
122struct pam_ctxt {
123 sp_pthread_t pam_thread;
124 int pam_psock;
125 int pam_csock;
126 int pam_done;
Damien Millere72b7af1999-12-30 15:08:44 +1100127};
Damien Millere72b7af1999-12-30 15:08:44 +1100128
Damien Miller4f9f42a2003-05-10 19:28:02 +1000129static void sshpam_free_ctx(void *);
Darren Tucker8846a072003-10-07 11:30:15 +1000130static struct pam_ctxt *cleanup_ctxt;
Damien Miller9d5705a2000-09-16 16:09:27 +1100131
Damien Millerc756e9b2003-11-17 21:41:42 +1100132/* Some PAM implementations don't implement this */
133#ifndef HAVE_PAM_GETENVLIST
134static char **
135pam_getenvlist(pam_handle_t *pamh)
136{
137 /*
138 * XXX - If necessary, we can still support envrionment passing
139 * for platforms without pam_getenvlist by searching for known
140 * env vars (e.g. KRB5CCNAME) from the PAM environment.
141 */
142 return NULL;
143}
144#endif
145
146/* Import regular and PAM environment from subprocess */
147static void
148import_environments(Buffer *b)
149{
150 char *env;
151 u_int i, num_env;
152 int err;
153
154 /* Import environment from subprocess */
155 num_env = buffer_get_int(b);
156 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
157 debug3("PAM: num env strings %d", num_env);
158 for(i = 0; i < num_env; i++)
159 sshpam_env[i] = buffer_get_string(b, NULL);
160
161 sshpam_env[num_env] = NULL;
162
163 /* Import PAM environment from subprocess */
164 num_env = buffer_get_int(b);
165 debug("PAM: num PAM env strings %d", num_env);
166 for(i = 0; i < num_env; i++) {
167 env = buffer_get_string(b, NULL);
168
169 /* Errors are not fatal here */
170 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
171 error("PAM: pam_putenv: %s",
172 pam_strerror(sshpam_handle, sshpam_err));
173 }
174 }
175}
176
Damien Miller9d5705a2000-09-16 16:09:27 +1100177/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000178 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100179 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000180static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000181sshpam_thread_conv(int n, const struct pam_message **msg,
182 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100183{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000184 Buffer buffer;
185 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000186 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000187 int i;
188
Damien Miller5c3a5582003-09-23 22:12:38 +1000189 *resp = NULL;
190
Damien Miller4f9f42a2003-05-10 19:28:02 +1000191 ctxt = data;
192 if (n <= 0 || n > PAM_MAX_NUM_MSG)
193 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000194
195 if ((reply = malloc(n * sizeof(*reply))) == NULL)
196 return (PAM_CONV_ERR);
197 memset(reply, 0, n * sizeof(*reply));
198
Damien Miller4f9f42a2003-05-10 19:28:02 +1000199 buffer_init(&buffer);
200 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000201 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
202 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000203 buffer_put_cstring(&buffer,
204 PAM_MSG_MEMBER(msg, i, msg));
Damien Miller9bdba702003-11-17 21:27:55 +1100205 if (ssh_msg_send(ctxt->pam_csock,
206 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
207 goto fail;
208 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
209 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000210 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
211 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000212 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213 break;
214 case PAM_PROMPT_ECHO_ON:
Damien Miller5c3a5582003-09-23 22:12:38 +1000215 buffer_put_cstring(&buffer,
216 PAM_MSG_MEMBER(msg, i, msg));
Damien Miller9bdba702003-11-17 21:27:55 +1100217 if (ssh_msg_send(ctxt->pam_csock,
218 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
219 goto fail;
220 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
221 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000222 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
223 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000224 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000225 break;
226 case PAM_ERROR_MSG:
Damien Miller5c3a5582003-09-23 22:12:38 +1000227 buffer_put_cstring(&buffer,
228 PAM_MSG_MEMBER(msg, i, msg));
Damien Miller9bdba702003-11-17 21:27:55 +1100229 if (ssh_msg_send(ctxt->pam_csock,
230 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
231 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000232 break;
233 case PAM_TEXT_INFO:
Damien Miller5c3a5582003-09-23 22:12:38 +1000234 buffer_put_cstring(&buffer,
235 PAM_MSG_MEMBER(msg, i, msg));
Damien Miller9bdba702003-11-17 21:27:55 +1100236 if (ssh_msg_send(ctxt->pam_csock,
237 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
238 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000239 break;
240 default:
241 goto fail;
242 }
243 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000244 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000245 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000246 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000247 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000248
Damien Miller4f9f42a2003-05-10 19:28:02 +1000249 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000250 for(i = 0; i < n; i++) {
251 if (reply[i].resp != NULL)
252 xfree(reply[i].resp);
253 }
254 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000255 buffer_free(&buffer);
256 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000257}
258
Damien Miller4f9f42a2003-05-10 19:28:02 +1000259/*
260 * Authentication thread.
261 */
262static void *
263sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100264{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000265 struct pam_ctxt *ctxt = ctxtp;
266 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000267 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000268#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100269 extern char **environ;
270 char **env_from_pam;
271 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000272 const char *pam_user;
273
274 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
275 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100276 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000277#endif
278
Damien Millerf4b6f102003-09-02 23:12:06 +1000279 sshpam_conv.conv = sshpam_thread_conv;
280 sshpam_conv.appdata_ptr = ctxt;
281
Damien Miller4f9f42a2003-05-10 19:28:02 +1000282 buffer_init(&buffer);
283 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
284 (const void *)&sshpam_conv);
285 if (sshpam_err != PAM_SUCCESS)
286 goto auth_fail;
287 sshpam_err = pam_authenticate(sshpam_handle, 0);
288 if (sshpam_err != PAM_SUCCESS)
289 goto auth_fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000290 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100291
292#ifndef USE_POSIX_THREADS
293 /* Export any environment strings set in child */
294 for(i = 0; environ[i] != NULL; i++)
295 ; /* Count */
296 buffer_put_int(&buffer, i);
297 for(i = 0; environ[i] != NULL; i++)
298 buffer_put_cstring(&buffer, environ[i]);
299
300 /* Export any environment strings set by PAM in child */
301 env_from_pam = pam_getenvlist(sshpam_handle);
302 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
303 ; /* Count */
304 buffer_put_int(&buffer, i);
305 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
306 buffer_put_cstring(&buffer, env_from_pam[i]);
307#endif /* USE_POSIX_THREADS */
308
Damien Miller9bdba702003-11-17 21:27:55 +1100309 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000310 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
311 buffer_free(&buffer);
312 pthread_exit(NULL);
313
314 auth_fail:
315 buffer_put_cstring(&buffer,
316 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100317 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000318 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
319 buffer_free(&buffer);
320 pthread_exit(NULL);
321
322 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000323}
324
Darren Tucker8846a072003-10-07 11:30:15 +1000325void
326sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000327{
Darren Tucker8846a072003-10-07 11:30:15 +1000328 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000329
Darren Tucker8846a072003-10-07 11:30:15 +1000330 if (ctxt != NULL && ctxt->pam_thread != 0) {
331 pthread_cancel(ctxt->pam_thread);
332 pthread_join(ctxt->pam_thread, NULL);
333 close(ctxt->pam_psock);
334 close(ctxt->pam_csock);
335 memset(ctxt, 0, sizeof(*ctxt));
336 cleanup_ctxt = NULL;
337 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000338}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000339
Damien Miller4f9f42a2003-05-10 19:28:02 +1000340static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000341sshpam_null_conv(int n, const struct pam_message **msg,
342 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000343{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000344 return (PAM_CONV_ERR);
345}
Damien Miller63dc3e92001-02-07 12:58:33 +1100346
Damien Miller4f9f42a2003-05-10 19:28:02 +1000347static struct pam_conv null_conv = { sshpam_null_conv, NULL };
348
Darren Tucker8846a072003-10-07 11:30:15 +1000349void
350sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000351{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000352 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000353 if (sshpam_handle == NULL)
354 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000355 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
356 if (sshpam_cred_established) {
357 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
358 sshpam_cred_established = 0;
359 }
360 if (sshpam_session_open) {
361 pam_close_session(sshpam_handle, PAM_SILENT);
362 sshpam_session_open = 0;
363 }
364 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
365 pam_end(sshpam_handle, sshpam_err);
366 sshpam_handle = NULL;
367}
368
369static int
370sshpam_init(const char *user)
371{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000372 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000373 extern char *__progname;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000374 const char *pam_rhost, *pam_user;
375
376 if (sshpam_handle != NULL) {
377 /* We already have a PAM context; check if the user matches */
378 sshpam_err = pam_get_item(sshpam_handle,
379 PAM_USER, (const void **)&pam_user);
380 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
381 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000382 pam_end(sshpam_handle, sshpam_err);
383 sshpam_handle = NULL;
384 }
385 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000386 sshpam_err =
387 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000388 if (sshpam_err != PAM_SUCCESS) {
389 pam_end(sshpam_handle, sshpam_err);
390 sshpam_handle = NULL;
391 return (-1);
392 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000393 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000394 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000395 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
396 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000397 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000398 sshpam_handle = NULL;
399 return (-1);
400 }
401#ifdef PAM_TTY_KLUDGE
402 /*
403 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
404 * sshd doesn't set the tty until too late in the auth process and
405 * may not even set one (for tty-less connections)
406 */
407 debug("PAM: setting PAM_TTY to \"ssh\"");
408 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
409 if (sshpam_err != PAM_SUCCESS) {
410 pam_end(sshpam_handle, sshpam_err);
411 sshpam_handle = NULL;
412 return (-1);
413 }
414#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000415 return (0);
416}
417
418static void *
419sshpam_init_ctx(Authctxt *authctxt)
420{
421 struct pam_ctxt *ctxt;
422 int socks[2];
423
Damien Miller4e448a32003-05-14 15:11:48 +1000424 /* Refuse to start if we don't have PAM enabled */
425 if (!options.use_pam)
426 return NULL;
427
Damien Miller4f9f42a2003-05-10 19:28:02 +1000428 /* Initialize PAM */
429 if (sshpam_init(authctxt->user) == -1) {
430 error("PAM: initialization failed");
431 return (NULL);
432 }
433
434 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000435 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000436
437 /* Start the authentication thread */
438 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
439 error("PAM: failed create sockets: %s", strerror(errno));
440 xfree(ctxt);
441 return (NULL);
442 }
443 ctxt->pam_psock = socks[0];
444 ctxt->pam_csock = socks[1];
445 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
446 error("PAM: failed to start authentication thread: %s",
447 strerror(errno));
448 close(socks[0]);
449 close(socks[1]);
450 xfree(ctxt);
451 return (NULL);
452 }
Darren Tucker8846a072003-10-07 11:30:15 +1000453 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000454 return (ctxt);
455}
456
457static int
458sshpam_query(void *ctx, char **name, char **info,
459 u_int *num, char ***prompts, u_int **echo_on)
460{
461 Buffer buffer;
462 struct pam_ctxt *ctxt = ctx;
463 size_t plen;
464 u_char type;
465 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000466 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000467
468 buffer_init(&buffer);
469 *name = xstrdup("");
470 *info = xstrdup("");
471 *prompts = xmalloc(sizeof(char *));
472 **prompts = NULL;
473 plen = 0;
474 *echo_on = xmalloc(sizeof(u_int));
475 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
476 type = buffer_get_char(&buffer);
477 msg = buffer_get_string(&buffer, NULL);
478 switch (type) {
479 case PAM_PROMPT_ECHO_ON:
480 case PAM_PROMPT_ECHO_OFF:
481 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000482 len = plen + strlen(msg) + 1;
483 **prompts = xrealloc(**prompts, len);
484 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000485 **echo_on = (type == PAM_PROMPT_ECHO_ON);
486 xfree(msg);
487 return (0);
488 case PAM_ERROR_MSG:
489 case PAM_TEXT_INFO:
490 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100491 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000492 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100493 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000494 xfree(msg);
495 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000496 case PAM_SUCCESS:
497 case PAM_AUTH_ERR:
498 if (**prompts != NULL) {
499 /* drain any accumulated messages */
500#if 0 /* XXX - not compatible with privsep */
501 packet_start(SSH2_MSG_USERAUTH_BANNER);
502 packet_put_cstring(**prompts);
503 packet_put_cstring("");
504 packet_send();
505 packet_write_wait();
506#endif
507 xfree(**prompts);
508 **prompts = NULL;
509 }
510 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100511 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000512 *num = 0;
513 **echo_on = 0;
514 ctxt->pam_done = 1;
515 xfree(msg);
516 return (0);
517 }
518 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000519 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000520 default:
521 *num = 0;
522 **echo_on = 0;
523 xfree(msg);
524 ctxt->pam_done = -1;
525 return (-1);
526 }
527 }
528 return (-1);
529}
530
531/* XXX - see also comment in auth-chall.c:verify_response */
532static int
533sshpam_respond(void *ctx, u_int num, char **resp)
534{
535 Buffer buffer;
536 struct pam_ctxt *ctxt = ctx;
537
538 debug2("PAM: %s", __func__);
539 switch (ctxt->pam_done) {
540 case 1:
541 sshpam_authenticated = 1;
542 return (0);
543 case 0:
544 break;
545 default:
546 return (-1);
547 }
548 if (num != 1) {
549 error("PAM: expected one response, got %u", num);
550 return (-1);
551 }
552 buffer_init(&buffer);
553 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100554 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
555 buffer_free(&buffer);
556 return (-1);
557 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000558 buffer_free(&buffer);
559 return (1);
560}
561
562static void
563sshpam_free_ctx(void *ctxtp)
564{
565 struct pam_ctxt *ctxt = ctxtp;
566
Darren Tucker8846a072003-10-07 11:30:15 +1000567 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000568 xfree(ctxt);
569 /*
570 * We don't call sshpam_cleanup() here because we may need the PAM
571 * handle at a later stage, e.g. when setting up a session. It's
572 * still on the cleanup list, so pam_end() *will* be called before
573 * the server process terminates.
574 */
575}
576
577KbdintDevice sshpam_device = {
578 "pam",
579 sshpam_init_ctx,
580 sshpam_query,
581 sshpam_respond,
582 sshpam_free_ctx
583};
584
585KbdintDevice mm_sshpam_device = {
586 "pam",
587 mm_sshpam_init_ctx,
588 mm_sshpam_query,
589 mm_sshpam_respond,
590 mm_sshpam_free_ctx
591};
592
593/*
594 * This replaces auth-pam.c
595 */
596void
597start_pam(const char *user)
598{
Damien Miller9d507da2003-05-14 15:31:12 +1000599 if (!options.use_pam)
600 fatal("PAM: initialisation requested when UsePAM=no");
601
Damien Miller4f9f42a2003-05-10 19:28:02 +1000602 if (sshpam_init(user) == -1)
603 fatal("PAM: initialisation failed");
604}
605
606void
607finish_pam(void)
608{
Darren Tucker8846a072003-10-07 11:30:15 +1000609 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000610}
611
Damien Miller1f499fd2003-08-25 13:08:49 +1000612u_int
613do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000614{
Damien Miller1f499fd2003-08-25 13:08:49 +1000615 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
616 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
617
618 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD)
619 return (0);
620
621 if (sshpam_err == PAM_NEW_AUTHTOK_REQD) {
622 sshpam_new_authtok_reqd = 1;
623
624 /* Prevent forwardings until password changed */
625 no_port_forwarding_flag |= 2;
626 no_agent_forwarding_flag |= 2;
627 no_x11_forwarding_flag |= 2;
628 }
629
Damien Miller4f9f42a2003-05-10 19:28:02 +1000630 return (1);
631}
632
633void
Damien Miller341c6e62003-09-02 23:18:52 +1000634do_pam_session(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000635{
636 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
637 (const void *)&null_conv);
638 if (sshpam_err != PAM_SUCCESS)
639 fatal("PAM: failed to set PAM_CONV: %s",
640 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller341c6e62003-09-02 23:18:52 +1000641 sshpam_err = pam_open_session(sshpam_handle, 0);
642 if (sshpam_err != PAM_SUCCESS)
643 fatal("PAM: pam_open_session(): %s",
644 pam_strerror(sshpam_handle, sshpam_err));
645 sshpam_session_open = 1;
646}
647
648void
649do_pam_set_tty(const char *tty)
650{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000651 if (tty != NULL) {
652 debug("PAM: setting PAM_TTY to \"%s\"", tty);
653 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
654 if (sshpam_err != PAM_SUCCESS)
655 fatal("PAM: failed to set PAM_TTY: %s",
656 pam_strerror(sshpam_handle, sshpam_err));
657 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000658}
659
660void
661do_pam_setcred(int init)
662{
663 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
664 (const void *)&null_conv);
665 if (sshpam_err != PAM_SUCCESS)
666 fatal("PAM: failed to set PAM_CONV: %s",
667 pam_strerror(sshpam_handle, sshpam_err));
668 if (init) {
669 debug("PAM: establishing credentials");
670 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
671 } else {
672 debug("PAM: reinitializing credentials");
673 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
674 }
675 if (sshpam_err == PAM_SUCCESS) {
676 sshpam_cred_established = 1;
677 return;
678 }
679 if (sshpam_authenticated)
680 fatal("PAM: pam_setcred(): %s",
681 pam_strerror(sshpam_handle, sshpam_err));
682 else
683 debug("PAM: pam_setcred(): %s",
684 pam_strerror(sshpam_handle, sshpam_err));
685}
686
687int
688is_pam_password_change_required(void)
689{
690 return (sshpam_new_authtok_reqd);
691}
692
693static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000694pam_chauthtok_conv(int n, const struct pam_message **msg,
695 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000696{
697 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000698 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000699 int i;
700
Damien Miller5c3a5582003-09-23 22:12:38 +1000701 *resp = NULL;
702
Damien Miller4f9f42a2003-05-10 19:28:02 +1000703 if (n <= 0 || n > PAM_MAX_NUM_MSG)
704 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000705
706 if ((reply = malloc(n * sizeof(*reply))) == NULL)
707 return (PAM_CONV_ERR);
708 memset(reply, 0, n * sizeof(*reply));
709
Damien Miller4f9f42a2003-05-10 19:28:02 +1000710 for (i = 0; i < n; ++i) {
711 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
712 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000713 reply[i].resp =
Damien Miller4f9f42a2003-05-10 19:28:02 +1000714 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
715 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000716 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000717 break;
718 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100719 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000720 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000721 reply[i].resp = xstrdup(input);
722 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000723 break;
724 case PAM_ERROR_MSG:
725 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100726 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000727 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000728 break;
729 default:
730 goto fail;
731 }
732 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000733 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000734 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000735
Damien Miller4f9f42a2003-05-10 19:28:02 +1000736 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000737 for(i = 0; i < n; i++) {
738 if (reply[i].resp != NULL)
739 xfree(reply[i].resp);
740 }
741 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000742 return (PAM_CONV_ERR);
743}
744
745/*
746 * XXX this should be done in the authentication phase, but ssh1 doesn't
747 * support that
748 */
749void
750do_pam_chauthtok(void)
751{
Damien Millerf4b6f102003-09-02 23:12:06 +1000752 struct pam_conv pam_conv;
753
754 pam_conv.conv = pam_chauthtok_conv;
755 pam_conv.appdata_ptr = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000756
757 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000758 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000759 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
760 (const void *)&pam_conv);
761 if (sshpam_err != PAM_SUCCESS)
762 fatal("PAM: failed to set PAM_CONV: %s",
763 pam_strerror(sshpam_handle, sshpam_err));
764 debug("PAM: changing password");
765 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
766 if (sshpam_err != PAM_SUCCESS)
767 fatal("PAM: pam_chauthtok(): %s",
768 pam_strerror(sshpam_handle, sshpam_err));
769}
770
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000771/*
772 * Set a PAM environment string. We need to do this so that the session
773 * modules can handle things like Kerberos/GSI credentials that appear
774 * during the ssh authentication process.
775 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000776int
777do_pam_putenv(char *name, char *value)
778{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000779 int ret = 1;
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000780#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000781 char *compound;
782 size_t len;
783
784 len = strlen(name) + strlen(value) + 2;
785 compound = xmalloc(len);
786
787 snprintf(compound, len, "%s=%s", name, value);
788 ret = pam_putenv(sshpam_handle, compound);
789 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000790#endif
Damien Millerf2728092003-09-17 07:24:25 +1000791
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000792 return (ret);
793}
794
Damien Miller4f9f42a2003-05-10 19:28:02 +1000795void
796print_pam_messages(void)
797{
798 /* XXX */
799}
800
801char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100802fetch_pam_child_environment(void)
803{
804 return sshpam_env;
805}
806
807char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000808fetch_pam_environment(void)
809{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000810 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000811}
812
813void
814free_pam_environment(char **env)
815{
816 char **envp;
817
Damien Millere27c6cc2003-05-16 18:21:01 +1000818 if (env == NULL)
819 return;
820
Damien Miller4f9f42a2003-05-10 19:28:02 +1000821 for (envp = env; *envp; envp++)
822 xfree(*envp);
823 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100824}
825
826#endif /* USE_PAM */