1 /* @(#) $Id: ./src/shared/agent_op.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
18 /** Checks if syscheck is to be executed/restarted.
19 * Returns 1 on success or 0 on failure (shouldn't be executed now).
21 int os_check_restart_syscheck()
23 struct stat restart_status;
25 /* If the restart is not present, return 0.
30 if(stat(SYSCHECK_RESTART, &restart_status) == -1)
33 unlink(SYSCHECK_RESTART);
37 if(stat(SYSCHECK_RESTART_PATH, &restart_status) == -1)
40 unlink(SYSCHECK_RESTART_PATH);
49 /** Sets syscheck to be restarted.
50 * Returns 1 on success or 0 on failure.
52 int os_set_restart_syscheck()
56 fp = fopen(SYSCHECK_RESTART, "w");
59 merror(FOPEN_ERROR, __local_name, SYSCHECK_RESTART);
63 fprintf(fp, "%s\n", SYSCHECK_RESTART);
72 /** char *os_read_agent_name()
73 * Reads the agent name for the current agent.
74 * Returns NULL on error.
76 char* os_read_agent_name()
81 debug2("%s: calling os_read_agent_name().", ARGV0);
84 fp = fopen(AGENT_INFO_FILE, "r");
86 fp = fopen(AGENT_INFO_FILEP, "r");
88 /* We give 1 second for the file to be created... */
94 fp = fopen(AGENT_INFO_FILE, "r");
96 fp = fopen(AGENT_INFO_FILEP, "r");
101 debug1(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
109 if(fgets(buf, 1024, fp))
115 debug2("%s: os_read_agent_name returned (%s).", __local_name, ret);
126 /** char *os_read_agent_ip()
127 * Reads the agent ip for the current agent.
128 * Returns NULL on error.
130 char *os_read_agent_ip()
135 debug2("%s: calling os_read_agent_ip().", ARGV0);
137 fp = fopen(AGENT_INFO_FILE, "r");
140 merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
148 if(fgets(buf, 1024, fp) && fgets(buf, 1024, fp))
163 /** char *os_read_agent_id()
164 * Reads the agent id for the current agent.
165 * Returns NULL on error.
167 char *os_read_agent_id()
172 debug2("%s: calling os_read_agent_id().", ARGV0);
174 fp = fopen(AGENT_INFO_FILE, "r");
177 merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
185 if(fgets(buf, 1024, fp) && fgets(buf, 1024, fp) && fgets(buf, 1024, fp))
199 /* cmoraes: begin add */
201 /** char *os_read_agent_profile()
202 * Reads the agent profile name for the current agent.
203 * Returns NULL on error.
206 * Comma separated list of strings that used to identify what type
207 * of configuration is used for this agent.
208 * The profile name is set in the agent's etc/ossec.conf file
209 * It is matched with the ossec manager's agent.conf file to read
210 * configuration only applicable to this profile name.
213 char* os_read_agent_profile()
218 debug2("%s: calling os_read_agent_profile().", __local_name);
221 fp = fopen(AGENT_INFO_FILE, "r");
223 fp = fopen(AGENT_INFO_FILEP, "r");
227 debug2("%s: Failed to open file. Errno=%d.", ARGV0, errno);
228 merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
235 /* Getting profile */
236 if(fgets(buf, 1024, fp) && fgets(buf, 1024, fp) &&
237 fgets(buf, 1024, fp) && fgets(buf, 1024, fp))
241 /* Trim the /n and/or /r at the end of the string */
245 debug2("%s: os_read_agent_profile() = [%s]", __local_name, ret);
255 /* cmoraes: end add */
258 /** int os_write_agent_info(char *agent_name, char *agent_ip, char *agent_id)
259 * Writes the agent info inside the queue, for the other processes to read.
260 * Returns 1 on success or <= 0 on failure.
262 /* cmoraes: changed function. added cfg_profile_name parameter */
263 int os_write_agent_info(char *agent_name, char *agent_ip,
264 char *agent_id, char *cfg_profile_name)
268 fp = fopen(AGENT_INFO_FILE, "w");
271 merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
275 /*cmoraes: added cfg_profile_name parameter*/
276 fprintf(fp, "%s\n-\n%s\n%s\n", agent_name, agent_id, cfg_profile_name);
283 int os_agent_config_changed()