X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?a=blobdiff_plain;f=src%2Fwin32%2Fui%2Fcommon.c;h=9bcc0cd13e4aacfbcec37c874a1ff24bce6e587c;hb=789cbc8e52da68eba3517b920ef22e000cf3c9fd;hp=e385a3675f85e392ae2a93082c5fe5f1f73f3d08;hpb=301048b51990573e58a30dc4a5bb4ec285cad554;p=ossec-hids.git diff --git a/src/win32/ui/common.c b/src/win32/ui/common.c index e385a36..9bcc0cd 100644 --- a/src/win32/ui/common.c +++ b/src/win32/ui/common.c @@ -1,4 +1,5 @@ -/* @(#) $Id$ */ +/* @(#) $Id: ./src/win32/ui/common.c, 2011/09/08 dcid Exp $ + */ /* Copyright (C) 2009 Trend Micro Inc. * All rights reserved. @@ -8,29 +9,28 @@ * License (version 2) as published by the FSF - Free Software * Foundation. * - * License details at the LICENSE file included with OSSEC or + * License details at the LICENSE file included with OSSEC or * online at: http://www.ossec.net/en/licensing.html */ +#include "shared.h" #include "os_win32ui.h" #include "os_win.h" #include "os_xml/os_xml.h" -#include "os_xml/os_xml_writer.h" #include "os_net/os_net.h" #include "validate_op.h" -#include "shared.h" -/* Generate server info (for the main status */ +/* Generate server info (for the main status) */ int gen_server_info(HWND hwnd) { memset(ui_server_info, '\0', 2048 +1); - snprintf(ui_server_info, 2048, + snprintf(ui_server_info, 2048, "Agent: %s (%s) - %s\r\n\r\n" "Status: %s", - config_inst.agentname, - config_inst.agentid, + config_inst.agentname, + config_inst.agentid, config_inst.agentip, config_inst.status); @@ -41,14 +41,20 @@ int gen_server_info(HWND hwnd) SetDlgItemText(hwnd, UI_SERVER_TOP, config_inst.version); SetDlgItemText(hwnd, UI_SERVER_INFO, ui_server_info); } - + /* Initializing auth key */ SetDlgItemText(hwnd, UI_SERVER_AUTH, config_inst.key); /* Initializing server ip */ SetDlgItemText(hwnd, UI_SERVER_TEXT, config_inst.server); - SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net"); + /* Set status data */ + SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"http://www.ossec.net"); + if (config_inst.install_date) + { + SendMessage(hStatus, SB_SETTEXT, 1, (LPARAM)config_inst.install_date); + } + return(0); } @@ -117,31 +123,30 @@ int is_file(char *file) /* Clear configuration */ void config_clear() { - debug2("read config 1"); if(config_inst.version) { free(config_inst.version); } - debug2("read config 2"); if(config_inst.key) { free(config_inst.key); } - debug2("read config 3"); if(config_inst.agentid) { free(config_inst.agentid); } - debug2("read config 4"); if(config_inst.server) { free(config_inst.server); } - debug2("read config 5"); + if(config_inst.install_date) + { + free(config_inst.install_date); + } /* Initializing config instance */ config_inst.dir = NULL; @@ -157,8 +162,6 @@ void config_clear() config_inst.install_date = NULL; config_inst.status = ST_UNKNOWN; config_inst.msg_sent = 0; - - debug2("read config 6"); } @@ -182,61 +185,17 @@ void init_config() config_inst.admin_access = 1; - /* Checking if ui is on the right path */ - if(!is_file(CONFIG)) - { - chdir(DEFDIR); - if(!is_file(CONFIG)) - { - config_inst.admin_access = -1; - } - } - - - /* Testing for permission - this is a vista thing. - * For some reason vista is not reporting the return codes - * properly. + /* Checking if ui is on the right path + * and has the proper permissions */ + if(!is_file(CONFIG)) { - FILE *fp; - fp = fopen(CONFIG, "a"); - if(fp) - { - fclose(fp); - } - else + if(chdir(DEFDIR)) { config_inst.admin_access = 0; } - - fp = fopen(".test-file.tst", "w"); - if(fp) - { - if(fprintf(fp, ".test\n") == -1) - { - config_inst.admin_access = 0; - } - - fclose(fp); - - /* trying to open it to read. */ - fp = fopen(".test-file.tst", "r"); - if(fp) - { - fclose(fp); - } - else - { - config_inst.admin_access = 0; - } - - if(unlink(".test-file.tst")) - { - config_inst.admin_access = 0; - } - } - else + if(!is_file(CONFIG)) { config_inst.admin_access = 0; } @@ -248,8 +207,9 @@ void init_config() int config_read(HWND hwnd) { char *tmp_str; + char *delim = " - "; + - /* Clearing config */ config_clear(); @@ -266,20 +226,20 @@ int config_read(HWND hwnd) /* Getting version/install date */ - config_inst.version = cat_file(VERSION_FILE, NULL); + config_inst.version = cat_file(VERSION_FILE, NULL); if(config_inst.version) { - config_inst.install_date = strchr(config_inst.version, '-'); + config_inst.install_date = strstr(config_inst.version, delim); if(config_inst.install_date) { *config_inst.install_date = '\0'; - config_inst.install_date++; + config_inst.install_date += strlen(delim); } } /* Getting number of messages sent */ - tmp_str = cat_file(SENDER_FILE, NULL); + tmp_str = cat_file(SENDER_FILE, NULL); if(tmp_str) { unsigned long int tmp_val = 0; @@ -344,7 +304,7 @@ int config_read(HWND hwnd) } - if(config_inst.agentip == NULL) + if(config_inst.agentip == NULL) { config_inst.agentid = strdup(ST_NOTSET); config_inst.agentname = strdup("Auth key not imported."); @@ -357,7 +317,7 @@ int config_read(HWND hwnd) /* Getting server ip */ if(!get_ossec_server()) { - if(config_inst.status == ST_MISSING_IMPORT) + if(strcmp(config_inst.status, ST_MISSING_IMPORT) == 0) { config_inst.status = ST_MISSING_ALL; } @@ -380,8 +340,8 @@ int get_ossec_server() /* Definitions */ - char *(xml_serverip[])={"ossec_config","client","server-ip", NULL}; - char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL}; + const char *(xml_serverip[])={"ossec_config","client","server-ip", NULL}; + const char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL}; /* Reading XML */ @@ -418,7 +378,7 @@ int get_ossec_server() free(str); str = NULL; } - + str = OS_GetOneContentforElement(&xml, xml_serverhost); if(str) { @@ -442,20 +402,90 @@ int get_ossec_server() /* Setting up final server name when not available */ config_inst.server = strdup(FL_NOSERVER); - + OS_ClearXML(&xml); return(0); } +/* Run a cmd.exe command */ +int run_cmd(char *cmd, HWND hwnd) +{ + int result; + int cmdlen; + char *comspec; + STARTUPINFO si; + PROCESS_INFORMATION pi; + DWORD exit_code; + + /* Get cmd location from environment */ + comspec = getenv("COMSPEC"); + if (comspec == NULL || strncmp(comspec, "", strlen(comspec) == 0)) + { + MessageBox(hwnd, "Could not determine the location of " + "cmd.exe using the COMSPEC environment variable.", + "Error -- Failure Locating cmd.exe",MB_OK); + return(0); + } + + /* Build command */ + cmdlen = strlen(comspec) + 5 + strlen(cmd); + char finalcmd[cmdlen]; + snprintf(finalcmd, cmdlen, "%s /c %s", comspec, cmd); + + /* Log command being run */ + log2file("%s: INFO: Running the following command (%s)", ARGV0, finalcmd); + + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + ZeroMemory(&pi, sizeof(pi)); + + if(!CreateProcess(NULL, finalcmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, + &si, &pi)) + { + MessageBox(hwnd, "Unable to run command.", + "Error -- Failure Running Command",MB_OK); + return(0); + } + + /* Wait until process exits */ + WaitForSingleObject(pi.hProcess, INFINITE); + + /* Get exit code from command */ + result = GetExitCodeProcess(pi.hProcess, &exit_code); + + /* Close process and thread */ + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + + if (!result) + { + MessageBox(hwnd, "Could not determine exit code from command.", + "Error -- Failure Running Command",MB_OK); + + return(0); + } + + return(exit_code); +} + + /* Set OSSEC Server IP */ int set_ossec_server(char *ip, HWND hwnd) { - char **xml_pt = NULL; - char *(xml_serverip[])={"ossec_config","client","server-ip", NULL}; - char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL}; - + FILE *fp; + const char **xml_pt = NULL; + const char *(xml_serverip[])={"ossec_config","client","server-ip", NULL}; + const char *(xml_serverhost[])={"ossec_config","client","server-hostname", NULL}; + char *cacls; + int cmdlen; + + /* Build command line to change permissions */ + cacls = "echo y|cacls \"%s\" /T /G Administrators:f"; + cmdlen = strlen(cacls) + strlen(NEWCONFIG); + char cmd[cmdlen]; + snprintf(cmd, cmdlen, cacls, NEWCONFIG); /* Verifying IP Address */ if(OS_IsValidIP(ip, NULL) != 1) @@ -468,7 +498,7 @@ int set_ossec_server(char *ip, HWND hwnd) MessageBox(hwnd, "Invalid Server IP Address.\r\n" "It must be the valid Ipv4 address of the " "OSSEC server or its resolvable hostname.", - "Invalid Server IP Address.",MB_OK); + "Error -- Failure Setting IP",MB_OK); return(0); } config_inst.server_type = SERVER_HOST_USED; @@ -480,15 +510,42 @@ int set_ossec_server(char *ip, HWND hwnd) xml_pt = xml_serverip; } + /* Create file */ + fp = fopen(NEWCONFIG, "w"); + if(fp) + { + fclose(fp); + } + else + { + MessageBox(hwnd, "Could not create configuration file.", + "Error -- Failure Setting IP",MB_OK); + return(0); + } + + /* Change permissions */ + if (run_cmd(cmd, hwnd)) + { + MessageBox(hwnd, "Unable to set permissions on new configuration file.", + "Error -- Failure Setting IP",MB_OK); + + /* Remove config */ + if(unlink(NEWCONFIG)) + { + MessageBox(hwnd, "Unable to remove new configuration file.", + "Error -- Failure Setting IP",MB_OK); + } + return(0); + } - /* Reading the XML. Printing error and line number */ - if(OS_WriteXML(CONFIG, NEWCONFIG, xml_pt, - NULL, NULL, ip, 0) != 0) + /* Reading the XML. Printing error and line number. */ + if(OS_WriteXML(CONFIG, NEWCONFIG, xml_pt, + NULL, ip) != 0) { MessageBox(hwnd, "Unable to set OSSEC Server IP Address.\r\n" - "(Internal error on the XML Write).", - "Unable to set Server IP Address.",MB_OK); + "(Internal error on the XML Write).", + "Error -- Failure Setting IP",MB_OK); return(0); } @@ -497,6 +554,64 @@ int set_ossec_server(char *ip, HWND hwnd) rename(CONFIG, LASTCONFIG); rename(NEWCONFIG, CONFIG); + return(1); +} + + +/* Set OSSEC Authentication Key */ +int set_ossec_key(char *key, HWND hwnd) +{ + FILE *fp; + char *cacls; + int cmdlen; + + /* Build command line to change permissions */ + cacls = "echo y|cacls \"%s\" /T /G Administrators:f"; + cmdlen = strlen(cacls) + strlen(AUTH_FILE); + char cmd[cmdlen]; + snprintf(cmd, cmdlen, cacls, AUTH_FILE); + + /* Create file */ + fp = fopen(AUTH_FILE, "w"); + if(fp) + { + fclose(fp); + } + else + { + MessageBox(hwnd, "Could not open auth key file.", + "Error -- Failure Importing Key", MB_OK); + return(0); + } + + /* Change permissions */ + if (run_cmd(cmd, hwnd)) + { + MessageBox(hwnd, "Unable to set permissions on auth key file.", + "Error -- Failure Importing Key", MB_OK); + + /* Remove config */ + if(unlink(AUTH_FILE)) + { + MessageBox(hwnd, "Unable to remove auth key file.", + "Error -- Failure Importing Key", MB_OK); + } + + return(0); + } + + fp = fopen(AUTH_FILE, "w"); + if(fp) + { + fprintf(fp, "%s", key); + fclose(fp); + } + else + { + MessageBox(hwnd, "Could not open auth key file for write.", + "Error -- Failure Importing Key", MB_OK); + return(0); + } return(1); }