Imported Upstream version 2.7
[ossec-hids.git] / src / shared / agent_op.c
1 /* @(#) $Id: ./src/shared/agent_op.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
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
10  * Foundation
11  */
12
13
14 #include "shared.h"
15
16
17
18 /** Checks if syscheck is to be executed/restarted.
19  *  Returns 1 on success or 0 on failure (shouldn't be executed now).
20  */
21 int os_check_restart_syscheck()
22 {
23     struct stat restart_status;
24
25     /* If the restart is not present, return 0.
26      */
27
28     if(isChroot())
29     {
30         if(stat(SYSCHECK_RESTART, &restart_status) == -1)
31             return(0);
32
33         unlink(SYSCHECK_RESTART);
34     }
35     else
36     {
37         if(stat(SYSCHECK_RESTART_PATH, &restart_status) == -1)
38             return(0);
39
40         unlink(SYSCHECK_RESTART_PATH);
41     }
42
43
44     return(1);
45 }
46
47
48
49 /** Sets syscheck to be restarted.
50  *  Returns 1 on success or 0 on failure.
51  */
52 int os_set_restart_syscheck()
53 {
54     FILE *fp;
55
56     fp = fopen(SYSCHECK_RESTART, "w");
57     if(!fp)
58     {
59         merror(FOPEN_ERROR, __local_name, SYSCHECK_RESTART);
60         return(0);
61     }
62
63     fprintf(fp, "%s\n", SYSCHECK_RESTART);
64     fclose(fp);
65
66
67     return(1);
68 }
69
70
71
72 /** char *os_read_agent_name()
73  *  Reads the agent name for the current agent.
74  *  Returns NULL on error.
75  */
76 char* os_read_agent_name()
77 {
78     char buf[1024 + 1];
79     FILE *fp = NULL;
80
81     debug2("%s: calling os_read_agent_name().", ARGV0);
82
83     if(isChroot())
84         fp = fopen(AGENT_INFO_FILE, "r");
85     else
86         fp = fopen(AGENT_INFO_FILEP, "r");
87
88     /* We give 1 second for the file to be created... */
89     if(!fp)
90     {
91         sleep(1);
92
93         if(isChroot())
94             fp = fopen(AGENT_INFO_FILE, "r");
95         else
96             fp = fopen(AGENT_INFO_FILEP, "r");
97     }
98
99     if(!fp)
100     {
101         debug1(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
102         return(NULL);
103     }
104
105     buf[1024] = '\0';
106
107
108     /* Getting name */
109     if(fgets(buf, 1024, fp))
110     {
111         char *ret = NULL;
112         os_strdup(buf, ret);
113         fclose(fp);
114
115         debug2("%s: os_read_agent_name returned (%s).", __local_name, ret);
116
117         return(ret);
118     }
119
120     fclose(fp);
121     return(NULL);
122 }
123
124
125
126 /** char *os_read_agent_ip()
127  *  Reads the agent ip for the current agent.
128  *  Returns NULL on error.
129  */
130 char *os_read_agent_ip()
131 {
132     char buf[1024 + 1];
133     FILE *fp;
134
135     debug2("%s: calling os_read_agent_ip().", ARGV0);
136
137     fp = fopen(AGENT_INFO_FILE, "r");
138     if(!fp)
139     {
140         merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
141         return(NULL);
142     }
143
144     buf[1024] = '\0';
145
146
147     /* Getting IP */
148     if(fgets(buf, 1024, fp) && fgets(buf, 1024, fp))
149     {
150         char *ret = NULL;
151         os_strdup(buf, ret);
152         fclose(fp);
153
154         return(ret);
155     }
156
157     fclose(fp);
158     return(NULL);
159 }
160
161
162
163 /** char *os_read_agent_id()
164  *  Reads the agent id for the current agent.
165  *  Returns NULL on error.
166  */
167 char *os_read_agent_id()
168 {
169     char buf[1024 + 1];
170     FILE *fp;
171
172     debug2("%s: calling os_read_agent_id().", ARGV0);
173
174     fp = fopen(AGENT_INFO_FILE, "r");
175     if(!fp)
176     {
177         merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
178         return(NULL);
179     }
180
181     buf[1024] = '\0';
182
183
184     /* Getting id */
185     if(fgets(buf, 1024, fp) && fgets(buf, 1024, fp) && fgets(buf, 1024, fp))
186     {
187         char *ret = NULL;
188         os_strdup(buf, ret);
189         fclose(fp);
190
191         return(ret);
192     }
193
194     fclose(fp);
195     return(NULL);
196 }
197
198
199 /*  cmoraes: begin add */
200
201 /** char *os_read_agent_profile()
202  *  Reads the agent profile name for the current agent.
203  *  Returns NULL on error.
204  *
205  *  Description:
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.
211  *
212  */
213 char* os_read_agent_profile()
214 {
215     char buf[1024 + 1];
216     FILE *fp;
217
218     debug2("%s: calling os_read_agent_profile().", __local_name);
219
220     if(isChroot())
221         fp = fopen(AGENT_INFO_FILE, "r");
222     else
223         fp = fopen(AGENT_INFO_FILEP, "r");
224
225     if(!fp)
226     {
227         debug2("%s: Failed to open file. Errno=%d.", ARGV0, errno);
228         merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
229         return(NULL);
230     }
231
232     buf[1024] = '\0';
233
234
235     /* Getting profile */
236     if(fgets(buf, 1024, fp) && fgets(buf, 1024, fp) &&
237        fgets(buf, 1024, fp) && fgets(buf, 1024, fp))
238     {
239         char *ret = NULL;
240
241         /* Trim the /n and/or /r at the end of the string */
242         os_trimcrlf(buf);
243
244         os_strdup(buf, ret);
245         debug2("%s: os_read_agent_profile() = [%s]", __local_name, ret);
246
247         fclose(fp);
248
249         return(ret);
250     }
251
252     fclose(fp);
253     return(NULL);
254 }
255 /* cmoraes: end add */
256
257
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.
261  */
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)
265 {
266     FILE *fp;
267
268     fp = fopen(AGENT_INFO_FILE, "w");
269     if(!fp)
270     {
271         merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE);
272         return(0);
273     }
274
275     /*cmoraes: added cfg_profile_name parameter*/
276     fprintf(fp, "%s\n-\n%s\n%s\n", agent_name, agent_id, cfg_profile_name);
277     fclose(fp);
278     return(1);
279 }
280
281
282
283 int os_agent_config_changed()
284 {
285     return(0);
286 }
287
288
289 /* EOF */