crontab.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 (10 kb)"
  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_ler = getopt32(argv, "^" "u:c:lerd" "\0" "?1:dr"/*max one arg; -d implies -r*/,
  91. &user_name, &crontab_dir
  92. );
  93. argv += optind;
  94. if (sanitize_env_if_suid()) { /* Clears dangerous stuff, sets PATH */
  95. /* Run by non-root */
  96. if (opt_ler & (OPT_u|OPT_c))
  97. bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
  98. }
  99. if (opt_ler & OPT_u) {
  100. pas = xgetpwnam(user_name);
  101. } else {
  102. pas = xgetpwuid(getuid());
  103. }
  104. #define user_name DONT_USE_ME_BEYOND_THIS_POINT
  105. /* From now on, keep only -l, -e, -r bits */
  106. opt_ler &= OPT_ler;
  107. if ((opt_ler - 1) & opt_ler) /* more than one bit set? */
  108. bb_show_usage();
  109. /* Read replacement file under user's UID/GID/group vector */
  110. src_fd = STDIN_FILENO;
  111. if (!opt_ler) { /* Replace? */
  112. if (!argv[0])
  113. bb_show_usage();
  114. if (NOT_LONE_DASH(argv[0])) {
  115. src_fd = xopen_as_uid_gid(argv[0], O_RDONLY, pas->pw_uid, pas->pw_gid);
  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(src_fd);
  146. edit_file(pas, tmp_fname);
  147. /* The src_fd needs to be reopened to handle editors that do
  148. * save the buffer as new file and rename it to tmp_fname (so
  149. * for example vim). */
  150. src_fd = xopen3(tmp_fname, O_RDONLY, 0600);
  151. /* fall through */
  152. case 0: /* Replace (no -l, -e, or -r were given) */
  153. new_fname = xasprintf("%s.new", pas->pw_name);
  154. fd = open(new_fname, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0600);
  155. if (fd >= 0) {
  156. bb_copyfd_eof(src_fd, fd);
  157. close(fd);
  158. xrename(new_fname, pas->pw_name);
  159. } else {
  160. bb_error_msg("can't create %s/%s",
  161. crontab_dir, new_fname);
  162. }
  163. if (tmp_fname)
  164. unlink(tmp_fname);
  165. /*free(tmp_fname);*/
  166. /*free(new_fname);*/
  167. } /* switch */
  168. /* Bump notification file. Handle window where crond picks file up
  169. * before we can write our entry out.
  170. */
  171. while ((fd = open(CRONUPDATE, O_WRONLY|O_CREAT|O_APPEND, 0600)) >= 0) {
  172. struct stat st;
  173. fdprintf(fd, "%s\n", pas->pw_name);
  174. if (fstat(fd, &st) != 0 || st.st_nlink != 0) {
  175. /*close(fd);*/
  176. break;
  177. }
  178. /* st.st_nlink == 0:
  179. * file was deleted, maybe crond missed our notification */
  180. close(fd);
  181. /* loop */
  182. }
  183. if (fd < 0) {
  184. bb_error_msg("can't append to %s/%s",
  185. crontab_dir, CRONUPDATE);
  186. }
  187. return 0;
  188. }