void sttContactEnum(MCONTACT contactID, const char *szModule, CDbxKyoto *db) { OBJLIST<VarDescr> arSettings(1); SettingUgraderParam param = { db, szModule, contactID, &arSettings }; DBCONTACTENUMSETTINGS dbces = { 0 }; dbces.pfnEnumProc = sttSettingUgrader; dbces.szModule = szModule; dbces.lParam = (LPARAM)¶m; db->EnumContactSettings(NULL, &dbces); for (int i = 0; i < arSettings.getCount(); i++) { VarDescr &p = arSettings[i]; size_t len; BYTE *pResult = db->m_crypto->encodeString(p.szValue, &len); if (pResult != NULL) { DBCONTACTWRITESETTING dbcws = { szModule, p.szVar }; dbcws.value.type = DBVT_ENCRYPTED; dbcws.value.pbVal = pResult; dbcws.value.cpbVal = (WORD)len; db->WriteContactSetting(contactID, &dbcws); mir_free(pResult); } } }
STDMETHODIMP_(BOOL) CDbxMdb::EnumContactSettings(MCONTACT contactID, DBCONTACTENUMSETTINGS* dbces) { if (!dbces->szModule) return -1; int result = 0; mir_cslock lck(m_csDbAccess); DBSettingKey keySearch; keySearch.dwContactID = contactID; keySearch.dwOfsModule = GetModuleNameOfs(dbces->szModule); memset(keySearch.szSettingName, 0, SIZEOF(keySearch.szSettingName)); LIST<char> arSettings(50); { txn_ptr trnlck(m_pMdbEnv, true); cursor_ptr cursor(trnlck, m_dbSettings); MDB_val key = { sizeof(keySearch), &keySearch }, data; if (mdb_cursor_get(cursor, &key, &data, MDB_SET_RANGE) == MDB_SUCCESS) { do { DBSettingKey *pKey = (DBSettingKey*)key.mv_data; if (pKey->dwContactID != contactID || pKey->dwOfsModule != keySearch.dwOfsModule) break; char szSetting[256]; strncpy_s(szSetting, pKey->szSettingName, key.mv_size - sizeof(DWORD) * 2); arSettings.insert(mir_strdup(szSetting)); } while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == MDB_SUCCESS); } } if (arSettings.getCount() == 0) return -1; for (int i = 0; i < arSettings.getCount(); i++) { result = (dbces->pfnEnumProc)(arSettings[i], dbces->lParam); mir_free(arSettings[i]); } return result; }
void DBDataManage(MCONTACT hContact, WORD Mode, WPARAM wParam, LPARAM) { LIST<char> arSettings(10); // get all the settings and stored them in a temporary list DBCONTACTENUMSETTINGS dbces; dbces.lParam = (LPARAM)&arSettings; dbces.pfnEnumProc = GetWeatherDataFromDB; dbces.szModule = WEATHERCONDITION; CallService(MS_DB_CONTACT_ENUMSETTINGS, hContact, (LPARAM)&dbces); // begin deleting settings for (int i = arSettings.getCount() - 1; i >= 0; i--) { char *szSetting = arSettings[i]; DBVARIANT dbv; if (!db_get_ts(hContact, WEATHERCONDITION, szSetting, &dbv)) { switch (Mode) { case WDBM_REMOVE: db_unset(hContact, WEATHERCONDITION, szSetting); break; case WDBM_DETAILDISPLAY: // skip the "WeatherInfo" variable if (!mir_strcmp(szSetting, "WeatherInfo") || !mir_strcmp(szSetting, "Ignore") || szSetting[0] == '#') continue; HWND hList = GetDlgItem((HWND)wParam, IDC_DATALIST); LV_ITEM lvi = { 0 }; lvi.mask = LVIF_TEXT | LVIF_PARAM; lvi.lParam = i; lvi.pszText = TranslateTS(_A2T(szSetting)); lvi.iItem = ListView_InsertItem(hList, &lvi); lvi.pszText = dbv.ptszVal; ListView_SetItemText(hList, lvi.iItem, 1, dbv.ptszVal); break; } db_free(&dbv); } mir_free(szSetting); } }
static void ProcessIniFile(TCHAR* szIniPath, char *szSafeSections, char *szUnsafeSections, int secur, bool secFN) { FILE *fp = _tfopen(szIniPath, _T("rt")); if (fp == NULL) return; bool warnThisSection = false; char szSection[128]; szSection[0] = 0; while (!feof(fp)) { char szLine[2048]; if (fgets(szLine, sizeof(szLine), fp) == NULL) break; LBL_NewLine: size_t lineLength = mir_strlen(szLine); while (lineLength && (BYTE)(szLine[lineLength - 1]) <= ' ') szLine[--lineLength] = '\0'; if (szLine[0] == ';' || szLine[0] <= ' ') continue; if (szLine[0] == '[') { char *szEnd = strchr(szLine + 1, ']'); if (szEnd == NULL) continue; if (szLine[1] == '!') szSection[0] = '\0'; else { mir_strncpy(szSection, szLine + 1, min(sizeof(szSection), (int)(szEnd - szLine))); switch (secur) { case 0: warnThisSection = false; break; case 1: warnThisSection = !IsInSpaceSeparatedList(szSection, szSafeSections); break; case 2: warnThisSection = IsInSpaceSeparatedList(szSection, szUnsafeSections); break; default: warnThisSection = true; break; } if (secFN) warnThisSection = 0; } if (szLine[1] == '?') { DBCONTACTENUMSETTINGS dbces; dbces.pfnEnumProc = SettingsEnumProc; mir_strncpy(szSection, szLine+2, min(sizeof(szSection), (int)(szEnd-szLine-1))); dbces.szModule = szSection; dbces.ofsSettings = 0; CallService(MS_DB_CONTACT_ENUMSETTINGS, 0, (LPARAM)&dbces); while (setting_items) { SettingsList *next = setting_items->next; db_unset(NULL, szSection, setting_items->name); mir_free(setting_items->name); mir_free(setting_items); setting_items = next; } } continue; } if (szSection[0] == '\0') continue; char *szValue = strchr(szLine, '='); if (szValue == NULL) continue; char szName[128]; mir_strncpy(szName, szLine, min(sizeof(szName), (int)(szValue-szLine+1))); szValue++; { warnSettingChangeInfo_t warnInfo; warnInfo.szIniPath = szIniPath; warnInfo.szName = szName; warnInfo.szSafeSections = szSafeSections; warnInfo.szSection = szSection; warnInfo.szUnsafeSections = szUnsafeSections; warnInfo.szValue = szValue; warnInfo.warnNoMore = 0; warnInfo.cancel = 0; if (warnThisSection && IDNO == DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_WARNINICHANGE), NULL, WarnIniChangeDlgProc, (LPARAM)&warnInfo)) continue; if (warnInfo.cancel) break; if (warnInfo.warnNoMore) warnThisSection = 0; } switch (szValue[0]) { case 'b': case 'B': db_set_b(NULL, szSection, szName, (BYTE)strtol(szValue+1, NULL, 0)); break; case 'w': case 'W': db_set_w(NULL, szSection, szName, (WORD)strtol(szValue+1, NULL, 0)); break; case 'd': case 'D': db_set_dw(NULL, szSection, szName, (DWORD)strtoul(szValue+1, NULL, 0)); break; case 'l': case 'L': case '-': if (szValue[1] == '*') { LIST<char> arSettings(1); ESFDParam param = { &arSettings, szName }; DBCONTACTENUMSETTINGS dbep = {}; dbep.pfnEnumProc = EnumSettingsForDeletion; dbep.szModule = szSection; dbep.lParam = (LPARAM)¶m; CallService(MS_DB_CONTACT_ENUMSETTINGS, NULL, LPARAM(&dbep)); while (arSettings.getCount()) { db_unset(NULL, szSection, arSettings[0]); mir_free(arSettings[0]); arSettings.remove(0); } } db_unset(NULL, szSection, szName); break; case 'e': case 'E': ConvertBackslashes(szValue+1, Langpack_GetDefaultCodePage()); case 's': case 'S': db_set_s(NULL, szSection, szName, szValue+1); break; case 'g': case 'G': for (char *pstr = szValue + 1; *pstr; pstr++) { if (*pstr == '\\') { switch (pstr[1]) { case 'n': *pstr = '\n'; break; case 't': *pstr = '\t'; break; case 'r': *pstr = '\r'; break; default: *pstr = pstr[1]; break; } memmove(pstr + 1, pstr + 2, mir_strlen(pstr + 2) + 1); } } case 'u': case 'U': db_set_utf(NULL, szSection, szName, szValue + 1); break; case 'm': case 'M': { CMStringA memo(szValue + 1); memo.Append("\r\n"); while (fgets(szLine, sizeof(szLine), fp) != NULL) { switch (szLine[0]) { case 0: case '\r': case '\n': case ' ': case '\t': break; default: db_set_utf(NULL, szSection, szName, memo); goto LBL_NewLine; } memo.Append(rtrim(szLine + 1)); memo.Append("\r\n"); } db_set_utf(NULL, szSection, szName, memo); } break; case 'n': case 'h': case 'N': case 'H': { int len; char *pszValue, *pszEnd; PBYTE buf = (PBYTE)mir_alloc(mir_strlen(szValue + 1)); for (len = 0, pszValue = szValue + 1;; len++) { buf[len] = (BYTE)strtol(pszValue, &pszEnd, 0x10); if (pszValue == pszEnd) break; pszValue = pszEnd; } db_set_blob(NULL, szSection, szName, buf, len); mir_free(buf); } break; default: TCHAR buf[250]; mir_sntprintf(buf, TranslateT("Invalid setting type for '%s'. The first character of every value must be b, w, d, l, s, e, u, g, h or n."), _A2T(szName)); MessageBox(NULL, buf, TranslateT("Install database settings"), MB_ICONWARNING | MB_OK); break; } } fclose(fp); }
void CDb3Mmap::ToggleSettingsEncryption(MCONTACT contactID) { DWORD ofsContact = GetContactOffset(contactID); if (ofsContact == 0) return; DBContact *contact = (DBContact*)DBRead(ofsContact, NULL); if (contact->ofsFirstSettings == 0) return; // fast cycle through all settings DBContactSettings *setting = (DBContactSettings*)DBRead(contact->ofsFirstSettings, NULL); DWORD offset = contact->ofsFirstSettings; char *szModule = GetModuleNameByOfs(setting->ofsModuleName); if (szModule == NULL) return; while (true) { OBJLIST<VarDescr> arSettings(10); char szSetting[256]; int bytesRemaining, len; DWORD ofsBlobPtr = offset + offsetof(DBContactSettings, blob), ofsNext = setting->ofsNext; PBYTE pBlob = (PBYTE)DBRead(ofsBlobPtr, &bytesRemaining); while (pBlob[0]) { NeedBytes(1); len = pBlob[0]; memcpy(szSetting, pBlob + 1, len); szSetting[len] = 0; NeedBytes(1 + pBlob[0]); MoveAlong(1 + pBlob[0]); NeedBytes(5); switch (pBlob[0]) { case DBVT_ASCIIZ: len = *(PWORD)(pBlob + 1); // we need to convert a string into utf8 and encrypt it if (!m_bEncrypted) { BYTE bSave = pBlob[len + 3]; pBlob[len + 3] = 0; arSettings.insert(new VarDescr(szSetting, mir_utf8encode((LPCSTR)pBlob + 3))); pBlob[len + 3] = bSave; } NeedBytes(3 + len); break; case DBVT_UTF8: len = *(PWORD)(pBlob + 1); // we need to encrypt these strings if (!m_bEncrypted) { BYTE bSave = pBlob[len + 3]; pBlob[len + 3] = 0; arSettings.insert(new VarDescr(szSetting, (LPCSTR)pBlob + 3)); pBlob[len + 3] = bSave; } NeedBytes(3 + len); break; case DBVT_ENCRYPTED: len = *(PWORD)(pBlob + 1); // we need to decrypt these strings if (m_bEncrypted && !IsSettingEncrypted(szModule, szSetting)) arSettings.insert(new VarDescr(szSetting, pBlob + 3, len)); NeedBytes(3 + len); break; case DBVT_BLOB: NeedBytes(3 + *(PWORD)(pBlob + 1)); break; } NeedBytes(3); MoveAlong(1 + GetSettingValueLength(pBlob)); NeedBytes(1); } for (int i = 0; i < arSettings.getCount(); i++) { VarDescr &p = arSettings[i]; if (!m_bEncrypted) { size_t encodedLen; BYTE *pResult = m_crypto->encodeString(p.szValue, &encodedLen); if (pResult != NULL) { DBCONTACTWRITESETTING dbcws = { szModule, p.szVar }; dbcws.value.type = DBVT_ENCRYPTED; dbcws.value.pbVal = pResult; dbcws.value.cpbVal = (WORD)encodedLen; WriteContactSetting(contactID, &dbcws); mir_free(pResult); } } else { size_t realLen; ptrA decoded(m_crypto->decodeString((PBYTE)(char*)p.szValue, p.iLen, &realLen)); if (decoded != NULL) { DBCONTACTWRITESETTING dbcws = { szModule, p.szVar }; dbcws.value.type = DBVT_UNENCRYPTED; dbcws.value.pszVal = decoded; dbcws.value.cchVal = (WORD)realLen; WriteContactSetting(contactID, &dbcws); } } } if (!ofsNext) break; setting = (DBContactSettings*)DBRead(offset = ofsNext, NULL); if ((szModule = GetModuleNameByOfs(setting->ofsModuleName)) == NULL) break; } }