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 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 struct stat st;
00131 extern char **environ;
00132 char *path = "PATH=/bin:/usr/bin:/sbin:/usr/sbin";
00133
00134
00135
00136
00137
00138 for(i= 0; i < 3; i++) {
00139
00140 if(fstat(i, &st) == -1 && open("/dev/null", O_RDWR) != i) {
00141
00142 error("Cannot open /dev/null -- %s\n", STRERROR);
00143 exit(1);
00144
00145 }
00146
00147 }
00148
00149
00150
00151
00152
00153 for(i = 3; i < 1024; i++)
00154 (void) close(i);
00155 errno= 0;
00156
00157
00158
00159
00160
00161
00162 environ[0]= 0;
00163
00164 if(putenv(path)) {
00165
00166 error("%s: cannot set the PATH variable -- %s\n", prog, STRERROR);
00167 exit(1);
00168
00169 }
00170
00171 }
00172
00173
00177 static void set_environment(void) {
00178
00179 struct passwd *pw;
00180 char cwd[STRLEN];
00181
00182
00183 if ( ! (pw= getpwuid(geteuid())) ) {
00184
00185 error("%s: You don't exist. Go away.\n", prog);
00186 exit(1);
00187
00188 }
00189
00190
00191 if ( ! (getcwd(cwd, sizeof(cwd))) ) {
00192
00193 error("%s: Cannot read current directory -- %s\n", prog, STRERROR);
00194 exit(1);
00195
00196 }
00197
00198
00199
00200
00201 Run.localhostname= get_localhostname();
00202
00203
00204
00205
00206 Run.umask= umask(0);
00207
00208
00209
00210
00211 Run.Env.home= xstrdup(pw->pw_dir);
00212 Run.Env.cwd= xstrdup(cwd);
00213 Run.Env.user= xstrdup(pw->pw_name);
00214
00215 }