1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
13 #include "os_execd/execd.h"
14 #include "os_crypto/md5/md5_op.h"
15 #include "os_net/os_net.h"
19 /* Receive events from the server */
20 void *receiver_thread(__attribute__((unused)) void *none)
24 char file[OS_SIZE_1024 + 1];
25 char buffer[OS_MAXSTR + 1];
27 char cleartext[OS_MAXSTR + 1];
33 struct timeval selecttime;
37 /* Set FP to null before starting */
40 memset(cleartext, '\0', OS_MAXSTR + 1);
41 memset(buffer, '\0', OS_MAXSTR + 1);
42 memset(file, '\0', OS_SIZE_1024 + 1);
43 memset(file_sum, '\0', 34);
46 /* sock must be set */
47 if (agt->sock == -1) {
53 FD_SET(agt->sock, &fdset);
55 /* Wait for 30 seconds */
56 selecttime.tv_sec = 30;
57 selecttime.tv_usec = 0;
59 /* Wait with a timeout for any descriptor */
60 recv_b = select(0, &fdset, NULL, NULL, &selecttime);
62 merror(SELECT_ERROR, ARGV0, errno, strerror(errno));
65 } else if (recv_b == 0) {
69 /* Read until no more messages are available */
70 while ((recv_b = recv(agt->sock, buffer, OS_SIZE_1024, 0)) > 0) {
71 /* Id of zero -- only one key allowed */
72 tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b - 1);
73 if (tmp_msg == NULL) {
74 merror(MSG_ERROR, ARGV0, agt->rip[agt->rip_id]);
78 /* Check for commands */
79 if (IsValidHeader(tmp_msg)) {
80 /* This is the only thread that modifies it */
81 available_server = (int)time(NULL);
83 /* Run timeout commands */
84 if (agt->execdq >= 0) {
85 WinTimeoutRun(available_server);
88 /* If it is an active response message */
89 if (strncmp(tmp_msg, EXECD_HEADER, strlen(EXECD_HEADER)) == 0) {
90 tmp_msg += strlen(EXECD_HEADER);
93 if (agt->execdq >= 0) {
100 /* Restart syscheck */
101 else if (strcmp(tmp_msg, HC_SK_RESTART) == 0) {
102 os_set_restart_syscheck();
106 /* Ack from server */
107 else if (strcmp(tmp_msg, HC_ACK) == 0) {
111 /* Close any open file pointer if it was being written to */
117 /* File update message */
118 if (strncmp(tmp_msg, FILE_UPDATE_HEADER,
119 strlen(FILE_UPDATE_HEADER)) == 0) {
121 tmp_msg += strlen(FILE_UPDATE_HEADER);
123 /* Going to after the file sum */
124 validate_file = strchr(tmp_msg, ' ');
125 if (!validate_file) {
129 *validate_file = '\0';
131 /* Copy the file sum */
132 strncpy(file_sum, tmp_msg, 33);
134 /* Set tmp_msg to the beginning of the file name */
136 tmp_msg = validate_file;
138 if ((validate_file = strchr(tmp_msg, '\n')) != NULL) {
139 *validate_file = '\0';
142 while ((validate_file = strchr(tmp_msg, '/')) != NULL) {
143 *validate_file = '-';
146 if (tmp_msg[0] == '.') {
150 snprintf(file, OS_SIZE_1024, "%s/%s",
154 fp = fopen(file, "wb");
156 merror(FOPEN_ERROR, ARGV0, file, errno, strerror(errno));
160 else if (strncmp(tmp_msg, FILE_CLOSE_HEADER,
161 strlen(FILE_CLOSE_HEADER)) == 0) {
163 os_md5 currently_md5;
165 /* Close for the rename to work */
171 if (file[0] == '\0') {
172 /* Nothing to be done */
175 else if (OS_MD5_File(file, currently_md5, OS_TEXT) < 0) {
180 if (strcmp(currently_md5, file_sum) != 0) {
181 debug1("%s: Failed md5 for: %s -- deleting.",
187 /* Rename the file to its original name */
188 final_file = strrchr(file, '/');
190 if (strcmp(final_file + 1, SHAREDCFG_FILENAME) == 0) {
191 UnmergeFiles(file, SHAREDCFG_DIR);
203 merror("%s: WARN: Unknown message received from server.", ARGV0);
208 available_server = (int)time(NULL);
209 fprintf(fp, "%s", tmp_msg);
213 merror("%s: WARN: Unknown message received. No action defined.",
222 if (file[0] != '\0') {