crontab.c 5.4 KB

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