public static ISshKey GetSshKey(this PwEntry entry) { var settings = entry.GetKeeAgentSettings(); var context = new SprContext(entry, entry.GetDatabase(), SprCompileFlags.Deref); return(settings.GetSshKey(entry.Strings, entry.Binaries, context)); }
private void customTreeViewEx_AfterSelect(object sender, TreeViewEventArgs e) { SelectedEntry = e.Node.Tag as PwEntry; // Set constraint flag from selected item mConfirmConstraintControl.Checked = SelectedEntry.GetKeeAgentSettings().UseConfirmConstraintWhenAdding; // Set lifetime flag from selected item; also, if item does have a // lifetime, set the lifetime duration mLifetimeConstraintControl.Checked = SelectedEntry.GetKeeAgentSettings().UseLifetimeConstraintWhenAdding; if (mLifetimeConstraintControl.Checked) { mLifetimeConstraintControl.Lifetime = SelectedEntry.GetKeeAgentSettings().LifetimeConstraintDuration; } }
public ISshKey AddEntry(PwEntry entry, ICollection<Agent.KeyConstraint> constraints) { var settings = entry.GetKeeAgentSettings(); try { var key = entry.GetSshKey(); string db_name = "<Unknown database>"; try { var database = pluginHost.MainWindow.DocumentManager.GetOpenDatabases() .Where((db) => db.RootGroup.FindEntry(entry.Uuid, true) != null).Single(); db_name = database.Name; } catch (Exception) { Debug.Fail("Duplicate UUIDs?"); } key.Source = string.Format("{0}: {1}", db_name, entry.GetFullPath()); if (agent is PageantClient) { // Pageant errors if you try to add a key that is already loaded // so try to remove the key first so that it behaves like other agents try { agent.RemoveKey(key); } catch (Exception) { // ignore failure } } else { // also, Pageant does not support constraints if (constraints != null) { foreach (var constraint in constraints) { key.AddConstraint(constraint); } } else { if (settings.UseConfirmConstraintWhenAdding) { key.addConfirmConstraint(); } if (settings.UseLifetimeConstraintWhenAdding) { key.addLifetimeConstraint(settings.LifetimeConstraintDuration); } } if (Options.AlwaysConfirm && !key.HasConstraint(Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM)) { key.addConfirmConstraint(); } } agent.AddKey(key); if (settings.Location.SelectedType == EntrySettings.LocationType.Attachment && settings.Location.SaveAttachmentToTempFile) { try { var data = entry.Binaries.Get(settings.Location.AttachmentName).ReadData(); var tempPath = Path.Combine(UrlUtil.GetTempPath(), "KeeAgent"); if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); var fileName = Path.Combine(tempPath, settings.Location.AttachmentName); File.WriteAllBytes(fileName, data); keyFileMap[key.GetMD5Fingerprint().ToHexString()] = new KeyFileInfo(fileName, true); } catch (Exception ex) { Debug.Fail(ex.Message, ex.StackTrace); } } if (settings.Location.SelectedType == EntrySettings.LocationType.File) { keyFileMap[key.GetMD5Fingerprint().ToHexString()] = new KeyFileInfo(settings.Location.FileName, false); } return key; } catch (Exception ex) { var firstLine = string.Format("KeeAgent: Error while loading key from entry '{0}'", entry.GetFullPath()); if (ex is NoAttachmentException) { MessageService.ShowWarning(new string[] { firstLine, "No attachment specified in KeePass entry" }); } else if (ex is FileNotFoundException || ex is DirectoryNotFoundException) { if (!Options.IgnoreMissingExternalKeyFiles) { MessageService.ShowWarning(new string[] { firstLine, "Could not find file", settings.Location.FileName }); } } else if (ex is KeyFormatterException || ex is PpkFormatterException) { MessageService.ShowWarning(new string[] { firstLine, string.Format ("Could not load file {0}", settings.Location.SelectedType == EntrySettings.LocationType.File ? string.Format("'{0}'", settings.Location.FileName) : string.Format("from attachment '{0}'", settings.Location.AttachmentName)), "Possible causes:", "- Passphrase was entered incorrectly", "- File is corrupt or has been tampered" }); } else if (ex is AgentFailureException) { MessageService.ShowWarning(new string[] { firstLine, "Agent Failure", "Possible causes:", "- Key is already loaded in agent", "- Agent is locked" }); } else if (ex is AgentNotRunningException) { MessageService.ShowWarning(new string[] { firstLine, "Could not add key because no SSH agent was found.", "Please make sure your SSH agent program is running (e.g. Pageant)." }); } else { MessageService.ShowWarning(new string[] { firstLine, "Unexpected error", ex.ToString() }); Debug.Fail(ex.ToString()); } throw; } }