Exemplo n.º 1
0
 public void LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, CompositeKey compKey, ProgressDialogStatusLogger statusLogger, IDatabaseFormat databaseFormat)
 {
     _db.LoadData(this, ioConnectionInfo, memoryStream, compKey, statusLogger, databaseFormat);
 }
Exemplo n.º 2
0
        protected virtual void PopulateDatabaseFromStream(PwDatabase pwDatabase, Stream s, IOConnectionInfo iocInfo, CompositeKey compositeKey, ProgressDialogStatusLogger status, IDatabaseFormat databaseFormat)
        {
            IFileStorage fileStorage = _app.GetFileStorage(iocInfo);
            var          filename    = fileStorage.GetFilenameWithoutPathAndExt(iocInfo);

            pwDatabase.Open(s, filename, iocInfo, compositeKey, status, databaseFormat);
        }
 public void LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, CompositeKey compKey, ProgressDialogStatusLogger statusLogger, IDatabaseFormat databaseFormat)
 {
     _db.LoadData(this, ioConnectionInfo, memoryStream, compKey, statusLogger, databaseFormat);
 }
Exemplo n.º 4
0
        public Database LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, CompositeKey compositeKey, ProgressDialogStatusLogger statusLogger, IDatabaseFormat databaseFormat, bool makeCurrent)
        {
            var prefs        = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
            var createBackup = prefs.GetBoolean(Application.Context.GetString(Resource.String.CreateBackups_key), true) &&
                               !(new LocalFileStorage(this).IsLocalBackup(ioConnectionInfo));

            MemoryStream backupCopy = new MemoryStream();

            if (createBackup)
            {
                memoryStream.CopyTo(backupCopy);
                backupCopy.Seek(0, SeekOrigin.Begin);
                //reset stream if we need to reuse it later:
                memoryStream.Seek(0, SeekOrigin.Begin);
            }

            foreach (Database openDb in _openDatabases)
            {
                if (openDb.Ioc.IsSameFileAs(ioConnectionInfo))
                {
                    //TODO check this earlier and simply open the database's root group
                    throw new Exception("Database already loaded!");
                }
            }

            _openAttempts.Add(ioConnectionInfo);
            var newDb = new Database(new DrawableFactory(), this);

            newDb.LoadData(this, ioConnectionInfo, memoryStream, compositeKey, statusLogger, databaseFormat);



            if ((_currentDatabase == null) || makeCurrent)
            {
                _currentDatabase = newDb;
            }
            _openDatabases.Add(newDb);



            if (createBackup)
            {
                statusLogger.UpdateMessage(Application.Context.GetString(Resource.String.UpdatingBackup));
                Java.IO.File internalDirectory = IoUtil.GetInternalDirectory(Application.Context);
                string       baseDisplayName   = App.Kp2a.GetFileStorage(ioConnectionInfo).GetDisplayName(ioConnectionInfo);
                string       targetPath        = baseDisplayName;
                var          charsToRemove     = "|\\?*<\":>+[]/'";
                foreach (char c in charsToRemove)
                {
                    targetPath = targetPath.Replace(c.ToString(), string.Empty);
                }
                if (targetPath == "")
                {
                    targetPath = "local_backup";
                }

                var targetIoc = IOConnectionInfo.FromPath(new Java.IO.File(internalDirectory, targetPath).CanonicalPath);

                using (var transaction = new LocalFileStorage(App.Kp2a).OpenWriteTransaction(targetIoc, false))
                {
                    using (var file = transaction.OpenFile())
                    {
                        backupCopy.CopyTo(file);
                        transaction.CommitWrite();
                    }
                }
                Java.Lang.Object baseIocDisplayName = baseDisplayName;

                string keyfile = App.Kp2a.FileDbHelper.GetKeyFileForFile(ioConnectionInfo.Path);
                App.Kp2a.StoreOpenedFileAsRecent(targetIoc, keyfile, false, Application.Context.
                                                 GetString(Resource.String.LocalBackupOf, new Java.Lang.Object[] { baseIocDisplayName }));

                prefs.Edit()
                .PutBoolean(IoUtil.GetIocPrefKey(ioConnectionInfo, "has_local_backup"), true)
                .PutBoolean(IoUtil.GetIocPrefKey(targetIoc, "is_local_backup"), true)
                .Commit();
            }
            else
            {
                prefs.Edit()
                .PutBoolean(IoUtil.GetIocPrefKey(ioConnectionInfo, "has_local_backup"), false)             //there might be an older local backup, but we won't "advertise" this anymore
                .Commit();
            }

            UpdateOngoingNotification();

            return(newDb);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Do not call this method directly. Call App.Kp2a.LoadDatabase instead.
        /// </summary>
        public void LoadData(IKp2aApp app, IOConnectionInfo iocInfo, MemoryStream databaseData, CompositeKey compositeKey, ProgressDialogStatusLogger status, IDatabaseFormat databaseFormat)
        {
            PwDatabase pwDatabase = new PwDatabase();

            IFileStorage fileStorage = _app.GetFileStorage(iocInfo);
            Stream       s           = databaseData ?? fileStorage.OpenFileForRead(iocInfo);
            var          fileVersion = _app.GetFileStorage(iocInfo).GetCurrentFileVersionFast(iocInfo);

            PopulateDatabaseFromStream(pwDatabase, s, iocInfo, compositeKey, status, databaseFormat);
            try
            {
                LastFileVersion = fileVersion;

                status.UpdateSubMessage("");

                Root = pwDatabase.RootGroup;
                PopulateGlobals(Root);


                KpDatabase   = pwDatabase;
                SearchHelper = new SearchDbHelper(app);

                _databaseFormat = databaseFormat;

                CanWrite = databaseFormat.CanWrite && !fileStorage.IsReadOnly(iocInfo);
                Loaded   = true;
            }
            catch (Exception)
            {
                Clear();
                throw;
            }
        }
Exemplo n.º 6
0
        public override void Run()
        {
            try
            {
                try
                {
                    StatusLogger.UpdateMessage(UiStringKey.loading_database);
                    //get the stream data into a single stream variable (databaseStream) regardless whether its preloaded or not:
                    MemoryStream preloadedMemoryStream = _databaseData == null ? null : _databaseData.Result;
                    MemoryStream databaseStream;
                    if (preloadedMemoryStream != null)
                    {
                        databaseStream = preloadedMemoryStream;
                    }
                    else
                    {
                        using (Stream s = _app.GetFileStorage(_ioc).OpenFileForRead(_ioc))
                        {
                            databaseStream = new MemoryStream();
                            s.CopyTo(databaseStream);
                            databaseStream.Seek(0, SeekOrigin.Begin);
                        }
                    }

                    //ok, try to load the database. Let's start with Kdbx format and retry later if that is the wrong guess:
                    _format = new KdbxDatabaseFormat(KdbpFile.GetFormatToUse(_ioc));
                    TryLoad(databaseStream);
                }
                catch (Exception e)
                {
                    this.Exception = e;
                    throw;
                }
            }
            catch (KeyFileException)
            {
                Kp2aLog.Log("KeyFileException");
                Finish(false,                 /*TODO Localize: use Keepass error text KPRes.KeyFileError (including "or invalid format")*/
                       _app.GetResourceString(UiStringKey.keyfile_does_not_exist), Exception);
            }
            catch (AggregateException e)
            {
                string message = e.Message;
                foreach (var innerException in e.InnerExceptions)
                {
                    message = innerException.Message;
                    // Override the message shown with the last (hopefully most recent) inner exception
                    Kp2aLog.LogUnexpectedError(innerException);
                }
                Finish(false, _app.GetResourceString(UiStringKey.ErrorOcurred) + " " + message, Exception);
                return;
            }
            catch (DuplicateUuidsException e)
            {
                Kp2aLog.Log(e.ToString());
                Finish(false, _app.GetResourceString(UiStringKey.DuplicateUuidsError) + " " + e.Message + _app.GetResourceString(UiStringKey.DuplicateUuidsErrorAdditional), Exception);
                return;
            }
            catch (Exception e)
            {
                if (!(e is InvalidCompositeKeyException))
                {
                    Kp2aLog.LogUnexpectedError(e);
                }
                Finish(false, _app.GetResourceString(UiStringKey.ErrorOcurred) + " " + e.Message, Exception);
                return;
            }
        }
Exemplo n.º 7
0
        public override void Run()
        {
            try
            {
                StatusLogger.UpdateMessage(UiStringKey.loading_database);
                //get the stream data into a single stream variable (databaseStream) regardless whether its preloaded or not:
                MemoryStream preloadedMemoryStream = _databaseData == null ? null : _databaseData.Result;
                MemoryStream databaseStream;
                if (preloadedMemoryStream != null)
                    databaseStream = preloadedMemoryStream;
                else
                {
                    using (Stream s = _app.GetFileStorage(_ioc).OpenFileForRead(_ioc))
                    {
                        databaseStream = new MemoryStream();
                        s.CopyTo(databaseStream);
                        databaseStream.Seek(0, SeekOrigin.Begin);
                    }
                }

                //ok, try to load the database. Let's start with Kdbx format and retry later if that is the wrong guess:
                _format = new KdbxDatabaseFormat(KdbpFile.GetFormatToUse(_ioc));
                TryLoad(databaseStream);
            }
            catch (KeyFileException)
            {
                Kp2aLog.Log("KeyFileException");
                Finish(false, /*TODO Localize: use Keepass error text KPRes.KeyFileError (including "or invalid format")*/
                       _app.GetResourceString(UiStringKey.keyfile_does_not_exist));
            }
            catch (AggregateException e)
            {
                string message = e.Message;
                foreach (var innerException in e.InnerExceptions)
                {
                    message = innerException.Message;
                    // Override the message shown with the last (hopefully most recent) inner exception
                    Kp2aLog.Log("Exception: " + innerException);
                }
                Finish(false, _app.GetResourceString(UiStringKey.ErrorOcurred) + " " + message);
                return;
            }
            catch (DuplicateUuidsException e)
            {
                Kp2aLog.Log("Exception: " + e);
                Finish(false, _app.GetResourceString(UiStringKey.DuplicateUuidsError)+" " +e.Message+ _app.GetResourceString(UiStringKey.DuplicateUuidsErrorAdditional));
                return;
            }
            catch (Exception e)
            {
                Kp2aLog.Log("Exception: " + e);
                Finish(false, _app.GetResourceString(UiStringKey.ErrorOcurred) + " " + e.Message);
                return;
            }
        }
Exemplo n.º 8
0
        private void TryLoad(MemoryStream databaseStream)
        {
            //create a copy of the stream so we can try again if we get an exception which indicates we should change parameters
            //This is not optimal in terms of (short-time) memory usage but is hard to avoid because the Keepass library closes streams also in case of errors.
            //Alternatives would involve increased traffic (if file is on remote) and slower loading times, so this seems to be the best choice.
            MemoryStream workingCopy = new MemoryStream();
            databaseStream.CopyTo(workingCopy);
            workingCopy.Seek(0, SeekOrigin.Begin);
            //reset stream if we need to reuse it later:
            databaseStream.Seek(0, SeekOrigin.Begin);
            //now let's go:
            try
            {
                _app.LoadDatabase(_ioc, workingCopy, _compositeKey, StatusLogger, _format);
                SaveFileData(_ioc, _keyfileOrProvider);
                Kp2aLog.Log("LoadDB OK");
                Finish(true, _format.SuccessMessage);
            }
            catch (OldFormatException)
            {
                _format = new KdbDatabaseFormat(_app);
                TryLoad(databaseStream);
            }
            catch (InvalidCompositeKeyException)
            {
                KcpPassword passwordKey = (KcpPassword)_compositeKey.GetUserKey(typeof(KcpPassword));

                if ((passwordKey != null) && (passwordKey.Password.ReadString() == "") && (_compositeKey.UserKeyCount > 1))
                {
                    //if we don't get a password, we don't know whether this means "empty password" or "no password"
                    //retry without password:
                    _compositeKey.RemoveUserKey(passwordKey);
                    //retry:
                    TryLoad(databaseStream);
                }
                else throw;
            }
        }
Exemplo n.º 9
0
 protected virtual void PopulateDatabaseFromStream(PwDatabase pwDatabase, Stream s, IOConnectionInfo iocInfo, CompositeKey compositeKey, ProgressDialogStatusLogger status, IDatabaseFormat databaseFormat)
 {
     IFileStorage fileStorage = _app.GetFileStorage(iocInfo);
     var filename = fileStorage.GetFilenameWithoutPathAndExt(iocInfo);
     pwDatabase.Open(s, filename, iocInfo, compositeKey, status, databaseFormat);
 }
Exemplo n.º 10
0
        /// <summary>
        /// Do not call this method directly. Call App.Kp2a.LoadDatabase instead.
        /// </summary>
        public void LoadData(IKp2aApp app, IOConnectionInfo iocInfo, MemoryStream databaseData, CompositeKey compositeKey, ProgressDialogStatusLogger status, IDatabaseFormat databaseFormat)
        {
            PwDatabase pwDatabase = new PwDatabase();

            IFileStorage fileStorage = _app.GetFileStorage(iocInfo);
            Stream s = databaseData ?? fileStorage.OpenFileForRead(iocInfo);
            var fileVersion = _app.GetFileStorage(iocInfo).GetCurrentFileVersionFast(iocInfo);
            PopulateDatabaseFromStream(pwDatabase, s, iocInfo, compositeKey, status, databaseFormat);
            try
            {
                LastFileVersion = fileVersion;

                status.UpdateSubMessage("");

                Root = pwDatabase.RootGroup;
                PopulateGlobals(Root);

                Loaded = true;
                KpDatabase = pwDatabase;
                SearchHelper = new SearchDbHelper(app);

                _databaseFormat = databaseFormat;

                CanWrite = databaseFormat.CanWrite && !fileStorage.IsReadOnly(iocInfo);
            }
            catch (Exception)
            {
                Clear();
                throw;
            }
        }