Imported Upstream version 2.5.1
[ossec-hids.git] / src / client-agent / notify.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13 #include "shared.h"
14
15 #include "os_crypto/md5/md5_op.h"
16 #include "os_net/os_net.h"
17 #include "agentd.h"
18
19 time_t g_saved_time = 0;
20
21
22
23 /* getfiles: Return the name of the files in a directory
24  */
25 char *getsharedfiles()
26 {
27     int m_size = 512;
28
29     char *ret;
30     
31     os_md5 md5sum;
32     
33
34     if(OS_MD5_File(SHAREDCFG_FILE, md5sum) != 0)
35     {
36         md5sum[0] = 'x';
37         md5sum[1] = 'x';
38         md5sum[1] = '\0';
39     }
40
41
42     /* we control these files, max size is m_size */
43     ret = (char *)calloc(m_size +1, sizeof(char));
44     if(!ret)
45     {
46         merror(MEM_ERROR, ARGV0);
47         return(NULL);
48     }
49
50
51     snprintf(ret, m_size, "%s merged.mg\n", md5sum);
52     
53
54     return(ret);
55 }
56
57 #ifndef WIN32
58
59 /* run_notify: Send periodically notification to server */
60 void run_notify()
61 {
62     char tmp_msg[OS_SIZE_1024 +1];
63     char *uname;
64     char *shared_files;
65
66     time_t curr_time;
67
68     curr_time = time(0);
69
70
71     #ifndef ONEWAY
72     /* Check if the server has responded */
73     if((curr_time - available_server) > (3*NOTIFY_TIME))
74     {
75         /* If response is not available, set lock and
76          * wait for it.
77          */
78         verbose(SERVER_UNAV, ARGV0);
79         os_setwait();
80
81         /* Send sync message */
82         start_agent(0);
83
84         verbose(SERVER_UP, ARGV0);
85         os_delwait();
86     }
87     #endif
88
89
90     /* Check if time has elapsed */
91     if((curr_time - g_saved_time) < (NOTIFY_TIME - 120))
92     {
93         return;
94     }
95     g_saved_time = curr_time;
96     
97     debug1("%s: DEBUG: Sending agent notification.", ARGV0);
98
99
100     /* Send the message.
101      * Message is going to be the 
102      * uname\n checksum file\n checksum file\n 
103      */   
104
105     /* Getting uname */
106     uname = getuname();
107     if(!uname)
108     {
109         merror(MEM_ERROR,ARGV0);
110         return;
111     }
112
113
114     /* get shared files */
115     shared_files = getsharedfiles();
116     if(!shared_files)
117     {
118         shared_files = strdup("\0");
119         if(!shared_files)
120         {
121             free(uname);
122             merror(MEM_ERROR,ARGV0);
123             return;
124         }
125     }
126
127
128     /* creating message */
129     if(File_DateofChange(AGENTCONFIGINT) > 0)
130     {
131         os_md5 md5sum;
132         if(OS_MD5_File(AGENTCONFIGINT, md5sum) != 0)
133         {
134             snprintf(tmp_msg, OS_SIZE_1024, "#!-%s\n%s",uname, shared_files);
135         }
136         else
137         {
138             snprintf(tmp_msg, OS_SIZE_1024, "#!-%s / %s\n%s",uname, md5sum, shared_files);
139         }
140     }
141     else
142     {
143         snprintf(tmp_msg, OS_SIZE_1024, "#!-%s\n%s",uname, shared_files);
144     }
145
146
147     /* Sending status message */
148     send_msg(0, tmp_msg);
149
150
151     free(uname);
152     free(shared_files);
153
154     return;
155 }
156 #endif
157
158
159 /* EOF */