diff options
Diffstat (limited to 'main/fopen_wrappers.c')
-rw-r--r-- | main/fopen_wrappers.c | 188 |
1 files changed, 81 insertions, 107 deletions
diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c index f8cc1be3a8..38d410018b 100644 --- a/main/fopen_wrappers.c +++ b/main/fopen_wrappers.c @@ -2,7 +2,7 @@ +----------------------------------------------------------------------+ | PHP HTML Embedded Scripting Language Version 3.0 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-1999 PHP Development Team (See Credits file) | + | Copyright (c) 1997,1998 PHP Development Team (See Credits file) | +----------------------------------------------------------------------+ | This program is free software; you can redistribute it and/or modify | | it under the terms of one of the following licenses: | @@ -51,6 +51,7 @@ #include "safe_mode.h" #include "php3_realpath.h" +#include "ext/standard/head.h" #include "ext/standard/php3_standard.h" #include "zend_compile.h" @@ -87,128 +88,97 @@ #include <sys/un.h> #endif -static FILE *php3_fopen_url_wrapper(char *path, char *mode, int options, int *issock, int *socketd); +static FILE *php3_fopen_url_wrapper(const char *path, char *mode, int options, int *issock, int *socketd); int _php3_getftpresult(int socketd); - /* When open_basedir is not NULL, check if the given filename is located in open_basedir. Returns -1 if error or not in the open_basedir, else 0 When open_basedir is NULL, always return 0 */ -PHPAPI int _php3_check_specific_open_basedir(char *basedir, char *path) +PHPAPI int _php3_check_open_basedir(char *path) { char resolved_name[MAXPATHLEN]; - char resolved_basedir[MAXPATHLEN]; char local_open_basedir[MAXPATHLEN]; int local_open_basedir_pos; + PLS_FETCH(); - /* Special case basedir==".": Use script-directory */ - if ((strcmp(basedir, ".") == 0) && - request_info.filename && - *request_info.filename - ) { - strcpy(local_open_basedir, request_info.filename); - local_open_basedir_pos = strlen(local_open_basedir) - 1; - - /* Strip filename */ - while (( -#if WIN32|WINNT - (local_open_basedir[local_open_basedir_pos] != '\\') || -#endif - (local_open_basedir[local_open_basedir_pos] != '/') - ) && - (local_open_basedir_pos >= 0) + /* Only check when open_basedir is available */ + if (PG(open_basedir) && *PG(open_basedir)) { + + /* Special case basedir==".": Use script-directory */ + if ((strcmp(PG(open_basedir), ".") == 0) && + request_info.filename && + *request_info.filename ) { - local_open_basedir[local_open_basedir_pos--] = 0; - } -/* stripping unnecessary slashes is left - as an exercise to the underlying OS */ -#if 0 - /* Strip double (back)slashes */ - if (local_open_basedir_pos > 0) { + strcpy(local_open_basedir, request_info.filename); + local_open_basedir_pos = strlen(local_open_basedir) - 1; + + /* Strip filename */ while (( #if WIN32|WINNT - (local_open_basedir[local_open_basedir_pos-1] == '\\') || + (local_open_basedir[local_open_basedir_pos] != '\\') || #endif - (local_open_basedir[local_open_basedir_pos-1] == '/') + (local_open_basedir[local_open_basedir_pos] != '/') ) && - (local_open_basedir_pos > 0) + (local_open_basedir_pos >= 0) ) { local_open_basedir[local_open_basedir_pos--] = 0; } - } -#endif - } else { - /* Else use the unmodified path */ - strcpy(local_open_basedir, basedir); - } - - /* Resolve the real path into resolved_name */ - if ((_php3_realpath(path, resolved_name) != NULL) && (_php3_realpath(local_open_basedir, resolved_basedir) != NULL)) { - /* Check the path */ + +#if 0 + /* Strip double (back)slashes */ + if (local_open_basedir_pos > 0) { + while (( #if WIN32|WINNT - if (strncasecmp(resolved_basedir, resolved_name, strlen(resolved_basedir)) == 0) { -#else - if (strncmp(resolved_basedir, resolved_name, strlen(resolved_basedir)) == 0) { + (local_open_basedir[local_open_basedir_pos-1] == '\\') || #endif - /* File is in the right directory */ - return 0; + (local_open_basedir[local_open_basedir_pos-1] == '/') + ) && + (local_open_basedir_pos > 0) + ) { + local_open_basedir[local_open_basedir_pos--] = 0; + } + } +#endif + } else { - return -1; + /* Else use the unmodified path */ + strcpy(local_open_basedir, PG(open_basedir)); } - } else { - /* Unable to resolve the real path, return -1 */ - return -1; - } -} - -PHPAPI int _php3_check_open_basedir(char *path) -{ - /* Only check when open_basedir is available */ - if (PG(open_basedir) && *PG(open_basedir)) { - char *pathbuf; - char *ptr; - char *end; - - pathbuf = estrdup(PG(open_basedir)); - - ptr = pathbuf; - - while (ptr && *ptr) { + + /* Resolve the real path into resolved_name */ + if (_php3_realpath(path, resolved_name) != NULL) { + /* Check the path */ #if WIN32|WINNT - end = strchr(ptr, ';'); + if (strncasecmp(local_open_basedir, resolved_name, strlen(local_open_basedir)) == 0) { #else - end = strchr(ptr, ':'); + if (strncmp(local_open_basedir, resolved_name, strlen(local_open_basedir)) == 0) { #endif - if (end != NULL) { - *end = '\0'; - end++; - } - - if (_php3_check_specific_open_basedir(ptr, path) == 0) { - efree(pathbuf); + /* File is in the right directory */ return 0; + } else { + php3_error(E_WARNING, "open_basedir restriction in effect. File is in wrong directory."); + return -1; } - - ptr = end; + } else { + /* Unable to resolve the real path, return -1 */ + php3_error(E_WARNING, "open_basedir restriction in effect. Unable to verify location of file."); + return -1; } - php3_error(E_WARNING, "open_basedir restriction in effect. File is in wrong directory."); - efree(pathbuf); - return -1; + } else { + /* open_basedir is not available, return 0 */ + return 0; } - - /* Nothing to check... */ - return 0; } PHPAPI FILE *php3_fopen_wrapper(char *path, char *mode, int options, int *issock, int *socketd) { int cm=2; /* checkuid mode: 2 = if file does not exist, check directory */ - /* FIXME Lets not get in the habit of doing stuff like this. This should - be runtime enabled, NOT compile time. */ + PLS_FETCH(); + #if PHP3_URL_FOPEN if (!(options & IGNORE_URL)) { return php3_fopen_url_wrapper(path, mode, options, issock, socketd); @@ -229,17 +199,17 @@ PHPAPI FILE *php3_fopen_wrapper(char *path, char *mode, int options, int *issock #if CGI_BINARY || FHTTPD || USE_SAPI -FILE *php3_fopen_for_parser(void) +PHPAPI FILE *php3_fopen_for_parser(void) { FILE *fp; struct stat st; char *temp, *path_info, *fn; int l; PLS_FETCH(); - + SLS_FETCH(); fn = request_info.filename; - path_info = request_info.path_info; + path_info = SG(request_info).request_uri; #if HAVE_PWD_H if (PG(user_dir) && *PG(user_dir) && path_info && '/' == path_info[0] && '~' == path_info[1]) { @@ -310,17 +280,17 @@ FILE *php3_fopen_for_parser(void) fp = NULL; } if (!fp) { - php3_error(E_ERROR, "Unable to open %s", fn); + php3_error(E_CORE_ERROR, "Unable to open %s", fn); STR_FREE(request_info.filename); /* for same reason as above */ return NULL; } - _php3_hash_index_update(&include_names, 0, (void *) &fn, sizeof(char *), NULL); - - temp = strdup(fn); + + temp = estrdup(fn); _php3_dirname(temp, strlen(temp)); - if (*temp) + if (*temp) { chdir(temp); - free(temp); + } + efree(temp); return fp; } @@ -447,7 +417,7 @@ PHPAPI FILE *php3_fopen_with_path(char *filename, char *mode, char *path, char * * Otherwise, fopen is called as usual and the file pointer is returned. */ -static FILE *php3_fopen_url_wrapper(char *path, char *mode, int options, int *issock, int *socketd) +static FILE *php3_fopen_url_wrapper(const char *path, char *mode, int options, int *issock, int *socketd) { url *resource; int result; @@ -473,9 +443,10 @@ static FILE *php3_fopen_url_wrapper(char *path, char *mode, int options, int *is FILE *fp = NULL; struct sockaddr_in server; unsigned short portno; + char winfeof; if (!strncasecmp(path, "http://", 7)) { - resource = url_parse(path); + resource = url_parse((char *) path); if (resource == NULL) { php3_error(E_WARNING, "Invalid URL specified, %s", path); *issock = BAD_URL; @@ -492,15 +463,16 @@ static FILE *php3_fopen_url_wrapper(char *path, char *mode, int options, int *is free_url(resource); return NULL; } - server.sin_family = AF_INET; + lookup_hostname(resource->host, &server.sin_addr); - if (lookup_hostname(resource->host, &server.sin_addr)) { + if (server.sin_addr.s_addr == -1) { SOCK_FCLOSE(*socketd); *socketd = 0; free_url(resource); return NULL; } server.sin_port = htons(resource->port); + server.sin_family = AF_INET; if (connect(*socketd, (struct sockaddr *) &server, sizeof(server)) == SOCK_CONN_ERR) { SOCK_FCLOSE(*socketd); @@ -582,8 +554,8 @@ static FILE *php3_fopen_url_wrapper(char *path, char *mode, int options, int *is /* Read past http header */ body = 0; location[0] = '\0'; - while (!body && !SOCK_FEOF(*socketd)) { - if ((buf[0] = SOCK_FGETC(*socketd)) == EOF) { + while (!body && recv(*socketd, (char *) &winfeof, 1, MSG_PEEK)) { + if (SOCK_FGETC(buf, *socketd) == SOCK_RECV_ERR) { SOCK_FCLOSE(*socketd); *socketd = 0; free_url(resource); @@ -635,7 +607,7 @@ static FILE *php3_fopen_url_wrapper(char *path, char *mode, int options, int *is *issock = 1; return (fp); } else if (!strncasecmp(path, "ftp://", 6)) { - resource = url_parse(path); + resource = url_parse((char *) path); if (resource == NULL) { php3_error(E_WARNING, "Invalid URL specified, %s", path); *issock = BAD_URL; @@ -657,9 +629,9 @@ static FILE *php3_fopen_url_wrapper(char *path, char *mode, int options, int *is free_url(resource); return NULL; } - server.sin_family = AF_INET; + lookup_hostname(resource->host, &server.sin_addr); - if (lookup_hostname(resource->host, &server.sin_addr)) { + if (server.sin_addr.s_addr == -1) { SOCK_FCLOSE(*socketd); *socketd = 0; free_url(resource); @@ -869,9 +841,9 @@ static FILE *php3_fopen_url_wrapper(char *path, char *mode, int options, int *is free_url(resource); return NULL; } - server.sin_family = AF_INET; + lookup_hostname(resource->host, &server.sin_addr); - if (lookup_hostname(resource->host, &server.sin_addr)) { + if (server.sin_addr.s_addr == -1) { free_url(resource); SOCK_FCLOSE(*socketd); *socketd = 0; @@ -910,15 +882,17 @@ static FILE *php3_fopen_url_wrapper(char *path, char *mode, int options, int *is return (fp); } else { + PLS_FETCH(); + if (options & USE_PATH) { - fp = php3_fopen_with_path(path, mode, PG(include_path), NULL); + fp = php3_fopen_with_path((char *) path, mode, PG(include_path), NULL); } else { int cm=2; if(!strcmp(mode,"r") || !strcmp(mode,"r+")) cm=0; if (options & ENFORCE_SAFE_MODE && PG(safe_mode) && (!_php3_checkuid(path, cm))) { fp = NULL; } else { - if (_php3_check_open_basedir(path)) { + if (_php3_check_open_basedir((char *) path)) { fp = NULL; } else { fp = fopen(path, mode); |