Advertisement
Cukor

Untitled

Jun 5th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 17.73 KB | None | 0 0
  1. #r "System.ServiceProcess.dll"
  2. #r "System.Configuration.Install.dll"
  3.  
  4. open System
  5. open System.ServiceProcess
  6. open System.Threading
  7. open System.Runtime.InteropServices
  8.  
  9. module advapi32 =
  10.     module ACCESS_MASK =
  11.         let DELETE = uint32 0x00010000
  12.         let READ_CONTROL = uint32 0x00020000
  13.         let WRITE_DAC = uint32 0x00040000
  14.         let WRITE_OWNER = uint32 0x00080000
  15.         let SYNCHRONIZE = uint32 0x00100000
  16.  
  17.         let STANDARD_RIGHTS_REQUIRED = uint32 0x000F0000
  18.  
  19.         let STANDARD_RIGHTS_READ = uint32 0x00020000
  20.         let STANDARD_RIGHTS_WRITE = uint32 0x00020000
  21.         let STANDARD_RIGHTS_EXECUTE = uint32 0x00020000
  22.  
  23.         let STANDARD_RIGHTS_ALL = uint32 0x001F0000
  24.  
  25.         let SPECIFIC_RIGHTS_ALL = uint32 0x0000FFFF
  26.  
  27.         let ACCESS_SYSTEM_SECURITY = uint32 0x01000000
  28.  
  29.         let MAXIMUM_ALLOWED = uint32 0x02000000
  30.  
  31.         let GENERIC_READ = uint32 0x80000000
  32.         let GENERIC_WRITE = uint32 0x40000000
  33.         let GENERIC_EXECUTE = uint32 0x20000000
  34.         let GENERIC_ALL = uint32 0x10000000
  35.  
  36.         let DESKTOP_READOBJECTS = uint32 0x00000001
  37.         let DESKTOP_CREATEWINDOW = uint32 0x00000002
  38.         let DESKTOP_CREATEMENU = uint32 0x00000004
  39.         let DESKTOP_HOOKCONTROL = uint32 0x00000008
  40.         let DESKTOP_JOURNALRECORD = uint32 0x00000010
  41.         let DESKTOP_JOURNALPLAYBACK = uint32 0x00000020
  42.         let DESKTOP_ENUMERATE = uint32 0x00000040
  43.         let DESKTOP_WRITEOBJECTS = uint32 0x00000080
  44.         let DESKTOP_SWITCHDESKTOP = uint32 0x00000100
  45.  
  46.         let WINSTA_ENUMDESKTOPS = uint32 0x00000001
  47.         let WINSTA_READATTRIBUTES = uint32 0x00000002
  48.         let WINSTA_ACCESSCLIPBOARD = uint32 0x00000004
  49.         let WINSTA_CREATEDESKTOP = uint32 0x00000008
  50.         let WINSTA_WRITEATTRIBUTES = uint32 0x00000010
  51.         let WINSTA_ACCESSGLOBALATOMS = uint32 0x00000020
  52.         let WINSTA_EXITWINDOWS = uint32 0x00000040
  53.         let WINSTA_ENUMERATE = uint32 0x00000100
  54.         let WINSTA_READSCREEN = uint32 0x00000200
  55.  
  56.         let WINSTA_ALL_ACCESS = uint32 0x0000037F
  57.  
  58.  
  59.     module SERVICE_ACCESS =
  60.         /// <summary>
  61.         /// Required to call the QueryServiceConfig and
  62.         /// QueryServiceConfig2 functions to query the service configuration.
  63.         /// </summary>
  64.         let SERVICE_QUERY_CONFIG = uint32 0x00001
  65.  
  66.         /// <summary>
  67.         /// Required to call the ChangeServiceConfig or ChangeServiceConfig2 function
  68.         /// to change the service configuration. Because this grants the caller
  69.         /// the right to change the executable file that the system runs,
  70.         /// it should be granted only to administrators.
  71.         /// </summary>
  72.         let SERVICE_CHANGE_CONFIG = uint32 0x00002
  73.  
  74.         /// <summary>
  75.         /// Required to call the QueryServiceStatusEx function to ask the service
  76.         /// control manager about the status of the service.
  77.         /// </summary>
  78.         let SERVICE_QUERY_STATUS = uint32 0x00004
  79.  
  80.         /// <summary>
  81.         /// Required to call the EnumDependentServices function to enumerate all
  82.         /// the services dependent on the service.
  83.         /// </summary>
  84.         let SERVICE_ENUMERATE_DEPENDENTS = uint32 0x00008
  85.  
  86.         /// <summary>
  87.         /// Required to call the StartService function to start the service.
  88.         /// </summary>
  89.         let SERVICE_START = uint32 0x00010
  90.  
  91.         /// <summary>
  92.         ///     Required to call the ControlService function to stop the service.
  93.         /// </summary>
  94.         let SERVICE_STOP = uint32 0x00020
  95.  
  96.         /// <summary>
  97.         /// Required to call the ControlService function to pause or continue
  98.         /// the service.
  99.         /// </summary>
  100.         let SERVICE_PAUSE_CONTINUE = uint32 0x00040
  101.  
  102.         /// <summary>
  103.         /// Required to call the EnumDependentServices function to enumerate all
  104.         /// the services dependent on the service.
  105.         /// </summary>
  106.         let SERVICE_INTERROGATE = uint32 0x00080
  107.  
  108.         /// <summary>
  109.         /// Required to call the ControlService function to specify a user-defined
  110.         /// control code.
  111.         /// </summary>
  112.         let SERVICE_USER_DEFINED_CONTROL = uint32 0x00100
  113.  
  114.         /// <summary>
  115.         /// Includes STANDARD_RIGHTS_REQUIRED in addition to all access rights in this table.
  116.         /// </summary>
  117.         let SERVICE_ALL_ACCESS =
  118.             uint32 (
  119.                 ACCESS_MASK.STANDARD_RIGHTS_REQUIRED |||
  120.                 SERVICE_QUERY_CONFIG |||
  121.                 SERVICE_CHANGE_CONFIG |||
  122.                 SERVICE_QUERY_STATUS |||
  123.                 SERVICE_ENUMERATE_DEPENDENTS |||
  124.                 SERVICE_START |||
  125.                 SERVICE_STOP |||
  126.                 SERVICE_PAUSE_CONTINUE |||
  127.                 SERVICE_INTERROGATE |||
  128.                 SERVICE_USER_DEFINED_CONTROL)
  129.  
  130.         let GENERIC_READ =
  131.             uint32 (
  132.                 ACCESS_MASK.STANDARD_RIGHTS_READ |||
  133.                 SERVICE_QUERY_CONFIG |||
  134.                 SERVICE_QUERY_STATUS |||
  135.                 SERVICE_INTERROGATE |||
  136.                 SERVICE_ENUMERATE_DEPENDENTS)
  137.  
  138.         let GENERIC_WRITE =
  139.             uint32 (
  140.                 ACCESS_MASK.STANDARD_RIGHTS_WRITE |||
  141.                 SERVICE_CHANGE_CONFIG)
  142.  
  143.         let GENERIC_EXECUTE =
  144.             uint32 (
  145.                 ACCESS_MASK.STANDARD_RIGHTS_EXECUTE |||
  146.                 SERVICE_START |||
  147.                 SERVICE_STOP |||
  148.                 SERVICE_PAUSE_CONTINUE |||
  149.                 SERVICE_USER_DEFINED_CONTROL )
  150.  
  151.         /// <summary>
  152.         /// Required to call the QueryServiceObjectSecurity or
  153.         /// SetServiceObjectSecurity function to access the SACL. The proper
  154.         /// way to obtain this access is to enable the SE_SECURITY_NAME
  155.         /// privilege in the caller's current access token, open the handle
  156.         /// for ACCESS_SYSTEM_SECURITY access, and then disable the privilege.
  157.         /// </summary>
  158.         let ACCESS_SYSTEM_SECURITY = uint32 ACCESS_MASK.ACCESS_SYSTEM_SECURITY
  159.  
  160.         /// <summary>
  161.         /// Required to call the DeleteService function to delete the service.
  162.         /// </summary>
  163.         let DELETE = uint32 ACCESS_MASK.DELETE
  164.  
  165.         /// <summary>
  166.         /// Required to call the QueryServiceObjectSecurity function to query
  167.         /// the security descriptor of the service object.
  168.         /// </summary>
  169.         let READ_CONTROL = uint32 ACCESS_MASK.READ_CONTROL
  170.  
  171.         /// <summary>
  172.         /// Required to call the SetServiceObjectSecurity function to modify
  173.         /// the Dacl member of the service object's security descriptor.
  174.         /// </summary>
  175.         let WRITE_DAC = uint32 ACCESS_MASK.WRITE_DAC
  176.  
  177.         /// <summary>
  178.         /// Required to call the SetServiceObjectSecurity function to modify
  179.         /// the Owner and Group members of the service object's security
  180.         /// descriptor.
  181.         /// </summary>
  182.         let WRITE_OWNER = uint32 ACCESS_MASK.WRITE_OWNER
  183.  
  184.     module SERVICE_TYPE =
  185.  
  186.         /// <summary>
  187.         /// Driver service.
  188.         /// </summary>
  189.         let SERVICE_KERNEL_DRIVER = uint32 0x00000001
  190.  
  191.         /// <summary>
  192.         /// File system driver service.
  193.         /// </summary>
  194.         let SERVICE_FILE_SYSTEM_DRIVER = uint32 0x00000002
  195.  
  196.         /// <summary>
  197.         /// Service that runs in its own process.
  198.         /// </summary>
  199.         let SERVICE_WIN32_OWN_PROCESS = uint32 0x00000010
  200.  
  201.         /// <summary>
  202.         /// Service that shares a process with one or more other services.
  203.         /// </summary>
  204.         let SERVICE_WIN32_SHARE_PROCESS = uint32 0x00000020
  205.  
  206.         /// <summary>
  207.         /// The service can interact with the desktop.
  208.         /// </summary>
  209.         let SERVICE_INTERACTIVE_PROCESS = uint32 0x00000100
  210.  
  211.  
  212.     /// <summary>
  213.     /// Service start options
  214.     /// </summary>
  215.     module SERVICE_START =  
  216.         /// <summary>
  217.         /// A device driver started by the system loader. This value is valid
  218.         /// only for driver services.
  219.         /// </summary>
  220.         let SERVICE_BOOT_START = uint32 0x00000000
  221.  
  222.         /// <summary>
  223.         /// A device driver started by the IoInitSystem function. This value
  224.         /// is valid only for driver services.
  225.         /// </summary>
  226.         let SERVICE_SYSTEM_START = uint32 0x00000001
  227.  
  228.         /// <summary>
  229.         /// A service started automatically by the service control manager
  230.         /// during system startup. For more information, see Automatically
  231.         /// Starting Services.
  232.         /// </summary>        
  233.         let SERVICE_AUTO_START = uint32 0x00000002
  234.  
  235.         /// <summary>
  236.         /// A service started by the service control manager when a process
  237.         /// calls the StartService function. For more information, see
  238.         /// Starting Services on Demand.
  239.         /// </summary>
  240.         let SERVICE_DEMAND_START = uint32 0x00000003
  241.  
  242.         /// <summary>
  243.         /// A service that cannot be started. Attempts to start the service
  244.         /// result in the error code ERROR_SERVICE_DISABLED.
  245.         /// </summary>
  246.         let SERVICE_DISABLED = uint32 0x00000004      
  247.  
  248.  
  249.     /// <summary>
  250.     /// Severity of the error, and action taken, if this service fails
  251.     /// to start.
  252.     /// </summary>
  253.     module SERVICE_ERROR =
  254.         /// <summary>
  255.         /// The startup program ignores the error and continues the startup
  256.         /// operation.
  257.         /// </summary>
  258.         let SERVICE_ERROR_IGNORE = uint32 0x00000000
  259.  
  260.         /// <summary>
  261.         /// The startup program logs the error in the event log but continues
  262.         /// the startup operation.
  263.         /// </summary>
  264.         let SERVICE_ERROR_NORMAL = uint32 0x00000001
  265.  
  266.         /// <summary>
  267.         /// The startup program logs the error in the event log. If the
  268.         /// last-known-good configuration is being started, the startup
  269.         /// operation continues. Otherwise, the system is restarted with
  270.         /// the last-known-good configuration.
  271.         /// </summary>
  272.         let SERVICE_ERROR_SEVERE = uint32 0x00000002
  273.  
  274.         /// <summary>
  275.         /// The startup program logs the error in the event log, if possible.
  276.         /// If the last-known-good configuration is being started, the startup
  277.         /// operation fails. Otherwise, the system is restarted with the
  278.         /// last-known good configuration.
  279.         /// </summary>
  280.         let SERVICE_ERROR_CRITICAL = uint32 0x00000003
  281.  
  282.     module ServiceManagerRights =
  283.         /// <summary>
  284.         ///
  285.         /// </summary>
  286.         let Connect = unativeint 0x00001
  287.         /// <summary>
  288.         ///
  289.         /// </summary>
  290.         let CreateService = unativeint 0x00002
  291.         /// <summary>
  292.         ///
  293.         /// </summary>
  294.         let EnumerateService = unativeint 0x00004
  295.         /// <summary>
  296.         ///
  297.         /// </summary>
  298.         let Lock = unativeint 0x0008
  299.         /// <summary>
  300.         ///
  301.         /// </summary>
  302.         let QueryLockStatus = unativeint 0x00010
  303.         /// <summary>
  304.         ///
  305.         /// </summary>
  306.         let ModifyBootConfig = unativeint 0x00020
  307.         /// <summary>
  308.         ///
  309.         /// </summary>
  310.         let StandardRightsRequired = unativeint 0xF0000
  311.         /// <summary>
  312.         ///
  313.         /// </summary>
  314.         let AllAccess =
  315.             unativeint (StandardRightsRequired ||| Connect ||| CreateService |||
  316.                 EnumerateService ||| Lock ||| QueryLockStatus ||| ModifyBootConfig)
  317.  
  318.     /// <summary>
  319.     ///
  320.     /// </summary>
  321.     module ServiceState =
  322.         /// <summary>
  323.         ///
  324.         /// </summary>
  325.         let Unknown = -1 // The state cannot be (has not been) retrieved.
  326.         /// <summary>
  327.         ///
  328.         /// </summary>
  329.         let NotFound = 0 // The service is not known on the host server.
  330.         /// <summary>
  331.         ///
  332.         /// </summary>
  333.         let Stop = 1 // The service is NET stopped.
  334.         /// <summary>
  335.         ///
  336.         /// </summary>
  337.         let Run = 2 // The service is NET started.
  338.         /// <summary>
  339.         ///
  340.         /// </summary>
  341.         let Stopping = 3
  342.         /// <summary>
  343.         ///
  344.         /// </summary>
  345.         let Starting = 4
  346.  
  347.     /// <summary>
  348.     ///
  349.     /// </summary>
  350.     module ServiceControl =
  351.         /// <summary>
  352.         ///
  353.         /// </summary>
  354.         let Stop = 0x00000001
  355.         /// <summary>
  356.         ///
  357.         /// </summary>
  358.         let Pause = 0x00000002
  359.         /// <summary>
  360.         ///
  361.         /// </summary>
  362.         let Continue = 0x00000003
  363.         /// <summary>
  364.         ///
  365.         /// </summary>
  366.         let Interrogate = 0x00000004
  367.         /// <summary>
  368.         ///
  369.         /// </summary>
  370.         let Shutdown = 0x00000005
  371.         /// <summary>
  372.         ///
  373.         /// </summary>
  374.         let ParamChange = 0x00000006
  375.         /// <summary>
  376.         ///
  377.         /// </summary>
  378.         let NetBindAdd = 0x00000007
  379.         /// <summary>
  380.         ///
  381.         /// </summary>
  382.         let NetBindRemove = 0x00000008
  383.         /// <summary>
  384.         ///
  385.         /// </summary>
  386.         let NetBindEnable = 0x00000009
  387.         /// <summary>
  388.         ///
  389.         /// </summary>
  390.         let NetBindDisable = 0x0000000A
  391.  
  392.  
  393.     [<StructLayout(LayoutKind.Sequential)>]
  394.     type SERVICE_STATUS =
  395.         struct
  396.             [<DefaultValue>] val mutable dwServiceType: int
  397.             [<DefaultValue>] val mutable dwCurrentState: int
  398.             [<DefaultValue>] val mutable dwControlsAccepted: int
  399.             [<DefaultValue>] val mutable dwWin32ExitCode: int
  400.             [<DefaultValue>] val mutable dwServiceSpecificExitCode: int
  401.             [<DefaultValue>] val mutable dwCheckPoint: int
  402.             [<DefaultValue>] val mutable dwWaitHint: int
  403.         end                                
  404.  
  405.     [<DllImport("advapi32.dll", EntryPoint="OpenSCManagerW", CharSet=CharSet.Unicode, SetLastError=true)>]
  406.     extern IntPtr OpenSCManager(
  407.         string lpMachineName,
  408.         string lpDatabaseName,
  409.         unativeint dwDesiredAccess);
  410.  
  411.     [<DllImport("advapi32.dll", EntryPoint = "OpenServiceW", SetLastError=true, CharSet = CharSet.Unicode)>]
  412.     extern IntPtr OpenService(
  413.         IntPtr hSCManager,
  414.         string lpServiceName,
  415.         int dwDesiredAccess);
  416.  
  417.     [<DllImport(@"advapi32.dll", EntryPoint = "CreateServiceW", SetLastError=true, CharSet=CharSet.Unicode)>]
  418.     extern IntPtr CreateService(
  419.         IntPtr hSCManager,
  420.         string lpServiceName,
  421.         string lpDisplayName,
  422.         uint32 dwDesiredAccess,
  423.         uint32 dwServiceType,
  424.         uint32 dwStartType,
  425.         uint32 dwErrorControl,
  426.         string lpBinaryPathName,
  427.         string lpLoadOrderGroup,
  428.         string lpdwTagId,
  429.         string lpDependencies,
  430.         string lpServiceStartName,
  431.         string lpPassword);
  432.  
  433.  
  434.     [<DllImport("advapi32.dll", SetLastError=true)>]
  435.     extern int CloseServiceHandle(IntPtr hSCObject);
  436.     [<DllImport("advapi32.dll", SetLastError=true)>]
  437.     extern int QueryServiceStatus(
  438.         IntPtr hService,
  439.         SERVICE_STATUS lpServiceStatus);
  440.     [<DllImport("advapi32.dll", SetLastError = true)>]
  441.     extern int DeleteService(IntPtr hService);
  442.     [<DllImport("advapi32.dll", SetLastError=true)>]
  443.     extern int ControlService(
  444.         IntPtr hService,
  445.         int dwControl,
  446.         SERVICE_STATUS lpServiceStatus);
  447.     [<DllImport("advapi32.dll", EntryPoint = "StartServiceW", CharSet=CharSet.Unicode , SetLastError=true)>]
  448.     extern int StartService(
  449.         IntPtr hService,
  450.         int dwNumServiceArgs,
  451.         int lpServiceArgVectors);
  452.  
  453.  
  454. module ServiceControl =
  455.     open advapi32
  456.     /// <summary>
  457.     /// Takes a service name, a service display name and the path to the service executable and installs / starts the windows service.
  458.     /// </summary>
  459.     /// <param name="ServiceName">The service name that this service will have</param>
  460.     /// <param name="DisplayName">The display name that this service will have</param>
  461.     /// <param name="FileName">The path to the executable of the service</param>
  462.     /// <param name="UserName">Account name</param>
  463.     /// <param name="Password">Password</param>
  464.  
  465.     let Install
  466.         (ServiceName: string)
  467.         (DisplayName: string)
  468.         (FileName: string)
  469.         (UserName: string)
  470.         (Password: string) =
  471.             //ServiceManagerRights.CreateService will need elevation
  472.             let scman: IntPtr = OpenSCManager(null, null, unativeint(ServiceManagerRights.Connect ||| ServiceManagerRights.EnumerateService ||| ServiceManagerRights.CreateService));
  473.             try
  474.                 if scman = IntPtr.Zero then failwithf "OpenSCManager: %d" (Marshal.GetLastWin32Error())
  475.  
  476.                 let mutable service: IntPtr = OpenService(scman, ServiceName, int (SERVICE_ACCESS.SERVICE_QUERY_STATUS ||| SERVICE_ACCESS.SERVICE_START));
  477.                 if service = IntPtr.Zero then
  478.                     service <- CreateService(
  479.                         scman,
  480.                         ServiceName,
  481.                         DisplayName,
  482.                         SERVICE_ACCESS.SERVICE_QUERY_STATUS ||| SERVICE_ACCESS.SERVICE_START,
  483.                         SERVICE_TYPE.SERVICE_WIN32_OWN_PROCESS,
  484.                         SERVICE_START.SERVICE_AUTO_START,
  485.                         SERVICE_ERROR.SERVICE_ERROR_NORMAL,
  486.                         FileName,
  487.                         null,
  488.                         null,
  489.                         null,
  490.                         UserName,
  491.                         Password)
  492.                 if service = IntPtr.Zero then failwithf "CreateService: %d" (Marshal.GetLastWin32Error())
  493.                 service
  494.             finally
  495.                 CloseServiceHandle(scman) |> ignore
  496.  
  497. ServiceControl.InstallAndStart "lol" "lol" "lol" "Cukor" "tvojamatka";;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement