action.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <netdb.h>
  45. #include <stdlib.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <string.h>
  49. #ifdef DEBUG_DMALLOC
  50. #include <dmalloc.h>
  51. #endif
  52. /* ret= 0! if action -> end of list(e.g DROP),
  53. > 0 to continue processing next actions
  54. and <0 on error */
  55. int do_action(struct action* a, struct sip_msg* msg)
  56. {
  57. int ret;
  58. int v;
  59. union sockaddr_union* to;
  60. struct socket_info* send_sock;
  61. struct proxy_l* p;
  62. char* tmp;
  63. char *new_uri, *end, *crt;
  64. int len;
  65. int user;
  66. int err;
  67. struct sip_uri uri;
  68. struct sip_uri* u;
  69. unsigned short port;
  70. /* reset the value of error to E_UNSPEC so avoid unknowledgable
  71. functions to return with errror (status<0) and not setting it
  72. leaving there previous error; cache the previous value though
  73. for functions which want to process it */
  74. prev_ser_error=ser_error;
  75. ser_error=E_UNSPEC;
  76. ret=E_BUG;
  77. switch (a->type){
  78. case DROP_T:
  79. ret=0;
  80. break;
  81. case FORWARD_T:
  82. if (a->p1_type==URIHOST_ST){
  83. /*parse uri*/
  84. ret=parse_sip_msg_uri(msg);
  85. if (ret<0) {
  86. LOG(L_ERR, "ERROR: do_action: forward: bad_uri "
  87. " dropping packet\n");
  88. break;
  89. }
  90. u=&msg->parsed_uri;
  91. switch (a->p2_type){
  92. case URIPORT_ST:
  93. if (u->port.s){
  94. /*port=strtol(uri.port.s,&end,10);*/
  95. port=str2s((unsigned char*)u->port.s,
  96. u->port.len, &err);
  97. /*if ((end)&&(*end)){*/
  98. if (err){
  99. LOG(L_ERR, "ERROR: do_action: "
  100. "forward: bad port in "
  101. "uri: <%s>\n", u->port.s);
  102. ret=E_BAD_URI;
  103. goto error_fwd_uri;
  104. }
  105. }else port=0;
  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.s, 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);
  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);
  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. if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){
  140. LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
  141. a->p1_type, a->p2_type);
  142. ret=E_BUG;
  143. break;
  144. }
  145. to=(union sockaddr_union*) malloc(sizeof(union sockaddr_union));
  146. if (to==0){
  147. LOG(L_ERR, "ERROR: do_action: "
  148. "memory allocation failure\n");
  149. ret=E_OUT_OF_MEM;
  150. break;
  151. }
  152. p=(struct proxy_l*)a->p1.data;
  153. if (p->ok==0){
  154. if (p->host.h_addr_list[p->addr_idx+1])
  155. p->addr_idx++;
  156. else
  157. p->addr_idx=0;
  158. p->ok=1;
  159. }
  160. ret=hostent2su( to, &p->host, p->addr_idx,
  161. (p->port)?htons(p->port):htons(SIP_PORT) );
  162. if (ret==0){
  163. p->tx++;
  164. p->tx_bytes+=msg->len;
  165. send_sock=get_send_socket(to);
  166. if (send_sock!=0){
  167. ret=udp_send(send_sock, msg->orig, msg->len, to);
  168. }else{
  169. ret=-1;
  170. }
  171. }
  172. free(to);
  173. if (ret<0){
  174. p->errors++;
  175. p->ok=0;
  176. }else ret=1;
  177. break;
  178. case LOG_T:
  179. if ((a->p1_type!=NUMBER_ST)|(a->p2_type!=STRING_ST)){
  180. LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
  181. a->p1_type, a->p2_type);
  182. ret=E_BUG;
  183. break;
  184. }
  185. LOG(a->p1.number, a->p2.string);
  186. ret=1;
  187. break;
  188. /* jku -- introduce a new branch */
  189. case APPEND_BRANCH_T:
  190. if ((a->p1_type!=STRING_ST)) {
  191. LOG(L_CRIT, "BUG: do_action: bad append_branch_t %d\n",
  192. a->p1_type );
  193. ret=E_BUG;
  194. break;
  195. }
  196. ret=append_branch( msg, a->p1.string,
  197. a->p1.string ? strlen(a->p1.string):0 );
  198. break;
  199. /* jku begin: is_length_greater_than */
  200. case LEN_GT_T:
  201. if (a->p1_type!=NUMBER_ST) {
  202. LOG(L_CRIT, "BUG: do_action: bad len_gt type %d\n",
  203. a->p1_type );
  204. ret=E_BUG;
  205. break;
  206. }
  207. /* DBG("XXX: message length %d, max %d\n",
  208. msg->len, a->p1.number ); */
  209. ret = msg->len >= a->p1.number ? 1 : -1;
  210. break;
  211. /* jku end: is_length_greater_than */
  212. /* jku - begin : flag processing */
  213. case SETFLAG_T:
  214. if (a->p1_type!=NUMBER_ST) {
  215. LOG(L_CRIT, "BUG: do_action: bad setflag() type %d\n",
  216. a->p1_type );
  217. ret=E_BUG;
  218. break;
  219. }
  220. if (!flag_in_range( a->p1.number )) {
  221. ret=E_CFG;
  222. break;
  223. }
  224. setflag( msg, a->p1.number );
  225. ret=1;
  226. break;
  227. case RESETFLAG_T:
  228. if (a->p1_type!=NUMBER_ST) {
  229. LOG(L_CRIT, "BUG: do_action: bad resetflag() type %d\n",
  230. a->p1_type );
  231. ret=E_BUG;
  232. break;
  233. }
  234. if (!flag_in_range( a->p1.number )) {
  235. ret=E_CFG;
  236. break;
  237. }
  238. resetflag( msg, a->p1.number );
  239. ret=1;
  240. break;
  241. case ISFLAGSET_T:
  242. if (a->p1_type!=NUMBER_ST) {
  243. LOG(L_CRIT, "BUG: do_action: bad isflagset() type %d\n",
  244. a->p1_type );
  245. ret=E_BUG;
  246. break;
  247. }
  248. if (!flag_in_range( a->p1.number )) {
  249. ret=E_CFG;
  250. break;
  251. }
  252. ret=isflagset( msg, a->p1.number );
  253. break;
  254. /* jku - end : flag processing */
  255. case ERROR_T:
  256. if ((a->p1_type!=STRING_ST)|(a->p2_type!=STRING_ST)){
  257. LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
  258. a->p1_type, a->p2_type);
  259. ret=E_BUG;
  260. break;
  261. }
  262. LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
  263. "not implemented yet\n", a->p1.string, a->p2.string);
  264. ret=1;
  265. break;
  266. case ROUTE_T:
  267. if (a->p1_type!=NUMBER_ST){
  268. LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
  269. a->p1_type);
  270. ret=E_BUG;
  271. break;
  272. }
  273. if ((a->p1.number>RT_NO)||(a->p1.number<0)){
  274. LOG(L_ERR, "ERROR: invalid routing table number in"
  275. "route(%d)\n", a->p1.number);
  276. ret=E_CFG;
  277. break;
  278. }
  279. ret=((ret=run_actions(rlist[a->p1.number], msg))<0)?ret:1;
  280. break;
  281. case EXEC_T:
  282. if (a->p1_type!=STRING_ST){
  283. LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
  284. a->p1_type);
  285. ret=E_BUG;
  286. break;
  287. }
  288. LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
  289. " using dumb version...\n", a->p1.string);
  290. ret=system(a->p1.string);
  291. if (ret!=0){
  292. LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
  293. }
  294. ret=1;
  295. break;
  296. case REVERT_URI_T:
  297. if (msg->new_uri.s) {
  298. pkg_free(msg->new_uri.s);
  299. msg->new_uri.len=0;
  300. msg->new_uri.s=0;
  301. if (msg->parsed_uri_ok){
  302. msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
  303. free_uri(&msg->parsed_uri);
  304. }
  305. };
  306. ret=1;
  307. break;
  308. case SET_HOST_T:
  309. case SET_HOSTPORT_T:
  310. case SET_USER_T:
  311. case SET_USERPASS_T:
  312. case SET_PORT_T:
  313. case SET_URI_T:
  314. case PREFIX_T:
  315. case STRIP_T:
  316. user=0;
  317. if (a->type==STRIP_T) {
  318. if (a->p1_type!=NUMBER_ST) {
  319. LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
  320. a->p1_type);
  321. break;
  322. }
  323. } else if (a->p1_type!=STRING_ST){
  324. LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
  325. a->p1_type);
  326. ret=E_BUG;
  327. break;
  328. }
  329. if (a->type==SET_URI_T){
  330. if (msg->new_uri.s) {
  331. pkg_free(msg->new_uri.s);
  332. msg->new_uri.len=0;
  333. if (msg->parsed_uri_ok){
  334. msg->parsed_uri_ok=0;
  335. free_uri(&msg->parsed_uri);
  336. }
  337. }
  338. len=strlen(a->p1.string);
  339. msg->new_uri.s=pkg_malloc(len+1);
  340. if (msg->new_uri.s==0){
  341. LOG(L_ERR, "ERROR: do_action: memory allocation"
  342. " failure\n");
  343. ret=E_OUT_OF_MEM;
  344. break;
  345. }
  346. memcpy(msg->new_uri.s, a->p1.string, len);
  347. msg->new_uri.s[len]=0;
  348. msg->new_uri.len=len;
  349. ret=1;
  350. break;
  351. }
  352. if (msg->new_uri.s) {
  353. tmp=msg->new_uri.s;
  354. len=msg->new_uri.len;
  355. }else{
  356. tmp=msg->first_line.u.request.uri.s;
  357. len=msg->first_line.u.request.uri.len;
  358. }
  359. if (parse_uri(tmp, len, &uri)<0){
  360. LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
  361. " packet\n", tmp);
  362. ret=E_UNSPEC;
  363. break;
  364. }
  365. new_uri=pkg_malloc(MAX_URI_SIZE);
  366. if (new_uri==0){
  367. LOG(L_ERR, "ERROR: do_action: memory allocation "
  368. " failure\n");
  369. ret=E_OUT_OF_MEM;
  370. free_uri(&uri);
  371. break;
  372. }
  373. end=new_uri+MAX_URI_SIZE;
  374. crt=new_uri;
  375. /* begin copying */
  376. len=strlen("sip:"); if(crt+len>end) goto error_uri;
  377. memcpy(crt,"sip:",len);crt+=len;
  378. /* user */
  379. /* prefix (-jiri) */
  380. if (a->type==PREFIX_T) {
  381. tmp=a->p1.string;
  382. len=strlen(tmp); if(crt+len>end) goto error_uri;
  383. memcpy(crt,tmp,len);crt+=len;
  384. /* whateever we had before, with prefix we have username now */
  385. user=1;
  386. }
  387. if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
  388. tmp=a->p1.string;
  389. len=strlen(tmp);
  390. } else if (a->type==STRIP_T) {
  391. if (a->p1.number>uri.user.len) {
  392. LOG(L_WARN, "Error: too long strip asked; deleting username: "
  393. "%d of %s\n", a->p1.number, uri.user.s );
  394. len=0;
  395. } else if (a->p1.number==uri.user.len) {
  396. len=0;
  397. } else {
  398. tmp=uri.user.s + a->p1.number;
  399. len=uri.user.len - a->p1.number;
  400. }
  401. } else {
  402. tmp=uri.user.s;
  403. len=uri.user.len;
  404. }
  405. if (len){
  406. if(crt+len>end) goto error_uri;
  407. memcpy(crt,tmp,len);crt+=len;
  408. user=1; /* we have an user field so mark it */
  409. }
  410. if (a->type==SET_USERPASS_T) tmp=0;
  411. else tmp=uri.passwd.s;
  412. /* passwd */
  413. if (tmp){
  414. len=strlen(":"); if(crt+len>end) goto error_uri;
  415. memcpy(crt,":",len);crt+=len;
  416. len=strlen(tmp); if(crt+len>end) goto error_uri;
  417. memcpy(crt,tmp,len);crt+=len;
  418. }
  419. /* host */
  420. if (user || tmp){ /* add @ */
  421. len=strlen("@"); if(crt+len>end) goto error_uri;
  422. memcpy(crt,"@",len);crt+=len;
  423. }
  424. if ((a->type==SET_HOST_T) ||(a->type==SET_HOSTPORT_T))
  425. tmp=a->p1.string;
  426. else
  427. tmp=uri.host.s;
  428. if (tmp){
  429. len=strlen(tmp); if(crt+len>end) goto error_uri;
  430. memcpy(crt,tmp,len);crt+=len;
  431. }
  432. /* port */
  433. if (a->type==SET_HOSTPORT_T) tmp=0;
  434. else if (a->type==SET_PORT_T) tmp=a->p1.string;
  435. else tmp=uri.port.s;
  436. if (tmp){
  437. len=strlen(":"); if(crt+len>end) goto error_uri;
  438. memcpy(crt,":",len);crt+=len;
  439. len=strlen(tmp); if(crt+len>end) goto error_uri;
  440. memcpy(crt,tmp,len);crt+=len;
  441. }
  442. /* params */
  443. tmp=uri.params.s;
  444. if (tmp){
  445. len=strlen(";"); if(crt+len>end) goto error_uri;
  446. memcpy(crt,";",len);crt+=len;
  447. len=strlen(tmp); if(crt+len>end) goto error_uri;
  448. memcpy(crt,tmp,len);crt+=len;
  449. }
  450. /* headers */
  451. tmp=uri.headers.s;
  452. if (tmp){
  453. len=strlen("?"); if(crt+len>end) goto error_uri;
  454. memcpy(crt,"?",len);crt+=len;
  455. len=strlen(tmp); if(crt+len>end) goto error_uri;
  456. memcpy(crt,tmp,len);crt+=len;
  457. }
  458. *crt=0; /* null terminate the thing */
  459. /* copy it to the msg */
  460. if (msg->new_uri.s) pkg_free(msg->new_uri.s);
  461. msg->new_uri.s=new_uri;
  462. msg->new_uri.len=crt-new_uri;
  463. if (msg->parsed_uri_ok){
  464. msg->parsed_uri_ok=0;
  465. free_uri(&msg->parsed_uri);
  466. }
  467. free_uri(&uri);
  468. ret=1;
  469. break;
  470. case IF_T:
  471. /* if null expr => ignore if? */
  472. if ((a->p1_type==EXPR_ST)&&a->p1.data){
  473. v=eval_expr((struct expr*)a->p1.data, msg);
  474. if (v<0){
  475. if (v==EXPR_DROP){ /* hack to quit on DROP*/
  476. ret=0;
  477. break;
  478. }else{
  479. LOG(L_WARN,"WARNING: do_action:"
  480. "error in expression\n");
  481. }
  482. }
  483. ret=1; /*default is continue */
  484. if (v>0) {
  485. if ((a->p2_type==ACTIONS_ST)&&a->p2.data){
  486. ret=run_actions((struct action*)a->p2.data, msg);
  487. }
  488. }else if ((a->p3_type==ACTIONS_ST)&&a->p3.data){
  489. ret=run_actions((struct action*)a->p3.data, msg);
  490. }
  491. }
  492. break;
  493. case MODULE_T:
  494. if ( ((a->p1_type==CMDF_ST)&&a->p1.data)/*&&
  495. ((a->p2_type==STRING_ST)&&a->p2.data)*/ ){
  496. ret=((cmd_function)(a->p1.data))(msg, (char*)a->p2.data,
  497. (char*)a->p3.data);
  498. }else{
  499. LOG(L_CRIT,"BUG: do_action: bad module call\n");
  500. }
  501. break;
  502. default:
  503. LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
  504. }
  505. /*skip:*/
  506. return ret;
  507. error_uri:
  508. LOG(L_ERR, "ERROR: do_action: set*: uri too long\n");
  509. free_uri(&uri);
  510. if (new_uri) free(new_uri);
  511. return E_UNSPEC;
  512. error_fwd_uri:
  513. /*free_uri(&uri); -- not needed anymore, using msg->parsed_uri*/
  514. return ret;
  515. }
  516. /* returns: 0, or 1 on success, <0 on error */
  517. /* (0 if drop or break encountered, 1 if not ) */
  518. int run_actions(struct action* a, struct sip_msg* msg)
  519. {
  520. struct action* t;
  521. int ret=E_UNSPEC;
  522. static int rec_lev=0;
  523. struct sr_module *mod;
  524. rec_lev++;
  525. if (rec_lev>ROUTE_MAX_REC_LEV){
  526. LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
  527. " giving up!\n", rec_lev);
  528. ret=E_UNSPEC;
  529. goto error;
  530. }
  531. if (a==0){
  532. LOG(L_ERR, "WARNING: run_actions: null action list (rec_level=%d)\n",
  533. rec_lev);
  534. ret=0;
  535. }
  536. for (t=a; t!=0; t=t->next){
  537. ret=do_action(t, msg);
  538. if(ret==0) break;
  539. /* ignore errors */
  540. /*else if (ret<0){ ret=-1; goto error; }*/
  541. }
  542. rec_lev--;
  543. /* process module onbreak handlers if present */
  544. if (rec_lev==0 && ret==0)
  545. for (mod=modules;mod;mod=mod->next)
  546. if (mod->exports && mod->exports->onbreak_f) {
  547. mod->exports->onbreak_f( msg );
  548. DBG("DEBUG: %s onbreak handler called\n", mod->exports->name);
  549. }
  550. return ret;
  551. error:
  552. rec_lev--;
  553. return ret;
  554. }