3
0

crontab.c 5.4 KB

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