crontab.c 6.4 KB

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