Bernhard Reutner-Fischer | d9cf7ac | 2006-04-12 18:39:58 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 2 | /* |
| 3 | * CRONTAB |
| 4 | * |
| 5 | * usually setuid root, -c option only works if getuid() == geteuid() |
| 6 | * |
| 7 | * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com) |
Mike Frysinger | f284c76 | 2006-04-16 20:38:26 +0000 | [diff] [blame] | 8 | * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 9 | * |
Mike Frysinger | f284c76 | 2006-04-16 20:38:26 +0000 | [diff] [blame] | 10 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 14 | |
| 15 | #ifndef CRONTABS |
| 16 | #define CRONTABS "/var/spool/cron/crontabs" |
| 17 | #endif |
| 18 | #ifndef TMPDIR |
| 19 | #define TMPDIR "/var/spool/cron" |
| 20 | #endif |
| 21 | #ifndef CRONUPDATE |
| 22 | #define CRONUPDATE "cron.update" |
| 23 | #endif |
| 24 | #ifndef PATH_VI |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 25 | #define PATH_VI "/bin/vi" /* location of vi */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 28 | static const char *CDir = CRONTABS; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 29 | |
| 30 | static void EditFile(const char *user, const char *file); |
| 31 | static int GetReplaceStream(const char *user, const char *file); |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 32 | static int ChangeUser(const char *user, short dochdir); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 33 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 34 | int crontab_main(int ac, char **av) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 35 | { |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 36 | enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE; |
| 37 | const struct passwd *pas; |
| 38 | const char *repFile = NULL; |
| 39 | int repFd = 0; |
| 40 | int i; |
| 41 | char caller[256]; /* user that ran program */ |
| 42 | char buf[1024]; |
| 43 | int UserId; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 45 | UserId = getuid(); |
| 46 | pas = getpwuid(UserId); |
| 47 | if (pas == NULL) |
| 48 | bb_perror_msg_and_die("getpwuid"); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 50 | safe_strncpy(caller, pas->pw_name, sizeof(caller)); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 52 | i = 1; |
| 53 | if (ac > 1) { |
| 54 | if (av[1][0] == '-' && av[1][1] == 0) { |
| 55 | option = REPLACE; |
| 56 | ++i; |
| 57 | } else if (av[1][0] != '-') { |
| 58 | option = REPLACE; |
| 59 | ++i; |
| 60 | repFile = av[1]; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 61 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 62 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 64 | for (; i < ac; ++i) { |
| 65 | char *ptr = av[i]; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 67 | if (*ptr != '-') |
| 68 | break; |
| 69 | ptr += 2; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 71 | switch (ptr[-1]) { |
| 72 | case 'l': |
| 73 | if (ptr[-1] == 'l') |
| 74 | option = LIST; |
| 75 | /* fall through */ |
| 76 | case 'e': |
| 77 | if (ptr[-1] == 'e') |
| 78 | option = EDIT; |
| 79 | /* fall through */ |
| 80 | case 'd': |
| 81 | if (ptr[-1] == 'd') |
| 82 | option = DELETE; |
| 83 | /* fall through */ |
| 84 | case 'u': |
| 85 | if (i + 1 < ac && av[i+1][0] != '-') { |
| 86 | ++i; |
| 87 | if (getuid() == geteuid()) { |
| 88 | pas = getpwnam(av[i]); |
| 89 | if (pas) { |
| 90 | UserId = pas->pw_uid; |
| 91 | } else { |
| 92 | bb_error_msg_and_die("user %s unknown", av[i]); |
| 93 | } |
| 94 | } else { |
| 95 | bb_error_msg_and_die("only the superuser may specify a user"); |
| 96 | } |
| 97 | } |
| 98 | break; |
| 99 | case 'c': |
| 100 | if (getuid() == geteuid()) { |
| 101 | CDir = (*ptr) ? ptr : av[++i]; |
| 102 | } else { |
| 103 | bb_error_msg_and_die("-c option: superuser only"); |
| 104 | } |
| 105 | break; |
| 106 | default: |
| 107 | i = ac; |
| 108 | break; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 109 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 110 | } |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 111 | if (i != ac || option == NONE) |
| 112 | bb_show_usage(); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 113 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 114 | /* |
| 115 | * Get password entry |
| 116 | */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 117 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 118 | pas = getpwuid(UserId); |
| 119 | if (pas == NULL) |
| 120 | bb_perror_msg_and_die("getpwuid"); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 122 | /* |
| 123 | * If there is a replacement file, obtain a secure descriptor to it. |
| 124 | */ |
| 125 | |
| 126 | if (repFile) { |
| 127 | repFd = GetReplaceStream(caller, repFile); |
| 128 | if (repFd < 0) |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 129 | bb_error_msg_and_die("cannot read replacement file"); |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Change directory to our crontab directory |
| 134 | */ |
| 135 | |
| 136 | xchdir(CDir); |
| 137 | |
| 138 | /* |
| 139 | * Handle options as appropriate |
| 140 | */ |
| 141 | |
| 142 | switch (option) { |
| 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 | } |
| 155 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 156 | break; |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 157 | case EDIT: |
| 158 | { |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame^] | 159 | /* FIXME: messy code here! we have file copying helpers for this! */ |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 160 | FILE *fi; |
| 161 | int fd; |
| 162 | int n; |
| 163 | char tmp[128]; |
| 164 | |
| 165 | snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid()); |
| 166 | fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600); |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame^] | 167 | /* race, use fchown */ |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 168 | chown(tmp, getuid(), getgid()); |
| 169 | fi = fopen(pas->pw_name, "r"); |
| 170 | if (fi) { |
| 171 | while ((n = fread(buf, 1, sizeof(buf), fi)) > 0) |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame^] | 172 | full_write(fd, buf, n); |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 173 | } |
| 174 | EditFile(caller, tmp); |
| 175 | remove(tmp); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 176 | lseek(fd, 0L, SEEK_SET); |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 177 | repFd = fd; |
| 178 | } |
| 179 | option = REPLACE; |
| 180 | /* fall through */ |
| 181 | case REPLACE: |
| 182 | { |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame^] | 183 | /* same here */ |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 184 | char path[1024]; |
| 185 | int fd; |
| 186 | int n; |
| 187 | |
| 188 | snprintf(path, sizeof(path), "%s.new", pas->pw_name); |
| 189 | fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600); |
| 190 | if (fd >= 0) { |
| 191 | while ((n = read(repFd, buf, sizeof(buf))) > 0) { |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame^] | 192 | full_write(fd, buf, n); |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 193 | } |
| 194 | close(fd); |
| 195 | rename(path, pas->pw_name); |
| 196 | } else { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 197 | bb_error_msg("cannot create %s/%s", CDir, path); |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 198 | } |
| 199 | close(repFd); |
| 200 | } |
| 201 | break; |
| 202 | case DELETE: |
| 203 | remove(pas->pw_name); |
| 204 | break; |
| 205 | case NONE: |
| 206 | default: |
| 207 | break; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 208 | } |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Bump notification file. Handle window where crond picks file up |
| 212 | * before we can write our entry out. |
| 213 | */ |
| 214 | |
| 215 | if (option == REPLACE || option == DELETE) { |
| 216 | FILE *fo; |
| 217 | struct stat st; |
| 218 | |
| 219 | while ((fo = fopen(CRONUPDATE, "a"))) { |
| 220 | fprintf(fo, "%s\n", pas->pw_name); |
| 221 | fflush(fo); |
| 222 | if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) { |
| 223 | fclose(fo); |
| 224 | break; |
| 225 | } |
| 226 | fclose(fo); |
| 227 | /* loop */ |
| 228 | } |
| 229 | if (fo == NULL) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 230 | bb_error_msg("cannot append to %s/%s", CDir, CRONUPDATE); |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 231 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 232 | } |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 233 | return 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 236 | static int GetReplaceStream(const char *user, const char *file) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 237 | { |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 238 | int filedes[2]; |
| 239 | int pid; |
| 240 | int fd; |
| 241 | int n; |
| 242 | char buf[1024]; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 243 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 244 | if (pipe(filedes) < 0) { |
| 245 | perror("pipe"); |
| 246 | return -1; |
| 247 | } |
| 248 | pid = fork(); |
| 249 | if (pid < 0) { |
| 250 | perror("fork"); |
| 251 | return -1; |
| 252 | } |
| 253 | if (pid > 0) { |
| 254 | /* |
| 255 | * PARENT |
| 256 | */ |
| 257 | |
| 258 | close(filedes[1]); |
| 259 | if (read(filedes[0], buf, 1) != 1) { |
| 260 | close(filedes[0]); |
| 261 | filedes[0] = -1; |
| 262 | } |
| 263 | return filedes[0]; |
| 264 | } |
| 265 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 266 | /* |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 267 | * CHILD |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 268 | */ |
| 269 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 270 | close(filedes[0]); |
| 271 | |
| 272 | if (ChangeUser(user, 0) < 0) |
| 273 | exit(0); |
| 274 | |
Denis Vlasenko | 4092082 | 2006-10-03 20:28:06 +0000 | [diff] [blame] | 275 | xfunc_error_retval = 0; |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 276 | fd = xopen(file, O_RDONLY); |
| 277 | buf[0] = 0; |
| 278 | write(filedes[1], buf, 1); |
| 279 | while ((n = read(fd, buf, sizeof(buf))) > 0) { |
| 280 | write(filedes[1], buf, n); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 281 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 282 | exit(0); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 285 | static void EditFile(const char *user, const char *file) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 286 | { |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 287 | int pid = fork(); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 288 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 289 | if (pid == 0) { |
| 290 | /* |
| 291 | * CHILD - change user and run editor |
| 292 | */ |
| 293 | char *ptr; |
| 294 | char visual[1024]; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 295 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 296 | if (ChangeUser(user, 1) < 0) |
| 297 | exit(0); |
| 298 | ptr = getenv("VISUAL"); |
| 299 | if (ptr == NULL || strlen(ptr) > 256) |
| 300 | ptr = PATH_VI; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 301 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 302 | snprintf(visual, sizeof(visual), "%s %s", ptr, file); |
| 303 | execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", visual, NULL); |
| 304 | perror("exec"); |
| 305 | exit(0); |
| 306 | } |
| 307 | if (pid < 0) { |
| 308 | /* |
| 309 | * PARENT - failure |
| 310 | */ |
| 311 | bb_perror_msg_and_die("fork"); |
| 312 | } |
| 313 | wait4(pid, NULL, 0, NULL); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 316 | static int ChangeUser(const char *user, short dochdir) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 317 | { |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 318 | struct passwd *pas; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 319 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 320 | /* |
| 321 | * Obtain password entry and change privileges |
| 322 | */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 323 | |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 324 | pas = getpwnam(user); |
| 325 | if (pas == NULL) { |
| 326 | bb_perror_msg_and_die("failed to get uid for %s", user); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 327 | } |
Denis Vlasenko | 94d5d82 | 2006-09-27 19:48:56 +0000 | [diff] [blame] | 328 | setenv("USER", pas->pw_name, 1); |
| 329 | setenv("HOME", pas->pw_dir, 1); |
| 330 | setenv("SHELL", DEFAULT_SHELL, 1); |
| 331 | |
| 332 | /* |
| 333 | * Change running state to the user in question |
| 334 | */ |
| 335 | change_identity(pas); |
| 336 | |
| 337 | if (dochdir) { |
| 338 | if (chdir(pas->pw_dir) < 0) { |
| 339 | bb_perror_msg("chdir(%s) by %s failed", pas->pw_dir, user); |
| 340 | xchdir(TMPDIR); |
| 341 | } |
| 342 | } |
| 343 | return pas->pw_uid; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 344 | } |