php-src/ext/session/mod_files.c

716 lines
19 KiB
C
Raw Normal View History

2009-05-18 16:10:09 +00:00
/*
1999-06-05 19:52:58 +00:00
+----------------------------------------------------------------------+
2014-09-19 16:33:14 +00:00
| PHP Version 7 |
1999-06-05 19:52:58 +00:00
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
1999-06-05 19:52:58 +00:00
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 12:51:34 +00:00
| http://www.php.net/license/3_01.txt |
1999-07-16 13:13:16 +00: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-06-05 19:52:58 +00:00
+----------------------------------------------------------------------+
2002-02-28 08:29:35 +00:00
| Author: Sascha Schumann <sascha@schumann.cx> |
1999-06-05 19:52:58 +00:00
+----------------------------------------------------------------------+
*/
2015-01-21 10:13:59 +00:00
/**************************************************************************
* Files save handler should be used as reference implementations of session
* save handlers. PS_* functions are called as follows with standard usage.
*
* PS_OPEN_FUNC() - Create module data that manages save handler.
* PS_CREATE_SID() and/or PS_VALIDATE_SID()
* - PS_CREATE_ID() is called if session ID(key) is not
* provided or invalid. PS_VALIDATE_SID() is called to
* verify session ID already exists or not to mitigate
2015-02-03 20:00:03 +00:00
* session adoption vulnerability risk.
2015-01-21 10:13:59 +00:00
* PS_READ_FUNC() - Read data from storage.
* PS_GC_FUNC() - Perform GC. Called by probability
* (session.gc_probability/session.gc_divisor).
* PS_WRITE_FUNC() or PS_UPDATE_TIMESTAMP()
* - Write session data or update session data timestamp.
* It depends on session data change.
* PS_CLOSE_FUNC() - Clean up module data created by PS_OPEN_FUNC().
*
2015-02-03 20:00:03 +00:00
* Session module guarantees PS_OPEN_FUNC() is called before calling other
2015-01-21 10:13:59 +00:00
* PS_*_FUNC() functions. Other than this, session module may call any
* PS_*_FUNC() at any time. You may assume non null *mod_data created by
* PS_OPEN_FUNC() is passed to PS_*_FUNC().
*
* NOTE:
* - Save handlers _MUST_NOT_ change/refer PS() values.
* i.e. PS(id), PS(session_status), PS(mod) and any other PS() values.
* Use only function parameters passed from session module.
* - Save handler _MUST_ use PS_GET_MOD_DATA()/PS_SET_MOD_DATA() macro to
* set/get save handler module data(mod_data). mod_data contains
* data required by PS modules. It will not be NULL except PS_OPEN_FUNC().
2015-02-03 20:00:03 +00:00
* - Refer to PS_* macros in php_session.h for function/parameter definitions.
2015-01-21 10:13:59 +00:00
* - Returning FAILURE state from PS_* function results in raising errors.
* Avoid failure state as much as possible.
* - Use static ps_[module name]_[function name] functions for internal use.
*************************************************************************/
2001-06-06 14:32:27 +00:00
1999-07-15 19:43:26 +00:00
#include "php.h"
1999-06-05 19:52:58 +00:00
#include <sys/stat.h>
#include <sys/types.h>
1999-07-03 23:43:02 +00:00
#if HAVE_SYS_FILE_H
1999-07-03 19:08:03 +00:00
#include <sys/file.h>
1999-07-03 23:43:02 +00:00
#endif
#if HAVE_DIRENT_H
#include <dirent.h>
#endif
2000-02-11 15:59:30 +00:00
#ifdef PHP_WIN32
#include "win32/readdir.h"
#endif
#include <time.h>
1999-06-05 19:52:58 +00:00
#include <fcntl.h>
#include <errno.h>
1999-06-05 19:52:58 +00:00
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
1999-06-05 19:52:58 +00:00
#include "php_session.h"
#include "mod_files.h"
#include "ext/standard/flock_compat.h"
#include "php_open_temporary_file.h"
1999-06-05 19:52:58 +00:00
#define FILE_PREFIX "sess_"
2014-04-14 21:29:38 +00:00
#ifdef PHP_WIN32
# ifndef O_NOFOLLOW
# define O_NOFOLLOW 0
# endif
#endif
1999-06-05 19:52:58 +00:00
typedef struct {
char *lastkey;
char *basedir;
size_t basedir_len;
2001-05-13 07:37:28 +00:00
size_t dirdepth;
size_t st_size;
int filemode;
2014-09-13 21:34:08 +00:00
int fd;
1999-06-05 19:52:58 +00:00
} ps_files;
ps_module ps_mod_files = {
2015-01-21 10:13:59 +00:00
/* New save handlers MUST use PS_MOD_UPDATE_TIMESTAMP macro */
PS_MOD_UPDATE_TIMESTAMP(files)
1999-06-05 19:52:58 +00:00
};
static char *ps_files_path_create(char *buf, size_t buflen, ps_files *data, const char *key)
1999-06-05 19:52:58 +00:00
{
size_t key_len;
const char *p;
int i;
2015-03-24 21:01:40 +00:00
size_t n;
2009-05-18 16:10:09 +00:00
key_len = strlen(key);
if (key_len <= data->dirdepth ||
buflen < (strlen(data->basedir) + 2 * data->dirdepth + key_len + 5 + sizeof(FILE_PREFIX))) {
1999-09-04 08:33:31 +00:00
return NULL;
}
1999-09-04 08:33:31 +00:00
p = key;
memcpy(buf, data->basedir, data->basedir_len);
n = data->basedir_len;
buf[n++] = PHP_DIR_SEPARATOR;
2001-06-10 23:26:35 +00:00
for (i = 0; i < (int)data->dirdepth; i++) {
1999-09-04 08:33:31 +00:00
buf[n++] = *p++;
2000-08-20 22:18:19 +00:00
buf[n++] = PHP_DIR_SEPARATOR;
1999-09-04 08:33:31 +00:00
}
memcpy(buf + n, FILE_PREFIX, sizeof(FILE_PREFIX) - 1);
n += sizeof(FILE_PREFIX) - 1;
memcpy(buf + n, key, key_len);
n += key_len;
buf[n] = '\0';
2009-05-18 16:10:09 +00:00
1999-09-04 08:33:31 +00:00
return buf;
}
#ifndef O_BINARY
2009-05-18 16:10:09 +00:00
# define O_BINARY 0
#endif
2000-10-16 10:35:43 +00:00
static void ps_files_close(ps_files *data)
{
if (data->fd != -1) {
2009-05-18 16:10:09 +00:00
#ifdef PHP_WIN32
2003-09-25 14:53:41 +00:00
/* On Win32 locked files that are closed without being explicitly unlocked
will be unlocked only when "system resources become available". */
flock(data->fd, LOCK_UN);
2003-09-25 14:53:41 +00:00
#endif
2000-10-16 10:35:43 +00:00
close(data->fd);
data->fd = -1;
}
}
2014-12-13 22:06:14 +00:00
static void ps_files_open(ps_files *data, const char *key)
1999-09-04 08:33:31 +00:00
{
char buf[MAXPATHLEN];
#if !defined(O_NOFOLLOW) || !defined(PHP_WIN32)
2014-08-29 15:21:02 +00:00
struct stat sbuf;
2014-05-17 03:50:01 +00:00
#endif
int ret;
2000-02-11 13:41:30 +00:00
if (data->fd < 0 || !data->lastkey || strcmp(key, data->lastkey)) {
if (data->lastkey) {
efree(data->lastkey);
data->lastkey = NULL;
}
2000-10-16 10:35:43 +00:00
2000-10-17 02:50:15 +00:00
ps_files_close(data);
2013-06-25 10:47:04 +00:00
if (php_session_valid_key(key) == FAILURE) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,'");
return;
}
2013-06-25 10:47:04 +00:00
if (!ps_files_path_create(buf, sizeof(buf), data, key)) {
return;
}
1999-06-05 19:52:58 +00:00
data->lastkey = estrdup(key);
2014-04-14 20:16:53 +00:00
/* O_NOFOLLOW to prevent us from following evil symlinks */
#ifdef O_NOFOLLOW
2014-04-14 20:16:53 +00:00
data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY | O_NOFOLLOW, data->filemode);
#else
2015-01-03 09:22:58 +00:00
/* Check to make sure that the opened file is not outside of allowable dirs.
2014-04-14 20:16:53 +00:00
This is not 100% safe but it's hard to do something better without O_NOFOLLOW */
2014-12-13 22:06:14 +00:00
if(PG(open_basedir) && lstat(buf, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) && php_check_open_basedir(buf)) {
2014-04-14 20:16:53 +00:00
return;
}
data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY, data->filemode);
#endif
if (data->fd != -1) {
#ifndef PHP_WIN32
2014-04-14 20:16:53 +00:00
/* check that this session file was created by us or root we
don't want to end up accepting the sessions of another webapp */
if (fstat(data->fd, &sbuf) || (sbuf.st_uid != 0 && sbuf.st_uid != getuid() && sbuf.st_uid != geteuid())) {
close(data->fd);
data->fd = -1;
return;
}
#endif
do {
ret = flock(data->fd, LOCK_EX);
} while (ret == -1 && errno == EINTR);
2002-04-23 19:58:31 +00:00
#ifdef F_SETFD
# ifndef FD_CLOEXEC
# define FD_CLOEXEC 1
# endif
if (fcntl(data->fd, F_SETFD, FD_CLOEXEC)) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "fcntl(%d, F_SETFD, FD_CLOEXEC) failed: %s (%d)", data->fd, strerror(errno), errno);
}
2002-04-23 19:58:31 +00:00
#endif
} else {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "open(%s, O_RDWR) failed: %s (%d)", buf, strerror(errno), errno);
}
1999-06-05 19:52:58 +00:00
}
}
2015-01-21 10:13:59 +00:00
static int ps_files_write(ps_files *data, zend_string *key, zend_string *val)
{
2015-03-24 21:01:40 +00:00
zend_long n = 0;
2015-01-21 10:13:59 +00:00
/* PS(id) may be changed by calling session_regenerate_id().
Re-initialization should be tried here. ps_files_open() checks
data->lastkey and reopen when it is needed. */
ps_files_open(data, ZSTR_VAL(key));
2015-01-21 10:13:59 +00:00
if (data->fd < 0) {
return FAILURE;
}
/* Truncate file if the amount of new data is smaller than the existing data set. */
if (ZSTR_LEN(val) < data->st_size) {
2015-01-21 10:13:59 +00:00
php_ignore_value(ftruncate(data->fd, 0));
}
#if defined(HAVE_PWRITE)
n = pwrite(data->fd, ZSTR_VAL(val), ZSTR_LEN(val), 0);
2015-01-21 10:13:59 +00:00
#else
lseek(data->fd, 0, SEEK_SET);
2015-03-24 21:01:40 +00:00
#ifdef PHP_WIN32
{
unsigned int to_write = ZSTR_LEN(val) > UINT_MAX ? UINT_MAX : (unsigned int)ZSTR_LEN(val);
char *buf = ZSTR_VAL(val);
2015-03-24 21:01:40 +00:00
int wrote;
do {
wrote = _write(data->fd, buf, to_write);
n += wrote;
buf = wrote > -1 ? buf + wrote : 0;
to_write = wrote > -1 ? (ZSTR_LEN(val) - n > UINT_MAX ? UINT_MAX : (unsigned int)(ZSTR_LEN(val) - n)): 0;
2015-03-24 21:01:40 +00:00
} while(wrote > 0);
}
#else
n = write(data->fd, ZSTR_VAL(val), ZSTR_LEN(val));
2015-03-24 21:01:40 +00:00
#endif
2015-01-21 10:13:59 +00:00
#endif
if (n != ZSTR_LEN(val)) {
2015-01-21 10:13:59 +00:00
if (n == -1) {
php_error_docref(NULL, E_WARNING, "write failed: %s (%d)", strerror(errno), errno);
} else {
php_error_docref(NULL, E_WARNING, "write wrote less bytes than requested");
}
return FAILURE;
}
return SUCCESS;
}
2015-03-24 21:01:40 +00:00
static int ps_files_cleanup_dir(const char *dirname, zend_long maxlifetime)
1999-09-04 08:33:31 +00:00
{
DIR *dir;
char dentry[sizeof(struct dirent) + MAXPATHLEN];
2000-09-11 15:24:28 +00:00
struct dirent *entry = (struct dirent *) &dentry;
2014-08-25 18:22:49 +00:00
zend_stat_t sbuf;
1999-09-04 08:33:31 +00:00
char buf[MAXPATHLEN];
time_t now;
2000-03-29 20:37:12 +00:00
int nrdels = 0;
size_t dirname_len;
1999-09-04 08:33:31 +00:00
dir = opendir(dirname);
2000-03-29 20:37:12 +00:00
if (!dir) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_NOTICE, "ps_files_cleanup_dir: opendir(%s) failed: %s (%d)", dirname, strerror(errno), errno);
2000-03-29 20:37:12 +00:00
return (0);
}
1999-09-04 08:33:31 +00:00
time(&now);
dirname_len = strlen(dirname);
/* Prepare buffer (dirname never changes) */
memcpy(buf, dirname, dirname_len);
buf[dirname_len] = PHP_DIR_SEPARATOR;
2009-05-18 16:10:09 +00:00
while (php_readdir_r(dir, (struct dirent *) dentry, &entry) == 0 && entry) {
1999-09-04 08:33:31 +00:00
/* does the file start with our prefix? */
if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1)) {
size_t entry_len = strlen(entry->d_name);
/* does it fit into our buffer? */
if (entry_len + dirname_len + 2 < MAXPATHLEN) {
/* create the full path.. */
memcpy(buf + dirname_len + 1, entry->d_name, entry_len);
/* NUL terminate it and */
buf[dirname_len + entry_len + 1] = '\0';
2015-01-31 20:14:27 +00:00
/* check whether its last access was more than maxlifetime ago */
2009-05-18 16:10:09 +00:00
if (VCWD_STAT(buf, &sbuf) == 0 &&
(now - sbuf.st_mtime) > maxlifetime) {
VCWD_UNLINK(buf);
nrdels++;
}
}
1999-09-04 08:33:31 +00:00
}
}
closedir(dir);
2000-03-29 20:37:12 +00:00
return (nrdels);
1999-09-04 08:33:31 +00:00
}
2014-12-13 22:06:14 +00:00
static int ps_files_key_exists(ps_files *data, const char *key)
2013-06-25 10:47:04 +00:00
{
char buf[MAXPATHLEN];
2014-08-25 18:22:49 +00:00
zend_stat_t sbuf;
2013-06-25 10:47:04 +00:00
if (!key || !ps_files_path_create(buf, sizeof(buf), data, key)) {
return FAILURE;
}
if (VCWD_STAT(buf, &sbuf)) {
return FAILURE;
}
return SUCCESS;
}
1999-09-04 08:33:31 +00:00
#define PS_FILES_DATA ps_files *data = PS_GET_MOD_DATA()
2015-01-21 10:13:59 +00:00
/*
* Open save handler. Setup resources that are needed by the handler.
* PARAMETERS: PS_OPEN_ARGS in php_session.h
* RETURN VALUE: SUCCESS or FAILURE. Must set non-NULL valid module data
* (void **mod_data) with SUCCESS, NULL(default) for FAILUREs.
*
* Files save handler checks/create save_path directory and setup ps_files data.
* Note that files save handler supports splitting session data into multiple
* directories.
2015-02-03 20:00:03 +00:00
* *mod_data, *save_path, *session_name are guaranteed to have non-NULL values.
2015-01-21 10:13:59 +00:00
*/
1999-09-04 08:33:31 +00:00
PS_OPEN_FUNC(files)
{
ps_files *data;
const char *p, *last;
const char *argv[3];
int argc = 0;
size_t dirdepth = 0;
int filemode = 0600;
if (*save_path == '\0') {
/* if save path is an empty string, determine the temporary dir */
2014-12-13 22:06:14 +00:00
save_path = php_get_temporary_directory();
2007-01-04 23:49:35 +00:00
2014-12-13 22:06:14 +00:00
if (php_check_open_basedir(save_path)) {
2008-08-31 14:49:58 +00:00
return FAILURE;
2007-01-04 23:49:35 +00:00
}
}
2009-05-18 16:10:09 +00:00
/* split up input parameter */
last = save_path;
p = strchr(save_path, ';');
while (p) {
argv[argc++] = last;
last = ++p;
p = strchr(p, ';');
if (argc > 1) break;
}
argv[argc++] = last;
1999-09-04 08:33:31 +00:00
if (argc > 1) {
2003-01-12 13:05:32 +00:00
errno = 0;
2014-08-25 18:22:49 +00:00
dirdepth = (size_t) ZEND_STRTOL(argv[0], NULL, 10);
2003-01-12 13:05:32 +00:00
if (errno == ERANGE) {
php_error(E_WARNING, "The first parameter in session.save_path is invalid");
return FAILURE;
}
}
2009-05-18 16:10:09 +00:00
if (argc > 2) {
errno = 0;
2015-03-24 20:24:49 +00:00
filemode = (int)ZEND_STRTOL(argv[1], NULL, 8);
if (errno == ERANGE || filemode < 0 || filemode > 07777) {
php_error(E_WARNING, "The second parameter in session.save_path is invalid");
2003-01-12 13:05:32 +00:00
return FAILURE;
}
1999-09-04 08:33:31 +00:00
}
save_path = argv[argc - 1];
2009-05-18 16:10:09 +00:00
data = ecalloc(1, sizeof(*data));
data->fd = -1;
data->dirdepth = dirdepth;
data->filemode = filemode;
data->basedir_len = strlen(save_path);
data->basedir = estrndup(save_path, data->basedir_len);
2009-05-18 16:10:09 +00:00
if (PS_GET_MOD_DATA()) {
2014-12-13 22:06:14 +00:00
ps_close_files(mod_data);
}
PS_SET_MOD_DATA(data);
2009-05-18 16:10:09 +00:00
1999-09-04 08:33:31 +00:00
return SUCCESS;
}
2015-01-21 10:13:59 +00:00
/*
* Clean up opened resources.
* PARAMETERS: PS_CLOSE_ARGS in php_session.h
* RETURN VALUE: SUCCESS. Must set PS module data(void **mod_data) to NULL.
*
* Files save handler closes open files and it's memory.
2015-02-03 20:00:03 +00:00
* *mod_data is guaranteed to have non-NULL value.
2015-01-21 10:13:59 +00:00
* PS_CLOSE_FUNC() must set *mod_data to NULL. PS_CLOSE_FUNC() should not
* fail.
*/
1999-09-04 08:33:31 +00:00
PS_CLOSE_FUNC(files)
{
PS_FILES_DATA;
2000-10-16 10:35:43 +00:00
ps_files_close(data);
if (data->lastkey) {
2000-02-11 13:41:30 +00:00
efree(data->lastkey);
2015-04-24 07:04:01 +00:00
data->lastkey = NULL;
}
1999-09-04 08:33:31 +00:00
efree(data->basedir);
efree(data);
2015-11-07 15:43:18 +00:00
PS_SET_MOD_DATA(NULL);
1999-09-04 08:33:31 +00:00
return SUCCESS;
}
2015-01-21 10:13:59 +00:00
/*
* Read session data from opened resource.
* PARAMETERS: PS_READ_ARGS in php_session.h
* RETURN VALUE: SUCCESS or FAILURE. Must set non-NULL session data to (zend_string **val)
* for SUCCESS. NULL(default) for FAILUREs.
*
* Files save handler supports splitting session data into multiple
* directories.
2015-02-03 20:00:03 +00:00
* *mod_data, *key are guaranteed to have non-NULL values.
2015-01-21 10:13:59 +00:00
*/
1999-06-05 19:52:58 +00:00
PS_READ_FUNC(files)
{
2015-03-24 21:01:40 +00:00
zend_long n = 0;
2014-08-29 10:53:37 +00:00
zend_stat_t sbuf;
1999-06-05 19:52:58 +00:00
PS_FILES_DATA;
ps_files_open(data, ZSTR_VAL(key));
if (data->fd < 0) {
1999-06-05 19:52:58 +00:00
return FAILURE;
}
2014-08-29 11:43:40 +00:00
if (zend_fstat(data->fd, &sbuf)) {
1999-06-05 19:52:58 +00:00
return FAILURE;
}
2014-03-28 10:46:25 +00:00
data->st_size = sbuf.st_size;
if (sbuf.st_size == 0) {
*val = ZSTR_EMPTY_ALLOC();
return SUCCESS;
}
2014-08-25 17:24:55 +00:00
*val = zend_string_alloc(sbuf.st_size, 0);
1999-06-05 19:52:58 +00:00
#if defined(HAVE_PREAD)
n = pread(data->fd, ZSTR_VAL(*val), ZSTR_LEN(*val), 0);
#else
lseek(data->fd, 0, SEEK_SET);
2015-03-24 21:01:40 +00:00
#ifdef PHP_WIN32
{
unsigned int to_read = ZSTR_LEN(*val) > UINT_MAX ? UINT_MAX : (unsigned int)ZSTR_LEN(*val);
char *buf = ZSTR_VAL(*val);
2015-03-24 21:01:40 +00:00
int read_in;
do {
read_in = _read(data->fd, buf, to_read);
n += read_in;
buf = read_in > -1 ? buf + read_in : 0;
to_read = read_in > -1 ? (ZSTR_LEN(*val) - n > UINT_MAX ? UINT_MAX : (unsigned int)(ZSTR_LEN(*val) - n)): 0;
2015-03-24 21:01:40 +00:00
} while(read_in > 0);
}
#else
n = read(data->fd, ZSTR_VAL(*val), ZSTR_LEN(*val));
2015-03-24 21:01:40 +00:00
#endif
#endif
2015-03-24 21:01:40 +00:00
if (n != (zend_long)sbuf.st_size) {
if (n == -1) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "read failed: %s (%d)", strerror(errno), errno);
} else {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "read returned less bytes than requested");
}
2014-08-25 17:24:55 +00:00
zend_string_release(*val);
*val = ZSTR_EMPTY_ALLOC();
1999-06-05 19:52:58 +00:00
return FAILURE;
}
2009-05-18 16:10:09 +00:00
ZSTR_VAL(*val)[ZSTR_LEN(*val)] = '\0';
1999-06-05 19:52:58 +00:00
return SUCCESS;
}
2015-01-21 10:13:59 +00:00
/*
* Write session data.
* PARAMETERS: PS_WRITE_ARGS in php_session.h
* RETURN VALUE: SUCCESS or FAILURE.
*
* PS_WRITE_FUNC() must write session data(zend_string *val) unconditionally.
2015-02-03 20:00:03 +00:00
* *mod_data, *key, *val are guaranteed to have non-NULL values.
2015-01-21 10:13:59 +00:00
*/
1999-06-05 19:52:58 +00:00
PS_WRITE_FUNC(files)
{
PS_FILES_DATA;
2015-01-21 10:13:59 +00:00
return ps_files_write(data, key, val);
}
1999-06-05 19:52:58 +00:00
2009-05-18 16:10:09 +00:00
2015-01-21 10:13:59 +00:00
/*
* Update session data modification/access time stamp.
* PARAMETERS: PS_UPDATE_TIMESTAMP_ARGS in php_session.h
* RETURN VALUE: SUCCESS or FAILURE.
*
* PS_UPDATE_TIMESTAMP_FUNC() updates time stamp(mtime) so that active session
* data files will not be purged by GC. If session data storage does not need to
* update timestamp, it should return SUCCESS simply. (e.g. Memcache)
2015-02-03 20:00:03 +00:00
* *mod_data, *key, *val are guaranteed to have non-NULL values.
2015-01-21 10:13:59 +00:00
*
* NOTE: Updating access timestamp at PS_READ_FUNC() may extend life of obsolete
2015-02-03 20:00:03 +00:00
* session data. Use of PS_UPDATE_TIMESTAMP_FUNC() is preferred whenever it is
2015-01-21 10:13:59 +00:00
* possible.
*/
PS_UPDATE_TIMESTAMP_FUNC(files)
{
char buf[MAXPATHLEN];
struct utimbuf newtimebuf;
struct utimbuf *newtime = &newtimebuf;
int ret;
PS_FILES_DATA;
if (!ps_files_path_create(buf, sizeof(buf), data, ZSTR_VAL(key))) {
2015-01-21 10:13:59 +00:00
return FAILURE;
}
2015-01-21 10:13:59 +00:00
/* Update mtime */
#ifdef HAVE_UTIME_NULL
newtime = NULL;
#else
2015-01-21 10:13:59 +00:00
newtime->modtime = newtime->actime = time(NULL);
#endif
2015-01-21 10:13:59 +00:00
ret = VCWD_UTIME(buf, newtime);
if (ret == -1) {
/* New session ID, create data file */
return ps_files_write(data, key, val);
2000-05-15 13:50:39 +00:00
}
1999-06-05 19:52:58 +00:00
return SUCCESS;
}
2015-01-21 10:13:59 +00:00
/*
* Delete session data.
* PARAMETERS: PS_DESTROY_ARGS in php_session.h
* RETURN VALUE: SUCCESS or FAILURE.
*
* PS_DESTROY_FUNC() must remove the session data specified by *key from
* session data storage unconditionally. It must not return FAILURE for
* non-existent session data.
2015-02-03 20:00:03 +00:00
* *mod_data, *key are guaranteed to have non-NULL values.
2015-01-21 10:13:59 +00:00
*/
PS_DESTROY_FUNC(files)
1999-06-05 19:52:58 +00:00
{
char buf[MAXPATHLEN];
1999-06-05 19:52:58 +00:00
PS_FILES_DATA;
if (!ps_files_path_create(buf, sizeof(buf), data, ZSTR_VAL(key))) {
1999-09-04 08:33:31 +00:00
return FAILURE;
}
2006-04-18 00:31:45 +00:00
if (data->fd != -1) {
ps_files_close(data);
2009-05-18 16:10:09 +00:00
2006-04-18 00:31:45 +00:00
if (VCWD_UNLINK(buf) == -1) {
/* This is a little safety check for instances when we are dealing with a regenerated session
2009-05-18 16:10:09 +00:00
* that was not yet written to disk. */
if (!VCWD_ACCESS(buf, F_OK)) {
return FAILURE;
}
2006-04-18 00:31:45 +00:00
}
}
1999-06-05 19:52:58 +00:00
return SUCCESS;
}
2015-01-21 10:13:59 +00:00
/*
* Cleanup expired session data.
* PARAMETERS: PS_GC_ARGS in php_session.h
* RETURN VALUE: SUCCESS or FAILURE. Number of deleted records(int *nrdels(default=-1)).
*
* PS_GC_FUNC() must remove session data that are not accessed
* 'session.maxlifetime'(seconds). If storage does not need manual GC, it
* may return SUCCESS simply. (e.g. Memcache) It must set number of records
* deleted(nrdels).
2015-02-03 20:00:03 +00:00
* *mod_data is guaranteed to have non-NULL value.
2015-01-21 10:13:59 +00:00
*/
2009-05-18 16:10:09 +00:00
PS_GC_FUNC(files)
1999-06-05 19:52:58 +00:00
{
PS_FILES_DATA;
2009-05-18 16:10:09 +00:00
2015-01-21 10:13:59 +00:00
/* We don't perform any cleanup, if dirdepth is larger than 0.
we return SUCCESS, since all cleanup should be handled by
an external entity (i.e. find -ctime x | xargs rm) */
2009-05-18 16:10:09 +00:00
if (data->dirdepth == 0) {
2014-12-13 22:06:14 +00:00
*nrdels = ps_files_cleanup_dir(data->basedir, maxlifetime);
}
1999-06-05 19:52:58 +00:00
return SUCCESS;
}
2001-06-06 14:32:27 +00:00
2015-01-21 10:13:59 +00:00
/*
* Create session ID.
* PARAMETERS: PS_CREATE_SID_ARGS in php_session.h
* RETURN VALUE: Valid session ID(zend_string *) or NULL for FAILURE.
*
* PS_CREATE_SID_FUNC() must check collision. i.e. Check session data if
* new sid exists already.
2015-02-03 20:00:03 +00:00
* *mod_data is guaranteed to have non-NULL value.
2015-01-21 10:13:59 +00:00
* NOTE: Default php_session_create_id() does not check collision. If
* NULL is returned, session module create new ID by using php_session_create_id().
* If php_session_create_id() fails due to invalid configuration, it raises E_ERROR.
* NULL return value checks from php_session_create_id() is not required generally.
*/
2013-06-25 10:47:04 +00:00
PS_CREATE_SID_FUNC(files)
{
2014-03-28 10:46:25 +00:00
zend_string *sid;
int maxfail = 3;
2013-06-25 10:47:04 +00:00
PS_FILES_DATA;
do {
2014-12-13 22:06:14 +00:00
sid = php_session_create_id((void**)&data);
2015-01-21 10:13:59 +00:00
if (!sid) {
if (--maxfail < 0) {
return NULL;
} else {
continue;
}
}
/* Check collision */
2015-01-21 10:13:59 +00:00
/* FIXME: mod_data(data) should not be NULL (User handler could be NULL) */
if (data && ps_files_key_exists(data, ZSTR_VAL(sid)) == SUCCESS) {
if (sid) {
2014-08-25 17:24:55 +00:00
zend_string_release(sid);
sid = NULL;
}
2015-01-21 10:13:59 +00:00
if (--maxfail < 0) {
return NULL;
}
}
} while(!sid);
2013-06-25 10:47:04 +00:00
return sid;
}
2015-01-21 10:13:59 +00:00
/*
* Check session ID existence for use_strict_mode support.
* PARAMETERS: PS_VALIDATE_SID_ARGS in php_session.h
* RETURN VALUE: SUCCESS or FAILURE.
*
2015-02-03 20:00:03 +00:00
* Return SUCCESS for valid key(already existing session).
2015-01-21 10:13:59 +00:00
* Return FAILURE for invalid key(non-existing session).
2015-02-03 20:00:03 +00:00
* *mod_data, *key are guaranteed to have non-NULL values.
2015-01-21 10:13:59 +00:00
*/
PS_VALIDATE_SID_FUNC(files)
{
PS_FILES_DATA;
return ps_files_key_exists(data, ZSTR_VAL(key));
2015-01-21 10:13:59 +00:00
}
2001-06-06 14:32:27 +00:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
2001-06-06 14:32:27 +00:00
*/