3
0

crontab.c 4.9 KB

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