Skip to content

Commit

Permalink
Merge pull request #1627 from atomicturtle/manage_agents_json_update_02
Browse files Browse the repository at this point in the history
Manage agents json update 02
  • Loading branch information
ddpbsd authored Jan 4, 2019
2 parents ff9d760 + f434725 commit 1a63648
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 63 deletions.
55 changes: 42 additions & 13 deletions src/addagent/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2009 Trend Micro Inc.
/* Copyright (C) 2019 OSSEC Foundation
* All rights reserved.
*
* This program is a free software; you can redistribute it
Expand Down Expand Up @@ -34,13 +34,18 @@ static int setenv(const char *name, const char *val, __attribute__((unused)) int
static void helpmsg()
{
print_header();
print_out(" %s: -[Vhl] [-e id] [-r id] [-i id] [-f file]", ARGV0);
print_out(" %s: -[Vhlj] [-a <ip> -n <name>] [-e id] [-r id] [-i id] [-f file]", ARGV0);
print_out(" -V Version and license message");
print_out(" -h This help message");
print_out(" -j Use JSON output");
print_out(" -l List available agents.");
print_out(" -a <ip> Add new agent");

print_out(" -e <id> Extracts key for an agent (Manager only)");
print_out(" -r <id> Remove an agent (Manager only)");
print_out(" -i <id> Import authentication key (Agent only)");
print_out(" -n <name> Name for new agent");

print_out(" -f <file> Bulk generate client keys from file (Manager only)");
print_out(" <file> contains lines in IP,NAME format");
print_out(" <file> should also exist within /var/ossec due to manage_agents chrooting");
Expand Down Expand Up @@ -78,7 +83,7 @@ static void manage_shutdown(__attribute__((unused)) int sig)
int main(int argc, char **argv)
{
char *user_msg;
int c = 0, cmdlist = 0;
int c = 0, cmdlist = 0, json_output = 0;
const char *cmdexport = NULL;
const char *cmdimport = NULL;
const char *cmdbulk = NULL;
Expand All @@ -99,7 +104,7 @@ int main(int argc, char **argv)
/* Set the name */
OS_SetName(ARGV0);

while ((c = getopt(argc, argv, "Vhle:r:i:f:")) != -1) {
while ((c = getopt(argc, argv, "Vhle:r:i:f:ja:n:")) != -1) {
switch (c) {
case 'V':
print_version();
Expand Down Expand Up @@ -152,6 +157,23 @@ int main(int argc, char **argv)
case 'l':
cmdlist = 1;
break;
case 'j':
json_output = 1;
break;
case 'a':
if (!optarg)
ErrorExit("%s: -a needs an argument.", ARGV0);
setenv("OSSEC_ACTION", "a", 1);
setenv("OSSEC_ACTION_CONFIRMED", "y", 1);
setenv("OSSEC_AGENT_IP", optarg, 1);
setenv("OSSEC_AGENT_ID", "0", 1);
break;
case 'n':
if (!optarg)
ErrorExit("%s: -n needs an argument.", ARGV0);
setenv("OSSEC_AGENT_NAME", optarg, 1);
break;

default:
helpmsg();
break;
Expand Down Expand Up @@ -235,7 +257,7 @@ int main(int argc, char **argv)
k_import(cmdimport);
exit(0);
} else if (cmdexport) {
k_extract(cmdexport);
k_extract(cmdexport, json_output);
exit(0);
} else if (cmdbulk) {
k_bulkload(cmdbulk);
Expand All @@ -245,7 +267,10 @@ int main(int argc, char **argv)
/* Little shell */
while (1) {
int leave_s = 0;
print_banner();

if (!json_output)
print_banner();


/* Get ACTION from the environment. If ACTION is specified,
* we must set leave_s = 1 to ensure that the loop will end */
Expand All @@ -264,15 +289,15 @@ int main(int argc, char **argv)
printf("\n ** Agent adding only available on a master ** \n\n");
break;
#endif
add_agent();
add_agent(json_output);
break;
case 'e':
case 'E':
#ifdef CLIENT
printf("\n ** Key export only available on a master ** \n\n");
break;
#endif
k_extract(NULL);
k_extract(NULL, json_output);
break;
case 'i':
case 'I':
Expand All @@ -293,7 +318,7 @@ int main(int argc, char **argv)
printf("\n ** Key removal only available on a master ** \n\n");
break;
#endif
remove_agent();
remove_agent(json_output);
break;
case 'q':
case 'Q':
Expand All @@ -314,10 +339,14 @@ int main(int argc, char **argv)
continue;
}

if (restart_necessary) {
printf(MUST_RESTART);
} else {
printf("\n");
if (!json_output) {
if (restart_necessary) {
printf(MUST_RESTART);
} else {
printf("\n");
}

printf(EXIT);
}
printf(EXIT);

Expand Down
Loading

0 comments on commit 1a63648

Please sign in to comment.