diff options
| author | Eric Andersen <andersen@codepoet.org> | 2002-10-22 12:24:59 +0000 |
|---|---|---|
| committer | Eric Andersen <andersen@codepoet.org> | 2002-10-22 12:24:59 +0000 |
| commit | f6f7bfb8e0a257137f8c2dad83ae4ed826b4e4bb (patch) | |
| tree | 7ea00c66d341d324294df54238acff2c1795d72b /miscutils | |
| parent | 44608e9693b03661fbab5e27650bb040c6871d11 (diff) | |
| download | busybox-w32-f6f7bfb8e0a257137f8c2dad83ae4ed826b4e4bb.tar.gz busybox-w32-f6f7bfb8e0a257137f8c2dad83ae4ed826b4e4bb.tar.bz2 busybox-w32-f6f7bfb8e0a257137f8c2dad83ae4ed826b4e4bb.zip | |
last_patch63 from vodz: add in crond and crontab applets
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/Makefile.in | 2 | ||||
| -rw-r--r-- | miscutils/config.in | 2 | ||||
| -rw-r--r-- | miscutils/crond.c | 1146 | ||||
| -rw-r--r-- | miscutils/crontab.c | 393 |
4 files changed, 1543 insertions, 0 deletions
diff --git a/miscutils/Makefile.in b/miscutils/Makefile.in index 078f87001..a5e12e7ee 100644 --- a/miscutils/Makefile.in +++ b/miscutils/Makefile.in | |||
| @@ -25,6 +25,8 @@ endif | |||
| 25 | 25 | ||
| 26 | MISCUTILS-y:= | 26 | MISCUTILS-y:= |
| 27 | MISCUTILS-$(CONFIG_ADJTIMEX) += adjtimex.o | 27 | MISCUTILS-$(CONFIG_ADJTIMEX) += adjtimex.o |
| 28 | MISCUTILS-$(CONFIG_CROND) += crond.o | ||
| 29 | MISCUTILS-$(CONFIG_CRONTAB) += crontab.o | ||
| 28 | MISCUTILS-$(CONFIG_DC) += dc.o | 30 | MISCUTILS-$(CONFIG_DC) += dc.o |
| 29 | MISCUTILS-$(CONFIG_DUTMP) += dutmp.o | 31 | MISCUTILS-$(CONFIG_DUTMP) += dutmp.o |
| 30 | MISCUTILS-$(CONFIG_MAKEDEVS) += makedevs.o | 32 | MISCUTILS-$(CONFIG_MAKEDEVS) += makedevs.o |
diff --git a/miscutils/config.in b/miscutils/config.in index 20310eb6c..1d2751444 100644 --- a/miscutils/config.in +++ b/miscutils/config.in | |||
| @@ -7,6 +7,8 @@ mainmenu_option next_comment | |||
| 7 | comment 'Miscellaneous Utilities' | 7 | comment 'Miscellaneous Utilities' |
| 8 | 8 | ||
| 9 | bool 'adjtimex' CONFIG_ADJTIMEX | 9 | bool 'adjtimex' CONFIG_ADJTIMEX |
| 10 | bool 'crond' CONFIG_CROND | ||
| 11 | bool 'crontab' CONFIG_CRONTAB | ||
| 10 | bool 'dc' CONFIG_DC | 12 | bool 'dc' CONFIG_DC |
| 11 | bool 'dutmp' CONFIG_DUTMP | 13 | bool 'dutmp' CONFIG_DUTMP |
| 12 | bool 'makedevs' CONFIG_MAKEDEVS | 14 | bool 'makedevs' CONFIG_MAKEDEVS |
diff --git a/miscutils/crond.c b/miscutils/crond.c new file mode 100644 index 000000000..225d5026c --- /dev/null +++ b/miscutils/crond.c | |||
| @@ -0,0 +1,1146 @@ | |||
| 1 | /* | ||
| 2 | * crond -d[#] -c <crondir> -f -b | ||
| 3 | * | ||
| 4 | * run as root, but NOT setuid root | ||
| 5 | * | ||
| 6 | * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com) | ||
| 7 | * May be distributed under the GNU General Public License | ||
| 8 | * | ||
| 9 | * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 to be used in busybox | ||
| 10 | */ | ||
| 11 | |||
| 12 | #define VERSION "2.3.2" | ||
| 13 | |||
| 14 | #undef FEATURE_DEBUG_OPT | ||
| 15 | |||
| 16 | |||
| 17 | #include <stdio.h> | ||
| 18 | #include <stdlib.h> | ||
| 19 | #include <stdarg.h> | ||
| 20 | #include <string.h> | ||
| 21 | #include <errno.h> | ||
| 22 | #include <time.h> | ||
| 23 | #include <dirent.h> | ||
| 24 | #include <fcntl.h> | ||
| 25 | #include <pwd.h> | ||
| 26 | #include <unistd.h> | ||
| 27 | #include <grp.h> | ||
| 28 | #include <syslog.h> | ||
| 29 | #include <signal.h> | ||
| 30 | #include <getopt.h> | ||
| 31 | #include <sys/ioctl.h> | ||
| 32 | #include <sys/wait.h> | ||
| 33 | #include <sys/stat.h> | ||
| 34 | #include <sys/resource.h> | ||
| 35 | |||
| 36 | #include "busybox.h" | ||
| 37 | |||
| 38 | #define arysize(ary) (sizeof(ary)/sizeof((ary)[0])) | ||
| 39 | |||
| 40 | #ifndef CRONTABS | ||
| 41 | #define CRONTABS "/var/spool/cron/crontabs" | ||
| 42 | #endif | ||
| 43 | #ifndef TMPDIR | ||
| 44 | #define TMPDIR "/var/spool/cron" | ||
| 45 | #endif | ||
| 46 | #ifndef LOG_FILE | ||
| 47 | #define LOG_FILE "/var/log/cron" | ||
| 48 | #endif | ||
| 49 | #ifndef SENDMAIL | ||
| 50 | #define SENDMAIL "/usr/sbin/sendmail" | ||
| 51 | #endif | ||
| 52 | #ifndef SENDMAIL_ARGS | ||
| 53 | #define SENDMAIL_ARGS "-t", "-oem", "-i" | ||
| 54 | #endif | ||
| 55 | #ifndef CRONUPDATE | ||
| 56 | #define CRONUPDATE "cron.update" | ||
| 57 | #endif | ||
| 58 | #ifndef MAXLINES | ||
| 59 | #define MAXLINES 256 /* max lines in non-root crontabs */ | ||
| 60 | #endif | ||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | typedef struct CronFile { | ||
| 65 | struct CronFile *cf_Next; | ||
| 66 | struct CronLine *cf_LineBase; | ||
| 67 | char *cf_User; /* username */ | ||
| 68 | int cf_Ready; /* bool: one or more jobs ready */ | ||
| 69 | int cf_Running; /* bool: one or more jobs running */ | ||
| 70 | int cf_Deleted; /* marked for deletion, ignore */ | ||
| 71 | } CronFile; | ||
| 72 | |||
| 73 | typedef struct CronLine { | ||
| 74 | struct CronLine *cl_Next; | ||
| 75 | char *cl_Shell; /* shell command */ | ||
| 76 | int cl_Pid; /* running pid, 0, or armed (-1) */ | ||
| 77 | int cl_MailFlag; /* running pid is for mail */ | ||
| 78 | int cl_MailPos; /* 'empty file' size */ | ||
| 79 | char cl_Mins[60]; /* 0-59 */ | ||
| 80 | char cl_Hrs[24]; /* 0-23 */ | ||
| 81 | char cl_Days[32]; /* 1-31 */ | ||
| 82 | char cl_Mons[12]; /* 0-11 */ | ||
| 83 | char cl_Dow[7]; /* 0-6, beginning sunday */ | ||
| 84 | } CronLine; | ||
| 85 | |||
| 86 | #define RUN_RANOUT 1 | ||
| 87 | #define RUN_RUNNING 2 | ||
| 88 | #define RUN_FAILED 3 | ||
| 89 | |||
| 90 | #define DaemonUid 0 | ||
| 91 | |||
| 92 | #ifdef FEATURE_DEBUG_OPT | ||
| 93 | static short DebugOpt; | ||
| 94 | #endif | ||
| 95 | |||
| 96 | static short LogLevel = 8; | ||
| 97 | static short ForegroundOpt; | ||
| 98 | static short LoggerOpt; | ||
| 99 | static const char *LogFile = LOG_FILE; | ||
| 100 | static const char *CDir = CRONTABS; | ||
| 101 | |||
| 102 | static void log(int level, const char *ctl, ...); | ||
| 103 | static void log9(const char *ctl, ...); | ||
| 104 | static void startlogger(void); | ||
| 105 | |||
| 106 | static void CheckUpdates(void); | ||
| 107 | static void SynchronizeDir(void); | ||
| 108 | static int TestJobs(time_t t1, time_t t2); | ||
| 109 | static void RunJobs(void); | ||
| 110 | static int CheckJobs(void); | ||
| 111 | static void RunJob(CronFile *file, CronLine *line); | ||
| 112 | static void EndJob(const CronFile *file, CronLine *line); | ||
| 113 | |||
| 114 | static void DeleteFile(const char *userName); | ||
| 115 | |||
| 116 | static CronFile *FileBase; | ||
| 117 | |||
| 118 | |||
| 119 | int | ||
| 120 | crond_main(int ac, char **av) | ||
| 121 | { | ||
| 122 | int i; | ||
| 123 | |||
| 124 | opterr = 0; /* disable getopt 'errors' message.*/ | ||
| 125 | |||
| 126 | while ((i = getopt(ac,av, | ||
| 127 | #ifdef FEATURE_DEBUG_OPT | ||
| 128 | "d:" | ||
| 129 | #endif | ||
| 130 | "l:L:fbSc:")) != EOF){ | ||
| 131 | |||
| 132 | switch (i){ | ||
| 133 | case 'l': | ||
| 134 | LogLevel = atoi(optarg); | ||
| 135 | break; | ||
| 136 | #ifdef FEATURE_DEBUG_OPT | ||
| 137 | case 'd': | ||
| 138 | DebugOpt = atoi(optarg); | ||
| 139 | LogLevel = 0; | ||
| 140 | break; | ||
| 141 | #endif | ||
| 142 | case 'f': | ||
| 143 | ForegroundOpt = 1; | ||
| 144 | break; | ||
| 145 | case 'b': | ||
| 146 | ForegroundOpt = 0; | ||
| 147 | break; | ||
| 148 | case 'S': /* select logging to syslog */ | ||
| 149 | LoggerOpt = 0; | ||
| 150 | break; | ||
| 151 | case 'L': /* select internal file logger */ | ||
| 152 | LoggerOpt = 1; | ||
| 153 | if (*optarg != 0) LogFile = optarg; | ||
| 154 | break; | ||
| 155 | case 'c': | ||
| 156 | if (*optarg != 0) CDir = optarg; | ||
| 157 | break; | ||
| 158 | default: /* parse error */ | ||
| 159 | show_usage(); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | /* | ||
| 164 | * change directory | ||
| 165 | */ | ||
| 166 | |||
| 167 | if (chdir(CDir) != 0) | ||
| 168 | perror_msg_and_die("chdir"); | ||
| 169 | |||
| 170 | /* | ||
| 171 | * close stdin and stdout, stderr. | ||
| 172 | * close unused descriptors - don't need. | ||
| 173 | * optional detach from controlling terminal | ||
| 174 | */ | ||
| 175 | |||
| 176 | if (ForegroundOpt == 0) { | ||
| 177 | if(daemon(1, 0) < 0) | ||
| 178 | perror_msg_and_die("daemon"); | ||
| 179 | } | ||
| 180 | |||
| 181 | (void)startlogger(); /* need if syslog mode selected */ | ||
| 182 | signal(SIGHUP,SIG_IGN); /* hmm.. but, if kill -HUP original | ||
| 183 | * version - his died. ;( | ||
| 184 | */ | ||
| 185 | |||
| 186 | /* | ||
| 187 | * main loop - synchronize to 1 second after the minute, minimum sleep | ||
| 188 | * of 1 second. | ||
| 189 | */ | ||
| 190 | |||
| 191 | log(9,"%s " VERSION " dillon, started, log level %d\n", av[0], LogLevel); | ||
| 192 | |||
| 193 | SynchronizeDir(); | ||
| 194 | |||
| 195 | { | ||
| 196 | time_t t1 = time(NULL); | ||
| 197 | time_t t2; | ||
| 198 | long dt; | ||
| 199 | short rescan = 60; | ||
| 200 | short sleep_time = 60; | ||
| 201 | |||
| 202 | for (;;) { | ||
| 203 | sleep((sleep_time + 1) - (short)(time(NULL) % sleep_time)); | ||
| 204 | |||
| 205 | t2 = time(NULL); | ||
| 206 | dt = t2 - t1; | ||
| 207 | |||
| 208 | /* | ||
| 209 | * The file 'cron.update' is checked to determine new cron | ||
| 210 | * jobs. The directory is rescanned once an hour to deal | ||
| 211 | * with any screwups. | ||
| 212 | * | ||
| 213 | * check for disparity. Disparities over an hour either way | ||
| 214 | * result in resynchronization. A reverse-indexed disparity | ||
| 215 | * less then an hour causes us to effectively sleep until we | ||
| 216 | * match the original time (i.e. no re-execution of jobs that | ||
| 217 | * have just been run). A forward-indexed disparity less then | ||
| 218 | * an hour causes intermediate jobs to be run, but only once | ||
| 219 | * in the worst case. | ||
| 220 | * | ||
| 221 | * when running jobs, the inequality used is greater but not | ||
| 222 | * equal to t1, and less then or equal to t2. | ||
| 223 | */ | ||
| 224 | |||
| 225 | if (--rescan == 0) { | ||
| 226 | rescan = 60; | ||
| 227 | SynchronizeDir(); | ||
| 228 | } | ||
| 229 | CheckUpdates(); | ||
| 230 | #ifdef FEATURE_DEBUG_OPT | ||
| 231 | if (DebugOpt) | ||
| 232 | log(5, "Wakeup dt=%d\n", dt); | ||
| 233 | #endif | ||
| 234 | if (dt < -60*60 || dt > 60*60) { | ||
| 235 | t1 = t2; | ||
| 236 | log9("time disparity of %d minutes detected\n", dt / 60); | ||
| 237 | } else if (dt > 0) { | ||
| 238 | TestJobs(t1, t2); | ||
| 239 | RunJobs(); | ||
| 240 | sleep(5); | ||
| 241 | if (CheckJobs() > 0) | ||
| 242 | sleep_time = 10; | ||
| 243 | else | ||
| 244 | sleep_time = 60; | ||
| 245 | t1 = t2; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | } | ||
| 249 | /* not reached */ | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | static void | ||
| 254 | vlog(int level, int MLOG_LEVEL, const char *ctl, va_list va) | ||
| 255 | { | ||
| 256 | char buf[1024]; | ||
| 257 | int logfd; | ||
| 258 | |||
| 259 | if (level >= LogLevel) { | ||
| 260 | |||
| 261 | vsnprintf(buf,sizeof(buf), ctl, va); | ||
| 262 | #ifdef FEATURE_DEBUG_OPT | ||
| 263 | if (DebugOpt) fprintf(stderr,"%s",buf); | ||
| 264 | else | ||
| 265 | #endif | ||
| 266 | if (LoggerOpt == 0) syslog(MLOG_LEVEL, "%s", buf); | ||
| 267 | else { | ||
| 268 | if ((logfd = open(LogFile,O_WRONLY|O_CREAT|O_APPEND,600)) >= 0){ | ||
| 269 | write(logfd, buf, strlen(buf)); | ||
| 270 | close(logfd); | ||
| 271 | } else | ||
| 272 | #ifdef FEATURE_DEBUG_OPT | ||
| 273 | perror_msg("Can't open log file") | ||
| 274 | #endif | ||
| 275 | ; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | /* | ||
| 281 | set log_level=9 and log messages | ||
| 282 | */ | ||
| 283 | |||
| 284 | static void | ||
| 285 | log9(const char *ctl, ...) | ||
| 286 | { | ||
| 287 | va_list va; | ||
| 288 | |||
| 289 | va_start(va, ctl); | ||
| 290 | vlog(9, LOG_WARNING, ctl, va); | ||
| 291 | va_end(va); | ||
| 292 | } | ||
| 293 | |||
| 294 | /* | ||
| 295 | normal logger call point. | ||
| 296 | */ | ||
| 297 | |||
| 298 | static void | ||
| 299 | log(int level, const char *ctl, ...) | ||
| 300 | { | ||
| 301 | va_list va; | ||
| 302 | |||
| 303 | va_start(va, ctl); | ||
| 304 | vlog(level, LOG_NOTICE, ctl, va); | ||
| 305 | va_end(va); | ||
| 306 | } | ||
| 307 | |||
| 308 | /* | ||
| 309 | Original: void | ||
| 310 | logfd(int fd, const char *ctl, ...) | ||
| 311 | Updated to: log_error (used by jobs.c) | ||
| 312 | */ | ||
| 313 | |||
| 314 | static void | ||
| 315 | log_err(const char *ctl, ...) | ||
| 316 | { | ||
| 317 | va_list va; | ||
| 318 | |||
| 319 | va_start(va, ctl); | ||
| 320 | vlog(20, LOG_ERR, ctl, va); | ||
| 321 | va_end(va); | ||
| 322 | } | ||
| 323 | |||
| 324 | /* | ||
| 325 | used by jobs.c (write to temp file..) | ||
| 326 | */ | ||
| 327 | |||
| 328 | static void | ||
| 329 | fdprintf(int fd, const char *ctl, ...) | ||
| 330 | { | ||
| 331 | va_list va; | ||
| 332 | |||
| 333 | va_start(va, ctl); | ||
| 334 | vdprintf(fd, ctl, va); | ||
| 335 | va_end(va); | ||
| 336 | } | ||
| 337 | |||
| 338 | |||
| 339 | static int | ||
| 340 | ChangeUser(const char *user, short dochdir) | ||
| 341 | { | ||
| 342 | struct passwd *pas; | ||
| 343 | |||
| 344 | /* | ||
| 345 | * Obtain password entry and change privilages | ||
| 346 | */ | ||
| 347 | |||
| 348 | if ((pas = getpwnam(user)) == 0) { | ||
| 349 | log(9, "failed to get uid for %s", user); | ||
| 350 | return(-1); | ||
| 351 | } | ||
| 352 | setenv("USER", pas->pw_name, 1); | ||
| 353 | setenv("HOME", pas->pw_dir, 1); | ||
| 354 | setenv("SHELL", "/bin/sh", 1); | ||
| 355 | |||
| 356 | /* | ||
| 357 | * Change running state to the user in question | ||
| 358 | */ | ||
| 359 | |||
| 360 | if (initgroups(user, pas->pw_gid) < 0) { | ||
| 361 | log(9, "initgroups failed: %s %m", user); | ||
| 362 | return(-1); | ||
| 363 | } | ||
| 364 | if (setregid(pas->pw_gid, pas->pw_gid) < 0) { | ||
| 365 | log(9, "setregid failed: %s %d", user, pas->pw_gid); | ||
| 366 | return(-1); | ||
| 367 | } | ||
| 368 | if (setreuid(pas->pw_uid, pas->pw_uid) < 0) { | ||
| 369 | log(9, "setreuid failed: %s %d", user, pas->pw_uid); | ||
| 370 | return(-1); | ||
| 371 | } | ||
| 372 | if (dochdir) { | ||
| 373 | if (chdir(pas->pw_dir) < 0) { | ||
| 374 | log(8, "chdir failed: %s %s", user, pas->pw_dir); | ||
| 375 | if (chdir(TMPDIR) < 0) { | ||
| 376 | log(9, "chdir failed: %s %s", TMPDIR, user); | ||
| 377 | return(-1); | ||
| 378 | } | ||
| 379 | } | ||
| 380 | } | ||
| 381 | return(pas->pw_uid); | ||
| 382 | } | ||
| 383 | |||
| 384 | static void | ||
| 385 | startlogger(void) | ||
| 386 | { | ||
| 387 | int logfd; | ||
| 388 | |||
| 389 | if (LoggerOpt == 0) | ||
| 390 | openlog(applet_name, LOG_CONS|LOG_PID,LOG_CRON); | ||
| 391 | |||
| 392 | else { /* test logfile */ | ||
| 393 | if ((logfd = open(LogFile,O_WRONLY|O_CREAT|O_APPEND,600)) >= 0) | ||
| 394 | close(logfd); | ||
| 395 | else | ||
| 396 | #ifdef FEATURE_DEBUG_OPT | ||
| 397 | printf("Failed to open log file '%s' reason: %m", LogFile) | ||
| 398 | #endif | ||
| 399 | ; | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | |||
| 404 | static const char * const DowAry[] = { | ||
| 405 | "sun", | ||
| 406 | "mon", | ||
| 407 | "tue", | ||
| 408 | "wed", | ||
| 409 | "thu", | ||
| 410 | "fri", | ||
| 411 | "sat", | ||
| 412 | |||
| 413 | "Sun", | ||
| 414 | "Mon", | ||
| 415 | "Tue", | ||
| 416 | "Wed", | ||
| 417 | "Thu", | ||
| 418 | "Fri", | ||
| 419 | "Sat", | ||
| 420 | NULL | ||
| 421 | }; | ||
| 422 | |||
| 423 | static const char * const MonAry[] = { | ||
| 424 | "jan", | ||
| 425 | "feb", | ||
| 426 | "mar", | ||
| 427 | "apr", | ||
| 428 | "may", | ||
| 429 | "jun", | ||
| 430 | "jul", | ||
| 431 | "aug", | ||
| 432 | "sep", | ||
| 433 | "oct", | ||
| 434 | "nov", | ||
| 435 | "dec", | ||
| 436 | |||
| 437 | "Jan", | ||
| 438 | "Feb", | ||
| 439 | "Mar", | ||
| 440 | "Apr", | ||
| 441 | "May", | ||
| 442 | "Jun", | ||
| 443 | "Jul", | ||
| 444 | "Aug", | ||
| 445 | "Sep", | ||
| 446 | "Oct", | ||
| 447 | "Nov", | ||
| 448 | "Dec", | ||
| 449 | NULL | ||
| 450 | }; | ||
| 451 | |||
| 452 | static char * | ||
| 453 | ParseField(char *user, char *ary, int modvalue, int off, | ||
| 454 | const char * const *names, char *ptr) | ||
| 455 | { | ||
| 456 | char *base = ptr; | ||
| 457 | int n1 = -1; | ||
| 458 | int n2 = -1; | ||
| 459 | |||
| 460 | if (base == NULL) | ||
| 461 | return(NULL); | ||
| 462 | |||
| 463 | while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') { | ||
| 464 | int skip = 0; | ||
| 465 | |||
| 466 | /* | ||
| 467 | * Handle numeric digit or symbol or '*' | ||
| 468 | */ | ||
| 469 | |||
| 470 | if (*ptr == '*') { | ||
| 471 | n1 = 0; /* everything will be filled */ | ||
| 472 | n2 = modvalue - 1; | ||
| 473 | skip = 1; | ||
| 474 | ++ptr; | ||
| 475 | } else if (*ptr >= '0' && *ptr <= '9') { | ||
| 476 | if (n1 < 0) | ||
| 477 | n1 = strtol(ptr, &ptr, 10) + off; | ||
| 478 | else | ||
| 479 | n2 = strtol(ptr, &ptr, 10) + off; | ||
| 480 | skip = 1; | ||
| 481 | } else if (names) { | ||
| 482 | int i; | ||
| 483 | |||
| 484 | for (i = 0; names[i]; ++i) { | ||
| 485 | if (strncmp(ptr, names[i], strlen(names[i])) == 0) { | ||
| 486 | break; | ||
| 487 | } | ||
| 488 | } | ||
| 489 | if (names[i]) { | ||
| 490 | ptr += strlen(names[i]); | ||
| 491 | if (n1 < 0) | ||
| 492 | n1 = i; | ||
| 493 | else | ||
| 494 | n2 = i; | ||
| 495 | skip = 1; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | /* | ||
| 500 | * handle optional range '-' | ||
| 501 | */ | ||
| 502 | |||
| 503 | if (skip == 0) { | ||
| 504 | log9("failed user %s parsing %s\n", user, base); | ||
| 505 | return(NULL); | ||
| 506 | } | ||
| 507 | if (*ptr == '-' && n2 < 0) { | ||
| 508 | ++ptr; | ||
| 509 | continue; | ||
| 510 | } | ||
| 511 | |||
| 512 | /* | ||
| 513 | * collapse single-value ranges, handle skipmark, and fill | ||
| 514 | * in the character array appropriately. | ||
| 515 | */ | ||
| 516 | |||
| 517 | if (n2 < 0) | ||
| 518 | n2 = n1; | ||
| 519 | |||
| 520 | if (*ptr == '/') | ||
| 521 | skip = strtol(ptr + 1, &ptr, 10); | ||
| 522 | |||
| 523 | /* | ||
| 524 | * fill array, using a failsafe is the easiest way to prevent | ||
| 525 | * an endless loop | ||
| 526 | */ | ||
| 527 | |||
| 528 | { | ||
| 529 | int s0 = 1; | ||
| 530 | int failsafe = 1024; | ||
| 531 | |||
| 532 | --n1; | ||
| 533 | do { | ||
| 534 | n1 = (n1 + 1) % modvalue; | ||
| 535 | |||
| 536 | if (--s0 == 0) { | ||
| 537 | ary[n1 % modvalue] = 1; | ||
| 538 | s0 = skip; | ||
| 539 | } | ||
| 540 | } while (n1 != n2 && --failsafe); | ||
| 541 | |||
| 542 | if (failsafe == 0) { | ||
| 543 | log9("failed user %s parsing %s\n", user, base); | ||
| 544 | return(NULL); | ||
| 545 | } | ||
| 546 | } | ||
| 547 | if (*ptr != ',') | ||
| 548 | break; | ||
| 549 | ++ptr; | ||
| 550 | n1 = -1; | ||
| 551 | n2 = -1; | ||
| 552 | } | ||
| 553 | |||
| 554 | if (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') { | ||
| 555 | log9("failed user %s parsing %s\n", user, base); | ||
| 556 | return(NULL); | ||
| 557 | } | ||
| 558 | |||
| 559 | while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') | ||
| 560 | ++ptr; | ||
| 561 | |||
| 562 | #ifdef FEATURE_DEBUG_OPT | ||
| 563 | if (DebugOpt) { | ||
| 564 | int i; | ||
| 565 | |||
| 566 | for (i = 0; i < modvalue; ++i) | ||
| 567 | log(5, "%d", ary[i]); | ||
| 568 | log(5, "\n"); | ||
| 569 | } | ||
| 570 | #endif | ||
| 571 | |||
| 572 | return(ptr); | ||
| 573 | } | ||
| 574 | |||
| 575 | static void | ||
| 576 | FixDayDow(CronLine *line) | ||
| 577 | { | ||
| 578 | short i; | ||
| 579 | short weekUsed = 0; | ||
| 580 | short daysUsed = 0; | ||
| 581 | |||
| 582 | for (i = 0; i < arysize(line->cl_Dow); ++i) { | ||
| 583 | if (line->cl_Dow[i] == 0) { | ||
| 584 | weekUsed = 1; | ||
| 585 | break; | ||
| 586 | } | ||
| 587 | } | ||
| 588 | for (i = 0; i < arysize(line->cl_Days); ++i) { | ||
| 589 | if (line->cl_Days[i] == 0) { | ||
| 590 | daysUsed = 1; | ||
| 591 | break; | ||
| 592 | } | ||
| 593 | } | ||
| 594 | if (weekUsed && !daysUsed) { | ||
| 595 | memset(line->cl_Days, 0, sizeof(line->cl_Days)); | ||
| 596 | } | ||
| 597 | if (daysUsed && !weekUsed) { | ||
| 598 | memset(line->cl_Dow, 0, sizeof(line->cl_Dow)); | ||
| 599 | } | ||
| 600 | } | ||
| 601 | |||
| 602 | |||
| 603 | |||
| 604 | static void | ||
| 605 | SynchronizeFile(const char *fileName) | ||
| 606 | { | ||
| 607 | int maxEntries = MAXLINES; | ||
| 608 | int maxLines; | ||
| 609 | char buf[1024]; | ||
| 610 | |||
| 611 | if (strcmp(fileName, "root") == 0) | ||
| 612 | maxEntries = 65535; | ||
| 613 | maxLines = maxEntries * 10; | ||
| 614 | |||
| 615 | if (fileName) { | ||
| 616 | FILE *fi; | ||
| 617 | |||
| 618 | DeleteFile(fileName); | ||
| 619 | |||
| 620 | if ((fi = fopen(fileName, "r")) != NULL) { | ||
| 621 | struct stat sbuf; | ||
| 622 | |||
| 623 | if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) { | ||
| 624 | CronFile *file = calloc(1, sizeof(CronFile)); | ||
| 625 | CronLine **pline; | ||
| 626 | |||
| 627 | file->cf_User = strdup(fileName); | ||
| 628 | pline = &file->cf_LineBase; | ||
| 629 | |||
| 630 | while (fgets(buf, sizeof(buf), fi) != NULL && --maxLines) { | ||
| 631 | CronLine line; | ||
| 632 | char *ptr; | ||
| 633 | |||
| 634 | if (buf[0]) | ||
| 635 | buf[strlen(buf)-1] = 0; | ||
| 636 | |||
| 637 | if (buf[0] == 0 || buf[0] == '#' || buf[0] == ' ' || buf[0] == '\t') | ||
| 638 | continue; | ||
| 639 | |||
| 640 | if (--maxEntries == 0) | ||
| 641 | break; | ||
| 642 | |||
| 643 | memset(&line, 0, sizeof(line)); | ||
| 644 | |||
| 645 | #ifdef FEATURE_DEBUG_OPT | ||
| 646 | if (DebugOpt) | ||
| 647 | log9("User %s Entry %s\n", fileName, buf); | ||
| 648 | #endif | ||
| 649 | |||
| 650 | /* | ||
| 651 | * parse date ranges | ||
| 652 | */ | ||
| 653 | |||
| 654 | ptr = ParseField(file->cf_User, line.cl_Mins, 60, 0, NULL, buf); | ||
| 655 | ptr = ParseField(file->cf_User, line.cl_Hrs, 24, 0, NULL, ptr); | ||
| 656 | ptr = ParseField(file->cf_User, line.cl_Days, 32, 0, NULL, ptr); | ||
| 657 | ptr = ParseField(file->cf_User, line.cl_Mons, 12, -1, MonAry, ptr); | ||
| 658 | ptr = ParseField(file->cf_User, line.cl_Dow, 7, 0, DowAry, ptr); | ||
| 659 | |||
| 660 | /* | ||
| 661 | * check failure | ||
| 662 | */ | ||
| 663 | |||
| 664 | if (ptr == NULL) | ||
| 665 | continue; | ||
| 666 | |||
| 667 | /* | ||
| 668 | * fix days and dow - if one is not * and the other | ||
| 669 | * is *, the other is set to 0, and vise-versa | ||
| 670 | */ | ||
| 671 | |||
| 672 | FixDayDow(&line); | ||
| 673 | |||
| 674 | *pline = calloc(1, sizeof(CronLine)); | ||
| 675 | **pline = line; | ||
| 676 | |||
| 677 | /* | ||
| 678 | * copy command | ||
| 679 | */ | ||
| 680 | |||
| 681 | (*pline)->cl_Shell = strdup(ptr); | ||
| 682 | |||
| 683 | #ifdef FEATURE_DEBUG_OPT | ||
| 684 | if (DebugOpt) { | ||
| 685 | log9(" Command %s\n", ptr); | ||
| 686 | } | ||
| 687 | #endif | ||
| 688 | |||
| 689 | pline = &((*pline)->cl_Next); | ||
| 690 | } | ||
| 691 | *pline = NULL; | ||
| 692 | |||
| 693 | file->cf_Next = FileBase; | ||
| 694 | FileBase = file; | ||
| 695 | |||
| 696 | if (maxLines == 0 || maxEntries == 0) | ||
| 697 | log9("Maximum number of lines reached for user %s\n", fileName); | ||
| 698 | } | ||
| 699 | fclose(fi); | ||
| 700 | } | ||
| 701 | } | ||
| 702 | } | ||
| 703 | |||
| 704 | static void | ||
| 705 | CheckUpdates(void) | ||
| 706 | { | ||
| 707 | FILE *fi; | ||
| 708 | char buf[256]; | ||
| 709 | |||
| 710 | if ((fi = fopen(CRONUPDATE, "r")) != NULL) { | ||
| 711 | remove(CRONUPDATE); | ||
| 712 | while (fgets(buf, sizeof(buf), fi) != NULL) { | ||
| 713 | SynchronizeFile(strtok(buf, " \t\r\n")); | ||
| 714 | } | ||
| 715 | fclose(fi); | ||
| 716 | } | ||
| 717 | } | ||
| 718 | |||
| 719 | static void | ||
| 720 | SynchronizeDir(void) | ||
| 721 | { | ||
| 722 | /* | ||
| 723 | * Attempt to delete the database. Note that we have to make a copy | ||
| 724 | * of the string | ||
| 725 | */ | ||
| 726 | |||
| 727 | for (;;) { | ||
| 728 | CronFile *file; | ||
| 729 | char *user; | ||
| 730 | |||
| 731 | for (file = FileBase; file && file->cf_Deleted; file = file->cf_Next) | ||
| 732 | ; | ||
| 733 | if (file == NULL) | ||
| 734 | break; | ||
| 735 | user = strdup(file->cf_User); | ||
| 736 | DeleteFile(user); | ||
| 737 | free(user); | ||
| 738 | } | ||
| 739 | |||
| 740 | /* | ||
| 741 | * Remove cron update file | ||
| 742 | * | ||
| 743 | * Re-chdir, in case directory was renamed & deleted, or otherwise | ||
| 744 | * screwed up. | ||
| 745 | * | ||
| 746 | * scan directory and add associated users | ||
| 747 | */ | ||
| 748 | |||
| 749 | remove(CRONUPDATE); | ||
| 750 | if (chdir(CDir) < 0) { | ||
| 751 | log9("unable to find %s\n", CDir); | ||
| 752 | exit(20); | ||
| 753 | } | ||
| 754 | { | ||
| 755 | DIR *dir; | ||
| 756 | struct dirent *den; | ||
| 757 | |||
| 758 | if ((dir = opendir("."))) { | ||
| 759 | while ((den = readdir(dir))) { | ||
| 760 | if (strchr(den->d_name, '.') != NULL) | ||
| 761 | continue; | ||
| 762 | if (getpwnam(den->d_name)) | ||
| 763 | SynchronizeFile(den->d_name); | ||
| 764 | else | ||
| 765 | log(7, "ignoring %s\n", den->d_name); | ||
| 766 | } | ||
| 767 | closedir(dir); | ||
| 768 | } else { | ||
| 769 | log9("Unable to open current dir!\n"); | ||
| 770 | exit(20); | ||
| 771 | } | ||
| 772 | } | ||
| 773 | } | ||
| 774 | |||
| 775 | |||
| 776 | /* | ||
| 777 | * DeleteFile() - delete user database | ||
| 778 | * | ||
| 779 | * Note: multiple entries for same user may exist if we were unable to | ||
| 780 | * completely delete a database due to running processes. | ||
| 781 | */ | ||
| 782 | |||
| 783 | static void | ||
| 784 | DeleteFile(const char *userName) | ||
| 785 | { | ||
| 786 | CronFile **pfile = &FileBase; | ||
| 787 | CronFile *file; | ||
| 788 | |||
| 789 | while ((file = *pfile) != NULL) { | ||
| 790 | if (strcmp(userName, file->cf_User) == 0) { | ||
| 791 | CronLine **pline = &file->cf_LineBase; | ||
| 792 | CronLine *line; | ||
| 793 | |||
| 794 | file->cf_Running = 0; | ||
| 795 | file->cf_Deleted = 1; | ||
| 796 | |||
| 797 | while ((line = *pline) != NULL) { | ||
| 798 | if (line->cl_Pid > 0) { | ||
| 799 | file->cf_Running = 1; | ||
| 800 | pline = &line->cl_Next; | ||
| 801 | } else { | ||
| 802 | *pline = line->cl_Next; | ||
| 803 | free(line->cl_Shell); | ||
| 804 | free(line); | ||
| 805 | } | ||
| 806 | } | ||
| 807 | if (file->cf_Running == 0) { | ||
| 808 | *pfile = file->cf_Next; | ||
| 809 | free(file->cf_User); | ||
| 810 | free(file); | ||
| 811 | } else { | ||
| 812 | pfile = &file->cf_Next; | ||
| 813 | } | ||
| 814 | } else { | ||
| 815 | pfile = &file->cf_Next; | ||
| 816 | } | ||
| 817 | } | ||
| 818 | } | ||
| 819 | |||
| 820 | /* | ||
| 821 | * TestJobs() | ||
| 822 | * | ||
| 823 | * determine which jobs need to be run. Under normal conditions, the | ||
| 824 | * period is about a minute (one scan). Worst case it will be one | ||
| 825 | * hour (60 scans). | ||
| 826 | */ | ||
| 827 | |||
| 828 | static int | ||
| 829 | TestJobs(time_t t1, time_t t2) | ||
| 830 | { | ||
| 831 | short nJobs = 0; | ||
| 832 | time_t t; | ||
| 833 | |||
| 834 | /* | ||
| 835 | * Find jobs > t1 and <= t2 | ||
| 836 | */ | ||
| 837 | |||
| 838 | for (t = t1 - t1 % 60; t <= t2; t += 60) { | ||
| 839 | if (t > t1) { | ||
| 840 | struct tm *tp = localtime(&t); | ||
| 841 | CronFile *file; | ||
| 842 | CronLine *line; | ||
| 843 | |||
| 844 | for (file = FileBase; file; file = file->cf_Next) { | ||
| 845 | #ifdef FEATURE_DEBUG_OPT | ||
| 846 | if (DebugOpt) | ||
| 847 | log(5, "FILE %s:\n", file->cf_User); | ||
| 848 | #endif | ||
| 849 | if (file->cf_Deleted) | ||
| 850 | continue; | ||
| 851 | for (line = file->cf_LineBase; line; line = line->cl_Next) { | ||
| 852 | #ifdef FEATURE_DEBUG_OPT | ||
| 853 | if (DebugOpt) | ||
| 854 | log(5, " LINE %s\n", line->cl_Shell); | ||
| 855 | #endif | ||
| 856 | if (line->cl_Mins[tp->tm_min] && | ||
| 857 | line->cl_Hrs[tp->tm_hour] && | ||
| 858 | (line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday]) && | ||
| 859 | line->cl_Mons[tp->tm_mon] | ||
| 860 | ) { | ||
| 861 | #ifdef FEATURE_DEBUG_OPT | ||
| 862 | if (DebugOpt) | ||
| 863 | log(5, " JobToDo: %d %s\n", line->cl_Pid, line->cl_Shell); | ||
| 864 | #endif | ||
| 865 | if (line->cl_Pid > 0) { | ||
| 866 | log(8, " process already running: %s %s\n", | ||
| 867 | file->cf_User, | ||
| 868 | line->cl_Shell | ||
| 869 | ); | ||
| 870 | } else if (line->cl_Pid == 0) { | ||
| 871 | line->cl_Pid = -1; | ||
| 872 | file->cf_Ready = 1; | ||
| 873 | ++nJobs; | ||
| 874 | } | ||
| 875 | } | ||
| 876 | } | ||
| 877 | } | ||
| 878 | } | ||
| 879 | } | ||
| 880 | return(nJobs); | ||
| 881 | } | ||
| 882 | |||
| 883 | static void | ||
| 884 | RunJobs(void) | ||
| 885 | { | ||
| 886 | CronFile *file; | ||
| 887 | CronLine *line; | ||
| 888 | |||
| 889 | for (file = FileBase; file; file = file->cf_Next) { | ||
| 890 | if (file->cf_Ready) { | ||
| 891 | file->cf_Ready = 0; | ||
| 892 | |||
| 893 | for (line = file->cf_LineBase; line; line = line->cl_Next) { | ||
| 894 | if (line->cl_Pid < 0) { | ||
| 895 | |||
| 896 | RunJob(file, line); | ||
| 897 | |||
| 898 | log(8, "USER %s pid %3d cmd %s\n", | ||
| 899 | file->cf_User, | ||
| 900 | line->cl_Pid, | ||
| 901 | line->cl_Shell | ||
| 902 | ); | ||
| 903 | if (line->cl_Pid < 0) | ||
| 904 | file->cf_Ready = 1; | ||
| 905 | else if (line->cl_Pid > 0) | ||
| 906 | file->cf_Running = 1; | ||
| 907 | } | ||
| 908 | } | ||
| 909 | } | ||
| 910 | } | ||
| 911 | } | ||
| 912 | |||
| 913 | /* | ||
| 914 | * CheckJobs() - check for job completion | ||
| 915 | * | ||
| 916 | * Check for job completion, return number of jobs still running after | ||
| 917 | * all done. | ||
| 918 | */ | ||
| 919 | |||
| 920 | static int | ||
| 921 | CheckJobs(void) | ||
| 922 | { | ||
| 923 | CronFile *file; | ||
| 924 | CronLine *line; | ||
| 925 | int nStillRunning = 0; | ||
| 926 | |||
| 927 | for (file = FileBase; file; file = file->cf_Next) { | ||
| 928 | if (file->cf_Running) { | ||
| 929 | file->cf_Running = 0; | ||
| 930 | |||
| 931 | for (line = file->cf_LineBase; line; line = line->cl_Next) { | ||
| 932 | if (line->cl_Pid > 0) { | ||
| 933 | int status; | ||
| 934 | int r = wait4(line->cl_Pid, &status, WNOHANG, NULL); | ||
| 935 | |||
| 936 | if (r < 0 || r == line->cl_Pid) { | ||
| 937 | EndJob(file, line); | ||
| 938 | if (line->cl_Pid) | ||
| 939 | file->cf_Running = 1; | ||
| 940 | } else if (r == 0) { | ||
| 941 | file->cf_Running = 1; | ||
| 942 | } | ||
| 943 | } | ||
| 944 | } | ||
| 945 | } | ||
| 946 | nStillRunning += file->cf_Running; | ||
| 947 | } | ||
| 948 | return(nStillRunning); | ||
| 949 | } | ||
| 950 | |||
| 951 | |||
| 952 | |||
| 953 | static void | ||
| 954 | RunJob(CronFile *file, CronLine *line) | ||
| 955 | { | ||
| 956 | char mailFile[128]; | ||
| 957 | int mailFd; | ||
| 958 | |||
| 959 | line->cl_Pid = 0; | ||
| 960 | line->cl_MailFlag = 0; | ||
| 961 | |||
| 962 | /* | ||
| 963 | * open mail file - owner root so nobody can screw with it. | ||
| 964 | */ | ||
| 965 | |||
| 966 | snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", | ||
| 967 | file->cf_User, getpid()); | ||
| 968 | mailFd = open(mailFile, O_CREAT|O_TRUNC|O_WRONLY|O_EXCL|O_APPEND, 0600); | ||
| 969 | |||
| 970 | if (mailFd >= 0) { | ||
| 971 | line->cl_MailFlag = 1; | ||
| 972 | fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", | ||
| 973 | file->cf_User, | ||
| 974 | line->cl_Shell | ||
| 975 | ); | ||
| 976 | line->cl_MailPos = lseek(mailFd, 0, 1); | ||
| 977 | } | ||
| 978 | |||
| 979 | /* | ||
| 980 | * Fork as the user in question and run program | ||
| 981 | */ | ||
| 982 | |||
| 983 | if ((line->cl_Pid = fork()) == 0) { | ||
| 984 | /* | ||
| 985 | * CHILD, FORK OK | ||
| 986 | */ | ||
| 987 | |||
| 988 | /* | ||
| 989 | * Change running state to the user in question | ||
| 990 | */ | ||
| 991 | |||
| 992 | if (ChangeUser(file->cf_User, 1) < 0) | ||
| 993 | return; | ||
| 994 | |||
| 995 | #ifdef FEATURE_DEBUG_OPT | ||
| 996 | if (DebugOpt) | ||
| 997 | log(5, "Child Running %s\n", line->cl_Shell); | ||
| 998 | #endif | ||
| 999 | |||
| 1000 | /* | ||
| 1001 | * stdin is already /dev/null, setup stdout and stderr | ||
| 1002 | */ | ||
| 1003 | |||
| 1004 | if (mailFd >= 0) { | ||
| 1005 | dup2(mailFd, 1); | ||
| 1006 | dup2(mailFd, 2); | ||
| 1007 | close(mailFd); | ||
| 1008 | } else { | ||
| 1009 | log_err("unable to create mail file user %s file %s, output to /dev/null\n", | ||
| 1010 | file->cf_User, | ||
| 1011 | mailFile | ||
| 1012 | ); | ||
| 1013 | } | ||
| 1014 | execl("/bin/sh", "/bin/sh", "-c", line->cl_Shell, NULL, NULL); | ||
| 1015 | log_err("unable to exec, user %s cmd /bin/sh -c %s\n", | ||
| 1016 | file->cf_User, | ||
| 1017 | line->cl_Shell | ||
| 1018 | ); | ||
| 1019 | fdprintf(1, "Exec failed: /bin/sh -c %s\n", line->cl_Shell); | ||
| 1020 | exit(0); | ||
| 1021 | } else if (line->cl_Pid < 0) { | ||
| 1022 | /* | ||
| 1023 | * PARENT, FORK FAILED | ||
| 1024 | */ | ||
| 1025 | log_err("couldn't fork, user %s\n", file->cf_User); | ||
| 1026 | line->cl_Pid = 0; | ||
| 1027 | remove(mailFile); | ||
| 1028 | } else { | ||
| 1029 | /* | ||
| 1030 | * PARENT, FORK SUCCESS | ||
| 1031 | * | ||
| 1032 | * rename mail-file based on pid of process | ||
| 1033 | */ | ||
| 1034 | char mailFile2[128]; | ||
| 1035 | |||
| 1036 | snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d", | ||
| 1037 | file->cf_User, line->cl_Pid); | ||
| 1038 | rename(mailFile, mailFile2); | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | /* | ||
| 1042 | * Close the mail file descriptor.. we can't just leave it open in | ||
| 1043 | * a structure, closing it later, because we might run out of descriptors | ||
| 1044 | */ | ||
| 1045 | |||
| 1046 | if (mailFd >= 0) | ||
| 1047 | close(mailFd); | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | /* | ||
| 1051 | * EndJob - called when job terminates and when mail terminates | ||
| 1052 | */ | ||
| 1053 | |||
| 1054 | static void | ||
| 1055 | EndJob(const CronFile *file, CronLine *line) | ||
| 1056 | { | ||
| 1057 | int mailFd; | ||
| 1058 | char mailFile[128]; | ||
| 1059 | struct stat sbuf; | ||
| 1060 | |||
| 1061 | /* | ||
| 1062 | * No job | ||
| 1063 | */ | ||
| 1064 | |||
| 1065 | if (line->cl_Pid <= 0) { | ||
| 1066 | line->cl_Pid = 0; | ||
| 1067 | return; | ||
| 1068 | } | ||
| 1069 | |||
| 1070 | /* | ||
| 1071 | * End of job and no mail file | ||
| 1072 | * End of sendmail job | ||
| 1073 | */ | ||
| 1074 | |||
| 1075 | snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", | ||
| 1076 | file->cf_User, line->cl_Pid); | ||
| 1077 | line->cl_Pid = 0; | ||
| 1078 | |||
| 1079 | if (line->cl_MailFlag != 1) | ||
| 1080 | return; | ||
| 1081 | |||
| 1082 | line->cl_MailFlag = 0; | ||
| 1083 | |||
| 1084 | /* | ||
| 1085 | * End of primary job - check for mail file. If size has increased and | ||
| 1086 | * the file is still valid, we sendmail it. | ||
| 1087 | */ | ||
| 1088 | |||
| 1089 | mailFd = open(mailFile, O_RDONLY); | ||
| 1090 | remove(mailFile); | ||
| 1091 | if (mailFd < 0) { | ||
| 1092 | return; | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | if (fstat(mailFd, &sbuf) < 0 || | ||
| 1096 | sbuf.st_uid != DaemonUid || | ||
| 1097 | sbuf.st_nlink != 0 || | ||
| 1098 | sbuf.st_size == line->cl_MailPos || | ||
| 1099 | !S_ISREG(sbuf.st_mode) | ||
| 1100 | ) { | ||
| 1101 | close(mailFd); | ||
| 1102 | return; | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | if ((line->cl_Pid = fork()) == 0) { | ||
| 1106 | /* | ||
| 1107 | * CHILD, FORK OK | ||
| 1108 | */ | ||
| 1109 | |||
| 1110 | /* | ||
| 1111 | * change user id - no way in hell security can be compromised | ||
| 1112 | * by the mailing and we already verified the mail file. | ||
| 1113 | */ | ||
| 1114 | |||
| 1115 | if (ChangeUser(file->cf_User, 1) < 0) | ||
| 1116 | exit(0); | ||
| 1117 | |||
| 1118 | /* | ||
| 1119 | * run sendmail with mail file as standard input, only if | ||
| 1120 | * mail file exists! | ||
| 1121 | */ | ||
| 1122 | |||
| 1123 | dup2(mailFd, 0); | ||
| 1124 | dup2(1, 2); | ||
| 1125 | close(mailFd); | ||
| 1126 | |||
| 1127 | execl(SENDMAIL, SENDMAIL, SENDMAIL_ARGS, NULL, NULL); | ||
| 1128 | log_err("unable to exec %s %s, user %s, output to sink null", | ||
| 1129 | SENDMAIL, | ||
| 1130 | SENDMAIL_ARGS, | ||
| 1131 | file->cf_User | ||
| 1132 | ); | ||
| 1133 | exit(0); | ||
| 1134 | } else if (line->cl_Pid < 0) { | ||
| 1135 | /* | ||
| 1136 | * PARENT, FORK FAILED | ||
| 1137 | */ | ||
| 1138 | log_err("unable to fork, user %s", file->cf_User); | ||
| 1139 | line->cl_Pid = 0; | ||
| 1140 | } else { | ||
| 1141 | /* | ||
| 1142 | * PARENT, FORK OK | ||
| 1143 | */ | ||
| 1144 | } | ||
| 1145 | close(mailFd); | ||
| 1146 | } | ||
diff --git a/miscutils/crontab.c b/miscutils/crontab.c new file mode 100644 index 000000000..c86990653 --- /dev/null +++ b/miscutils/crontab.c | |||
| @@ -0,0 +1,393 @@ | |||
| 1 | /* | ||
| 2 | * CRONTAB | ||
| 3 | * | ||
| 4 | * usually setuid root, -c option only works if getuid() == geteuid() | ||
| 5 | * | ||
| 6 | * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com) | ||
| 7 | * May be distributed under the GNU General Public License | ||
| 8 | * | ||
| 9 | * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 to be used in busybox | ||
| 10 | * | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include <stdio.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include <stdarg.h> | ||
| 16 | #include <string.h> | ||
| 17 | #include <errno.h> | ||
| 18 | #include <time.h> | ||
| 19 | #include <dirent.h> | ||
| 20 | #include <fcntl.h> | ||
| 21 | #include <pwd.h> | ||
| 22 | #include <unistd.h> | ||
| 23 | #include <grp.h> | ||
| 24 | #include <syslog.h> | ||
| 25 | #include <signal.h> | ||
| 26 | #include <getopt.h> | ||
| 27 | #include <sys/ioctl.h> | ||
| 28 | #include <sys/wait.h> | ||
| 29 | #include <sys/stat.h> | ||
| 30 | #include <sys/resource.h> | ||
| 31 | |||
| 32 | #ifndef CRONTABS | ||
| 33 | #define CRONTABS "/var/spool/cron/crontabs" | ||
| 34 | #endif | ||
| 35 | #ifndef TMPDIR | ||
| 36 | #define TMPDIR "/var/spool/cron" | ||
| 37 | #endif | ||
| 38 | #ifndef CRONUPDATE | ||
| 39 | #define CRONUPDATE "cron.update" | ||
| 40 | #endif | ||
| 41 | #ifndef PATH_VI | ||
| 42 | #define PATH_VI "/usr/bin/vi" /* location of vi */ | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #include "busybox.h" | ||
| 46 | |||
| 47 | static const char *CDir = CRONTABS; | ||
| 48 | |||
| 49 | static void EditFile(const char *user, const char *file); | ||
| 50 | static int GetReplaceStream(const char *user, const char *file); | ||
| 51 | static int ChangeUser(const char *user, short dochdir); | ||
| 52 | |||
| 53 | int | ||
| 54 | crontab_main(int ac, char **av) | ||
| 55 | { | ||
| 56 | enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE; | ||
| 57 | const struct passwd *pas; | ||
| 58 | const char *repFile = NULL; | ||
| 59 | int repFd = 0; | ||
| 60 | int i; | ||
| 61 | char caller[256]; /* user that ran program */ | ||
| 62 | int UserId; | ||
| 63 | |||
| 64 | UserId = getuid(); | ||
| 65 | if ((pas = getpwuid(UserId)) == NULL) | ||
| 66 | perror_msg_and_die("getpwuid"); | ||
| 67 | |||
| 68 | strncpy(caller, pas->pw_name, sizeof(caller)); | ||
| 69 | |||
| 70 | i = 1; | ||
| 71 | if (ac > 1) { | ||
| 72 | if (av[1][0] == '-' && av[1][1] == 0) { | ||
| 73 | option = REPLACE; | ||
| 74 | ++i; | ||
| 75 | } else if (av[1][0] != '-') { | ||
| 76 | option = REPLACE; | ||
| 77 | ++i; | ||
| 78 | repFile = av[1]; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | for (; i < ac; ++i) { | ||
| 83 | char *ptr = av[i]; | ||
| 84 | |||
| 85 | if (*ptr != '-') | ||
| 86 | break; | ||
| 87 | ptr += 2; | ||
| 88 | |||
| 89 | switch(ptr[-1]) { | ||
| 90 | case 'l': | ||
| 91 | if (ptr[-1] == 'l') | ||
| 92 | option = LIST; | ||
| 93 | /* fall through */ | ||
| 94 | case 'e': | ||
| 95 | if (ptr[-1] == 'e') | ||
| 96 | option = EDIT; | ||
| 97 | /* fall through */ | ||
| 98 | case 'd': | ||
| 99 | if (ptr[-1] == 'd') | ||
| 100 | option = DELETE; | ||
| 101 | /* fall through */ | ||
| 102 | case 'u': | ||
| 103 | if (i + 1 < ac && av[i+1][0] != '-') { | ||
| 104 | ++i; | ||
| 105 | if (getuid() == geteuid()) { | ||
| 106 | pas = getpwnam(av[i]); | ||
| 107 | if (pas) { | ||
| 108 | UserId = pas->pw_uid; | ||
| 109 | } else { | ||
| 110 | error_msg_and_die("user %s unknown", av[i]); | ||
| 111 | } | ||
| 112 | } else { | ||
| 113 | error_msg_and_die("only the superuser may specify a user"); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | break; | ||
| 117 | case 'c': | ||
| 118 | if (getuid() == geteuid()) { | ||
| 119 | CDir = (*ptr) ? ptr : av[++i]; | ||
| 120 | } else { | ||
| 121 | error_msg_and_die("-c option: superuser only"); | ||
| 122 | } | ||
| 123 | break; | ||
| 124 | default: | ||
| 125 | i = ac; | ||
| 126 | break; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | if (i != ac || option == NONE) | ||
| 130 | show_usage(); | ||
| 131 | |||
| 132 | /* | ||
| 133 | * Get password entry | ||
| 134 | */ | ||
| 135 | |||
| 136 | if ((pas = getpwuid(UserId)) == NULL) | ||
| 137 | perror_msg_and_die("getpwuid"); | ||
| 138 | |||
| 139 | /* | ||
| 140 | * If there is a replacement file, obtain a secure descriptor to it. | ||
| 141 | */ | ||
| 142 | |||
| 143 | if (repFile) { | ||
| 144 | repFd = GetReplaceStream(caller, repFile); | ||
| 145 | if (repFd < 0) | ||
| 146 | error_msg_and_die("unable to read replacement file"); | ||
| 147 | } | ||
| 148 | |||
| 149 | /* | ||
| 150 | * Change directory to our crontab directory | ||
| 151 | */ | ||
| 152 | |||
| 153 | if (chdir(CDir) < 0) | ||
| 154 | perror_msg_and_die("cannot change dir to %s", CDir); | ||
| 155 | |||
| 156 | /* | ||
| 157 | * Handle options as appropriate | ||
| 158 | */ | ||
| 159 | |||
| 160 | switch(option) { | ||
| 161 | case LIST: | ||
| 162 | { | ||
| 163 | FILE *fi; | ||
| 164 | char buf[1024]; | ||
| 165 | |||
| 166 | if ((fi = fopen(pas->pw_name, "r"))) { | ||
| 167 | while (fgets(buf, sizeof(buf), fi) != NULL) | ||
| 168 | fputs(buf, stdout); | ||
| 169 | fclose(fi); | ||
| 170 | } else { | ||
| 171 | error_msg("no crontab for %s", pas->pw_name); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | break; | ||
| 175 | case EDIT: | ||
| 176 | { | ||
| 177 | FILE *fi; | ||
| 178 | int fd; | ||
| 179 | int n; | ||
| 180 | char tmp[128]; | ||
| 181 | char buf[1024]; | ||
| 182 | |||
| 183 | snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid()); | ||
| 184 | if ((fd = open(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600)) >= 0) { | ||
| 185 | chown(tmp, getuid(), getgid()); | ||
| 186 | if ((fi = fopen(pas->pw_name, "r"))) { | ||
| 187 | while ((n = fread(buf, 1, sizeof(buf), fi)) > 0) | ||
| 188 | write(fd, buf, n); | ||
| 189 | } | ||
| 190 | EditFile(caller, tmp); | ||
| 191 | remove(tmp); | ||
| 192 | lseek(fd, 0L, 0); | ||
| 193 | repFd = fd; | ||
| 194 | } else { | ||
| 195 | error_msg_and_die("unable to create %s", tmp); | ||
| 196 | } | ||
| 197 | |||
| 198 | } | ||
| 199 | option = REPLACE; | ||
| 200 | /* fall through */ | ||
| 201 | case REPLACE: | ||
| 202 | { | ||
| 203 | char buf[1024]; | ||
| 204 | char path[1024]; | ||
| 205 | int fd; | ||
| 206 | int n; | ||
| 207 | |||
| 208 | snprintf(path, sizeof(path), "%s.new", pas->pw_name); | ||
| 209 | if ((fd = open(path, O_CREAT|O_TRUNC|O_EXCL|O_APPEND|O_WRONLY, 0600)) >= 0) { | ||
| 210 | while ((n = read(repFd, buf, sizeof(buf))) > 0) { | ||
| 211 | write(fd, buf, n); | ||
| 212 | } | ||
| 213 | close(fd); | ||
| 214 | rename(path, pas->pw_name); | ||
| 215 | } else { | ||
| 216 | error_msg("unable to create %s/%s", CDir, buf); | ||
| 217 | } | ||
| 218 | close(repFd); | ||
| 219 | } | ||
| 220 | break; | ||
| 221 | case DELETE: | ||
| 222 | remove(pas->pw_name); | ||
| 223 | break; | ||
| 224 | case NONE: | ||
| 225 | default: | ||
| 226 | break; | ||
| 227 | } | ||
| 228 | |||
| 229 | /* | ||
| 230 | * Bump notification file. Handle window where crond picks file up | ||
| 231 | * before we can write our entry out. | ||
| 232 | */ | ||
| 233 | |||
| 234 | if (option == REPLACE || option == DELETE) { | ||
| 235 | FILE *fo; | ||
| 236 | struct stat st; | ||
| 237 | |||
| 238 | while ((fo = fopen(CRONUPDATE, "a"))) { | ||
| 239 | fprintf(fo, "%s\n", pas->pw_name); | ||
| 240 | fflush(fo); | ||
| 241 | if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) { | ||
| 242 | fclose(fo); | ||
| 243 | break; | ||
| 244 | } | ||
| 245 | fclose(fo); | ||
| 246 | /* loop */ | ||
| 247 | } | ||
| 248 | if (fo == NULL) { | ||
| 249 | error_msg("unable to append to %s/%s", CDir, CRONUPDATE); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | return 0; | ||
| 253 | } | ||
| 254 | |||
| 255 | static int | ||
| 256 | GetReplaceStream(const char *user, const char *file) | ||
| 257 | { | ||
| 258 | int filedes[2]; | ||
| 259 | int pid; | ||
| 260 | int fd; | ||
| 261 | int n; | ||
| 262 | char buf[1024]; | ||
| 263 | |||
| 264 | if (pipe(filedes) < 0) { | ||
| 265 | perror("pipe"); | ||
| 266 | return(-1); | ||
| 267 | } | ||
| 268 | if ((pid = fork()) < 0) { | ||
| 269 | perror("fork"); | ||
| 270 | return(-1); | ||
| 271 | } | ||
| 272 | if (pid > 0) { | ||
| 273 | /* | ||
| 274 | * PARENT | ||
| 275 | */ | ||
| 276 | |||
| 277 | close(filedes[1]); | ||
| 278 | if (read(filedes[0], buf, 1) != 1) { | ||
| 279 | close(filedes[0]); | ||
| 280 | filedes[0] = -1; | ||
| 281 | } | ||
| 282 | return(filedes[0]); | ||
| 283 | } | ||
| 284 | |||
| 285 | /* | ||
| 286 | * CHILD | ||
| 287 | */ | ||
| 288 | |||
| 289 | close(filedes[0]); | ||
| 290 | |||
| 291 | if (ChangeUser(user, 0) < 0) | ||
| 292 | exit(0); | ||
| 293 | |||
| 294 | fd = open(file, O_RDONLY); | ||
| 295 | if (fd < 0) { | ||
| 296 | error_msg("unable to open %s", file); | ||
| 297 | exit(0); | ||
| 298 | } | ||
| 299 | buf[0] = 0; | ||
| 300 | write(filedes[1], buf, 1); | ||
| 301 | while ((n = read(fd, buf, sizeof(buf))) > 0) { | ||
| 302 | write(filedes[1], buf, n); | ||
| 303 | } | ||
| 304 | exit(0); | ||
| 305 | } | ||
| 306 | |||
| 307 | static void | ||
| 308 | EditFile(const char *user, const char *file) | ||
| 309 | { | ||
| 310 | int pid; | ||
| 311 | |||
| 312 | if ((pid = fork()) == 0) { | ||
| 313 | /* | ||
| 314 | * CHILD - change user and run editor | ||
| 315 | */ | ||
| 316 | char *ptr; | ||
| 317 | char visual[1024]; | ||
| 318 | |||
| 319 | if (ChangeUser(user, 1) < 0) | ||
| 320 | exit(0); | ||
| 321 | if ((ptr = getenv("VISUAL")) == NULL || strlen(ptr) > 256) | ||
| 322 | ptr = PATH_VI; | ||
| 323 | |||
| 324 | snprintf(visual, sizeof(visual), "%s %s", ptr, file); | ||
| 325 | execl("/bin/sh", "/bin/sh", "-c", visual, NULL); | ||
| 326 | perror("exec"); | ||
| 327 | exit(0); | ||
| 328 | } | ||
| 329 | if (pid < 0) { | ||
| 330 | /* | ||
| 331 | * PARENT - failure | ||
| 332 | */ | ||
| 333 | perror_msg_and_die("fork"); | ||
| 334 | } | ||
| 335 | wait4(pid, NULL, 0, NULL); | ||
| 336 | } | ||
| 337 | |||
| 338 | static void | ||
| 339 | log(const char *ctl, ...) | ||
| 340 | { | ||
| 341 | va_list va; | ||
| 342 | char buf[1024]; | ||
| 343 | |||
| 344 | va_start(va, ctl); | ||
| 345 | vsnprintf(buf, sizeof(buf), ctl, va); | ||
| 346 | syslog(LOG_NOTICE, "%s",buf ); | ||
| 347 | va_end(va); | ||
| 348 | } | ||
| 349 | |||
| 350 | static int | ||
| 351 | ChangeUser(const char *user, short dochdir) | ||
| 352 | { | ||
| 353 | struct passwd *pas; | ||
| 354 | |||
| 355 | /* | ||
| 356 | * Obtain password entry and change privilages | ||
| 357 | */ | ||
| 358 | |||
| 359 | if ((pas = getpwnam(user)) == 0) { | ||
| 360 | log("failed to get uid for %s", user); | ||
| 361 | return(-1); | ||
| 362 | } | ||
| 363 | setenv("USER", pas->pw_name, 1); | ||
| 364 | setenv("HOME", pas->pw_dir, 1); | ||
| 365 | setenv("SHELL", "/bin/sh", 1); | ||
| 366 | |||
| 367 | /* | ||
| 368 | * Change running state to the user in question | ||
| 369 | */ | ||
| 370 | |||
| 371 | if (initgroups(user, pas->pw_gid) < 0) { | ||
| 372 | log("initgroups failed: %s %m", user); | ||
| 373 | return(-1); | ||
| 374 | } | ||
| 375 | if (setregid(pas->pw_gid, pas->pw_gid) < 0) { | ||
| 376 | log("setregid failed: %s %d", user, pas->pw_gid); | ||
| 377 | return(-1); | ||
| 378 | } | ||
| 379 | if (setreuid(pas->pw_uid, pas->pw_uid) < 0) { | ||
| 380 | log("setreuid failed: %s %d", user, pas->pw_uid); | ||
| 381 | return(-1); | ||
| 382 | } | ||
| 383 | if (dochdir) { | ||
| 384 | if (chdir(pas->pw_dir) < 0) { | ||
| 385 | if (chdir(TMPDIR) < 0) { | ||
| 386 | log("chdir failed: %s %s", user, pas->pw_dir); | ||
| 387 | log("chdir failed: %s " TMPDIR, user); | ||
| 388 | return(-1); | ||
| 389 | } | ||
| 390 | } | ||
| 391 | } | ||
| 392 | return(pas->pw_uid); | ||
| 393 | } | ||
