1 /* @(#) $Id: os_win32ui.c,v 1.10 2009/06/24 18:53:10 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
11 * License details at the LICENSE file included with OSSEC or
12 * online at: http://www.ossec.net/en/licensing.html
17 #include "os_win32ui.h"
22 /* Dialog -- About OSSEC */
23 BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message,
24 WPARAM wParam, LPARAM lParam)
33 switch(LOWORD(wParam))
36 EndDialog(hwnd, IDOK);
42 EndDialog(hwnd, IDOK);
52 BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
61 int statwidths[] = {130, -1};
62 HMENU hMenu, hSubMenu;
64 UINT menuflags = MF_STRING;
66 if(config_inst.admin_access == 0)
68 menuflags = MF_STRING|MF_GRAYED;
73 /* Creating management menu */
74 hSubMenu = CreatePopupMenu();
75 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_START,"&Start OSSEC");
76 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_STOP,"&Stop OSSEC");
77 AppendMenu(hSubMenu, MF_SEPARATOR, UI_MENU_NONE,"");
78 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_RESTART,"&Restart");
79 AppendMenu(hSubMenu, menuflags, UI_MENU_MANAGE_STATUS,"&Status");
80 AppendMenu(hSubMenu, MF_SEPARATOR, UI_MENU_NONE,"");
81 AppendMenu(hSubMenu, MF_STRING,UI_MENU_MANAGE_EXIT,"&Exit");
82 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu,"&Manage");
84 /* Create view menu */
85 hSubMenu = CreatePopupMenu();
86 AppendMenu(hSubMenu, MF_STRING, UI_MENU_VIEW_LOGS, "&View Logs");
87 AppendMenu(hSubMenu, MF_STRING, UI_MENU_VIEW_CONFIG,"V&iew Config");
88 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu,"&View");
90 hSubMenu = CreatePopupMenu();
91 AppendMenu(hSubMenu, MF_STRING, UI_MENU_HELP_ABOUT, "A&bout");
92 AppendMenu(hSubMenu, MF_STRING, UI_MENU_HELP_HELP, "Help");
93 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
96 AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
100 hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
101 WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP,
103 hwnd, (HMENU)IDC_MAIN_STATUS,
104 GetModuleHandle(NULL), NULL);
106 SendMessage(hStatus, SB_SETPARTS,
107 sizeof(statwidths)/sizeof(int),
109 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 values from the user (if chosen save)
156 * We should probably create another function for it...
159 /* Getting server ip */
160 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_TEXT));
166 /* Allocating buffer */
167 buf = (char*)GlobalAlloc(GPTR, len + 1);
173 GetDlgItemText(hwnd, UI_SERVER_TEXT, buf, len + 1);
175 /* If auth key changed, set it */
176 if(strcmp(buf, config_inst.server) != 0)
178 if(set_ossec_server(buf, hwnd))
190 /* Getting auth key */
191 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_AUTH));
196 /* Allocating buffer */
197 buf = (char*)GlobalAlloc(GPTR, len + 1);
203 GetDlgItemText(hwnd, UI_SERVER_AUTH, buf, len + 1);
205 /* If auth key changed, set it */
206 if(strcmp(buf, config_inst.key) != 0)
210 char *decd_buf = NULL;
211 char *decd_to_write = NULL;
217 /* Getting new fields */
218 decd_buf = decode_base64(buf);
221 decd_to_write = strdup(decd_buf);
223 /* Getting id, name and ip */
225 name = strchr(id, ' ');
231 ip = strchr(name, ' ');
237 tmp_str = strchr(ip, ' ');
246 /* If ip isn't set, it is because we have an invalid
251 MessageBox(hwnd, "Unable to import "
252 "authentication key. Invalid.",
253 "Error Saving.", MB_OK);
257 char mbox_msg[1024 +1];
258 mbox_msg[1024] = '\0';
260 snprintf(mbox_msg, 1024, "Adding key for:\r\n\r\n"
263 "IP Address: %s\r\n",
266 ret = MessageBox(hwnd, mbox_msg,
267 "Confirm Importing Key", MB_OKCANCEL);
271 fp = fopen(AUTH_FILE, "w");
275 fprintf(fp, "%s", decd_to_write);
283 /* Free used memory */
295 } /* Finished adding AUTH KEY */
297 /* Re-printing messages */
302 /* Set status to restart */
303 if(strcmp(config_inst.status,ST_RUNNING) == 0)
305 config_inst.status = ST_RUNNING_RESTART;
308 gen_server_info(hwnd);
312 SendMessage(hStatus, SB_SETTEXT, 0,
313 (LPARAM)"Server IP Saved ..");
317 SendMessage(hStatus, SB_SETTEXT, 0,
318 (LPARAM)"Auth key imported ..");
323 SendMessage(hStatus, SB_SETTEXT, 0,
324 (LPARAM)"Auth key and server ip saved ..");
331 case UI_MENU_MANAGE_EXIT:
332 PostMessage(hwnd, WM_CLOSE, 0, 0);
335 case UI_MENU_VIEW_LOGS:
336 _spawnlp( _P_NOWAIT, "notepad", "notepad " OSSECLOGS, NULL );
338 case UI_MENU_VIEW_CONFIG:
339 _spawnlp( _P_NOWAIT, "notepad", "notepad " CONFIG, NULL );
341 case UI_MENU_HELP_HELP:
342 _spawnlp( _P_NOWAIT, "notepad", "notepad " HELPTXT, NULL );
344 case UI_MENU_HELP_ABOUT:
346 DialogBox(GetModuleHandle(NULL),
347 MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
352 gen_server_info(hwnd);
355 case UI_MENU_MANAGE_START:
357 /* Starting OSSEC -- must have a valid config before. */
358 if((strcmp(config_inst.key, FL_NOKEY) != 0) &&
359 (strcmp(config_inst.server, FL_NOSERVER) != 0))
361 ret_code = os_start_service();
370 MessageBox(hwnd, "Unable to start OSSEC (check config).",
371 "Error -- Unable to start", MB_OK);
373 else if(ret_code == 1)
376 gen_server_info(hwnd);
378 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Started..");
380 MessageBox(hwnd, "OSSEC Agent Started.",
385 MessageBox(hwnd, "Agent already running (try restart).",
386 "Already running..", MB_OK);
389 case UI_MENU_MANAGE_STOP:
392 ret_code = os_stop_service();
396 gen_server_info(hwnd);
398 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Stopped..");
399 MessageBox(hwnd, "OSSEC Agent Stopped.",
404 MessageBox(hwnd, "Agent already stopped.",
405 "Already stopped..", MB_OK);
408 case UI_MENU_MANAGE_STATUS:
409 if(CheckServiceRunning())
411 MessageBox(hwnd, "OSSEC Agent running.",
412 "Agent running..", MB_OK);
417 MessageBox(hwnd, "OSSEC Agent stopped.",
418 "Agent stopped.", MB_OK);
421 case UI_MENU_MANAGE_RESTART:
423 if((strcmp(config_inst.key, FL_NOKEY) == 0) ||
424 (strcmp(config_inst.server, FL_NOSERVER) == 0))
426 MessageBox(hwnd, "Unable to restart OSSEC (check config).",
427 "Error -- Unable to restart", MB_OK);
432 ret_code = os_stop_service();
435 ret_code = os_start_service();
438 MessageBox(hwnd, "Unable to restart OSSEC (check config).",
439 "Error -- Unable to restart", MB_OK);
444 gen_server_info(hwnd);
446 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Restarted..");
447 MessageBox(hwnd, "OSSEC Agent Restarted.",
448 "Restarted..", MB_OK);
467 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
468 LPSTR lpCmdLine, int nCmdShow)
474 /* Starting Winsock -- for name resolution. */
475 WSAStartup(MAKEWORD(2, 0), &wsaData);
478 /* Initializing config */
481 /* Initializing controls */
482 InitCommonControls();
484 /* Creating main dialogbox */
485 DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
488 /* Check if service is running and try to start it */
489 if((strcmp(config_inst.key, FL_NOKEY) != 0)&&
490 (strcmp(config_inst.server, FL_NOSERVER) != 0) &&
491 !CheckServiceRunning() &&
492 (config_inst.admin_access != 0))
494 ret = MessageBox(NULL, "OSSEC Agent not running. "
495 "Do you wish to start it?",
496 "Wish to start the agent?", MB_OKCANCEL);
499 /* Starting the service */