novi upstream verzije 2.8.3
[ossec-hids.git] / src / win32 / ui / os_win32ui.c
index 62a8e87..8b26954 100644 (file)
@@ -1,14 +1,15 @@
-/* @(#) $Id: os_win32ui.c,v 1.10 2009/06/24 18:53:10 dcid Exp $ */
+/* @(#) $Id: ./src/win32/ui/os_win32ui.c, 2011/09/08 dcid Exp $
+ */
 
 /* Copyright (C) 2009 Trend Micro Inc.
  * All rights reserved.
  *
  * This program is a free software; you can redistribute it
  * and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
  * Foundation.
  *
- * License details at the LICENSE file included with OSSEC or 
+ * License details at the LICENSE file included with OSSEC or
  * online at: http://www.ossec.net/en/licensing.html
  */
 
@@ -20,7 +21,7 @@
 
 
 /* Dialog -- About OSSEC */
-BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, 
+BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message,
                            WPARAM wParam, LPARAM lParam)
 {
     switch(Message)
@@ -53,7 +54,7 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
 {
     int ret_code = 0;
 
-    
+
     switch(Message)
     {
         case WM_INITDIALOG:
@@ -98,17 +99,16 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
 
 
             hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
-                    WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP, 
+                    WS_CHILD|WS_VISIBLE,
                     0, 0, 0, 0,
-                    hwnd, (HMENU)IDC_MAIN_STATUS, 
+                    hwnd, (HMENU)IDC_MAIN_STATUS,
                     GetModuleHandle(NULL), NULL);
 
-            SendMessage(hStatus, SB_SETPARTS, 
-                    sizeof(statwidths)/sizeof(int), 
+            SendMessage(hStatus, SB_SETPARTS,
+                    sizeof(statwidths)/sizeof(int),
                     (LPARAM)statwidths);
             SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
 
-           
 
             /* Initializing config */
             config_read(hwnd);
@@ -116,21 +116,21 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
 
 
             /* Setting the icons */
-            SendMessage(hwnd, WM_SETICON, ICON_SMALL, 
-                    (LPARAM)LoadIcon(GetModuleHandle(NULL), 
+            SendMessage(hwnd, WM_SETICON, ICON_SMALL,
+                    (LPARAM)LoadIcon(GetModuleHandle(NULL),
                                      MAKEINTRESOURCE(IDI_OSSECICON)));
-            SendMessage(hwnd, WM_SETICON, ICON_BIG, 
-                    (LPARAM)LoadIcon(GetModuleHandle(NULL), 
+            SendMessage(hwnd, WM_SETICON, ICON_BIG,
+                    (LPARAM)LoadIcon(GetModuleHandle(NULL),
                                      MAKEINTRESOURCE(IDI_OSSECICON)));
 
             if(config_inst.admin_access == 0)
             {
                 MessageBox(hwnd, "Admin access required. Some features may not work properly. \n\n"
                         "**If on Vista (or Server 2008), choose the \"Run as administrator\" option.",
-                        "Admin access required.", MB_OK);
+                        "Admin Access Required", MB_OK);
                 break;
             }
-                                                                                            
+
         }
         break;
 
@@ -152,10 +152,6 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                     break;
                 }
 
-                /** Getting values from the user (if chosen save) 
-                 * We should probably create another function for it...
-                 **/
-                
                 /* Getting server ip */
                 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_TEXT));
                 if(len > 0)
@@ -169,7 +165,7 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                     {
                         exit(-1);
                     }
-                    
+
                     GetDlgItemText(hwnd, UI_SERVER_TEXT, buf, len + 1);
 
                     /* If auth key changed, set it */
@@ -185,8 +181,8 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                         GlobalFree(buf);
                     }
                 }
-                
-                
+
+
                 /* Getting auth key */
                 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_AUTH));
                 if(len > 0)
@@ -213,7 +209,6 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                         char *name = NULL;
                         char *ip = NULL;
 
-
                         /* Getting new fields */
                         decd_buf = decode_base64(buf);
                         if(decd_buf)
@@ -224,8 +219,8 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                             id = decd_buf;
                             name = strchr(id, ' ');
                             if(name)
-                            { 
-                                *name = '\0'; 
+                            {
+                                *name = '\0';
                                 name++;
 
                                 ip = strchr(name, ' ');
@@ -249,35 +244,29 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                         if(!ip)
                         {
                             MessageBox(hwnd, "Unable to import "
-                                             "authentication key. Invalid.", 
-                                             "Error Saving.", MB_OK);
+                                             "authentication key because it was invalid.",
+                                             "Error -- Failure Saving Auth Key", MB_OK);
                         }
                         else
                         {
                             char mbox_msg[1024 +1];
                             mbox_msg[1024] = '\0';
-                            
+
                             snprintf(mbox_msg, 1024, "Adding key for:\r\n\r\n"
-                                               "Agent ID: %s\r\n" 
-                                               "Agent Name: %s\r\n" 
+                                               "Agent ID: %s\r\n"
+                                               "Agent Name: %s\r\n"
                                                "IP Address: %s\r\n",
                                                id, name, ip);
-                             
-                            ret = MessageBox(hwnd, mbox_msg, 
-                                         "Confirm Importing Key", MB_OKCANCEL);
+
+                            ret = MessageBox(hwnd, mbox_msg,
+                                             "Confirm Importing Key", MB_OKCANCEL);
                             if(ret == IDOK)
                             {
-                                FILE *fp;
-                                fp = fopen(AUTH_FILE, "w");
-                                if(fp)
+                                if(set_ossec_key(decd_to_write, hwnd))
                                 {
-                                    chd+=2;
-                                    fprintf(fp, "%s", decd_to_write);
-                                    fclose(fp);
+                                    chd += 2;
                                 }
                             }
-
-                            
                         }
 
                         /* Free used memory */
@@ -310,24 +299,24 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                     if(chd == 1)
                     {
                         SendMessage(hStatus, SB_SETTEXT, 0,
-                                (LPARAM)"Server IP Saved ..");
+                                (LPARAM)"Server IP saved");
                     }
                     else if(chd == 2)
                     {
                         SendMessage(hStatus, SB_SETTEXT, 0,
-                                (LPARAM)"Auth key imported ..");
+                                (LPARAM)"Auth key imported");
 
                     }
                     else
                     {
                         SendMessage(hStatus, SB_SETTEXT, 0,
-                                (LPARAM)"Auth key and server ip saved ..");
+                                (LPARAM)"Auth key and IP saved");
 
                     }
-                }         
+                }
             }
             break;
-            
+
             case UI_MENU_MANAGE_EXIT:
                 PostMessage(hwnd, WM_CLOSE, 0, 0);
                 break;
@@ -335,7 +324,7 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
             case UI_MENU_VIEW_LOGS:
                 _spawnlp( _P_NOWAIT, "notepad", "notepad " OSSECLOGS, NULL );
                 break;
-            case UI_MENU_VIEW_CONFIG:    
+            case UI_MENU_VIEW_CONFIG:
                 _spawnlp( _P_NOWAIT, "notepad", "notepad " CONFIG, NULL );
                 break;
             case UI_MENU_HELP_HELP:
@@ -343,17 +332,17 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                 break;
             case UI_MENU_HELP_ABOUT:
                 {
-                    DialogBox(GetModuleHandle(NULL), 
+                    DialogBox(GetModuleHandle(NULL),
                             MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
                 }
                 break;
             case IDC_CANCEL:
-                config_read(hwnd);    
+                config_read(hwnd);
                 gen_server_info(hwnd);
                 break;
-                
+
             case UI_MENU_MANAGE_START:
-            
+
                 /* Starting OSSEC  -- must have a valid config before. */
                 if((strcmp(config_inst.key, FL_NOKEY) != 0) &&
                    (strcmp(config_inst.server, FL_NOSERVER) != 0))
@@ -364,30 +353,30 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                 {
                     ret_code = 0;
                 }
-                
+
                 if(ret_code == 0)
                 {
-                    MessageBox(hwnd, "Unable to start OSSEC (check config).",
-                                     "Error -- Unable to start", MB_OK);
+                    MessageBox(hwnd, "Unable to start agent (check config)",
+                                     "Error -- Unable to Start Agent", MB_OK);
                 }
                 else if(ret_code == 1)
                 {
                     config_read(hwnd);
                     gen_server_info(hwnd);
 
-                    SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Started..");
+                    SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Started");
 
-                    MessageBox(hwnd, "OSSEC Agent Started.",
-                                     "Started..", MB_OK);
+                    MessageBox(hwnd, "Agent started",
+                                     "Agent Started", MB_OK);
                 }
                 else
                 {
-                    MessageBox(hwnd, "Agent already running (try restart).",
-                                     "Already running..", MB_OK);
+                    MessageBox(hwnd, "Agent already running (try restart)",
+                                     "Agent Running", MB_OK);
                 }
-                break;    
+                break;
             case UI_MENU_MANAGE_STOP:
-                
+
                 /* Stopping OSSEC */
                 ret_code = os_stop_service();
                 if(ret_code == 1)
@@ -395,66 +384,66 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
                     config_read(hwnd);
                     gen_server_info(hwnd);
 
-                    SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Stopped..");
-                    MessageBox(hwnd, "OSSEC Agent Stopped.",
-                                     "Stopped..", MB_OK);
+                    SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Stopped");
+                    MessageBox(hwnd, "Agent stopped",
+                                     "Agent Stopped", MB_OK);
                 }
                 else
                 {
-                    MessageBox(hwnd, "Agent already stopped.",
-                                     "Already stopped..", MB_OK);
+                    MessageBox(hwnd, "Agent already stopped",
+                                     "Agent Stopped", MB_OK);
                 }
                 break;
             case UI_MENU_MANAGE_STATUS:
                 if(CheckServiceRunning())
                 {
-                    MessageBox(hwnd, "OSSEC Agent running.",
-                                     "Agent running..", MB_OK);
+                    MessageBox(hwnd, "Agent running",
+                                     "Agent Running", MB_OK);
 
                 }
                 else
                 {
-                    MessageBox(hwnd, "OSSEC Agent stopped.",
-                                     "Agent stopped.", MB_OK);
+                    MessageBox(hwnd, "Agent stopped",
+                                     "Agent Stopped", MB_OK);
                 }
                 break;
             case UI_MENU_MANAGE_RESTART:
-                
+
                 if((strcmp(config_inst.key, FL_NOKEY) == 0) ||
                    (strcmp(config_inst.server, FL_NOSERVER) == 0))
                 {
-                    MessageBox(hwnd, "Unable to restart OSSEC (check config).",
-                                     "Error -- Unable to restart", MB_OK);
+                    MessageBox(hwnd, "Unable to restart agent (check config)",
+                                     "Error -- Unable to Restart Agent", MB_OK);
                     break;
-                    
+
                 }
-                                                                            
+
                 ret_code = os_stop_service();
-                
+
                 /* Starting OSSEC */
                 ret_code = os_start_service();
                 if(ret_code == 0)
                 {
-                    MessageBox(hwnd, "Unable to restart OSSEC (check config).",
-                                     "Error -- Unable to restart", MB_OK);
+                    MessageBox(hwnd, "Unable to restart agent (check config)",
+                                     "Error -- Unable to Restart Agent", MB_OK);
                 }
                 else
                 {
                     config_read(hwnd);
                     gen_server_info(hwnd);
 
-                    SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Restarted..");
-                    MessageBox(hwnd, "OSSEC Agent Restarted.",
-                                     "Restarted..", MB_OK);
+                    SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Restarted");
+                    MessageBox(hwnd, "Agent restarted",
+                                     "Agent Restarted", MB_OK);
                 }
-                break;        
+                break;
         }
         break;
-        
+
         case WM_CLOSE:
             EndDialog(hwnd, 0);
             break;
-        
+
         default:
             return FALSE;
     }
@@ -467,14 +456,11 @@ BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
     LPSTR lpCmdLine, int nCmdShow)
 {
-    int ret;
     WSADATA wsaData;
 
-
     /* Starting Winsock -- for name resolution. */
     WSAStartup(MAKEWORD(2, 0), &wsaData);
 
-
     /* Initializing config */
     init_config();
 
@@ -484,23 +470,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
     /* Creating main dialogbox */
     DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
 
-
-    /* Check if service is running and try to start it */
-    if((strcmp(config_inst.key, FL_NOKEY) != 0)&&
-            (strcmp(config_inst.server, FL_NOSERVER) != 0) &&
-            !CheckServiceRunning() &&
-            (config_inst.admin_access != 0))
-    {
-        ret = MessageBox(NULL, "OSSEC Agent not running. "
-                "Do you wish to start it?",
-                "Wish to start the agent?", MB_OKCANCEL);
-        if(ret == IDOK)
-        {
-            /* Starting the service */
-            os_start_service();
-        }
-    }
-
     return(0);
 }