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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 131 - (hide annotations) (download) (as text)
Sun Jul 3 19:05:34 2005 UTC (9 years, 4 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 19753 byte(s)
Remove trailing ':' for SANDBOX_PREDICT.
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 <fcntl.h>
21     #include <signal.h>
22     #include <stdio.h>
23     #include <stdlib.h>
24     #include <limits.h>
25     #include <string.h>
26     #include <sys/file.h>
27     #include <sys/stat.h>
28     #include <sys/time.h>
29     #include <sys/types.h>
30     #include <sys/resource.h>
31     #include <sys/wait.h>
32     #include <unistd.h>
33     #include <fcntl.h>
34     #include "sandbox.h"
35    
36 azarah 101 struct sandbox_info_t {
37     char sandbox_log[SB_PATH_MAX];
38     char sandbox_debug_log[SB_PATH_MAX];
39     char sandbox_dir[SB_PATH_MAX];
40     char sandbox_lib[SB_PATH_MAX];
41     char sandbox_rc[SB_PATH_MAX];
42     char *sandbox_pids_file;
43     char portage_tmp_dir[SB_PATH_MAX];
44     char var_tmp_dir[SB_PATH_MAX];
45     char tmp_dir[SB_PATH_MAX];
46     char *home_dir;
47     } sandbox_info_t;
48 ferringb 2
49 azarah 101 static char *tmp_dir;
50    
51 azarah 99 static int cleaned_up = 0;
52     static int print_debug = 0;
53     static int stop_called = 0;
54    
55 azarah 30 /* Read pids file, and load active pids into an array. Return number of pids in array */
56     int load_active_pids(int fd, int **pids)
57 ferringb 2 {
58     char *data = NULL;
59     char *ptr = NULL, *ptr2 = NULL;
60     int my_pid;
61     int num_pids = 0;
62     long len;
63    
64     pids[0] = NULL;
65    
66     len = file_length(fd);
67    
68     /* Allocate and zero datablock to read pids file */
69 azarah 30 data = (char *)malloc((len + 1) * sizeof(char));
70 ferringb 2 memset(data, 0, len + 1);
71    
72     /* Start at beginning of file */
73     lseek(fd, 0L, SEEK_SET);
74    
75     /* read entire file into a buffer */
76     read(fd, data, len);
77    
78     ptr = data;
79    
80     /* Loop and read all pids */
81     while (1) {
82     /* Find new line */
83     ptr2 = strchr(ptr, '\n');
84     if (ptr2 == NULL)
85 azarah 30 break; /* No more PIDs */
86 ferringb 2
87     /* Clear the \n. And ptr should have a null-terminated decimal string */
88     ptr2[0] = 0;
89    
90     my_pid = atoi(ptr);
91    
92     /* If the PID is still alive, add it to our array */
93     if ((0 != my_pid) && (0 == kill(my_pid, 0))) {
94 azarah 30 pids[0] = (int *)realloc(pids[0], (num_pids + 1) * sizeof(int));
95 ferringb 2 pids[0][num_pids] = my_pid;
96     num_pids++;
97     }
98    
99     /* Put ptr past the NULL we just wrote */
100     ptr = ptr2 + 1;
101     }
102    
103     if (data)
104     free(data);
105     data = NULL;
106    
107     return num_pids;
108     }
109    
110 azarah 101 int write_pids_file(struct sandbox_info_t *sandbox_info)
111 ferringb 2 {
112 azarah 101 int pids_file = -1;
113     int *pids_array = NULL;
114     int num_of_pids = 0;
115     int i = 0, success = 1;
116    
117     char pid_string[SB_BUF_LEN];
118    
119     if (file_exist(sandbox_info->sandbox_pids_file, 1) < 0) {
120     fprintf(stderr, ">>> %s is not a regular file\n",
121     sandbox_info->sandbox_pids_file);
122     return -1;
123     } else {
124     pids_file = file_open(sandbox_info->sandbox_pids_file,
125     "r+", 1, 0664, "portage");
126     if (-1 == pids_file)
127     return -1;
128     }
129    
130     /* Grab still active pids */
131     num_of_pids = load_active_pids(pids_file, &pids_array);
132    
133     /* Zero out file */
134     file_truncate(pids_file);
135    
136     /* Output active pids, and append our pid */
137     for (i = 0; i < num_of_pids + 1; i++) {
138     /* Time for our entry */
139     if (i == num_of_pids)
140     sprintf(pid_string, "%d\n", getpid());
141     else
142     sprintf(pid_string, "%d\n", pids_array[i]);
143    
144     if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
145     success = 0;
146     break;
147     }
148     }
149    
150     /* Clean pids_array */
151     if (pids_array)
152     free(pids_array);
153    
154     /* We're done with the pids file */
155     file_close(pids_file);
156    
157     if (!success)
158     return -1;
159    
160     return 0;
161     }
162    
163     int sandbox_setup(char *argv[], struct sandbox_info_t *sandbox_info)
164     {
165     if (NULL == realpath(getenv(ENV_PORTAGE_TMPDIR) ? getenv(ENV_PORTAGE_TMPDIR)
166     : PORTAGE_TMPDIR,
167     sandbox_info->portage_tmp_dir)) {
168     perror(">>> get portage_tmp_dir");
169     return -1;
170     }
171 azarah 108 setenv(ENV_PORTAGE_TMPDIR, sandbox_info->portage_tmp_dir, 1);
172 azarah 101
173     if (NULL == realpath(VAR_TMPDIR, sandbox_info->var_tmp_dir)) {
174     perror(">>> get var_tmp_dir");
175     return -1;
176     }
177    
178     if (-1 == get_tmp_dir(sandbox_info->tmp_dir)) {
179     perror(">>> get tmp_dir");
180     return -1;
181     }
182     tmp_dir = sandbox_info->tmp_dir;
183 azarah 108 setenv(ENV_TMPDIR, tmp_dir, 1);
184 azarah 101
185     sandbox_info->home_dir = getenv("HOME");
186     if (!sandbox_info->home_dir) {
187     sandbox_info->home_dir = tmp_dir;
188     setenv("HOME", sandbox_info->home_dir, 1);
189     }
190    
191     /* Generate base sandbox path */
192     snprintf(sandbox_info->sandbox_dir, SB_PATH_MAX, "%s/",
193     get_sandbox_path(argv[0]));
194    
195     /* Generate sandbox lib path */
196     snprintf(sandbox_info->sandbox_lib, SB_PATH_MAX, "%s",
197     get_sandbox_lib(sandbox_info->sandbox_dir));
198    
199     /* Generate sandbox pids-file path */
200     sandbox_info->sandbox_pids_file = get_sandbox_pids_file(tmp_dir);
201    
202     /* Generate sandbox bashrc path */
203     snprintf(sandbox_info->sandbox_rc, SB_PATH_MAX, "%s",
204     get_sandbox_rc(sandbox_info->sandbox_dir));
205    
206     /* Generate sandbox log full path */
207     snprintf(sandbox_info->sandbox_log, SB_PATH_MAX, "%s",
208     get_sandbox_log(tmp_dir));
209    
210     /* Generate sandbox debug log full path */
211     snprintf(sandbox_info->sandbox_debug_log, SB_PATH_MAX, "%s",
212     get_sandbox_debug_log(tmp_dir));
213    
214     return 0;
215     }
216    
217     void sandbox_cleanup()
218     {
219 ferringb 2 int i = 0;
220     int success = 1;
221     int pids_file = -1, num_of_pids = 0;
222     int *pids_array = NULL;
223 azarah 93 char pid_string[SB_BUF_LEN];
224 ferringb 2 char *sandbox_pids_file;
225    
226     /* Generate sandbox pids-file path */
227 azarah 99 sandbox_pids_file = get_sandbox_pids_file(tmp_dir);
228 ferringb 2
229     /* Remove this sandbox's bash pid from the global pids
230 azarah 90 * file if we have not already done so */
231     if (0 == cleaned_up) {
232 ferringb 2 cleaned_up = 1;
233     success = 1;
234    
235     if (print_debug)
236     printf("Cleaning up pids file.\n");
237    
238     /* Stat the PIDs file, make sure it exists and is a regular file */
239     if (file_exist(sandbox_pids_file, 1) <= 0) {
240 ferringb 34 fprintf(stderr, ">>> pids file is not a regular file\n");
241 ferringb 2 success = 0;
242     /* We should really not fail if the pidsfile is missing here, but
243     * rather just exit cleanly, as there is still some cleanup to do */
244     return;
245     }
246    
247     pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
248     if (-1 == pids_file) {
249     success = 0;
250     /* Nothing more to do here */
251     return;
252     }
253    
254     /* Load "still active" pids into an array */
255     num_of_pids = load_active_pids(pids_file, &pids_array);
256     //printf("pids: %d\r\n", num_of_pids);
257    
258    
259     file_truncate(pids_file);
260    
261     /* if pids are still running, write only the running pids back to the file */
262     if (num_of_pids > 1) {
263     for (i = 0; i < num_of_pids; i++) {
264     if (pids_array[i] != getpid()) {
265     sprintf(pid_string, "%d\n", pids_array[i]);
266    
267 azarah 30 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
268 ferringb 2 perror(">>> pids file write");
269     success = 0;
270     break;
271     }
272     }
273     }
274    
275     file_close(pids_file);
276     pids_file = -1;
277     } else {
278    
279     file_close(pids_file);
280     pids_file = -1;
281    
282     /* remove the pidsfile, as this was the last sandbox */
283     unlink(sandbox_pids_file);
284     }
285    
286     if (pids_array != NULL)
287     free(pids_array);
288     pids_array = NULL;
289     }
290    
291     free(sandbox_pids_file);
292     }
293    
294 azarah 88 int print_sandbox_log(char *sandbox_log)
295     {
296     int sandbox_log_file = -1;
297     char *beep_count_env = NULL;
298     int i, color, beep_count = 0;
299     long len = 0;
300     char *buffer = NULL;
301    
302     sandbox_log_file = file_open(sandbox_log, "r", 1, 0664, "portage");
303     if (-1 == sandbox_log_file)
304     return 0;
305    
306     len = file_length(sandbox_log_file);
307     buffer = (char *)malloc((len + 1) * sizeof(char));
308     memset(buffer, 0, len + 1);
309     read(sandbox_log_file, buffer, len);
310     file_close(sandbox_log_file);
311    
312     color = ((getenv("NOCOLOR") != NULL) ? 0 : 1);
313    
314     if (color)
315     printf("\e[31;01m");
316     printf("--------------------------- ACCESS VIOLATION SUMMARY ---------------------------");
317     if (color)
318     printf("\033[0m");
319     if (color)
320     printf("\e[31;01m");
321     printf("\nLOG FILE = \"%s\"", sandbox_log);
322     if (color)
323     printf("\033[0m");
324     printf("\n\n");
325     printf("%s", buffer);
326     if (buffer)
327     free(buffer);
328     buffer = NULL;
329     printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\n");
330    
331     beep_count_env = getenv(ENV_SANDBOX_BEEP);
332     if (beep_count_env)
333     beep_count = atoi(beep_count_env);
334     else
335     beep_count = DEFAULT_BEEP_COUNT;
336    
337     for (i = 0; i < beep_count; i++) {
338     fputc('\a', stderr);
339     if (i < beep_count - 1)
340     sleep(1);
341     }
342     return 1;
343     }
344    
345 azarah 30 void stop(int signum)
346 ferringb 2 {
347     if (stop_called == 0) {
348     stop_called = 1;
349     printf("Caught signal %d in pid %d\r\n", signum, getpid());
350 azarah 101 sandbox_cleanup();
351 ferringb 2 } else {
352     fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid());
353     }
354     }
355    
356 azarah 101 void get_sandbox_write_envvar(char *buf, struct sandbox_info_t *sandbox_info)
357 ferringb 2 {
358 azarah 89 /* bzero out entire buffer then append trailing 0 */
359 azarah 93 memset(buf, 0, SB_BUF_LEN);
360 azarah 30
361 azarah 89 /* these could go into make.globals later on */
362     snprintf(buf, SB_BUF_LEN,
363     "%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
364 azarah 113 "/dev/zero:/dev/null:/dev/fd:/proc/self/fd:/dev/pts/:"
365 azarah 105 "/dev/vc/:/dev/pty:/dev/tty:/dev/console:"
366 azarah 89 "/dev/shm/ngpt:/var/log/scrollkeeper.log:"
367     "/usr/tmp/conftest:/usr/lib/conftest:"
368     "/usr/lib32/conftest:/usr/lib64/conftest:"
369     "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
370 azarah 101 sandbox_info->home_dir, sandbox_info->home_dir,
371     (NULL != sandbox_info->portage_tmp_dir) ? sandbox_info->portage_tmp_dir : tmp_dir,
372     sandbox_info->tmp_dir, sandbox_info->var_tmp_dir,
373     "/tmp/:/var/tmp/");
374 azarah 89 }
375    
376 azarah 101 void get_sandbox_predict_envvar(char *buf, struct sandbox_info_t *sandbox_info)
377 azarah 89 {
378 ferringb 2 /* bzero out entire buffer then append trailing 0 */
379 azarah 93 memset(buf, 0, SB_BUF_LEN);
380 ferringb 2
381 azarah 89 /* these should go into make.globals later on */
382     snprintf(buf, SB_BUF_LEN, "%s/.:"
383     "/usr/lib/python2.0/:"
384     "/usr/lib/python2.1/:"
385     "/usr/lib/python2.2/:"
386     "/usr/lib/python2.3/:"
387     "/usr/lib/python2.4/:"
388     "/usr/lib/python2.5/:"
389 azarah 126 "/usr/lib/python3.0/:"
390     "/var/db/aliases.db:"
391     "/var/db/netgroup.db:"
392     "/var/db/netmasks.db:"
393     "/var/db/ethers.db:"
394     "/var/db/rpc.db:"
395     "/var/db/protocols.db:"
396     "/var/db/services.db:"
397     "/var/db/networks.db:"
398     "/var/db/hosts.db:"
399     "/var/db/group.db:"
400 azarah 131 "/var/db/passwd.db",
401 azarah 101 sandbox_info->home_dir);
402 azarah 89 }
403 ferringb 2
404 azarah 124 int sandbox_setenv(char **env, const char *name, const char *val) {
405 azarah 89 char **tmp_env = env;
406     char *tmp_string = NULL;
407    
408 azarah 100 /* XXX: We add the new variable to the end (no replacing). If this
409     * is changed, we need to fix sandbox_setup_environ() below */
410 azarah 89 while (NULL != *tmp_env)
411     tmp_env++;
412    
413     /* strlen(name) + strlen(val) + '=' + '\0' */
414 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
415     * a real leak that will cause issues. */
416 azarah 89 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
417     if (NULL == tmp_string) {
418     perror(">>> out of memory (sandbox_setenv)");
419     exit(1);
420 ferringb 2 }
421 azarah 89
422     snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
423     name, val);
424     *tmp_env = tmp_string;
425    
426     return 0;
427 ferringb 2 }
428    
429 azarah 89 /* We setup the environment child side only to prevent issues with
430     * setting LD_PRELOAD parent side */
431 azarah 124 char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info)
432 ferringb 2 {
433 azarah 89 int env_size = 0;
434 azarah 100 int have_ld_preload = 0;
435 azarah 89
436     char **new_environ;
437     char **env_ptr = environ;
438 azarah 124 char sandbox_write_envvar[SB_BUF_LEN];
439     char sandbox_predict_envvar[SB_BUF_LEN];
440 azarah 89 char *ld_preload_envvar = NULL;
441 azarah 100 char *orig_ld_preload_envvar = NULL;
442 ferringb 2
443 azarah 89 /* Unset these, as its easier than replacing when setting up our
444     * new environment below */
445     unsetenv(ENV_SANDBOX_DIR);
446     unsetenv(ENV_SANDBOX_LIB);
447     unsetenv(ENV_SANDBOX_BASHRC);
448     unsetenv(ENV_SANDBOX_LOG);
449     unsetenv(ENV_SANDBOX_DEBUG_LOG);
450    
451 azarah 100 if (NULL != getenv(ENV_LD_PRELOAD)) {
452     have_ld_preload = 1;
453     orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD);
454    
455 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
456     * than a real leak that will cause issues. */
457 azarah 100 ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) +
458 azarah 101 strlen(sandbox_info->sandbox_lib) + 2,
459     sizeof(char *));
460 azarah 89 if (NULL == ld_preload_envvar)
461     return NULL;
462 azarah 100 snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) +
463 azarah 101 strlen(sandbox_info->sandbox_lib) + 2, "%s %s",
464     sandbox_info->sandbox_lib, orig_ld_preload_envvar);
465 azarah 89 } else {
466 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
467     * than a real leak that will cause issues. */
468 azarah 101 ld_preload_envvar = strndup(sandbox_info->sandbox_lib,
469     strlen(sandbox_info->sandbox_lib));
470 azarah 89 if (NULL == ld_preload_envvar)
471     return NULL;
472     }
473 azarah 100 /* Do not unset this, as strange things might happen */
474     /* unsetenv(ENV_LD_PRELOAD); */
475 ferringb 2
476 azarah 89 while (NULL != *env_ptr) {
477     env_size++;
478     env_ptr++;
479     }
480 ferringb 2
481 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
482     * a real leak that will cause issues. */
483 azarah 89 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
484     if (NULL == new_environ)
485     return NULL;
486    
487     /* First add our new variables to the beginning - this is due to some
488     * weirdness that I cannot remember */
489 azarah 101 sandbox_setenv(new_environ, ENV_SANDBOX_DIR, sandbox_info->sandbox_dir);
490     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 100 /* If LD_PRELOAD was not set, set it here, else do it below */
496     if (1 != have_ld_preload)
497     sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar);
498 azarah 89
499     if (!getenv(ENV_SANDBOX_DENY))
500     sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
501    
502     if (!getenv(ENV_SANDBOX_READ))
503     sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
504    
505 azarah 124 get_sandbox_write_envvar(sandbox_write_envvar, sandbox_info);
506 azarah 89 if (!getenv(ENV_SANDBOX_WRITE))
507     sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
508    
509 azarah 124 get_sandbox_predict_envvar(sandbox_predict_envvar, sandbox_info);
510 azarah 89 if (!getenv(ENV_SANDBOX_PREDICT))
511     sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
512    
513     /* This one should NEVER be set in ebuilds, as it is the one
514     * private thing libsandbox.so use to test if the sandbox
515     * should be active for this pid, or not.
516     *
517     * azarah (3 Aug 2002)
518     */
519    
520     sandbox_setenv(new_environ, "SANDBOX_ACTIVE", "armedandready");
521    
522     env_size = 0;
523     while (NULL != new_environ[env_size])
524     env_size++;
525    
526     /* Now add the rest */
527     env_ptr = environ;
528     while (NULL != *env_ptr) {
529 azarah 100 if ((1 == have_ld_preload) &&
530     (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr))
531     /* If LD_PRELOAD was set, and this is it in the original
532     * environment, replace it with our new copy */
533 azarah 101 /* XXX: The following works as it just add whatever as
534     * the last variable to nev_environ */
535 azarah 100 sandbox_setenv(new_environ, ENV_LD_PRELOAD,
536     ld_preload_envvar);
537     else
538     new_environ[env_size + (env_ptr - environ)] = *env_ptr;
539 azarah 89 env_ptr++;
540 ferringb 2 }
541 azarah 89
542     return new_environ;
543 ferringb 2 }
544    
545 azarah 89 int spawn_shell(char *argv_bash[], char *env[])
546 ferringb 2 {
547     int pid;
548     int status = 0;
549     int ret = 0;
550    
551     pid = fork();
552    
553     /* Child's process */
554     if (0 == pid) {
555 azarah 89 execve(argv_bash[0], argv_bash, env);
556 ferringb 2 return 0;
557     } else if (pid < 0) {
558     return 0;
559     }
560     ret = waitpid(pid, &status, 0);
561     if ((-1 == ret) || (status > 0))
562     return 0;
563 azarah 86
564 ferringb 2 return 1;
565     }
566    
567 azarah 30 int main(int argc, char **argv)
568 ferringb 2 {
569 azarah 101 int i = 0, success = 1;
570 ferringb 2 int sandbox_log_presence = 0;
571     long len;
572    
573 azarah 101 struct sandbox_info_t sandbox_info;
574 ferringb 2
575 azarah 89 char **sandbox_environ;
576 ferringb 2 char **argv_bash = NULL;
577    
578     char *run_str = "-c";
579     char *home_dir = NULL;
580     char *tmp_string = NULL;
581    
582     /* Only print info if called with no arguments .... */
583     if (argc < 2)
584     print_debug = 1;
585    
586     if (print_debug)
587 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
588 ferringb 2
589     /* check if a sandbox is already running */
590     if (NULL != getenv(ENV_SANDBOX_ON)) {
591 azarah 30 fprintf(stderr, "Not launching a new sandbox instance\n");
592     fprintf(stderr, "Another one is already running in this process hierarchy.\n");
593 ferringb 2 exit(1);
594     } else {
595    
596     /* determine the location of all the sandbox support files */
597     if (print_debug)
598     printf("Detection of the support files.\n");
599    
600 azarah 101 if (-1 == sandbox_setup(argv, &sandbox_info)) {
601     perror(">>> setup");
602 azarah 99 exit(1);
603     }
604 azarah 101
605 ferringb 2 /* verify the existance of required files */
606     if (print_debug)
607     printf("Verification of the required files.\n");
608    
609     #ifndef SB_HAVE_64BIT_ARCH
610 azarah 101 if (file_exist(sandbox_info.sandbox_lib, 0) <= 0) {
611     fprintf(stderr, "Could not open the sandbox library at '%s'.\n",
612     sandbox_info.sandbox_lib);
613 ferringb 2 return -1;
614 azarah 30 }
615 ferringb 2 #endif
616 azarah 101 if (file_exist(sandbox_info.sandbox_rc, 0) <= 0) {
617     fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n",
618     sandbox_info.sandbox_rc);
619 ferringb 2 return -1;
620     }
621    
622     /* set up the required environment variables */
623     if (print_debug)
624     printf("Setting up the required environment variables.\n");
625    
626 azarah 91 /* This one should not be child only, as we check above to see
627     * if we are already running (check sandbox_setup_environ).
628     * This needs to be set before calling sandbox_setup_environ(),
629     * else its not set for the child */
630     setenv(ENV_SANDBOX_ON, "1", 0);
631    
632     /* Setup the child environment stuff */
633 azarah 124 sandbox_environ = sandbox_setup_environ(&sandbox_info);
634 azarah 89 if (NULL == sandbox_environ) {
635     perror(">>> out of memory (environ)");
636     exit(1);
637 azarah 71 }
638 ferringb 2
639     /* if the portage temp dir was present, cd into it */
640 azarah 101 if (NULL != sandbox_info.portage_tmp_dir)
641     chdir(sandbox_info.portage_tmp_dir);
642 ferringb 2
643 azarah 30 argv_bash = (char **)malloc(6 * sizeof(char *));
644 ferringb 2 argv_bash[0] = strdup("/bin/bash");
645     argv_bash[1] = strdup("-rcfile");
646 azarah 101 argv_bash[2] = strdup(sandbox_info.sandbox_rc);
647 ferringb 2
648     if (argc < 2)
649     argv_bash[3] = NULL;
650     else
651     argv_bash[3] = strdup(run_str); /* "-c" */
652    
653 azarah 30 argv_bash[4] = NULL; /* strdup(run_arg); */
654 ferringb 2 argv_bash[5] = NULL;
655    
656     if (argc >= 2) {
657     for (i = 1; i < argc; i++) {
658     if (NULL == argv_bash[4])
659     len = 0;
660     else
661     len = strlen(argv_bash[4]);
662    
663 azarah 89 argv_bash[4] = (char *)realloc(argv_bash[4],
664     (len + strlen(argv[i]) + 2) * sizeof(char));
665 ferringb 2
666     if (0 == len)
667     argv_bash[4][0] = 0;
668     if (1 != i)
669     strcat(argv_bash[4], " ");
670    
671     strcat(argv_bash[4], argv[i]);
672     }
673     }
674    
675     /* set up the required signal handlers */
676     signal(SIGHUP, &stop);
677     signal(SIGINT, &stop);
678     signal(SIGQUIT, &stop);
679     signal(SIGTERM, &stop);
680    
681     /* Load our PID into PIDs file */
682 azarah 101 if (-1 == write_pids_file(&sandbox_info)) {
683 ferringb 2 perror(">>> pids file write");
684     exit(1);
685     }
686    
687     /* STARTING PROTECTED ENVIRONMENT */
688     if (print_debug) {
689     printf("The protected environment has been started.\n");
690 azarah 30 printf("--------------------------------------------------------------------------------\n");
691 ferringb 2 }
692    
693     if (print_debug)
694     printf("Shell being started in forked process.\n");
695    
696     /* Start Bash */
697 azarah 89 if (!spawn_shell(argv_bash, sandbox_environ)) {
698 ferringb 2 if (print_debug)
699     fprintf(stderr, ">>> shell process failed to spawn\n");
700     success = 0;
701     }
702    
703     /* Free bash stuff */
704     for (i = 0; i < 6; i++) {
705     if (argv_bash[i])
706     free(argv_bash[i]);
707     argv_bash[i] = NULL;
708     }
709     if (argv_bash)
710     free(argv_bash);
711     argv_bash = NULL;
712    
713     if (print_debug)
714     printf("Cleaning up sandbox process\n");
715    
716 azarah 101 sandbox_cleanup();
717 ferringb 2
718     if (print_debug) {
719 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
720 ferringb 2 printf("The protected environment has been shut down.\n");
721     }
722    
723 azarah 101 if (file_exist(sandbox_info.sandbox_log, 0)) {
724 ferringb 2 sandbox_log_presence = 1;
725 azarah 123 print_sandbox_log(sandbox_info.sandbox_log);
726 ferringb 2 } else if (print_debug) {
727 azarah 30 printf("--------------------------------------------------------------------------------\n");
728 ferringb 2 }
729    
730 azarah 101 free(sandbox_info.sandbox_pids_file);
731 azarah 35
732 ferringb 2 if ((sandbox_log_presence) || (!success))
733     return 1;
734     else
735     return 0;
736     }
737     }
738    
739 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