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,
102 WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP,
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");
114 /* Initializing config */
116 gen_server_info(hwnd);
119 /* Setting the icons */
120 SendMessage(hwnd, WM_SETICON, ICON_SMALL,
121 (LPARAM)LoadIcon(GetModuleHandle(NULL),
122 MAKEINTRESOURCE(IDI_OSSECICON)));
123 SendMessage(hwnd, WM_SETICON, ICON_BIG,
124 (LPARAM)LoadIcon(GetModuleHandle(NULL),
125 MAKEINTRESOURCE(IDI_OSSECICON)));
127 if(config_inst.admin_access == 0)
129 MessageBox(hwnd, "Admin access required. Some features may not work properly. \n\n"
130 "**If on Vista (or Server 2008), choose the \"Run as administrator\" option.",
131 "Admin access required.", MB_OK);
139 switch(LOWORD(wParam))
141 /* In case of SAVE */
148 if(config_inst.admin_access == 0)
150 MessageBox(hwnd, "Unable to edit configuration. "
151 "Admin access required.",
152 "Error Saving.", MB_OK);
156 /** Getting values from the user (if chosen save)
157 * We should probably create another function for it...
160 /* Getting server ip */
161 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_TEXT));
167 /* Allocating buffer */
168 buf = (char*)GlobalAlloc(GPTR, len + 1);
174 GetDlgItemText(hwnd, UI_SERVER_TEXT, buf, len + 1);
176 /* If auth key changed, set it */
177 if(strcmp(buf, config_inst.server) != 0)
179 if(set_ossec_server(buf, hwnd))
191 /* Getting auth key */
192 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_AUTH));
197 /* Allocating buffer */
198 buf = (char*)GlobalAlloc(GPTR, len + 1);
204 GetDlgItemText(hwnd, UI_SERVER_AUTH, buf, len + 1);
206 /* If auth key changed, set it */
207 if(strcmp(buf, config_inst.key) != 0)
211 char *decd_buf = NULL;
212 char *decd_to_write = NULL;
218 /* Getting new fields */
219 decd_buf = decode_base64(buf);
222 decd_to_write = strdup(decd_buf);
224 /* Getting id, name and ip */
226 name = strchr(id, ' ');
232 ip = strchr(name, ' ');
238 tmp_str = strchr(ip, ' ');
247 /* If ip isn't set, it is because we have an invalid
252 MessageBox(hwnd, "Unable to import "
253 "authentication key. Invalid.",
254 "Error Saving.", MB_OK);
258 char mbox_msg[1024 +1];
259 mbox_msg[1024] = '\0';
261 snprintf(mbox_msg, 1024, "Adding key for:\r\n\r\n"
264 "IP Address: %s\r\n",
267 ret = MessageBox(hwnd, mbox_msg,
268 "Confirm Importing Key", MB_OKCANCEL);
272 fp = fopen(AUTH_FILE, "w");
276 fprintf(fp, "%s", decd_to_write);
284 /* Free used memory */
296 } /* Finished adding AUTH KEY */
298 /* Re-printing messages */
303 /* Set status to restart */
304 if(strcmp(config_inst.status,ST_RUNNING) == 0)
306 config_inst.status = ST_RUNNING_RESTART;
309 gen_server_info(hwnd);
313 SendMessage(hStatus, SB_SETTEXT, 0,
314 (LPARAM)"Server IP Saved ..");
318 SendMessage(hStatus, SB_SETTEXT, 0,
319 (LPARAM)"Auth key imported ..");
324 SendMessage(hStatus, SB_SETTEXT, 0,
325 (LPARAM)"Auth key and server ip saved ..");
332 case UI_MENU_MANAGE_EXIT:
333 PostMessage(hwnd, WM_CLOSE, 0, 0);
336 case UI_MENU_VIEW_LOGS:
337 _spawnlp( _P_NOWAIT, "notepad", "notepad " OSSECLOGS, NULL );
339 case UI_MENU_VIEW_CONFIG:
340 _spawnlp( _P_NOWAIT, "notepad", "notepad " CONFIG, NULL );
342 case UI_MENU_HELP_HELP:
343 _spawnlp( _P_NOWAIT, "notepad", "notepad " HELPTXT, NULL );
345 case UI_MENU_HELP_ABOUT:
347 DialogBox(GetModuleHandle(NULL),
348 MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
353 gen_server_info(hwnd);
356 case UI_MENU_MANAGE_START:
358 /* Starting OSSEC -- must have a valid config before. */
359 if((strcmp(config_inst.key, FL_NOKEY) != 0) &&
360 (strcmp(config_inst.server, FL_NOSERVER) != 0))
362 ret_code = os_start_service();
371 MessageBox(hwnd, "Unable to start OSSEC (check config).",
372 "Error -- Unable to start", MB_OK);
374 else if(ret_code == 1)
377 gen_server_info(hwnd);
379 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Started..");
381 MessageBox(hwnd, "OSSEC Agent Started.",
386 MessageBox(hwnd, "Agent already running (try restart).",
387 "Already running..", MB_OK);
390 case UI_MENU_MANAGE_STOP:
393 ret_code = os_stop_service();
397 gen_server_info(hwnd);
399 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Stopped..");
400 MessageBox(hwnd, "OSSEC Agent Stopped.",
405 MessageBox(hwnd, "Agent already stopped.",
406 "Already stopped..", MB_OK);
409 case UI_MENU_MANAGE_STATUS:
410 if(CheckServiceRunning())
412 MessageBox(hwnd, "OSSEC Agent running.",
413 "Agent running..", MB_OK);
418 MessageBox(hwnd, "OSSEC Agent stopped.",
419 "Agent stopped.", MB_OK);
422 case UI_MENU_MANAGE_RESTART:
424 if((strcmp(config_inst.key, FL_NOKEY) == 0) ||
425 (strcmp(config_inst.server, FL_NOSERVER) == 0))
427 MessageBox(hwnd, "Unable to restart OSSEC (check config).",
428 "Error -- Unable to restart", MB_OK);
433 ret_code = os_stop_service();
436 ret_code = os_start_service();
439 MessageBox(hwnd, "Unable to restart OSSEC (check config).",
440 "Error -- Unable to restart", MB_OK);
445 gen_server_info(hwnd);
447 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Restarted..");
448 MessageBox(hwnd, "OSSEC Agent Restarted.",
449 "Restarted..", MB_OK);
468 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
469 LPSTR lpCmdLine, int nCmdShow)
475 /* Starting Winsock -- for name resolution. */
476 WSAStartup(MAKEWORD(2, 0), &wsaData);
479 /* Initializing config */
482 /* Initializing controls */
483 InitCommonControls();
485 /* Creating main dialogbox */
486 DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
489 /* Check if service is running and try to start it */
490 if((strcmp(config_inst.key, FL_NOKEY) != 0)&&
491 (strcmp(config_inst.server, FL_NOSERVER) != 0) &&
492 !CheckServiceRunning() &&
493 (config_inst.admin_access != 0))
495 ret = MessageBox(NULL, "OSSEC Agent not running. "
496 "Do you wish to start it?",
497 "Wish to start the agent?", MB_OKCANCEL);
500 /* Starting the service */