Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix manage agents keys #336

Merged
merged 8 commits into from
Oct 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/addagent/manage_agents.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ extern fpos_t fp_pos;
#define ADD_CONFIRM "Confirm adding it?(y/n): "
#define AGENT_ADD "Agent added.\n"
#define ADDED "Added.\n"
#define ADD_NOT "Not Adding ..\n"
#define ADD_NOT "Not Adding.\n"
#define PRESS_ENTER "** Press ENTER to return to the main menu.\n"
#define MUST_RESTART "\n** You must restart OSSEC for your changes" \
" to take effect.\n\n"
Expand All @@ -110,7 +110,7 @@ extern fpos_t fp_pos;
#define REMOVE_ID "Provide the ID of the agent to be removed (or '\\q' to quit): "
#define REMOVE_CONFIRM "Confirm deleting it?(y/n): "
#define REMOVE_DONE "Agent '%s' removed.\n"
#define REMOVE_NOT "Not removing ..\n"
#define REMOVE_NOT "Not removing.\n"

/* Import agent */
#define IMPORT_KEY "\n* Provide the Key generated by the server.\n" \
Expand All @@ -128,7 +128,7 @@ extern fpos_t fp_pos;
#define ERROR_KEYS "Unable to handle keys file. Exiting.\n"
#define EXTRACT_ERROR "Unable to extract agent key.\n"
#define INPUT_LARGE ARGV0 ": Input too large. Not adding it.\n"
#define EXIT ARGV0 ": Exiting ..\n"
#define EXIT ARGV0 ": Exiting.\n"

#define BANNER "\n****************************************" \
"\n* %s %s Agent manager. *" \
Expand All @@ -147,15 +147,10 @@ extern fpos_t fp_pos;
"Choose your action: I or Q: "

/* WIN32 errors */
#define CHDIR_ERROR_2 ARGV0 ": Could not chdir (%s) (Make sure path exists and executable is running with Administrative priviliges).\n"
#define CHDIR_ERROR_2 ARGV0 ": Could not chdir (%s) (Make sure path exists and executable is running with Administrative priviliges).\n"
#define CONF_ERROR ARGV0 ": Could not read (%s) (Make sure config exists and executable is running with Administrative priviliges).\n"
#define COMPSEC_ERROR ARGV0 ": Could not find cmd.exe using COMPSEC environment variable.\n"
#define PROC_ERROR ARGV0 ": Could not start process running command (%s).\n"
#define RESULT_ERROR ARGV0 ": Could not run command (%s) which returned (%ld).\n"
#define CACLS_ERROR ARGV0 ": Could not set permissions running (%s) which exited with (%ld).\n"
#define GMF_ERROR ARGV0 ": Could not run GetModuleFileName.\n"
#define GMF_BUFF_ERROR ARGV0 ": Could not get path because it is too long and was shrunk by (%d) characters with a max of (%d).\n"
#define GMF_UNKN_ERROR ARGV0 ": Could not run GetModuleFileName with returned (%ld).\n"
#define DELETE_ERROR ARGV0 ": Could not unlink file (%s).\n"
#define GMF_UNKN_ERROR ARGV0 ": Could not run GetModuleFileName which returned (%ld).\n"

/* EOF */
97 changes: 25 additions & 72 deletions src/addagent/manage_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,12 @@ int k_import(const 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
char auth_file_tmp[] = AUTH_FILE;
char *keys_file = basename_ex(auth_file_tmp);

char tmp_path[strlen(TMP_DIR) + 1 + strlen(keys_file) + 6 + 1];

snprintf(tmp_path, sizeof(tmp_path), "%s/%sXXXXXX", TMP_DIR, keys_file);

/* Parsing user argument. */
if(cmdimport)
Expand Down Expand Up @@ -131,87 +126,45 @@ int k_import(const char *cmdimport)

if(user_input[0] == 'y' || user_input[0] == 'Y')
{
fp = fopen(KEYS_FILE,"w");
if(!fp)
if (mkstemp_ex(tmp_path))
{
ErrorExit(FOPEN_ERROR, ARGV0, KEYS_FILE);
ErrorExit(MKSTEMP_ERROR, ARGV0, tmp_path);
}
fprintf(fp,"%s\n",line_read);
fclose(fp);

#ifndef WIN32
if(chmod(KEYS_FILE, 0440) == -1)
{
ErrorExit(CHMOD_ERROR, ARGV0, KEYS_FILE);
}
#else
/* Get cmd location from environment */
comspec = getenv("COMSPEC");
if (comspec == NULL || strncmp(comspec, "", strlen(comspec) == 0))
if (chmod(tmp_path, 0440))
{
if(unlink(KEYS_FILE))
if (unlink(tmp_path))
{
verbose(DELETE_ERROR, KEYS_FILE);
verbose(DELETE_ERROR, ARGV0, tmp_path, errno, strerror(errno));
}
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(DELETE_ERROR, KEYS_FILE);
}
ErrorExit(PROC_ERROR, cmd);
ErrorExit(CHMOD_ERROR, ARGV0, tmp_path, errno, strerror(errno));
}
#endif

/* 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)
fp = fopen(tmp_path,"w");
if(!fp)
{
if(unlink(KEYS_FILE))
if (unlink(tmp_path))
{
verbose(DELETE_ERROR, KEYS_FILE);
verbose(DELETE_ERROR, ARGV0, tmp_path, errno, strerror(errno));
}
ErrorExit(RESULT_ERROR, cmd, GetLastError());

ErrorExit(FOPEN_ERROR, ARGV0, tmp_path);
}
fprintf(fp,"%s\n",line_read);
fclose(fp);

if (exit_code)
if (rename_ex(tmp_path, KEYS_FILE))
{
if(unlink(KEYS_FILE))
if (unlink(tmp_path))
{
verbose(DELETE_ERROR, KEYS_FILE);
verbose(DELETE_ERROR, ARGV0, tmp_path, errno, strerror(errno));
}
ErrorExit(CACLS_ERROR, cmd, exit_code);

ErrorExit(RENAME_ERROR, ARGV0, tmp_path);
}
#endif

/* Removing sender counter. */
OS_RemoveCounter("sender");
Expand Down
7 changes: 5 additions & 2 deletions src/error_messages/error_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@


/* SYSTEM ERRORS */
#define FORK_ERROR "%s(1101): ERROR: Unable to fork. Exiting."
#define MEM_ERROR "%s(1102): ERROR: Not enough Memory. Exiting."
#define FORK_ERROR "%s(1101): ERROR: Unable to fork. Exiting."
#define MEM_ERROR "%s(1102): ERROR: Not enough Memory. Exiting."
#define FOPEN_ERROR "%s(1103): ERROR: Unable to open file '%s'."
#define SIZE_ERROR "%s(1104): ERROR: Maximum string size reached for: %s."
#define NULL_ERROR "%s(1105): ERROR: Attempted to use null string. "
Expand All @@ -50,6 +50,9 @@
#define RENAME_ERROR "%s(1124): ERROR: Unable to rename file: '%s'."
#define INT_ERROR "%s(1125): ERROR: Internal error (undefined)."
#define OPEN_ERROR "%s(1126): ERROR: Unable to open file '%s' reason '%s'"
#define CHMOD_ERROR "%s(1127): ERROR: Could not chmod (%s) which returned [(%d)-(%s)]."
#define MKSTEMP_ERROR "%s(1128): ERROR: Could not create temporary file (%s)."
#define DELETE_ERROR "%s(1129): ERROR: Could not unlink file (%s) which returned [(%d)-(%s)]."


/* COMMON ERRORS */
Expand Down
9 changes: 8 additions & 1 deletion src/headers/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ published by the Free Software Foundation. For more details, go to \n\
#define GROUPGLOBAL "ossec"
#endif

#ifndef DEFAULTDIR
#ifndef DEFAULTDIR
#define DEFAULTDIR "/var/ossec"
#endif

Expand Down Expand Up @@ -279,6 +279,13 @@ published by the Free Software Foundation. For more details, go to \n\
#define WAIT_FILE_PATH DEFAULTDIR WAIT_FILE


#define TMP_DIR "tmp"


/* Windows COMSPEC */
#define COMSPEC "C:\\Windows\\System32\\cmd.exe"


/* Default ports */
#ifndef DEFAULT_SECURE
#define DEFAULT_SECURE 1514 /* Default encrypted */
Expand Down
9 changes: 9 additions & 0 deletions src/headers/file_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ void goDaemonLight(void);
/* not really a file operation, but returns the uname */
char *getuname(void);

/* return basename of path */
char *basename_ex(char *path) __attribute__((nonnull));

/* rename file or directory */
int rename_ex(const char *source, const char *destination) __attribute__((nonnull));

/* create temporary file */
int mkstemp_ex(char *tmp_path) __attribute__((nonnull));

/* Checks for vista. */
#ifdef WIN32
int checkVista();
Expand Down
Loading