1999-06-06 03:52:58 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| PHP Version 4 |
|
1999-06-06 03:52:58 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| Copyright (c) 1997-2002 The PHP Group |
|
1999-06-06 03:52:58 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-05-18 23:34:45 +08:00
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
1999-07-16 21:13:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
2000-05-18 23:34:45 +08:00
|
|
|
| http://www.php.net/license/2_02.txt. |
|
1999-07-16 21:13:16 +08: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-06 03:52:58 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-07-10 18:09:15 +08:00
|
|
|
| Authors: Sascha Schumann <sascha@schumann.cx> |
|
1999-06-06 03:52:58 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2001-06-06 22:32:27 +08:00
|
|
|
/* $Id$ */
|
|
|
|
|
1999-07-16 03:43:26 +08:00
|
|
|
#include "php.h"
|
|
|
|
|
1999-06-06 03:52:58 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
1999-07-04 07:43:02 +08:00
|
|
|
|
|
|
|
#if HAVE_SYS_FILE_H
|
1999-07-04 03:08:03 +08:00
|
|
|
#include <sys/file.h>
|
1999-07-04 07:43:02 +08:00
|
|
|
#endif
|
|
|
|
|
1999-09-04 16:20:43 +08:00
|
|
|
#if HAVE_DIRENT_H
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
1999-09-06 03:04:40 +08:00
|
|
|
#include "win32/readdir.h"
|
|
|
|
#endif
|
1999-09-12 01:20:31 +08:00
|
|
|
#include <time.h>
|
1999-09-06 03:04:40 +08:00
|
|
|
|
1999-06-06 03:52:58 +08:00
|
|
|
#include <fcntl.h>
|
1999-12-04 01:05:23 +08:00
|
|
|
#include <errno.h>
|
1999-06-06 03:52:58 +08:00
|
|
|
|
|
|
|
#include "php_session.h"
|
1999-06-08 00:43:24 +08:00
|
|
|
#include "mod_files.h"
|
|
|
|
#include "ext/standard/flock_compat.h"
|
1999-06-06 03:52:58 +08:00
|
|
|
|
1999-09-04 16:20:43 +08:00
|
|
|
#define FILE_PREFIX "sess_"
|
|
|
|
|
1999-06-06 03:52:58 +08:00
|
|
|
typedef struct {
|
|
|
|
int fd;
|
|
|
|
char *lastkey;
|
|
|
|
char *basedir;
|
2001-03-14 00:53:34 +08:00
|
|
|
size_t basedir_len;
|
2001-05-13 15:37:28 +08:00
|
|
|
size_t dirdepth;
|
2001-05-12 01:43:40 +08:00
|
|
|
size_t st_size;
|
1999-06-06 03:52:58 +08:00
|
|
|
} ps_files;
|
|
|
|
|
|
|
|
ps_module ps_mod_files = {
|
|
|
|
PS_MOD(files)
|
|
|
|
};
|
|
|
|
|
2000-09-06 22:16:12 +08:00
|
|
|
static int ps_files_valid_key(const char *key)
|
1999-10-21 20:28:36 +08:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
const char *p;
|
|
|
|
char c;
|
|
|
|
int ret = 1;
|
|
|
|
|
2000-02-11 21:41:30 +08:00
|
|
|
for (p = key; (c = *p); p++) {
|
1999-10-21 20:28:36 +08:00
|
|
|
/* valid characters are a..z,A..Z,0..9 */
|
2000-02-11 21:41:30 +08:00
|
|
|
if (!((c >= 'a' && c <= 'z') ||
|
1999-10-21 20:28:36 +08:00
|
|
|
(c >= 'A' && c <= 'Z') ||
|
1999-10-22 02:51:57 +08:00
|
|
|
(c >= '0' && c <= '9'))) {
|
1999-10-21 20:28:36 +08:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = p - key;
|
|
|
|
|
2000-02-11 21:41:30 +08:00
|
|
|
if (len == 0)
|
1999-10-21 20:28:36 +08:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-09-06 22:16:12 +08:00
|
|
|
static char *ps_files_path_create(char *buf, size_t buflen, ps_files *data, const char *key)
|
1999-06-06 03:52:58 +08:00
|
|
|
{
|
2001-03-14 00:53:34 +08:00
|
|
|
size_t key_len;
|
1999-06-08 03:51:54 +08:00
|
|
|
const char *p;
|
|
|
|
int i;
|
|
|
|
int n;
|
1999-09-04 16:33:31 +08:00
|
|
|
|
2001-03-14 00:53:34 +08: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 16:33:31 +08:00
|
|
|
return NULL;
|
|
|
|
p = key;
|
2001-03-14 00:53:34 +08:00
|
|
|
memcpy(buf, data->basedir, data->basedir_len);
|
|
|
|
n = data->basedir_len;
|
|
|
|
buf[n++] = PHP_DIR_SEPARATOR;
|
2001-06-11 07:26:35 +08:00
|
|
|
for (i = 0; i < (int)data->dirdepth; i++) {
|
1999-09-04 16:33:31 +08:00
|
|
|
buf[n++] = *p++;
|
2000-08-21 06:18:19 +08:00
|
|
|
buf[n++] = PHP_DIR_SEPARATOR;
|
1999-09-04 16:33:31 +08:00
|
|
|
}
|
2001-03-14 00:53:34 +08:00
|
|
|
memcpy(buf + n, FILE_PREFIX, sizeof(FILE_PREFIX) - 1);
|
|
|
|
n += sizeof(FILE_PREFIX) - 1;
|
|
|
|
memcpy(buf + n, key, key_len);
|
|
|
|
n += key_len;
|
1999-12-13 23:55:49 +08:00
|
|
|
buf[n] = '\0';
|
1999-09-04 16:33:31 +08:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2000-08-05 18:25:00 +08:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2000-10-16 18:35:43 +08:00
|
|
|
static void ps_files_close(ps_files *data)
|
|
|
|
{
|
|
|
|
if (data->fd != -1) {
|
|
|
|
close(data->fd);
|
|
|
|
data->fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-06 22:16:12 +08:00
|
|
|
static void ps_files_open(ps_files *data, const char *key)
|
1999-09-04 16:33:31 +08:00
|
|
|
{
|
|
|
|
char buf[MAXPATHLEN];
|
2001-08-05 09:43:02 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-06-08 03:51:54 +08:00
|
|
|
|
2000-02-11 21:41:30 +08:00
|
|
|
if (data->fd < 0 || !data->lastkey || strcmp(key, data->lastkey)) {
|
|
|
|
if (data->lastkey) {
|
1999-08-22 22:14:46 +08:00
|
|
|
efree(data->lastkey);
|
|
|
|
data->lastkey = NULL;
|
|
|
|
}
|
2000-10-16 18:35:43 +08:00
|
|
|
|
2000-10-17 10:50:15 +08:00
|
|
|
ps_files_close(data);
|
1999-09-04 16:33:31 +08:00
|
|
|
|
2000-09-06 22:16:12 +08:00
|
|
|
if (!ps_files_valid_key(key) ||
|
|
|
|
!ps_files_path_create(buf, sizeof(buf), data, key))
|
1999-06-08 03:51:54 +08:00
|
|
|
return;
|
1999-06-06 03:52:58 +08:00
|
|
|
|
|
|
|
data->lastkey = estrdup(key);
|
|
|
|
|
1999-08-22 21:43:23 +08:00
|
|
|
#ifdef O_EXCL
|
2001-08-05 09:43:02 +08:00
|
|
|
data->fd = VCWD_OPEN(buf, O_RDWR | O_BINARY);
|
2001-05-12 01:43:40 +08:00
|
|
|
|
|
|
|
if (data->fd == -1 && errno == ENOENT)
|
2001-08-05 09:43:02 +08:00
|
|
|
data->fd = VCWD_OPEN_MODE(buf, O_EXCL | O_RDWR | O_CREAT | O_BINARY, 0600);
|
1999-08-22 21:43:23 +08:00
|
|
|
#else
|
2001-08-05 09:43:02 +08:00
|
|
|
data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY, 0600);
|
1999-08-22 21:43:23 +08:00
|
|
|
#endif
|
2001-05-12 01:43:40 +08:00
|
|
|
if (data->fd != -1)
|
|
|
|
flock(data->fd, LOCK_EX);
|
2000-03-30 04:37:12 +08:00
|
|
|
|
|
|
|
if (data->fd == -1)
|
2001-05-12 01:43:40 +08:00
|
|
|
php_error(E_WARNING, "open(%s, O_RDWR) failed: %s (%d)", buf,
|
|
|
|
strerror(errno), errno);
|
1999-06-06 03:52:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-06 22:16:12 +08:00
|
|
|
static int ps_files_cleanup_dir(const char *dirname, int maxlifetime)
|
1999-09-04 16:33:31 +08:00
|
|
|
{
|
|
|
|
DIR *dir;
|
2000-10-18 22:14:14 +08:00
|
|
|
char dentry[sizeof(struct dirent) + MAXPATHLEN];
|
2000-09-11 23:24:28 +08:00
|
|
|
struct dirent *entry = (struct dirent *) &dentry;
|
1999-09-04 16:33:31 +08:00
|
|
|
struct stat sbuf;
|
|
|
|
char buf[MAXPATHLEN];
|
|
|
|
time_t now;
|
2000-03-30 04:37:12 +08:00
|
|
|
int nrdels = 0;
|
2001-03-14 00:53:34 +08:00
|
|
|
size_t dirname_len;
|
2001-08-05 09:43:02 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-09-04 16:33:31 +08:00
|
|
|
|
|
|
|
dir = opendir(dirname);
|
2000-03-30 04:37:12 +08:00
|
|
|
if (!dir) {
|
2001-05-12 01:43:40 +08:00
|
|
|
php_error(E_NOTICE, "ps_files_cleanup_dir: opendir(%s) failed: %s (%d)\n", dirname, strerror(errno), errno);
|
2000-03-30 04:37:12 +08:00
|
|
|
return (0);
|
|
|
|
}
|
1999-09-04 16:33:31 +08:00
|
|
|
|
|
|
|
time(&now);
|
|
|
|
|
2001-03-14 00:53:34 +08:00
|
|
|
dirname_len = strlen(dirname);
|
|
|
|
|
|
|
|
/* Prepare buffer (dirname never changes) */
|
|
|
|
memcpy(buf, dirname, dirname_len);
|
|
|
|
buf[dirname_len] = PHP_DIR_SEPARATOR;
|
|
|
|
|
2000-09-01 17:38:19 +08:00
|
|
|
while (php_readdir_r(dir, (struct dirent *) dentry, &entry) == 0 && entry) {
|
1999-09-04 16:33:31 +08:00
|
|
|
/* does the file start with our prefix? */
|
2001-03-14 00:53:34 +08:00
|
|
|
if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1)) {
|
|
|
|
size_t entry_len;
|
|
|
|
|
|
|
|
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';
|
|
|
|
/* check whether its last access was more than maxlifet ago */
|
2001-04-30 20:45:02 +08:00
|
|
|
if (VCWD_STAT(buf, &sbuf) == 0 &&
|
2001-03-14 00:53:34 +08:00
|
|
|
(now - sbuf.st_atime) > maxlifetime) {
|
2001-04-30 20:45:02 +08:00
|
|
|
VCWD_UNLINK(buf);
|
2001-03-14 00:53:34 +08:00
|
|
|
nrdels++;
|
|
|
|
}
|
|
|
|
}
|
1999-09-04 16:33:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
2000-03-30 04:37:12 +08:00
|
|
|
|
|
|
|
return (nrdels);
|
1999-09-04 16:33:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PS_FILES_DATA ps_files *data = PS_GET_MOD_DATA()
|
|
|
|
|
|
|
|
PS_OPEN_FUNC(files)
|
|
|
|
{
|
|
|
|
ps_files *data;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
data = ecalloc(sizeof(*data), 1);
|
|
|
|
PS_SET_MOD_DATA(data);
|
|
|
|
|
|
|
|
data->fd = -1;
|
2000-05-28 00:38:49 +08:00
|
|
|
if ((p = strchr(save_path, ';'))) {
|
2001-05-13 15:37:28 +08:00
|
|
|
data->dirdepth = (size_t) strtol(save_path, NULL, 10);
|
1999-09-04 16:33:31 +08:00
|
|
|
save_path = p + 1;
|
|
|
|
}
|
2001-03-14 00:53:34 +08:00
|
|
|
data->basedir_len = strlen(save_path);
|
|
|
|
data->basedir = estrndup(save_path, data->basedir_len);
|
1999-09-04 16:33:31 +08:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PS_CLOSE_FUNC(files)
|
|
|
|
{
|
|
|
|
PS_FILES_DATA;
|
|
|
|
|
2000-10-16 18:35:43 +08:00
|
|
|
ps_files_close(data);
|
|
|
|
|
2000-02-11 21:41:30 +08:00
|
|
|
if (data->lastkey)
|
|
|
|
efree(data->lastkey);
|
1999-09-04 16:33:31 +08:00
|
|
|
efree(data->basedir);
|
|
|
|
efree(data);
|
|
|
|
*mod_data = NULL;
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
1999-06-06 03:52:58 +08:00
|
|
|
PS_READ_FUNC(files)
|
|
|
|
{
|
2001-05-12 16:39:33 +08:00
|
|
|
long n;
|
1999-06-06 03:52:58 +08:00
|
|
|
struct stat sbuf;
|
|
|
|
PS_FILES_DATA;
|
|
|
|
|
2000-09-06 22:16:12 +08:00
|
|
|
ps_files_open(data, key);
|
2000-02-11 21:41:30 +08:00
|
|
|
if (data->fd < 0)
|
1999-06-06 03:52:58 +08:00
|
|
|
return FAILURE;
|
|
|
|
|
2000-02-11 21:41:30 +08:00
|
|
|
if (fstat(data->fd, &sbuf))
|
1999-06-06 03:52:58 +08:00
|
|
|
return FAILURE;
|
|
|
|
|
2001-05-12 01:43:40 +08:00
|
|
|
data->st_size = *vallen = sbuf.st_size;
|
1999-06-06 03:52:58 +08:00
|
|
|
*val = emalloc(sbuf.st_size);
|
|
|
|
|
2001-05-12 01:43:40 +08:00
|
|
|
#ifdef HAVE_PREAD
|
|
|
|
n = pread(data->fd, *val, sbuf.st_size, 0);
|
|
|
|
#else
|
|
|
|
lseek(data->fd, 0, SEEK_SET);
|
1999-06-06 03:52:58 +08:00
|
|
|
n = read(data->fd, *val, sbuf.st_size);
|
2001-05-12 01:43:40 +08:00
|
|
|
#endif
|
2000-02-11 21:41:30 +08:00
|
|
|
if (n != sbuf.st_size) {
|
1999-06-06 03:52:58 +08:00
|
|
|
efree(*val);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PS_WRITE_FUNC(files)
|
|
|
|
{
|
2001-05-12 16:39:33 +08:00
|
|
|
long n;
|
1999-06-06 03:52:58 +08:00
|
|
|
PS_FILES_DATA;
|
|
|
|
|
2000-09-06 22:16:12 +08:00
|
|
|
ps_files_open(data, key);
|
2000-02-11 21:41:30 +08:00
|
|
|
if (data->fd < 0)
|
1999-06-06 03:52:58 +08:00
|
|
|
return FAILURE;
|
|
|
|
|
2001-05-12 01:43:40 +08:00
|
|
|
/*
|
|
|
|
* truncate file, if the amount of new data is smaller than
|
|
|
|
* the existing data set.
|
|
|
|
*/
|
|
|
|
|
2001-06-11 07:26:35 +08:00
|
|
|
if (vallen < (int)data->st_size)
|
2001-05-12 01:43:40 +08:00
|
|
|
ftruncate(data->fd, 0);
|
|
|
|
|
|
|
|
#ifdef HAVE_PWRITE
|
|
|
|
n = pwrite(data->fd, val, vallen, 0);
|
|
|
|
#else
|
1999-06-06 03:52:58 +08:00
|
|
|
lseek(data->fd, 0, SEEK_SET);
|
2001-05-12 01:43:40 +08:00
|
|
|
n = write(data->fd, val, vallen);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (n != vallen) {
|
|
|
|
php_error(E_WARNING, "write failed: %s (%d)", strerror(errno), errno);
|
2000-05-15 21:50:39 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
1999-06-06 03:52:58 +08:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
1999-06-11 17:23:00 +08:00
|
|
|
PS_DESTROY_FUNC(files)
|
1999-06-06 03:52:58 +08:00
|
|
|
{
|
1999-06-07 03:53:59 +08:00
|
|
|
char buf[MAXPATHLEN];
|
1999-06-06 03:52:58 +08:00
|
|
|
PS_FILES_DATA;
|
2001-08-05 09:43:02 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-06-06 03:52:58 +08:00
|
|
|
|
2000-09-06 22:16:12 +08:00
|
|
|
if (!ps_files_path_create(buf, sizeof(buf), data, key))
|
1999-09-04 16:33:31 +08:00
|
|
|
return FAILURE;
|
|
|
|
|
2000-10-16 18:35:43 +08:00
|
|
|
ps_files_close(data);
|
|
|
|
|
2001-04-30 20:45:02 +08:00
|
|
|
if (VCWD_UNLINK(buf) == -1) {
|
2000-07-05 09:26:22 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
1999-06-06 03:52:58 +08:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PS_GC_FUNC(files)
|
|
|
|
{
|
1999-09-04 16:20:43 +08:00
|
|
|
PS_FILES_DATA;
|
|
|
|
|
|
|
|
/* 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) */
|
|
|
|
|
2000-02-11 21:41:30 +08:00
|
|
|
if (data->dirdepth == 0)
|
2000-09-06 22:16:12 +08:00
|
|
|
*nrdels = ps_files_cleanup_dir(data->basedir, maxlifetime);
|
1999-06-08 03:51:54 +08:00
|
|
|
|
1999-06-06 03:52:58 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-06 22:32:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
2001-06-06 22:32:27 +08:00
|
|
|
*/
|