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