1 | /* SAMHAIN file system integrity testing */
|
---|
2 | /* Copyright (C) 2002 Rainer Wichmann */
|
---|
3 | /* */
|
---|
4 | /* This program is free software; you can redistribute it */
|
---|
5 | /* and/or modify */
|
---|
6 | /* it under the terms of the GNU General Public License as */
|
---|
7 | /* published by */
|
---|
8 | /* the Free Software Foundation; either version 2 of the License, or */
|
---|
9 | /* (at your option) any later version. */
|
---|
10 | /* */
|
---|
11 | /* This program is distributed in the hope that it will be useful, */
|
---|
12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
---|
13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
---|
14 | /* GNU General Public License for more details. */
|
---|
15 | /* */
|
---|
16 | /* You should have received a copy of the GNU General Public License */
|
---|
17 | /* along with this program; if not, write to the Free Software */
|
---|
18 | /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
19 |
|
---|
20 | #include "config_xor.h"
|
---|
21 |
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <string.h>
|
---|
25 | #include <ctype.h>
|
---|
26 |
|
---|
27 | #include <errno.h>
|
---|
28 | #include <sys/types.h>
|
---|
29 | #include <unistd.h>
|
---|
30 |
|
---|
31 | /*
|
---|
32 | gcc -Wall -O2 -o mysched sh_schedule.c -DTESTONLY
|
---|
33 | */
|
---|
34 | #ifndef TESTONLY
|
---|
35 |
|
---|
36 |
|
---|
37 | #undef FIL__
|
---|
38 | #define FIL__ _("sh_schedule.c")
|
---|
39 |
|
---|
40 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
41 | #define SCHEDULER_YES
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #if TIME_WITH_SYS_TIME
|
---|
45 | #include <sys/time.h>
|
---|
46 | #include <time.h>
|
---|
47 | #else
|
---|
48 | #if HAVE_SYS_TIME_H
|
---|
49 | #include <sys/time.h>
|
---|
50 | #else
|
---|
51 | #include <time.h>
|
---|
52 | #endif
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #include "samhain.h"
|
---|
56 | #include "sh_mem.h"
|
---|
57 |
|
---|
58 | /* TESTONLY */
|
---|
59 | #else
|
---|
60 |
|
---|
61 | #define SCHEDULER_YES
|
---|
62 | #include <time.h>
|
---|
63 |
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #include "sh_schedule.h"
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | #ifdef SCHEDULER_YES
|
---|
71 |
|
---|
72 | /************************************************
|
---|
73 | *
|
---|
74 | * Scheduler class - private area
|
---|
75 | *
|
---|
76 | ************************************************/
|
---|
77 |
|
---|
78 |
|
---|
79 | static const int sh_schedule_max[5] = { 59, 23, 31, 12, 7 };
|
---|
80 | static const int sh_schedule_min[5] = { 0, 0, 0, 0, 0 };
|
---|
81 |
|
---|
82 | static
|
---|
83 | int test_val (int i, int min, int max, int min_step,
|
---|
84 | time_t * last, time_t now, int nval, int first_flag)
|
---|
85 | {
|
---|
86 | /* don't miss a minute's task
|
---|
87 | * IDEA: set last = now after first check (? seems to work)
|
---|
88 | */
|
---|
89 | if (i == 0 && max == min && nval > max
|
---|
90 | /* && ( ((now - *last) > min_step) || (*last == (time_t)-1) ) */ )
|
---|
91 | {
|
---|
92 | if (*last == (time_t)-1)
|
---|
93 | {
|
---|
94 | /* fake execution at nval-max
|
---|
95 | */
|
---|
96 | *last = now - 60 * (nval-max);
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 | if ((int)(now - *last) > min_step)
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /* out of range
|
---|
104 | */
|
---|
105 | if (nval > max || nval < min)
|
---|
106 | return 0;
|
---|
107 |
|
---|
108 | /* first call - invalid last_exec
|
---|
109 |
|
---|
110 | if (*last == (time_t)-1)
|
---|
111 | return 1;
|
---|
112 | */
|
---|
113 |
|
---|
114 | if (first_flag == 0)
|
---|
115 | return 1;
|
---|
116 |
|
---|
117 |
|
---|
118 | /* before min_step - too early (e.g. same minute)
|
---|
119 | */
|
---|
120 | if ((int)(now - *last) <= min_step)
|
---|
121 | return 0;
|
---|
122 |
|
---|
123 | return 1;
|
---|
124 | }
|
---|
125 |
|
---|
126 | static
|
---|
127 | int test_sched_int (sh_schedule_t * isched)
|
---|
128 | {
|
---|
129 | time_t now;
|
---|
130 | struct tm * tval;
|
---|
131 | int count, i, nval;
|
---|
132 |
|
---|
133 | if (!isched)
|
---|
134 | return 0;
|
---|
135 |
|
---|
136 | now = time(NULL);
|
---|
137 | tval = localtime(&now);
|
---|
138 |
|
---|
139 | count = 0;
|
---|
140 | for (i = 0; i < 5; ++i)
|
---|
141 | {
|
---|
142 | if (i == 0) nval = tval->tm_min;
|
---|
143 | else if (i == 1) nval = tval->tm_hour;
|
---|
144 | else if (i == 2) nval = tval->tm_mday;
|
---|
145 | else if (i == 3) nval = tval->tm_mon;
|
---|
146 | else nval = tval->tm_wday;
|
---|
147 | count += test_val (i, isched->min[i], isched->max[i],
|
---|
148 | isched->min_step, &(isched->last_exec),
|
---|
149 | now, nval, isched->first);
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (count == 5)
|
---|
153 | {
|
---|
154 | isched->first = 1;
|
---|
155 | isched->last_exec = now;
|
---|
156 | return 1;
|
---|
157 | }
|
---|
158 |
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /* test a linked list of schedules
|
---|
163 | */
|
---|
164 | int test_sched (sh_schedule_t * isched)
|
---|
165 | {
|
---|
166 | sh_schedule_t * intern = isched;
|
---|
167 | int retval = 0;
|
---|
168 |
|
---|
169 | while (intern != NULL)
|
---|
170 | {
|
---|
171 | if (test_sched_int(intern) == 1)
|
---|
172 | retval = 1;
|
---|
173 | intern = intern->next;
|
---|
174 | }
|
---|
175 | return retval;
|
---|
176 | }
|
---|
177 |
|
---|
178 | static
|
---|
179 | char DayNames[7][4] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
|
---|
180 | static
|
---|
181 | char MonNames[12][4] = { "jan", "feb", "mar", "apr", "may", "jun",
|
---|
182 | "jul", "aug", "sep", "oct", "nov", "dec" };
|
---|
183 |
|
---|
184 | static
|
---|
185 | int parse_func (int i, char * p)
|
---|
186 | {
|
---|
187 | int j, k, l;
|
---|
188 | char *tail;
|
---|
189 |
|
---|
190 | errno = 0;
|
---|
191 | j = (int) strtol(p, &tail, 10);
|
---|
192 |
|
---|
193 | if (errno != 0) /* overflow */
|
---|
194 | return -1;
|
---|
195 | if (j < 0)
|
---|
196 | return -1;
|
---|
197 | if (tail != p) /* numeric */
|
---|
198 | return j;
|
---|
199 | if (i < 3) /* names not allowed */
|
---|
200 | return -1;
|
---|
201 |
|
---|
202 | if (i == 3)
|
---|
203 | {
|
---|
204 | for (j = 0; j < 12; ++j) {
|
---|
205 | l = 0;
|
---|
206 | /*@+charint@*//* Incompatible types for == (char, char): ??? */
|
---|
207 | for (k = 0; k < 3; ++k)
|
---|
208 | if (p[k] != '\0' && tolower(p[k]) == MonNames[j][k]) ++l;
|
---|
209 | /*@-charint@*/
|
---|
210 | if (l == 3)
|
---|
211 | return j;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | if (i == 4)
|
---|
215 | {
|
---|
216 | for (j = 0; j < 7; ++j) {
|
---|
217 | l = 0;
|
---|
218 | /*@+charint@*//* Incompatible types for == (char, char): ??? */
|
---|
219 | for (k = 0; k < 3; ++k)
|
---|
220 | if (p[k] != '\0' && tolower(p[k]) == DayNames[j][k]) ++l;
|
---|
221 | /*@-charint@*/
|
---|
222 | if (l == 3)
|
---|
223 | return j;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | return -1;
|
---|
228 | }
|
---|
229 |
|
---|
230 | static
|
---|
231 | int parse_token(int i, sh_schedule_t * isched, char * p)
|
---|
232 | {
|
---|
233 | char * q;
|
---|
234 |
|
---|
235 | if ( NULL != (q = strchr(p, ',')))
|
---|
236 | return -1;
|
---|
237 |
|
---|
238 | if (*p == '*')
|
---|
239 | {
|
---|
240 | isched->min[i] = sh_schedule_min[i];
|
---|
241 | isched->max[i] = sh_schedule_max[i];
|
---|
242 | }
|
---|
243 | else
|
---|
244 | {
|
---|
245 | isched->min[i] = parse_func(i, p);
|
---|
246 | if (i == 4 && isched->min[i] == 7)
|
---|
247 | isched->min[i] = 0;
|
---|
248 | if (isched->min[i] < sh_schedule_min[i] ||
|
---|
249 | isched->min[i] > sh_schedule_max[i])
|
---|
250 | {
|
---|
251 | return -1;
|
---|
252 | }
|
---|
253 | if ( NULL != (q = strchr(p, '-')))
|
---|
254 | {
|
---|
255 | ++q;
|
---|
256 | isched->max[i] = parse_func(i, q);
|
---|
257 | if (i == 4 && isched->max[i] == 7)
|
---|
258 | isched->max[i] = 0;
|
---|
259 | if (isched->max[i] < sh_schedule_min[i] ||
|
---|
260 | isched->max[i] > sh_schedule_max[i] ||
|
---|
261 | isched->max[i] < isched->min[i])
|
---|
262 | {
|
---|
263 | return -1;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | else
|
---|
267 | isched->max[i] = isched->min[i];
|
---|
268 | }
|
---|
269 |
|
---|
270 | if ( NULL != (q = strchr(p, '/')))
|
---|
271 | {
|
---|
272 | ++q;
|
---|
273 | isched->step[i] = atoi(q);
|
---|
274 | if (isched->step[i] < 1 || isched->step[i] > sh_schedule_max[i])
|
---|
275 | {
|
---|
276 | return -1;
|
---|
277 | }
|
---|
278 | if (i == 4 && isched->step[i] == 7)
|
---|
279 | isched->step[i] = 6;
|
---|
280 | }
|
---|
281 | else
|
---|
282 | {
|
---|
283 | isched->step[i] = 1;
|
---|
284 | }
|
---|
285 |
|
---|
286 | switch (i)
|
---|
287 | {
|
---|
288 | case 0:
|
---|
289 | if (isched->max[i] == isched->min[i])
|
---|
290 | isched->min_step = 3599;
|
---|
291 | else
|
---|
292 | isched->min_step = (isched->step[i] * 60) - 1;
|
---|
293 | break;
|
---|
294 | case 1:
|
---|
295 | if (isched->max[i] == isched->min[i])
|
---|
296 | {
|
---|
297 | /* fix for daylight saving time: subtract 3600 sec
|
---|
298 | */
|
---|
299 | if (isched->min_step == 3599)
|
---|
300 | isched->min_step = 86399 - 3600;
|
---|
301 | }
|
---|
302 | else
|
---|
303 | {
|
---|
304 | if (isched->min_step == 3599)
|
---|
305 | isched->min_step = (isched->step[i] * 3600) - 1;
|
---|
306 | }
|
---|
307 | break;
|
---|
308 | default:
|
---|
309 | break;
|
---|
310 | }
|
---|
311 |
|
---|
312 | return 0;
|
---|
313 | }
|
---|
314 |
|
---|
315 | static
|
---|
316 | int parse_sched (const char * ssched, sh_schedule_t * isched)
|
---|
317 | {
|
---|
318 | char * p;
|
---|
319 | char * copy;
|
---|
320 | int i = 0;
|
---|
321 | size_t len;
|
---|
322 |
|
---|
323 | if (!ssched || !isched)
|
---|
324 | return -1;
|
---|
325 |
|
---|
326 | len = strlen(ssched)+1;
|
---|
327 | #ifdef TESTONLY
|
---|
328 | copy = malloc(len); /* testonly code */
|
---|
329 | #else
|
---|
330 | copy = SH_ALLOC(len);
|
---|
331 | #endif
|
---|
332 | sl_strlcpy(copy, ssched, len);
|
---|
333 |
|
---|
334 | p = strtok(copy, " \t"); /* parse crontab-style schedule */
|
---|
335 | if (!p)
|
---|
336 | goto err;
|
---|
337 | if (parse_token(i, isched, p) == -1)
|
---|
338 | goto err;
|
---|
339 |
|
---|
340 | for (i = 1; i < 5; ++i)
|
---|
341 | {
|
---|
342 | p = strtok(NULL, " \t"); /* parse crontab-style schedule */
|
---|
343 | if (!p)
|
---|
344 | goto err;
|
---|
345 | if (parse_token(i, isched, p) == -1)
|
---|
346 | goto err;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /*
|
---|
350 | for (i = 0; i < 5; ++i)
|
---|
351 | printf("%2d MIN %3d MAX %3d STEP %3d\n",
|
---|
352 | i, isched->min[i], isched->max[i], isched->step[i]);
|
---|
353 | */
|
---|
354 |
|
---|
355 | isched->last_exec = (time_t)-1;
|
---|
356 | isched->first = 0;
|
---|
357 | isched->next = NULL;
|
---|
358 |
|
---|
359 | #ifdef TESTONLY
|
---|
360 | free(copy);
|
---|
361 | #else
|
---|
362 | SH_FREE(copy);
|
---|
363 | #endif
|
---|
364 | return 0;
|
---|
365 |
|
---|
366 | err:
|
---|
367 | #ifdef TESTONLY
|
---|
368 | free(copy);
|
---|
369 | #else
|
---|
370 | SH_FREE(copy);
|
---|
371 | #endif
|
---|
372 | return -1;
|
---|
373 | }
|
---|
374 |
|
---|
375 | int create_sched (const char * ssched, sh_schedule_t * isched)
|
---|
376 | {
|
---|
377 | int j;
|
---|
378 |
|
---|
379 | if (!isched || !ssched)
|
---|
380 | return -1;
|
---|
381 |
|
---|
382 | j = parse_sched(ssched, isched);
|
---|
383 |
|
---|
384 | #ifdef TESTONLY
|
---|
385 | if (j == 0)
|
---|
386 | {
|
---|
387 | int i;
|
---|
388 | for (i = 0; i < 5; ++i)
|
---|
389 | printf("%2d MIN %3d MAX %3d STEP %3d\n",
|
---|
390 | i, isched->max[i], isched->min[i], isched->step[i]);
|
---|
391 | printf("MINSTEP %7d\n", isched->min_step);
|
---|
392 | printf("LASTEXEC %7ld\n", (long) isched->last_exec);
|
---|
393 | }
|
---|
394 | #endif
|
---|
395 |
|
---|
396 | return j;
|
---|
397 | }
|
---|
398 |
|
---|
399 | /* #ifdef SCHEDULER_YES */
|
---|
400 | #endif
|
---|
401 |
|
---|
402 | /**************************************************
|
---|
403 | *
|
---|
404 | * Schedule class - Test driver
|
---|
405 | *
|
---|
406 | **************************************************/
|
---|
407 | #ifdef TESTONLY
|
---|
408 |
|
---|
409 | int main(int argc, char * argv[])
|
---|
410 | {
|
---|
411 | sh_schedule_t isched;
|
---|
412 |
|
---|
413 | if (argc < 2)
|
---|
414 | {
|
---|
415 | fprintf(stderr, "Usage: %s 'schedule'\n", argv[0]);
|
---|
416 | exit (1);
|
---|
417 | }
|
---|
418 |
|
---|
419 | if (create_sched(argv[1], &isched) < 0)
|
---|
420 | {
|
---|
421 | fprintf(stderr, "Bad schedule <%s>\n", argv[1]);
|
---|
422 | exit (1);
|
---|
423 | }
|
---|
424 |
|
---|
425 | while (1 == 1)
|
---|
426 | {
|
---|
427 | if (test_sched(&isched))
|
---|
428 | printf("EXECUTE at: %s", ctime(&(isched.last_exec)));
|
---|
429 | sleep (1); /* TESTONLY */
|
---|
430 | }
|
---|
431 | return 0;
|
---|
432 | }
|
---|
433 | #endif
|
---|