Exemplo n.º 1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string svcPath = Path.Combine(Environment.CurrentDirectory, "DS4ToolService.exe");

            ServiceController sc = new ServiceController(Constants.SERVICE_NAME, Environment.MachineName);
            ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, Constants.SERVICE_NAME);
            scp.Assert();
            sc.Refresh();
            ServiceInstaller si = new ServiceInstaller();

            if (si.DoesServiceExist(Constants.SERVICE_NAME))
            {
                if (sc.Status == ServiceControllerStatus.Running)
                    sc.Stop();

                sc.WaitForStatus(ServiceControllerStatus.Stopped);
                si.UnInstallService(Constants.SERVICE_NAME);
                MessageBox.Show("Service removed");
            }

            EventLog eventLog = new EventLog();
            eventLog.Source = Constants.SERVICE_NAME;
            eventLog.Log = "Application";
            if (!EventLog.SourceExists(eventLog.Source))
            {
                EventLog.DeleteEventSource(eventLog.Source, Environment.MachineName);
                MessageBox.Show("EventLog removed");
            }
        }
 internal ServiceControllerPermissionEntryCollection(ServiceControllerPermission owner, ResourcePermissionBaseEntry[] entries)
 {
     this.owner = owner;
     for (int i = 0; i < entries.Length; i++)
     {
         base.InnerList.Add(new ServiceControllerPermissionEntry(entries[i]));
     }
 }
        public ServiceControllerPermissionEntry(ServiceControllerPermissionAccess permissionAccess,
                                                string machineName, string serviceName)
        {
            ServiceControllerPermission.ValidateMachineName(machineName);
            ServiceControllerPermission.ValidateServiceName(serviceName);

            permission_access = permissionAccess;
            machine_name      = machineName;
            service_name      = serviceName;
        }
		internal ServiceControllerPermissionEntryCollection (ServiceControllerPermission owner)
		{
			this.owner = owner;
			ResourcePermissionBaseEntry[] entries = owner.GetEntries ();
			if (entries.Length > 0) {
				foreach (ResourcePermissionBaseEntry entry in entries) {
					ServiceControllerPermissionAccess scpa = (ServiceControllerPermissionAccess) entry.PermissionAccess;
					string machine = entry.PermissionAccessPath [0];
					string service = entry.PermissionAccessPath [1];
					ServiceControllerPermissionEntry scpe = new ServiceControllerPermissionEntry (scpa, machine, service);
					// we don't want to add them (again) to the base class
					InnerList.Add (scpe);
				}
			}
		}
 internal ServiceControllerPermissionEntryCollection(ServiceControllerPermission owner)
 {
     this.owner = owner;
     ResourcePermissionBaseEntry[] entries = owner.GetEntries();
     if (entries.Length > 0)
     {
         foreach (ResourcePermissionBaseEntry entry in entries)
         {
             ServiceControllerPermissionAccess scpa = (ServiceControllerPermissionAccess)entry.PermissionAccess;
             string machine = entry.PermissionAccessPath [0];
             string service = entry.PermissionAccessPath [1];
             ServiceControllerPermissionEntry scpe = new ServiceControllerPermissionEntry(scpa, machine, service);
             // we don't want to add them (again) to the base class
             InnerList.Add(scpe);
         }
     }
 }
		public void PermissionState_Unrestricted ()
		{
			PermissionState ps = PermissionState.Unrestricted;
			ServiceControllerPermission scp = new ServiceControllerPermission (ps);
			Assert.AreEqual (0, scp.PermissionEntries.Count, "PermissionEntries");
			Assert.IsTrue (scp.IsUnrestricted (), "IsUnrestricted");

			SecurityElement se = scp.ToXml ();
			// only class and version are present
			Assert.AreEqual ("true", se.Attribute ("Unrestricted"), "Xml-Unrestricted");
			Assert.IsNull (se.Children, "Xml-Children");

			ServiceControllerPermission copy = (ServiceControllerPermission)scp.Copy ();
			Assert.IsFalse (Object.ReferenceEquals (scp, copy), "ReferenceEquals");
			Assert.AreEqual (scp.PermissionEntries.Count, copy.PermissionEntries.Count, "copy-PermissionEntries");
			Assert.AreEqual (scp.IsUnrestricted (), copy.IsUnrestricted (), "copy-IsUnrestricted ()");
		}
Exemplo n.º 7
0
        // Service start, if the service isn't active, we run it. Otherwise, nothing is done.
        /// <summary>
        /// Starting Service
        /// </summary>
        private static void StartService(){
            ImpersonateUser iu = new ImpersonateUser();
            
            iu.Impersonate(@".", "Axel", Settings.Default.Password);

            using (var controller = new ServiceController("RedXService")){
                ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, ".", "RedXService");
                scp.Assert();

                controller.Refresh();
                if (controller.Status != ServiceControllerStatus.Running){
                    controller.Start();
                }
                controller.WaitForStatus(ServiceControllerStatus.StartPending);
                controller.WaitForStatus(ServiceControllerStatus.Running);

                controller.Refresh();
            }
  
            iu.Undo();
        }
		public void FromXml_WrongClass ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
			SecurityElement se = scp.ToXml ();

			SecurityElement w = new SecurityElement (se.Tag);
			w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
			w.AddAttribute ("version", se.Attribute ("version"));
			scp.FromXml (w);
			// doesn't care of the class name at that stage
			// anyway the class has already be created so...
		}
		public void FromXml_NoClass ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
			SecurityElement se = scp.ToXml ();

			SecurityElement w = new SecurityElement (se.Tag);
			w.AddAttribute ("version", se.Attribute ("version"));
			scp.FromXml (w);
			// doesn't even care of the class attribute presence
		}
		public void Intersect_None ()
		{
			ServiceControllerPermission scp1 = new ServiceControllerPermission (PermissionState.None);
			ServiceControllerPermission scp2 = new ServiceControllerPermission (PermissionState.None);
			// 1. None N None
			ServiceControllerPermission result = (ServiceControllerPermission)scp1.Intersect (scp2);
			Assert.IsNull (result, "Empty N Empty");
			// 2. None N Entry
			scp2.PermissionEntries.Add (new ServiceControllerPermissionEntry (ServiceControllerPermissionAccess.None, "localhost", "http"));
			result = (ServiceControllerPermission)scp1.Intersect (scp2);
			Assert.IsNull (result, "Empty N Entry");
			// 3. Entry N None
			result = (ServiceControllerPermission)scp2.Intersect (scp1);
			Assert.IsNull (result, "Entry N Empty");
		}
		public void FromXml_WrongTagCase ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
			SecurityElement se = scp.ToXml ();
			se.Tag = "IPERMISSION"; // instead of IPermission
			scp.FromXml (se);
			// note: normally IPermission classes (in corlib) DO care about the
			// IPermission tag
		}
		public void ServiceControllerPermissionAccesss_Bad ()
		{
			ServiceControllerPermissionAccess scpa = (ServiceControllerPermissionAccess)Int32.MinValue;
			ServiceControllerPermission scp = new ServiceControllerPermission (scpa, "localhost", "http");
			Assert.AreEqual (1, scp.PermissionEntries.Count, "Count");
			Assert.AreEqual ((ServiceControllerPermissionAccess)Int32.MinValue, scp.PermissionEntries [0].PermissionAccess, "PermissionAccess");
		}
		public void IsSubset_Null ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
#if NET_2_0
			Assert.IsTrue (scp.IsSubsetOf (null), "null");
#else
			Assert.IsFalse (scp.IsSubsetOf (null), "null");
#endif
		}
		public void Union_Null ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
			scp.PermissionEntries.Add (new ServiceControllerPermissionEntry (ServiceControllerPermissionAccess.None, "localhost", "http"));
			// Union with null is a simple copy
			ServiceControllerPermission union = (ServiceControllerPermission)scp.Union (null);
			Assert.AreEqual (1, union.PermissionEntries.Count, "Count");
		}
		public void Copy ()
		{
			foreach (ServiceControllerPermissionAccess scpa in AllAccess) {
				ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
				ServiceControllerPermissionEntry scpe = new ServiceControllerPermissionEntry (scpa, "localhost", scpa.ToString ());
				scp.PermissionEntries.Add (scpe);
				ServiceControllerPermission copy = (ServiceControllerPermission)scp.Copy ();
				Assert.AreEqual (1, copy.PermissionEntries.Count, "Count==1");
				Assert.AreEqual (scpa, scp.PermissionEntries [0].PermissionAccess, scpa.ToString ());
			}
		}
		public void IsSubset_Unrestricted ()
		{
			// IsSubset with unrestricted
			// a. source (this) is unrestricted -> target is never a subset
			// b. destination (target) is unrestricted -> source is always a subset
			ServiceControllerPermission scp1 = new ServiceControllerPermission (PermissionState.Unrestricted);
			foreach (ServiceControllerPermissionAccess scpa in AllAccess) {
				ServiceControllerPermission scp2 = new ServiceControllerPermission (PermissionState.None);
				scp2.PermissionEntries.Add (new ServiceControllerPermissionEntry (scpa, "localhost", scpa.ToString ()));
				Assert.IsFalse (scp1.IsSubsetOf (scp2), "target " + scpa.ToString ());
				Assert.IsTrue (scp2.IsSubsetOf (scp1), "source " + scpa.ToString ());
			}
			Assert.IsTrue (scp1.IsSubsetOf (scp1), "Unrestricted.IsSubsetOf(Unrestricted)");
		}
// "special" behavior inherited from ResourceBasePermission
//		[ExpectedException (typeof (ArgumentException))]
		public void IsSubsetOf_BadPermission ()
		{
			ServiceControllerPermission scp1 = new ServiceControllerPermission (PermissionState.Unrestricted);
			Assert.IsFalse (scp1.IsSubsetOf (new SecurityPermission (SecurityPermissionFlag.Assertion)));
		}
		public void IsSubset_Self ()
		{
			foreach (ServiceControllerPermissionAccess scpa in AllAccess) {
				ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
				scp.PermissionEntries.Add (new ServiceControllerPermissionEntry (scpa, "localhost", scpa.ToString ()));
				Assert.IsTrue (scp.IsSubsetOf (scp), scpa.ToString ());
			}
		}
		public void IsSubset_Null ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
			Assert.IsTrue (scp.IsSubsetOf (null), "null");
		}
		public void Intersect_Unrestricted ()
		{
			// Intersection with unrestricted == Copy
			// a. source (this) is unrestricted
			ServiceControllerPermission scp1 = new ServiceControllerPermission (PermissionState.Unrestricted);
			ServiceControllerPermission scp2 = new ServiceControllerPermission (PermissionState.None);

			// 1. Unrestricted N None
			ServiceControllerPermission result = (ServiceControllerPermission)scp1.Intersect (scp2);
			Assert.IsFalse (result.IsUnrestricted (), "(Unrestricted N None).IsUnrestricted");
			Assert.AreEqual (0, result.PermissionEntries.Count, "(Unrestricted N None).Count");

			// 2. None N Unrestricted
			result = (ServiceControllerPermission)scp2.Intersect (scp1);
			Assert.IsFalse (result.IsUnrestricted (), "(None N Unrestricted).IsUnrestricted");
			Assert.AreEqual (0, result.PermissionEntries.Count, "(None N Unrestricted).Count");

			// 3. Unrestricted N Unrestricted
			result = (ServiceControllerPermission)scp1.Intersect (scp1);
			Assert.IsTrue (result.IsUnrestricted (), "(Unrestricted N Unrestricted).IsUnrestricted");
			Assert.AreEqual (0, result.PermissionEntries.Count, "(Unrestricted N Unrestricted).Count");

			// 4. Unrestricted N Entry
			scp2.PermissionEntries.Add (new ServiceControllerPermissionEntry (ServiceControllerPermissionAccess.None, "localhost", "http"));
			result = (ServiceControllerPermission)scp1.Intersect (scp2);
			Assert.IsFalse (result.IsUnrestricted (), "(Unrestricted N Entry).IsUnrestricted");
			Assert.AreEqual (1, result.PermissionEntries.Count, "(Unrestricted N Entry).Count");

			// 5. Entry N Unrestricted
			result = (ServiceControllerPermission)scp2.Intersect (scp1);
			Assert.IsFalse (result.IsUnrestricted (), "(Entry N Unrestricted).IsUnrestricted");
			Assert.AreEqual (1, result.PermissionEntries.Count, "(Entry N Unrestricted).Count");

			// 6. Unrestricted N Unrestricted
			scp1.PermissionEntries.Add (new ServiceControllerPermissionEntry (ServiceControllerPermissionAccess.None, "localhost", "http"));
			result = (ServiceControllerPermission)scp1.Intersect (scp1);
			Assert.IsTrue (result.IsUnrestricted (), "(Unrestricted N Unrestricted).IsUnrestricted");
			Assert.AreEqual (1, result.PermissionEntries.Count, "(Unrestricted N Unrestricted).Count");
		}
		public void FromXml_WrongVersion ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
			SecurityElement se = scp.ToXml ();
			se.Attributes.Remove ("version");
			se.Attributes.Add ("version", "2");
			scp.FromXml (se);
		}
Exemplo n.º 22
0
        static void Main(string[] args)
        {
            ServiceController[] scServices = ServiceController.GetServices();
            foreach (ServiceController scTemp in scServices)
            {
                if (scTemp.ServiceName.Equals("TestService"))
                {
                    ServiceController sc = new ServiceController("TestService");
                    Console.WriteLine("Status = " + sc.Status);
                    Console.WriteLine("Can Pause and Continue = " + sc.CanPauseAndContinue);
                    Console.WriteLine("Can Shutdown = " + sc.CanShutdown);
                    Console.WriteLine("Can Stop = " + sc.CanStop);
                    if (sc.Status == ServiceControllerStatus.Stopped)
                    {
                        sc.Start();
                        while (sc.Status == ServiceControllerStatus.Stopped)
                        {
                            Thread.Sleep(1000);
                            sc.Refresh();
                        }
                    }

                    sc.ExecuteCommand((int)SimpleServiceCustomCommands.StopWorker);
                    sc.ExecuteCommand((int)SimpleServiceCustomCommands.RestartWorker);

                    sc.Stop();
                    while (sc.Status != ServiceControllerStatus.Stopped)
                    {
                        Thread.Sleep(1000);
                        sc.Refresh();
                    }

                    Console.WriteLine("Status = " + sc.Status);
                    sc.Start();
                    while (sc.Status == ServiceControllerStatus.Stopped)
                    {
                        Thread.Sleep(1000);
                        sc.Refresh();
                    }
                    Console.WriteLine("Status = " + sc.Status);

                    Console.Write(scTemp.ServiceName.PadRight(30));
                    Console.WriteLine(("Status: " + scTemp.Status));

                    EventLog el = new EventLog();
                    el.Source = "MySource";
                    Console.WriteLine("Log: " + el.Log);
                    Console.WriteLine("LogName: " + el.LogDisplayName);

                    EventLogEntryCollection elec = el.Entries;
                    foreach (EventLogEntry ele in elec)
                    {
                        Console.Write(ele.Source + ": ");
                        Console.WriteLine(ele.Message);
                    }

                    ServiceControllerPermission permission = new ServiceControllerPermission();
                    Console.WriteLine("ServiceControllerPermission.Any: " + ServiceControllerPermission.Any);
                    Console.WriteLine("ServicecontrollerPermissions.Local: " + ServiceControllerPermission.Local);
                }
            }
        }
		public void FromXml_NoVersion ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
			SecurityElement se = scp.ToXml ();

			SecurityElement w = new SecurityElement (se.Tag);
			w.AddAttribute ("class", se.Attribute ("class"));
			scp.FromXml (w);
		}
		public void Union_Unrestricted ()
		{
			// Union with unrestricted is unrestricted
			ServiceControllerPermission scp1 = new ServiceControllerPermission (PermissionState.Unrestricted);
			foreach (ServiceControllerPermissionAccess scpa in AllAccess) {
				ServiceControllerPermission scp2 = new ServiceControllerPermission (PermissionState.None);
				scp2.PermissionEntries.Add (new ServiceControllerPermissionEntry (scpa, "localhost", scpa.ToString ()));
				ServiceControllerPermission union = (ServiceControllerPermission)scp1.Union (scp2);
				Assert.IsTrue (union.IsUnrestricted (), "target.IsUnrestricted " + scpa.ToString ());
				Assert.AreEqual (0, union.PermissionEntries.Count, "target.Count " + scpa.ToString ());

				union = (ServiceControllerPermission)scp2.Union (scp1);
				Assert.IsTrue (union.IsUnrestricted (), "source.IsUnrestricted " + scpa.ToString ());
				Assert.AreEqual (0, union.PermissionEntries.Count, "source.Count " + scpa.ToString ());
			}
		}
		public void PermissionState_Bad ()
		{
			PermissionState ps = (PermissionState)77;
			ServiceControllerPermission scp = new ServiceControllerPermission (ps);
			Assert.IsFalse (scp.IsUnrestricted (), "IsUnrestricted");
		}
		public void Union_BadPermission ()
		{
			ServiceControllerPermission scp1 = new ServiceControllerPermission (PermissionState.Unrestricted);
			scp1.Union (new SecurityPermission (SecurityPermissionFlag.Assertion));
		}
		public void PermissionEntries ()
		{
			ServiceControllerPermissionAccess scpa = ServiceControllerPermissionAccess.None;
			ServiceControllerPermission scp = new ServiceControllerPermission (scpa, "localhost", "http");
			ServiceControllerPermissionEntryCollection scpec = scp.PermissionEntries;
			Assert.AreEqual (1, scpec.Count, "Count==1");

			ServiceControllerPermissionEntry scpe = new ServiceControllerPermissionEntry (ServiceControllerPermissionAccess.Browse, "*", "ftp");
			scp.PermissionEntries.Add (scpe);
			Assert.AreEqual (2, scpec.Count, "Count==2");

			// remove (same instance)
			scp.PermissionEntries.Remove (scpe);
			Assert.AreEqual (1, scpec.Count, "Count==1 (b)");

			// remove different instance (doesn't work)
			scpe = new ServiceControllerPermissionEntry (ServiceControllerPermissionAccess.None, "localhost", "http");
			Assert.AreEqual (1, scpec.Count, "Count==1");
		}
		public void FromXml_Null ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
			scp.FromXml (null);
		}
		public void Union_Self ()
		{
			foreach (ServiceControllerPermissionAccess scpa in AllAccess) {
				ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
				scp.PermissionEntries.Add (new ServiceControllerPermissionEntry (scpa, "localhost", scpa.ToString ()));
				ServiceControllerPermission union = (ServiceControllerPermission)scp.Union (scp);
				Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted " + scpa.ToString ());
				Assert.AreEqual (1, union.PermissionEntries.Count, "Count " + scpa.ToString ());
			}
		}
		public void Intersect_Null ()
		{
			ServiceControllerPermission scp = new ServiceControllerPermission (PermissionState.None);
			// No intersection with null
			Assert.IsNull (scp.Intersect (null), "None N null");
		}