public static OTPHandler_DB GetOTPHandler(PwDatabase db) { if (db == null) { return(null); } OTPHandler_Base h; if (!Config.UseDBForOTPSeeds(db)) { if (m_EntryOTPDAOHandler.TryGetValue(db.IOConnectionInfo.Path, out h)) { RemoveHandler(db.IOConnectionInfo.Path, true); } return(null); } h = null; if (m_EntryOTPDAOHandler.TryGetValue(db.IOConnectionInfo.Path, out h)) { return(h as OTPHandler_DB); } h = new OTPHandler_DB(); m_EntryOTPDAOHandler[db.IOConnectionInfo.Path] = h; (h as OTPHandler_DB).SetDB(db, false); return(h as OTPHandler_DB); }
private static void OnEntryTouched(object sender, ObjectTouchedEventArgs e) { // !e.Modified = Entry was not modified => nothing to do PwEntry pe = e.Object as PwEntry; if (pe == null) { return; } OTPHandler_DB h = GetOTPHandler(pe) as OTPHandler_DB; if (m_dEntryOTPData.ContainsKey(pe) && (h != null)) { h.UpdateOTPData(pe, true); } m_dEntryOTPData.Remove(pe); }
private static void OTPDAO_FileOpened(object sender, FileOpenedEventArgs e) { List <string> lMsg = new List <string>(); bool bUseDB = Config.UseDBForOTPSeeds(e.Database); bool bPreload = Config.PreloadOTPDB(e.Database); if (bUseDB) { //OTP-DB has to be used //Now check for KeePassOTB.DB entry that needs to be migrated //If CustomData exists => No need to check any further CheckAndMigrate(e.Database, OTP_Migrations.Entry2CustomData); lMsg.Add("Uses OTP database: true"); lMsg.Add("Preload OTP database: " + bPreload.ToString()); OTPHandler_DB h = GetOTPHandler(e.Database); if (h == null) { lMsg.Add("Error getting OTP-DB: Handler: true"); RemoveHandler(e.Database.IOConnectionInfo.Path, true); } h = GetOTPHandler(e.Database); if (h != null) { h.RememberInitialOTPDB(e.Database); h.SetDB(e.Database, bPreload); } else { lMsg.Add("Error restoring OTP-DB: Handler: true"); } } else { CheckAndMigrate(e.Database); bool bRemoved = RemoveHandler(e.Database.IOConnectionInfo.Path, true); lMsg.Add("Uses OTP database: " + bUseDB.ToString()); lMsg.Add("Preload OTP database: " + bPreload.ToString()); lMsg.Add("Removed OTP database from buffer:" + bRemoved.ToString()); } PluginDebug.AddInfo("Database file opened", 0, lMsg.ToArray()); }
/// <summary> /// Perform all kind of migrations between different KeePassOTP versions /// </summary> /// <param name="db"></param> /// <returns>true if something was migrated, false if nothing was done</returns> private static bool CheckAndMigrate(PwDatabase db, OTP_Migrations omFlags) { const string Migration_EntryDB = "KeePassOTP.MigrationStatus"; const string Migration_OTPDB = "KeePassOTPDB.MigrationStatus"; string sMigrationStatus = string.Empty; bool bMigrated = false; //Get DB to work on OTPDAO.OTPHandler_DB h = GetOTPHandler(db); if (h != null) { sMigrationStatus = Migration_OTPDB; } else { sMigrationStatus = Migration_EntryDB; } OTP_Migrations omStatusOld = OTP_Migrations.None; if (!OTP_Migrations.TryParse(db.CustomData.Get(sMigrationStatus), out omStatusOld)) { omStatusOld = OTP_Migrations.None; } OTP_Migrations omStatusNew = omStatusOld; if (MigrationRequired(OTP_Migrations.Entry2CustomData, omFlags, omStatusOld)) { if (!db.UseDBForOTPSeeds() || !db.CustomData.Exists(OTPDAO.OTPHandler_DB.DBNAME)) { PwEntry pe = OTPHandler_DB.GetOTPDBEntry(db); if (pe != null) { bMigrated = true; OTPDAO.MigrateToCustomdata(db, pe); } } omStatusNew |= OTP_Migrations.Entry2CustomData; } if (MigrationRequired(OTP_Migrations.KeePassOTP2OtpAuth, omFlags, omStatusOld)) { int r = CheckOTPDataMigration(db); bMigrated |= r > 0; if (r >= 0) { omStatusNew |= OTP_Migrations.KeePassOTP2OtpAuth; } } if (MigrationRequired(OTP_Migrations.SanitizeSeed, omFlags, omStatusOld)) { int r = SanitizeSeeds(db); bMigrated |= r > 0; if (r >= 0) { omStatusNew |= OTP_Migrations.SanitizeSeed; } } if (MigrationRequired(OTP_Migrations.OTPAuthFormatCorrection, omFlags, omStatusOld)) { int r = OTPAuthFormatCorrection(db); bMigrated |= r > 0; if (r >= 0) { omStatusNew |= OTP_Migrations.OTPAuthFormatCorrection; } } if ((omStatusNew != omStatusOld) || bMigrated) { db.CustomData.Set(sMigrationStatus, omStatusNew.ToString()); db.SettingsChanged = DateTime.UtcNow; db.Modified = true; Program.MainForm.UpdateUI(false, null, false, null, false, null, Program.MainForm.ActiveDatabase == db); } return(bMigrated); }