]> granicus.if.org Git - fcron/blob - conf.c
mark as dev release
[fcron] / conf.c
1
2 /*
3  * FCRON - periodic command scheduler 
4  *
5  *  Copyright 2000-2016 Thibault Godouet <fcron@free.fr>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  * 
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  * 
21  *  The GNU General Public License can also be found in the file
22  *  `LICENSE' that comes with the fcron source distribution.
23  */
24
25
26 #include "fcron.h"
27
28 #include "conf.h"
29 #include "database.h"
30
31 int read_file(const char *file_name, cf_t * cf, int is_system_startup);
32 int add_line_to_file(cl_t * cl, cf_t * cf, uid_t runas, char *runas_str,
33                      time_t t_save, int is_system_startup);
34 int read_strn(int fd, char **str, short int size);
35 int read_type(int fd, short int *type, short int *size);
36 void synchronize_file(char *file_name, int is_system_startup);
37
38
39 /* this is used to create a list of files to remove, to add */
40 typedef struct list_t {
41     char *str;
42     struct list_t *next;
43 } list_t;
44
45
46 void
47 reload_all(const char *dir_name)
48     /* save all current configuration, remove it from the memory,
49      * and reload from dir_name */
50 {
51     cf_t *f = NULL;
52
53     explain("Removing current configuration from memory");
54
55     f = file_base;
56     while (f != NULL) {
57         if (f->cf_running > 0)
58             wait_all(&f->cf_running);
59         save_file(f);
60         delete_file(f->cf_user);
61
62         /* delete_file remove the f file from the list :
63          * next file to remove is now pointed by file_base. */
64         f = file_base;
65     }
66
67     synchronize_dir(dir_name, 0);
68
69 }
70
71
72 void
73 synchronize_dir(const char *dir_name, int is_system_startup)
74     /* read dir_name and create three list of files to remove,
75      * new files and normal files. Then remove each file
76      * listed in the first list, then read normal files,
77      * finally add new files. */
78 {
79
80     list_t *rm_list = NULL;
81     list_t *new_list = NULL;
82     list_t *file_list = NULL;
83     list_t *list_cur = NULL;
84     DIR *dir;
85     struct dirent *den;
86
87     if (strcmp(dir_name, ".") == 0)
88         explain("updating configuration from %s", fcrontabs);
89     else
90         explain("updating configuration from %s", dir_name);
91
92     if ((dir = opendir("."))) {
93         while ((den = readdir(dir))) {
94
95             if (strncmp(den->d_name, "rm.", 3) == 0) {
96                 /* this is a file to remove from database */
97                 Alloc(list_cur, list_t);
98                 list_cur->str = strdup2(den->d_name);
99                 list_cur->next = rm_list;
100                 rm_list = list_cur;
101             }
102             else if (strncmp(den->d_name, "new.", 4) == 0) {
103                 /* this is a file to append to database */
104                 Alloc(list_cur, list_t);
105                 list_cur->str = strdup2(den->d_name);
106                 list_cur->next = new_list;
107                 new_list = list_cur;
108             }
109             else if (strchr(den->d_name, '.') != NULL)
110                 continue;
111             else
112                 /* this is a normal file : if file_base is not null,
113                  * so if a database has already been created, we
114                  * ignore it */
115             if (file_base == NULL) {
116                 Alloc(list_cur, list_t);
117                 list_cur->str = strdup2(den->d_name);
118                 list_cur->next = file_list;
119                 file_list = list_cur;
120             }
121
122         }
123         closedir(dir);
124     }
125     else
126         die("Unable to open current dir!");
127
128
129     /* proceed to adds or removes */
130
131     /* begin by removing files which are no longer wanted */
132     for (list_cur = rm_list; list_cur; list_cur = list_cur->next) {
133         explain("removing file %s", list_cur->str + 3);
134         delete_file(list_cur->str + 3); /* len("rm.") = 3 */
135         if (remove(list_cur->str + 3) != 0 && errno != ENOENT)
136             error_e("Could not remove %s", list_cur->str + 3);
137         if (remove(list_cur->str) != 0 && errno != ENOENT)
138             error_e("Could not remove %s", list_cur->str);
139     }
140
141     /* then add normal files, if any, to database */
142     for (list_cur = file_list; list_cur; list_cur = list_cur->next) {
143         errno = 0;
144         if (getpwnam(list_cur->str)
145 #ifdef SYSFCRONTAB
146             || strcmp(list_cur->str, SYSFCRONTAB) == 0
147 #endif
148             ) {
149             explain("adding file %s", list_cur->str);
150             synchronize_file(list_cur->str, is_system_startup);
151         }
152         else
153             error_e("ignoring file \"%s\" : not in passwd file.",
154                     list_cur->str);
155     }
156
157     /* finally add new files */
158     for (list_cur = new_list; list_cur; list_cur = list_cur->next) {
159         /* len("new.") = 4 : */
160         errno = 0;
161         if (getpwnam(list_cur->str + 4)
162 #ifdef SYSFCRONTAB
163             || strcmp(list_cur->str + 4, SYSFCRONTAB) == 0
164 #endif
165             ) {
166             explain("adding new file %s", list_cur->str + 4);
167             synchronize_file(list_cur->str, is_system_startup);
168         }
169         else
170             error_e("ignoring file %s : not in passwd file.",
171                     (list_cur->str + 4));
172     }
173
174     /* free lists */
175     {
176         list_t *l = NULL;
177         list_t *next = NULL;
178
179         next = rm_list;
180         while ((l = next) != NULL) {
181             next = l->next;
182             Free_safe(l->str);
183             Free_safe(l);
184         }
185
186         next = new_list;
187         while ((l = next) != NULL) {
188             next = l->next;
189             Free_safe(l->str);
190             Free_safe(l);
191         }
192
193         next = file_list;
194         while ((l = next) != NULL) {
195             next = l->next;
196             Free_safe(l->str);
197             Free_safe(l);
198         }
199
200     }
201
202 }
203
204
205 void
206 synchronize_file(char *file_name, int is_system_startup)
207 {
208
209     cf_t *cur_f = NULL;
210     char *user = NULL;
211
212     if (strchr(file_name, '.') != NULL) {
213         /* this is a new file : we have to check if there is an old
214          * version in database in order to keep a maximum of fields
215          * (cl_nextexe) to their current value */
216
217         cf_t *prev = NULL;
218
219         /* set user name  */
220         /* we add 4 to file_name pointer because of the "new."
221          * string at the beginning of a new file */
222         user = (file_name + 4);
223
224         for (cur_f = file_base; cur_f; cur_f = cur_f->cf_next) {
225             if (strcmp(user, cur_f->cf_user) == 0)
226                 break;
227             prev = cur_f;
228         }
229
230         if (cur_f != NULL) {
231             /* an old version of this file exist in database */
232
233             cf_t *old = NULL;
234             cl_t *old_l = NULL;
235             cl_t *new_l = NULL;
236             /* size used when comparing two line :
237              * it's the size of all time table (mins, days ...) */
238             const size_t size = (bitstr_size(60) + bitstr_size(24) +
239                                  bitstr_size(32) + bitstr_size(12) +
240                                  bitstr_size(7));
241
242             old = cur_f;
243
244             /* load new file */
245             Alloc(cur_f, cf_t);
246             if (read_file(file_name, cur_f, is_system_startup) != 0) {
247                 /* an error as occured */
248                 return;
249             }
250
251             /* assign old pointer to the old file, and move it to the first
252              * place of the list : delete_file() only remove the first
253              * occurrence of the file which has the name given in argument */
254             if (prev != NULL) {
255                 prev->cf_next = old->cf_next;
256                 old->cf_next = file_base;
257                 file_base = old;
258             }
259             else
260                 /* this is the first file in the list : no need to move it */
261                 ;
262
263             /* compare each lines between the new and the old
264              * version of the file */
265             for (new_l = cur_f->cf_line_base; new_l; new_l = new_l->cl_next)
266                 for (old_l = old->cf_line_base; old_l; old_l = old_l->cl_next) {
267
268                     /* compare the shell command and the fields
269                      * from cl_mins down to cl_runfreq or the timefreq */
270                     if (strcmp(new_l->cl_shell, old_l->cl_shell) == 0
271                         && ((is_freq(new_l->cl_option)
272                              && new_l->cl_timefreq == old_l->cl_timefreq)
273                             || (is_td(new_l->cl_option)
274                                 && memcmp(&(new_l->cl_mins), &(old_l->cl_mins),
275                                           size) == 0
276                                 && is_dayor(new_l->cl_option) ==
277                                 is_dayor(old_l->cl_option))
278                         )) {
279
280                         if (new_l->cl_runfreq == old_l->cl_runfreq)
281                             new_l->cl_remain = old_l->cl_remain;
282                         /* check if there is a change about the tz diff */
283                         if ((new_l->cl_file->cf_tzdiff !=
284                              old_l->cl_file->cf_tzdiff) &&
285                             (old_l->cl_nextexe - old_l->cl_file->cf_tzdiff
286                              + new_l->cl_file->cf_tzdiff > now))
287                             new_l->cl_nextexe = old_l->cl_nextexe
288                                 - old_l->cl_file->cf_tzdiff +
289                                 new_l->cl_file->cf_tzdiff;
290                         else
291                             new_l->cl_nextexe = old_l->cl_nextexe;
292
293                         if (is_runonce(new_l->cl_option)
294                             && is_runonce(old_l->cl_option)
295                             && is_hasrun(old_l->cl_option)) {
296                             explain
297                                 ("  from last conf: job '%s' with runonce set has "
298                                  "already run since last system startup: not "
299                                  "re-scheduling.", new_l->cl_shell);
300                             set_hasrun(new_l->cl_option);
301                             /* job has already run: remove from the queue */
302                             job_queue_remove(new_l);
303                         }
304                         else
305                             /* update the position in the queue */
306                             insert_nextexe(new_l);
307
308                         if (debug_opt && !is_hasrun(new_l->cl_option)) {
309                             struct tm *ftime;
310                             ftime = localtime(&new_l->cl_nextexe);
311                             debug
312                                 ("  from last conf: '%s' next exec %04d-%02d-%02d"
313                                  " wday:%d %02d:%02d:%02d (system time)",
314                                  new_l->cl_shell, (ftime->tm_year + 1900),
315                                  (ftime->tm_mon + 1), ftime->tm_mday,
316                                  ftime->tm_wday, ftime->tm_hour, ftime->tm_min,
317                                  ftime->tm_sec);
318                         }
319
320                         break;
321
322                     }
323                 }
324
325             /* remove old file from the list */
326             delete_file(user);
327
328             /* insert new file in the list */
329             cur_f->cf_next = file_base;
330             file_base = cur_f;
331
332             /* save final file */
333             save_file(cur_f);
334
335             /* delete new.user file */
336             if (remove(file_name) != 0)
337                 error_e("could not remove %s", file_name);
338
339         }
340
341         else {
342             /* no old version exist in database : load this file
343              * as a normal file, but change its name */
344
345             Alloc(cur_f, cf_t);
346
347             if (read_file(file_name, cur_f, is_system_startup) != 0) {
348                 /* an error as occured */
349                 return;
350             }
351
352             /* insert the file in the list */
353             cur_f->cf_next = file_base;
354             file_base = cur_f;
355
356             /* save as a normal file */
357             save_file(cur_f);
358
359             /* delete new.user file */
360             if (remove(file_name) != 0)
361                 error_e("could not remove %s", file_name);
362         }
363
364     }
365
366     else {
367         /* this is a normal file */
368
369         Alloc(cur_f, cf_t);
370
371         if (read_file(file_name, cur_f, is_system_startup) != 0) {
372             /* an error as occured */
373             return;
374         }
375
376         /* insert the file in the list */
377         cur_f->cf_next = file_base;
378         file_base = cur_f;
379
380     }
381
382 }
383
384
385 int
386 read_strn(int fd, char **str, short int size)
387 /* read a "size"-length string in a binary fcrontab file */
388 {
389     if ((*str = calloc(size + 1, sizeof(char))) == NULL)
390         goto err;
391
392     if (read(fd, *str, size) < size)
393         goto err;
394     (*str)[size] = '\0';
395     return OK;
396
397  err:
398     Free_safe(*str);
399     return ERR;
400
401 }
402
403 int
404 read_type(int fd, short int *type, short int *size)
405 /* read the type and size of the next field in a binary fcrontab file */
406 {
407     if (read(fd, type, sizeof(short int)) < sizeof(short int))
408         goto err;
409     if (read(fd, size, sizeof(short int)) < sizeof(short int))
410         goto err;
411
412     return OK;
413
414  err:
415     return ERR;
416
417 }
418
419
420 /* macros for read_file() */
421 /* read "size" bytes from file "ff", put them in "to", and check for errors */
422 #define Read(TO, SIZE, ERR_STR) \
423         { \
424           if ( read(fileno(ff), &(TO), SIZE) < SIZE ) { \
425             error_e(ERR_STR); \
426             goto err; \
427           } \
428         }
429
430 #define Read_strn(TO, SIZE, ERR_STR) \
431         { \
432           if ( read_strn(fileno(ff), &(TO), SIZE) != OK ) { \
433             error_e(ERR_STR); \
434             goto err; \
435           } \
436         }
437
438 int
439 read_file(const char *file_name, cf_t * cf, int is_system_startup)
440     /* read a formated fcrontab.
441      * return ERR on error, OK otherwise */
442 {
443     FILE *ff = NULL;
444     cl_t *cl = NULL;
445     long int bufi = 0;
446     time_t t_save = 0;
447     uid_t runas = 0;
448     char *runas_str = NULL;
449     struct stat file_stat;
450     struct passwd *pass = NULL;
451     short int type = 0, size = 0;
452     int rc;
453     int has_read_cl_first = 0;  /* have we read S_FIRST_T? */
454 #ifdef WITH_SELINUX
455     int flask_enabled = is_selinux_enabled();
456     int retval;
457     struct av_decision avd;
458     char *user_name = NULL;
459 #endif
460
461     /* open file */
462     if ((ff = fopen(file_name, "r")) == NULL) {
463         warn_e("Could not read %s (may have just been removed)", file_name);
464         goto err;
465     }
466
467     /* check if this file is owned by root : otherwise, all runas fields
468      * of this file should be set to the owner */
469     rc = fstat(fileno(ff), &file_stat);
470     if (rc != 0) {
471         error_e("Could not stat %s", file_name);
472         goto err;
473     }
474 #ifdef WITH_SELINUX
475     if (flask_enabled && fgetfilecon(fileno(ff), &cf->cf_file_context) < 0) {
476         error_e("Could not get context of %s", file_name);
477         goto err;
478     }
479 #endif
480
481     if (strncmp(file_name, "new.", 4) == 0) {
482         if (file_stat.st_uid == rootuid) {
483             /* file is owned by root : no test needed : set runas to rootuid */
484             runas = rootuid;
485         }
486         else {
487             /* this is a standard user's new fcrontab : set the runas field to
488              * the owner of the file */
489             runas = file_stat.st_uid;
490             if ((pass = getpwuid(file_stat.st_uid)) == NULL) {
491                 error_e("Could not getpwuid(%d)", file_stat.st_uid);
492                 goto err;
493             }
494             runas_str = strdup2(pass->pw_name);
495         }
496         cf->cf_user = strdup2(file_name + 4);
497     }
498     else {
499         if (!cf->cf_user)
500             cf->cf_user = strdup2(file_name);
501         if (file_stat.st_uid == rootuid) {
502             /* file is owned by root : either this file has already been parsed
503              * at least once by fcron, or it is root's fcrontab */
504             runas = rootuid;
505         }
506         else {
507             error("Non-new file %s owned by someone else than root", file_name);
508             goto err;
509         }
510     }
511
512 #ifdef WITH_SELINUX
513     /*
514      * Since fcrontab files are not directly executed,
515      * fcron daemon must ensure that the fcrontab file has
516      * a context that is appropriate for the context of
517      * the user fcron job.  It performs an entrypoint
518      * permission check for this purpose.
519      */
520     if (flask_enabled) {
521         char *sename = NULL;
522         char *selevl = NULL;
523
524         /* first, get the SELinux user for that Linux user */
525 #ifdef SYSFCRONTAB
526         if (!strcmp(cf->cf_user, SYSFCRONTAB))
527             /* system_u is the default SELinux user for running system services */
528             user_name = "system_u";
529         else
530 #endif                          /* def SYSFCRONTAB */
531         {
532             if (getseuserbyname(cf->cf_user, &sename, &selevl) < 0) {
533                 error_e("Cannot find SELinux user for user \"%s\"\n",
534                         cf->cf_user);
535                 goto err;
536             }
537             user_name = sename;
538         }
539
540         if (get_default_context(user_name, NULL, &cf->cf_user_context))
541             error_e("NO CONTEXT for Linux user '%s' (SELinux user '%s')",
542                     cf->cf_user, user_name);
543         retval =
544             security_compute_av(cf->cf_user_context, cf->cf_file_context,
545                                 SECCLASS_FILE, FILE__ENTRYPOINT, &avd);
546
547         if (retval || ((FILE__ENTRYPOINT & avd.allowed) != FILE__ENTRYPOINT)) {
548             syslog(LOG_ERR, "ENTRYPOINT FAILED for Linux user '%s' "
549                    "(CONTEXT %s) for file CONTEXT %s", cf->cf_user,
550                    cf->cf_user_context, cf->cf_file_context);
551             goto err;
552         }
553
554         Free_safe(sename);
555         Free_safe(selevl);
556     }
557 #endif
558
559     debug("User %s Entry", file_name);
560
561     /* get version of fcrontab file: it permits to daemon not to load
562      * a file which he won't understand the syntax, for example
563      * a file using a depreciated format generated by an old fcrontab,
564      * if the syntax has changed */
565     if (read_type(fileno(ff), &type, &size) != OK || type != S_HEADER_T ||
566         read(fileno(ff), &bufi, size) < size || bufi != S_FILEVERSION) {
567         error("File %s is not valid: ignored.", file_name);
568         error("This file may have been generated by an old version of fcron.");
569         error("In that case, you should try to use the converter given in the "
570               "source package, or install it again using fcrontab.");
571         goto err;
572     }
573
574     if (read_type(fileno(ff), &type, &size) != OK || type != S_USER_T) {
575         error("Invalid binary fcrontab (no USER field)");
576         goto err;
577     }
578     /* get the owner's name */
579     /* we set cf->cf_user before for SE Linux, so we need to free it here */
580     Free_safe(cf->cf_user);
581     if (read_strn(fileno(ff), &cf->cf_user, size) != OK) {
582         error("Cannot read user's name : file ignored");
583         goto err;
584     }
585     if (runas != rootuid) {
586         /* we use file owner's name for more security (see above) */
587         /* free the value obtained by read_strn() (we need to read it anyway
588          * to set the file ptr to the next thing to read) */
589         Free_safe(cf->cf_user);
590         cf->cf_user = runas_str;
591     }
592
593     /* get the time & date of the saving */
594     /* a new file generated by fcrontab has t_save set to 0 */
595     if (read_type(fileno(ff), &type, &size) != OK || type != S_TIMEDATE_T
596         || read(fileno(ff), &t_save, size) < size) {
597         error("could not get time and date of saving");
598         goto err;
599     }
600     else {
601         debug("file saved at %lu.", t_save);
602     }
603
604     if (cf->cf_env_list == NULL)
605         cf->cf_env_list = env_list_init();
606
607     Alloc(cl, cl_t);
608     /* main loop : read env variables, and lines */
609     while (read_type(fileno(ff), &type, &size) == OK) {
610         /* action is determined by the type of the field */
611         switch (type) {
612
613         case S_ENVVAR_T:
614             /* read a env variable and add it to the env var list */
615             {
616                 char *envvar = NULL;
617
618                 /* Read_strn go to "err" on error */
619                 Read_strn(envvar, size, "Error while reading env var");
620                 debug("  Env: \"%s\"", envvar);
621                 /* we do not allow USER or LOGNAME assignment.
622                  * this was already checked by fcrontab, but we check again
623                  * just in case... */
624                 if (strcmp_until(envvar, "USER", '=') == 0
625                     || strcmp_until(envvar, "LOGNAME", '=') == 0) {
626                     error
627                         ("USER or LOGNAME assignement is not allowed: ignored.");
628                 }
629                 else {
630                     env_list_putenv(cf->cf_env_list, envvar, 1);
631                 }
632                 Free_safe(envvar);
633             }
634             break;
635
636         case S_TZDIFF_T:
637             /* time diff between local (real) and system hour */
638             Read(bufi, size, "Error while reading tzdiff field");
639             cf->cf_tzdiff = (signed char)bufi;
640             break;
641
642         case S_TZ_T:
643             /* read the timezone (string) in which the line should run */
644             Read_strn(cl->cl_tz, size, "Error while reading timezone field");
645             break;
646
647         case S_SHELL_T:
648             Read_strn(cl->cl_shell, size, "Error while reading shell field");
649             break;
650
651         case S_RUNAS_T:
652             Read_strn(cl->cl_runas, size, "Error while reading runas field");
653             break;
654
655         case S_MAILFROM_T:
656             Read_strn(cl->cl_mailfrom, size,
657                       "Error while reading mailfrom field");
658             break;
659
660         case S_MAILTO_T:
661             Read_strn(cl->cl_mailto, size, "Error while reading mailto field");
662             break;
663
664         case S_NEXTEXE_T:
665             Read(bufi, size, "Error while reading nextexe field");
666             cl->cl_nextexe = (time_t) bufi;
667             break;
668
669         case S_FIRST_T:
670             Read(bufi, size, "Error while reading first field");
671             cl->cl_first = (time_t) bufi;
672             has_read_cl_first = 1;
673             break;
674
675         case S_OPTION_T:
676             if (size < OPTION_SIZE)
677                 /* set the options not defined in the savefile to default */
678                 set_default_opt(cl->cl_option);
679             Read(cl->cl_option, size, "Error while reading option field");
680             break;
681
682         case S_NUMEXE_T:
683             Read(cl->cl_numexe, size, "Error while reading numexe field");
684             break;
685
686         case S_LAVG_T:
687             Read(cl->cl_lavg, size, "Error while reading lavg field");
688             break;
689
690         case S_UNTIL_T:
691             Read(bufi, size, "Error while reading until field");
692             cl->cl_until = (time_t) bufi;
693             break;
694
695         case S_NICE_T:
696             Read(cl->cl_nice, size, "Error while reading nice field");
697             break;
698
699         case S_RUNFREQ_T:
700             Read(bufi, size, "Error while reading runfreq field");
701             cl->cl_runfreq = (unsigned short)bufi;
702             break;
703
704         case S_REMAIN_T:
705             Read(bufi, size, "Error while reading remain field");
706             cl->cl_remain = (unsigned short)bufi;
707             break;
708
709         case S_TIMEFREQ_T:
710             Read(bufi, size, "Error while reading timefreq field");
711             cl->cl_timefreq = (time_t) bufi;
712             break;
713
714         case S_JITTER_T:
715             /* read the jitter (uchar) to use to set next execution time */
716             Read(bufi, size, "Error while reading jitter field");
717             cl->cl_jitter = (unsigned char)bufi;
718             break;
719
720         case S_MINS_T:
721             Read(cl->cl_mins, size, "Error while reading mins field");
722             break;
723
724         case S_HRS_T:
725             Read(cl->cl_hrs, size, "Error while reading hrs field");
726             break;
727
728         case S_DAYS_T:
729             Read(cl->cl_days, size, "Error while reading days field");
730             break;
731
732         case S_MONS_T:
733             Read(cl->cl_mons, size, "Error while reading mons field");
734             break;
735
736         case S_DOW_T:
737             Read(cl->cl_dow, size, "Error while reading dow field");
738             break;
739
740         case S_ENDLINE_T:
741             if (is_freq(cl->cl_option) && !has_read_cl_first) {
742                 /* Up to fcron 3.0.X, cl_first/S_FIRST_T was not saved for all @-lines */
743                 cl->cl_first = cl->cl_nextexe;
744             }
745             if (add_line_to_file
746                 (cl, cf, runas, runas_str, t_save, is_system_startup) != 0)
747                 free_line(cl);
748             Alloc(cl, cl_t);
749             break;
750
751             /* default case in "switch(type)" */
752         default:
753             error("Error while loading %s : unknown field type %d (ignored)",
754                   file_name, type);
755             free_line(cl);
756             Alloc(cl, cl_t);
757             /* skip the data corresponding to the unknown field */
758             {
759                 /* we avoid using fseek(), as it seems not to work correctly
760                  * on some systems when we use read() on the FILE stream */
761                 int i;
762                 for (i = 0; i < size; i++)
763                     if (getc(ff) == EOF)
764                         goto err;
765             }
766         }
767     }
768
769     /* free last cl Alloc : unused */
770     Free_safe(cl);
771
772     /* check for an error */
773     if (ferror(ff) != 0)
774         error("file %s is truncated : you should reinstall it with fcrontab",
775               file_name);
776
777     xfclose_check(&ff, file_name);
778
779     return OK;
780
781  err:
782     if (ff != NULL)
783         xfclose_check(&ff, file_name);
784
785     if (cl != NULL && cl->cl_next == NULL) {
786         /* line is not yet in the line list of the file : free it */
787         Free_safe(cl->cl_shell);
788         Free_safe(cl->cl_runas);
789         Free_safe(cl->cl_mailfrom);
790         Free_safe(cl->cl_mailto);
791         Free_safe(cl);
792     }
793
794     /* check if we have started to read the lines and env var */
795     if (cl != NULL) {
796         /* insert the line in the file list in order to be able to free
797          * the memory using delete_file() */
798
799         cf->cf_next = file_base;
800         file_base = cf;
801
802         delete_file(cf->cf_user);
803
804     }
805     else {
806         Free_safe(cf->cf_user);
807     }
808
809     return ERR;
810
811 }
812
813
814 int
815 add_line_to_file(cl_t * cl, cf_t * cf, uid_t runas, char *runas_str,
816                  time_t t_save, int is_system_startup)
817     /* check if the line is valid, and if yes, add it to the file cf */
818 {
819     time_t sleep_duration = now - t_save;
820
821     if (cl->cl_shell == NULL || cl->cl_runas == NULL || cl->cl_mailto == NULL) {
822         error("Line is not valid (empty shell, runas or mailto field)"
823               " : ignored");
824         return 1;
825     }
826
827     /* set runas field if necessary (to improve security) */
828     if (runas != rootuid) {
829         if (strcmp(cl->cl_runas, runas_str) != 0)
830             warn("warning: runas(%s) is not owner (%s): overridden.",
831                  cl->cl_runas, runas_str);
832         Set(cl->cl_runas, runas_str);
833     }
834
835     /* we need that here because the user's name contained in the
836      * struct cf_t may be required */
837     cl->cl_file = cf;
838
839     /* check if the mailfrom and mailto fields are safe */
840     if (cl->cl_mailfrom && (*(cl->cl_mailfrom) == '-' ||
841                             strcspn(cl->cl_mailfrom,
842                                     " \t\n;|") != strlen(cl->cl_mailfrom))) {
843         error
844             ("mailfrom field \'%s\' is not safe : unsetting to use default value.",
845              cl->cl_mailfrom);
846         Free_safe(cl->cl_mailfrom);
847     }
848     if (cl->cl_mailto && (*(cl->cl_mailto) == '-' ||
849                           strcspn(cl->cl_mailto,
850                                   " \t\n;|") != strlen(cl->cl_mailto))) {
851         error("mailto field \'%s\' is not safe : setting to owner %s.",
852               cl->cl_mailto, cl->cl_file->cf_user);
853         Set(cl->cl_mailto, cl->cl_file->cf_user);
854     }
855
856     /* make sure the timefreq is valid on @-lines or we could end up with
857      * infinite loops */
858     if (!is_td(cl->cl_option) && cl->cl_timefreq < 1) {
859         error("Invalid timefreq %ld for job '%s': setting to 1 day",
860               cl->cl_timefreq, cl->cl_shell);
861         cl->cl_timefreq = 3600 * 24;
862     }
863
864     set_next_exe_startup(cl, is_system_startup ? CONTEXT_BOOT : CONTEXT_DEFAULT,
865                          sleep_duration);
866
867     /* add the current line to the list, and allocate a new line */
868     if ((cl->cl_id = next_id++) >= ULONG_MAX - 1) {
869         warn("Line id reached %ld: cycling back to zero.", ULONG_MAX);
870         next_id = 0;
871     }
872     cl->cl_next = cf->cf_line_base;
873     cf->cf_line_base = cl;
874     return 0;
875 }
876
877 void
878 delete_file(const char *user_name)
879     /* free a file if user_name is not null 
880      *   otherwise free all files */
881 {
882     cf_t *file;
883     cf_t *prev_file = NULL;
884     cl_t *line;
885     cl_t *cur_line;
886     struct job_t *j = NULL;
887     struct job_t *prev_j;
888     int i, k;
889     struct cl_t **s_a = NULL;
890     exe_t *e = NULL;
891     lavg_t *l = NULL;
892
893     file = file_base;
894     while (file != NULL) {
895         if (strcmp(user_name, file->cf_user) != 0) {
896             prev_file = file;
897             file = file->cf_next;
898             continue;
899         }
900
901         for (e = exe_list_first(exe_list); e != NULL;
902              e = exe_list_next(exe_list))
903             if (e->e_line != NULL && e->e_line->cl_file == file) {
904                 /* we set the e_line to NULL, as so we know in wait_chld()
905                  * and wait_all() the corresponding file has been removed.
906                  * Plus, we decrement serial_running and lavg_serial_running
907                  * as we won't be able to do it at the end of the job */
908                 if ((is_serial(e->e_line->cl_option) ||
909                      is_serial_once(e->e_line->cl_option)) &&
910                     !is_lavg(e->e_line->cl_option))
911                     serial_running--;
912                 else if (is_serial(e->e_line->cl_option) &&
913                          is_lavg(e->e_line->cl_option))
914                     lavg_serial_running--;
915                 e->e_line = NULL;
916             }
917
918         /* free lavg queue entries */
919         for (l = lavg_list_first(lavg_list); l != NULL;
920              l = lavg_list_next(lavg_list))
921             if (l->l_line->cl_file == file) {
922                 debug("removing '%s' from lavg queue", l->l_line->cl_shell);
923                 lavg_list_remove_cur(lavg_list);
924             }
925
926         /* free serial queue entries */
927         for (i = 0; i < serial_array_size; i++)
928             if (serial_array[i] != NULL && serial_array[i]->cl_file == file) {
929                 if (!s_a) {
930                     s_a =
931                         alloc_safe(serial_array_size * sizeof(cl_t *),
932                                    "serial queue");
933                 }
934                 debug("removing '%s' from serial queue",
935                       serial_array[i]->cl_shell);
936                 serial_num--;
937                 serial_array[i]->cl_numexe--;
938                 serial_array[i] = NULL;
939             }
940         /* remove from queue and move the rest of the jobs to get
941          * a queue in order without empty entries */
942         if (!s_a)
943             goto end_of_serial_recomputing;
944
945         if ((k = serial_array_index + serial_num) >= serial_array_size)
946             k -= serial_array_size;
947         for (i = k = 0; i < serial_array_size; i++) {
948             if (serial_array_index + i < serial_array_size) {
949                 if ((s_a[k] = serial_array[serial_array_index + i]) != NULL)
950                     k++;
951             }
952             else if ((s_a[k] =
953                       serial_array[serial_array_index + i - serial_array_size])
954                      != NULL)
955                 k++;
956         }
957         Free_safe(serial_array);
958         serial_array = s_a;
959         serial_array_index = 0;
960
961  end_of_serial_recomputing:
962
963         /* free lines */
964         cur_line = file->cf_line_base;
965         while ((line = cur_line) != NULL) {
966             cur_line = line->cl_next;
967
968             /* remove from the main queue */
969             prev_j = NULL;
970             for (j = queue_base; j != NULL; j = j->j_next)
971                 if (j->j_line == line) {
972                     if (prev_j != NULL)
973                         prev_j->j_next = j->j_next;
974                     else
975                         queue_base = j->j_next;
976                     Free_safe(j);
977                     break;
978                 }
979                 else
980                     prev_j = j;
981
982             /* free line itself */
983             free_line(line);
984         }
985         /* delete_file() MUST remove only the first occurrence :
986          * this is needed by synchronize_file() */
987         break;
988     }
989
990     if (file == NULL)
991         /* file not in the file list */
992         return;
993
994     /* remove file from file list */
995     if (prev_file == NULL)
996         file_base = file->cf_next;
997     else
998         prev_file->cf_next = file->cf_next;
999
1000     /* free env variables */
1001     env_list_destroy(file->cf_env_list);
1002
1003     /* finally free file itself */
1004     Free_safe(file->cf_user);
1005     Free_safe(file);
1006
1007 }
1008
1009
1010 void
1011 save_file(cf_t * arg_file)
1012 /* Store the informations relatives to the executions
1013  * of tasks at a defined frequency of system's running time */
1014 {
1015     cf_t *file = NULL;
1016     cf_t *start_file = NULL;
1017
1018     if (arg_file != NULL)
1019         start_file = arg_file;
1020     else
1021         start_file = file_base;
1022
1023
1024     for (file = start_file; file; file = file->cf_next) {
1025
1026         debug("Saving %s...", file->cf_user);
1027
1028         /* save the file safely : save it to a temporary name, then rename() it */
1029         /* chown the file to root:root : this file should only be read and
1030          * modified by fcron (not fcrontab) */
1031         save_file_safe(file, file->cf_user, "fcron", rootuid, rootgid, now);
1032
1033         if (arg_file != NULL)
1034             /* we have to save only a single file */
1035             break;
1036     }
1037 }