1*242be47eSzrj /*- 2*242be47eSzrj * Copyright (c) 2003 Networks Associates Technology, Inc. 3*242be47eSzrj * All rights reserved. 4*242be47eSzrj * 5*242be47eSzrj * This software was developed for the FreeBSD Project by ThinkSec AS and 6*242be47eSzrj * NAI Labs, the Security Research Division of Network Associates, Inc. 7*242be47eSzrj * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 8*242be47eSzrj * DARPA CHATS research program. 9*242be47eSzrj * 10*242be47eSzrj * Redistribution and use in source and binary forms, with or without 11*242be47eSzrj * modification, are permitted provided that the following conditions 12*242be47eSzrj * are met: 13*242be47eSzrj * 1. Redistributions of source code must retain the above copyright 14*242be47eSzrj * notice, this list of conditions and the following disclaimer. 15*242be47eSzrj * 2. Redistributions in binary form must reproduce the above copyright 16*242be47eSzrj * notice, this list of conditions and the following disclaimer in the 17*242be47eSzrj * documentation and/or other materials provided with the distribution. 18*242be47eSzrj * 3. The name of the author may not be used to endorse or promote 19*242be47eSzrj * products derived from this software without specific prior written 20*242be47eSzrj * permission. 21*242be47eSzrj * 22*242be47eSzrj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23*242be47eSzrj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24*242be47eSzrj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25*242be47eSzrj * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26*242be47eSzrj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27*242be47eSzrj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28*242be47eSzrj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29*242be47eSzrj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30*242be47eSzrj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31*242be47eSzrj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32*242be47eSzrj * SUCH DAMAGE. 33*242be47eSzrj * 34*242be47eSzrj * $FreeBSD: src/lib/libpam/modules/pam_chroot/pam_chroot.c,v 1.3 2003/04/30 00:40:24 des Exp $ 35*242be47eSzrj */ 36*242be47eSzrj 37*242be47eSzrj #include <sys/param.h> 38*242be47eSzrj 39*242be47eSzrj #include <pwd.h> 40*242be47eSzrj #include <stdio.h> 41*242be47eSzrj #include <string.h> 42*242be47eSzrj #include <unistd.h> 43*242be47eSzrj 44*242be47eSzrj #define PAM_SM_SESSION 45*242be47eSzrj 46*242be47eSzrj #include <security/pam_appl.h> 47*242be47eSzrj #include <security/pam_modules.h> 48*242be47eSzrj #include <security/openpam.h> 49*242be47eSzrj 50*242be47eSzrj PAM_EXTERN int 51*242be47eSzrj pam_sm_open_session(pam_handle_t *pamh, int flags __unused, 52*242be47eSzrj int argc __unused, const char *argv[] __unused) 53*242be47eSzrj { 54*242be47eSzrj const char *dir, *end, *cwd, *user; 55*242be47eSzrj struct passwd *pwd; 56*242be47eSzrj char buf[PATH_MAX]; 57*242be47eSzrj 58*242be47eSzrj if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS || 59*242be47eSzrj user == NULL || (pwd = getpwnam(user)) == NULL) 60*242be47eSzrj return (PAM_SESSION_ERR); 61*242be47eSzrj if (pwd->pw_uid == 0 && !openpam_get_option(pamh, "also_root")) 62*242be47eSzrj return (PAM_SUCCESS); 63*242be47eSzrj if (pwd->pw_dir == NULL) 64*242be47eSzrj return (PAM_SESSION_ERR); 65*242be47eSzrj if ((end = strstr(pwd->pw_dir, "/./")) != NULL) { 66*242be47eSzrj if (snprintf(buf, sizeof(buf), "%.*s", 67*242be47eSzrj (int)(end - pwd->pw_dir), pwd->pw_dir) > (int)sizeof(buf)) { 68*242be47eSzrj openpam_log(PAM_LOG_ERROR, 69*242be47eSzrj "%s's home directory is too long", user); 70*242be47eSzrj return (PAM_SESSION_ERR); 71*242be47eSzrj } 72*242be47eSzrj dir = buf; 73*242be47eSzrj cwd = end + 2; 74*242be47eSzrj } else if ((dir = openpam_get_option(pamh, "dir")) != NULL) { 75*242be47eSzrj if ((cwd = openpam_get_option(pamh, "cwd")) == NULL) 76*242be47eSzrj cwd = "/"; 77*242be47eSzrj } else { 78*242be47eSzrj if (openpam_get_option(pamh, "always")) { 79*242be47eSzrj openpam_log(PAM_LOG_ERROR, 80*242be47eSzrj "%s has no chroot directory", user); 81*242be47eSzrj return (PAM_SESSION_ERR); 82*242be47eSzrj } 83*242be47eSzrj return (PAM_SUCCESS); 84*242be47eSzrj } 85*242be47eSzrj 86*242be47eSzrj openpam_log(PAM_LOG_DEBUG, "chrooting %s to %s", dir, user); 87*242be47eSzrj 88*242be47eSzrj if (chroot(dir) == -1) { 89*242be47eSzrj openpam_log(PAM_LOG_ERROR, "chroot(): %m"); 90*242be47eSzrj return (PAM_SESSION_ERR); 91*242be47eSzrj } 92*242be47eSzrj if (chdir(cwd) == -1) { 93*242be47eSzrj openpam_log(PAM_LOG_ERROR, "chdir(): %m"); 94*242be47eSzrj return (PAM_SESSION_ERR); 95*242be47eSzrj } 96*242be47eSzrj pam_setenv(pamh, "HOME", cwd, 1); 97*242be47eSzrj return (PAM_SUCCESS); 98*242be47eSzrj } 99*242be47eSzrj 100*242be47eSzrj PAM_EXTERN int 101*242be47eSzrj pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused, 102*242be47eSzrj int argc __unused, const char *argv[] __unused) 103*242be47eSzrj { 104*242be47eSzrj 105*242be47eSzrj return (PAM_SUCCESS); 106*242be47eSzrj } 107*242be47eSzrj 108*242be47eSzrj PAM_MODULE_ENTRY("pam_chroot"); 109