- add fortify to lintian
[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|SBARS_SIZEGRIP,
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
114             /* Initializing config */
115             config_read(hwnd);
116             gen_server_info(hwnd);
117
118
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)));
126
127             if(config_inst.admin_access == 0)
128             {
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);
132                 break;
133             }
134
135         }
136         break;
137
138         case WM_COMMAND:
139         switch(LOWORD(wParam))
140         {
141             /* In case of SAVE */
142             case IDC_ADD:
143             {
144                 int chd = 0;
145                 int len;
146
147
148                 if(config_inst.admin_access == 0)
149                 {
150                     MessageBox(hwnd, "Unable to edit configuration. "
151                                      "Admin access required.",
152                                      "Error Saving.", MB_OK);
153                     break;
154                 }
155
156                 /** Getting values from the user (if chosen save)
157                  * We should probably create another function for it...
158                  **/
159
160                 /* Getting server ip */
161                 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_TEXT));
162                 if(len > 0)
163                 {
164                     char *buf;
165
166
167                     /* Allocating buffer */
168                     buf = (char*)GlobalAlloc(GPTR, len + 1);
169                     if(!buf)
170                     {
171                         exit(-1);
172                     }
173
174                     GetDlgItemText(hwnd, UI_SERVER_TEXT, buf, len + 1);
175
176                     /* If auth key changed, set it */
177                     if(strcmp(buf, config_inst.server) != 0)
178                     {
179                         if(set_ossec_server(buf, hwnd))
180                         {
181                             chd = 1;
182                         }
183                     }
184                     else
185                     {
186                         GlobalFree(buf);
187                     }
188                 }
189
190
191                 /* Getting auth key */
192                 len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_AUTH));
193                 if(len > 0)
194                 {
195                     char *buf;
196
197                     /* Allocating buffer */
198                     buf = (char*)GlobalAlloc(GPTR, len + 1);
199                     if(!buf)
200                     {
201                         exit(-1);
202                     }
203
204                     GetDlgItemText(hwnd, UI_SERVER_AUTH, buf, len + 1);
205
206                     /* If auth key changed, set it */
207                     if(strcmp(buf, config_inst.key) != 0)
208                     {
209                         int ret;
210                         char *tmp_str;
211                         char *decd_buf = NULL;
212                         char *decd_to_write = NULL;
213                         char *id = NULL;
214                         char *name = NULL;
215                         char *ip = NULL;
216
217
218                         /* Getting new fields */
219                         decd_buf = decode_base64(buf);
220                         if(decd_buf)
221                         {
222                             decd_to_write = strdup(decd_buf);
223
224                             /* Getting id, name and ip */
225                             id = decd_buf;
226                             name = strchr(id, ' ');
227                             if(name)
228                             {
229                                 *name = '\0';
230                                 name++;
231
232                                 ip = strchr(name, ' ');
233                                 if(ip)
234                                 {
235                                     *ip = '\0';
236                                     ip++;
237
238                                     tmp_str = strchr(ip, ' ');
239                                     if(tmp_str)
240                                     {
241                                         *tmp_str = '\0';
242                                     }
243                                 }
244                             }
245                         }
246
247                         /* If ip isn't set, it is because we have an invalid
248                          * auth key.
249                          */
250                         if(!ip)
251                         {
252                             MessageBox(hwnd, "Unable to import "
253                                              "authentication key. Invalid.",
254                                              "Error Saving.", MB_OK);
255                         }
256                         else
257                         {
258                             char mbox_msg[1024 +1];
259                             mbox_msg[1024] = '\0';
260
261                             snprintf(mbox_msg, 1024, "Adding key for:\r\n\r\n"
262                                                "Agent ID: %s\r\n"
263                                                "Agent Name: %s\r\n"
264                                                "IP Address: %s\r\n",
265                                                id, name, ip);
266
267                             ret = MessageBox(hwnd, mbox_msg,
268                                          "Confirm Importing Key", MB_OKCANCEL);
269                             if(ret == IDOK)
270                             {
271                                 FILE *fp;
272                                 fp = fopen(AUTH_FILE, "w");
273                                 if(fp)
274                                 {
275                                     chd+=2;
276                                     fprintf(fp, "%s", decd_to_write);
277                                     fclose(fp);
278                                 }
279                             }
280
281
282                         }
283
284                         /* Free used memory */
285                         if(decd_buf)
286                         {
287                             free(decd_to_write);
288                             free(decd_buf);
289                         }
290                     }
291                     else
292                     {
293                         GlobalFree(buf);
294                     }
295
296                 } /* Finished adding AUTH KEY */
297
298                 /* Re-printing messages */
299                 if(chd)
300                 {
301                     config_read(hwnd);
302
303                     /* Set status to restart */
304                     if(strcmp(config_inst.status,ST_RUNNING) == 0)
305                     {
306                         config_inst.status = ST_RUNNING_RESTART;
307                     }
308
309                     gen_server_info(hwnd);
310
311                     if(chd == 1)
312                     {
313                         SendMessage(hStatus, SB_SETTEXT, 0,
314                                 (LPARAM)"Server IP Saved ..");
315                     }
316                     else if(chd == 2)
317                     {
318                         SendMessage(hStatus, SB_SETTEXT, 0,
319                                 (LPARAM)"Auth key imported ..");
320
321                     }
322                     else
323                     {
324                         SendMessage(hStatus, SB_SETTEXT, 0,
325                                 (LPARAM)"Auth key and server ip saved ..");
326
327                     }
328                 }
329             }
330             break;
331
332             case UI_MENU_MANAGE_EXIT:
333                 PostMessage(hwnd, WM_CLOSE, 0, 0);
334                 break;
335
336             case UI_MENU_VIEW_LOGS:
337                 _spawnlp( _P_NOWAIT, "notepad", "notepad " OSSECLOGS, NULL );
338                 break;
339             case UI_MENU_VIEW_CONFIG:
340                 _spawnlp( _P_NOWAIT, "notepad", "notepad " CONFIG, NULL );
341                 break;
342             case UI_MENU_HELP_HELP:
343                 _spawnlp( _P_NOWAIT, "notepad", "notepad " HELPTXT, NULL );
344                 break;
345             case UI_MENU_HELP_ABOUT:
346                 {
347                     DialogBox(GetModuleHandle(NULL),
348                             MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
349                 }
350                 break;
351             case IDC_CANCEL:
352                 config_read(hwnd);
353                 gen_server_info(hwnd);
354                 break;
355
356             case UI_MENU_MANAGE_START:
357
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))
361                 {
362                     ret_code = os_start_service();
363                 }
364                 else
365                 {
366                     ret_code = 0;
367                 }
368
369                 if(ret_code == 0)
370                 {
371                     MessageBox(hwnd, "Unable to start OSSEC (check config).",
372                                      "Error -- Unable to start", MB_OK);
373                 }
374                 else if(ret_code == 1)
375                 {
376                     config_read(hwnd);
377                     gen_server_info(hwnd);
378
379                     SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Started..");
380
381                     MessageBox(hwnd, "OSSEC Agent Started.",
382                                      "Started..", MB_OK);
383                 }
384                 else
385                 {
386                     MessageBox(hwnd, "Agent already running (try restart).",
387                                      "Already running..", MB_OK);
388                 }
389                 break;
390             case UI_MENU_MANAGE_STOP:
391
392                 /* Stopping OSSEC */
393                 ret_code = os_stop_service();
394                 if(ret_code == 1)
395                 {
396                     config_read(hwnd);
397                     gen_server_info(hwnd);
398
399                     SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Stopped..");
400                     MessageBox(hwnd, "OSSEC Agent Stopped.",
401                                      "Stopped..", MB_OK);
402                 }
403                 else
404                 {
405                     MessageBox(hwnd, "Agent already stopped.",
406                                      "Already stopped..", MB_OK);
407                 }
408                 break;
409             case UI_MENU_MANAGE_STATUS:
410                 if(CheckServiceRunning())
411                 {
412                     MessageBox(hwnd, "OSSEC Agent running.",
413                                      "Agent running..", MB_OK);
414
415                 }
416                 else
417                 {
418                     MessageBox(hwnd, "OSSEC Agent stopped.",
419                                      "Agent stopped.", MB_OK);
420                 }
421                 break;
422             case UI_MENU_MANAGE_RESTART:
423
424                 if((strcmp(config_inst.key, FL_NOKEY) == 0) ||
425                    (strcmp(config_inst.server, FL_NOSERVER) == 0))
426                 {
427                     MessageBox(hwnd, "Unable to restart OSSEC (check config).",
428                                      "Error -- Unable to restart", MB_OK);
429                     break;
430
431                 }
432
433                 ret_code = os_stop_service();
434
435                 /* Starting OSSEC */
436                 ret_code = os_start_service();
437                 if(ret_code == 0)
438                 {
439                     MessageBox(hwnd, "Unable to restart OSSEC (check config).",
440                                      "Error -- Unable to restart", MB_OK);
441                 }
442                 else
443                 {
444                     config_read(hwnd);
445                     gen_server_info(hwnd);
446
447                     SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Restarted..");
448                     MessageBox(hwnd, "OSSEC Agent Restarted.",
449                                      "Restarted..", MB_OK);
450                 }
451                 break;
452         }
453         break;
454
455         case WM_CLOSE:
456             EndDialog(hwnd, 0);
457             break;
458
459         default:
460             return FALSE;
461     }
462     return TRUE;
463 }
464
465
466
467
468 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
469     LPSTR lpCmdLine, int nCmdShow)
470 {
471     int ret;
472     WSADATA wsaData;
473
474
475     /* Starting Winsock -- for name resolution. */
476     WSAStartup(MAKEWORD(2, 0), &wsaData);
477
478
479     /* Initializing config */
480     init_config();
481
482     /* Initializing controls */
483     InitCommonControls();
484
485     /* Creating main dialogbox */
486     DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
487
488
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))
494     {
495         ret = MessageBox(NULL, "OSSEC Agent not running. "
496                 "Do you wish to start it?",
497                 "Wish to start the agent?", MB_OKCANCEL);
498         if(ret == IDOK)
499         {
500             /* Starting the service */
501             os_start_service();
502         }
503     }
504
505     return(0);
506 }
507
508
509 /* EOF */