modules_k/exec/exec_mod.c

00001 /*
00002  * execution module
00003  *
00004  * $Id$
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * -------
00026  * 2003-03-11: New module interface (janakj)
00027  * 2003-03-16: flags export parameter added (janakj)
00028  */
00029 
00030 
00031 #include <stdio.h>
00032 
00033 #include "../../parser/msg_parser.h"
00034 #include "../../str.h"
00035 #include "../../sr_module.h"
00036 #include "../../dprint.h"
00037 #include "../../mod_fix.h"
00038 #include "../../parser/parse_uri.h"
00039 
00040 #include "exec.h"
00041 #include "kill.h"
00042 #include "exec_hf.h"
00043 
00044 MODULE_VERSION
00045 
00046 unsigned int time_to_kill=0;
00047 
00048 static int mod_init( void );
00049 
00050 inline static int w_exec_dset(struct sip_msg* msg, char* cmd, char* foo);
00051 inline static int w_exec_msg(struct sip_msg* msg, char* cmd, char* foo);
00052 inline static int w_exec_avp(struct sip_msg* msg, char* cmd, char* avpl);
00053 
00054 static int exec_avp_fixup(void** param, int param_no);
00055 
00056 inline static void exec_shutdown(void);
00057 
00058 /*
00059  * Exported functions
00060  */
00061 static cmd_export_t cmds[] = {
00062         {"exec_dset", (cmd_function)w_exec_dset, 1, fixup_spve_null,  0,
00063                 REQUEST_ROUTE|FAILURE_ROUTE},
00064         {"exec_msg",  (cmd_function)w_exec_msg,  1, fixup_spve_null,  0,
00065                 REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
00066         {"exec_avp",  (cmd_function)w_exec_avp,  1, fixup_spve_null,  0,
00067                 REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
00068         {"exec_avp",  (cmd_function)w_exec_avp,  2, exec_avp_fixup, 0,
00069                 REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
00070         {0, 0, 0, 0, 0, 0}
00071 };
00072 
00073 
00074 /*
00075  * Exported parameters
00076  */
00077 static param_export_t params[] = {
00078         {"time_to_kill", INT_PARAM, &time_to_kill},
00079         {"setvars",      INT_PARAM, &setvars     },
00080         {0, 0, 0}
00081 };
00082 
00083 
00084 struct module_exports exports= {
00085         "exec",
00086         DEFAULT_DLFLAGS,/* dlopen flags */
00087         cmds,           /* Exported functions */
00088         params,         /* Exported parameters */
00089         0,              /* exported statistics */
00090         0,              /* exported MI functions */
00091         0,              /* exported pseudo-variables */
00092         0,              /* extra processes */
00093         mod_init,       /* initialization module */
00094         0,              /* response function */
00095         exec_shutdown,  /* destroy function */
00096         0               /* per-child init function */
00097 };
00098 
00099 void exec_shutdown(void)
00100 {
00101         if (time_to_kill) destroy_kill();
00102 }
00103 
00104 
00105 static int mod_init( void )
00106 {
00107         if (time_to_kill) initialize_kill();
00108         return 0;
00109 }
00110 
00111 inline static int w_exec_dset(struct sip_msg* msg, char* cmd, char* foo)
00112 {
00113         str *uri;
00114         environment_t *backup;
00115         int ret;
00116         str command;
00117         
00118         if(msg==0 || cmd==0)
00119                 return -1;
00120         
00121         backup=0;
00122         if (setvars) {
00123                 backup=set_env(msg);
00124                 if (!backup) {
00125                         LM_ERR("no env created\n");
00126                         return -1;
00127                 }
00128         }
00129 
00130         if (msg->new_uri.s && msg->new_uri.len)
00131                 uri=&msg->new_uri;
00132         else
00133                 uri=&msg->first_line.u.request.uri;
00134         
00135         if(fixup_get_svalue(msg, (gparam_p)cmd, &command)!=0)
00136         {
00137                 LM_ERR("invalid command parameter");
00138                 return -1;
00139         }
00140         
00141         LM_DBG("executing [%s]\n", command.s);
00142 
00143         ret=exec_str(msg, command.s, uri->s, uri->len);
00144         if (setvars) {
00145                 unset_env(backup);
00146         }
00147         return ret;
00148 }
00149 
00150 
00151 inline static int w_exec_msg(struct sip_msg* msg, char* cmd, char* foo)
00152 {
00153         environment_t *backup;
00154         int ret;
00155         str command;
00156         
00157         if(msg==0 || cmd==0)
00158                 return -1;
00159 
00160         backup=0;
00161         if (setvars) {
00162                 backup=set_env(msg);
00163                 if (!backup) {
00164                         LM_ERR("no env created\n");
00165                         return -1;
00166                 }
00167         }
00168         
00169         if(fixup_get_svalue(msg, (gparam_p)cmd, &command)!=0)
00170         {
00171                 LM_ERR("invalid command parameter");
00172                 return -1;
00173         }
00174         
00175         LM_DBG("executing [%s]\n", command.s);
00176         
00177         ret=exec_msg(msg, command.s);
00178         if (setvars) {
00179                 unset_env(backup);
00180         }
00181         return ret;
00182 }
00183 
00184 inline static int w_exec_avp(struct sip_msg* msg, char* cmd, char* avpl)
00185 {
00186         environment_t *backup;
00187         int ret;
00188         str command;
00189         
00190         if(msg==0 || cmd==0)
00191                 return -1;
00192         
00193         backup=0;
00194         if (setvars) {
00195                 backup=set_env(msg);
00196                 if (!backup) {
00197                         LM_ERR("no env created\n");
00198                         return -1;
00199                 }
00200         }
00201 
00202         if(fixup_get_svalue(msg, (gparam_p)cmd, &command)!=0)
00203         {
00204                 LM_ERR("invalid command parameter");
00205                 return -1;
00206         }
00207         
00208         LM_DBG("executing [%s]\n", command.s);
00209 
00210         ret=exec_avp(msg, command.s, (pvname_list_p)avpl);
00211         if (setvars) {
00212                 unset_env(backup);
00213         }
00214         return ret;
00215 }
00216 
00217 static int exec_avp_fixup(void** param, int param_no)
00218 {
00219         pvname_list_t *anlist = NULL;
00220         str s;
00221 
00222         s.s = (char*)(*param);
00223         if (param_no==1)
00224         {
00225                 if(s.s==NULL)
00226                 {
00227                         LM_ERR("null format in P%d\n", param_no);
00228                         return E_UNSPEC;
00229                 }
00230                 return fixup_spve_null(param, 1);
00231         } else if(param_no==2) {
00232                 if(s.s==NULL)
00233                 {
00234                         LM_ERR("null format in P%d\n", param_no);
00235                         return E_UNSPEC;
00236                 }
00237                 s.len =  strlen(s.s);
00238                 anlist = parse_pvname_list(&s, PVT_AVP);
00239                 if(anlist==NULL)
00240                 {
00241                         LM_ERR("bad format in P%d [%s]\n", param_no, s.s);
00242                         return E_UNSPEC;
00243                 }
00244                 *param = (void*)anlist;
00245                 return 0;
00246         }
00247 
00248         return 0;
00249 }
00250 
00251