Exemplo n.º 1
0
        /// <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);
        }