diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-09-27 19:48:56 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-09-27 19:48:56 +0000 |
commit | 94d5d82bd84ec65d52c3df92276221d48990f306 (patch) | |
tree | dc05f1d8e8f9cb0ccf7b9192db44f03e694eb6f6 | |
parent | 956a5693603be076a7a2b0bacc22eea4a3b93cbe (diff) | |
download | busybox-w32-94d5d82bd84ec65d52c3df92276221d48990f306.tar.gz busybox-w32-94d5d82bd84ec65d52c3df92276221d48990f306.tar.bz2 busybox-w32-94d5d82bd84ec65d52c3df92276221d48990f306.zip |
crontab: we indent using tabs
-rw-r--r-- | miscutils/crontab.c | 549 |
1 files changed, 277 insertions, 272 deletions
diff --git a/miscutils/crontab.c b/miscutils/crontab.c index 1acacc676..3706f9fd8 100644 --- a/miscutils/crontab.c +++ b/miscutils/crontab.c | |||
@@ -22,315 +22,320 @@ | |||
22 | #define CRONUPDATE "cron.update" | 22 | #define CRONUPDATE "cron.update" |
23 | #endif | 23 | #endif |
24 | #ifndef PATH_VI | 24 | #ifndef PATH_VI |
25 | #define PATH_VI "/bin/vi" /* location of vi */ | 25 | #define PATH_VI "/bin/vi" /* location of vi */ |
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | static const char *CDir = CRONTABS; | 28 | static const char *CDir = CRONTABS; |
29 | 29 | ||
30 | static void EditFile(const char *user, const char *file); | 30 | static void EditFile(const char *user, const char *file); |
31 | static int GetReplaceStream(const char *user, const char *file); | 31 | static int GetReplaceStream(const char *user, const char *file); |
32 | static int ChangeUser(const char *user, short dochdir); | 32 | static int ChangeUser(const char *user, short dochdir); |
33 | 33 | ||
34 | int crontab_main(int ac, char **av) | 34 | int crontab_main(int ac, char **av) |
35 | { | 35 | { |
36 | enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE; | 36 | enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE; |
37 | const struct passwd *pas; | 37 | const struct passwd *pas; |
38 | const char *repFile = NULL; | 38 | const char *repFile = NULL; |
39 | int repFd = 0; | 39 | int repFd = 0; |
40 | int i; | 40 | int i; |
41 | char caller[256]; /* user that ran program */ | 41 | char caller[256]; /* user that ran program */ |
42 | int UserId; | 42 | char buf[1024]; |
43 | 43 | int UserId; | |
44 | UserId = getuid(); | 44 | |
45 | if ((pas = getpwuid(UserId)) == NULL) | 45 | UserId = getuid(); |
46 | bb_perror_msg_and_die("getpwuid"); | 46 | pas = getpwuid(UserId); |
47 | 47 | if (pas == NULL) | |
48 | safe_strncpy(caller, pas->pw_name, sizeof(caller)); | 48 | bb_perror_msg_and_die("getpwuid"); |
49 | 49 | ||
50 | i = 1; | 50 | safe_strncpy(caller, pas->pw_name, sizeof(caller)); |
51 | if (ac > 1) { | 51 | |
52 | if (av[1][0] == '-' && av[1][1] == 0) { | 52 | i = 1; |
53 | option = REPLACE; | 53 | if (ac > 1) { |
54 | ++i; | 54 | if (av[1][0] == '-' && av[1][1] == 0) { |
55 | } else if (av[1][0] != '-') { | 55 | option = REPLACE; |
56 | option = REPLACE; | 56 | ++i; |
57 | ++i; | 57 | } else if (av[1][0] != '-') { |
58 | repFile = av[1]; | 58 | option = REPLACE; |
59 | } | 59 | ++i; |
60 | } | 60 | repFile = av[1]; |
61 | |||
62 | for (; i < ac; ++i) { | ||
63 | char *ptr = av[i]; | ||
64 | |||
65 | if (*ptr != '-') | ||
66 | break; | ||
67 | ptr += 2; | ||
68 | |||
69 | switch(ptr[-1]) { | ||
70 | case 'l': | ||
71 | if (ptr[-1] == 'l') | ||
72 | option = LIST; | ||
73 | /* fall through */ | ||
74 | case 'e': | ||
75 | if (ptr[-1] == 'e') | ||
76 | option = EDIT; | ||
77 | /* fall through */ | ||
78 | case 'd': | ||
79 | if (ptr[-1] == 'd') | ||
80 | option = DELETE; | ||
81 | /* fall through */ | ||
82 | case 'u': | ||
83 | if (i + 1 < ac && av[i+1][0] != '-') { | ||
84 | ++i; | ||
85 | if (getuid() == geteuid()) { | ||
86 | pas = getpwnam(av[i]); | ||
87 | if (pas) { | ||
88 | UserId = pas->pw_uid; | ||
89 | } else { | ||
90 | bb_error_msg_and_die("user %s unknown", av[i]); | ||
91 | } | ||
92 | } else { | ||
93 | bb_error_msg_and_die("only the superuser may specify a user"); | ||
94 | } | 61 | } |
95 | } | ||
96 | break; | ||
97 | case 'c': | ||
98 | if (getuid() == geteuid()) { | ||
99 | CDir = (*ptr) ? ptr : av[++i]; | ||
100 | } else { | ||
101 | bb_error_msg_and_die("-c option: superuser only"); | ||
102 | } | ||
103 | break; | ||
104 | default: | ||
105 | i = ac; | ||
106 | break; | ||
107 | } | 62 | } |
108 | } | 63 | |
109 | if (i != ac || option == NONE) | 64 | for (; i < ac; ++i) { |
110 | bb_show_usage(); | 65 | char *ptr = av[i]; |
111 | 66 | ||
112 | /* | 67 | if (*ptr != '-') |
113 | * Get password entry | 68 | break; |
114 | */ | 69 | ptr += 2; |
115 | 70 | ||
116 | if ((pas = getpwuid(UserId)) == NULL) | 71 | switch (ptr[-1]) { |
117 | bb_perror_msg_and_die("getpwuid"); | 72 | case 'l': |
118 | 73 | if (ptr[-1] == 'l') | |
119 | /* | 74 | option = LIST; |
120 | * If there is a replacement file, obtain a secure descriptor to it. | 75 | /* fall through */ |
121 | */ | 76 | case 'e': |
122 | 77 | if (ptr[-1] == 'e') | |
123 | if (repFile) { | 78 | option = EDIT; |
124 | repFd = GetReplaceStream(caller, repFile); | 79 | /* fall through */ |
125 | if (repFd < 0) | 80 | case 'd': |
126 | bb_error_msg_and_die("unable to read replacement file"); | 81 | if (ptr[-1] == 'd') |
127 | } | 82 | option = DELETE; |
128 | 83 | /* fall through */ | |
129 | /* | 84 | case 'u': |
130 | * Change directory to our crontab directory | 85 | if (i + 1 < ac && av[i+1][0] != '-') { |
131 | */ | 86 | ++i; |
132 | 87 | if (getuid() == geteuid()) { | |
133 | xchdir(CDir); | 88 | pas = getpwnam(av[i]); |
134 | 89 | if (pas) { | |
135 | /* | 90 | UserId = pas->pw_uid; |
136 | * Handle options as appropriate | 91 | } else { |
137 | */ | 92 | bb_error_msg_and_die("user %s unknown", av[i]); |
138 | 93 | } | |
139 | switch(option) { | 94 | } else { |
140 | case LIST: | 95 | bb_error_msg_and_die("only the superuser may specify a user"); |
141 | { | 96 | } |
142 | FILE *fi; | 97 | } |
143 | char buf[1024]; | 98 | break; |
144 | 99 | case 'c': | |
145 | if ((fi = fopen(pas->pw_name, "r"))) { | 100 | if (getuid() == geteuid()) { |
146 | while (fgets(buf, sizeof(buf), fi) != NULL) | 101 | CDir = (*ptr) ? ptr : av[++i]; |
147 | fputs(buf, stdout); | 102 | } else { |
148 | fclose(fi); | 103 | bb_error_msg_and_die("-c option: superuser only"); |
149 | } else { | 104 | } |
150 | bb_error_msg("no crontab for %s", pas->pw_name); | 105 | break; |
151 | } | 106 | default: |
107 | i = ac; | ||
108 | break; | ||
109 | } | ||
152 | } | 110 | } |
153 | break; | 111 | if (i != ac || option == NONE) |
154 | case EDIT: | 112 | bb_show_usage(); |
155 | { | 113 | |
156 | FILE *fi; | 114 | /* |
157 | int fd; | 115 | * Get password entry |
158 | int n; | 116 | */ |
159 | char tmp[128]; | 117 | |
160 | char buf[1024]; | 118 | pas = getpwuid(UserId); |
161 | 119 | if (pas == NULL) | |
162 | snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid()); | 120 | bb_perror_msg_and_die("getpwuid"); |
163 | fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600); | 121 | |
164 | chown(tmp, getuid(), getgid()); | 122 | /* |
165 | if ((fi = fopen(pas->pw_name, "r"))) { | 123 | * If there is a replacement file, obtain a secure descriptor to it. |
166 | while ((n = fread(buf, 1, sizeof(buf), fi)) > 0) | 124 | */ |
167 | write(fd, buf, n); | 125 | |
168 | } | 126 | if (repFile) { |
169 | EditFile(caller, tmp); | 127 | repFd = GetReplaceStream(caller, repFile); |
170 | remove(tmp); | 128 | if (repFd < 0) |
171 | lseek(fd, 0L, 0); | 129 | bb_error_msg_and_die("unable to read replacement file"); |
172 | repFd = fd; | ||
173 | } | 130 | } |
174 | option = REPLACE; | 131 | |
175 | /* fall through */ | 132 | /* |
176 | case REPLACE: | 133 | * Change directory to our crontab directory |
177 | { | 134 | */ |
178 | char buf[1024]; | 135 | |
179 | char path[1024]; | 136 | xchdir(CDir); |
180 | int fd; | 137 | |
181 | int n; | 138 | /* |
182 | 139 | * Handle options as appropriate | |
183 | snprintf(path, sizeof(path), "%s.new", pas->pw_name); | 140 | */ |
184 | if ((fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600)) >= 0) { | 141 | |
185 | while ((n = read(repFd, buf, sizeof(buf))) > 0) { | 142 | switch (option) { |
186 | write(fd, buf, n); | 143 | case LIST: |
144 | { | ||
145 | FILE *fi; | ||
146 | |||
147 | fi = fopen(pas->pw_name, "r"); | ||
148 | if (fi) { | ||
149 | while (fgets(buf, sizeof(buf), fi) != NULL) | ||
150 | fputs(buf, stdout); | ||
151 | fclose(fi); | ||
152 | } else { | ||
153 | bb_error_msg("no crontab for %s", pas->pw_name); | ||
154 | } | ||
187 | } | 155 | } |
188 | close(fd); | ||
189 | rename(path, pas->pw_name); | ||
190 | } else { | ||
191 | bb_error_msg("unable to create %s/%s", CDir, path); | ||
192 | } | ||
193 | close(repFd); | ||
194 | } | ||
195 | break; | ||
196 | case DELETE: | ||
197 | remove(pas->pw_name); | ||
198 | break; | ||
199 | case NONE: | ||
200 | default: | ||
201 | break; | ||
202 | } | ||
203 | |||
204 | /* | ||
205 | * Bump notification file. Handle window where crond picks file up | ||
206 | * before we can write our entry out. | ||
207 | */ | ||
208 | |||
209 | if (option == REPLACE || option == DELETE) { | ||
210 | FILE *fo; | ||
211 | struct stat st; | ||
212 | |||
213 | while ((fo = fopen(CRONUPDATE, "a"))) { | ||
214 | fprintf(fo, "%s\n", pas->pw_name); | ||
215 | fflush(fo); | ||
216 | if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) { | ||
217 | fclose(fo); | ||
218 | break; | 156 | break; |
219 | } | 157 | case EDIT: |
220 | fclose(fo); | 158 | { |
221 | /* loop */ | 159 | FILE *fi; |
160 | int fd; | ||
161 | int n; | ||
162 | char tmp[128]; | ||
163 | |||
164 | snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid()); | ||
165 | fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600); | ||
166 | chown(tmp, getuid(), getgid()); | ||
167 | fi = fopen(pas->pw_name, "r"); | ||
168 | if (fi) { | ||
169 | while ((n = fread(buf, 1, sizeof(buf), fi)) > 0) | ||
170 | write(fd, buf, n); | ||
171 | } | ||
172 | EditFile(caller, tmp); | ||
173 | remove(tmp); | ||
174 | lseek(fd, 0L, 0); | ||
175 | repFd = fd; | ||
176 | } | ||
177 | option = REPLACE; | ||
178 | /* fall through */ | ||
179 | case REPLACE: | ||
180 | { | ||
181 | char path[1024]; | ||
182 | int fd; | ||
183 | int n; | ||
184 | |||
185 | snprintf(path, sizeof(path), "%s.new", pas->pw_name); | ||
186 | fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600); | ||
187 | if (fd >= 0) { | ||
188 | while ((n = read(repFd, buf, sizeof(buf))) > 0) { | ||
189 | write(fd, buf, n); | ||
190 | } | ||
191 | close(fd); | ||
192 | rename(path, pas->pw_name); | ||
193 | } else { | ||
194 | bb_error_msg("unable to create %s/%s", CDir, path); | ||
195 | } | ||
196 | close(repFd); | ||
197 | } | ||
198 | break; | ||
199 | case DELETE: | ||
200 | remove(pas->pw_name); | ||
201 | break; | ||
202 | case NONE: | ||
203 | default: | ||
204 | break; | ||
222 | } | 205 | } |
223 | if (fo == NULL) { | 206 | |
224 | bb_error_msg("unable to append to %s/%s", CDir, CRONUPDATE); | 207 | /* |
208 | * Bump notification file. Handle window where crond picks file up | ||
209 | * before we can write our entry out. | ||
210 | */ | ||
211 | |||
212 | if (option == REPLACE || option == DELETE) { | ||
213 | FILE *fo; | ||
214 | struct stat st; | ||
215 | |||
216 | while ((fo = fopen(CRONUPDATE, "a"))) { | ||
217 | fprintf(fo, "%s\n", pas->pw_name); | ||
218 | fflush(fo); | ||
219 | if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) { | ||
220 | fclose(fo); | ||
221 | break; | ||
222 | } | ||
223 | fclose(fo); | ||
224 | /* loop */ | ||
225 | } | ||
226 | if (fo == NULL) { | ||
227 | bb_error_msg("unable to append to %s/%s", CDir, CRONUPDATE); | ||
228 | } | ||
225 | } | 229 | } |
226 | } | 230 | return 0; |
227 | return 0; | ||
228 | } | 231 | } |
229 | 232 | ||
230 | static int GetReplaceStream(const char *user, const char *file) | 233 | static int GetReplaceStream(const char *user, const char *file) |
231 | { | 234 | { |
232 | int filedes[2]; | 235 | int filedes[2]; |
233 | int pid; | 236 | int pid; |
234 | int fd; | 237 | int fd; |
235 | int n; | 238 | int n; |
236 | char buf[1024]; | 239 | char buf[1024]; |
237 | 240 | ||
238 | if (pipe(filedes) < 0) { | 241 | if (pipe(filedes) < 0) { |
239 | perror("pipe"); | 242 | perror("pipe"); |
240 | return(-1); | 243 | return -1; |
241 | } | 244 | } |
242 | if ((pid = fork()) < 0) { | 245 | pid = fork(); |
243 | perror("fork"); | 246 | if (pid < 0) { |
244 | return(-1); | 247 | perror("fork"); |
245 | } | 248 | return -1; |
246 | if (pid > 0) { | 249 | } |
250 | if (pid > 0) { | ||
251 | /* | ||
252 | * PARENT | ||
253 | */ | ||
254 | |||
255 | close(filedes[1]); | ||
256 | if (read(filedes[0], buf, 1) != 1) { | ||
257 | close(filedes[0]); | ||
258 | filedes[0] = -1; | ||
259 | } | ||
260 | return filedes[0]; | ||
261 | } | ||
262 | |||
247 | /* | 263 | /* |
248 | * PARENT | 264 | * CHILD |
249 | */ | 265 | */ |
250 | 266 | ||
251 | close(filedes[1]); | 267 | close(filedes[0]); |
252 | if (read(filedes[0], buf, 1) != 1) { | ||
253 | close(filedes[0]); | ||
254 | filedes[0] = -1; | ||
255 | } | ||
256 | return(filedes[0]); | ||
257 | } | ||
258 | |||
259 | /* | ||
260 | * CHILD | ||
261 | */ | ||
262 | 268 | ||
263 | close(filedes[0]); | 269 | if (ChangeUser(user, 0) < 0) |
270 | exit(0); | ||
264 | 271 | ||
265 | if (ChangeUser(user, 0) < 0) | 272 | bb_default_error_retval = 0; |
273 | fd = xopen(file, O_RDONLY); | ||
274 | buf[0] = 0; | ||
275 | write(filedes[1], buf, 1); | ||
276 | while ((n = read(fd, buf, sizeof(buf))) > 0) { | ||
277 | write(filedes[1], buf, n); | ||
278 | } | ||
266 | exit(0); | 279 | exit(0); |
267 | |||
268 | bb_default_error_retval = 0; | ||
269 | fd = xopen(file, O_RDONLY); | ||
270 | buf[0] = 0; | ||
271 | write(filedes[1], buf, 1); | ||
272 | while ((n = read(fd, buf, sizeof(buf))) > 0) { | ||
273 | write(filedes[1], buf, n); | ||
274 | } | ||
275 | exit(0); | ||
276 | } | 280 | } |
277 | 281 | ||
278 | static void EditFile(const char *user, const char *file) | 282 | static void EditFile(const char *user, const char *file) |
279 | { | 283 | { |
280 | int pid; | 284 | int pid = fork(); |
285 | |||
286 | if (pid == 0) { | ||
287 | /* | ||
288 | * CHILD - change user and run editor | ||
289 | */ | ||
290 | char *ptr; | ||
291 | char visual[1024]; | ||
292 | |||
293 | if (ChangeUser(user, 1) < 0) | ||
294 | exit(0); | ||
295 | ptr = getenv("VISUAL"); | ||
296 | if (ptr == NULL || strlen(ptr) > 256) | ||
297 | ptr = PATH_VI; | ||
298 | |||
299 | snprintf(visual, sizeof(visual), "%s %s", ptr, file); | ||
300 | execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", visual, NULL); | ||
301 | perror("exec"); | ||
302 | exit(0); | ||
303 | } | ||
304 | if (pid < 0) { | ||
305 | /* | ||
306 | * PARENT - failure | ||
307 | */ | ||
308 | bb_perror_msg_and_die("fork"); | ||
309 | } | ||
310 | wait4(pid, NULL, 0, NULL); | ||
311 | } | ||
312 | |||
313 | static int ChangeUser(const char *user, short dochdir) | ||
314 | { | ||
315 | struct passwd *pas; | ||
281 | 316 | ||
282 | if ((pid = fork()) == 0) { | ||
283 | /* | 317 | /* |
284 | * CHILD - change user and run editor | 318 | * Obtain password entry and change privileges |
285 | */ | 319 | */ |
286 | char *ptr; | ||
287 | char visual[1024]; | ||
288 | 320 | ||
289 | if (ChangeUser(user, 1) < 0) | 321 | pas = getpwnam(user); |
290 | exit(0); | 322 | if (pas == NULL) { |
291 | if ((ptr = getenv("VISUAL")) == NULL || strlen(ptr) > 256) | 323 | bb_perror_msg_and_die("failed to get uid for %s", user); |
292 | ptr = PATH_VI; | 324 | } |
325 | setenv("USER", pas->pw_name, 1); | ||
326 | setenv("HOME", pas->pw_dir, 1); | ||
327 | setenv("SHELL", DEFAULT_SHELL, 1); | ||
293 | 328 | ||
294 | snprintf(visual, sizeof(visual), "%s %s", ptr, file); | ||
295 | execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", visual, NULL); | ||
296 | perror("exec"); | ||
297 | exit(0); | ||
298 | } | ||
299 | if (pid < 0) { | ||
300 | /* | 329 | /* |
301 | * PARENT - failure | 330 | * Change running state to the user in question |
302 | */ | 331 | */ |
303 | bb_perror_msg_and_die("fork"); | 332 | change_identity(pas); |
304 | } | ||
305 | wait4(pid, NULL, 0, NULL); | ||
306 | } | ||
307 | 333 | ||
308 | static int ChangeUser(const char *user, short dochdir) | 334 | if (dochdir) { |
309 | { | 335 | if (chdir(pas->pw_dir) < 0) { |
310 | struct passwd *pas; | 336 | bb_perror_msg("chdir(%s) by %s failed", pas->pw_dir, user); |
311 | 337 | xchdir(TMPDIR); | |
312 | /* | 338 | } |
313 | * Obtain password entry and change privileges | ||
314 | */ | ||
315 | |||
316 | if ((pas = getpwnam(user)) == NULL) { | ||
317 | bb_perror_msg_and_die("failed to get uid for %s", user); | ||
318 | return(-1); | ||
319 | } | ||
320 | setenv("USER", pas->pw_name, 1); | ||
321 | setenv("HOME", pas->pw_dir, 1); | ||
322 | setenv("SHELL", DEFAULT_SHELL, 1); | ||
323 | |||
324 | /* | ||
325 | * Change running state to the user in question | ||
326 | */ | ||
327 | change_identity(pas); | ||
328 | |||
329 | if (dochdir) { | ||
330 | if (chdir(pas->pw_dir) < 0) { | ||
331 | bb_perror_msg("chdir failed: %s %s", user, pas->pw_dir); | ||
332 | xchdir(TMPDIR); | ||
333 | } | 339 | } |
334 | } | 340 | return pas->pw_uid; |
335 | return(pas->pw_uid); | ||
336 | } | 341 | } |