Skip to content

Commit

Permalink
check validity for to/from in sipmsg_validate
Browse files Browse the repository at this point in the history
  • Loading branch information
tallicamike committed Aug 7, 2014
1 parent f5d422b commit d2ad483
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modules/sipmsgops/README
Expand Up @@ -516,6 +516,10 @@ add_body("Hello World!", "text/plain");
* 'm' - don't check the Max-Forwards header.
* 'r' - checks the R-URI and whether the domain contains
valid characters.
* 'f' - checks the URI of the 'From' field and whether the
domain contains valid characters.
* 't' - checks the URI of the 'To' field and whether the
domain contains valid characters.

The result_pvar parameter sets resulting pvar with text error
reason in case of negative result ( easy for logging or
Expand Down Expand Up @@ -547,6 +551,10 @@ add_body("Hello World!", "text/plain");
* -19 - No Unsupported header in 420 reply.
* -20 - No WWW-Authorize header in 401 reply.
* -21 - No Content-Type header
* -22 - To header parse error
* -23 - From header parse error
* -24 - Bad hostname in To header
* -25 - Bad hostname in From header
* -255 - undefined errors.

This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
Expand Down
14 changes: 14 additions & 0 deletions modules/sipmsgops/doc/sipmsgops_admin.xml
Expand Up @@ -722,6 +722,12 @@ add_body("Hello World!", "text/plain");
<listitem><para><emphasis>'r'</emphasis> - checks the R-URI
and whether the domain contains valid characters.
</para></listitem>
<listitem><para><emphasis>'f'</emphasis> - checks the URI of the 'From' field
and whether the domain contains valid characters.
</para></listitem>
<listitem><para><emphasis>'t'</emphasis> - checks the URI of the 'To' field
and whether the domain contains valid characters.
</para></listitem>
</itemizedlist>
</para>
<para>The result_pvar parameter sets resulting pvar with text error reason in case of
Expand Down Expand Up @@ -775,6 +781,14 @@ add_body("Hello World!", "text/plain");
</para></listitem>
<listitem><para><emphasis>-21</emphasis> - No Content-Type header
</para></listitem>
<listitem><para><emphasis>-22</emphasis> - To header parse error
</para></listitem>
<listitem><para><emphasis>-23</emphasis> - From header parse error
</para></listitem>
<listitem><para><emphasis>-24</emphasis> - Bad hostname in To header
</para></listitem>
<listitem><para><emphasis>-25</emphasis> - Bad hostname in From header
</para></listitem>
<listitem><para><emphasis>-255</emphasis> - undefined errors.
</para></listitem>
</itemizedlist>
Expand Down
71 changes: 71 additions & 0 deletions modules/sipmsgops/sipmsgops.c
Expand Up @@ -1413,6 +1413,8 @@ static int is_audio_on_hold_f(struct sip_msg *msg, char *str1, char *str2 )
#define SIP_PARSE_HDR 0x2
#define SIP_PARSE_NOMF 0x4
#define SIP_PARSE_RURI 0x8
#define SIP_PARSE_TO 0x10
#define SIP_PARSE_FROM 0x20

static int fixup_sip_validate(void** param, int param_no)
{
Expand Down Expand Up @@ -1450,6 +1452,16 @@ static int fixup_sip_validate(void** param, int param_no)
flags |= SIP_PARSE_RURI;
break;

case 't':
case 'T':
flags |= SIP_PARSE_TO;
break;

case 'f':
case 'F':
flags |= SIP_PARSE_FROM;
break;

default:
LM_DBG("unknown option \'%c\'\n", *flags_s);
break;
Expand Down Expand Up @@ -1796,6 +1808,10 @@ enum sip_validation_failures {
SV_NO_UNSUPPORTED=-19,
SV_NO_WWW_AUTH=-20,
SV_NO_CONTENT_TYPE=-21,
SV_TO_PARSE_ERROR=-22,
SV_FROM_PARSE_ERROR=-23,
SV_TO_DOMAIN_ERROR=-24,
SV_FROM_DOMAIN_ERROR=-25,
SV_GENERIC_FAILURE=-255
};

Expand All @@ -1805,6 +1821,7 @@ static int w_sip_validate(struct sip_msg *msg, char *flags_s, char* pv_result)
int method;
str body;
struct cseq_body * cbody;
struct to_body *from, *to;
unsigned long flags;
pv_elem_t* pv_res = (pv_elem_t*)pv_result;
pv_value_t pv_val;
Expand Down Expand Up @@ -1905,6 +1922,60 @@ static int w_sip_validate(struct sip_msg *msg, char *flags_s, char* pv_result)
/* check only if Via1 is present */
ret = SV_NO_VIA1;
CHECK_HEADER("", via1);

/* test to header uri */
if(flags & SIP_PARSE_TO) {
if(!msg->to->parsed) {
if(parse_to_header(msg) < 0) {
strcpy(reason, "failed to parse \'To\' field URI");
ret = SV_TO_PARSE_ERROR;
goto failed;
}
}

to = (struct to_body*)msg->to->parsed;

if(to->error == PARSE_ERROR ||
parse_uri(to->uri.s, to->uri.len, &to->parsed_uri) < 0) {
strcpy(reason, "failed to parse \'To\' field URI");
ret = SV_TO_PARSE_ERROR;
goto failed;
}

/* check for valid domain format */
if(check_hostname(&to->parsed_uri.host) < 0) {
strcpy(reason, "invalid domain for \'To\' field");
ret = SV_TO_DOMAIN_ERROR;
goto failed;
}
}

/* test from header uri */
if(flags & SIP_PARSE_FROM) {
if(!msg->from->parsed) {
if(parse_from_header(msg) < 0) {
strcpy(reason, "failed to parse \'From\' field URI");
ret = SV_FROM_PARSE_ERROR;
goto failed;
}
}

from = (struct to_body*)msg->from->parsed;

if(from->error == PARSE_ERROR ||
parse_uri(from->uri.s, from->uri.len, &from->parsed_uri) < 0) {
strcpy(reason, "failed to parse \'From\' field URI");
ret = SV_FROM_PARSE_ERROR;
goto failed;
}

/* check for valid domain format */
if(check_hostname(&from->parsed_uri.host) < 0) {
strcpy(reason, "invalid domain for \'From\' field");
ret = SV_FROM_DOMAIN_ERROR;
goto failed;
}
}

/* request or reply */
switch (msg->first_line.type) {
Expand Down

0 comments on commit d2ad483

Please sign in to comment.