Imported Upstream version 2.7
[ossec-hids.git] / src / client-agent / receiver.c
1 /* @(#) $Id: ./src/client-agent/receiver.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right reserved.
6  *
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
10  * Foundation
11  */
12
13
14 #include "shared.h"
15
16
17 #ifdef WIN32
18 #include "os_execd/execd.h"
19 #endif
20
21
22 #include "os_crypto/md5/md5_op.h"
23 #include "os_net/os_net.h"
24
25
26 #include "agentd.h"
27
28 FILE *fp = NULL;
29 char file_sum[34] = "";
30 char file[OS_SIZE_1024 +1] = "";
31
32
33 /* receive_msg:
34  * Receive events from the server.
35  */
36 void *receive_msg()
37 {
38     int recv_b;
39
40     char buffer[OS_MAXSTR +1];
41
42     char cleartext[OS_MAXSTR + 1];
43     char *tmp_msg;
44
45
46     memset(cleartext, '\0', OS_MAXSTR +1);
47     memset(buffer, '\0', OS_MAXSTR +1);
48
49
50
51     /* Read until no more messages are available */
52     while((recv_b = recv(logr->sock, buffer, OS_SIZE_1024, MSG_DONTWAIT)) > 0)
53     {
54         buffer[recv_b] = '\0';
55
56         tmp_msg = ReadSecMSG(&keys, buffer, cleartext, 0, recv_b -1);
57         if(tmp_msg == NULL)
58         {
59             merror(MSG_ERROR,ARGV0,logr->rip[logr->rip_id]);
60             continue;
61         }
62
63
64         /* Check for commands */
65         if(IsValidHeader(tmp_msg))
66         {
67             available_server = (int)time(NULL);
68
69
70             #ifdef WIN32
71             /* Run timeout commands. */
72             if(logr->execdq >= 0)
73                 WinTimeoutRun(available_server);
74             #endif
75
76
77             /* If it is an active response message */
78             if(strncmp(tmp_msg, EXECD_HEADER, strlen(EXECD_HEADER)) == 0)
79             {
80                 tmp_msg+=strlen(EXECD_HEADER);
81
82                 #ifndef WIN32
83                 if(logr->execdq >= 0)
84                 {
85                     if(OS_SendUnix(logr->execdq, tmp_msg, 0) < 0)
86                     {
87                         merror("%s: Error communicating with execd",
88                                 ARGV0);
89                     }
90                 }
91
92                 #else
93
94
95                 /* Run on windows. */
96                 if(logr->execdq >= 0)
97                 {
98                     WinExecdRun(tmp_msg);
99                 }
100
101                 #endif
102
103
104                 continue;
105             }
106
107
108             /* Restart syscheck. */
109             else if(strcmp(tmp_msg, HC_SK_RESTART) == 0)
110             {
111                 os_set_restart_syscheck();
112                 continue;
113             }
114
115
116             /* Ack from server */
117             else if(strcmp(tmp_msg, HC_ACK) == 0)
118             {
119                 continue;
120             }
121
122
123             /* Close any open file pointer if it was being written to */
124             if(fp)
125             {
126                 fclose(fp);
127                 fp = NULL;
128             }
129
130
131             /* File update message */
132             if(strncmp(tmp_msg, FILE_UPDATE_HEADER,
133                        strlen(FILE_UPDATE_HEADER)) == 0)
134             {
135                 char *validate_file;
136
137                 tmp_msg += strlen(FILE_UPDATE_HEADER);
138
139                 /* Going to after the file sum */
140                 validate_file = strchr(tmp_msg, ' ');
141                 if(!validate_file)
142                 {
143                     continue;
144                 }
145
146                 *validate_file = '\0';
147
148                 /* copying the file sum */
149                 strncpy(file_sum, tmp_msg, 33);
150
151
152                 /* Setting tmp_msg to the beginning of the file name */
153                 validate_file++;
154                 tmp_msg = validate_file;
155
156
157                 if((validate_file = strchr(tmp_msg, '\n')) != NULL)
158                 {
159                     *validate_file = '\0';
160                 }
161
162                 while((validate_file = strchr(tmp_msg, '/')) != NULL)
163                 {
164                     *validate_file = '-';
165                 }
166
167                 if(tmp_msg[0] == '.')
168                     tmp_msg[0] = '-';
169
170
171                 snprintf(file, OS_SIZE_1024, "%s/%s",
172                         SHAREDCFG_DIR,
173                         tmp_msg);
174
175
176                 fp = fopen(file, "w");
177                 if(!fp)
178                 {
179                     merror(FOPEN_ERROR, ARGV0, file);
180                 }
181             }
182
183             else if(strncmp(tmp_msg, FILE_CLOSE_HEADER,
184                         strlen(FILE_CLOSE_HEADER)) == 0)
185             {
186                 /* no error */
187                 os_md5 currently_md5;
188
189                 /* Making sure to close for the rename to work */
190                 if(fp)
191                 {
192                     fclose(fp);
193                     fp = NULL;
194                 }
195
196                 if(file[0] == '\0')
197                 {
198                     /* nada */
199                 }
200
201                 else if(OS_MD5_File(file, currently_md5) < 0)
202                 {
203                     /* Removing file */
204                     unlink(file);
205                     file[0] = '\0';
206                 }
207                 else
208                 {
209                     if(strcmp(currently_md5, file_sum) != 0)
210                     {
211                         debug1("%s: ERROR: Failed md5 for: %s -- deleting.",
212                                 ARGV0, file);
213                         unlink(file);
214                     }
215                     else
216                     {
217                         char *final_file;
218
219                         /* Renaming the file to its orignal name */
220                         final_file = strrchr(file, '/');
221                         if(final_file)
222                         {
223                             if(strcmp(final_file + 1, SHAREDCFG_FILENAME) == 0)
224                             {
225                                 UnmergeFiles(file, SHAREDCFG_DIR);
226                             }
227                         }
228                         else
229                         {
230                             /* Removing file. */
231                             unlink(file);
232                         }
233                     }
234
235                     file[0] = '\0';
236                 }
237             }
238
239             else
240             {
241                 merror("%s: WARN: Unknown message received from server.", ARGV0);
242             }
243         }
244
245         else if(fp)
246         {
247             available_server = (int)time(NULL);
248             fprintf(fp, "%s", tmp_msg);
249         }
250
251         else
252         {
253             merror("%s: WARN: Unknown message received. No action defined.",
254                     ARGV0);
255         }
256     }
257
258
259     return(NULL);
260
261 }
262
263
264 /* EOF */