env.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C), 2000-2002 by Contributors to the monit codebase. 
00003  * All Rights Reserved.
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 /* Private prototypes */
00052 static void set_sandbox(void);
00053 static void set_environment(void);
00054 
00067 /* ------------------------------------------------------------------ Public */
00068 
00069 
00073 void init_env() {
00074   
00075   /*
00076    * Do we have a valid tty?
00077    */
00078   have_tty= (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO));
00079 
00080   /*
00081    * Setup for safe(r) exec
00082    */
00083   set_sandbox();
00084 
00085   /*
00086    * Setup program environment
00087    */
00088   set_environment();
00089   
00090 }
00091 
00092 
00093 /* ----------------------------------------------------------------- Private */
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    * Require that file descriptors 0,1,2 are open. Mysterious things
00136    * can happen if that is not the case.
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    * Require that the other file descriptios are closed. Should we use
00151    * sysconf() or getdtablesize() or getrlimit() or...
00152    */
00153   for(i = 3; i < 1024; i++)
00154       (void) close(i);
00155   errno= 0;
00156 
00157   /*
00158    * Purge the environment. Then make sure PATH is set; some shells default
00159    * to a path with '.' first. You may have to putenv() other stuff, too,
00160    * but be careful with importing too much.
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   /* Get password struct */
00183   if ( ! (pw= getpwuid(geteuid())) ) {
00184     
00185     error("%s: You don't exist. Go away.\n", prog);
00186     exit(1);
00187     
00188   }
00189   
00190   /* Get CWD */
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    * Get the localhost host name
00200    */
00201   Run.localhostname= get_localhostname();
00202 
00203   /*
00204    * Save and clear the file creation mask
00205    */
00206   Run.umask= umask(0);
00207   
00208   /*
00209    * Initialize the runtime environment object
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 }