// // OpenVPN Administrator // // Author(s): Everaldo Canuto // // (C) 2006 Everaldo Canuto // (C) 2006 The Gang // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // using System; using Gtk; using Glade; namespace OpenVPN.Admin { public class DetailForm : BaseDialog { #region Fields [Widget] Dialog detailForm; [Widget] Image imageLogo; [Widget] Container area1; [Widget] Container area2; [Widget] Container area3; [Widget] Container area4; [Widget] Container area5; [Widget] Container area6; [Widget] Gtk.Button buttonCancel; [Widget] Gtk.Button buttonOk; [Widget] Gtk.Button buttonClose; [Widget] Gtk.Entry entryNickname; [Widget] Gtk.Entry entryDescription; [Widget] Gtk.Entry entryType; // General [Widget] Gtk.Entry entryProtocol; [Widget] Gtk.SpinButton spinVerbosity; [Widget] Gtk.Entry entryDevice; [Widget] Gtk.Entry entryDeviceNode; [Widget] Gtk.Entry entryRemoteAddress; [Widget] Gtk.Entry entryRemotePort; [Widget] Gtk.Entry entryLPort; [Widget] Gtk.Entry entryRPort; [Widget] Gtk.Entry entryUser; [Widget] Gtk.Entry entryGroup; [Widget] Gtk.Entry entryLocalEnd; [Widget] Gtk.Entry entryRemoteEnd; [Widget] Gtk.Entry entryPing; [Widget] Gtk.Entry entryPingRestart; [Widget] Gtk.CheckButton checkPull; [Widget] Gtk.CheckButton checkNoBind; [Widget] Gtk.CheckButton checkPersistKey; [Widget] Gtk.CheckButton checkPersistTun; [Widget] Gtk.CheckButton checkMuteReplay; [Widget] Gtk.CheckButton checkLZOCompression; [Widget] Gtk.CheckButton checkPersistLocal; [Widget] Gtk.CheckButton checkPersistRemote; [Widget] Gtk.CheckButton checkShowNetUp; [Widget] Gtk.CheckButton checkAuthUserPass; // Certificate [Widget] Gtk.CheckButton checkTlsClient; [Widget] Gtk.Entry entrySecret; [Widget] Gtk.Entry entryCA; [Widget] Gtk.Entry entryCert; [Widget] Gtk.Entry entryKey; [Widget] Gtk.Entry entryTlsAuth; [Widget] Gtk.Button buttonSecret; [Widget] Gtk.Button buttonCA; [Widget] Gtk.Button buttonCert; [Widget] Gtk.Button buttonKey; [Widget] Gtk.Button buttonTlsAuth; // Proxy [Widget] Gtk.CheckButton checkHttpProxyRetry; [Widget] Gtk.CheckButton checkSocksProxyRetry; [Widget] Gtk.Entry entryHttpAddress; [Widget] Gtk.Entry entryHttpPort; [Widget] Gtk.Entry entryProxyUser; [Widget] Gtk.Entry entryProxyPass; [Widget] Gtk.Entry entryProxyMethod; [Widget] Gtk.Entry entrySocksAddress; [Widget] Gtk.Entry entrySocksPort; // Network [Widget] Gtk.Entry entryRouteGateway; [Widget] Gtk.Entry entryDHCP; [Widget] Gtk.Entry entryRoute; [Widget] Gtk.Entry entryRedirectGateway; [Widget] Gtk.CheckButton checkRedirectGateway; [Widget] Gtk.Entry entryRouteDelay; [Widget] Gtk.Entry entryShaper; [Widget] Gtk.Entry entryInactive; [Widget] Gtk.Entry entryTunMtu; [Widget] Gtk.Entry entryTunMtuExtra; [Widget] Gtk.Entry entryFragment; [Widget] Gtk.Entry entryMssfix; [Widget] Gtk.Entry entryPingExit; [Widget] Gtk.Entry entryMute; // Security [Widget] Gtk.Entry entryAuthAlgo; [Widget] Gtk.Entry entryCipherAlgo; [Widget] Gtk.Entry entryCertType; [Widget] Gtk.Entry entryTlsRemote; [Widget] Gtk.Entry entryChroot; private string name; private string proxypassfile; #endregion #region Constructors and destructors public DetailForm() { InitializeComponent("detailForm"); } public DetailForm(string entry) { InitializeComponent("detailForm"); this.name = entry; entryNickname.Text = entry; if (Configuration.IsLocked(entry)) { buttonCancel.Visible = false; buttonOk.Visible = false; buttonClose.Visible = true; area1.Sensitive = false; area2.Sensitive = false; area3.Sensitive = false; area4.Sensitive = false; area5.Sensitive = false; area6.Sensitive = false; entryNickname.Sensitive = false; entryDescription.Sensitive = false; } RefreshEntries(); } protected override void InitializeComponent(string formname) { base.InitializeComponent(formname); Window.Icon = Gdk.Pixbuf.LoadFromResource("openvpn-admin.png"); imageLogo.Pixbuf = Gdk.Pixbuf.LoadFromResource("openvpn-admin.png").ScaleSimple(48, 48, Gdk.InterpType.Hyper); buttonSecret.Clicked += new EventHandler(on_openfile_clicked); buttonCA.Clicked += new EventHandler(on_openfile_clicked); buttonCert.Clicked += new EventHandler(on_openfile_clicked); buttonKey.Clicked += new EventHandler(on_openfile_clicked); buttonTlsAuth.Clicked += new EventHandler(on_openfile_clicked); } #endregion #region Private methods private string FlatDir(string folder) { #if WIN32 string dir = folder.ToLower(); string cfg = Configuration.ConfigDir.ToLower(); #else string dir = folder; string cfg = Configuration.ConfigDir; #endif if (dir.StartsWith(cfg)) { return dir.Remove(0, cfg.Length+1); } else { return dir; } } #endregion #region Public methods public void RefreshEntries() { string[] split; char[] delim = {' '}; Configuration config = new Configuration(entryNickname.Text); // Main entryDescription.Text = config.Description; entryType.Text = (config["proto"].EndsWith("-server") ? "server" : "client"); entryProtocol.Text = (config["proto"].StartsWith("tcp-") ? "tcp" : (config["proto"].StartsWith("udp-") ? "udp" : config["proto"])); spinVerbosity.Value = Double.Parse(config["verb"]); entryDevice.Text = config["dev"]; entryDeviceNode.Text = config["dev-node"]; split = config["remote"].Split(delim, 2); entryRemoteAddress.Text = (split.Length > 0) ? split[0].Trim() : ""; if (split.Length > 1) entryRemotePort.Text = split[1].Trim(); else entryRemotePort.Text = (config["port"].Length > 1) ? config["port"] : ""; if (entryRemotePort.Text == "") { entryLPort.Text = config["lport"]; entryRPort.Text = config["rport"]; } entryUser.Text = config["user"]; entryGroup.Text = config["group"]; entryPing.Text = config["ping"]; entryPingRestart.Text = config["ping-restart"]; split = config["ifconfig"].Split(delim, 2); entryLocalEnd.Text = (split.Length > 0) ? split[0].Trim() : ""; entryRemoteEnd.Text = (split.Length > 1) ? split[1].Trim() : ""; checkPull.Active = config.GetBool("pull"); checkNoBind.Active = config.GetBool("nobind"); checkPersistKey.Active = config.GetBool("persist-key"); checkPersistTun.Active = config.GetBool("persist-tun"); checkMuteReplay.Active = config.GetBool("mute-replay-warnings"); checkLZOCompression.Active = config.GetBool("comp-lzo"); checkPersistLocal.Active = config.GetBool("persist-local-ip"); checkPersistRemote.Active = config.GetBool("persist-remote-ip"); checkShowNetUp.Active = config.GetBool("show-net-up"); checkAuthUserPass.Active = config.GetBool("auth-user-pass"); // Certificate checkTlsClient.Active = config.GetBool("tls-client"); entrySecret.Text = config["secret"]; entryCA.Text = config["ca"]; entryCert.Text = config["cert"]; entryKey.Text = config["key"]; split = config["tls-auth"].Split(delim, 2); entryTlsAuth.Text = (split.Length > 0) ? split[0].Trim() : ""; // Proxy checkHttpProxyRetry.Active = config.GetBool("http-proxy-retry"); checkSocksProxyRetry.Active = config.GetBool("socks-proxy-retry"); split = config["http-proxy"].Split(delim, 4); entryHttpAddress.Text = (split.Length > 0) ? split[0].Trim() : ""; entryHttpPort.Text = (split.Length > 1) ? split[1].Trim() : ""; entryProxyMethod.Text = (split.Length > 3) ? split[3].Trim() : ""; // When have a proxy user and pass must check auth file. if (split.Length > 2) { try { proxypassfile = split[2].Trim(); System.IO.Directory.SetCurrentDirectory(Configuration.ConfigDir); System.IO.StreamReader SR = System.IO.File.OpenText(proxypassfile); entryProxyUser.Text = SR.ReadLine(); entryProxyPass.Text = SR.ReadLine(); SR.Close(); } catch { } } split = config["socks-proxy"].Split(delim, 2); entrySocksAddress.Text = (split.Length > 0) ? split[0].Trim() : ""; entrySocksPort.Text = (split.Length > 1) ? split[1].Trim() : ""; // Network entryRouteGateway.Text = config["route-gateway"]; entryDHCP.Text = config["dhcp-option"]; entryRoute.Text = config["route"]; entryRedirectGateway.Text = config["redirect-gateway"]; entryRouteDelay.Text = config["route-delay"]; entryShaper.Text = config["shaper"]; entryInactive.Text = config["inactive"]; entryTunMtu.Text = config["tun-mtu"]; entryTunMtuExtra.Text = config["tun-mtu-extra"]; entryFragment.Text = config["fragment"]; entryMssfix.Text = config["mssfix"]; entryPingExit.Text = config["ping-exit"]; entryMute.Text = config["mute"]; checkRedirectGateway.Active = config.GetBool("redirect-gateway"); // Security entryAuthAlgo.Text = config["auth"]; entryCipherAlgo.Text = config["cipher"]; entryCertType.Text = config["ns-cert-type"]; entryTlsRemote.Text = config["tls-remote"]; entryChroot.Text = config["chroot"]; } public void SaveEntries() { Configuration config = new Configuration(this.name); // Main config.Name = entryNickname.Text; config.Description = entryDescription.Text; if (checkPull.Active) { entryLocalEnd.Text = ""; entryRemoteEnd.Text = ""; } // General config["proto"] = (entryProtocol.Text == "tcp") ? "tcp-client" : entryProtocol.Text; config["verb"] = spinVerbosity.Text; config["dev"] = entryDevice.Text; config["dev-node"] = entryDeviceNode.Text; config["remote"] = entryRemoteAddress.Text; config["user"] = entryUser.Text; config["group"] = entryGroup.Text; config["ifconfig"] = entryLocalEnd.Text + " " + entryRemoteEnd.Text; config["ping"] = entryPing.Text; config["ping-restart"] = entryPingRestart.Text; // Port config["port"] = entryRemotePort.Text; config["lport"] = entryLPort.Text; config["rport"] = entryRPort.Text; // Options config.SetBool("pull", checkPull.Active); config.SetBool("nobind", checkNoBind.Active); config.SetBool("persist-key", checkPersistKey.Active); config.SetBool("persist-tun", checkPersistTun.Active); config.SetBool("mute-replay-warnings", checkMuteReplay.Active); config.SetBool("comp-lzo", checkLZOCompression.Active); config.SetBool("persist-local-ip", checkPersistLocal.Active); config.SetBool("persist-remote-ip", checkPersistRemote.Active); config.SetBool("show-net-up", checkShowNetUp.Active); config.SetBool("auth-user-pass", checkAuthUserPass.Active); // Certificate config.SetBool("tls-client", checkTlsClient.Active); config["secret"] = FlatDir(entrySecret.Text); config["ca"] = FlatDir(entryCA.Text); config["cert"] = FlatDir(entryCert.Text); config["key"] = FlatDir(entryKey.Text); config["tls-auth"] = (entryTlsAuth.Text != "") ? FlatDir(entryTlsAuth.Text) + " 1" : ""; // Proxy config.SetBool("http-proxy-retry", checkHttpProxyRetry.Active); config.SetBool("socks-proxy-retry", checkSocksProxyRetry.Active); config["http-proxy"] = entryHttpAddress.Text + " " + entryHttpPort.Text; config["socks-proxy"] = entrySocksAddress.Text + " " + entrySocksPort.Text; // When have a proxy user and pass must check auth file. if (entryProxyUser.Text.Trim().Length > 0) { if (proxypassfile == null) proxypassfile = config.Name + ".proxy" ; config["http-proxy"] = entryHttpAddress.Text + " " + entryHttpPort.Text + " " + proxypassfile; if (entryProxyMethod.Text.Length > 0) config["http-proxy"] = config["http-proxy"] + " " + entryProxyMethod.Text; try { System.IO.Directory.SetCurrentDirectory(Configuration.ConfigDir); System.IO.File.Delete(proxypassfile); System.IO.StreamWriter SW = System.IO.File.CreateText(proxypassfile); SW.WriteLine(entryProxyUser.Text); SW.WriteLine(entryProxyPass.Text); SW.Close(); } catch { } } // Network config["route-gateway"] = entryRouteGateway.Text; config["dhcp-option"] = entryDHCP.Text; config["route"] = entryRoute.Text; config["route-delay"] = entryRouteDelay.Text; config["redirect-gateway"] = entryRedirectGateway.Text; config["shaper"] = entryShaper.Text; config["inactive"] = entryInactive.Text; config["tun-mtu"] = entryTunMtu.Text; config["tun-mtu-extra"] = entryTunMtuExtra.Text; config["fragment"] = entryFragment.Text; config["mssfix"] = entryMssfix.Text; config["ping-exit"] = entryPingExit.Text; config["mute"] = entryMute.Text; // To redirect-gateway with empty value. if (entryRedirectGateway.Text.Trim() == "") config.SetBool("redirect-gateway", checkRedirectGateway.Active); // Security config["auth"] = entryAuthAlgo.Text; config["cipher"] = entryCipherAlgo.Text; config["ns-cert-type"] = entryCertType.Text; config["tls-remote"] = entryTlsRemote.Text; config["chroot"] = entryChroot.Text; config.Save(); this.name = config.Name; } #endregion #region Public events public void on_checkPull_clicked(object o, EventArgs args) { entryLocalEnd.Sensitive = ! checkPull.Active; entryRemoteEnd.Sensitive = ! checkPull.Active; } public void on_ok_clicked(object o, EventArgs args) { try { SaveEntries(); detailForm.Hide(); } catch (Exception e) { ErrorBox(e.Message); } } public void on_cancel_clicked(object o, EventArgs args) { detailForm.Hide(); } #endregion Public events #region Protected events protected void on_entryRemotePort_changed(object o, EventArgs args) { if (entryRemotePort.Text != "") { entryLPort.Text = ""; entryRPort.Text = ""; } } protected void on_entryLPort_changed(object o, EventArgs args) { if ((entryLPort.Text != "") || (entryRPort.Text != "")) entryRemotePort.Text = ""; } protected void on_entryRPort_changed(object o, EventArgs args) { if ((entryLPort.Text != "") || (entryRPort.Text != "")) entryRemotePort.Text = ""; } #endregion Private events } }