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-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>
|
|
|
|
|
|
|
|
static const char modname[] = "mod_xml_rpc";
|
|
|
|
|
|
|
|
static switch_loadable_module_interface_t xml_rpc_module_interface = {
|
|
|
|
/*.module_name */ modname,
|
|
|
|
/*.endpoint_interface */ NULL,
|
|
|
|
/*.timer_interface */ NULL,
|
|
|
|
/*.dialplan_interface */ NULL,
|
|
|
|
/*.codec_interface */ NULL,
|
|
|
|
/*.application_interface */ NULL,
|
|
|
|
/*.api_interface */ NULL,
|
|
|
|
/*.file_interface */ NULL,
|
|
|
|
/*.speech_interface */ NULL,
|
|
|
|
/*.directory_interface */ NULL
|
|
|
|
};
|
|
|
|
|
2006-12-10 02:02:30 +00:00
|
|
|
static struct {
|
|
|
|
uint16_t port;
|
|
|
|
uint8_t running;
|
|
|
|
char *realm;
|
|
|
|
char *user;
|
|
|
|
char *pass;
|
|
|
|
} globals;
|
|
|
|
|
|
|
|
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-05-10 03:30:22 +00:00
|
|
|
static switch_status_t do_config(void)
|
|
|
|
{
|
|
|
|
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")) {
|
2006-05-10 16:37:56 +00:00
|
|
|
globals.port = (uint16_t)atoi(val);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename)
|
|
|
|
{
|
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
|
|
|
*module_interface = &xml_rpc_module_interface;
|
|
|
|
|
|
|
|
memset(&globals, 0, sizeof(globals));
|
|
|
|
|
2006-12-10 02:02:30 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-10 15:47:54 +00:00
|
|
|
static switch_status_t http_stream_write(switch_stream_handle_t *handle, char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
TSession *r = handle->data;
|
|
|
|
int ret = 0;
|
|
|
|
char *data;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#ifdef HAVE_VASPRINTF
|
|
|
|
ret = vasprintf(&data, fmt, ap);
|
|
|
|
#else
|
|
|
|
if ((data = (char *) malloc(2048))) {
|
|
|
|
vsnprintf(data, 2048, fmt, ap);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
ret = 0;
|
2006-05-10 16:37:56 +00:00
|
|
|
HTTPWrite(r, data, (uint32_t)strlen(data));
|
2006-05-10 15:47:54 +00:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-06-01 17:16:15 +00:00
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
abyss_bool HandleHook(TSession *r)
|
|
|
|
{
|
|
|
|
char *m = "text/html";
|
2006-05-10 15:47:54 +00:00
|
|
|
switch_stream_handle_t stream = {0};
|
2006-05-10 16:10:18 +00:00
|
|
|
char *command;
|
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;
|
|
|
|
|
|
|
|
if (globals.realm) {
|
|
|
|
if (!RequestAuth(r,globals.realm, globals.user, globals.pass)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
if(strncmp(r->uri, "/api/", 5)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-05-10 19:07:38 +00:00
|
|
|
if (switch_event_create(&stream.event, SWITCH_EVENT_API) == SWITCH_STATUS_SUCCESS) {
|
2007-02-13 04:43:49 +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);
|
2006-05-10 19:07:38 +00:00
|
|
|
if (r->port) switch_event_add_header(stream.event, SWITCH_STACK_BOTTOM, "HTTP-PORT", "%u", r->port);
|
|
|
|
}
|
|
|
|
|
2006-05-10 16:10:18 +00:00
|
|
|
command = r->uri + 5;
|
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
ResponseChunked(r);
|
|
|
|
ResponseStatus(r,200);
|
|
|
|
ResponseContentType(r, m);
|
|
|
|
ResponseWrite(r);
|
2006-07-26 20:12:49 +00:00
|
|
|
switch_api_execute(command, r->query, NULL, &stream);
|
2006-05-10 03:30:22 +00:00
|
|
|
HTTPWriteEnd(r);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-05-10 15:47:54 +00:00
|
|
|
#define CMDLEN 1024 * 256
|
2006-05-10 03:30:22 +00:00
|
|
|
static xmlrpc_value *freeswitch_api(xmlrpc_env *const envP, xmlrpc_value *const paramArrayP, void *const userData)
|
|
|
|
{
|
|
|
|
char *command, *arg;
|
|
|
|
char *retbuf = malloc(CMDLEN);
|
2006-05-10 15:47:54 +00:00
|
|
|
switch_stream_handle_t stream = {0};
|
2006-05-10 03:30:22 +00:00
|
|
|
xmlrpc_value *val;
|
|
|
|
|
|
|
|
/* Parse our argument array. */
|
|
|
|
xmlrpc_decompose_value(envP, paramArrayP, "(ss)", &command, &arg);
|
|
|
|
if (envP->fault_occurred) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(retbuf, 0, CMDLEN);
|
2006-05-10 15:47:54 +00:00
|
|
|
stream.data = retbuf;
|
|
|
|
stream.end = stream.data;
|
|
|
|
stream.data_size = CMDLEN;
|
|
|
|
stream.write_function = switch_console_stream_write;
|
2006-07-26 20:12:49 +00:00
|
|
|
switch_api_execute(command, arg, NULL, &stream);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
|
|
|
/* Return our result. */
|
|
|
|
val = xmlrpc_build_value(envP, "s", retbuf);
|
|
|
|
free(retbuf);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_MOD_DECLARE(switch_status_t) switch_module_runtime(void)
|
|
|
|
{
|
|
|
|
TServer abyssServer;
|
|
|
|
xmlrpc_registry * registryP;
|
|
|
|
xmlrpc_env env;
|
|
|
|
char logfile[512];
|
|
|
|
|
|
|
|
globals.running = 1;
|
|
|
|
|
|
|
|
xmlrpc_env_init(&env);
|
|
|
|
|
|
|
|
registryP = xmlrpc_registry_new(&env);
|
|
|
|
|
|
|
|
xmlrpc_registry_add_method(&env, registryP, NULL, "freeswitch.api", &freeswitch_api, NULL);
|
2007-01-22 18:18:48 +00:00
|
|
|
xmlrpc_registry_add_method(&env, registryP, NULL,"freeswitch_api", &freeswitch_api, NULL);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
|
|
|
MIMETypeInit();
|
|
|
|
MIMETypeAdd("text/html", "html");
|
|
|
|
|
2006-06-05 18:12:33 +00:00
|
|
|
snprintf(logfile, sizeof(logfile), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "freeswitch_http.log");
|
2006-05-10 03:30:22 +00:00
|
|
|
ServerCreate(&abyssServer, "XmlRpcServer", globals.port, SWITCH_GLOBAL_dirs.htdocs_dir, logfile);
|
|
|
|
|
|
|
|
xmlrpc_server_abyss_set_handler(&env, &abyssServer, "/RPC2", registryP);
|
|
|
|
|
|
|
|
ServerInit(&abyssServer);
|
|
|
|
ServerAddHandler(&abyssServer, HandleHook);
|
|
|
|
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Starting HTTP Port %d, DocRoot [%s]\n", globals.port, SWITCH_GLOBAL_dirs.htdocs_dir);
|
|
|
|
while (globals.running) {
|
|
|
|
ServerRunOnce2(&abyssServer, ABYSS_FOREGROUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_MOD_DECLARE(switch_status_t) switch_module_shutdown(void)
|
|
|
|
{
|
|
|
|
globals.running = 0;
|
|
|
|
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:
|
|
|
|
*/
|