Imported Upstream version 2.3
[ossec-hids.git] / src / client-agent / notify.c
1 /* @(#) $Id: notify.c,v 1.23 2009/06/26 13:50:02 dcid Exp $ */
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 3) 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     /* Check if the server has responded */
72     if((curr_time - available_server) > (3*NOTIFY_TIME))
73     {
74         /* If response is not available, set lock and
75          * wait for it.
76          */
77         verbose(SERVER_UNAV, ARGV0);
78         os_setwait();
79
80         /* Send sync message */
81         start_agent(0);
82
83         verbose(SERVER_UP, ARGV0);
84         os_delwait();
85     }
86
87
88     /* Check if time has elapsed */
89     if((curr_time - g_saved_time) < (NOTIFY_TIME - 120))
90     {
91         return;
92     }
93     g_saved_time = curr_time;
94     
95     debug1("%s: DEBUG: Sending agent notification.", ARGV0);
96
97
98     /* Send the message.
99      * Message is going to be the 
100      * uname\n checksum file\n checksum file\n 
101      */   
102
103     /* Getting uname */
104     uname = getuname();
105     if(!uname)
106     {
107         merror(MEM_ERROR,ARGV0);
108         return;
109     }
110
111
112     /* get shared files */
113     shared_files = getsharedfiles();
114     if(!shared_files)
115     {
116         shared_files = strdup("\0");
117         if(!shared_files)
118         {
119             free(uname);
120             merror(MEM_ERROR,ARGV0);
121             return;
122         }
123     }
124
125
126     /* creating message */
127     if(File_DateofChange(AGENTCONFIGINT) > 0)
128     {
129         os_md5 md5sum;
130         if(OS_MD5_File(AGENTCONFIGINT, md5sum) != 0)
131         {
132             snprintf(tmp_msg, OS_SIZE_1024, "#!-%s\n%s",uname, shared_files);
133         }
134         else
135         {
136             snprintf(tmp_msg, OS_SIZE_1024, "#!-%s / %s\n%s",uname, md5sum, shared_files);
137         }
138     }
139     else
140     {
141         snprintf(tmp_msg, OS_SIZE_1024, "#!-%s\n%s",uname, shared_files);
142     }
143
144
145     /* Sending status message */
146     send_msg(0, tmp_msg);
147
148
149     free(uname);
150     free(shared_files);
151
152     return;
153 }
154 #endif
155
156
157 /* EOF */