Changeset 34 for trunk


Ignore:
Timestamp:
2006-05-27T08:53:32+02:00 (9 years ago)
Author:
moo
Message:

XCache Administration web frontend

Location:
trunk
Files:
7 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/xcache.c

    r32 r34  
    1212#include "php.h"
    1313#include "ext/standard/info.h"
     14#include "ext/standard/md5.h"
    1415#include "zend_extensions.h"
    1516#include "SAPI.h"
     
    10481049
    10491050/* user functions */
    1050 /* {{{ xcache_op */
     1051static int xcache_admin_auth_check(TSRMLS_C) /* {{{ */
     1052{
     1053    zval **server = NULL;
     1054    zval **user = NULL;
     1055    zval **pass = NULL;
     1056    char *admin_user = NULL;
     1057    char *admin_pass = NULL;
     1058    HashTable *ht;
     1059
     1060    if (cfg_get_string("xcache.admin.user", &admin_user) == FAILURE || !admin_user[0]) {
     1061        admin_user = NULL;
     1062    }
     1063    if (cfg_get_string("xcache.admin.pass", &admin_pass) == FAILURE || !admin_pass[0]) {
     1064        admin_pass = NULL;
     1065    }
     1066
     1067    if (admin_user == NULL || admin_pass == NULL) {
     1068        php_error_docref(NULL TSRMLS_CC, E_ERROR, "xcache.admin.user and xcache.admin.pass is required");
     1069        zend_bailout();
     1070    }
     1071    if (strlen(admin_pass) != 32) {
     1072        php_error_docref(NULL TSRMLS_CC, E_ERROR, "unexpect %d bytes of xcache.admin.pass, expected 32 bytes, the password after md5()", strlen(admin_pass));
     1073        zend_bailout();
     1074    }
     1075
     1076    if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &server) != SUCCESS || Z_TYPE_PP(server) != IS_ARRAY) {
     1077        php_error_docref(NULL TSRMLS_CC, E_ERROR, "_SERVER is corrupted");
     1078        zend_bailout();
     1079    }
     1080    ht = HASH_OF((*server));
     1081
     1082    if (zend_hash_find(ht, "PHP_AUTH_USER", sizeof("PHP_AUTH_USER"), (void **) &user) == FAILURE) {
     1083        user = NULL;
     1084    }
     1085    else if (Z_TYPE_PP(user) != IS_STRING) {
     1086        user = NULL;
     1087    }
     1088
     1089    if (zend_hash_find(ht, "PHP_AUTH_PW", sizeof("PHP_AUTH_PW"), (void **) &pass) == FAILURE) {
     1090        pass = NULL;
     1091    }
     1092    else if (Z_TYPE_PP(pass) != IS_STRING) {
     1093        pass = NULL;
     1094    }
     1095
     1096    if (user != NULL && pass != NULL && strcmp(admin_user, Z_STRVAL_PP(user)) == 0) {
     1097        PHP_MD5_CTX context;
     1098        char md5str[33];
     1099        unsigned char digest[16];
     1100
     1101        PHP_MD5Init(&context);
     1102        PHP_MD5Update(&context, Z_STRVAL_PP(pass), Z_STRLEN_PP(pass));
     1103        PHP_MD5Final(digest, &context);
     1104
     1105        md5str[0] = '\0';
     1106        make_digest(md5str, digest);
     1107        if (strcmp(admin_pass, md5str) == 0) {
     1108            return 1;
     1109        }
     1110    }
     1111
     1112#define STR "WWW-authenticate: basic realm='XCache Administration'"
     1113    sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC);
     1114#undef STR
     1115#define STR "HTTP/1.0 401 Unauthorized"
     1116    sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC);
     1117#undef STR
     1118    ZEND_PUTS("XCache Auth Failed. User and Password is case sense\n");
     1119
     1120    zend_bailout();
     1121    return 0;
     1122}
     1123/* }}} */
     1124/* {{{ xcache_admin_operate */
    10511125typedef enum { XC_OP_COUNT, XC_OP_INFO, XC_OP_LIST, XC_OP_CLEAR } xcache_op_type;
    1052 static void xcache_op(xcache_op_type optype, INTERNAL_FUNCTION_PARAMETERS)
     1126static void xcache_admin_operate(xcache_op_type optype, INTERNAL_FUNCTION_PARAMETERS)
    10531127{
    10541128    long type;
     
    10611135        RETURN_FALSE;
    10621136    }
     1137
     1138    xcache_admin_auth_check(TSRMLS_C);
    10631139
    10641140    if (optype == XC_OP_COUNT) {
     
    11431219PHP_FUNCTION(xcache_count)
    11441220{
    1145     xcache_op(XC_OP_COUNT, INTERNAL_FUNCTION_PARAM_PASSTHRU);
     1221    xcache_admin_operate(XC_OP_COUNT, INTERNAL_FUNCTION_PARAM_PASSTHRU);
    11461222}
    11471223/* }}} */
     
    11501226PHP_FUNCTION(xcache_info)
    11511227{
    1152     xcache_op(XC_OP_INFO, INTERNAL_FUNCTION_PARAM_PASSTHRU);
     1228    xcache_admin_operate(XC_OP_INFO, INTERNAL_FUNCTION_PARAM_PASSTHRU);
    11531229}
    11541230/* }}} */
     
    11571233PHP_FUNCTION(xcache_list)
    11581234{
    1159     xcache_op(XC_OP_LIST, INTERNAL_FUNCTION_PARAM_PASSTHRU);
     1235    xcache_admin_operate(XC_OP_LIST, INTERNAL_FUNCTION_PARAM_PASSTHRU);
    11601236}
    11611237/* }}} */
     
    11641240PHP_FUNCTION(xcache_clear_cache)
    11651241{
    1166     xcache_op(XC_OP_CLEAR, INTERNAL_FUNCTION_PARAM_PASSTHRU);
     1242    xcache_admin_operate(XC_OP_CLEAR, INTERNAL_FUNCTION_PARAM_PASSTHRU);
    11671243}
    11681244/* }}} */
  • trunk/xcache.ini

    r33 r34  
    77; required for >=php5.1 if you turn xcache on
    88auto_globals_jit = Off
     9
     10[xcache.admin]
     11xcache.admin.user = "mOo"
     12; xcache.admin.pass = md5($your_password)
     13xcache.admin.pass = ""
    914
    1015[xcache]
Note: See TracChangeset for help on using the changeset viewer.