1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include "libbb.h"
24
25#define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
26#ifndef CRONUPDATE
27#define CRONUPDATE "cron.update"
28#endif
29
30static void edit_file(const struct passwd *pas, const char *file)
31{
32 const char *ptr;
33 pid_t pid;
34
35 pid = xvfork();
36 if (pid) {
37 wait4pid(pid);
38 return;
39 }
40
41
42
43 change_identity(pas);
44 setup_environment(pas->pw_shell,
45 SETUP_ENV_CHANGEENV | SETUP_ENV_TO_TMP,
46 pas);
47 ptr = getenv("VISUAL");
48 if (!ptr) {
49 ptr = getenv("EDITOR");
50 if (!ptr)
51 ptr = "vi";
52 }
53
54 BB_EXECLP(ptr, ptr, file, NULL);
55 bb_perror_msg_and_die("can't execute '%s'", ptr);
56}
57
58int crontab_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59int crontab_main(int argc UNUSED_PARAM, char **argv)
60{
61 const struct passwd *pas;
62 const char *crontab_dir = CRONTABS;
63 char *tmp_fname;
64 char *new_fname;
65 char *user_name;
66 int fd;
67 int src_fd;
68 int opt_ler;
69
70
71
72
73
74
75
76
77
78
79
80 enum {
81 OPT_u = (1 << 0),
82 OPT_c = (1 << 1),
83 OPT_l = (1 << 2),
84 OPT_e = (1 << 3),
85 OPT_r = (1 << 4),
86 OPT_ler = OPT_l + OPT_e + OPT_r,
87 };
88
89 opt_complementary = "?1:dr";
90 opt_ler = getopt32(argv, "u:c:lerd", &user_name, &crontab_dir);
91 argv += optind;
92
93 if (sanitize_env_if_suid()) {
94
95 if (opt_ler & (OPT_u|OPT_c))
96 bb_error_msg_and_die(bb_msg_you_must_be_root);
97 }
98
99 if (opt_ler & OPT_u) {
100 pas = xgetpwnam(user_name);
101 } else {
102 pas = xgetpwuid(getuid());
103 }
104
105#define user_name DONT_USE_ME_BEYOND_THIS_POINT
106
107
108 opt_ler &= OPT_ler;
109 if ((opt_ler - 1) & opt_ler)
110 bb_show_usage();
111
112
113 src_fd = STDIN_FILENO;
114 if (!opt_ler) {
115 if (!argv[0])
116 bb_show_usage();
117 if (NOT_LONE_DASH(argv[0])) {
118 src_fd = xopen_as_uid_gid(argv[0], O_RDONLY, pas->pw_uid, pas->pw_gid);
119 }
120 }
121
122
123 xchdir(crontab_dir);
124
125 tmp_fname = NULL;
126
127
128 switch (opt_ler) {
129
130 default:
131 unlink(pas->pw_name);
132 break;
133
134 case OPT_l:
135 {
136 char *args[2] = { pas->pw_name, NULL };
137 return bb_cat(args);
138
139
140 }
141
142 case OPT_e:
143 tmp_fname = xasprintf("%s.%u", crontab_dir, (unsigned)getpid());
144
145
146 src_fd = xopen3(tmp_fname, O_RDWR|O_CREAT|O_TRUNC, 0600);
147 fchown(src_fd, pas->pw_uid, pas->pw_gid);
148 fd = open(pas->pw_name, O_RDONLY);
149 if (fd >= 0) {
150 bb_copyfd_eof(fd, src_fd);
151 close(fd);
152 xlseek(src_fd, 0, SEEK_SET);
153 }
154 close_on_exec_on(src_fd);
155 edit_file(pas, tmp_fname);
156
157
158 case 0:
159 new_fname = xasprintf("%s.new", pas->pw_name);
160 fd = open(new_fname, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0600);
161 if (fd >= 0) {
162 bb_copyfd_eof(src_fd, fd);
163 close(fd);
164 xrename(new_fname, pas->pw_name);
165 } else {
166 bb_error_msg("can't create %s/%s",
167 crontab_dir, new_fname);
168 }
169 if (tmp_fname)
170 unlink(tmp_fname);
171
172
173 }
174
175
176
177
178 while ((fd = open(CRONUPDATE, O_WRONLY|O_CREAT|O_APPEND, 0600)) >= 0) {
179 struct stat st;
180
181 fdprintf(fd, "%s\n", pas->pw_name);
182 if (fstat(fd, &st) != 0 || st.st_nlink != 0) {
183
184 break;
185 }
186
187
188 close(fd);
189
190 }
191 if (fd < 0) {
192 bb_error_msg("can't append to %s/%s",
193 crontab_dir, CRONUPDATE);
194 }
195 return 0;
196}
197