crontab.c 7.2 KB

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