php_open_temporary_file.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 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_01.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: Zeev Suraski <zeev@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "php_open_temporary_file.h"
  20. #include <errno.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #ifdef PHP_WIN32
  25. #define O_RDONLY _O_RDONLY
  26. #include "win32/param.h"
  27. #include "win32/winutil.h"
  28. #else
  29. #include <sys/param.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <netdb.h>
  33. #if HAVE_ARPA_INET_H
  34. #include <arpa/inet.h>
  35. #endif
  36. #endif
  37. #ifdef HAVE_SYS_TIME_H
  38. #include <sys/time.h>
  39. #endif
  40. #ifdef HAVE_SYS_FILE_H
  41. #include <sys/file.h>
  42. #endif
  43. #if !defined(P_tmpdir)
  44. #define P_tmpdir ""
  45. #endif
  46. /* {{{ php_do_open_temporary_file */
  47. /* Loosely based on a tempnam() implementation by UCLA */
  48. /*
  49. * Copyright (c) 1988, 1993
  50. * The Regents of the University of California. All rights reserved.
  51. *
  52. * Redistribution and use in source and binary forms, with or without
  53. * modification, are permitted provided that the following conditions
  54. * are met:
  55. * 1. Redistributions of source code must retain the above copyright
  56. * notice, this list of conditions and the following disclaimer.
  57. * 2. Redistributions in binary form must reproduce the above copyright
  58. * notice, this list of conditions and the following disclaimer in the
  59. * documentation and/or other materials provided with the distribution.
  60. * 3. All advertising materials mentioning features or use of this software
  61. * must display the following acknowledgement:
  62. * This product includes software developed by the University of
  63. * California, Berkeley and its contributors.
  64. * 4. Neither the name of the University nor the names of its contributors
  65. * may be used to endorse or promote products derived from this software
  66. * without specific prior written permission.
  67. *
  68. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  69. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  71. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  72. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  73. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  74. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  75. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  76. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  77. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  78. * SUCH DAMAGE.
  79. */
  80. static int php_do_open_temporary_file(const char *path, const char *pfx, zend_string **opened_path_p)
  81. {
  82. #ifdef PHP_WIN32
  83. char *opened_path = NULL;
  84. size_t opened_path_len;
  85. wchar_t *cwdw, *pfxw, pathw[MAXPATHLEN];
  86. #else
  87. char opened_path[MAXPATHLEN];
  88. char *trailing_slash;
  89. #endif
  90. char cwd[MAXPATHLEN];
  91. cwd_state new_state;
  92. int fd = -1;
  93. #ifndef HAVE_MKSTEMP
  94. int open_flags = O_CREAT | O_TRUNC | O_RDWR
  95. #ifdef PHP_WIN32
  96. | _O_BINARY
  97. #endif
  98. ;
  99. #endif
  100. if (!path || !path[0]) {
  101. return -1;
  102. }
  103. #ifdef PHP_WIN32
  104. if (!php_win32_check_trailing_space(pfx, strlen(pfx))) {
  105. SetLastError(ERROR_INVALID_NAME);
  106. return -1;
  107. }
  108. #endif
  109. if (!VCWD_GETCWD(cwd, MAXPATHLEN)) {
  110. cwd[0] = '\0';
  111. }
  112. new_state.cwd = estrdup(cwd);
  113. new_state.cwd_length = strlen(cwd);
  114. if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
  115. efree(new_state.cwd);
  116. return -1;
  117. }
  118. #ifndef PHP_WIN32
  119. if (IS_SLASH(new_state.cwd[new_state.cwd_length - 1])) {
  120. trailing_slash = "";
  121. } else {
  122. trailing_slash = "/";
  123. }
  124. if (snprintf(opened_path, MAXPATHLEN, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) {
  125. efree(new_state.cwd);
  126. return -1;
  127. }
  128. #endif
  129. #ifdef PHP_WIN32
  130. cwdw = php_win32_ioutil_any_to_w(new_state.cwd);
  131. pfxw = php_win32_ioutil_any_to_w(pfx);
  132. if (!cwdw || !pfxw) {
  133. free(cwdw);
  134. free(pfxw);
  135. efree(new_state.cwd);
  136. return -1;
  137. }
  138. if (GetTempFileNameW(cwdw, pfxw, 0, pathw)) {
  139. opened_path = php_win32_ioutil_conv_w_to_any(pathw, PHP_WIN32_CP_IGNORE_LEN, &opened_path_len);
  140. if (!opened_path || opened_path_len >= MAXPATHLEN) {
  141. free(cwdw);
  142. free(pfxw);
  143. efree(new_state.cwd);
  144. return -1;
  145. }
  146. assert(strlen(opened_path) == opened_path_len);
  147. /* Some versions of windows set the temp file to be read-only,
  148. * which means that opening it will fail... */
  149. if (VCWD_CHMOD(opened_path, 0600)) {
  150. free(cwdw);
  151. free(pfxw);
  152. efree(new_state.cwd);
  153. free(opened_path);
  154. return -1;
  155. }
  156. fd = VCWD_OPEN_MODE(opened_path, open_flags, 0600);
  157. }
  158. free(cwdw);
  159. free(pfxw);
  160. #elif defined(HAVE_MKSTEMP)
  161. fd = mkstemp(opened_path);
  162. #else
  163. if (mktemp(opened_path)) {
  164. fd = VCWD_OPEN(opened_path, open_flags);
  165. }
  166. #endif
  167. #ifdef PHP_WIN32
  168. if (fd != -1 && opened_path_p) {
  169. *opened_path_p = zend_string_init(opened_path, opened_path_len, 0);
  170. }
  171. free(opened_path);
  172. #else
  173. if (fd != -1 && opened_path_p) {
  174. *opened_path_p = zend_string_init(opened_path, strlen(opened_path), 0);
  175. }
  176. #endif
  177. efree(new_state.cwd);
  178. return fd;
  179. }
  180. /* }}} */
  181. /*
  182. * Determine where to place temporary files.
  183. */
  184. PHPAPI const char* php_get_temporary_directory(void)
  185. {
  186. /* Did we determine the temporary directory already? */
  187. if (PG(php_sys_temp_dir)) {
  188. return PG(php_sys_temp_dir);
  189. }
  190. /* Is there a temporary directory "sys_temp_dir" in .ini defined? */
  191. {
  192. char *sys_temp_dir = PG(sys_temp_dir);
  193. if (sys_temp_dir) {
  194. size_t len = strlen(sys_temp_dir);
  195. if (len >= 2 && sys_temp_dir[len - 1] == DEFAULT_SLASH) {
  196. PG(php_sys_temp_dir) = estrndup(sys_temp_dir, len - 1);
  197. return PG(php_sys_temp_dir);
  198. } else if (len >= 1 && sys_temp_dir[len - 1] != DEFAULT_SLASH) {
  199. PG(php_sys_temp_dir) = estrndup(sys_temp_dir, len);
  200. return PG(php_sys_temp_dir);
  201. }
  202. }
  203. }
  204. #ifdef PHP_WIN32
  205. /* We can't count on the environment variables TEMP or TMP,
  206. * and so must make the Win32 API call to get the default
  207. * directory for temporary files. Note this call checks
  208. * the environment values TMP and TEMP (in order) first.
  209. */
  210. {
  211. wchar_t sTemp[MAXPATHLEN];
  212. char *tmp;
  213. size_t len = GetTempPathW(MAXPATHLEN, sTemp);
  214. if (!len) {
  215. return NULL;
  216. }
  217. if (NULL == (tmp = php_win32_ioutil_conv_w_to_any(sTemp, len, &len))) {
  218. return NULL;
  219. }
  220. PG(php_sys_temp_dir) = estrndup(tmp, len - 1);
  221. free(tmp);
  222. return PG(php_sys_temp_dir);
  223. }
  224. #else
  225. /* On Unix use the (usual) TMPDIR environment variable. */
  226. {
  227. char* s = getenv("TMPDIR");
  228. if (s && *s) {
  229. size_t len = strlen(s);
  230. if (s[len - 1] == DEFAULT_SLASH) {
  231. PG(php_sys_temp_dir) = estrndup(s, len - 1);
  232. } else {
  233. PG(php_sys_temp_dir) = estrndup(s, len);
  234. }
  235. return PG(php_sys_temp_dir);
  236. }
  237. }
  238. #ifdef P_tmpdir
  239. /* Use the standard default temporary directory. */
  240. if (P_tmpdir) {
  241. PG(php_sys_temp_dir) = estrdup(P_tmpdir);
  242. return PG(php_sys_temp_dir);
  243. }
  244. #endif
  245. /* Shouldn't ever(!) end up here ... last ditch default. */
  246. PG(php_sys_temp_dir) = estrdup("/tmp");
  247. return PG(php_sys_temp_dir);
  248. #endif
  249. }
  250. /* {{{ php_open_temporary_file
  251. *
  252. * Unlike tempnam(), the supplied dir argument takes precedence
  253. * over the TMPDIR environment variable
  254. * This function should do its best to return a file pointer to a newly created
  255. * unique file, on every platform.
  256. */
  257. PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_string **opened_path_p, uint32_t flags)
  258. {
  259. int fd;
  260. const char *temp_dir;
  261. if (!pfx) {
  262. pfx = "tmp.";
  263. }
  264. if (opened_path_p) {
  265. *opened_path_p = NULL;
  266. }
  267. if (!dir || *dir == '\0') {
  268. def_tmp:
  269. temp_dir = php_get_temporary_directory();
  270. if (temp_dir && *temp_dir != '\0' && (!(flags & PHP_TMP_FILE_OPEN_BASEDIR_CHECK) || !php_check_open_basedir(temp_dir))) {
  271. return php_do_open_temporary_file(temp_dir, pfx, opened_path_p);
  272. } else {
  273. return -1;
  274. }
  275. }
  276. /* Try the directory given as parameter. */
  277. fd = php_do_open_temporary_file(dir, pfx, opened_path_p);
  278. if (fd == -1) {
  279. /* Use default temporary directory. */
  280. if (!(flags & PHP_TMP_FILE_SILENT)) {
  281. php_error_docref(NULL, E_NOTICE, "file created in the system's temporary directory");
  282. }
  283. goto def_tmp;
  284. }
  285. return fd;
  286. }
  287. PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, zend_string **opened_path_p)
  288. {
  289. return php_open_temporary_fd_ex(dir, pfx, opened_path_p, 0);
  290. }
  291. PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_p)
  292. {
  293. FILE *fp;
  294. int fd = php_open_temporary_fd(dir, pfx, opened_path_p);
  295. if (fd == -1) {
  296. return NULL;
  297. }
  298. fp = fdopen(fd, "r+b");
  299. if (fp == NULL) {
  300. close(fd);
  301. }
  302. return fp;
  303. }
  304. /* }}} */
  305. /*
  306. * Local variables:
  307. * tab-width: 4
  308. * c-basic-offset: 4
  309. * End:
  310. * vim600: sw=4 ts=4 fdm=marker
  311. * vim<600: sw=4 ts=4
  312. */