novi upstream verzije 2.8.3
[ossec-hids.git] / src / win32 / ui / common.c
1 /* @(#) $Id: ./src/win32/ui/common.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 #include "shared.h"
18 #include "os_win32ui.h"
19 #include "os_win.h"
20 #include "os_xml/os_xml.h"
21 #include "os_net/os_net.h"
22 #include "validate_op.h"
23
24
25 /* Generate server info (for the main status) */
26 int gen_server_info(HWND hwnd)
27 {
28     memset(ui_server_info, '\0', 2048 +1);
29     snprintf(ui_server_info, 2048,
30             "Agent: %s (%s)  -  %s\r\n\r\n"
31             "Status: %s",
32             config_inst.agentname,
33             config_inst.agentid,
34             config_inst.agentip,
35             config_inst.status);
36
37
38     /* Initializing top */
39     if(config_inst.version)
40     {
41         SetDlgItemText(hwnd, UI_SERVER_TOP, config_inst.version);
42         SetDlgItemText(hwnd, UI_SERVER_INFO, ui_server_info);
43     }
44
45     /* Initializing auth key */
46     SetDlgItemText(hwnd, UI_SERVER_AUTH, config_inst.key);
47
48     /* Initializing server ip */
49     SetDlgItemText(hwnd, UI_SERVER_TEXT, config_inst.server);
50
51     /* Set status data */
52     SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
53     if (config_inst.install_date)
54     {
55         SendMessage(hStatus, SB_SETTEXT, 1, (LPARAM)config_inst.install_date);
56     }
57
58     return(0);
59 }
60
61
62 /* Reads the first line of a specific file  --must free after */
63 char *cat_file(char *file, FILE *fp2)
64 {
65     FILE *fp;
66
67     if(!fp2)
68     {
69         fp = fopen(file, "r");
70     }
71     else
72     {
73         fp = fp2;
74     }
75
76     if(fp)
77     {
78         char buf[1024 +1];
79         char *ret = NULL;
80
81         buf[1024] = '\0';
82         if(fgets(buf, 1024, fp) != NULL)
83         {
84             ret = strchr(buf, '\n');
85             if(ret)
86             {
87                 *ret = '\0';
88             }
89             ret = strchr(buf, '\r');
90             if(ret)
91             {
92                 *ret = '\0';
93             }
94
95             ret = strdup(buf);
96         }
97
98         if(!fp2)
99         {
100             fclose(fp);
101         }
102         return(ret);
103     }
104
105     return(NULL);
106 }
107
108
109 /* Check if a file exists */
110 int is_file(char *file)
111 {
112     FILE *fp;
113     fp = fopen(file, "r");
114     if(fp)
115     {
116         fclose(fp);
117         return(1);
118     }
119     return(0);
120 }
121
122
123 /* Clear configuration */
124 void config_clear()
125 {
126     if(config_inst.version)
127     {
128         free(config_inst.version);
129     }
130
131     if(config_inst.key)
132     {
133         free(config_inst.key);
134     }
135
136     if(config_inst.agentid)
137     {
138         free(config_inst.agentid);
139     }
140
141     if(config_inst.server)
142     {
143         free(config_inst.server);
144     }
145
146     if(config_inst.install_date)
147     {
148         free(config_inst.install_date);
149     }
150
151     /* Initializing config instance */
152     config_inst.dir = NULL;
153     config_inst.key = FL_NOKEY;
154     config_inst.server = strdup(FL_NOSERVER);
155     config_inst.config = NULL;
156
157     config_inst.agentid = NULL;
158     config_inst.agentname= NULL;
159     config_inst.agentip = NULL;
160
161     config_inst.version = NULL;
162     config_inst.install_date = NULL;
163     config_inst.status = ST_UNKNOWN;
164     config_inst.msg_sent = 0;
165 }
166
167
168 /* Initializes the config */
169 void init_config()
170 {
171     /* Initializing config instance */
172     config_inst.dir = NULL;
173     config_inst.key = FL_NOKEY;
174     config_inst.server = NULL;
175     config_inst.config = NULL;
176
177     config_inst.agentid = NULL;
178     config_inst.agentname= NULL;
179     config_inst.agentip = NULL;
180
181     config_inst.version = NULL;
182     config_inst.install_date = NULL;
183     config_inst.status = ST_UNKNOWN;
184     config_inst.msg_sent = 0;
185     config_inst.admin_access = 1;
186
187
188     /* Checking if ui is on the right path
189      * and has the proper permissions
190      */
191     if(!is_file(CONFIG))
192     {
193         if(chdir(DEFDIR))
194         {
195             config_inst.admin_access = 0;
196         }
197
198         if(!is_file(CONFIG))
199         {
200             config_inst.admin_access = 0;
201         }
202     }
203 }
204
205
206 /* Reads ossec config */
207 int config_read(HWND hwnd)
208 {
209     char *tmp_str;
210     char *delim = " - ";
211
212
213     /* Clearing config */
214     config_clear();
215
216
217     /* Getting OSSEC status */
218     if(CheckServiceRunning())
219     {
220         config_inst.status = ST_RUNNING;
221     }
222     else
223     {
224         config_inst.status = ST_STOPPED;
225     }
226
227
228     /* Getting version/install date */
229     config_inst.version = cat_file(VERSION_FILE, NULL);
230     if(config_inst.version)
231     {
232         config_inst.install_date = strstr(config_inst.version, delim);
233         if(config_inst.install_date)
234         {
235             *config_inst.install_date = '\0';
236             config_inst.install_date += strlen(delim);
237         }
238     }
239
240
241     /* Getting number of messages sent */
242     tmp_str = cat_file(SENDER_FILE, NULL);
243     if(tmp_str)
244     {
245         unsigned long int tmp_val = 0;
246         char *to_free = tmp_str;
247
248         tmp_val = atol(tmp_str);
249         if(tmp_val)
250         {
251             config_inst.msg_sent = tmp_val * 9999;
252
253             tmp_str = strchr(tmp_str, ':');
254             if(tmp_str)
255             {
256                 tmp_str++;
257                 tmp_val = atol(tmp_str);
258                 config_inst.msg_sent += tmp_val;
259             }
260         }
261
262         free(to_free);
263     }
264
265
266     /* Getting agent id, name and ip */
267     tmp_str = cat_file(AUTH_FILE, NULL);
268     if(tmp_str)
269     {
270         /* Getting base 64 */
271         config_inst.key = encode_base64(strlen(tmp_str),tmp_str);
272         if(config_inst.key == NULL)
273         {
274             config_inst.key = FL_NOKEY;
275         }
276
277         /* Getting id */
278         config_inst.agentid = tmp_str;
279
280         tmp_str = strchr(tmp_str, ' ');
281         if(tmp_str)
282         {
283             *tmp_str = '\0';
284             tmp_str++;
285
286             /* Getting name */
287             config_inst.agentname = tmp_str;
288             tmp_str = strchr(tmp_str, ' ');
289             if(tmp_str)
290             {
291                 *tmp_str = '\0';
292                 tmp_str++;
293
294                 /* Getting ip */
295                 config_inst.agentip = tmp_str;
296
297                 tmp_str = strchr(tmp_str, ' ');
298                 if(tmp_str)
299                 {
300                     *tmp_str = '\0';
301                 }
302             }
303         }
304     }
305
306
307     if(config_inst.agentip == NULL)
308     {
309         config_inst.agentid = strdup(ST_NOTSET);
310         config_inst.agentname = strdup("Auth key not imported.");
311         config_inst.agentip = ST_NOTSET;
312
313         config_inst.status = ST_MISSING_IMPORT;
314     }
315
316
317     /* Getting server ip */
318     if(!get_ossec_server())
319     {
320         if(strcmp(config_inst.status, ST_MISSING_IMPORT) == 0)
321         {
322             config_inst.status = ST_MISSING_ALL;
323         }
324         else
325         {
326             config_inst.status = ST_MISSING_SERVER;
327         }
328     }
329
330     return(0);
331 }
332
333
334 /* Get OSSEC Server IP */
335 int get_ossec_server()
336 {
337     OS_XML xml;
338
339     char *str = NULL;
340
341
342     /* Definitions */
343     const char *(xml_serverip[])={"ossec_config","client","server-ip", NULL};
344     const char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL};
345
346
347     /* Reading XML */
348     if(OS_ReadXML(CONFIG, &xml) < 0)
349     {
350         return(0);
351     }
352
353
354     /* We need to remove the entry for the server */
355     if(config_inst.server)
356     {
357         free(config_inst.server);
358         config_inst.server = NULL;
359     }
360     config_inst.server_type = 0;
361
362
363     /* Getting ip */
364     str = OS_GetOneContentforElement(&xml, xml_serverip);
365     if(str && (OS_IsValidIP(str, NULL) == 1))
366     {
367         config_inst.server_type = SERVER_IP_USED;
368         config_inst.server = str;
369
370         OS_ClearXML(&xml);
371         return(1);
372     }
373     /* If we dont find the ip, try the server-hostname */
374     else
375     {
376         if(str)
377         {
378             free(str);
379             str = NULL;
380         }
381
382         str = OS_GetOneContentforElement(&xml, xml_serverhost);
383         if(str)
384         {
385             char *s_ip;
386             s_ip = OS_GetHost(str, 0);
387             if(s_ip)
388             {
389                 /* Clearing the host memory */
390                 free(s_ip);
391
392                 /* Assigning the hostname to the server info */
393                 config_inst.server_type = SERVER_HOST_USED;
394                 config_inst.server = str;
395                 OS_ClearXML(&xml);
396                 return(1);
397             }
398             free(str);
399         }
400     }
401
402
403     /* Setting up final server name when not available */
404     config_inst.server = strdup(FL_NOSERVER);
405
406
407     OS_ClearXML(&xml);
408     return(0);
409 }
410
411
412 /* Run a cmd.exe command */
413 int run_cmd(char *cmd, HWND hwnd)
414 {
415     int result;
416     int cmdlen;
417     char *comspec;
418     STARTUPINFO si;
419     PROCESS_INFORMATION pi;
420     DWORD exit_code;
421
422     /* Get cmd location from environment */
423     comspec = getenv("COMSPEC");
424     if (comspec == NULL || strncmp(comspec, "", strlen(comspec) == 0))
425     {
426         MessageBox(hwnd, "Could not determine the location of "
427                          "cmd.exe using the COMSPEC environment variable.",
428                          "Error -- Failure Locating cmd.exe",MB_OK);
429         return(0);
430     }
431
432     /* Build command */
433     cmdlen = strlen(comspec) + 5 + strlen(cmd);
434     char finalcmd[cmdlen];
435     snprintf(finalcmd, cmdlen, "%s /c %s", comspec, cmd);
436
437     /* Log command being run */
438     log2file("%s: INFO: Running the following command (%s)", ARGV0, finalcmd);
439
440     ZeroMemory(&si, sizeof(si));
441     si.cb = sizeof(si);
442     ZeroMemory(&pi, sizeof(pi));
443
444     if(!CreateProcess(NULL, finalcmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL,
445                       &si, &pi))
446     {
447         MessageBox(hwnd, "Unable to run command.",
448                          "Error -- Failure Running Command",MB_OK);
449         return(0);
450     }
451
452     /* Wait until process exits */
453     WaitForSingleObject(pi.hProcess, INFINITE);
454
455     /* Get exit code from command */
456     result = GetExitCodeProcess(pi.hProcess, &exit_code);
457
458     /* Close process and thread */
459     CloseHandle(pi.hProcess);
460     CloseHandle(pi.hThread);
461
462     if (!result)
463     {
464         MessageBox(hwnd, "Could not determine exit code from command.",
465                          "Error -- Failure Running Command",MB_OK);
466
467         return(0);
468     }
469
470     return(exit_code);
471 }
472
473
474 /* Set OSSEC Server IP */
475 int set_ossec_server(char *ip, HWND hwnd)
476 {
477     FILE *fp;
478     const char **xml_pt = NULL;
479     const char *(xml_serverip[])={"ossec_config","client","server-ip", NULL};
480     const char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL};
481     char *cacls;
482     int cmdlen;
483
484     /* Build command line to change permissions */
485     cacls = "echo y|cacls \"%s\" /T /G Administrators:f";
486     cmdlen = strlen(cacls) + strlen(NEWCONFIG);
487     char cmd[cmdlen];
488     snprintf(cmd, cmdlen, cacls, NEWCONFIG);
489
490     /* Verifying IP Address */
491     if(OS_IsValidIP(ip, NULL) != 1)
492     {
493         char *s_ip;
494         s_ip = OS_GetHost(ip, 0);
495
496         if(!s_ip)
497         {
498             MessageBox(hwnd, "Invalid Server IP Address.\r\n"
499                              "It must be the valid Ipv4 address of the "
500                              "OSSEC server or its resolvable hostname.",
501                              "Error -- Failure Setting IP",MB_OK);
502             return(0);
503         }
504         config_inst.server_type = SERVER_HOST_USED;
505         xml_pt = xml_serverhost;
506     }
507     else
508     {
509         config_inst.server_type = SERVER_IP_USED;
510         xml_pt = xml_serverip;
511     }
512
513     /* Create file */
514     fp = fopen(NEWCONFIG, "w");
515     if(fp)
516     {
517         fclose(fp);
518     }
519     else
520     {
521         MessageBox(hwnd, "Could not create configuration file.",
522                          "Error -- Failure Setting IP",MB_OK);
523         return(0);
524     }
525
526     /* Change permissions */
527     if (run_cmd(cmd, hwnd))
528     {
529         MessageBox(hwnd, "Unable to set permissions on new configuration file.",
530                          "Error -- Failure Setting IP",MB_OK);
531
532         /* Remove config */
533         if(unlink(NEWCONFIG))
534         {
535             MessageBox(hwnd, "Unable to remove new configuration file.",
536                              "Error -- Failure Setting IP",MB_OK);
537         }
538
539         return(0);
540     }
541
542     /* Reading the XML. Printing error and line number. */
543     if(OS_WriteXML(CONFIG, NEWCONFIG, xml_pt,
544                    NULL, ip) != 0)
545     {
546         MessageBox(hwnd, "Unable to set OSSEC Server IP Address.\r\n"
547                          "(Internal error on the XML Write).",
548                          "Error -- Failure Setting IP",MB_OK);
549         return(0);
550     }
551
552     /* Renaming config files */
553     unlink(LASTCONFIG);
554     rename(CONFIG, LASTCONFIG);
555     rename(NEWCONFIG, CONFIG);
556
557     return(1);
558 }
559
560
561 /* Set OSSEC Authentication Key */
562 int set_ossec_key(char *key, HWND hwnd)
563 {
564     FILE *fp;
565     char *cacls;
566     int cmdlen;
567
568     /* Build command line to change permissions */
569     cacls = "echo y|cacls \"%s\" /T /G Administrators:f";
570     cmdlen = strlen(cacls) + strlen(AUTH_FILE);
571     char cmd[cmdlen];
572     snprintf(cmd, cmdlen, cacls, AUTH_FILE);
573
574     /* Create file */
575     fp = fopen(AUTH_FILE, "w");
576     if(fp)
577     {
578         fclose(fp);
579     }
580     else
581     {
582         MessageBox(hwnd, "Could not open auth key file.",
583                          "Error -- Failure Importing Key", MB_OK);
584         return(0);
585     }
586
587     /* Change permissions */
588     if (run_cmd(cmd, hwnd))
589     {
590         MessageBox(hwnd, "Unable to set permissions on auth key file.",
591                          "Error -- Failure Importing Key", MB_OK);
592
593         /* Remove config */
594         if(unlink(AUTH_FILE))
595         {
596             MessageBox(hwnd, "Unable to remove auth key file.",
597                              "Error -- Failure Importing Key", MB_OK);
598         }
599
600         return(0);
601     }
602
603     fp = fopen(AUTH_FILE, "w");
604     if(fp)
605     {
606         fprintf(fp, "%s", key);
607         fclose(fp);
608     }
609     else
610     {
611         MessageBox(hwnd, "Could not open auth key file for write.",
612                          "Error -- Failure Importing Key", MB_OK);
613         return(0);
614     }
615
616     return(1);
617 }
618
619
620 /* EOF */