xhttp_mod.c

00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 
00025 #include <string.h>
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include "../../sr_module.h"
00029 #include "../../error.h"
00030 #include "../../mem/mem.h"
00031 #include "../../ut.h"
00032 #include "../../dset.h"
00033 #include "../../dprint.h"
00034 #include "../../data_lump.h"
00035 #include "../../data_lump_rpl.h"
00036 #include "../../receive.h"
00037 #include "../../msg_translator.h"
00038 #include "../../modules/sl/sl.h"
00039 #include "../../nonsip_hooks.h"
00040 #include "../../action.h"
00041 #include "../../script_cb.h"
00042 #include "../../route.h"
00043 #include "../../sip_msg_clone.h"
00044 #include "../../mod_fix.h"
00045 #include "../../pvar.h"
00046 
00047 #include "api.h"
00048 
00049 MODULE_VERSION
00050 
00051 static int xhttp_handler(sip_msg_t* msg);
00052 static int w_xhttp_send_reply(sip_msg_t* msg, char* pcode, char* preason,
00053                 char *pctype, char* pbody);
00054 static int mod_init(void);
00055 
00056 static int fixup_xhttp_reply(void** param, int param_no);
00057 
00058 static int pv_get_huri(struct sip_msg *msg, pv_param_t *param,
00059                 pv_value_t *res);
00060 
00061 static int xhttp_route_no=DEFAULT_RT;
00062 static char* xhttp_url_match = NULL;
00063 static regex_t xhttp_url_match_regexp;
00064 static char* xhttp_url_skip = NULL;
00065 static regex_t xhttp_url_skip_regexp;
00066 
00068 sl_api_t slb;
00069 
00070 
00071 static cmd_export_t cmds[] = {
00072         {"xhttp_reply",    (cmd_function)w_xhttp_send_reply,
00073                 4, fixup_xhttp_reply,  0, REQUEST_ROUTE},
00074         {"bind_xhttp",     (cmd_function)bind_xhttp,
00075                 0, 0, 0, ANY_ROUTE},
00076         {0, 0, 0, 0, 0}
00077 };
00078 
00079 static pv_export_t mod_pvs[] = {
00080         {{"hu", (sizeof("hu")-1)}, /* */
00081                 PVT_OTHER, pv_get_huri, 0,
00082                 0, 0, 0, 0},
00083 
00084         { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
00085 };
00086 
00087 static param_export_t params[] = {
00088         {"url_match",       STR_PARAM, &xhttp_url_match},
00089         {"url_skip",        STR_PARAM, &xhttp_url_skip},
00090         {0, 0, 0}
00091 };
00092 
00094 struct module_exports exports= {
00095         "xhttp",
00096         DEFAULT_DLFLAGS, /* dlopen flags */
00097         cmds,
00098         params,
00099         0,          /* exported statistics */
00100         0  ,        /* exported MI functions */
00101         mod_pvs,    /* exported pseudo-variables */
00102         0,          /* extra processes */
00103         mod_init,   /* module initialization function */
00104         0,
00105         0,
00106         0           /* per-child init function */
00107 };
00108 
00112 static int mod_init(void)
00113 {
00114         struct nonsip_hook nsh;
00115         int route_no;
00116         
00117         route_no=route_get(&event_rt, "xhttp:request");
00118         if (route_no==-1)
00119         {
00120                 LM_ERR("failed to find event_route[xhttp:request]\n");
00121                 return -1;
00122         }
00123         if (event_rt.rlist[route_no]==0)
00124         {
00125                 LM_WARN("event_route[xhttp:request] is empty\n");
00126         }
00127         xhttp_route_no=route_no;
00128         
00129         /* bind the SL API */
00130         if (sl_load_api(&slb)!=0) {
00131                 LM_ERR("cannot bind to SL API\n");
00132                 return -1;
00133         }
00134 
00135         /* register non-sip hooks */
00136         memset(&nsh, 0, sizeof(nsh));
00137         nsh.name = "xhttp";
00138         nsh.destroy = 0;
00139         nsh.on_nonsip_req = xhttp_handler;
00140         if (register_nonsip_msg_hook(&nsh)<0)
00141         {
00142                 LM_ERR("Failed to register non sip msg hooks\n");
00143                 return -1;
00144         }
00145 
00146         if(xhttp_url_match!=NULL)
00147         {
00148                 memset(&xhttp_url_match_regexp, 0, sizeof(regex_t));
00149                 if (regcomp(&xhttp_url_match_regexp, xhttp_url_match, REG_EXTENDED)!=0) {
00150                         LM_ERR("bad match re %s\n", xhttp_url_match);
00151                         return E_BAD_RE;
00152                 }
00153         }
00154         if(xhttp_url_skip!=NULL)
00155         {
00156                 memset(&xhttp_url_skip_regexp, 0, sizeof(regex_t));
00157                 if (regcomp(&xhttp_url_skip_regexp, xhttp_url_skip, REG_EXTENDED)!=0) {
00158                         LM_ERR("bad skip re %s\n", xhttp_url_skip);
00159                         return E_BAD_RE;
00160                 }
00161         }
00162         return 0;
00163 }
00164 
00168 static int pv_get_huri(struct sip_msg *msg, pv_param_t *param,
00169                 pv_value_t *res)
00170 {
00171         if(msg==NULL || res==NULL)
00172                 return -1;
00173 
00174         return pv_get_strval(msg, param, res, &msg->first_line.u.request.uri);
00175 }
00176 
00177 
00181 static char* xhttp_to_sip(sip_msg_t* msg, int* new_msg_len)
00182 {
00183         unsigned int len, via_len;
00184         char* via, *new_msg, *p;
00185         str ip, port;
00186         struct hostport hp;
00187         struct dest_info dst;
00188         
00189         ip.s = ip_addr2a(&msg->rcv.src_ip);
00190         ip.len = strlen(ip.s);
00191         port.s = int2str(msg->rcv.src_port, &port.len);
00192         hp.host = &ip;
00193         hp.port = &port;
00194         init_dst_from_rcv(&dst, &msg->rcv);
00195         via = via_builder(&via_len, &dst, 0, 0, &hp);
00196         if (via == 0)
00197         {
00198                 LM_DBG("failed to build via\n");
00199                 return 0;
00200         }
00201         len = via_len + msg->len;
00202         p = new_msg = pkg_malloc(len + 1);
00203         if (new_msg == 0)
00204         {
00205                 LM_DBG("memory allocation failure (%d bytes)\n", len);
00206                 pkg_free(via);
00207                 return 0;
00208         }
00209 
00210         /* new message:
00211          * <orig first line> 
00212          * Via: <faked via>
00213          * <orig http message w/o the first line>
00214          */
00215         memcpy(p, msg->first_line.u.request.method.s, 
00216                    msg->first_line.len);
00217         p += msg->first_line.len;
00218         memcpy(p, via, via_len);
00219         p += via_len;
00220         memcpy(p,  SIP_MSG_START(msg) + msg->first_line.len, 
00221                    msg->len - msg->first_line.len);
00222         new_msg[len] = 0;
00223         pkg_free(via);
00224         *new_msg_len = len;
00225         return new_msg;
00226 }
00227 
00228 
00232 static int xhttp_process_request(sip_msg_t* orig_msg, 
00233                                                           char* new_buf, unsigned int new_len)
00234 {
00235         int ret;
00236         sip_msg_t tmp_msg, *msg;
00237         struct run_act_ctx ra_ctx;
00238         
00239         ret=0;
00240         if (new_buf && new_len)
00241         {
00242                 memset(&tmp_msg, 0, sizeof(sip_msg_t));
00243                 tmp_msg.buf = new_buf;
00244                 tmp_msg.len = new_len;
00245                 tmp_msg.rcv = orig_msg->rcv;
00246                 tmp_msg.id = orig_msg->id;
00247                 tmp_msg.set_global_address = orig_msg->set_global_address;
00248                 tmp_msg.set_global_port = orig_msg->set_global_port;
00249                 if (parse_msg(new_buf, new_len, &tmp_msg) != 0)
00250                 {
00251                         LM_ERR("parse_msg failed\n");
00252                         goto error;
00253                 }
00254                 msg = &tmp_msg;
00255         } else {
00256                 msg = orig_msg;
00257         }
00258         
00259         if ((msg->first_line.type != SIP_REQUEST) || (msg->via1 == 0) ||
00260                 (msg->via1->error != PARSE_OK))
00261         {
00262                 LM_CRIT("strange message: %.*s\n", msg->len, msg->buf);
00263                 goto error;
00264         }
00265         if (exec_pre_script_cb(msg, REQUEST_CB_TYPE) == 0)
00266         {
00267                 goto done;
00268         }
00269 
00270         init_run_actions_ctx(&ra_ctx);
00271         if (run_actions(&ra_ctx, event_rt.rlist[xhttp_route_no], msg) < 0)
00272         {
00273                 ret=-1;
00274                 LM_DBG("error while trying script\n");
00275                 goto done;
00276         }
00277 
00278 done:
00279         exec_post_script_cb(msg, REQUEST_CB_TYPE);
00280         if (msg != orig_msg)
00281         {
00282                 free_sip_msg(msg);
00283         }
00284         return ret;
00285 
00286 error:
00287         return -1;
00288 }
00289 
00290 
00294 static int xhttp_handler(sip_msg_t* msg)
00295 {
00296         int ret;
00297         char* fake_msg;
00298         int fake_msg_len;
00299         regmatch_t pmatch;
00300         char c;
00301 
00302         ret=NONSIP_MSG_DROP;
00303 
00304         if(!IS_HTTP(msg))
00305         {
00306                 /* oly http msg type */
00307                 return NONSIP_MSG_PASS;
00308         }
00309 
00310         if(xhttp_url_skip!=NULL || xhttp_url_match!=NULL)
00311         {
00312                 c = msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len];
00313                 msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]
00314                         = '\0';
00315                 if (xhttp_url_skip!=NULL &&
00316                         regexec(&xhttp_url_skip_regexp, msg->first_line.u.request.uri.s,
00317                                         1, &pmatch, 0)==0)
00318                 {
00319                         LM_DBG("URL matched skip re\n");
00320                         msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]
00321                                 = c;
00322                         return NONSIP_MSG_PASS;
00323                 }
00324                 if (xhttp_url_match!=NULL &&
00325                         regexec(&xhttp_url_match_regexp, msg->first_line.u.request.uri.s,
00326                                         1, &pmatch, 0)!=0)
00327                 {
00328                         LM_DBG("URL not matched\n");
00329                         msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]
00330                                 = c;
00331                         return NONSIP_MSG_PASS;
00332                 }
00333                 msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] = c;
00334         }
00335 
00336         if (msg->via1 == 0)
00337         {
00338                 fake_msg = xhttp_to_sip(msg, &fake_msg_len);
00339                 if (fake_msg == 0)
00340                 {
00341                         LM_ERR("out of memory\n");
00342                         ret=NONSIP_MSG_ERROR;
00343                 } else {
00344                         DBG("new fake msg created (%d bytes):\n<%.*s>\n",
00345                                         fake_msg_len, fake_msg_len, fake_msg);
00346                         if (xhttp_process_request(msg, fake_msg, fake_msg_len)<0)
00347                                 ret=NONSIP_MSG_ERROR;
00348                                 pkg_free(fake_msg);
00349                         }
00350                         return ret;
00351         } else {
00352                 LM_DBG("http msg unchanged (%d bytes):\n<%.*s>\n",
00353                                 msg->len, msg->len, msg->buf);
00354                 if (xhttp_process_request(msg, 0, 0)<0)
00355                         ret=NONSIP_MSG_ERROR;
00356                 return ret;
00357         }
00358 }
00359 
00360 
00364 static int xhttp_send_reply(sip_msg_t *msg, int code, str *reason,
00365                 str *ctype, str *body)
00366 {
00367         str tbuf;
00368 
00369         if(ctype!=NULL && ctype->len>0)
00370         {
00371                 /* add content-type */
00372                 tbuf.len=sizeof("Content-Type: ") - 1 + ctype->len + CRLF_LEN;
00373                 tbuf.s=pkg_malloc(sizeof(char)*(tbuf.len));
00374 
00375                 if (tbuf.len==0)
00376                 {
00377                         LM_ERR("out of pkg memory\n");
00378                         return -1;
00379                 }
00380                 memcpy(tbuf.s, "Content-Type: ", sizeof("Content-Type: ") - 1);
00381                 memcpy(tbuf.s+sizeof("Content-Type: ") - 1, ctype->s, ctype->len);
00382                 memcpy(tbuf.s+sizeof("Content-Type: ") - 1 + ctype->len,
00383                                 CRLF, CRLF_LEN);
00384                 if (add_lump_rpl(msg, tbuf.s, tbuf.len, LUMP_RPL_HDR) == 0)
00385                 {
00386                         LM_ERR("failed to insert content-type lump\n");
00387                         pkg_free(tbuf.s);
00388                         return -1;
00389                 }
00390                 pkg_free(tbuf.s);
00391         }
00392 
00393         if(body!=NULL && body->len>0)
00394         {
00395                 if (add_lump_rpl(msg, body->s, body->len, LUMP_RPL_BODY) < 0)
00396                 {
00397                         LM_ERR("Error while adding reply lump\n");
00398                         return -1;
00399                 }
00400         }
00401         if (slb.freply(msg, code, reason) < 0)
00402         {
00403                 LM_ERR("Error while sending reply\n");
00404                 return -1;
00405         }
00406         return 0;
00407 }
00408 
00412 static int w_xhttp_send_reply(sip_msg_t* msg, char* pcode, char* preason,
00413                 char *pctype, char* pbody)
00414 {
00415         str body = {0, 0};
00416         str reason = {"OK", 2};
00417         str ctype = {"text/plain", 10};
00418         int code = 200;
00419 
00420         if(pcode==0 || preason==0 || pctype==0 || pbody==0)
00421         {
00422                 LM_ERR("invalid parameters\n");
00423                 return -1;
00424         }
00425 
00426         if(fixup_get_ivalue(msg, (gparam_p)pcode, &code)!=0)
00427         {
00428                 LM_ERR("no reply code value\n");
00429                 return -1;
00430         }
00431         if(code<100 || code>700)
00432         {
00433                 LM_ERR("invalid code parameter\n");
00434                 return -1;
00435         }
00436 
00437         if(fixup_get_svalue(msg, (gparam_p)preason, &reason)!=0)
00438         {
00439                 LM_ERR("unable to get reason\n");
00440                 return -1;
00441         }
00442         if(reason.s==NULL || reason.len == 0)
00443         {
00444                 LM_ERR("invalid reason parameter\n");
00445                 return -1;
00446         }
00447 
00448         if(fixup_get_svalue(msg, (gparam_p)pctype, &ctype)!=0)
00449         {
00450                 LM_ERR("unable to get content type\n");
00451                 return -1;
00452         }
00453         if(ctype.s==NULL)
00454         {
00455                 LM_ERR("invalid content-type parameter\n");
00456                 return -1;
00457         }
00458 
00459         if(fixup_get_svalue(msg, (gparam_p)pbody, &body)!=0)
00460         {
00461                 LM_ERR("unable to get body\n");
00462                 return -1;
00463         }
00464         if(body.s==NULL)
00465         {
00466                 LM_ERR("invalid body parameter\n");
00467                 return -1;
00468         }
00469 
00470         if(xhttp_send_reply(msg, code, &reason, &ctype, &body)<0)
00471                 return -1;
00472         return 1;
00473 }
00474 
00475 
00479 static int fixup_xhttp_reply(void** param, int param_no)
00480 {
00481         if (param_no == 1) {
00482             return fixup_igp_null(param, 1);
00483         } else if (param_no == 2) {
00484             return fixup_spve_null(param, 1);
00485         } else if (param_no == 3) {
00486             return fixup_spve_null(param, 1);
00487         } else if (param_no == 4) {
00488             return fixup_spve_null(param, 1);
00489         }
00490         return 0;
00491 }
00492 
00496 int bind_xhttp(xhttp_api_t* api)
00497 {
00498         if (!api) {
00499                 ERR("Invalid parameter value\n");
00500                 return -1;
00501         }
00502         api->reply = xhttp_send_reply;
00503         return 0;
00504 }
00505