Browse code

topoh: check if via param has the expected prefix, otherwise skip

- GH #2437

Daniel-Constantin Mierla authored on 11/08/2020 10:36:02
Showing 1 changed files
... ...
@@ -408,7 +408,7 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
408 408
 				if (th_uri_prefix_checks && (via->host.len!=th_ip.len
409 409
 						|| strncasecmp(via->host.s, th_ip.s, th_ip.len)!=0))
410 410
 				{
411
-					LM_DBG("via %d is not encoded",i);
411
+					LM_DBG("via %d is not encoded - skip\n",i);
412 412
 					continue;
413 413
 				}
414 414
 
... ...
@@ -418,9 +418,16 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
418 418
 					LM_ERR("cannot find param in via %d\n", i);
419 419
 					return -1;
420 420
 				}
421
-				if(vp->value.len <= th_vparam_prefix.len) {
422
-					LM_ERR("invalid param len in via %d\n", i);
423
-					return -1;
421
+				if(th_vparam_prefix.len > 0) {
422
+					if(vp->value.len <= th_vparam_prefix.len) {
423
+						LM_DBG("shorter param len in via %d - skip\n", i);
424
+						continue;
425
+					}
426
+					if(strncmp(vp->value.s, th_vparam_prefix.s,
427
+									th_vparam_prefix.len) != 0) {
428
+						LM_DBG("no prefix in via %d - skip\n", i);
429
+						continue;
430
+					}
424 431
 				}
425 432
 				if(i==2) {
426 433
 					out.s = th_mask_decode(vp->value.s, vp->value.len,
Browse code

topoh: convert to memory logging helper

Henning Westerholt authored on 13/02/2020 21:56:38
Showing 1 changed files
... ...
@@ -294,7 +294,7 @@ int th_mask_contact(sip_msg_t *msg)
294 294
 		p = (char*)pkg_malloc(out.len+3);
295 295
 		if(p==NULL)
296 296
 		{
297
-			LM_ERR("failed to get more pkg\n");
297
+			PKG_MEM_ERROR;
298 298
 			pkg_free(out.s);
299 299
 			return -1;
300 300
 		}
... ...
@@ -441,7 +441,7 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
441 441
 					via2=pkg_malloc(sizeof(struct via_body));
442 442
 					if (via2==0)
443 443
 					{
444
-						LM_ERR("out of memory\n");
444
+						PKG_MEM_ERROR;
445 445
 						pkg_free(out.s);
446 446
 						return -1;
447 447
 
... ...
@@ -983,7 +983,7 @@ int th_add_via_cookie(sip_msg_t *msg, struct via_body *via)
983 983
 	out.s = (char*)pkg_malloc(out.len+1);
984 984
 	if(out.s==0)
985 985
 	{
986
-		LM_ERR("no pkg memory\n");
986
+		PKG_MEM_ERROR;
987 987
 		return -1;
988 988
 	}
989 989
 	out.s[0] = ';';
... ...
@@ -1009,7 +1009,7 @@ int th_add_hdr_cookie(sip_msg_t *msg)
1009 1009
 	h.s = (char*)pkg_malloc(h.len+1);
1010 1010
 	if(h.s == 0)
1011 1011
 	{
1012
-		LM_ERR("no more pkg\n");
1012
+		PKG_MEM_ERROR;
1013 1013
 		return -1;
1014 1014
 	}
1015 1015
 	anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
Browse code

topoh: check returned pointer when decoding call-id

Daniel-Constantin Mierla authored on 11/03/2019 08:57:57
Showing 1 changed files
... ...
@@ -559,6 +559,10 @@ int th_unmask_callid_str(str *icallid, str *ocallid)
559 559
 	}
560 560
 	out.s = th_mask_decode(icallid->s, icallid->len,
561 561
 					&th_callid_prefix, 0, &out.len);
562
+	if(out.s == NULL) {
563
+		LM_ERR("failed to decode call-id\n");
564
+		return -2;
565
+	}
562 566
 	if(out.len>=TH_CALLID_SIZE) {
563 567
 		pkg_free(out.s);
564 568
 		LM_ERR("not enough callid buf size (needed %d)\n", out.len);
Browse code

topoh: detect when via param is shorter than expected prefix

Daniel-Constantin Mierla authored on 02/03/2019 12:40:47
Showing 1 changed files
... ...
@@ -418,12 +418,17 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
418 418
 					LM_ERR("cannot find param in via %d\n", i);
419 419
 					return -1;
420 420
 				}
421
-				if(i==2)
421
+				if(vp->value.len <= th_vparam_prefix.len) {
422
+					LM_ERR("invalid param len in via %d\n", i);
423
+					return -1;
424
+				}
425
+				if(i==2) {
422 426
 					out.s = th_mask_decode(vp->value.s, vp->value.len,
423 427
 							&th_vparam_prefix, CRLF_LEN+1, &out.len);
424
-				else
428
+				} else {
425 429
 					out.s = th_mask_decode(vp->value.s, vp->value.len,
426 430
 							&th_vparam_prefix, 0, &out.len);
431
+				}
427 432
 				if(out.s==NULL || out.len<=0)
428 433
 				{
429 434
 					LM_ERR("cannot decode via %d\n", i);
Browse code

topoh: improve checks for contact header processing

Henning Westerholt authored on 25/02/2019 15:45:42
Showing 1 changed files
... ...
@@ -275,6 +275,11 @@ int th_mask_contact(sip_msg_t *msg)
275 275
 	}
276 276
 
277 277
 	c = ((contact_body_t*)msg->contact->parsed)->contacts;
278
+	if(c == NULL)
279
+	{
280
+		LM_ERR("invalid contact header\n");
281
+		return -1;
282
+	}
278 283
 	in = c->uri;
279 284
 
280 285
 	out.s = th_mask_encode(in.s, in.len, &th_uri_prefix, &out.len);
Browse code

topoh: improve checks for via header processing

Henning Westerholt authored on 24/02/2019 20:25:25
Showing 1 changed files
... ...
@@ -419,7 +419,7 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
419 419
 				else
420 420
 					out.s = th_mask_decode(vp->value.s, vp->value.len,
421 421
 							&th_vparam_prefix, 0, &out.len);
422
-				if(out.s==NULL)
422
+				if(out.s==NULL || out.len<=0)
423 423
 				{
424 424
 					LM_ERR("cannot decode via %d\n", i);
425 425
 					return -1;
... ...
@@ -949,7 +949,12 @@ int th_add_via_cookie(sip_msg_t *msg, struct via_body *via)
949 949
 	if (via->params.s) {
950 950
 		viap = via->params.s - via->hdr.s - 1;
951 951
 	} else {
952
-		viap = via->host.s - via->hdr.s + via->host.len;
952
+		if (via->host.s) {
953
+			viap = via->host.s - via->hdr.s + via->host.len;
954
+		} else {
955
+			LM_ERR("no via parameter and no via host, can't insert cookie\n");
956
+			return -1;
957
+		}
953 958
 		if (via->port!=0)
954 959
 			viap += via->port_str.len + 1; /* +1 for ':'*/
955 960
 	}
Browse code

topoh: aded debug message for knowing the reason of no decoding rr

Daniel-Constantin Mierla authored on 07/01/2019 11:01:21
Showing 1 changed files
... ...
@@ -701,7 +701,7 @@ int th_unmask_route(sip_msg_t *msg)
701 701
 						(strncasecmp(rr->nameaddr.uri.s,th_uri_prefix.s,
702 702
 									th_uri_prefix.len)!=0)))
703 703
 				{
704
-					LM_DBG("rr %d is not encoded: [%.*s]", i,
704
+					LM_DBG("rr %d is not encoded: [%.*s] - missing prefix\n", i,
705 705
 							rr->nameaddr.uri.len, rr->nameaddr.uri.s);
706 706
 					rr = rr->next;
707 707
 					continue;
... ...
@@ -710,6 +710,8 @@ int th_unmask_route(sip_msg_t *msg)
710 710
 				if(th_get_uri_param_value(&rr->nameaddr.uri, &th_uparam_name,
711 711
 							&eval)<0 || eval.len<=0)
712 712
 				{
713
+					LM_DBG("rr %d is not encoded: [%.*s] - missing param\n", i,
714
+							rr->nameaddr.uri.len, rr->nameaddr.uri.s);
713 715
 					rr = rr->next;
714 716
 					continue;
715 717
 				}
Browse code

topoh: fixed unmask route issue, if maksing fails for one of the routes

Balajee authored on 23/12/2018 17:57:49
Showing 1 changed files
... ...
@@ -709,7 +709,10 @@ int th_unmask_route(sip_msg_t *msg)
709 709
 
710 710
 				if(th_get_uri_param_value(&rr->nameaddr.uri, &th_uparam_name,
711 711
 							&eval)<0 || eval.len<=0)
712
-					return -1;
712
+				{
713
+					rr = rr->next;
714
+					continue;
715
+				}
713 716
 
714 717
 				out.s = th_mask_decode(eval.s, eval.len,
715 718
 							&th_uparam_prefix, 0, &out.len);
Browse code

topoh: try to use sip uri parameters for decoding

- when UA adds user=phone, turns the uri type in tel mapped over sip
uri, default params pointing to tel params
- reported by GH #1541

Daniel-Constantin Mierla authored on 14/07/2018 07:20:29
Showing 1 changed files
... ...
@@ -108,17 +108,25 @@ int th_get_param_value(str *in, str *name, str *value)
108 108
 
109 109
 int th_get_uri_param_value(str *uri, str *name, str *value)
110 110
 {
111
-	struct sip_uri puri;
111
+	sip_uri_t puri;
112 112
 
113 113
 	memset(value, 0, sizeof(str));
114 114
 	if(parse_uri(uri->s, uri->len, &puri)<0)
115 115
 		return -1;
116
+
117
+	LM_DBG("uri params: [%.*s] - sip uri params: [%.*s]\n",
118
+			puri.params.len, (puri.params.s)?puri.params.s:"",
119
+			puri.sip_params.len, (puri.sip_params.s)?puri.sip_params.s:"");
120
+
121
+	if(puri.sip_params.len>0)
122
+		return th_get_param_value(&puri.sip_params, name, value);
123
+
116 124
 	return th_get_param_value(&puri.params, name, value);
117 125
 }
118 126
 
119 127
 int th_get_uri_type(str *uri, int *mode, str *value)
120 128
 {
121
-	struct sip_uri puri;
129
+	sip_uri_t puri;
122 130
 	int ret;
123 131
 	str r2 = {"r2", 2};
124 132
 
Browse code

topoh: more verbose debug messages when decoding r-uri

Daniel-Constantin Mierla authored on 22/05/2018 18:21:02
Showing 1 changed files
... ...
@@ -749,14 +749,19 @@ int th_unmask_ruri(sip_msg_t *msg)
749 749
 	}
750 750
 
751 751
 	if(th_get_uri_param_value(&REQ_LINE(msg).uri, &th_uparam_name, &eval)<0
752
-			|| eval.len<=0)
752
+			|| eval.len<=0) {
753
+		LM_DBG("no uri param [%.*s] in [%.*s]\n",
754
+				th_uparam_name.len, th_uparam_name.s,
755
+				REQ_LINE(msg).uri.len,REQ_LINE(msg).uri.s);
753 756
 		return -1;
757
+	}
754 758
 
755 759
 	out.s = th_mask_decode(eval.s, eval.len,
756 760
 				&th_uparam_prefix, 0, &out.len);
757 761
 	if(out.s==NULL)
758 762
 	{
759
-		LM_ERR("cannot decode r-uri\n");
763
+		LM_ERR("cannot decode r-uri [%.*s]\n",
764
+				REQ_LINE(msg).uri.len,REQ_LINE(msg).uri.s);
760 765
 		return -1;
761 766
 	}
762 767
 
Browse code

topoh: coherent whitespacing and indentation

Daniel-Constantin Mierla authored on 25/06/2017 07:54:09
Showing 1 changed files
... ...
@@ -100,7 +100,7 @@ int th_get_param_value(str *in, str *name, str *value)
100 100
 			return 0;
101 101
 		}
102 102
 	}
103
-	
103
+
104 104
 	if(params) free_params(params);
105 105
 	return 1;
106 106
 
... ...
@@ -188,7 +188,7 @@ int th_mask_via(sip_msg_t *msg)
188 188
 					LM_ERR("cannot encode via %d\n", i);
189 189
 					return -1;
190 190
 				}
191
-					
191
+
192 192
 				LM_DBG("+body: %d: [%.*s]\n", out.len, out.len, out.s);
193 193
 				l=del_lump(msg, via->name.s-msg->buf, vlen, 0);
194 194
 				if (l==0)
... ...
@@ -221,7 +221,7 @@ int th_mask_callid(sip_msg_t *msg)
221 221
 		LM_ERR("cannot get Call-Id header\n");
222 222
 		return -1;
223 223
 	}
224
-				
224
+
225 225
 	out.s = th_mask_encode(msg->callid->body.s, msg->callid->body.len,
226 226
 				&th_callid_prefix, &out.len);
227 227
 	if(out.s==NULL)
... ...
@@ -229,7 +229,7 @@ int th_mask_callid(sip_msg_t *msg)
229 229
 		LM_ERR("cannot encode callid\n");
230 230
 		return -1;
231 231
 	}
232
-				
232
+
233 233
 	l=del_lump(msg, msg->callid->body.s-msg->buf, msg->callid->body.len, 0);
234 234
 	if (l==0)
235 235
 	{
... ...
@@ -254,7 +254,7 @@ int th_mask_contact(sip_msg_t *msg)
254 254
 	char *p;
255 255
 	contact_t *c;
256 256
 
257
-	if(msg->contact==NULL) 
257
+	if(msg->contact==NULL)
258 258
 	{
259 259
 		LM_DBG("no contact header\n");
260 260
 		return 0;
... ...
@@ -265,7 +265,7 @@ int th_mask_contact(sip_msg_t *msg)
265 265
 		LM_ERR("failed parsing contact header\n");
266 266
 		return -1;
267 267
 	}
268
-	
268
+
269 269
 	c = ((contact_body_t*)msg->contact->parsed)->contacts;
270 270
 	in = c->uri;
271 271
 
... ...
@@ -293,7 +293,7 @@ int th_mask_contact(sip_msg_t *msg)
293 293
 		out.s = p;
294 294
 		out.len += 2;
295 295
 	}
296
-				
296
+
297 297
 	l=del_lump(msg, in.s-msg->buf, in.len, 0);
298 298
 	if (l==0)
299 299
 	{
... ...
@@ -325,9 +325,9 @@ int th_mask_record_route(sip_msg_t *msg)
325 325
 	}
326 326
 	hdr = msg->record_route;
327 327
 	i = 0;
328
-	while(hdr!=NULL) 
328
+	while(hdr!=NULL)
329 329
 	{
330
-		if (parse_rr(hdr) < 0) 
330
+		if (parse_rr(hdr) < 0)
331 331
 		{
332 332
 			LM_ERR("failed to parse RR\n");
333 333
 			return -1;
... ...
@@ -416,7 +416,7 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
416 416
 					LM_ERR("cannot decode via %d\n", i);
417 417
 					return -1;
418 418
 				}
419
-					
419
+
420 420
 				LM_DBG("+body: %d: [%.*s]\n", out.len, out.len, out.s);
421 421
 				if(i==2)
422 422
 				{
... ...
@@ -428,7 +428,7 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
428 428
 						return -1;
429 429
 
430 430
 					}
431
-					
431
+
432 432
 					memset(via2, 0, sizeof(struct via_body));
433 433
 					memcpy(out.s+out.len, CRLF, CRLF_LEN);
434 434
 					out.s[out.len+CRLF_LEN]='X';
... ...
@@ -475,10 +475,10 @@ int th_unmask_callid(sip_msg_t *msg)
475 475
 {
476 476
 	struct lump* l;
477 477
 	str out;
478
-	
478
+
479 479
 	if(th_param_mask_callid==0)
480 480
 		return 0;
481
-	
481
+
482 482
 	if(msg->callid==NULL)
483 483
 	{
484 484
 		LM_ERR("cannot get Call-Id header\n");
... ...
@@ -492,7 +492,7 @@ int th_unmask_callid(sip_msg_t *msg)
492 492
 		LM_DBG("call-id [%.*s] not encoded",msg->callid->body.len,msg->callid->body.s);
493 493
 		return 0;
494 494
 	}
495
-				
495
+
496 496
 	out.s = th_mask_decode(msg->callid->body.s, msg->callid->body.len,
497 497
 					&th_callid_prefix, 0, &out.len);
498 498
 	if(out.s==NULL)
... ...
@@ -500,7 +500,7 @@ int th_unmask_callid(sip_msg_t *msg)
500 500
 		LM_ERR("cannot decode callid\n");
501 501
 		return -1;
502 502
 	}
503
-				
503
+
504 504
 	l=del_lump(msg, msg->callid->body.s-msg->buf, msg->callid->body.len, 0);
505 505
 	if (l==0)
506 506
 	{
... ...
@@ -580,9 +580,9 @@ int th_flip_record_route(sip_msg_t *msg, int mode)
580 580
 	act = 0;
581 581
 	if(mode==1)
582 582
 		act = 2;
583
-	while(hdr!=NULL) 
583
+	while(hdr!=NULL)
584 584
 	{
585
-		if (parse_rr(hdr) < 0) 
585
+		if (parse_rr(hdr) < 0)
586 586
 		{
587 587
 			LM_ERR("failed to parse RR\n");
588 588
 			return -1;
... ...
@@ -673,9 +673,9 @@ int th_unmask_route(sip_msg_t *msg)
673 673
 	}
674 674
 	hdr = msg->route;
675 675
 	i = 0;
676
-	while(hdr!=NULL) 
676
+	while(hdr!=NULL)
677 677
 	{
678
-		if (parse_rr(hdr) < 0) 
678
+		if (parse_rr(hdr) < 0)
679 679
 		{
680 680
 			LM_ERR("failed to parse RR\n");
681 681
 			return -1;
... ...
@@ -691,7 +691,7 @@ int th_unmask_route(sip_msg_t *msg)
691 691
 				if (th_uri_prefix_checks
692 692
 						&& ((rr->nameaddr.uri.len<th_uri_prefix.len) ||
693 693
 						(strncasecmp(rr->nameaddr.uri.s,th_uri_prefix.s,
694
-									 th_uri_prefix.len)!=0)))
694
+									th_uri_prefix.len)!=0)))
695 695
 				{
696 696
 					LM_DBG("rr %d is not encoded: [%.*s]", i,
697 697
 							rr->nameaddr.uri.len, rr->nameaddr.uri.s);
... ...
@@ -702,7 +702,7 @@ int th_unmask_route(sip_msg_t *msg)
702 702
 				if(th_get_uri_param_value(&rr->nameaddr.uri, &th_uparam_name,
703 703
 							&eval)<0 || eval.len<=0)
704 704
 					return -1;
705
-	
705
+
706 706
 				out.s = th_mask_decode(eval.s, eval.len,
707 707
 							&th_uparam_prefix, 0, &out.len);
708 708
 
... ...
@@ -742,7 +742,7 @@ int th_unmask_ruri(sip_msg_t *msg)
742 742
 	/* Do nothing if ruri is not encoded */
743 743
 	if (th_uri_prefix_checks && ((REQ_LINE(msg).uri.len<th_uri_prefix.len) ||
744 744
 			(strncasecmp(REQ_LINE(msg).uri.s, th_uri_prefix.s,
745
-						 th_uri_prefix.len)!=0)))
745
+						th_uri_prefix.len)!=0)))
746 746
 	{
747 747
 		LM_DBG("ruri [%.*s] is not encoded",REQ_LINE(msg).uri.len,REQ_LINE(msg).uri.s);
748 748
 		return 0;
... ...
@@ -751,7 +751,7 @@ int th_unmask_ruri(sip_msg_t *msg)
751 751
 	if(th_get_uri_param_value(&REQ_LINE(msg).uri, &th_uparam_name, &eval)<0
752 752
 			|| eval.len<=0)
753 753
 		return -1;
754
-	
754
+
755 755
 	out.s = th_mask_decode(eval.s, eval.len,
756 756
 				&th_uparam_prefix, 0, &out.len);
757 757
 	if(out.s==NULL)
... ...
@@ -759,7 +759,7 @@ int th_unmask_ruri(sip_msg_t *msg)
759 759
 		LM_ERR("cannot decode r-uri\n");
760 760
 		return -1;
761 761
 	}
762
-					
762
+
763 763
 	LM_DBG("+decoded: %d: [%.*s]\n", out.len, out.len, out.s);
764 764
 	l=del_lump(msg, REQ_LINE(msg).uri.s-msg->buf, REQ_LINE(msg).uri.len, 0);
765 765
 	if (l==0)
... ...
@@ -941,7 +941,7 @@ int th_add_via_cookie(sip_msg_t *msg, struct via_body *via)
941 941
 		LM_ERR("failed adding cookie to via [%p]\n", via);
942 942
 		return -1;
943 943
 	}
944
-	
944
+
945 945
 	out.len = 1+th_cookie_name.len+1+th_cookie_value.len+1;
946 946
 	out.s = (char*)pkg_malloc(out.len+1);
947 947
 	if(out.s==0)
... ...
@@ -1141,7 +1141,7 @@ int th_route_direction(sip_msg_t *msg)
1141 1141
 		LM_DBG("no route header - downstream\n");
1142 1142
 		return 0;
1143 1143
 	}
1144
-	if (parse_rr(msg->route) < 0) 
1144
+	if (parse_rr(msg->route) < 0)
1145 1145
 	{
1146 1146
 		LM_ERR("failed to parse route header\n");
1147 1147
 		return -1;
Browse code

topoh: option to disable uri prefix checks

- some devices do not copy the exact URI as received in headers
(Contact, Record-Route) - they can add default port or mix parameters
- reported by GH #1165

Daniel-Constantin Mierla authored on 25/06/2017 07:48:26
Showing 1 changed files
... ...
@@ -57,6 +57,7 @@ extern str th_vparam_prefix;
57 57
 
58 58
 extern int th_param_mask_callid;
59 59
 extern int th_mask_addr_myself;
60
+extern int th_uri_prefix_checks;
60 61
 
61 62
 int th_skip_rw(char *s, int len)
62 63
 {
... ...
@@ -391,8 +392,8 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
391 392
 			if(i!=1)
392 393
 			{
393 394
 				/* Skip if via is not encoded */
394
-				if (via->host.len!=th_ip.len
395
-						|| strncasecmp(via->host.s, th_ip.s, th_ip.len)!=0)
395
+				if (th_uri_prefix_checks && (via->host.len!=th_ip.len
396
+						|| strncasecmp(via->host.s, th_ip.s, th_ip.len)!=0))
396 397
 				{
397 398
 					LM_DBG("via %d is not encoded",i);
398 399
 					continue;
... ...
@@ -687,10 +688,13 @@ int th_unmask_route(sip_msg_t *msg)
687 688
 			if(i!=1)
688 689
 			{
689 690
 				/* Skip if route is not encoded */
690
-				if ((rr->nameaddr.uri.len<th_uri_prefix.len) ||
691
-						(strncasecmp(rr->nameaddr.uri.s,th_uri_prefix.s,th_uri_prefix.len)!=0))
691
+				if (th_uri_prefix_checks
692
+						&& ((rr->nameaddr.uri.len<th_uri_prefix.len) ||
693
+						(strncasecmp(rr->nameaddr.uri.s,th_uri_prefix.s,
694
+									 th_uri_prefix.len)!=0)))
692 695
 				{
693
-					LM_DBG("rr %d is not encoded: [%.*s]",i,rr->nameaddr.uri.len,rr->nameaddr.uri.s);
696
+					LM_DBG("rr %d is not encoded: [%.*s]", i,
697
+							rr->nameaddr.uri.len, rr->nameaddr.uri.s);
694 698
 					rr = rr->next;
695 699
 					continue;
696 700
 				}
... ...
@@ -736,8 +740,9 @@ int th_unmask_ruri(sip_msg_t *msg)
736 740
 	str out;
737 741
 
738 742
 	/* Do nothing if ruri is not encoded */
739
-	if ((REQ_LINE(msg).uri.len<th_uri_prefix.len) ||
740
-			(strncasecmp(REQ_LINE(msg).uri.s,th_uri_prefix.s,th_uri_prefix.len)!=0))
743
+	if (th_uri_prefix_checks && ((REQ_LINE(msg).uri.len<th_uri_prefix.len) ||
744
+			(strncasecmp(REQ_LINE(msg).uri.s, th_uri_prefix.s,
745
+						 th_uri_prefix.len)!=0)))
741 746
 	{
742 747
 		LM_DBG("ruri [%.*s] is not encoded",REQ_LINE(msg).uri.len,REQ_LINE(msg).uri.s);
743 748
 		return 0;
... ...
@@ -798,8 +803,8 @@ int th_unmask_refer_to(sip_msg_t *msg)
798 803
 	uri = &(get_refer_to(msg)->uri);
799 804
 
800 805
 	/* Do nothing if refer_to is not encoded */
801
-	if ((uri->len<th_uri_prefix.len)
802
-			|| (strncasecmp(uri->s, th_uri_prefix.s, th_uri_prefix.len)!=0))
806
+	if (th_uri_prefix_checks && ((uri->len<th_uri_prefix.len)
807
+			|| (strncasecmp(uri->s, th_uri_prefix.s, th_uri_prefix.len)!=0)))
803 808
 	{
804 809
 		LM_DBG("refer-to [%.*s] is not encoded",uri->len,uri->s);
805 810
 		return 0;
Browse code

topoh: add additional safety checks

- verify that headers contain expected prefix (including mask ip)
before trying to unmask them; otherwise there may be issues when
kamailio receives messages that are not masked and topoh is enabled.

Claudiu Boriga authored on 03/04/2017 09:13:30
Showing 1 changed files
... ...
@@ -390,6 +390,14 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
390 390
 			LM_DBG("body: %d: [%.*s]\n", vlen, vlen, via->name.s);
391 391
 			if(i!=1)
392 392
 			{
393
+				/* Skip if via is not encoded */
394
+				if (via->host.len!=th_ip.len
395
+						|| strncasecmp(via->host.s, th_ip.s, th_ip.len)!=0)
396
+				{
397
+					LM_DBG("via %d is not encoded",i);
398
+					continue;
399
+				}
400
+
393 401
 				vp = th_get_via_param(via, &th_vparam_name);
394 402
 				if(vp==NULL)
395 403
 				{
... ...
@@ -404,7 +412,7 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
404 412
 							&th_vparam_prefix, 0, &out.len);
405 413
 				if(out.s==NULL)
406 414
 				{
407
-					LM_ERR("cannot encode via %d\n", i);
415
+					LM_ERR("cannot decode via %d\n", i);
408 416
 					return -1;
409 417
 				}
410 418
 					
... ...
@@ -475,6 +483,14 @@ int th_unmask_callid(sip_msg_t *msg)
475 483
 		LM_ERR("cannot get Call-Id header\n");
476 484
 		return -1;
477 485
 	}
486
+
487
+	/* Do nothing if call-id is not encoded */
488
+	if ((msg->callid->body.len<th_callid_prefix.len) ||
489
+			(strncasecmp(msg->callid->body.s,th_callid_prefix.s,th_callid_prefix.len)!=0))
490
+	{
491
+		LM_DBG("call-id [%.*s] not encoded",msg->callid->body.len,msg->callid->body.s);
492
+		return 0;
493
+	}
478 494
 				
479 495
 	out.s = th_mask_decode(msg->callid->body.s, msg->callid->body.len,
480 496
 					&th_callid_prefix, 0, &out.len);
... ...
@@ -670,6 +686,15 @@ int th_unmask_route(sip_msg_t *msg)
670 686
 			i++;
671 687
 			if(i!=1)
672 688
 			{
689
+				/* Skip if route is not encoded */
690
+				if ((rr->nameaddr.uri.len<th_uri_prefix.len) ||
691
+						(strncasecmp(rr->nameaddr.uri.s,th_uri_prefix.s,th_uri_prefix.len)!=0))
692
+				{
693
+					LM_DBG("rr %d is not encoded: [%.*s]",i,rr->nameaddr.uri.len,rr->nameaddr.uri.s);
694
+					rr = rr->next;
695
+					continue;
696
+				}
697
+
673 698
 				if(th_get_uri_param_value(&rr->nameaddr.uri, &th_uparam_name,
674 699
 							&eval)<0 || eval.len<=0)
675 700
 					return -1;
... ...
@@ -710,6 +735,14 @@ int th_unmask_ruri(sip_msg_t *msg)
710 735
 	struct lump* l;
711 736
 	str out;
712 737
 
738
+	/* Do nothing if ruri is not encoded */
739
+	if ((REQ_LINE(msg).uri.len<th_uri_prefix.len) ||
740
+			(strncasecmp(REQ_LINE(msg).uri.s,th_uri_prefix.s,th_uri_prefix.len)!=0))
741
+	{
742
+		LM_DBG("ruri [%.*s] is not encoded",REQ_LINE(msg).uri.len,REQ_LINE(msg).uri.s);
743
+		return 0;
744
+	}
745
+
713 746
 	if(th_get_uri_param_value(&REQ_LINE(msg).uri, &th_uparam_name, &eval)<0
714 747
 			|| eval.len<=0)
715 748
 		return -1;
... ...
@@ -763,6 +796,15 @@ int th_unmask_refer_to(sip_msg_t *msg)
763 796
 	}
764 797
 
765 798
 	uri = &(get_refer_to(msg)->uri);
799
+
800
+	/* Do nothing if refer_to is not encoded */
801
+	if ((uri->len<th_uri_prefix.len)
802
+			|| (strncasecmp(uri->s, th_uri_prefix.s, th_uri_prefix.len)!=0))
803
+	{
804
+		LM_DBG("refer-to [%.*s] is not encoded",uri->len,uri->s);
805
+		return 0;
806
+	}
807
+
766 808
 	if(th_get_uri_param_value(uri, &th_uparam_name, &eval)<0
767 809
 			|| eval.len<=0)
768 810
 		return -1;
Browse code

topoh: safety check to avoid crash when there is no via header

- based on GH #952

Minh Phan authored on 27/01/2017 08:06:08 • Daniel-Constantin Mierla committed on 27/01/2017 08:06:08
Showing 1 changed files
... ...
@@ -954,6 +954,10 @@ int th_add_hdr_cookie(sip_msg_t *msg)
954 954
 struct via_param *th_get_via_cookie(sip_msg_t *msg, struct via_body *via)
955 955
 {
956 956
 	struct via_param *p;
957
+
958
+	if (!via) {
959
+		return NULL;
960
+	}
957 961
 	for(p=via->param_lst; p; p=p->next)
958 962
 	{
959 963
 		if(p->name.len==th_cookie_name.len
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -26,20 +26,20 @@
26 26
 
27 27
 #include <string.h>
28 28
 
29
-#include "../../dprint.h"
30
-#include "../../mem/mem.h"
31
-#include "../../data_lump.h"
32
-#include "../../forward.h"
33
-#include "../../trim.h"
34
-#include "../../msg_translator.h"
35
-#include "../../parser/parse_rr.h"
36
-#include "../../parser/parse_uri.h"
37
-#include "../../parser/parse_param.h"
38
-#include "../../parser/parse_from.h"
39
-#include "../../parser/parse_to.h"
40
-#include "../../parser/parse_via.h"
41
-#include "../../parser/contact/parse_contact.h"
42
-#include "../../parser/parse_refer_to.h"
29
+#include "../../core/dprint.h"
30
+#include "../../core/mem/mem.h"
31
+#include "../../core/data_lump.h"
32
+#include "../../core/forward.h"
33
+#include "../../core/trim.h"
34
+#include "../../core/msg_translator.h"
35
+#include "../../core/parser/parse_rr.h"
36
+#include "../../core/parser/parse_uri.h"
37
+#include "../../core/parser/parse_param.h"
38
+#include "../../core/parser/parse_from.h"
39
+#include "../../core/parser/parse_to.h"
40
+#include "../../core/parser/parse_via.h"
41
+#include "../../core/parser/contact/parse_contact.h"
42
+#include "../../core/parser/parse_refer_to.h"
43 43
 #include "th_mask.h"
44 44
 #include "th_msg.h"
45 45
 
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,1130 @@
1
+/**
2
+ *
3
+ * Copyright (C) 2009 SIP-Router.org
4
+ *
5
+ * This file is part of Kamailio, a free SIP server.
6
+ *
7
+ * Permission to use, copy, modify, and distribute this software for any
8
+ * purpose with or without fee is hereby granted, provided that the above
9
+ * copyright notice and this permission notice appear in all copies.
10
+ *
11
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
+ */
19
+
20
+/*!
21
+ * \file
22
+ * \brief SIP-router topoh ::
23
+ * \ingroup topoh
24
+ * Module: \ref topoh
25
+ */
26
+
27
+#include <string.h>
28
+
29
+#include "../../dprint.h"
30
+#include "../../mem/mem.h"
31
+#include "../../data_lump.h"
32
+#include "../../forward.h"
33
+#include "../../trim.h"
34
+#include "../../msg_translator.h"
35
+#include "../../parser/parse_rr.h"
36
+#include "../../parser/parse_uri.h"
37
+#include "../../parser/parse_param.h"
38
+#include "../../parser/parse_from.h"
39
+#include "../../parser/parse_to.h"
40
+#include "../../parser/parse_via.h"
41
+#include "../../parser/contact/parse_contact.h"
42
+#include "../../parser/parse_refer_to.h"
43
+#include "th_mask.h"
44
+#include "th_msg.h"
45
+
46
+extern str th_cookie_name;
47
+extern str th_cookie_value;
48
+extern str th_via_prefix;
49
+extern str th_uri_prefix;
50
+extern str th_callid_prefix;
51
+
52
+extern str th_ip;
53
+extern str th_uparam_name;
54
+extern str th_uparam_prefix;
55
+extern str th_vparam_name;
56
+extern str th_vparam_prefix;
57
+
58
+extern int th_param_mask_callid;
59
+extern int th_mask_addr_myself;
60
+
61
+int th_skip_rw(char *s, int len)
62
+{
63
+	while(len>0)
64
+	{
65
+		if(s[len-1]==' ' || s[len-1]=='\t' || s[len-1]=='\n' || s[len-1]=='\r'
66
+				|| s[len-1]==',')
67
+			len--;
68
+		else return len;
69
+	}
70
+	return 0;
71
+}
72
+
73
+struct via_param *th_get_via_param(struct via_body *via, str *name)
74
+{
75
+	struct via_param *p;
76
+	for(p=via->param_lst; p; p=p->next)
77
+	{
78
+		if(p->name.len==name->len
79
+				&& strncasecmp(p->name.s, name->s, name->len)==0)
80
+			return p;
81
+	}
82
+	return NULL;
83
+}
84
+
85
+int th_get_param_value(str *in, str *name, str *value)
86
+{
87
+	param_t* params = NULL;
88
+	param_t* p = NULL;
89
+	param_hooks_t phooks;
90
+	if (parse_params(in, CLASS_ANY, &phooks, &params)<0)
91
+		return -1;
92
+	for (p = params; p; p=p->next)
93
+	{
94
+		if (p->name.len==name->len
95
+				&& strncasecmp(p->name.s, name->s, name->len)==0)
96
+		{
97
+			*value = p->body;
98
+			free_params(params);
99
+			return 0;
100
+		}
101
+	}
102
+	
103
+	if(params) free_params(params);
104
+	return 1;
105
+
106
+}
107
+
108
+int th_get_uri_param_value(str *uri, str *name, str *value)
109
+{
110
+	struct sip_uri puri;
111
+
112
+	memset(value, 0, sizeof(str));
113
+	if(parse_uri(uri->s, uri->len, &puri)<0)
114
+		return -1;
115
+	return th_get_param_value(&puri.params, name, value);
116
+}
117
+
118
+int th_get_uri_type(str *uri, int *mode, str *value)
119
+{
120
+	struct sip_uri puri;
121
+	int ret;
122
+	str r2 = {"r2", 2};
123
+
124
+	memset(value, 0, sizeof(str));
125
+	*mode = 0;
126
+	if(parse_uri(uri->s, uri->len, &puri)<0)
127
+		return -1;
128
+
129
+	LM_DBG("PARAMS [%.*s]\n", puri.params.len, puri.params.s);
130
+	if(puri.host.len==th_ip.len
131
+			&& strncasecmp(puri.host.s, th_ip.s, th_ip.len)==0)
132
+	{
133
+		/* host matches TH ip */
134
+		ret = th_get_param_value(&puri.params, &th_uparam_name, value);
135
+		if(ret<0)
136
+			return -1; /* eroor parsing parameters */
137
+		if(ret==0)
138
+			return 2; /* param found - decode */
139
+		if(th_mask_addr_myself==0)
140
+			return 0; /* param not found - skip */
141
+	}
142
+
143
+	if(check_self(&puri.host, puri.port_no, 0)==1)
144
+	{
145
+		/* myself -- matched on all protos */
146
+		ret = th_get_param_value(&puri.params, &r2, value);
147
+		if(ret<0)
148
+			return -1;
149
+		if(ret==1) /* not found */
150
+			return 0; /* skip */
151
+		LM_DBG("VALUE [%.*s]\n",
152
+				value->len, value->s);
153
+		if(value->len==2 && strncasecmp(value->s, "on", 2)==0)
154
+			*mode = 1;
155
+		memset(value, 0, sizeof(str));
156
+		return 0; /* skip */
157
+	}
158
+	/* not myself & not mask ip */
159
+	return 1; /* encode */
160
+}
161
+
162
+int th_mask_via(sip_msg_t *msg)
163
+{
164
+	hdr_field_t *hdr;
165
+	struct via_body *via;
166
+	struct lump* l;
167
+	int i;
168
+	str out;
169
+	int vlen;
170
+
171
+	i=0;
172
+	for(hdr=msg->h_via1; hdr; hdr=next_sibling_hdr(hdr))
173
+	{
174
+		for(via=(struct via_body*)hdr->parsed; via; via=via->next)
175
+		{
176
+			i++;
177
+			LM_DBG("=======via[%d]\n", i);
178
+			LM_DBG("hdr: [%.*s]\n", via->hdr.len, via->hdr.s);
179
+			vlen = th_skip_rw(via->name.s, via->bsize);
180
+			LM_DBG("body: %d: [%.*s]\n", vlen, vlen, via->name.s);
181
+			if(i!=1)
182
+			{
183
+				out.s = th_mask_encode(via->name.s, vlen, &th_via_prefix,
184
+						&out.len);
185
+				if(out.s==NULL)
186
+				{
187
+					LM_ERR("cannot encode via %d\n", i);
188
+					return -1;
189
+				}
190
+					
191
+				LM_DBG("+body: %d: [%.*s]\n", out.len, out.len, out.s);
192
+				l=del_lump(msg, via->name.s-msg->buf, vlen, 0);
193
+				if (l==0)
194
+				{
195
+					LM_ERR("failed deleting via [%d]\n", i);
196
+					pkg_free(out.s);
197
+					return -1;
198
+				}
199
+				if (insert_new_lump_after(l, out.s, out.len, 0)==0){
200
+					LM_ERR("could not insert new lump\n");
201
+					pkg_free(out.s);
202
+					return -1;
203
+				}
204
+			}
205
+		}
206
+	}
207
+	return 0;
208
+}
209
+
210
+int th_mask_callid(sip_msg_t *msg)
211
+{
212
+	struct lump* l;
213
+	str out;
214
+
215
+	if(th_param_mask_callid==0)
216
+		return 0;
217
+
218
+	if(msg->callid==NULL)
219
+	{
220
+		LM_ERR("cannot get Call-Id header\n");
221
+		return -1;
222
+	}
223
+				
224
+	out.s = th_mask_encode(msg->callid->body.s, msg->callid->body.len,
225
+				&th_callid_prefix, &out.len);
226
+	if(out.s==NULL)
227
+	{
228
+		LM_ERR("cannot encode callid\n");
229
+		return -1;
230
+	}
231
+				
232
+	l=del_lump(msg, msg->callid->body.s-msg->buf, msg->callid->body.len, 0);
233
+	if (l==0)
234
+	{
235
+		LM_ERR("failed deleting callid\n");
236
+		pkg_free(out.s);
237
+		return -1;
238
+	}
239
+	if (insert_new_lump_after(l, out.s, out.len, 0)==0) {
240
+		LM_ERR("could not insert new lump\n");
241
+		pkg_free(out.s);
242
+		return -1;
243
+	}
244
+
245
+	return 0;
246
+}
247
+
248
+int th_mask_contact(sip_msg_t *msg)
249
+{
250
+	struct lump* l;
251
+	str out;
252
+	str in;
253
+	char *p;
254
+	contact_t *c;
255
+
256
+	if(msg->contact==NULL) 
257
+	{
258
+		LM_DBG("no contact header\n");
259
+		return 0;
260
+	}
261
+
262
+	if(parse_contact(msg->contact) < 0)
263
+	{
264
+		LM_ERR("failed parsing contact header\n");
265
+		return -1;
266
+	}
267
+	
268
+	c = ((contact_body_t*)msg->contact->parsed)->contacts;
269
+	in = c->uri;
270
+
271
+	out.s = th_mask_encode(in.s, in.len, &th_uri_prefix, &out.len);
272
+	if(out.s==NULL)
273
+	{
274
+		LM_ERR("cannot encode contact uri\n");
275
+		return -1;
276
+	}
277
+	if(*(in.s-1)!='<')
278
+	{
279
+		/* add < > around contact uri if not there */
280
+		p = (char*)pkg_malloc(out.len+3);
281
+		if(p==NULL)
282
+		{
283
+			LM_ERR("failed to get more pkg\n");
284
+			pkg_free(out.s);
285
+			return -1;
286
+		}
287
+		*p = '<';
288
+		strncpy(p+1, out.s, out.len);
289
+		p[out.len+1] = '>';
290