private void UpdateUIState() { ListView.SelectedListViewItemCollection lvsc = m_lvFormats.SelectedItems; m_fmtCur = (((lvsc != null) && (lvsc.Count == 1)) ? (lvsc[0].Tag as FileFormatProvider) : null); bool bFormat = (m_fmtCur != null); bool bFileReq = (bFormat && m_fmtCur.RequiresFile); UIUtil.SetEnabledFast(bFileReq, m_lblFiles, m_tbFile, m_btnSelFile); bool bExportExt = (m_bExport && (m_piExport != null)); UIUtil.SetEnabledFast((bExportExt && bFormat && m_fmtCur.RequiresKey), m_cbExportMasterKeySpec, m_lblExportMasterKeySpec); UIUtil.SetEnabledFast((bExportExt && bFormat && (m_pd != null) && (m_pg != m_pd.RootGroup)), m_cbExportParentGroups, m_lnkExportParentGroups); UIUtil.SetEnabledFast((bExportExt && bFileReq), m_cbExportPostOpen, m_cbExportPostShow); m_btnOK.Enabled = (bFormat && ((m_tbFile.Text.Length != 0) || !m_fmtCur.RequiresFile)); }
private void UpdateUIState() { bool bFormatSelected = true; ListView.SelectedListViewItemCollection lvsc = m_lvFormats.SelectedItems; if ((lvsc == null) || (lvsc.Count != 1)) { bFormatSelected = false; } if (bFormatSelected) { m_fmtCur = (lvsc[0].Tag as FileFormatProvider); } else { m_fmtCur = null; } if (m_fmtCur != null) { m_tbFile.Enabled = m_btnSelFile.Enabled = m_fmtCur.RequiresFile; } else { m_tbFile.Enabled = m_btnSelFile.Enabled = false; } m_btnOK.Enabled = (bFormatSelected && ((m_tbFile.Text.Length != 0) || !m_fmtCur.RequiresFile)); }
public OTPHandler_DB() : base() { Program.MainForm.FileSaving += OTPDB_FileSaved; DB = OTPDB = null; m_FFP = Program.FileFormatPool.Find(FILEFORMAT); if (m_FFP == null) { throw new ArgumentNullException("ffp - Could not find FileFormatProvider"); } }
private bool PrepareExchangeEx() { UpdateUIState(); if (m_fmtCur == null) { return(false); } string strFiles = m_tbFile.Text; string[] vFiles = strFiles.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (m_fmtCur.RequiresFile) { if (vFiles.Length == 0) { return(false); } foreach (string strFile in vFiles) { IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile); if (ioc.IsLocalFile() && !UrlUtil.IsAbsolutePath(strFile)) { MessageService.ShowWarning(strFile, KPRes.FilePathFullReq); return(false); } } // Allow only one file when exporting if (m_bExport && !CheckFilePath(strFiles)) { return(false); } } else { vFiles = new string[0]; } if (m_piExport != null) { m_piExport.ExportMasterKeySpec = m_cbExportMasterKeySpec.Checked; m_piExport.ExportParentGroups = m_cbExportParentGroups.Checked; m_piExport.ExportPostOpen = m_cbExportPostOpen.Checked; m_piExport.ExportPostShow = m_cbExportPostShow.Checked; } m_fmtFinal = m_fmtCur; m_vFiles = vFiles; return(true); }
public override bool Initialize(IPluginHost host) { Contract.Requires(host != null); this.host = host; provider = new BrowserProvider(); host.FileFormatPool.Add(provider); return(true); }
private bool PrepareExchangeEx() { UpdateUIState(); if(m_fmtCur == null) return false; m_fmtFinal = m_fmtCur; m_vFiles = m_tbFile.Text.Split(new char[]{ ';' }, StringSplitOptions.RemoveEmptyEntries); if(m_bExport) { Debug.Assert(m_vFiles.Length <= 1); } return true; }
public override bool Initialize(IPluginHost host) { Contract.Requires(host != null); //System.Diagnostics.Debugger.Launch(); this.host = host; provider = new BrowserProvider(); host.FileFormatPool.Add(provider); return(true); }
private static FileFormatProvider GetFormatProv(CommandLineArgs args) { string strFormat = args["format"]; if (string.IsNullOrEmpty(strFormat)) { KPScript.Program.WriteLineColored("E: Invalid format!", ConsoleColor.Red); return(null); } FileFormatPool ffp = KeePass.Program.FileFormatPool; FileFormatProvider prov = ffp.Find(strFormat); if (prov == null) { KPScript.Program.WriteLineColored("E: Unknown format!", ConsoleColor.Red); return(null); } return(prov); }
private void OTPDB_Synchronize(PwDatabase targetdb, FileFormatProvider fmtImp, byte[] bDecrypted, string text) { IStatusLogger dlgStatus = new OnDemandStatusDialog(true, Program.MainForm); dlgStatus.StartLogging(text, false); try { dlgStatus.SetText(text, LogStatusType.Info); PwDatabase pwImp; pwImp = new PwDatabase(); pwImp.New(new IOConnectionInfo(), targetdb.MasterKey); pwImp.MemoryProtection = targetdb.MemoryProtection.CloneDeep(); pwImp.MasterKey = targetdb.MasterKey; dlgStatus.SetText(text, LogStatusType.Info); using (var s = new MemoryStream(bDecrypted)) { fmtImp.Import(pwImp, s, null); } targetdb.KdfParameters = pwImp.KdfParameters; targetdb.DataCipherUuid = pwImp.DataCipherUuid; targetdb.HistoryMaxItems = pwImp.HistoryMaxItems; targetdb.HistoryMaxSize = pwImp.HistoryMaxSize; PwMergeMethod mm = PwMergeMethod.Synchronize; targetdb.RootGroup.Uuid = pwImp.RootGroup.Uuid; targetdb.MergeIn(pwImp, mm, dlgStatus); CleanupSyncedDB(targetdb); } catch (Exception ex) { PluginDebug.AddError("Error loading OTP db", 0, ex.Message); throw ex; } finally { dlgStatus.EndLogging(); } }
private static void ImportIntoCurrentDatabase(EcasAction a, EcasContext ctx) { PwDatabase pd = Program.MainForm.ActiveDatabase; if ((pd == null) || !pd.IsOpen) { return; } string strPath = EcasUtil.GetParamString(a.Parameters, 0, true); if (string.IsNullOrEmpty(strPath)) { return; } IOConnectionInfo ioc = IOConnectionInfo.FromPath(strPath); string strFormat = EcasUtil.GetParamString(a.Parameters, 1, true); if (string.IsNullOrEmpty(strFormat)) { return; } FileFormatProvider ff = Program.FileFormatPool.Find(strFormat); if (ff == null) { throw new Exception(KPRes.Unknown + ": " + strFormat); } uint uMethod = EcasUtil.GetParamUInt(a.Parameters, 2); Type tMM = Enum.GetUnderlyingType(typeof(PwMergeMethod)); object oMethod = Convert.ChangeType(uMethod, tMM); PwMergeMethod mm = PwMergeMethod.None; if (Enum.IsDefined(typeof(PwMergeMethod), oMethod)) { mm = (PwMergeMethod)oMethod; } else { Debug.Assert(false); } if (mm == PwMergeMethod.None) { mm = PwMergeMethod.CreateNewUuids; } CompositeKey cmpKey = KeyFromParams(a, 3, 4, 5); if ((cmpKey == null) && ff.RequiresKey) { KeyPromptForm kpf = new KeyPromptForm(); kpf.InitEx(ioc, false, true); if (UIUtil.ShowDialogNotValue(kpf, DialogResult.OK)) { return; } cmpKey = kpf.CompositeKey; UIUtil.DestroyForm(kpf); } bool?b = true; try { b = ImportUtil.Import(pd, ff, ioc, mm, cmpKey); } finally { if (b.GetValueOrDefault(false)) { Program.MainForm.UpdateUI(false, null, true, null, true, null, true); } } }
public ExportDbProcessManager(int requestCode, Activity activity, FileFormatProvider ffp) : base(requestCode, activity) { _ffp = ffp; }
public ExportDb(Activity activity, IKp2aApp app, OnFinish onFinish, FileFormatProvider fileFormat, IOConnectionInfo targetIoc) : base(activity, onFinish) { _app = app; this._fileFormat = fileFormat; _targetIoc = targetIoc; }
private static void PerformExport(PwDatabase pwDb, CommandLineArgs args) { FileFormatProvider prov = GetFormatProv(args); if (prov == null) { return; } if (!prov.SupportsExport) { KPScript.Program.WriteLineColored("E: No export support for this format!", ConsoleColor.Red); return; } if (!prov.TryBeginExport()) { KPScript.Program.WriteLineColored("E: Format initialization failed!", ConsoleColor.Red); return; } FileStream fs; try { fs = new FileStream(args["outfile"], FileMode.Create, FileAccess.Write, FileShare.None); } catch (Exception exFs) { KPScript.Program.WriteLineColored("E: " + exFs.Message, ConsoleColor.Red); return; } PwGroup pg = pwDb.RootGroup; string str = args[EntryMod.ParamGroupPath]; if (!string.IsNullOrEmpty(str)) { pg = pg.FindCreateSubTree(str, new char[] { '/' }, false); if (pg == null) { KPScript.Program.WriteLineColored(@"E: Group '" + str + @"' not found!", ConsoleColor.Red); return; } } PwExportInfo pwInfo = new PwExportInfo(pg, pwDb, true); str = args[ParamXslFileCL]; if (!string.IsNullOrEmpty(str)) { pwInfo.Parameters[ParamXslFile] = str; } if (prov.Export(pwInfo, fs, null)) { KPScript.Program.WriteLineColored("OK: Export succeeded!", ConsoleColor.Green); } else { KPScript.Program.WriteLineColored("E: Export failed!", ConsoleColor.Red); } }
private static bool PerformImport(PwDatabase pwDb, CommandLineArgs args) { string strFile = args["file"]; if (string.IsNullOrEmpty(strFile)) { KPScript.Program.WriteLineColored("E: No file specified to import!", ConsoleColor.Red); return(false); } IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile); FileFormatProvider prov = GetFormatProv(args); if (prov == null) { return(false); } if (!prov.SupportsImport) { KPScript.Program.WriteLineColored("E: No import support for this format!", ConsoleColor.Red); return(false); } if (!prov.TryBeginImport()) { KPScript.Program.WriteLineColored("E: Format initialization failed!", ConsoleColor.Red); return(false); } PwMergeMethod mm = PwMergeMethod.CreateNewUuids; string strMM = args["mm"]; if (!string.IsNullOrEmpty(strMM)) { if (strMM.Equals("CreateNewUuids", StrUtil.CaseIgnoreCmp)) { mm = PwMergeMethod.CreateNewUuids; } else if (strMM.Equals("KeepExisting", StrUtil.CaseIgnoreCmp)) { mm = PwMergeMethod.KeepExisting; } else if (strMM.Equals("OverwriteExisting", StrUtil.CaseIgnoreCmp)) { mm = PwMergeMethod.OverwriteExisting; } else if (strMM.Equals("OverwriteIfNewer", StrUtil.CaseIgnoreCmp)) { mm = PwMergeMethod.OverwriteIfNewer; } else if (strMM.Equals("Sync", StrUtil.CaseIgnoreCmp)) { mm = PwMergeMethod.Synchronize; } } CompositeKey cmpKey = KpsUtil.GetMasterKey(args, "imp_", ioc); if ((cmpKey == null) || (cmpKey.UserKeyCount == 0)) { cmpKey = pwDb.MasterKey; } bool? b = false; string strError = "Import failed!"; try { b = ImportUtil.Import(pwDb, prov, ioc, mm, cmpKey); } catch (Exception ex) { if ((ex != null) && !string.IsNullOrEmpty(ex.Message)) { strError = ex.Message; } } bool r = (b.HasValue && b.Value); if (r) { KPScript.Program.WriteLineColored("OK: Import succeeded!", ConsoleColor.Green); } else { KPScript.Program.WriteLineColored("E: " + strError, ConsoleColor.Red); } return(r); }
/// <summary> /// Synchronizes the local database that <paramref name="host" /> has open with the database (if any) by the same name stored on the bucket that <paramref name="client" /> has access to. /// </summary> /// <param name="client">The <see cref="B2Client" /> created by <see cref="GetClient" /> with access to a bucket to synchronize with.</param> /// <param name="host">The <see cref="IPluginHost" /> that hosts the currently-running instance of <see cref="B2SyncExt" />.</param> /// <returns><see langword="true" /> if the synchronization was successful, or <see langword="false" /> otherwise.</returns> public static async Task <bool> SynchronizeDbAsync(B2Client client, IPluginHost host) { if (client == null) { return(false); } Synchronizing = true; Interface.UpdateStatus("Synchronizing database with B2..."); //Download the remote copy PwDatabase sourceDb = host.Database; string remoteDbPath = await DownloadDbAsync(client, GetDbFileName(sourceDb)); bool localMatchesRemote = true; //If the file exists on the remote server, synchronize it with the local copy if (remoteDbPath != null) { string localHash = HashFileOnDisk(sourceDb.IOConnectionInfo.Path); string remoteHash = HashFileOnDisk(remoteDbPath); localMatchesRemote = localHash == remoteHash; if (!localMatchesRemote) { IOConnectionInfo connInfo = IOConnectionInfo.FromPath(remoteDbPath); FileFormatProvider formatter = host.FileFormatPool.Find("KeePass KDBX (2.x)"); bool?importResult = ImportUtil.Import(sourceDb, formatter, new[] { connInfo }, true, host.MainWindow, false, host.MainWindow); //Since the Import operation automatically adds it to the list of recent files, remove it from the list afterwards host.MainWindow.FileMruList.RemoveItem(remoteDbPath); //Remove the copy of the database from the temp location File.Delete(remoteDbPath); if (!importResult.GetValueOrDefault(false)) { Interface.UpdateStatus("Something went wrong while synchronizing the local copy with the remote one."); return(false); } } } //Upload the local copy to the server once all synchronization is completed bool uploadResult = ((!localMatchesRemote || remoteDbPath == null) && await UploadDbAsync(client, sourceDb)) || localMatchesRemote; if (uploadResult) { if (!localMatchesRemote || remoteDbPath == null) { Interface.UpdateStatus("Synchronized database with B2 successfully."); } else { Interface.UpdateStatus( "No synchronization was necessary. The database is in sync with the copy on B2."); } } else { Interface.UpdateStatus("Something went wrong while uploading to B2."); } Synchronizing = false; return(uploadResult); }