blob: 2fe1e3382cb9f9664913259ecb6668af7d5af4bf [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 Tucker07705c72003-12-18 15:34:31 +110034RCSID("$Id: auth-pam.c,v 1.85 2003/12/18 04:34:32 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;
Darren Tucker07705c72003-12-18 15:34:31 +110056extern int compat20;
Damien Miller4e448a32003-05-14 15:11:48 +100057
Damien Miller4f9f42a2003-05-10 19:28:02 +100058#define __unused
Kevin Steves85ecbe72001-04-20 17:43:47 +000059
Damien Miller4f9f42a2003-05-10 19:28:02 +100060#ifdef USE_POSIX_THREADS
61#include <pthread.h>
62/*
Damien Millera8e06ce2003-11-21 23:48:55 +110063 * Avoid namespace clash when *not* using pthreads for systems *with*
64 * pthreads, which unconditionally define pthread_t via sys/types.h
Damien Miller4f9f42a2003-05-10 19:28:02 +100065 * (e.g. Linux)
66 */
Damien Millera8e06ce2003-11-21 23:48:55 +110067typedef pthread_t sp_pthread_t;
Damien Miller4f9f42a2003-05-10 19:28:02 +100068#else
69/*
70 * Simulate threads with processes.
71 */
72typedef pid_t sp_pthread_t;
Kevin Steves63007d42002-07-21 17:57:01 +000073
Damien Miller4f9f42a2003-05-10 19:28:02 +100074static void
75pthread_exit(void *value __unused)
76{
77 _exit(0);
78}
Damien Miller2f6a0ad2000-05-31 11:20:11 +100079
Damien Miller4f9f42a2003-05-10 19:28:02 +100080static int
81pthread_create(sp_pthread_t *thread, const void *attr __unused,
82 void *(*thread_start)(void *), void *arg)
83{
84 pid_t pid;
Damien Millere72b7af1999-12-30 15:08:44 +110085
Damien Miller4f9f42a2003-05-10 19:28:02 +100086 switch ((pid = fork())) {
87 case -1:
88 error("fork(): %s", strerror(errno));
89 return (-1);
90 case 0:
91 thread_start(arg);
92 _exit(1);
93 default:
94 *thread = pid;
95 return (0);
96 }
97}
Damien Millere72b7af1999-12-30 15:08:44 +110098
Damien Miller4f9f42a2003-05-10 19:28:02 +100099static int
100pthread_cancel(sp_pthread_t thread)
101{
102 return (kill(thread, SIGTERM));
103}
104
105static int
106pthread_join(sp_pthread_t thread, void **value __unused)
107{
108 int status;
109
110 waitpid(thread, &status, 0);
111 return (status);
112}
113#endif
114
115
Damien Miller5c3a5582003-09-23 22:12:38 +1000116static pam_handle_t *sshpam_handle = NULL;
117static int sshpam_err = 0;
118static int sshpam_authenticated = 0;
119static int sshpam_new_authtok_reqd = 0;
120static int sshpam_session_open = 0;
121static int sshpam_cred_established = 0;
Darren Tucker07705c72003-12-18 15:34:31 +1100122static int sshpam_account_status = -1;
Damien Millerc756e9b2003-11-17 21:41:42 +1100123static char **sshpam_env = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000124
125struct pam_ctxt {
126 sp_pthread_t pam_thread;
127 int pam_psock;
128 int pam_csock;
129 int pam_done;
Damien Millere72b7af1999-12-30 15:08:44 +1100130};
Damien Millere72b7af1999-12-30 15:08:44 +1100131
Damien Miller4f9f42a2003-05-10 19:28:02 +1000132static void sshpam_free_ctx(void *);
Darren Tucker8846a072003-10-07 11:30:15 +1000133static struct pam_ctxt *cleanup_ctxt;
Damien Miller9d5705a2000-09-16 16:09:27 +1100134
Damien Millerc756e9b2003-11-17 21:41:42 +1100135/* Some PAM implementations don't implement this */
136#ifndef HAVE_PAM_GETENVLIST
137static char **
138pam_getenvlist(pam_handle_t *pamh)
139{
140 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100141 * XXX - If necessary, we can still support envrionment passing
Damien Millerc756e9b2003-11-17 21:41:42 +1100142 * for platforms without pam_getenvlist by searching for known
143 * env vars (e.g. KRB5CCNAME) from the PAM environment.
144 */
145 return NULL;
146}
147#endif
148
Darren Tucker07705c72003-12-18 15:34:31 +1100149void
150pam_password_change_required(int reqd)
151{
152 sshpam_new_authtok_reqd = reqd;
153 if (reqd) {
154 no_port_forwarding_flag |= 2;
155 no_agent_forwarding_flag |= 2;
156 no_x11_forwarding_flag |= 2;
157 } else {
158 no_port_forwarding_flag &= ~2;
159 no_agent_forwarding_flag &= ~2;
160 no_x11_forwarding_flag &= ~2;
161
162 }
163}
Damien Millerc756e9b2003-11-17 21:41:42 +1100164/* Import regular and PAM environment from subprocess */
165static void
166import_environments(Buffer *b)
167{
168 char *env;
169 u_int i, num_env;
170 int err;
171
Darren Tucker07705c72003-12-18 15:34:31 +1100172 /* Import variables set by do_pam_account */
173 sshpam_account_status = buffer_get_int(b);
174 pam_password_change_required(buffer_get_int(b));
175
Damien Millerc756e9b2003-11-17 21:41:42 +1100176 /* Import environment from subprocess */
177 num_env = buffer_get_int(b);
178 sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env));
179 debug3("PAM: num env strings %d", num_env);
180 for(i = 0; i < num_env; i++)
181 sshpam_env[i] = buffer_get_string(b, NULL);
182
183 sshpam_env[num_env] = NULL;
184
185 /* Import PAM environment from subprocess */
186 num_env = buffer_get_int(b);
187 debug("PAM: num PAM env strings %d", num_env);
188 for(i = 0; i < num_env; i++) {
189 env = buffer_get_string(b, NULL);
190
Darren Tucker8a1624c2003-11-18 12:45:35 +1100191#ifdef HAVE_PAM_PUTENV
Damien Millerc756e9b2003-11-17 21:41:42 +1100192 /* Errors are not fatal here */
193 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
194 error("PAM: pam_putenv: %s",
195 pam_strerror(sshpam_handle, sshpam_err));
196 }
Darren Tucker8a1624c2003-11-18 12:45:35 +1100197#endif
Damien Millerc756e9b2003-11-17 21:41:42 +1100198 }
199}
200
Damien Miller9d5705a2000-09-16 16:09:27 +1100201/*
Damien Miller4f9f42a2003-05-10 19:28:02 +1000202 * Conversation function for authentication thread.
Damien Miller9d5705a2000-09-16 16:09:27 +1100203 */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000204static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000205sshpam_thread_conv(int n, const struct pam_message **msg,
206 struct pam_response **resp, void *data)
Damien Millere72b7af1999-12-30 15:08:44 +1100207{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000208 Buffer buffer;
209 struct pam_ctxt *ctxt;
Damien Miller5c3a5582003-09-23 22:12:38 +1000210 struct pam_response *reply;
Kevin Steves38b050a2002-07-23 00:44:07 +0000211 int i;
212
Damien Miller5c3a5582003-09-23 22:12:38 +1000213 *resp = NULL;
214
Damien Miller4f9f42a2003-05-10 19:28:02 +1000215 ctxt = data;
216 if (n <= 0 || n > PAM_MAX_NUM_MSG)
217 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000218
219 if ((reply = malloc(n * sizeof(*reply))) == NULL)
220 return (PAM_CONV_ERR);
221 memset(reply, 0, n * sizeof(*reply));
222
Damien Miller4f9f42a2003-05-10 19:28:02 +1000223 buffer_init(&buffer);
224 for (i = 0; i < n; ++i) {
Damien Miller4f9f42a2003-05-10 19:28:02 +1000225 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
226 case PAM_PROMPT_ECHO_OFF:
Damien Millera8e06ce2003-11-21 23:48:55 +1100227 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000228 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100229 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100230 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
231 goto fail;
Damien Millera8e06ce2003-11-21 23:48:55 +1100232 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
Damien Miller9bdba702003-11-17 21:27:55 +1100233 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000234 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
235 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000236 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000237 break;
238 case PAM_PROMPT_ECHO_ON:
Damien Millera8e06ce2003-11-21 23:48:55 +1100239 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000240 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100241 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100242 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
243 goto fail;
244 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
245 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000246 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
247 goto fail;
Damien Miller5c3a5582003-09-23 22:12:38 +1000248 reply[i].resp = buffer_get_string(&buffer, NULL);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000249 break;
250 case PAM_ERROR_MSG:
Damien Millera8e06ce2003-11-21 23:48:55 +1100251 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000252 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100253 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100254 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
255 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000256 break;
257 case PAM_TEXT_INFO:
Damien Millera8e06ce2003-11-21 23:48:55 +1100258 buffer_put_cstring(&buffer,
Damien Miller5c3a5582003-09-23 22:12:38 +1000259 PAM_MSG_MEMBER(msg, i, msg));
Damien Millera8e06ce2003-11-21 23:48:55 +1100260 if (ssh_msg_send(ctxt->pam_csock,
Damien Miller9bdba702003-11-17 21:27:55 +1100261 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
262 goto fail;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000263 break;
264 default:
265 goto fail;
266 }
267 buffer_clear(&buffer);
Kevin Steves38b050a2002-07-23 00:44:07 +0000268 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000269 buffer_free(&buffer);
Damien Miller5c3a5582003-09-23 22:12:38 +1000270 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000271 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000272
Damien Miller4f9f42a2003-05-10 19:28:02 +1000273 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000274 for(i = 0; i < n; i++) {
275 if (reply[i].resp != NULL)
276 xfree(reply[i].resp);
277 }
278 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000279 buffer_free(&buffer);
280 return (PAM_CONV_ERR);
Kevin Steves38b050a2002-07-23 00:44:07 +0000281}
282
Damien Miller4f9f42a2003-05-10 19:28:02 +1000283/*
284 * Authentication thread.
285 */
286static void *
287sshpam_thread(void *ctxtp)
Damien Millere72b7af1999-12-30 15:08:44 +1100288{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000289 struct pam_ctxt *ctxt = ctxtp;
290 Buffer buffer;
Damien Millerf4b6f102003-09-02 23:12:06 +1000291 struct pam_conv sshpam_conv;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000292#ifndef USE_POSIX_THREADS
Damien Millerc756e9b2003-11-17 21:41:42 +1100293 extern char **environ;
294 char **env_from_pam;
295 u_int i;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000296 const char *pam_user;
297
298 pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user);
299 setproctitle("%s [pam]", pam_user);
Damien Millerc756e9b2003-11-17 21:41:42 +1100300 environ[0] = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000301#endif
302
Damien Millerf4b6f102003-09-02 23:12:06 +1000303 sshpam_conv.conv = sshpam_thread_conv;
304 sshpam_conv.appdata_ptr = ctxt;
305
Damien Miller4f9f42a2003-05-10 19:28:02 +1000306 buffer_init(&buffer);
307 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
308 (const void *)&sshpam_conv);
309 if (sshpam_err != PAM_SUCCESS)
310 goto auth_fail;
311 sshpam_err = pam_authenticate(sshpam_handle, 0);
312 if (sshpam_err != PAM_SUCCESS)
313 goto auth_fail;
Darren Tucker07705c72003-12-18 15:34:31 +1100314
315 /* if (compat20) { */
316 if (!do_pam_account())
317 goto auth_fail;
318 if (sshpam_new_authtok_reqd) {
319 sshpam_err = pam_chauthtok(sshpam_handle,
320 PAM_CHANGE_EXPIRED_AUTHTOK);
321 if (sshpam_err != PAM_SUCCESS)
322 goto auth_fail;
323 pam_password_change_required(0);
324 }
325 /* } */
326
Damien Miller4f9f42a2003-05-10 19:28:02 +1000327 buffer_put_cstring(&buffer, "OK");
Damien Millerc756e9b2003-11-17 21:41:42 +1100328
329#ifndef USE_POSIX_THREADS
Darren Tucker07705c72003-12-18 15:34:31 +1100330 /* Export variables set by do_pam_account */
331 buffer_put_int(&buffer, sshpam_account_status);
332 buffer_put_int(&buffer, sshpam_new_authtok_reqd);
333
Damien Millerc756e9b2003-11-17 21:41:42 +1100334 /* Export any environment strings set in child */
335 for(i = 0; environ[i] != NULL; i++)
336 ; /* Count */
337 buffer_put_int(&buffer, i);
338 for(i = 0; environ[i] != NULL; i++)
339 buffer_put_cstring(&buffer, environ[i]);
340
341 /* Export any environment strings set by PAM in child */
342 env_from_pam = pam_getenvlist(sshpam_handle);
343 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
344 ; /* Count */
345 buffer_put_int(&buffer, i);
346 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
347 buffer_put_cstring(&buffer, env_from_pam[i]);
348#endif /* USE_POSIX_THREADS */
349
Damien Miller9bdba702003-11-17 21:27:55 +1100350 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000351 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
352 buffer_free(&buffer);
353 pthread_exit(NULL);
354
355 auth_fail:
356 buffer_put_cstring(&buffer,
357 pam_strerror(sshpam_handle, sshpam_err));
Damien Miller9bdba702003-11-17 21:27:55 +1100358 /* XXX - can't do much about an error here */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000359 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
360 buffer_free(&buffer);
361 pthread_exit(NULL);
Damien Miller787b2ec2003-11-21 23:56:47 +1100362
Damien Miller4f9f42a2003-05-10 19:28:02 +1000363 return (NULL); /* Avoid warning for non-pthread case */
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000364}
365
Darren Tucker8846a072003-10-07 11:30:15 +1000366void
367sshpam_thread_cleanup(void)
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000368{
Darren Tucker8846a072003-10-07 11:30:15 +1000369 struct pam_ctxt *ctxt = cleanup_ctxt;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000370
Darren Tucker8846a072003-10-07 11:30:15 +1000371 if (ctxt != NULL && ctxt->pam_thread != 0) {
372 pthread_cancel(ctxt->pam_thread);
373 pthread_join(ctxt->pam_thread, NULL);
374 close(ctxt->pam_psock);
375 close(ctxt->pam_csock);
376 memset(ctxt, 0, sizeof(*ctxt));
377 cleanup_ctxt = NULL;
378 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000379}
Kevin Stevesef4eea92001-02-05 12:42:17 +0000380
Damien Miller4f9f42a2003-05-10 19:28:02 +1000381static int
Damien Miller1f499fd2003-08-25 13:08:49 +1000382sshpam_null_conv(int n, const struct pam_message **msg,
383 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000384{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000385 return (PAM_CONV_ERR);
386}
Damien Miller63dc3e92001-02-07 12:58:33 +1100387
Damien Miller4f9f42a2003-05-10 19:28:02 +1000388static struct pam_conv null_conv = { sshpam_null_conv, NULL };
389
Darren Tucker8846a072003-10-07 11:30:15 +1000390void
391sshpam_cleanup(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000392{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000393 debug("PAM: cleanup");
Damien Miller5c3a5582003-09-23 22:12:38 +1000394 if (sshpam_handle == NULL)
395 return;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000396 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
397 if (sshpam_cred_established) {
398 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
399 sshpam_cred_established = 0;
400 }
401 if (sshpam_session_open) {
402 pam_close_session(sshpam_handle, PAM_SILENT);
403 sshpam_session_open = 0;
404 }
405 sshpam_authenticated = sshpam_new_authtok_reqd = 0;
406 pam_end(sshpam_handle, sshpam_err);
407 sshpam_handle = NULL;
408}
409
410static int
411sshpam_init(const char *user)
412{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000413 extern u_int utmp_len;
Darren Tucker455813b2003-09-13 22:12:11 +1000414 extern char *__progname;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000415 const char *pam_rhost, *pam_user;
416
417 if (sshpam_handle != NULL) {
418 /* We already have a PAM context; check if the user matches */
419 sshpam_err = pam_get_item(sshpam_handle,
420 PAM_USER, (const void **)&pam_user);
421 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
422 return (0);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000423 pam_end(sshpam_handle, sshpam_err);
424 sshpam_handle = NULL;
425 }
426 debug("PAM: initializing for \"%s\"", user);
Darren Tuckerc58c2ee2003-09-13 22:02:05 +1000427 sshpam_err =
428 pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000429 if (sshpam_err != PAM_SUCCESS) {
430 pam_end(sshpam_handle, sshpam_err);
431 sshpam_handle = NULL;
432 return (-1);
433 }
Damien Miller3a961dc2003-06-03 10:25:48 +1000434 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
Damien Miller46337202003-06-02 11:04:39 +1000435 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
Damien Miller25d93422003-05-18 20:45:47 +1000436 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
437 if (sshpam_err != PAM_SUCCESS) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000438 pam_end(sshpam_handle, sshpam_err);
Damien Miller25d93422003-05-18 20:45:47 +1000439 sshpam_handle = NULL;
440 return (-1);
441 }
442#ifdef PAM_TTY_KLUDGE
Damien Millera8e06ce2003-11-21 23:48:55 +1100443 /*
444 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
445 * sshd doesn't set the tty until too late in the auth process and
Damien Miller25d93422003-05-18 20:45:47 +1000446 * may not even set one (for tty-less connections)
Damien Millera8e06ce2003-11-21 23:48:55 +1100447 */
Damien Miller25d93422003-05-18 20:45:47 +1000448 debug("PAM: setting PAM_TTY to \"ssh\"");
449 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
450 if (sshpam_err != PAM_SUCCESS) {
451 pam_end(sshpam_handle, sshpam_err);
452 sshpam_handle = NULL;
453 return (-1);
454 }
455#endif
Damien Miller4f9f42a2003-05-10 19:28:02 +1000456 return (0);
457}
458
459static void *
460sshpam_init_ctx(Authctxt *authctxt)
461{
462 struct pam_ctxt *ctxt;
463 int socks[2];
464
Damien Miller4e448a32003-05-14 15:11:48 +1000465 /* Refuse to start if we don't have PAM enabled */
466 if (!options.use_pam)
467 return NULL;
468
Damien Miller4f9f42a2003-05-10 19:28:02 +1000469 /* Initialize PAM */
470 if (sshpam_init(authctxt->user) == -1) {
471 error("PAM: initialization failed");
472 return (NULL);
473 }
474
475 ctxt = xmalloc(sizeof *ctxt);
Darren Tucker8846a072003-10-07 11:30:15 +1000476 memset(ctxt, 0, sizeof(*ctxt));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000477
478 /* Start the authentication thread */
479 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
480 error("PAM: failed create sockets: %s", strerror(errno));
481 xfree(ctxt);
482 return (NULL);
483 }
484 ctxt->pam_psock = socks[0];
485 ctxt->pam_csock = socks[1];
486 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
487 error("PAM: failed to start authentication thread: %s",
488 strerror(errno));
489 close(socks[0]);
490 close(socks[1]);
491 xfree(ctxt);
492 return (NULL);
493 }
Darren Tucker8846a072003-10-07 11:30:15 +1000494 cleanup_ctxt = ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000495 return (ctxt);
496}
497
498static int
499sshpam_query(void *ctx, char **name, char **info,
500 u_int *num, char ***prompts, u_int **echo_on)
501{
502 Buffer buffer;
503 struct pam_ctxt *ctxt = ctx;
504 size_t plen;
505 u_char type;
506 char *msg;
Damien Miller7f2d7952003-07-30 14:53:11 +1000507 size_t len;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000508
509 buffer_init(&buffer);
510 *name = xstrdup("");
511 *info = xstrdup("");
512 *prompts = xmalloc(sizeof(char *));
513 **prompts = NULL;
514 plen = 0;
515 *echo_on = xmalloc(sizeof(u_int));
516 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
517 type = buffer_get_char(&buffer);
518 msg = buffer_get_string(&buffer, NULL);
519 switch (type) {
520 case PAM_PROMPT_ECHO_ON:
521 case PAM_PROMPT_ECHO_OFF:
522 *num = 1;
Damien Miller7f2d7952003-07-30 14:53:11 +1000523 len = plen + strlen(msg) + 1;
524 **prompts = xrealloc(**prompts, len);
525 plen += snprintf(**prompts + plen, len, "%s", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000526 **echo_on = (type == PAM_PROMPT_ECHO_ON);
527 xfree(msg);
528 return (0);
529 case PAM_ERROR_MSG:
530 case PAM_TEXT_INFO:
531 /* accumulate messages */
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100532 len = plen + strlen(msg) + 2;
Damien Miller7f2d7952003-07-30 14:53:11 +1000533 **prompts = xrealloc(**prompts, len);
Darren Tuckerae52b7c2003-11-13 19:52:31 +1100534 plen += snprintf(**prompts + plen, len, "%s\n", msg);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000535 xfree(msg);
536 break;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000537 case PAM_SUCCESS:
538 case PAM_AUTH_ERR:
539 if (**prompts != NULL) {
540 /* drain any accumulated messages */
Darren Tucker18df00c2003-11-18 12:42:07 +1100541 debug("PAM: %s", **prompts);
542 buffer_append(&loginmsg, **prompts,
543 strlen(**prompts));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000544 xfree(**prompts);
545 **prompts = NULL;
546 }
547 if (type == PAM_SUCCESS) {
Damien Millerc756e9b2003-11-17 21:41:42 +1100548 import_environments(&buffer);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000549 *num = 0;
550 **echo_on = 0;
551 ctxt->pam_done = 1;
552 xfree(msg);
553 return (0);
554 }
555 error("PAM: %s", msg);
Darren Tucker439ce0d2003-10-09 14:20:15 +1000556 /* FALLTHROUGH */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000557 default:
558 *num = 0;
559 **echo_on = 0;
560 xfree(msg);
561 ctxt->pam_done = -1;
562 return (-1);
563 }
564 }
565 return (-1);
566}
567
568/* XXX - see also comment in auth-chall.c:verify_response */
569static int
570sshpam_respond(void *ctx, u_int num, char **resp)
571{
572 Buffer buffer;
573 struct pam_ctxt *ctxt = ctx;
574
575 debug2("PAM: %s", __func__);
576 switch (ctxt->pam_done) {
577 case 1:
578 sshpam_authenticated = 1;
579 return (0);
580 case 0:
581 break;
582 default:
583 return (-1);
584 }
585 if (num != 1) {
586 error("PAM: expected one response, got %u", num);
587 return (-1);
588 }
589 buffer_init(&buffer);
590 buffer_put_cstring(&buffer, *resp);
Damien Miller9bdba702003-11-17 21:27:55 +1100591 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
592 buffer_free(&buffer);
593 return (-1);
594 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000595 buffer_free(&buffer);
596 return (1);
597}
598
599static void
600sshpam_free_ctx(void *ctxtp)
601{
602 struct pam_ctxt *ctxt = ctxtp;
603
Darren Tucker8846a072003-10-07 11:30:15 +1000604 sshpam_thread_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000605 xfree(ctxt);
606 /*
607 * We don't call sshpam_cleanup() here because we may need the PAM
608 * handle at a later stage, e.g. when setting up a session. It's
609 * still on the cleanup list, so pam_end() *will* be called before
610 * the server process terminates.
611 */
612}
613
614KbdintDevice sshpam_device = {
615 "pam",
616 sshpam_init_ctx,
617 sshpam_query,
618 sshpam_respond,
619 sshpam_free_ctx
620};
621
622KbdintDevice mm_sshpam_device = {
623 "pam",
624 mm_sshpam_init_ctx,
625 mm_sshpam_query,
626 mm_sshpam_respond,
627 mm_sshpam_free_ctx
628};
629
630/*
631 * This replaces auth-pam.c
632 */
633void
634start_pam(const char *user)
635{
Damien Miller9d507da2003-05-14 15:31:12 +1000636 if (!options.use_pam)
637 fatal("PAM: initialisation requested when UsePAM=no");
638
Damien Miller4f9f42a2003-05-10 19:28:02 +1000639 if (sshpam_init(user) == -1)
640 fatal("PAM: initialisation failed");
641}
642
643void
644finish_pam(void)
645{
Darren Tucker8846a072003-10-07 11:30:15 +1000646 sshpam_cleanup();
Damien Miller4f9f42a2003-05-10 19:28:02 +1000647}
648
Damien Miller1f499fd2003-08-25 13:08:49 +1000649u_int
650do_pam_account(void)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000651{
Darren Tucker07705c72003-12-18 15:34:31 +1100652 if (sshpam_account_status != -1)
653 return (sshpam_account_status);
654
Damien Miller1f499fd2003-08-25 13:08:49 +1000655 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
656 debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err);
Darren Tucker07705c72003-12-18 15:34:31 +1100657
658 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
659 sshpam_account_status = 0;
660 return (sshpam_account_status);
Damien Miller1f499fd2003-08-25 13:08:49 +1000661 }
662
Darren Tucker07705c72003-12-18 15:34:31 +1100663 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
664 pam_password_change_required(1);
665
666 sshpam_account_status = 1;
667 return (sshpam_account_status);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000668}
669
670void
Damien Miller341c6e62003-09-02 23:18:52 +1000671do_pam_set_tty(const char *tty)
672{
Darren Tuckerf38db7f2003-08-08 13:43:37 +1000673 if (tty != NULL) {
674 debug("PAM: setting PAM_TTY to \"%s\"", tty);
675 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
676 if (sshpam_err != PAM_SUCCESS)
677 fatal("PAM: failed to set PAM_TTY: %s",
678 pam_strerror(sshpam_handle, sshpam_err));
679 }
Damien Miller4f9f42a2003-05-10 19:28:02 +1000680}
681
682void
683do_pam_setcred(int init)
684{
685 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
686 (const void *)&null_conv);
687 if (sshpam_err != PAM_SUCCESS)
688 fatal("PAM: failed to set PAM_CONV: %s",
689 pam_strerror(sshpam_handle, sshpam_err));
690 if (init) {
691 debug("PAM: establishing credentials");
692 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
693 } else {
694 debug("PAM: reinitializing credentials");
695 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
696 }
697 if (sshpam_err == PAM_SUCCESS) {
698 sshpam_cred_established = 1;
699 return;
700 }
701 if (sshpam_authenticated)
702 fatal("PAM: pam_setcred(): %s",
703 pam_strerror(sshpam_handle, sshpam_err));
704 else
705 debug("PAM: pam_setcred(): %s",
706 pam_strerror(sshpam_handle, sshpam_err));
707}
708
709int
710is_pam_password_change_required(void)
711{
712 return (sshpam_new_authtok_reqd);
713}
714
715static int
Darren Tucker18df00c2003-11-18 12:42:07 +1100716pam_tty_conv(int n, const struct pam_message **msg,
Damien Miller1f499fd2003-08-25 13:08:49 +1000717 struct pam_response **resp, void *data)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000718{
719 char input[PAM_MAX_MSG_SIZE];
Damien Miller5c3a5582003-09-23 22:12:38 +1000720 struct pam_response *reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000721 int i;
722
Damien Miller5c3a5582003-09-23 22:12:38 +1000723 *resp = NULL;
724
Darren Tucker18df00c2003-11-18 12:42:07 +1100725 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
Damien Miller4f9f42a2003-05-10 19:28:02 +1000726 return (PAM_CONV_ERR);
Damien Miller5c3a5582003-09-23 22:12:38 +1000727
728 if ((reply = malloc(n * sizeof(*reply))) == NULL)
729 return (PAM_CONV_ERR);
730 memset(reply, 0, n * sizeof(*reply));
731
Damien Miller4f9f42a2003-05-10 19:28:02 +1000732 for (i = 0; i < n; ++i) {
733 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
734 case PAM_PROMPT_ECHO_OFF:
Damien Miller5c3a5582003-09-23 22:12:38 +1000735 reply[i].resp =
Damien Millera8e06ce2003-11-21 23:48:55 +1100736 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
Damien Miller4f9f42a2003-05-10 19:28:02 +1000737 RP_ALLOW_STDIN);
Damien Miller5c3a5582003-09-23 22:12:38 +1000738 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000739 break;
740 case PAM_PROMPT_ECHO_ON:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100741 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000742 fgets(input, sizeof input, stdin);
Damien Miller5c3a5582003-09-23 22:12:38 +1000743 reply[i].resp = xstrdup(input);
744 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000745 break;
746 case PAM_ERROR_MSG:
747 case PAM_TEXT_INFO:
Darren Tucker0947ddf2003-11-13 11:21:31 +1100748 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
Damien Miller5c3a5582003-09-23 22:12:38 +1000749 reply[i].resp_retcode = PAM_SUCCESS;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000750 break;
751 default:
752 goto fail;
753 }
754 }
Damien Miller5c3a5582003-09-23 22:12:38 +1000755 *resp = reply;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000756 return (PAM_SUCCESS);
Damien Miller5c3a5582003-09-23 22:12:38 +1000757
Damien Miller4f9f42a2003-05-10 19:28:02 +1000758 fail:
Damien Miller5c3a5582003-09-23 22:12:38 +1000759 for(i = 0; i < n; i++) {
760 if (reply[i].resp != NULL)
761 xfree(reply[i].resp);
762 }
763 xfree(reply);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000764 return (PAM_CONV_ERR);
765}
766
Darren Tucker18df00c2003-11-18 12:42:07 +1100767static struct pam_conv tty_conv = { pam_tty_conv, NULL };
768
Damien Miller4f9f42a2003-05-10 19:28:02 +1000769/*
770 * XXX this should be done in the authentication phase, but ssh1 doesn't
771 * support that
772 */
773void
774do_pam_chauthtok(void)
775{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000776 if (use_privsep)
Damien Miller1f499fd2003-08-25 13:08:49 +1000777 fatal("Password expired (unable to change with privsep)");
Damien Miller4f9f42a2003-05-10 19:28:02 +1000778 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100779 (const void *)&tty_conv);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000780 if (sshpam_err != PAM_SUCCESS)
781 fatal("PAM: failed to set PAM_CONV: %s",
782 pam_strerror(sshpam_handle, sshpam_err));
783 debug("PAM: changing password");
784 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
785 if (sshpam_err != PAM_SUCCESS)
786 fatal("PAM: pam_chauthtok(): %s",
787 pam_strerror(sshpam_handle, sshpam_err));
788}
789
Darren Tucker18df00c2003-11-18 12:42:07 +1100790void
791do_pam_session(void)
792{
Damien Millera8e06ce2003-11-21 23:48:55 +1100793 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
Darren Tucker18df00c2003-11-18 12:42:07 +1100794 (const void *)&tty_conv);
795 if (sshpam_err != PAM_SUCCESS)
796 fatal("PAM: failed to set PAM_CONV: %s",
797 pam_strerror(sshpam_handle, sshpam_err));
798 sshpam_err = pam_open_session(sshpam_handle, 0);
799 if (sshpam_err != PAM_SUCCESS)
800 fatal("PAM: pam_open_session(): %s",
801 pam_strerror(sshpam_handle, sshpam_err));
802 sshpam_session_open = 1;
803}
804
Damien Millera8e06ce2003-11-21 23:48:55 +1100805/*
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000806 * Set a PAM environment string. We need to do this so that the session
807 * modules can handle things like Kerberos/GSI credentials that appear
808 * during the ssh authentication process.
809 */
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000810int
Damien Millera8e06ce2003-11-21 23:48:55 +1100811do_pam_putenv(char *name, char *value)
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000812{
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000813 int ret = 1;
Damien Miller787b2ec2003-11-21 23:56:47 +1100814#ifdef HAVE_PAM_PUTENV
Damien Millerf2728092003-09-17 07:24:25 +1000815 char *compound;
816 size_t len;
817
818 len = strlen(name) + strlen(value) + 2;
819 compound = xmalloc(len);
820
821 snprintf(compound, len, "%s=%s", name, value);
822 ret = pam_putenv(sshpam_handle, compound);
823 xfree(compound);
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000824#endif
Damien Millerf2728092003-09-17 07:24:25 +1000825
Darren Tucker49aaf4a2003-08-26 11:58:16 +1000826 return (ret);
827}
828
Damien Miller4f9f42a2003-05-10 19:28:02 +1000829void
830print_pam_messages(void)
831{
832 /* XXX */
833}
834
835char **
Damien Millerc756e9b2003-11-17 21:41:42 +1100836fetch_pam_child_environment(void)
837{
838 return sshpam_env;
839}
840
841char **
Damien Miller4f9f42a2003-05-10 19:28:02 +1000842fetch_pam_environment(void)
843{
Damien Miller4f9f42a2003-05-10 19:28:02 +1000844 return (pam_getenvlist(sshpam_handle));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000845}
846
847void
848free_pam_environment(char **env)
849{
850 char **envp;
851
Damien Millere27c6cc2003-05-16 18:21:01 +1000852 if (env == NULL)
853 return;
854
Damien Miller4f9f42a2003-05-10 19:28:02 +1000855 for (envp = env; *envp; envp++)
856 xfree(*envp);
857 xfree(env);
Damien Millere72b7af1999-12-30 15:08:44 +1100858}
859
860#endif /* USE_PAM */