Author: mjordan Date: Tue Mar 10 13:11:26 2015 New Revision: 432695 URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=432695 Log: 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) Modified: branches/11/apps/app_voicemail.c Modified: branches/11/apps/app_voicemail.c URL: http://svnview.digium.com/svn/asterisk/branches/11/apps/app_voicemail.c?view=diff&rev=432695&r1=432694&r2=432695 ============================================================================== --- branches/11/apps/app_voicemail.c (original) +++ branches/11/apps/app_voicemail.c Tue Mar 10 13:11:26 2015 @@ -2085,6 +2085,8 @@ 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 @@ -2118,6 +2120,10 @@ *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); @@ -2132,21 +2138,28 @@ 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; + } + } + + 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 -1; + return ret; } static int imap_retrieve_file(const char *dir, const int msgnum, const char *mailbox, const char *context) @@ -2160,12 +2173,13 @@ 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); @@ -2188,6 +2202,19 @@ * and should have its msgArray properly set up. */ ast_log(LOG_ERROR, "Couldn't find a vm_state for mailbox %s!!! Oh no!\n", vmu->mailbox); + res = -1; + 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; } -- _____________________________________________________________________ -- Bandwidth and Colocation Provided by http://www.api-digital.com -- svn-commits mailing list To UNSUBSCRIBE or update options visit: http://lists.digium.com/mailman/listinfo/svn-commits