crontab.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* vi: set sw=4 ts=4: */
  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)
  8. * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. */
  12. #include "libbb.h"
  13. #define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
  14. #ifndef CRONUPDATE
  15. #define CRONUPDATE "cron.update"
  16. #endif
  17. static void edit_file(const struct passwd *pas, const char *file)
  18. {
  19. const char *ptr;
  20. int pid = xvfork();
  21. if (pid) { /* parent */
  22. wait4pid(pid);
  23. return;
  24. }
  25. /* CHILD - change user and run editor */
  26. /* initgroups, setgid, setuid */
  27. change_identity(pas);
  28. setup_environment(DEFAULT_SHELL,
  29. SETUP_ENV_CHANGEENV | SETUP_ENV_TO_TMP,
  30. pas);
  31. ptr = getenv("VISUAL");
  32. if (!ptr) {
  33. ptr = getenv("EDITOR");
  34. if (!ptr)
  35. ptr = "vi";
  36. }
  37. BB_EXECLP(ptr, ptr, file, NULL);
  38. bb_perror_msg_and_die("exec %s", ptr);
  39. }
  40. static int open_as_user(const struct passwd *pas, const char *file)
  41. {
  42. pid_t pid;
  43. char c;
  44. pid = xvfork();
  45. if (pid) { /* PARENT */
  46. if (wait4pid(pid) == 0) {
  47. /* exitcode 0: child says it can read */
  48. return open(file, O_RDONLY);
  49. }
  50. return -1;
  51. }
  52. /* CHILD */
  53. /* initgroups, setgid, setuid */
  54. change_identity(pas);
  55. /* We just try to read one byte. If it works, file is readable
  56. * under this user. We signal that by exiting with 0. */
  57. _exit(safe_read(xopen(file, O_RDONLY), &c, 1) < 0);
  58. }
  59. int crontab_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  60. int crontab_main(int argc UNUSED_PARAM, char **argv)
  61. {
  62. const struct passwd *pas;
  63. const char *crontab_dir = CRONTABS;
  64. char *tmp_fname;
  65. char *new_fname;
  66. char *user_name; /* -u USER */
  67. int fd;
  68. int src_fd;
  69. int opt_ler;
  70. /* file [opts] Replace crontab from file
  71. * - [opts] Replace crontab from stdin
  72. * -u user User
  73. * -c dir Crontab directory
  74. * -l List crontab for user
  75. * -e Edit crontab for user
  76. * -r Delete crontab for user
  77. * bbox also supports -d == -r, but most other crontab
  78. * implementations do not. Deprecated.
  79. */
  80. enum {
  81. OPT_u = (1 << 0),
  82. OPT_c = (1 << 1),
  83. OPT_l = (1 << 2),
  84. OPT_e = (1 << 3),
  85. OPT_r = (1 << 4),
  86. OPT_ler = OPT_l + OPT_e + OPT_r,
  87. };
  88. opt_complementary = "?1:dr"; /* max one argument; -d implies -r */
  89. opt_ler = getopt32(argv, "u:c:lerd", &user_name, &crontab_dir);
  90. argv += optind;
  91. if (sanitize_env_if_suid()) { /* Clears dangerous stuff, sets PATH */
  92. /* Run by non-root */
  93. if (opt_ler & (OPT_u|OPT_c))
  94. bb_error_msg_and_die(bb_msg_you_must_be_root);
  95. }
  96. if (opt_ler & OPT_u) {
  97. pas = xgetpwnam(user_name);
  98. } else {
  99. pas = xgetpwuid(getuid());
  100. }
  101. #define user_name DONT_USE_ME_BEYOND_THIS_POINT
  102. /* From now on, keep only -l, -e, -r bits */
  103. opt_ler &= OPT_ler;
  104. if ((opt_ler - 1) & opt_ler) /* more than one bit set? */
  105. bb_show_usage();
  106. /* Read replacement file under user's UID/GID/group vector */
  107. src_fd = STDIN_FILENO;
  108. if (!opt_ler) { /* Replace? */
  109. if (!argv[0])
  110. bb_show_usage();
  111. if (NOT_LONE_DASH(argv[0])) {
  112. src_fd = open_as_user(pas, argv[0]);
  113. if (src_fd < 0)
  114. bb_error_msg_and_die("user %s cannot read %s",
  115. pas->pw_name, argv[0]);
  116. }
  117. }
  118. /* cd to our crontab directory */
  119. xchdir(crontab_dir);
  120. tmp_fname = NULL;
  121. /* Handle requested operation */
  122. switch (opt_ler) {
  123. default: /* case OPT_r: Delete */
  124. unlink(pas->pw_name);
  125. break;
  126. case OPT_l: /* List */
  127. {
  128. char *args[2] = { pas->pw_name, NULL };
  129. return bb_cat(args);
  130. /* list exits,
  131. * the rest go play with cron update file */
  132. }
  133. case OPT_e: /* Edit */
  134. tmp_fname = xasprintf("%s.%u", crontab_dir, (unsigned)getpid());
  135. /* No O_EXCL: we don't want to be stuck if earlier crontabs
  136. * were killed, leaving stale temp file behind */
  137. src_fd = xopen3(tmp_fname, O_RDWR|O_CREAT|O_TRUNC, 0600);
  138. fchown(src_fd, pas->pw_uid, pas->pw_gid);
  139. fd = open(pas->pw_name, O_RDONLY);
  140. if (fd >= 0) {
  141. bb_copyfd_eof(fd, src_fd);
  142. close(fd);
  143. xlseek(src_fd, 0, SEEK_SET);
  144. }
  145. close_on_exec_on(src_fd); /* don't want editor to see this fd */
  146. edit_file(pas, tmp_fname);
  147. /* fall through */
  148. case 0: /* Replace (no -l, -e, or -r were given) */
  149. new_fname = xasprintf("%s.new", pas->pw_name);
  150. fd = open(new_fname, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0600);
  151. if (fd >= 0) {
  152. bb_copyfd_eof(src_fd, fd);
  153. close(fd);
  154. xrename(new_fname, pas->pw_name);
  155. } else {
  156. bb_error_msg("can't create %s/%s",
  157. crontab_dir, new_fname);
  158. }
  159. if (tmp_fname)
  160. unlink(tmp_fname);
  161. /*free(tmp_fname);*/
  162. /*free(new_fname);*/
  163. } /* switch */
  164. /* Bump notification file. Handle window where crond picks file up
  165. * before we can write our entry out.
  166. */
  167. while ((fd = open(CRONUPDATE, O_WRONLY|O_CREAT|O_APPEND, 0600)) >= 0) {
  168. struct stat st;
  169. fdprintf(fd, "%s\n", pas->pw_name);
  170. if (fstat(fd, &st) != 0 || st.st_nlink != 0) {
  171. /*close(fd);*/
  172. break;
  173. }
  174. /* st.st_nlink == 0:
  175. * file was deleted, maybe crond missed our notification */
  176. close(fd);
  177. /* loop */
  178. }
  179. if (fd < 0) {
  180. bb_error_msg("can't append to %s/%s",
  181. crontab_dir, CRONUPDATE);
  182. }
  183. return 0;
  184. }