1 /* Copyright (C) 2009 Trend Micro Inc.
2 * All right reserved.
3 *
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
7 * Foundation
8 */
9
10 #include "shared.h"
11 #include "os_crypto/md5/md5_op.h"
12 #include "os_net/os_net.h"
13 #include "agentd.h"
14
15
16 #ifndef WIN32
17 static time_t g_saved_time = 0;
18 static char *rand_keepalive_str2(char *dst, int size);
19
rand_keepalive_str2(char * dst,int size)20 static char *rand_keepalive_str2(char *dst, int size)
21 {
22 static const char text[] = "abcdefghijklmnopqrstuvwxyz"
23 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24 "0123456789"
25 "!@#$%^&*()_+-=;'[],./?";
26 int i, len = rand() % (size - 1);
27 for ( i = 0; i < len; ++i ) {
28 dst[i] = text[(unsigned)rand() % (sizeof text - 1)];
29 }
30 dst[i] = '\0';
31 return dst;
32 }
33 #endif
34
35 /* Return the names of the files in a directory */
getsharedfiles()36 char *getsharedfiles()
37 {
38 unsigned int m_size = 512;
39 char *ret;
40 os_md5 md5sum;
41
42 if (OS_MD5_File(SHAREDCFG_FILE, md5sum, OS_TEXT) != 0) {
43 md5sum[0] = 'x';
44 md5sum[1] = '\0';
45 }
46
47 /* We control these files, max size is m_size */
48 ret = (char *)calloc(m_size + 1, sizeof(char));
49 if (!ret) {
50 merror(MEM_ERROR, ARGV0, errno, strerror(errno));
51 return (NULL);
52 }
53
54 snprintf(ret, m_size, "%s merged.mg\n", md5sum);
55
56 return (ret);
57 }
58
59 #ifndef WIN32
60
61 /* Periodically send notification to server */
run_notify()62 void run_notify()
63 {
64 char keep_alive_random[1024];
65 char tmp_msg[OS_SIZE_1024 + 1];
66 char *uname;
67 char *shared_files;
68 os_md5 md5sum;
69 time_t curr_time;
70
71 keep_alive_random[0] = '\0';
72 curr_time = time(0);
73
74 #ifndef ONEWAY_ENABLED
75 /* Check if the server has responded */
76 if ((curr_time - available_server) > agt->max_time_reconnect_try) {
77 /* If response is not available, set lock and wait for it */
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 /* Check if time has elapsed */
90 if ((curr_time - g_saved_time) < agt->notify_time) {
91 return;
92 }
93 g_saved_time = curr_time;
94
95 debug1("%s: DEBUG: Sending agent notification.", ARGV0);
96
97 /* Send the message
98 * Message is going to be the uname\n checksum file\n checksum file\n
99 */
100
101 /* Get uname */
102 uname = getuname();
103 if (!uname) {
104 merror(MEM_ERROR, ARGV0, errno, strerror(errno));
105 return;
106 }
107
108 /* Get shared files */
109 shared_files = getsharedfiles();
110 if (!shared_files) {
111 shared_files = strdup("\0");
112 if (!shared_files) {
113 free(uname);
114 merror(MEM_ERROR, ARGV0, errno, strerror(errno));
115 return;
116 }
117 }
118
119 rand_keepalive_str2(keep_alive_random, 700);
120
121 /* Create message */
122 if ((File_DateofChange(AGENTCONFIGINT) > 0 ) &&
123 (OS_MD5_File(AGENTCONFIGINT, md5sum, OS_TEXT) == 0)) {
124 snprintf(tmp_msg, OS_SIZE_1024, "#!-%s / %s\n%s\n%s",
125 uname, md5sum, shared_files, keep_alive_random);
126 } else {
127 snprintf(tmp_msg, OS_SIZE_1024, "#!-%s\n%s\n%s",
128 uname, shared_files, keep_alive_random);
129 }
130
131 /* Send status message */
132 send_msg(0, tmp_msg);
133
134 free(uname);
135 free(shared_files);
136
137 return;
138 }
139 #endif /* !WIN32 */
140
141