| 1 |
/* |
| 2 |
libfakechroot -- fake chroot environment |
| 3 |
Copyright (c) 2003-2015 Piotr Roszatycki <[email protected]> |
| 4 |
Copyright (c) 2007 Mark Eichin <[email protected]> |
| 5 |
Copyright (c) 2006, 2007 Alexander Shishkin <[email protected]> |
| 6 |
|
| 7 |
klik2 support -- give direct access to a list of directories |
| 8 |
Copyright (c) 2006, 2007 Lionel Tricon <[email protected]> |
| 9 |
|
| 10 |
This library is free software; you can redistribute it and/or |
| 11 |
modify it under the terms of the GNU Lesser General Public |
| 12 |
License as published by the Free Software Foundation; either |
| 13 |
version 2.1 of the License, or (at your option) any later version. |
| 14 |
|
| 15 |
This library is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
Lesser General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU Lesser General Public |
| 21 |
License along with this library; if not, write to the Free Software |
| 22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <config.h> |
| 26 |
|
| 27 |
#define _GNU_SOURCE |
| 28 |
|
| 29 |
#include <stdarg.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <string.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <pwd.h> |
| 35 |
#include <dlfcn.h> |
| 36 |
|
| 37 |
#include "setenv.h" |
| 38 |
#include "libfakechroot.h" |
| 39 |
#include "getcwd_real.h" |
| 40 |
#include "strchrnul.h" |
| 41 |
|
| 42 |
#define EXCLUDE_LIST_SIZE 100 |
| 43 |
|
| 44 |
|
| 45 |
/* Useful to exclude a list of directories or files */ |
| 46 |
static char *exclude_list[EXCLUDE_LIST_SIZE]; |
| 47 |
static int exclude_length[EXCLUDE_LIST_SIZE]; |
| 48 |
static int list_max = 0; |
| 49 |
static int first = 0; |
| 50 |
|
| 51 |
|
| 52 |
/* List of environment variables to preserve on clearenv() */ |
| 53 |
char *preserve_env_list[] = { |
| 54 |
"FAKECHROOT_BASE", |
| 55 |
"FAKECHROOT_CMD_SUBST", |
| 56 |
"FAKECHROOT_DEBUG", |
| 57 |
"FAKECHROOT_DETECT", |
| 58 |
"FAKECHROOT_ELFLOADER", |
| 59 |
"FAKECHROOT_ELFLOADER_OPT_ARGV0", |
| 60 |
"FAKECHROOT_EXCLUDE_PATH", |
| 61 |
"FAKECHROOT_LDLIBPATH", |
| 62 |
"FAKECHROOT_VERSION", |
| 63 |
"FAKEROOTKEY", |
| 64 |
"FAKED_MODE", |
| 65 |
"LD_LIBRARY_PATH", |
| 66 |
"LD_PRELOAD" |
| 67 |
}; |
| 68 |
const int preserve_env_list_count = sizeof preserve_env_list / sizeof preserve_env_list[0]; |
| 69 |
|
| 70 |
|
| 71 |
LOCAL int fakechroot_debug (const char *fmt, ...) |
| 72 |
{ |
| 73 |
int ret; |
| 74 |
char newfmt[2048]; |
| 75 |
|
| 76 |
va_list ap; |
| 77 |
va_start(ap, fmt); |
| 78 |
|
| 79 |
if (!getenv("FAKECHROOT_DEBUG")) |
| 80 |
return 0; |
| 81 |
|
| 82 |
sprintf(newfmt, PACKAGE ": %s\n", fmt); |
| 83 |
|
| 84 |
ret = vfprintf(stderr, newfmt, ap); |
| 85 |
va_end(ap); |
| 86 |
|
| 87 |
return ret; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
#include "getcwd.h" |
| 92 |
|
| 93 |
|
| 94 |
/* Bootstrap the library */ |
| 95 |
void fakechroot_init (void) CONSTRUCTOR; |
| 96 |
void fakechroot_init (void) |
| 97 |
{ |
| 98 |
char *detect = getenv("FAKECHROOT_DETECT"); |
| 99 |
|
| 100 |
|
| 101 |
if (detect) { |
| 102 |
/* printf causes coredump on FreeBSD */ |
| 103 |
if (write(STDOUT_FILENO, PACKAGE, sizeof(PACKAGE)-1) && |
| 104 |
write(STDOUT_FILENO, " ", 1) && |
| 105 |
write(STDOUT_FILENO, VERSION, sizeof(VERSION)-1) && |
| 106 |
write(STDOUT_FILENO, "\n", 1)) { /* -Wunused-result */ } |
| 107 |
_Exit(atoi(detect)); |
| 108 |
} |
| 109 |
|
| 110 |
debug("fakechroot_init()"); |
| 111 |
debug("FAKECHROOT_BASE=\"%s\"", getenv("FAKECHROOT_BASE")); |
| 112 |
debug("FAKECHROOT_BASE_ORIG=\"%s\"", getenv("FAKECHROOT_BASE_ORIG")); |
| 113 |
debug("FAKECHROOT_CMD_ORIG=\"%s\"", getenv("FAKECHROOT_CMD_ORIG")); |
| 114 |
|
| 115 |
if (!first) { |
| 116 |
char *exclude_path = getenv("FAKECHROOT_EXCLUDE_PATH"); |
| 117 |
|
| 118 |
first = 1; |
| 119 |
|
| 120 |
/* We get a list of directories or files */ |
| 121 |
if (exclude_path) { |
| 122 |
int i; |
| 123 |
for (i = 0; list_max < EXCLUDE_LIST_SIZE; ) { |
| 124 |
int j; |
| 125 |
for (j = i; exclude_path[j] != ':' && exclude_path[j] != '\0'; j++); |
| 126 |
exclude_list[list_max] = malloc(j - i + 2); |
| 127 |
memset(exclude_list[list_max], '\0', j - i + 2); |
| 128 |
strncpy(exclude_list[list_max], &(exclude_path[i]), j - i); |
| 129 |
exclude_length[list_max] = strlen(exclude_list[list_max]); |
| 130 |
list_max++; |
| 131 |
if (exclude_path[j] != ':') break; |
| 132 |
i = j + 1; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
__setenv("FAKECHROOT", "true", 1); |
| 137 |
__setenv("FAKECHROOT_VERSION", FAKECHROOT, 1); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/* Lazily load function */ |
| 143 |
LOCAL fakechroot_wrapperfn_t fakechroot_loadfunc (struct fakechroot_wrapper * w) |
| 144 |
{ |
| 145 |
char *msg; |
| 146 |
if (!(w->nextfunc = dlsym(RTLD_NEXT, w->name))) {; |
| 147 |
msg = dlerror(); |
| 148 |
fprintf(stderr, "%s: %s: %s\n", PACKAGE, w->name, msg != NULL ? msg : "unresolved symbol"); |
| 149 |
exit(EXIT_FAILURE); |
| 150 |
} |
| 151 |
return w->nextfunc; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/* Check if path is on exclude list */ |
| 156 |
LOCAL int fakechroot_localdir (const char * p_path) |
| 157 |
{ |
| 158 |
char *v_path = (char *)p_path; |
| 159 |
char cwd_path[FAKECHROOT_PATH_MAX]; |
| 160 |
|
| 161 |
if (!p_path) |
| 162 |
return 0; |
| 163 |
|
| 164 |
if (!first) |
| 165 |
fakechroot_init(); |
| 166 |
|
| 167 |
/* We need to expand relative paths */ |
| 168 |
if (p_path[0] != '/') { |
| 169 |
getcwd_real(cwd_path, FAKECHROOT_PATH_MAX); |
| 170 |
v_path = cwd_path; |
| 171 |
narrow_chroot_path(v_path); |
| 172 |
} |
| 173 |
|
| 174 |
/* We try to find if we need direct access to a file */ |
| 175 |
{ |
| 176 |
const size_t len = strlen(v_path); |
| 177 |
int i; |
| 178 |
|
| 179 |
for (i = 0; i < list_max; i++) { |
| 180 |
if (exclude_length[i] > len || |
| 181 |
v_path[exclude_length[i] - 1] != (exclude_list[i])[exclude_length[i] - 1] || |
| 182 |
strncmp(exclude_list[i], v_path, exclude_length[i]) != 0) continue; |
| 183 |
if (exclude_length[i] == len || v_path[exclude_length[i]] == '/') return 1; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/* |
| 192 |
* Parse the FAKECHROOT_CMD_SUBST environment variable (the first |
| 193 |
* parameter) and if there is a match with filename, return the |
| 194 |
* substitution in cmd_subst. Returns non-zero if there was a match. |
| 195 |
* |
| 196 |
* FAKECHROOT_CMD_SUBST=cmd=subst:cmd=subst:... |
| 197 |
*/ |
| 198 |
LOCAL int fakechroot_try_cmd_subst (char * env, const char * filename, char * cmd_subst) |
| 199 |
{ |
| 200 |
int len, len2; |
| 201 |
char *p; |
| 202 |
|
| 203 |
if (env == NULL || filename == NULL) |
| 204 |
return 0; |
| 205 |
|
| 206 |
/* Remove trailing dot from filename */ |
| 207 |
if (filename[0] == '.' && filename[1] == '/') |
| 208 |
filename++; |
| 209 |
len = strlen(filename); |
| 210 |
|
| 211 |
do { |
| 212 |
p = strchrnul(env, ':'); |
| 213 |
|
| 214 |
if (strncmp(env, filename, len) == 0 && env[len] == '=') { |
| 215 |
len2 = p - &env[len+1]; |
| 216 |
if (len2 >= FAKECHROOT_PATH_MAX) |
| 217 |
len2 = FAKECHROOT_PATH_MAX - 1; |
| 218 |
strncpy(cmd_subst, &env[len+1], len2); |
| 219 |
cmd_subst[len2] = '\0'; |
| 220 |
return 1; |
| 221 |
} |
| 222 |
|
| 223 |
env = p; |
| 224 |
} while (*env++ != '\0'); |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |