PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/session/mod_files.c

https://bitbucket.org/luobailiang/php-src
C | 464 lines | 331 code | 85 blank | 48 comment | 79 complexity | 3924e80327e30f742a688ed69b66aa23 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2012 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: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php.h"
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #if HAVE_SYS_FILE_H
  23. #include <sys/file.h>
  24. #endif
  25. #if HAVE_DIRENT_H
  26. #include <dirent.h>
  27. #endif
  28. #ifdef PHP_WIN32
  29. #include "win32/readdir.h"
  30. #endif
  31. #include <time.h>
  32. #include <fcntl.h>
  33. #include <errno.h>
  34. #if HAVE_UNISTD_H
  35. #include <unistd.h>
  36. #endif
  37. #include "php_session.h"
  38. #include "mod_files.h"
  39. #include "ext/standard/flock_compat.h"
  40. #include "php_open_temporary_file.h"
  41. #define FILE_PREFIX "sess_"
  42. typedef struct {
  43. int fd;
  44. char *lastkey;
  45. char *basedir;
  46. size_t basedir_len;
  47. size_t dirdepth;
  48. size_t st_size;
  49. int filemode;
  50. } ps_files;
  51. ps_module ps_mod_files = {
  52. PS_MOD(files)
  53. };
  54. /* If you change the logic here, please also update the error message in
  55. * ps_files_open() appropriately */
  56. static int ps_files_valid_key(const char *key)
  57. {
  58. size_t len;
  59. const char *p;
  60. char c;
  61. int ret = 1;
  62. for (p = key; (c = *p); p++) {
  63. /* valid characters are a..z,A..Z,0..9 */
  64. if (!((c >= 'a' && c <= 'z')
  65. || (c >= 'A' && c <= 'Z')
  66. || (c >= '0' && c <= '9')
  67. || c == ','
  68. || c == '-')) {
  69. ret = 0;
  70. break;
  71. }
  72. }
  73. len = p - key;
  74. /* Somewhat arbitrary length limit here, but should be way more than
  75. anyone needs and avoids file-level warnings later on if we exceed MAX_PATH */
  76. if (len == 0 || len > 128) {
  77. ret = 0;
  78. }
  79. return ret;
  80. }
  81. static char *ps_files_path_create(char *buf, size_t buflen, ps_files *data, const char *key)
  82. {
  83. size_t key_len;
  84. const char *p;
  85. int i;
  86. int n;
  87. key_len = strlen(key);
  88. if (key_len <= data->dirdepth ||
  89. buflen < (strlen(data->basedir) + 2 * data->dirdepth + key_len + 5 + sizeof(FILE_PREFIX))) {
  90. return NULL;
  91. }
  92. p = key;
  93. memcpy(buf, data->basedir, data->basedir_len);
  94. n = data->basedir_len;
  95. buf[n++] = PHP_DIR_SEPARATOR;
  96. for (i = 0; i < (int)data->dirdepth; i++) {
  97. buf[n++] = *p++;
  98. buf[n++] = PHP_DIR_SEPARATOR;
  99. }
  100. memcpy(buf + n, FILE_PREFIX, sizeof(FILE_PREFIX) - 1);
  101. n += sizeof(FILE_PREFIX) - 1;
  102. memcpy(buf + n, key, key_len);
  103. n += key_len;
  104. buf[n] = '\0';
  105. return buf;
  106. }
  107. #ifndef O_BINARY
  108. # define O_BINARY 0
  109. #endif
  110. static void ps_files_close(ps_files *data)
  111. {
  112. if (data->fd != -1) {
  113. #ifdef PHP_WIN32
  114. /* On Win32 locked files that are closed without being explicitly unlocked
  115. will be unlocked only when "system resources become available". */
  116. flock(data->fd, LOCK_UN);
  117. #endif
  118. close(data->fd);
  119. data->fd = -1;
  120. }
  121. }
  122. static void ps_files_open(ps_files *data, const char *key TSRMLS_DC)
  123. {
  124. char buf[MAXPATHLEN];
  125. if (data->fd < 0 || !data->lastkey || strcmp(key, data->lastkey)) {
  126. if (data->lastkey) {
  127. efree(data->lastkey);
  128. data->lastkey = NULL;
  129. }
  130. ps_files_close(data);
  131. if (!ps_files_valid_key(key)) {
  132. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,'");
  133. PS(invalid_session_id) = 1;
  134. return;
  135. }
  136. if (!ps_files_path_create(buf, sizeof(buf), data, key)) {
  137. return;
  138. }
  139. data->lastkey = estrdup(key);
  140. data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY, data->filemode);
  141. if (data->fd != -1) {
  142. #ifndef PHP_WIN32
  143. /* check to make sure that the opened file is not a symlink, linking to data outside of allowable dirs */
  144. if (PG(open_basedir)) {
  145. struct stat sbuf;
  146. if (fstat(data->fd, &sbuf)) {
  147. close(data->fd);
  148. return;
  149. }
  150. if (S_ISLNK(sbuf.st_mode) && php_check_open_basedir(buf TSRMLS_CC)) {
  151. close(data->fd);
  152. return;
  153. }
  154. }
  155. #endif
  156. flock(data->fd, LOCK_EX);
  157. #ifdef F_SETFD
  158. # ifndef FD_CLOEXEC
  159. # define FD_CLOEXEC 1
  160. # endif
  161. if (fcntl(data->fd, F_SETFD, FD_CLOEXEC)) {
  162. php_error_docref(NULL TSRMLS_CC, E_WARNING, "fcntl(%d, F_SETFD, FD_CLOEXEC) failed: %s (%d)", data->fd, strerror(errno), errno);
  163. }
  164. #endif
  165. } else {
  166. php_error_docref(NULL TSRMLS_CC, E_WARNING, "open(%s, O_RDWR) failed: %s (%d)", buf, strerror(errno), errno);
  167. }
  168. }
  169. }
  170. static int ps_files_cleanup_dir(const char *dirname, int maxlifetime TSRMLS_DC)
  171. {
  172. DIR *dir;
  173. char dentry[sizeof(struct dirent) + MAXPATHLEN];
  174. struct dirent *entry = (struct dirent *) &dentry;
  175. struct stat sbuf;
  176. char buf[MAXPATHLEN];
  177. time_t now;
  178. int nrdels = 0;
  179. size_t dirname_len;
  180. dir = opendir(dirname);
  181. if (!dir) {
  182. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ps_files_cleanup_dir: opendir(%s) failed: %s (%d)", dirname, strerror(errno), errno);
  183. return (0);
  184. }
  185. time(&now);
  186. dirname_len = strlen(dirname);
  187. /* Prepare buffer (dirname never changes) */
  188. memcpy(buf, dirname, dirname_len);
  189. buf[dirname_len] = PHP_DIR_SEPARATOR;
  190. while (php_readdir_r(dir, (struct dirent *) dentry, &entry) == 0 && entry) {
  191. /* does the file start with our prefix? */
  192. if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1)) {
  193. size_t entry_len = strlen(entry->d_name);
  194. /* does it fit into our buffer? */
  195. if (entry_len + dirname_len + 2 < MAXPATHLEN) {
  196. /* create the full path.. */
  197. memcpy(buf + dirname_len + 1, entry->d_name, entry_len);
  198. /* NUL terminate it and */
  199. buf[dirname_len + entry_len + 1] = '\0';
  200. /* check whether its last access was more than maxlifet ago */
  201. if (VCWD_STAT(buf, &sbuf) == 0 &&
  202. (now - sbuf.st_mtime) > maxlifetime) {
  203. VCWD_UNLINK(buf);
  204. nrdels++;
  205. }
  206. }
  207. }
  208. }
  209. closedir(dir);
  210. return (nrdels);
  211. }
  212. #define PS_FILES_DATA ps_files *data = PS_GET_MOD_DATA()
  213. PS_OPEN_FUNC(files)
  214. {
  215. ps_files *data;
  216. const char *p, *last;
  217. const char *argv[3];
  218. int argc = 0;
  219. size_t dirdepth = 0;
  220. int filemode = 0600;
  221. if (*save_path == '\0') {
  222. /* if save path is an empty string, determine the temporary dir */
  223. save_path = php_get_temporary_directory();
  224. if (php_check_open_basedir(save_path TSRMLS_CC)) {
  225. return FAILURE;
  226. }
  227. }
  228. /* split up input parameter */
  229. last = save_path;
  230. p = strchr(save_path, ';');
  231. while (p) {
  232. argv[argc++] = last;
  233. last = ++p;
  234. p = strchr(p, ';');
  235. if (argc > 1) break;
  236. }
  237. argv[argc++] = last;
  238. if (argc > 1) {
  239. errno = 0;
  240. dirdepth = (size_t) strtol(argv[0], NULL, 10);
  241. if (errno == ERANGE) {
  242. php_error(E_WARNING, "The first parameter in session.save_path is invalid");
  243. return FAILURE;
  244. }
  245. }
  246. if (argc > 2) {
  247. errno = 0;
  248. filemode = strtol(argv[1], NULL, 8);
  249. if (errno == ERANGE || filemode < 0 || filemode > 07777) {
  250. php_error(E_WARNING, "The second parameter in session.save_path is invalid");
  251. return FAILURE;
  252. }
  253. }
  254. save_path = argv[argc - 1];
  255. data = ecalloc(1, sizeof(*data));
  256. data->fd = -1;
  257. data->dirdepth = dirdepth;
  258. data->filemode = filemode;
  259. data->basedir_len = strlen(save_path);
  260. data->basedir = estrndup(save_path, data->basedir_len);
  261. if (PS_GET_MOD_DATA()) {
  262. ps_close_files(mod_data TSRMLS_CC);
  263. }
  264. PS_SET_MOD_DATA(data);
  265. return SUCCESS;
  266. }
  267. PS_CLOSE_FUNC(files)
  268. {
  269. PS_FILES_DATA;
  270. ps_files_close(data);
  271. if (data->lastkey) {
  272. efree(data->lastkey);
  273. }
  274. efree(data->basedir);
  275. efree(data);
  276. *mod_data = NULL;
  277. return SUCCESS;
  278. }
  279. PS_READ_FUNC(files)
  280. {
  281. long n;
  282. struct stat sbuf;
  283. PS_FILES_DATA;
  284. ps_files_open(data, key TSRMLS_CC);
  285. if (data->fd < 0) {
  286. return FAILURE;
  287. }
  288. if (fstat(data->fd, &sbuf)) {
  289. return FAILURE;
  290. }
  291. data->st_size = *vallen = sbuf.st_size;
  292. if (sbuf.st_size == 0) {
  293. *val = STR_EMPTY_ALLOC();
  294. return SUCCESS;
  295. }
  296. *val = emalloc(sbuf.st_size);
  297. #if defined(HAVE_PREAD)
  298. n = pread(data->fd, *val, sbuf.st_size, 0);
  299. #else
  300. lseek(data->fd, 0, SEEK_SET);
  301. n = read(data->fd, *val, sbuf.st_size);
  302. #endif
  303. if (n != sbuf.st_size) {
  304. if (n == -1) {
  305. php_error_docref(NULL TSRMLS_CC, E_WARNING, "read failed: %s (%d)", strerror(errno), errno);
  306. } else {
  307. php_error_docref(NULL TSRMLS_CC, E_WARNING, "read returned less bytes than requested");
  308. }
  309. efree(*val);
  310. return FAILURE;
  311. }
  312. return SUCCESS;
  313. }
  314. PS_WRITE_FUNC(files)
  315. {
  316. long n;
  317. PS_FILES_DATA;
  318. ps_files_open(data, key TSRMLS_CC);
  319. if (data->fd < 0) {
  320. return FAILURE;
  321. }
  322. /* Truncate file if the amount of new data is smaller than the existing data set. */
  323. if (vallen < (int)data->st_size) {
  324. php_ignore_value(ftruncate(data->fd, 0));
  325. }
  326. #if defined(HAVE_PWRITE)
  327. n = pwrite(data->fd, val, vallen, 0);
  328. #else
  329. lseek(data->fd, 0, SEEK_SET);
  330. n = write(data->fd, val, vallen);
  331. #endif
  332. if (n != vallen) {
  333. if (n == -1) {
  334. php_error_docref(NULL TSRMLS_CC, E_WARNING, "write failed: %s (%d)", strerror(errno), errno);
  335. } else {
  336. php_error_docref(NULL TSRMLS_CC, E_WARNING, "write wrote less bytes than requested");
  337. }
  338. return FAILURE;
  339. }
  340. return SUCCESS;
  341. }
  342. PS_DESTROY_FUNC(files)
  343. {
  344. char buf[MAXPATHLEN];
  345. PS_FILES_DATA;
  346. if (!ps_files_path_create(buf, sizeof(buf), data, key)) {
  347. return FAILURE;
  348. }
  349. if (data->fd != -1) {
  350. ps_files_close(data);
  351. if (VCWD_UNLINK(buf) == -1) {
  352. /* This is a little safety check for instances when we are dealing with a regenerated session
  353. * that was not yet written to disk. */
  354. if (!VCWD_ACCESS(buf, F_OK)) {
  355. return FAILURE;
  356. }
  357. }
  358. }
  359. return SUCCESS;
  360. }
  361. PS_GC_FUNC(files)
  362. {
  363. PS_FILES_DATA;
  364. /* we don't perform any cleanup, if dirdepth is larger than 0.
  365. we return SUCCESS, since all cleanup should be handled by
  366. an external entity (i.e. find -ctime x | xargs rm) */
  367. if (data->dirdepth == 0) {
  368. *nrdels = ps_files_cleanup_dir(data->basedir, maxlifetime TSRMLS_CC);
  369. }
  370. return SUCCESS;
  371. }
  372. /*
  373. * Local variables:
  374. * tab-width: 4
  375. * c-basic-offset: 4
  376. * End:
  377. * vim600: sw=4 ts=4 fdm=marker
  378. * vim<600: sw=4 ts=4
  379. */