8b26954339f33034f45ffb2b8dfe1beb213e8c38
[ossec-hids.git] / src / win32 / ui / os_win32ui.c
1 /* @(#) $Id: ./src/win32/ui/os_win32ui.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
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
10  * Foundation.
11  *
12  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17
18 #include "os_win32ui.h"
19 #include <process.h>
20 #include "os_win.h"
21
22
23 /* Dialog -- About OSSEC */
24 BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message,
25                            WPARAM wParam, LPARAM lParam)
26 {
27     switch(Message)
28     {
29         case WM_CREATE:
30         case WM_INITDIALOG:
31
32             return TRUE;
33         case WM_COMMAND:
34             switch(LOWORD(wParam))
35             {
36                 case UI_ID_CLOSE:
37                     EndDialog(hwnd, IDOK);
38                     break;
39             }
40             break;
41
42         case WM_CLOSE:
43             EndDialog(hwnd, IDOK);
44             break;
45         default:
46             return FALSE;
47     }
48     return TRUE;
49 }
50
51
52 /* Main Dialog */
53 BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
54 {
55     int ret_code = 0;
56
57
58     switch(Message)
59     {
60         case WM_INITDIALOG:
61         {
62             int statwidths[] = {130, -1};
63             HMENU hMenu, hSubMenu;
64
65             UINT menuflags = MF_STRING;
66
67             if(config_inst.admin_access == 0)
68             {
69                 menuflags = MF_STRING|MF_GRAYED;
70             }
71
72             hMenu = CreateMenu();
73
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");
84
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");
90
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");
95
96
97             AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
98             SetMenu(hwnd, hMenu);
99
100
101             hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
102                     WS_CHILD|WS_VISIBLE,
103                     0, 0, 0, 0,
104                     hwnd, (HMENU)IDC_MAIN_STATUS,
105                     GetModuleHandle(NULL), NULL);
106
107             SendMessage(hStatus, SB_SETPARTS,
108                     sizeof(statwidths)/sizeof(int),
109                     (LPARAM)statwidths);
110             SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
111
112
113             /* Initializing config */
114             config_read(hwnd);
115             gen_server_info(hwnd);
116
117
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)));
125
126             if(config_inst.admin_access == 0)
127             {
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);
131                 break;
132             }
133
134         }
135         break;
136
137         case WM_COMMAND:
138         switch(LOWORD(wParam))
139         {
140             /* In case of SAVE */
141             case IDC_ADD:
142             {
143                 int chd = 0;
144                 int len;
145
146
147                 if(config_inst.admin_access == 0)
148                 {
149                     MessageBox(hwnd, "Unable to edit configuration. "
150                                      "Admin access required.",
151                                      "Error Saving.", MB_OK);
152                     break;
153                 }
154
155                 /* Getting server ip */
156                 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_TEXT));
157                 if(len > 0)
158                 {
159                     char *buf;
160
161
162                     /* Allocating buffer */
163                     buf = (char*)GlobalAlloc(GPTR, len + 1);
164                     if(!buf)
165                     {
166                         exit(-1);
167                     }
168
169                     GetDlgItemText(hwnd, UI_SERVER_TEXT, buf, len + 1);
170
171                     /* If auth key changed, set it */
172                     if(strcmp(buf, config_inst.server) != 0)
173                     {
174                         if(set_ossec_server(buf, hwnd))
175                         {
176                             chd = 1;
177                         }
178                     }
179                     else
180                     {
181                         GlobalFree(buf);
182                     }
183                 }
184
185
186                 /* Getting auth key */
187                 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_AUTH));
188                 if(len > 0)
189                 {
190                     char *buf;
191
192                     /* Allocating buffer */
193                     buf = (char*)GlobalAlloc(GPTR, len + 1);
194                     if(!buf)
195                     {
196                         exit(-1);
197                     }
198
199                     GetDlgItemText(hwnd, UI_SERVER_AUTH, buf, len + 1);
200
201                     /* If auth key changed, set it */
202                     if(strcmp(buf, config_inst.key) != 0)
203                     {
204                         int ret;
205                         char *tmp_str;
206                         char *decd_buf = NULL;
207                         char *decd_to_write = NULL;
208                         char *id = NULL;
209                         char *name = NULL;
210                         char *ip = NULL;
211
212                         /* Getting new fields */
213                         decd_buf = decode_base64(buf);
214                         if(decd_buf)
215                         {
216                             decd_to_write = strdup(decd_buf);
217
218                             /* Getting id, name and ip */
219                             id = decd_buf;
220                             name = strchr(id, ' ');
221                             if(name)
222                             {
223                                 *name = '\0';
224                                 name++;
225
226                                 ip = strchr(name, ' ');
227                                 if(ip)
228                                 {
229                                     *ip = '\0';
230                                     ip++;
231
232                                     tmp_str = strchr(ip, ' ');
233                                     if(tmp_str)
234                                     {
235                                         *tmp_str = '\0';
236                                     }
237                                 }
238                             }
239                         }
240
241                         /* If ip isn't set, it is because we have an invalid
242                          * auth key.
243                          */
244                         if(!ip)
245                         {
246                             MessageBox(hwnd, "Unable to import "
247                                              "authentication key because it was invalid.",
248                                              "Error -- Failure Saving Auth Key", MB_OK);
249                         }
250                         else
251                         {
252                             char mbox_msg[1024 +1];
253                             mbox_msg[1024] = '\0';
254
255                             snprintf(mbox_msg, 1024, "Adding key for:\r\n\r\n"
256                                                "Agent ID: %s\r\n"
257                                                "Agent Name: %s\r\n"
258                                                "IP Address: %s\r\n",
259                                                id, name, ip);
260
261                             ret = MessageBox(hwnd, mbox_msg,
262                                              "Confirm Importing Key", MB_OKCANCEL);
263                             if(ret == IDOK)
264                             {
265                                 if(set_ossec_key(decd_to_write, hwnd))
266                                 {
267                                     chd += 2;
268                                 }
269                             }
270                         }
271
272                         /* Free used memory */
273                         if(decd_buf)
274                         {
275                             free(decd_to_write);
276                             free(decd_buf);
277                         }
278                     }
279                     else
280                     {
281                         GlobalFree(buf);
282                     }
283
284                 } /* Finished adding AUTH KEY */
285
286                 /* Re-printing messages */
287                 if(chd)
288                 {
289                     config_read(hwnd);
290
291                     /* Set status to restart */
292                     if(strcmp(config_inst.status,ST_RUNNING) == 0)
293                     {
294                         config_inst.status = ST_RUNNING_RESTART;
295                     }
296
297                     gen_server_info(hwnd);
298
299                     if(chd == 1)
300                     {
301                         SendMessage(hStatus, SB_SETTEXT, 0,
302                                 (LPARAM)"Server IP saved");
303                     }
304                     else if(chd == 2)
305                     {
306                         SendMessage(hStatus, SB_SETTEXT, 0,
307                                 (LPARAM)"Auth key imported");
308
309                     }
310                     else
311                     {
312                         SendMessage(hStatus, SB_SETTEXT, 0,
313                                 (LPARAM)"Auth key and IP saved");
314
315                     }
316                 }
317             }
318             break;
319
320             case UI_MENU_MANAGE_EXIT:
321                 PostMessage(hwnd, WM_CLOSE, 0, 0);
322                 break;
323
324             case UI_MENU_VIEW_LOGS:
325                 _spawnlp( _P_NOWAIT, "notepad", "notepad " OSSECLOGS, NULL );
326                 break;
327             case UI_MENU_VIEW_CONFIG:
328                 _spawnlp( _P_NOWAIT, "notepad", "notepad " CONFIG, NULL );
329                 break;
330             case UI_MENU_HELP_HELP:
331                 _spawnlp( _P_NOWAIT, "notepad", "notepad " HELPTXT, NULL );
332                 break;
333             case UI_MENU_HELP_ABOUT:
334                 {
335                     DialogBox(GetModuleHandle(NULL),
336                             MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
337                 }
338                 break;
339             case IDC_CANCEL:
340                 config_read(hwnd);
341                 gen_server_info(hwnd);
342                 break;
343
344             case UI_MENU_MANAGE_START:
345
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))
349                 {
350                     ret_code = os_start_service();
351                 }
352                 else
353                 {
354                     ret_code = 0;
355                 }
356
357                 if(ret_code == 0)
358                 {
359                     MessageBox(hwnd, "Unable to start agent (check config)",
360                                      "Error -- Unable to Start Agent", MB_OK);
361                 }
362                 else if(ret_code == 1)
363                 {
364                     config_read(hwnd);
365                     gen_server_info(hwnd);
366
367                     SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Started");
368
369                     MessageBox(hwnd, "Agent started",
370                                      "Agent Started", MB_OK);
371                 }
372                 else
373                 {
374                     MessageBox(hwnd, "Agent already running (try restart)",
375                                      "Agent Running", MB_OK);
376                 }
377                 break;
378             case UI_MENU_MANAGE_STOP:
379
380                 /* Stopping OSSEC */
381                 ret_code = os_stop_service();
382                 if(ret_code == 1)
383                 {
384                     config_read(hwnd);
385                     gen_server_info(hwnd);
386
387                     SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Stopped");
388                     MessageBox(hwnd, "Agent stopped",
389                                      "Agent Stopped", MB_OK);
390                 }
391                 else
392                 {
393                     MessageBox(hwnd, "Agent already stopped",
394                                      "Agent Stopped", MB_OK);
395                 }
396                 break;
397             case UI_MENU_MANAGE_STATUS:
398                 if(CheckServiceRunning())
399                 {
400                     MessageBox(hwnd, "Agent running",
401                                      "Agent Running", MB_OK);
402
403                 }
404                 else
405                 {
406                     MessageBox(hwnd, "Agent stopped",
407                                      "Agent Stopped", MB_OK);
408                 }
409                 break;
410             case UI_MENU_MANAGE_RESTART:
411
412                 if((strcmp(config_inst.key, FL_NOKEY) == 0) ||
413                    (strcmp(config_inst.server, FL_NOSERVER) == 0))
414                 {
415                     MessageBox(hwnd, "Unable to restart agent (check config)",
416                                      "Error -- Unable to Restart Agent", MB_OK);
417                     break;
418
419                 }
420
421                 ret_code = os_stop_service();
422
423                 /* Starting OSSEC */
424                 ret_code = os_start_service();
425                 if(ret_code == 0)
426                 {
427                     MessageBox(hwnd, "Unable to restart agent (check config)",
428                                      "Error -- Unable to Restart Agent", MB_OK);
429                 }
430                 else
431                 {
432                     config_read(hwnd);
433                     gen_server_info(hwnd);
434
435                     SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Restarted");
436                     MessageBox(hwnd, "Agent restarted",
437                                      "Agent Restarted", MB_OK);
438                 }
439                 break;
440         }
441         break;
442
443         case WM_CLOSE:
444             EndDialog(hwnd, 0);
445             break;
446
447         default:
448             return FALSE;
449     }
450     return TRUE;
451 }
452
453
454
455
456 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
457     LPSTR lpCmdLine, int nCmdShow)
458 {
459     WSADATA wsaData;
460
461     /* Starting Winsock -- for name resolution. */
462     WSAStartup(MAKEWORD(2, 0), &wsaData);
463
464     /* Initializing config */
465     init_config();
466
467     /* Initializing controls */
468     InitCommonControls();
469
470     /* Creating main dialogbox */
471     DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
472
473     return(0);
474 }
475
476
477 /* EOF */