forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron-lib.pl
executable file
·1758 lines (1565 loc) · 45.4 KB
/
cron-lib.pl
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
=head1 cron-lib.pl
Functions for listing, creating and managing Unix users' cron jobs.
foreign_require("cron", "cron-lib.pl");
@jobs = cron::list_cron_jobs();
$job = { 'user' => 'root',
'active' => 1,
'command' => 'ls -l >/dev/null',
'special' => 'hourly' };
cron::create_cron_job($job);
=cut
BEGIN { push(@INC, ".."); };
use WebminCore;
&init_config();
%access = &get_module_acl();
$env_support = $config{'vixie_cron'};
if ($module_info{'usermin'}) {
$single_user = $remote_user;
&switch_to_remote_user();
&create_user_config_dirs();
$range_cmd = "$user_module_config_directory/range.pl";
$hourly_only = 0;
}
else {
$range_cmd = "$module_config_directory/range.pl";
$hourly_only = $access{'hourly'} == 0 ? 0 :
$access{'hourly'} == 1 ? 1 :
$config{'hourly_only'};
}
$temp_delete_cmd = "$module_config_directory/tempdelete.pl";
$cron_temp_file = &transname();
use Time::Local;
=head2 list_cron_jobs
Returns a lists of structures of all cron jobs, each of which is a hash
reference with the following keys :
=item user - Unix user the job runs as.
=item command - The full command to be run.
=item active - Set to 0 if the job is commented out, 1 if active.
=item mins - Minute or comma-separated list of minutes the job will run, or * for all.
=item hours - Hour or comma-separated list of hours the job will run, or * for all.
=item days - Day or comma-separated list of days of the month the job will run, or * for all.
=item month - Month number or comma-separated list of months (started from 1) the job will run, or * for all.
=item weekday - Day of the week or comma-separated list of days (where 0 is sunday) the job will run, or * for all
=cut
sub list_cron_jobs
{
local (@rv, $lnum, $f);
if (scalar(@cron_jobs_cache)) {
return @cron_jobs_cache;
}
# read the master crontab file
if ($config{'system_crontab'}) {
$lnum = 0;
&open_readfile(TAB, $config{'system_crontab'});
while(<TAB>) {
# Comment line in Fedora 13
next if (/^#+\s+\*\s+\*\s+\*\s+\*\s+\*\s+(user-name\s+)?command\s+to\s+be\s+executed/);
if (/^(#+)?[\s\&]*(-)?\s*([0-9\-\*\/,]+)\s+([0-9\-\*\/,]+)\s+([0-9\-\*\/,]+)\s+(([0-9\-\*\/]+|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|,)+)\s+(([0-9\-\*\/]+|sun|mon|tue|wed|thu|fri|sat|,)+)\s+(\S+)\s+(.*)/i) {
# A normal h m s d w time
push(@rv, { 'file' => $config{'system_crontab'},
'line' => $lnum,
'type' => 1,
'nolog' => $2,
'active' => !$1,
'mins' => $3, 'hours' => $4,
'days' => $5, 'months' => $6,
'weekdays' => $8, 'user' => $10,
'command' => $11,
'index' => scalar(@rv) });
if ($rv[$#rv]->{'user'} =~ /^\//) {
# missing the user, as in redhat 7 !
$rv[$#rv]->{'command'} = $rv[$#rv]->{'user'}.
' '.$rv[$#rv]->{'command'};
$rv[$#rv]->{'user'} = 'root';
}
&fix_names($rv[$#rv]);
}
elsif (/^(#+)?\s*@([a-z]+)\s+(\S+)\s+(.*)/i) {
# An @ time
push(@rv, { 'file' => $config{'system_crontab'},
'line' => $lnum,
'type' => 1,
'active' => !$1,
'special' => $2,
'user' => $3,
'command' => $4,
'index' => scalar(@rv) });
}
$lnum++;
}
close(TAB);
}
# read package-specific cron files
opendir(DIR, &translate_filename($config{'cronfiles_dir'}));
my @files = sort { $a cmp $b } readdir(DIR);
closedir(DIR);
foreach my $f (@files) {
next if ($f =~ /^\./);
$lnum = 0;
&open_readfile(TAB, "$config{'cronfiles_dir'}/$f");
while(<TAB>) {
if (/^(#+)?[\s\&]*(-)?\s*([0-9\-\*\/,]+)\s+([0-9\-\*\/,]+)\s+([0-9\-\*\/,]+)\s+(([0-9\-\*\/]+|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|,)+)\s+(([0-9\-\*\/]+|sun|mon|tue|wed|thu|fri|sat|,)+)\s+(\S+)\s+(.*)/i) {
push(@rv, { 'file' => "$config{'cronfiles_dir'}/$f",
'line' => $lnum,
'type' => 2,
'active' => !$1,
'nolog' => $2,
'mins' => $3, 'hours' => $4,
'days' => $5, 'months' => $6,
'weekdays' => $8, 'user' => $10,
'command' => $11,
'index' => scalar(@rv) });
&fix_names($rv[$#rv]);
}
elsif (/^(#+)?\s*@([a-z]+)\s+(\S+)\s+(.*)/i) {
push(@rv, { 'file' => "$config{'cronfiles_dir'}/$f",
'line' => $lnum,
'type' => 2,
'active' => !$1,
'special' => $2,
'user' => $3,
'command' => $4,
'index' => scalar(@rv) });
}
$lnum++;
}
close(TAB);
}
# Read a single user's crontab file
if ($config{'single_file'}) {
&open_readfile(TAB, $config{'single_file'});
$lnum = 0;
while(<TAB>) {
if (/^(#+)?[\s\&]*(-)?\s*([0-9\-\*\/,]+)\s+([0-9\-\*\/,]+)\s+([0-9\-\*\/,]+)\s+(([0-9\-\*\/]+|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|,)+)\s+(([0-9\-\*\/]+|sun|mon|tue|wed|thu|fri|sat|,)+)\s+(.*)/i) {
# A normal m h d m wd time
push(@rv, { 'file' => $config{'single_file'},
'line' => $lnum,
'type' => 3,
'active' => !$1, 'nolog' => $2,
'mins' => $3, 'hours' => $4,
'days' => $5, 'months' => $6,
'weekdays' => $8, 'user' => "NONE",
'command' => $10,
'index' => scalar(@rv) });
&fix_names($rv[$#rv]);
}
elsif (/^(#+)?\s*([a-zA-Z0-9\_]+)\s*=\s*'([^']*)'/ ||
/^(#+)?\s*([a-zA-Z0-9\_]+)\s*=\s*"([^']*)"/ ||
/^(#+)?\s*([a-zA-Z0-9\_]+)\s*=\s*(\S+)/) {
# An environment variable
push(@rv, { 'file' => $config{'single_file'},
'line' => $lnum,
'active' => !$1,
'name' => $2,
'value' => $3,
'user' => "NONE",
'command' => '',
'index' => scalar(@rv) });
}
$lnum++;
}
close(TAB);
}
# read per-user cron files
local $fcron = ($config{'cron_dir'} =~ /\/fcron$/);
local @users;
if ($single_user) {
@users = ( $single_user );
}
else {
opendir(DIR, &translate_filename($config{'cron_dir'}));
@users = grep { !/^\./ } readdir(DIR);
closedir(DIR);
}
foreach $f (@users) {
next if (!(@uinfo = getpwnam($f)));
$lnum = 0;
if ($single_user) {
&open_execute_command(TAB, $config{'cron_user_get_command'}, 1);
}
elsif ($fcron) {
&open_execute_command(TAB,
&user_sub($config{'cron_get_command'}, $f), 1);
}
else {
&open_readfile(TAB, "$config{'cron_dir'}/$f");
}
while(<TAB>) {
if (/^(#+)?[\s\&]*(-)?\s*([0-9\-\*\/,]+)\s+([0-9\-\*\/,]+)\s+([0-9\-\*\/,]+)\s+(([0-9\-\*\/]+|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|,)+)\s+(([0-9\-\*\/]+|sun|mon|tue|wed|thu|fri|sat|,)+)\s+(.*)/i) {
# A normal m h d m wd time
push(@rv, { 'file' => "$config{'cron_dir'}/$f",
'line' => $lnum,
'type' => 0,
'active' => !$1, 'nolog' => $2,
'mins' => $3, 'hours' => $4,
'days' => $5, 'months' => $6,
'weekdays' => $8, 'user' => $f,
'command' => $10,
'index' => scalar(@rv) });
$rv[$#rv]->{'file'} =~ s/\s+\|$//;
&fix_names($rv[$#rv]);
}
elsif (/^(#+)?\s*@([a-z]+)\s+(.*)/i) {
# An @ time
push(@rv, { 'file' => "$config{'cron_dir'}/$f",
'line' => $lnum,
'type' => 0,
'active' => !$1,
'special' => $2,
'user' => $f,
'command' => $3,
'index' => scalar(@rv) });
}
elsif (/^(#+)?\s*([a-zA-Z0-9\_]+)\s*=\s*'([^']*)'/ ||
/^(#+)?\s*([a-zA-Z0-9\_]+)\s*=\s*"([^']*)"/ ||
/^(#+)?\s*([a-zA-Z0-9\_]+)\s*=\s*(\S+)/) {
# An environment variable
push(@rv, { 'file' => "$config{'cron_dir'}/$f",
'line' => $lnum,
'active' => !$1,
'name' => $2,
'value' => $3,
'user' => $f,
'index' => scalar(@rv) });
}
$lnum++;
}
close(TAB);
}
closedir(DIR);
@cron_jobs_cache = @rv;
return @cron_jobs_cache;
}
=head2 cron_job_line(&job)
Internal function to generate a crontab format line for a cron job.
=cut
sub cron_job_line
{
local @c;
push(@c, "#") if (!$_[0]->{'active'});
if ($_[0]->{'name'}) {
push(@c, $_[0]->{'name'});
push(@c, "=");
push(@c, $_[0]->{'value'} =~ /'/ ? "\"$_[0]->{'value'}\"" :
$_[0]->{'value'} =~ /"/ ? "'$_[0]->{'value'}'" :
$_[0]->{'value'} !~ /^\S+$/ ? "\"$_[0]->{'value'}\""
: $_[0]->{'value'});
}
else {
if ($_[0]->{'special'}) {
push(@c, ($_[0]->{'nolog'} ? '-' : '').'@'.$_[0]->{'special'});
}
else {
push(@c, ($_[0]->{'nolog'} ? '-' : '').$_[0]->{'mins'},
$_[0]->{'hours'}, $_[0]->{'days'},
$_[0]->{'months'}, $_[0]->{'weekdays'});
}
push(@c, $_[0]->{'user'}) if ($_[0]->{'type'} != 0 &&
$_[0]->{'type'} != 3);
push(@c, $_[0]->{'command'});
}
if ($gconfig{'os_type'} eq 'syno-linux') {
return join("\t", @c);
}
else {
return join(" ", @c);
}
}
=head2 copy_cron_temp(&job)
Copies a job's user's current cron configuration to the temp file. For internal
use only.
=cut
sub copy_cron_temp
{
local $fcron = ($config{'cron_dir'} =~ /\/fcron$/);
unlink($cron_temp_file);
if ($single_user) {
&execute_command($config{'cron_user_get_command'},
undef, $cron_temp_file, undef);
}
elsif ($fcron) {
&execute_command(&user_sub($config{'cron_get_command'},$_[0]->{'user'}),
undef, $cron_temp_file, undef);
}
else {
system("cp ".&translate_filename("$config{'cron_dir'}/$_[0]->{'user'}").
" $cron_temp_file 2>/dev/null");
}
}
=head2 create_cron_job(&job)
Add a Cron job to a user's file. The job parameter must be a hash reference
in the same format as returned by list_cron_jobs.
=cut
sub create_cron_job
{
&check_cron_config_or_error();
&list_cron_jobs(); # init cache
if ($config{'add_file'}) {
# Add to a specific file, typically something like /etc/cron.d/webmin
$_[0]->{'type'} = 1;
local $lref = &read_file_lines($config{'add_file'});
push(@$lref, &cron_job_line($_[0]));
&flush_file_lines($config{'add_file'});
}
elsif ($config{'single_file'} && !$config{'cron_dir'}) {
# Add to the single file
$_[0]->{'type'} = 3;
local $lref = &read_file_lines($config{'single_file'});
push(@$lref, &cron_job_line($_[0]));
&flush_file_lines($config{'single_file'});
}
else {
# Add to the specified user's crontab
©_cron_temp($_[0]);
local $lref = &read_file_lines($cron_temp_file);
$_[0]->{'line'} = scalar(@$lref);
push(@$lref, &cron_job_line($_[0]));
&flush_file_lines($cron_temp_file);
&set_ownership_permissions($_[0]->{'user'}, undef, undef,
$cron_temp_file);
©_crontab($_[0]->{'user'});
$_[0]->{'file'} = "$config{'cron_dir'}/$_[0]->{'user'}";
$_[0]->{'index'} = scalar(@cron_jobs_cache);
push(@cron_jobs_cache, $_[0]);
}
}
=head2 insert_cron_job(&job)
Add a Cron job at the top of the user's file. The job parameter must be a hash
reference in the same format as returned by list_cron_jobs.
=cut
sub insert_cron_job
{
&check_cron_config_or_error();
&list_cron_jobs(); # init cache
if ($config{'single_file'} && !$config{'cron_dir'}) {
# Insert into single file
$_[0]->{'type'} = 3;
local $lref = &read_file_lines($config{'single_file'});
splice(@$lref, 0, 0, &cron_job_line($_[0]));
&flush_file_lines($config{'single_file'});
}
else {
# Insert into the user's crontab
©_cron_temp($_[0]);
local $lref = &read_file_lines($cron_temp_file);
$_[0]->{'line'} = 0;
splice(@$lref, 0, 0, &cron_job_line($_[0]));
&flush_file_lines();
system("chown $_[0]->{'user'} $cron_temp_file");
©_crontab($_[0]->{'user'});
$_[0]->{'file'} = "$config{'cron_dir'}/$_[0]->{'user'}";
$_[0]->{'index'} = scalar(@cron_jobs_cache);
&renumber($_[0]->{'file'}, $_[0]->{'line'}, 1);
push(@cron_jobs_cache, $_[0]);
}
}
=head2 renumber(file, line, offset)
All jobs in this file whose line is at or after the given one will be
incremented by the offset. For internal use.
=cut
sub renumber
{
local $j;
foreach $j (@cron_jobs_cache) {
if ($j->{'line'} >= $_[1] &&
$j->{'file'} eq $_[0]) {
$j->{'line'} += $_[2];
}
}
}
=head2 renumber_index(index, offset)
Internal function to change the index of all cron jobs in the cache after
some index by a given offset. For internal use.
=cut
sub renumber_index
{
local $j;
foreach $j (@cron_jobs_cache) {
if ($j->{'index'} >= $_[0]) {
$j->{'index'} += $_[1];
}
}
}
=head2 change_cron_job(&job)
Updates the given cron job, which must be a hash ref returned by list_cron_jobs
and modified with a new active flag, command or schedule.
=cut
sub change_cron_job
{
if ($_[0]->{'type'} == 0) {
©_cron_temp($_[0]);
&replace_file_line($cron_temp_file, $_[0]->{'line'},
&cron_job_line($_[0])."\n");
©_crontab($_[0]->{'user'});
}
else {
&replace_file_line($_[0]->{'file'}, $_[0]->{'line'},
&cron_job_line($_[0])."\n");
}
}
=head2 delete_cron_job(&job)
Removes the cron job defined by the given hash ref, as returned by
list_cron_jobs.
=cut
sub delete_cron_job
{
if ($_[0]->{'type'} == 0) {
©_cron_temp($_[0]);
&replace_file_line($cron_temp_file, $_[0]->{'line'});
©_crontab($_[0]->{'user'});
}
else {
&replace_file_line($_[0]->{'file'}, $_[0]->{'line'});
}
@cron_jobs_cache = grep { $_ ne $_[0] } @cron_jobs_cache;
&renumber($_[0]->{'file'}, $_[0]->{'line'}, -1);
&renumber_index($_[0]->{'index'}, -1);
}
=head2 read_crontab(user)
Return an array containing the lines of the cron table for some user. For
internal use mainly.
=cut
sub read_crontab
{
local(@tab);
&open_readfile(TAB, "$config{cron_dir}/$_[0]");
@tab = <TAB>;
close(TAB);
if (@tab >= 3 && $tab[0] =~ /DO NOT EDIT/ &&
$tab[1] =~ /^\s*#/ && $tab[2] =~ /^\s*#/) {
@tab = @tab[3..$#tab];
}
return @tab;
}
=head2 copy_crontab(user)
Copy the cron temp file to that for this user. For internal use only.
=cut
sub copy_crontab
{
if (&is_readonly_mode()) {
# Do nothing
return undef;
}
local($pwd);
if (&read_file_contents($cron_temp_file) =~ /\S/) {
local $temp = &transname();
local $rv;
if (!&has_crontab_cmd()) {
# We have no crontab command .. emulate by copying to user file
$rv = system("cat $cron_temp_file".
" >$config{'cron_dir'}/$_[0] 2>/dev/null");
&set_ownership_permissions($_[0], undef, 0600,
"$config{'cron_dir'}/$_[0]");
}
elsif ($config{'cron_edit_command'}) {
# fake being an editor
local $notemp = &transname();
&open_tempfile(NO, ">$notemp");
&print_tempfile(NO, "No\n");
&print_tempfile(NO, "N\n");
&print_tempfile(NO, "no\n");
&close_tempfile(NO);
$ENV{"VISUAL"} = $ENV{"EDITOR"} =
"$module_root_directory/cron_editor.pl";
$ENV{"CRON_EDITOR_COPY"} = $cron_temp_file;
system("chown $_[0] $cron_temp_file");
local $oldpwd = &get_current_dir();
chdir("/");
if ($single_user) {
$rv = system($config{'cron_user_edit_command'}.
" >$temp 2>&1 <$notemp");
}
else {
$rv = system(
&user_sub($config{'cron_edit_command'},$_[0]).
" >$temp 2>&1 <$notemp");
}
unlink($notemp);
chdir($oldpwd);
} else {
# use the cron copy command
if ($single_user) {
$rv = &execute_command(
$config{'cron_user_copy_command'},
$cron_temp_file, $temp, $temp);
}
else {
$rv = &execute_command(
&user_sub($config{'cron_copy_command'}, $_[0]),
$cron_temp_file, $temp, $temp);
}
}
local $out = &read_file_contents($temp);
unlink($temp);
if ($rv || $out =~ /error/i) {
local $cronin = &read_file_contents($cron_temp_file);
&error(&text('ecopy', "<pre>$out</pre>", "<pre>$cronin</pre>"));
}
}
else {
# No more cron jobs left, so just delete
if (!&has_crontab_cmd()) {
# We have no crontab command .. emulate by deleting user crontab
$_[0] || &error("No user given!");
&unlink_logged("$config{'cron_dir'}/$_[0]");
}
else{
if ($single_user) {
&execute_command($config{'cron_user_delete_command'});
}
else {
&execute_command(&user_sub(
$config{'cron_delete_command'}, $_[0]));
}
}
}
if (!&has_crontab_cmd()) {
# to reload config
&kill_byname("crond", "SIGHUP");
}
unlink($cron_temp_file);
}
=head2 parse_job(job-line)
Parse a crontab line into an array containing:
active, mins, hrs, days, mons, weekdays, command
=cut
sub parse_job
{
local($job, $active) = ($_[0], 1);
if ($job =~ /^#+\s*(.*)$/) {
$active = 0;
$job = $1;
}
$job =~ /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/;
return ($active, $1, $2, $3, $4, $5, $6);
}
=head2 user_sub(command, user)
Replace the string 'USER' in the command with the user name. For internal
use only.
=cut
sub user_sub
{
local($tmp);
$tmp = $_[0];
$tmp =~ s/USER/$_[1]/g;
return $tmp;
}
=head2 list_allowed
Returns a list of all Unix usernames who are allowed to use Cron.
=cut
sub list_allowed
{
local(@rv, $_);
&open_readfile(ALLOW, $config{cron_allow_file});
while(<ALLOW>) {
next if (/^\s*#/);
chop; push(@rv, $_) if (/\S/);
}
close(ALLOW);
return @rv;
}
=head2 list_denied
Return a list of all Unix usernames who are not allowed to use Cron.
=cut
sub list_denied
{
local(@rv, $_);
&open_readfile(DENY, $config{cron_deny_file});
while(<DENY>) {
next if (/^\s*#/);
chop; push(@rv, $_) if (/\S/);
}
close(DENY);
return @rv;
}
=head2 save_allowed(user, user, ...)
Save the list of allowed Unix usernames.
=cut
sub save_allowed
{
local($_);
&open_tempfile(ALLOW, ">$config{cron_allow_file}");
foreach (@_) {
&print_tempfile(ALLOW, $_,"\n");
}
&close_tempfile(ALLOW);
chmod(0444, $config{cron_allow_file});
}
=head2 save_denied(user, user, ...)
Save the list of denied Unix usernames.
=cut
sub save_denied
{
local($_);
&open_tempfile(DENY, "> $config{cron_deny_file}");
foreach (@_) {
&print_tempfile(DENY, $_,"\n");
}
&close_tempfile(DENY);
chmod(0444, $config{cron_deny_file});
}
=head2 read_envs(user)
Returns an array of "name value" strings containing the environment settings
from the crontab for some user
=cut
sub read_envs
{
local(@tab, @rv, $_);
@tab = &read_crontab($_[0]);
foreach (@tab) {
chop; s/#.*$//g;
if (/^\s*(\S+)\s*=\s*(.*)$/) { push(@rv, "$1 $2"); }
}
return @rv;
}
=head2 save_envs(user, [name, value]*)
Updates the cron file for some user with the given list of environment
variables. All others in the file are removed.
=cut
sub save_envs
{
local($i, @tab, $line);
@tab = &read_crontab($_[0]);
open(TAB, ">".$cron_temp_file);
for($i=1; $i<@_; $i+=2) {
print TAB "$_[$i]=$_[$i+1]\n";
}
foreach (@tab) {
chop($line = $_); $line =~ s/#.*$//g;
if ($line !~ /^\s*(\S+)\s*=\s*(.*)$/) { print TAB $_; }
}
close(TAB);
©_crontab($_[0]);
}
=head2 expand_run_parts(directory)
Internal function to convert a directory like /etc/cron.hourly into a list
of scripts in that directory.
=cut
sub expand_run_parts
{
local $dir = $_[0];
$dir = "$config{'run_parts_dir'}/$dir"
if ($config{'run_parts_dir'} && $dir !~ /^\//);
opendir(DIR, &translate_filename($dir));
local @rv = readdir(DIR);
closedir(DIR);
@rv = grep { /^[a-zA-Z0-9\_\-]+$/ } @rv;
@rv = map { $dir."/".$_ } @rv;
@rv = grep { -x $_ } @rv;
return @rv;
}
=head2 is_run_parts(command)
Returns the dir if some cron job runs a list of commands in some directory,
like /etc/cron.hourly. Returns undef otherwise.
=cut
sub is_run_parts
{
local ($cmd) = @_;
local $rp = $config{'run_parts'};
$cmd =~ s/\s*#.*$//;
return $rp && $cmd =~ /$rp(.*)\s+(\-\-\S+\s+)*([a-z0-9\.\-\/_]+)(\s*\))?$/i ? $3 : undef;
}
=head2 can_edit_user(&access, user)
Returns 1 if the Webmin user whose permissions are defined by the access hash
ref can manage cron jobs for a given Unix user.
=cut
sub can_edit_user
{
local %umap;
map { $umap{$_}++; } split(/\s+/, $_[0]->{'users'})
if ($_[0]->{'mode'} == 1 || $_[0]->{'mode'} == 2);
if ($_[0]->{'mode'} == 1 && !$umap{$_[1]} ||
$_[0]->{'mode'} == 2 && $umap{$_[1]}) { return 0; }
elsif ($_[0]->{'mode'} == 3) {
return $remote_user eq $_[1];
}
elsif ($_[0]->{'mode'} == 4) {
local @u = getpwnam($_[1]);
return (!$_[0]->{'uidmin'} || $u[2] >= $_[0]->{'uidmin'}) &&
(!$_[0]->{'uidmax'} || $u[2] <= $_[0]->{'uidmax'});
}
elsif ($_[0]->{'mode'} == 5) {
local @u = getpwnam($_[1]);
return $u[3] == $_[0]->{'users'};
}
else {
return 1;
}
}
=head2 list_cron_specials()
Returns a list of the names of special cron times, prefixed by an @ in crontab
=cut
sub list_cron_specials
{
return ('hourly', 'daily', 'weekly', 'monthly', 'yearly', 'reboot');
}
=head2 get_times_input(&job, [nospecial], [width-in-cols], [message])
Returns HTML for selecting the schedule for a cron job, defined by the first
parameter which must be a hash ref returned by list_cron_jobs. Suitable for
use inside a ui_table_start/end
=cut
sub get_times_input
{
return &theme_get_times_input(@_) if (defined(&theme_get_times_input));
my ($job, $nospecial, $width, $msg) = @_;
$width ||= 2;
# Javascript to disable and enable fields
my $rv = <<EOF;
<script>
function enable_cron_fields(name, form, ena)
{
var els = form.elements[name];
els.disabled = !ena;
for(i=0; i<els.length; i++) {
els[i].disabled = !ena;
}
change_special_mode(form, 0);
}
function change_special_mode(form, special)
{
if(form.special_def) {
form.special_def[0].checked = special;
form.special_def[1].checked = !special;
}
}
</script>
EOF
if ($config{'vixie_cron'} && (!$nospecial || $job->{'special'})) {
# Allow selection of special @ times
my $sp = $job->{'special'} eq 'midnight' ? 'daily' :
$job->{'special'} eq 'annually' ? 'yearly' : $job->{'special'};
my $specialsel = &ui_select("special", $sp,
[ map { [ $_, $text{'edit_special_'.$_} ] }
&list_cron_specials() ],
1, 0, 0, 0, "onChange='change_special_mode(form, 1)'");
$rv .= &ui_table_row($msg,
&ui_radio("special_def", $job->{'special'} ? 1 : 0,
[ [ 1, $text{'edit_special1'}." ".$specialsel ],
[ 0, $text{'edit_special0'} ] ]),
$msg ? $width-1 : $width);
}
# Section for time selections
my $table = &ui_columns_start([ $text{'edit_mins'}, $text{'edit_hours'},
$text{'edit_days'}, $text{'edit_months'},
$text{'edit_weekdays'} ], 100);
my @mins = (0..59);
my @hours = (0..23);
my @days = (1..31);
my @months = map { $text{"month_$_"}."=".$_ } (1 .. 12);
my @weekdays = map { $text{"day_$_"}."=".$_ } (0 .. 6);
my %arrmap = ( 'mins' => \@mins,
'hours' => \@hours,
'days' => \@days,
'months' => \@months,
'weekdays' => \@weekdays );
my @cols;
foreach my $arr ("mins", "hours", "days", "months", "weekdays") {
# Find out which ones are being used
my %inuse;
my $min = ($arr =~ /days|months/ ? 1 : 0);
my @arrlist = @{$arrmap{$arr}};
my $max = $min+scalar(@arrlist)-1;
foreach my $w (split(/,/ , $job->{$arr})) {
if ($w eq "*") {
# all values
for($j=$min; $j<=$max; $j++) { $inuse{$j}++; }
}
elsif ($w =~ /^\*\/(\d+)$/) {
# only every Nth
for($j=$min; $j<=$max; $j+=$1) { $inuse{$j}++; }
}
elsif ($w =~ /^(\d+)-(\d+)\/(\d+)$/) {
# only every Nth of some range
for($j=$1; $j<=$2; $j+=$3) { $inuse{int($j)}++; }
}
elsif ($w =~ /^(\d+)-(\d+)$/) {
# all of some range
for($j=$1; $j<=$2; $j++) { $inuse{int($j)}++; }
}
else {
# One value
$inuse{int($w)}++;
}
}
if ($job->{$arr} eq "*") {
%inuse = ( );
}
# Output selection list
my $dis = $arr eq "mins" && $hourly_only;
my $col = &ui_radio(
"all_$arr", $job->{$arr} eq "*" ||
$job->{$arr} eq "" ? 1 : 0,
[ [ 1, $text{'edit_all'}."<br>",
"onClick='enable_cron_fields(\"$arr\", form, 0)'" ],
[ 0, $text{'edit_selected'}."<br>",
"onClick='enable_cron_fields(\"$arr\", form, 1)'" ] ],
$dis);
$col .= "<table> <tr>\n";
for(my $j=0; $j<@arrlist; $j+=($arr eq "mins" && $hourly_only ? 60 : 12)) {
my $jj = $j+($arr eq "mins" && $hourly_only ? 59 : 11);
if ($jj >= @arrlist) { $jj = @arrlist - 1; }
my @sec = @arrlist[$j .. $jj];
my @opts;
foreach my $v (@sec) {
if ($v =~ /^(.*)=(.*)$/) {
push(@opts, [ $2, $1 ]);
}
else {
push(@opts, [ $v, $v ]);
}
}
my $dis = $job->{$arr} eq "*" || $job->{$arr} eq "";
$col .= "<td valign=top>".
&ui_select($arr, [ keys %inuse ], \@opts,
@sec > 12 ? ($arr eq "mins" && $hourly_only ? 1 : 12)
: scalar(@sec),
$arr eq "mins" && $hourly_only ? 0 : 1,
0, $dis).
"</td>\n";
}
$col .= "</tr></table>\n";
push(@cols, $col);
}
$table .= &ui_columns_row(\@cols, [ "valign=top", "valign=top", "valign=top",
"valign=top", "valign=top" ]);
$table .= &ui_columns_end();
$table .= $text{'edit_ctrl'};
$rv .= &ui_table_row(undef, $table, $width);
return $rv;
}
=head2 show_times_input(&job, [nospecial])
Print HTML for inputs for selecting the schedule for a cron job, defined
by the first parameter which must be a hash ref returned by list_cron_jobs.
This must be used inside a <table>, as the HTML starts and ends with <tr>
tags.
=cut
sub show_times_input
{
return &theme_show_times_input(@_) if (defined(&theme_show_times_input));
local $job = $_[0];
if ($config{'vixie_cron'} && (!$_[1] || $_[0]->{'special'})) {
# Allow selection of special @ times
print "<tr data-schedule-tr $cb> <td colspan=6>\n";
printf "<input type=radio name=special_def value=1 %s> %s\n",
$job->{'special'} ? "checked" : "", $text{'edit_special1'};
print "<select name=special onChange='change_special_mode(form, 1)'>\n";
local $s;
local $sp = $job->{'special'} eq 'midnight' ? 'daily' :
$job->{'special'} eq 'annually' ? 'yearly' : $job->{'special'};
foreach $s ('hourly', 'daily', 'weekly', 'monthly', 'yearly', 'reboot'){
printf "<option value=%s %s>%s</option>\n",
$s, $sp eq $s ? "selected" : "", $text{'edit_special_'.$s};
}
print "</select>\n";
printf "<input type=radio name=special_def value=0 %s> %s\n",
$job->{'special'} ? "" : "checked", $text{'edit_special0'};
print "</td></tr>\n";
}
# Javascript to disable and enable fields
print <<EOF;
<script>
function enable_cron_fields(name, form, ena)
{
var els = form.elements[name];
els.disabled = !ena;
for(i=0; i<els.length; i++) {
els[i].disabled = !ena;
}
change_special_mode(form, 0);
}
function change_special_mode(form, special)
{
if(form.special_def) {
form.special_def[0].checked = special;
form.special_def[1].checked = !special;
}
}
</script>
EOF
print "<tr $tb>\n";
print "<td><b>$text{'edit_mins'}</b></td> <td><b>$text{'edit_hours'}</b></td> ",
"<td><b>$text{'edit_days'}</b></td> <td><b>$text{'edit_months'}</b></td>",
"<td><b>$text{'edit_weekdays'}</b></td> </tr> <tr $cb>\n";
local @mins = (0..59);
local @hours = (0..23);
local @days = (1..31);
local @months = map { $text{"month_$_"}."=".$_ } (1 .. 12);
local @weekdays = map { $text{"day_$_"}."=".$_ } (0 .. 6);
foreach $arr ("mins", "hours", "days", "months", "weekdays") {
# Find out which ones are being used
local %inuse;
local $min = ($arr =~ /days|months/ ? 1 : 0);