| 1 |
/*
|
| 2 |
* Copyright (C) 2002 Brad House <brad@mainstreetsoftworks.com>
|
| 3 |
* Distributed under the terms of the GNU General Public License, v2 or later
|
| 4 |
* Author: Brad House <brad@mainstreetsoftworks.com>
|
| 5 |
*
|
| 6 |
* $Header$
|
| 7 |
*
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include <errno.h>
|
| 11 |
#include <stdio.h>
|
| 12 |
#include <stdlib.h>
|
| 13 |
#include <limits.h>
|
| 14 |
#include <string.h>
|
| 15 |
#include <sys/stat.h>
|
| 16 |
#include <unistd.h>
|
| 17 |
#include <fcntl.h>
|
| 18 |
#include <libgen.h>
|
| 19 |
|
| 20 |
#include "sandbox.h"
|
| 21 |
#include "config.h"
|
| 22 |
|
| 23 |
/* glibc modified getcwd() functions */
|
| 24 |
char *egetcwd(char *, size_t);
|
| 25 |
|
| 26 |
void get_sandbox_lib(char *path)
|
| 27 |
{
|
| 28 |
#ifdef SB_HAVE_MULTILIB
|
| 29 |
snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
|
| 30 |
#else
|
| 31 |
snprintf(path, SB_PATH_MAX, "%s/%s", LIBSANDBOX_PATH, LIB_NAME);
|
| 32 |
if (0 >= exists(path)) {
|
| 33 |
snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
|
| 34 |
}
|
| 35 |
#endif
|
| 36 |
}
|
| 37 |
|
| 38 |
#ifdef OUTSIDE_LIBSANDBOX
|
| 39 |
|
| 40 |
void get_sandbox_rc(char *path)
|
| 41 |
{
|
| 42 |
snprintf(path, SB_PATH_MAX, "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
|
| 43 |
}
|
| 44 |
|
| 45 |
void get_sandbox_log(char *path)
|
| 46 |
{
|
| 47 |
char *sandbox_log_env = NULL;
|
| 48 |
|
| 49 |
sandbox_log_env = getenv(ENV_SANDBOX_LOG);
|
| 50 |
|
| 51 |
/* THIS CHUNK BREAK THINGS BY DOING THIS:
|
| 52 |
* SANDBOX_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
|
| 53 |
*/
|
| 54 |
if ((NULL != sandbox_log_env) &&
|
| 55 |
(NULL != strchr(sandbox_log_env, '/')))
|
| 56 |
sandbox_log_env = NULL;
|
| 57 |
|
| 58 |
snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
|
| 59 |
SANDBOX_LOG_LOCATION, LOG_FILE_PREFIX,
|
| 60 |
(sandbox_log_env == NULL ? "" : sandbox_log_env),
|
| 61 |
(sandbox_log_env == NULL ? "" : "-"),
|
| 62 |
getpid(), LOG_FILE_EXT);
|
| 63 |
}
|
| 64 |
|
| 65 |
void get_sandbox_debug_log(char *path)
|
| 66 |
{
|
| 67 |
char *sandbox_debug_log_env = NULL;
|
| 68 |
|
| 69 |
sandbox_debug_log_env = getenv(ENV_SANDBOX_DEBUG_LOG);
|
| 70 |
|
| 71 |
/* THIS CHUNK BREAK THINGS BY DOING THIS:
|
| 72 |
* SANDBOX_DEBUG_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
|
| 73 |
*/
|
| 74 |
if ((NULL != sandbox_debug_log_env) &&
|
| 75 |
(NULL != strchr(sandbox_debug_log_env, '/')))
|
| 76 |
sandbox_debug_log_env = NULL;
|
| 77 |
|
| 78 |
snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
|
| 79 |
SANDBOX_LOG_LOCATION, DEBUG_LOG_FILE_PREFIX,
|
| 80 |
(sandbox_debug_log_env == NULL ? "" : sandbox_debug_log_env),
|
| 81 |
(sandbox_debug_log_env == NULL ? "" : "-"),
|
| 82 |
getpid(), LOG_FILE_EXT);
|
| 83 |
}
|
| 84 |
|
| 85 |
int get_tmp_dir(char *path)
|
| 86 |
{
|
| 87 |
if (NULL == realpath(getenv(ENV_TMPDIR) ? getenv(ENV_TMPDIR)
|
| 88 |
: TMPDIR,
|
| 89 |
path)) {
|
| 90 |
if (NULL == realpath(TMPDIR, path))
|
| 91 |
return -1;
|
| 92 |
}
|
| 93 |
|
| 94 |
return 0;
|
| 95 |
}
|
| 96 |
|
| 97 |
#endif /* OUTSIDE_LIBSANDBOX */
|
| 98 |
|
| 99 |
int exists(const char *pathname)
|
| 100 |
{
|
| 101 |
struct stat buf;
|
| 102 |
int retval;
|
| 103 |
|
| 104 |
if ((NULL == pathname) || (0 == strlen(pathname)))
|
| 105 |
return 0;
|
| 106 |
|
| 107 |
retval = lstat(pathname, &buf);
|
| 108 |
if (-1 != retval)
|
| 109 |
return 1;
|
| 110 |
/* Some or other error occurred */
|
| 111 |
if (ENOENT != errno)
|
| 112 |
return -1;
|
| 113 |
|
| 114 |
return 0;
|
| 115 |
}
|
| 116 |
|
| 117 |
#ifdef OUTSIDE_LIBSANDBOX
|
| 118 |
|
| 119 |
int is_file(const char *pathname)
|
| 120 |
{
|
| 121 |
struct stat buf;
|
| 122 |
int retval;
|
| 123 |
|
| 124 |
if ((NULL == pathname) || (0 == strlen(pathname)))
|
| 125 |
return 0;
|
| 126 |
|
| 127 |
retval = lstat(pathname, &buf);
|
| 128 |
if ((-1 != retval) && (S_ISREG(buf.st_mode)))
|
| 129 |
return 1;
|
| 130 |
/* Some or other error occurred */
|
| 131 |
if ((-1 == retval) && (ENOENT != errno))
|
| 132 |
return -1;
|
| 133 |
|
| 134 |
return 0;
|
| 135 |
}
|
| 136 |
|
| 137 |
int is_dir(const char *pathname, int follow_link)
|
| 138 |
{
|
| 139 |
struct stat buf;
|
| 140 |
int retval;
|
| 141 |
|
| 142 |
if ((NULL == pathname) || (0 == strlen(pathname)))
|
| 143 |
return 0;
|
| 144 |
|
| 145 |
retval = follow_link ? stat(pathname, &buf) : lstat(pathname, &buf);
|
| 146 |
if ((-1 != retval) && (S_ISDIR(buf.st_mode)))
|
| 147 |
return 1;
|
| 148 |
/* Some or other error occurred */
|
| 149 |
if ((-1 == retval) && (ENOENT != errno))
|
| 150 |
return -1;
|
| 151 |
|
| 152 |
return 0;
|
| 153 |
}
|
| 154 |
|
| 155 |
long file_length(int fd)
|
| 156 |
{
|
| 157 |
long pos, len;
|
| 158 |
|
| 159 |
pos = lseek(fd, 0L, SEEK_CUR);
|
| 160 |
len = lseek(fd, 0L, SEEK_END);
|
| 161 |
lseek(fd, pos, SEEK_SET);
|
| 162 |
|
| 163 |
return (len);
|
| 164 |
}
|
| 165 |
|
| 166 |
#endif /* OUTSIDE_LIBSANDBOX */
|
| 167 |
|
| 168 |
|
| 169 |
// vim:noexpandtab noai:cindent ai
|