Imported Upstream version 2.7
[ossec-hids.git] / src / client-agent / notify.c
1 /* @(#) $Id: ./src/client-agent/notify.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 #include "os_crypto/md5/md5_op.h"
17 #include "os_net/os_net.h"
18 #include "agentd.h"
19
20 time_t g_saved_time = 0;
21
22
23 char *rand_keepalive_str2(char *dst, int size)
24 {
25     static const char text[] = "abcdefghijklmnopqrstuvwxyz"
26                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
27                                "0123456789"
28                                "!@#$%^&*()_+-=;'[],./?";
29     int i, len = rand() % (size - 1);
30     for ( i = 0; i < len; ++i )
31     {
32         dst[i] = text[rand() % (sizeof text - 1)];
33     }
34     dst[i] = '\0';
35     return dst;
36 }
37
38 /* getfiles: Return the name of the files in a directory
39  */
40 char *getsharedfiles()
41 {
42     int m_size = 512;
43
44     char *ret;
45
46     os_md5 md5sum;
47
48
49     if(OS_MD5_File(SHAREDCFG_FILE, md5sum) != 0)
50     {
51         md5sum[0] = 'x';
52         md5sum[1] = 'x';
53         md5sum[1] = '\0';
54     }
55
56
57     /* we control these files, max size is m_size */
58     ret = (char *)calloc(m_size +1, sizeof(char));
59     if(!ret)
60     {
61         merror(MEM_ERROR, ARGV0);
62         return(NULL);
63     }
64
65
66     snprintf(ret, m_size, "%s merged.mg\n", md5sum);
67
68
69     return(ret);
70 }
71
72 #ifndef WIN32
73
74 /* run_notify: Send periodically notification to server */
75 void run_notify()
76 {
77     char keep_alive_random[1024];
78     char tmp_msg[OS_SIZE_1024 +1];
79     char *uname;
80     char *shared_files;
81     os_md5 md5sum;
82
83
84     keep_alive_random[0] = '\0';
85
86     time_t curr_time;
87
88     curr_time = time(0);
89
90
91     #ifndef ONEWAY
92     /* Check if the server has responded */
93     if((curr_time - available_server) > (3*NOTIFY_TIME))
94     {
95         /* If response is not available, set lock and
96          * wait for it.
97          */
98         verbose(SERVER_UNAV, ARGV0);
99         os_setwait();
100
101         /* Send sync message */
102         start_agent(0);
103
104         verbose(SERVER_UP, ARGV0);
105         os_delwait();
106     }
107     #endif
108
109
110     /* Check if time has elapsed */
111     if((curr_time - g_saved_time) < (NOTIFY_TIME - 120))
112     {
113         return;
114     }
115     g_saved_time = curr_time;
116
117     debug1("%s: DEBUG: Sending agent notification.", ARGV0);
118
119
120     /* Send the message.
121      * Message is going to be the
122      * uname\n checksum file\n checksum file\n
123      */
124
125     /* Getting uname */
126     uname = getuname();
127     if(!uname)
128     {
129         merror(MEM_ERROR,ARGV0);
130         return;
131     }
132
133
134     /* get shared files */
135     shared_files = getsharedfiles();
136     if(!shared_files)
137     {
138         shared_files = strdup("\0");
139         if(!shared_files)
140         {
141             free(uname);
142             merror(MEM_ERROR,ARGV0);
143             return;
144         }
145     }
146
147     rand_keepalive_str2(keep_alive_random, 700);
148
149
150     /* creating message */
151     if((File_DateofChange(AGENTCONFIGINT) > 0 ) &&
152        (OS_MD5_File(AGENTCONFIGINT, md5sum) == 0))
153     {
154         snprintf(tmp_msg, OS_SIZE_1024, "#!-%s / %s\n%s\n%s",
155                  uname, md5sum, shared_files, keep_alive_random);
156     }
157     else
158     {
159         snprintf(tmp_msg, OS_SIZE_1024, "#!-%s\n%s\n%s",
160                  uname, shared_files, keep_alive_random);
161     }
162
163
164     /* Sending status message */
165     send_msg(0, tmp_msg);
166
167
168     free(uname);
169     free(shared_files);
170
171     return;
172 }
173 #endif
174
175
176 /* EOF */