php-src/ext/session/mod_files.c

353 lines
7.4 KiB
C
Raw Normal View History

1999-06-05 19:52:58 +00:00
/*
+----------------------------------------------------------------------+
1999-07-16 13:13:16 +00:00
| PHP version 4.0 |
1999-06-05 19:52:58 +00:00
+----------------------------------------------------------------------+
2001-02-26 06:11:02 +00:00
| Copyright (c) 1997-2001 The PHP Group |
1999-06-05 19:52:58 +00:00
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 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 at through the world-wide-web at |
| http://www.php.net/license/2_02.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
+----------------------------------------------------------------------+
2000-07-10 10:09:15 +00:00
| Authors: Sascha Schumann <sascha@schumann.cx> |
1999-06-05 19:52:58 +00:00
+----------------------------------------------------------------------+
*/
2001-06-06 14:32:27 +00:00
/* $Id$ */
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
#include "php_session.h"
#include "mod_files.h"
#include "ext/standard/flock_compat.h"
1999-06-05 19:52:58 +00:00
#define FILE_PREFIX "sess_"
1999-06-05 19:52:58 +00:00
typedef struct {
int fd;
char *lastkey;
char *basedir;
size_t basedir_len;
2001-05-13 07:37:28 +00:00
size_t dirdepth;
size_t st_size;
1999-06-05 19:52:58 +00:00
} ps_files;
ps_module ps_mod_files = {
PS_MOD(files)
};
static int ps_files_valid_key(const char *key)
{
size_t len;
const char *p;
char c;
int ret = 1;
2000-02-11 13:41:30 +00:00
for (p = key; (c = *p); p++) {
/* valid characters are a..z,A..Z,0..9 */
2000-02-11 13:41:30 +00:00
if (!((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
1999-10-21 18:51:57 +00:00
(c >= '0' && c <= '9'))) {
ret = 0;
break;
}
}
len = p - key;
2000-02-11 13:41:30 +00:00
if (len == 0)
ret = 0;
return ret;
}
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;
int n;
1999-09-04 08:33:31 +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;
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';
1999-09-04 08:33:31 +00:00
return buf;
}
#ifndef O_BINARY
#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) {
close(data->fd);
data->fd = -1;
}
}
static void ps_files_open(ps_files *data, const char *key)
1999-09-04 08:33:31 +00:00
{
char buf[MAXPATHLEN];
2001-08-05 01:43:02 +00:00
TSRMLS_FETCH();
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);
1999-09-04 08:33:31 +00:00
if (!ps_files_valid_key(key) ||
!ps_files_path_create(buf, sizeof(buf), data, key))
return;
1999-06-05 19:52:58 +00:00
data->lastkey = estrdup(key);
1999-08-22 13:43:23 +00:00
#ifdef O_EXCL
2001-08-05 01:43:02 +00:00
data->fd = VCWD_OPEN(buf, O_RDWR | O_BINARY);
if (data->fd == -1 && errno == ENOENT)
2001-08-05 01:43:02 +00:00
data->fd = VCWD_OPEN_MODE(buf, O_EXCL | O_RDWR | O_CREAT | O_BINARY, 0600);
1999-08-22 13:43:23 +00:00
#else
2001-08-05 01:43:02 +00:00
data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY, 0600);
1999-08-22 13:43:23 +00:00
#endif
if (data->fd != -1)
flock(data->fd, LOCK_EX);
2000-03-29 20:37:12 +00:00
if (data->fd == -1)
php_error(E_WARNING, "open(%s, O_RDWR) failed: %s (%d)", buf,
strerror(errno), errno);
1999-06-05 19:52:58 +00:00
}
}
static int ps_files_cleanup_dir(const char *dirname, int 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;
1999-09-04 08:33:31 +00:00
struct stat sbuf;
char buf[MAXPATHLEN];
time_t now;
2000-03-29 20:37:12 +00:00
int nrdels = 0;
size_t dirname_len;
2001-08-05 01:43:02 +00:00
TSRMLS_FETCH();
1999-09-04 08:33:31 +00:00
dir = opendir(dirname);
2000-03-29 20:37:12 +00:00
if (!dir) {
php_error(E_NOTICE, "ps_files_cleanup_dir: opendir(%s) failed: %s (%d)\n", 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;
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;
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 */
if (VCWD_STAT(buf, &sbuf) == 0 &&
(now - sbuf.st_atime) > 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
}
#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;
if ((p = strchr(save_path, ';'))) {
2001-05-13 07:37:28 +00:00
data->dirdepth = (size_t) strtol(save_path, NULL, 10);
1999-09-04 08:33:31 +00:00
save_path = p + 1;
}
data->basedir_len = strlen(save_path);
data->basedir = estrndup(save_path, data->basedir_len);
1999-09-04 08:33:31 +00:00
return SUCCESS;
}
PS_CLOSE_FUNC(files)
{
PS_FILES_DATA;
2000-10-16 10:35:43 +00:00
ps_files_close(data);
2000-02-11 13:41:30 +00:00
if (data->lastkey)
efree(data->lastkey);
1999-09-04 08:33:31 +00:00
efree(data->basedir);
efree(data);
*mod_data = NULL;
return SUCCESS;
}
1999-06-05 19:52:58 +00:00
PS_READ_FUNC(files)
{
long n;
1999-06-05 19:52:58 +00:00
struct stat sbuf;
PS_FILES_DATA;
ps_files_open(data, key);
2000-02-11 13:41:30 +00:00
if (data->fd < 0)
1999-06-05 19:52:58 +00:00
return FAILURE;
2000-02-11 13:41:30 +00:00
if (fstat(data->fd, &sbuf))
1999-06-05 19:52:58 +00:00
return FAILURE;
data->st_size = *vallen = sbuf.st_size;
1999-06-05 19:52:58 +00:00
*val = emalloc(sbuf.st_size);
#ifdef HAVE_PREAD
n = pread(data->fd, *val, sbuf.st_size, 0);
#else
lseek(data->fd, 0, SEEK_SET);
1999-06-05 19:52:58 +00:00
n = read(data->fd, *val, sbuf.st_size);
#endif
2000-02-11 13:41:30 +00:00
if (n != sbuf.st_size) {
1999-06-05 19:52:58 +00:00
efree(*val);
return FAILURE;
}
return SUCCESS;
}
PS_WRITE_FUNC(files)
{
long n;
1999-06-05 19:52:58 +00:00
PS_FILES_DATA;
ps_files_open(data, key);
2000-02-11 13:41:30 +00:00
if (data->fd < 0)
1999-06-05 19:52:58 +00:00
return FAILURE;
/*
* truncate file, if the amount of new data is smaller than
* the existing data set.
*/
2001-06-10 23:26:35 +00:00
if (vallen < (int)data->st_size)
ftruncate(data->fd, 0);
#ifdef HAVE_PWRITE
n = pwrite(data->fd, val, vallen, 0);
#else
1999-06-05 19:52:58 +00:00
lseek(data->fd, 0, SEEK_SET);
n = write(data->fd, val, vallen);
#endif
if (n != vallen) {
php_error(E_WARNING, "write failed: %s (%d)", strerror(errno), errno);
2000-05-15 13:50:39 +00:00
return FAILURE;
}
1999-06-05 19:52:58 +00:00
return SUCCESS;
}
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;
2001-08-05 01:43:02 +00:00
TSRMLS_FETCH();
1999-06-05 19:52:58 +00:00
if (!ps_files_path_create(buf, sizeof(buf), data, key))
1999-09-04 08:33:31 +00:00
return FAILURE;
2000-10-16 10:35:43 +00:00
ps_files_close(data);
if (VCWD_UNLINK(buf) == -1) {
return FAILURE;
}
1999-06-05 19:52:58 +00:00
return SUCCESS;
}
PS_GC_FUNC(files)
{
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 13:41:30 +00:00
if (data->dirdepth == 0)
*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
/*
* 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
*/