1 /* Copyright (C) 2009 Trend Micro Inc.
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
11 #include "os_crypto/md5/md5_op.h"
12 #include "os_net/os_net.h"
17 static time_t g_saved_time = 0;
18 static char *rand_keepalive_str2(char *dst, int size);
20 static char *rand_keepalive_str2(char *dst, int size)
22 static const char text[] = "abcdefghijklmnopqrstuvwxyz"
23 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
25 "!@#$%^&*()_+-=;'[],./?";
26 int i, len = rand() % (size - 1);
27 for ( i = 0; i < len; ++i ) {
28 dst[i] = text[(unsigned)rand() % (sizeof text - 1)];
35 /* Return the names of the files in a directory */
36 char *getsharedfiles()
38 unsigned int m_size = 512;
42 if (OS_MD5_File(SHAREDCFG_FILE, md5sum, OS_TEXT) != 0) {
47 /* We control these files, max size is m_size */
48 ret = (char *)calloc(m_size + 1, sizeof(char));
50 merror(MEM_ERROR, ARGV0, errno, strerror(errno));
54 snprintf(ret, m_size, "%s merged.mg\n", md5sum);
61 /* Periodically send notification to server */
64 char keep_alive_random[1024];
65 char tmp_msg[OS_SIZE_1024 + 1];
71 keep_alive_random[0] = '\0';
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);
81 /* Send sync message */
84 verbose(SERVER_UP, ARGV0);
89 /* Check if time has elapsed */
90 if ((curr_time - g_saved_time) < agt->notify_time) {
93 g_saved_time = curr_time;
95 debug1("%s: DEBUG: Sending agent notification.", ARGV0);
98 * Message is going to be the uname\n checksum file\n checksum file\n
104 merror(MEM_ERROR, ARGV0, errno, strerror(errno));
108 /* Get shared files */
109 shared_files = getsharedfiles();
111 shared_files = strdup("\0");
114 merror(MEM_ERROR, ARGV0, errno, strerror(errno));
119 rand_keepalive_str2(keep_alive_random, 700);
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);
127 snprintf(tmp_msg, OS_SIZE_1024, "#!-%s\n%s\n%s",
128 uname, shared_files, keep_alive_random);
131 /* Send status message */
132 send_msg(0, tmp_msg);