-/* @(#) $Id: manager.c,v 1.47 2009/06/24 18:53:07 dcid Exp $ */
+/* @(#) $Id: ./src/remoted/manager.c, 2011/09/08 dcid Exp $
+ */
/* Copyright (C) 2009 Trend Micro Inc.
* All right reserved.
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
* Foundation
*/
{
char msg_ack[OS_FLSIZE +1];
-
+
/* Replying to the agent. */
snprintf(msg_ack, OS_FLSIZE, "%s%s", CONTROL_HEADER, HC_ACK);
send_msg(agentid, msg_ack);
{
utimes(_keep_alive[agentid], NULL);
}
-
+
else if(strcmp(r_msg, HC_STARTUP) == 0)
{
return;
}
-
+
else
{
FILE *fp;
char *uname = r_msg;
+ char *random_leftovers;
/* locking mutex. */
*r_msg = '\0';
+ random_leftovers = strchr(r_msg, '\n');
+ if(random_leftovers)
+ {
+ *random_leftovers = '\0';
+ }
/* Updating the keep alive. */
os_strdup(agent_file, _keep_alive[agentid]);
}
-
+
/* Writing to the file. */
fp = fopen(_keep_alive[agentid], "w");
}
}
-
+
/* Locking now to notify of change. */
if(pthread_mutex_lock(&lastmsg_mutex) != 0)
{
return;
}
-
+
/* Assign new values */
_changed[agentid] = 1;
modified_agentid = agentid;
/* Signal that new data is available */
pthread_cond_signal(&awake_mutex);
-
+
/* Unlocking mutex */
if(pthread_mutex_unlock(&lastmsg_mutex) != 0)
{
return;
}
-
+
return;
-}
+}
int i;
if(!f_sum)
return;
- for(i = 0;;i++)
+ for(i = 0;;i++)
{
if(f_sum[i] == NULL)
break;
-
+
if(f_sum[i]->name)
free(f_sum[i]->name);
-
+
free(f_sum[i]);
f_sum[i] = NULL;
}
DIR *dp;
struct dirent *entry;
-
+
os_md5 md5sum;
-
+
int f_size = 0;
/* Opening the directory given */
dp = opendir(SHAREDCFG_DIR);
- if(!dp)
+ if(!dp)
{
merror("%s: Error opening directory: '%s': %s ",
ARGV0,
SHAREDCFG_DIR,
strerror(errno));
return;
- }
+ }
/* Reading directory */
while((entry = readdir(dp)) != NULL)
{
char tmp_dir[512];
-
+
/* Just ignore . and .. */
if((strcmp(entry->d_name,".") == 0) ||
(strcmp(entry->d_name,"..") == 0))
continue;
}
-
+
if(OS_MD5_File(tmp_dir, md5sum) != 0)
{
merror("%s: Error accessing file '%s'",ARGV0, tmp_dir);
continue;
}
-
-
+
+
f_sum = (file_sum **)realloc(f_sum, (f_size +2) * sizeof(file_sum *));
if(!f_sum)
{
ErrorExit(MEM_ERROR,ARGV0);
}
-
+
strncpy(f_sum[f_size]->sum, md5sum, 32);
os_strdup(entry->d_name, f_sum[f_size]->name);
f_sum[f_size]->mark = 0;
MergeAppendFile(SHAREDCFG_FILE, tmp_dir);
f_size++;
}
-
+
if(f_sum != NULL)
f_sum[f_size] = NULL;
merror("%s: Error accessing file '%s'",ARGV0, SHAREDCFG_FILE);
f_sum[0]->sum[0] = '\0';
}
- strncpy(f_sum[0]->sum, md5sum, 32);
+ strncpy(f_sum[0]->sum, md5sum, 32);
os_strdup(SHAREDCFG_FILENAME, f_sum[0]->name);
- return;
+ return;
}
-
+
/* send_file_toagent: Sends a file to the agent.
* Returns -1 on error
int i = 0, n = 0;
char file[OS_SIZE_1024 +1];
char buf[OS_SIZE_1024 +1];
-
+
FILE *fp;
-
+
snprintf(file, OS_SIZE_1024, "%s/%s",SHAREDCFG_DIR, name);
fp = fopen(file, "r");
if(!fp)
/* Sending the file name first */
- snprintf(buf, OS_SIZE_1024, "%s%s%s %s\n",
+ snprintf(buf, OS_SIZE_1024, "%s%s%s %s\n",
CONTROL_HEADER, FILE_UPDATE_HEADER, sum, name);
if(send_msg(agentid, buf) == -1)
i++;
}
-
+
/* Sending the message to close the file */
snprintf(buf, OS_SIZE_1024, "%s%s", CONTROL_HEADER, FILE_CLOSE_HEADER);
if(send_msg(agentid, buf) == -1)
fclose(fp);
return(-1);
}
-
+
fclose(fp);
-
+
return(0);
}
* the agent.
*/
void read_controlmsg(int agentid, char *msg)
-{
+{
int i;
}
- /* Parse message */
+ /* Parse message */
while(*msg != '\0')
{
char *md5;
if(!msg)
{
merror("%s: Invalid message from '%s' (strchr \\n)",
- ARGV0,
+ ARGV0,
keys.keyentries[agentid]->ip->ip);
break;
}
if(!file)
{
merror("%s: Invalid message from '%s' (strchr ' ')",
- ARGV0,
+ ARGV0,
keys.keyentries[agentid]->ip->ip);
break;
}
{
if(strcmp(f_sum[0]->sum, md5) != 0)
{
- debug1("%s: DEBUG Sending file '%s' to agent.", ARGV0,
+ debug1("%s: DEBUG Sending file '%s' to agent.", ARGV0,
f_sum[0]->name);
if(send_file_toagent(agentid,f_sum[0]->name,f_sum[0]->sum)<0)
{
i = 0;
while(f_sum[i])
{
- f_sum[i]->mark = 0;
+ f_sum[i]->mark = 0;
i++;
}
{
f_sum[i]->mark = 2;
}
- break;
+ break;
}
}
if((f_sum[i]->mark == 1) ||
(f_sum[i]->mark == 0))
{
-
+
debug1("%s: Sending file '%s' to agent.", ARGV0, f_sum[i]->name);
if(send_file_toagent(agentid,f_sum[i]->name,f_sum[i]->sum) < 0)
{
}
}
- f_sum[i]->mark = 0;
+ f_sum[i]->mark = 0;
}
-
- return;
+
+ return;
}
{
int id, i;
char msg[OS_SIZE_1024 +2];
-
+
/* Initializing the memory */
memset(msg, '\0', OS_SIZE_1024 +2);
-
+
/* should never leave this loop */
while(1)
{
/* Every NOTIFY * 30 minutes, re read the files.
- * If something changed, notify all agents
+ * If something changed, notify all agents
*/
_ctime = time(0);
if((_ctime - _stime) > (NOTIFY_TIME*30))
_stime = _ctime;
}
-
-
+
+
/* locking mutex */
if(pthread_mutex_lock(&lastmsg_mutex) != 0)
{
{
continue;
}
-
+
id = 0;
-
+
/* locking mutex */
if(pthread_mutex_lock(&lastmsg_mutex) != 0)
{
id = 1;
}
-
+
/* Unlocking mutex */
if(pthread_mutex_unlock(&lastmsg_mutex) != 0)
{
pthread_mutex_init(&lastmsg_mutex, NULL);
pthread_cond_init(&awake_mutex, NULL);
}
-
+
modified_agentid = -1;
return;