static void Main(string[] aArgs) { //Debugger.Launch(); CommandLineArgs.Parse(aArgs); if (CommandLineArgs.Mode == AgentMode.Server && PageantAgent.CheckPageantRunning()) { MessageBox.Show(Strings.errPageantRunning, Util.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); Environment.Exit(1); return; } if (CommandLineArgs.Mode == AgentMode.Auto) { if (PageantAgent.CheckPageantRunning()) { CommandLineArgs.Mode = AgentMode.Client; } else { CommandLineArgs.Mode = AgentMode.Server; } } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); switch (CommandLineArgs.Mode) { case AgentMode.Server: try { Agent = new PageantAgent(); } catch (PageantRunningException) { Debug.Fail("should not get here unless Pageant started in last few msec."); Environment.Exit(1); return; } break; case AgentMode.Client: Agent = new PageantClient(); break; default: Debug.Fail("unknown mode"); Environment.Exit(1); return; } Agent.AddKeysFromFiles(CommandLineArgs.Files); Application.ApplicationExit += delegate(object aSender, EventArgs aEventArgs) { if (Agent is Agent) { ((Agent)Agent).Dispose(); } }; var keyManagerForm = new KeyManagerForm(Agent); if (!(Agent is Agent)) { keyManagerForm.Text += " - client mode"; } keyManagerForm.FormClosed += delegate(object aSender, FormClosedEventArgs aEventArgs) { Environment.Exit(0); }; keyManagerForm.Show(); Application.Run(); }
public void ShowFileOpenDialog() { string[] fileNames; List <Agent.KeyConstraint> constraints = new List <Agent.KeyConstraint>(); if (mAgent is PageantClient) { // Client Mode with Pageant - Show standard file dialog since we don't // need / can't use constraints using (var openFileDialog = new OpenFileDialog()) { openFileDialog.Multiselect = true; openFileDialog.Filter = string.Join("|", Strings.filterPuttyPrivateKeyFiles, "*.ppk", Strings.filterAllFiles, "*.*"); var result = openFileDialog.ShowDialog(); if (result != DialogResult.OK) { return; } fileNames = openFileDialog.FileNames; } } else if (CommonOpenFileDialog.IsPlatformSupported) { // Windows Vista/7/8 has new style file open dialog that can be extended // using the Windows API via the WindowsAPICodepack library var win7OpenFileDialog = new CommonOpenFileDialog(); win7OpenFileDialog.Multiselect = true; win7OpenFileDialog.EnsureFileExists = true; var confirmConstraintCheckBox = new CommonFileDialogCheckBox(cConfirmConstraintCheckBox, "Require user confirmation"); var lifetimeConstraintTextBox = new CommonFileDialogTextBox(cLifetimeConstraintTextBox, string.Empty); lifetimeConstraintTextBox.Visible = false; var lifetimeConstraintCheckBox = new CommonFileDialogCheckBox(cLifetimeConstraintCheckBox, "Set lifetime (in seconds)"); lifetimeConstraintCheckBox.CheckedChanged += (s, e) => { lifetimeConstraintTextBox.Visible = lifetimeConstraintCheckBox.IsChecked; }; var confirmConstraintGroupBox = new CommonFileDialogGroupBox(); var lifetimeConstraintGroupBox = new CommonFileDialogGroupBox(); confirmConstraintGroupBox.Items.Add(confirmConstraintCheckBox); lifetimeConstraintGroupBox.Items.Add(lifetimeConstraintCheckBox); lifetimeConstraintGroupBox.Items.Add(lifetimeConstraintTextBox); win7OpenFileDialog.Controls.Add(confirmConstraintGroupBox); win7OpenFileDialog.Controls.Add(lifetimeConstraintGroupBox); var filter = new CommonFileDialogFilter( Strings.filterPuttyPrivateKeyFiles, "*.ppk"); win7OpenFileDialog.Filters.Add(filter); filter = new CommonFileDialogFilter(Strings.filterAllFiles, "*.*"); win7OpenFileDialog.Filters.Add(filter); win7OpenFileDialog.FileOk += win7OpenFileDialog_FileOk; /* add help listeners to win7OpenFileDialog */ // declare variables here so that the GC does not eat them. WndProcDelegate newWndProc, oldWndProc = null; win7OpenFileDialog.DialogOpening += (sender, e) => { var hwnd = win7OpenFileDialog.GetWindowHandle(); // hook into WndProc to catch WM_HELP, i.e. user pressed F1 newWndProc = (hWnd, msg, wParam, lParam) => { const short shellHelpCommand = 0x7091; var win32Msg = (Win32Types.Msg)msg; switch (win32Msg) { case Win32Types.Msg.WM_HELP: var helpInfo = (HELPINFO)Marshal.PtrToStructure(lParam, typeof(HELPINFO)); // Ignore if we are on an unknown control or control 100. // These are the windows shell control. The help command is // issued by these controls so by not ignoring, we would call // the help method twice. if (helpInfo.iCtrlId != 0 && helpInfo.iCtrlId != 100) { OnAddFromFileHelpRequested(win7OpenFileDialog, EventArgs.Empty); } return((IntPtr)1); // TRUE case Win32Types.Msg.WM_COMMAND: var wParamBytes = BitConverter.GetBytes(wParam.ToInt32()); var highWord = BitConverter.ToInt16(wParamBytes, 0); var lowWord = BitConverter.ToInt16(wParamBytes, 2); if (lowWord == 0 && highWord == shellHelpCommand) { OnAddFromFileHelpRequested(win7OpenFileDialog, EventArgs.Empty); return((IntPtr)0); } break; } return(CallWindowProc(oldWndProc, hwnd, msg, wParam, lParam)); }; var newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc); var oldWndProcPtr = SetWindowLongPtr(hwnd, WindowLongFlags.GWL_WNDPROC, newWndProcPtr); oldWndProc = (WndProcDelegate) Marshal.GetDelegateForFunctionPointer(oldWndProcPtr, typeof(WndProcDelegate)); }; var result = win7OpenFileDialog.ShowDialog(); if (result != CommonFileDialogResult.Ok) { return; } if (confirmConstraintCheckBox.IsChecked) { var constraint = new Agent.KeyConstraint(); constraint.Type = Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM; constraints.Add(constraint); } if (lifetimeConstraintCheckBox.IsChecked) { // error checking for parse done in fileOK event handler uint lifetime = uint.Parse(lifetimeConstraintTextBox.Text); var constraint = new Agent.KeyConstraint(); constraint.Type = Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME; constraint.Data = lifetime; constraints.Add(constraint); } fileNames = win7OpenFileDialog.FileNames.ToArray(); } else { using (var openFileDialog = new OpenFileDialog()) { openFileDialog.Multiselect = true; openFileDialog.Filter = string.Join("|", Strings.filterPuttyPrivateKeyFiles, "*.ppk", Strings.filterAllFiles, "*.*"); openFileDialog.FileOk += xpOpenFileDialog_FileOk; // Windows XP uses old style file open dialog that can be extended // using the Windows API via FileDlgExtenders library XPOpenFileDialog xpOpenFileDialog = null; if (Type.GetType("Mono.Runtime") == null) { xpOpenFileDialog = new XPOpenFileDialog(); xpOpenFileDialog.FileDlgStartLocation = AddonWindowLocation.Bottom; mOpenFileDialogMap.Add(openFileDialog, xpOpenFileDialog); } openFileDialog.HelpRequest += OnAddFromFileHelpRequested; // TODO: technically, a listener could be added after this openFileDialog.ShowHelp = AddFromFileHelpRequested != null; var result = xpOpenFileDialog == null? openFileDialog.ShowDialog() : openFileDialog.ShowDialog(xpOpenFileDialog, null); if (result != DialogResult.OK) { return; } if (xpOpenFileDialog == null) { // If dialog could not be extended, then we add constraints by holding // down the control key when clicking the Open button. if (Control.ModifierKeys.HasFlag(Keys.Control)) { var constraintDialog = new ConstraintsInputDialog(); constraintDialog.ShowDialog(); if (constraintDialog.DialogResult == DialogResult.OK) { if (constraintDialog.ConfirmConstraintChecked) { constraints.AddConfirmConstraint(); } if (constraintDialog.LifetimeConstraintChecked) { constraints.AddLifetimeConstraint(constraintDialog.LifetimeDuration); } } } } else { mOpenFileDialogMap.Remove(openFileDialog); if (xpOpenFileDialog.UseConfirmConstraintChecked) { constraints.AddConfirmConstraint(); } if (xpOpenFileDialog.UseLifetimeConstraintChecked) { constraints.AddLifetimeConstraint (xpOpenFileDialog.LifetimeConstraintDuration); } } fileNames = openFileDialog.FileNames; } } UseWaitCursor = true; mAgent.AddKeysFromFiles(fileNames, constraints); if (!(mAgent is Agent)) { ReloadKeyListView(); } UseWaitCursor = false; }