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