Ejemplo n.º 1
0
        public static bool?Synchronize(PwDatabase pwStorage, IUIOperations uiOps,
                                       IOConnectionInfo iocSyncWith, bool bForceSave, Form fParent)
        {
            if (pwStorage == null)
            {
                throw new ArgumentNullException("pwStorage");
            }
            if (!pwStorage.IsOpen)
            {
                return(null);                              // No assert or throw
            }
            if (iocSyncWith == null)
            {
                throw new ArgumentNullException("iocSyncWith");
            }
            if (!AppPolicy.Try(AppPolicyId.Import))
            {
                return(null);
            }

            List <IOConnectionInfo> vConnections = new List <IOConnectionInfo>();

            vConnections.Add(iocSyncWith);

            return(Import(pwStorage, new KeePassKdb2x(), vConnections.ToArray(),
                          true, uiOps, bForceSave, fParent));
        }
Ejemplo n.º 2
0
		public static bool? Synchronize(PwDatabase pwStorage, IUIOperations uiOps,
			bool bOpenFromUrl, Form fParent)
		{
			if(pwStorage == null) throw new ArgumentNullException("pwStorage");
			if(!pwStorage.IsOpen) return null;
			if(!AppPolicy.Try(AppPolicyId.Import)) return null;

			List<IOConnectionInfo> vConnections = new List<IOConnectionInfo>();
			if(bOpenFromUrl == false)
			{
				OpenFileDialog ofd = UIUtil.CreateOpenFileDialog(KPRes.Synchronize,
					UIUtil.CreateFileTypeFilter(null, null, true), 1, null, true, true);

				if(ofd.ShowDialog() != DialogResult.OK) return null;

				foreach(string strSelFile in ofd.FileNames)
					vConnections.Add(IOConnectionInfo.FromPath(strSelFile));
			}
			else // Open URL
			{
				IOConnectionForm iocf = new IOConnectionForm();
				iocf.InitEx(false, new IOConnectionInfo(), true, true);

				if(iocf.ShowDialog() != DialogResult.OK) return null;

				vConnections.Add(iocf.IOConnectionInfo);
			}

			return Import(pwStorage, new KeePassKdb2x(), vConnections.ToArray(),
				true, uiOps, false, fParent);
		}
Ejemplo n.º 3
0
        public static bool?Synchronize(PwDatabase pwStorage, IUIOperations uiOps,
                                       bool bOpenFromUrl, Form fParent)
        {
            if (pwStorage == null)
            {
                throw new ArgumentNullException("pwStorage");
            }
            if (!pwStorage.IsOpen)
            {
                return(null);
            }
            if (!AppPolicy.Try(AppPolicyId.Import))
            {
                return(null);
            }

            List <IOConnectionInfo> vConnections = new List <IOConnectionInfo>();

            if (bOpenFromUrl == false)
            {
                OpenFileDialogEx ofd = UIUtil.CreateOpenFileDialog(KPRes.Synchronize,
                                                                   UIUtil.CreateFileTypeFilter(AppDefs.FileExtension.FileExt,
                                                                                               KPRes.KdbxFiles, true), 1, null, true,
                                                                   AppDefs.FileDialogContext.Sync);

                if (ofd.ShowDialog() != DialogResult.OK)
                {
                    return(null);
                }

                foreach (string strSelFile in ofd.FileNames)
                {
                    vConnections.Add(IOConnectionInfo.FromPath(strSelFile));
                }
            }
            else             // Open URL
            {
                IOConnectionForm iocf = new IOConnectionForm();
                iocf.InitEx(false, null, true, true);

                if (UIUtil.ShowDialogNotValue(iocf, DialogResult.OK))
                {
                    return(null);
                }

                vConnections.Add(iocf.IOConnectionInfo);
                UIUtil.DestroyForm(iocf);
            }

            return(Import(pwStorage, new KeePassKdb2x(), vConnections.ToArray(),
                          true, uiOps, false, fParent));
        }
Ejemplo n.º 4
0
        public static bool Synchronize(PwDatabase pwStorage, IUIOperations uiOps,
                                       bool bOpenFromUrl)
        {
            if (pwStorage == null)
            {
                throw new ArgumentNullException("pwStorage");
            }
            if (!pwStorage.IsOpen)
            {
                return(false);
            }
            if (!AppPolicy.Try(AppPolicyId.Import))
            {
                return(false);
            }

            List <IOConnectionInfo> vConnections = new List <IOConnectionInfo>();

            if (bOpenFromUrl == false)
            {
                OpenFileDialog ofd = UIUtil.CreateOpenFileDialog(KPRes.Synchronize,
                                                                 UIUtil.CreateFileTypeFilter(null, null, true), 1, null, true, true);

                if (ofd.ShowDialog() != DialogResult.OK)
                {
                    return(true);
                }

                foreach (string strSelFile in ofd.FileNames)
                {
                    vConnections.Add(IOConnectionInfo.FromPath(strSelFile));
                }
            }
            else             // Open URL
            {
                IOConnectionForm iocf = new IOConnectionForm();
                iocf.InitEx(false, new IOConnectionInfo(), false, true);

                if (iocf.ShowDialog() != DialogResult.OK)
                {
                    return(true);
                }

                vConnections.Add(iocf.IOConnectionInfo);
            }

            return(PerformImport(pwStorage, new KeePassKdb2x(), vConnections.ToArray(),
                                 true, uiOps, false));
        }
        public static PwEntry Import(IPluginHost host,
                                     PluginEntryFactory creator)
        {
            Form          fParent = host.MainWindow;
            IUIOperations uiOps   = host.MainWindow;

            IOConnectionInfo[] vCx = new IOConnectionInfo[] { };
            bool?success           = ImportUtil.Import(host.Database, creator,
                                                       vCx, false, uiOps, false, fParent);

            if (success.HasValue && success.Value &&
                host.MainWindow.GetSelectedGroup()
                == host.Database.RootGroup)
            {
                host.MainWindow.UpdateUI(false, null, false, null,
                                         true, host.Database.RootGroup, true);
            }
            return(creator.Entry);
        }
Ejemplo n.º 6
0
        // Start of modification by B.L

        //Shamir Synchronize
        public static bool?ShamirSynchronize(PwDatabase pwStorage, IUIOperations uiOps,
                                             bool bOpenFromUrl, Form fParent, List <string> paths)
        {
            if (pwStorage == null)
            {
                throw new ArgumentNullException("pwStorage");
            }
            if (!pwStorage.IsOpen)
            {
                return(null);
            }
            if (!AppPolicy.Try(AppPolicyId.Import))
            {
                return(null);
            }

            List <IOConnectionInfo> vConnections = new List <IOConnectionInfo>();

            if (bOpenFromUrl == false)
            {
                foreach (string s in paths)
                {
                    vConnections.Add(IOConnectionInfo.FromPath(s));
                }
            }
            else // Open URL
            {
                IOConnectionForm iocf = new IOConnectionForm();
                iocf.InitEx(false, null, true, true);

                if (UIUtil.ShowDialogNotValue(iocf, DialogResult.OK))
                {
                    return(null);
                }

                vConnections.Add(iocf.IOConnectionInfo);
                UIUtil.DestroyForm(iocf);
            }

            return(Import(pwStorage, new KeePassKdb2x(), vConnections.ToArray(),
                          true, uiOps, false, fParent));
        }
Ejemplo n.º 7
0
        public static bool?Synchronize(PwDatabase pwStorage, IUIOperations uiOps,
                                       IOConnectionInfo iocSyncWith, bool bForceSave, Form fParent)
        {
            if (pwStorage == null)
            {
                throw new ArgumentNullException("pwStorage");
            }
            if (!pwStorage.IsOpen)
            {
                return(null);                              // No assert or throw
            }
            if (iocSyncWith == null)
            {
                throw new ArgumentNullException("iocSyncWith");
            }
            if (!AppPolicy.Try(AppPolicyId.Import))
            {
                return(null);
            }

            Program.TriggerSystem.RaiseEvent(EcasEventIDs.SynchronizingDatabaseFile,
                                             EcasProperty.Database, pwStorage);

            List <IOConnectionInfo> vConnections = new List <IOConnectionInfo>();

            vConnections.Add(iocSyncWith);

            bool?ob = Import(pwStorage, new KeePassKdb2x(), vConnections.ToArray(),
                             true, uiOps, bForceSave, fParent);

            // Always raise the post event, such that the event pair can
            // for instance be used to turn off/on other triggers
            Program.TriggerSystem.RaiseEvent(EcasEventIDs.SynchronizedDatabaseFile,
                                             EcasProperty.Database, pwStorage);

            return(ob);
        }
Ejemplo n.º 8
0
        public static bool? Synchronize(PwDatabase pwStorage, IUIOperations uiOps,
            IOConnectionInfo iocSyncWith, bool bForceSave, Form fParent)
        {
            if(pwStorage == null) throw new ArgumentNullException("pwStorage");
            if(!pwStorage.IsOpen) return null; // No assert or throw
            if(iocSyncWith == null) throw new ArgumentNullException("iocSyncWith");
            if(!AppPolicy.Try(AppPolicyId.Import)) return null;

            List<IOConnectionInfo> vConnections = new List<IOConnectionInfo>();
            vConnections.Add(iocSyncWith);

            return Import(pwStorage, new KeePassKdb2x(), vConnections.ToArray(),
                true, uiOps, bForceSave, fParent);
        }
Ejemplo n.º 9
0
        public static bool? Import(PwDatabase pwDatabase, FileFormatProvider fmtImp,
            IOConnectionInfo[] vConnections, bool bSynchronize, IUIOperations uiOps,
            bool bForceSave, Form fParent)
        {
            if(pwDatabase == null) throw new ArgumentNullException("pwDatabase");
            if(!pwDatabase.IsOpen) return null;
            if(fmtImp == null) throw new ArgumentNullException("fmtImp");
            if(vConnections == null) throw new ArgumentNullException("vConnections");

            if(!AppPolicy.Try(AppPolicyId.Import)) return false;
            if(!fmtImp.TryBeginImport()) return false;

            bool bUseTempDb = (fmtImp.SupportsUuids || fmtImp.RequiresKey);
            bool bAllSuccess = true;

            // if(bSynchronize) { Debug.Assert(vFiles.Length == 1); }

            IStatusLogger dlgStatus;
            if(Program.Config.UI.ShowImportStatusDialog)
                dlgStatus = new OnDemandStatusDialog(false, fParent);
            else dlgStatus = new UIBlockerStatusLogger(fParent);

            dlgStatus.StartLogging(PwDefs.ShortProductName + " - " + (bSynchronize ?
                KPRes.Synchronizing : KPRes.ImportingStatusMsg), false);
            dlgStatus.SetText(bSynchronize ? KPRes.Synchronizing :
                KPRes.ImportingStatusMsg, LogStatusType.Info);

            if(vConnections.Length == 0)
            {
                try { fmtImp.Import(pwDatabase, null, dlgStatus); }
                catch(Exception exSingular)
                {
                    if((exSingular.Message != null) && (exSingular.Message.Length > 0))
                    {
                        // slf.SetText(exSingular.Message, LogStatusType.Warning);
                        MessageService.ShowWarning(exSingular);
                    }
                }

                dlgStatus.EndLogging();
                return true;
            }

            foreach(IOConnectionInfo iocIn in vConnections)
            {
                Stream s = null;

                try { s = IOConnection.OpenRead(iocIn); }
                catch(Exception exFile)
                {
                    MessageService.ShowWarning(iocIn.GetDisplayName(), exFile);
                    bAllSuccess = false;
                    continue;
                }
                if(s == null) { Debug.Assert(false); bAllSuccess = false; continue; }

                PwDatabase pwImp;
                if(bUseTempDb)
                {
                    pwImp = new PwDatabase();
                    pwImp.New(new IOConnectionInfo(), pwDatabase.MasterKey);
                    pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
                }
                else pwImp = pwDatabase;

                if(fmtImp.RequiresKey && !bSynchronize)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    kpf.InitEx(iocIn, false, true);

                    if(UIUtil.ShowDialogNotValue(kpf, DialogResult.OK)) { s.Close(); continue; }

                    pwImp.MasterKey = kpf.CompositeKey;
                    UIUtil.DestroyForm(kpf);
                }
                else if(bSynchronize) pwImp.MasterKey = pwDatabase.MasterKey;

                dlgStatus.SetText((bSynchronize ? KPRes.Synchronizing :
                    KPRes.ImportingStatusMsg) + " (" + iocIn.GetDisplayName() +
                    ")", LogStatusType.Info);

                try { fmtImp.Import(pwImp, s, dlgStatus); }
                catch(Exception excpFmt)
                {
                    string strMsgEx = excpFmt.Message;
                    if(bSynchronize && (excpFmt is InvalidCompositeKeyException))
                        strMsgEx = KLRes.InvalidCompositeKey + MessageService.NewParagraph +
                            KPRes.SynchronizingHint;

                    MessageService.ShowWarning(strMsgEx);

                    s.Close();
                    bAllSuccess = false;
                    continue;
                }

                s.Close();

                if(bUseTempDb)
                {
                    PwMergeMethod mm;
                    if(!fmtImp.SupportsUuids) mm = PwMergeMethod.CreateNewUuids;
                    else if(bSynchronize) mm = PwMergeMethod.Synchronize;
                    else
                    {
                        ImportMethodForm imf = new ImportMethodForm();
                        if(UIUtil.ShowDialogNotValue(imf, DialogResult.OK)) continue;
                        mm = imf.MergeMethod;
                        UIUtil.DestroyForm(imf);
                    }

                    // slf.SetText(KPRes.MergingData, LogStatusType.Info);

                    try { pwDatabase.MergeIn(pwImp, mm, dlgStatus); }
                    catch(Exception exMerge)
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(),
                            KPRes.ImportFailed, exMerge);

                        bAllSuccess = false;
                        continue;
                    }
                }
            }

            dlgStatus.EndLogging();

            if(bSynchronize && bAllSuccess)
            {
                Debug.Assert(uiOps != null);
                if(uiOps == null) throw new ArgumentNullException("uiOps");

                if(uiOps.UIFileSave(bForceSave))
                {
                    foreach(IOConnectionInfo ioc in vConnections)
                    {
                        try
                        {
                            if(ioc.Path != pwDatabase.IOConnectionInfo.Path)
                            {
                                if(pwDatabase.IOConnectionInfo.IsLocalFile() &&
                                    ioc.IsLocalFile())
                                {
                                    File.Copy(pwDatabase.IOConnectionInfo.Path,
                                        ioc.Path, true);
                                }
                                else pwDatabase.SaveAs(ioc, false, null);
                            }
                            else { } // No assert (sync on save)

                            Program.MainForm.FileMruList.AddItem(ioc.GetDisplayName(),
                                ioc.CloneDeep(), true);
                        }
                        catch(Exception exSync)
                        {
                            MessageService.ShowWarning(KPRes.SyncFailed,
                                pwDatabase.IOConnectionInfo.GetDisplayName() +
                                MessageService.NewLine + ioc.GetDisplayName(), exSync);

                            bAllSuccess = false;
                            continue;
                        }
                    }
                }
                else
                {
                    MessageService.ShowWarning(KPRes.SyncFailed,
                        pwDatabase.IOConnectionInfo.GetDisplayName());

                    bAllSuccess = false;
                }
            }

            return bAllSuccess;
        }
Ejemplo n.º 10
0
        public static bool?Import(PwDatabase pwDatabase, FileFormatProvider fmtImp,
                                  IOConnectionInfo[] vConnections, bool bSynchronize, IUIOperations uiOps,
                                  bool bForceSave, Form fParent)
        {
            if (pwDatabase == null)
            {
                throw new ArgumentNullException("pwDatabase");
            }
            if (!pwDatabase.IsOpen)
            {
                return(null);
            }
            if (fmtImp == null)
            {
                throw new ArgumentNullException("fmtImp");
            }
            if (vConnections == null)
            {
                throw new ArgumentNullException("vConnections");
            }

            if (!AppPolicy.Try(AppPolicyId.Import))
            {
                return(false);
            }
            if (!fmtImp.TryBeginImport())
            {
                return(false);
            }

            bool     bUseTempDb  = (fmtImp.SupportsUuids || fmtImp.RequiresKey);
            bool     bAllSuccess = true;
            MainForm mf          = Program.MainForm;    // Null for KPScript

            // if(bSynchronize) { Debug.Assert(vFiles.Length == 1); }

            IStatusLogger dlgStatus;

            if (Program.Config.UI.ShowImportStatusDialog ||
                ((mf != null) && !mf.HasFormLoaded))
            {
                dlgStatus = new OnDemandStatusDialog(false, fParent);
            }
            else
            {
                dlgStatus = new UIBlockerStatusLogger(fParent);
            }

            dlgStatus.StartLogging(PwDefs.ShortProductName + " - " + (bSynchronize ?
                                                                      KPRes.Synchronizing : KPRes.ImportingStatusMsg), false);
            dlgStatus.SetText(bSynchronize ? KPRes.Synchronizing :
                              KPRes.ImportingStatusMsg, LogStatusType.Info);

            if (vConnections.Length == 0)
            {
                try { fmtImp.Import(pwDatabase, null, dlgStatus); }
                catch (Exception exSingular)
                {
                    if (!string.IsNullOrEmpty(exSingular.Message))
                    {
                        // slf.SetText(exSingular.Message, LogStatusType.Warning);
                        MessageService.ShowWarning(exSingular);
                    }
                }

                dlgStatus.EndLogging();
                return(true);
            }

            foreach (IOConnectionInfo iocIn in vConnections)
            {
                Stream s = null;

                try { s = IOConnection.OpenRead(iocIn); }
                catch (Exception exFile)
                {
                    MessageService.ShowWarning(iocIn.GetDisplayName(), exFile);
                    bAllSuccess = false;
                    continue;
                }
                if (s == null)
                {
                    Debug.Assert(false); bAllSuccess = false; continue;
                }

                PwDatabase pwImp;
                if (bUseTempDb)
                {
                    pwImp = new PwDatabase();
                    pwImp.New(new IOConnectionInfo(), pwDatabase.MasterKey);
                    pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
                }
                else
                {
                    pwImp = pwDatabase;
                }

                if (fmtImp.RequiresKey && !bSynchronize)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    kpf.InitEx(iocIn, false, true);

                    if (UIUtil.ShowDialogNotValue(kpf, DialogResult.OK))
                    {
                        s.Close(); continue;
                    }

                    pwImp.MasterKey = kpf.CompositeKey;
                    UIUtil.DestroyForm(kpf);
                }
                else if (bSynchronize)
                {
                    pwImp.MasterKey = pwDatabase.MasterKey;
                }

                dlgStatus.SetText((bSynchronize ? KPRes.Synchronizing :
                                   KPRes.ImportingStatusMsg) + " (" + iocIn.GetDisplayName() +
                                  ")", LogStatusType.Info);

                try { fmtImp.Import(pwImp, s, dlgStatus); }
                catch (Exception excpFmt)
                {
                    string strMsgEx = excpFmt.Message;
                    if (bSynchronize && (excpFmt is InvalidCompositeKeyException))
                    {
                        strMsgEx = KLRes.InvalidCompositeKey + MessageService.NewParagraph +
                                   KPRes.SynchronizingHint;
                    }

                    MessageService.ShowWarning(iocIn.GetDisplayName(),
                                               KPRes.FileImportFailed, strMsgEx);

                    bAllSuccess = false;
                    continue;
                }
                finally { s.Close(); }

                if (bUseTempDb)
                {
                    PwMergeMethod mm;
                    if (!fmtImp.SupportsUuids)
                    {
                        mm = PwMergeMethod.CreateNewUuids;
                    }
                    else if (bSynchronize)
                    {
                        mm = PwMergeMethod.Synchronize;
                    }
                    else
                    {
                        ImportMethodForm imf = new ImportMethodForm();
                        if (UIUtil.ShowDialogNotValue(imf, DialogResult.OK))
                        {
                            continue;
                        }
                        mm = imf.MergeMethod;
                        UIUtil.DestroyForm(imf);
                    }

                    try { pwDatabase.MergeIn(pwImp, mm, dlgStatus); }
                    catch (Exception exMerge)
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(),
                                                   KPRes.ImportFailed, exMerge);

                        bAllSuccess = false;
                        continue;
                    }
                }
            }

            if (bSynchronize && bAllSuccess)
            {
                Debug.Assert(uiOps != null);
                if (uiOps == null)
                {
                    throw new ArgumentNullException("uiOps");
                }

                dlgStatus.SetText(KPRes.Synchronizing + " (" +
                                  KPRes.SavingDatabase + ")", LogStatusType.Info);

                if (mf != null)
                {
                    try { mf.DocumentManager.ActiveDatabase = pwDatabase; }
                    catch (Exception) { Debug.Assert(false); }
                }

                if (uiOps.UIFileSave(bForceSave))
                {
                    foreach (IOConnectionInfo ioc in vConnections)
                    {
                        try
                        {
                            // dlgStatus.SetText(KPRes.Synchronizing + " (" +
                            //	KPRes.SavingDatabase + " " + ioc.GetDisplayName() +
                            //	")", LogStatusType.Info);

                            string strSource = pwDatabase.IOConnectionInfo.Path;
                            if (!string.Equals(ioc.Path, strSource, StrUtil.CaseIgnoreCmp))
                            {
                                bool bSaveAs = true;

                                if (pwDatabase.IOConnectionInfo.IsLocalFile() &&
                                    ioc.IsLocalFile())
                                {
                                    // Do not try to copy an encrypted file;
                                    // https://sourceforge.net/p/keepass/discussion/329220/thread/9c9eb989/
                                    // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363851.aspx
                                    if ((long)(File.GetAttributes(strSource) &
                                               FileAttributes.Encrypted) == 0)
                                    {
                                        File.Copy(strSource, ioc.Path, true);
                                        bSaveAs = false;
                                    }
                                }

                                if (bSaveAs)
                                {
                                    pwDatabase.SaveAs(ioc, false, null);
                                }
                            }
                            // else { } // No assert (sync on save)

                            if (mf != null)
                            {
                                mf.FileMruList.AddItem(ioc.GetDisplayName(),
                                                       ioc.CloneDeep());
                            }
                        }
                        catch (Exception exSync)
                        {
                            MessageService.ShowWarning(KPRes.SyncFailed,
                                                       pwDatabase.IOConnectionInfo.GetDisplayName() +
                                                       MessageService.NewLine + ioc.GetDisplayName(), exSync);

                            bAllSuccess = false;
                            continue;
                        }
                    }
                }
                else
                {
                    MessageService.ShowWarning(KPRes.SyncFailed,
                                               pwDatabase.IOConnectionInfo.GetDisplayName());

                    bAllSuccess = false;
                }
            }

            dlgStatus.EndLogging();
            return(bAllSuccess);
        }
Ejemplo n.º 11
0
        public static bool?Import(PwDatabase pwDatabase, FileFormatProvider fmtImp,
                                  IOConnectionInfo[] vConnections, bool bSynchronize, IUIOperations uiOps,
                                  bool bForceSave, Form fParent)
        {
            if (pwDatabase == null)
            {
                throw new ArgumentNullException("pwDatabase");
            }
            if (!pwDatabase.IsOpen)
            {
                return(null);
            }
            if (fmtImp == null)
            {
                throw new ArgumentNullException("fmtImp");
            }
            if (vConnections == null)
            {
                throw new ArgumentNullException("vConnections");
            }

            if (!AppPolicy.Try(AppPolicyId.Import))
            {
                return(false);
            }
            if (!fmtImp.TryBeginImport())
            {
                return(false);
            }

            bool bUseTempDb  = (fmtImp.SupportsUuids || fmtImp.RequiresKey);
            bool bAllSuccess = true;

            // if(bSynchronize) { Debug.Assert(vFiles.Length == 1); }

            IStatusLogger dlgStatus;

            if (Program.Config.UI.ShowImportStatusDialog)
            {
                dlgStatus = new OnDemandStatusDialog(false, fParent);
            }
            else
            {
                dlgStatus = new UIBlockerStatusLogger(fParent);
            }

            dlgStatus.StartLogging(PwDefs.ShortProductName + " - " + (bSynchronize ?
                                                                      KPRes.Synchronizing : KPRes.ImportingStatusMsg), false);
            dlgStatus.SetText(bSynchronize ? KPRes.Synchronizing :
                              KPRes.ImportingStatusMsg, LogStatusType.Info);

            if (vConnections.Length == 0)
            {
                try { fmtImp.Import(pwDatabase, null, dlgStatus); }
                catch (Exception exSingular)
                {
                    if ((exSingular.Message != null) && (exSingular.Message.Length > 0))
                    {
                        // slf.SetText(exSingular.Message, LogStatusType.Warning);
                        MessageService.ShowWarning(exSingular);
                    }
                }

                dlgStatus.EndLogging();
                return(true);
            }

            foreach (IOConnectionInfo iocIn in vConnections)
            {
                Stream s = null;

                try { s = IOConnection.OpenRead(iocIn); }
                catch (Exception exFile)
                {
                    // Transacted-file operations can leave behind intact *.kdbx.tmp files when
                    // the file rename doesn't get completed (can happen easily with slow/unreliable
                    // remote collections. We check if that's the case here and fix the situation if
                    // an kdbx file does *not* exist (to avoid possibly overwriting good data with bad
                    // data (i.e. an interrupted kdbx.tmp write).

                    // Make a copy of the IOC like FileTransactionEx.cs:Initialize does
                    IOConnectionInfo iocTemp = iocIn.CloneDeep();
                    iocTemp.Path += FileTransactionEx.StrTempSuffix;

                    if (IOConnection.FileExists(iocTemp) && !IOConnection.FileExists(iocIn))
                    {
                        // Try and rename iocTemp to ioc.Path, then retry file opening.
                        IOConnection.RenameFile(iocTemp, iocIn);
                        try {
                            s = IOConnection.OpenRead(iocIn);
                        } catch (Exception nexFile) {
                            MessageService.ShowWarning(iocIn.GetDisplayName(), nexFile);
                            bAllSuccess = false;
                            continue;
                        }
                    }
                    else
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(), exFile);
                        bAllSuccess = false;
                        continue;
                    }
                }
                if (s == null)
                {
                    Debug.Assert(false); bAllSuccess = false; continue;
                }

                PwDatabase pwImp;
                if (bUseTempDb)
                {
                    pwImp = new PwDatabase();
                    pwImp.New(new IOConnectionInfo(), pwDatabase.MasterKey);
                    pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
                }
                else
                {
                    pwImp = pwDatabase;
                }

                if (fmtImp.RequiresKey && !bSynchronize)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    kpf.InitEx(iocIn, false, true);

                    if (UIUtil.ShowDialogNotValue(kpf, DialogResult.OK))
                    {
                        s.Close(); continue;
                    }

                    pwImp.MasterKey = kpf.CompositeKey;
                    UIUtil.DestroyForm(kpf);
                }
                else if (bSynchronize)
                {
                    pwImp.MasterKey = pwDatabase.MasterKey;
                }

                dlgStatus.SetText((bSynchronize ? KPRes.Synchronizing :
                                   KPRes.ImportingStatusMsg) + " (" + iocIn.GetDisplayName() +
                                  ")", LogStatusType.Info);

                try { fmtImp.Import(pwImp, s, dlgStatus); }
                catch (Exception excpFmt)
                {
                    string strMsgEx = excpFmt.Message;
                    if (bSynchronize && (excpFmt is InvalidCompositeKeyException))
                    {
                        strMsgEx = KLRes.InvalidCompositeKey + MessageService.NewParagraph +
                                   KPRes.SynchronizingHint;
                    }

                    MessageService.ShowWarning(strMsgEx);

                    s.Close();
                    bAllSuccess = false;
                    continue;
                }

                s.Close();

                if (bUseTempDb)
                {
                    PwMergeMethod mm;
                    if (!fmtImp.SupportsUuids)
                    {
                        mm = PwMergeMethod.CreateNewUuids;
                    }
                    else if (bSynchronize)
                    {
                        mm = PwMergeMethod.Synchronize;
                    }
                    else
                    {
                        ImportMethodForm imf = new ImportMethodForm();
                        if (UIUtil.ShowDialogNotValue(imf, DialogResult.OK))
                        {
                            continue;
                        }
                        mm = imf.MergeMethod;
                        UIUtil.DestroyForm(imf);
                    }

                    try { pwDatabase.MergeIn(pwImp, mm, dlgStatus); }
                    catch (Exception exMerge)
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(),
                                                   KPRes.ImportFailed, exMerge);

                        bAllSuccess = false;
                        continue;
                    }
                }
            }

            if (bSynchronize && bAllSuccess)
            {
                Debug.Assert(uiOps != null);
                if (uiOps == null)
                {
                    throw new ArgumentNullException("uiOps");
                }

                dlgStatus.SetText(KPRes.Synchronizing + " (" +
                                  KPRes.SavingDatabase + ")", LogStatusType.Info);

                MainForm mf = Program.MainForm;                 // Null for KPScript
                if (mf != null)
                {
                    try { mf.DocumentManager.ActiveDatabase = pwDatabase; }
                    catch (Exception) { Debug.Assert(false); }
                }

                if (uiOps.UIFileSave(bForceSave))
                {
                    foreach (IOConnectionInfo ioc in vConnections)
                    {
                        try
                        {
                            // dlgStatus.SetText(KPRes.Synchronizing + " (" +
                            //	KPRes.SavingDatabase + " " + ioc.GetDisplayName() +
                            //	")", LogStatusType.Info);

                            string strSource = pwDatabase.IOConnectionInfo.Path;
                            if (ioc.Path != strSource)
                            {
                                bool bSaveAs = true;

                                if (pwDatabase.IOConnectionInfo.IsLocalFile() &&
                                    ioc.IsLocalFile())
                                {
                                    // Do not try to copy an encrypted file;
                                    // https://sourceforge.net/p/keepass/discussion/329220/thread/9c9eb989/
                                    // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363851.aspx
                                    if ((long)(File.GetAttributes(strSource) &
                                               FileAttributes.Encrypted) == 0)
                                    {
                                        File.Copy(strSource, ioc.Path, true);
                                        bSaveAs = false;
                                    }
                                }

                                if (bSaveAs)
                                {
                                    pwDatabase.SaveAs(ioc, false, null);
                                }
                            }
                            // else { } // No assert (sync on save)

                            if (mf != null)
                            {
                                mf.FileMruList.AddItem(ioc.GetDisplayName(),
                                                       ioc.CloneDeep());
                            }
                        }
                        catch (Exception exSync)
                        {
                            MessageService.ShowWarning(KPRes.SyncFailed,
                                                       pwDatabase.IOConnectionInfo.GetDisplayName() +
                                                       MessageService.NewLine + ioc.GetDisplayName(), exSync);

                            bAllSuccess = false;
                            continue;
                        }
                    }
                }
                else
                {
                    MessageService.ShowWarning(KPRes.SyncFailed,
                                               pwDatabase.IOConnectionInfo.GetDisplayName());

                    bAllSuccess = false;
                }
            }

            dlgStatus.EndLogging();
            return(bAllSuccess);
        }
Ejemplo n.º 12
0
        public static bool?Import(PwDatabase pwDatabase, FileFormatProvider fmtImp,
                                  IOConnectionInfo[] vConnections, bool bSynchronize, IUIOperations uiOps,
                                  bool bForceSave, Form fParent)
        {
            if (pwDatabase == null)
            {
                throw new ArgumentNullException("pwDatabase");
            }
            if (!pwDatabase.IsOpen)
            {
                return(null);
            }
            if (fmtImp == null)
            {
                throw new ArgumentNullException("fmtImp");
            }
            if (vConnections == null)
            {
                throw new ArgumentNullException("vConnections");
            }

            if (!AppPolicy.Try(AppPolicyId.Import))
            {
                return(false);
            }
            if (!fmtImp.TryBeginImport())
            {
                return(false);
            }

            bool bUseTempDb  = (fmtImp.SupportsUuids || fmtImp.RequiresKey);
            bool bAllSuccess = true;

            // if(bSynchronize) { Debug.Assert(vFiles.Length == 1); }

            IStatusLogger dlgStatus;

            if (Program.Config.UI.ShowImportStatusDialog)
            {
                dlgStatus = new OnDemandStatusDialog(false, fParent);
            }
            else
            {
                dlgStatus = new UIBlockerStatusLogger(fParent);
            }

            dlgStatus.StartLogging(PwDefs.ShortProductName + " - " + (bSynchronize ?
                                                                      KPRes.Synchronizing : KPRes.ImportingStatusMsg), false);
            dlgStatus.SetText(bSynchronize ? KPRes.Synchronizing :
                              KPRes.ImportingStatusMsg, LogStatusType.Info);

            if (vConnections.Length == 0)
            {
                try { fmtImp.Import(pwDatabase, null, dlgStatus); }
                catch (Exception exSingular)
                {
                    if ((exSingular.Message != null) && (exSingular.Message.Length > 0))
                    {
                        // slf.SetText(exSingular.Message, LogStatusType.Warning);
                        MessageService.ShowWarning(exSingular);
                    }
                }

                dlgStatus.EndLogging();
                return(true);
            }

            foreach (IOConnectionInfo iocIn in vConnections)
            {
                Stream s = null;

                try { s = IOConnection.OpenRead(iocIn); }
                catch (Exception exFile)
                {
                    MessageService.ShowWarning(iocIn.GetDisplayName(), exFile);
                    bAllSuccess = false;
                    continue;
                }
                if (s == null)
                {
                    Debug.Assert(false); bAllSuccess = false; continue;
                }

                PwDatabase pwImp;
                if (bUseTempDb)
                {
                    pwImp = new PwDatabase();
                    pwImp.New(new IOConnectionInfo(), pwDatabase.MasterKey);
                    pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
                }
                else
                {
                    pwImp = pwDatabase;
                }

                if (fmtImp.RequiresKey && !bSynchronize)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    kpf.InitEx(iocIn, false, true);

                    if (kpf.ShowDialog() != DialogResult.OK)
                    {
                        s.Close(); continue;
                    }

                    pwImp.MasterKey = kpf.CompositeKey;
                }
                else if (bSynchronize)
                {
                    pwImp.MasterKey = pwDatabase.MasterKey;
                }

                dlgStatus.SetText((bSynchronize ? KPRes.Synchronizing :
                                   KPRes.ImportingStatusMsg) + " (" + iocIn.GetDisplayName() +
                                  ")", LogStatusType.Info);

                try { fmtImp.Import(pwImp, s, dlgStatus); }
                catch (Exception excpFmt)
                {
                    string strMsgEx = excpFmt.Message;
                    if (bSynchronize && (excpFmt is InvalidCompositeKeyException))
                    {
                        strMsgEx = KLRes.InvalidCompositeKey + MessageService.NewParagraph +
                                   KPRes.SynchronizingHint;
                    }

                    MessageService.ShowWarning(strMsgEx);

                    s.Close();
                    bAllSuccess = false;
                    continue;
                }

                s.Close();

                if (bUseTempDb)
                {
                    PwMergeMethod mm;
                    if (!fmtImp.SupportsUuids)
                    {
                        mm = PwMergeMethod.CreateNewUuids;
                    }
                    else if (bSynchronize)
                    {
                        mm = PwMergeMethod.Synchronize;
                    }
                    else
                    {
                        ImportMethodForm imf = new ImportMethodForm();
                        if (imf.ShowDialog() != DialogResult.OK)
                        {
                            continue;
                        }
                        mm = imf.MergeMethod;
                    }

                    // slf.SetText(KPRes.MergingData, LogStatusType.Info);

                    try { pwDatabase.MergeIn(pwImp, mm, dlgStatus); }
                    catch (Exception exMerge)
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(),
                                                   KPRes.ImportFailed, exMerge);

                        bAllSuccess = false;
                        continue;
                    }
                }
            }

            dlgStatus.EndLogging();

            if (bSynchronize && bAllSuccess)
            {
                Debug.Assert(uiOps != null);
                if (uiOps == null)
                {
                    throw new ArgumentNullException("uiOps");
                }

                if (uiOps.UIFileSave(bForceSave))
                {
                    foreach (IOConnectionInfo ioc in vConnections)
                    {
                        try
                        {
                            if (ioc.Path != pwDatabase.IOConnectionInfo.Path)
                            {
                                if (pwDatabase.IOConnectionInfo.IsLocalFile() &&
                                    ioc.IsLocalFile())
                                {
                                    File.Copy(pwDatabase.IOConnectionInfo.Path,
                                              ioc.Path, true);
                                }
                                else
                                {
                                    pwDatabase.SaveAs(ioc, false, null);
                                }
                            }
                            else
                            {
                            }                                    // No assert (sync on save)

                            Program.MainForm.FileMruList.AddItem(ioc.GetDisplayName(),
                                                                 ioc.CloneDeep(), true);
                        }
                        catch (Exception exSync)
                        {
                            MessageService.ShowWarning(KPRes.SyncFailed,
                                                       pwDatabase.IOConnectionInfo.GetDisplayName() +
                                                       MessageService.NewLine + ioc.GetDisplayName(), exSync);

                            bAllSuccess = false;
                            continue;
                        }
                    }
                }
                else
                {
                    MessageService.ShowWarning(KPRes.SyncFailed,
                                               pwDatabase.IOConnectionInfo.GetDisplayName());

                    bAllSuccess = false;
                }
            }

            return(bAllSuccess);
        }
Ejemplo n.º 13
0
        private static bool PerformImport(PwDatabase pwDatabase, FileFormatProvider fmtImp,
            IOConnectionInfo[] vConnections, bool bSynchronize, IUIOperations uiOps,
            bool bForceSave)
        {
            if(fmtImp.TryBeginImport() == false) return false;

            bool bUseTempDb = (fmtImp.SupportsUuids || fmtImp.RequiresKey);
            bool bAllSuccess = true;

            // if(bSynchronize) { Debug.Assert(vFiles.Length == 1); }

            StatusLoggerForm slf = new StatusLoggerForm();
            slf.InitEx(false);
            slf.Show();

            if(bSynchronize) slf.StartLogging(KPRes.Synchronize, false);
            else slf.StartLogging(KPRes.ImportingStatusMsg, false);

            if(vConnections.Length == 0)
            {
                try { fmtImp.Import(pwDatabase, null, slf); }
                catch(Exception exSingular)
                {
                    if((exSingular.Message != null) && (exSingular.Message.Length > 0))
                    {
                        slf.SetText(exSingular.Message, LogStatusType.Warning);
                        MessageService.ShowWarning(exSingular);
                    }
                }

                slf.EndLogging(); slf.Close();
                return true;
            }

            foreach(IOConnectionInfo iocIn in vConnections)
            {
                Stream s = null;

                try { s = IOConnection.OpenRead(iocIn); }
                catch(Exception exFile)
                {
                    MessageService.ShowWarning(iocIn.GetDisplayName(), exFile);
                    bAllSuccess = false;
                    continue;
                }

                if(s == null) { Debug.Assert(false); bAllSuccess = false; continue; }

                PwDatabase pwImp;
                if(bUseTempDb)
                {
                    pwImp = new PwDatabase();
                    pwImp.New(new IOConnectionInfo(), pwDatabase.MasterKey);
                    pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
                }
                else pwImp = pwDatabase;

                if(fmtImp.RequiresKey && !bSynchronize)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    kpf.InitEx(iocIn.GetDisplayName(), false);

                    if(kpf.ShowDialog() != DialogResult.OK) { s.Close(); continue; }

                    pwImp.MasterKey = kpf.CompositeKey;
                }
                else if(bSynchronize) pwImp.MasterKey = pwDatabase.MasterKey;

                slf.SetText((bSynchronize ? KPRes.Synchronizing : KPRes.ImportingStatusMsg) +
                    " " + iocIn.GetDisplayName(), LogStatusType.Info);

                try { fmtImp.Import(pwImp, s, slf); }
                catch(Exception excpFmt)
                {
                    string strMsgEx = excpFmt.Message;
                    if(bSynchronize)
                    {
                        strMsgEx += MessageService.NewParagraph +
                            KPRes.SynchronizingHint;
                    }

                    MessageService.ShowWarning(strMsgEx);

                    s.Close();
                    bAllSuccess = false;
                    continue;
                }

                s.Close();

                if(bUseTempDb)
                {
                    PwMergeMethod mm;
                    if(!fmtImp.SupportsUuids) mm = PwMergeMethod.CreateNewUuids;
                    else if(bSynchronize) mm = PwMergeMethod.Synchronize;
                    else
                    {
                        ImportMethodForm imf = new ImportMethodForm();
                        if(imf.ShowDialog() != DialogResult.OK)
                            continue;
                        mm = imf.MergeMethod;
                    }

                    slf.SetText(KPRes.MergingData, LogStatusType.Info);

                    try { pwDatabase.MergeIn(pwImp, mm); }
                    catch(Exception exMerge)
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(),
                            KPRes.ImportFailed, exMerge);

                        bAllSuccess = false;
                        continue;
                    }
                }
            }

            slf.EndLogging(); slf.Close();

            if(bSynchronize && bAllSuccess)
            {
                Debug.Assert(uiOps != null);
                if(uiOps == null) throw new ArgumentNullException("uiOps");

                if(uiOps.UIFileSave(bForceSave))
                {
                    foreach(IOConnectionInfo ioc in vConnections)
                    {
                        try
                        {
                            if(pwDatabase.IOConnectionInfo.IsLocalFile())
                            {
                                if((pwDatabase.IOConnectionInfo.Path != ioc.Path) &&
                                    ioc.IsLocalFile())
                                {
                                    File.Copy(pwDatabase.IOConnectionInfo.Path,
                                        ioc.Path, true);
                                }
                            }
                            else pwDatabase.SaveAs(ioc, false, null);
                        }
                        catch(Exception exSync)
                        {
                            MessageService.ShowWarning(KPRes.SyncFailed,
                                pwDatabase.IOConnectionInfo.GetDisplayName() +
                                MessageService.NewLine + ioc.GetDisplayName(), exSync);

                            bAllSuccess = false;
                            continue;
                        }
                    }
                }
                else
                {
                    MessageService.ShowWarning(KPRes.SyncFailed,
                        pwDatabase.IOConnectionInfo.GetDisplayName());

                    bAllSuccess = false;
                }
            }
            else if(bSynchronize) // Synchronized but not successfully imported
            {
                MessageService.ShowWarning(KPRes.SyncFailed,
                    pwDatabase.IOConnectionInfo.GetDisplayName());

                bAllSuccess = false;
            }

            return bAllSuccess;
        }
Ejemplo n.º 14
0
        private static bool PerformImport(PwDatabase pwDatabase, FileFormatProvider fmtImp,
                                          IOConnectionInfo[] vConnections, bool bSynchronize, IUIOperations uiOps,
                                          bool bForceSave)
        {
            if (fmtImp.TryBeginImport() == false)
            {
                return(false);
            }

            bool bUseTempDb  = (fmtImp.SupportsUuids || fmtImp.RequiresKey);
            bool bAllSuccess = true;

            // if(bSynchronize) { Debug.Assert(vFiles.Length == 1); }

            StatusLoggerForm slf = new StatusLoggerForm();

            slf.InitEx(false);
            slf.Show();

            if (bSynchronize)
            {
                slf.StartLogging(KPRes.Synchronize, false);
            }
            else
            {
                slf.StartLogging(KPRes.ImportingStatusMsg, false);
            }

            if (vConnections.Length == 0)
            {
                try { fmtImp.Import(pwDatabase, null, slf); }
                catch (Exception exSingular)
                {
                    if ((exSingular.Message != null) && (exSingular.Message.Length > 0))
                    {
                        slf.SetText(exSingular.Message, LogStatusType.Warning);
                        MessageService.ShowWarning(exSingular);
                    }
                }

                slf.EndLogging(); slf.Close();
                return(true);
            }

            foreach (IOConnectionInfo iocIn in vConnections)
            {
                Stream s = null;

                try { s = IOConnection.OpenRead(iocIn); }
                catch (Exception exFile)
                {
                    MessageService.ShowWarning(iocIn.GetDisplayName(), exFile);
                    bAllSuccess = false;
                    continue;
                }

                if (s == null)
                {
                    Debug.Assert(false); bAllSuccess = false; continue;
                }

                PwDatabase pwImp;
                if (bUseTempDb)
                {
                    pwImp = new PwDatabase();
                    pwImp.New(new IOConnectionInfo(), pwDatabase.MasterKey);
                    pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
                }
                else
                {
                    pwImp = pwDatabase;
                }

                if (fmtImp.RequiresKey && !bSynchronize)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    kpf.InitEx(iocIn.GetDisplayName(), false);

                    if (kpf.ShowDialog() != DialogResult.OK)
                    {
                        s.Close(); continue;
                    }

                    pwImp.MasterKey = kpf.CompositeKey;
                }
                else if (bSynchronize)
                {
                    pwImp.MasterKey = pwDatabase.MasterKey;
                }

                slf.SetText((bSynchronize ? KPRes.Synchronizing : KPRes.ImportingStatusMsg) +
                            " " + iocIn.GetDisplayName(), LogStatusType.Info);

                try { fmtImp.Import(pwImp, s, slf); }
                catch (Exception excpFmt)
                {
                    string strMsgEx = excpFmt.Message;
                    if (bSynchronize)
                    {
                        strMsgEx += MessageService.NewParagraph +
                                    KPRes.SynchronizingHint;
                    }

                    MessageService.ShowWarning(strMsgEx);

                    s.Close();
                    bAllSuccess = false;
                    continue;
                }

                s.Close();

                if (bUseTempDb)
                {
                    PwMergeMethod mm;
                    if (!fmtImp.SupportsUuids)
                    {
                        mm = PwMergeMethod.CreateNewUuids;
                    }
                    else if (bSynchronize)
                    {
                        mm = PwMergeMethod.Synchronize;
                    }
                    else
                    {
                        ImportMethodForm imf = new ImportMethodForm();
                        if (imf.ShowDialog() != DialogResult.OK)
                        {
                            continue;
                        }
                        mm = imf.MergeMethod;
                    }

                    slf.SetText(KPRes.MergingData, LogStatusType.Info);

                    try { pwDatabase.MergeIn(pwImp, mm); }
                    catch (Exception exMerge)
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(),
                                                   KPRes.ImportFailed, exMerge);

                        bAllSuccess = false;
                        continue;
                    }
                }
            }

            slf.EndLogging(); slf.Close();

            if (bSynchronize && bAllSuccess)
            {
                Debug.Assert(uiOps != null);
                if (uiOps == null)
                {
                    throw new ArgumentNullException("uiOps");
                }

                if (uiOps.UIFileSave(bForceSave))
                {
                    foreach (IOConnectionInfo ioc in vConnections)
                    {
                        try
                        {
                            if (pwDatabase.IOConnectionInfo.IsLocalFile())
                            {
                                if ((pwDatabase.IOConnectionInfo.Path != ioc.Path) &&
                                    ioc.IsLocalFile())
                                {
                                    File.Copy(pwDatabase.IOConnectionInfo.Path,
                                              ioc.Path, true);
                                }
                            }
                            else
                            {
                                pwDatabase.SaveAs(ioc, false, null);
                            }
                        }
                        catch (Exception exSync)
                        {
                            MessageService.ShowWarning(KPRes.SyncFailed,
                                                       pwDatabase.IOConnectionInfo.GetDisplayName() +
                                                       MessageService.NewLine + ioc.GetDisplayName(), exSync);

                            bAllSuccess = false;
                            continue;
                        }
                    }
                }
                else
                {
                    MessageService.ShowWarning(KPRes.SyncFailed,
                                               pwDatabase.IOConnectionInfo.GetDisplayName());

                    bAllSuccess = false;
                }
            }
            else if (bSynchronize)            // Synchronized but not successfully imported
            {
                MessageService.ShowWarning(KPRes.SyncFailed,
                                           pwDatabase.IOConnectionInfo.GetDisplayName());

                bAllSuccess = false;
            }

            return(bAllSuccess);
        }
Ejemplo n.º 15
0
        public static bool? Import(PwDatabase pwDatabase, FileFormatProvider fmtImp,
			IOConnectionInfo[] vConnections, bool bSynchronize, IUIOperations uiOps,
			bool bForceSave, Form fParent)
        {
            if(pwDatabase == null) throw new ArgumentNullException("pwDatabase");
            if(!pwDatabase.IsOpen) return null;
            if(fmtImp == null) throw new ArgumentNullException("fmtImp");
            if(vConnections == null) throw new ArgumentNullException("vConnections");

            if(!AppPolicy.Try(AppPolicyId.Import)) return false;
            if(!fmtImp.TryBeginImport()) return false;

            bool bUseTempDb = (fmtImp.SupportsUuids || fmtImp.RequiresKey);
            bool bAllSuccess = true;

            // if(bSynchronize) { Debug.Assert(vFiles.Length == 1); }

            IStatusLogger dlgStatus;
            if(Program.Config.UI.ShowImportStatusDialog)
                dlgStatus = new OnDemandStatusDialog(false, fParent);
            else dlgStatus = new UIBlockerStatusLogger(fParent);

            dlgStatus.StartLogging(PwDefs.ShortProductName + " - " + (bSynchronize ?
                KPRes.Synchronizing : KPRes.ImportingStatusMsg), false);
            dlgStatus.SetText(bSynchronize ? KPRes.Synchronizing :
                KPRes.ImportingStatusMsg, LogStatusType.Info);

            if(vConnections.Length == 0)
            {
                try { fmtImp.Import(pwDatabase, null, dlgStatus); }
                catch(Exception exSingular)
                {
                    if((exSingular.Message != null) && (exSingular.Message.Length > 0))
                    {
                        // slf.SetText(exSingular.Message, LogStatusType.Warning);
                        MessageService.ShowWarning(exSingular);
                    }
                }

                dlgStatus.EndLogging();
                return true;
            }

            foreach(IOConnectionInfo iocIn in vConnections)
            {
                Stream s = null;

                try { s = IOConnection.OpenRead(iocIn); }
                catch(Exception exFile)
                {
                    // Transacted-file operations can leave behind intact *.kdbx.tmp files when
                    // the file rename doesn't get completed (can happen easily with slow/unreliable
                    // remote collections. We check if that's the case here and fix the situation if
                    // an kdbx file does *not* exist (to avoid possibly overwriting good data with bad
                    // data (i.e. an interrupted kdbx.tmp write).

                    // Make a copy of the IOC like FileTransactionEx.cs:Initialize does
                    IOConnectionInfo iocTemp = iocIn.CloneDeep();
                    iocTemp.Path += FileTransactionEx.StrTempSuffix;

                    if (IOConnection.FileExists(iocTemp) && !IOConnection.FileExists(iocIn))
                    {
                            // Try and rename iocTemp to ioc.Path, then retry file opening.
                            IOConnection.RenameFile(iocTemp, iocIn);
                            try { s = IOConnection.OpenRead(iocIn); }
                            catch(Exception nexFile)
                            {
                                    MessageService.ShowWarning(iocIn.GetDisplayName(), nexFile);
                                    bAllSuccess = false;
                                    continue;
                            }
                    }
                    else
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(), exFile);
                        bAllSuccess = false;
                        continue;
                    }
                }
                if(s == null) { Debug.Assert(false); bAllSuccess = false; continue; }

                PwDatabase pwImp;
                if(bUseTempDb)
                {
                    pwImp = new PwDatabase();
                    pwImp.New(new IOConnectionInfo(), pwDatabase.MasterKey);
                    pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
                }
                else pwImp = pwDatabase;

                if(fmtImp.RequiresKey && !bSynchronize)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    kpf.InitEx(iocIn, false, true);

                    if(UIUtil.ShowDialogNotValue(kpf, DialogResult.OK)) { s.Close(); continue; }

                    pwImp.MasterKey = kpf.CompositeKey;
                    UIUtil.DestroyForm(kpf);
                }
                else if(bSynchronize) pwImp.MasterKey = pwDatabase.MasterKey;

                dlgStatus.SetText((bSynchronize ? KPRes.Synchronizing :
                    KPRes.ImportingStatusMsg) + " (" + iocIn.GetDisplayName() +
                    ")", LogStatusType.Info);

                try { fmtImp.Import(pwImp, s, dlgStatus); }
                catch(Exception excpFmt)
                {
                    string strMsgEx = excpFmt.Message;
                    if(bSynchronize && (excpFmt is InvalidCompositeKeyException))
                        strMsgEx = KLRes.InvalidCompositeKey + MessageService.NewParagraph +
                            KPRes.SynchronizingHint;

                    MessageService.ShowWarning(strMsgEx);

                    s.Close();
                    bAllSuccess = false;
                    continue;
                }

                s.Close();

                if(bUseTempDb)
                {
                    PwMergeMethod mm;
                    if(!fmtImp.SupportsUuids) mm = PwMergeMethod.CreateNewUuids;
                    else if(bSynchronize) mm = PwMergeMethod.Synchronize;
                    else
                    {
                        ImportMethodForm imf = new ImportMethodForm();
                        if(UIUtil.ShowDialogNotValue(imf, DialogResult.OK)) continue;
                        mm = imf.MergeMethod;
                        UIUtil.DestroyForm(imf);
                    }

                    try { pwDatabase.MergeIn(pwImp, mm, dlgStatus); }
                    catch(Exception exMerge)
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(),
                            KPRes.ImportFailed, exMerge);

                        bAllSuccess = false;
                        continue;
                    }
                }
            }

            if(bSynchronize && bAllSuccess)
            {
                Debug.Assert(uiOps != null);
                if(uiOps == null) throw new ArgumentNullException("uiOps");

                dlgStatus.SetText(KPRes.Synchronizing + " (" +
                    KPRes.SavingDatabase + ")", LogStatusType.Info);

                MainForm mf = Program.MainForm; // Null for KPScript
                if(mf != null)
                {
                    try { mf.DocumentManager.ActiveDatabase = pwDatabase; }
                    catch(Exception) { Debug.Assert(false); }
                }

                if(uiOps.UIFileSave(bForceSave))
                {
                    foreach(IOConnectionInfo ioc in vConnections)
                    {
                        try
                        {
                            // dlgStatus.SetText(KPRes.Synchronizing + " (" +
                            //	KPRes.SavingDatabase + " " + ioc.GetDisplayName() +
                            //	")", LogStatusType.Info);

                            if(ioc.Path != pwDatabase.IOConnectionInfo.Path)
                            {
                                if(pwDatabase.IOConnectionInfo.IsLocalFile() &&
                                    ioc.IsLocalFile())
                                {
                                    File.Copy(pwDatabase.IOConnectionInfo.Path,
                                        ioc.Path, true);
                                }
                                else pwDatabase.SaveAs(ioc, false, null);
                            }
                            // else { } // No assert (sync on save)

                            if(mf != null)
                                mf.FileMruList.AddItem(ioc.GetDisplayName(),
                                    ioc.CloneDeep());
                        }
                        catch(Exception exSync)
                        {
                            MessageService.ShowWarning(KPRes.SyncFailed,
                                pwDatabase.IOConnectionInfo.GetDisplayName() +
                                MessageService.NewLine + ioc.GetDisplayName(), exSync);

                            bAllSuccess = false;
                            continue;
                        }
                    }
                }
                else
                {
                    MessageService.ShowWarning(KPRes.SyncFailed,
                        pwDatabase.IOConnectionInfo.GetDisplayName());

                    bAllSuccess = false;
                }
            }

            dlgStatus.EndLogging();
            return bAllSuccess;
        }