exec.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. *
  3. * $Id$
  4. *
  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-02-28 scratchpad compatibility abandoned (jiri)
  27. * 2003-01-28 scratchpad removed
  28. * 2004-07-21 rewrite uri done via action() (bogdan)
  29. */
  30. #include <stdio.h>
  31. #include <strings.h>
  32. #include <errno.h>
  33. #include <stdlib.h>
  34. #include <sys/types.h>
  35. /*
  36. #include <sys/resource.h>
  37. */
  38. #include <sys/wait.h>
  39. #include "../../mem/mem.h"
  40. #include "../../error.h"
  41. #include "../../config.h"
  42. #include "../../parser/msg_parser.h"
  43. #include "../../dprint.h"
  44. #include "../../dset.h"
  45. #include "../../action.h"
  46. #include "../../usr_avp.h"
  47. #include "exec.h"
  48. int exec_msg(struct sip_msg *msg, char *cmd )
  49. {
  50. FILE *pipe;
  51. int exit_status;
  52. int ret;
  53. ret=-1; /* pessimist: assume error */
  54. pipe=popen( cmd, "w" );
  55. if (pipe==NULL) {
  56. LM_ERR("cannot open pipe: %s\n", cmd);
  57. ser_error=E_EXEC;
  58. return -1;
  59. }
  60. if (fwrite(msg->buf, 1, msg->len, pipe)!=msg->len) {
  61. LM_ERR("failed to write to pipe\n");
  62. ser_error=E_EXEC;
  63. goto error01;
  64. }
  65. /* success */
  66. ret=1;
  67. error01:
  68. if (ferror(pipe)) {
  69. LM_ERR("pipe: %s\n", strerror(errno));
  70. ser_error=E_EXEC;
  71. ret=-1;
  72. }
  73. exit_status=pclose(pipe);
  74. if (WIFEXITED(exit_status)) { /* exited properly .... */
  75. /* return false if script exited with non-zero status */
  76. if (WEXITSTATUS(exit_status)!=0) ret=-1;
  77. } else { /* exited erroneously */
  78. LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n",
  79. cmd, exit_status, errno, strerror(errno) );
  80. ret=-1;
  81. }
  82. return ret;
  83. }
  84. int exec_str(struct sip_msg *msg, char *cmd, char *param, int param_len) {
  85. struct action act;
  86. struct run_act_ctx ra_ctx;
  87. int cmd_len;
  88. FILE *pipe;
  89. char *cmd_line;
  90. int ret;
  91. int l1;
  92. static char uri_line[MAX_URI_SIZE+1];
  93. int uri_cnt;
  94. str uri;
  95. int exit_status;
  96. /* pessimist: assume error by default */
  97. ret=-1;
  98. l1=strlen(cmd);
  99. if(param_len>0)
  100. cmd_len=l1+param_len+4;
  101. else
  102. cmd_len=l1+1;
  103. cmd_line=pkg_malloc(cmd_len);
  104. if (cmd_line==0) {
  105. ret=ser_error=E_OUT_OF_MEM;
  106. LM_ERR("no pkg mem for command\n");
  107. goto error00;
  108. }
  109. /* 'command parameter \0' */
  110. memcpy(cmd_line, cmd, l1);
  111. if(param_len>0)
  112. {
  113. cmd_line[l1]=' ';
  114. cmd_line[l1+1]='\'';
  115. memcpy(cmd_line+l1+2, param, param_len);
  116. cmd_line[l1+param_len+2]='\'';
  117. cmd_line[l1+param_len+3]=0;
  118. } else {
  119. cmd_line[l1] = 0;
  120. }
  121. pipe=popen( cmd_line, "r" );
  122. if (pipe==NULL) {
  123. LM_ERR("cannot open pipe: %s\n", cmd_line);
  124. ser_error=E_EXEC;
  125. goto error01;
  126. }
  127. /* read now line by line */
  128. uri_cnt=0;
  129. while( fgets(uri_line, MAX_URI_SIZE, pipe)!=NULL){
  130. uri.s = uri_line;
  131. uri.len=strlen(uri.s);
  132. /* trim from right */
  133. while(uri.len && (uri.s[uri.len-1]=='\r'
  134. || uri.s[uri.len-1]=='\n'
  135. || uri.s[uri.len-1]=='\t'
  136. || uri.s[uri.len-1]==' ' )) {
  137. LM_DBG("rtrim\n");
  138. uri.len--;
  139. }
  140. /* skip empty line */
  141. if (uri.len==0) continue;
  142. /* ZT */
  143. uri.s[uri.len]=0;
  144. if (uri_cnt==0) {
  145. memset(&act, 0, sizeof(act));
  146. act.type = SET_URI_T;
  147. act.val[0].type = STRING_ST;
  148. act.val[0].u.string = uri.s;
  149. init_run_actions_ctx(&ra_ctx);
  150. if (do_action(&ra_ctx, &act, msg)<0) {
  151. LM_ERR("the action for has failed\n");
  152. ser_error=E_OUT_OF_MEM;
  153. goto error02;
  154. }
  155. } else {
  156. if (append_branch(msg, &uri, 0, 0, Q_UNSPECIFIED, 0, 0,
  157. 0, 0, 0, 0) == -1) {
  158. LM_ERR("append_branch failed; too many or too long URIs?\n");
  159. goto error02;
  160. }
  161. }
  162. uri_cnt++;
  163. }
  164. if (uri_cnt==0) {
  165. LM_ERR("no uri from %s\n", cmd_line );
  166. goto error02;
  167. }
  168. /* success */
  169. ret=1;
  170. error02:
  171. if (ferror(pipe)) {
  172. LM_ERR("in pipe: %s\n", strerror(errno));
  173. ser_error=E_EXEC;
  174. ret=-1;
  175. }
  176. exit_status=pclose(pipe);
  177. if (WIFEXITED(exit_status)) { /* exited properly .... */
  178. /* return false if script exited with non-zero status */
  179. if (WEXITSTATUS(exit_status)!=0) ret=-1;
  180. } else { /* exited erroneously */
  181. LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n",
  182. cmd, exit_status, errno, strerror(errno) );
  183. ret=-1;
  184. }
  185. error01:
  186. pkg_free(cmd_line);
  187. error00:
  188. return ret;
  189. }
  190. int exec_avp(struct sip_msg *msg, char *cmd, pvname_list_p avpl)
  191. {
  192. int_str avp_val;
  193. int_str avp_name;
  194. unsigned short avp_type;
  195. FILE *pipe;
  196. int ret;
  197. char res_line[MAX_URI_SIZE+1];
  198. str res;
  199. int exit_status;
  200. int i;
  201. pvname_list_t* crt;
  202. /* pessimist: assume error by default */
  203. ret=-1;
  204. pipe=popen( cmd, "r" );
  205. if (pipe==NULL) {
  206. LM_ERR("cannot open pipe: %s\n", cmd);
  207. ser_error=E_EXEC;
  208. return ret;
  209. }
  210. /* read now line by line */
  211. i=0;
  212. crt = avpl;
  213. while( fgets(res_line, MAX_URI_SIZE, pipe)!=NULL){
  214. res.s = res_line;
  215. res.len=strlen(res.s);
  216. /* trim from right */
  217. while(res.len && (res.s[res.len-1]=='\r'
  218. || res.s[res.len-1]=='\n'
  219. || res.s[res.len-1]=='\t'
  220. || res.s[res.len-1]==' ' )) {
  221. res.len--;
  222. }
  223. /* skip empty line */
  224. if (res.len==0) continue;
  225. /* ZT */
  226. res.s[res.len]=0;
  227. avp_type = 0;
  228. if(crt==NULL)
  229. {
  230. avp_name.n = i+1;
  231. } else {
  232. if(pv_get_avp_name(msg, &(crt->sname.pvp), &avp_name, &avp_type)!=0)
  233. {
  234. LM_ERR("can't get item name [%d]\n",i);
  235. goto error;
  236. }
  237. }
  238. avp_type |= AVP_VAL_STR;
  239. avp_val.s = res;
  240. if(add_avp(avp_type, avp_name, avp_val)!=0)
  241. {
  242. LM_ERR("unable to add avp\n");
  243. goto error;
  244. }
  245. if(crt)
  246. crt = crt->next;
  247. i++;
  248. }
  249. if (i==0)
  250. LM_DBG("no result from %s\n", cmd);
  251. /* success */
  252. ret=1;
  253. error:
  254. if (ferror(pipe)) {
  255. LM_ERR("pipe: %d/%s\n", errno, strerror(errno));
  256. ser_error=E_EXEC;
  257. ret=-1;
  258. }
  259. exit_status=pclose(pipe);
  260. if (WIFEXITED(exit_status)) { /* exited properly .... */
  261. /* return false if script exited with non-zero status */
  262. if (WEXITSTATUS(exit_status)!=0) ret=-1;
  263. } else { /* exited erroneously */
  264. LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n",
  265. cmd, exit_status, errno, strerror(errno) );
  266. ret=-1;
  267. }
  268. return ret;
  269. }