php-src/ext/session/mod_files.c

296 lines
6.1 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
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999, 2000 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
+----------------------------------------------------------------------+
*/
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;
int dirdepth;
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;
}
1999-09-04 08:33:31 +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
{
1999-09-04 08:33:31 +00:00
int keylen;
const char *p;
int i;
int n;
1999-09-04 08:33:31 +00:00
keylen = strlen(key);
2000-02-11 13:41:30 +00:00
if (keylen <= data->dirdepth || buflen <
1999-09-04 08:33:31 +00:00
(strlen(data->basedir) + 2 * data->dirdepth + keylen + 5 + sizeof(FILE_PREFIX)))
return NULL;
p = key;
n = sprintf(buf, "%s%c", data->basedir, PHP_SEPARATOR);
2000-02-11 13:41:30 +00:00
for (i = 0; i < data->dirdepth; i++) {
1999-09-04 08:33:31 +00:00
buf[n++] = *p++;
buf[n++] = PHP_SEPARATOR;
1999-09-04 08:33:31 +00:00
}
buf[n] = '\0';
1999-09-04 08:33:31 +00:00
strcat(buf, FILE_PREFIX);
strcat(buf, key);
1999-09-04 08:33:31 +00:00
return buf;
}
#ifndef O_BINARY
#define O_BINARY 0
#endif
1999-09-04 08:33:31 +00:00
static void _ps_files_open(ps_files *data, const char *key)
{
char buf[MAXPATHLEN];
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-02-11 13:41:30 +00:00
if (data->fd != -1) {
close(data->fd);
data->fd = -1;
}
1999-09-04 08:33:31 +00:00
2000-02-11 13:41:30 +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
data->fd = V_OPEN((buf, O_RDWR | O_BINARY));
if (data->fd == -1) {
if (errno == ENOENT) {
data->fd = V_OPEN((buf, O_EXCL | O_RDWR | O_CREAT | O_BINARY, 0600));
}
} else {
flock(data->fd, LOCK_EX);
1999-08-22 13:43:23 +00:00
}
#else
data->fd = V_OPEN((buf, O_CREAT | O_RDWR | O_BINARY, 0600));
2000-02-11 13:41:30 +00:00
if (data->fd != -1)
1999-06-05 19:52:58 +00:00
flock(data->fd, LOCK_EX);
1999-08-22 13:43:23 +00:00
#endif
2000-03-29 20:37:12 +00:00
if (data->fd == -1)
php_error(E_WARNING, "open(%s, O_RDWR) failed: %m (%d)", buf, errno);
1999-06-05 19:52:58 +00:00
}
}
2000-03-29 20:37:12 +00:00
static int _ps_files_cleanup_dir(const char *dirname, int maxlifetime)
1999-09-04 08:33:31 +00:00
{
DIR *dir;
struct dirent *entry, 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;
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: %m (%d)\n", dirname, errno);
return (0);
}
1999-09-04 08:33:31 +00:00
time(&now);
while (php_readdir_r(dir, &dentry, &entry) == 0 && entry) {
1999-09-04 08:33:31 +00:00
/* does the file start with our prefix? */
2000-02-11 13:41:30 +00:00
if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1) &&
1999-09-04 08:33:31 +00:00
/* create full path */
snprintf(buf, MAXPATHLEN, "%s%c%s", dirname, PHP_SEPARATOR,
1999-09-04 08:33:31 +00:00
entry->d_name) > 0 &&
/* stat the directory entry */
V_STAT(buf, &sbuf) == 0 &&
1999-09-04 08:33:31 +00:00
/* is it expired? */
(now - sbuf.st_atime) > maxlifetime) {
2000-05-23 14:36:27 +00:00
V_UNLINK(buf);
2000-03-29 20:37:12 +00:00
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, ';'))) {
1999-09-04 08:33:31 +00:00
data->dirdepth = strtol(save_path, NULL, 10);
save_path = p + 1;
}
data->basedir = estrdup(save_path);
return SUCCESS;
}
PS_CLOSE_FUNC(files)
{
PS_FILES_DATA;
2000-02-11 13:41:30 +00:00
if (data->fd > -1)
close(data->fd);
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)
{
int n;
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;
lseek(data->fd, 0, SEEK_SET);
*vallen = sbuf.st_size;
*val = emalloc(sbuf.st_size);
n = read(data->fd, *val, sbuf.st_size);
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)
{
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;
ftruncate(data->fd, 0);
lseek(data->fd, 0, SEEK_SET);
2000-05-15 13:50:39 +00:00
if (write(data->fd, val, vallen) != vallen) {
php_error(E_WARNING, "write failed: %m (%d)", errno);
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;
2000-02-11 13:41:30 +00:00
if (!_ps_files_path_create(buf, sizeof(buf), data, key))
1999-09-04 08:33:31 +00:00
return FAILURE;
if (V_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)
2000-03-29 20:37:12 +00:00
*nrdels = _ps_files_cleanup_dir(data->basedir, maxlifetime);
1999-06-05 19:52:58 +00:00
return SUCCESS;
}