1 /* @(#) $Id: ./src/win32/ui/os_win32ui.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/en/licensing.html
18 #include "os_win32ui.h"
23 /* Dialog -- About OSSEC */
24 BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message,
25 WPARAM wParam, LPARAM lParam)
34 switch(LOWORD(wParam))
37 EndDialog(hwnd, IDOK);
43 EndDialog(hwnd, IDOK);
53 BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
62 int statwidths[] = {130, -1};
63 HMENU hMenu, hSubMenu;
65 UINT menuflags = MF_STRING;
67 if(config_inst.admin_access == 0)
69 menuflags = MF_STRING|MF_GRAYED;
74 /* Creating management menu */
75 hSubMenu = CreatePopupMenu();
76 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_START,"&Start OSSEC");
77 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_STOP,"&Stop OSSEC");
78 AppendMenu(hSubMenu, MF_SEPARATOR, UI_MENU_NONE,"");
79 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_RESTART,"&Restart");
80 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_STATUS,"&Status");
81 AppendMenu(hSubMenu, MF_SEPARATOR, UI_MENU_NONE,"");
82 AppendMenu(hSubMenu, MF_STRING,UI_MENU_MANAGE_EXIT,"&Exit");
83 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu,"&Manage");
85 /* Create view menu */
86 hSubMenu = CreatePopupMenu();
87 AppendMenu(hSubMenu, MF_STRING, UI_MENU_VIEW_LOGS, "&View Logs");
88 AppendMenu(hSubMenu, MF_STRING, UI_MENU_VIEW_CONFIG,"V&iew Config");
89 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu,"&View");
91 hSubMenu = CreatePopupMenu();
92 AppendMenu(hSubMenu, MF_STRING, UI_MENU_HELP_ABOUT, "A&bout");
93 AppendMenu(hSubMenu, MF_STRING, UI_MENU_HELP_HELP, "Help");
94 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
97 AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
101 hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
104 hwnd, (HMENU)IDC_MAIN_STATUS,
105 GetModuleHandle(NULL), NULL);
107 SendMessage(hStatus, SB_SETPARTS,
108 sizeof(statwidths)/sizeof(int),
110 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
113 /* Initializing config */
115 gen_server_info(hwnd);
118 /* Setting the icons */
119 SendMessage(hwnd, WM_SETICON, ICON_SMALL,
120 (LPARAM)LoadIcon(GetModuleHandle(NULL),
121 MAKEINTRESOURCE(IDI_OSSECICON)));
122 SendMessage(hwnd, WM_SETICON, ICON_BIG,
123 (LPARAM)LoadIcon(GetModuleHandle(NULL),
124 MAKEINTRESOURCE(IDI_OSSECICON)));
126 if(config_inst.admin_access == 0)
128 MessageBox(hwnd, "Admin access required. Some features may not work properly. \n\n"
129 "**If on Vista (or Server 2008), choose the \"Run as administrator\" option.",
130 "Admin Access Required", MB_OK);
138 switch(LOWORD(wParam))
140 /* In case of SAVE */
147 if(config_inst.admin_access == 0)
149 MessageBox(hwnd, "Unable to edit configuration. "
150 "Admin access required.",
151 "Error Saving.", MB_OK);
155 /* Getting server ip */
156 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_TEXT));
162 /* Allocating buffer */
163 buf = (char*)GlobalAlloc(GPTR, len + 1);
169 GetDlgItemText(hwnd, UI_SERVER_TEXT, buf, len + 1);
171 /* If auth key changed, set it */
172 if(strcmp(buf, config_inst.server) != 0)
174 if(set_ossec_server(buf, hwnd))
186 /* Getting auth key */
187 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_AUTH));
192 /* Allocating buffer */
193 buf = (char*)GlobalAlloc(GPTR, len + 1);
199 GetDlgItemText(hwnd, UI_SERVER_AUTH, buf, len + 1);
201 /* If auth key changed, set it */
202 if(strcmp(buf, config_inst.key) != 0)
206 char *decd_buf = NULL;
207 char *decd_to_write = NULL;
212 /* Getting new fields */
213 decd_buf = decode_base64(buf);
216 decd_to_write = strdup(decd_buf);
218 /* Getting id, name and ip */
220 name = strchr(id, ' ');
226 ip = strchr(name, ' ');
232 tmp_str = strchr(ip, ' ');
241 /* If ip isn't set, it is because we have an invalid
246 MessageBox(hwnd, "Unable to import "
247 "authentication key because it was invalid.",
248 "Error -- Failure Saving Auth Key", MB_OK);
252 char mbox_msg[1024 +1];
253 mbox_msg[1024] = '\0';
255 snprintf(mbox_msg, 1024, "Adding key for:\r\n\r\n"
258 "IP Address: %s\r\n",
261 ret = MessageBox(hwnd, mbox_msg,
262 "Confirm Importing Key", MB_OKCANCEL);
265 if(set_ossec_key(decd_to_write, hwnd))
272 /* Free used memory */
284 } /* Finished adding AUTH KEY */
286 /* Re-printing messages */
291 /* Set status to restart */
292 if(strcmp(config_inst.status,ST_RUNNING) == 0)
294 config_inst.status = ST_RUNNING_RESTART;
297 gen_server_info(hwnd);
301 SendMessage(hStatus, SB_SETTEXT, 0,
302 (LPARAM)"Server IP saved");
306 SendMessage(hStatus, SB_SETTEXT, 0,
307 (LPARAM)"Auth key imported");
312 SendMessage(hStatus, SB_SETTEXT, 0,
313 (LPARAM)"Auth key and IP saved");
320 case UI_MENU_MANAGE_EXIT:
321 PostMessage(hwnd, WM_CLOSE, 0, 0);
324 case UI_MENU_VIEW_LOGS:
325 _spawnlp( _P_NOWAIT, "notepad", "notepad " OSSECLOGS, NULL );
327 case UI_MENU_VIEW_CONFIG:
328 _spawnlp( _P_NOWAIT, "notepad", "notepad " CONFIG, NULL );
330 case UI_MENU_HELP_HELP:
331 _spawnlp( _P_NOWAIT, "notepad", "notepad " HELPTXT, NULL );
333 case UI_MENU_HELP_ABOUT:
335 DialogBox(GetModuleHandle(NULL),
336 MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
341 gen_server_info(hwnd);
344 case UI_MENU_MANAGE_START:
346 /* Starting OSSEC -- must have a valid config before. */
347 if((strcmp(config_inst.key, FL_NOKEY) != 0) &&
348 (strcmp(config_inst.server, FL_NOSERVER) != 0))
350 ret_code = os_start_service();
359 MessageBox(hwnd, "Unable to start agent (check config)",
360 "Error -- Unable to Start Agent", MB_OK);
362 else if(ret_code == 1)
365 gen_server_info(hwnd);
367 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Started");
369 MessageBox(hwnd, "Agent started",
370 "Agent Started", MB_OK);
374 MessageBox(hwnd, "Agent already running (try restart)",
375 "Agent Running", MB_OK);
378 case UI_MENU_MANAGE_STOP:
381 ret_code = os_stop_service();
385 gen_server_info(hwnd);
387 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Stopped");
388 MessageBox(hwnd, "Agent stopped",
389 "Agent Stopped", MB_OK);
393 MessageBox(hwnd, "Agent already stopped",
394 "Agent Stopped", MB_OK);
397 case UI_MENU_MANAGE_STATUS:
398 if(CheckServiceRunning())
400 MessageBox(hwnd, "Agent running",
401 "Agent Running", MB_OK);
406 MessageBox(hwnd, "Agent stopped",
407 "Agent Stopped", MB_OK);
410 case UI_MENU_MANAGE_RESTART:
412 if((strcmp(config_inst.key, FL_NOKEY) == 0) ||
413 (strcmp(config_inst.server, FL_NOSERVER) == 0))
415 MessageBox(hwnd, "Unable to restart agent (check config)",
416 "Error -- Unable to Restart Agent", MB_OK);
421 ret_code = os_stop_service();
424 ret_code = os_start_service();
427 MessageBox(hwnd, "Unable to restart agent (check config)",
428 "Error -- Unable to Restart Agent", MB_OK);
433 gen_server_info(hwnd);
435 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Restarted");
436 MessageBox(hwnd, "Agent restarted",
437 "Agent Restarted", MB_OK);
456 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
457 LPSTR lpCmdLine, int nCmdShow)
461 /* Starting Winsock -- for name resolution. */
462 WSAStartup(MAKEWORD(2, 0), &wsaData);
464 /* Initializing config */
467 /* Initializing controls */
468 InitCommonControls();
470 /* Creating main dialogbox */
471 DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);