bool CMapiMessage::CopyBinAttachToFile(LPATTACH lpAttach, nsIFile **tmp_file) { nsCOMPtr<nsIFile> _tmp_file; nsresult rv = GetSpecialDirectoryWithFileName(NS_OS_TEMP_DIR, "mapiattach.tmp", getter_AddRefs(_tmp_file)); NS_ENSURE_SUCCESS(rv, false); rv = _tmp_file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 00600); NS_ENSURE_SUCCESS(rv, false); nsCString tmpPath; _tmp_file->GetNativePath(tmpPath); LPSTREAM lpStreamFile; HRESULT hr = CMapiApi::OpenStreamOnFile(gpMapiAllocateBuffer, gpMapiFreeBuffer, STGM_READWRITE | STGM_CREATE, const_cast<char*>(tmpPath.get()), NULL, &lpStreamFile); if (HR_FAILED(hr)) { MAPI_TRACE1("~~ERROR~~ OpenStreamOnFile failed - temp path: %s\r\n", tmpPath.get()); return false; } bool bResult = true; LPSTREAM lpAttachStream; hr = lpAttach->OpenProperty(PR_ATTACH_DATA_BIN, &IID_IStream, 0, 0, (LPUNKNOWN *)&lpAttachStream); if (HR_FAILED(hr)) { MAPI_TRACE0("~~ERROR~~ OpenProperty failed for PR_ATTACH_DATA_BIN.\r\n"); lpAttachStream = NULL; bResult = false; } else { STATSTG st; hr = lpAttachStream->Stat(&st, STATFLAG_NONAME); if (HR_FAILED(hr)) { MAPI_TRACE0("~~ERROR~~ Stat failed for attachment stream\r\n"); bResult = false; } else { hr = lpAttachStream->CopyTo(lpStreamFile, st.cbSize, NULL, NULL); if (HR_FAILED(hr)) { MAPI_TRACE0("~~ERROR~~ Attach Stream CopyTo temp file failed.\r\n"); bResult = false; } } } if (lpAttachStream) lpAttachStream->Release(); lpStreamFile->Release(); if (!bResult) _tmp_file->Remove(false); else CallQueryInterface(_tmp_file, tmp_file); return bResult; }
bool CMapiMessage::CopyMsgAttachToFile(LPATTACH lpAttach, /*out*/ nsIFile **tmp_file) { bool bResult = true; LPMESSAGE lpMsg; HRESULT hr = lpAttach->OpenProperty(PR_ATTACH_DATA_OBJ, &IID_IMessage, 0, 0, reinterpret_cast<LPUNKNOWN *>(&lpMsg)); NS_ENSURE_SUCCESS(hr, false); if (!GetTmpFile(tmp_file)) return false; nsCOMPtr<nsIOutputStream> destOutputStream; nsresult rv = MsgNewBufferedFileOutputStream(getter_AddRefs(destOutputStream), *tmp_file, -1, 0600); if (NS_SUCCEEDED(rv)) rv = nsOutlookMail::ImportMessage(lpMsg, destOutputStream, nsIMsgSend::nsMsgSaveAsDraft); if (NS_FAILED(rv)) { (*tmp_file)->Remove(false); (*tmp_file)->Release(); *tmp_file = 0; } return NS_SUCCEEDED(rv); }
BOOL CMapiMessage::CopyBinAttachToFile( LPATTACH lpAttach) { LPSTREAM lpStreamFile; m_ownsAttachFile = FALSE; m_attachPath.Truncate(); nsCOMPtr<nsIFile> tmpFile; nsresult rv = GetSpecialDirectoryWithFileName(NS_OS_TEMP_DIR, "mapiattach.tmp", getter_AddRefs(tmpFile)); NS_ENSURE_SUCCESS(rv, rv); rv = tmpFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 00600); NS_ENSURE_SUCCESS(rv, rv); nsCString tmpPath; tmpFile->GetNativePath(tmpPath); HRESULT hr = CMapiApi::OpenStreamOnFile( gpMapiAllocateBuffer, gpMapiFreeBuffer, STGM_READWRITE | STGM_CREATE, (char *) tmpPath.get(), NULL, &lpStreamFile); if (HR_FAILED(hr)) { MAPI_TRACE1("~~ERROR~~ OpenStreamOnFile failed - temp path: %s\r\n", tmpPath.get()); return( FALSE); } MAPI_TRACE1("\t\t** Attachment extracted to temp file: %s\r\n", m_attachPath.get()); BOOL bResult = TRUE; LPSTREAM lpAttachStream; hr = lpAttach->OpenProperty( PR_ATTACH_DATA_BIN, &IID_IStream, 0, 0, (LPUNKNOWN *)&lpAttachStream); if (HR_FAILED( hr)) { MAPI_TRACE0( "~~ERROR~~ OpenProperty failed for PR_ATTACH_DATA_BIN.\r\n"); lpAttachStream = NULL; bResult = FALSE; } else { STATSTG st; hr = lpAttachStream->Stat( &st, STATFLAG_NONAME); if (HR_FAILED( hr)) { MAPI_TRACE0( "~~ERROR~~ Stat failed for attachment stream\r\n"); bResult = FALSE; } else { hr = lpAttachStream->CopyTo( lpStreamFile, st.cbSize, NULL, NULL); if (HR_FAILED( hr)) { MAPI_TRACE0( "~~ERROR~~ Attach Stream CopyTo temp file failed.\r\n"); bResult = FALSE; } } } m_attachPath = tmpPath; if (lpAttachStream) lpAttachStream->Release(); lpStreamFile->Release(); if (!bResult) tmpFile->Remove(PR_FALSE); else m_ownsAttachFile = TRUE; return( bResult); }
/* This seems to be called if one clicks on Save in the context menu. And also sometimes before an Open click. */ STDMETHODIMP GpgolAttachedFileEvents::OnWritePattToSzFile (LPATTACH att, LPTSTR file, ULONG flags) { HRESULT hr; ULONG tag; char *iv; size_t ivlen; symenc_t symenc; LPSTREAM stream; char tmpbuf[16]; ULONG nread; int rc; log_debug ("%s:%s: att=%p file=`%s' flags=%lx\n", SRCNAME, __func__, att, file, flags); if (!att) return E_FAIL; if (get_gpgolprotectiv_tag ((LPMESSAGE)att, &tag) ) return E_ABORT; iv = mapi_get_binary_prop ((LPMESSAGE)att, tag, &ivlen); if (!iv) return S_FALSE; /* Not encrypted by us - Let OL continue as usual. */ symenc = symenc_open (get_128bit_session_key (), 16, iv, ivlen); xfree (iv); if (!symenc) { log_error ("%s:%s: can't open encryption context", SRCNAME, __func__); return E_ABORT; } hr = att->OpenProperty (PR_ATTACH_DATA_BIN, &IID_IStream, 0, 0, (LPUNKNOWN*) &stream); if (FAILED (hr)) { log_error ("%s:%s: can't open data stream of attachment: hr=%#lx", SRCNAME, __func__, hr); symenc_close (symenc); return E_ABORT; } hr = stream->Read (tmpbuf, 16, &nread); if (hr) { log_debug ("%s:%s: Read failed: hr=%#lx", SRCNAME, __func__, hr); stream->Release (); symenc_close (symenc); return E_ABORT; } symenc_cfb_decrypt (symenc, tmpbuf, tmpbuf, 16); if (memcmp (tmpbuf, "GpgOL attachment", 16)) { MessageBox (NULL, _("Sorry, we are not able to decrypt this attachment.\n\n" "Please use the decrypt/verify button to decrypt the\n" "entire message again. Then open this attachment."), "GpgOL", MB_ICONERROR|MB_OK); stream->Release (); symenc_close (symenc); return E_ABORT; } rc = decrypt_and_write_file (stream, file, symenc); stream->Release (); symenc_close (symenc); return rc; }