-
Notifications
You must be signed in to change notification settings - Fork 13
/
fcrontab.c
1163 lines (972 loc) · 32.4 KB
/
fcrontab.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-2021 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.
*/
/*
* The goal of this program is simple : giving a user interface to fcron
* daemon, by allowing each user to see, modify, append or remove his
* fcrontabs.
* Fcron daemon use a specific formated format of file, so fcrontab generate
* that kind of file from human readable files. In order allowing users to
* see and modify their fcrontabs, the source file is always saved with the
* formated one.
* Fcrontab makes a temporary formated file, and then sends a signal
* to the daemon to force it to update its configuration, remove the temp
* file and save a new and final formated file.
* That way, not the simple, allows the daemon to keep a maximum of
* informations like the time remaining before next execution, or the date
* and hour of next execution.
*/
#include "fcrontab.h"
#include "allow.h"
#include "fileconf.h"
#include "temp_file.h"
#include "read_string.h"
void info(void);
void usage(void);
/* used in temp_file() */
char *tmp_path = "/tmp/";
/* command line options */
char rm_opt = 0;
char list_opt = 0;
char edit_opt = 0;
char reinstall_opt = 0;
char ignore_prev = 0;
int file_opt = 0;
/* uid/gid of users/groups
* (we don't use the static UID or GID as we ask for user and group names
* in the configure script) */
char *user = NULL;
char *runas = NULL;
uid_t useruid = 0; /* uid of the user */
gid_t usergid = 0; /* gid of the user */
uid_t asuid = 0; /* uid of the user whose fcrontab we are working on */
gid_t asgid = 0; /* gid of the user whose fcrontab we are working on */
uid_t fcrontab_uid = 0; /* uid of the fcron user */
gid_t fcrontab_gid = 0; /* gid of the fcron user */
uid_t rootuid = 0; /* uid of root */
gid_t rootgid = 0; /* gid of root */
char need_sig = 0; /* do we need to signal fcron daemon */
char orig_dir[PATH_LEN];
cf_t *file_base = NULL;
char buf[PATH_LEN];
char file[PATH_LEN];
/* needed by log part : */
char *prog_name = NULL;
char foreground = 1;
pid_t daemon_pid = 0;
#ifdef HAVE_LIBPAM
int conv_pam(int num_msg, const struct pam_message **msgm,
struct pam_response **response, void *appdata_ptr);
pam_handle_t *pamh = NULL;
const struct pam_conv apamconv = { conv_pam, NULL };
#endif /* HAVE_LIBPAM */
void
info(void)
/* print some informations about this program :
* version, license */
{
fprintf(stderr,
"fcrontab " VERSION_QUOTED " - user interface to daemon fcron\n"
"Copyright " COPYRIGHT_QUOTED " Thibault Godouet <fcron@free.fr>\n"
"This program is free software distributed WITHOUT ANY WARRANTY.\n"
"See the GNU General Public License for more details.\n");
exit(EXIT_OK);
}
void
usage(void)
/* print a help message about command line options and exit */
{
fprintf(stderr,
"fcrontab [-n] file [user|-u user]\n"
"fcrontab { -l | -r | -e | -z } [-n] [user|-u user]\n"
"fcrontab -h\n"
" -u user specify user name.\n"
" -l list user's current fcrontab.\n"
" -r remove user's current fcrontab.\n"
" -e edit user's current fcrontab.\n"
" -z reinstall user's fcrontab from source code.\n"
" -n ignore previous version of file.\n"
" -c f make fcrontab use config file f.\n"
" -d set up debug mode.\n"
" -h display this help message.\n"
" -V display version & infos about fcrontab.\n" "\n");
exit(EXIT_ERR);
}
void
xexit(int exit_val)
/* launch signal if needed and exit */
{
pid_t pid = 0;
/* WARNING: make sure we never call die_e() or something that could call
* die_e() here, as die_e() would then call xexit() and we could
* go into a loop! */
if (need_sig == 1) {
/* fork and exec fcronsighup */
switch (pid = fork()) {
case 0:
/* child */
if (getegid() != fcrontab_gid && setegid(fcrontab_gid) != 0) {
error_e("could not change egid to fcrontab_gid[%d]",
fcrontab_gid);
exit(ERR);
}
execl(BINDIREX "/fcronsighup", BINDIREX "/fcronsighup", fcronconf,
NULL);
error_e("Could not exec " BINDIREX " fcronsighup");
exit(ERR);
break;
case -1:
error_e("Could not fork (fcron has not been signaled)");
exit(ERR);
break;
default:
/* parent */
waitpid(pid, NULL, 0);
break;
}
}
#ifdef HAVE_LIBPAM
/* we need those rights for pam to close properly */
if (geteuid() != fcrontab_uid && seteuid(fcrontab_uid) != 0)
die_e("could not change euid to %d", fcrontab_uid);
if (getegid() != fcrontab_gid && setegid(fcrontab_gid) != 0)
die_e("could not change egid to %d", fcrontab_gid);
pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
pam_end(pamh, pam_close_session(pamh, PAM_SILENT));
#endif
exit(exit_val);
}
int
copy_src(int from, const char *dest)
/* copy src file orig (already opened) to dest */
/* we first copy the file to a temp file name, and then we rename it,
* so as to avoid data loss if the filesystem is full. */
{
int to_fd = -1;
int nb;
char *copy_buf[LINE_LEN];
char tmp_filename_str[PATH_LEN + 4];
int dest_path_len, tmp_filename_index;
char *tmp_suffix_str = ".tmp";
int max_dest_len = sizeof(tmp_filename_str) - sizeof(tmp_suffix_str);
if (from < 0) {
die("copy_src() called with an invalid 'from' argument");
}
/* just in case the file was read in the past... */
lseek(from, 0, SEEK_SET);
/* the temp file must be in the same directory as the dest file */
dest_path_len = strlen(dest);
strncpy(tmp_filename_str, dest, max_dest_len);
tmp_filename_index = (dest_path_len > max_dest_len) ?
max_dest_len : dest_path_len;
strcpy(&tmp_filename_str[tmp_filename_index], tmp_suffix_str);
/* create it as fcrontab_uid (to avoid problem if user's uid changed)
* except for root. Root requires filesystem uid root for security
* reasons */
to_fd =
open_as_user(tmp_filename_str,
(asuid == rootuid) ? rootuid : fcrontab_uid, fcrontab_gid,
O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
S_IRUSR | S_IWUSR | S_IRGRP);
if (to_fd < 0) {
error_e("could not open file %s", tmp_filename_str);
goto exiterr;
}
if (asuid == rootuid) {
if (fchmod(to_fd, S_IWUSR | S_IRUSR) != 0) {
error_e("Could not fchmod %s to 600", tmp_filename_str);
goto exiterr;
}
if (fchown(to_fd, rootuid, fcrontab_gid) != 0) {
error_e("Could not fchown %s to root", tmp_filename_str);
goto exiterr;
}
}
while ((nb = read(from, copy_buf, sizeof(copy_buf))) != -1 && nb != 0)
if (write(to_fd, copy_buf, nb) != nb) {
error_e("Error while copying file (no space left ?)."
" Aborting : old source file kept");
goto exiterr;
}
xclose_check(&to_fd, dest);
if (rename_as_user(tmp_filename_str, dest, useruid, fcrontab_gid) < 0) {
error_e("Unable to rename %s to %s : old source file kept",
tmp_filename_str, dest);
goto exiterr;
}
return OK;
exiterr:
if (to_fd != -1)
xclose_check(&to_fd, dest);
return ERR;
}
int
remove_fcrontab(char rm_orig)
/* remove user's fcrontab and tell daemon to update his conf */
/* note : the binary fcrontab is removed by fcron */
{
int return_val = OK;
int fd;
if (rm_orig)
explain("removing %s's fcrontab", user);
/* remove source and formated file */
if ((rm_orig && remove_as_user(buf, fcrontab_uid, fcrontab_gid)) != 0) {
if (errno == ENOENT)
return_val = ENOENT;
else
error_e("could not remove %s", buf);
}
/* try to remove the temp file in case he has not
* been read by fcron daemon */
snprintf(buf, sizeof(buf), "new.%s", user);
remove_as_user(buf, useruid, fcrontab_gid);
/* finally create a file in order to tell the daemon
* a file was removed, and launch a signal to daemon */
snprintf(buf, sizeof(buf), "rm.%s", user);
fd = open_as_user(buf, fcrontab_uid, fcrontab_gid,
O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR);
if (fd == -1) {
if (errno != EEXIST)
error_e("Can't create file %s", buf);
}
else if (asuid == rootuid && fchown(fd, rootuid, fcrontab_gid) != 0)
error_e("Could not fchown %s to root", buf);
xclose_check(&fd, buf);
need_sig = 1;
return return_val;
}
int
write_file(int fd)
{
int return_val = OK;
if (ignore_prev == 1)
/* if user wants to ignore previous version, we remove it *
* ( fcron daemon remove files no longer wanted before
* adding new ones ) */
remove_fcrontab(0);
/* copy original file to fcrontabs dir */
snprintf(buf, sizeof(buf), "%s.orig", user);
if (copy_src(fd, buf) == ERR) {
return_val = ERR;
}
else {
if (file_base->cf_line_base == NULL) {
/* no entries */
explain("%s's fcrontab contains no entries : removed.", user);
remove_fcrontab(0);
}
else {
/* write the binary fcrontab on disk */
snprintf(buf, sizeof(buf), "new.%s", user);
if (save_file(buf) != OK)
return_val = ERR;
}
}
return return_val;
}
int
make_file(char *file, int fd)
{
explain("installing file %s for user %s", file, user);
/* read file and create a list in memory */
switch (read_file(file, fd)) {
case 2:
case OK:
if (write_file(fd) == ERR)
return ERR;
else
/* tell daemon to update the conf */
need_sig = 1;
/* free memory used to store the list */
delete_file(user);
break;
case ERR:
return ERR;
}
return OK;
}
void
list_file(const char *file)
{
FILE *f = NULL;
int c;
int fd = -1;
explain("listing %s's fcrontab", user);
fd = open_as_user(file, useruid, fcrontab_gid, O_RDONLY);
if (fd < 0) {
if (errno == ENOENT) {
explain("user %s has no fcrontab.", user);
return;
}
else
die_e("User %s could not read file \"%s\"", user, file);
}
f = fdopen(fd, "r");
if (f == NULL) {
xclose_check(&fd, file);
die_e("User %s could not read file \"%s\"", user, file);
}
while ((c = getc(f)) != EOF)
putchar(c);
/* also closes the underlying file descriptor fd: */
xfclose_check(&f, file);
}
void
edit_file(const char *fcron_orig)
/* copy file to a temp file, edit that file, and install it
* if necessary */
{
char *cureditor = NULL;
char editorcmd[PATH_LEN];
pid_t pid;
int status;
struct stat st;
time_t mtime = 0;
char *tmp_str = NULL;
FILE *f = NULL, *fi = NULL;
int file = -1, origfd = -1;
int c;
char correction = 0;
short return_val = EXIT_OK;
explain("fcrontab : editing %s's fcrontab", user);
if ((cureditor = getenv("VISUAL")) == NULL || strcmp(cureditor, "\0") == 0)
if ((cureditor = getenv("EDITOR")) == NULL
|| strcmp(cureditor, "\0") == 0)
cureditor = editor;
/* temp_file() dies on error, so tmp_str is always set */
file = temp_file(&tmp_str);
if ((fi = fdopen(file, "w")) == NULL) {
error_e("could not fdopen");
goto exiterr;
}
#ifndef USE_SETE_ID
if (fchown(file, asuid, asgid) != 0) {
error_e("Could not fchown %s to asuid and asgid", tmp_str);
goto exiterr;
}
#endif
/* copy user's fcrontab (if any) to a temp file */
origfd = open_as_user(fcron_orig, useruid, fcrontab_gid, O_RDONLY);
if (origfd < 0) {
if (errno != ENOENT) {
error_e("could not open file %s", fcron_orig);
goto exiterr;
}
else
fprintf(stderr, "no fcrontab for %s - using an empty one\n", user);
}
else {
f = fdopen(origfd, "r");
if (f == NULL) {
error_e("could not fdopen file %s", fcron_orig);
goto exiterr;
}
/* copy original file to temp file */
while ((c = getc(f)) != EOF) {
if (putc(c, fi) == EOF) {
error_e("could not write to file %s", tmp_str);
goto exiterr;
}
}
xfclose_check(&f, fcron_orig);
if (ferror(fi))
error_e("Error while writing new fcrontab to %s");
}
/* Don't close fi, because we still need the file descriptor 'file' */
if (fflush(fi) != 0)
die_e("Could not fflush(%s)", fi);
fi = NULL;
do {
if (fstat(file, &st) == 0)
mtime = st.st_mtime;
else {
error_e("could not stat \"%s\"", tmp_str);
goto exiterr;
}
#ifndef USE_SETE_ID
/* chown the file (back if correction) to asuid/asgid so as user can edit it */
if (fchown(file, asuid, asgid) != 0
|| fchmod(file, S_IRUSR | S_IWUSR) != 0) {
fprintf(stderr, "Can't chown or chmod %s.\n", tmp_str);
goto exiterr;
}
#endif
/* close the file before the user edits it */
xclose_check(&file, tmp_str);
switch (pid = fork()) {
case 0:
/* child */
if (useruid != rootuid) {
if (setgid(asgid) < 0) {
error_e("setgid(asgid)");
goto exiterr;
}
if (setuid(asuid) < 0) {
error_e("setuid(asuid)");
goto exiterr;
}
}
else {
/* Some programs, like perl, require gid=egid : */
if (setgid(getgid()) < 0) {
error_e("setgid(getgid())");
goto exiterr;
}
}
snprintf(editorcmd, sizeof(editorcmd), "%s %s", cureditor, tmp_str);
if (chdir(tmp_path) != 0)
error_e("Could not chdir to %s", tmp_path);
execlp(shell, shell, "-c", editorcmd, tmp_str, NULL);
error_e("Error while running \"%s\"", cureditor);
goto exiterr;
case -1:
error_e("fork");
goto exiterr;
default:
/* parent */
break;
}
/* only reached by parent */
waitpid(pid, &status, 0);
if (!WIFEXITED(status)) {
fprintf(stderr,
"Editor exited abnormally:" " fcrontab is unchanged.\n");
goto exiterr;
}
/* re-open the file that has just been edited */
file = open_as_user(tmp_str, useruid, usergid, O_RDONLY);
if (file < 0) {
error_e("Could not open file %s", tmp_str);
goto exiterr;
}
#ifndef USE_SETE_ID
/* chown the file back to rootuid/rootgid */
if (fchown(file, rootuid, rootgid) != 0
|| fchmod(file, S_IRUSR | S_IWUSR) != 0) {
fprintf(stderr, "Can't chown or chmod %s.\n", tmp_str);
goto exiterr;
}
#endif
/* check if file has been modified */
if (fstat(file, &st) != 0) {
error_e("could not stat %s", tmp_str);
goto exiterr;
}
else if (st.st_mtime > mtime || correction == 1) {
correction = 0;
switch (read_file(tmp_str, file)) {
case ERR:
goto exiterr;
case 2:
fprintf(stderr, "\nFile contains some errors. "
"Ignore [i] or Correct [c] ? ");
while ((c = getchar())) {
/* consume the rest of the line, e.g. the newline char (\n) */
while (c != '\n' && (getchar() != '\n')) ;
if (c == 'i') {
break;
}
else if (c == 'c') {
/* free memory used to store the list */
delete_file(user);
correction = 1;
break;
}
else {
fprintf(stderr,
"Please press c to correct, "
"or i to ignore: ");
}
}
break;
default:
break;
}
}
else {
fprintf(stderr,
"Fcrontab is unchanged :" " no need to install it.\n");
goto end;
}
} while (correction == 1);
if (write_file(file) != OK)
return_val = EXIT_ERR;
else
/* tell daemon to update the conf */
need_sig = 1;
/* free memory used to store the list */
delete_file(user);
end:
xclose_check(&file, tmp_str);
if (remove_as_user(tmp_str, useruid, fcrontab_gid) != 0)
error_e("could not remove %s", tmp_str);
Free_safe(tmp_str);
xexit(return_val);
exiterr:
xfclose_check(&fi, tmp_str);
xclose_check(&file, tmp_str);
if (remove_as_user(tmp_str, useruid, fcrontab_gid) != 0)
error_e("could not remove %s", tmp_str);
xfclose_check(&f, fcron_orig);
Free_safe(tmp_str);
xexit(EXIT_ERR);
}
int
install_stdin(void)
/* install what we get through stdin */
{
int tmp_fd = 0;
FILE *tmp_file = NULL;
char *tmp_str = NULL;
int c;
short return_val = EXIT_OK;
tmp_fd = temp_file(&tmp_str);
if ((tmp_file = fdopen(tmp_fd, "w")) == NULL)
die_e("Could not fdopen file %s", tmp_str);
while ((c = getc(stdin)) != EOF)
putc(c, tmp_file);
/* // */
debug("Copied stdin to %s, about to parse file %s...", tmp_str, tmp_str);
/* don't closes tmp_fd as it will be used for make_file(): */
if (fflush(tmp_file) != 0)
die_e("Could not fflush(%s)", tmp_file);
if (make_file(tmp_str, tmp_fd) == ERR)
goto exiterr;
else
goto exit;
exiterr:
return_val = EXIT_ERR;
exit:
if (remove(tmp_str) != 0)
error_e("Could not remove %s", tmp_str);
free(tmp_str);
return return_val;
}
void
reinstall(char *fcron_orig)
{
int i = -1;
explain("reinstalling %s's fcrontab", user);
if ((i = open_as_user(fcron_orig, useruid, fcrontab_gid, O_RDONLY)) < 0) {
if (errno == ENOENT) {
fprintf(stderr, "Could not reinstall: user %s has no fcrontab\n",
user);
}
else
fprintf(stderr, "Could not open \"%s\": %s\n", fcron_orig,
strerror(errno));
xexit(EXIT_ERR);
}
close(0);
dup2(i, 0);
xclose(&i);
xexit(install_stdin());
}
#ifdef HAVE_LIBPAM
int
conv_pam(int num_msg, const struct pam_message **msgm,
struct pam_response **response, void *appdata_ptr)
/* text based conversation for pam. */
{
int count = 0;
struct pam_response *reply;
if (num_msg <= 0)
return PAM_CONV_ERR;
reply = (struct pam_response *)calloc(num_msg, sizeof(struct pam_response));
if (reply == NULL) {
debug("no memory for responses");
return PAM_CONV_ERR;
}
for (count = 0; count < num_msg; ++count) {
char *string = NULL;
switch (msgm[count]->msg_style) {
case PAM_PROMPT_ECHO_OFF:
string = read_string(CONV_ECHO_OFF, msgm[count]->msg);
if (string == NULL) {
goto failed_conversation;
}
break;
case PAM_PROMPT_ECHO_ON:
string = read_string(CONV_ECHO_ON, msgm[count]->msg);
if (string == NULL) {
goto failed_conversation;
}
break;
case PAM_ERROR_MSG:
if (fprintf(stderr, "%s\n", msgm[count]->msg) < 0) {
goto failed_conversation;
}
break;
case PAM_TEXT_INFO:
if (fprintf(stdout, "%s\n", msgm[count]->msg) < 0) {
goto failed_conversation;
}
break;
default:
fprintf(stderr, "erroneous conversation (%d)\n",
msgm[count]->msg_style);
goto failed_conversation;
}
if (string) { /* must add to reply array */
/* add string to list of responses */
reply[count].resp_retcode = 0;
reply[count].resp = string;
string = NULL;
}
}
/* New (0.59+) behavior is to always have a reply - this is
* compatable with the X/Open (March 1997) spec. */
*response = reply;
reply = NULL;
return PAM_SUCCESS;
failed_conversation:
if (reply) {
for (count = 0; count < num_msg; ++count) {
if (reply[count].resp == NULL) {
continue;
}
switch (msgm[count]->msg_style) {
case PAM_PROMPT_ECHO_ON:
case PAM_PROMPT_ECHO_OFF:
Overwrite(reply[count].resp);
free(reply[count].resp);
break;
case PAM_ERROR_MSG:
case PAM_TEXT_INFO:
/* should not actually be able to get here... */
free(reply[count].resp);
}
reply[count].resp = NULL;
}
/* forget reply too */
free(reply);
reply = NULL;
}
return PAM_CONV_ERR;
}
#endif /* HAVE_LIBPAM */
void
parseopt(int argc, char *argv[])
/* set options */
{
int c;
extern char *optarg;
extern int optind, opterr, optopt;
struct passwd *pass;
#ifdef SYSFCRONTAB
char is_sysfcrontab = 0;
#endif
/* constants and variables defined by command line */
while (1) {
c = getopt(argc, argv, "u:lrezdnhVc:");
if (c == EOF)
break;
switch (c) {
case 'V':
info();
break;
case 'h':
usage();
break;
case 'u':
if (useruid != rootuid) {
fprintf(stderr, "must be privileged to use -u\n");
xexit(EXIT_ERR);
}
user = strdup2(optarg);
break;
case 'd':
debug_opt = 1;
break;
case 'l':
if (rm_opt || edit_opt || reinstall_opt) {
fprintf(stderr, "Only one of the options -l, -r, -e and -z"
"may be used simultaneously.\n");
xexit(EXIT_ERR);
}
list_opt = 1;
rm_opt = edit_opt = reinstall_opt = 0;
break;
case 'r':
if (list_opt || edit_opt || reinstall_opt) {
fprintf(stderr, "Only one of the options -l, -r, -e and -z"
"may be used simultaneously.\n");
xexit(EXIT_ERR);
}
rm_opt = 1;
list_opt = edit_opt = reinstall_opt = 0;
break;
case 'e':
if (list_opt || rm_opt || reinstall_opt) {
fprintf(stderr, "Only one of the options -l, -r, -e and -z"
"may be used simultaneously.\n");
xexit(EXIT_ERR);
}
edit_opt = 1;
list_opt = rm_opt = reinstall_opt = 0;
break;
case 'z':
if (list_opt || rm_opt || edit_opt) {
fprintf(stderr, "Only one of the options -l, -r, -e and -z"
"may be used simultaneously.\n");
xexit(EXIT_ERR);
}
reinstall_opt = ignore_prev = 1;
list_opt = rm_opt = edit_opt = 0;
break;
case 'n':
ignore_prev = 1;
break;
case 'c':
if (optarg[0] == '/') {
Set(fcronconf, optarg);
}
else {
char buf[PATH_LEN];
snprintf(buf, sizeof(buf), "%s/%s", orig_dir, optarg);
Set(fcronconf, buf);
}
break;
case ':':
fprintf(stderr, "(setopt) Missing parameter.\n");
usage();
case '?':
usage();
default:
fprintf(stderr, "(setopt) Warning: getopt returned %c.\n", c);
}
}
/* read fcron.conf and update global parameters */
read_conf();
/* read the file name and/or user and check validity of the arguments */
if (argc - optind > 2)
usage();
else if (argc - optind == 2) {
if (list_opt + rm_opt + edit_opt + reinstall_opt == 0)
file_opt = optind++;
else
usage();
if (useruid != rootuid) {
fprintf(stderr, "must be privileged to use -u\n");
xexit(EXIT_ERR);
}
Set(user, argv[optind]);
}
else if (argc - optind == 1) {
if (list_opt + rm_opt + edit_opt + reinstall_opt == 0)
file_opt = optind;
else {
if (useruid != rootuid) {
fprintf(stderr, "must be privileged to use [user|-u user]\n");
xexit(EXIT_ERR);
}
Set(user, argv[optind]);
}
}
else if (list_opt + rm_opt + edit_opt + reinstall_opt != 1)
usage();
if (user == NULL) {
/* get user's name using getpwuid() */
if (!(pass = getpwuid(useruid)))
die_e("user \"%s\" is not in passwd file. Aborting.", USERNAME);
/* we need to strdup2 the value given by getpwuid() because we free
* file->cf_user in delete_file */
user = strdup2(pass->pw_name);
asuid = pass->pw_uid;
asgid = pass->pw_gid;
}
else {
#ifdef SYSFCRONTAB
if (strcmp(user, SYSFCRONTAB) == 0) {
is_sysfcrontab = 1;
asuid = rootuid;
asgid = rootgid;
}
else
#endif /* def SYSFCRONTAB */
{
errno = 0;
if ((pass = getpwnam(user))) {
asuid = pass->pw_uid;
asgid = pass->pw_gid;
}
else
die_e("user \"%s\" is not in passwd file. Aborting.", user);
}
}
if (
#ifdef SYSFCRONTAB
!is_sysfcrontab &&
#endif
!is_allowed(user)) {
die("User \"%s\" is not allowed to use %s. Aborting.", user, prog_name);
}
#ifdef SYSFCRONTAB
if (is_sysfcrontab)
runas = ROOTNAME;
else
#endif
runas = user;
}
int
main(int argc, char **argv)
{
#ifdef HAVE_LIBPAM
int retcode = 0;
const char *const *env;
#endif
struct passwd *pass;
rootuid = get_user_uid_safe(ROOTNAME);
rootgid = get_group_gid_safe(ROOTGROUP);