action.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. * History:
  28. * ---------
  29. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  30. * 2003-01-29 removed scratchpad (jiri)
  31. * 2003-03-19 fixed set* len calculation bug & simplified a little the code
  32. * (should be a little faster now) (andrei)
  33. * replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  34. * 2003-04-01 Added support for loose routing in forward (janakj)
  35. * 2003-04-12 FORCE_RPORT_T added (andrei)
  36. * 2003-04-22 strip_tail added (jiri)
  37. * 2003-10-02 added SET_ADV_ADDR_T & SET_ADV_PORT_T (andrei)
  38. * 2003-10-29 added FORCE_TCP_ALIAS_T (andrei)
  39. * 2004-11-30 added FORCE_SEND_SOCKET_T (andrei)
  40. * 2005-12-12 return & drop/exit differentiation (andrei)
  41. * 2005-12-19 select framework (mma)
  42. * 2006-04-12 updated *_send() calls to use a struct dest_info (andrei)
  43. * 2006-07-27 dns cache and dns based send address failover support (andrei)
  44. * 2006-12-06 on popular request last_retcode set also by module functions
  45. * (andrei)
  46. * 2007-06-14 run_actions & do_action need a ctx or handle now, no more
  47. * static vars (andrei)
  48. * 2008-12-17 added UDP_MTU_TRY_PROTO_T (andrei)
  49. */
  50. #include "comp_defs.h"
  51. #include "action.h"
  52. #include "config.h"
  53. #include "error.h"
  54. #include "dprint.h"
  55. #include "proxy.h"
  56. #include "forward.h"
  57. #include "udp_server.h"
  58. #include "route.h"
  59. #include "parser/msg_parser.h"
  60. #include "parser/parse_uri.h"
  61. #include "ut.h"
  62. #include "sr_module.h"
  63. #include "mem/mem.h"
  64. #include "globals.h"
  65. #include "dset.h"
  66. #include "onsend.h"
  67. #include "resolve.h"
  68. #ifdef USE_TCP
  69. #include "tcp_server.h"
  70. #endif
  71. #ifdef USE_SCTP
  72. #include "sctp_server.h"
  73. #endif
  74. #include <sys/types.h>
  75. #include <sys/socket.h>
  76. #include <netdb.h>
  77. #include <stdlib.h>
  78. #include <netinet/in.h>
  79. #include <arpa/inet.h>
  80. #include <string.h>
  81. #ifdef DEBUG_DMALLOC
  82. #include <dmalloc.h>
  83. #endif
  84. struct onsend_info* p_onsend=0; /* onsend route send info */
  85. /* ret= 0! if action -> end of list(e.g DROP),
  86. > 0 to continue processing next actions
  87. and <0 on error */
  88. int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
  89. {
  90. int ret;
  91. int v;
  92. struct dest_info dst;
  93. char* tmp;
  94. char *new_uri, *end, *crt;
  95. int len;
  96. int user;
  97. struct sip_uri uri, next_hop;
  98. struct sip_uri *u;
  99. unsigned short port;
  100. unsigned short flags;
  101. int_str name, value;
  102. str* dst_host;
  103. int orig_p2t;
  104. /* reset the value of error to E_UNSPEC so avoid unknowledgable
  105. functions to return with error (status<0) and not setting it
  106. leaving there previous error; cache the previous value though
  107. for functions which want to process it */
  108. prev_ser_error=ser_error;
  109. ser_error=E_UNSPEC;
  110. ret=E_BUG;
  111. switch ((unsigned char)a->type){
  112. case DROP_T:
  113. if (a->val[0].type==RETCODE_ST)
  114. ret=h->last_retcode;
  115. else
  116. ret=(int) a->val[0].u.number;
  117. h->run_flags|=(unsigned int)a->val[1].u.number;
  118. break;
  119. case FORWARD_T:
  120. #ifdef USE_TCP
  121. case FORWARD_TCP_T:
  122. #endif
  123. #ifdef USE_TLS
  124. case FORWARD_TLS_T:
  125. #endif
  126. #ifdef USE_SCTP
  127. case FORWARD_SCTP_T:
  128. #endif
  129. case FORWARD_UDP_T:
  130. /* init dst */
  131. init_dest_info(&dst);
  132. if (a->type==FORWARD_UDP_T) dst.proto=PROTO_UDP;
  133. #ifdef USE_TCP
  134. else if (a->type==FORWARD_TCP_T) dst.proto= PROTO_TCP;
  135. #endif
  136. #ifdef USE_TLS
  137. else if (a->type==FORWARD_TLS_T) dst.proto= PROTO_TLS;
  138. #endif
  139. #ifdef USE_SCTP
  140. else if (a->type==FORWARD_SCTP_T) dst.proto=PROTO_SCTP;
  141. #endif
  142. else dst.proto=PROTO_NONE;
  143. if (a->val[0].type==URIHOST_ST){
  144. /*parse uri*/
  145. if (msg->dst_uri.len) {
  146. ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len,
  147. &next_hop);
  148. u = &next_hop;
  149. } else {
  150. ret = parse_sip_msg_uri(msg);
  151. u = &msg->parsed_uri;
  152. }
  153. if (ret<0) {
  154. LOG(L_ERR, "ERROR: do_action: forward: bad_uri "
  155. " dropping packet\n");
  156. break;
  157. }
  158. switch (a->val[1].type){
  159. case URIPORT_ST:
  160. port=u->port_no;
  161. break;
  162. case NUMBER_ST:
  163. port=a->val[1].u.number;
  164. break;
  165. default:
  166. LOG(L_CRIT, "BUG: do_action bad forward 2nd"
  167. " param type (%d)\n", a->val[1].type);
  168. ret=E_UNSPEC;
  169. goto error_fwd_uri;
  170. }
  171. if (dst.proto == PROTO_NONE){ /* only if proto not set get it
  172. from the uri */
  173. switch(u->proto){
  174. case PROTO_NONE:
  175. /*dst.proto=PROTO_UDP; */
  176. /* no proto, try to get it from the dns */
  177. break;
  178. case PROTO_UDP:
  179. #ifdef USE_TCP
  180. case PROTO_TCP:
  181. #endif
  182. #ifdef USE_TLS
  183. case PROTO_TLS:
  184. #endif
  185. #ifdef USE_SCTP
  186. case PROTO_SCTP:
  187. #endif
  188. dst.proto=u->proto;
  189. break;
  190. default:
  191. LOG(L_ERR,"ERROR: do action: forward: bad uri"
  192. " transport %d\n", u->proto);
  193. ret=E_BAD_PROTO;
  194. goto error_fwd_uri;
  195. }
  196. #ifdef USE_TLS
  197. if (u->type==SIPS_URI_T){
  198. if (u->proto==PROTO_UDP){
  199. LOG(L_ERR, "ERROR: do_action: forward: secure uri"
  200. " incompatible with transport %d\n",
  201. u->proto);
  202. ret=E_BAD_PROTO;
  203. goto error_fwd_uri;
  204. }
  205. dst.proto=PROTO_TLS;
  206. }
  207. #endif
  208. }
  209. #ifdef HONOR_MADDR
  210. if (u->maddr_val.s && u->maddr_val.len)
  211. dst_host=&u->maddr_val;
  212. else
  213. #endif
  214. dst_host=&u->host;
  215. #ifdef USE_COMP
  216. dst.comp=u->comp;
  217. #endif
  218. ret=forward_request(msg, dst_host, port, &dst);
  219. if (ret>=0){
  220. ret=1;
  221. }
  222. }else if ((a->val[0].type==PROXY_ST) && (a->val[1].type==NUMBER_ST)){
  223. if (dst.proto==PROTO_NONE)
  224. dst.proto=msg->rcv.proto;
  225. proxy2su(&dst.to, (struct proxy_l*)a->val[0].u.data);
  226. ret=forward_request(msg, 0, 0, &dst);
  227. if (ret>=0){
  228. ret=1;
  229. proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
  230. }else if (ser_error!=E_OK){
  231. proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
  232. }
  233. }else{
  234. LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
  235. a->val[0].type, a->val[1].type);
  236. ret=E_BUG;
  237. }
  238. break;
  239. case SEND_T:
  240. case SEND_TCP_T:
  241. if ((a->val[0].type!= PROXY_ST)|(a->val[1].type!=NUMBER_ST)){
  242. LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
  243. a->val[0].type, a->val[1].type);
  244. ret=E_BUG;
  245. break;
  246. }
  247. /* init dst */
  248. init_dest_info(&dst);
  249. ret=proxy2su(&dst.to, (struct proxy_l*)a->val[0].u.data);
  250. if (ret==0){
  251. if (p_onsend){
  252. tmp=p_onsend->buf;
  253. len=p_onsend->len;
  254. }else{
  255. tmp=msg->buf;
  256. len=msg->len;
  257. }
  258. if (a->type==SEND_T){
  259. /*udp*/
  260. dst.proto=PROTO_UDP; /* not really needed for udp_send */
  261. dst.send_sock=get_send_socket(msg, &dst.to, PROTO_UDP);
  262. if (dst.send_sock!=0){
  263. ret=udp_send(&dst, tmp, len);
  264. }else{
  265. ret=-1;
  266. }
  267. }
  268. #ifdef USE_TCP
  269. else{
  270. /*tcp*/
  271. dst.proto=PROTO_TCP;
  272. dst.id=0;
  273. ret=tcp_send(&dst, 0, tmp, len);
  274. }
  275. #endif
  276. }else{
  277. ret=E_BUG;
  278. break;
  279. }
  280. proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
  281. if (ret>=0) ret=1;
  282. break;
  283. case LOG_T:
  284. if ((a->val[0].type!=NUMBER_ST)|(a->val[1].type!=STRING_ST)){
  285. LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
  286. a->val[0].type, a->val[1].type);
  287. ret=E_BUG;
  288. break;
  289. }
  290. LOG_(a->val[0].u.number, "<script>: ", "%s", a->val[1].u.string);
  291. ret=1;
  292. break;
  293. /* jku -- introduce a new branch */
  294. case APPEND_BRANCH_T:
  295. if ((a->val[0].type!=STRING_ST)) {
  296. LOG(L_CRIT, "BUG: do_action: bad append_branch_t %d\n",
  297. a->val[0].type );
  298. ret=E_BUG;
  299. break;
  300. }
  301. ret=append_branch( msg, a->val[0].u.string,
  302. a->val[0].u.string ? strlen(a->val[0].u.string):0,
  303. 0, 0, a->val[1].u.number, 0);
  304. break;
  305. /* jku begin: is_length_greater_than */
  306. case LEN_GT_T:
  307. if (a->val[0].type!=NUMBER_ST) {
  308. LOG(L_CRIT, "BUG: do_action: bad len_gt type %d\n",
  309. a->val[0].type );
  310. ret=E_BUG;
  311. break;
  312. }
  313. /* DBG("XXX: message length %d, max %d\n",
  314. msg->len, a->val[0].u.number ); */
  315. ret = msg->len >= a->val[0].u.number ? 1 : -1;
  316. break;
  317. /* jku end: is_length_greater_than */
  318. /* jku - begin : flag processing */
  319. case SETFLAG_T:
  320. if (a->val[0].type!=NUMBER_ST) {
  321. LOG(L_CRIT, "BUG: do_action: bad setflag() type %d\n",
  322. a->val[0].type );
  323. ret=E_BUG;
  324. break;
  325. }
  326. if (!flag_in_range( a->val[0].u.number )) {
  327. ret=E_CFG;
  328. break;
  329. }
  330. setflag( msg, a->val[0].u.number );
  331. ret=1;
  332. break;
  333. case RESETFLAG_T:
  334. if (a->val[0].type!=NUMBER_ST) {
  335. LOG(L_CRIT, "BUG: do_action: bad resetflag() type %d\n",
  336. a->val[0].type );
  337. ret=E_BUG;
  338. break;
  339. }
  340. if (!flag_in_range( a->val[0].u.number )) {
  341. ret=E_CFG;
  342. break;
  343. }
  344. resetflag( msg, a->val[0].u.number );
  345. ret=1;
  346. break;
  347. case ISFLAGSET_T:
  348. if (a->val[0].type!=NUMBER_ST) {
  349. LOG(L_CRIT, "BUG: do_action: bad isflagset() type %d\n",
  350. a->val[0].type );
  351. ret=E_BUG;
  352. break;
  353. }
  354. if (!flag_in_range( a->val[0].u.number )) {
  355. ret=E_CFG;
  356. break;
  357. }
  358. ret=isflagset( msg, a->val[0].u.number );
  359. break;
  360. /* jku - end : flag processing */
  361. case AVPFLAG_OPER_T: {
  362. struct search_state st;
  363. avp_t* avp;
  364. int flag;
  365. ret = 0;
  366. flag = a->val[1].u.number;
  367. if ((a->val[0].u.attr->type & AVP_INDEX_ALL) == AVP_INDEX_ALL || (a->val[0].u.attr->type & AVP_NAME_RE)!=0) {
  368. for (avp=search_first_avp(a->val[0].u.attr->type, a->val[0].u.attr->name, NULL, &st); avp; avp = search_next_avp(&st, NULL)) {
  369. switch (a->val[2].u.number) { /* oper: 0..reset, 1..set, -1..no change */
  370. case 0:
  371. avp->flags &= ~(avp_flags_t)a->val[1].u.number;
  372. break;
  373. case 1:
  374. avp->flags |= (avp_flags_t)a->val[1].u.number;
  375. break;
  376. default:;
  377. }
  378. ret = ret || ((avp->flags & (avp_flags_t)a->val[1].u.number) != 0);
  379. }
  380. }
  381. else {
  382. avp = search_avp_by_index(a->val[0].u.attr->type, a->val[0].u.attr->name, NULL, a->val[0].u.attr->index);
  383. if (avp) {
  384. switch (a->val[2].u.number) { /* oper: 0..reset, 1..set, -1..no change */
  385. case 0:
  386. avp->flags &= ~(avp_flags_t)a->val[1].u.number;
  387. break;
  388. case 1:
  389. avp->flags |= (avp_flags_t)a->val[1].u.number;
  390. break;
  391. default:;
  392. }
  393. ret = (avp->flags & (avp_flags_t)a->val[1].u.number) != 0;
  394. }
  395. }
  396. if (ret==0)
  397. ret = -1;
  398. break;
  399. }
  400. case ERROR_T:
  401. if ((a->val[0].type!=STRING_ST)|(a->val[1].type!=STRING_ST)){
  402. LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
  403. a->val[0].type, a->val[1].type);
  404. ret=E_BUG;
  405. break;
  406. }
  407. LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
  408. "not implemented yet\n", a->val[0].u.string, a->val[1].u.string);
  409. ret=1;
  410. break;
  411. case ROUTE_T:
  412. if (a->val[0].type!=NUMBER_ST){
  413. LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
  414. a->val[0].type);
  415. ret=E_BUG;
  416. break;
  417. }
  418. if ((a->val[0].u.number>=main_rt.idx)||(a->val[0].u.number<0)){
  419. LOG(L_ERR, "ERROR: invalid routing table number in"
  420. "route(%lu)\n", a->val[0].u.number);
  421. ret=E_CFG;
  422. break;
  423. }
  424. /*ret=((ret=run_actions(rlist[a->val[0].u.number], msg))<0)?ret:1;*/
  425. ret=run_actions(h, main_rt.rlist[a->val[0].u.number], msg);
  426. h->last_retcode=ret;
  427. h->run_flags&=~RETURN_R_F; /* absorb returns */
  428. break;
  429. case EXEC_T:
  430. if (a->val[0].type!=STRING_ST){
  431. LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
  432. a->val[0].type);
  433. ret=E_BUG;
  434. break;
  435. }
  436. LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
  437. " using dumb version...\n", a->val[0].u.string);
  438. ret=system(a->val[0].u.string);
  439. if (ret!=0){
  440. LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
  441. }
  442. ret=1;
  443. break;
  444. case REVERT_URI_T:
  445. if (msg->new_uri.s) {
  446. pkg_free(msg->new_uri.s);
  447. msg->new_uri.len=0;
  448. msg->new_uri.s=0;
  449. msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
  450. };
  451. ret=1;
  452. break;
  453. case SET_HOST_T:
  454. case SET_HOSTPORT_T:
  455. case SET_HOSTPORTTRANS_T:
  456. case SET_USER_T:
  457. case SET_USERPASS_T:
  458. case SET_PORT_T:
  459. case SET_URI_T:
  460. case PREFIX_T:
  461. case STRIP_T:
  462. case STRIP_TAIL_T:
  463. case SET_USERPHONE_T:
  464. user=0;
  465. if (a->type==STRIP_T || a->type==STRIP_TAIL_T) {
  466. if (a->val[0].type!=NUMBER_ST) {
  467. LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
  468. a->val[0].type);
  469. ret=E_BUG;
  470. break;
  471. }
  472. } else if (a->type!=SET_USERPHONE_T) {
  473. if (a->val[0].type!=STRING_ST) {
  474. LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
  475. a->val[0].type);
  476. ret=E_BUG;
  477. break;
  478. }
  479. }
  480. if (a->type==SET_URI_T){
  481. if (msg->new_uri.s) {
  482. pkg_free(msg->new_uri.s);
  483. msg->new_uri.len=0;
  484. }
  485. msg->parsed_uri_ok=0;
  486. len=strlen(a->val[0].u.string);
  487. msg->new_uri.s=pkg_malloc(len+1);
  488. if (msg->new_uri.s==0){
  489. LOG(L_ERR, "ERROR: do_action: memory allocation"
  490. " failure\n");
  491. ret=E_OUT_OF_MEM;
  492. break;
  493. }
  494. memcpy(msg->new_uri.s, a->val[0].u.string, len);
  495. msg->new_uri.s[len]=0;
  496. msg->new_uri.len=len;
  497. ret=1;
  498. break;
  499. }
  500. if ((msg->parsed_uri_ok==0) || ((uri.flags & URI_SIP_USER_PHONE)!=0)) {
  501. if (msg->new_uri.s) {
  502. tmp=msg->new_uri.s;
  503. len=msg->new_uri.len;
  504. }else{
  505. tmp=msg->first_line.u.request.uri.s;
  506. len=msg->first_line.u.request.uri.len;
  507. }
  508. /* don't convert sip:user=phone to tel, otherwise we loose parameters */
  509. orig_p2t=phone2tel;
  510. phone2tel=0;
  511. msg->parsed_uri_ok=0;
  512. if (parse_uri(tmp, len, &uri)<0){
  513. phone2tel=orig_p2t;
  514. LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
  515. " packet\n", tmp);
  516. ret=E_UNSPEC;
  517. break;
  518. }
  519. phone2tel=orig_p2t;
  520. } else {
  521. uri=msg->parsed_uri;
  522. }
  523. /* skip SET_USERPHONE_T action if the URI is already
  524. * a tel: or tels: URI, or contains the user=phone param */
  525. if ((a->type==SET_USERPHONE_T)
  526. && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T)
  527. || ((uri.user_param_val.len==5) && (memcmp(uri.user_param_val.s, "phone", 5)==0)))
  528. ) {
  529. ret=1;
  530. break;
  531. }
  532. /* SET_PORT_T does not work with tel: URIs */
  533. if ((a->type==SET_PORT_T)
  534. && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
  535. && ((uri.flags & URI_SIP_USER_PHONE)==0)
  536. ) {
  537. LOG(L_ERR, "ERROR: do_action: port number of a tel: URI cannot be set\n");
  538. ret=E_UNSPEC;
  539. break;
  540. }
  541. new_uri=pkg_malloc(MAX_URI_SIZE);
  542. if (new_uri==0){
  543. LOG(L_ERR, "ERROR: do_action: memory allocation "
  544. " failure\n");
  545. ret=E_OUT_OF_MEM;
  546. break;
  547. }
  548. end=new_uri+MAX_URI_SIZE;
  549. crt=new_uri;
  550. /* begin copying */
  551. /* Preserve the URI scheme unless the host part needs
  552. * to be rewritten, and the shceme is tel: or tels: */
  553. switch (uri.type) {
  554. case SIP_URI_T:
  555. len=s_sip.len;
  556. tmp=s_sip.s;
  557. break;
  558. case SIPS_URI_T:
  559. len=s_sips.len;
  560. tmp=s_sips.s;
  561. break;
  562. case TEL_URI_T:
  563. if ((uri.flags & URI_SIP_USER_PHONE)
  564. || (a->type==SET_HOST_T)
  565. || (a->type==SET_HOSTPORT_T)
  566. || (a->type==SET_HOSTPORTTRANS_T)
  567. ) {
  568. len=s_sip.len;
  569. tmp=s_sip.s;
  570. break;
  571. }
  572. len=s_tel.len;
  573. tmp=s_tel.s;
  574. break;
  575. case TELS_URI_T:
  576. if ((uri.flags & URI_SIP_USER_PHONE)
  577. || (a->type==SET_HOST_T)
  578. || (a->type==SET_HOSTPORT_T)
  579. || (a->type==SET_HOSTPORTTRANS_T)
  580. ) {
  581. len=s_sips.len;
  582. tmp=s_sips.s;
  583. break;
  584. }
  585. len=s_tels.len;
  586. tmp=s_tels.s;
  587. break;
  588. default:
  589. LOG(L_ERR, "ERROR: Unsupported URI scheme (%d), "
  590. "reverted to sip:\n",
  591. uri.type);
  592. len=s_sip.len;
  593. tmp=s_sip.s;
  594. }
  595. if(crt+len+1 /* colon */ >end) goto error_uri;
  596. memcpy(crt,tmp,len);crt+=len;
  597. *crt=':'; crt++;
  598. /* user */
  599. /* prefix (-jiri) */
  600. if (a->type==PREFIX_T) {
  601. tmp=a->val[0].u.string;
  602. len=strlen(tmp); if(crt+len>end) goto error_uri;
  603. memcpy(crt,tmp,len);crt+=len;
  604. /* whatever we had before, with prefix we have username
  605. now */
  606. user=1;
  607. }
  608. if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
  609. tmp=a->val[0].u.string;
  610. len=strlen(tmp);
  611. } else if (a->type==STRIP_T) {
  612. if (a->val[0].u.number>uri.user.len) {
  613. LOG(L_WARN, "Error: too long strip asked; "
  614. " deleting username: %lu of <%.*s>\n",
  615. a->val[0].u.number, uri.user.len, uri.user.s );
  616. len=0;
  617. } else if (a->val[0].u.number==uri.user.len) {
  618. len=0;
  619. } else {
  620. tmp=uri.user.s + a->val[0].u.number;
  621. len=uri.user.len - a->val[0].u.number;
  622. }
  623. } else if (a->type==STRIP_TAIL_T) {
  624. if (a->val[0].u.number>uri.user.len) {
  625. LOG(L_WARN, "WARNING: too long strip_tail asked; "
  626. " deleting username: %lu of <%.*s>\n",
  627. a->val[0].u.number, uri.user.len, uri.user.s );
  628. len=0;
  629. } else if (a->val[0].u.number==uri.user.len) {
  630. len=0;
  631. } else {
  632. tmp=uri.user.s;
  633. len=uri.user.len - a->val[0].u.number;
  634. }
  635. } else {
  636. tmp=uri.user.s;
  637. len=uri.user.len;
  638. }
  639. if (len){
  640. if(crt+len>end) goto error_uri;
  641. memcpy(crt,tmp,len);crt+=len;
  642. user=1; /* we have an user field so mark it */
  643. }
  644. if (a->type==SET_USERPASS_T) tmp=0;
  645. else tmp=uri.passwd.s;
  646. /* passwd */
  647. if (tmp){
  648. len=uri.passwd.len; if(crt+len+1>end) goto error_uri;
  649. *crt=':'; crt++;
  650. memcpy(crt,tmp,len);crt+=len;
  651. }
  652. /* host */
  653. if ((a->type==SET_HOST_T)
  654. || (a->type==SET_HOSTPORT_T)
  655. || (a->type==SET_HOSTPORTTRANS_T)
  656. ) {
  657. tmp=a->val[0].u.string;
  658. if (tmp) len = strlen(tmp);
  659. else len=0;
  660. } else if ((uri.type==SIP_URI_T)
  661. || (uri.type==SIPS_URI_T)
  662. || (uri.flags & URI_SIP_USER_PHONE)
  663. ) {
  664. tmp=uri.host.s;
  665. len=uri.host.len;
  666. } else {
  667. tmp=0;
  668. }
  669. if (tmp){
  670. if (user) { /* add @ */
  671. if(crt+1>end) goto error_uri;
  672. *crt='@'; crt++;
  673. }
  674. if(crt+len>end) goto error_uri;
  675. memcpy(crt,tmp,len);crt+=len;
  676. }
  677. /* port */
  678. if ((a->type==SET_HOSTPORT_T)
  679. || (a->type==SET_HOSTPORTTRANS_T))
  680. tmp=0;
  681. else if (a->type==SET_PORT_T) {
  682. tmp=a->val[0].u.string;
  683. if (tmp) len = strlen(tmp);
  684. else len = 0;
  685. } else {
  686. tmp=uri.port.s;
  687. len = uri.port.len;
  688. }
  689. if (tmp){
  690. if(crt+len+1>end) goto error_uri;
  691. *crt=':'; crt++;
  692. memcpy(crt,tmp,len);crt+=len;
  693. }
  694. /* params */
  695. if ((a->type==SET_HOSTPORTTRANS_T) && uri.transport.s) {
  696. /* bypass the transport parameter */
  697. if (uri.params.s < uri.transport.s) {
  698. /* there are parameters before transport */
  699. len = uri.transport.s - uri.params.s - 1;
  700. /* ignore the ';' at the end */
  701. if (crt+len+1>end) goto error_uri;
  702. *crt=';'; crt++;
  703. memcpy(crt,uri.params.s,len);crt+=len;
  704. }
  705. len = (uri.params.s + uri.params.len) -
  706. (uri.transport.s + uri.transport.len);
  707. if (len > 0) {
  708. /* there are parameters after transport */
  709. if (crt+len>end) goto error_uri;
  710. tmp = uri.transport.s + uri.transport.len;
  711. memcpy(crt,tmp,len);crt+=len;
  712. }
  713. } else {
  714. tmp=uri.params.s;
  715. if (tmp){
  716. len=uri.params.len; if(crt+len+1>end) goto error_uri;
  717. *crt=';'; crt++;
  718. memcpy(crt,tmp,len);crt+=len;
  719. }
  720. }
  721. /* Add the user=phone param if a tel: or tels:
  722. * URI was converted to sip: or sips:.
  723. * (host part of a tel/tels URI was set.)
  724. * Or in case of sip: URI and SET_USERPHONE_T action */
  725. if (((((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
  726. && ((uri.flags & URI_SIP_USER_PHONE)==0))
  727. && ((a->type==SET_HOST_T)
  728. || (a->type==SET_HOSTPORT_T)
  729. || (a->type==SET_HOSTPORTTRANS_T)))
  730. || (a->type==SET_USERPHONE_T)
  731. ) {
  732. tmp=";user=phone";
  733. len=strlen(tmp);
  734. if(crt+len>end) goto error_uri;
  735. memcpy(crt,tmp,len);crt+=len;
  736. }
  737. /* headers */
  738. tmp=uri.headers.s;
  739. if (tmp){
  740. len=uri.headers.len; if(crt+len+1>end) goto error_uri;
  741. *crt='?'; crt++;
  742. memcpy(crt,tmp,len);crt+=len;
  743. }
  744. *crt=0; /* null terminate the thing */
  745. /* copy it to the msg */
  746. if (msg->new_uri.s) pkg_free(msg->new_uri.s);
  747. msg->new_uri.s=new_uri;
  748. msg->new_uri.len=crt-new_uri;
  749. msg->parsed_uri_ok=0;
  750. ret=1;
  751. break;
  752. case IF_T:
  753. /* if null expr => ignore if? */
  754. if ((a->val[0].type==EXPR_ST)&&a->val[0].u.data){
  755. v=eval_expr(h, (struct expr*)a->val[0].u.data, msg);
  756. #if 0
  757. if (v<0){
  758. if (v==EXPR_DROP){ /* hack to quit on DROP*/
  759. ret=0;
  760. break;
  761. }else{
  762. LOG(L_WARN,"WARNING: do_action:"
  763. "error in expression\n");
  764. }
  765. }
  766. #endif
  767. if (h->run_flags & EXIT_R_F){
  768. ret=0;
  769. break;
  770. }
  771. h->run_flags &= ~RETURN_R_F; /* catch returns in expr */
  772. ret=1; /*default is continue */
  773. if (v>0) {
  774. if ((a->val[1].type==ACTIONS_ST)&&a->val[1].u.data){
  775. ret=run_actions(h,
  776. (struct action*)a->val[1].u.data, msg);
  777. }
  778. }else if ((a->val[2].type==ACTIONS_ST)&&a->val[2].u.data){
  779. ret=run_actions(h,
  780. (struct action*)a->val[2].u.data, msg);
  781. }
  782. }
  783. break;
  784. case MODULE_T:
  785. if ( a->val[0].type==MODEXP_ST && a->val[0].u.data && ((cmd_export_t*)a->val[0].u.data)->function ){
  786. ret=((cmd_export_t*)a->val[0].u.data)->function(msg,
  787. (char*)a->val[2].u.data,
  788. (char*)a->val[3].u.data
  789. );
  790. if (ret==0) h->run_flags|=EXIT_R_F;
  791. h->last_retcode=ret;
  792. } else {
  793. LOG(L_CRIT,"BUG: do_action: bad module call\n");
  794. }
  795. break;
  796. case FORCE_RPORT_T:
  797. msg->msg_flags|=FL_FORCE_RPORT;
  798. ret=1; /* continue processing */
  799. break;
  800. case UDP_MTU_TRY_PROTO_T:
  801. msg->msg_flags|= (unsigned int)a->val[0].u.number & FL_MTU_FB_MASK;
  802. ret=1; /* continue processing */
  803. break;
  804. case SET_ADV_ADDR_T:
  805. if (a->val[0].type!=STR_ST){
  806. LOG(L_CRIT, "BUG: do_action: bad set_advertised_address() "
  807. "type %d\n", a->val[0].type);
  808. ret=E_BUG;
  809. break;
  810. }
  811. msg->set_global_address=*((str*)a->val[0].u.data);
  812. ret=1; /* continue processing */
  813. break;
  814. case SET_ADV_PORT_T:
  815. if (a->val[0].type!=STR_ST){
  816. LOG(L_CRIT, "BUG: do_action: bad set_advertised_port() "
  817. "type %d\n", a->val[0].type);
  818. ret=E_BUG;
  819. break;
  820. }
  821. msg->set_global_port=*((str*)a->val[0].u.data);
  822. ret=1; /* continue processing */
  823. break;
  824. #ifdef USE_TCP
  825. case FORCE_TCP_ALIAS_T:
  826. if ( msg->rcv.proto==PROTO_TCP
  827. #ifdef USE_TLS
  828. || msg->rcv.proto==PROTO_TLS
  829. #endif
  830. ){
  831. if (a->val[0].type==NOSUBTYPE) port=msg->via1->port;
  832. else if (a->val[0].type==NUMBER_ST) port=(int)a->val[0].u.number;
  833. else{
  834. LOG(L_CRIT, "BUG: do_action: bad force_tcp_alias"
  835. " port type %d\n", a->val[0].type);
  836. ret=E_BUG;
  837. break;
  838. }
  839. if (tcpconn_add_alias(msg->rcv.proto_reserved1, port,
  840. msg->rcv.proto)!=0){
  841. LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n");
  842. ret=E_UNSPEC;
  843. break;
  844. }
  845. }
  846. #endif
  847. ret=1; /* continue processing */
  848. break;
  849. case FORCE_SEND_SOCKET_T:
  850. if (a->val[0].type!=SOCKETINFO_ST){
  851. LOG(L_CRIT, "BUG: do_action: bad force_send_socket argument"
  852. " type: %d\n", a->val[0].type);
  853. ret=E_BUG;
  854. break;
  855. }
  856. msg->force_send_socket=(struct socket_info*)a->val[0].u.data;
  857. ret=1; /* continue processing */
  858. break;
  859. case ADD_T:
  860. case ASSIGN_T:
  861. /* If the left attr was specified without indexing brackets delete
  862. * existing AVPs before adding new ones
  863. */
  864. if ((a->val[0].u.attr->type & AVP_INDEX_ALL) != AVP_INDEX_ALL) delete_avp(a->val[0].u.attr->type, a->val[0].u.attr->name);
  865. if (a->val[1].type == STRING_ST) {
  866. value.s = a->val[1].u.str;
  867. flags = a->val[0].u.attr->type | AVP_VAL_STR;
  868. name = a->val[0].u.attr->name;
  869. ret = 1;
  870. } else if (a->val[1].type == NUMBER_ST) {
  871. value.n = a->val[1].u.number;
  872. flags = a->val[0].u.attr->type;
  873. name = a->val[0].u.attr->name;
  874. ret = 1;
  875. } else if (a->val[1].type == ACTION_ST) {
  876. flags = a->val[0].u.attr->type;
  877. name = a->val[0].u.attr->name;
  878. if (a->val[1].u.data) {
  879. value.n = run_actions(h, (struct action*)a->val[1].u.data,
  880. msg);
  881. } else {
  882. value.n = -1;
  883. }
  884. ret = value.n;
  885. } else if(a->val[1].type == EXPR_ST && a->val[1].u.data) {
  886. v = eval_expr(h, (struct expr*)a->val[1].u.data, msg);
  887. if (v < 0) {
  888. if (v == EXPR_DROP){ /* hack to quit on DROP*/
  889. ret = 0;
  890. break;
  891. } else {
  892. LOG(L_WARN,"WARNING: do_action: error in expression\n");
  893. v = 0; /* error is treated as false (Miklos) */
  894. }
  895. }
  896. flags = a->val[0].u.attr->type;
  897. name = a->val[0].u.attr->name;
  898. value.n = v;
  899. } else if (a->val[1].type == AVP_ST) {
  900. struct search_state st;
  901. avp_t* avp;
  902. avp_t* avp_mark;
  903. avp_mark = NULL;
  904. if ((a->val[1].u.attr->type & AVP_INDEX_ALL) == AVP_INDEX_ALL) {
  905. avp = search_first_avp(a->val[1].u.attr->type, a->val[1].u.attr->name, &value, &st);
  906. while(avp) {
  907. /* We take only the type of value and name from the source avp
  908. * and reset class and track flags
  909. */
  910. flags = (a->val[0].u.attr->type & ~AVP_INDEX_ALL) | (avp->flags & ~(AVP_CLASS_ALL|AVP_TRACK_ALL));
  911. if (add_avp_before(avp_mark, flags, a->val[0].u.attr->name, value) < 0) {
  912. LOG(L_CRIT, "ERROR: Failed to assign value to attribute\n");
  913. ret=E_UNSPEC;
  914. break;
  915. }
  916. /* move the mark, so the next found AVP will come before the one currently added
  917. * so they will have the same order as in the source list
  918. */
  919. if (avp_mark) {
  920. avp_mark=avp_mark->next;
  921. } else {
  922. avp_mark=search_first_avp(flags, a->val[0].u.attr->name, NULL, NULL);
  923. }
  924. avp = search_next_avp(&st, &value);
  925. }
  926. ret = 1;
  927. break;
  928. } else {
  929. avp = search_avp_by_index(a->val[1].u.attr->type, a->val[1].u.attr->name, &value, a->val[1].u.attr->index);
  930. if (avp) {
  931. flags = a->val[0].u.attr->type | (avp->flags & ~(AVP_CLASS_ALL|AVP_TRACK_ALL));
  932. name = a->val[0].u.attr->name;
  933. ret = 1;
  934. } else {
  935. ret = E_UNSPEC;
  936. break;
  937. }
  938. }
  939. } else if (a->val[1].type == SELECT_ST) {
  940. int r;
  941. r = run_select(&value.s, a->val[1].u.select, msg);
  942. if (r < 0) {
  943. ret=E_UNSPEC;
  944. break;
  945. } else if (r > 0) {
  946. value.s.s = "";
  947. value.s.len = 0;
  948. }
  949. flags = a->val[0].u.attr->type | AVP_VAL_STR;
  950. name = a->val[0].u.attr->name;
  951. ret = 1;
  952. } else {
  953. LOG(L_CRIT, "BUG: do_action: Bad right side of avp assignment\n");
  954. ret=E_BUG;
  955. break;
  956. }
  957. /* If the action is assign then remove the old avp value
  958. * before adding new ones */
  959. /* if ((unsigned char)a->type == ASSIGN_T) delete_avp(flags, name); */
  960. if (add_avp(flags & ~AVP_INDEX_ALL, name, value) < 0) {
  961. LOG(L_CRIT, "ERROR: Failed to assign value to attribute\n");
  962. ret=E_UNSPEC;
  963. break;
  964. }
  965. break;
  966. default:
  967. LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
  968. }
  969. /*skip:*/
  970. return ret;
  971. error_uri:
  972. LOG(L_ERR, "ERROR: do_action: set*: uri too long\n");
  973. if (new_uri) pkg_free(new_uri);
  974. return E_UNSPEC;
  975. error_fwd_uri:
  976. /*free_uri(&uri); -- not needed anymore, using msg->parsed_uri*/
  977. return ret;
  978. }
  979. /* returns: 0, or 1 on success, <0 on error */
  980. /* (0 if drop or break encountered, 1 if not ) */
  981. int run_actions(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
  982. {
  983. struct action* t;
  984. int ret;
  985. struct sr_module *mod;
  986. ret=E_UNSPEC;
  987. h->rec_lev++;
  988. if (h->rec_lev>ROUTE_MAX_REC_LEV){
  989. LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
  990. " giving up!\n", h->rec_lev);
  991. ret=E_UNSPEC;
  992. goto error;
  993. }
  994. if (h->rec_lev==1){
  995. h->run_flags=0;
  996. h->last_retcode=0;
  997. #ifdef USE_LONGJMP
  998. if (setjmp(h->jmp_env)){
  999. h->rec_lev=0;
  1000. ret=h->last_retcode;
  1001. goto end;
  1002. }
  1003. #endif
  1004. }
  1005. if (a==0){
  1006. DBG("DEBUG: run_actions: null action list (rec_level=%d)\n",
  1007. h->rec_lev);
  1008. ret=1;
  1009. }
  1010. for (t=a; t!=0; t=t->next){
  1011. ret=do_action(h, t, msg);
  1012. if (h->run_flags & (RETURN_R_F|EXIT_R_F)){
  1013. if (h->run_flags & EXIT_R_F){
  1014. #ifdef USE_LONGJMP
  1015. h->last_retcode=ret;
  1016. longjmp(h->jmp_env, ret);
  1017. #endif
  1018. }
  1019. break;
  1020. }
  1021. /* ignore error returns */
  1022. }
  1023. h->rec_lev--;
  1024. end:
  1025. /* process module onbreak handlers if present */
  1026. if (h->rec_lev==0 && ret==0)
  1027. for (mod=modules;mod;mod=mod->next)
  1028. if (mod->exports && mod->exports->onbreak_f) {
  1029. mod->exports->onbreak_f( msg );
  1030. DBG("DEBUG: %s onbreak handler called\n", mod->exports->name);
  1031. }
  1032. return ret;
  1033. error:
  1034. h->rec_lev--;
  1035. return ret;
  1036. }