2006-05-10 03:30:22 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (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.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* mod_xml_rpc.c -- XML RPC
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <switch.h>
|
2007-11-28 19:56:25 +00:00
|
|
|
#include <switch_version.h>
|
2007-02-13 21:38:12 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable:4142)
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
#include <xmlrpc-c/base.h>
|
2006-05-10 16:37:56 +00:00
|
|
|
#ifdef ABYSS_WIN32
|
|
|
|
#undef strcasecmp
|
|
|
|
#endif
|
2006-05-10 03:30:22 +00:00
|
|
|
#include <xmlrpc-c/abyss.h>
|
|
|
|
#include <xmlrpc-c/server.h>
|
|
|
|
#include <xmlrpc-c/server_abyss.h>
|
2007-11-27 19:25:16 +00:00
|
|
|
#include "../../libs/xmlrpc-c/lib/abyss/src/token.h"
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_xml_rpc_load);
|
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_rpc_shutdown);
|
|
|
|
SWITCH_MODULE_RUNTIME_FUNCTION(mod_xml_rpc_runtime);
|
|
|
|
SWITCH_MODULE_DEFINITION(mod_xml_rpc, mod_xml_rpc_load, mod_xml_rpc_shutdown, mod_xml_rpc_runtime);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2006-12-10 02:02:30 +00:00
|
|
|
static struct {
|
|
|
|
uint16_t port;
|
|
|
|
uint8_t running;
|
|
|
|
char *realm;
|
|
|
|
char *user;
|
|
|
|
char *pass;
|
2007-11-30 22:56:01 +00:00
|
|
|
TServer abyssServer;
|
2006-12-10 02:02:30 +00:00
|
|
|
} globals;
|
|
|
|
|
2007-10-17 14:59:17 +00:00
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_realm, globals.realm);
|
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_user, globals.user);
|
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_pass, globals.pass);
|
2006-12-10 02:02:30 +00:00
|
|
|
|
2007-10-17 14:59:17 +00:00
|
|
|
static switch_status_t do_config(void)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
|
|
|
char *cf = "xml_rpc.conf";
|
|
|
|
switch_xml_t cfg, xml, settings, param;
|
2006-06-01 17:16:15 +00:00
|
|
|
char *realm, *user, *pass;
|
|
|
|
|
|
|
|
realm = user = pass = NULL;
|
2006-05-10 15:47:54 +00:00
|
|
|
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
|
2006-05-10 03:30:22 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
|
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((settings = switch_xml_child(cfg, "settings"))) {
|
|
|
|
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
|
2006-05-17 00:58:21 +00:00
|
|
|
char *var = (char *) switch_xml_attr_soft(param, "name");
|
|
|
|
char *val = (char *) switch_xml_attr_soft(param, "value");
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2006-06-01 17:16:15 +00:00
|
|
|
if (!strcasecmp(var, "auth-realm")) {
|
|
|
|
realm = val;
|
|
|
|
} else if (!strcasecmp(var, "auth-user")) {
|
|
|
|
user = val;
|
|
|
|
} else if (!strcasecmp(var, "auth-pass")) {
|
|
|
|
pass = val;
|
2006-05-16 17:08:21 +00:00
|
|
|
} else if (!strcasecmp(var, "http-port")) {
|
2007-03-29 22:31:56 +00:00
|
|
|
globals.port = (uint16_t) atoi(val);
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
if (!globals.port) {
|
|
|
|
globals.port = 8080;
|
|
|
|
}
|
2006-06-01 17:16:15 +00:00
|
|
|
if (user && pass && realm) {
|
|
|
|
set_global_realm(realm);
|
|
|
|
set_global_user(user);
|
|
|
|
set_global_pass(pass);
|
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
switch_xml_free(xml);
|
|
|
|
|
2006-12-10 02:02:30 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_xml_rpc_load)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
2007-06-20 08:18:18 +00:00
|
|
|
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
|
|
|
memset(&globals, 0, sizeof(globals));
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
do_config();
|
2006-05-12 15:33:49 +00:00
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
/* indicate that the module should continue to be loaded */
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
static switch_status_t http_stream_raw_write(switch_stream_handle_t *handle, uint8_t *data, switch_size_t datalen)
|
|
|
|
{
|
|
|
|
TSession *r = handle->data;
|
|
|
|
|
|
|
|
return HTTPWrite(r, (char *)data, (uint32_t) datalen) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-02-14 18:08:11 +00:00
|
|
|
static switch_status_t http_stream_write(switch_stream_handle_t *handle, const char *fmt, ...)
|
2006-05-10 15:47:54 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
TSession *r = handle->data;
|
|
|
|
int ret = 0;
|
|
|
|
char *data;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2007-03-09 20:44:13 +00:00
|
|
|
ret = switch_vasprintf(&data, fmt, ap);
|
2006-05-10 15:47:54 +00:00
|
|
|
va_end(ap);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-05-10 15:47:54 +00:00
|
|
|
if (data) {
|
|
|
|
ret = 0;
|
2007-03-29 22:31:56 +00:00
|
|
|
HTTPWrite(r, data, (uint32_t) strlen(data));
|
2006-05-10 15:47:54 +00:00
|
|
|
free(data);
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-05-10 15:47:54 +00:00
|
|
|
return ret ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-06-01 17:16:15 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
static abyss_bool http_directory_auth(TSession *r, char *domain_name)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
2007-11-27 19:25:16 +00:00
|
|
|
char *p, *x;
|
2007-12-10 19:03:46 +00:00
|
|
|
char z[256], t[80];
|
2007-11-27 19:25:16 +00:00
|
|
|
char user[512];
|
|
|
|
char *pass;
|
|
|
|
const char *mypass1 = NULL, *mypass2 = NULL;
|
|
|
|
switch_xml_t x_domain, x_domain_root = NULL, x_user, x_params, x_param;
|
2007-11-28 19:56:25 +00:00
|
|
|
const char *box;
|
2007-12-10 19:03:46 +00:00
|
|
|
int at = 0;
|
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
p = RequestHeaderValue(r, "authorization");
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
NextToken(&p);
|
|
|
|
x = GetToken(&p);
|
|
|
|
if (x) {
|
|
|
|
if (!strcasecmp(x,"basic")) {
|
|
|
|
|
|
|
|
|
|
|
|
NextToken(&p);
|
|
|
|
switch_b64_decode(p, user, sizeof(user));
|
|
|
|
if ((pass = strchr(user, ':'))) {
|
|
|
|
*pass++ = '\0';
|
|
|
|
}
|
|
|
|
|
2007-12-10 19:03:46 +00:00
|
|
|
if (!domain_name) {
|
|
|
|
if ((domain_name = strchr(user, '@'))) {
|
|
|
|
*domain_name++ = '\0';
|
|
|
|
at++;
|
|
|
|
} else {
|
|
|
|
domain_name = r->host;
|
|
|
|
if (!strncasecmp(domain_name, "www.", 3)) {
|
|
|
|
domain_name += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!domain_name) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "can't find user [%s] no domain!\n", user);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2007-11-29 02:48:44 +00:00
|
|
|
if (switch_xml_locate_user("id", user, domain_name, NULL, &x_domain_root, &x_domain, &x_user, "mailbox=check") != SWITCH_STATUS_SUCCESS) {
|
2007-11-27 19:25:16 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "can't find user [%s@%s]\n", user, domain_name);
|
|
|
|
goto fail;
|
|
|
|
}
|
2007-11-28 19:56:25 +00:00
|
|
|
|
|
|
|
box = switch_xml_attr_soft(x_user, "mailbox");
|
|
|
|
|
2007-12-07 16:58:19 +00:00
|
|
|
for (x_param = switch_xml_child(x_domain, "param"); x_param; x_param = x_param->next) {
|
|
|
|
const char *var = switch_xml_attr_soft(x_param, "name");
|
|
|
|
const char *val = switch_xml_attr_soft(x_param, "value");
|
|
|
|
|
|
|
|
if (!strcasecmp(var, "password")) {
|
|
|
|
mypass1 = val;
|
|
|
|
} else if (!strcasecmp(var, "vm-password")) {
|
|
|
|
mypass2 = val;
|
|
|
|
} else if (!strncasecmp(var, "http-", 5)) {
|
|
|
|
ResponseAddField(r, (char *)var, (char *)val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
if (!(x_params = switch_xml_child(x_user, "params"))) {
|
|
|
|
goto authed;
|
|
|
|
}
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
for (x_param = switch_xml_child(x_params, "param"); x_param; x_param = x_param->next) {
|
|
|
|
const char *var = switch_xml_attr_soft(x_param, "name");
|
|
|
|
const char *val = switch_xml_attr_soft(x_param, "value");
|
|
|
|
|
|
|
|
if (!strcasecmp(var, "password")) {
|
|
|
|
mypass1 = val;
|
|
|
|
} else if (!strcasecmp(var, "vm-password")) {
|
|
|
|
mypass2 = val;
|
|
|
|
} else if (!strncasecmp(var, "http-", 5)) {
|
|
|
|
ResponseAddField(r, (char *)var, (char *)val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (!(mypass1 && mypass2)) {
|
|
|
|
r->user=strdup(user);
|
2007-11-27 19:25:16 +00:00
|
|
|
goto authed;
|
2007-11-28 19:56:25 +00:00
|
|
|
} else {
|
|
|
|
if (mypass1) {
|
2007-12-10 19:03:46 +00:00
|
|
|
if (at) {
|
|
|
|
snprintf(z, sizeof(z), "%s@%s:%s", user, domain_name, mypass1);
|
|
|
|
} else {
|
|
|
|
snprintf(z, sizeof(z), "%s:%s", user, mypass1);
|
|
|
|
}
|
2007-11-28 19:56:25 +00:00
|
|
|
Base64Encode(z, t);
|
|
|
|
|
|
|
|
if (!strcmp(p, t)) {
|
2007-12-10 19:03:46 +00:00
|
|
|
r->user=strdup(box ? box : user);
|
2007-11-28 19:56:25 +00:00
|
|
|
goto authed;
|
|
|
|
}
|
|
|
|
}
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (mypass2) {
|
2007-12-10 19:03:46 +00:00
|
|
|
if (at) {
|
|
|
|
snprintf(z, sizeof(z), "%s@%s:%s", user, domain_name, mypass2);
|
|
|
|
} else {
|
|
|
|
snprintf(z, sizeof(z), "%s:%s", user, mypass2);
|
|
|
|
}
|
2007-11-28 19:56:25 +00:00
|
|
|
Base64Encode(z, t);
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (!strcmp(p, t)) {
|
2007-12-10 19:03:46 +00:00
|
|
|
r->user=strdup(box ? box : user);
|
2007-11-28 19:56:25 +00:00
|
|
|
goto authed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (box) {
|
|
|
|
if (mypass1) {
|
2007-12-10 19:03:46 +00:00
|
|
|
if (at) {
|
|
|
|
snprintf(z, sizeof(z), "%s@%s:%s", box, domain_name, mypass1);
|
|
|
|
} else {
|
|
|
|
snprintf(z, sizeof(z), "%s:%s", box, mypass1);
|
|
|
|
}
|
2007-11-28 19:56:25 +00:00
|
|
|
Base64Encode(z, t);
|
|
|
|
|
|
|
|
if (!strcmp(p, t)) {
|
|
|
|
r->user=strdup(box);
|
|
|
|
goto authed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mypass2) {
|
2007-12-10 19:03:46 +00:00
|
|
|
if (at) {
|
|
|
|
snprintf(z, sizeof(z), "%s@%s:%s", box, domain_name, mypass2);
|
|
|
|
} else {
|
|
|
|
snprintf(z, sizeof(z), "%s:%s", box, mypass2);
|
|
|
|
}
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
Base64Encode(z, t);
|
|
|
|
|
|
|
|
if (!strcmp(p, t)) {
|
|
|
|
r->user=strdup(box);
|
|
|
|
goto authed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
|
|
authed:
|
|
|
|
|
2007-12-10 19:03:46 +00:00
|
|
|
ResponseAddField(r, "freeswitch-user", r->user);
|
|
|
|
ResponseAddField(r, "freeswitch-domain", domain_name);
|
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
if (x_domain_root) {
|
|
|
|
switch_xml_free(x_domain_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
if (x_domain_root) {
|
|
|
|
switch_xml_free(x_domain_root);
|
|
|
|
}
|
|
|
|
|
2007-12-10 19:03:46 +00:00
|
|
|
snprintf(z, sizeof(z), "Basic realm=\"%s\"", domain_name);
|
2007-11-27 19:25:16 +00:00
|
|
|
ResponseAddField(r, "WWW-Authenticate", z);
|
|
|
|
ResponseStatus(r, 401);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
abyss_bool auth_hook(TSession * r)
|
|
|
|
{
|
|
|
|
char *domain_name, *e;
|
|
|
|
abyss_bool ret = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!strncmp(r->uri, "/domains/", 9)) {
|
|
|
|
domain_name = strdup(r->uri + 9);
|
|
|
|
assert(domain_name);
|
|
|
|
|
|
|
|
if ((e = strchr(domain_name, '/'))) {
|
|
|
|
*e++ = '\0';
|
|
|
|
}
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (!strcmp(domain_name, "this")) {
|
|
|
|
free(domain_name);
|
|
|
|
domain_name = strdup(r->host);
|
|
|
|
}
|
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
ret = !http_directory_auth(r, domain_name);
|
|
|
|
|
|
|
|
free(domain_name);
|
|
|
|
} else {
|
2007-11-28 19:56:25 +00:00
|
|
|
char tmp[512];
|
|
|
|
const char *list[2] = {"index.html", "index.txt"};
|
|
|
|
int x;
|
|
|
|
|
|
|
|
if (!strncmp(r->uri, "/pub", 4)) {
|
|
|
|
char *p = r->uri;
|
|
|
|
char *new_uri = p + 4;
|
|
|
|
if (!new_uri) {
|
|
|
|
new_uri = "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s%s",
|
|
|
|
SWITCH_GLOBAL_dirs.htdocs_dir,
|
|
|
|
new_uri
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (switch_directory_exists(tmp, NULL) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
for (x = 0; x < 2; x++) {
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s%s%s%s",
|
|
|
|
SWITCH_GLOBAL_dirs.htdocs_dir,
|
|
|
|
new_uri,
|
|
|
|
end_of(new_uri) == *SWITCH_PATH_SEPARATOR ? "" : SWITCH_PATH_SEPARATOR,
|
|
|
|
list[x]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (switch_file_exists(tmp, NULL) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s%s%s",
|
|
|
|
new_uri,
|
|
|
|
end_of(new_uri) == '/' ? "" : "/",
|
|
|
|
list[x]
|
|
|
|
);
|
|
|
|
new_uri = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r->uri = strdup(new_uri);
|
|
|
|
free(p);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (globals.realm && strncmp(r->uri, "/pub", 4)) {
|
2007-12-10 19:03:46 +00:00
|
|
|
ret = !http_directory_auth(r, NULL);
|
|
|
|
if (ret) {
|
|
|
|
ret = !RequestAuth(r, globals.realm, globals.user, globals.pass);
|
2007-11-28 19:56:25 +00:00
|
|
|
}
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-28 19:56:25 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
abyss_bool handler_hook(TSession * r)
|
|
|
|
{
|
|
|
|
//char *mime = "text/html";
|
2007-11-30 22:56:01 +00:00
|
|
|
char buf[80] = "HTTP/1.1 200 OK\n";
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_stream_handle_t stream = { 0 };
|
2006-05-10 16:10:18 +00:00
|
|
|
char *command;
|
2007-11-27 19:25:16 +00:00
|
|
|
int i, j = 0;
|
|
|
|
TTableItem *ti;
|
|
|
|
char *dup = NULL;
|
2007-11-28 19:56:25 +00:00
|
|
|
int auth = 0;
|
|
|
|
char *fs_user = NULL, *fs_domain = NULL;
|
|
|
|
char *path_info = NULL;
|
|
|
|
abyss_bool ret = TRUE;
|
2007-12-04 16:22:02 +00:00
|
|
|
int html = 0;
|
2006-05-10 19:07:38 +00:00
|
|
|
|
2006-06-01 17:16:15 +00:00
|
|
|
stream.data = r;
|
|
|
|
stream.write_function = http_stream_write;
|
2007-11-28 19:56:25 +00:00
|
|
|
stream.raw_write_function = http_stream_raw_write;
|
2006-06-01 17:16:15 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
if ((command = strstr(r->uri, "/api/"))) {
|
|
|
|
command += 5;
|
2007-12-04 16:22:02 +00:00
|
|
|
} else if ((command = strstr(r->uri, "/webapi/"))) {
|
|
|
|
command += 8;
|
|
|
|
html++;
|
2007-11-27 19:25:16 +00:00
|
|
|
} else {
|
2007-11-28 19:56:25 +00:00
|
|
|
ret = FALSE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((path_info = strchr(command, '/'))) {
|
|
|
|
*path_info++ = '\0';
|
|
|
|
}
|
|
|
|
|
2006-06-01 17:16:15 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
for (i=0;i<r->response_headers.size;i++) {
|
|
|
|
ti=&r->response_headers.item[i];
|
2007-11-28 19:56:25 +00:00
|
|
|
if (!strcasecmp(ti->name, "freeswitch-user")) {
|
|
|
|
fs_user = ti->value;
|
|
|
|
} else if (!strcasecmp(ti->name, "freeswitch-domain")) {
|
|
|
|
fs_domain = ti->value;
|
|
|
|
} else if (!strcasecmp(ti->name, "http-allowed-api")) {
|
2007-11-27 19:25:16 +00:00
|
|
|
int argc, x;
|
|
|
|
char *argv[256] = { 0 };
|
|
|
|
j++;
|
|
|
|
|
|
|
|
if (!strcasecmp(ti->value, "any")) {
|
2007-11-28 19:56:25 +00:00
|
|
|
auth++;
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
2006-06-01 17:16:15 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
dup = strdup(ti->value);
|
|
|
|
argc = switch_separate_string(dup, ',', argv, (sizeof(argv) / sizeof(argv[0])));
|
2007-11-28 19:56:25 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
for (x = 0; x < argc; x++) {
|
|
|
|
if (!strcasecmp(command, argv[x])) {
|
2007-11-28 19:56:25 +00:00
|
|
|
auth++;
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2007-12-10 19:03:46 +00:00
|
|
|
if (fs_user && fs_domain && !j) {
|
2007-11-28 19:56:25 +00:00
|
|
|
auth = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auth) {
|
|
|
|
goto auth;
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
//unauth:
|
2007-11-27 19:25:16 +00:00
|
|
|
ResponseStatus(r, 403);
|
|
|
|
ResponseError(r);
|
|
|
|
switch_safe_free(dup);
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
ret = TRUE;
|
|
|
|
goto end;
|
2007-11-27 19:25:16 +00:00
|
|
|
|
|
|
|
auth:
|
|
|
|
|
|
|
|
|
2006-05-10 19:07:38 +00:00
|
|
|
if (switch_event_create(&stream.event, SWITCH_EVENT_API) == SWITCH_STATUS_SUCCESS) {
|
2007-04-27 21:13:02 +00:00
|
|
|
const char * const content_length = RequestHeaderValue(r, "content-length");
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (fs_user)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "FreeSWITCH-User", "%s", fs_user);
|
|
|
|
if (fs_domain)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "FreeSWITCH-Domain", "%s", fs_domain);
|
|
|
|
if (path_info)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-Path-Info", "%s", path_info);
|
2007-03-29 22:31:56 +00:00
|
|
|
if (r->uri)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-URI", "%s", r->uri);
|
|
|
|
if (r->query)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-QUERY", "%s", r->query);
|
|
|
|
if (r->host)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-HOST", "%s", r->host);
|
|
|
|
if (r->from)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-FROM", "%s", r->from);
|
|
|
|
if (r->useragent)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-USER-AGENT", "%s", r->useragent);
|
|
|
|
if (r->referer)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-REFERER", "%s", r->referer);
|
|
|
|
if (r->requestline)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-REQUESTLINE", "%s", r->requestline);
|
|
|
|
if (r->user)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-USER", "%s", r->user);
|
|
|
|
if (r->port)
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-PORT", "%u", r->port);
|
2007-04-27 21:13:02 +00:00
|
|
|
if (r->query || content_length) {
|
|
|
|
char *q, *qd;
|
|
|
|
char *next;
|
|
|
|
char *query = r->query;
|
|
|
|
char *name, *val;
|
|
|
|
char qbuf[8192] = "";
|
|
|
|
|
|
|
|
if (r->method == m_post && content_length) {
|
|
|
|
int len = atoi(content_length);
|
|
|
|
int qlen = 0;
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
int succeeded;
|
|
|
|
char *qp = qbuf;
|
|
|
|
do {
|
|
|
|
int blen = r->conn->buffersize - r->conn->bufferpos;
|
|
|
|
|
|
|
|
if ((qlen + blen) > len) {
|
|
|
|
blen = len - qlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
qlen += blen;
|
|
|
|
|
|
|
|
if ( qlen > sizeof(qbuf) ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(qp, r->conn->buffer + r->conn->bufferpos, blen);
|
|
|
|
qp += blen;
|
|
|
|
|
|
|
|
if (qlen >= len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ((succeeded = ConnRead(r->conn, r->server->timeout)));
|
|
|
|
|
|
|
|
query = qbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (query) {
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-QUERY", "%s", query);
|
|
|
|
|
|
|
|
qd = strdup(query);
|
|
|
|
assert(qd != NULL);
|
|
|
|
|
|
|
|
q = qd;
|
|
|
|
next = q;
|
|
|
|
|
|
|
|
do {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if ((next = strchr(next, '&'))) {
|
|
|
|
*next++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = q; p && *p; p++) {
|
|
|
|
if (*p == '+') {
|
|
|
|
*p = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_url_decode(q);
|
|
|
|
|
|
|
|
|
|
|
|
name = q;
|
|
|
|
if ((val = strchr(name, '='))) {
|
|
|
|
*val++ = '\0';
|
|
|
|
switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, name, "%s", val);
|
|
|
|
}
|
|
|
|
q = next;
|
|
|
|
} while (q != NULL);
|
|
|
|
|
|
|
|
free(qd);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2006-05-10 19:07:38 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
|
|
|
|
//ResponseChunked(r);
|
|
|
|
|
|
|
|
//ResponseContentType(r, mime);
|
|
|
|
//ResponseWrite(r);
|
|
|
|
|
|
|
|
HTTPWrite(r, buf, (uint32_t) strlen(buf));
|
|
|
|
|
2007-11-30 22:56:01 +00:00
|
|
|
//HTTPWrite(r, "<pre>\n\n", 7);
|
|
|
|
|
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
/* generation of the date field */
|
|
|
|
if (DateToString(&r->date, buf)) {
|
|
|
|
ResponseAddField(r,"Date", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generation of the server field */
|
2007-11-28 19:56:25 +00:00
|
|
|
ResponseAddField(r,"Server", "FreeSWITCH-" SWITCH_VERSION_FULL "-mod_xml_rpc");
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2007-12-04 16:22:02 +00:00
|
|
|
if (html) {
|
|
|
|
ResponseAddField(r, "Content-Type", "text/html");
|
|
|
|
}
|
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
for (i=0;i<r->response_headers.size;i++) {
|
|
|
|
ti=&r->response_headers.item[i];
|
2007-11-28 22:27:33 +00:00
|
|
|
ConnWrite(r->conn, ti->name, (uint32_t)strlen(ti->name));
|
2007-11-27 19:25:16 +00:00
|
|
|
ConnWrite(r->conn,": ",2);
|
2007-11-28 22:27:33 +00:00
|
|
|
ConnWrite(r->conn, ti->value, (uint32_t)strlen(ti->value));
|
2007-11-27 19:25:16 +00:00
|
|
|
ConnWrite(r->conn,CRLF,2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-30 22:56:01 +00:00
|
|
|
snprintf(buf, sizeof(buf), "Connection: close\r\n");
|
|
|
|
ConnWrite(r->conn, buf, (uint32_t) strlen(buf));
|
|
|
|
|
2007-12-04 16:22:02 +00:00
|
|
|
if (html) {
|
|
|
|
ConnWrite(r->conn, "\r\n", 2);
|
|
|
|
}
|
|
|
|
|
2007-11-30 22:56:01 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
if (switch_api_execute(command, r->query, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
|
2007-11-30 22:56:01 +00:00
|
|
|
ResponseStatus(r, 200);
|
2007-11-27 19:25:16 +00:00
|
|
|
r->done = TRUE;
|
|
|
|
} else {
|
|
|
|
ResponseStatus(r, 404);
|
|
|
|
ResponseError(r);
|
|
|
|
}
|
|
|
|
|
2007-11-30 22:56:01 +00:00
|
|
|
SocketClose(&(r->conn->socket));
|
|
|
|
HTTPWriteEnd(r);
|
|
|
|
ConnClose(r->conn);
|
2007-11-28 19:56:25 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
return ret;
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 18:34:22 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
static xmlrpc_value *freeswitch_api(xmlrpc_env * const envP, xmlrpc_value * const paramArrayP, void *const userData)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
2007-09-11 22:29:29 +00:00
|
|
|
char *command = NULL, *arg = NULL;
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_stream_handle_t stream = { 0 };
|
2007-03-07 18:34:22 +00:00
|
|
|
xmlrpc_value *val = NULL;
|
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
/* Parse our argument array. */
|
|
|
|
xmlrpc_decompose_value(envP, paramArrayP, "(ss)", &command, &arg);
|
|
|
|
if (envP->fault_occurred) {
|
2007-09-11 22:29:29 +00:00
|
|
|
goto done;
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 18:34:22 +00:00
|
|
|
SWITCH_STANDARD_STREAM(stream);
|
|
|
|
if (switch_api_execute(command, arg, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
/* Return our result. */
|
|
|
|
val = xmlrpc_build_value(envP, "s", stream.data);
|
|
|
|
free(stream.data);
|
|
|
|
} else {
|
|
|
|
val = xmlrpc_build_value(envP, "s", "ERROR!");
|
|
|
|
}
|
|
|
|
|
2007-10-17 14:59:17 +00:00
|
|
|
done:
|
2007-09-11 22:29:29 +00:00
|
|
|
/* xmlrpc-c requires us to free memory it malloced from xmlrpc_decompose_value */
|
|
|
|
switch_safe_free(command);
|
|
|
|
switch_safe_free(arg);
|
2007-03-07 18:34:22 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
static xmlrpc_value *freeswitch_man(xmlrpc_env * const envP, xmlrpc_value * const paramArrayP, void *const userData)
|
2007-03-07 18:34:22 +00:00
|
|
|
{
|
2007-09-11 22:29:29 +00:00
|
|
|
char *oid = NULL, *relative_oid, *s_action = NULL, *data = NULL;
|
2007-03-07 18:34:22 +00:00
|
|
|
char buf[SWITCH_MAX_MANAGEMENT_BUFFER_LEN] = "";
|
|
|
|
switch_management_action_t action = SMA_NONE;
|
2007-09-11 22:29:29 +00:00
|
|
|
xmlrpc_value *val = NULL;
|
2007-03-07 18:34:22 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
/* Parse our argument array. */
|
|
|
|
xmlrpc_decompose_value(envP, paramArrayP, "(sss)", &oid, &s_action, &data);
|
|
|
|
if (envP->fault_occurred) {
|
2007-09-11 22:29:29 +00:00
|
|
|
goto done;
|
2007-03-07 18:34:22 +00:00
|
|
|
}
|
|
|
|
|
2007-03-17 20:12:13 +00:00
|
|
|
if (!strncasecmp(oid, FREESWITCH_OID_PREFIX, strlen(FREESWITCH_OID_PREFIX))) {
|
|
|
|
relative_oid = oid + strlen(FREESWITCH_OID_PREFIX);
|
2007-03-07 18:34:22 +00:00
|
|
|
} else {
|
|
|
|
relative_oid = oid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!switch_strlen_zero(data)) {
|
|
|
|
switch_copy_string(buf, data, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(s_action, "get")) {
|
|
|
|
action = SMA_GET;
|
|
|
|
} else if (!strcasecmp(s_action, "set")) {
|
|
|
|
action = SMA_SET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
if (switch_core_management_exec(relative_oid, action, buf, sizeof(buf)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
if (action == SMA_SET) {
|
2007-08-03 21:29:01 +00:00
|
|
|
if (*buf != '\0') {
|
2007-03-07 18:34:22 +00:00
|
|
|
snprintf(buf, sizeof(buf), "OK\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-08-03 21:29:01 +00:00
|
|
|
if (*buf != '\0') {
|
2007-03-07 18:34:22 +00:00
|
|
|
snprintf(buf, sizeof(buf), "ERROR\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snprintf(buf, sizeof(buf), "Invalid Action %s\n", s_action);
|
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
/* Return our result. */
|
|
|
|
val = xmlrpc_build_value(envP, "s", buf);
|
2007-03-07 18:34:22 +00:00
|
|
|
|
2007-10-17 14:59:17 +00:00
|
|
|
done:
|
2007-09-11 22:29:29 +00:00
|
|
|
/* xmlrpc-c requires us to free memory it malloced from xmlrpc_decompose_value */
|
|
|
|
switch_safe_free(oid);
|
|
|
|
switch_safe_free(s_action);
|
|
|
|
switch_safe_free(data);
|
2006-05-10 03:30:22 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_RUNTIME_FUNCTION(mod_xml_rpc_runtime)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
2007-11-30 22:56:01 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
xmlrpc_registry *registryP;
|
|
|
|
xmlrpc_env env;
|
2006-05-10 03:30:22 +00:00
|
|
|
char logfile[512];
|
2007-11-28 20:44:18 +00:00
|
|
|
switch_hash_index_t *hi;
|
|
|
|
const void *var;
|
|
|
|
void *val;
|
2006-05-10 03:30:22 +00:00
|
|
|
|
|
|
|
globals.running = 1;
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
xmlrpc_env_init(&env);
|
|
|
|
|
|
|
|
registryP = xmlrpc_registry_new(&env);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
xmlrpc_registry_add_method(&env, registryP, NULL, "freeswitch.api", &freeswitch_api, NULL);
|
|
|
|
xmlrpc_registry_add_method(&env, registryP, NULL, "freeswitch_api", &freeswitch_api, NULL);
|
|
|
|
xmlrpc_registry_add_method(&env, registryP, NULL, "freeswitch.management", &freeswitch_man, NULL);
|
|
|
|
xmlrpc_registry_add_method(&env, registryP, NULL, "freeswitch_management", &freeswitch_man, NULL);
|
|
|
|
|
|
|
|
MIMETypeInit();
|
2006-05-10 03:30:22 +00:00
|
|
|
MIMETypeAdd("text/html", "html");
|
2007-11-28 20:44:18 +00:00
|
|
|
for(hi = switch_core_mime_index(); hi; hi = switch_hash_next(hi)) {
|
|
|
|
switch_hash_this(hi, &var, NULL, &val);
|
|
|
|
if (var && val) {
|
|
|
|
MIMETypeAdd((char *) val, (char *) var);
|
|
|
|
}
|
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
snprintf(logfile, sizeof(logfile), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "freeswitch_http.log");
|
2007-11-30 22:56:01 +00:00
|
|
|
ServerCreate(&globals.abyssServer, "XmlRpcServer", globals.port, SWITCH_GLOBAL_dirs.htdocs_dir, logfile);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-11-30 22:56:01 +00:00
|
|
|
xmlrpc_server_abyss_set_handler(&env, &globals.abyssServer, "/RPC2", registryP);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-11-30 22:56:01 +00:00
|
|
|
if (ServerInit(&globals.abyssServer) != TRUE) {
|
2007-04-25 19:57:14 +00:00
|
|
|
globals.running = 0;
|
2007-04-26 13:52:56 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to start HTTP Port %d\n", globals.port);
|
|
|
|
return SWITCH_STATUS_TERM;
|
2007-04-25 19:57:14 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-11-30 22:56:01 +00:00
|
|
|
ServerAddHandler(&globals.abyssServer, handler_hook);
|
|
|
|
ServerAddHandler(&globals.abyssServer, auth_hook);
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Starting HTTP Port %d, DocRoot [%s]\n", globals.port, SWITCH_GLOBAL_dirs.htdocs_dir);
|
2007-03-29 22:31:56 +00:00
|
|
|
while (globals.running) {
|
2007-11-30 22:56:01 +00:00
|
|
|
//ServerRunOnce2(&globals.abyssServer, ABYSS_FOREGROUND);
|
|
|
|
ServerRun(&globals.abyssServer);
|
2007-03-29 22:31:56 +00:00
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_rpc_shutdown)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
2007-11-30 22:56:01 +00:00
|
|
|
globals.abyssServer.running = 0;
|
2006-05-10 03:30:22 +00:00
|
|
|
globals.running = 0;
|
2007-11-30 22:56:01 +00:00
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-11-27 22:30:48 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2007-02-09 02:36:03 +00:00
|
|
|
* indent-tabs-mode:t
|
2006-11-27 22:30:48 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
|
|
|
|
*/
|