Update of /cvsroot/mod-auth/mod_vhost_dbi/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12444/mod_vhost_dbi/src
Added Files:
Makefile.am mod_vhost_dbi.c
Log Message:
adding mod_vhost_dbi code.
This project may move somewhere else later. Right now the plan is to make
it work with mod_dbi_pool.
--- NEW FILE: Makefile.am ---
CLEANFILES = .libs/libmod_vhost_dbi *~
libmod_vhost_dbi_la_SOURCES = mod_vhost_dbi.c
libmod_vhost_dbi_la_CFLAGS = -I$(DBI_INCLUDES)
libmod_vhost_dbi_la_LDFLAGS = -L$(DBI_LIB) -ldbi
lib_LTLIBRARIES = libmod_vhost_dbi.la
make_so:
@if test ! -L mod_vhost_dbi.so ; then ln -s .libs/libmod_vhost_dbi.so mod_vhost_dbi.so ; fi
install: make_so
@${APXS} -i -a -n vhost_dbi mod_vhost_dbi.so
@echo ""
@echo ""
@echo "***********************************************"
@echo ""
@echo " Please read the documentation at "
@echo " http://mod-auth.sourceforge.net/docs/ for "
@echo " details on configuration of this module "
@echo ""
@echo "***********************************************"
@echo ""
--- NEW FILE: mod_vhost_dbi.c ---
/* ====================================================================
* Copyright 2003-2004 Paul Querna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_lib.h"
#include "apr_uri.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_main.h"
#include "http_protocol.h"
#include "http_request.h"
#include <dbi/dbi.h>
// include php support.
#define HAVE_PHP_SUPPORT
#ifdef HAVE_PHP_SUPPORT
#include <stdlib.h>
#include "zend.h"
#include "zend_qsort.h"
#include "zend_API.h"
#include "zend_ini.h"
#include "zend_alloc.h"
#include "zend_operators.h"
#endif
typedef struct {
} vhost_dbi_config_rec;
static void *vhost_dbi_create_server_config(apr_pool_t * p, server_rec * s)
{
vhost_dbi_config_rec *vdbir =
(vhost_dbi_config_rec *) apr_pcalloc(p, sizeof(vhost_dbi_config_rec));
return vdbir;
}
static void *vhost_dbi_merge_server_config(apr_pool_t * p, void *parentv,
void *childv)
{
vhost_dbi_config_rec *parent = (vhost_dbi_config_rec *) parentv;
vhost_dbi_config_rec *child = (vhost_dbi_config_rec *) childv;
vhost_dbi_config_rec *conf =
(vhost_dbi_config_rec *) apr_pcalloc(p, sizeof(vhost_dbi_config_rec));
if (child->fl == 1) {
conf->fl = child->fl;
conf->dbi_database =
(child->dbi_database ? child->dbi_database : parent->dbi_database);
conf->dbi_info_table =
(child->dbi_info_table ? child->dbi_info_table : parent->
dbi_info_table);
conf->dbi_alias_table =
(child->dbi_alias_table ? child->dbi_alias_table : parent->
dbi_alias_table);
conf->dbi_host_field =
(child->dbi_host_field ? child->dbi_host_field : parent->
dbi_host_field);
conf->dbi_path_field =
(child->dbi_path_field ? child->dbi_path_field : parent->
dbi_path_field);
conf->dbi_host =
(child->dbi_host ? child->dbi_host : parent->dbi_host);
conf->dbi_port =
(child->dbi_port ? child->dbi_port : parent->dbi_port);
conf->dbi_username =
(child->dbi_username ? child->dbi_username : parent->dbi_username);
conf->dbi_password =
(child->dbi_password ? child->dbi_password : parent->dbi_password);
conf->default_host =
(child->default_host ? child->default_host : parent->default_host);
conf->dbi_driver =
(child->dbi_driver ? child->dbi_driver : parent->dbi_driver);
}
return conf;
}
static const command_rec vhost_dbi_commands[] = {
AP_INIT_FLAG("VhostDbiEnabled",
set_switch,
(void *)1,
RSRC_CONF,
"turn mod_vhost_dbi on or off."),
NULL
};
static char *get_path(request_rec * r, const char *host, char **env,
vhost_dbi_config_rec * vdbir)
{
dbi_result *res;
dbi_conn *dbi = NULL;
char *qstr;
char *rstr;
char *path;
int dbi_err;
const char *dbi_err_str;
if (dbi == NULL) {
// XXXXX: use mod_dbi_pool
}
if (dbi == NULL) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r->server,
"vhost_dbi: get_path: DBI connection was not acquired!");
return NULL;
}
rstr = malloc(strlen(host) + 1);
strcpy(rstr, host);
dbi_driver_quote_string(dbi_conn_get_driver(dbi), &rstr);
// XXXXX: Custom SQL Query Here
qstr =
apr_psprintf(r->pool,
"SELECT vdbi_info.root_path, vdbi_info.php_path "
"FROM vdbi_info, vdbi_alias "
"WHERE vdbi_info.idx = vdbi_alias.owner_idx AND "
"vdbi_alias.hostname=%s LIMIT 0,1", rstr);
res = dbi_conn_query(dbi, qstr);
if (res == NULL) {
dbi_err = dbi_conn_error(dbi, &dbi_err_str);
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r->server,
"vhost_dbi: get_path: SQL Error: (%d) %s on query: %s", dbi_err,
dbi_err_str, qstr);
free(rstr);
dbi_result_free(res);
dbi_conn_close(dbi);
dbi_shutdown();
return NULL;
}
switch (dbi_result_get_numrows(res)) {
case 1:
break;
case 0:
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r->server,
"vhost_dbi: get_path: no results for %s", host);
free(rstr);
dbi_result_free(res);
dbi_conn_close(dbi);
dbi_shutdown();
return NULL;
default:
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r->server,
"vhost_dbi: get_path: %s has more than 1 server row, failing.",
host);
free(rstr);
dbi_result_free(res);
dbi_conn_close(dbi);
dbi_shutdown();
return NULL;
}
if (dbi_result_next_row(res) == 0) {
dbi_err = dbi_conn_error(dbi, &dbi_err_str);
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r->server,
"vhost_dbi: get_path: SQL Result Error: (%d) %s on query: %s",
dbi_err, dbi_err_str, qstr);
free(rstr);
dbi_result_free(res);
dbi_conn_close(dbi);
dbi_shutdown();
return NULL;
}
path =
apr_psprintf(r->pool, "%s", dbi_result_get_string(res, "root_path"));
free(rstr);
dbi_result_free(res);
dbi_conn_close(dbi);
dbi_shutdown();
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"get_path: %s from SQL: %s", path, qstr);
return path;
}
static int vhost_dbi_init_handler(apr_pool_t * pconf, apr_pool_t * plog,
apr_pool_t * ptemp, server_rec * s)
{
ap_add_version_component(pconf, "mod_vhost_dbi/0.1.0");
return OK;
}
static int vhost_dbi_translate_name(request_rec * r)
{
vhost_dbi_config_rec *vdbir =
(vhost_dbi_config_rec *) ap_get_module_config(r->server->module_config,
&vhost_dbi_module);
const char *host;
char *path;
char *env = NULL;
char *ptr;
int i;
if (!(vdbir->fl == 1)) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"vhost_dbi_translate_name: declined http://%s%s module is not configured for this server",
apr_table_get(r->headers_in, "Host"), r->uri);
return DECLINED;
}
if (!(host = apr_table_get(r->headers_in, "Host"))) {
if (!vdbir->default_host) {
ap_log_error(APLOG_MARK, APLOG_ALERT, 0, r->server,
"vhost_dbi_translate_name: no host found (non HTTP/1.1 request, no default set) %s",
host);
return DECLINED;
}
else {
host = vdbir->default_host;
}
}
if (ptr = strchr(host, ':')) {
*ptr = '\0';
}
path = get_path(r, host, &env, vdbir);
if (!path) {
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, r->server,
"vhost_dbi_translate_name: no host found in database for %s",
host);
return DECLINED;
}
apr_table_set(r->subprocess_env, "vhost_dbi_HOST", host);
apr_table_set(r->subprocess_env, "vhost_dbi_PATH", path);
r->filename = apr_psprintf(r->pool, "%s/public_html%s", path, r->uri);
#ifdef HAVE_PHP_SUPPORT
zend_alter_ini_entry("safe_mode", 13, "on", strlen("on"), 4, 16);
zend_alter_ini_entry("open_basedir", 13, path, strlen(path), 4, 16);
#endif
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"vhost_dbi_translate_name: translated http://%s%s to file %s from uri %s and path %s",
host, r->uri, r->filename, r->uri, path);
return OK;
}
static void register_hooks(apr_pool_t * p)
{
ap_hook_post_config(vhost_dbi_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_translate_name(vhost_dbi_translate_name, NULL, NULL,
APR_HOOK_FIRST);
}
AP_DECLARE_DATA module vhost_dbi_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
vhost_dbi_create_server_config, /* create per-server config structure */
vhost_dbi_merge_server_config, /* merge per-server config structures */
vhost_dbi_commands, /* command apr_table_t */
register_hooks /* register hooks */
};
|