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
18 #include "os_win32ui.h"
20 #include "os_xml/os_xml.h"
21 #include "os_net/os_net.h"
22 #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);
52 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net");
53 if (config_inst.install_date)
55 SendMessage(hStatus, SB_SETTEXT, 1, (LPARAM)config_inst.install_date);
62 /* Reads the first line of a specific file --must free after */
63 char *cat_file(char *file, FILE *fp2)
69 fp = fopen(file, "r");
82 if(fgets(buf, 1024, fp) != NULL)
84 ret = strchr(buf, '\n');
89 ret = strchr(buf, '\r');
109 /* Check if a file exists */
110 int is_file(char *file)
113 fp = fopen(file, "r");
123 /* Clear configuration */
126 if(config_inst.version)
128 free(config_inst.version);
133 free(config_inst.key);
136 if(config_inst.agentid)
138 free(config_inst.agentid);
141 if(config_inst.server)
143 free(config_inst.server);
146 if(config_inst.install_date)
148 free(config_inst.install_date);
151 /* Initializing config instance */
152 config_inst.dir = NULL;
153 config_inst.key = FL_NOKEY;
154 config_inst.server = strdup(FL_NOSERVER);
155 config_inst.config = NULL;
157 config_inst.agentid = NULL;
158 config_inst.agentname= NULL;
159 config_inst.agentip = NULL;
161 config_inst.version = NULL;
162 config_inst.install_date = NULL;
163 config_inst.status = ST_UNKNOWN;
164 config_inst.msg_sent = 0;
168 /* Initializes the config */
171 /* Initializing config instance */
172 config_inst.dir = NULL;
173 config_inst.key = FL_NOKEY;
174 config_inst.server = NULL;
175 config_inst.config = NULL;
177 config_inst.agentid = NULL;
178 config_inst.agentname= NULL;
179 config_inst.agentip = NULL;
181 config_inst.version = NULL;
182 config_inst.install_date = NULL;
183 config_inst.status = ST_UNKNOWN;
184 config_inst.msg_sent = 0;
185 config_inst.admin_access = 1;
188 /* Checking if ui is on the right path
189 * and has the proper permissions
195 config_inst.admin_access = 0;
200 config_inst.admin_access = 0;
206 /* Reads ossec config */
207 int config_read(HWND hwnd)
213 /* Clearing config */
217 /* Getting OSSEC status */
218 if(CheckServiceRunning())
220 config_inst.status = ST_RUNNING;
224 config_inst.status = ST_STOPPED;
228 /* Getting version/install date */
229 config_inst.version = cat_file(VERSION_FILE, NULL);
230 if(config_inst.version)
232 config_inst.install_date = strstr(config_inst.version, delim);
233 if(config_inst.install_date)
235 *config_inst.install_date = '\0';
236 config_inst.install_date += strlen(delim);
241 /* Getting number of messages sent */
242 tmp_str = cat_file(SENDER_FILE, NULL);
245 unsigned long int tmp_val = 0;
246 char *to_free = tmp_str;
248 tmp_val = atol(tmp_str);
251 config_inst.msg_sent = tmp_val * 9999;
253 tmp_str = strchr(tmp_str, ':');
257 tmp_val = atol(tmp_str);
258 config_inst.msg_sent += tmp_val;
266 /* Getting agent id, name and ip */
267 tmp_str = cat_file(AUTH_FILE, NULL);
270 /* Getting base 64 */
271 config_inst.key = encode_base64(strlen(tmp_str),tmp_str);
272 if(config_inst.key == NULL)
274 config_inst.key = FL_NOKEY;
278 config_inst.agentid = tmp_str;
280 tmp_str = strchr(tmp_str, ' ');
287 config_inst.agentname = tmp_str;
288 tmp_str = strchr(tmp_str, ' ');
295 config_inst.agentip = tmp_str;
297 tmp_str = strchr(tmp_str, ' ');
307 if(config_inst.agentip == NULL)
309 config_inst.agentid = strdup(ST_NOTSET);
310 config_inst.agentname = strdup("Auth key not imported.");
311 config_inst.agentip = ST_NOTSET;
313 config_inst.status = ST_MISSING_IMPORT;
317 /* Getting server ip */
318 if(!get_ossec_server())
320 if(strcmp(config_inst.status, ST_MISSING_IMPORT) == 0)
322 config_inst.status = ST_MISSING_ALL;
326 config_inst.status = ST_MISSING_SERVER;
334 /* Get OSSEC Server IP */
335 int get_ossec_server()
343 const char *(xml_serverip[])={"ossec_config","client","server-ip", NULL};
344 const char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL};
348 if(OS_ReadXML(CONFIG, &xml) < 0)
354 /* We need to remove the entry for the server */
355 if(config_inst.server)
357 free(config_inst.server);
358 config_inst.server = NULL;
360 config_inst.server_type = 0;
364 str = OS_GetOneContentforElement(&xml, xml_serverip);
365 if(str && (OS_IsValidIP(str, NULL) == 1))
367 config_inst.server_type = SERVER_IP_USED;
368 config_inst.server = str;
373 /* If we dont find the ip, try the server-hostname */
382 str = OS_GetOneContentforElement(&xml, xml_serverhost);
386 s_ip = OS_GetHost(str, 0);
389 /* Clearing the host memory */
392 /* Assigning the hostname to the server info */
393 config_inst.server_type = SERVER_HOST_USED;
394 config_inst.server = str;
403 /* Setting up final server name when not available */
404 config_inst.server = strdup(FL_NOSERVER);
412 /* Run a cmd.exe command */
413 int run_cmd(char *cmd, HWND hwnd)
419 PROCESS_INFORMATION pi;
422 /* Get cmd location from environment */
423 comspec = getenv("COMSPEC");
424 if (comspec == NULL || strncmp(comspec, "", strlen(comspec) == 0))
426 MessageBox(hwnd, "Could not determine the location of "
427 "cmd.exe using the COMSPEC environment variable.",
428 "Error -- Failure Locating cmd.exe",MB_OK);
433 cmdlen = strlen(comspec) + 5 + strlen(cmd);
434 char finalcmd[cmdlen];
435 snprintf(finalcmd, cmdlen, "%s /c %s", comspec, cmd);
437 /* Log command being run */
438 log2file("%s: INFO: Running the following command (%s)", ARGV0, finalcmd);
440 ZeroMemory(&si, sizeof(si));
442 ZeroMemory(&pi, sizeof(pi));
444 if(!CreateProcess(NULL, finalcmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL,
447 MessageBox(hwnd, "Unable to run command.",
448 "Error -- Failure Running Command",MB_OK);
452 /* Wait until process exits */
453 WaitForSingleObject(pi.hProcess, INFINITE);
455 /* Get exit code from command */
456 result = GetExitCodeProcess(pi.hProcess, &exit_code);
458 /* Close process and thread */
459 CloseHandle(pi.hProcess);
460 CloseHandle(pi.hThread);
464 MessageBox(hwnd, "Could not determine exit code from command.",
465 "Error -- Failure Running Command",MB_OK);
474 /* Set OSSEC Server IP */
475 int set_ossec_server(char *ip, HWND hwnd)
478 const char **xml_pt = NULL;
479 const char *(xml_serverip[])={"ossec_config","client","server-ip", NULL};
480 const char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL};
484 /* Build command line to change permissions */
485 cacls = "echo y|cacls \"%s\" /T /G Administrators:f";
486 cmdlen = strlen(cacls) + strlen(NEWCONFIG);
488 snprintf(cmd, cmdlen, cacls, NEWCONFIG);
490 /* Verifying IP Address */
491 if(OS_IsValidIP(ip, NULL) != 1)
494 s_ip = OS_GetHost(ip, 0);
498 MessageBox(hwnd, "Invalid Server IP Address.\r\n"
499 "It must be the valid Ipv4 address of the "
500 "OSSEC server or its resolvable hostname.",
501 "Error -- Failure Setting IP",MB_OK);
504 config_inst.server_type = SERVER_HOST_USED;
505 xml_pt = xml_serverhost;
509 config_inst.server_type = SERVER_IP_USED;
510 xml_pt = xml_serverip;
514 fp = fopen(NEWCONFIG, "w");
521 MessageBox(hwnd, "Could not create configuration file.",
522 "Error -- Failure Setting IP",MB_OK);
526 /* Change permissions */
527 if (run_cmd(cmd, hwnd))
529 MessageBox(hwnd, "Unable to set permissions on new configuration file.",
530 "Error -- Failure Setting IP",MB_OK);
533 if(unlink(NEWCONFIG))
535 MessageBox(hwnd, "Unable to remove new configuration file.",
536 "Error -- Failure Setting IP",MB_OK);
542 /* Reading the XML. Printing error and line number. */
543 if(OS_WriteXML(CONFIG, NEWCONFIG, xml_pt,
546 MessageBox(hwnd, "Unable to set OSSEC Server IP Address.\r\n"
547 "(Internal error on the XML Write).",
548 "Error -- Failure Setting IP",MB_OK);
552 /* Renaming config files */
554 rename(CONFIG, LASTCONFIG);
555 rename(NEWCONFIG, CONFIG);
561 /* Set OSSEC Authentication Key */
562 int set_ossec_key(char *key, HWND hwnd)
568 /* Build command line to change permissions */
569 cacls = "echo y|cacls \"%s\" /T /G Administrators:f";
570 cmdlen = strlen(cacls) + strlen(AUTH_FILE);
572 snprintf(cmd, cmdlen, cacls, AUTH_FILE);
575 fp = fopen(AUTH_FILE, "w");
582 MessageBox(hwnd, "Could not open auth key file.",
583 "Error -- Failure Importing Key", MB_OK);
587 /* Change permissions */
588 if (run_cmd(cmd, hwnd))
590 MessageBox(hwnd, "Unable to set permissions on auth key file.",
591 "Error -- Failure Importing Key", MB_OK);
594 if(unlink(AUTH_FILE))
596 MessageBox(hwnd, "Unable to remove auth key file.",
597 "Error -- Failure Importing Key", MB_OK);
603 fp = fopen(AUTH_FILE, "w");
606 fprintf(fp, "%s", key);
611 MessageBox(hwnd, "Could not open auth key file for write.",
612 "Error -- Failure Importing Key", MB_OK);