[svn-inject] Installing original source of suphp
[manu/suphp.git] / src / check.c
1 /*
2     suPHP - (c)2002-2004 Sebastian Marsching <sebastian@marsching.com>
3     
4     This file is part of suPHP.
5
6     suPHP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     suPHP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with suPHP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include "suphp.h"
27
28 int check_path(char *script_path)
29 {
30  char *document_root = getenv("DOCUMENT_ROOT");
31  if(!document_root)
32  {
33   suphp_log_error("Could not get DOCUMENT_ROOT from the environment processing %s", script_path);
34   error_msg_exit(ERRCODE_WRONG_ENVIRONMENT, "DOCUMENT_ROOT not set", __FILE__, __LINE__);
35  }
36  if (strncmp(document_root, script_path, strlen(document_root))!=0)
37   return 0;
38  else
39   return 1;
40 }
41
42 int check_permissions(char *script_path)
43 {
44  struct stat file_info;
45  
46  if (stat(script_path, &file_info))
47  {
48   suphp_log_error("Could not stat() script %s", script_path);
49   error_sysmsg_exit(ERRCODE_UNKNOWN, "stat() failed", __FILE__, __LINE__);
50  }
51  
52  if (!(file_info.st_mode & S_IRUSR))
53  {
54   suphp_log_error("Owner doesn't have read permission on %s", script_path);
55   return 0;
56  }
57  
58  if (file_info.st_mode & (S_IWGRP | S_IWOTH))
59  {
60   suphp_log_error("Script (%s) is writeable by others", script_path);
61   return 0;
62  }
63   
64  return 1;
65 }