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