php_open_temporary_file.c 9.4 KB

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