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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 286 - (hide annotations) (download) (as text)
Thu Jul 6 16:37:20 2006 UTC (8 years ago) by azarah
File MIME type: text/x-csrc
File size: 19460 byte(s)
Set default values via a config file.  Also support sandbox.d
config directory for package specific configuration files.

1 ferringb 2 /*
2     ** Path sandbox for the gentoo linux portage package system, initially
3     ** based on the ROCK Linux Wrapper for getting a list of created files
4     **
5     ** to integrate with bash, bash should have been built like this
6     **
7     ** ./configure --prefix=<prefix> --host=<host> --without-gnu-malloc
8     **
9     ** it's very important that the --enable-static-link option is NOT specified
10     **
11     ** Copyright (C) 2001 Geert Bevin, Uwyn, http://www.uwyn.com
12     ** Distributed under the terms of the GNU General Public License, v2 or later
13     ** Author : Geert Bevin <gbevin@uwyn.com>
14     ** $Header$
15     */
16    
17     /* #define _GNU_SOURCE */
18    
19     #include <errno.h>
20     #include <signal.h>
21     #include <stdio.h>
22     #include <stdlib.h>
23     #include <limits.h>
24     #include <string.h>
25     #include <sys/wait.h>
26 azarah 261 #include <signal.h>
27 ferringb 2 #include <unistd.h>
28     #include <fcntl.h>
29 azarah 142
30 ferringb 2 #include "sandbox.h"
31 azarah 280 #include "rcscripts/rcutil.h"
32 ferringb 2
33 azarah 101 struct sandbox_info_t {
34     char sandbox_log[SB_PATH_MAX];
35     char sandbox_debug_log[SB_PATH_MAX];
36     char sandbox_lib[SB_PATH_MAX];
37     char sandbox_rc[SB_PATH_MAX];
38 azarah 157 char work_dir[SB_PATH_MAX];
39 azarah 101 char var_tmp_dir[SB_PATH_MAX];
40     char tmp_dir[SB_PATH_MAX];
41     char *home_dir;
42     } sandbox_info_t;
43 ferringb 2
44 azarah 99 static int print_debug = 0;
45    
46 azarah 267 volatile static int stop_called = 0;
47 azarah 261 volatile static pid_t child_pid = 0;
48    
49 azarah 283 static char log_domain[] = "sandbox";
50    
51 azarah 216 extern char **environ;
52    
53 azarah 286 int sandbox_setup(struct sandbox_info_t *sandbox_info, bool interactive)
54 azarah 101 {
55 azarah 165 if (NULL != getenv(ENV_PORTAGE_TMPDIR)) {
56     /* Portage handle setting SANDBOX_WRITE itself. */
57     sandbox_info->work_dir[0] = '\0';
58 azarah 157 } else {
59 azarah 224 if (NULL == getcwd(sandbox_info->work_dir, SB_PATH_MAX)) {
60 azarah 157 perror("sandbox: Failed to get current directory");
61     return -1;
62     }
63 azarah 286 if (interactive)
64     setenv(ENV_SANDBOX_WORKDIR, sandbox_info->work_dir, 1);
65 azarah 101 }
66    
67 azarah 152 /* Do not resolve symlinks, etc .. libsandbox will handle that. */
68 azarah 282 if (!rc_is_dir(VAR_TMPDIR, TRUE)) {
69 azarah 145 perror("sandbox: Failed to get var_tmp_dir");
70 azarah 101 return -1;
71     }
72 azarah 152 snprintf(sandbox_info->var_tmp_dir, SB_PATH_MAX, "%s", VAR_TMPDIR);
73 azarah 139
74 azarah 101 if (-1 == get_tmp_dir(sandbox_info->tmp_dir)) {
75 azarah 145 perror("sandbox: Failed to get tmp_dir");
76 azarah 101 return -1;
77     }
78 azarah 158 setenv(ENV_TMPDIR, sandbox_info->tmp_dir, 1);
79 azarah 101
80     sandbox_info->home_dir = getenv("HOME");
81     if (!sandbox_info->home_dir) {
82 azarah 158 sandbox_info->home_dir = sandbox_info->tmp_dir;
83 azarah 101 setenv("HOME", sandbox_info->home_dir, 1);
84     }
85    
86     /* Generate sandbox lib path */
87 azarah 140 get_sandbox_lib(sandbox_info->sandbox_lib);
88 azarah 101
89     /* Generate sandbox bashrc path */
90 azarah 140 get_sandbox_rc(sandbox_info->sandbox_rc);
91 azarah 101
92     /* Generate sandbox log full path */
93 azarah 140 get_sandbox_log(sandbox_info->sandbox_log);
94 azarah 282 if (rc_file_exists(sandbox_info->sandbox_log)) {
95 azarah 146 if (-1 == unlink(sandbox_info->sandbox_log)) {
96     perror("sandbox: Could not unlink old log file");
97 azarah 147 return -1;
98 azarah 146 }
99     }
100 azarah 101
101     /* Generate sandbox debug log full path */
102 azarah 140 get_sandbox_debug_log(sandbox_info->sandbox_debug_log);
103 azarah 282 if (rc_file_exists(sandbox_info->sandbox_debug_log)) {
104 azarah 146 if (-1 == unlink(sandbox_info->sandbox_debug_log)) {
105     perror("sandbox: Could not unlink old debug log file");
106 azarah 147 return -1;
107 azarah 146 }
108     }
109 azarah 101
110     return 0;
111     }
112    
113 azarah 88 int print_sandbox_log(char *sandbox_log)
114     {
115     int sandbox_log_file = -1;
116     char *beep_count_env = NULL;
117     int i, color, beep_count = 0;
118     long len = 0;
119     char *buffer = NULL;
120    
121 azarah 282 if (!rc_is_file(sandbox_log, FALSE)) {
122 azarah 145 perror("sandbox: Log file is not a regular file");
123 azarah 88 return 0;
124 azarah 139 }
125    
126     sandbox_log_file = open(sandbox_log, O_RDONLY);
127     if (-1 == sandbox_log_file) {
128 azarah 145 perror("sandbox: Could not open Log file");
129 azarah 139 return 0;
130     }
131 azarah 88
132     len = file_length(sandbox_log_file);
133     buffer = (char *)malloc((len + 1) * sizeof(char));
134     memset(buffer, 0, len + 1);
135     read(sandbox_log_file, buffer, len);
136 azarah 139 close(sandbox_log_file);
137 azarah 88
138 azarah 263 color = ((is_env_on(ENV_NOCOLOR)) ? 0 : 1);
139 azarah 88
140 azarah 279 SB_EERROR(color,
141 azarah 144 "--------------------------- ACCESS VIOLATION SUMMARY ---------------------------",
142     "\n");
143 azarah 279 SB_EERROR(color, "LOG FILE = \"%s\"", "\n\n", sandbox_log);
144 azarah 144 fprintf(stderr, "%s", buffer);
145     if (NULL != buffer)
146 azarah 88 free(buffer);
147 azarah 279 SB_EERROR(color,
148 azarah 144 "--------------------------------------------------------------------------------",
149     "\n");
150 azarah 88
151     beep_count_env = getenv(ENV_SANDBOX_BEEP);
152     if (beep_count_env)
153     beep_count = atoi(beep_count_env);
154     else
155     beep_count = DEFAULT_BEEP_COUNT;
156    
157     for (i = 0; i < beep_count; i++) {
158     fputc('\a', stderr);
159     if (i < beep_count - 1)
160     sleep(1);
161     }
162 azarah 139
163 azarah 88 return 1;
164     }
165    
166 azarah 30 void stop(int signum)
167 ferringb 2 {
168 azarah 267 if (0 == stop_called) {
169 ferringb 2 stop_called = 1;
170 azarah 145 printf("sandbox: Caught signal %d in pid %d\n",
171     signum, getpid());
172 ferringb 2 } else {
173 azarah 145 fprintf(stderr,
174     "sandbox: Signal already caught and busy still cleaning up!\n");
175 ferringb 2 }
176     }
177    
178 azarah 268 void usr1_handler(int signum, siginfo_t *siginfo, void *ucontext)
179     {
180     if (0 == stop_called) {
181     stop_called = 1;
182     printf("sandbox: Caught signal %d in pid %d\n",
183     signum, getpid());
184    
185     /* FIXME: This is really bad form, as we should kill the whole process
186     * tree, but currently that is too much work and not worth the
187     * effort. Thus we only kill the calling process and our child
188     * for now.
189     */
190     if (siginfo->si_pid > 0)
191     kill(siginfo->si_pid, SIGKILL);
192     kill(child_pid, SIGKILL);
193     } else {
194     fprintf(stderr,
195     "sandbox: Signal already caught and busy still cleaning up!\n");
196     }
197     }
198    
199 azarah 286 char *sandbox_subst_env_vars(dyn_buf_t *env_data)
200 ferringb 2 {
201 azarah 286 dyn_buf_t *new_data = NULL;
202     char *tmp_ptr, *tmp_data = NULL;
203     char *var_start, *var_stop;
204 azarah 30
205 azarah 286 new_data = new_dyn_buf();
206     if (NULL == new_data)
207     return NULL;
208    
209     tmp_data = read_line_dyn_buf(env_data);
210     if (NULL == tmp_data)
211     goto error;
212     tmp_ptr = tmp_data;
213    
214     while (NULL != (var_start = strchr(tmp_ptr, '$'))) {
215     char *env = NULL;
216    
217     var_stop = strchr(var_start, '}');
218    
219     /* We only support ${} style env var names, so just skip any
220     * '$' that do not follow this syntax */
221     if (('{' != var_start[1]) || (NULL == var_stop)) {
222     tmp_ptr = var_start + 1;
223     continue;
224     }
225    
226     /* Terminate part before env string so that we can copy it */
227     var_start[0] = '\0';
228     /* Move var_start past '${' */
229     var_start += 2;
230     /* Terminate the name of the env var */
231     var_stop[0] = '\0';
232    
233     if (strlen(var_start) > 0)
234     env = getenv(var_start);
235     if (-1 == sprintf_dyn_buf(new_data, "%s%s",
236     tmp_ptr ? tmp_ptr : "",
237     env ? env : ""))
238     goto error;
239    
240     /* Move tmp_ptr past the '}' of the env var */
241     tmp_ptr = var_stop + 1;
242     }
243    
244     if (0 != strlen(tmp_ptr))
245     if (-1 == write_dyn_buf(new_data, tmp_ptr, strlen(tmp_ptr)))
246     goto error;
247    
248     free(tmp_data);
249    
250     tmp_data = read_line_dyn_buf(new_data);
251     if (NULL == tmp_data)
252     goto error;
253    
254     free_dyn_buf(new_data);
255    
256     return tmp_data;
257    
258     error:
259     if (NULL != new_data)
260     free_dyn_buf(new_data);
261     if (NULL != tmp_data)
262     free(tmp_data);
263    
264     return NULL;
265     }
266    
267     void sandbox_set_env_var(const char *env_var)
268     {
269     char *config;
270    
271     /* We check if the variable is set in the environment, and if not, we
272     * get it from sandbox.conf, and if they exist, we just add them to the
273     * environment if not already present. */
274     if (NULL == getenv(env_var)) {
275     config = rc_get_cnf_entry(SANDBOX_CONF_FILE, env_var, NULL);
276     if (NULL != config) {
277     setenv(ENV_SANDBOX_VERBOSE, config, 0);
278     free(config);
279     }
280     }
281     }
282    
283     int sandbox_set_env_access_var(const char *access_var)
284     {
285     dyn_buf_t *env_data = NULL;
286     int count = 0;
287     char *config = NULL;
288     char **confd_files = NULL;
289     bool use_confd = TRUE;
290    
291     env_data = new_dyn_buf();
292     if (NULL == env_data)
293 azarah 159 return -1;
294 azarah 286
295     /* Now get the defaults for the access variable from sandbox.conf.
296     * These do not get overridden via the environment. */
297     config = rc_get_cnf_entry(SANDBOX_CONF_FILE, access_var, ":");
298     if (NULL != config) {
299     if (-1 == write_dyn_buf(env_data, config, strlen(config)))
300     goto error;
301     free(config);
302     config = NULL;
303 azarah 159 }
304 azarah 286 /* Append whatever might be already set. If anything is set, we do
305     * not process the sandbox.d/ files for this variable. */
306     if (NULL != getenv(access_var)) {
307     use_confd = FALSE;
308     if (-1 == sprintf_dyn_buf(env_data, env_data->wr_index ? ":%s" : "%s",
309     getenv(access_var)))
310     goto error;
311     }
312 azarah 159
313 azarah 286 if (!use_confd)
314     goto done;
315    
316     /* Now scan the files in sandbox.d/ if the access variable was not
317     * alreay set. */
318     confd_files = rc_ls_dir(SANDBOX_CONFD_DIR, FALSE, TRUE);
319     if (NULL != confd_files) {
320     while (NULL != confd_files[count]) {
321     config = rc_get_cnf_entry(confd_files[count], access_var, ":");
322     if (NULL != config) {
323     if (-1 == sprintf_dyn_buf(env_data,
324     env_data->wr_index ? ":%s" : "%s",
325     config))
326     goto error;
327     free(config);
328     config = NULL;
329     }
330     count++;
331     }
332    
333     str_list_free(confd_files);
334     }
335    
336     done:
337     if (env_data->wr_index > 0) {
338     char *subst;
339    
340     subst = sandbox_subst_env_vars(env_data);
341     if (NULL == subst)
342     goto error;
343    
344     setenv(access_var, subst, 1);
345     free(subst);
346     }
347    
348     free_dyn_buf(env_data);
349    
350 azarah 159 return 0;
351 azarah 286
352     error:
353     if (NULL != env_data)
354     free_dyn_buf(env_data);
355     if (NULL != config)
356     free(config);
357     if (NULL != confd_files)
358     str_list_free(confd_files);
359    
360     return -1;
361 azarah 89 }
362    
363 azarah 286 int sandbox_setup_env_config(struct sandbox_info_t *sandbox_info)
364 azarah 89 {
365 azarah 286 sandbox_set_env_var(ENV_SANDBOX_VERBOSE);
366     sandbox_set_env_var(ENV_SANDBOX_DEBUG);
367     sandbox_set_env_var(ENV_SANDBOX_BEEP);
368     sandbox_set_env_var(ENV_NOCOLOR);
369 ferringb 2
370 azarah 286 if (-1 == sandbox_set_env_access_var(ENV_SANDBOX_DENY))
371 azarah 159 return -1;
372 azarah 286 if (NULL == getenv(ENV_SANDBOX_DENY))
373     setenv(ENV_SANDBOX_DENY, LD_PRELOAD_FILE, 1);
374 azarah 159
375 azarah 286 if (-1 == sandbox_set_env_access_var(ENV_SANDBOX_READ))
376     return -1;
377     if (NULL == getenv(ENV_SANDBOX_READ))
378     setenv(ENV_SANDBOX_READ, "/", 1);
379    
380     if (-1 == sandbox_set_env_access_var(ENV_SANDBOX_WRITE))
381     return -1;
382     if ((NULL == getenv(ENV_SANDBOX_WRITE)) &&
383     (NULL != sandbox_info->work_dir))
384     setenv(ENV_SANDBOX_WRITE, sandbox_info->work_dir, 1);
385    
386     if (-1 == sandbox_set_env_access_var(ENV_SANDBOX_PREDICT))
387     return -1;
388     if ((NULL == getenv(ENV_SANDBOX_PREDICT)) &&
389     (NULL != sandbox_info->home_dir))
390     setenv(ENV_SANDBOX_PREDICT, sandbox_info->home_dir, 1);
391    
392 azarah 159 return 0;
393 azarah 89 }
394 ferringb 2
395 azarah 124 int sandbox_setenv(char **env, const char *name, const char *val) {
396 azarah 89 char **tmp_env = env;
397     char *tmp_string = NULL;
398 azarah 159 int retval = 0;
399 azarah 89
400 azarah 100 /* XXX: We add the new variable to the end (no replacing). If this
401     * is changed, we need to fix sandbox_setup_environ() below */
402 azarah 89 while (NULL != *tmp_env)
403     tmp_env++;
404    
405     /* strlen(name) + strlen(val) + '=' + '\0' */
406 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
407     * a real leak that will cause issues. */
408 azarah 89 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
409     if (NULL == tmp_string) {
410 azarah 145 perror("sandbox: Out of memory (sandbox_setenv)");
411     exit(EXIT_FAILURE);
412 ferringb 2 }
413 azarah 89
414 azarah 159 retval = snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
415     name, val);
416 azarah 89 *tmp_env = tmp_string;
417    
418     return 0;
419 ferringb 2 }
420    
421 azarah 89 /* We setup the environment child side only to prevent issues with
422     * setting LD_PRELOAD parent side */
423 azarah 265 char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info, bool interactive)
424 ferringb 2 {
425 azarah 89 int env_size = 0;
426 azarah 100 int have_ld_preload = 0;
427 azarah 89
428     char **new_environ;
429     char **env_ptr = environ;
430     char *ld_preload_envvar = NULL;
431 azarah 100 char *orig_ld_preload_envvar = NULL;
432 azarah 261 char sb_pid[64];
433 ferringb 2
434 azarah 286 if (-1 == sandbox_setup_env_config(sandbox_info))
435     return NULL;
436    
437 azarah 89 /* Unset these, as its easier than replacing when setting up our
438     * new environment below */
439 azarah 193 unsetenv(ENV_SANDBOX_ON);
440 azarah 261 unsetenv(ENV_SANDBOX_PID);
441 azarah 89 unsetenv(ENV_SANDBOX_LIB);
442     unsetenv(ENV_SANDBOX_BASHRC);
443     unsetenv(ENV_SANDBOX_LOG);
444     unsetenv(ENV_SANDBOX_DEBUG_LOG);
445 azarah 286 unsetenv(ENV_SANDBOX_WORKDIR);
446 azarah 261 unsetenv(ENV_SANDBOX_ACTIVE);
447 azarah 89
448 azarah 100 if (NULL != getenv(ENV_LD_PRELOAD)) {
449     have_ld_preload = 1;
450     orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD);
451    
452 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
453     * than a real leak that will cause issues. */
454 azarah 100 ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) +
455 azarah 101 strlen(sandbox_info->sandbox_lib) + 2,
456     sizeof(char *));
457 azarah 89 if (NULL == ld_preload_envvar)
458     return NULL;
459 azarah 100 snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) +
460 azarah 101 strlen(sandbox_info->sandbox_lib) + 2, "%s %s",
461     sandbox_info->sandbox_lib, orig_ld_preload_envvar);
462 azarah 89 } else {
463 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
464     * than a real leak that will cause issues. */
465 azarah 280 ld_preload_envvar = rc_strndup(sandbox_info->sandbox_lib,
466 azarah 101 strlen(sandbox_info->sandbox_lib));
467 azarah 89 if (NULL == ld_preload_envvar)
468     return NULL;
469     }
470 azarah 100 /* Do not unset this, as strange things might happen */
471     /* unsetenv(ENV_LD_PRELOAD); */
472 ferringb 2
473 azarah 89 while (NULL != *env_ptr) {
474     env_size++;
475     env_ptr++;
476     }
477 ferringb 2
478 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
479     * a real leak that will cause issues. */
480 azarah 89 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
481     if (NULL == new_environ)
482     return NULL;
483    
484 azarah 261 snprintf(sb_pid, sizeof(sb_pid), "%i", getpid());
485    
486 azarah 89 /* First add our new variables to the beginning - this is due to some
487     * weirdness that I cannot remember */
488 azarah 193 sandbox_setenv(new_environ, ENV_SANDBOX_ON, "1");
489 azarah 261 sandbox_setenv(new_environ, ENV_SANDBOX_PID, sb_pid);
490 azarah 101 sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_info->sandbox_lib);
491     sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_info->sandbox_rc);
492     sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_info->sandbox_log);
493     sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG,
494     sandbox_info->sandbox_debug_log);
495 azarah 265 /* Is this an interactive session? */
496     if (interactive)
497     sandbox_setenv(new_environ, ENV_SANDBOX_INTRACTV, "1");
498 azarah 263 /* Just set the these if not already set so that is_env_on() work */
499     if (!getenv(ENV_SANDBOX_VERBOSE))
500     sandbox_setenv(new_environ, ENV_SANDBOX_VERBOSE, "1");
501     if (!getenv(ENV_SANDBOX_DEBUG))
502     sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG, "0");
503     if (!getenv(ENV_NOCOLOR))
504 azarah 269 sandbox_setenv(new_environ, ENV_NOCOLOR, "no");
505 azarah 100 /* If LD_PRELOAD was not set, set it here, else do it below */
506     if (1 != have_ld_preload)
507     sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar);
508 azarah 89
509 azarah 175 /* Make sure our bashrc gets preference */
510     sandbox_setenv(new_environ, ENV_BASH_ENV, sandbox_info->sandbox_rc);
511    
512 azarah 89 /* This one should NEVER be set in ebuilds, as it is the one
513     * private thing libsandbox.so use to test if the sandbox
514     * should be active for this pid, or not.
515     *
516     * azarah (3 Aug 2002)
517     */
518    
519 azarah 173 sandbox_setenv(new_environ, ENV_SANDBOX_ACTIVE, SANDBOX_ACTIVE);
520 azarah 89
521     env_size = 0;
522     while (NULL != new_environ[env_size])
523     env_size++;
524    
525     /* Now add the rest */
526     env_ptr = environ;
527     while (NULL != *env_ptr) {
528 azarah 100 if ((1 == have_ld_preload) &&
529     (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr))
530     /* If LD_PRELOAD was set, and this is it in the original
531     * environment, replace it with our new copy */
532 azarah 101 /* XXX: The following works as it just add whatever as
533     * the last variable to nev_environ */
534 azarah 100 sandbox_setenv(new_environ, ENV_LD_PRELOAD,
535     ld_preload_envvar);
536     else
537     new_environ[env_size + (env_ptr - environ)] = *env_ptr;
538 azarah 89 env_ptr++;
539 ferringb 2 }
540 azarah 89
541     return new_environ;
542 ferringb 2 }
543    
544 azarah 145 int spawn_shell(char *argv_bash[], char *env[], int debug)
545 ferringb 2 {
546     int status = 0;
547     int ret = 0;
548    
549 azarah 261 child_pid = fork();
550 ferringb 2
551     /* Child's process */
552 azarah 261 if (0 == child_pid) {
553 azarah 89 execve(argv_bash[0], argv_bash, env);
554 ferringb 2 return 0;
555 azarah 261 } else if (child_pid < 0) {
556 azarah 145 if (debug)
557     fprintf(stderr, "Process failed to spawn!\n");
558 ferringb 2 return 0;
559     }
560 azarah 261 ret = waitpid(child_pid, &status, 0);
561 azarah 145 if ((-1 == ret) || (status > 0)) {
562     if (debug)
563     fprintf(stderr, "Process returned with failed exit status!\n");
564 ferringb 2 return 0;
565 azarah 145 }
566 azarah 86
567 ferringb 2 return 1;
568     }
569    
570 azarah 30 int main(int argc, char **argv)
571 ferringb 2 {
572 azarah 268 struct sigaction act_new;
573    
574 azarah 101 int i = 0, success = 1;
575 ferringb 2 int sandbox_log_presence = 0;
576     long len;
577    
578 azarah 101 struct sandbox_info_t sandbox_info;
579 ferringb 2
580 azarah 89 char **sandbox_environ;
581 ferringb 2 char **argv_bash = NULL;
582    
583     char *run_str = "-c";
584    
585 azarah 283 rc_log_domain(log_domain);
586    
587 ferringb 2 /* Only print info if called with no arguments .... */
588     if (argc < 2)
589     print_debug = 1;
590    
591     if (print_debug)
592 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
593 ferringb 2
594     /* check if a sandbox is already running */
595 azarah 184 if (NULL != getenv(ENV_SANDBOX_ACTIVE)) {
596 azarah 30 fprintf(stderr, "Not launching a new sandbox instance\n");
597     fprintf(stderr, "Another one is already running in this process hierarchy.\n");
598 azarah 145 exit(EXIT_FAILURE);
599     }
600 ferringb 2
601 azarah 145 /* determine the location of all the sandbox support files */
602     if (print_debug)
603     printf("Detection of the support files.\n");
604 ferringb 2
605 azarah 286 if (-1 == sandbox_setup(&sandbox_info, print_debug)) {
606 azarah 147 fprintf(stderr, "sandbox: Failed to setup sandbox.");
607 azarah 145 exit(EXIT_FAILURE);
608     }
609    
610     /* verify the existance of required files */
611     if (print_debug)
612     printf("Verification of the required files.\n");
613 ferringb 2
614 azarah 181 #ifndef SB_HAVE_MULTILIB
615 azarah 282 if (!rc_file_exists(sandbox_info.sandbox_lib)) {
616 azarah 145 perror("sandbox: Could not open the sandbox library");
617     exit(EXIT_FAILURE);
618     }
619 ferringb 2 #endif
620 azarah 282 if (!rc_file_exists(sandbox_info.sandbox_rc)) {
621 azarah 145 perror("sandbox: Could not open the sandbox rc file");
622     exit(EXIT_FAILURE);
623     }
624 ferringb 2
625 azarah 145 /* set up the required environment variables */
626     if (print_debug)
627     printf("Setting up the required environment variables.\n");
628 ferringb 2
629 azarah 145 /* Setup the child environment stuff */
630 azarah 265 sandbox_environ = sandbox_setup_environ(&sandbox_info, print_debug);
631 azarah 145 if (NULL == sandbox_environ) {
632     perror("sandbox: Out of memory (environ)");
633     exit(EXIT_FAILURE);
634     }
635 ferringb 2
636 azarah 165 /* If not in portage, cd into it work directory */
637     if ('\0' != sandbox_info.work_dir[0])
638 azarah 157 chdir(sandbox_info.work_dir);
639 ferringb 2
640 azarah 145 argv_bash = (char **)malloc(6 * sizeof(char *));
641     argv_bash[0] = strdup("/bin/bash");
642     argv_bash[1] = strdup("-rcfile");
643     argv_bash[2] = strdup(sandbox_info.sandbox_rc);
644 ferringb 2
645 azarah 145 if (argc < 2)
646     argv_bash[3] = NULL;
647     else
648     argv_bash[3] = strdup(run_str); /* "-c" */
649 ferringb 2
650 azarah 145 argv_bash[4] = NULL; /* strdup(run_arg); */
651     argv_bash[5] = NULL;
652 ferringb 2
653 azarah 145 if (argc >= 2) {
654     for (i = 1; i < argc; i++) {
655     if (NULL == argv_bash[4])
656     len = 0;
657     else
658     len = strlen(argv_bash[4]);
659 ferringb 2
660 azarah 145 argv_bash[4] = (char *)realloc(argv_bash[4],
661     (len + strlen(argv[i]) + 2) * sizeof(char));
662 ferringb 2
663 azarah 145 if (0 == len)
664     argv_bash[4][0] = 0;
665     if (1 != i)
666     strcat(argv_bash[4], " ");
667 ferringb 2
668 azarah 145 strcat(argv_bash[4], argv[i]);
669 ferringb 2 }
670 azarah 145 }
671 ferringb 2
672 azarah 145 /* set up the required signal handlers */
673     signal(SIGHUP, &stop);
674     signal(SIGINT, &stop);
675     signal(SIGQUIT, &stop);
676     signal(SIGTERM, &stop);
677 azarah 268 act_new.sa_sigaction = usr1_handler;
678     sigemptyset (&act_new.sa_mask);
679     act_new.sa_flags = SA_SIGINFO | SA_RESTART;
680     sigaction (SIGUSR1, &act_new, NULL);
681 ferringb 2
682 azarah 145 /* STARTING PROTECTED ENVIRONMENT */
683     if (print_debug) {
684     printf("The protected environment has been started.\n");
685     printf("--------------------------------------------------------------------------------\n");
686     }
687 ferringb 2
688 azarah 145 if (print_debug)
689     printf("Process being started in forked instance.\n");
690 ferringb 2
691 azarah 145 /* Start Bash */
692     if (!spawn_shell(argv_bash, sandbox_environ, print_debug))
693     success = 0;
694 ferringb 2
695 azarah 145 /* Free bash stuff */
696     for (i = 0; i < 6; i++) {
697     if (argv_bash[i])
698     free(argv_bash[i]);
699     argv_bash[i] = NULL;
700     }
701     if (argv_bash)
702     free(argv_bash);
703     argv_bash = NULL;
704 ferringb 2
705 azarah 145 if (print_debug)
706     printf("Cleaning up sandbox process\n");
707 ferringb 2
708 azarah 145 if (print_debug) {
709     printf("========================== Gentoo linux path sandbox ===========================\n");
710     printf("The protected environment has been shut down.\n");
711     }
712 ferringb 2
713 azarah 282 if (rc_file_exists(sandbox_info.sandbox_log)) {
714 azarah 145 sandbox_log_presence = 1;
715     print_sandbox_log(sandbox_info.sandbox_log);
716     } else if (print_debug) {
717     printf("--------------------------------------------------------------------------------\n");
718     }
719 ferringb 2
720 azarah 145 if ((sandbox_log_presence) || (!success))
721     return 1;
722     else
723     return 0;
724 ferringb 2 }
725    
726 azarah 30 // vim:noexpandtab noai:cindent ai

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20