diff options
Diffstat (limited to 'miscutils')
-rw-r--r-- | miscutils/crontab.c | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/miscutils/crontab.c b/miscutils/crontab.c index f8662babb..01656cc6e 100644 --- a/miscutils/crontab.c +++ b/miscutils/crontab.c | |||
@@ -93,6 +93,7 @@ int crontab_main(int argc UNUSED_PARAM, char **argv) | |||
93 | char *new_fname; | 93 | char *new_fname; |
94 | char *user_name; /* -u USER */ | 94 | char *user_name; /* -u USER */ |
95 | int fd; | 95 | int fd; |
96 | int src_fd; | ||
96 | int opt_ler; | 97 | int opt_ler; |
97 | 98 | ||
98 | /* file [opts] Replace crontab from file | 99 | /* file [opts] Replace crontab from file |
@@ -144,15 +145,15 @@ int crontab_main(int argc UNUSED_PARAM, char **argv) | |||
144 | bb_show_usage(); | 145 | bb_show_usage(); |
145 | 146 | ||
146 | /* Read replacement file under user's UID/GID/group vector */ | 147 | /* Read replacement file under user's UID/GID/group vector */ |
148 | src_fd = STDIN_FILENO; | ||
147 | if (!opt_ler) { /* Replace? */ | 149 | if (!opt_ler) { /* Replace? */ |
148 | if (!argv[0]) | 150 | if (!argv[0]) |
149 | bb_show_usage(); | 151 | bb_show_usage(); |
150 | if (NOT_LONE_DASH(argv[0])) { | 152 | if (NOT_LONE_DASH(argv[0])) { |
151 | fd = open_as_user(pas, argv[0]); | 153 | src_fd = open_as_user(pas, argv[0]); |
152 | if (fd < 0) | 154 | if (src_fd < 0) |
153 | bb_error_msg_and_die("user %s cannot read %s", | 155 | bb_error_msg_and_die("user %s cannot read %s", |
154 | pas->pw_name, argv[0]); | 156 | pas->pw_name, argv[0]); |
155 | xmove_fd(fd, STDIN_FILENO); | ||
156 | } | 157 | } |
157 | } | 158 | } |
158 | 159 | ||
@@ -180,23 +181,23 @@ int crontab_main(int argc UNUSED_PARAM, char **argv) | |||
180 | tmp_fname = xasprintf("%s.%u", crontab_dir, (unsigned)getpid()); | 181 | tmp_fname = xasprintf("%s.%u", crontab_dir, (unsigned)getpid()); |
181 | /* No O_EXCL: we don't want to be stuck if earlier crontabs | 182 | /* No O_EXCL: we don't want to be stuck if earlier crontabs |
182 | * were killed, leaving stale temp file behind */ | 183 | * were killed, leaving stale temp file behind */ |
183 | fd = xopen3(tmp_fname, O_RDWR|O_CREAT|O_TRUNC, 0600); | 184 | src_fd = xopen3(tmp_fname, O_RDWR|O_CREAT|O_TRUNC, 0600); |
184 | xmove_fd(fd, STDIN_FILENO); | 185 | fchown(src_fd, pas->pw_uid, pas->pw_gid); |
185 | fchown(STDIN_FILENO, pas->pw_uid, pas->pw_gid); | ||
186 | fd = open(pas->pw_name, O_RDONLY); | 186 | fd = open(pas->pw_name, O_RDONLY); |
187 | if (fd >= 0) { | 187 | if (fd >= 0) { |
188 | bb_copyfd_eof(fd, STDIN_FILENO); | 188 | bb_copyfd_eof(fd, src_fd); |
189 | close(fd); | 189 | close(fd); |
190 | xlseek(src_fd, 0, SEEK_SET); | ||
190 | } | 191 | } |
192 | close_on_exec_on(src_fd); /* don't want editor to see this fd */ | ||
191 | edit_file(pas, tmp_fname); | 193 | edit_file(pas, tmp_fname); |
192 | xlseek(STDIN_FILENO, 0, SEEK_SET); | ||
193 | /* fall through */ | 194 | /* fall through */ |
194 | 195 | ||
195 | case 0: /* Replace (no -l, -e, or -r were given) */ | 196 | case 0: /* Replace (no -l, -e, or -r were given) */ |
196 | new_fname = xasprintf("%s.new", pas->pw_name); | 197 | new_fname = xasprintf("%s.new", pas->pw_name); |
197 | fd = open(new_fname, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0600); | 198 | fd = open(new_fname, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0600); |
198 | if (fd >= 0) { | 199 | if (fd >= 0) { |
199 | bb_copyfd_eof(STDIN_FILENO, fd); | 200 | bb_copyfd_eof(src_fd, fd); |
200 | close(fd); | 201 | close(fd); |
201 | xrename(new_fname, pas->pw_name); | 202 | xrename(new_fname, pas->pw_name); |
202 | } else { | 203 | } else { |