Imported Upstream version 2.7
[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 "os_win32ui.h"
18 #include "os_win.h"
19 #include "os_xml/os_xml.h"
20 #include "os_xml/os_xml_writer.h"
21 #include "os_net/os_net.h"
22 #include "validate_op.h"
23 #include "shared.h"
24
25
26 /* Generate server info (for the main status */
27 int gen_server_info(HWND hwnd)
28 {
29     memset(ui_server_info, '\0', 2048 +1);
30     snprintf(ui_server_info, 2048,
31             "Agent: %s (%s)  -  %s\r\n\r\n"
32             "Status: %s",
33             config_inst.agentname,
34             config_inst.agentid,
35             config_inst.agentip,
36             config_inst.status);
37
38
39     /* Initializing top */
40     if(config_inst.version)
41     {
42         SetDlgItemText(hwnd, UI_SERVER_TOP, config_inst.version);
43         SetDlgItemText(hwnd, UI_SERVER_INFO, ui_server_info);
44     }
45
46     /* Initializing auth key */
47     SetDlgItemText(hwnd, UI_SERVER_AUTH, config_inst.key);
48
49     /* Initializing server ip */
50     SetDlgItemText(hwnd, UI_SERVER_TEXT, config_inst.server);
51
52     SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
53     return(0);
54 }
55
56
57 /* Reads the first line of a specific file  --must free after */
58 char *cat_file(char *file, FILE *fp2)
59 {
60     FILE *fp;
61
62     if(!fp2)
63     {
64         fp = fopen(file, "r");
65     }
66     else
67     {
68         fp = fp2;
69     }
70
71     if(fp)
72     {
73         char buf[1024 +1];
74         char *ret = NULL;
75
76         buf[1024] = '\0';
77         if(fgets(buf, 1024, fp) != NULL)
78         {
79             ret = strchr(buf, '\n');
80             if(ret)
81             {
82                 *ret = '\0';
83             }
84             ret = strchr(buf, '\r');
85             if(ret)
86             {
87                 *ret = '\0';
88             }
89
90             ret = strdup(buf);
91         }
92
93         if(!fp2)
94         {
95             fclose(fp);
96         }
97         return(ret);
98     }
99
100     return(NULL);
101 }
102
103
104 /* Check if a file exists */
105 int is_file(char *file)
106 {
107     FILE *fp;
108     fp = fopen(file, "r");
109     if(fp)
110     {
111         fclose(fp);
112         return(1);
113     }
114     return(0);
115 }
116
117
118 /* Clear configuration */
119 void config_clear()
120 {
121         debug2("read config 1");
122     if(config_inst.version)
123     {
124         free(config_inst.version);
125     }
126
127         debug2("read config 2");
128     if(config_inst.key)
129     {
130         free(config_inst.key);
131     }
132
133         debug2("read config 3");
134     if(config_inst.agentid)
135     {
136         free(config_inst.agentid);
137     }
138
139         debug2("read config 4");
140     if(config_inst.server)
141     {
142         free(config_inst.server);
143     }
144         debug2("read config 5");
145
146
147     /* Initializing config instance */
148     config_inst.dir = NULL;
149     config_inst.key = FL_NOKEY;
150     config_inst.server = strdup(FL_NOSERVER);
151     config_inst.config = NULL;
152
153     config_inst.agentid = NULL;
154     config_inst.agentname= NULL;
155     config_inst.agentip = NULL;
156
157     config_inst.version = NULL;
158     config_inst.install_date = NULL;
159     config_inst.status = ST_UNKNOWN;
160     config_inst.msg_sent = 0;
161
162         debug2("read config 6");
163 }
164
165
166 /* Initializes the config */
167 void init_config()
168 {
169     /* Initializing config instance */
170     config_inst.dir = NULL;
171     config_inst.key = FL_NOKEY;
172     config_inst.server = NULL;
173     config_inst.config = NULL;
174
175     config_inst.agentid = NULL;
176     config_inst.agentname= NULL;
177     config_inst.agentip = NULL;
178
179     config_inst.version = NULL;
180     config_inst.install_date = NULL;
181     config_inst.status = ST_UNKNOWN;
182     config_inst.msg_sent = 0;
183     config_inst.admin_access = 1;
184
185
186     /* Checking if ui is on the right path */
187     if(!is_file(CONFIG))
188     {
189         chdir(DEFDIR);
190         if(!is_file(CONFIG))
191         {
192             config_inst.admin_access = -1;
193         }
194     }
195
196
197     /* Testing for permission - this is a vista thing.
198      * For some reason vista is not reporting the return codes
199      * properly.
200      */
201     {
202         FILE *fp;
203         fp = fopen(CONFIG, "a");
204         if(fp)
205         {
206             fclose(fp);
207         }
208         else
209         {
210             config_inst.admin_access = 0;
211         }
212
213
214         fp = fopen(".test-file.tst", "w");
215         if(fp)
216         {
217             if(fprintf(fp, ".test\n") == -1)
218             {
219                 config_inst.admin_access = 0;
220             }
221
222             fclose(fp);
223
224             /* trying to open it to read. */
225             fp = fopen(".test-file.tst", "r");
226             if(fp)
227             {
228                 fclose(fp);
229             }
230             else
231             {
232                 config_inst.admin_access = 0;
233             }
234
235             if(unlink(".test-file.tst"))
236             {
237                 config_inst.admin_access = 0;
238             }
239         }
240         else
241         {
242             config_inst.admin_access = 0;
243         }
244     }
245 }
246
247
248 /* Reads ossec config */
249 int config_read(HWND hwnd)
250 {
251     char *tmp_str;
252
253
254     /* Clearing config */
255     config_clear();
256
257
258     /* Getting OSSEC status */
259     if(CheckServiceRunning())
260     {
261         config_inst.status = ST_RUNNING;
262     }
263     else
264     {
265         config_inst.status = ST_STOPPED;
266     }
267
268
269     /* Getting version/install date */
270     config_inst.version = cat_file(VERSION_FILE, NULL);
271     if(config_inst.version)
272     {
273         config_inst.install_date = strchr(config_inst.version, '-');
274         if(config_inst.install_date)
275         {
276             *config_inst.install_date = '\0';
277             config_inst.install_date++;
278         }
279     }
280
281
282     /* Getting number of messages sent */
283     tmp_str = cat_file(SENDER_FILE, NULL);
284     if(tmp_str)
285     {
286         unsigned long int tmp_val = 0;
287         char *to_free = tmp_str;
288
289         tmp_val = atol(tmp_str);
290         if(tmp_val)
291         {
292             config_inst.msg_sent = tmp_val * 9999;
293
294             tmp_str = strchr(tmp_str, ':');
295             if(tmp_str)
296             {
297                 tmp_str++;
298                 tmp_val = atol(tmp_str);
299                 config_inst.msg_sent += tmp_val;
300             }
301         }
302
303         free(to_free);
304     }
305
306
307     /* Getting agent id, name and ip */
308     tmp_str = cat_file(AUTH_FILE, NULL);
309     if(tmp_str)
310     {
311         /* Getting base 64 */
312         config_inst.key = encode_base64(strlen(tmp_str),tmp_str);
313         if(config_inst.key == NULL)
314         {
315             config_inst.key = FL_NOKEY;
316         }
317
318         /* Getting id */
319         config_inst.agentid = tmp_str;
320
321         tmp_str = strchr(tmp_str, ' ');
322         if(tmp_str)
323         {
324             *tmp_str = '\0';
325             tmp_str++;
326
327             /* Getting name */
328             config_inst.agentname = tmp_str;
329             tmp_str = strchr(tmp_str, ' ');
330             if(tmp_str)
331             {
332                 *tmp_str = '\0';
333                 tmp_str++;
334
335                 /* Getting ip */
336                 config_inst.agentip = tmp_str;
337
338                 tmp_str = strchr(tmp_str, ' ');
339                 if(tmp_str)
340                 {
341                     *tmp_str = '\0';
342                 }
343             }
344         }
345     }
346
347
348     if(config_inst.agentip == NULL)     
349     {
350         config_inst.agentid = strdup(ST_NOTSET);
351         config_inst.agentname = strdup("Auth key not imported.");
352         config_inst.agentip = ST_NOTSET;
353
354         config_inst.status = ST_MISSING_IMPORT;
355     }
356
357
358     /* Getting server ip */
359     if(!get_ossec_server())
360     {
361         if(strcmp(config_inst.status, ST_MISSING_IMPORT) == 0)
362         {
363             config_inst.status = ST_MISSING_ALL;
364         }
365         else
366         {
367             config_inst.status = ST_MISSING_SERVER;
368         }
369     }
370
371     return(0);
372 }
373
374
375 /* Get OSSEC Server IP */
376 int get_ossec_server()
377 {
378     OS_XML xml;
379
380     char *str = NULL;
381
382
383     /* Definitions */
384     char *(xml_serverip[])={"ossec_config","client","server-ip", NULL};
385     char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL};
386
387
388     /* Reading XML */
389     if(OS_ReadXML(CONFIG, &xml) < 0)
390     {
391         return(0);
392     }
393
394
395     /* We need to remove the entry for the server */
396     if(config_inst.server)
397     {
398         free(config_inst.server);
399         config_inst.server = NULL;
400     }
401     config_inst.server_type = 0;
402
403
404     /* Getting ip */
405     str = OS_GetOneContentforElement(&xml, xml_serverip);
406     if(str && (OS_IsValidIP(str, NULL) == 1))
407     {
408         config_inst.server_type = SERVER_IP_USED;
409         config_inst.server = str;
410
411         OS_ClearXML(&xml);
412         return(1);
413     }
414     /* If we dont find the ip, try the server-hostname */
415     else
416     {
417         if(str)
418         {
419             free(str);
420             str = NULL;
421         }
422
423         str = OS_GetOneContentforElement(&xml, xml_serverhost);
424         if(str)
425         {
426             char *s_ip;
427             s_ip = OS_GetHost(str, 0);
428             if(s_ip)
429             {
430                 /* Clearing the host memory */
431                 free(s_ip);
432
433                 /* Assigning the hostname to the server info */
434                 config_inst.server_type = SERVER_HOST_USED;
435                 config_inst.server = str;
436                 OS_ClearXML(&xml);
437                 return(1);
438             }
439             free(str);
440         }
441     }
442
443
444     /* Setting up final server name when not available */
445     config_inst.server = strdup(FL_NOSERVER);
446
447
448     OS_ClearXML(&xml);
449     return(0);
450 }
451
452
453 /* Set OSSEC Server IP */
454 int set_ossec_server(char *ip, HWND hwnd)
455 {
456     char **xml_pt = NULL;
457     char *(xml_serverip[])={"ossec_config","client","server-ip", NULL};
458     char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL};
459
460
461     /* Verifying IP Address */
462     if(OS_IsValidIP(ip, NULL) != 1)
463     {
464         char *s_ip;
465         s_ip = OS_GetHost(ip, 0);
466
467         if(!s_ip)
468         {
469             MessageBox(hwnd, "Invalid Server IP Address.\r\n"
470                              "It must be the valid Ipv4 address of the "
471                              "OSSEC server or its resolvable hostname.",
472                              "Invalid Server IP Address.",MB_OK);
473             return(0);
474         }
475         config_inst.server_type = SERVER_HOST_USED;
476         xml_pt = xml_serverhost;
477     }
478     else
479     {
480         config_inst.server_type = SERVER_IP_USED;
481         xml_pt = xml_serverip;
482     }
483
484
485
486     /* Reading the XML. Printing error and line number */
487     if(OS_WriteXML(CONFIG, NEWCONFIG, xml_pt,
488                    NULL, NULL, ip, 0) != 0)
489     {
490         MessageBox(hwnd, "Unable to set OSSEC Server IP Address.\r\n"
491                    "(Internal error on the XML Write).",
492                    "Unable to set Server IP Address.",MB_OK);
493         return(0);
494     }
495
496     /* Renaming config files */
497     unlink(LASTCONFIG);
498     rename(CONFIG, LASTCONFIG);
499     rename(NEWCONFIG, CONFIG);
500
501
502     return(1);
503 }
504
505
506 /* EOF */