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_complementary = "?1:dr";
103 opt_ler = getopt32(argv, "u:c:lerd", &user_name, &crontab_dir);
104 argv += optind;
105
106 if (sanitize_env_if_suid()) {
107
108 if (opt_ler & (OPT_u|OPT_c))
109 bb_error_msg_and_die(bb_msg_you_must_be_root);
110 }
111
112 if (opt_ler & OPT_u) {
113 pas = xgetpwnam(user_name);
114 } else {
115 pas = xgetpwuid(getuid());
116 }
117
118#define user_name DONT_USE_ME_BEYOND_THIS_POINT
119
120
121 opt_ler &= OPT_ler;
122 if ((opt_ler - 1) & opt_ler)
123 bb_show_usage();
124
125
126 src_fd = STDIN_FILENO;
127 if (!opt_ler) {
128 if (!argv[0])
129 bb_show_usage();
130 if (NOT_LONE_DASH(argv[0])) {
131 src_fd = xopen_as_uid_gid(argv[0], O_RDONLY, pas->pw_uid, pas->pw_gid);
132 }
133 }
134
135
136 xchdir(crontab_dir);
137
138 tmp_fname = NULL;
139
140
141 switch (opt_ler) {
142
143 default:
144 unlink(pas->pw_name);
145 break;
146
147 case OPT_l:
148 {
149 char *args[2] = { pas->pw_name, NULL };
150 return bb_cat(args);
151
152
153 }
154
155 case OPT_e:
156 tmp_fname = xasprintf("%s.%u", crontab_dir, (unsigned)getpid());
157
158
159 src_fd = xopen3(tmp_fname, O_RDWR|O_CREAT|O_TRUNC, 0600);
160 fchown(src_fd, pas->pw_uid, pas->pw_gid);
161 fd = open(pas->pw_name, O_RDONLY);
162 if (fd >= 0) {
163 bb_copyfd_eof(fd, src_fd);
164 close(fd);
165 xlseek(src_fd, 0, SEEK_SET);
166 }
167 close_on_exec_on(src_fd);
168 edit_file(pas, tmp_fname);
169
170
171 case 0:
172 new_fname = xasprintf("%s.new", pas->pw_name);
173 fd = open(new_fname, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0600);
174 if (fd >= 0) {
175 bb_copyfd_eof(src_fd, fd);
176 close(fd);
177 xrename(new_fname, pas->pw_name);
178 } else {
179 bb_error_msg("can't create %s/%s",
180 crontab_dir, new_fname);
181 }
182 if (tmp_fname)
183 unlink(tmp_fname);
184
185
186 }
187
188
189
190
191 while ((fd = open(CRONUPDATE, O_WRONLY|O_CREAT|O_APPEND, 0600)) >= 0) {
192 struct stat st;
193
194 fdprintf(fd, "%s\n", pas->pw_name);
195 if (fstat(fd, &st) != 0 || st.st_nlink != 0) {
196
197 break;
198 }
199
200
201 close(fd);
202
203 }
204 if (fd < 0) {
205 bb_error_msg("can't append to %s/%s",
206 crontab_dir, CRONUPDATE);
207 }
208 return 0;
209}
210