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
12 #include "os_execd/execd.h"
14 #include "os_crypto/md5/md5_op.h"
15 #include "os_net/os_net.h"
18 /* Global variables */
19 static FILE *fp = NULL;
20 static char file_sum[34] = "";
21 static char file[OS_SIZE_1024 + 1] = "";
24 /* Receive events from the server */
28 char buffer[OS_MAXSTR + 1];
29 char cleartext[OS_MAXSTR + 1];
32 memset(cleartext, '\0', OS_MAXSTR + 1);
33 memset(buffer, '\0', OS_MAXSTR + 1);
35 /* Read until no more messages are available */
36 while ((recv_b = recv(agt->sock, buffer, OS_SIZE_1024, MSG_DONTWAIT)) > 0) {
37 buffer[recv_b] = '\0';
39 tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b - 1);
40 if (tmp_msg == NULL) {
41 merror(MSG_ERROR, ARGV0, agt->rip[agt->rip_id]);
45 /* Check for commands */
46 if (IsValidHeader(tmp_msg)) {
47 available_server = (int)time(NULL);
50 /* Run timeout commands */
51 if (agt->execdq >= 0) {
52 WinTimeoutRun(available_server);
56 /* If it is an active response message */
57 if (strncmp(tmp_msg, EXECD_HEADER, strlen(EXECD_HEADER)) == 0) {
58 tmp_msg += strlen(EXECD_HEADER);
60 if (agt->execdq >= 0) {
61 if (OS_SendUnix(agt->execdq, tmp_msg, 0) < 0) {
62 merror("%s: Error communicating with execd",
68 if (agt->execdq >= 0) {
76 /* Restart syscheck */
77 else if (strcmp(tmp_msg, HC_SK_RESTART) == 0) {
78 os_set_restart_syscheck();
83 else if (strcmp(tmp_msg, HC_ACK) == 0) {
87 /* Close any open file pointer if it was being written to */
93 /* File update message */
94 if (strncmp(tmp_msg, FILE_UPDATE_HEADER,
95 strlen(FILE_UPDATE_HEADER)) == 0) {
98 tmp_msg += strlen(FILE_UPDATE_HEADER);
100 /* Going to after the file sum */
101 validate_file = strchr(tmp_msg, ' ');
102 if (!validate_file) {
106 *validate_file = '\0';
108 /* Copy the file sum */
109 strncpy(file_sum, tmp_msg, 33);
111 /* Set tmp_msg to the beginning of the file name */
113 tmp_msg = validate_file;
115 if ((validate_file = strchr(tmp_msg, '\n')) != NULL) {
116 *validate_file = '\0';
119 while ((validate_file = strchr(tmp_msg, '/')) != NULL) {
120 *validate_file = '-';
123 if (tmp_msg[0] == '.') {
127 snprintf(file, OS_SIZE_1024, "%s/%s",
131 fp = fopen(file, "w");
133 merror(FOPEN_ERROR, ARGV0, file, errno, strerror(errno));
137 else if (strncmp(tmp_msg, FILE_CLOSE_HEADER,
138 strlen(FILE_CLOSE_HEADER)) == 0) {
140 os_md5 currently_md5;
142 /* Close for the rename to work */
148 if (file[0] == '\0') {
149 /* Nothing to be done */
152 else if (OS_MD5_File(file, currently_md5, OS_TEXT) < 0) {
157 if (strcmp(currently_md5, file_sum) != 0) {
158 debug1("%s: ERROR: Failed md5 for: %s -- deleting.",
164 /* Rename the file to its original name */
165 final_file = strrchr(file, '/');
167 if (strcmp(final_file + 1, SHAREDCFG_FILENAME) == 0) {
168 UnmergeFiles(file, SHAREDCFG_DIR);
181 merror("%s: WARN: Unknown message received from server.", ARGV0);
186 available_server = (int)time(NULL);
187 fprintf(fp, "%s", tmp_msg);
191 merror("%s: WARN: Unknown message received. No action defined.",