3 /* Copyright (C) 2009 Trend Micro Inc.
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
11 * License details at the LICENSE file included with OSSEC or
12 * online at: http://www.ossec.net/en/licensing.html
16 #include "os_win32ui.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"
25 /* Generate server info (for the main status */
26 int gen_server_info(HWND hwnd)
28 memset(ui_server_info, '\0', 2048 +1);
29 snprintf(ui_server_info, 2048,
30 "Agent: %s (%s) - %s\r\n\r\n"
32 config_inst.agentname,
38 /* Initializing top */
39 if(config_inst.version)
41 SetDlgItemText(hwnd, UI_SERVER_TOP, config_inst.version);
42 SetDlgItemText(hwnd, UI_SERVER_INFO, ui_server_info);
45 /* Initializing auth key */
46 SetDlgItemText(hwnd, UI_SERVER_AUTH, config_inst.key);
48 /* Initializing server ip */
49 SetDlgItemText(hwnd, UI_SERVER_TEXT, config_inst.server);
51 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
56 /* Reads the first line of a specific file --must free after */
57 char *cat_file(char *file, FILE *fp2)
63 fp = fopen(file, "r");
76 if(fgets(buf, 1024, fp) != NULL)
78 ret = strchr(buf, '\n');
83 ret = strchr(buf, '\r');
103 /* Check if a file exists */
104 int is_file(char *file)
107 fp = fopen(file, "r");
117 /* Clear configuration */
120 debug2("read config 1");
121 if(config_inst.version)
123 free(config_inst.version);
126 debug2("read config 2");
129 free(config_inst.key);
132 debug2("read config 3");
133 if(config_inst.agentid)
135 free(config_inst.agentid);
138 debug2("read config 4");
139 if(config_inst.server)
141 free(config_inst.server);
143 debug2("read config 5");
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;
152 config_inst.agentid = NULL;
153 config_inst.agentname= NULL;
154 config_inst.agentip = NULL;
156 config_inst.version = NULL;
157 config_inst.install_date = NULL;
158 config_inst.status = ST_UNKNOWN;
159 config_inst.msg_sent = 0;
161 debug2("read config 6");
165 /* Initializes the config */
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;
174 config_inst.agentid = NULL;
175 config_inst.agentname= NULL;
176 config_inst.agentip = NULL;
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;
185 /* Checking if ui is on the right path */
191 config_inst.admin_access = -1;
196 /* Testing for permission - this is a vista thing.
197 * For some reason vista is not reporting the return codes
202 fp = fopen(CONFIG, "a");
209 config_inst.admin_access = 0;
213 fp = fopen(".test-file.tst", "w");
216 if(fprintf(fp, ".test\n") == -1)
218 config_inst.admin_access = 0;
223 /* trying to open it to read. */
224 fp = fopen(".test-file.tst", "r");
231 config_inst.admin_access = 0;
234 if(unlink(".test-file.tst"))
236 config_inst.admin_access = 0;
241 config_inst.admin_access = 0;
247 /* Reads ossec config */
248 int config_read(HWND hwnd)
253 /* Clearing config */
257 /* Getting OSSEC status */
258 if(CheckServiceRunning())
260 config_inst.status = ST_RUNNING;
264 config_inst.status = ST_STOPPED;
268 /* Getting version/install date */
269 config_inst.version = cat_file(VERSION_FILE, NULL);
270 if(config_inst.version)
272 config_inst.install_date = strchr(config_inst.version, '-');
273 if(config_inst.install_date)
275 *config_inst.install_date = '\0';
276 config_inst.install_date++;
281 /* Getting number of messages sent */
282 tmp_str = cat_file(SENDER_FILE, NULL);
285 unsigned long int tmp_val = 0;
286 char *to_free = tmp_str;
288 tmp_val = atol(tmp_str);
291 config_inst.msg_sent = tmp_val * 9999;
293 tmp_str = strchr(tmp_str, ':');
297 tmp_val = atol(tmp_str);
298 config_inst.msg_sent += tmp_val;
306 /* Getting agent id, name and ip */
307 tmp_str = cat_file(AUTH_FILE, NULL);
310 /* Getting base 64 */
311 config_inst.key = encode_base64(strlen(tmp_str),tmp_str);
312 if(config_inst.key == NULL)
314 config_inst.key = FL_NOKEY;
318 config_inst.agentid = tmp_str;
320 tmp_str = strchr(tmp_str, ' ');
327 config_inst.agentname = tmp_str;
328 tmp_str = strchr(tmp_str, ' ');
335 config_inst.agentip = tmp_str;
337 tmp_str = strchr(tmp_str, ' ');
347 if(config_inst.agentip == NULL)
349 config_inst.agentid = strdup(ST_NOTSET);
350 config_inst.agentname = strdup("Auth key not imported.");
351 config_inst.agentip = ST_NOTSET;
353 config_inst.status = ST_MISSING_IMPORT;
357 /* Getting server ip */
358 if(!get_ossec_server())
360 if(config_inst.status == ST_MISSING_IMPORT)
362 config_inst.status = ST_MISSING_ALL;
366 config_inst.status = ST_MISSING_SERVER;
374 /* Get OSSEC Server IP */
375 int get_ossec_server()
383 char *(xml_serverip[])={"ossec_config","client","server-ip", NULL};
384 char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL};
388 if(OS_ReadXML(CONFIG, &xml) < 0)
394 /* We need to remove the entry for the server */
395 if(config_inst.server)
397 free(config_inst.server);
398 config_inst.server = NULL;
400 config_inst.server_type = 0;
404 str = OS_GetOneContentforElement(&xml, xml_serverip);
405 if(str && (OS_IsValidIP(str, NULL) == 1))
407 config_inst.server_type = SERVER_IP_USED;
408 config_inst.server = str;
413 /* If we dont find the ip, try the server-hostname */
422 str = OS_GetOneContentforElement(&xml, xml_serverhost);
426 s_ip = OS_GetHost(str, 0);
429 /* Clearing the host memory */
432 /* Assigning the hostname to the server info */
433 config_inst.server_type = SERVER_HOST_USED;
434 config_inst.server = str;
443 /* Setting up final server name when not available */
444 config_inst.server = strdup(FL_NOSERVER);
452 /* Set OSSEC Server IP */
453 int set_ossec_server(char *ip, HWND hwnd)
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};
460 /* Verifying IP Address */
461 if(OS_IsValidIP(ip, NULL) != 1)
464 s_ip = OS_GetHost(ip, 0);
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);
474 config_inst.server_type = SERVER_HOST_USED;
475 xml_pt = xml_serverhost;
479 config_inst.server_type = SERVER_IP_USED;
480 xml_pt = xml_serverip;
485 /* Reading the XML. Printing error and line number */
486 if(OS_WriteXML(CONFIG, NEWCONFIG, xml_pt,
487 NULL, NULL, ip, 0) != 0)
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);
495 /* Renaming config files */
497 rename(CONFIG, LASTCONFIG);
498 rename(NEWCONFIG, CONFIG);