Files

275 lines
6.4 KiB
C

/*
+----------------------------------------------------------------------+
| Copyright © The PHP Group and Contributors. |
+----------------------------------------------------------------------+
| This source file is subject to the Modified BSD License that is |
| bundled with this package in the file LICENSE, and is available |
| through the World Wide Web at <https://www.php.net/license/>. |
| |
| SPDX-License-Identifier: BSD-3-Clause |
+----------------------------------------------------------------------+
| Author: Sascha Schumann <sascha@schumann.cx> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_session.h"
#include "mod_user.h"
#include "zend_exceptions.h"
const ps_module ps_mod_user = {
PS_MOD_UPDATE_TIMESTAMP(user)
};
static void ps_call_handler(zval *func, uint32_t argc, zval *argv, zval *retval)
{
if (PS(in_save_handler)) {
PS(in_save_handler) = false;
ZVAL_UNDEF(retval);
php_error_docref(NULL, E_WARNING, "Cannot call session save handler in a recursive manner");
} else {
PS(in_save_handler) = true;
if (call_user_function(NULL, NULL, func, retval, argc, argv) == FAILURE) {
zval_ptr_dtor(retval);
ZVAL_UNDEF(retval);
}
PS(in_save_handler) = false;
}
for (uint32_t i = 0; i < argc; i++) {
zval_ptr_dtor(&argv[i]);
}
}
#define PSF(a) PS(mod_user_names).ps_##a
static zend_result verify_bool_return_type_userland_calls(const zval *value)
{
switch (Z_TYPE_P(value)) {
case IS_TRUE:
return SUCCESS;
case IS_FALSE:
/* Exit or exception in userland call */
case IS_UNDEF:
return FAILURE;
case IS_LONG:
/* Deprecated cases */
if (Z_LVAL_P(value) == 0) {
php_error_docref(NULL, E_DEPRECATED, "Session callback must have a return value of type bool, %s returned", zend_zval_value_name(value));
return SUCCESS;
} else if (Z_LVAL_P(value) == -1) {
php_error_docref(NULL, E_DEPRECATED, "Session callback must have a return value of type bool, %s returned", zend_zval_value_name(value));
return FAILURE;
}
ZEND_FALLTHROUGH;
default:
zend_type_error("Session callback must have a return value of type bool, %s returned", zend_zval_value_name(value));
return FAILURE;
}
}
PS_OPEN_FUNC(user)
{
zval args[2];
zval retval;
ZEND_ASSERT(!Z_ISUNDEF(PSF(open)));
ZVAL_STR(&args[0], zend_string_dup(save_path, false));
ZVAL_STR(&args[1], zend_string_dup(session_name, false));
zend_try {
ps_call_handler(&PSF(open), 2, args, &retval);
} zend_catch {
PS(session_status) = php_session_none;
zval_ptr_dtor(&retval);
zend_bailout();
} zend_end_try();
PS(mod_user_implemented) = true;
zend_result ret = verify_bool_return_type_userland_calls(&retval);
zval_ptr_dtor(&retval);
return ret;
}
PS_CLOSE_FUNC(user)
{
zval retval;
zend_result ret = FAILURE;
ZEND_ASSERT(!Z_ISUNDEF(PSF(close)));
if (!PS(mod_user_implemented)) {
/* already closed */
return SUCCESS;
}
zend_try {
ps_call_handler(&PSF(close), 0, NULL, &retval);
} zend_catch {
PS(mod_user_implemented) = false;
zval_ptr_dtor(&retval);
zend_bailout();
} zend_end_try();
PS(mod_user_implemented) = false;
ret = verify_bool_return_type_userland_calls(&retval);
zval_ptr_dtor(&retval);
return ret;
}
PS_READ_FUNC(user)
{
zval args[1];
zval retval;
zend_result ret = FAILURE;
ZEND_ASSERT(!Z_ISUNDEF(PSF(read)));
ZVAL_STR_COPY(&args[0], key);
ps_call_handler(&PSF(read), 1, args, &retval);
if (!Z_ISUNDEF(retval)) {
if (Z_TYPE(retval) == IS_STRING) {
*val = zend_string_copy(Z_STR(retval));
ret = SUCCESS;
}
zval_ptr_dtor(&retval);
}
return ret;
}
PS_WRITE_FUNC(user)
{
zval args[2];
zval retval;
zend_result ret = FAILURE;
ZEND_ASSERT(!Z_ISUNDEF(PSF(write)));
ZVAL_STR_COPY(&args[0], key);
ZVAL_STR_COPY(&args[1], val);
ps_call_handler(&PSF(write), 2, args, &retval);
ret = verify_bool_return_type_userland_calls(&retval);
zval_ptr_dtor(&retval);
return ret;
}
PS_DESTROY_FUNC(user)
{
zval args[1];
zval retval;
zend_result ret = FAILURE;
ZEND_ASSERT(!Z_ISUNDEF(PSF(destroy)));
ZVAL_STR_COPY(&args[0], key);
ps_call_handler(&PSF(destroy), 1, args, &retval);
ret = verify_bool_return_type_userland_calls(&retval);
zval_ptr_dtor(&retval);
return ret;
}
PS_GC_FUNC(user)
{
zval args[1];
zval retval;
ZEND_ASSERT(!Z_ISUNDEF(PSF(gc)));
ZVAL_LONG(&args[0], maxlifetime);
ps_call_handler(&PSF(gc), 1, args, &retval);
if (Z_TYPE(retval) == IS_LONG) {
*nrdels = Z_LVAL(retval);
} else if (Z_TYPE(retval) == IS_TRUE) {
/* This is for older API compatibility */
*nrdels = 1;
} else {
/* Anything else is some kind of error */
*nrdels = -1; // Error
}
zval_ptr_dtor(&retval);
return *nrdels;
}
PS_CREATE_SID_FUNC(user)
{
/* maintain backwards compatibility */
if (!Z_ISUNDEF(PSF(create_sid))) {
zval retval;
ps_call_handler(&PSF(create_sid), 0, NULL, &retval);
/* Exception was thrown */
if (Z_ISUNDEF(retval)) {
return NULL;
}
if (UNEXPECTED(Z_TYPE(retval) != IS_STRING)) {
/* Will no longer be needed in PHP 9 as the interface return type will be in effect */
if (!EG(exception)) {
zend_throw_error(zend_ce_type_error, "Session id must be of type string, %s given", zend_zval_type_name(&retval));
}
zval_ptr_dtor(&retval);
return NULL;
}
ZEND_ASSERT(Z_TYPE(retval) == IS_STRING);
return Z_STR(retval);
}
/* function as defined by PS_MOD */
return php_session_create_id(mod_data);
}
PS_VALIDATE_SID_FUNC(user)
{
/* maintain backwards compatibility */
if (!Z_ISUNDEF(PSF(validate_sid))) {
zval args[1];
zval retval;
zend_result ret = FAILURE;
ZVAL_STR_COPY(&args[0], key);
ps_call_handler(&PSF(validate_sid), 1, args, &retval);
ret = verify_bool_return_type_userland_calls(&retval);
zval_ptr_dtor(&retval);
return ret;
}
/* dummy function defined by PS_MOD */
return php_session_validate_sid(mod_data, key);
}
PS_UPDATE_TIMESTAMP_FUNC(user)
{
zval args[2];
zval retval;
zend_result ret = FAILURE;
ZVAL_STR_COPY(&args[0], key);
ZVAL_STR_COPY(&args[1], val);
/* maintain backwards compatibility */
if (!Z_ISUNDEF(PSF(update_timestamp))) {
ps_call_handler(&PSF(update_timestamp), 2, args, &retval);
} else {
ps_call_handler(&PSF(write), 2, args, &retval);
}
ret = verify_bool_return_type_userland_calls(&retval);
zval_ptr_dtor(&retval);
return ret;
}