mod_files.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. #ifdef PHP_WIN32
  43. # ifndef O_NOFOLLOW
  44. # define O_NOFOLLOW 0
  45. # endif
  46. #endif
  47. typedef struct {
  48. int fd;
  49. char *lastkey;
  50. char *basedir;
  51. size_t basedir_len;
  52. size_t dirdepth;
  53. size_t st_size;
  54. int filemode;
  55. } ps_files;
  56. ps_module ps_mod_files = {
  57. PS_MOD_SID(files)
  58. };
  59. static char *ps_files_path_create(char *buf, size_t buflen, ps_files *data, const char *key)
  60. {
  61. size_t key_len;
  62. const char *p;
  63. int i;
  64. int n;
  65. key_len = strlen(key);
  66. if (!data || key_len <= data->dirdepth ||
  67. buflen < (strlen(data->basedir) + 2 * data->dirdepth + key_len + 5 + sizeof(FILE_PREFIX))) {
  68. return NULL;
  69. }
  70. p = key;
  71. memcpy(buf, data->basedir, data->basedir_len);
  72. n = data->basedir_len;
  73. buf[n++] = PHP_DIR_SEPARATOR;
  74. for (i = 0; i < (int)data->dirdepth; i++) {
  75. buf[n++] = *p++;
  76. buf[n++] = PHP_DIR_SEPARATOR;
  77. }
  78. memcpy(buf + n, FILE_PREFIX, sizeof(FILE_PREFIX) - 1);
  79. n += sizeof(FILE_PREFIX) - 1;
  80. memcpy(buf + n, key, key_len);
  81. n += key_len;
  82. buf[n] = '\0';
  83. return buf;
  84. }
  85. #ifndef O_BINARY
  86. # define O_BINARY 0
  87. #endif
  88. static void ps_files_close(ps_files *data)
  89. {
  90. if (data->fd != -1) {
  91. #ifdef PHP_WIN32
  92. /* On Win32 locked files that are closed without being explicitly unlocked
  93. will be unlocked only when "system resources become available". */
  94. flock(data->fd, LOCK_UN);
  95. #endif
  96. close(data->fd);
  97. data->fd = -1;
  98. }
  99. }
  100. static void ps_files_open(ps_files *data, const char *key TSRMLS_DC)
  101. {
  102. char buf[MAXPATHLEN];
  103. struct stat sbuf;
  104. int ret;
  105. if (data->fd < 0 || !data->lastkey || strcmp(key, data->lastkey)) {
  106. if (data->lastkey) {
  107. efree(data->lastkey);
  108. data->lastkey = NULL;
  109. }
  110. ps_files_close(data);
  111. if (php_session_valid_key(key) == FAILURE) {
  112. 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 '-,'");
  113. return;
  114. }
  115. if (!ps_files_path_create(buf, sizeof(buf), data, key)) {
  116. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create session data file path. Too short session ID, invalid save_path or path lentgth exceeds MAXPATHLEN(%d)", MAXPATHLEN);
  117. return;
  118. }
  119. data->lastkey = estrdup(key);
  120. /* O_NOFOLLOW to prevent us from following evil symlinks */
  121. #ifdef O_NOFOLLOW
  122. data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY | O_NOFOLLOW, data->filemode);
  123. #else
  124. /* Check to make sure that the opened file is not outside of allowable dirs.
  125. This is not 100% safe but it's hard to do something better without O_NOFOLLOW */
  126. if(PG(open_basedir) && lstat(buf, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) && php_check_open_basedir(buf TSRMLS_CC)) {
  127. return;
  128. }
  129. data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY, data->filemode);
  130. #endif
  131. if (data->fd != -1) {
  132. #ifndef PHP_WIN32
  133. /* check that this session file was created by us or root – we
  134. don't want to end up accepting the sessions of another webapp */
  135. if (fstat(data->fd, &sbuf) || (sbuf.st_uid != 0 && sbuf.st_uid != getuid() && sbuf.st_uid != geteuid())) {
  136. close(data->fd);
  137. data->fd = -1;
  138. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session data file is not created by your uid");
  139. return;
  140. }
  141. #endif
  142. do {
  143. ret = flock(data->fd, LOCK_EX);
  144. } while (ret == -1 && errno == EINTR);
  145. #ifdef F_SETFD
  146. # ifndef FD_CLOEXEC
  147. # define FD_CLOEXEC 1
  148. # endif
  149. if (fcntl(data->fd, F_SETFD, FD_CLOEXEC)) {
  150. php_error_docref(NULL TSRMLS_CC, E_WARNING, "fcntl(%d, F_SETFD, FD_CLOEXEC) failed: %s (%d)", data->fd, strerror(errno), errno);
  151. }
  152. #endif
  153. } else {
  154. php_error_docref(NULL TSRMLS_CC, E_WARNING, "open(%s, O_RDWR) failed: %s (%d)", buf, strerror(errno), errno);
  155. }
  156. }
  157. }
  158. static int ps_files_cleanup_dir(const char *dirname, int maxlifetime TSRMLS_DC)
  159. {
  160. DIR *dir;
  161. char dentry[sizeof(struct dirent) + MAXPATHLEN];
  162. struct dirent *entry = (struct dirent *) &dentry;
  163. struct stat sbuf;
  164. char buf[MAXPATHLEN];
  165. time_t now;
  166. int nrdels = 0;
  167. size_t dirname_len;
  168. dir = opendir(dirname);
  169. if (!dir) {
  170. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ps_files_cleanup_dir: opendir(%s) failed: %s (%d)", dirname, strerror(errno), errno);
  171. return (0);
  172. }
  173. time(&now);
  174. dirname_len = strlen(dirname);
  175. /* Prepare buffer (dirname never changes) */
  176. memcpy(buf, dirname, dirname_len);
  177. buf[dirname_len] = PHP_DIR_SEPARATOR;
  178. while (php_readdir_r(dir, (struct dirent *) dentry, &entry) == 0 && entry) {
  179. /* does the file start with our prefix? */
  180. if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1)) {
  181. size_t entry_len = strlen(entry->d_name);
  182. /* does it fit into our buffer? */
  183. if (entry_len + dirname_len + 2 < MAXPATHLEN) {
  184. /* create the full path.. */
  185. memcpy(buf + dirname_len + 1, entry->d_name, entry_len);
  186. /* NUL terminate it and */
  187. buf[dirname_len + entry_len + 1] = '\0';
  188. /* check whether its last access was more than maxlifetime ago */
  189. if (VCWD_STAT(buf, &sbuf) == 0 &&
  190. (now - sbuf.st_mtime) > maxlifetime) {
  191. VCWD_UNLINK(buf);
  192. nrdels++;
  193. }
  194. }
  195. }
  196. }
  197. closedir(dir);
  198. return (nrdels);
  199. }
  200. static int ps_files_key_exists(ps_files *data, const char *key TSRMLS_DC)
  201. {
  202. char buf[MAXPATHLEN];
  203. struct stat sbuf;
  204. if (!key || !ps_files_path_create(buf, sizeof(buf), data, key)) {
  205. return FAILURE;
  206. }
  207. if (VCWD_STAT(buf, &sbuf)) {
  208. return FAILURE;
  209. }
  210. return SUCCESS;
  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(TSRMLS_C);
  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. data->lastkey = NULL;
  274. }
  275. efree(data->basedir);
  276. efree(data);
  277. *mod_data = NULL;
  278. return SUCCESS;
  279. }
  280. PS_READ_FUNC(files)
  281. {
  282. long n;
  283. struct stat sbuf;
  284. PS_FILES_DATA;
  285. /* If strict mode, check session id existence */
  286. if (PS(use_strict_mode) &&
  287. ps_files_key_exists(data, key TSRMLS_CC) == FAILURE) {
  288. /* key points to PS(id), but cannot change here. */
  289. if (key) {
  290. efree(PS(id));
  291. PS(id) = NULL;
  292. }
  293. PS(id) = PS(mod)->s_create_sid((void **)&data, NULL TSRMLS_CC);
  294. if (!PS(id)) {
  295. return FAILURE;
  296. }
  297. if (PS(use_cookies)) {
  298. PS(send_cookie) = 1;
  299. }
  300. php_session_reset_id(TSRMLS_C);
  301. PS(session_status) = php_session_active;
  302. }
  303. if (!PS(id)) {
  304. return FAILURE;
  305. }
  306. ps_files_open(data, PS(id) TSRMLS_CC);
  307. if (data->fd < 0) {
  308. return FAILURE;
  309. }
  310. if (fstat(data->fd, &sbuf)) {
  311. return FAILURE;
  312. }
  313. data->st_size = *vallen = sbuf.st_size;
  314. if (sbuf.st_size == 0) {
  315. *val = STR_EMPTY_ALLOC();
  316. return SUCCESS;
  317. }
  318. *val = emalloc(sbuf.st_size);
  319. #if defined(HAVE_PREAD)
  320. n = pread(data->fd, *val, sbuf.st_size, 0);
  321. #else
  322. lseek(data->fd, 0, SEEK_SET);
  323. n = read(data->fd, *val, sbuf.st_size);
  324. #endif
  325. if (n != sbuf.st_size) {
  326. if (n == -1) {
  327. php_error_docref(NULL TSRMLS_CC, E_WARNING, "read failed: %s (%d)", strerror(errno), errno);
  328. } else {
  329. php_error_docref(NULL TSRMLS_CC, E_WARNING, "read returned less bytes than requested");
  330. }
  331. efree(*val);
  332. return FAILURE;
  333. }
  334. return SUCCESS;
  335. }
  336. PS_WRITE_FUNC(files)
  337. {
  338. long n;
  339. PS_FILES_DATA;
  340. ps_files_open(data, key TSRMLS_CC);
  341. if (data->fd < 0) {
  342. return FAILURE;
  343. }
  344. /* Truncate file if the amount of new data is smaller than the existing data set. */
  345. if (vallen < (int)data->st_size) {
  346. php_ignore_value(ftruncate(data->fd, 0));
  347. }
  348. #if defined(HAVE_PWRITE)
  349. n = pwrite(data->fd, val, vallen, 0);
  350. #else
  351. lseek(data->fd, 0, SEEK_SET);
  352. n = write(data->fd, val, vallen);
  353. #endif
  354. if (n != vallen) {
  355. if (n == -1) {
  356. php_error_docref(NULL TSRMLS_CC, E_WARNING, "write failed: %s (%d)", strerror(errno), errno);
  357. } else {
  358. php_error_docref(NULL TSRMLS_CC, E_WARNING, "write wrote less bytes than requested");
  359. }
  360. return FAILURE;
  361. }
  362. return SUCCESS;
  363. }
  364. PS_DESTROY_FUNC(files)
  365. {
  366. char buf[MAXPATHLEN];
  367. PS_FILES_DATA;
  368. if (!ps_files_path_create(buf, sizeof(buf), data, key)) {
  369. return FAILURE;
  370. }
  371. if (data->fd != -1) {
  372. ps_files_close(data);
  373. if (VCWD_UNLINK(buf) == -1) {
  374. /* This is a little safety check for instances when we are dealing with a regenerated session
  375. * that was not yet written to disk. */
  376. if (!VCWD_ACCESS(buf, F_OK)) {
  377. return FAILURE;
  378. }
  379. }
  380. }
  381. return SUCCESS;
  382. }
  383. PS_GC_FUNC(files)
  384. {
  385. PS_FILES_DATA;
  386. /* we don't perform any cleanup, if dirdepth is larger than 0.
  387. we return SUCCESS, since all cleanup should be handled by
  388. an external entity (i.e. find -ctime x | xargs rm) */
  389. if (data->dirdepth == 0) {
  390. *nrdels = ps_files_cleanup_dir(data->basedir, maxlifetime TSRMLS_CC);
  391. }
  392. return SUCCESS;
  393. }
  394. PS_CREATE_SID_FUNC(files)
  395. {
  396. char *sid;
  397. int maxfail = 3;
  398. PS_FILES_DATA;
  399. do {
  400. sid = php_session_create_id((void **)&data, newlen TSRMLS_CC);
  401. /* Check collision */
  402. if (data && ps_files_key_exists(data, sid TSRMLS_CC) == SUCCESS) {
  403. if (sid) {
  404. efree(sid);
  405. sid = NULL;
  406. }
  407. if (!(maxfail--)) {
  408. return NULL;
  409. }
  410. }
  411. } while(!sid);
  412. return sid;
  413. }
  414. /*
  415. * Local variables:
  416. * tab-width: 4
  417. * c-basic-offset: 4
  418. * End:
  419. * vim600: sw=4 ts=4 fdm=marker
  420. * vim<600: sw=4 ts=4
  421. */