action.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 Fhg Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include "action.h"
  28. #include "config.h"
  29. #include "error.h"
  30. #include "dprint.h"
  31. #include "proxy.h"
  32. #include "forward.h"
  33. #include "udp_server.h"
  34. #include "route.h"
  35. #include "parser/msg_parser.h"
  36. #include "parser/parse_uri.h"
  37. #include "ut.h"
  38. #include "sr_module.h"
  39. #include "mem/mem.h"
  40. #include "globals.h"
  41. #include "dset.h"
  42. #ifdef USE_TCP
  43. #include "tcp_server.h"
  44. #endif
  45. #include <sys/types.h>
  46. #include <sys/socket.h>
  47. #include <netdb.h>
  48. #include <stdlib.h>
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include <string.h>
  52. #ifdef DEBUG_DMALLOC
  53. #include <dmalloc.h>
  54. #endif
  55. /* ret= 0! if action -> end of list(e.g DROP),
  56. > 0 to continue processing next actions
  57. and <0 on error */
  58. int do_action(struct action* a, struct sip_msg* msg)
  59. {
  60. int ret;
  61. int v;
  62. union sockaddr_union* to;
  63. struct socket_info* send_sock;
  64. struct proxy_l* p;
  65. char* tmp;
  66. char *new_uri, *end, *crt;
  67. int len;
  68. int user;
  69. struct sip_uri uri;
  70. struct sip_uri* u;
  71. unsigned short port;
  72. int proto;
  73. /* reset the value of error to E_UNSPEC so avoid unknowledgable
  74. functions to return with errror (status<0) and not setting it
  75. leaving there previous error; cache the previous value though
  76. for functions which want to process it */
  77. prev_ser_error=ser_error;
  78. ser_error=E_UNSPEC;
  79. ret=E_BUG;
  80. switch ((unsigned char)a->type){
  81. case DROP_T:
  82. ret=0;
  83. break;
  84. case FORWARD_T:
  85. #ifdef USE_TCP
  86. case FORWARD_TCP_T:
  87. #endif
  88. case FORWARD_UDP_T:
  89. if (a->type==FORWARD_UDP_T) proto=PROTO_UDP;
  90. #ifdef USE_TCP
  91. else if (a->type==FORWARD_TCP_T) proto= PROTO_TCP;
  92. #endif
  93. else proto=msg->rcv.proto;
  94. if (a->p1_type==URIHOST_ST){
  95. /*parse uri*/
  96. ret=parse_sip_msg_uri(msg);
  97. if (ret<0) {
  98. LOG(L_ERR, "ERROR: do_action: forward: bad_uri "
  99. " dropping packet\n");
  100. break;
  101. }
  102. u=&msg->parsed_uri;
  103. switch (a->p2_type){
  104. case URIPORT_ST:
  105. port=u->port_no;
  106. break;
  107. case NUMBER_ST:
  108. port=a->p2.number;
  109. break;
  110. default:
  111. LOG(L_CRIT, "BUG: do_action bad forward 2nd"
  112. " param type (%d)\n", a->p2_type);
  113. ret=E_UNSPEC;
  114. goto error_fwd_uri;
  115. }
  116. /* create a temporary proxy*/
  117. p=mk_proxy(&u->host, port);
  118. if (p==0){
  119. LOG(L_ERR, "ERROR: bad host name in uri,"
  120. " dropping packet\n");
  121. ret=E_BAD_ADDRESS;
  122. goto error_fwd_uri;
  123. }
  124. ret=forward_request(msg, p, proto);
  125. /*free_uri(&uri); -- no longer needed, in sip_msg*/
  126. free_proxy(p); /* frees only p content, not p itself */
  127. free(p);
  128. if (ret>=0) ret=1;
  129. }else if ((a->p1_type==PROXY_ST) && (a->p2_type==NUMBER_ST)){
  130. ret=forward_request(msg,(struct proxy_l*)a->p1.data, proto);
  131. if (ret>=0) ret=1;
  132. }else{
  133. LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
  134. a->p1_type, a->p2_type);
  135. ret=E_BUG;
  136. }
  137. break;
  138. case SEND_T:
  139. case SEND_TCP_T:
  140. if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){
  141. LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
  142. a->p1_type, a->p2_type);
  143. ret=E_BUG;
  144. break;
  145. }
  146. to=(union sockaddr_union*) malloc(sizeof(union sockaddr_union));
  147. if (to==0){
  148. LOG(L_ERR, "ERROR: do_action: "
  149. "memory allocation failure\n");
  150. ret=E_OUT_OF_MEM;
  151. break;
  152. }
  153. p=(struct proxy_l*)a->p1.data;
  154. if (p->ok==0){
  155. if (p->host.h_addr_list[p->addr_idx+1])
  156. p->addr_idx++;
  157. else
  158. p->addr_idx=0;
  159. p->ok=1;
  160. }
  161. ret=hostent2su( to, &p->host, p->addr_idx,
  162. (p->port)?htons(p->port):htons(SIP_PORT) );
  163. if (ret==0){
  164. p->tx++;
  165. p->tx_bytes+=msg->len;
  166. if (a->type==SEND_T){
  167. /*udp*/
  168. send_sock=get_send_socket(to, PROTO_UDP);
  169. if (send_sock!=0){
  170. ret=udp_send(send_sock, msg->orig, msg->len, to);
  171. }else{
  172. ret=-1;
  173. }
  174. }
  175. #ifdef USE_TCP
  176. else{
  177. /*tcp*/
  178. ret=tcp_send(msg->orig, msg->len, to, 0);
  179. }
  180. #endif
  181. }
  182. free(to);
  183. if (ret<0){
  184. p->errors++;
  185. p->ok=0;
  186. }else ret=1;
  187. break;
  188. case LOG_T:
  189. if ((a->p1_type!=NUMBER_ST)|(a->p2_type!=STRING_ST)){
  190. LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
  191. a->p1_type, a->p2_type);
  192. ret=E_BUG;
  193. break;
  194. }
  195. LOG(a->p1.number, a->p2.string);
  196. ret=1;
  197. break;
  198. /* jku -- introduce a new branch */
  199. case APPEND_BRANCH_T:
  200. if ((a->p1_type!=STRING_ST)) {
  201. LOG(L_CRIT, "BUG: do_action: bad append_branch_t %d\n",
  202. a->p1_type );
  203. ret=E_BUG;
  204. break;
  205. }
  206. ret=append_branch( msg, a->p1.string,
  207. a->p1.string ? strlen(a->p1.string):0 );
  208. break;
  209. /* jku begin: is_length_greater_than */
  210. case LEN_GT_T:
  211. if (a->p1_type!=NUMBER_ST) {
  212. LOG(L_CRIT, "BUG: do_action: bad len_gt type %d\n",
  213. a->p1_type );
  214. ret=E_BUG;
  215. break;
  216. }
  217. /* DBG("XXX: message length %d, max %d\n",
  218. msg->len, a->p1.number ); */
  219. ret = msg->len >= a->p1.number ? 1 : -1;
  220. break;
  221. /* jku end: is_length_greater_than */
  222. /* jku - begin : flag processing */
  223. case SETFLAG_T:
  224. if (a->p1_type!=NUMBER_ST) {
  225. LOG(L_CRIT, "BUG: do_action: bad setflag() type %d\n",
  226. a->p1_type );
  227. ret=E_BUG;
  228. break;
  229. }
  230. if (!flag_in_range( a->p1.number )) {
  231. ret=E_CFG;
  232. break;
  233. }
  234. setflag( msg, a->p1.number );
  235. ret=1;
  236. break;
  237. case RESETFLAG_T:
  238. if (a->p1_type!=NUMBER_ST) {
  239. LOG(L_CRIT, "BUG: do_action: bad resetflag() type %d\n",
  240. a->p1_type );
  241. ret=E_BUG;
  242. break;
  243. }
  244. if (!flag_in_range( a->p1.number )) {
  245. ret=E_CFG;
  246. break;
  247. }
  248. resetflag( msg, a->p1.number );
  249. ret=1;
  250. break;
  251. case ISFLAGSET_T:
  252. if (a->p1_type!=NUMBER_ST) {
  253. LOG(L_CRIT, "BUG: do_action: bad isflagset() type %d\n",
  254. a->p1_type );
  255. ret=E_BUG;
  256. break;
  257. }
  258. if (!flag_in_range( a->p1.number )) {
  259. ret=E_CFG;
  260. break;
  261. }
  262. ret=isflagset( msg, a->p1.number );
  263. break;
  264. /* jku - end : flag processing */
  265. case ERROR_T:
  266. if ((a->p1_type!=STRING_ST)|(a->p2_type!=STRING_ST)){
  267. LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
  268. a->p1_type, a->p2_type);
  269. ret=E_BUG;
  270. break;
  271. }
  272. LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
  273. "not implemented yet\n", a->p1.string, a->p2.string);
  274. ret=1;
  275. break;
  276. case ROUTE_T:
  277. if (a->p1_type!=NUMBER_ST){
  278. LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
  279. a->p1_type);
  280. ret=E_BUG;
  281. break;
  282. }
  283. if ((a->p1.number>RT_NO)||(a->p1.number<0)){
  284. LOG(L_ERR, "ERROR: invalid routing table number in"
  285. "route(%d)\n", a->p1.number);
  286. ret=E_CFG;
  287. break;
  288. }
  289. ret=((ret=run_actions(rlist[a->p1.number], msg))<0)?ret:1;
  290. break;
  291. case EXEC_T:
  292. if (a->p1_type!=STRING_ST){
  293. LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
  294. a->p1_type);
  295. ret=E_BUG;
  296. break;
  297. }
  298. LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
  299. " using dumb version...\n", a->p1.string);
  300. ret=system(a->p1.string);
  301. if (ret!=0){
  302. LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
  303. }
  304. ret=1;
  305. break;
  306. case REVERT_URI_T:
  307. if (msg->new_uri.s) {
  308. pkg_free(msg->new_uri.s);
  309. msg->new_uri.len=0;
  310. msg->new_uri.s=0;
  311. msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
  312. };
  313. ret=1;
  314. break;
  315. case SET_HOST_T:
  316. case SET_HOSTPORT_T:
  317. case SET_USER_T:
  318. case SET_USERPASS_T:
  319. case SET_PORT_T:
  320. case SET_URI_T:
  321. case PREFIX_T:
  322. case STRIP_T:
  323. user=0;
  324. if (a->type==STRIP_T) {
  325. if (a->p1_type!=NUMBER_ST) {
  326. LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
  327. a->p1_type);
  328. break;
  329. }
  330. } else if (a->p1_type!=STRING_ST){
  331. LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
  332. a->p1_type);
  333. ret=E_BUG;
  334. break;
  335. }
  336. if (a->type==SET_URI_T){
  337. if (msg->new_uri.s) {
  338. pkg_free(msg->new_uri.s);
  339. msg->new_uri.len=0;
  340. }
  341. msg->parsed_uri_ok=0;
  342. len=strlen(a->p1.string);
  343. msg->new_uri.s=pkg_malloc(len+1);
  344. if (msg->new_uri.s==0){
  345. LOG(L_ERR, "ERROR: do_action: memory allocation"
  346. " failure\n");
  347. ret=E_OUT_OF_MEM;
  348. break;
  349. }
  350. memcpy(msg->new_uri.s, a->p1.string, len);
  351. msg->new_uri.s[len]=0;
  352. msg->new_uri.len=len;
  353. ret=1;
  354. break;
  355. }
  356. if (msg->new_uri.s) {
  357. tmp=msg->new_uri.s;
  358. len=msg->new_uri.len;
  359. }else{
  360. tmp=msg->first_line.u.request.uri.s;
  361. len=msg->first_line.u.request.uri.len;
  362. }
  363. if (parse_uri(tmp, len, &uri)<0){
  364. LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
  365. " packet\n", tmp);
  366. ret=E_UNSPEC;
  367. break;
  368. }
  369. new_uri=pkg_malloc(MAX_URI_SIZE);
  370. if (new_uri==0){
  371. LOG(L_ERR, "ERROR: do_action: memory allocation "
  372. " failure\n");
  373. ret=E_OUT_OF_MEM;
  374. break;
  375. }
  376. end=new_uri+MAX_URI_SIZE;
  377. crt=new_uri;
  378. /* begin copying */
  379. len=strlen("sip:"); if(crt+len>end) goto error_uri;
  380. memcpy(crt,"sip:",len);crt+=len;
  381. /* user */
  382. /* prefix (-jiri) */
  383. if (a->type==PREFIX_T) {
  384. tmp=a->p1.string;
  385. len=strlen(tmp); if(crt+len>end) goto error_uri;
  386. memcpy(crt,tmp,len);crt+=len;
  387. /* whateever we had before, with prefix we have username now */
  388. user=1;
  389. }
  390. if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
  391. tmp=a->p1.string;
  392. len=strlen(tmp);
  393. } else if (a->type==STRIP_T) {
  394. if (a->p1.number>uri.user.len) {
  395. LOG(L_WARN, "Error: too long strip asked; deleting username: "
  396. "%d of <%.*s>\n", a->p1.number, uri.user.len, uri.user.s );
  397. len=0;
  398. } else if (a->p1.number==uri.user.len) {
  399. len=0;
  400. } else {
  401. tmp=uri.user.s + a->p1.number;
  402. len=uri.user.len - a->p1.number;
  403. }
  404. } else {
  405. tmp=uri.user.s;
  406. len=uri.user.len;
  407. }
  408. if (len){
  409. if(crt+len>end) goto error_uri;
  410. memcpy(crt,tmp,len);crt+=len;
  411. user=1; /* we have an user field so mark it */
  412. }
  413. if (a->type==SET_USERPASS_T) tmp=0;
  414. else tmp=uri.passwd.s;
  415. /* passwd */
  416. if (tmp){
  417. len=strlen(":"); if(crt+len>end) goto error_uri;
  418. memcpy(crt,":",len);crt+=len;
  419. len=uri.passwd.len; if(crt+len>end) goto error_uri;
  420. memcpy(crt,tmp,len);crt+=len;
  421. }
  422. /* host */
  423. if (user || tmp){ /* add @ */
  424. len=strlen("@"); if(crt+len>end) goto error_uri;
  425. memcpy(crt,"@",len);crt+=len;
  426. }
  427. if ((a->type==SET_HOST_T) ||(a->type==SET_HOSTPORT_T)) {
  428. tmp=a->p1.string;
  429. if (tmp) len = strlen(tmp);
  430. } else {
  431. tmp=uri.host.s;
  432. len = uri.host.len;
  433. }
  434. if (tmp){
  435. if(crt+len>end) goto error_uri;
  436. memcpy(crt,tmp,len);crt+=len;
  437. }
  438. /* port */
  439. if (a->type==SET_HOSTPORT_T) tmp=0;
  440. else if (a->type==SET_PORT_T) {
  441. tmp=a->p1.string;
  442. if (tmp) len = strlen(tmp);
  443. } else {
  444. tmp=uri.port.s;
  445. len = uri.port.len;
  446. }
  447. if (tmp){
  448. len=strlen(":"); if(crt+len>end) goto error_uri;
  449. memcpy(crt,":",len);crt+=len;
  450. len=strlen(tmp); if(crt+len>end) goto error_uri;
  451. memcpy(crt,tmp,len);crt+=len;
  452. }
  453. /* params */
  454. tmp=uri.params.s;
  455. if (tmp){
  456. len=strlen(";"); if(crt+len>end) goto error_uri;
  457. memcpy(crt,";",len);crt+=len;
  458. len=uri.params.len; if(crt+len>end) goto error_uri;
  459. memcpy(crt,tmp,len);crt+=len;
  460. }
  461. /* headers */
  462. tmp=uri.headers.s;
  463. if (tmp){
  464. len=strlen("?"); if(crt+len>end) goto error_uri;
  465. memcpy(crt,"?",len);crt+=len;
  466. len=uri.headers.len; if(crt+len>end) goto error_uri;
  467. memcpy(crt,tmp,len);crt+=len;
  468. }
  469. *crt=0; /* null terminate the thing */
  470. /* copy it to the msg */
  471. if (msg->new_uri.s) pkg_free(msg->new_uri.s);
  472. msg->new_uri.s=new_uri;
  473. msg->new_uri.len=crt-new_uri;
  474. msg->parsed_uri_ok=0;
  475. ret=1;
  476. break;
  477. case IF_T:
  478. /* if null expr => ignore if? */
  479. if ((a->p1_type==EXPR_ST)&&a->p1.data){
  480. v=eval_expr((struct expr*)a->p1.data, msg);
  481. if (v<0){
  482. if (v==EXPR_DROP){ /* hack to quit on DROP*/
  483. ret=0;
  484. break;
  485. }else{
  486. LOG(L_WARN,"WARNING: do_action:"
  487. "error in expression\n");
  488. }
  489. }
  490. ret=1; /*default is continue */
  491. if (v>0) {
  492. if ((a->p2_type==ACTIONS_ST)&&a->p2.data){
  493. ret=run_actions((struct action*)a->p2.data, msg);
  494. }
  495. }else if ((a->p3_type==ACTIONS_ST)&&a->p3.data){
  496. ret=run_actions((struct action*)a->p3.data, msg);
  497. }
  498. }
  499. break;
  500. case MODULE_T:
  501. if ( ((a->p1_type==CMDF_ST)&&a->p1.data)/*&&
  502. ((a->p2_type==STRING_ST)&&a->p2.data)*/ ){
  503. ret=((cmd_function)(a->p1.data))(msg, (char*)a->p2.data,
  504. (char*)a->p3.data);
  505. }else{
  506. LOG(L_CRIT,"BUG: do_action: bad module call\n");
  507. }
  508. break;
  509. default:
  510. LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
  511. }
  512. /*skip:*/
  513. return ret;
  514. error_uri:
  515. LOG(L_ERR, "ERROR: do_action: set*: uri too long\n");
  516. if (new_uri) free(new_uri);
  517. return E_UNSPEC;
  518. error_fwd_uri:
  519. /*free_uri(&uri); -- not needed anymore, using msg->parsed_uri*/
  520. return ret;
  521. }
  522. /* returns: 0, or 1 on success, <0 on error */
  523. /* (0 if drop or break encountered, 1 if not ) */
  524. int run_actions(struct action* a, struct sip_msg* msg)
  525. {
  526. struct action* t;
  527. int ret=E_UNSPEC;
  528. static int rec_lev=0;
  529. struct sr_module *mod;
  530. rec_lev++;
  531. if (rec_lev>ROUTE_MAX_REC_LEV){
  532. LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
  533. " giving up!\n", rec_lev);
  534. ret=E_UNSPEC;
  535. goto error;
  536. }
  537. if (a==0){
  538. LOG(L_ERR, "WARNING: run_actions: null action list (rec_level=%d)\n",
  539. rec_lev);
  540. ret=0;
  541. }
  542. for (t=a; t!=0; t=t->next){
  543. ret=do_action(t, msg);
  544. if(ret==0) break;
  545. /* ignore errors */
  546. /*else if (ret<0){ ret=-1; goto error; }*/
  547. }
  548. rec_lev--;
  549. /* process module onbreak handlers if present */
  550. if (rec_lev==0 && ret==0)
  551. for (mod=modules;mod;mod=mod->next)
  552. if (mod->exports && mod->exports->onbreak_f) {
  553. mod->exports->onbreak_f( msg );
  554. DBG("DEBUG: %s onbreak handler called\n", mod->exports->name);
  555. }
  556. return ret;
  557. error:
  558. rec_lev--;
  559. return ret;
  560. }