php-src/main/safe_mode.c

139 lines
3.6 KiB
C
Raw Normal View History

1999-04-08 05:05:13 +08:00
/*
+----------------------------------------------------------------------+
1999-07-16 21:13:16 +08:00
| PHP version 4.0 |
1999-04-08 05:05:13 +08:00
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
1999-04-08 05:05:13 +08:00
+----------------------------------------------------------------------+
| This source file is subject to version 2.01 of the PHP license, |
1999-07-16 21:13:16 +08:00
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_01.txt. |
1999-07-16 21:13:16 +08:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
1999-04-08 05:05:13 +08:00
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
1999-04-24 04:06:01 +08:00
1999-04-08 05:05:13 +08:00
#include "php.h"
#include <stdio.h>
#include <stdlib.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/stat.h>
#include "ext/standard/pageinfo.h"
1999-04-08 05:05:13 +08:00
#include "safe_mode.h"
#include "SAPI.h"
1999-04-08 05:05:13 +08:00
/*
1999-12-18 03:16:50 +08:00
* php_checkuid
1999-04-08 05:05:13 +08:00
*
* This function has four modes:
*
* 0 - return invalid (0) if file does not exist
* 1 - return valid (1) if file does not exist
* 2 - if file does not exist, check directory
* 3 - only check directory (needed for mkdir)
*/
1999-12-18 03:16:50 +08:00
PHPAPI int php_checkuid(const char *fn, int mode) {
1999-04-08 05:05:13 +08:00
struct stat sb;
int ret;
long uid=0L, duid=0L;
char *s;
if (!fn) return(0); /* path must be provided */
/*
* If given filepath is a URL, allow - safe mode stuff
* related to URL's is checked in individual functions
*/
if (!strncasecmp(fn,"http://",7) || !strncasecmp(fn,"ftp://",6)) {
return(1);
}
if (mode<3) {
ret = stat(fn,&sb);
if (ret<0 && mode < 2) {
php_error(E_WARNING,"Unable to access %s",fn);
1999-04-08 05:05:13 +08:00
return(mode);
}
if (ret>-1) {
uid=sb.st_uid;
1999-12-18 03:51:39 +08:00
if (uid==php_getuid()) return(1);
1999-04-08 05:05:13 +08:00
}
}
s = strrchr(fn,'/');
/* This loop gets rid of trailing slashes which could otherwise be
* used to confuse the function.
*/
while(s && *(s+1)=='\0' && s>fn) {
2000-01-08 22:36:12 +08:00
*s='\0';
1999-04-08 05:05:13 +08:00
s = strrchr(fn,'/');
}
if (s) {
*s='\0';
ret = stat(fn,&sb);
*s='/';
if (ret<0) {
php_error(E_WARNING, "Unable to access %s",fn);
1999-04-08 05:05:13 +08:00
return(0);
}
duid = sb.st_uid;
} else {
s = emalloc(MAXPATHLEN+1);
if (!getcwd(s,MAXPATHLEN)) {
php_error(E_WARNING, "Unable to access current working directory");
1999-04-08 05:05:13 +08:00
return(0);
}
ret = stat(s,&sb);
efree(s);
if (ret<0) {
php_error(E_WARNING, "Unable to access %s",s);
1999-04-08 05:05:13 +08:00
return(0);
}
duid = sb.st_uid;
}
1999-12-18 03:51:39 +08:00
if (duid == (uid=php_getuid())) return(1);
1999-04-08 05:05:13 +08:00
else {
php_error(E_WARNING, "SAFE MODE Restriction in effect. The script whose uid is %ld is not allowed to access %s owned by uid %ld",uid,fn,duid);
1999-04-08 05:05:13 +08:00
return(0);
}
}
1999-12-18 03:16:50 +08:00
PHPAPI char *php_get_current_user()
1999-04-08 05:05:13 +08:00
{
struct passwd *pwd;
2000-02-11 02:19:04 +08:00
struct stat *pstat;
SLS_FETCH();
1999-04-08 05:05:13 +08:00
if (SG(request_info).current_user) {
return SG(request_info).current_user;
1999-04-08 05:05:13 +08:00
}
/* FIXME: I need to have this somehow handled if
USE_SAPI is defined, because cgi will also be
interfaced in USE_SAPI */
2000-02-11 01:26:57 +08:00
2000-02-11 02:19:04 +08:00
pstat = sapi_get_stat();
2000-02-11 01:26:57 +08:00
2000-02-11 02:19:04 +08:00
if (!pstat) {
1999-04-08 05:05:13 +08:00
return empty_string;
}
2000-02-11 02:19:04 +08:00
if ((pwd=getpwuid(pstat->st_uid))==NULL) {
1999-04-08 05:05:13 +08:00
return empty_string;
}
SG(request_info).current_user_length = strlen(pwd->pw_name);
SG(request_info).current_user = estrndup(pwd->pw_name, SG(request_info).current_user_length);
1999-04-08 05:05:13 +08:00
return SG(request_info).current_user;
1999-04-08 05:05:13 +08:00
}