exec_mod.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * execution module
  3. *
  4. * $Id$
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * -------
  26. * 2003-03-11: New module interface (janakj)
  27. * 2003-03-16: flags export parameter added (janakj)
  28. */
  29. #include <stdio.h>
  30. #include "../../parser/msg_parser.h"
  31. #include "../../str.h"
  32. #include "../../sr_module.h"
  33. #include "../../dprint.h"
  34. #include "../../mod_fix.h"
  35. #include "../../parser/parse_uri.h"
  36. #include "exec.h"
  37. #include "kill.h"
  38. #include "exec_hf.h"
  39. MODULE_VERSION
  40. unsigned int time_to_kill=0;
  41. int exec_bash_safety=1;
  42. static int mod_init( void );
  43. inline static int w_exec_dset(struct sip_msg* msg, char* cmd, char* foo);
  44. inline static int w_exec_msg(struct sip_msg* msg, char* cmd, char* foo);
  45. inline static int w_exec_avp(struct sip_msg* msg, char* cmd, char* avpl);
  46. static int exec_avp_fixup(void** param, int param_no);
  47. inline static void exec_shutdown(void);
  48. /*
  49. * Exported functions
  50. */
  51. static cmd_export_t cmds[] = {
  52. {"exec_dset", (cmd_function)w_exec_dset, 1, fixup_spve_null, 0,
  53. REQUEST_ROUTE|FAILURE_ROUTE},
  54. {"exec_msg", (cmd_function)w_exec_msg, 1, fixup_spve_null, 0,
  55. REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
  56. {"exec_avp", (cmd_function)w_exec_avp, 1, fixup_spve_null, 0,
  57. REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
  58. {"exec_avp", (cmd_function)w_exec_avp, 2, exec_avp_fixup, 0,
  59. REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
  60. {0, 0, 0, 0, 0, 0}
  61. };
  62. /*
  63. * Exported parameters
  64. */
  65. static param_export_t params[] = {
  66. {"time_to_kill", INT_PARAM, &time_to_kill},
  67. {"setvars", INT_PARAM, &setvars },
  68. {"bash_safety", INT_PARAM, &exec_bash_safety },
  69. {0, 0, 0}
  70. };
  71. struct module_exports exports= {
  72. "exec",
  73. DEFAULT_DLFLAGS,/* dlopen flags */
  74. cmds, /* Exported functions */
  75. params, /* Exported parameters */
  76. 0, /* exported statistics */
  77. 0, /* exported MI functions */
  78. 0, /* exported pseudo-variables */
  79. 0, /* extra processes */
  80. mod_init, /* initialization module */
  81. 0, /* response function */
  82. exec_shutdown, /* destroy function */
  83. 0 /* per-child init function */
  84. };
  85. void exec_shutdown(void)
  86. {
  87. if (time_to_kill) destroy_kill();
  88. }
  89. static int mod_init( void )
  90. {
  91. if (time_to_kill) initialize_kill();
  92. return 0;
  93. }
  94. inline static int w_exec_dset(struct sip_msg* msg, char* cmd, char* foo)
  95. {
  96. str *uri;
  97. environment_t *backup;
  98. int ret;
  99. str command;
  100. if(msg==0 || cmd==0)
  101. return -1;
  102. backup=0;
  103. if (setvars) {
  104. backup=set_env(msg);
  105. if (!backup) {
  106. LM_ERR("no env created\n");
  107. return -1;
  108. }
  109. }
  110. if (msg->new_uri.s && msg->new_uri.len)
  111. uri=&msg->new_uri;
  112. else
  113. uri=&msg->first_line.u.request.uri;
  114. if(fixup_get_svalue(msg, (gparam_p)cmd, &command)!=0)
  115. {
  116. LM_ERR("invalid command parameter");
  117. return -1;
  118. }
  119. LM_DBG("executing [%s]\n", command.s);
  120. ret=exec_str(msg, command.s, uri->s, uri->len);
  121. if (setvars) {
  122. unset_env(backup);
  123. }
  124. return ret;
  125. }
  126. inline static int w_exec_msg(struct sip_msg* msg, char* cmd, char* foo)
  127. {
  128. environment_t *backup;
  129. int ret;
  130. str command;
  131. if(msg==0 || cmd==0)
  132. return -1;
  133. backup=0;
  134. if (setvars) {
  135. backup=set_env(msg);
  136. if (!backup) {
  137. LM_ERR("no env created\n");
  138. return -1;
  139. }
  140. }
  141. if(fixup_get_svalue(msg, (gparam_p)cmd, &command)!=0)
  142. {
  143. LM_ERR("invalid command parameter");
  144. return -1;
  145. }
  146. LM_DBG("executing [%s]\n", command.s);
  147. ret=exec_msg(msg, command.s);
  148. if (setvars) {
  149. unset_env(backup);
  150. }
  151. return ret;
  152. }
  153. inline static int w_exec_avp(struct sip_msg* msg, char* cmd, char* avpl)
  154. {
  155. environment_t *backup;
  156. int ret;
  157. str command;
  158. if(msg==0 || cmd==0)
  159. return -1;
  160. backup=0;
  161. if (setvars) {
  162. backup=set_env(msg);
  163. if (!backup) {
  164. LM_ERR("no env created\n");
  165. return -1;
  166. }
  167. }
  168. if(fixup_get_svalue(msg, (gparam_p)cmd, &command)!=0)
  169. {
  170. LM_ERR("invalid command parameter");
  171. return -1;
  172. }
  173. LM_DBG("executing [%s]\n", command.s);
  174. ret=exec_avp(msg, command.s, (pvname_list_p)avpl);
  175. if (setvars) {
  176. unset_env(backup);
  177. }
  178. return ret;
  179. }
  180. static int exec_avp_fixup(void** param, int param_no)
  181. {
  182. pvname_list_t *anlist = NULL;
  183. str s;
  184. s.s = (char*)(*param);
  185. if (param_no==1)
  186. {
  187. if(s.s==NULL)
  188. {
  189. LM_ERR("null format in P%d\n", param_no);
  190. return E_UNSPEC;
  191. }
  192. return fixup_spve_null(param, 1);
  193. } else if(param_no==2) {
  194. if(s.s==NULL)
  195. {
  196. LM_ERR("null format in P%d\n", param_no);
  197. return E_UNSPEC;
  198. }
  199. s.len = strlen(s.s);
  200. anlist = parse_pvname_list(&s, PVT_AVP);
  201. if(anlist==NULL)
  202. {
  203. LM_ERR("bad format in P%d [%s]\n", param_no, s.s);
  204. return E_UNSPEC;
  205. }
  206. *param = (void*)anlist;
  207. return 0;
  208. }
  209. return 0;
  210. }