Skip to content
Open
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
121 changes: 73 additions & 48 deletions src/addagent/manage_agents.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,67 @@ int add_agent()
return (0);
}

int remove_agent()
int remove_agent_by_id(const char* id)
{
FILE *fp;
int id_exist;
char u_id[FILE_SIZE + 1];
u_id[FILE_SIZE] = '\0';

id_exist = IDExist(id);

if (!id_exist) {
printf(NO_ID, id);
return (1);
}

strncpy(u_id, id, FILE_SIZE);

do {

char *full_name = getFullnameById(u_id);

if (!full_name) {
printf(NO_ID, u_id);
return (1);
}

if (isChroot()) {
fp = fopen(AUTH_FILE, "r+");
} else {
fp = fopen(KEYSFILE_PATH, "r+");
}

if (!fp) {
free(full_name);
ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE, errno, strerror(errno));
}
#ifndef WIN32
chmod(AUTH_FILE, 0440);
#endif

/* Remove the agent, but keep the id */
fsetpos(fp, &fp_pos);
fprintf(fp, "%s #*#*#*#*#*#*#*#*#*#*#", u_id);

fclose(fp);

/* Remove counter for ID */
delete_agentinfo(full_name);
OS_RemoveCounter(u_id);
free(full_name);
full_name = NULL;

printf(REMOVE_DONE, u_id);
restart_necessary = 1;
break;

} while (1);

return (0);
}
int remove_agent()
{
char *user_input;
char u_id[FILE_SIZE + 1];
int id_exist;
Expand Down Expand Up @@ -333,55 +391,22 @@ int remove_agent()
}
} while (!id_exist);

do {
printf(REMOVE_CONFIRM);
fflush(stdout);

user_input = getenv("OSSEC_ACTION_CONFIRMED");
if (user_input == NULL) {
user_input = read_from_user();
} else {
printf("%s\n", user_input);
}

/* If user confirms */
if (user_input[0] == 'y' || user_input[0] == 'Y') {
/* Get full agent name */
char *full_name = getFullnameById(u_id);
if (!full_name) {
printf(NO_ID, u_id);
return (1);
}

fp = fopen(AUTH_FILE, "r+");
if (!fp) {
free(full_name);
ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE, errno, strerror(errno));
}
#ifndef WIN32
chmod(AUTH_FILE, 0440);
#endif

/* Remove the agent, but keep the id */
fsetpos(fp, &fp_pos);
fprintf(fp, "%s #*#*#*#*#*#*#*#*#*#*#", u_id);

fclose(fp);
printf(REMOVE_CONFIRM);
fflush(stdout);

/* Remove counter for ID */
delete_agentinfo(full_name);
OS_RemoveCounter(u_id);
free(full_name);
full_name = NULL;
user_input = getenv("OSSEC_ACTION_CONFIRMED");
if (user_input == NULL) {
user_input = read_from_user();
} else {
printf("%s\n", user_input);
}

printf(REMOVE_DONE, u_id);
restart_necessary = 1;
break;
} else { /* if(user_input[0] == 'n' || user_input[0] == 'N') */
printf(REMOVE_NOT);
break;
}
} while (1);
/* If user confirms */
if (user_input[0] == 'y' || user_input[0] == 'Y') {
remove_agent_by_id(u_id);
} else { /* if(user_input[0] == 'n' || user_input[0] == 'N') */
printf(REMOVE_NOT);
}

return (0);
}
Expand Down
1 change: 1 addition & 0 deletions src/addagent/manage_agents.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ char *read_from_user(void);
/* Add or remove an agent */
int add_agent(void);
int remove_agent(void);
int remove_agent_by_id(const char* id);

/* Extract or import a key */
int k_extract(const char *cmdextract);
Expand Down
19 changes: 16 additions & 3 deletions src/addagent/validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ char *OS_AddNewAgent(const char *name, const char *ip, const char *id)
id = nid;
}

fp = fopen(KEYSFILE_PATH, "a");
if (isChroot()) {
fp = fopen(AUTH_FILE, "a");
} else {
fp = fopen(KEYSFILE_PATH, "a");
}
if (!fp) {
return (NULL);
}
Expand Down Expand Up @@ -107,7 +111,12 @@ char *getFullnameById(const char *id)
return (NULL);
}

fp = fopen(AUTH_FILE, "r");
if (isChroot()) {
fp = fopen(AUTH_FILE, "r");
} else {
fp = fopen(KEYSFILE_PATH, "r");
}

if (!fp) {
return (NULL);
}
Expand Down Expand Up @@ -304,7 +313,11 @@ int print_agents(int print_status, int active_only, int csv_output)
char line_read[FILE_SIZE + 1];
line_read[FILE_SIZE] = '\0';

fp = fopen(AUTH_FILE, "r");
if (isChroot()) {
fp = fopen(AUTH_FILE, "r");
} else {
fp = fopen(KEYSFILE_PATH, "r");
}
if (!fp) {
return (0);
}
Expand Down