new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / win32 / win_service.c
old mode 100755 (executable)
new mode 100644 (file)
index c2463d5..b7451f9
@@ -1,6 +1,3 @@
-/* @(#) $Id: ./src/win32/win_service.c, 2011/09/08 dcid Exp $
- */
-
 /* Copyright (C) 2009 Trend Micro Inc.
  * All rights reserved.
  *
@@ -8,12 +5,8 @@
  * and/or modify it under the terms of the GNU General Public
  * License (version 2) as published by the FSF - Free Software
  * Foundation.
- *
- * License details at the LICENSE file included with OSSEC or
- * online at: http://www.ossec.net/en/licensing.html
  */
 
-
 #ifdef WIN32
 
 #include "shared.h"
@@ -31,34 +24,25 @@ static LPTSTR g_lpszServiceDescription = "OSSEC HIDS Windows Agent";
 static SERVICE_STATUS          ossecServiceStatus;
 static SERVICE_STATUS_HANDLE   ossecServiceStatusHandle;
 
-/* ServiceStart */
 void WINAPI OssecServiceStart (DWORD argc, LPTSTR *argv);
 
 
-
-/* os_start_service: Starts ossec service */
+/* Start OSSEC-HIDS service */
 int os_start_service()
 {
     int rc = 0;
     SC_HANDLE schSCManager, schService;
 
-
     /* Start the database */
     schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
-    if (schSCManager)
-    {
-        schService = OpenService(schSCManager,g_lpszServiceName,
+    if (schSCManager) {
+        schService = OpenService(schSCManager, g_lpszServiceName,
                                  SC_MANAGER_ALL_ACCESS);
-        if(schService)
-        {
-            if(StartService(schService, 0, NULL))
-            {
+        if (schService) {
+            if (StartService(schService, 0, NULL)) {
                 rc = 1;
-            }
-            else
-            {
-                if(GetLastError() == ERROR_SERVICE_ALREADY_RUNNING)
-                {
+            } else {
+                if (GetLastError() == ERROR_SERVICE_ALREADY_RUNNING) {
                     rc = -1;
                 }
             }
@@ -69,29 +53,24 @@ int os_start_service()
         CloseServiceHandle(schSCManager);
     }
 
-    return(rc);
+    return (rc);
 }
 
-
-/* os_stop_service: Stops ossec service */
+/* Stop OSSEC-HIDS service */
 int os_stop_service()
 {
     int rc = 0;
     SC_HANDLE schSCManager, schService;
 
-
     /* Stop the service database */
     schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
-    if (schSCManager)
-    {
-        schService = OpenService(schSCManager,g_lpszServiceName,
+    if (schSCManager) {
+        schService = OpenService(schSCManager, g_lpszServiceName,
                                  SC_MANAGER_ALL_ACCESS);
-        if(schService)
-        {
+        if (schService) {
             SERVICE_STATUS lpServiceStatus;
 
-            if(ControlService(schService, SERVICE_CONTROL_STOP, &lpServiceStatus))
-            {
+            if (ControlService(schService, SERVICE_CONTROL_STOP, &lpServiceStatus)) {
                 rc = 1;
             }
 
@@ -101,32 +80,28 @@ int os_stop_service()
         CloseServiceHandle(schSCManager);
     }
 
-    return(rc);
+    return (rc);
 }
 
-
-/* int CheckServiceRunning(): Checks if service is running. */
+/* Check if the OSSEC-HIDS agent service is running
+ * Returns 1 on success (running) or 0 if not running
+ */
 int CheckServiceRunning()
 {
     int rc = 0;
     SC_HANDLE schSCManager, schService;
 
-
-    /* Checking service status */
+    /* Check service status */
     schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
-    if (schSCManager)
-    {
-        schService = OpenService(schSCManager,g_lpszServiceName,
+    if (schSCManager) {
+        schService = OpenService(schSCManager, g_lpszServiceName,
                                  SC_MANAGER_ALL_ACCESS);
-        if(schService)
-        {
-            /* Checking status */
+        if (schService) {
+            /* Check status */
             SERVICE_STATUS lpServiceStatus;
 
-            if(QueryServiceStatus(schService, &lpServiceStatus))
-            {
-                if(lpServiceStatus.dwCurrentState == SERVICE_RUNNING)
-                {
+            if (QueryServiceStatus(schService, &lpServiceStatus)) {
+                if (lpServiceStatus.dwCurrentState == SERVICE_RUNNING) {
                     rc = 1;
                 }
             }
@@ -136,44 +111,34 @@ int CheckServiceRunning()
         CloseServiceHandle(schSCManager);
     }
 
-    return(rc);
+    return (rc);
 }
 
-
-/* int InstallService()
- * Install the OSSEC HIDS agent service.
- */
+/* Install the OSSEC-HIDS agent service */
 int InstallService(char *path)
 {
     int ret;
-
     SC_HANDLE schSCManager, schService;
     LPCTSTR lpszBinaryPathName = NULL;
     SERVICE_DESCRIPTION sdBuf;
 
-
     /* Uninstall service (if it exists) */
-    if (!UninstallService())
-    {
+    if (!UninstallService()) {
         verbose("%s: ERROR: Failure running UninstallService().", ARGV0);
-        return(0);
+        return (0);
     }
 
-
-    /* Executable path -- it must be called with the
-     * full path
-     */
+    /* Executable path -- it must be called with the full path */
     lpszBinaryPathName = path;
 
     /* Opening the service database */
-    schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
+    schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
 
-    if (schSCManager == NULL)
-    {
+    if (schSCManager == NULL) {
         goto install_error;
     }
 
-    /* Creating the service */
+    /* Create the service */
     schService = CreateService(schSCManager,
                                g_lpszServiceName,
                                g_lpszServiceDisplayName,
@@ -184,13 +149,12 @@ int InstallService(char *path)
                                lpszBinaryPathName,
                                NULL, NULL, NULL, NULL, NULL);
 
-    if (schService == NULL)
-    {
+    if (schService == NULL) {
         CloseServiceHandle(schSCManager);
         goto install_error;
     }
 
-    /* Setting description */
+    /* Set description */
     sdBuf.lpDescription = g_lpszServiceDescription;
     ret = ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &sdBuf);
 
@@ -198,18 +162,14 @@ int InstallService(char *path)
     CloseServiceHandle(schSCManager);
 
     /* Check for errors */
-    if (!ret)
-    {
+    if (!ret) {
         goto install_error;
     }
 
-
     verbose("%s: INFO: Successfully added to the service database.", ARGV0);
-    return(1);
-
+    return (1);
 
-    install_error:
-    {
+install_error: {
         char local_msg[1025];
         LPVOID lpMsgBuf;
 
@@ -226,14 +186,11 @@ int InstallService(char *path)
                        NULL);
 
         verbose("%s: ERROR: Unable to create service entry: %s", ARGV0, (LPCTSTR)lpMsgBuf);
-        return(0);
+        return (0);
     }
 }
 
-
-/* int UninstallService()
- * Uninstall the OSSEC HIDS agent service.
- */
+/* Uninstall the OSSEC-HIDS agent service */
 int UninstallService()
 {
     int ret;
@@ -241,65 +198,47 @@ int UninstallService()
     SC_HANDLE schSCManager, schService;
     SERVICE_STATUS lpServiceStatus;
 
-
-    /* Removing from the service database */
+    /* Remove from the service database */
     schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
-    if(schSCManager)
-    {
-        schService = OpenService(schSCManager,g_lpszServiceName,SERVICE_STOP|DELETE);
-        if(schService)
-        {
-            if(CheckServiceRunning())
-            {
+    if (schSCManager) {
+        schService = OpenService(schSCManager, g_lpszServiceName, SERVICE_STOP | DELETE);
+        if (schService) {
+            if (CheckServiceRunning()) {
                 verbose("%s: INFO: Found (%s) service is running going to try and stop it.", ARGV0, g_lpszServiceName);
                 ret = ControlService(schService, SERVICE_CONTROL_STOP, &lpServiceStatus);
-                if(!ret)
-                {
+                if (!ret) {
                     verbose("%s: ERROR: Failure stopping service (%s) before removing it (%ld).", ARGV0, g_lpszServiceName, GetLastError());
-                }
-                else
-                {
+                } else {
                     verbose("%s: INFO: Successfully stopped (%s).", ARGV0, g_lpszServiceName);
                 }
-            }
-            else
-            {
+            } else {
                 verbose("%s: INFO: Found (%s) service is not running.", ARGV0, g_lpszServiceName);
                 ret = 1;
             }
 
-            if(ret && DeleteService(schService))
-            {
+            if (ret && DeleteService(schService)) {
                 verbose("%s: INFO: Successfully removed (%s) from the service database.", ARGV0, g_lpszServiceName);
                 rc = 1;
             }
             CloseServiceHandle(schService);
-        }
-        else
-        {
-                verbose("%s: INFO: Service does not exist (%s) nothing to remove.", ARGV0, g_lpszServiceName);
-                rc = 1;
+        } else {
+            verbose("%s: INFO: Service does not exist (%s) nothing to remove.", ARGV0, g_lpszServiceName);
+            rc = 1;
         }
         CloseServiceHandle(schSCManager);
     }
 
-    if(!rc)
-    {
+    if (!rc) {
         verbose("%s: ERROR: Failure removing (%s) from the service database.", ARGV0, g_lpszServiceName);
     }
 
-    return(rc);
+    return (rc);
 }
 
-
-
-/** VOID WINAPI OssecServiceCtrlHandler (DWORD dwOpcode)
- * "Signal" handler
- */
+/* "Signal" handler */
 VOID WINAPI OssecServiceCtrlHandler(DWORD dwOpcode)
 {
-    switch(dwOpcode)
-    {
+    switch (dwOpcode) {
         case SERVICE_CONTROL_STOP:
             ossecServiceStatus.dwCurrentState           = SERVICE_STOPPED;
             ossecServiceStatus.dwWin32ExitCode          = 0;
@@ -316,47 +255,36 @@ VOID WINAPI OssecServiceCtrlHandler(DWORD dwOpcode)
     return;
 }
 
-
-/** void WinSetError()
- * Sets the error code in the service
- */
+/* Set the error code in the service */
 void WinSetError()
 {
     OssecServiceCtrlHandler(SERVICE_CONTROL_STOP);
 }
 
-
-/** int os_WinMain(int argc, char **argv)
- * Initializes OSSEC dispatcher
- */
-int os_WinMain(int argc, char **argv)
+/* Initialize OSSEC-HIDS dispatcher */
+int os_WinMain(__attribute__((unused)) int argc, __attribute__((unused)) char **argv)
 {
-    SERVICE_TABLE_ENTRY   steDispatchTable[] =
-    {
+    SERVICE_TABLE_ENTRY   steDispatchTable[] = {
         { g_lpszServiceName, OssecServiceStart },
         { NULL,       NULL                     }
     };
 
-    if(!StartServiceCtrlDispatcher(steDispatchTable))
-    {
+    if (!StartServiceCtrlDispatcher(steDispatchTable)) {
         verbose("%s: INFO: Unable to set service information.", ARGV0);
-        return(1);
+        return (1);
     }
 
-    return(1);
+    return (1);
 }
 
-
-/** void WINAPI OssecServiceStart (DWORD argc, LPTSTR *argv)
- * Starts OSSEC service
- */
-void WINAPI OssecServiceStart (DWORD argc, LPTSTR *argv)
+/* Start OSSEC service */
+void WINAPI OssecServiceStart (__attribute__((unused)) DWORD argc, __attribute__((unused)) LPTSTR *argv)
 {
     ossecServiceStatus.dwServiceType            = SERVICE_WIN32;
     ossecServiceStatus.dwCurrentState           = SERVICE_START_PENDING;
     ossecServiceStatus.dwControlsAccepted       = SERVICE_ACCEPT_STOP;
     ossecServiceStatus.dwWin32ExitCode          = 0;
-    ossecServiceStatus.dwServiceSpecificExitCode= 0;
+    ossecServiceStatus.dwServiceSpecificExitCode = 0;
     ossecServiceStatus.dwCheckPoint             = 0;
     ossecServiceStatus.dwWaitHint               = 0;
 
@@ -364,8 +292,7 @@ void WINAPI OssecServiceStart (DWORD argc, LPTSTR *argv)
         RegisterServiceCtrlHandler(g_lpszServiceName,
                                    OssecServiceCtrlHandler);
 
-    if (ossecServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
-    {
+    if (ossecServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) {
         verbose("%s: INFO: RegisterServiceCtrlHandler failed.", ARGV0);
         return;
     }
@@ -374,19 +301,15 @@ void WINAPI OssecServiceStart (DWORD argc, LPTSTR *argv)
     ossecServiceStatus.dwCheckPoint = 0;
     ossecServiceStatus.dwWaitHint = 0;
 
-    if (!SetServiceStatus(ossecServiceStatusHandle, &ossecServiceStatus))
-    {
+    if (!SetServiceStatus(ossecServiceStatusHandle, &ossecServiceStatus)) {
         verbose("%s: INFO: SetServiceStatus error.", ARGV0);
         return;
     }
 
-
-    #ifdef OSSECHIDS
-    /* Starting process */
+#ifdef OSSECHIDS
+    /* Start process */
     local_start();
-    #endif
+#endif
 }
 
-
-#endif
-/* EOF */
+#endif /* WIN32 */