<p>Nathan Bruning has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/19892">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">media cache: make sure generated filenames cannot conflict<br><br>In rare cases, two entries of the media cache would have the same<br>base file name, only with different extensions. During media lookup,<br>the wrong file can be played, depending on the channel format.<br><br>This patch avoids the 'lookup file by extension' for files that<br>originate from the media cache.<br><br>ASTERISK-30408 #close<br><br>Change-Id: I89ad019b4344a8f2f09d388740f78751a8e6673b<br>---<br>M main/file.c<br>1 file changed, 129 insertions(+), 42 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/92/19892/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/file.c b/main/file.c</span><br><span>index d7c7543..871dedb 100644</span><br><span>--- a/main/file.c</span><br><span>+++ b/main/file.c</span><br><span>@@ -535,6 +535,71 @@</span><br><span> ACTION_COPY /* copy file. return 0 on success, -1 on error */</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int stream_file(const char *fn, struct ast_channel *chan, struct ast_format_def *f)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ FILE *bfile;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_filestream *s;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_format_cmp(ast_channel_writeformat(chan), f->format) == AST_FORMAT_CMP_NOT_EQUAL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0; /* not a supported format */</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if ( (bfile = fopen(fn, "r")) == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0; /* cannot open file */</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ s = get_filestream(f, bfile);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!s) {</span><br><span style="color: hsl(120, 100%, 40%);">+ fclose(bfile);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0; /* cannot allocate descriptor */</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (open_wrapper(s)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_closestream(s);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0; /* cannot run open on file */</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ /* ok this is good for OPEN */</span><br><span style="color: hsl(120, 100%, 40%);">+ s->lasttimeout = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ s->fmt = f;</span><br><span style="color: hsl(120, 100%, 40%);">+ s->trans = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ s->filename = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_channel_stream(chan))</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_closestream(ast_channel_stream(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_channel_stream_set(chan, s);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_channel_vstream(chan))</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_closestream(ast_channel_vstream(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_channel_vstream_set(chan, s);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1; /* found */</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int stream_mediacache_file(const char *uri, const char *filename, struct ast_channel *chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char ext[PATH_MAX];</span><br><span style="color: hsl(120, 100%, 40%);">+ char fullpath[PATH_MAX];</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_format_def *f;</span><br><span style="color: hsl(120, 100%, 40%);">+ int format_found = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ </span><br><span style="color: hsl(120, 100%, 40%);">+ ast_media_cache_retrieve_metadata(uri, "ext", ext, PATH_MAX);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(fullpath, PATH_MAX, "%s%s", filename, ext);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* lookup the format */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_RWLIST_RDLOCK(&formats);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_RWLIST_TRAVERSE(&formats, f, list)</span><br><span style="color: hsl(120, 100%, 40%);">+ {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (exts_compare(f->exts, ext + 1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ format_found = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_RWLIST_UNLOCK(&formats);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!format_found) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "No format found for extension %s\n", ext+1);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return stream_file(fullpath, chan, f);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*!</span><br><span> * \internal</span><br><span> * \brief perform various actions on a file. Second argument</span><br><span>@@ -578,13 +643,12 @@</span><br><span> ast_free(fn);</span><br><span> continue;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* for 'OPEN' we need to be sure that the format matches</span><br><span> * what the channel can process</span><br><span> */</span><br><span> if (action == ACTION_OPEN) {</span><br><span> struct ast_channel *chan = (struct ast_channel *)arg2;</span><br><span style="color: hsl(0, 100%, 40%);">- FILE *bfile;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_filestream *s;</span><br><span> </span><br><span> if ((ast_format_cmp(ast_channel_writeformat(chan), f->format) == AST_FORMAT_CMP_NOT_EQUAL) &&</span><br><span> !(((ast_format_get_type(f->format) == AST_MEDIA_TYPE_AUDIO) && fmt) ||</span><br><span>@@ -592,40 +656,11 @@</span><br><span> ast_free(fn);</span><br><span> continue; /* not a supported format */</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- if ( (bfile = fopen(fn, "r")) == NULL) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(fn);</span><br><span style="color: hsl(0, 100%, 40%);">- continue; /* cannot open file */</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- s = get_filestream(f, bfile);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!s) {</span><br><span style="color: hsl(0, 100%, 40%);">- fclose(bfile);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(fn); /* cannot allocate descriptor */</span><br><span style="color: hsl(120, 100%, 40%);">+ res = stream_file(fn, chan, f);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(fn);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!res) {</span><br><span> continue;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- if (open_wrapper(s)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(fn);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_closestream(s);</span><br><span style="color: hsl(0, 100%, 40%);">- continue; /* cannot run open on file */</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (st.st_size == 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- /* ok this is good for OPEN */</span><br><span style="color: hsl(0, 100%, 40%);">- res = 1; /* found */</span><br><span style="color: hsl(0, 100%, 40%);">- s->lasttimeout = -1;</span><br><span style="color: hsl(0, 100%, 40%);">- s->fmt = f;</span><br><span style="color: hsl(0, 100%, 40%);">- s->trans = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- s->filename = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_channel_stream(chan))</span><br><span style="color: hsl(0, 100%, 40%);">- ast_closestream(ast_channel_stream(chan));</span><br><span style="color: hsl(0, 100%, 40%);">- ast_channel_stream_set(chan, s);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_channel_vstream(chan))</span><br><span style="color: hsl(0, 100%, 40%);">- ast_closestream(ast_channel_vstream(chan));</span><br><span style="color: hsl(0, 100%, 40%);">- ast_channel_vstream_set(chan, s);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(fn);</span><br><span> break;</span><br><span> }</span><br><span> switch (action) {</span><br><span>@@ -695,10 +730,6 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (is_remote_path(filename) && !ast_media_cache_retrieve(filename, NULL, buf, buflen)) {</span><br><span style="color: hsl(0, 100%, 40%);">- return filehelper(buf, result_cap, NULL, ACTION_EXISTS);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */</span><br><span> if (lang) {</span><br><span> snprintf(buf, buflen, "%s/%s", lang, filename);</span><br><span>@@ -787,6 +818,35 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int get_mediacache_file(const char *uri, char *file_path, size_t len, struct ast_format_cap *file_fmt_cap) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_format* format;</span><br><span style="color: hsl(120, 100%, 40%);">+ char ext[PATH_MAX];</span><br><span style="color: hsl(120, 100%, 40%);">+ </span><br><span style="color: hsl(120, 100%, 40%);">+ /* download / find file */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_media_cache_retrieve(uri, NULL, file_path, len)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Could not retrieve uri %s from media cache\n", uri);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /*</span><br><span style="color: hsl(120, 100%, 40%);">+ * we have a local filename without extension</span><br><span style="color: hsl(120, 100%, 40%);">+ * retrieve metadata for format</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_media_cache_retrieve_metadata(uri, "ext", ext, PATH_MAX)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "No extension in media cache for file %s\n", uri);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ </span><br><span style="color: hsl(120, 100%, 40%);">+ format = ast_get_format_for_file_ext(ext+1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!format) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Could not retrieve format for file extension %s\n", ext);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_format_cap_append(file_fmt_cap, format, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Retrieved uri %s to local file %s (extension %s)\n", uri, file_path, ext);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)</span><br><span> {</span><br><span> return ast_openstream_full(chan, filename, preflang, 0);</span><br><span>@@ -803,6 +863,7 @@</span><br><span> int res;</span><br><span> int buflen;</span><br><span> char *buf;</span><br><span style="color: hsl(120, 100%, 40%);">+ int from_mediacache;</span><br><span> </span><br><span> if (!asis) {</span><br><span> /* do this first, otherwise we detect the wrong writeformat */</span><br><span>@@ -818,9 +879,13 @@</span><br><span> if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {</span><br><span> return NULL;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||</span><br><span style="color: hsl(0, 100%, 40%);">- !ast_format_cap_has_type(file_fmt_cap, AST_MEDIA_TYPE_AUDIO)) {</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(120, 100%, 40%);">+ </span><br><span style="color: hsl(120, 100%, 40%);">+ if (is_remote_path(filename) && get_mediacache_file(filename, buf, buflen, file_fmt_cap)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ from_mediacache = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) &&</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_format_cap_has_type(file_fmt_cap, AST_MEDIA_TYPE_AUDIO)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ from_mediacache = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span> ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);</span><br><span> ao2_ref(file_fmt_cap, -1);</span><br><span> return NULL;</span><br><span>@@ -838,7 +903,11 @@</span><br><span> if (res == -1) { /* No format available that works with this channel */</span><br><span> return NULL;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- res = filehelper(buf, chan, NULL, ACTION_OPEN);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (from_mediacache) {</span><br><span style="color: hsl(120, 100%, 40%);">+ res = stream_mediacache_file(filename, buf, chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ res = filehelper(buf, chan, NULL, ACTION_OPEN);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> if (res >= 0)</span><br><span> return ast_channel_stream(chan);</span><br><span> return NULL;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/19892">change 19892</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/19892"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I89ad019b4344a8f2f09d388740f78751a8e6673b </div>
<div style="display:none"> Gerrit-Change-Number: 19892 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Nathan Bruning <nathan@iperity.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>