source: branches/samhain-2_2-branch/src/sh_schedule.c@ 137

Last change on this file since 137 was 34, checked in by rainer, 19 years ago

Code cleanup and minor fixes

File size: 8.8 KB
Line 
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
79static const int sh_schedule_max[5] = { 59, 23, 31, 12, 7 };
80static const int sh_schedule_min[5] = { 0, 0, 0, 0, 0 };
81
82static
83int 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
126static
127int 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 */
164int 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
178static
179char DayNames[7][4] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
180static
181char MonNames[12][4] = { "jan", "feb", "mar", "apr", "may", "jun",
182 "jul", "aug", "sep", "oct", "nov", "dec" };
183
184static
185int 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
230static
231int 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
315static
316int 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 isched->last_exec = (time_t)-1;
350 isched->first = 0;
351 isched->next = NULL;
352
353#ifdef TESTONLY
354 free(copy);
355#else
356 SH_FREE(copy);
357#endif
358 return 0;
359
360 err:
361#ifdef TESTONLY
362 free(copy);
363#else
364 SH_FREE(copy);
365#endif
366 return -1;
367}
368
369int create_sched (const char * ssched, sh_schedule_t * isched)
370{
371 int j;
372
373 if (!isched || !ssched)
374 return -1;
375
376 j = parse_sched(ssched, isched);
377
378#ifdef TESTONLY
379 if (j == 0)
380 {
381 int i;
382 for (i = 0; i < 5; ++i)
383 printf("%2d MIN %3d MAX %3d STEP %3d\n",
384 i, isched->max[i], isched->min[i], isched->step[i]);
385 printf("MINSTEP %7d\n", isched->min_step);
386 printf("LASTEXEC %7ld\n", (long) isched->last_exec);
387 }
388#endif
389
390 return j;
391}
392
393/* #ifdef SCHEDULER_YES */
394#endif
395
396/**************************************************
397 *
398 * Schedule class - Test driver
399 *
400 **************************************************/
401#ifdef TESTONLY
402
403int main(int argc, char * argv[])
404{
405 sh_schedule_t isched;
406
407 if (argc < 2)
408 {
409 fprintf(stderr, "Usage: %s 'schedule'\n", argv[0]);
410 exit (1);
411 }
412
413 if (create_sched(argv[1], &isched) < 0)
414 {
415 fprintf(stderr, "Bad schedule <%s>\n", argv[1]);
416 exit (1);
417 }
418
419 while (1 == 1)
420 {
421 if (test_sched(&isched))
422 printf("EXECUTE at: %s", ctime(&(isched.last_exec)));
423 sleep (1); /* TESTONLY */
424 }
425 return 0;
426}
427#endif
Note: See TracBrowser for help on using the repository browser.