1 /* @(#) $Id: receiver.c,v 1.28 2009/06/24 17:06:24 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
17 #include "os_execd/execd.h"
21 #include "os_crypto/md5/md5_op.h"
22 #include "os_net/os_net.h"
28 char file_sum[34] = "";
29 char file[OS_SIZE_1024 +1] = "";
33 * Receive events from the server.
39 char buffer[OS_MAXSTR +1];
41 char cleartext[OS_MAXSTR + 1];
45 memset(cleartext, '\0', OS_MAXSTR +1);
46 memset(buffer, '\0', OS_MAXSTR +1);
50 /* Read until no more messages are available */
51 while((recv_b = recv(logr->sock, buffer, OS_SIZE_1024, MSG_DONTWAIT)) > 0)
53 buffer[recv_b] = '\0';
55 tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b -1);
58 merror(MSG_ERROR,ARGV0,logr->rip[logr->rip_id]);
63 /* Check for commands */
64 if(IsValidHeader(tmp_msg))
66 available_server = (int)time(NULL);
70 /* Run timeout commands. */
72 WinTimeoutRun(available_server);
76 /* If it is an active response message */
77 if(strncmp(tmp_msg, EXECD_HEADER, strlen(EXECD_HEADER)) == 0)
79 tmp_msg+=strlen(EXECD_HEADER);
84 if(OS_SendUnix(logr->execdq, tmp_msg, 0) < 0)
86 merror("%s: Error communicating with execd",
107 /* Restart syscheck. */
108 else if(strcmp(tmp_msg, HC_SK_RESTART) == 0)
110 os_set_restart_syscheck();
115 /* Ack from server */
116 else if(strcmp(tmp_msg, HC_ACK) == 0)
122 /* Close any open file pointer if it was being written to */
130 /* File update message */
131 if(strncmp(tmp_msg, FILE_UPDATE_HEADER,
132 strlen(FILE_UPDATE_HEADER)) == 0)
136 tmp_msg += strlen(FILE_UPDATE_HEADER);
138 /* Going to after the file sum */
139 validate_file = strchr(tmp_msg, ' ');
145 *validate_file = '\0';
147 /* copying the file sum */
148 strncpy(file_sum, tmp_msg, 33);
151 /* Setting tmp_msg to the beginning of the file name */
153 tmp_msg = validate_file;
156 if((validate_file = strchr(tmp_msg, '\n')) != NULL)
158 *validate_file = '\0';
161 while((validate_file = strchr(tmp_msg, '/')) != NULL)
163 *validate_file = '-';
166 if(tmp_msg[0] == '.')
170 snprintf(file, OS_SIZE_1024, "%s/%s",
175 fp = fopen(file, "w");
178 merror(FOPEN_ERROR, ARGV0, file);
182 else if(strncmp(tmp_msg, FILE_CLOSE_HEADER,
183 strlen(FILE_CLOSE_HEADER)) == 0)
186 os_md5 currently_md5;
188 /* Making sure to close for the rename to work */
200 else if(OS_MD5_File(file, currently_md5) < 0)
208 if(strcmp(currently_md5, file_sum) != 0)
210 debug1("%s: ERROR: Failed md5 for: %s -- deleting.",
218 /* Renaming the file to its orignal name */
219 final_file = strrchr(file, '/');
222 if(strcmp(final_file + 1, SHAREDCFG_FILENAME) == 0)
224 UnmergeFiles(file, SHAREDCFG_DIR);
240 merror("%s: WARN: Unknown message received from server.", ARGV0);
246 available_server = (int)time(NULL);
247 fprintf(fp, "%s", tmp_msg);
252 merror("%s: WARN: Unknown message received. No action defined.",