Exemple #1
0
        public OpenVpnChild(OpenVpnServiceConfiguration config, string configFile)
        {
            this.config = config;
            /// SET UP LOG FILES

            /* Because we will be using the filenames in our closures,
             * so make sure we are working on a copy */
            this.configFile = String.Copy(configFile);
            var justFilename = System.IO.Path.GetFileName(configFile);
            var logFilename  = config.logDir + "\\" +
                               justFilename.Substring(0, justFilename.Length - config.configExt.Length) + ".log";

            // FIXME: if (!init_security_attributes_allow_all (&sa))
            //{
            //    MSG (M_SYSERR, "InitializeSecurityDescriptor start_" PACKAGE " failed");
            //    goto finish;
            //}

            logFile = new StreamWriter(File.Open(logFilename,
                                                 config.logAppend ? FileMode.Append : FileMode.Create,
                                                 FileAccess.Write,
                                                 FileShare.Read), new UTF8Encoding(false));

            /// SET UP PROCESS START INFO
            string[] procArgs =
            {
                "--config",
                "\"" + configFile + "\""
            };
            this.startInfo = new System.Diagnostics.ProcessStartInfo()
            {
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WindowStyle            = System.Diagnostics.ProcessWindowStyle.Hidden,

                FileName         = config.exePath,
                Arguments        = String.Join(" ", procArgs),
                WorkingDirectory = config.configDir,

                UseShellExecute = false,
                /* create_new_console is not exposed -- but we probably don't need it?*/
            };

            /// SET UP FLUSH TIMER

            /** .NET has a very annoying habit of taking a very long time to flush
             *  output streams **/
            var flushTimer = new System.Timers.Timer(60000);

            flushTimer.AutoReset = true;
            flushTimer.Elapsed  += (object source, System.Timers.ElapsedEventArgs e) =>
            {
                logFile.Flush();
            };
            flushTimer.Start();
        }
Exemple #2
0
        public OpenVpnChild(OpenVpnServiceConfiguration config, string configFile)
        {
            this.config = config;
            /// SET UP LOG FILES

            /* Because we will be using the filenames in our closures,
             * so make sure we are working on a copy */
            this.configFile = String.Copy(configFile);
            this.exitEvent  = Path.GetFileName(configFile) + "_" + Process.GetCurrentProcess().Id.ToString();
            var justFilename = System.IO.Path.GetFileName(configFile);
            var logFilename  = config.logDir + "\\" +
                               justFilename.Substring(0, justFilename.Length - config.configExt.Length) + ".log";

            // FIXME: if (!init_security_attributes_allow_all (&sa))
            //{
            //    MSG (M_SYSERR, "InitializeSecurityDescriptor start_" PACKAGE " failed");
            //    goto finish;
            //}

            logFile = new StreamWriter(File.Open(logFilename,
                                                 config.logAppend ? FileMode.Append : FileMode.Create,
                                                 FileAccess.Write,
                                                 FileShare.Read), new UTF8Encoding(false));
            logFile.AutoFlush = true;

            /// SET UP PROCESS START INFO
            string[] procArgs =
            {
                "--config",
                "\"" + configFile + "\"",
                "--service ",
                "\"" + exitEvent + "\"" + " 0"
            };
            this.startInfo = new System.Diagnostics.ProcessStartInfo()
            {
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WindowStyle            = System.Diagnostics.ProcessWindowStyle.Hidden,

                FileName         = config.exePath,
                Arguments        = String.Join(" ", procArgs),
                WorkingDirectory = config.configDir,

                UseShellExecute = false,
                /* create_new_console is not exposed -- but we probably don't need it?*/
            };
        }
Exemple #3
0
        protected override void OnStart(string[] args)
        {
            try
            {
                List <RegistryKey> rkOvpns = new List <RegistryKey>();

                // Search 64-bit registry, then 32-bit registry for OpenVpn
                var key = GetRegistrySubkey(RegistryView.Registry64);
                if (key != null)
                {
                    rkOvpns.Add(key);
                }
                key = GetRegistrySubkey(RegistryView.Registry32);
                if (key != null)
                {
                    rkOvpns.Add(key);
                }

                if (rkOvpns.Count() == 0)
                {
                    throw new Exception("Registry key missing");
                }

                var configDirsConsidered = new HashSet <string>();

                foreach (var rkOvpn in rkOvpns)
                {
                    try {
                        bool append = false;
                        {
                            var logAppend = (string)rkOvpn.GetValue("log_append");
                            if (logAppend[0] == '0' || logAppend[0] == '1')
                            {
                                append = logAppend[0] == '1';
                            }
                            else
                            {
                                throw new Exception("Log file append flag must be 1 or 0");
                            }
                        }

                        var config = new OpenVpnServiceConfiguration()
                        {
                            exePath       = (string)rkOvpn.GetValue("exe_path"),
                            configDir     = (string)rkOvpn.GetValue("config_dir"),
                            configExt     = "." + (string)rkOvpn.GetValue("config_ext"),
                            logDir        = (string)rkOvpn.GetValue("log_dir"),
                            logAppend     = append,
                            priorityClass = GetPriorityClass((string)rkOvpn.GetValue("priority")),

                            eventLog = EventLog,
                        };

                        if (configDirsConsidered.Contains(config.configDir))
                        {
                            continue;
                        }
                        configDirsConsidered.Add(config.configDir);

                        /// Only attempt to start the service
                        /// if openvpn.exe is present. This should help if there are old files
                        /// and registry settings left behind from a previous OpenVPN 32-bit installation
                        /// on a 64-bit system.
                        if (!File.Exists(config.exePath))
                        {
                            EventLog.WriteEntry("OpenVPN binary does not exist at " + config.exePath);
                            continue;
                        }

                        foreach (var configFilename in Directory.EnumerateFiles(config.configDir,
                                                                                "*" + config.configExt,
                                                                                System.IO.SearchOption.AllDirectories))
                        {
                            try {
                                var child = new OpenVpnChild(config, configFilename);
                                Subprocesses.Add(child);
                                child.Start();
                            }
                            catch (Exception e)
                            {
                                EventLog.WriteEntry("Caught exception " + e.Message + " when starting openvpn for "
                                                    + configFilename);
                            }
                        }
                    }
                    catch (NullReferenceException e) /* e.g. missing registry values */
                    {
                        EventLog.WriteEntry("Registry values are incomplete for " + rkOvpn.View.ToString() + e.StackTrace);
                    }
                }
            }
            catch (Exception e)
            {
                EventLog.WriteEntry("Exception occured during OpenVPN service start: " + e.Message + e.StackTrace);
                throw e;
            }
        }
Exemple #4
0
        protected override void OnStart(string[] args)
        {
            try
            {
                List<RegistryKey> rkOvpns = new List<RegistryKey>();

                // Search 64-bit registry, then 32-bit registry for OpenVpn
                var key = GetRegistrySubkey(RegistryView.Registry64);
                if (key != null) rkOvpns.Add(key);
                key = GetRegistrySubkey(RegistryView.Registry32);
                if (key != null) rkOvpns.Add(key);

                if (rkOvpns.Count() == 0)
                    throw new Exception("Registry key missing");

                var configDirsConsidered = new HashSet<string>();

                foreach (var rkOvpn in rkOvpns)
                {
                    try {
                        bool append = false;
                        {
                            var logAppend = (string)rkOvpn.GetValue("log_append");
                            if (logAppend[0] == '0' || logAppend[0] == '1')
                                append = logAppend[0] == '1';
                            else
                                throw new Exception("Log file append flag must be 1 or 0");
                        }

                        var config = new OpenVpnServiceConfiguration()
                        {
                            exePath = (string)rkOvpn.GetValue("exe_path"),
                            configDir = (string)rkOvpn.GetValue("config_dir"),
                            configExt = "." + (string)rkOvpn.GetValue("config_ext"),
                            logDir = (string)rkOvpn.GetValue("log_dir"),
                            logAppend = append,
                            priorityClass = GetPriorityClass((string)rkOvpn.GetValue("priority")),

                            eventLog = EventLog,
                        };

                        if (configDirsConsidered.Contains(config.configDir)) {
                            continue;
                        }
                        configDirsConsidered.Add(config.configDir);

                        /// Only attempt to start the service
                        /// if openvpn.exe is present. This should help if there are old files
                        /// and registry settings left behind from a previous OpenVPN 32-bit installation
                        /// on a 64-bit system.
                        if (!File.Exists(config.exePath))
                        {
                            EventLog.WriteEntry("OpenVPN binary does not exist at " + config.exePath);
                            continue;
                        }

                        foreach (var configFilename in Directory.EnumerateFiles(config.configDir,
                                                                                "*" + config.configExt,
                                                                                System.IO.SearchOption.AllDirectories))
                        {
                            try {
                                var child = new OpenVpnChild(config, configFilename);
                                Subprocesses.Add(child);
                                child.Start();
                            }
                            catch (Exception e)
                            {
                                EventLog.WriteEntry("Caught exception " + e.Message + " when starting openvpn for "
                                    + configFilename);
                            }
                        }
                    }
                    catch (NullReferenceException e) /* e.g. missing registry values */
                    {
                        EventLog.WriteEntry("Registry values are incomplete for " + rkOvpn.View.ToString() + e.StackTrace);
                    }
                }

            }
            catch (Exception e)
            {
                EventLog.WriteEntry("Exception occured during OpenVPN service start: " + e.Message + e.StackTrace);
                throw e;
            }
        }
Exemple #5
0
        public OpenVpnChild(OpenVpnServiceConfiguration config, string configFile) {
            this.config = config;
            /// SET UP LOG FILES
            /* Because we will be using the filenames in our closures,
             * so make sure we are working on a copy */
            this.configFile = String.Copy(configFile);
            var justFilename = System.IO.Path.GetFileName(configFile);
            var logFilename = config.logDir + "\\" +
                    justFilename.Substring(0, justFilename.Length - config.configExt.Length) + ".log";
            
            // FIXME: if (!init_security_attributes_allow_all (&sa))
            //{
            //    MSG (M_SYSERR, "InitializeSecurityDescriptor start_" PACKAGE " failed");
            //    goto finish;
            //}
            
            logFile = new StreamWriter(File.Open(logFilename,
                config.logAppend ? FileMode.Append : FileMode.Create,
                FileAccess.Write,
                FileShare.Read), new UTF8Encoding(false));
            
            /// SET UP PROCESS START INFO
            string[] procArgs = {
                "--config",
                "\"" + configFile + "\""
            };
            this.startInfo = new System.Diagnostics.ProcessStartInfo()
            {
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,

                FileName = config.exePath,
                Arguments = String.Join(" ", procArgs),
                WorkingDirectory = config.configDir,

                UseShellExecute = false,
                /* create_new_console is not exposed -- but we probably don't need it?*/
            };
            
            /// SET UP FLUSH TIMER
            /** .NET has a very annoying habit of taking a very long time to flush
                output streams **/
            var flushTimer = new System.Timers.Timer(60000);
            flushTimer.AutoReset = true;
            flushTimer.Elapsed += (object source, System.Timers.ElapsedEventArgs e) =>
                {
                    logFile.Flush();
                };
            flushTimer.Start();
        }