source: trunk/src/sh_schedule.c@ 167

Last change on this file since 167 was 131, checked in by rainer, 17 years ago

Use thread-safe libc functions.

File size: 9.6 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#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
133 struct tm time_tm;
134#endif
135
136 if (!isched)
137 return 0;
138
139 now = time(NULL);
140#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
141 tval = localtime_r(&now, &time_tm);
142#else
143 tval = localtime(&now);
144#endif
145 count = 0;
146 for (i = 0; i < 5; ++i)
147 {
148 if (i == 0) nval = tval->tm_min;
149 else if (i == 1) nval = tval->tm_hour;
150 else if (i == 2) nval = tval->tm_mday;
151 else if (i == 3) nval = tval->tm_mon;
152 else nval = tval->tm_wday;
153 count += test_val (i, isched->min[i], isched->max[i],
154 isched->min_step, &(isched->last_exec),
155 now, nval, isched->first);
156 }
157
158 if (count == 5)
159 {
160 isched->first = 1;
161 isched->last_exec = now;
162 return 1;
163 }
164
165 return 0;
166}
167
168/* test a linked list of schedules
169 */
170int test_sched (sh_schedule_t * isched)
171{
172 sh_schedule_t * intern = isched;
173 int retval = 0;
174
175 while (intern != NULL)
176 {
177 if (test_sched_int(intern) == 1)
178 retval = 1;
179 intern = intern->next;
180 }
181 return retval;
182}
183
184static
185char DayNames[7][4] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
186static
187char MonNames[12][4] = { "jan", "feb", "mar", "apr", "may", "jun",
188 "jul", "aug", "sep", "oct", "nov", "dec" };
189
190static
191int parse_func (int i, char * p)
192{
193 int j, k, l;
194 char *tail;
195
196 errno = 0;
197 j = (int) strtol(p, &tail, 10);
198
199 if (errno != 0) /* overflow */
200 return -1;
201 if (j < 0)
202 return -1;
203 if (tail != p) /* numeric */
204 return j;
205 if (i < 3) /* names not allowed */
206 return -1;
207
208 if (i == 3)
209 {
210 for (j = 0; j < 12; ++j) {
211 l = 0;
212 /*@+charint@*//* Incompatible types for == (char, char): ??? */
213 for (k = 0; k < 3; ++k)
214 if (p[k] != '\0' && tolower(p[k]) == MonNames[j][k]) ++l;
215 /*@-charint@*/
216 if (l == 3)
217 return j;
218 }
219 }
220 if (i == 4)
221 {
222 for (j = 0; j < 7; ++j) {
223 l = 0;
224 /*@+charint@*//* Incompatible types for == (char, char): ??? */
225 for (k = 0; k < 3; ++k)
226 if (p[k] != '\0' && tolower(p[k]) == DayNames[j][k]) ++l;
227 /*@-charint@*/
228 if (l == 3)
229 return j;
230 }
231 }
232
233 return -1;
234}
235
236static
237int parse_token(int i, sh_schedule_t * isched, char * p)
238{
239 char * q;
240
241 if ( NULL != (q = strchr(p, ',')))
242 return -1;
243
244 if (*p == '*')
245 {
246 isched->min[i] = sh_schedule_min[i];
247 isched->max[i] = sh_schedule_max[i];
248 }
249 else
250 {
251 isched->min[i] = parse_func(i, p);
252 if (i == 4 && isched->min[i] == 7)
253 isched->min[i] = 0;
254 if (isched->min[i] < sh_schedule_min[i] ||
255 isched->min[i] > sh_schedule_max[i])
256 {
257 return -1;
258 }
259 if ( NULL != (q = strchr(p, '-')))
260 {
261 ++q;
262 isched->max[i] = parse_func(i, q);
263 if (i == 4 && isched->max[i] == 7)
264 isched->max[i] = 0;
265 if (isched->max[i] < sh_schedule_min[i] ||
266 isched->max[i] > sh_schedule_max[i] ||
267 isched->max[i] < isched->min[i])
268 {
269 return -1;
270 }
271 }
272 else
273 isched->max[i] = isched->min[i];
274 }
275
276 if ( NULL != (q = strchr(p, '/')))
277 {
278 ++q;
279 isched->step[i] = atoi(q);
280 if (isched->step[i] < 1 || isched->step[i] > sh_schedule_max[i])
281 {
282 return -1;
283 }
284 if (i == 4 && isched->step[i] == 7)
285 isched->step[i] = 6;
286 }
287 else
288 {
289 isched->step[i] = 1;
290 }
291
292 switch (i)
293 {
294 case 0:
295 if (isched->max[i] == isched->min[i])
296 isched->min_step = 3599;
297 else
298 isched->min_step = (isched->step[i] * 60) - 1;
299 break;
300 case 1:
301 if (isched->max[i] == isched->min[i])
302 {
303 /* fix for daylight saving time: subtract 3600 sec
304 */
305 if (isched->min_step == 3599)
306 isched->min_step = 86399 - 3600;
307 }
308 else
309 {
310 if (isched->min_step == 3599)
311 isched->min_step = (isched->step[i] * 3600) - 1;
312 }
313 break;
314 default:
315 break;
316 }
317
318 return 0;
319}
320
321static
322int parse_sched (const char * ssched, sh_schedule_t * isched)
323{
324 char * p;
325 char * copy;
326 int i = 0;
327 size_t len;
328#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
329 char * saveptr;
330#endif
331
332 if (!ssched || !isched)
333 return -1;
334
335 len = strlen(ssched)+1;
336#ifdef TESTONLY
337 copy = malloc(len); /* testonly code */
338#else
339 copy = SH_ALLOC(len);
340#endif
341 sl_strlcpy(copy, ssched, len);
342
343#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
344 p = strtok_r(copy, " \t", &saveptr); /* parse crontab-style schedule */
345#else
346 p = strtok(copy, " \t"); /* parse crontab-style schedule */
347#endif
348
349 if (!p)
350 goto err;
351 if (parse_token(i, isched, p) == -1)
352 goto err;
353
354 for (i = 1; i < 5; ++i)
355 {
356#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
357 p = strtok_r(NULL, " \t", &saveptr); /* parse crontab-style schedule */
358#else
359 p = strtok(NULL, " \t"); /* parse crontab-style schedule */
360#endif
361 if (!p)
362 goto err;
363 if (parse_token(i, isched, p) == -1)
364 goto err;
365 }
366
367 isched->last_exec = (time_t)-1;
368 isched->first = 0;
369 isched->next = NULL;
370
371#ifdef TESTONLY
372 free(copy);
373#else
374 SH_FREE(copy);
375#endif
376 return 0;
377
378 err:
379#ifdef TESTONLY
380 free(copy);
381#else
382 SH_FREE(copy);
383#endif
384 return -1;
385}
386
387int create_sched (const char * ssched, sh_schedule_t * isched)
388{
389 int j;
390
391 if (!isched || !ssched)
392 return -1;
393
394 j = parse_sched(ssched, isched);
395
396#ifdef TESTONLY
397 if (j == 0)
398 {
399 int i;
400 for (i = 0; i < 5; ++i)
401 printf("%2d MIN %3d MAX %3d STEP %3d\n",
402 i, isched->max[i], isched->min[i], isched->step[i]);
403 printf("MINSTEP %7d\n", isched->min_step);
404 printf("LASTEXEC %7ld\n", (long) isched->last_exec);
405 }
406#endif
407
408 return j;
409}
410
411/* #ifdef SCHEDULER_YES */
412#endif
413
414/**************************************************
415 *
416 * Schedule class - Test driver
417 *
418 **************************************************/
419#ifdef TESTONLY
420
421int main(int argc, char * argv[])
422{
423 sh_schedule_t isched;
424
425 if (argc < 2)
426 {
427 fprintf(stderr, "Usage: %s 'schedule'\n", argv[0]);
428 exit (1);
429 }
430
431 if (create_sched(argv[1], &isched) < 0)
432 {
433 fprintf(stderr, "Bad schedule <%s>\n", argv[1]);
434 exit (1);
435 }
436
437 while (1 == 1)
438 {
439 if (test_sched(&isched))
440 printf("EXECUTE at: %s", ctime(&(isched.last_exec))); /* TESTONLY */
441 sleep (1); /* TESTONLY */
442 }
443 return 0;
444}
445#endif
Note: See TracBrowser for help on using the repository browser.