X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?p=ossec-hids.git;a=blobdiff_plain;f=src%2Faddagent%2Fmanage_keys.c;fp=src%2Faddagent%2Fmanage_keys.c;h=f18f32b94935841a9f281a72e1ac71081699c998;hp=a46379d74760aae0794f65c17219ba7c0ca4cbac;hb=789cbc8e52da68eba3517b920ef22e000cf3c9fd;hpb=ef70704f0b31b59bb719b884d6a99cb9e3e2044a diff --git a/src/addagent/manage_keys.c b/src/addagent/manage_keys.c index a46379d..f18f32b 100755 --- a/src/addagent/manage_keys.c +++ b/src/addagent/manage_keys.c @@ -53,6 +53,17 @@ int k_import(char *cmdimport) char line_read[FILE_SIZE +1]; + #ifdef WIN32 + int result; + int cmdlen; + int caclslen; + char *comspec; + char *cacls; + STARTUPINFO si; + PROCESS_INFORMATION pi; + DWORD exit_code; + #endif + /* Parsing user argument. */ if(cmdimport) @@ -129,8 +140,76 @@ int k_import(char *cmdimport) } fprintf(fp,"%s\n",line_read); fclose(fp); + #ifndef WIN32 chmod(KEYS_FILE, 0440); + #else + /* Get cmd location from environment */ + comspec = getenv("COMSPEC"); + if (comspec == NULL || strncmp(comspec, "", strlen(comspec) == 0)) + { + if(unlink(KEYS_FILE)) + { + verbose(AGENT_DELETE_ERROR, KEYS_FILE); + } + ErrorExit(COMPSEC_ERROR); + } + + /* Build cacls command */ + cacls = "echo y|cacls \"%s\" /T /G Administrators:f"; + caclslen = strlen(cacls) + strlen(KEYS_FILE); + char caclscmd[caclslen]; + snprintf(caclscmd, caclslen, cacls, KEYS_FILE); + + /* Build final command */ + cmdlen = strlen(comspec) + 5 + caclslen; + char cmd[cmdlen]; + snprintf(cmd, cmdlen, "%s /c %s", comspec, caclscmd); + + /* Log command being run */ + log2file("%s: INFO: Running the following command (%s)", ARGV0, cmd); + + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + ZeroMemory(&pi, sizeof(pi)); + + if(!CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, + &si, &pi)) + { + if(unlink(KEYS_FILE)) + { + verbose(AGENT_DELETE_ERROR, KEYS_FILE); + } + ErrorExit(PROC_ERROR, cmd); + } + + /* 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) + { + if(unlink(KEYS_FILE)) + { + verbose(AGENT_DELETE_ERROR, KEYS_FILE); + } + ErrorExit(RESULT_ERROR, cmd, GetLastError()); + } + + if (exit_code) + { + if(unlink(KEYS_FILE)) + { + verbose(AGENT_DELETE_ERROR, KEYS_FILE); + } + ErrorExit(CACLS_ERROR, cmd, exit_code); + } #endif /* Removing sender counter. */ @@ -350,33 +429,35 @@ int k_bulkload(char *cmdbulk) continue; } - do - { - /* Default ID */ - i = 1024; - snprintf(id, 8, "%03d", i); - while(!IDExist(id)) - { - i--; - snprintf(id, 8, "%03d", i); - - /* No key present, use id 0 */ - if(i <= 0) - { - i = 0; - break; - } - } - snprintf(id, 8, "%03d", i+1); - - if(!OS_IsValidID(id)) - printf(INVALID_ID, id); - - /* Search for ID KEY -- no duplicates */ - if(IDExist(id)) - printf(ADD_ERROR_ID, id); - - } while(IDExist(id) || !OS_IsValidID(id)); + /* Default ID */ + i = MAX_AGENTS + 32512; + snprintf(id, 8, "%03d", i); + while(!IDExist(id)) + { + i--; + snprintf(id, 8, "%03d", i); + + /* No key present, use id 0 */ + if(i <= 0) + { + i = 0; + break; + } + } + snprintf(id, 8, "%03d", i+1); + + if(!OS_IsValidID(id)) + { + printf(INVALID_ID, id); + continue; + } + + /* Search for ID KEY -- no duplicates */ + if(IDExist(id)) + { + printf(NO_DEFAULT, i+1); + continue; + } printf(AGENT_INFO, id, name, ip); fflush(stdout);