crontab.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 TMPDIR
  17. #define TMPDIR "/var/spool/cron"
  18. #endif
  19. #ifndef CRONUPDATE
  20. #define CRONUPDATE "cron.update"
  21. #endif
  22. #ifndef PATH_VI
  23. #define PATH_VI "/bin/vi" /* location of vi */
  24. #endif
  25. static const char *CDir = CRONTABS;
  26. static void EditFile(const char *user, const char *file);
  27. static int GetReplaceStream(const char *user, const char *file);
  28. static int ChangeUser(const char *user, short dochdir);
  29. int crontab_main(int ac, char **av);
  30. int crontab_main(int ac, char **av)
  31. {
  32. enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE;
  33. const struct passwd *pas;
  34. const char *repFile = NULL;
  35. int repFd = 0;
  36. int i;
  37. char caller[256]; /* user that ran program */
  38. char buf[1024];
  39. int UserId;
  40. UserId = getuid();
  41. pas = getpwuid(UserId);
  42. if (pas == NULL)
  43. bb_perror_msg_and_die("getpwuid");
  44. safe_strncpy(caller, pas->pw_name, sizeof(caller));
  45. i = 1;
  46. if (ac > 1) {
  47. if (LONE_DASH(av[1])) {
  48. option = REPLACE;
  49. ++i;
  50. } else if (av[1][0] != '-') {
  51. option = REPLACE;
  52. ++i;
  53. repFile = av[1];
  54. }
  55. }
  56. for (; i < ac; ++i) {
  57. char *ptr = av[i];
  58. if (*ptr != '-')
  59. break;
  60. ptr += 2;
  61. switch (ptr[-1]) {
  62. case 'l':
  63. if (ptr[-1] == 'l')
  64. option = LIST;
  65. /* fall through */
  66. case 'e':
  67. if (ptr[-1] == 'e')
  68. option = EDIT;
  69. /* fall through */
  70. case 'd':
  71. if (ptr[-1] == 'd')
  72. option = DELETE;
  73. /* fall through */
  74. case 'u':
  75. if (i + 1 < ac && av[i+1][0] != '-') {
  76. ++i;
  77. if (getuid() == geteuid()) {
  78. pas = getpwnam(av[i]);
  79. if (pas) {
  80. UserId = pas->pw_uid;
  81. } else {
  82. bb_error_msg_and_die("user %s unknown", av[i]);
  83. }
  84. } else {
  85. bb_error_msg_and_die("only the superuser may specify a user");
  86. }
  87. }
  88. break;
  89. case 'c':
  90. if (getuid() == geteuid()) {
  91. CDir = (*ptr) ? ptr : av[++i];
  92. } else {
  93. bb_error_msg_and_die("-c option: superuser only");
  94. }
  95. break;
  96. default:
  97. i = ac;
  98. break;
  99. }
  100. }
  101. if (i != ac || option == NONE)
  102. bb_show_usage();
  103. /*
  104. * Get password entry
  105. */
  106. pas = getpwuid(UserId);
  107. if (pas == NULL)
  108. bb_perror_msg_and_die("getpwuid");
  109. /*
  110. * If there is a replacement file, obtain a secure descriptor to it.
  111. */
  112. if (repFile) {
  113. repFd = GetReplaceStream(caller, repFile);
  114. if (repFd < 0)
  115. bb_error_msg_and_die("cannot read replacement file");
  116. }
  117. /*
  118. * Change directory to our crontab directory
  119. */
  120. xchdir(CDir);
  121. /*
  122. * Handle options as appropriate
  123. */
  124. switch (option) {
  125. case LIST:
  126. {
  127. FILE *fi;
  128. fi = fopen(pas->pw_name, "r");
  129. if (fi) {
  130. while (fgets(buf, sizeof(buf), fi) != NULL)
  131. fputs(buf, stdout);
  132. fclose(fi);
  133. } else {
  134. bb_error_msg("no crontab for %s", pas->pw_name);
  135. }
  136. }
  137. break;
  138. case EDIT:
  139. {
  140. /* FIXME: messy code here! we have file copying helpers for this! */
  141. FILE *fi;
  142. int fd;
  143. int n;
  144. char tmp[128];
  145. snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
  146. fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
  147. /* race, use fchown */
  148. chown(tmp, getuid(), getgid());
  149. fi = fopen(pas->pw_name, "r");
  150. if (fi) {
  151. while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
  152. full_write(fd, buf, n);
  153. }
  154. EditFile(caller, tmp);
  155. remove(tmp);
  156. lseek(fd, 0L, SEEK_SET);
  157. repFd = fd;
  158. }
  159. option = REPLACE;
  160. /* fall through */
  161. case REPLACE:
  162. {
  163. /* same here */
  164. char path[1024];
  165. int fd;
  166. int n;
  167. snprintf(path, sizeof(path), "%s.new", pas->pw_name);
  168. fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600);
  169. if (fd >= 0) {
  170. while ((n = read(repFd, buf, sizeof(buf))) > 0) {
  171. full_write(fd, buf, n);
  172. }
  173. close(fd);
  174. rename(path, pas->pw_name);
  175. } else {
  176. bb_error_msg("cannot create %s/%s", CDir, path);
  177. }
  178. close(repFd);
  179. }
  180. break;
  181. case DELETE:
  182. remove(pas->pw_name);
  183. break;
  184. case NONE:
  185. default:
  186. break;
  187. }
  188. /*
  189. * Bump notification file. Handle window where crond picks file up
  190. * before we can write our entry out.
  191. */
  192. if (option == REPLACE || option == DELETE) {
  193. FILE *fo;
  194. struct stat st;
  195. while ((fo = fopen(CRONUPDATE, "a"))) {
  196. fprintf(fo, "%s\n", pas->pw_name);
  197. fflush(fo);
  198. if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) {
  199. fclose(fo);
  200. break;
  201. }
  202. fclose(fo);
  203. /* loop */
  204. }
  205. if (fo == NULL) {
  206. bb_error_msg("cannot append to %s/%s", CDir, CRONUPDATE);
  207. }
  208. }
  209. return 0;
  210. }
  211. static int GetReplaceStream(const char *user, const char *file)
  212. {
  213. int filedes[2];
  214. int pid;
  215. int fd;
  216. int n;
  217. char buf[1024];
  218. if (pipe(filedes) < 0) {
  219. perror("pipe");
  220. return -1;
  221. }
  222. pid = fork();
  223. if (pid < 0) {
  224. perror("fork");
  225. return -1;
  226. }
  227. if (pid > 0) {
  228. /*
  229. * PARENT
  230. */
  231. close(filedes[1]);
  232. if (read(filedes[0], buf, 1) != 1) {
  233. close(filedes[0]);
  234. filedes[0] = -1;
  235. }
  236. return filedes[0];
  237. }
  238. /*
  239. * CHILD
  240. */
  241. close(filedes[0]);
  242. if (ChangeUser(user, 0) < 0)
  243. exit(0);
  244. xfunc_error_retval = 0;
  245. fd = xopen(file, O_RDONLY);
  246. buf[0] = 0;
  247. write(filedes[1], buf, 1);
  248. while ((n = read(fd, buf, sizeof(buf))) > 0) {
  249. write(filedes[1], buf, n);
  250. }
  251. exit(0);
  252. }
  253. static void EditFile(const char *user, const char *file)
  254. {
  255. int pid = fork();
  256. if (pid == 0) {
  257. /*
  258. * CHILD - change user and run editor
  259. */
  260. const char *ptr;
  261. if (ChangeUser(user, 1) < 0)
  262. exit(0);
  263. ptr = getenv("VISUAL");
  264. if (ptr == NULL || strlen(ptr) > 256)
  265. ptr = PATH_VI;
  266. ptr = xasprintf("%s %s", ptr, file);
  267. execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", ptr, NULL);
  268. bb_perror_msg_and_die("exec");
  269. }
  270. if (pid < 0) {
  271. /*
  272. * PARENT - failure
  273. */
  274. bb_perror_msg_and_die("fork");
  275. }
  276. wait4(pid, NULL, 0, NULL);
  277. }
  278. static int ChangeUser(const char *user, short dochdir)
  279. {
  280. struct passwd *pas;
  281. /*
  282. * Obtain password entry and change privileges
  283. */
  284. pas = getpwnam(user);
  285. if (pas == NULL) {
  286. bb_perror_msg_and_die("failed to get uid for %s", user);
  287. }
  288. setenv("USER", pas->pw_name, 1);
  289. setenv("HOME", pas->pw_dir, 1);
  290. setenv("SHELL", DEFAULT_SHELL, 1);
  291. /*
  292. * Change running state to the user in question
  293. */
  294. change_identity(pas);
  295. if (dochdir) {
  296. if (chdir(pas->pw_dir) < 0) {
  297. bb_perror_msg("chdir(%s) by %s failed", pas->pw_dir, user);
  298. xchdir(TMPDIR);
  299. }
  300. }
  301. return pas->pw_uid;
  302. }