exec_mod.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * execution module
  3. *
  4. * $Id$
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser 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. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. * History:
  30. * -------
  31. * 2003-03-11: New module interface (janakj)
  32. * 2003-03-16: flags export parameter added (janakj)
  33. */
  34. #include <stdio.h>
  35. #include "../../parser/msg_parser.h"
  36. #include "../../str.h"
  37. #include "../../sr_module.h"
  38. #include "../../dprint.h"
  39. #include "../../parser/parse_uri.h"
  40. #include "exec.h"
  41. #include "kill.h"
  42. #include "exec_hf.h"
  43. MODULE_VERSION
  44. unsigned int time_to_kill=0;
  45. static int mod_init( void );
  46. inline static int w_exec_dset(struct sip_msg* msg, char* cmd, char* foo);
  47. inline static int w_exec_msg(struct sip_msg* msg, char* cmd, char* foo);
  48. inline static void exec_shutdown();
  49. /*
  50. * Exported functions
  51. */
  52. static cmd_export_t cmds[] = {
  53. {"exec_dset", w_exec_dset, 1, fixup_var_str_1, REQUEST_ROUTE | FAILURE_ROUTE},
  54. {"exec_msg", w_exec_msg, 1, fixup_var_str_1, REQUEST_ROUTE | FAILURE_ROUTE},
  55. {0, 0, 0, 0, 0}
  56. };
  57. /*
  58. * Exported parameters
  59. */
  60. static param_export_t params[] = {
  61. {"time_to_kill", PARAM_INT, &time_to_kill},
  62. {"setvars", PARAM_INT, &setvars },
  63. {0, 0, 0}
  64. };
  65. #ifdef STATIC_EXEC
  66. struct module_exports exec_exports = {
  67. #else
  68. struct module_exports exports= {
  69. #endif
  70. "exec",
  71. cmds, /* Exported functions */
  72. 0, /* RPC methods */
  73. params, /* Exported parameters */
  74. mod_init, /* initialization module */
  75. 0, /* response function */
  76. exec_shutdown, /* destroy function */
  77. 0, /* oncancel function */
  78. 0 /* per-child init function */
  79. };
  80. void exec_shutdown()
  81. {
  82. if (time_to_kill) destroy_kill();
  83. }
  84. static int mod_init( void )
  85. {
  86. fprintf( stderr, "exec - initializing\n");
  87. if (time_to_kill) initialize_kill();
  88. return 0;
  89. }
  90. inline static int w_exec_dset(struct sip_msg* msg, char* p1, char* foo)
  91. {
  92. str cmd;
  93. str *uri;
  94. environment_t *backup;
  95. int ret;
  96. if (get_str_fparam(&cmd, msg, (fparam_t*)p1) < 0) {
  97. ERR("Error while obtaining command name\n");
  98. return -1;
  99. }
  100. backup=0;
  101. if (setvars) {
  102. backup=set_env(msg);
  103. if (!backup) {
  104. LOG(L_ERR, "ERROR: w_exec_msg: no env created\n");
  105. return -1;
  106. }
  107. }
  108. if (msg->new_uri.s && msg->new_uri.len)
  109. uri=&msg->new_uri;
  110. else
  111. uri=&msg->first_line.u.request.uri;
  112. ret=exec_str(msg, &cmd, uri->s, uri->len);
  113. if (setvars) {
  114. unset_env(backup);
  115. }
  116. return ret;
  117. }
  118. inline static int w_exec_msg(struct sip_msg* msg, char* p1, char* foo)
  119. {
  120. str cmd;
  121. environment_t *backup;
  122. int ret;
  123. if (get_str_fparam(&cmd, msg, (fparam_t*)p1) < 0) {
  124. ERR("Error while obtaining command name\n");
  125. return -1;
  126. }
  127. backup=0;
  128. if (setvars) {
  129. backup=set_env(msg);
  130. if (!backup) {
  131. LOG(L_ERR, "ERROR: w_exec_msg: no env created\n");
  132. return -1;
  133. }
  134. }
  135. ret=exec_msg(msg, &cmd);
  136. if (setvars) {
  137. unset_env(backup);
  138. }
  139. return ret;
  140. }