/[path-sandbox]/trunk/sandbox_futils.c
Gentoo

Diff of /trunk/sandbox_futils.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 30 Revision 43
29 29
30#include "sandbox.h" 30#include "sandbox.h"
31#include "config.h" 31#include "config.h"
32 32
33/* BEGIN Prototypes */ 33/* BEGIN Prototypes */
34int file_security_check(char *filename); 34SB_STATIC int file_security_check(char *filename);
35/* END Prototypes */ 35/* END Prototypes */
36 36
37/* glibc modified getcwd() functions */ 37/* glibc modified getcwd() functions */
38char *egetcwd(char *, size_t); 38SB_STATIC char *egetcwd(char *, size_t);
39 39
40char *get_sandbox_path(char *argv0) 40SB_STATIC char *get_sandbox_path(char *argv0)
41{ 41{
42 char path[255]; 42 char path[255];
43 char *cwd = NULL; 43 char *cwd = NULL;
44 44
45 memset(path, 0, sizeof(path)); 45 memset(path, 0, sizeof(path));
58 58
59 /* Return just directory */ 59 /* Return just directory */
60 return (sb_dirname(path)); 60 return (sb_dirname(path));
61} 61}
62 62
63char *get_sandbox_lib(char *sb_path) 63SB_STATIC char *get_sandbox_lib(char *sb_path)
64{ 64{
65 char path[255]; 65 char path[255];
66 66
67#ifdef SB_HAVE_64BIT_ARCH 67#ifdef SB_HAVE_64BIT_ARCH
68 snprintf(path, sizeof(path), "%s", LIB_NAME); 68 snprintf(path, sizeof(path), "%s", LIB_NAME);
73 } 73 }
74#endif 74#endif
75 return (strdup(path)); 75 return (strdup(path));
76} 76}
77 77
78char *get_sandbox_pids_file(void) 78SB_STATIC char *get_sandbox_pids_file(void)
79{ 79{
80 if (0 < getenv("SANDBOX_PIDS_FILE")) { 80 if (0 < getenv("SANDBOX_PIDS_FILE")) {
81 return (strdup(getenv("SANDBOX_PIDS_FILE"))); 81 return (strdup(getenv("SANDBOX_PIDS_FILE")));
82 } 82 }
83 return (strdup(PIDS_FILE)); 83 return (strdup(PIDS_FILE));
84} 84}
85 85
86char *get_sandbox_rc(char *sb_path) 86SB_STATIC char *get_sandbox_rc(char *sb_path)
87{ 87{
88 char path[255]; 88 char path[255];
89 89
90 snprintf(path, sizeof(path), "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME); 90 snprintf(path, sizeof(path), "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
91 if (file_exist(path, 0) <= 0) { 91 if (file_exist(path, 0) <= 0) {
92 snprintf(path, sizeof(path), "%s%s", sb_path, BASHRC_NAME); 92 snprintf(path, sizeof(path), "%s%s", sb_path, BASHRC_NAME);
93 } 93 }
94 return (strdup(path)); 94 return (strdup(path));
95} 95}
96 96
97char *get_sandbox_log() 97SB_STATIC char *get_sandbox_log()
98{ 98{
99 char path[255]; 99 char path[255];
100 char *sandbox_log_env = NULL; 100 char *sandbox_log_env = NULL;
101 101
102 /* THIS CHUNK BREAK THINGS BY DOING THIS: 102 /* THIS CHUNK BREAK THINGS BY DOING THIS:
109 (sandbox_log_env == NULL ? "" : "-"), getpid(), LOG_FILE_EXT); 109 (sandbox_log_env == NULL ? "" : "-"), getpid(), LOG_FILE_EXT);
110 return (strdup(path)); 110 return (strdup(path));
111} 111}
112 112
113/* Obtain base directory name. Do not allow trailing / */ 113/* Obtain base directory name. Do not allow trailing / */
114char *sb_dirname(const char *path) 114SB_STATIC char *sb_dirname(const char *path)
115{ 115{
116 char *ret = NULL; 116 char *ret = NULL;
117 char *ptr = NULL; 117 char *ptr = NULL;
118 int loc = 0, i; 118 int loc = 0, i;
119 int cut_len = -1; 119 int cut_len = -1;
150 150
151 return (ret); 151 return (ret);
152} 152}
153 153
154/* 154/*
155char* dirname(const char* path) 155SB_STATIC char* dirname(const char* path)
156{ 156{
157 char* base = NULL; 157 char* base = NULL;
158 unsigned int length = 0; 158 unsigned int length = 0;
159 159
160 base = strrchr(path, '/'); 160 base = strrchr(path, '/');
174 174
175 return base; 175 return base;
176}*/ 176}*/
177 177
178/* Convert text (string) modes to integer values */ 178/* Convert text (string) modes to integer values */
179int file_getmode(char *mode) 179SB_STATIC int file_getmode(char *mode)
180{ 180{
181 int mde = 0; 181 int mde = 0;
182 if (0 == strcasecmp(mode, "r+")) { 182 if (0 == strcasecmp(mode, "r+")) {
183 mde = O_RDWR | O_CREAT; 183 mde = O_RDWR | O_CREAT;
184 } else if (0 == strcasecmp(mode, "w+")) { 184 } else if (0 == strcasecmp(mode, "w+")) {
196 } 196 }
197 return (mde); 197 return (mde);
198} 198}
199 199
200/* Get current position in file */ 200/* Get current position in file */
201long file_tell(int fp) 201SB_STATIC long file_tell(int fp)
202{ 202{
203 return (lseek(fp, 0L, SEEK_CUR)); 203 return (lseek(fp, 0L, SEEK_CUR));
204} 204}
205 205
206/* lock the file, preferrably the POSIX way */ 206/* lock the file, preferrably the POSIX way */
207int file_lock(int fd, int lock, char *filename) 207SB_STATIC int file_lock(int fd, int lock, char *filename)
208{ 208{
209 int err; 209 int err;
210#ifdef USE_FLOCK 210#ifdef USE_FLOCK
211 if (flock(fd, lock) < 0) { 211 if (flock(fd, lock) < 0) {
212 err = errno; 212 err = errno;
228#endif 228#endif
229 return 1; 229 return 1;
230} 230}
231 231
232/* unlock the file, preferrably the POSIX way */ 232/* unlock the file, preferrably the POSIX way */
233int file_unlock(int fd) 233SB_STATIC int file_unlock(int fd)
234{ 234{
235#ifdef USE_FLOCK 235#ifdef USE_FLOCK
236 if (flock(fd, LOCK_UN) < 0) { 236 if (flock(fd, LOCK_UN) < 0) {
237 perror(">>> flock file unlock"); 237 perror(">>> flock file unlock");
238 return 0; 238 return 0;
253} 253}
254 254
255/* Auto-determine from how the file was opened, what kind of lock to lock 255/* Auto-determine from how the file was opened, what kind of lock to lock
256 * the file with 256 * the file with
257 */ 257 */
258int file_locktype(char *mode) 258SB_STATIC int file_locktype(char *mode)
259{ 259{
260#ifdef USE_FLOCK 260#ifdef USE_FLOCK
261 if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+')) 261 if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+'))
262 || (NULL != strchr(mode, 'a'))) 262 || (NULL != strchr(mode, 'a')))
263 return (LOCK_EX); 263 return (LOCK_EX);
271} 271}
272 272
273/* Use standard fopen style modes to open the specified file. Also auto-determines and 273/* Use standard fopen style modes to open the specified file. Also auto-determines and
274 * locks the file either in shared or exclusive mode depending on opening mode 274 * locks the file either in shared or exclusive mode depending on opening mode
275 */ 275 */
276int file_open(char *filename, char *mode, int perm_specified, ...) 276SB_STATIC int file_open(char *filename, char *mode, int perm_specified, ...)
277{ 277{
278 int fd; 278 int fd;
279 char error[250]; 279 char error[250];
280 va_list ap; 280 va_list ap;
281 int perm; 281 int perm;
336 } 336 }
337 return (fd); 337 return (fd);
338} 338}
339 339
340/* Close and unlock file */ 340/* Close and unlock file */
341void file_close(int fd) 341SB_STATIC void file_close(int fd)
342{ 342{
343 if (-1 != fd) { 343 if (-1 != fd) {
344 file_unlock(fd); 344 file_unlock(fd);
345 close(fd); 345 close(fd);
346 } 346 }
347} 347}
348 348
349/* Return length of file */ 349/* Return length of file */
350long file_length(int fd) 350SB_STATIC long file_length(int fd)
351{ 351{
352 long pos, len; 352 long pos, len;
353 pos = file_tell(fd); 353 pos = file_tell(fd);
354 len = lseek(fd, 0L, SEEK_END); 354 len = lseek(fd, 0L, SEEK_END);
355 lseek(fd, pos, SEEK_SET); 355 lseek(fd, pos, SEEK_SET);
356 return (len); 356 return (len);
357} 357}
358 358
359/* Zero out file */ 359/* Zero out file */
360int file_truncate(int fd) 360SB_STATIC int file_truncate(int fd)
361{ 361{
362 lseek(fd, 0L, SEEK_SET); 362 lseek(fd, 0L, SEEK_SET);
363 if (ftruncate(fd, 0) < 0) { 363 if (ftruncate(fd, 0) < 0) {
364 perror(">>> file truncate"); 364 perror(">>> file truncate");
365 return 0; 365 return 0;
366 } 366 }
367 return 1; 367 return 1;
368} 368}
369 369
370/* Check to see if a file exists Return: 1 success, 0 file not found, -1 error */ 370/* Check to see if a file exists Return: 1 success, 0 file not found, -1 error */
371int file_exist(char *filename, int checkmode) 371SB_STATIC int file_exist(char *filename, int checkmode)
372{ 372{
373 struct stat mystat; 373 struct stat mystat;
374 374
375 /* Verify file exists and is regular file (not sym link) */ 375 /* Verify file exists and is regular file (not sym link) */
376 if (checkmode) { 376 if (checkmode) {
400 } 400 }
401 401
402 return 1; 402 return 1;
403} 403}
404 404
405int file_security_check(char *filename) 405SB_STATIC int file_security_check(char *filename)
406{ /* 0 == fine, >0 == problem */ 406{ /* 0 == fine, >0 == problem */
407 struct stat stat_buf; 407 struct stat stat_buf;
408 struct group *group_buf; 408 struct group *group_buf;
409 struct passwd *passwd_buf; 409 struct passwd *passwd_buf;
410 410

Legend:
Removed from v.30  
changed lines
  Added in v.43

  ViewVC Help
Powered by ViewVC 1.1.20