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