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
|
|
|
+----------------------------------------------------------------------+
|
2000-01-01 09:32:05 +08:00
|
|
|
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
|
1999-04-08 05:05:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-05-18 23:34:45 +08:00
|
|
|
| This source file is subject to version 2.02 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 |
|
2000-05-18 23:34:45 +08:00
|
|
|
| http://www.php.net/license/2_02.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> |
|
|
|
|
| Jim Winstead <jimw@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "php.h"
|
1999-04-10 20:17:20 +08:00
|
|
|
#include "php_globals.h"
|
1999-05-03 03:54:02 +08:00
|
|
|
#include "SAPI.h"
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
1999-04-08 05:05:13 +08:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winsock.h>
|
|
|
|
#define O_RDONLY _O_RDONLY
|
|
|
|
#include "win32/param.h"
|
|
|
|
#else
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "safe_mode.h"
|
1999-06-16 05:51:00 +08:00
|
|
|
#include "ext/standard/head.h"
|
1999-12-05 03:19:57 +08:00
|
|
|
#include "ext/standard/php_standard.h"
|
1999-04-08 05:05:13 +08:00
|
|
|
#include "zend_compile.h"
|
2000-09-05 23:59:08 +08:00
|
|
|
#include "php_network.h"
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
#if HAVE_PWD_H
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
1999-04-08 05:05:13 +08:00
|
|
|
#include "win32/pwd.h"
|
|
|
|
#else
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#if HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef S_ISREG
|
2000-04-01 06:48:59 +08:00
|
|
|
#define S_ISREG(mode) (((mode)&S_IFMT) & S_IFREG)
|
1999-04-08 05:05:13 +08:00
|
|
|
#endif
|
|
|
|
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
1999-04-08 05:05:13 +08:00
|
|
|
#include <winsock.h>
|
|
|
|
#else
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
1999-04-08 05:05:13 +08:00
|
|
|
#undef AF_UNIX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(AF_UNIX)
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
|
2000-06-08 17:43:12 +08:00
|
|
|
typedef FILE * (*php_fopen_url_wrapper_t) (const char *, char *, int, int *, int *, char **) ;
|
|
|
|
|
|
|
|
static FILE *php_fopen_url_wrapper(const char *, char *, int, int *, int *, char **);
|
|
|
|
static FILE *php_fopen_url_wrap_http(const char *, char *, int, int *, int *, char **);
|
|
|
|
static FILE *php_fopen_url_wrap_ftp(const char *, char *, int, int *, int *, char **);
|
|
|
|
static FILE *php_fopen_url_wrap_php(const char *, char *, int, int *, int *, char **);
|
1999-04-08 05:05:13 +08:00
|
|
|
|
1999-12-18 03:51:39 +08:00
|
|
|
int php_get_ftp_result(int socketd);
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-08 17:43:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
HashTable fopen_url_wrappers_hash;
|
|
|
|
|
2000-07-29 18:48:57 +08:00
|
|
|
PHPAPI int php_register_url_wrapper(char *protocol, FILE * (*wrapper)(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path))
|
|
|
|
{
|
2000-06-08 17:43:12 +08:00
|
|
|
#if PHP_URL_FOPEN
|
|
|
|
return zend_hash_add(&fopen_url_wrappers_hash, protocol, strlen(protocol)+1, &wrapper, sizeof(wrapper), NULL);
|
|
|
|
#else
|
|
|
|
return FAILURE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-07-29 18:48:57 +08:00
|
|
|
PHPAPI int php_unregister_url_wrapper(char *protocol)
|
|
|
|
{
|
2000-06-08 17:43:12 +08:00
|
|
|
#if PHP_URL_FOPEN
|
|
|
|
return zend_hash_del(&fopen_url_wrappers_hash, protocol, strlen(protocol)+1);
|
|
|
|
#else
|
|
|
|
return SUCCESS;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int php_init_fopen_wrappers(void)
|
|
|
|
{
|
|
|
|
int status = SUCCESS;
|
|
|
|
#if PHP_URL_FOPEN
|
|
|
|
if (zend_hash_init(&fopen_url_wrappers_hash, 0, NULL, NULL, 1)==FAILURE) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILURE==php_register_url_wrapper("http",php_fopen_url_wrap_http)) {
|
|
|
|
status = FAILURE;
|
|
|
|
} else
|
|
|
|
if(FAILURE==php_register_url_wrapper("ftp",php_fopen_url_wrap_ftp)) {
|
|
|
|
status = FAILURE;
|
|
|
|
} else
|
|
|
|
if(FAILURE==php_register_url_wrapper("php",php_fopen_url_wrap_php)) {
|
|
|
|
status = FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILURE==status) {
|
|
|
|
zend_hash_destroy(&fopen_url_wrappers_hash);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int php_shutdown_fopen_wrappers(void)
|
|
|
|
{
|
|
|
|
#if PHP_URL_FOPEN
|
|
|
|
zend_hash_destroy(&fopen_url_wrappers_hash);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
1999-12-18 03:16:50 +08:00
|
|
|
PHPAPI int php_check_specific_open_basedir(char *basedir, char *path PLS_DC)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
char resolved_name[MAXPATHLEN];
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
char resolved_basedir[MAXPATHLEN];
|
1999-04-08 05:05:13 +08:00
|
|
|
char local_open_basedir[MAXPATHLEN];
|
|
|
|
int local_open_basedir_pos;
|
1999-06-19 21:04:55 +08:00
|
|
|
SLS_FETCH();
|
1999-04-08 05:05:13 +08:00
|
|
|
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
/* Special case basedir==".": Use script-directory */
|
|
|
|
if ((strcmp(PG(open_basedir), ".") == 0) &&
|
|
|
|
SG(request_info).path_translated &&
|
|
|
|
*SG(request_info).path_translated
|
1999-04-08 05:05:13 +08:00
|
|
|
) {
|
2000-03-11 02:19:08 +08:00
|
|
|
strlcpy(local_open_basedir, SG(request_info).path_translated, sizeof(local_open_basedir));
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
local_open_basedir_pos = strlen(local_open_basedir) - 1;
|
1999-06-16 05:51:00 +08:00
|
|
|
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
/* Strip filename */
|
2000-06-16 08:56:38 +08:00
|
|
|
while (!IS_SLASH(local_open_basedir[local_open_basedir_pos])
|
|
|
|
&& (local_open_basedir_pos >= 0)) {
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
local_open_basedir[local_open_basedir_pos--] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Else use the unmodified path */
|
2000-03-11 02:19:08 +08:00
|
|
|
strlcpy(local_open_basedir, basedir, sizeof(local_open_basedir));
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Resolve the real path into resolved_name */
|
2000-08-09 00:36:25 +08:00
|
|
|
if ((expand_filepath(path, resolved_name) != NULL) && (expand_filepath(local_open_basedir, resolved_basedir) != NULL)) {
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
/* Check the path */
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
if (strncasecmp(resolved_basedir, resolved_name, strlen(resolved_basedir)) == 0) {
|
|
|
|
#else
|
|
|
|
if (strncmp(resolved_basedir, resolved_name, strlen(resolved_basedir)) == 0) {
|
1999-06-16 05:51:00 +08:00
|
|
|
#endif
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
/* File is in the right directory */
|
|
|
|
return 0;
|
1999-04-08 05:05:13 +08:00
|
|
|
} else {
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
return -1;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
} else {
|
|
|
|
/* Unable to resolve the real path, return -1 */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-18 03:16:50 +08:00
|
|
|
PHPAPI int php_check_open_basedir(char *path)
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
{
|
1999-06-19 21:04:55 +08:00
|
|
|
PLS_FETCH();
|
|
|
|
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
/* 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) {
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
end = strchr(ptr, ';');
|
1999-04-08 05:05:13 +08:00
|
|
|
#else
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
end = strchr(ptr, ':');
|
1999-04-08 05:05:13 +08:00
|
|
|
#endif
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
if (end != NULL) {
|
|
|
|
*end = '\0';
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
1999-12-18 03:16:50 +08:00
|
|
|
if (php_check_specific_open_basedir(ptr, path PLS_CC) == 0) {
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
efree(pathbuf);
|
1999-04-08 05:05:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
|
|
|
|
ptr = end;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "open_basedir restriction in effect. File is in wrong directory.");
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
efree(pathbuf);
|
2000-08-09 00:36:25 +08:00
|
|
|
errno = EPERM; /* we deny permission to open it */
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
return -1;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
|
|
|
|
/* Nothing to check... */
|
|
|
|
return 0;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
2000-08-21 17:50:53 +08:00
|
|
|
static FILE *php_fopen_and_set_opened_path(const char *path, char *mode, char **opened_path)
|
2000-08-17 03:26:21 +08:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
2000-08-19 07:03:49 +08:00
|
|
|
if (php_check_open_basedir((char *)path)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-08-17 03:26:21 +08:00
|
|
|
fp = V_FOPEN(path, mode);
|
|
|
|
if (fp && opened_path) {
|
|
|
|
*opened_path = expand_filepath(path,NULL);
|
|
|
|
}
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
1999-12-18 03:16:50 +08:00
|
|
|
PHPAPI FILE *php_fopen_wrapper(char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
1999-06-16 05:51:00 +08:00
|
|
|
PLS_FETCH();
|
|
|
|
|
1999-12-06 23:30:44 +08:00
|
|
|
if (opened_path) {
|
|
|
|
*opened_path = NULL;
|
|
|
|
}
|
|
|
|
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
/* FIXME Lets not get in the habit of doing stuff like this. This should
|
|
|
|
be runtime enabled, NOT compile time. */
|
2000-02-26 11:20:55 +08:00
|
|
|
#if PHP_URL_FOPEN
|
1999-04-08 05:05:13 +08:00
|
|
|
if (!(options & IGNORE_URL)) {
|
1999-12-18 03:51:39 +08:00
|
|
|
return php_fopen_url_wrapper(path, mode, options, issock, socketd, opened_path);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-04-11 00:25:23 +08:00
|
|
|
if (options & USE_PATH && PG(include_path) != NULL) {
|
1999-12-18 03:16:50 +08:00
|
|
|
return php_fopen_with_path(path, mode, PG(include_path), opened_path);
|
1999-04-08 05:05:13 +08:00
|
|
|
} else {
|
2000-06-26 01:02:59 +08:00
|
|
|
if (options & ENFORCE_SAFE_MODE && PG(safe_mode) && (!php_checkuid(path, mode, 0))) {
|
1999-04-08 05:05:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-08-17 03:26:21 +08:00
|
|
|
return php_fopen_and_set_opened_path(path, mode, opened_path);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-18 03:16:50 +08:00
|
|
|
PHPAPI FILE *php_fopen_primary_script(void)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
struct stat st;
|
2000-10-03 23:05:50 +08:00
|
|
|
char *path_info, *filename;
|
2000-06-16 08:56:38 +08:00
|
|
|
int length;
|
1999-04-21 12:02:11 +08:00
|
|
|
PLS_FETCH();
|
1999-06-16 05:51:00 +08:00
|
|
|
SLS_FETCH();
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-16 08:56:38 +08:00
|
|
|
filename = SG(request_info).path_translated;
|
1999-06-16 05:51:00 +08:00
|
|
|
path_info = SG(request_info).request_uri;
|
1999-04-08 05:05:13 +08:00
|
|
|
#if HAVE_PWD_H
|
1999-04-11 00:25:23 +08:00
|
|
|
if (PG(user_dir) && *PG(user_dir)
|
1999-04-08 05:05:13 +08:00
|
|
|
&& path_info && '/' == path_info[0] && '~' == path_info[1]) {
|
|
|
|
|
|
|
|
char user[32];
|
|
|
|
struct passwd *pw;
|
|
|
|
char *s = strchr(path_info + 2, '/');
|
|
|
|
|
2000-06-16 08:56:38 +08:00
|
|
|
filename = NULL; /* discard the original filename, it must not be used */
|
|
|
|
if (s) { /* if there is no path name after the file, do not bother */
|
|
|
|
/* to try open the directory */
|
|
|
|
length = s - (path_info + 2);
|
|
|
|
if (length > sizeof(user) - 1)
|
|
|
|
length = sizeof(user) - 1;
|
|
|
|
memcpy(user, path_info + 2, length);
|
|
|
|
user[length] = '\0';
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
pw = getpwnam(user);
|
|
|
|
if (pw && pw->pw_dir) {
|
2000-06-16 08:56:38 +08:00
|
|
|
filename = emalloc(strlen(PG(user_dir)) + strlen(path_info) + strlen(pw->pw_dir) + 4);
|
|
|
|
if (filename) {
|
2000-08-21 03:56:24 +08:00
|
|
|
sprintf(filename, "%s%c%s%c%s", pw->pw_dir, PHP_DIR_SEPARATOR,
|
|
|
|
PG(user_dir), PHP_DIR_SEPARATOR, s+1); /* Safe */
|
1999-06-27 03:14:26 +08:00
|
|
|
STR_FREE(SG(request_info).path_translated);
|
2000-06-16 08:56:38 +08:00
|
|
|
SG(request_info).path_translated = filename;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
2000-06-16 09:23:03 +08:00
|
|
|
if (PG(doc_root) && path_info) {
|
|
|
|
length = strlen(PG(doc_root));
|
2000-09-01 03:49:36 +08:00
|
|
|
if (IS_ABSOLUTE_PATH(PG(doc_root), length)) {
|
2000-06-16 09:23:03 +08:00
|
|
|
filename = emalloc(length + strlen(path_info) + 2);
|
|
|
|
if (filename) {
|
|
|
|
memcpy(filename, PG(doc_root), length);
|
|
|
|
if (!IS_SLASH(filename[length - 1])) { /* length is never 0 */
|
2000-08-21 03:56:24 +08:00
|
|
|
filename[length++] = PHP_DIR_SEPARATOR;
|
2000-06-16 09:23:03 +08:00
|
|
|
}
|
|
|
|
if (IS_SLASH(path_info[0])) {
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
strcpy(filename + length, path_info);
|
|
|
|
STR_FREE(SG(request_info).path_translated);
|
|
|
|
SG(request_info).path_translated = filename;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2000-06-16 09:23:03 +08:00
|
|
|
} /* if doc_root && path_info */
|
|
|
|
|
2000-06-16 08:56:38 +08:00
|
|
|
if (!filename) {
|
1999-06-27 03:14:26 +08:00
|
|
|
/* we have to free SG(request_info).path_translated here because
|
1999-12-22 03:43:38 +08:00
|
|
|
php_destroy_request_info assumes that it will get
|
1999-04-08 05:05:13 +08:00
|
|
|
freed when the include_names hash is emptied, but
|
|
|
|
we're not adding it in this case */
|
1999-06-27 03:14:26 +08:00
|
|
|
STR_FREE(SG(request_info).path_translated);
|
|
|
|
SG(request_info).path_translated = NULL;
|
1999-04-08 05:05:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-26 01:02:59 +08:00
|
|
|
fp = V_FOPEN(filename, "rb");
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/* refuse to open anything that is not a regular file */
|
|
|
|
if (fp && (0 > fstat(fileno(fp), &st) || !S_ISREG(st.st_mode))) {
|
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
if (!fp) {
|
2000-06-16 08:56:38 +08:00
|
|
|
php_error(E_ERROR, "Unable to open %s", filename);
|
1999-06-27 03:14:26 +08:00
|
|
|
STR_FREE(SG(request_info).path_translated); /* for same reason as above */
|
1999-04-08 05:05:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-10-03 23:05:50 +08:00
|
|
|
V_CHDIR_FILE(filename);
|
2000-06-16 08:56:38 +08:00
|
|
|
SG(request_info).path_translated = filename;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tries to open a file with a PATH-style list of directories.
|
|
|
|
* If the filename starts with "." or "/", the path is ignored.
|
|
|
|
*/
|
1999-12-18 03:16:50 +08:00
|
|
|
PHPAPI FILE *php_fopen_with_path(char *filename, char *mode, char *path, char **opened_path)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
char *pathbuf, *ptr, *end;
|
|
|
|
char trypath[MAXPATHLEN + 1];
|
|
|
|
struct stat sb;
|
2000-08-19 07:03:49 +08:00
|
|
|
FILE *fp;
|
2000-09-15 04:47:35 +08:00
|
|
|
int filename_length;
|
1999-04-21 12:02:11 +08:00
|
|
|
PLS_FETCH();
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
if (opened_path) {
|
|
|
|
*opened_path = NULL;
|
|
|
|
}
|
1999-12-06 23:30:44 +08:00
|
|
|
|
2000-09-17 13:52:26 +08:00
|
|
|
filename_length = strlen(filename);
|
|
|
|
|
|
|
|
/* Absolute & relative path open */
|
|
|
|
if ((*filename == '.') || (IS_ABSOLUTE_PATH(filename, filename_length))) {
|
2000-06-26 01:02:59 +08:00
|
|
|
if (PG(safe_mode) && (!php_checkuid(filename, mode, 0))) {
|
1999-04-08 05:05:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-08-17 03:26:21 +08:00
|
|
|
return php_fopen_and_set_opened_path(filename, mode, opened_path);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2000-09-15 04:47:35 +08:00
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
if (!path || (path && !*path)) {
|
2000-06-26 01:02:59 +08:00
|
|
|
if (PG(safe_mode) && (!php_checkuid(filename, mode, 0))) {
|
1999-04-08 05:05:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-08-17 03:26:21 +08:00
|
|
|
return php_fopen_and_set_opened_path(filename, mode, opened_path);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
pathbuf = estrdup(path);
|
|
|
|
|
|
|
|
ptr = pathbuf;
|
|
|
|
|
|
|
|
while (ptr && *ptr) {
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
1999-04-08 05:05:13 +08:00
|
|
|
end = strchr(ptr, ';');
|
|
|
|
#else
|
|
|
|
end = strchr(ptr, ':');
|
|
|
|
#endif
|
|
|
|
if (end != NULL) {
|
|
|
|
*end = '\0';
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename);
|
1999-04-10 20:17:20 +08:00
|
|
|
if (PG(safe_mode)) {
|
2000-06-26 01:02:59 +08:00
|
|
|
if (V_STAT(trypath, &sb) == 0 && (!php_checkuid(trypath, mode, 0))) {
|
1999-04-08 05:05:13 +08:00
|
|
|
efree(pathbuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2000-08-19 07:03:49 +08:00
|
|
|
fp = php_fopen_and_set_opened_path(trypath, mode, opened_path);
|
|
|
|
if (fp) {
|
1999-04-08 05:05:13 +08:00
|
|
|
efree(pathbuf);
|
2000-08-19 07:03:49 +08:00
|
|
|
return fp;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
ptr = end;
|
|
|
|
}
|
|
|
|
efree(pathbuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the specified path starts with "http://" (insensitive to case),
|
|
|
|
* a socket is opened to the specified web server and a file pointer is
|
|
|
|
* position at the start of the body of the response (past any headers).
|
|
|
|
* This makes a HTTP/1.0 request, and knows how to pass on the username
|
|
|
|
* and password for basic authentication.
|
|
|
|
*
|
|
|
|
* If the specified path starts with "ftp://" (insensitive to case),
|
|
|
|
* a pair of sockets are used to request the specified file and a file
|
|
|
|
* pointer to the requested file is returned. Passive mode ftp is used,
|
1999-06-18 22:45:48 +08:00
|
|
|
* so if the server doesn't support this, it will fail!
|
1999-04-08 05:05:13 +08:00
|
|
|
*
|
|
|
|
* Otherwise, fopen is called as usual and the file pointer is returned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-06-08 17:43:12 +08:00
|
|
|
static FILE *php_fopen_url_wrap_http(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
|
|
|
|
{
|
|
|
|
FILE *fp=NULL;
|
2000-07-30 06:32:21 +08:00
|
|
|
php_url *resource=NULL;
|
1999-06-18 22:22:59 +08:00
|
|
|
char tmp_line[512];
|
|
|
|
char location[512];
|
|
|
|
char hdr_line[8192];
|
1999-04-08 05:05:13 +08:00
|
|
|
int body = 0;
|
2000-06-08 17:43:12 +08:00
|
|
|
char *scratch;
|
|
|
|
unsigned char *tmp;
|
|
|
|
int len;
|
1999-04-08 05:05:13 +08:00
|
|
|
int reqok = 0;
|
|
|
|
|
2000-06-08 17:43:12 +08:00
|
|
|
resource = url_parse((char *) path);
|
|
|
|
if (resource == NULL) {
|
|
|
|
php_error(E_WARNING, "Invalid URL specified, %s", path);
|
|
|
|
*issock = BAD_URL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* use port 80 if one wasn't specified */
|
|
|
|
if (resource->port == 0)
|
|
|
|
resource->port = 80;
|
|
|
|
|
2000-09-06 00:36:56 +08:00
|
|
|
*socketd = php_hostconnect(resource->host, resource->port, SOCK_STREAM, 0);
|
2000-09-05 23:59:08 +08:00
|
|
|
if (*socketd == -1) {
|
2000-06-08 17:43:12 +08:00
|
|
|
SOCK_FCLOSE(*socketd);
|
|
|
|
*socketd = 0;
|
|
|
|
free_url(resource);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
#if 0
|
2000-06-08 17:43:12 +08:00
|
|
|
if ((fp = fdopen(*socketd, "r+")) == NULL) {
|
|
|
|
free_url(resource);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
#ifdef HAVE_SETVBUF
|
2000-06-08 17:43:12 +08:00
|
|
|
if ((setvbuf(fp, NULL, _IONBF, 0)) != 0) {
|
|
|
|
free_url(resource);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /*win32 */
|
|
|
|
|
|
|
|
strcpy(hdr_line, "GET ");
|
|
|
|
|
|
|
|
/* tell remote http which file to get */
|
|
|
|
if (resource->path != NULL) {
|
|
|
|
strlcat(hdr_line, resource->path, sizeof(hdr_line));
|
|
|
|
} else {
|
|
|
|
strlcat(hdr_line, "/", sizeof(hdr_line));
|
|
|
|
}
|
|
|
|
/* append the query string, if any */
|
|
|
|
if (resource->query != NULL) {
|
|
|
|
strlcat(hdr_line, "?", sizeof(hdr_line));
|
|
|
|
strlcat(hdr_line, resource->query, sizeof(hdr_line));
|
|
|
|
}
|
|
|
|
strlcat(hdr_line, " HTTP/1.0\r\n", sizeof(hdr_line));
|
|
|
|
SOCK_WRITE(hdr_line, *socketd);
|
|
|
|
|
|
|
|
/* send authorization header if we have user/pass */
|
|
|
|
if (resource->user != NULL && resource->pass != NULL) {
|
|
|
|
scratch = (char *) emalloc(strlen(resource->user) + strlen(resource->pass) + 2);
|
|
|
|
if (!scratch) {
|
1999-04-08 05:05:13 +08:00
|
|
|
free_url(resource);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
strcpy(scratch, resource->user);
|
|
|
|
strcat(scratch, ":");
|
|
|
|
strcat(scratch, resource->pass);
|
|
|
|
tmp = php_base64_encode((unsigned char *)scratch, strlen(scratch), NULL);
|
1999-09-15 04:12:46 +08:00
|
|
|
|
2000-06-08 17:43:12 +08:00
|
|
|
if (snprintf(hdr_line, sizeof(hdr_line),
|
|
|
|
"Authorization: Basic %s\r\n", tmp) > 0) {
|
|
|
|
SOCK_WRITE(hdr_line, *socketd);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
1999-06-22 09:59:30 +08:00
|
|
|
|
2000-06-08 17:43:12 +08:00
|
|
|
efree(scratch);
|
|
|
|
efree(tmp);
|
|
|
|
}
|
|
|
|
/* if the user has configured who they are, send a From: line */
|
|
|
|
if (cfg_get_string("from", &scratch) == SUCCESS) {
|
|
|
|
if (snprintf(hdr_line, sizeof(hdr_line),
|
|
|
|
"From: %s\r\n", scratch) > 0) {
|
1999-06-22 09:59:30 +08:00
|
|
|
SOCK_WRITE(hdr_line, *socketd);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
/* send a Host: header so name-based virtual hosts work */
|
|
|
|
if (resource->port != 80) {
|
|
|
|
len = snprintf(hdr_line, sizeof(hdr_line),
|
|
|
|
"Host: %s:%i\r\n", resource->host, resource->port);
|
|
|
|
} else {
|
|
|
|
len = snprintf(hdr_line, sizeof(hdr_line),
|
|
|
|
"Host: %s\r\n", resource->host);
|
|
|
|
}
|
2000-09-08 20:52:05 +08:00
|
|
|
if(len > sizeof(hdr_line) - 1) {
|
|
|
|
len = sizeof(hdr_line) - 1;
|
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
if (len > 0) {
|
|
|
|
SOCK_WRITE(hdr_line, *socketd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* identify ourselves and end the headers */
|
|
|
|
SOCK_WRITE("User-Agent: PHP/" PHP_VERSION "\r\n\r\n", *socketd);
|
|
|
|
|
|
|
|
body = 0;
|
|
|
|
location[0] = '\0';
|
|
|
|
if (!SOCK_FEOF(*socketd)) {
|
|
|
|
/* get response header */
|
|
|
|
if (SOCK_FGETS(tmp_line, sizeof(tmp_line)-1, *socketd) != NULL) {
|
|
|
|
if (strncmp(tmp_line + 8, " 200 ", 5) == 0) {
|
|
|
|
reqok = 1;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
1999-06-18 22:22:59 +08:00
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
|
|
|
/* Read past HTTP headers */
|
|
|
|
while (!body && !SOCK_FEOF(*socketd)) {
|
|
|
|
if (SOCK_FGETS(tmp_line, sizeof(tmp_line)-1, *socketd) != NULL) {
|
|
|
|
char *p = tmp_line;
|
|
|
|
|
|
|
|
tmp_line[sizeof(tmp_line)-1] = '\0';
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '\n' || *p == '\r') {
|
|
|
|
*p = '\0';
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
p++;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
|
|
|
|
if (!strncasecmp(tmp_line, "Location: ", 10)) {
|
|
|
|
strlcpy(location, tmp_line + 10, sizeof(location));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp_line[0] == '\0') {
|
|
|
|
body = 1;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
|
|
|
if (!reqok) {
|
|
|
|
SOCK_FCLOSE(*socketd);
|
|
|
|
*socketd = 0;
|
1999-04-08 05:05:13 +08:00
|
|
|
free_url(resource);
|
2000-06-08 17:43:12 +08:00
|
|
|
if (location[0] != '\0') {
|
|
|
|
return php_fopen_url_wrapper(location, mode, options, issock, socketd, opened_path);
|
|
|
|
} else {
|
1999-04-08 05:05:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
|
|
|
free_url(resource);
|
|
|
|
*issock = 1;
|
|
|
|
return (fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *php_fopen_url_wrap_ftp(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
|
|
|
|
{
|
|
|
|
FILE *fp=NULL;
|
2000-07-30 06:32:21 +08:00
|
|
|
php_url *resource=NULL;
|
2000-06-08 17:43:12 +08:00
|
|
|
char tmp_line[512];
|
|
|
|
unsigned short portno;
|
|
|
|
char *scratch;
|
|
|
|
int result;
|
|
|
|
int i;
|
|
|
|
char *tpath, *ttpath;
|
|
|
|
|
|
|
|
resource = url_parse((char *) path);
|
|
|
|
if (resource == NULL) {
|
|
|
|
php_error(E_WARNING, "Invalid URL specified, %s", path);
|
|
|
|
*issock = BAD_URL;
|
|
|
|
return NULL;
|
|
|
|
} else if (resource->path == NULL) {
|
|
|
|
php_error(E_WARNING, "No file-path specified");
|
|
|
|
free_url(resource);
|
|
|
|
*issock = BAD_URL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* use port 21 if one wasn't specified */
|
|
|
|
if (resource->port == 0)
|
|
|
|
resource->port = 21;
|
2000-09-10 05:29:37 +08:00
|
|
|
|
|
|
|
*socketd = php_hostconnect(resource->host, resource->port, SOCK_STREAM, 0);
|
|
|
|
if (*socketd == -1)
|
|
|
|
goto errexit;
|
1999-04-08 05:05:13 +08:00
|
|
|
#if 0
|
2000-06-08 17:43:12 +08:00
|
|
|
if ((fpc = fdopen(*socketd, "r+")) == NULL) {
|
|
|
|
free_url(resource);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
#ifdef HAVE_SETVBUF
|
2000-06-08 17:43:12 +08:00
|
|
|
if ((setvbuf(fpc, NULL, _IONBF, 0)) != 0) {
|
|
|
|
free_url(resource);
|
|
|
|
fclose(fpc);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
2000-06-08 17:43:12 +08:00
|
|
|
|
|
|
|
/* Start talking to ftp server */
|
|
|
|
result = php_get_ftp_result(*socketd);
|
2000-09-10 05:29:37 +08:00
|
|
|
if (result > 299 || result < 200)
|
|
|
|
goto errexit;
|
|
|
|
|
2000-06-08 17:43:12 +08:00
|
|
|
/* send the user name */
|
|
|
|
SOCK_WRITE("USER ", *socketd);
|
|
|
|
if (resource->user != NULL) {
|
|
|
|
php_raw_url_decode(resource->user, strlen(resource->user));
|
|
|
|
SOCK_WRITE(resource->user, *socketd);
|
|
|
|
} else {
|
|
|
|
SOCK_WRITE("anonymous", *socketd);
|
|
|
|
}
|
|
|
|
SOCK_WRITE("\r\n", *socketd);
|
|
|
|
|
|
|
|
/* get the response */
|
|
|
|
result = php_get_ftp_result(*socketd);
|
|
|
|
|
|
|
|
/* if a password is required, send it */
|
|
|
|
if (result >= 300 && result <= 399) {
|
|
|
|
SOCK_WRITE("PASS ", *socketd);
|
|
|
|
if (resource->pass != NULL) {
|
|
|
|
php_raw_url_decode(resource->pass, strlen(resource->pass));
|
|
|
|
SOCK_WRITE(resource->pass, *socketd);
|
1999-04-08 05:05:13 +08:00
|
|
|
} else {
|
2000-06-08 17:43:12 +08:00
|
|
|
/* if the user has configured who they are,
|
|
|
|
send that as the password */
|
|
|
|
if (cfg_get_string("from", &scratch) == SUCCESS) {
|
|
|
|
SOCK_WRITE(scratch, *socketd);
|
1999-04-08 05:05:13 +08:00
|
|
|
} else {
|
2000-06-08 17:43:12 +08:00
|
|
|
SOCK_WRITE("anonymous", *socketd);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
}
|
1999-11-11 21:49:42 +08:00
|
|
|
SOCK_WRITE("\r\n", *socketd);
|
2000-06-08 17:43:12 +08:00
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
/* read the response */
|
1999-12-18 03:51:39 +08:00
|
|
|
result = php_get_ftp_result(*socketd);
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
2000-09-10 05:29:37 +08:00
|
|
|
if (result > 299 || result < 200)
|
|
|
|
goto errexit;
|
2000-06-08 17:43:12 +08:00
|
|
|
|
|
|
|
/* set the connection to be binary */
|
|
|
|
SOCK_WRITE("TYPE I\r\n", *socketd);
|
|
|
|
result = php_get_ftp_result(*socketd);
|
2000-09-10 05:29:37 +08:00
|
|
|
if (result > 299 || result < 200)
|
|
|
|
goto errexit;
|
2000-06-08 17:43:12 +08:00
|
|
|
|
|
|
|
/* find out the size of the file (verifying it exists) */
|
|
|
|
SOCK_WRITE("SIZE ", *socketd);
|
|
|
|
SOCK_WRITE(resource->path, *socketd);
|
|
|
|
SOCK_WRITE("\r\n", *socketd);
|
|
|
|
|
|
|
|
/* read the response */
|
|
|
|
result = php_get_ftp_result(*socketd);
|
|
|
|
if (mode[0] == 'r') {
|
|
|
|
/* when reading file, it must exist */
|
|
|
|
if (result > 299 || result < 200) {
|
|
|
|
php_error(E_WARNING, "File not found");
|
1999-04-08 05:05:13 +08:00
|
|
|
free_url(resource);
|
|
|
|
SOCK_FCLOSE(*socketd);
|
|
|
|
*socketd = 0;
|
2000-06-08 17:43:12 +08:00
|
|
|
errno = ENOENT;
|
1999-04-08 05:05:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
} else {
|
|
|
|
/* when writing file, it must NOT exist */
|
|
|
|
if (result <= 299 && result >= 200) {
|
|
|
|
php_error(E_WARNING, "File already exists");
|
1999-04-08 05:05:13 +08:00
|
|
|
free_url(resource);
|
|
|
|
SOCK_FCLOSE(*socketd);
|
|
|
|
*socketd = 0;
|
2000-06-08 17:43:12 +08:00
|
|
|
errno = EEXIST;
|
1999-04-08 05:05:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set up the passive connection */
|
2000-09-10 05:29:37 +08:00
|
|
|
|
|
|
|
/* We try EPSV first, needed for IPv6 and works on some IPv4 servers */
|
|
|
|
SOCK_WRITE("EPSV\r\n", *socketd);
|
2000-06-08 17:43:12 +08:00
|
|
|
while (SOCK_FGETS(tmp_line, sizeof(tmp_line)-1, *socketd) &&
|
|
|
|
!(isdigit((int) tmp_line[0]) && isdigit((int) tmp_line[1]) &&
|
|
|
|
isdigit((int) tmp_line[2]) && tmp_line[3] == ' '));
|
2000-09-10 05:29:37 +08:00
|
|
|
|
|
|
|
/* check if we got a 229 response */
|
|
|
|
if (strncmp(tmp_line, "229", 3)) {
|
|
|
|
/* EPSV failed, let's try PASV */
|
|
|
|
SOCK_WRITE("PASV\r\n", *socketd);
|
|
|
|
while (SOCK_FGETS(tmp_line, sizeof(tmp_line)-1, *socketd) &&
|
|
|
|
!(isdigit((int) tmp_line[0]) && isdigit((int) tmp_line[1]) &&
|
|
|
|
isdigit((int) tmp_line[2]) && tmp_line[3] == ' '));
|
|
|
|
/* make sure we got a 227 response */
|
|
|
|
if (strncmp(tmp_line, "227", 3))
|
|
|
|
goto errexit;
|
|
|
|
/* parse pasv command (129,80,95,25,13,221) */
|
|
|
|
tpath = tmp_line;
|
|
|
|
/* skip over the "227 Some message " part */
|
|
|
|
for (tpath += 4; *tpath && !isdigit((int) *tpath); tpath++);
|
|
|
|
if (!*tpath)
|
|
|
|
goto errexit;
|
|
|
|
/* skip over the host ip, we just assume it's the same */
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
for (; isdigit((int) *tpath); tpath++);
|
|
|
|
if (*tpath != ',')
|
|
|
|
goto errexit;
|
1999-04-08 05:05:13 +08:00
|
|
|
tpath++;
|
|
|
|
}
|
2000-09-10 05:29:37 +08:00
|
|
|
/* pull out the MSB of the port */
|
|
|
|
portno = (unsigned short) strtol(tpath, &ttpath, 10) * 256;
|
|
|
|
if (ttpath == NULL) {
|
|
|
|
/* didn't get correct response from PASV */
|
|
|
|
goto errexit;
|
|
|
|
}
|
|
|
|
tpath = ttpath;
|
|
|
|
if (*tpath != ',')
|
|
|
|
goto errexit;
|
2000-06-08 17:43:12 +08:00
|
|
|
tpath++;
|
2000-09-10 05:29:37 +08:00
|
|
|
/* pull out the LSB of the port */
|
|
|
|
portno += (unsigned short) strtol(tpath, &ttpath, 10);
|
2000-06-08 17:43:12 +08:00
|
|
|
} else {
|
2000-09-10 05:29:37 +08:00
|
|
|
/* parse epsv command (|||6446|) */
|
|
|
|
for (i = 0, tpath = tmp_line + 4; *tpath; tpath++) {
|
|
|
|
if (*tpath == '|') {
|
|
|
|
i++;
|
|
|
|
if (i == 3)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < 3)
|
|
|
|
goto errexit;
|
|
|
|
/* pull out the port */
|
|
|
|
portno = (unsigned short) strtol(tpath + 1, &ttpath, 10);
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ttpath == NULL) {
|
2000-09-10 05:29:37 +08:00
|
|
|
/* didn't get correct response from EPSV/PASV */
|
|
|
|
goto errexit;
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode[0] == 'r') {
|
|
|
|
/* retrieve file */
|
|
|
|
SOCK_WRITE("RETR ", *socketd);
|
|
|
|
} else {
|
|
|
|
/* store file */
|
|
|
|
SOCK_WRITE("STOR ", *socketd);
|
|
|
|
}
|
|
|
|
if (resource->path != NULL) {
|
|
|
|
SOCK_WRITE(resource->path, *socketd);
|
|
|
|
} else {
|
|
|
|
SOCK_WRITE("/", *socketd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close control connection */
|
|
|
|
SOCK_WRITE("\r\nQUIT\r\n", *socketd);
|
|
|
|
SOCK_FCLOSE(*socketd);
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-08 17:43:12 +08:00
|
|
|
/* open the data channel */
|
2000-09-10 05:29:37 +08:00
|
|
|
*socketd = php_hostconnect(resource->host, portno, SOCK_STREAM, 0);
|
|
|
|
if (*socketd == -1)
|
|
|
|
goto errexit;
|
2000-06-08 17:43:12 +08:00
|
|
|
#if 0
|
|
|
|
if (mode[0] == 'r') {
|
|
|
|
if ((fp = fdopen(*socketd, "r+")) == NULL) {
|
1999-04-08 05:05:13 +08:00
|
|
|
free_url(resource);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
} else {
|
|
|
|
if ((fp = fdopen(*socketd, "w+")) == NULL) {
|
1999-04-08 05:05:13 +08:00
|
|
|
free_url(resource);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
#ifdef HAVE_SETVBUF
|
2000-06-08 17:43:12 +08:00
|
|
|
if ((setvbuf(fp, NULL, _IONBF, 0)) != 0) {
|
|
|
|
free_url(resource);
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
2000-06-08 17:43:12 +08:00
|
|
|
free_url(resource);
|
|
|
|
*issock = 1;
|
|
|
|
return (fp);
|
2000-09-10 05:29:37 +08:00
|
|
|
|
|
|
|
errexit:
|
|
|
|
free_url(resource);
|
|
|
|
SOCK_FCLOSE(*socketd);
|
|
|
|
*socketd = 0;
|
|
|
|
return NULL;
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *php_fopen_url_wrap_php(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
|
|
|
|
{
|
|
|
|
const char *res = path + 6;
|
|
|
|
|
|
|
|
*issock = 0;
|
|
|
|
|
|
|
|
if (!strcasecmp(res, "stdin")) {
|
|
|
|
return fdopen(STDIN_FILENO, mode);
|
|
|
|
} else if (!strcasecmp(res, "stdout")) {
|
|
|
|
return fdopen(STDOUT_FILENO, mode);
|
|
|
|
} else if (!strcasecmp(res, "stderr")) {
|
|
|
|
return fdopen(STDERR_FILENO, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *php_fopen_url_wrapper(const char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
|
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
const char *p;
|
|
|
|
const char *protocol=NULL;
|
|
|
|
int n=0;
|
|
|
|
|
2000-06-09 21:50:39 +08:00
|
|
|
for(p=path;isalnum((int)*p);p++)
|
2000-06-08 17:43:12 +08:00
|
|
|
n++;
|
|
|
|
if((*p==':')&&(n>1)) {
|
|
|
|
protocol=path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(protocol) {
|
|
|
|
php_fopen_url_wrapper_t *wrapper=NULL;
|
|
|
|
char *protocopy = emalloc(n+1);
|
|
|
|
|
|
|
|
if(protocopy) {
|
|
|
|
strncpy(protocopy,protocol,n);
|
|
|
|
protocopy[n]='\0';
|
|
|
|
if(FAILURE==zend_hash_find(&fopen_url_wrappers_hash, protocopy, n+1,(void **)&wrapper)) {
|
|
|
|
wrapper=NULL;
|
2000-08-03 23:09:27 +08:00
|
|
|
protocol=NULL;
|
2000-06-08 17:43:12 +08:00
|
|
|
}
|
|
|
|
efree(protocopy);
|
|
|
|
}
|
|
|
|
if(wrapper)
|
|
|
|
return (*wrapper)(path, mode, options, issock, socketd, opened_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !protocol || !strncasecmp(protocol, "file",n)){
|
1999-06-16 05:51:00 +08:00
|
|
|
PLS_FETCH();
|
2000-06-08 17:43:12 +08:00
|
|
|
|
|
|
|
*issock = 0;
|
|
|
|
|
|
|
|
if(protocol) {
|
|
|
|
if(path[n+1]=='/') {
|
|
|
|
if(path[n+2]=='/') {
|
|
|
|
php_error(E_WARNING, "remote host file access not supported, %s", path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path+= n+1;
|
|
|
|
}
|
1999-06-16 05:51:00 +08:00
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
if (options & USE_PATH) {
|
1999-12-18 03:16:50 +08:00
|
|
|
fp = php_fopen_with_path((char *) path, mode, PG(include_path), opened_path);
|
1999-04-08 05:05:13 +08:00
|
|
|
} else {
|
2000-06-26 01:02:59 +08:00
|
|
|
if (options & ENFORCE_SAFE_MODE && PG(safe_mode) && (!php_checkuid(path, mode, 0))) {
|
1999-04-08 05:05:13 +08:00
|
|
|
fp = NULL;
|
|
|
|
} else {
|
2000-08-19 07:03:49 +08:00
|
|
|
fp = php_fopen_and_set_opened_path(path, mode, opened_path);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (fp);
|
|
|
|
}
|
2000-06-08 17:43:12 +08:00
|
|
|
|
|
|
|
php_error(E_WARNING, "Invalid URL specified, %s", path);
|
1999-04-08 05:05:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-12-18 03:51:39 +08:00
|
|
|
int php_get_ftp_result(int socketd)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
1999-06-17 02:43:59 +08:00
|
|
|
char tmp_line[513];
|
1999-04-08 05:05:13 +08:00
|
|
|
|
1999-08-26 00:24:14 +08:00
|
|
|
while (SOCK_FGETS(tmp_line, sizeof(tmp_line)-1, socketd) &&
|
1999-04-08 05:05:13 +08:00
|
|
|
!(isdigit((int) tmp_line[0]) && isdigit((int) tmp_line[1]) &&
|
|
|
|
isdigit((int) tmp_line[2]) && tmp_line[3] == ' '));
|
|
|
|
|
|
|
|
return strtol(tmp_line, NULL, 10);
|
|
|
|
}
|
|
|
|
|
1999-12-18 03:16:50 +08:00
|
|
|
PHPAPI int php_is_url(char *path)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
return (!strncasecmp(path, "http://", 7) || !strncasecmp(path, "ftp://", 6));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-18 03:16:50 +08:00
|
|
|
PHPAPI char *php_strip_url_passwd(char *url)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
register char *p = url, *url_start;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
if (*p==':' && *(p+1)=='/' && *(p+2)=='/') {
|
|
|
|
/* found protocol */
|
|
|
|
url_start = p = p+3;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
if (*p=='@') {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<3 && url_start<p; i++, url_start++) {
|
|
|
|
*url_start = '.';
|
|
|
|
}
|
|
|
|
for (; *p; p++) {
|
|
|
|
*url_start++ = *p;
|
|
|
|
}
|
|
|
|
*url_start=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-17 03:26:21 +08:00
|
|
|
PHPAPI char *expand_filepath(const char *filepath, char *real_path)
|
2000-04-10 03:54:50 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
char cwd[MAXPATHLEN+1];
|
|
|
|
char *result;
|
|
|
|
|
2000-04-21 05:07:39 +08:00
|
|
|
result = V_GETCWD(cwd, MAXPATHLEN);
|
2000-04-10 03:54:50 +08:00
|
|
|
if (!result) {
|
|
|
|
cwd[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
new_state.cwd = strdup(cwd);
|
|
|
|
new_state.cwd_length = strlen(cwd);
|
|
|
|
|
2000-08-09 00:36:25 +08:00
|
|
|
if(virtual_file_ex(&new_state, filepath, NULL)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(real_path) {
|
|
|
|
int copy_len = new_state.cwd_length>MAXPATHLEN-1?MAXPATHLEN-1:new_state.cwd_length;
|
|
|
|
memcpy(real_path,new_state.cwd,copy_len);
|
|
|
|
real_path[copy_len]='\0';
|
|
|
|
} else {
|
2000-09-01 06:24:20 +08:00
|
|
|
real_path = estrndup(new_state.cwd, new_state.cwd_length);
|
|
|
|
free(new_state.cwd);
|
2000-08-09 00:36:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return real_path;
|
2000-04-10 03:54:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
*/
|