1 /* @(#) $Id: ./src/win32/ui/common.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/en/licensing.html
17 #include "os_win32ui.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"
26 /* Generate server info (for the main status */
27 int gen_server_info(HWND hwnd)
29 memset(ui_server_info, '\0', 2048 +1);
30 snprintf(ui_server_info, 2048,
31 "Agent: %s (%s) - %s\r\n\r\n"
33 config_inst.agentname,
39 /* Initializing top */
40 if(config_inst.version)
42 SetDlgItemText(hwnd, UI_SERVER_TOP, config_inst.version);
43 SetDlgItemText(hwnd, UI_SERVER_INFO, ui_server_info);
46 /* Initializing auth key */
47 SetDlgItemText(hwnd, UI_SERVER_AUTH, config_inst.key);
49 /* Initializing server ip */
50 SetDlgItemText(hwnd, UI_SERVER_TEXT, config_inst.server);
52 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
57 /* Reads the first line of a specific file --must free after */
58 char *cat_file(char *file, FILE *fp2)
64 fp = fopen(file, "r");
77 if(fgets(buf, 1024, fp) != NULL)
79 ret = strchr(buf, '\n');
84 ret = strchr(buf, '\r');
104 /* Check if a file exists */
105 int is_file(char *file)
108 fp = fopen(file, "r");
118 /* Clear configuration */
121 debug2("read config 1");
122 if(config_inst.version)
124 free(config_inst.version);
127 debug2("read config 2");
130 free(config_inst.key);
133 debug2("read config 3");
134 if(config_inst.agentid)
136 free(config_inst.agentid);
139 debug2("read config 4");
140 if(config_inst.server)
142 free(config_inst.server);
144 debug2("read config 5");
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;
153 config_inst.agentid = NULL;
154 config_inst.agentname= NULL;
155 config_inst.agentip = NULL;
157 config_inst.version = NULL;
158 config_inst.install_date = NULL;
159 config_inst.status = ST_UNKNOWN;
160 config_inst.msg_sent = 0;
162 debug2("read config 6");
166 /* Initializes the config */
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;
175 config_inst.agentid = NULL;
176 config_inst.agentname= NULL;
177 config_inst.agentip = NULL;
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;
186 /* Checking if ui is on the right path */
192 config_inst.admin_access = -1;
197 /* Testing for permission - this is a vista thing.
198 * For some reason vista is not reporting the return codes
203 fp = fopen(CONFIG, "a");
210 config_inst.admin_access = 0;
214 fp = fopen(".test-file.tst", "w");
217 if(fprintf(fp, ".test\n") == -1)
219 config_inst.admin_access = 0;
224 /* trying to open it to read. */
225 fp = fopen(".test-file.tst", "r");
232 config_inst.admin_access = 0;
235 if(unlink(".test-file.tst"))
237 config_inst.admin_access = 0;
242 config_inst.admin_access = 0;
248 /* Reads ossec config */
249 int config_read(HWND hwnd)
254 /* Clearing config */
258 /* Getting OSSEC status */
259 if(CheckServiceRunning())
261 config_inst.status = ST_RUNNING;
265 config_inst.status = ST_STOPPED;
269 /* Getting version/install date */
270 config_inst.version = cat_file(VERSION_FILE, NULL);
271 if(config_inst.version)
273 config_inst.install_date = strchr(config_inst.version, '-');
274 if(config_inst.install_date)
276 *config_inst.install_date = '\0';
277 config_inst.install_date++;
282 /* Getting number of messages sent */
283 tmp_str = cat_file(SENDER_FILE, NULL);
286 unsigned long int tmp_val = 0;
287 char *to_free = tmp_str;
289 tmp_val = atol(tmp_str);
292 config_inst.msg_sent = tmp_val * 9999;
294 tmp_str = strchr(tmp_str, ':');
298 tmp_val = atol(tmp_str);
299 config_inst.msg_sent += tmp_val;
307 /* Getting agent id, name and ip */
308 tmp_str = cat_file(AUTH_FILE, NULL);
311 /* Getting base 64 */
312 config_inst.key = encode_base64(strlen(tmp_str),tmp_str);
313 if(config_inst.key == NULL)
315 config_inst.key = FL_NOKEY;
319 config_inst.agentid = tmp_str;
321 tmp_str = strchr(tmp_str, ' ');
328 config_inst.agentname = tmp_str;
329 tmp_str = strchr(tmp_str, ' ');
336 config_inst.agentip = tmp_str;
338 tmp_str = strchr(tmp_str, ' ');
348 if(config_inst.agentip == NULL)
350 config_inst.agentid = strdup(ST_NOTSET);
351 config_inst.agentname = strdup("Auth key not imported.");
352 config_inst.agentip = ST_NOTSET;
354 config_inst.status = ST_MISSING_IMPORT;
358 /* Getting server ip */
359 if(!get_ossec_server())
361 if(strcmp(config_inst.status, ST_MISSING_IMPORT) == 0)
363 config_inst.status = ST_MISSING_ALL;
367 config_inst.status = ST_MISSING_SERVER;
375 /* Get OSSEC Server IP */
376 int get_ossec_server()
384 char *(xml_serverip[])={"ossec_config","client","server-ip", NULL};
385 char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL};
389 if(OS_ReadXML(CONFIG, &xml) < 0)
395 /* We need to remove the entry for the server */
396 if(config_inst.server)
398 free(config_inst.server);
399 config_inst.server = NULL;
401 config_inst.server_type = 0;
405 str = OS_GetOneContentforElement(&xml, xml_serverip);
406 if(str && (OS_IsValidIP(str, NULL) == 1))
408 config_inst.server_type = SERVER_IP_USED;
409 config_inst.server = str;
414 /* If we dont find the ip, try the server-hostname */
423 str = OS_GetOneContentforElement(&xml, xml_serverhost);
427 s_ip = OS_GetHost(str, 0);
430 /* Clearing the host memory */
433 /* Assigning the hostname to the server info */
434 config_inst.server_type = SERVER_HOST_USED;
435 config_inst.server = str;
444 /* Setting up final server name when not available */
445 config_inst.server = strdup(FL_NOSERVER);
453 /* Set OSSEC Server IP */
454 int set_ossec_server(char *ip, HWND hwnd)
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};
461 /* Verifying IP Address */
462 if(OS_IsValidIP(ip, NULL) != 1)
465 s_ip = OS_GetHost(ip, 0);
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);
475 config_inst.server_type = SERVER_HOST_USED;
476 xml_pt = xml_serverhost;
480 config_inst.server_type = SERVER_IP_USED;
481 xml_pt = xml_serverip;
486 /* Reading the XML. Printing error and line number */
487 if(OS_WriteXML(CONFIG, NEWCONFIG, xml_pt,
488 NULL, NULL, ip, 0) != 0)
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);
496 /* Renaming config files */
498 rename(CONFIG, LASTCONFIG);
499 rename(NEWCONFIG, CONFIG);