new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / shared / agent_op.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights 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 "agent_op.h"
11 #include "shared.h"
12
13
14 /* Check if syscheck is to be executed/restarted
15  * Returns 1 on success or 0 on failure (shouldn't be executed now)
16  */
17 int os_check_restart_syscheck()
18 {
19     /* If the restart is not present, return 0 */
20     if (isChroot()) {
21         if (unlink(SYSCHECK_RESTART) == -1) {
22             return (0);
23         }
24     } else {
25         if (unlink(SYSCHECK_RESTART_PATH) == -1) {
26             return (0);
27         }
28     }
29
30     return (1);
31 }
32
33 /* Set syscheck to be restarted
34  * Returns 1 on success or 0 on failure
35  */
36 int os_set_restart_syscheck()
37 {
38     FILE *fp;
39
40     fp = fopen(SYSCHECK_RESTART, "w");
41     if (!fp) {
42         merror(FOPEN_ERROR, __local_name, SYSCHECK_RESTART, errno, strerror(errno));
43         return (0);
44     }
45
46     fprintf(fp, "%s\n", SYSCHECK_RESTART);
47     fclose(fp);
48
49     return (1);
50 }
51
52 /* Read the agent name for the current agent
53  * Returns NULL on error
54  */
55 char *os_read_agent_name()
56 {
57     char buf[1024 + 1];
58     FILE *fp = NULL;
59
60     debug2("%s: calling os_read_agent_name().", __local_name);
61
62     if (isChroot()) {
63         fp = fopen(AGENT_INFO_FILE, "r");
64     } else {
65         fp = fopen(AGENT_INFO_FILEP, "r");
66     }
67
68     /* We give 1 second for the file to be created */
69     if (!fp) {
70         sleep(1);
71
72         if (isChroot()) {
73             fp = fopen(AGENT_INFO_FILE, "r");
74         } else {
75             fp = fopen(AGENT_INFO_FILEP, "r");
76         }
77     }
78
79     if (!fp) {
80         debug1(FOPEN_ERROR, __local_name, AGENT_INFO_FILE, errno, strerror(errno));
81         return (NULL);
82     }
83
84     buf[1024] = '\0';
85
86     /* Get name */
87     if (fgets(buf, 1024, fp)) {
88         char *ret = NULL;
89         int len;
90
91         // strip the newlines
92         len = strlen(buf) - 1; 
93         while (len > 0 && buf[len] == '\n') 
94             buf[len--] = '\0'; 
95
96         os_strdup(buf, ret);
97         fclose(fp);
98
99         debug2("%s: os_read_agent_name returned (%s).", __local_name, ret);
100
101         return (ret);
102     }
103
104     fclose(fp);
105     return (NULL);
106 }
107
108 /* Read the agent ip for the current agent
109  * Returns NULL on error
110  */
111 char *os_read_agent_ip()
112 {
113     char buf[1024 + 1];
114     FILE *fp;
115
116     debug2("%s: calling os_read_agent_ip().", __local_name);
117
118     fp = fopen(AGENT_INFO_FILE, "r");
119     if (!fp) {
120         merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE, errno, strerror(errno));
121         return (NULL);
122     }
123
124     buf[1024] = '\0';
125
126     /* Get IP */
127     if (fgets(buf, 1024, fp) && fgets(buf, 1024, fp)) {
128         char *ret = NULL;
129         os_strdup(buf, ret);
130         fclose(fp);
131
132         return (ret);
133     }
134
135     fclose(fp);
136     return (NULL);
137 }
138
139 /* Read the agent id for the current agent
140  * Returns NULL on error
141  */
142 char *os_read_agent_id()
143 {
144     char buf[1024 + 1];
145     FILE *fp;
146
147     debug2("%s: calling os_read_agent_id().", __local_name);
148
149     fp = fopen(AGENT_INFO_FILE, "r");
150     if (!fp) {
151         merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE, errno, strerror(errno));
152         return (NULL);
153     }
154
155     buf[1024] = '\0';
156
157     /* Get id */
158     if (fgets(buf, 1024, fp) && fgets(buf, 1024, fp) && fgets(buf, 1024, fp)) {
159         char *ret = NULL;
160         os_strdup(buf, ret);
161         fclose(fp);
162
163         return (ret);
164     }
165
166     fclose(fp);
167     return (NULL);
168 }
169
170 /*  Read the agent profile name for the current agent
171  *  Returns NULL on error
172  *
173  *  Description:
174  *  Comma separated list of strings that used to identify what type
175  *  of configuration is used for this agent.
176  *  The profile name is set in the agent's etc/ossec.conf file
177  *  It is matched with the ossec manager's agent.conf file to read
178  *  configuration only applicable to this profile name.
179  */
180 char *os_read_agent_profile()
181 {
182     char buf[1024 + 1];
183     FILE *fp;
184
185     debug2("%s: calling os_read_agent_profile().", __local_name);
186
187     if (isChroot()) {
188         fp = fopen(AGENT_INFO_FILE, "r");
189     } else {
190         fp = fopen(AGENT_INFO_FILEP, "r");
191     }
192
193     if (!fp) {
194         debug2("%s: Failed to open file. Errno=%d.", __local_name, errno);
195         merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE, errno, strerror(errno));
196         return (NULL);
197     }
198
199     buf[1024] = '\0';
200
201     /* Get profile */
202     if (fgets(buf, 1024, fp) && fgets(buf, 1024, fp) &&
203             fgets(buf, 1024, fp) && fgets(buf, 1024, fp)) {
204         char *ret = NULL;
205
206         /* Trim the /n and/or /r at the end of the string */
207         os_trimcrlf(buf);
208
209         os_strdup(buf, ret);
210         debug2("%s: os_read_agent_profile() = [%s]", __local_name, ret);
211
212         fclose(fp);
213
214         return (ret);
215     }
216
217     fclose(fp);
218     return (NULL);
219 }
220
221 /* Write the agent info to the queue, for the other processes to read
222  * Returns 1 on success or <= 0 on failure
223  */
224 int os_write_agent_info(const char *agent_name, __attribute__((unused)) const char *agent_ip,
225                         const char *agent_id, const char *cfg_profile_name)
226 {
227     FILE *fp;
228
229     fp = fopen(AGENT_INFO_FILE, "w");
230     if (!fp) {
231         merror(FOPEN_ERROR, __local_name, AGENT_INFO_FILE, errno, strerror(errno));
232         return (0);
233     }
234
235     fprintf(
236         fp,
237         "%s\n-\n%s\n%s\n",
238         agent_name,
239         agent_id,
240         (cfg_profile_name) ? cfg_profile_name : "-"
241     );
242     fclose(fp);
243     return (1);
244 }
245