5 * Copyright (C) 2001-2003 FhG Fokus
7 * This file is part of ser, a free SIP server.
9 * ser is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version
14 * For a license to use the ser software under conditions
15 * other than those described here, or to purchase support for this
16 * software, please contact iptel.org by e-mail at the following addresses:
19 * ser is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * 2003-02-28 scratchpad compatibility abandoned (jiri)
31 * 2003-01-29 removed scratchpad (jiri)
32 * 2003-03-19 fixed set* len calculation bug & simplified a little the code
33 * (should be a little faster now) (andrei)
34 * replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
35 * 2003-04-01 Added support for loose routing in forward (janakj)
36 * 2003-04-12 FORCE_RPORT_T added (andrei)
37 * 2003-04-22 strip_tail added (jiri)
38 * 2003-10-02 added SET_ADV_ADDR_T & SET_ADV_PORT_T (andrei)
39 * 2003-10-29 added FORCE_TCP_ALIAS_T (andrei)
40 * 2004-11-30 added FORCE_SEND_SOCKET_T (andrei)
41 * 2005-12-12 return & drop/exit differentiation (andrei)
42 * 2005-12-19 select framework (mma)
43 * 2006-04-12 updated *_send() calls to use a struct dest_info (andrei)
44 * 2006-07-27 dns cache and dns based send address failover support (andrei)
45 * 2006-12-06 on popular request last_retcode set also by module functions
47 * 2007-06-14 run_actions & do_action need a ctx or handle now, no more
48 * static vars (andrei)
52 #include "comp_defs.h"
60 #include "udp_server.h"
62 #include "parser/msg_parser.h"
63 #include "parser/parse_uri.h"
65 #include "sr_module.h"
72 #include "tcp_server.h"
75 #include "sctp_server.h"
78 #include <sys/types.h>
79 #include <sys/socket.h>
82 #include <netinet/in.h>
83 #include <arpa/inet.h>
92 struct onsend_info* p_onsend=0; /* onsend route send info */
94 /* ret= 0! if action -> end of list(e.g DROP),
95 > 0 to continue processing next actions
97 int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
101 struct dest_info dst;
103 char *new_uri, *end, *crt;
106 struct sip_uri uri, next_hop;
109 unsigned short flags;
113 /* reset the value of error to E_UNSPEC so avoid unknowledgable
114 functions to return with error (status<0) and not setting it
115 leaving there previous error; cache the previous value though
116 for functions which want to process it */
117 prev_ser_error=ser_error;
121 switch ((unsigned char)a->type){
123 if (a->val[0].type==RETCODE_ST)
126 ret=(int) a->val[0].u.number;
127 h->run_flags|=(unsigned int)a->val[1].u.number;
141 init_dest_info(&dst);
142 if (a->type==FORWARD_UDP_T) dst.proto=PROTO_UDP;
144 else if (a->type==FORWARD_TCP_T) dst.proto= PROTO_TCP;
147 else if (a->type==FORWARD_TLS_T) dst.proto= PROTO_TLS;
150 else if (a->type==FORWARD_SCTP_T) dst.proto=PROTO_SCTP;
152 else dst.proto=PROTO_NONE;
153 if (a->val[0].type==URIHOST_ST){
156 if (msg->dst_uri.len) {
157 ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len,
161 ret = parse_sip_msg_uri(msg);
162 u = &msg->parsed_uri;
166 LOG(L_ERR, "ERROR: do_action: forward: bad_uri "
167 " dropping packet\n");
171 switch (a->val[1].type){
176 port=a->val[1].u.number;
179 LOG(L_CRIT, "BUG: do_action bad forward 2nd"
180 " param type (%d)\n", a->val[1].type);
184 if (dst.proto == PROTO_NONE){ /* only if proto not set get it
188 /*dst.proto=PROTO_UDP; */
189 /* no proto, try to get it from the dns */
204 LOG(L_ERR,"ERROR: do action: forward: bad uri"
205 " transport %d\n", u->proto);
210 if (u->type==SIPS_URI_T){
211 if (u->proto==PROTO_UDP){
212 LOG(L_ERR, "ERROR: do_action: forward: secure uri"
213 " incompatible with transport %d\n",
224 if (u->maddr_val.s && u->maddr_val.len)
225 dst_host=&u->maddr_val;
232 ret=forward_request(msg, dst_host, port, &dst);
236 }else if ((a->val[0].type==PROXY_ST) && (a->val[1].type==NUMBER_ST)){
237 if (dst.proto==PROTO_NONE)
238 dst.proto=msg->rcv.proto;
239 proxy2su(&dst.to, (struct proxy_l*)a->val[0].u.data);
240 ret=forward_request(msg, 0, 0, &dst);
243 proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
244 }else if (ser_error!=E_OK){
245 proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
248 LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
249 a->val[0].type, a->val[1].type);
255 if ((a->val[0].type!= PROXY_ST)|(a->val[1].type!=NUMBER_ST)){
256 LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
257 a->val[0].type, a->val[1].type);
262 init_dest_info(&dst);
263 ret=proxy2su(&dst.to, (struct proxy_l*)a->val[0].u.data);
272 if (a->type==SEND_T){
274 dst.proto=PROTO_UDP; /* not really needed for udp_send */
275 dst.send_sock=get_send_socket(msg, &dst.to, PROTO_UDP);
276 if (dst.send_sock!=0){
277 ret=udp_send(&dst, tmp, len);
287 ret=tcp_send(&dst, 0, tmp, len);
294 proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
299 if ((a->val[0].type!=NUMBER_ST)|(a->val[1].type!=STRING_ST)){
300 LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
301 a->val[0].type, a->val[1].type);
305 LOG(a->val[0].u.number, "%s", a->val[1].u.string);
309 /* jku -- introduce a new branch */
310 case APPEND_BRANCH_T:
311 if ((a->val[0].type!=STRING_ST)) {
312 LOG(L_CRIT, "BUG: do_action: bad append_branch_t %d\n",
317 ret=append_branch( msg, a->val[0].u.string,
318 a->val[0].u.string ? strlen(a->val[0].u.string):0,
319 0, 0, a->val[1].u.number, 0);
322 /* jku begin: is_length_greater_than */
324 if (a->val[0].type!=NUMBER_ST) {
325 LOG(L_CRIT, "BUG: do_action: bad len_gt type %d\n",
330 /* DBG("XXX: message length %d, max %d\n",
331 msg->len, a->val[0].u.number ); */
332 ret = msg->len >= a->val[0].u.number ? 1 : -1;
334 /* jku end: is_length_greater_than */
336 /* jku - begin : flag processing */
339 if (a->val[0].type!=NUMBER_ST) {
340 LOG(L_CRIT, "BUG: do_action: bad setflag() type %d\n",
345 if (!flag_in_range( a->val[0].u.number )) {
349 setflag( msg, a->val[0].u.number );
354 if (a->val[0].type!=NUMBER_ST) {
355 LOG(L_CRIT, "BUG: do_action: bad resetflag() type %d\n",
360 if (!flag_in_range( a->val[0].u.number )) {
364 resetflag( msg, a->val[0].u.number );
369 if (a->val[0].type!=NUMBER_ST) {
370 LOG(L_CRIT, "BUG: do_action: bad isflagset() type %d\n",
375 if (!flag_in_range( a->val[0].u.number )) {
379 ret=isflagset( msg, a->val[0].u.number );
381 /* jku - end : flag processing */
383 case AVPFLAG_OPER_T: {
384 struct search_state st;
388 flag = a->val[1].u.number;
389 if ((a->val[0].u.attr->type & AVP_INDEX_ALL) == AVP_INDEX_ALL || (a->val[0].u.attr->type & AVP_NAME_RE)!=0) {
390 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)) {
391 switch (a->val[2].u.number) { /* oper: 0..reset, 1..set, -1..no change */
393 avp->flags &= ~(avp_flags_t)a->val[1].u.number;
396 avp->flags |= (avp_flags_t)a->val[1].u.number;
400 ret = ret || ((avp->flags & (avp_flags_t)a->val[1].u.number) != 0);
404 avp = search_avp_by_index(a->val[0].u.attr->type, a->val[0].u.attr->name, NULL, a->val[0].u.attr->index);
406 switch (a->val[2].u.number) { /* oper: 0..reset, 1..set, -1..no change */
408 avp->flags &= ~(avp_flags_t)a->val[1].u.number;
411 avp->flags |= (avp_flags_t)a->val[1].u.number;
415 ret = (avp->flags & (avp_flags_t)a->val[1].u.number) != 0;
423 if ((a->val[0].type!=STRING_ST)|(a->val[1].type!=STRING_ST)){
424 LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
425 a->val[0].type, a->val[1].type);
429 LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
430 "not implemented yet\n", a->val[0].u.string, a->val[1].u.string);
434 if (a->val[0].type!=NUMBER_ST){
435 LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
440 if ((a->val[0].u.number>=main_rt.idx)||(a->val[0].u.number<0)){
441 LOG(L_ERR, "ERROR: invalid routing table number in"
442 "route(%lu)\n", a->val[0].u.number);
446 /*ret=((ret=run_actions(rlist[a->val[0].u.number], msg))<0)?ret:1;*/
447 ret=run_actions(h, main_rt.rlist[a->val[0].u.number], msg);
449 h->run_flags&=~RETURN_R_F; /* absorb returns */
452 if (a->val[0].type!=STRING_ST){
453 LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
458 LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
459 " using dumb version...\n", a->val[0].u.string);
460 ret=system(a->val[0].u.string);
462 LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
467 if (msg->new_uri.s) {
468 pkg_free(msg->new_uri.s);
471 msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
477 case SET_HOSTPORTTRANS_T:
486 if (a->type==STRIP_T || a->type==STRIP_TAIL_T) {
487 if (a->val[0].type!=NUMBER_ST) {
488 LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
492 } else if (a->val[0].type!=STRING_ST){
493 LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
498 if (a->type==SET_URI_T){
499 if (msg->new_uri.s) {
500 pkg_free(msg->new_uri.s);
503 msg->parsed_uri_ok=0;
504 len=strlen(a->val[0].u.string);
505 msg->new_uri.s=pkg_malloc(len+1);
506 if (msg->new_uri.s==0){
507 LOG(L_ERR, "ERROR: do_action: memory allocation"
512 memcpy(msg->new_uri.s, a->val[0].u.string, len);
513 msg->new_uri.s[len]=0;
514 msg->new_uri.len=len;
519 if (msg->parsed_uri_ok==0) {
520 if (msg->new_uri.s) {
522 len=msg->new_uri.len;
524 tmp=msg->first_line.u.request.uri.s;
525 len=msg->first_line.u.request.uri.len;
527 if (parse_uri(tmp, len, &uri)<0){
528 LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
537 new_uri=pkg_malloc(MAX_URI_SIZE);
539 LOG(L_ERR, "ERROR: do_action: memory allocation "
544 end=new_uri+MAX_URI_SIZE;
547 len=strlen("sip:"); if(crt+len>end) goto error_uri;
548 memcpy(crt,"sip:",len);crt+=len;
553 if (a->type==PREFIX_T) {
554 tmp=a->val[0].u.string;
555 len=strlen(tmp); if(crt+len>end) goto error_uri;
556 memcpy(crt,tmp,len);crt+=len;
557 /* whatever we had before, with prefix we have username
562 if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
563 tmp=a->val[0].u.string;
565 } else if (a->type==STRIP_T) {
566 if (a->val[0].u.number>uri.user.len) {
567 LOG(L_WARN, "Error: too long strip asked; "
568 " deleting username: %lu of <%.*s>\n",
569 a->val[0].u.number, uri.user.len, uri.user.s );
571 } else if (a->val[0].u.number==uri.user.len) {
574 tmp=uri.user.s + a->val[0].u.number;
575 len=uri.user.len - a->val[0].u.number;
577 } else if (a->type==STRIP_TAIL_T) {
578 if (a->val[0].u.number>uri.user.len) {
579 LOG(L_WARN, "WARNING: too long strip_tail asked; "
580 " deleting username: %lu of <%.*s>\n",
581 a->val[0].u.number, uri.user.len, uri.user.s );
583 } else if (a->val[0].u.number==uri.user.len) {
587 len=uri.user.len - a->val[0].u.number;
595 if(crt+len>end) goto error_uri;
596 memcpy(crt,tmp,len);crt+=len;
597 user=1; /* we have an user field so mark it */
600 if (a->type==SET_USERPASS_T) tmp=0;
601 else tmp=uri.passwd.s;
604 len=uri.passwd.len; if(crt+len+1>end) goto error_uri;
606 memcpy(crt,tmp,len);crt+=len;
609 if (user || tmp){ /* add @ */
610 if(crt+1>end) goto error_uri;
613 if ((a->type==SET_HOST_T)
614 || (a->type==SET_HOSTPORT_T)
615 || (a->type==SET_HOSTPORTTRANS_T)) {
616 tmp=a->val[0].u.string;
617 if (tmp) len = strlen(tmp);
624 if(crt+len>end) goto error_uri;
625 memcpy(crt,tmp,len);crt+=len;
628 if ((a->type==SET_HOSTPORT_T)
629 || (a->type==SET_HOSTPORTTRANS_T))
631 else if (a->type==SET_PORT_T) {
632 tmp=a->val[0].u.string;
633 if (tmp) len = strlen(tmp);
640 if(crt+len+1>end) goto error_uri;
642 memcpy(crt,tmp,len);crt+=len;
645 if ((a->type==SET_HOSTPORTTRANS_T) && uri.transport.s) {
646 /* bypass the transport parameter */
647 if (uri.params.s < uri.transport.s) {
648 /* there are parameters before transport */
649 len = uri.transport.s - uri.params.s - 1;
650 /* ignore the ';' at the end */
651 if (crt+len+1>end) goto error_uri;
653 memcpy(crt,uri.params.s,len);crt+=len;
655 len = (uri.params.s + uri.params.len) -
656 (uri.transport.s + uri.transport.len);
658 /* there are parameters after transport */
659 if (crt+len>end) goto error_uri;
660 tmp = uri.transport.s + uri.transport.len;
661 memcpy(crt,tmp,len);crt+=len;
666 len=uri.params.len; if(crt+len+1>end) goto error_uri;
668 memcpy(crt,tmp,len);crt+=len;
674 len=uri.headers.len; if(crt+len+1>end) goto error_uri;
676 memcpy(crt,tmp,len);crt+=len;
678 *crt=0; /* null terminate the thing */
679 /* copy it to the msg */
680 if (msg->new_uri.s) pkg_free(msg->new_uri.s);
681 msg->new_uri.s=new_uri;
682 msg->new_uri.len=crt-new_uri;
683 msg->parsed_uri_ok=0;
687 /* if null expr => ignore if? */
688 if ((a->val[0].type==EXPR_ST)&&a->val[0].u.data){
689 v=eval_expr(h, (struct expr*)a->val[0].u.data, msg);
692 if (v==EXPR_DROP){ /* hack to quit on DROP*/
696 LOG(L_WARN,"WARNING: do_action:"
697 "error in expression\n");
701 if (h->run_flags & EXIT_R_F){
705 h->run_flags &= ~RETURN_R_F; /* catch returns in expr */
706 ret=1; /*default is continue */
708 if ((a->val[1].type==ACTIONS_ST)&&a->val[1].u.data){
710 (struct action*)a->val[1].u.data, msg);
712 }else if ((a->val[2].type==ACTIONS_ST)&&a->val[2].u.data){
714 (struct action*)a->val[2].u.data, msg);
719 if ( a->val[0].type==MODEXP_ST && a->val[0].u.data && ((cmd_export_t*)a->val[0].u.data)->function ){
720 ret=((cmd_export_t*)a->val[0].u.data)->function(msg,
721 (char*)a->val[2].u.data,
722 (char*)a->val[3].u.data
724 if (ret==0) h->run_flags|=EXIT_R_F;
727 LOG(L_CRIT,"BUG: do_action: bad module call\n");
731 msg->msg_flags|=FL_FORCE_RPORT;
732 ret=1; /* continue processing */
735 if (a->val[0].type!=STR_ST){
736 LOG(L_CRIT, "BUG: do_action: bad set_advertised_address() "
737 "type %d\n", a->val[0].type);
741 msg->set_global_address=*((str*)a->val[0].u.data);
742 ret=1; /* continue processing */
745 if (a->val[0].type!=STR_ST){
746 LOG(L_CRIT, "BUG: do_action: bad set_advertised_port() "
747 "type %d\n", a->val[0].type);
751 msg->set_global_port=*((str*)a->val[0].u.data);
752 ret=1; /* continue processing */
755 case FORCE_TCP_ALIAS_T:
756 if ( msg->rcv.proto==PROTO_TCP
758 || msg->rcv.proto==PROTO_TLS
762 if (a->val[0].type==NOSUBTYPE) port=msg->via1->port;
763 else if (a->val[0].type==NUMBER_ST) port=(int)a->val[0].u.number;
765 LOG(L_CRIT, "BUG: do_action: bad force_tcp_alias"
766 " port type %d\n", a->val[0].type);
771 if (tcpconn_add_alias(msg->rcv.proto_reserved1, port,
773 LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n");
779 ret=1; /* continue processing */
781 case FORCE_SEND_SOCKET_T:
782 if (a->val[0].type!=SOCKETINFO_ST){
783 LOG(L_CRIT, "BUG: do_action: bad force_send_socket argument"
784 " type: %d\n", a->val[0].type);
788 msg->force_send_socket=(struct socket_info*)a->val[0].u.data;
789 ret=1; /* continue processing */
795 /* If the left attr was specified without indexing brackets delete
796 * existing AVPs before adding new ones
798 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);
800 if (a->val[1].type == STRING_ST) {
801 value.s = a->val[1].u.str;
802 flags = a->val[0].u.attr->type | AVP_VAL_STR;
803 name = a->val[0].u.attr->name;
805 } else if (a->val[1].type == NUMBER_ST) {
806 value.n = a->val[1].u.number;
807 flags = a->val[0].u.attr->type;
808 name = a->val[0].u.attr->name;
810 } else if (a->val[1].type == ACTION_ST) {
811 flags = a->val[0].u.attr->type;
812 name = a->val[0].u.attr->name;
813 if (a->val[1].u.data) {
814 value.n = run_actions(h, (struct action*)a->val[1].u.data,
820 } else if(a->val[1].type == EXPR_ST && a->val[1].u.data) {
821 v = eval_expr(h, (struct expr*)a->val[1].u.data, msg);
823 if (v == EXPR_DROP){ /* hack to quit on DROP*/
827 LOG(L_WARN,"WARNING: do_action: error in expression\n");
828 v = 0; /* error is treated as false (Miklos) */
832 flags = a->val[0].u.attr->type;
833 name = a->val[0].u.attr->name;
835 } else if (a->val[1].type == AVP_ST) {
836 struct search_state st;
841 if ((a->val[1].u.attr->type & AVP_INDEX_ALL) == AVP_INDEX_ALL) {
842 avp = search_first_avp(a->val[1].u.attr->type, a->val[1].u.attr->name, &value, &st);
844 /* We take only the type of value and name from the source avp
845 * and reset class and track flags
847 flags = (a->val[0].u.attr->type & ~AVP_INDEX_ALL) | (avp->flags & ~(AVP_CLASS_ALL|AVP_TRACK_ALL));
849 if (add_avp_before(avp_mark, flags, a->val[0].u.attr->name, value) < 0) {
850 LOG(L_CRIT, "ERROR: Failed to assign value to attribute\n");
855 /* move the mark, so the next found AVP will come before the one currently added
856 * so they will have the same order as in the source list
859 avp_mark=avp_mark->next;
861 avp_mark=search_first_avp(flags, a->val[0].u.attr->name, NULL, NULL);
864 avp = search_next_avp(&st, &value);
869 avp = search_avp_by_index(a->val[1].u.attr->type, a->val[1].u.attr->name, &value, a->val[1].u.attr->index);
871 flags = a->val[0].u.attr->type | (avp->flags & ~(AVP_CLASS_ALL|AVP_TRACK_ALL));
872 name = a->val[0].u.attr->name;
879 } else if (a->val[1].type == SELECT_ST) {
881 r = run_select(&value.s, a->val[1].u.select, msg);
890 flags = a->val[0].u.attr->type | AVP_VAL_STR;
891 name = a->val[0].u.attr->name;
894 LOG(L_CRIT, "BUG: do_action: Bad right side of avp assignment\n");
899 /* If the action is assign then remove the old avp value
900 * before adding new ones */
901 /* if ((unsigned char)a->type == ASSIGN_T) delete_avp(flags, name); */
902 if (add_avp(flags & ~AVP_INDEX_ALL, name, value) < 0) {
903 LOG(L_CRIT, "ERROR: Failed to assign value to attribute\n");
910 LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
916 LOG(L_ERR, "ERROR: do_action: set*: uri too long\n");
917 if (new_uri) pkg_free(new_uri);
920 /*free_uri(&uri); -- not needed anymore, using msg->parsed_uri*/
926 /* returns: 0, or 1 on success, <0 on error */
927 /* (0 if drop or break encountered, 1 if not ) */
928 int run_actions(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
932 struct sr_module *mod;
936 if (h->rec_lev>ROUTE_MAX_REC_LEV){
937 LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
938 " giving up!\n", h->rec_lev);
946 if (setjmp(h->jmp_env)){
955 DBG("DEBUG: run_actions: null action list (rec_level=%d)\n",
960 for (t=a; t!=0; t=t->next){
961 ret=do_action(h, t, msg);
962 if (h->run_flags & (RETURN_R_F|EXIT_R_F)){
963 if (h->run_flags & EXIT_R_F){
966 longjmp(h->jmp_env, ret);
971 /* ignore error returns */
976 /* process module onbreak handlers if present */
977 if (h->rec_lev==0 && ret==0)
978 for (mod=modules;mod;mod=mod->next)
979 if (mod->exports && mod->exports->onbreak_f) {
980 mod->exports->onbreak_f( msg );
981 DBG("DEBUG: %s onbreak handler called\n", mod->exports->name);