diff --git a/doc/manpage.xml b/doc/manpage.xml index 8ac63170c..6bbcad936 100644 --- a/doc/manpage.xml +++ b/doc/manpage.xml @@ -186,6 +186,7 @@ + @@ -819,6 +820,22 @@ names. + + + + + + When the recording file name is built from the Call-ID and + from-tag (the R command and the + option), percent-encode the / + and % characters as %2F and + %25 instead of refusing to record calls whose + Call-ID or from-tag contains /, which is a legal + character in SIP. The original value can be recovered by decoding + the %XX sequences. File names given explicitly with + the C command are not affected. + + diff --git a/rtpproxy.8 b/rtpproxy.8 index 695805388..aa947f602 100644 --- a/rtpproxy.8 +++ b/rtpproxy.8 @@ -31,7 +31,7 @@ rtpproxy \- RTP (Real\-time Transport Protocol) Proxy Server .SH "SYNOPSIS" .HP \w'\fBrtpproxy\fR\ 'u -\fBrtpproxy\fR [\fB\-?\fR] [\fB\-2\fR] [\fB\-f\fR] [\fB\-v\fR] [\fB\-V\fR] [\fB\-R\fR] [\fB\-l\fR\ \fIaddr1\fR\fI[/addr2]\fR...] [\fB\-l\fR\ \fI[label@]\fR\fIaddress\fR\fI[=advertized_address]\fR...] [\fB\-6\fR\ \fIaddr1\fR\fI[/addr2]\fR...] [\fB\-6\fR\ \fI[label@]\fR\fIaddress\fR\fI[=advertized_address]\fR...] [\fB\-s\fR\ \fIctrl_socket\fR] [\fB\-t\fR\ \fItos\fR] [\fB\-p\fR\ \fIpidfile\fR] [\fB\-T\fR\ \fImax_ttl\fR] [\fB\-\-maxpps\fR\ \fIpps\fR\fI[,rtcp_pps]\fR] [\fB\-\-maxpsize\fR\ \fIsize\fR\fI[,rtcp_size]\fR] [\fB\-r\fR\ \fIrdir\fR\ [\fB\-S\fR\ \fIsdir\fR]] [\fB\-L\fR\ \fInofile_limit\fR] [\fB\-A\fR\ \fIadvaddr1\fR\fI[/advaddr2]\fR] [\fB\-m\fR\ \fImin_port\fR] [\fB\-M\fR\ \fImax_port\fR] [\fB\-u\fR\ \fIuname\fR\fI[:gname]\fR] [\fB\-w\fR\ \fIsock_mode\fR] [\fB\-F\fR] [\fB\-i\fR] [\fB\-n\fR\ \fItimeout_socket\fR] [\fB\-P\fR] [\fB\-a\fR] [\fB\-d\fR\ \fIlog_level\fR\fI[:log_facility]\fR] [\fB\-W\fR\ \fIsetup_ttl\fR] [\fB\-\-force_asymmetric\fR] [\fB\-b\fR] [\fB\-c\fR\ \fIfifo|rr\fR] [\fB\-\-nice\fR\ \fIlevel\fR] [\fB\-D\fR] [\fB\-\-no_redirect\fR] [\fB\-\-config\fR\ \fIconfig_file\fR] [\fB\-\-dso\fR\ \fImodule\fR...] [\fB\-\-bridge_symmetric\fR] [\fB\-\-no_resolve\fR] +\fBrtpproxy\fR [\fB\-?\fR] [\fB\-2\fR] [\fB\-f\fR] [\fB\-v\fR] [\fB\-V\fR] [\fB\-R\fR] [\fB\-l\fR\ \fIaddr1\fR\fI[/addr2]\fR...] [\fB\-l\fR\ \fI[label@]\fR\fIaddress\fR\fI[=advertized_address]\fR...] [\fB\-6\fR\ \fIaddr1\fR\fI[/addr2]\fR...] [\fB\-6\fR\ \fI[label@]\fR\fIaddress\fR\fI[=advertized_address]\fR...] [\fB\-s\fR\ \fIctrl_socket\fR] [\fB\-t\fR\ \fItos\fR] [\fB\-p\fR\ \fIpidfile\fR] [\fB\-T\fR\ \fImax_ttl\fR] [\fB\-\-maxpps\fR\ \fIpps\fR\fI[,rtcp_pps]\fR] [\fB\-\-maxpsize\fR\ \fIsize\fR\fI[,rtcp_size]\fR] [\fB\-r\fR\ \fIrdir\fR\ [\fB\-S\fR\ \fIsdir\fR]] [\fB\-L\fR\ \fInofile_limit\fR] [\fB\-A\fR\ \fIadvaddr1\fR\fI[/advaddr2]\fR] [\fB\-m\fR\ \fImin_port\fR] [\fB\-M\fR\ \fImax_port\fR] [\fB\-u\fR\ \fIuname\fR\fI[:gname]\fR] [\fB\-w\fR\ \fIsock_mode\fR] [\fB\-F\fR] [\fB\-i\fR] [\fB\-n\fR\ \fItimeout_socket\fR] [\fB\-P\fR] [\fB\-a\fR] [\fB\-d\fR\ \fIlog_level\fR\fI[:log_facility]\fR] [\fB\-W\fR\ \fIsetup_ttl\fR] [\fB\-\-force_asymmetric\fR] [\fB\-b\fR] [\fB\-c\fR\ \fIfifo|rr\fR] [\fB\-\-nice\fR\ \fIlevel\fR] [\fB\-D\fR] [\fB\-\-no_redirect\fR] [\fB\-\-config\fR\ \fIconfig_file\fR] [\fB\-\-dso\fR\ \fImodule\fR...] [\fB\-\-bridge_symmetric\fR] [\fB\-\-no_resolve\fR] [\fB\-\-record_fname_encode\fR] .HP \w'\fBrtpproxy\fR\ 'u \fBrtpproxy\fR \fB\-C\fR .SH "DESCRIPTION" @@ -502,6 +502,28 @@ In the bridging mode, do not treat all sessions as asymmetric, which is the defa .RS 4 Only accept numeric IP addresses, in the command line arguments and in the control commands, instead of resolving host names\&. .RE +.PP +\fB\-\-record_fname_encode\fR +.RS 4 +When the recording file name is built from the Call\-ID and from\-tag (the +\fBR\fR +command and the +\fB\-a\fR +option), percent\-encode the +"/" +and +"%" +characters as +"%2F" +and +"%25" +instead of refusing to record calls whose Call\-ID or from\-tag contains +"/", which is a legal character in SIP\&. The original value can be recovered by decoding the +"%XX" +sequences\&. File names given explicitly with the +\fBC\fR +command are not affected\&. +.RE .SH "PRINCIPLES OF OPERATION" .PP When the SIP controller receives an INVITE request, it extracts the Call\-ID and from_tag from INVITE\&. The call controller communicates it with the rtpproxy via Unix domain socket or a UDP socket\&. rtpproxy looks for an existing session with the given Call\-ID and from_tag\&. diff --git a/src/main.c b/src/main.c index fc1bce691..f5a24404b 100644 --- a/src/main.c +++ b/src/main.c @@ -152,6 +152,7 @@ usage(void) "[-n timeout_socket]\n\t [-d log_level[:log_facility]] [-p pid_file]\n" "\t [-c fifo|rr] [-A addr1[/addr2] [-W setup_ttl]\n" "\t [--maxpps pps[,rtcp_pps]] [--maxpsize size[,rtcp_size]]\n" + "\t [--record_fname_encode]\n" "\trtpproxy -V\n"); } @@ -266,6 +267,7 @@ ehandler(void) #define LOPT_NO_REDIR 263 #define LOPT_MAXPPS 264 #define LOPT_MAXPSIZE 265 +#define LOPT_RFNAME_ENC 266 const static struct option longopts[] = { { "dso", required_argument, NULL, LOPT_DSO }, @@ -278,6 +280,7 @@ const static struct option longopts[] = { { "no_redirect", no_argument, NULL, LOPT_NO_REDIR }, { "maxpps", required_argument, NULL, LOPT_MAXPPS }, { "maxpsize", required_argument, NULL, LOPT_MAXPSIZE }, + { "record_fname_encode", no_argument, NULL, LOPT_RFNAME_ENC }, { NULL, 0, NULL, 0 } }; @@ -519,6 +522,10 @@ init_config(struct rtpp_cfg *cfsp, int argc, const char * const *argv) } break; + case LOPT_RFNAME_ENC: + cfsp->record_fname_enc = 1; + break; + case 'c': if (strcmp(optarg, "fifo") == 0) { cfsp->sched_policy = SCHED_FIFO; diff --git a/src/rtpp_cfg.h b/src/rtpp_cfg.h index 9ea183660..724313041 100644 --- a/src/rtpp_cfg.h +++ b/src/rtpp_cfg.h @@ -108,6 +108,8 @@ struct rtpp_cfg { const char *sdir; int record_pcap; /* Record in the PCAP format? */ int record_all; /* Record everything */ + int record_fname_enc; /* Percent-encode '/' and '%' in file names + built from Call-ID and from-tag */ int rrtcp; /* Whether or not to relay RTCP? */ struct rtpp_log *glog; diff --git a/src/rtpp_record.c b/src/rtpp_record.c index 30c216e7b..a9427a448 100644 --- a/src/rtpp_record.c +++ b/src/rtpp_record.c @@ -164,6 +164,44 @@ ropen_remote_ctor_pa(const struct rtpp_cfg *cfg, struct rtpp_record_channel *rrc return (0); } +/* + * Copy slen bytes of src (a Call-ID or a from-tag, which are not + * NUL-terminated) into dst as a NUL-terminated file name component. With + * encode set, '/' and '%' are percent-encoded as "%2F" and "%25", so the + * result never contains '/' and the original value can be recovered by + * decoding "%XX". Returns the length of the result, or -1 if it does not + * fit into dlen bytes. + */ +static int +rtpp_record_fname_cpy(char *dst, size_t dlen, const char *src, size_t slen, + int encode) +{ + size_t i, j; + const char *esc; + + if (dlen == 0) + return (-1); + for (i = j = 0; i < slen; i++) { + esc = NULL; + if (encode && src[i] == '/') + esc = "%2F"; + else if (encode && src[i] == '%') + esc = "%25"; + if (esc != NULL) { + if (dlen - j < 4) + return (-1); + memcpy(dst + j, esc, 3); + j += 3; + } else { + if (dlen - j < 2) + return (-1); + dst[j++] = src[i]; + } + } + dst[j] = '\0'; + return ((int)j); +} + struct rtpp_record * rtpp_record_ctor(const struct rtpp_cfg *cfsp, const struct remote_copy_args *rap, struct rtpp_session *sp, const char *rname, int orig, int record_type) @@ -173,6 +211,7 @@ rtpp_record_ctor(const struct rtpp_cfg *cfsp, const struct remote_copy_args *rap int rval, remote; size_t len; pcap_hdr_t pcap_hdr; + char cid[PATH_MAX + 1], tag[PATH_MAX + 1]; remote = (rname != NULL && strncmp("udp:", rname, 4) == 0) ? 1 : 0; @@ -211,11 +250,20 @@ rtpp_record_ctor(const struct rtpp_cfg *cfsp, const struct remote_copy_args *rap goto e2; } - if (rname == NULL && (memchr(sp->call_id->s, '/', sp->call_id->len) != NULL || - memchr(sp->from_tag_nmn->s, '/', sp->from_tag_nmn->len) != NULL)) { - RTPP_LOG(sp->log, RTPP_LOG_ERR, "call-id or from-tag contains '/', " - "refusing to use it in the recording file name"); - goto e2; + if (rname == NULL) { + if (rtpp_record_fname_cpy(cid, sizeof(cid), sp->call_id->s, + sp->call_id->len, cfsp->record_fname_enc) < 0 || + rtpp_record_fname_cpy(tag, sizeof(tag), sp->from_tag_nmn->s, + sp->from_tag_nmn->len, cfsp->record_fname_enc) < 0) { + RTPP_LOG(sp->log, RTPP_LOG_ERR, "recording file name is too long"); + goto e2; + } + if (strchr(cid, '/') != NULL || strchr(tag, '/') != NULL) { + RTPP_LOG(sp->log, RTPP_LOG_ERR, "call-id or from-tag contains '/', " + "refusing to use it in the recording file name (see " + "--record_fname_encode)"); + goto e2; + } } if (cfsp->record_pcap != 0) { @@ -240,9 +288,8 @@ rtpp_record_ctor(const struct rtpp_cfg *cfsp, const struct remote_copy_args *rap sdir = cfsp->sdir; rrc->needspool = 1; if (rname == NULL) { - len = snprintf(rrc->rpath, sizeof(rrc->rpath), "%s/%.*s=%.*s%s%s", - cfsp->rdir, (int)sp->call_id->len, sp->call_id->s, - (int)sp->from_tag_nmn->len, sp->from_tag_nmn->s, suffix1, suffix2); + len = snprintf(rrc->rpath, sizeof(rrc->rpath), "%s/%s=%s%s%s", + cfsp->rdir, cid, tag, suffix1, suffix2); } else { len = snprintf(rrc->rpath, sizeof(rrc->rpath), "%s/%s%s", cfsp->rdir, rname, suffix2); @@ -253,9 +300,8 @@ rtpp_record_ctor(const struct rtpp_cfg *cfsp, const struct remote_copy_args *rap } } if (rname == NULL) { - len = snprintf(rrc->spath, sizeof(rrc->spath), "%s/%.*s=%.*s%s%s", - sdir, (int)sp->call_id->len, sp->call_id->s, - (int)sp->from_tag_nmn->len, sp->from_tag_nmn->s, suffix1, suffix2); + len = snprintf(rrc->spath, sizeof(rrc->spath), "%s/%s=%s%s%s", + sdir, cid, tag, suffix1, suffix2); } else { len = snprintf(rrc->spath, sizeof(rrc->spath), "%s/%s%s", sdir, rname, suffix2); diff --git a/tests/recording/basic b/tests/recording/basic index 2426e7763..b228386bf 100755 --- a/tests/recording/basic +++ b/tests/recording/basic @@ -37,7 +37,7 @@ run_recording() { report_rc ${RPTL_RC} "running basic.${stest}.rptl RPTL script" } -SUB_TESTS="${SUB_TESTS:-"files singlefile singlefile_c stopall files_stophalf singlefile_stophalf remote remote_p badpath"}" +SUB_TESTS="${SUB_TESTS:-"files singlefile singlefile_c stopall files_stophalf singlefile_stophalf remote remote_p badpath badpath_enc"}" for stest in ${SUB_TESTS} do diff --git a/tests/recording/basic.badpath_enc.rptl b/tests/recording/basic.badpath_enc.rptl new file mode 100644 index 000000000..83c7df093 --- /dev/null +++ b/tests/recording/basic.badpath_enc.rptl @@ -0,0 +1,51 @@ +# Same Call-IDs as in basic.badpath, with --record_fname_encode: '/' and +# '%' in the Call-ID or from-tag are written as "%2F" and "%25", so the +# recording (R, Rs) is created inside of the recording directory under the +# encoded name instead of being refused. Names that do not fit into +# PATH_MAX are still refused. + +.eval: 0 -> RES_OK +.eval: E50 -> RES_E50 +.eval: recording.%%SUBTEST%% -> WORK_BASE +.eval: ../%%WORK_BASE%%.rec. -> CALLID +.eval: ../%%WORK_BASE%%.recs. -> CALLID_S +.eval: %%WORK_BASE%%.pct%41 -> CALLID_P +.eval: %%RECORD_SPL_DIR%%/%%CALLID%%=from_tag_1 -> REC_BASE +.eval: %%RECORD_SPL_DIR%%/%%CALLID_S%%=from_tag_1 -> REC_BASE_S +# Same names, encoded ('?' stands for the '%' of the escape sequences). +.eval: %%RECORD_SPL_DIR%%/..?2F%%WORK_BASE%%.rec.=from_tag_1 -> ENC_BASE +.eval: %%RECORD_SPL_DIR%%/..?2F%%WORK_BASE%%.recs.=from_tag_1 -> ENC_BASE_S +.eval: %%RECORD_SPL_DIR%%/%%WORK_BASE%%.pct?2541=from_tag_1 -> ENC_BASE_P + +.exec: mkdir -p %%RECORD_SPL_DIR%% +.exec: rm -f %%REC_BASE%%.* %%REC_BASE_S%%* %%ENC_BASE%%.* %%ENC_BASE_S%%* %%ENC_BASE_P%%.* + +# Longer than PATH_MAX on any supported platform, yet short enough for +# the commands carrying it to fit into the RTPP_CMD_BUFLEN. +.exec: printf '%4200s' '' | tr ' ' x -> LONG_NAME +.eval: %%WORK_BASE%%.long.%%LONG_NAME%% -> CALLID_LONG + +.eval: -d dbug -T60 -W60 -P -r %%RECORD_SPL_DIR%% --record_fname_encode -> RTPPROXY_ARGS +socket rtpp: stdio: %%RTPPROXY_ARGS%% -> %%WORK_BASE%%.rout + +rtpp: U %%CALLID%% 127.0.0.1 12345 from_tag_1 -> validate_port(_) +rtpp: L %%CALLID%% 127.0.0.1 54321 from_tag_1 to_tag_1 -> validate_port(_) +rtpp: R %%CALLID%% to_tag_1 from_tag_1 -> str_compare(RES_OK) + +rtpp: U %%CALLID_S%% 127.0.0.1 12345 from_tag_1 -> validate_port(_) +rtpp: L %%CALLID_S%% 127.0.0.1 54321 from_tag_1 to_tag_1 -> validate_port(_) +rtpp: Rs %%CALLID_S%% to_tag_1 from_tag_1 -> str_compare(RES_OK) + +rtpp: U %%CALLID_P%% 127.0.0.1 12345 from_tag_1 -> validate_port(_) +rtpp: L %%CALLID_P%% 127.0.0.1 54321 from_tag_1 to_tag_1 -> validate_port(_) +rtpp: R %%CALLID_P%% to_tag_1 from_tag_1 -> str_compare(RES_OK) + +rtpp: U %%CALLID_LONG%% 127.0.0.1 12345 from_tag_1 -> validate_port(_) +rtpp: L %%CALLID_LONG%% 127.0.0.1 54321 from_tag_1 to_tag_1 -> validate_port(_) +rtpp: R %%CALLID_LONG%% to_tag_1 from_tag_1 -> str_compare(RES_E50) +rtpp: Rs %%CALLID_LONG%% to_tag_1 from_tag_1 -> str_compare(RES_E50) + +.close: rtpp +.exec: for f in %%REC_BASE%%.* %%REC_BASE_S%%*; do test ! -e "$f" || exit 1; done +.exec: ls %%ENC_BASE%%.* %%ENC_BASE_S%%* %%ENC_BASE_P%%.* > /dev/null +.exec: rm -f %%ENC_BASE%%.* %%ENC_BASE_S%%* %%ENC_BASE_P%%.*