1 /* @(#) $Id: ./src/client-agent/receiver-win.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
16 #include "os_execd/execd.h"
17 #include "os_crypto/md5/md5_op.h"
18 #include "os_net/os_net.h"
24 * Receive events from the server.
26 void *receiver_thread(void *none)
30 char file[OS_SIZE_1024 +1];
31 char buffer[OS_MAXSTR +1];
33 char cleartext[OS_MAXSTR + 1];
39 struct timeval selecttime;
44 /* Setting FP to null, before starting */
47 memset(cleartext, '\0', OS_MAXSTR +1);
48 memset(buffer, '\0', OS_MAXSTR +1);
49 memset(file, '\0', OS_SIZE_1024 +1);
50 memset(file_sum, '\0', 34);
55 /* sock must be set. */
63 FD_SET(agt->sock, &fdset);
66 /* Wait for 30 seconds. */
67 selecttime.tv_sec = 30;
68 selecttime.tv_usec = 0;
71 /* Wait with a timeout for any descriptor */
72 recv_b = select(0, &fdset, NULL, NULL, &selecttime);
75 merror(SELECT_ERROR, ARGV0);
84 /* Read until no more messages are available */
85 while((recv_b = recv(agt->sock,buffer,OS_SIZE_1024, 0))>0)
87 /* Id of zero -- only one key allowed */
88 tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b -1);
91 merror(MSG_ERROR,ARGV0,agt->rip[agt->rip_id]);
96 /* Check for commands */
97 if(IsValidHeader(tmp_msg))
99 /* This is the only thread that modifies it */
100 available_server = (int)time(NULL);
103 /* Run timeout commands. */
105 WinTimeoutRun(available_server);
107 /* If it is an active response message */
108 if(strncmp(tmp_msg, EXECD_HEADER, strlen(EXECD_HEADER)) == 0)
110 tmp_msg+=strlen(EXECD_HEADER);
113 /* Run on windows. */
116 WinExecdRun(tmp_msg);
124 /* Restart syscheck. */
125 else if(strcmp(tmp_msg, HC_SK_RESTART) == 0)
127 os_set_restart_syscheck();
132 /* Ack from server */
133 else if(strcmp(tmp_msg, HC_ACK) == 0)
138 /* Close any open file pointer if it was being written to */
145 /* File update message */
146 if(strncmp(tmp_msg, FILE_UPDATE_HEADER,
147 strlen(FILE_UPDATE_HEADER)) == 0)
150 tmp_msg+=strlen(FILE_UPDATE_HEADER);
152 /* Going to after the file sum */
153 validate_file = strchr(tmp_msg, ' ');
159 *validate_file = '\0';
161 /* copying the file sum */
162 strncpy(file_sum, tmp_msg, 33);
165 /* Setting tmp_msg to the beginning of the file name */
167 tmp_msg = validate_file;
170 if((validate_file = strchr(tmp_msg, '\n')) != NULL)
172 *validate_file = '\0';
175 while((validate_file = strchr(tmp_msg, '/')) != NULL)
177 *validate_file = '-';
180 if(tmp_msg[0] == '.')
184 snprintf(file, OS_SIZE_1024, "%s/%s",
188 fp = fopen(file, "w");
191 merror(FOPEN_ERROR, ARGV0, file);
195 else if(strncmp(tmp_msg, FILE_CLOSE_HEADER,
196 strlen(FILE_CLOSE_HEADER)) == 0)
199 os_md5 currently_md5;
201 /* Making sure to close for the rename to work */
213 else if(OS_MD5_File(file, currently_md5) < 0)
221 if(strcmp(currently_md5, file_sum) != 0)
223 debug1("%s: Failed md5 for: %s -- deleting.",
231 /* Renaming the file to its orignal name */
232 final_file = strrchr(file, '/');
235 if(strcmp(final_file + 1, SHAREDCFG_FILENAME) == 0)
237 UnmergeFiles(file, SHAREDCFG_DIR);
252 merror("%s: WARN: Unknown message received from server.", ARGV0);
258 available_server = (int)time(NULL);
259 fprintf(fp, "%s", tmp_msg);
264 merror("%s: WARN: Unknown message received. No action defined.",