1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
12 #include "os_win32ui.h"
14 #include "../os_win.h"
17 /* Dialog -- About OSSEC */
18 BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message,
20 __attribute__((unused))LPARAM lParam)
28 switch (LOWORD(wParam)) {
30 EndDialog(hwnd, IDOK);
36 EndDialog(hwnd, IDOK);
45 BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam,
46 __attribute__((unused))LPARAM lParam)
53 int statwidths[] = {130, -1};
54 HMENU hMenu, hSubMenu;
56 UINT menuflags = MF_STRING;
58 if (config_inst.admin_access == 0) {
59 menuflags = MF_STRING | MF_GRAYED;
64 /* Creating management menu */
65 hSubMenu = CreatePopupMenu();
66 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_START, "&Start OSSEC");
67 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_STOP, "&Stop OSSEC");
68 AppendMenu(hSubMenu, MF_SEPARATOR, UI_MENU_NONE, "");
69 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_RESTART, "&Restart");
70 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_STATUS, "&Status");
71 AppendMenu(hSubMenu, MF_SEPARATOR, UI_MENU_NONE, "");
72 AppendMenu(hSubMenu, MF_STRING, UI_MENU_MANAGE_EXIT, "&Exit");
73 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Manage");
75 /* Create view menu */
76 hSubMenu = CreatePopupMenu();
77 AppendMenu(hSubMenu, MF_STRING, UI_MENU_VIEW_LOGS, "&View Logs");
78 AppendMenu(hSubMenu, MF_STRING, UI_MENU_VIEW_CONFIG, "V&iew Config");
79 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&View");
81 hSubMenu = CreatePopupMenu();
82 AppendMenu(hSubMenu, MF_STRING, UI_MENU_HELP_ABOUT, "A&bout");
83 AppendMenu(hSubMenu, MF_STRING, UI_MENU_HELP_HELP, "Help");
84 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
87 AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
91 hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
92 WS_CHILD | WS_VISIBLE,
94 hwnd, (HMENU)IDC_MAIN_STATUS,
95 GetModuleHandle(NULL), NULL);
97 SendMessage(hStatus, SB_SETPARTS,
98 sizeof(statwidths) / sizeof(int),
100 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
103 /* Initializing config */
105 gen_server_info(hwnd);
108 /* Setting the icons */
109 SendMessage(hwnd, WM_SETICON, ICON_SMALL,
110 (LPARAM)LoadIcon(GetModuleHandle(NULL),
111 MAKEINTRESOURCE(IDI_OSSECICON)));
112 SendMessage(hwnd, WM_SETICON, ICON_BIG,
113 (LPARAM)LoadIcon(GetModuleHandle(NULL),
114 MAKEINTRESOURCE(IDI_OSSECICON)));
116 if (config_inst.admin_access == 0) {
117 MessageBox(hwnd, "Admin access required. Some features may not work properly. \n\n"
118 "**If on Vista (or Server 2008), choose the \"Run as administrator\" option.",
119 "Admin Access Required", MB_OK);
127 switch (LOWORD(wParam)) {
128 /* In case of SAVE */
133 if (config_inst.admin_access == 0) {
134 MessageBox(hwnd, "Unable to edit configuration. "
135 "Admin access required.",
136 "Error Saving.", MB_OK);
141 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_TEXT));
145 /* Allocate buffer */
146 buf = (char *)GlobalAlloc(GPTR, len + 1);
151 GetDlgItemText(hwnd, UI_SERVER_TEXT, buf, len + 1);
153 /* If auth key changed, set it */
154 if (strcmp(buf, config_inst.server) != 0) {
155 if (set_ossec_server(buf, hwnd)) {
164 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_AUTH));
168 /* Allocate buffer */
169 buf = (char *)GlobalAlloc(GPTR, len + 1);
174 GetDlgItemText(hwnd, UI_SERVER_AUTH, buf, len + 1);
176 /* If auth key changed, set it */
177 if (strcmp(buf, config_inst.key) != 0) {
180 char *decd_buf = NULL;
181 char *decd_to_write = NULL;
187 decd_buf = decode_base64(buf);
189 decd_to_write = strdup(decd_buf);
191 /* Get ID, name and IP */
193 name = strchr(id, ' ');
198 ip = strchr(name, ' ');
203 tmp_str = strchr(ip, ' ');
211 /* If IP isn't set, it is because we have an invalid
215 MessageBox(hwnd, "Unable to import "
216 "authentication key because it was invalid.",
217 "Error -- Failure Saving Auth Key", MB_OK);
219 char mbox_msg[1024 + 1];
220 mbox_msg[1024] = '\0';
222 snprintf(mbox_msg, 1024, "Adding key for:\r\n\r\n"
225 "IP Address: %s\r\n",
228 ret = MessageBox(hwnd, mbox_msg,
229 "Confirm Importing Key", MB_OKCANCEL);
231 if (set_ossec_key(decd_to_write, hwnd)) {
237 /* Free used memory */
246 } /* Finished adding AUTH KEY */
248 /* Re-print messages */
252 /* Set status to restart */
253 if (strcmp(config_inst.status, ST_RUNNING) == 0) {
254 config_inst.status = ST_RUNNING_RESTART;
257 gen_server_info(hwnd);
260 SendMessage(hStatus, SB_SETTEXT, 0,
261 (LPARAM)"Server IP saved");
262 } else if (chd == 2) {
263 SendMessage(hStatus, SB_SETTEXT, 0,
264 (LPARAM)"Auth key imported");
267 SendMessage(hStatus, SB_SETTEXT, 0,
268 (LPARAM)"Auth key and IP saved");
275 case UI_MENU_MANAGE_EXIT:
276 PostMessage(hwnd, WM_CLOSE, 0, 0);
279 case UI_MENU_VIEW_LOGS:
280 _spawnlp( _P_NOWAIT, "notepad", "notepad " OSSECLOGS, NULL );
282 case UI_MENU_VIEW_CONFIG:
283 _spawnlp( _P_NOWAIT, "notepad", "notepad " CONFIG, NULL );
285 case UI_MENU_HELP_HELP:
286 _spawnlp( _P_NOWAIT, "notepad", "notepad " HELPTXT, NULL );
288 case UI_MENU_HELP_ABOUT: {
289 DialogBox(GetModuleHandle(NULL),
290 MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
295 gen_server_info(hwnd);
298 case UI_MENU_MANAGE_START:
300 /* Start OSSEC -- must have a valid config before */
301 if ((strcmp(config_inst.key, FL_NOKEY) != 0) &&
302 (strcmp(config_inst.server, FL_NOSERVER) != 0)) {
303 ret_code = os_start_service();
309 MessageBox(hwnd, "Unable to start agent (check config)",
310 "Error -- Unable to Start Agent", MB_OK);
311 } else if (ret_code == 1) {
313 gen_server_info(hwnd);
315 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Started");
317 MessageBox(hwnd, "Agent started",
318 "Agent Started", MB_OK);
320 MessageBox(hwnd, "Agent already running (try restart)",
321 "Agent Running", MB_OK);
324 case UI_MENU_MANAGE_STOP:
327 ret_code = os_stop_service();
330 gen_server_info(hwnd);
332 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Stopped");
333 MessageBox(hwnd, "Agent stopped",
334 "Agent Stopped", MB_OK);
336 MessageBox(hwnd, "Agent already stopped",
337 "Agent Stopped", MB_OK);
340 case UI_MENU_MANAGE_STATUS:
341 if (CheckServiceRunning()) {
342 MessageBox(hwnd, "Agent running",
343 "Agent Running", MB_OK);
346 MessageBox(hwnd, "Agent stopped",
347 "Agent Stopped", MB_OK);
350 case UI_MENU_MANAGE_RESTART:
352 if ((strcmp(config_inst.key, FL_NOKEY) == 0) ||
353 (strcmp(config_inst.server, FL_NOSERVER) == 0)) {
354 MessageBox(hwnd, "Unable to restart agent (check config)",
355 "Error -- Unable to Restart Agent", MB_OK);
360 ret_code = os_stop_service();
363 ret_code = os_start_service();
365 MessageBox(hwnd, "Unable to restart agent (check config)",
366 "Error -- Unable to Restart Agent", MB_OK);
369 gen_server_info(hwnd);
371 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Restarted");
372 MessageBox(hwnd, "Agent restarted",
373 "Agent Restarted", MB_OK);
389 int WINAPI WinMain(HINSTANCE hInstance, __attribute__((unused))HINSTANCE hPrevInstance,
390 __attribute__((unused))LPSTR lpCmdLine, __attribute__((unused))int nCmdShow)
394 /* Start Winsock -- for name resolution */
395 WSAStartup(MAKEWORD(2, 0), &wsaData);
397 /* Initialize config */
400 /* Initialize controls */
401 InitCommonControls();
403 /* Create main dialogbox */
404 DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);