00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <errno.h>
00025 #include <pwd.h>
00026
00027 #ifdef HAVE_STRING_H
00028 #include <string.h>
00029 #endif
00030
00031 #ifdef HAVE_UNISTD_H
00032 #include <unistd.h>
00033 #endif
00034
00035 #ifdef HAVE_SYS_TYPES_H
00036 #include <sys/types.h>
00037 #endif
00038
00039 #ifdef HAVE_SYS_STAT_H
00040 #include <sys/stat.h>
00041 #endif
00042
00043 #ifdef HAVE_FCNTL_H
00044 #include <fcntl.h>
00045 #endif
00046
00047 #include "net.h"
00048 #include "monitor.h"
00049
00050
00051
00052 static void set_sandbox(void);
00053 static void set_environment(void);
00054
00067
00068
00069
00073 void init_env() {
00074
00075
00076
00077
00078 Run.have_tty= (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO));
00079
00080
00081
00082
00083 set_sandbox();
00084
00085
00086
00087
00088 set_environment();
00089
00090 }
00091
00092
00093
00094
00095
00127 static void set_sandbox(void) {
00128
00129 int i;
00130 int max_descriptors = 1024;
00131 struct stat st;
00132 extern char **environ;
00133 char *path = "PATH=/bin:/usr/bin:/sbin:/usr/sbin";
00134
00135
00136
00137
00138
00139 for(i= 0; i < 3; i++) {
00140
00141 if(fstat(i, &st) == -1 && open("/dev/null", O_RDWR) != i) {
00142
00143 error("Cannot open /dev/null -- %s\n", STRERROR);
00144 exit(1);
00145
00146 }
00147
00148 }
00149
00150
00151
00152
00153
00154
00155 #ifdef HAVE_UNISTD_H
00156 max_descriptors = getdtablesize();
00157 #endif
00158 for(i = 3; i < max_descriptors; i++)
00159 (void) close(i);
00160 errno= 0;
00161
00162
00163
00164
00165
00166
00167 environ[0]= 0;
00168
00169 if(putenv(path)) {
00170
00171 error("%s: cannot set the PATH variable -- %s\n", prog, STRERROR);
00172 exit(1);
00173
00174 }
00175
00176 }
00177
00178
00182 static void set_environment(void) {
00183
00184 struct passwd *pw;
00185 char cwd[STRLEN];
00186
00187
00188 if ( ! (pw= getpwuid(geteuid())) ) {
00189
00190 error("%s: You don't exist. Go away.\n", prog);
00191 exit(1);
00192
00193 }
00194
00195
00196 if ( ! (getcwd(cwd, sizeof(cwd))) ) {
00197
00198 error("%s: Cannot read current directory -- %s\n", prog, STRERROR);
00199 exit(1);
00200
00201 }
00202
00203
00204
00205
00206 Run.localhostname= get_localhostname();
00207
00208
00209
00210
00211 Run.umask= umask(0);
00212
00213
00214
00215
00216 Run.Env.home= xstrdup(pw->pw_dir);
00217 Run.Env.cwd= xstrdup(cwd);
00218 Run.Env.user= xstrdup(pw->pw_name);
00219
00220 }
00221