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   Run.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   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    * Require that file descriptors 0,1,2 are open. Mysterious things
00137    * can happen if that is not the case.
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    * Require that the other file descriptios are closed. Everything
00152    * seems to have getdtablesize, so we'll use it here, and back
00153    * out to use 1024 if getdtablesize not available.
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    * Purge the environment. Then make sure PATH is set; some shells default
00164    * to a path with '.' first. You may have to putenv() other stuff, too,
00165    * but be careful with importing too much.
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   /* Get password struct */
00188   if ( ! (pw= getpwuid(geteuid())) ) {
00189     
00190     error("%s: You don't exist. Go away.\n", prog);
00191     exit(1);
00192     
00193   }
00194   
00195   /* Get CWD */
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    * Get the localhost host name
00205    */
00206   Run.localhostname= get_localhostname();
00207 
00208   /*
00209    * Save and clear the file creation mask
00210    */
00211   Run.umask= umask(0);
00212   
00213   /*
00214    * Initialize the runtime environment object
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