123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- /*
- * Copyright 2005 - 2016 Zarafa and its licensors
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- #include <kopano/platform.h>
- #include <iostream>
- #include <utility>
- #include <kopano/CommonUtil.h>
- #include <kopano/mapiext.h>
- #include <kopano/mapiguidext.h>
- #include <kopano/memory.hpp>
- #include <mapiutil.h>
- #include <mapix.h>
- #include <kopano/namedprops.h>
- #include <kopano/charset/convert.h>
- #include <kopano/RecurrenceState.h>
- #include "fsck.h"
- using namespace KCHL;
- HRESULT FsckCalendar::ValidateMinimalNamedFields(LPMESSAGE lpMessage)
- {
- HRESULT hr = hrSuccess;
- memory_ptr<SPropValue> lpPropertyArray;
- memory_ptr<SPropTagArray> lpPropertyTagArray;
- memory_ptr<MAPINAMEID *> lppTagArray;
- enum {
- E_REMINDER,
- E_ALLDAYEVENT,
- TAG_COUNT
- };
- std::string strTagName[TAG_COUNT];
- /*
- * Allocate the NamedID list and initialize it to all
- * properties which could give us some information about the name.
- */
- hr = allocNamedIdList(TAG_COUNT, &~lppTagArray);
- if (hr != hrSuccess)
- return hr;
- lppTagArray[E_REMINDER]->lpguid = (LPGUID)&PSETID_Common;
- lppTagArray[E_REMINDER]->ulKind = MNID_ID;
- lppTagArray[E_REMINDER]->Kind.lID = dispidReminderSet;
- lppTagArray[E_ALLDAYEVENT]->lpguid = (LPGUID)&PSETID_Appointment;
- lppTagArray[E_ALLDAYEVENT]->ulKind = MNID_ID;
- lppTagArray[E_ALLDAYEVENT]->Kind.lID = dispidAllDayEvent;
- strTagName[E_REMINDER] = "dispidReminderSet";
- strTagName[E_ALLDAYEVENT] = "dispidAllDayEvent";
- hr = ReadNamedProperties(lpMessage, TAG_COUNT, lppTagArray,
- &~lpPropertyTagArray, &~lpPropertyArray);
- if (FAILED(hr))
- return hr;
- for (ULONG i = 0; i < TAG_COUNT; ++i) {
- if (PROP_TYPE(lpPropertyArray[i].ulPropTag) == PT_ERROR) {
- __UPV Value;
- Value.b = false;
- hr = AddMissingProperty(lpMessage, strTagName[i],
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[i], PT_BOOLEAN),
- Value);
- if (hr != hrSuccess)
- return hr;
- }
- }
- return hrSuccess;
- }
- HRESULT FsckCalendar::ValidateTimestamps(LPMESSAGE lpMessage)
- {
- HRESULT hr = hrSuccess;
- memory_ptr<SPropValue> lpPropertyArray;
- memory_ptr<SPropTagArray> lpPropertyTagArray;
- memory_ptr<MAPINAMEID *> lppTagArray;
- const FILETIME *lpStart, *lpEnd, *lpCommonStart, *lpCommonEnd;
- LONG ulDuration;
- enum {
- E_START,
- E_END,
- E_CSTART,
- E_CEND,
- E_DURATION,
- TAG_COUNT
- };
- /*
- * Allocate the NamedID list and initialize it to all
- * properties which could give us some information about the name.
- */
- hr = allocNamedIdList(TAG_COUNT, &~lppTagArray);
- if (hr != hrSuccess)
- return hr;
- lppTagArray[E_START]->lpguid = (LPGUID)&PSETID_Appointment;
- lppTagArray[E_START]->ulKind = MNID_ID;
- lppTagArray[E_START]->Kind.lID = dispidApptStartWhole;
- lppTagArray[E_END]->lpguid = (LPGUID)&PSETID_Appointment;
- lppTagArray[E_END]->ulKind = MNID_ID;
- lppTagArray[E_END]->Kind.lID = dispidApptEndWhole;
- lppTagArray[E_CSTART]->lpguid = (LPGUID)&PSETID_Common;
- lppTagArray[E_CSTART]->ulKind = MNID_ID;
- lppTagArray[E_CSTART]->Kind.lID = dispidCommonStart;
- lppTagArray[E_CEND]->lpguid = (LPGUID)&PSETID_Common;
- lppTagArray[E_CEND]->ulKind = MNID_ID;
- lppTagArray[E_CEND]->Kind.lID = dispidCommonEnd;
- lppTagArray[E_DURATION]->lpguid = (LPGUID)&PSETID_Appointment;
- lppTagArray[E_DURATION]->ulKind = MNID_ID;
- lppTagArray[E_DURATION]->Kind.lID = dispidApptDuration;
- hr = ReadNamedProperties(lpMessage, TAG_COUNT, lppTagArray,
- &~lpPropertyTagArray, &~lpPropertyArray);
- if (FAILED(hr))
- return hr;
- /*
- * Validate parameters:
- * If E_START is missing it can be substituted with E_CSTART and vice versa
- * If E_END is missing it can be substituted with E_CEND and vice versa
- * E_{C}START < E_{C}END
- * E_{C}END - E_{C}START / 60 = Duration
- * If duration is missing, calculate and set it.
- */
- if (PROP_TYPE(lpPropertyArray[E_START].ulPropTag) == PT_ERROR) {
- if (PROP_TYPE(lpPropertyArray[E_CSTART].ulPropTag) == PT_ERROR) {
- std::cout << "No valid starting address could be detected." << std::endl;
- return E_INVALIDARG;
- }
- hr = AddMissingProperty(lpMessage, "dispidApptStartWhole",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_START], PT_SYSTIME),
- lpPropertyArray[E_CSTART].Value);
- if (hr != hrSuccess)
- return hr;
- lpStart = &lpPropertyArray[E_CSTART].Value.ft;
- } else
- lpStart = &lpPropertyArray[E_START].Value.ft;
- if (PROP_TYPE(lpPropertyArray[E_CSTART].ulPropTag) == PT_ERROR) {
- if (PROP_TYPE(lpPropertyArray[E_START].ulPropTag) == PT_ERROR) {
- std::cout << "No valid starting address could be detected." << std::endl;
- return E_INVALIDARG;
- }
- hr = AddMissingProperty(lpMessage, "dispidCommonStart",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_CSTART], PT_SYSTIME),
- lpPropertyArray[E_START].Value);
- if (hr != hrSuccess)
- return hr;
- lpCommonStart = &lpPropertyArray[E_START].Value.ft;
- } else
- lpCommonStart = &lpPropertyArray[E_CSTART].Value.ft;
- if (PROP_TYPE(lpPropertyArray[E_END].ulPropTag) == PT_ERROR) {
- if (PROP_TYPE(lpPropertyArray[E_CEND].ulPropTag) == PT_ERROR) {
- std::cout << "No valid end address could be detected." << std::endl;
- return E_INVALIDARG;
- }
- hr = AddMissingProperty(lpMessage, "dispidApptEndWhole",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_END], PT_SYSTIME),
- lpPropertyArray[E_CEND].Value);
- if (hr != hrSuccess)
- return hr;
- lpEnd = &lpPropertyArray[E_CEND].Value.ft;
- } else
- lpEnd = &lpPropertyArray[E_END].Value.ft;
- if (PROP_TYPE(lpPropertyArray[E_CEND].ulPropTag) == PT_ERROR) {
- if (PROP_TYPE(lpPropertyArray[E_END].ulPropTag) == PT_ERROR) {
- std::cout << "No valid starting address could be detected." << std::endl;
- return E_INVALIDARG;
- }
- hr = AddMissingProperty(lpMessage, "dispidCommonEnd",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_CEND], PT_SYSTIME),
- lpPropertyArray[E_END].Value);
- if (hr != hrSuccess)
- return hr;
- lpCommonEnd = &lpPropertyArray[E_END].Value.ft;
- } else
- lpCommonEnd = &lpPropertyArray[E_CEND].Value.ft;
- if (*lpStart > *lpEnd && *lpCommonStart < *lpCommonEnd) {
- hr = ReplaceProperty(lpMessage, "dispidApptStartWhole",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_START], PT_SYSTIME),
- "Whole start after whole end date.",
- lpPropertyArray[E_CSTART].Value);
- if (hr != hrSuccess)
- return hr;
- hr = ReplaceProperty(lpMessage, "dispidApptEndWhole",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_END], PT_SYSTIME),
- "Whole start after whole end date.",
- lpPropertyArray[E_CEND].Value);
- if (hr != hrSuccess)
- return hr;
- }
- if (*lpCommonStart > *lpCommonEnd && *lpStart < *lpEnd) {
- hr = ReplaceProperty(lpMessage, "dispidCommonStart",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_CSTART], PT_SYSTIME),
- "Common start after common end date.",
- lpPropertyArray[E_START].Value);
- if (hr != hrSuccess)
- return hr;
- hr = ReplaceProperty(lpMessage, "dispidCommonEnd",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_CEND], PT_SYSTIME),
- "Common start after common end date.",
- lpPropertyArray[E_END].Value);
- if (hr != hrSuccess)
- return hr;
- }
- if ((*lpEnd - *lpStart) != (*lpCommonEnd - *lpCommonStart)) {
- std::cout << "Difference in duration: " << endl;
- std::cout << "Common duration (" << (*lpCommonEnd - *lpCommonStart) << ") ";
- std::cout << "- Whole duration (" << (*lpEnd - *lpStart) << ")" <<std::endl;
- return E_INVALIDARG;
- }
- /*
- * Common duration matches whole duration,
- * now we need to compare the duration itself.
- */
- __UPV Value;
- Value.l = (*lpEnd - *lpStart) / 60;
- if (PROP_TYPE(lpPropertyArray[E_DURATION].ulPropTag) == PT_ERROR)
- return AddMissingProperty(lpMessage, "dispidApptDuration",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_DURATION], PT_LONG),
- Value);
- ulDuration = lpPropertyArray[E_DURATION].Value.l;
- /*
- * We already compared duration between common and start,
- * now we have to check if that duration also equals what was set.
- */
- if (ulDuration != Value.l)
- return ReplaceProperty(lpMessage, "dispidApptDuration",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_DURATION], PT_LONG),
- "Duration does not match (End - Start / 60)",
- Value);
- return hrSuccess;
- }
- HRESULT FsckCalendar::ValidateRecurrence(LPMESSAGE lpMessage)
- {
- HRESULT hr = hrSuccess;
- memory_ptr<SPropValue> lpPropertyArray;
- memory_ptr<SPropTagArray> lpPropertyTagArray;
- BOOL bRecurring = FALSE;
- LONG ulType = 0;
- memory_ptr<char> lpData;
- memory_ptr<MAPINAMEID *> lppTagArray;
- unsigned int ulLen = 0;
- enum {
- E_RECURRENCE,
- E_RECURRENCE_TYPE,
- E_RECURRENCE_PATTERN,
- E_RECURRENCE_STATE,
- TAG_COUNT
- };
- /*
- * Allocate the NamedID list and initialize it to all
- * properties which could give us some information about the name.
- */
- hr = allocNamedIdList(TAG_COUNT, &~lppTagArray);
- if (hr != hrSuccess)
- return hr;
- lppTagArray[E_RECURRENCE]->lpguid = (LPGUID)&PSETID_Appointment;
- lppTagArray[E_RECURRENCE]->ulKind = MNID_ID;
- lppTagArray[E_RECURRENCE]->Kind.lID = dispidRecurring;
- lppTagArray[E_RECURRENCE_TYPE]->lpguid = (LPGUID)&PSETID_Appointment;
- lppTagArray[E_RECURRENCE_TYPE]->ulKind = MNID_ID;
- lppTagArray[E_RECURRENCE_TYPE]->Kind.lID = dispidRecurrenceType;
- lppTagArray[E_RECURRENCE_PATTERN]->lpguid = (LPGUID)&PSETID_Appointment;
- lppTagArray[E_RECURRENCE_PATTERN]->ulKind = MNID_ID;
- lppTagArray[E_RECURRENCE_PATTERN]->Kind.lID = dispidRecurrencePattern;
- lppTagArray[E_RECURRENCE_STATE]->lpguid = (LPGUID)&PSETID_Appointment;
- lppTagArray[E_RECURRENCE_STATE]->ulKind = MNID_ID;
- lppTagArray[E_RECURRENCE_STATE]->Kind.lID = dispidRecurrenceState;
- hr = ReadNamedProperties(lpMessage, TAG_COUNT, lppTagArray,
- &~lpPropertyTagArray, &~lpPropertyArray);
- if (FAILED(hr))
- return hr;
- if (PROP_TYPE(lpPropertyArray[E_RECURRENCE].ulPropTag) == PT_ERROR) {
- __UPV Value;
- /*
- * Check if the recurrence type is set, and if this is the case,
- * if the type indicates recurrence.
- */
- if (PROP_TYPE(lpPropertyArray[E_RECURRENCE_TYPE].ulPropTag) == PT_ERROR ||
- lpPropertyArray[E_RECURRENCE_TYPE].Value.l == 0)
- Value.b = false;
- else
- Value.b = true;
- hr = AddMissingProperty(lpMessage, "dispidRecurring",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_RECURRENCE], PT_BOOLEAN),
- Value);
- if (hr != hrSuccess)
- return hr;
- bRecurring = Value.b;
- } else
- bRecurring = lpPropertyArray[E_RECURRENCE].Value.b;
- /*
- * The type has 4 possible values:
- * 0 - No recurrence
- * 1 - Daily
- * 2 - Weekly
- * 3 - Monthly
- * 4 - Yearly
- */
- if (PROP_TYPE(lpPropertyArray[E_RECURRENCE_TYPE].ulPropTag) == PT_ERROR) {
- if (bRecurring) {
- std::cout << "Item is recurring but is missing recurrence type" << std::endl;
- return E_INVALIDARG;
- } else
- ulType = 0;
- } else
- ulType = lpPropertyArray[E_RECURRENCE_TYPE].Value.l;
- if (!bRecurring && ulType > 0) {
- __UPV Value;
- Value.l = 0;
- hr = ReplaceProperty(lpMessage, "dispidRecurrenceType",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_RECURRENCE_TYPE], PT_LONG),
- "No recurrence, but recurrence type is > 0.",
- Value);
- if (hr != hrSuccess)
- return hr;
- } else if (bRecurring && ulType == 0) {
- __UPV Value;
- Value.b = false;
- hr = ReplaceProperty(lpMessage, "dispidRecurring",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_RECURRENCE], PT_BOOLEAN),
- "Recurrence has been set, but type indicates no recurrence.",
- Value);
- if (hr != hrSuccess)
- return hr;
- } else if (ulType > 4) {
- __UPV Value;
- if (bRecurring) {
- Value.b = false;
- bRecurring = false;
- hr = ReplaceProperty(lpMessage, "dispidRecurring",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_RECURRENCE], PT_BOOLEAN),
- "Invalid recurrence type, disabling recurrence.",
- Value);
- if (hr != hrSuccess)
- return hr;
- }
- Value.l = 0;
- ulType = 0;
- hr = ReplaceProperty(lpMessage, "dispidRecurrenceType",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_RECURRENCE], PT_LONG),
- "Invalid recurrence type, disabling recurrence.",
- Value);
- if (hr != hrSuccess)
- return hr;
- }
- if (PROP_TYPE(lpPropertyArray[E_RECURRENCE_PATTERN].ulPropTag) == PT_ERROR ||
- strcmp(lpPropertyArray[E_RECURRENCE_PATTERN].Value.lpszA, "") == 0) {
- if (bRecurring) {
- __UPV Value;
-
- switch (ulType) {
- case 1:
- Value.lpszA = const_cast<char *>("Daily");
- break;
- case 2:
- Value.lpszA = const_cast<char *>("Weekly");
- break;
- case 3:
- Value.lpszA = const_cast<char *>("Monthly");
- break;
- case 4:
- Value.lpszA = const_cast<char *>("Yearly");
- break;
- default:
- Value.lpszA = const_cast<char *>("Invalid");
- break;
- }
- hr = AddMissingProperty(lpMessage, "dispidRecurrencePattern",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_RECURRENCE_PATTERN], PT_STRING8),
- Value);
- if (hr != hrSuccess)
- return hr;
- }
- }
-
- if (bRecurring && PROP_TYPE(lpPropertyArray[E_RECURRENCE_STATE].ulPropTag) != PT_ERROR) {
- // Check the actual recurrence state
- RecurrenceState r;
- __UPV Value;
- std::vector<RecurrenceState::Exception>::iterator iEx;
- std::vector<RecurrenceState::ExtendedException>::iterator iEEx;
- convert_context convertContext;
- switch (r.ParseBlob(reinterpret_cast<char *>(lpPropertyArray[E_RECURRENCE_STATE].Value.bin.lpb), lpPropertyArray[E_RECURRENCE_STATE].Value.bin.cb, RECURRENCE_STATE_CALENDAR)) {
- case hrSuccess:
- case MAPI_W_ERRORS_RETURNED:
- // Recurrence state is readable, but may have errors.
-
- // First, make sure the number of extended exceptions is correct
- while (r.lstExtendedExceptions.size() > r.lstExceptions.size())
- r.lstExtendedExceptions.erase(--r.lstExtendedExceptions.end());
-
- // Add new extendedexceptions if missing
- iEx = r.lstExceptions.begin();
-
- for (size_t i = 0; i < r.lstExtendedExceptions.size(); ++i)
- ++iEx;
-
- while(r.lstExtendedExceptions.size() < r.lstExceptions.size()) {
- wstring wstr;
- RecurrenceState::ExtendedException ex;
-
- ex.ulStartDateTime = iEx->ulStartDateTime;
- ex.ulEndDateTime = iEx->ulEndDateTime;
- ex.ulOriginalStartDate = iEx->ulOriginalStartDate;
-
- TryConvert(convertContext, iEx->strSubject, rawsize(iEx->strSubject), "windows-1252", wstr);
- ex.strWideCharSubject.assign(wstr.c_str(), wstr.size());
-
- TryConvert(convertContext, iEx->strLocation, rawsize(iEx->strLocation), "windows-1252", wstr);
- ex.strWideCharLocation.assign(wstr.c_str(), wstr.size());
-
- r.lstExtendedExceptions.push_back(std::move(ex));
- ++iEx;
- }
-
- // Set some defaults right for exceptions
- for (iEx = r.lstExceptions.begin(); iEx != r.lstExceptions.end(); ++iEx)
- iEx->ulOriginalStartDate = (iEx->ulOriginalStartDate / 1440) * 1440;
-
- // Set some defaults for extended exceptions
- iEx = r.lstExceptions.begin();
- for (iEEx = r.lstExtendedExceptions.begin(); iEEx != r.lstExtendedExceptions.end(); ++iEEx) {
- wstring wstr;
- iEEx->strReservedBlock1 = "";
- iEEx->strReservedBlock2 = "";
- iEEx->ulChangeHighlightValue = 0;
- iEEx->ulOriginalStartDate = (iEx->ulOriginalStartDate / 1440) * 1440;
- TryConvert(convertContext, iEx->strSubject, rawsize(iEx->strSubject), "windows-1252", wstr);
- iEEx->strWideCharSubject.assign(wstr.c_str(), wstr.size());
- TryConvert(convertContext, iEx->strLocation, rawsize(iEx->strLocation), "windows-1252", wstr);
- iEEx->strWideCharLocation.assign(wstr.c_str(), wstr.size());
- ++iEx;
- }
-
- // Reset reserved data to 0
- r.strReservedBlock1 = "";
- r.strReservedBlock2 = "";
-
- // These are constant
- r.ulReaderVersion = 0x3004;
- r.ulWriterVersion = 0x3004;
-
- r.GetBlob(&~lpData, &ulLen);
- Value.bin.lpb = reinterpret_cast<unsigned char *>(lpData.get());
- Value.bin.cb = ulLen;
-
- // Update the recurrence if there is a change
- if (ulLen != lpPropertyArray[E_RECURRENCE_STATE].Value.bin.cb ||
- memcmp(lpPropertyArray[E_RECURRENCE_STATE].Value.bin.lpb, lpData, ulLen) != 0)
- hr = ReplaceProperty(lpMessage, "dispidRecurrenceState", CHANGE_PROP_TYPE(lpPropertyArray[E_RECURRENCE_STATE].ulPropTag, PT_BINARY), "Recoverable recurrence state.", Value);
- break;
- default:
- /* Recurrence state is useless */
- Value.l = 0;
- hr = ReplaceProperty(lpMessage, "dispidRecurrenceState",
- CHANGE_PROP_TYPE(lpPropertyTagArray->aulPropTag[E_RECURRENCE], PT_LONG),
- "Invalid recurrence state, disabling recurrence.",
- Value);
- break;
- }
- if (hr != hrSuccess)
- return hr;
- }
- return hrSuccess;
- }
- HRESULT FsckCalendar::ValidateItem(LPMESSAGE lpMessage,
- const std::string &strClass)
- {
- HRESULT hr;
- bool bChanged = false;
- if (strClass != "IPM.Appointment") {
- std::cout << "Illegal class: \"" << strClass << "\"" << std::endl;
- return E_INVALIDARG;
- }
- hr = ValidateMinimalNamedFields(lpMessage);
- if (hr != hrSuccess)
- return hr;
- hr = ValidateTimestamps(lpMessage);
- if (hr != hrSuccess)
- return hr;
- hr = ValidateRecurrence(lpMessage);
- if (hr != hrSuccess)
- return hr;
- return ValidateRecursiveDuplicateRecipients(lpMessage, bChanged);
- }
|