1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
11 #include "os_win32ui.h"
12 #include "../os_win.h"
13 #include "os_xml/os_xml.h"
14 #include "os_net/os_net.h"
15 #include "validate_op.h"
18 /* Generate server info (for the main status) */
19 int gen_server_info(HWND hwnd)
21 memset(ui_server_info, '\0', 2048 + 1);
22 snprintf(ui_server_info, 2048,
23 "Agent: %s (%s) - %s\r\n\r\n"
25 config_inst.agentname,
31 if (config_inst.version) {
32 SetDlgItemText(hwnd, UI_SERVER_TOP, config_inst.version);
33 SetDlgItemText(hwnd, UI_SERVER_INFO, ui_server_info);
36 /* Initialize auth key */
37 SetDlgItemText(hwnd, UI_SERVER_AUTH, config_inst.key);
39 /* Initialize server IP */
40 SetDlgItemText(hwnd, UI_SERVER_TEXT, config_inst.server);
43 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
44 if (config_inst.install_date) {
45 SendMessage(hStatus, SB_SETTEXT, 1, (LPARAM)config_inst.install_date);
51 /* Read the first line of a specific file --must free after */
52 char *cat_file(char *file, FILE *fp2)
57 fp = fopen(file, "r");
67 if (fgets(buf, 1024, fp) != NULL) {
68 ret = strchr(buf, '\n');
72 ret = strchr(buf, '\r');
90 /* Check if a file exists */
91 int is_file(char *file)
94 fp = fopen(file, "r");
102 /* Clear configuration */
105 if (config_inst.version) {
106 free(config_inst.version);
109 if (config_inst.key) {
110 free(config_inst.key);
113 if (config_inst.agentid) {
114 free(config_inst.agentid);
117 if (config_inst.server) {
118 free(config_inst.server);
121 if (config_inst.install_date) {
122 free(config_inst.install_date);
125 /* Initialize config instance */
126 config_inst.dir = NULL;
127 config_inst.key = FL_NOKEY;
128 config_inst.server = strdup(FL_NOSERVER);
129 config_inst.config = NULL;
131 config_inst.agentid = NULL;
132 config_inst.agentname = NULL;
133 config_inst.agentip = NULL;
135 config_inst.version = NULL;
136 config_inst.install_date = NULL;
137 config_inst.status = ST_UNKNOWN;
138 config_inst.msg_sent = 0;
141 /* Initialize the config */
144 /* Initialize config instance */
145 config_inst.dir = NULL;
146 config_inst.key = FL_NOKEY;
147 config_inst.server = NULL;
148 config_inst.config = NULL;
150 config_inst.agentid = NULL;
151 config_inst.agentname = NULL;
152 config_inst.agentip = NULL;
154 config_inst.version = NULL;
155 config_inst.install_date = NULL;
156 config_inst.status = ST_UNKNOWN;
157 config_inst.msg_sent = 0;
158 config_inst.admin_access = 1;
160 /* Check if ui is on the right path and has the proper permissions */
161 if (!is_file(CONFIG)) {
163 config_inst.admin_access = 0;
166 if (!is_file(CONFIG)) {
167 config_inst.admin_access = 0;
172 /* Read ossec config */
173 int config_read(__attribute__((unused)) HWND hwnd)
181 /* Get OSSEC status */
182 if (CheckServiceRunning()) {
183 config_inst.status = ST_RUNNING;
185 config_inst.status = ST_STOPPED;
188 /* Get version/install date */
189 config_inst.version = cat_file(VERSION_FILE, NULL);
190 if (config_inst.version) {
191 config_inst.install_date = strstr(config_inst.version, delim);
192 if (config_inst.install_date) {
193 *config_inst.install_date = '\0';
194 config_inst.install_date += strlen(delim);
198 /* Get number of messages sent */
199 tmp_str = cat_file(SENDER_FILE, NULL);
201 unsigned long int tmp_val = 0;
202 char *to_free = tmp_str;
204 tmp_val = atol(tmp_str);
206 config_inst.msg_sent = tmp_val * 9999;
208 tmp_str = strchr(tmp_str, ':');
211 tmp_val = atol(tmp_str);
212 config_inst.msg_sent += tmp_val;
219 /* Get agent ID, name and IP */
220 tmp_str = cat_file(AUTH_FILE, NULL);
223 config_inst.key = encode_base64(strlen(tmp_str), tmp_str);
224 if (config_inst.key == NULL) {
225 config_inst.key = FL_NOKEY;
229 config_inst.agentid = tmp_str;
231 tmp_str = strchr(tmp_str, ' ');
237 config_inst.agentname = tmp_str;
238 tmp_str = strchr(tmp_str, ' ');
244 config_inst.agentip = tmp_str;
246 tmp_str = strchr(tmp_str, ' ');
254 if (config_inst.agentip == NULL) {
255 config_inst.agentid = strdup(ST_NOTSET);
256 config_inst.agentname = strdup("Auth key not imported.");
257 config_inst.agentip = ST_NOTSET;
259 config_inst.status = ST_MISSING_IMPORT;
263 if (!get_ossec_server()) {
264 if (strcmp(config_inst.status, ST_MISSING_IMPORT) == 0) {
265 config_inst.status = ST_MISSING_ALL;
267 config_inst.status = ST_MISSING_SERVER;
274 /* Get OSSEC Server IP */
275 int get_ossec_server()
281 const char *(xml_serverip[]) = {"ossec_config", "client", "server-ip", NULL};
282 const char *(xml_serverhost[]) = {"ossec_config", "client", "server-hostname", NULL};
285 if (OS_ReadXML(CONFIG, &xml) < 0) {
289 /* We need to remove the entry for the server */
290 if (config_inst.server) {
291 free(config_inst.server);
292 config_inst.server = NULL;
294 config_inst.server_type = 0;
297 str = OS_GetOneContentforElement(&xml, xml_serverip);
298 if (str && (OS_IsValidIP(str, NULL) == 1)) {
299 config_inst.server_type = SERVER_IP_USED;
300 config_inst.server = str;
305 /* If we don't find the IP, try the server hostname */
312 str = OS_GetOneContentforElement(&xml, xml_serverhost);
315 s_ip = OS_GetHost(str, 0);
317 /* Clear the host memory */
320 /* Assign the hostname to the server info */
321 config_inst.server_type = SERVER_HOST_USED;
322 config_inst.server = str;
330 /* Set up final server name when not available */
331 config_inst.server = strdup(FL_NOSERVER);
337 /* Run a cmd.exe command */
338 int run_cmd(char *cmd, HWND hwnd)
343 PROCESS_INFORMATION pi;
347 cmdlen = strlen(COMSPEC) + 5 + strlen(cmd);
348 char finalcmd[cmdlen];
349 snprintf(finalcmd, cmdlen, "%s /c %s", COMSPEC, cmd);
351 /* Log command being run */
352 log2file("%s: INFO: Running the following command (%s)", ARGV0, finalcmd);
354 ZeroMemory(&si, sizeof(si));
356 ZeroMemory(&pi, sizeof(pi));
358 if (!CreateProcess(NULL, finalcmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL,
360 MessageBox(hwnd, "Unable to run command.",
361 "Error -- Failure Running Command", MB_OK);
365 /* Wait until process exits */
366 WaitForSingleObject(pi.hProcess, INFINITE);
368 /* Get exit code from command */
369 result = GetExitCodeProcess(pi.hProcess, &exit_code);
371 /* Close process and thread */
372 CloseHandle(pi.hProcess);
373 CloseHandle(pi.hThread);
376 MessageBox(hwnd, "Could not determine exit code from command.",
377 "Error -- Failure Running Command", MB_OK);
385 /* Set OSSEC Server IP */
386 int set_ossec_server(char *ip, HWND hwnd)
388 const char **xml_pt = NULL;
389 const char *(xml_serverip[]) = {"ossec_config", "client", "server-ip", NULL};
390 const char *(xml_serverhost[]) = {"ossec_config", "client", "server-hostname", NULL};
392 char config_tmp[] = CONFIG;
393 char *conf_file = basename_ex(config_tmp);
395 char tmp_path[strlen(TMP_DIR) + 1 + strlen(conf_file) + 6 + 1];
397 snprintf(tmp_path, sizeof(tmp_path), "%s/%sXXXXXX", TMP_DIR, conf_file);
399 /* Verify IP Address */
400 if (OS_IsValidIP(ip, NULL) != 1) {
402 s_ip = OS_GetHost(ip, 0);
405 MessageBox(hwnd, "Invalid Server.\r\n"
406 "It must be the valid address of the "
407 "OSSEC server or the resolvable hostname.",
408 "Error -- Failure Setting IP", MB_OK);
411 config_inst.server_type = SERVER_HOST_USED;
412 xml_pt = xml_serverhost;
414 config_inst.server_type = SERVER_IP_USED;
415 xml_pt = xml_serverip;
418 /* Create temporary file */
419 if (mkstemp_ex(tmp_path) == -1) {
420 MessageBox(hwnd, "Could not create temporary file.",
421 "Error -- Failure Setting IP", MB_OK);
425 /* Read the XML. Print error and line number. */
426 if (OS_WriteXML(CONFIG, tmp_path, xml_pt, NULL, ip) != 0) {
427 MessageBox(hwnd, "Unable to set OSSEC Server IP Address.\r\n"
428 "(Internal error on the XML Write).",
429 "Error -- Failure Setting IP", MB_OK);
431 if (unlink(tmp_path)) {
432 MessageBox(hwnd, "Could not delete temporary file.",
433 "Error -- Failure Deleting Temporary File", MB_OK);
439 /* Rename config files */
440 if (rename_ex(CONFIG, LASTCONFIG)) {
441 MessageBox(hwnd, "Unable to backup configuration.",
442 "Error -- Failure Backing Up Configuration", MB_OK);
444 if (unlink(tmp_path)) {
445 MessageBox(hwnd, "Could not delete temporary file.",
446 "Error -- Failure Deleting Temporary File", MB_OK);
452 if (rename_ex(tmp_path, CONFIG)) {
453 MessageBox(hwnd, "Unable rename temporary file.",
454 "Error -- Failure Renaming Temporary File", MB_OK);
456 if (unlink(tmp_path)) {
457 MessageBox(hwnd, "Could not delete temporary file.",
458 "Error -- Failure Deleting Temporary File", MB_OK);
467 /* Set OSSEC Authentication Key */
468 int set_ossec_key(char *key, HWND hwnd)
472 char auth_file_tmp[] = AUTH_FILE;
473 char *keys_file = basename_ex(auth_file_tmp);
475 char tmp_path[strlen(TMP_DIR) + 1 + strlen(keys_file) + 6 + 1];
477 snprintf(tmp_path, sizeof(tmp_path), "%s/%sXXXXXX", TMP_DIR, keys_file);
479 /* Create temporary file */
480 if (mkstemp_ex(tmp_path) == -1) {
481 MessageBox(hwnd, "Could not create temporary file.",
482 "Error -- Failure Setting IP", MB_OK);
486 fp = fopen(tmp_path, "w");
488 fprintf(fp, "%s", key);
491 MessageBox(hwnd, "Could not open temporary file for write.",
492 "Error -- Failure Importing Key", MB_OK);
494 if (unlink(tmp_path)) {
495 MessageBox(hwnd, "Could not delete temporary file.",
496 "Error -- Failure Deleting Temporary File", MB_OK);
502 if (rename_ex(tmp_path, AUTH_FILE)) {
503 MessageBox(hwnd, "Unable to rename temporary file.",
504 "Error -- Failure Renaming Temporary File", MB_OK);
506 if (unlink(tmp_path)) {
507 MessageBox(hwnd, "Could not delete temporary file.",
508 "Error -- Failure Deleting Temporary File", MB_OK);