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

Check and save the agent group when deliver the keep-alive #9088

Merged
merged 10 commits into from Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
143 changes: 112 additions & 31 deletions src/remoted/manager.c
Expand Up @@ -37,7 +37,8 @@ typedef struct group_t {
static OSHash *invalid_files;

/* Internal functions prototypes */
static void read_controlmsg(const char *agent_id, char *msg);
static int lookfor_agent_group(const char *agent_id, char *msg, char **group);
static void read_controlmsg(const char *agent_id, char *msg, char *group);
static int send_file_toagent(const char *agent_id, const char *group, const char *name, const char *sum,char *sharedcfg_dir);
static void c_group(const char *group, char ** files, file_sum ***_f_sum,char * sharedcfg_dir);
static void c_multi_group(char *multi_group,file_sum ***_f_sum,char *hash_multigroup);
Expand Down Expand Up @@ -221,12 +222,18 @@ void save_controlmsg(const keyentry * key, char *r_msg, size_t msg_length, int *
}
}
}
} else {
} else {
/* Update message */
mdebug2("save_controlmsg(): inserting '%s'", msg);
os_free(data->message);
os_strdup(msg, data->message);

os_free(data->group);
if (OS_SUCCESS != lookfor_agent_group(key->id, data->message, &data->group)) {
merror("Error getting agent group for agent: '%s'", key->id);
TomasTurina marked this conversation as resolved.
Show resolved Hide resolved
return;
}

/* Mark data as changed and insert into queue */
if (!data->changed) {
char *id;
Expand All @@ -236,6 +243,7 @@ void save_controlmsg(const keyentry * key, char *r_msg, size_t msg_length, int *
data->changed = 1;
}


/* Unlock mutex */
w_mutex_unlock(&lastmsg_mutex);

Expand Down Expand Up @@ -1015,34 +1023,24 @@ int send_file_toagent(const char *agent_id, const char *group, const char *name,
return (0);
}

/* Read the available control message from the agent */
static void read_controlmsg(const char *agent_id, char *msg)
/* look for agent group */
static int lookfor_agent_group(const char *agent_id, char *msg, char **r_group)
{
int i;
char group[OS_SIZE_65536];
file_sum **f_sum = NULL;
os_md5 tmp_sum;
TomasTurina marked this conversation as resolved.
Show resolved Hide resolved
char *end;
agent_group *agt_group;
int ret = OS_INVALID;

if (!groups) {
/* Nothing to share with agent */
return;
mdebug2("Nothing to share with agent");
return OS_INVALID;
}

mdebug2("read_controlmsg(): reading '%s'", msg);
memset(&tmp_sum, 0, sizeof(os_md5));

// Skip agent-info and label data

if (msg = strchr(msg, '\n'), !msg) {
merror("Invalid message from agent ID '%s' (strchr \\n)", agent_id);
return;
}

for (msg++; (*msg == '\"' || *msg == '!') && (end = strchr(msg, '\n')); msg = end + 1);


// Get agent group
if (agt_group = w_parser_get_agent(agent_id), agt_group) {
strncpy(group, agt_group->group, OS_SIZE_65536);
Expand All @@ -1051,23 +1049,34 @@ static void read_controlmsg(const char *agent_id, char *msg)
} else if (get_agent_group(agent_id, group, OS_SIZE_65536) < 0) {
group[0] = '\0';
}
mdebug2("Agent '%s' group is '%s'",agent_id,group);
mdebug2("Agent '%s' group is '%s'", agent_id, group);

/* Lock mutex */
w_mutex_lock(&files_mutex);

// If group was got, get file sum array

if (group[0]) {
if (f_sum = find_sum(group), !f_sum) {
/* Unlock mutex */
w_mutex_unlock(&files_mutex);

mdebug1("No such group '%s' for agent '%s'", group, agent_id);
return;
return OS_INVALID;
}
mdebug2("file sum array found for group '%s'", group);
TomasTurina marked this conversation as resolved.
Show resolved Hide resolved
w_mutex_unlock(&files_mutex);
os_strdup(group, *r_group);
return OS_SUCCESS;
}

// Skip agent-info and label data

if (msg = strchr(msg, '\n'), !msg) {
merror("Invalid message from agent ID '%s' (strchr \\n)", agent_id);
TomasTurina marked this conversation as resolved.
Show resolved Hide resolved
return OS_INVALID;
}

for (msg++; (*msg == '\"' || *msg == '!') && (end = strchr(msg, '\n')); msg = end + 1);
TomasTurina marked this conversation as resolved.
Show resolved Hide resolved

/* Parse message */
while (*msg != '\0') {
char *md5;
Expand All @@ -1086,8 +1095,7 @@ static void read_controlmsg(const char *agent_id, char *msg)
msg++;

// Skip labeled data

if (*md5 == '\"' || *md5 == '!') {
if (*md5 == '\"' || *md5 == '!' || *md5 == '#') {
continue;
}

Expand All @@ -1101,7 +1109,6 @@ static void read_controlmsg(const char *agent_id, char *msg)
file++;

// If group was not got, guess it by matching sum

mdebug2("Agent '%s' with group '%s' file '%s' MD5 '%s'",agent_id,group,file,md5);
if (!f_sum) {
TomasTurina marked this conversation as resolved.
Show resolved Hide resolved
if (!guess_agent_group || (f_sum = find_group(file, md5, group), !f_sum)) {
Expand All @@ -1111,17 +1118,87 @@ static void read_controlmsg(const char *agent_id, char *msg)
strncpy(group, "default", OS_SIZE_65536);

if (f_sum = find_sum(group), !f_sum) {
/* Unlock mutex */
w_mutex_unlock(&files_mutex);

merror("No such group '%s' for agent '%s'", group, agent_id);
return;
ret = OS_INVALID;
break;
}
}

set_agent_group(agent_id, group);
os_strdup(group, *r_group);
ret = OS_SUCCESS;
mdebug2("group assigned: '%s'", group);
break;
}
}
/* Unlock mutex */
w_mutex_unlock(&files_mutex);

return ret;
}

/* Read the available control message from the agent */
static void read_controlmsg(const char *agent_id, char *msg, char *group)
{
int i;
file_sum **f_sum = NULL;
os_md5 tmp_sum;
char *end;

mdebug2("read_controlmsg(): reading '%s'", msg);
memset(&tmp_sum, 0, sizeof(os_md5));

// Skip agent-info and label data

if (msg = strchr(msg, '\n'), !msg) {
merror("Invalid message from agent ID '%s' (strchr \\n)", agent_id);
return;
}

for (msg++; (*msg == '\"' || *msg == '!') && (end = strchr(msg, '\n')); msg = end + 1);
TomasTurina marked this conversation as resolved.
Show resolved Hide resolved

/* Lock mutex */
w_mutex_lock(&files_mutex);

if (f_sum = find_sum(group), !f_sum) {
/* Unlock mutex */
w_mutex_unlock(&files_mutex);

mdebug1("No such group '%s' for agent '%s'", group, agent_id);
return;
}

/* Parse message */
while (*msg != '\0') {
char *md5;
char *file;

md5 = msg;
file = msg;

msg = strchr(msg, '\n');
if (!msg) {
merror("Invalid message 1 from agent ID '%s' (strchr \\n)", agent_id);
break;
}

*msg = '\0';
msg++;

// Skip labeled data

if (*md5 == '\"' || *md5 == '!') {
TomasTurina marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

file = strchr(file, ' ');
if (!file) {
merror("Invalid message 2 from agent ID '%s' (strchr ' ')", agent_id);
break;
}

*file = '\0';
file++;

/* New agents only have merged.mg */
if (strcmp(file, SHAREDCFG_FILENAME) == 0) {
for (i = 0; f_sum[i]; i++) {
Expand Down Expand Up @@ -1216,6 +1293,7 @@ void *wait_for_msgs(__attribute__((unused)) void *none)
/* Should never leave this loop */
while (1) {
char * msg = NULL;
char * group = NULL;

/* Pop data from queue */
char *agent_id = linked_queue_pop_ex(pending_queue);
Expand All @@ -1225,6 +1303,7 @@ void *wait_for_msgs(__attribute__((unused)) void *none)

if (data = OSHash_Get(pending_data, agent_id), data) {
os_strdup(data->message, msg);
os_strdup(data->group, group);
} else {
merror("Couldn't get pending data from hash table for agent ID '%s'.", agent_id);
os_free(agent_id);
Expand All @@ -1235,11 +1314,12 @@ void *wait_for_msgs(__attribute__((unused)) void *none)
/* Unlock mutex */
w_mutex_unlock(&lastmsg_mutex);

if (msg && agent_id) {
read_controlmsg(agent_id, msg);
if (msg && agent_id && group) {
read_controlmsg(agent_id, msg, group);
}
os_free(agent_id);
os_free(msg);
os_free(group);

// Mark message as dispatched
w_mutex_lock(&lastmsg_mutex);
Expand Down Expand Up @@ -1300,6 +1380,7 @@ void *update_shared_files(__attribute__((unused)) void *none) {
void free_pending_data(pending_data_t *data) {
if (!data) return;
os_free(data->message);
os_free(data->group);
os_free(data);
}

Expand Down
1 change: 1 addition & 0 deletions src/remoted/remoted.h
Expand Up @@ -28,6 +28,7 @@

typedef struct pending_data_t {
char *message;
char *group;
int changed;
} pending_data_t;

Expand Down