]> andersk Git - gssapi-openssh.git/blame_incremental - openssh/auth-pam.c
merged OpenSSH 5.3p1 to trunk
[gssapi-openssh.git] / openssh / auth-pam.c
... / ...
CommitLineData
1/*-
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.
9 *
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.
18 *
19 * 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.
30 */
31/*
32 * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
33 * Copyright (c) 2003,2004,2006 Darren Tucker <dtucker@zip.com.au>
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 */
47
48/* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */
49#include "includes.h"
50
51#include <sys/types.h>
52#include <sys/stat.h>
53#include <sys/wait.h>
54
55#include <errno.h>
56#include <signal.h>
57#include <stdarg.h>
58#include <string.h>
59#include <unistd.h>
60
61#ifdef USE_PAM
62#if defined(HAVE_SECURITY_PAM_APPL_H)
63#include <security/pam_appl.h>
64#elif defined (HAVE_PAM_PAM_APPL_H)
65#include <pam/pam_appl.h>
66#endif
67
68/* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
69#ifdef PAM_SUN_CODEBASE
70# define sshpam_const /* Solaris, HP-UX, AIX */
71#else
72# define sshpam_const const /* LinuxPAM, OpenPAM */
73#endif
74
75/* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
76#ifdef PAM_SUN_CODEBASE
77# define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
78#else
79# define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
80#endif
81
82#include "xmalloc.h"
83#include "buffer.h"
84#include "key.h"
85#include "hostfile.h"
86#include "auth.h"
87#include "auth-pam.h"
88#include "canohost.h"
89#include "log.h"
90#include "msg.h"
91#include "packet.h"
92#include "misc.h"
93#include "servconf.h"
94#include "ssh2.h"
95#include "auth-options.h"
96#ifdef GSSAPI
97#include "ssh-gss.h"
98#endif
99#include "monitor_wrap.h"
100
101extern ServerOptions options;
102extern Buffer loginmsg;
103extern int compat20;
104extern u_int utmp_len;
105
106/* so we don't silently change behaviour */
107#ifdef USE_POSIX_THREADS
108# error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
109#endif
110
111/*
112 * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
113 * and generally a bad idea. Use at own risk and do not expect support if
114 * this breaks.
115 */
116#ifdef UNSUPPORTED_POSIX_THREADS_HACK
117#include <pthread.h>
118/*
119 * Avoid namespace clash when *not* using pthreads for systems *with*
120 * pthreads, which unconditionally define pthread_t via sys/types.h
121 * (e.g. Linux)
122 */
123typedef pthread_t sp_pthread_t;
124#else
125typedef pid_t sp_pthread_t;
126#endif
127
128struct pam_ctxt {
129 sp_pthread_t pam_thread;
130 int pam_psock;
131 int pam_csock;
132 int pam_done;
133};
134
135static void sshpam_free_ctx(void *);
136static struct pam_ctxt *cleanup_ctxt;
137
138#ifndef UNSUPPORTED_POSIX_THREADS_HACK
139/*
140 * Simulate threads with processes.
141 */
142
143static int sshpam_thread_status = -1;
144static mysig_t sshpam_oldsig;
145
146static void
147sshpam_sigchld_handler(int sig)
148{
149 signal(SIGCHLD, SIG_DFL);
150 if (cleanup_ctxt == NULL)
151 return; /* handler called after PAM cleanup, shouldn't happen */
152 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
153 <= 0) {
154 /* PAM thread has not exitted, privsep slave must have */
155 kill(cleanup_ctxt->pam_thread, SIGTERM);
156 if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0)
157 <= 0)
158 return; /* could not wait */
159 }
160 if (WIFSIGNALED(sshpam_thread_status) &&
161 WTERMSIG(sshpam_thread_status) == SIGTERM)
162 return; /* terminated by pthread_cancel */
163 if (!WIFEXITED(sshpam_thread_status))
164 sigdie("PAM: authentication thread exited unexpectedly");
165 if (WEXITSTATUS(sshpam_thread_status) != 0)
166 sigdie("PAM: authentication thread exited uncleanly");
167}
168
169/* ARGSUSED */
170static void
171pthread_exit(void *value)
172{
173 _exit(0);
174}
175
176/* ARGSUSED */
177static int
178pthread_create(sp_pthread_t *thread, const void *attr,
179 void *(*thread_start)(void *), void *arg)
180{
181 pid_t pid;
182 struct pam_ctxt *ctx = arg;
183
184 sshpam_thread_status = -1;
185 switch ((pid = fork())) {
186 case -1:
187 error("fork(): %s", strerror(errno));
188 return (-1);
189 case 0:
190 close(ctx->pam_psock);
191 ctx->pam_psock = -1;
192 thread_start(arg);
193 _exit(1);
194 default:
195 *thread = pid;
196 close(ctx->pam_csock);
197 ctx->pam_csock = -1;
198 sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler);
199 return (0);
200 }
201}
202
203static int
204pthread_cancel(sp_pthread_t thread)
205{
206 signal(SIGCHLD, sshpam_oldsig);
207 return (kill(thread, SIGTERM));
208}
209
210/* ARGSUSED */
211static int
212pthread_join(sp_pthread_t thread, void **value)
213{
214 int status;
215
216 if (sshpam_thread_status != -1)
217 return (sshpam_thread_status);
218 signal(SIGCHLD, sshpam_oldsig);
219 waitpid(thread, &status, 0);
220 return (status);
221}
222#endif
223
224
225static pam_handle_t *sshpam_handle = NULL;
226static int sshpam_err = 0;
227static int sshpam_authenticated = 0;
228static int sshpam_session_open = 0;
229static int sshpam_cred_established = 0;
230static int sshpam_account_status = -1;
231static char **sshpam_env = NULL;
232static Authctxt *sshpam_authctxt = NULL;
233static const char *sshpam_password = NULL;
234static char badpw[] = "\b\n\r\177INCORRECT";
235
236/* Some PAM implementations don't implement this */
237#ifndef HAVE_PAM_GETENVLIST
238static char **
239pam_getenvlist(pam_handle_t *pamh)
240{
241 /*
242 * XXX - If necessary, we can still support envrionment passing
243 * for platforms without pam_getenvlist by searching for known
244 * env vars (e.g. KRB5CCNAME) from the PAM environment.
245 */
246 return NULL;
247}
248#endif
249
250/*
251 * Some platforms, notably Solaris, do not enforce password complexity
252 * rules during pam_chauthtok() if the real uid of the calling process
253 * is 0, on the assumption that it's being called by "passwd" run by root.
254 * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
255 * the right thing.
256 */
257#ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
258static int
259sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
260{
261 int result;
262
263 if (sshpam_authctxt == NULL)
264 fatal("PAM: sshpam_authctxt not initialized");
265 if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
266 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
267 result = pam_chauthtok(pamh, flags);
268 if (setreuid(0, -1) == -1)
269 fatal("%s: setreuid failed: %s", __func__, strerror(errno));
270 return result;
271}
272# define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
273#endif
274
275struct passwd *
276sshpam_getpw(const char *user)
277{
278 struct passwd *pw;
279
280 if ((pw = getpwnam(user)) != NULL)
281 return(pw);
282
283 debug("PAM: faking passwd struct for user '%.100s'", user);
284 if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
285 return NULL;
286 pw->pw_name = xstrdup(user); /* XXX leak */
287 pw->pw_shell = "/bin/true";
288 pw->pw_gecos = "sshd fake PAM user";
289 return (pw);
290}
291
292void
293sshpam_check_userchanged(void)
294{
295 int sshpam_err;
296 struct passwd *pw;
297 const char *user;
298
299 debug("sshpam_check_userchanged");
300 sshpam_err = pam_get_item(sshpam_handle, PAM_USER, &user);
301 if (sshpam_err != PAM_SUCCESS)
302 fatal("PAM: could not get PAM_USER: %s",
303 pam_strerror(sshpam_handle, sshpam_err));
304 if (strcmp(user, sshpam_authctxt->pw->pw_name) != 0) {
305 debug("PAM: user mapped from '%.100s' to '%.100s'",
306 sshpam_authctxt->pw->pw_name, user);
307 if ((pw = getpwnam(user)) == NULL)
308 fatal("PAM: could not get passwd entry for user "
309 "'%.100s' provided by PAM_USER", user);
310 pwfree(sshpam_authctxt->pw);
311 sshpam_authctxt->pw = pw;
312 sshpam_authctxt->valid = allowed_user(pw);
313 debug("PAM: user '%.100s' now %svalid", user,
314 sshpam_authctxt->valid ? "" : "in");
315 }
316}
317
318void
319sshpam_password_change_required(int reqd)
320{
321 debug3("%s %d", __func__, reqd);
322 if (sshpam_authctxt == NULL)
323 fatal("%s: PAM authctxt not initialized", __func__);
324 sshpam_authctxt->force_pwchange = reqd;
325 if (reqd) {
326 no_port_forwarding_flag |= 2;
327 no_agent_forwarding_flag |= 2;
328 no_x11_forwarding_flag |= 2;
329 } else {
330 no_port_forwarding_flag &= ~2;
331 no_agent_forwarding_flag &= ~2;
332 no_x11_forwarding_flag &= ~2;
333 }
334}
335
336/* Import regular and PAM environment from subprocess */
337static void
338import_environments(Buffer *b)
339{
340 char *env, *user;
341 u_int i, num_env;
342 int err;
343
344 debug3("PAM: %s entering", __func__);
345
346#ifndef UNSUPPORTED_POSIX_THREADS_HACK
347 /* Import variables set by do_pam_account */
348 sshpam_account_status = buffer_get_int(b);
349 sshpam_password_change_required(buffer_get_int(b));
350 if (options.permit_pam_user_change) {
351 user = buffer_get_string(b, NULL);
352 debug("PAM: got username '%.100s' from thread", user);
353 if ((err = pam_set_item(sshpam_handle, PAM_USER, user)) != PAM_SUCCESS)
354 fatal("PAM: failed to set PAM_USER: %s",
355 pam_strerror(sshpam_handle, err));
356 pwfree(sshpam_authctxt->pw);
357 sshpam_authctxt->pw = pwcopy(sshpam_getpw(user));
358 }
359
360 /* Import environment from subprocess */
361 num_env = buffer_get_int(b);
362 if (num_env > 1024)
363 fatal("%s: received %u environment variables, expected <= 1024",
364 __func__, num_env);
365 sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
366 debug3("PAM: num env strings %d", num_env);
367 for(i = 0; i < num_env; i++)
368 sshpam_env[i] = buffer_get_string(b, NULL);
369
370 sshpam_env[num_env] = NULL;
371
372 /* Import PAM environment from subprocess */
373 num_env = buffer_get_int(b);
374 debug("PAM: num PAM env strings %d", num_env);
375 for(i = 0; i < num_env; i++) {
376 env = buffer_get_string(b, NULL);
377
378#ifdef HAVE_PAM_PUTENV
379 /* Errors are not fatal here */
380 if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
381 error("PAM: pam_putenv: %s",
382 pam_strerror(sshpam_handle, sshpam_err));
383 }
384#endif
385 }
386#endif
387}
388
389/*
390 * Conversation function for authentication thread.
391 */
392static int
393sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
394 struct pam_response **resp, void *data)
395{
396 Buffer buffer;
397 struct pam_ctxt *ctxt;
398 struct pam_response *reply;
399 int i;
400
401 debug3("PAM: %s entering, %d messages", __func__, n);
402 *resp = NULL;
403
404 if (data == NULL) {
405 error("PAM: conversation function passed a null context");
406 return (PAM_CONV_ERR);
407 }
408 ctxt = data;
409 if (n <= 0 || n > PAM_MAX_NUM_MSG)
410 return (PAM_CONV_ERR);
411
412 if ((reply = calloc(n, sizeof(*reply))) == NULL)
413 return (PAM_CONV_ERR);
414
415 buffer_init(&buffer);
416 for (i = 0; i < n; ++i) {
417 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
418 case PAM_PROMPT_ECHO_OFF:
419 buffer_put_cstring(&buffer,
420 PAM_MSG_MEMBER(msg, i, msg));
421 if (ssh_msg_send(ctxt->pam_csock,
422 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
423 goto fail;
424 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
425 goto fail;
426 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
427 goto fail;
428 reply[i].resp = buffer_get_string(&buffer, NULL);
429 break;
430 case PAM_PROMPT_ECHO_ON:
431 buffer_put_cstring(&buffer,
432 PAM_MSG_MEMBER(msg, i, msg));
433 if (ssh_msg_send(ctxt->pam_csock,
434 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
435 goto fail;
436 if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1)
437 goto fail;
438 if (buffer_get_char(&buffer) != PAM_AUTHTOK)
439 goto fail;
440 reply[i].resp = buffer_get_string(&buffer, NULL);
441 break;
442 case PAM_ERROR_MSG:
443 buffer_put_cstring(&buffer,
444 PAM_MSG_MEMBER(msg, i, msg));
445 if (ssh_msg_send(ctxt->pam_csock,
446 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
447 goto fail;
448 break;
449 case PAM_TEXT_INFO:
450 buffer_put_cstring(&buffer,
451 PAM_MSG_MEMBER(msg, i, msg));
452 if (ssh_msg_send(ctxt->pam_csock,
453 PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1)
454 goto fail;
455 break;
456 default:
457 goto fail;
458 }
459 buffer_clear(&buffer);
460 }
461 buffer_free(&buffer);
462 *resp = reply;
463 return (PAM_SUCCESS);
464
465 fail:
466 for(i = 0; i < n; i++) {
467 if (reply[i].resp != NULL)
468 xfree(reply[i].resp);
469 }
470 xfree(reply);
471 buffer_free(&buffer);
472 return (PAM_CONV_ERR);
473}
474
475/*
476 * Authentication thread.
477 */
478static void *
479sshpam_thread(void *ctxtp)
480{
481 struct pam_ctxt *ctxt = ctxtp;
482 Buffer buffer;
483 struct pam_conv sshpam_conv;
484 int flags = (options.permit_empty_passwd == 0 ?
485 PAM_DISALLOW_NULL_AUTHTOK : 0);
486#ifndef UNSUPPORTED_POSIX_THREADS_HACK
487 extern char **environ;
488 char **env_from_pam;
489 u_int i;
490 const char *pam_user;
491 const char **ptr_pam_user = &pam_user;
492 char *tz = getenv("TZ");
493
494 pam_get_item(sshpam_handle, PAM_USER,
495 (sshpam_const void **)ptr_pam_user);
496
497 environ[0] = NULL;
498 if (tz != NULL)
499 if (setenv("TZ", tz, 1) == -1)
500 error("PAM: could not set TZ environment: %s",
501 strerror(errno));
502
503 if (sshpam_authctxt != NULL) {
504 setproctitle("%s [pam]",
505 sshpam_authctxt->valid ? pam_user : "unknown");
506 }
507#endif
508
509 sshpam_conv.conv = sshpam_thread_conv;
510 sshpam_conv.appdata_ptr = ctxt;
511
512 if (sshpam_authctxt == NULL)
513 fatal("%s: PAM authctxt not initialized", __func__);
514
515 buffer_init(&buffer);
516 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
517 (const void *)&sshpam_conv);
518 if (sshpam_err != PAM_SUCCESS)
519 goto auth_fail;
520 sshpam_err = pam_authenticate(sshpam_handle, flags);
521 if (sshpam_err != PAM_SUCCESS)
522 goto auth_fail;
523
524 if (options.permit_pam_user_change) {
525 sshpam_check_userchanged();
526 }
527 if (compat20) {
528 if (!do_pam_account()) {
529 sshpam_err = PAM_ACCT_EXPIRED;
530 goto auth_fail;
531 }
532 if (sshpam_authctxt->force_pwchange) {
533 sshpam_err = pam_chauthtok(sshpam_handle,
534 PAM_CHANGE_EXPIRED_AUTHTOK);
535 if (sshpam_err != PAM_SUCCESS)
536 goto auth_fail;
537 sshpam_password_change_required(0);
538 }
539 }
540
541 buffer_put_cstring(&buffer, "OK");
542
543#ifndef UNSUPPORTED_POSIX_THREADS_HACK
544 /* Export variables set by do_pam_account */
545 buffer_put_int(&buffer, sshpam_account_status);
546 buffer_put_int(&buffer, sshpam_authctxt->force_pwchange);
547 if (options.permit_pam_user_change) {
548 buffer_put_cstring(&buffer, sshpam_authctxt->pw->pw_name);
549 }
550
551 /* Export any environment strings set in child */
552 for(i = 0; environ[i] != NULL; i++)
553 ; /* Count */
554 buffer_put_int(&buffer, i);
555 for(i = 0; environ[i] != NULL; i++)
556 buffer_put_cstring(&buffer, environ[i]);
557
558 /* Export any environment strings set by PAM in child */
559 env_from_pam = pam_getenvlist(sshpam_handle);
560 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
561 ; /* Count */
562 buffer_put_int(&buffer, i);
563 for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++)
564 buffer_put_cstring(&buffer, env_from_pam[i]);
565#endif /* UNSUPPORTED_POSIX_THREADS_HACK */
566
567 /* XXX - can't do much about an error here */
568 ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer);
569 buffer_free(&buffer);
570 pthread_exit(NULL);
571
572 auth_fail:
573 buffer_put_cstring(&buffer,
574 pam_strerror(sshpam_handle, sshpam_err));
575 /* XXX - can't do much about an error here */
576 if (sshpam_err == PAM_ACCT_EXPIRED)
577 ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, &buffer);
578 else
579 ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer);
580 buffer_free(&buffer);
581 pthread_exit(NULL);
582
583 return (NULL); /* Avoid warning for non-pthread case */
584}
585
586void
587sshpam_thread_cleanup(void)
588{
589 struct pam_ctxt *ctxt = cleanup_ctxt;
590
591 debug3("PAM: %s entering", __func__);
592 if (ctxt != NULL && ctxt->pam_thread != 0) {
593 pthread_cancel(ctxt->pam_thread);
594 pthread_join(ctxt->pam_thread, NULL);
595 close(ctxt->pam_psock);
596 close(ctxt->pam_csock);
597 memset(ctxt, 0, sizeof(*ctxt));
598 cleanup_ctxt = NULL;
599 }
600}
601
602static int
603sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
604 struct pam_response **resp, void *data)
605{
606 debug3("PAM: %s entering, %d messages", __func__, n);
607 return (PAM_CONV_ERR);
608}
609
610static struct pam_conv null_conv = { sshpam_null_conv, NULL };
611
612static int
613sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
614 struct pam_response **resp, void *data)
615{
616 struct pam_response *reply;
617 int i;
618 size_t len;
619
620 debug3("PAM: %s called with %d messages", __func__, n);
621 *resp = NULL;
622
623 if (n <= 0 || n > PAM_MAX_NUM_MSG)
624 return (PAM_CONV_ERR);
625
626 if ((reply = calloc(n, sizeof(*reply))) == NULL)
627 return (PAM_CONV_ERR);
628
629 for (i = 0; i < n; ++i) {
630 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
631 case PAM_ERROR_MSG:
632 case PAM_TEXT_INFO:
633 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
634 buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len);
635 buffer_append(&loginmsg, "\n", 1 );
636 reply[i].resp_retcode = PAM_SUCCESS;
637 break;
638 default:
639 goto fail;
640 }
641 }
642 *resp = reply;
643 return (PAM_SUCCESS);
644
645 fail:
646 for(i = 0; i < n; i++) {
647 if (reply[i].resp != NULL)
648 xfree(reply[i].resp);
649 }
650 xfree(reply);
651 return (PAM_CONV_ERR);
652}
653
654static struct pam_conv store_conv = { sshpam_store_conv, NULL };
655
656void
657sshpam_cleanup(void)
658{
659 if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
660 return;
661 debug("PAM: cleanup");
662 pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
663 if (sshpam_session_open) {
664 debug("PAM: closing session");
665 pam_close_session(sshpam_handle, PAM_SILENT);
666 sshpam_session_open = 0;
667 }
668 if (sshpam_cred_established) {
669 debug("PAM: deleting credentials");
670 pam_setcred(sshpam_handle, PAM_DELETE_CRED);
671 sshpam_cred_established = 0;
672 }
673 sshpam_authenticated = 0;
674 pam_end(sshpam_handle, sshpam_err);
675 sshpam_handle = NULL;
676}
677
678static int
679sshpam_init(Authctxt *authctxt)
680{
681 extern char *__progname;
682 const char *pam_rhost, *pam_user, *user = authctxt->user;
683 const char **ptr_pam_user = &pam_user;
684
685 if (sshpam_handle != NULL) {
686 /* We already have a PAM context; check if the user matches */
687 sshpam_err = pam_get_item(sshpam_handle,
688 PAM_USER, (sshpam_const void **)ptr_pam_user);
689 if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
690 return (0);
691 pam_end(sshpam_handle, sshpam_err);
692 sshpam_handle = NULL;
693 }
694 debug("PAM: initializing for \"%s\"", user);
695 sshpam_err =
696 pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
697 sshpam_authctxt = authctxt;
698
699 if (sshpam_err != PAM_SUCCESS) {
700 pam_end(sshpam_handle, sshpam_err);
701 sshpam_handle = NULL;
702 return (-1);
703 }
704 pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns);
705 debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost);
706 sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost);
707 if (sshpam_err != PAM_SUCCESS) {
708 pam_end(sshpam_handle, sshpam_err);
709 sshpam_handle = NULL;
710 return (-1);
711 }
712#ifdef PAM_TTY_KLUDGE
713 /*
714 * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
715 * sshd doesn't set the tty until too late in the auth process and
716 * may not even set one (for tty-less connections)
717 */
718 debug("PAM: setting PAM_TTY to \"ssh\"");
719 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
720 if (sshpam_err != PAM_SUCCESS) {
721 pam_end(sshpam_handle, sshpam_err);
722 sshpam_handle = NULL;
723 return (-1);
724 }
725#endif
726 return (0);
727}
728
729static void *
730sshpam_init_ctx(Authctxt *authctxt)
731{
732 struct pam_ctxt *ctxt;
733 int socks[2];
734
735 debug3("PAM: %s entering", __func__);
736 /*
737 * Refuse to start if we don't have PAM enabled or do_pam_account
738 * has previously failed.
739 */
740 if (!options.use_pam || sshpam_account_status == 0)
741 return NULL;
742
743 /* Initialize PAM */
744 if (sshpam_init(authctxt) == -1) {
745 error("PAM: initialization failed");
746 return (NULL);
747 }
748
749 ctxt = xcalloc(1, sizeof *ctxt);
750
751 /* Start the authentication thread */
752 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
753 error("PAM: failed create sockets: %s", strerror(errno));
754 xfree(ctxt);
755 return (NULL);
756 }
757 ctxt->pam_psock = socks[0];
758 ctxt->pam_csock = socks[1];
759 if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) {
760 error("PAM: failed to start authentication thread: %s",
761 strerror(errno));
762 close(socks[0]);
763 close(socks[1]);
764 xfree(ctxt);
765 return (NULL);
766 }
767 cleanup_ctxt = ctxt;
768 return (ctxt);
769}
770
771static int
772sshpam_query(void *ctx, char **name, char **info,
773 u_int *num, char ***prompts, u_int **echo_on)
774{
775 Buffer buffer;
776 struct pam_ctxt *ctxt = ctx;
777 size_t plen;
778 u_char type;
779 char *msg;
780 size_t len, mlen;
781
782 debug3("PAM: %s entering", __func__);
783 buffer_init(&buffer);
784 *name = xstrdup("");
785 *info = xstrdup("");
786 *prompts = xmalloc(sizeof(char *));
787 **prompts = NULL;
788 plen = 0;
789 *echo_on = xmalloc(sizeof(u_int));
790 while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) {
791 type = buffer_get_char(&buffer);
792 msg = buffer_get_string(&buffer, NULL);
793 mlen = strlen(msg);
794 switch (type) {
795 case PAM_PROMPT_ECHO_ON:
796 case PAM_PROMPT_ECHO_OFF:
797 *num = 1;
798 len = plen + mlen + 1;
799 **prompts = xrealloc(**prompts, 1, len);
800 strlcpy(**prompts + plen, msg, len - plen);
801 plen += mlen;
802 **echo_on = (type == PAM_PROMPT_ECHO_ON);
803 xfree(msg);
804 return (0);
805 case PAM_ERROR_MSG:
806 case PAM_TEXT_INFO:
807 /* accumulate messages */
808 len = plen + mlen + 2;
809 **prompts = xrealloc(**prompts, 1, len);
810 strlcpy(**prompts + plen, msg, len - plen);
811 plen += mlen;
812 strlcat(**prompts + plen, "\n", len - plen);
813 plen++;
814 xfree(msg);
815 break;
816 case PAM_ACCT_EXPIRED:
817 sshpam_account_status = 0;
818 /* FALLTHROUGH */
819 case PAM_AUTH_ERR:
820 debug3("PAM: %s", pam_strerror(sshpam_handle, type));
821 if (**prompts != NULL && strlen(**prompts) != 0) {
822 *info = **prompts;
823 **prompts = NULL;
824 *num = 0;
825 **echo_on = 0;
826 ctxt->pam_done = -1;
827 xfree(msg);
828 return 0;
829 }
830 /* FALLTHROUGH */
831 case PAM_SUCCESS:
832 if (**prompts != NULL) {
833 /* drain any accumulated messages */
834 debug("PAM: %s", **prompts);
835 buffer_append(&loginmsg, **prompts,
836 strlen(**prompts));
837 xfree(**prompts);
838 **prompts = NULL;
839 }
840 if (type == PAM_SUCCESS) {
841 if (!sshpam_authctxt->valid ||
842 (sshpam_authctxt->pw->pw_uid == 0 &&
843 options.permit_root_login != PERMIT_YES))
844 fatal("Internal error: PAM auth "
845 "succeeded when it should have "
846 "failed");
847 import_environments(&buffer);
848 *num = 0;
849 **echo_on = 0;
850 ctxt->pam_done = 1;
851 xfree(msg);
852 return (0);
853 }
854 error("PAM: %s for %s%.100s from %.100s", msg,
855 sshpam_authctxt->valid ? "" : "illegal user ",
856 sshpam_authctxt->user,
857 get_remote_name_or_ip(utmp_len, options.use_dns));
858 /* FALLTHROUGH */
859 default:
860 *num = 0;
861 **echo_on = 0;
862 xfree(msg);
863 ctxt->pam_done = -1;
864 return (-1);
865 }
866 }
867 return (-1);
868}
869
870/* XXX - see also comment in auth-chall.c:verify_response */
871static int
872sshpam_respond(void *ctx, u_int num, char **resp)
873{
874 Buffer buffer;
875 struct pam_ctxt *ctxt = ctx;
876
877 debug2("PAM: %s entering, %u responses", __func__, num);
878 switch (ctxt->pam_done) {
879 case 1:
880 sshpam_authenticated = 1;
881 return (0);
882 case 0:
883 break;
884 default:
885 return (-1);
886 }
887 if (num != 1) {
888 error("PAM: expected one response, got %u", num);
889 return (-1);
890 }
891 buffer_init(&buffer);
892 if (sshpam_authctxt->valid &&
893 (sshpam_authctxt->pw->pw_uid != 0 ||
894 options.permit_root_login == PERMIT_YES))
895 buffer_put_cstring(&buffer, *resp);
896 else
897 buffer_put_cstring(&buffer, badpw);
898 if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) {
899 buffer_free(&buffer);
900 return (-1);
901 }
902 buffer_free(&buffer);
903 return (1);
904}
905
906static void
907sshpam_free_ctx(void *ctxtp)
908{
909 struct pam_ctxt *ctxt = ctxtp;
910
911 debug3("PAM: %s entering", __func__);
912 sshpam_thread_cleanup();
913 xfree(ctxt);
914 /*
915 * We don't call sshpam_cleanup() here because we may need the PAM
916 * handle at a later stage, e.g. when setting up a session. It's
917 * still on the cleanup list, so pam_end() *will* be called before
918 * the server process terminates.
919 */
920}
921
922KbdintDevice sshpam_device = {
923 "pam",
924 sshpam_init_ctx,
925 sshpam_query,
926 sshpam_respond,
927 sshpam_free_ctx
928};
929
930KbdintDevice mm_sshpam_device = {
931 "pam",
932 mm_sshpam_init_ctx,
933 mm_sshpam_query,
934 mm_sshpam_respond,
935 mm_sshpam_free_ctx
936};
937
938/*
939 * This replaces auth-pam.c
940 */
941void
942start_pam(Authctxt *authctxt)
943{
944 if (!options.use_pam)
945 fatal("PAM: initialisation requested when UsePAM=no");
946
947 if (sshpam_init(authctxt) == -1)
948 fatal("PAM: initialisation failed");
949}
950
951void
952finish_pam(void)
953{
954 sshpam_cleanup();
955}
956
957u_int
958do_pam_account(void)
959{
960 debug("%s: called", __func__);
961 if (sshpam_account_status != -1)
962 return (sshpam_account_status);
963
964 sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
965 debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
966 pam_strerror(sshpam_handle, sshpam_err));
967
968 if (options.permit_pam_user_change) {
969 sshpam_check_userchanged();
970 if (getpwnam(sshpam_authctxt->pw->pw_name) == NULL)
971 fatal("PAM: completed authentication but PAM account invalid");
972 }
973
974 if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
975 sshpam_account_status = 0;
976 return (sshpam_account_status);
977 }
978
979 if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
980 sshpam_password_change_required(1);
981
982 sshpam_account_status = 1;
983 return (sshpam_account_status);
984}
985
986void
987do_pam_set_tty(const char *tty)
988{
989 if (tty != NULL) {
990 debug("PAM: setting PAM_TTY to \"%s\"", tty);
991 sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty);
992 if (sshpam_err != PAM_SUCCESS)
993 fatal("PAM: failed to set PAM_TTY: %s",
994 pam_strerror(sshpam_handle, sshpam_err));
995 }
996}
997
998void
999do_pam_setcred(int init)
1000{
1001 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1002 (const void *)&store_conv);
1003 if (sshpam_err != PAM_SUCCESS)
1004 fatal("PAM: failed to set PAM_CONV: %s",
1005 pam_strerror(sshpam_handle, sshpam_err));
1006 if (init) {
1007 debug("PAM: establishing credentials");
1008 sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
1009 } else {
1010 debug("PAM: reinitializing credentials");
1011 sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
1012 }
1013 if (sshpam_err == PAM_SUCCESS) {
1014 sshpam_cred_established = 1;
1015 return;
1016 }
1017 if (sshpam_authenticated)
1018 fatal("PAM: pam_setcred(): %s",
1019 pam_strerror(sshpam_handle, sshpam_err));
1020 else
1021 debug("PAM: pam_setcred(): %s",
1022 pam_strerror(sshpam_handle, sshpam_err));
1023}
1024
1025static int
1026sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
1027 struct pam_response **resp, void *data)
1028{
1029 char input[PAM_MAX_MSG_SIZE];
1030 struct pam_response *reply;
1031 int i;
1032
1033 debug3("PAM: %s called with %d messages", __func__, n);
1034
1035 *resp = NULL;
1036
1037 if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
1038 return (PAM_CONV_ERR);
1039
1040 if ((reply = calloc(n, sizeof(*reply))) == NULL)
1041 return (PAM_CONV_ERR);
1042
1043 for (i = 0; i < n; ++i) {
1044 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1045 case PAM_PROMPT_ECHO_OFF:
1046 reply[i].resp =
1047 read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
1048 RP_ALLOW_STDIN);
1049 reply[i].resp_retcode = PAM_SUCCESS;
1050 break;
1051 case PAM_PROMPT_ECHO_ON:
1052 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1053 if (fgets(input, sizeof input, stdin) == NULL)
1054 input[0] = '\0';
1055 if ((reply[i].resp = strdup(input)) == NULL)
1056 goto fail;
1057 reply[i].resp_retcode = PAM_SUCCESS;
1058 break;
1059 case PAM_ERROR_MSG:
1060 case PAM_TEXT_INFO:
1061 fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
1062 reply[i].resp_retcode = PAM_SUCCESS;
1063 break;
1064 default:
1065 goto fail;
1066 }
1067 }
1068 *resp = reply;
1069 return (PAM_SUCCESS);
1070
1071 fail:
1072 for(i = 0; i < n; i++) {
1073 if (reply[i].resp != NULL)
1074 xfree(reply[i].resp);
1075 }
1076 xfree(reply);
1077 return (PAM_CONV_ERR);
1078}
1079
1080static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
1081
1082/*
1083 * XXX this should be done in the authentication phase, but ssh1 doesn't
1084 * support that
1085 */
1086void
1087do_pam_chauthtok(void)
1088{
1089 if (use_privsep)
1090 fatal("Password expired (unable to change with privsep)");
1091 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1092 (const void *)&tty_conv);
1093 if (sshpam_err != PAM_SUCCESS)
1094 fatal("PAM: failed to set PAM_CONV: %s",
1095 pam_strerror(sshpam_handle, sshpam_err));
1096 debug("PAM: changing password");
1097 sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
1098 if (sshpam_err != PAM_SUCCESS)
1099 fatal("PAM: pam_chauthtok(): %s",
1100 pam_strerror(sshpam_handle, sshpam_err));
1101}
1102
1103void
1104do_pam_session(void)
1105{
1106 debug3("PAM: opening session");
1107 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1108 (const void *)&store_conv);
1109 if (sshpam_err != PAM_SUCCESS)
1110 fatal("PAM: failed to set PAM_CONV: %s",
1111 pam_strerror(sshpam_handle, sshpam_err));
1112 sshpam_err = pam_open_session(sshpam_handle, 0);
1113 if (sshpam_err == PAM_SUCCESS)
1114 sshpam_session_open = 1;
1115 else {
1116 sshpam_session_open = 0;
1117 disable_forwarding();
1118 error("PAM: pam_open_session(): %s",
1119 pam_strerror(sshpam_handle, sshpam_err));
1120 }
1121
1122}
1123
1124int
1125is_pam_session_open(void)
1126{
1127 return sshpam_session_open;
1128}
1129
1130/*
1131 * Set a PAM environment string. We need to do this so that the session
1132 * modules can handle things like Kerberos/GSI credentials that appear
1133 * during the ssh authentication process.
1134 */
1135int
1136do_pam_putenv(char *name, char *value)
1137{
1138 int ret = 1;
1139#ifdef HAVE_PAM_PUTENV
1140 char *compound;
1141 size_t len;
1142
1143 len = strlen(name) + strlen(value) + 2;
1144 compound = xmalloc(len);
1145
1146 snprintf(compound, len, "%s=%s", name, value);
1147 ret = pam_putenv(sshpam_handle, compound);
1148 xfree(compound);
1149#endif
1150
1151 return (ret);
1152}
1153
1154char **
1155fetch_pam_child_environment(void)
1156{
1157 return sshpam_env;
1158}
1159
1160char **
1161fetch_pam_environment(void)
1162{
1163 return (pam_getenvlist(sshpam_handle));
1164}
1165
1166void
1167free_pam_environment(char **env)
1168{
1169 char **envp;
1170
1171 if (env == NULL)
1172 return;
1173
1174 for (envp = env; *envp; envp++)
1175 xfree(*envp);
1176 xfree(env);
1177}
1178
1179/*
1180 * "Blind" conversation function for password authentication. Assumes that
1181 * echo-off prompts are for the password and stores messages for later
1182 * display.
1183 */
1184static int
1185sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
1186 struct pam_response **resp, void *data)
1187{
1188 struct pam_response *reply;
1189 int i;
1190 size_t len;
1191
1192 debug3("PAM: %s called with %d messages", __func__, n);
1193
1194 *resp = NULL;
1195
1196 if (n <= 0 || n > PAM_MAX_NUM_MSG)
1197 return (PAM_CONV_ERR);
1198
1199 if ((reply = calloc(n, sizeof(*reply))) == NULL)
1200 return (PAM_CONV_ERR);
1201
1202 for (i = 0; i < n; ++i) {
1203 switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
1204 case PAM_PROMPT_ECHO_OFF:
1205 if (sshpam_password == NULL)
1206 goto fail;
1207 if ((reply[i].resp = strdup(sshpam_password)) == NULL)
1208 goto fail;
1209 reply[i].resp_retcode = PAM_SUCCESS;
1210 break;
1211 case PAM_ERROR_MSG:
1212 case PAM_TEXT_INFO:
1213 len = strlen(PAM_MSG_MEMBER(msg, i, msg));
1214 if (len > 0) {
1215 buffer_append(&loginmsg,
1216 PAM_MSG_MEMBER(msg, i, msg), len);
1217 buffer_append(&loginmsg, "\n", 1);
1218 }
1219 if ((reply[i].resp = strdup("")) == NULL)
1220 goto fail;
1221 reply[i].resp_retcode = PAM_SUCCESS;
1222 break;
1223 default:
1224 goto fail;
1225 }
1226 }
1227 *resp = reply;
1228 return (PAM_SUCCESS);
1229
1230 fail:
1231 for(i = 0; i < n; i++) {
1232 if (reply[i].resp != NULL)
1233 xfree(reply[i].resp);
1234 }
1235 xfree(reply);
1236 return (PAM_CONV_ERR);
1237}
1238
1239static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
1240
1241/*
1242 * Attempt password authentication via PAM
1243 */
1244int
1245sshpam_auth_passwd(Authctxt *authctxt, const char *password)
1246{
1247 int flags = (options.permit_empty_passwd == 0 ?
1248 PAM_DISALLOW_NULL_AUTHTOK : 0);
1249
1250 if (!options.use_pam || sshpam_handle == NULL)
1251 fatal("PAM: %s called when PAM disabled or failed to "
1252 "initialise.", __func__);
1253
1254 sshpam_password = password;
1255 sshpam_authctxt = authctxt;
1256
1257 /*
1258 * If the user logging in is invalid, or is root but is not permitted
1259 * by PermitRootLogin, use an invalid password to prevent leaking
1260 * information via timing (eg if the PAM config has a delay on fail).
1261 */
1262 if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
1263 options.permit_root_login != PERMIT_YES))
1264 sshpam_password = badpw;
1265
1266 sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
1267 (const void *)&passwd_conv);
1268 if (sshpam_err != PAM_SUCCESS)
1269 fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
1270 pam_strerror(sshpam_handle, sshpam_err));
1271
1272 sshpam_err = pam_authenticate(sshpam_handle, flags);
1273 if (options.permit_pam_user_change) {
1274 sshpam_check_userchanged();
1275 }
1276 sshpam_password = NULL;
1277 if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
1278 debug("PAM: password authentication accepted for %.100s",
1279 authctxt->user);
1280 return 1;
1281 } else {
1282 debug("PAM: password authentication failed for %.100s: %s",
1283 authctxt->valid ? authctxt->user : "an illegal user",
1284 pam_strerror(sshpam_handle, sshpam_err));
1285 return 0;
1286 }
1287}
1288#endif /* USE_PAM */
This page took 0.139877 seconds and 5 git commands to generate.