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