1 /* @(#) $Id: ./src/client-agent/receiver.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
18 #include "os_execd/execd.h"
22 #include "os_crypto/md5/md5_op.h"
23 #include "os_net/os_net.h"
29 char file_sum[34] = "";
30 char file[OS_SIZE_1024 +1] = "";
34 * Receive events from the server.
40 char buffer[OS_MAXSTR +1];
42 char cleartext[OS_MAXSTR + 1];
46 memset(cleartext, '\0', OS_MAXSTR +1);
47 memset(buffer, '\0', OS_MAXSTR +1);
51 /* Read until no more messages are available */
52 while((recv_b = recv(agt->sock, buffer, OS_SIZE_1024, MSG_DONTWAIT)) > 0)
54 buffer[recv_b] = '\0';
56 tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b -1);
59 merror(MSG_ERROR,ARGV0,agt->rip[agt->rip_id]);
64 /* Check for commands */
65 if(IsValidHeader(tmp_msg))
67 available_server = (int)time(NULL);
71 /* Run timeout commands. */
73 WinTimeoutRun(available_server);
77 /* If it is an active response message */
78 if(strncmp(tmp_msg, EXECD_HEADER, strlen(EXECD_HEADER)) == 0)
80 tmp_msg+=strlen(EXECD_HEADER);
85 if(OS_SendUnix(agt->execdq, tmp_msg, 0) < 0)
87 merror("%s: Error communicating with execd",
108 /* Restart syscheck. */
109 else if(strcmp(tmp_msg, HC_SK_RESTART) == 0)
111 os_set_restart_syscheck();
116 /* Ack from server */
117 else if(strcmp(tmp_msg, HC_ACK) == 0)
123 /* Close any open file pointer if it was being written to */
131 /* File update message */
132 if(strncmp(tmp_msg, FILE_UPDATE_HEADER,
133 strlen(FILE_UPDATE_HEADER)) == 0)
137 tmp_msg += strlen(FILE_UPDATE_HEADER);
139 /* Going to after the file sum */
140 validate_file = strchr(tmp_msg, ' ');
146 *validate_file = '\0';
148 /* copying the file sum */
149 strncpy(file_sum, tmp_msg, 33);
152 /* Setting tmp_msg to the beginning of the file name */
154 tmp_msg = validate_file;
157 if((validate_file = strchr(tmp_msg, '\n')) != NULL)
159 *validate_file = '\0';
162 while((validate_file = strchr(tmp_msg, '/')) != NULL)
164 *validate_file = '-';
167 if(tmp_msg[0] == '.')
171 snprintf(file, OS_SIZE_1024, "%s/%s",
176 fp = fopen(file, "w");
179 merror(FOPEN_ERROR, ARGV0, file);
183 else if(strncmp(tmp_msg, FILE_CLOSE_HEADER,
184 strlen(FILE_CLOSE_HEADER)) == 0)
187 os_md5 currently_md5;
189 /* Making sure to close for the rename to work */
201 else if(OS_MD5_File(file, currently_md5) < 0)
209 if(strcmp(currently_md5, file_sum) != 0)
211 debug1("%s: ERROR: Failed md5 for: %s -- deleting.",
219 /* Renaming the file to its orignal name */
220 final_file = strrchr(file, '/');
223 if(strcmp(final_file + 1, SHAREDCFG_FILENAME) == 0)
225 UnmergeFiles(file, SHAREDCFG_DIR);
241 merror("%s: WARN: Unknown message received from server.", ARGV0);
247 available_server = (int)time(NULL);
248 fprintf(fp, "%s", tmp_msg);
253 merror("%s: WARN: Unknown message received. No action defined.",