crontab.c 5.6 KB

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