2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 of the PHP license, |
8
| that is bundled with this package in the file LICENSE, and is |
9
| available through the world-wide-web at the following url: |
10
| http://www.php.net/license/3_0.txt. |
11
| If you did not receive a copy of the PHP license and are unable to |
12
| obtain it through the world-wide-web, please send a note to |
13
| license@php.net so we can mail you a copy immediately. |
14
+----------------------------------------------------------------------+
15
| Author: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
16
+----------------------------------------------------------------------+
19
/* $Id: safe_mode.c,v 1.58.2.2 2005/07/16 12:14:45 hyanantha Exp $ */
30
#include "ext/standard/pageinfo.h"
31
#include "safe_mode.h"
33
#include "php_globals.h"
38
* This function has six modes:
40
* 0 - return invalid (0) if file does not exist
41
* 1 - return valid (1) if file does not exist
42
* 2 - if file does not exist, check directory
43
* 3 - only check directory (needed for mkdir)
44
* 4 - check mode and param
48
PHPAPI int php_checkuid_ex(const char *filename, char *fopen_mode, int mode, int flags)
52
long uid=0L, gid=0L, duid=0L, dgid=0L;
53
char path[MAXPATHLEN];
54
char *s, filenamecopy[MAXPATHLEN];
55
php_stream_wrapper *wrapper = NULL;
59
return 0; /* path must be provided */
62
if (strlcpy(filenamecopy, filename, MAXPATHLEN)>=MAXPATHLEN) {
65
filename=(char *)&filenamecopy;
69
if (fopen_mode[0] == 'r') {
70
mode = CHECKUID_DISALLOW_FILE_NOT_EXISTS;
72
mode = CHECKUID_CHECK_FILE_AND_DIR;
77
* If given filepath is a URL, allow - safe mode stuff
78
* related to URL's is checked in individual functions
80
wrapper = php_stream_locate_url_wrapper(filename, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC);
84
/* First we see if the file is owned by the same user...
85
* If that fails, passthrough and check directory...
87
if (mode != CHECKUID_ALLOW_ONLY_DIR) {
88
VCWD_REALPATH(filename, path);
89
ret = VCWD_STAT(path, &sb);
91
if (mode == CHECKUID_DISALLOW_FILE_NOT_EXISTS) {
92
if ((flags & CHECKUID_NO_ERRORS) == 0) {
93
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to access %s", filename);
96
} else if (mode == CHECKUID_ALLOW_FILE_NOT_EXISTS) {
97
if ((flags & CHECKUID_NO_ERRORS) == 0) {
98
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to access %s", filename);
106
if (uid == php_getuid()) {
108
} else if (PG(safe_mode_gid) && gid == php_getgid()) {
113
/* Trim off filename */
114
if ((s = strrchr(path, DEFAULT_SLASH))) {
120
} else { /* CHECKUID_ALLOW_ONLY_DIR */
121
s = strrchr(filename, DEFAULT_SLASH);
125
path[0] = DEFAULT_SLASH;
129
VCWD_REALPATH(filename, path);
132
/* Under Solaris, getcwd() can fail if there are no
133
* read permissions on a component of the path, even
134
* though it has the required x permissions */
137
VCWD_GETCWD(path, sizeof(path));
139
} /* end CHECKUID_ALLOW_ONLY_DIR */
141
if (mode != CHECKUID_ALLOW_ONLY_FILE) {
142
/* check directory */
143
ret = VCWD_STAT(path, &sb);
145
if ((flags & CHECKUID_NO_ERRORS) == 0) {
146
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to access %s", filename);
152
if (duid == php_getuid()) {
154
} else if (PG(safe_mode_gid) && dgid == php_getgid()) {
159
if (SG(rfc1867_uploaded_files)) {
160
if (zend_hash_exists(SG(rfc1867_uploaded_files), (char *) filename, strlen(filename)+1)) {
167
if (mode == CHECKUID_ALLOW_ONLY_DIR) {
181
if ((flags & CHECKUID_NO_ERRORS) == 0) {
182
if (PG(safe_mode_gid)) {
183
php_error_docref(NULL TSRMLS_CC, E_WARNING, "SAFE MODE Restriction in effect. The script whose uid/gid is %ld/%ld is not allowed to access %s owned by uid/gid %ld/%ld", php_getuid(), php_getgid(), filename, uid, gid);
185
php_error_docref(NULL TSRMLS_CC, E_WARNING, "SAFE MODE Restriction in effect. The script whose uid is %ld is not allowed to access %s owned by uid %ld", php_getuid(), filename, uid);
192
PHPAPI int php_checkuid(const char *filename, char *fopen_mode, int mode) {
194
/* NetWare don't have uid*/
197
return php_checkuid_ex(filename, fopen_mode, mode, 0);
201
PHPAPI char *php_get_current_user()
207
if (SG(request_info).current_user) {
208
return SG(request_info).current_user;
211
/* FIXME: I need to have this somehow handled if
212
USE_SAPI is defined, because cgi will also be
213
interfaced in USE_SAPI */
215
pstat = sapi_get_stat(TSRMLS_C);
221
if ((pwd=getpwuid(pstat->st_uid))==NULL) {
224
SG(request_info).current_user_length = strlen(pwd->pw_name);
225
SG(request_info).current_user = estrndup(pwd->pw_name, SG(request_info).current_user_length);
227
return SG(request_info).current_user;
235
* vim600: sw=4 ts=4 fdm=marker