summaryrefslogtreecommitdiff
path: root/apps/app_voicemail.c
diff options
context:
space:
mode:
authorMatthew Jordan <mjordan@digium.com>2015-03-10 18:13:08 +0000
committerMatthew Jordan <mjordan@digium.com>2015-03-10 18:13:08 +0000
commit055001716cd6548ec644f3cc62841b3b7ff4f66c (patch)
tree78001ce2b5f0df4125435a667e9c5d78146c868f /apps/app_voicemail.c
parent92178247eefcf9b84bbc22a836345b43ab8770b2 (diff)
app_voicemail: Fix crash with IMAP backends when greetings aren't present
When an IMAP backend is in use and greetings are set to be used, but aren't present for a user in their IMAP folder, Asterisk will crash. This occurs due to the mailstream being set to the 'greetings' folder and being left in that particular state, regardless of the success/failure of the attempt to access the folder the mailstream points to. Later access of the mailstream assumes that it points to the 'INBOX' (or some other folder), resulting in either a crash (if the greetings folder didn't exist and the mailstream is invalid) or an inability to read messages from the 'INBOX' folder. This patch restores the mailstream to its correct state after accessing the greetings. This fixes the crash, and sets the mailstream to the state that VoiceMailMain expects. Note that while ASTERISK-23390 also contained a patch for this issue, the patch on ASTERISK-24786 is the one being merged here. Review: https://reviewboard.asterisk.org/r/4459/ ASTERISK-23390 #close Reported by: Ben Smithurst ASTERISK-24786 #close Reported by: Graham Barnett Tested by: Graham Barnett patches: app_voicemail.c.patch.SIGSEGV3rev2 uploaded by Graham Barnett (License 6685) ........ Merged revisions 432695 from http://svn.asterisk.org/svn/asterisk/branches/11 git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@432696 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'apps/app_voicemail.c')
-rw-r--r--apps/app_voicemail.c41
1 files changed, 34 insertions, 7 deletions
diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index fa334e350..e8cea623f 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -2173,6 +2173,8 @@ static int imap_retrieve_greeting(const char *dir, const int msgnum, struct ast_
char *attachment;
int i;
BODY *body;
+ int ret = 0;
+ int curr_mbox;
/* This function is only used for retrieval of IMAP greetings
* regular messages are not retrieved this way, nor are greetings
@@ -2206,6 +2208,10 @@ static int imap_retrieve_greeting(const char *dir, const int msgnum, struct ast_
*vms_p->introfn = '\0';
ast_mutex_lock(&vms_p->lock);
+
+ /* get the current mailbox so that we can point the mailstream back to it later */
+ curr_mbox = get_folder_by_name(vms_p->curbox);
+
if (init_mailstream(vms_p, GREETINGS_FOLDER) || !vms_p->mailstream) {
ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL or can't init_mailstream\n");
ast_mutex_unlock(&vms_p->lock);
@@ -2220,21 +2226,28 @@ static int imap_retrieve_greeting(const char *dir, const int msgnum, struct ast_
attachment = ast_strdupa(body->nested.part->next->body.parameter->value);
} else {
ast_log(AST_LOG_ERROR, "There is no file attached to this IMAP message.\n");
- ast_mutex_unlock(&vms_p->lock);
- return -1;
+ ret = -1;
+ break;
}
filename = strsep(&attachment, ".");
if (!strcmp(filename, file)) {
ast_copy_string(vms_p->fn, dir, sizeof(vms_p->fn));
vms_p->msgArray[vms_p->curmsg] = i + 1;
save_body(body, vms_p, "2", attachment, 0);
- ast_mutex_unlock(&vms_p->lock);
- return 0;
+ ret = 0;
+ break;
}
}
- ast_mutex_unlock(&vms_p->lock);
- return -1;
+ if (curr_mbox != -1) {
+ /* restore previous mbox stream */
+ if (init_mailstream(vms_p, curr_mbox) || !vms_p->mailstream) {
+ ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL or can't init_mailstream\n");
+ ret = -1;
+ }
+ }
+ ast_mutex_unlock(&vms_p->lock);
+ return ret;
}
static int imap_retrieve_file(const char *dir, const int msgnum, const char *mailbox, const char *context)
@@ -2248,12 +2261,13 @@ static int imap_retrieve_file(const char *dir, const int msgnum, const char *mai
FILE *text_file_ptr;
int res = 0;
struct ast_vm_user *vmu;
+ int curr_mbox;
if (!(vmu = find_user(NULL, context, mailbox))) {
ast_log(LOG_WARNING, "Couldn't find user with mailbox %s@%s\n", mailbox, context);
return -1;
}
-
+
if (msgnum < 0) {
if (imapgreetings) {
res = imap_retrieve_greeting(dir, msgnum, vmu);
@@ -2280,6 +2294,19 @@ static int imap_retrieve_file(const char *dir, const int msgnum, const char *mai
goto exit;
}
+ /* Ensure we have the correct mailbox open and have a valid mailstream for it */
+ curr_mbox = get_folder_by_name(vms->curbox);
+ if (curr_mbox < 0) {
+ ast_debug(3, "Mailbox folder curbox not set, defaulting to Inbox\n");
+ curr_mbox = 0;
+ }
+ init_mailstream(vms, curr_mbox);
+ if (!vms->mailstream) {
+ ast_log(AST_LOG_ERROR, "IMAP mailstream for %s is NULL\n", vmu->mailbox);
+ res = -1;
+ goto exit;
+ }
+
make_file(vms->fn, sizeof(vms->fn), dir, msgnum);
snprintf(vms->introfn, sizeof(vms->introfn), "%sintro", vms->fn);