new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / win32 / ui / os_win32ui.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
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
7  * Foundation.
8  */
9
10 #include <process.h>
11
12 #include "os_win32ui.h"
13 #include <process.h>
14 #include "../os_win.h"
15
16
17 /* Dialog -- About OSSEC */
18 BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message,
19        WPARAM wParam,
20        __attribute__((unused))LPARAM lParam)
21 {
22     switch (Message) {
23         case WM_CREATE:
24         case WM_INITDIALOG:
25
26             return TRUE;
27         case WM_COMMAND:
28             switch (LOWORD(wParam)) {
29                 case UI_ID_CLOSE:
30                     EndDialog(hwnd, IDOK);
31                     break;
32             }
33             break;
34
35         case WM_CLOSE:
36             EndDialog(hwnd, IDOK);
37             break;
38         default:
39             return FALSE;
40     }
41     return TRUE;
42 }
43
44 /* Main Dialog */
45 BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam,
46         __attribute__((unused))LPARAM lParam)
47 {
48     int ret_code = 0;
49
50
51     switch (Message) {
52         case WM_INITDIALOG: {
53             int statwidths[] = {130, -1};
54             HMENU hMenu, hSubMenu;
55
56             UINT menuflags = MF_STRING;
57
58             if (config_inst.admin_access == 0) {
59                 menuflags = MF_STRING | MF_GRAYED;
60             }
61
62             hMenu = CreateMenu();
63
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");
74
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");
80
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");
85
86
87             AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
88             SetMenu(hwnd, hMenu);
89
90
91             hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
92                                      WS_CHILD | WS_VISIBLE,
93                                      0, 0, 0, 0,
94                                      hwnd, (HMENU)IDC_MAIN_STATUS,
95                                      GetModuleHandle(NULL), NULL);
96
97             SendMessage(hStatus, SB_SETPARTS,
98                         sizeof(statwidths) / sizeof(int),
99                         (LPARAM)statwidths);
100             SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
101
102
103             /* Initializing config */
104             config_read(hwnd);
105             gen_server_info(hwnd);
106
107
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)));
115
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);
120                 break;
121             }
122
123         }
124         break;
125
126         case WM_COMMAND:
127             switch (LOWORD(wParam)) {
128                 /* In case of SAVE */
129                 case IDC_ADD: {
130                     int chd = 0;
131                     int len;
132
133                     if (config_inst.admin_access == 0) {
134                         MessageBox(hwnd, "Unable to edit configuration. "
135                                    "Admin access required.",
136                                    "Error Saving.", MB_OK);
137                         break;
138                     }
139
140                     /* Get server IP */
141                     len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_TEXT));
142                     if (len > 0) {
143                         char *buf;
144
145                         /* Allocate buffer */
146                         buf = (char *)GlobalAlloc(GPTR, len + 1);
147                         if (!buf) {
148                             exit(-1);
149                         }
150
151                         GetDlgItemText(hwnd, UI_SERVER_TEXT, buf, len + 1);
152
153                         /* If auth key changed, set it */
154                         if (strcmp(buf, config_inst.server) != 0) {
155                             if (set_ossec_server(buf, hwnd)) {
156                                 chd = 1;
157                             }
158                         } else {
159                             GlobalFree(buf);
160                         }
161                     }
162
163                     /* Get auth key */
164                     len = GetWindowTextLength(GetDlgItem(hwnd, UI_SERVER_AUTH));
165                     if (len > 0) {
166                         char *buf;
167
168                         /* Allocate buffer */
169                         buf = (char *)GlobalAlloc(GPTR, len + 1);
170                         if (!buf) {
171                             exit(-1);
172                         }
173
174                         GetDlgItemText(hwnd, UI_SERVER_AUTH, buf, len + 1);
175
176                         /* If auth key changed, set it */
177                         if (strcmp(buf, config_inst.key) != 0) {
178                             int ret;
179                             char *tmp_str;
180                             char *decd_buf = NULL;
181                             char *decd_to_write = NULL;
182                             char *id = NULL;
183                             char *name = NULL;
184                             char *ip = NULL;
185
186                             /* Get new fields */
187                             decd_buf = decode_base64(buf);
188                             if (decd_buf) {
189                                 decd_to_write = strdup(decd_buf);
190
191                                 /* Get ID, name and IP */
192                                 id = decd_buf;
193                                 name = strchr(id, ' ');
194                                 if (name) {
195                                     *name = '\0';
196                                     name++;
197
198                                     ip = strchr(name, ' ');
199                                     if (ip) {
200                                         *ip = '\0';
201                                         ip++;
202
203                                         tmp_str = strchr(ip, ' ');
204                                         if (tmp_str) {
205                                             *tmp_str = '\0';
206                                         }
207                                     }
208                                 }
209                             }
210
211                             /* If IP isn't set, it is because we have an invalid
212                              * auth key.
213                              */
214                             if (!ip) {
215                                 MessageBox(hwnd, "Unable to import "
216                                            "authentication key because it was invalid.",
217                                            "Error -- Failure Saving Auth Key", MB_OK);
218                             } else {
219                                 char mbox_msg[1024 + 1];
220                                 mbox_msg[1024] = '\0';
221
222                                 snprintf(mbox_msg, 1024, "Adding key for:\r\n\r\n"
223                                          "Agent ID: %s\r\n"
224                                          "Agent Name: %s\r\n"
225                                          "IP Address: %s\r\n",
226                                          id, name, ip);
227
228                                 ret = MessageBox(hwnd, mbox_msg,
229                                                  "Confirm Importing Key", MB_OKCANCEL);
230                                 if (ret == IDOK) {
231                                     if (set_ossec_key(decd_to_write, hwnd)) {
232                                         chd += 2;
233                                     }
234                                 }
235                             }
236
237                             /* Free used memory */
238                             if (decd_buf) {
239                                 free(decd_to_write);
240                                 free(decd_buf);
241                             }
242                         } else {
243                             GlobalFree(buf);
244                         }
245
246                     } /* Finished adding AUTH KEY */
247
248                     /* Re-print messages */
249                     if (chd) {
250                         config_read(hwnd);
251
252                         /* Set status to restart */
253                         if (strcmp(config_inst.status, ST_RUNNING) == 0) {
254                             config_inst.status = ST_RUNNING_RESTART;
255                         }
256
257                         gen_server_info(hwnd);
258
259                         if (chd == 1) {
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");
265
266                         } else {
267                             SendMessage(hStatus, SB_SETTEXT, 0,
268                                         (LPARAM)"Auth key and IP saved");
269
270                         }
271                     }
272                 }
273                 break;
274
275                 case UI_MENU_MANAGE_EXIT:
276                     PostMessage(hwnd, WM_CLOSE, 0, 0);
277                     break;
278
279                 case UI_MENU_VIEW_LOGS:
280                     _spawnlp( _P_NOWAIT, "notepad", "notepad " OSSECLOGS, NULL );
281                     break;
282                 case UI_MENU_VIEW_CONFIG:
283                     _spawnlp( _P_NOWAIT, "notepad", "notepad " CONFIG, NULL );
284                     break;
285                 case UI_MENU_HELP_HELP:
286                     _spawnlp( _P_NOWAIT, "notepad", "notepad " HELPTXT, NULL );
287                     break;
288                 case UI_MENU_HELP_ABOUT: {
289                     DialogBox(GetModuleHandle(NULL),
290                               MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
291                 }
292                 break;
293                 case IDC_CANCEL:
294                     config_read(hwnd);
295                     gen_server_info(hwnd);
296                     break;
297
298                 case UI_MENU_MANAGE_START:
299
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();
304                     } else {
305                         ret_code = 0;
306                     }
307
308                     if (ret_code == 0) {
309                         MessageBox(hwnd, "Unable to start agent (check config)",
310                                    "Error -- Unable to Start Agent", MB_OK);
311                     } else if (ret_code == 1) {
312                         config_read(hwnd);
313                         gen_server_info(hwnd);
314
315                         SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Started");
316
317                         MessageBox(hwnd, "Agent started",
318                                    "Agent Started", MB_OK);
319                     } else {
320                         MessageBox(hwnd, "Agent already running (try restart)",
321                                    "Agent Running", MB_OK);
322                     }
323                     break;
324                 case UI_MENU_MANAGE_STOP:
325
326                     /* Stop OSSEC */
327                     ret_code = os_stop_service();
328                     if (ret_code == 1) {
329                         config_read(hwnd);
330                         gen_server_info(hwnd);
331
332                         SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Stopped");
333                         MessageBox(hwnd, "Agent stopped",
334                                    "Agent Stopped", MB_OK);
335                     } else {
336                         MessageBox(hwnd, "Agent already stopped",
337                                    "Agent Stopped", MB_OK);
338                     }
339                     break;
340                 case UI_MENU_MANAGE_STATUS:
341                     if (CheckServiceRunning()) {
342                         MessageBox(hwnd, "Agent running",
343                                    "Agent Running", MB_OK);
344
345                     } else {
346                         MessageBox(hwnd, "Agent stopped",
347                                    "Agent Stopped", MB_OK);
348                     }
349                     break;
350                 case UI_MENU_MANAGE_RESTART:
351
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);
356                         break;
357
358                     }
359
360                     ret_code = os_stop_service();
361
362                     /* Start OSSEC */
363                     ret_code = os_start_service();
364                     if (ret_code == 0) {
365                         MessageBox(hwnd, "Unable to restart agent (check config)",
366                                    "Error -- Unable to Restart Agent", MB_OK);
367                     } else {
368                         config_read(hwnd);
369                         gen_server_info(hwnd);
370
371                         SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Restarted");
372                         MessageBox(hwnd, "Agent restarted",
373                                    "Agent Restarted", MB_OK);
374                     }
375                     break;
376             }
377             break;
378
379         case WM_CLOSE:
380             EndDialog(hwnd, 0);
381             break;
382
383         default:
384             return FALSE;
385     }
386     return TRUE;
387 }
388
389 int WINAPI WinMain(HINSTANCE hInstance, __attribute__((unused))HINSTANCE hPrevInstance,
390         __attribute__((unused))LPSTR lpCmdLine, __attribute__((unused))int nCmdShow)
391 {
392     WSADATA wsaData;
393
394     /* Start Winsock -- for name resolution */
395     WSAStartup(MAKEWORD(2, 0), &wsaData);
396
397     /* Initialize config */
398     init_config();
399
400     /* Initialize controls */
401     InitCommonControls();
402
403     /* Create main dialogbox */
404     DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
405
406     return (0);
407 }