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