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