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