2 7df505fb 2025-01-27 martijn * Copyright (c) 2016 Martijn van Duren <martijn@openbsd.org>
3 09a12e37 2016-09-08 martijn * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
5 09a12e37 2016-09-08 martijn * Permission to use, copy, modify, and distribute this software for any
6 09a12e37 2016-09-08 martijn * purpose with or without fee is hereby granted, provided that the above
7 09a12e37 2016-09-08 martijn * copyright notice and this permission notice appear in all copies.
9 09a12e37 2016-09-08 martijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 09a12e37 2016-09-08 martijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 09a12e37 2016-09-08 martijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 09a12e37 2016-09-08 martijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 09a12e37 2016-09-08 martijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 09a12e37 2016-09-08 martijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 09a12e37 2016-09-08 martijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 09a12e37 2016-09-08 martijn #include <sys/types.h>
19 09a12e37 2016-09-08 martijn #include <sys/stat.h>
20 09a12e37 2016-09-08 martijn #include <sys/wait.h>
21 09a12e37 2016-09-08 martijn #include <sys/ioctl.h>
23 09a12e37 2016-09-08 martijn #include <fcntl.h>
24 09a12e37 2016-09-08 martijn #include <limits.h>
25 09a12e37 2016-09-08 martijn #include <libgen.h>
26 09a12e37 2016-09-08 martijn #include <login_cap.h>
27 09a12e37 2016-09-08 martijn #include <bsd_auth.h>
28 09a12e37 2016-09-08 martijn #include <readpassphrase.h>
29 b2396b2c 2018-11-13 martijn #include <signal.h>
30 09a12e37 2016-09-08 martijn #include <string.h>
31 09a12e37 2016-09-08 martijn #include <stdio.h>
32 09a12e37 2016-09-08 martijn #include <stdlib.h>
33 09a12e37 2016-09-08 martijn #include <err.h>
34 09a12e37 2016-09-08 martijn #include <unistd.h>
35 09a12e37 2016-09-08 martijn #include <pwd.h>
36 09a12e37 2016-09-08 martijn #include <grp.h>
37 09a12e37 2016-09-08 martijn #include <syslog.h>
38 09a12e37 2016-09-08 martijn #include <errno.h>
40 09a12e37 2016-09-08 martijn #include "vias.h"
42 09a12e37 2016-09-08 martijn static void __dead
43 09a12e37 2016-09-08 martijn usage(void)
45 09a12e37 2016-09-08 martijn fprintf(stderr, "usage: vias [-a style] [-C config] "
46 09a12e37 2016-09-08 martijn "file [editor args]\n");
51 b6e88a9a 2020-12-18 martijn arraylen(const char * const*arr)
53 09a12e37 2016-09-08 martijn size_t cnt = 0;
55 09a12e37 2016-09-08 martijn while (*arr) {
59 09a12e37 2016-09-08 martijn return cnt;
62 09a12e37 2016-09-08 martijn static int
63 09a12e37 2016-09-08 martijn parseuid(const char *s, uid_t *uid)
65 09a12e37 2016-09-08 martijn struct passwd *pw;
66 09a12e37 2016-09-08 martijn const char *errstr;
68 09a12e37 2016-09-08 martijn if ((pw = getpwnam(s)) != NULL) {
69 09a12e37 2016-09-08 martijn *uid = pw->pw_uid;
70 09a12e37 2016-09-08 martijn return 0;
72 09a12e37 2016-09-08 martijn *uid = strtonum(s, 0, UID_MAX, &errstr);
73 09a12e37 2016-09-08 martijn if (errstr)
74 09a12e37 2016-09-08 martijn return -1;
75 09a12e37 2016-09-08 martijn return 0;
78 09a12e37 2016-09-08 martijn static int
79 09a12e37 2016-09-08 martijn uidcheck(const char *s, uid_t desired)
81 09a12e37 2016-09-08 martijn uid_t uid;
83 09a12e37 2016-09-08 martijn if (parseuid(s, &uid) != 0)
84 09a12e37 2016-09-08 martijn return -1;
85 09a12e37 2016-09-08 martijn if (uid != desired)
86 09a12e37 2016-09-08 martijn return -1;
87 09a12e37 2016-09-08 martijn return 0;
90 09a12e37 2016-09-08 martijn static int
91 09a12e37 2016-09-08 martijn parsegid(const char *s, gid_t *gid)
93 09a12e37 2016-09-08 martijn struct group *gr;
94 09a12e37 2016-09-08 martijn const char *errstr;
96 09a12e37 2016-09-08 martijn if ((gr = getgrnam(s)) != NULL) {
97 09a12e37 2016-09-08 martijn *gid = gr->gr_gid;
98 09a12e37 2016-09-08 martijn return 0;
100 09a12e37 2016-09-08 martijn *gid = strtonum(s, 0, GID_MAX, &errstr);
101 09a12e37 2016-09-08 martijn if (errstr)
102 09a12e37 2016-09-08 martijn return -1;
103 09a12e37 2016-09-08 martijn return 0;
106 09a12e37 2016-09-08 martijn static int
107 09a12e37 2016-09-08 martijn open_nosym(const char *file)
109 09a12e37 2016-09-08 martijn static int dep = 0;
110 09a12e37 2016-09-08 martijn struct stat sb;
111 cd4ec459 2020-12-18 martijn char path[PATH_MAX];
112 09a12e37 2016-09-08 martijn int fd, pfd;
115 cd4ec459 2020-12-18 martijn strlcpy(path, file, sizeof(path));
116 cd4ec459 2020-12-18 martijn (void) strlcpy(path, dirname(path), sizeof(path));
118 cd4ec459 2020-12-18 martijn if (path[1] == '\0') {
120 09a12e37 2016-09-08 martijn if ((pfd = open("/", O_CLOEXEC)) == -1)
121 09a12e37 2016-09-08 martijn return -1;
122 09a12e37 2016-09-08 martijn } else {
123 cd4ec459 2020-12-18 martijn pfd = open_nosym(path);
125 09a12e37 2016-09-08 martijn if (pfd == -1)
126 09a12e37 2016-09-08 martijn return -1;
130 cd4ec459 2020-12-18 martijn strlcpy(path, file, sizeof(path));
131 09a12e37 2016-09-08 martijn if (dep) {
132 cd4ec459 2020-12-18 martijn fd = openat(pfd, basename(path),
133 09a12e37 2016-09-08 martijn O_NOFOLLOW | O_CLOEXEC);
134 09a12e37 2016-09-08 martijn } else {
135 cd4ec459 2020-12-18 martijn fd = openat(pfd, basename(path),
136 09a12e37 2016-09-08 martijn O_RDWR | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0777);
138 09a12e37 2016-09-08 martijn } while (fd == -1 && errno == EINTR);
139 09a12e37 2016-09-08 martijn close(pfd);
140 09a12e37 2016-09-08 martijn if (fd == -1)
141 09a12e37 2016-09-08 martijn return -1;
143 09a12e37 2016-09-08 martijn if (fstat(fd, &sb) == -1)
144 09a12e37 2016-09-08 martijn err(1, "fstat");
146 09a12e37 2016-09-08 martijn * This should already have been checked in parse.y.
147 09a12e37 2016-09-08 martijn * It's only here to test for race-conditions
149 09a12e37 2016-09-08 martijn if (S_ISLNK(sb.st_mode))
150 09a12e37 2016-09-08 martijn errx(1, "Symbolic links not allowed");
151 09a12e37 2016-09-08 martijn if (dep && !S_ISDIR(sb.st_mode))
152 09a12e37 2016-09-08 martijn errc(1, ENOTDIR, NULL);
153 09a12e37 2016-09-08 martijn if (!dep && !S_ISREG(sb.st_mode))
154 09a12e37 2016-09-08 martijn errx(1, "File is not a regular file");
156 09a12e37 2016-09-08 martijn return fd;
159 09a12e37 2016-09-08 martijn static int
160 09a12e37 2016-09-08 martijn match(uid_t uid, gid_t *groups, int ngroups, const char *file, struct rule *r)
163 09a12e37 2016-09-08 martijn int flen;
165 09a12e37 2016-09-08 martijn if (r->ident[0] == ':') {
166 09a12e37 2016-09-08 martijn gid_t rgid;
167 09a12e37 2016-09-08 martijn if (parsegid(r->ident + 1, &rgid) == -1)
168 09a12e37 2016-09-08 martijn return 0;
169 09a12e37 2016-09-08 martijn for (i = 0; i < ngroups; i++) {
170 09a12e37 2016-09-08 martijn if (rgid == groups[i])
173 09a12e37 2016-09-08 martijn if (i == ngroups)
174 09a12e37 2016-09-08 martijn return 0;
175 09a12e37 2016-09-08 martijn } else {
176 09a12e37 2016-09-08 martijn if (uidcheck(r->ident, uid) != 0)
177 09a12e37 2016-09-08 martijn return 0;
179 09a12e37 2016-09-08 martijn if (r->files != NULL) {
180 09a12e37 2016-09-08 martijn for (i = 0; r->files[i] != NULL; i++) {
181 09a12e37 2016-09-08 martijn flen = strlen(r->files[i]);
182 09a12e37 2016-09-08 martijn /* Allow access to the entire directory tree */
183 09a12e37 2016-09-08 martijn if (r->files[i][flen-1] == '/') {
184 09a12e37 2016-09-08 martijn if (!strncmp(r->files[i], file, flen))
186 09a12e37 2016-09-08 martijn } else {
187 09a12e37 2016-09-08 martijn if (!strcmp(r->files[i], file))
191 09a12e37 2016-09-08 martijn if (r->files[i] == NULL)
192 09a12e37 2016-09-08 martijn return 0;
194 09a12e37 2016-09-08 martijn return 1;
197 09a12e37 2016-09-08 martijn static int
198 09a12e37 2016-09-08 martijn permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
199 09a12e37 2016-09-08 martijn const char *file)
202 09a12e37 2016-09-08 martijn int fd = -1, pfd = -1;
203 09a12e37 2016-09-08 martijn uid_t suid = -1;
204 09a12e37 2016-09-08 martijn struct rule *r;
205 09a12e37 2016-09-08 martijn char *rfile;
207 09a12e37 2016-09-08 martijn if ((rfile = realpath(file, NULL)) == NULL)
208 09a12e37 2016-09-08 martijn err(1, "Unable to open %s", file);
210 09a12e37 2016-09-08 martijn *lastr = NULL;
211 09a12e37 2016-09-08 martijn for (i = 0; i < nrules; i++) {
212 09a12e37 2016-09-08 martijn r = rules[i];
213 09a12e37 2016-09-08 martijn if (match(uid, groups, ngroups, rfile, r)) {
214 09a12e37 2016-09-08 martijn /* Try to open the file, so we know the rule is really permitted */
215 09a12e37 2016-09-08 martijn if (r->target) {
216 09a12e37 2016-09-08 martijn if (parseuid(r->target, &suid) == -1)
217 09a12e37 2016-09-08 martijn err(1, "getpwnam");
218 09a12e37 2016-09-08 martijn if (seteuid(suid) == -1)
219 09a12e37 2016-09-08 martijn err(1, "seteuid");
221 09a12e37 2016-09-08 martijn if ((fd = open_nosym(rfile)) != -1) {
222 09a12e37 2016-09-08 martijn if (pfd != -1)
223 09a12e37 2016-09-08 martijn if (close(pfd) == -1)
224 09a12e37 2016-09-08 martijn err(1, "close");
225 09a12e37 2016-09-08 martijn pfd = fd;
226 09a12e37 2016-09-08 martijn *lastr = rules[i];
227 09a12e37 2016-09-08 martijn } else if (errno != EPERM) {
228 09a12e37 2016-09-08 martijn err(1, "open %s", file);
230 09a12e37 2016-09-08 martijn if (r->target && seteuid(0))
231 09a12e37 2016-09-08 martijn err(1, "seteuid");
234 09a12e37 2016-09-08 martijn if (*lastr == NULL || (*lastr)->action != PERMIT) {
235 95866557 2017-09-25 martijn if (fd != -1 && close(fd) == -1)
236 76b35bc9 2017-09-25 martijn err(1, "close");
237 09a12e37 2016-09-08 martijn return -1;
240 09a12e37 2016-09-08 martijn return fd;
243 09a12e37 2016-09-08 martijn static void
244 09a12e37 2016-09-08 martijn parseconfig(const char *filename, int checkperms)
246 09a12e37 2016-09-08 martijn extern FILE *yyfp;
247 09a12e37 2016-09-08 martijn extern int yyparse(void);
248 09a12e37 2016-09-08 martijn struct stat sb;
250 09a12e37 2016-09-08 martijn yyfp = fopen(filename, "r");
251 09a12e37 2016-09-08 martijn if (!yyfp)
252 09a12e37 2016-09-08 martijn err(1, checkperms ? "vias is not enabled, %s" :
253 09a12e37 2016-09-08 martijn "could not open config file %s", filename);
255 09a12e37 2016-09-08 martijn if (checkperms) {
256 09a12e37 2016-09-08 martijn if (fstat(fileno(yyfp), &sb) != 0)
257 09a12e37 2016-09-08 martijn err(1, "fstat(\"%s\")", filename);
258 09a12e37 2016-09-08 martijn if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
259 09a12e37 2016-09-08 martijn errx(1, "%s is writable by group or other", filename);
260 09a12e37 2016-09-08 martijn if (sb.st_uid != 0)
261 09a12e37 2016-09-08 martijn errx(1, "%s is not owned by root", filename);
264 09a12e37 2016-09-08 martijn yyparse();
265 09a12e37 2016-09-08 martijn fclose(yyfp);
266 09a12e37 2016-09-08 martijn if (parse_errors)
267 09a12e37 2016-09-08 martijn exit(1);
270 09a12e37 2016-09-08 martijn static void __dead
271 09a12e37 2016-09-08 martijn checkconfig(const char *confpath, uid_t uid, gid_t *groups, int ngroups,
272 09a12e37 2016-09-08 martijn const char *file)
274 09a12e37 2016-09-08 martijn struct rule *rule;
276 09a12e37 2016-09-08 martijn parseconfig(confpath, 0);
277 09a12e37 2016-09-08 martijn if (!file)
278 09a12e37 2016-09-08 martijn exit(0);
280 09a12e37 2016-09-08 martijn if (permit(uid, groups, ngroups, &rule, file) != -1) {
281 09a12e37 2016-09-08 martijn printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
282 09a12e37 2016-09-08 martijn exit(0);
284 09a12e37 2016-09-08 martijn printf("deny\n");
285 09a12e37 2016-09-08 martijn exit(1);
288 09a12e37 2016-09-08 martijn static void
289 09a12e37 2016-09-08 martijn authuser(char *myname, char *login_style, int persist)
291 09a12e37 2016-09-08 martijn char *challenge = NULL, *response, rbuf[1024], cbuf[128];
292 09a12e37 2016-09-08 martijn auth_session_t *as;
293 09a12e37 2016-09-08 martijn int fd = -1;
295 09a12e37 2016-09-08 martijn if (persist)
296 09a12e37 2016-09-08 martijn fd = open("/dev/tty", O_RDWR);
297 09a12e37 2016-09-08 martijn if (fd != -1) {
298 09a12e37 2016-09-08 martijn if (ioctl(fd, TIOCCHKVERAUTH) == 0)
299 09a12e37 2016-09-08 martijn goto good;
302 09a12e37 2016-09-08 martijn if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
303 09a12e37 2016-09-08 martijn &challenge)))
304 09a12e37 2016-09-08 martijn errx(1, "Authorization failed");
305 09a12e37 2016-09-08 martijn if (!challenge) {
306 09a12e37 2016-09-08 martijn char host[HOST_NAME_MAX + 1];
307 09a12e37 2016-09-08 martijn if (gethostname(host, sizeof(host)))
308 09a12e37 2016-09-08 martijn snprintf(host, sizeof(host), "?");
309 09a12e37 2016-09-08 martijn snprintf(cbuf, sizeof(cbuf),
310 09a12e37 2016-09-08 martijn "\rvias (%.32s@%.32s) password: ", myname, host);
311 09a12e37 2016-09-08 martijn challenge = cbuf;
313 09a12e37 2016-09-08 martijn response = readpassphrase(challenge, rbuf, sizeof(rbuf),
314 09a12e37 2016-09-08 martijn RPP_REQUIRE_TTY);
315 09a12e37 2016-09-08 martijn if (response == NULL && errno == ENOTTY) {
316 09a12e37 2016-09-08 martijn syslog(LOG_AUTHPRIV | LOG_NOTICE,
317 09a12e37 2016-09-08 martijn "tty required for %s", myname);
318 09a12e37 2016-09-08 martijn errx(1, "a tty is required");
320 09a12e37 2016-09-08 martijn if (!auth_userresponse(as, response, 0)) {
321 3a96e431 2019-01-17 martijn explicit_bzero(rbuf, sizeof(rbuf));
322 09a12e37 2016-09-08 martijn syslog(LOG_AUTHPRIV | LOG_NOTICE,
323 09a12e37 2016-09-08 martijn "failed auth for %s", myname);
324 09a12e37 2016-09-08 martijn errc(1, EPERM, NULL);
326 09a12e37 2016-09-08 martijn explicit_bzero(rbuf, sizeof(rbuf));
328 09a12e37 2016-09-08 martijn if (fd != -1) {
329 09a12e37 2016-09-08 martijn int secs = 5 * 60;
330 09a12e37 2016-09-08 martijn ioctl(fd, TIOCSETVERAUTH, &secs);
331 09a12e37 2016-09-08 martijn close(fd);
334 09a12e37 2016-09-08 martijn static int
335 09a12e37 2016-09-08 martijn fcpy(int dfd, int sfd)
337 09a12e37 2016-09-08 martijn unsigned char buf[4096];
341 09a12e37 2016-09-08 martijn while ((r = read(sfd, buf, sizeof(buf))) > 0) {
342 09a12e37 2016-09-08 martijn if (write(dfd, buf, r) != r)
343 09a12e37 2016-09-08 martijn return 0;
345 09a12e37 2016-09-08 martijn } while (r == -1 && errno == EINTR);
346 09a12e37 2016-09-08 martijn if (r == -1)
347 09a12e37 2016-09-08 martijn return 0;
348 09a12e37 2016-09-08 martijn return 1;
352 09a12e37 2016-09-08 martijn main(int argc, char **argv)
354 09a12e37 2016-09-08 martijn const char *confpath = NULL;
355 09a12e37 2016-09-08 martijn char tmpfile[] = "/tmp/vias.XXXXXX";
356 09a12e37 2016-09-08 martijn char myname[_PW_NAME_LEN + 1];
357 09a12e37 2016-09-08 martijn struct passwd *pw;
358 09a12e37 2016-09-08 martijn struct rule *rule;
359 09a12e37 2016-09-08 martijn uid_t uid;
360 09a12e37 2016-09-08 martijn gid_t groups[NGROUPS_MAX + 1];
361 09a12e37 2016-09-08 martijn int ngroups;
362 09a12e37 2016-09-08 martijn int i, ch;
363 fe48b8a3 2020-12-18 martijn int ofd, tfd, ttyfd;
364 09a12e37 2016-09-08 martijn char cwdpath[PATH_MAX];
365 09a12e37 2016-09-08 martijn const char *cwd;
366 09a12e37 2016-09-08 martijn char *login_style = NULL;
367 09a12e37 2016-09-08 martijn char *file;
368 09a12e37 2016-09-08 martijn char **eargv;
369 09a12e37 2016-09-08 martijn int status;
370 13ffdf3b 2017-09-24 martijn pid_t ret, vipid;
372 09a12e37 2016-09-08 martijn setprogname("vias");
374 09a12e37 2016-09-08 martijn closefrom(STDERR_FILENO + 1);
376 09a12e37 2016-09-08 martijn uid = getuid();
378 09a12e37 2016-09-08 martijn while ((ch = getopt(argc, argv, "a:C:")) != -1) {
379 09a12e37 2016-09-08 martijn switch (ch) {
380 09a12e37 2016-09-08 martijn case 'a':
381 09a12e37 2016-09-08 martijn login_style = optarg;
383 09a12e37 2016-09-08 martijn case 'C':
384 09a12e37 2016-09-08 martijn confpath = optarg;
386 09a12e37 2016-09-08 martijn default:
387 09a12e37 2016-09-08 martijn usage();
390 09a12e37 2016-09-08 martijn argv += optind;
391 09a12e37 2016-09-08 martijn argc -= optind;
393 347af3ca 2017-09-25 martijn if (argc == 0 && confpath == NULL)
394 09a12e37 2016-09-08 martijn usage();
395 09a12e37 2016-09-08 martijn file = argv[0];
399 09a12e37 2016-09-08 martijn pw = getpwuid(uid);
400 09a12e37 2016-09-08 martijn if (!pw)
401 09a12e37 2016-09-08 martijn err(1, "getpwuid failed");
402 09a12e37 2016-09-08 martijn if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
403 09a12e37 2016-09-08 martijn errx(1, "pw_name too long");
404 09a12e37 2016-09-08 martijn ngroups = getgroups(NGROUPS_MAX, groups);
405 09a12e37 2016-09-08 martijn if (ngroups == -1)
406 09a12e37 2016-09-08 martijn err(1, "can't get groups");
407 09a12e37 2016-09-08 martijn groups[ngroups++] = getgid();
409 09a12e37 2016-09-08 martijn if (confpath)
410 09a12e37 2016-09-08 martijn checkconfig(confpath, uid, groups, ngroups, file);
412 332482f3 2017-09-25 martijn if (geteuid())
413 332482f3 2017-09-25 martijn errx(1, "not installed setuid");
415 09a12e37 2016-09-08 martijn parseconfig("/etc/vias.conf", 1);
417 73964625 2017-09-24 martijn if (setuid(0) == -1)
418 73964625 2017-09-24 martijn err(1, "setuid");
419 09a12e37 2016-09-08 martijn if ((ofd = permit(uid, groups, ngroups, &rule, file)) == -1) {
420 09a12e37 2016-09-08 martijn syslog(LOG_AUTHPRIV | LOG_NOTICE,
421 09a12e37 2016-09-08 martijn "failed edit for %s: %s", myname, file);
422 08393614 2017-09-25 martijn errc(1, EPERM, "%s", file);
425 73964625 2017-09-24 martijn if (setreuid(uid, 0) == -1)
426 73964625 2017-09-24 martijn err(1, "setreuid failed");
428 09a12e37 2016-09-08 martijn if (!(rule->options & NOPASS))
429 09a12e37 2016-09-08 martijn authuser(myname, login_style, rule->options & PERSIST);
431 fe48b8a3 2020-12-18 martijn if (pledge("stdio rpath wpath cpath exec proc id tty", NULL) == -1)
432 09a12e37 2016-09-08 martijn err(1, "pledge");
434 09a12e37 2016-09-08 martijn if ((setuid(uid)) == -1)
435 09a12e37 2016-09-08 martijn err(1, "setuid failed");
437 fe48b8a3 2020-12-18 martijn if (pledge("stdio rpath wpath cpath exec proc tty", NULL) == -1)
438 09a12e37 2016-09-08 martijn err(1, "pledge");
440 09a12e37 2016-09-08 martijn if ((tfd = mkstemp(tmpfile)) == -1)
441 09a12e37 2016-09-08 martijn err(1, "mkstemp failed");
443 fe48b8a3 2020-12-18 martijn if (pledge("stdio rpath cpath exec proc tty", NULL) == -1)
444 09a12e37 2016-09-08 martijn err(1, "pledge");
446 09a12e37 2016-09-08 martijn if (!fcpy(tfd, ofd)) {
447 09a12e37 2016-09-08 martijn unlink(tmpfile);
448 09a12e37 2016-09-08 martijn err(1, "temp copy failed");
451 09a12e37 2016-09-08 martijn if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
452 09a12e37 2016-09-08 martijn cwd = "(failed)";
454 09a12e37 2016-09-08 martijn cwd = cwdpath;
456 fe48b8a3 2020-12-18 martijn if (pledge("stdio cpath exec proc tty", NULL) == -1)
457 09a12e37 2016-09-08 martijn err(1, "pledge");
459 71897761 2018-09-05 martijn syslog(LOG_AUTHPRIV | LOG_INFO, "%s edited %s from %s",
460 71897761 2018-09-05 martijn myname, file, cwd);
462 b6e88a9a 2020-12-18 martijn if ((eargv = reallocarray(NULL, arraylen((const char * const*)argv) + 2,
463 09a12e37 2016-09-08 martijn sizeof(*eargv))) == NULL) {
464 09a12e37 2016-09-08 martijn unlink(tmpfile);
465 09a12e37 2016-09-08 martijn err(1, NULL);
467 09a12e37 2016-09-08 martijn eargv[0] = getenv("EDITOR");
468 09a12e37 2016-09-08 martijn if (eargv[0] == NULL || *(eargv[0]) == '\0')
469 09a12e37 2016-09-08 martijn eargv[0] = "vi";
471 13ffdf3b 2017-09-24 martijn switch ((vipid = fork())) {
472 09a12e37 2016-09-08 martijn case -1:
473 09a12e37 2016-09-08 martijn unlink(tmpfile);
474 09a12e37 2016-09-08 martijn err(1, "fork failed");
476 09a12e37 2016-09-08 martijn if (pledge("stdio exec", NULL) == -1)
477 09a12e37 2016-09-08 martijn err(1, "pledge");
479 09a12e37 2016-09-08 martijn for (i = 0; argv[i] != NULL; i++)
480 09a12e37 2016-09-08 martijn eargv[i+1] = argv[i];
481 09a12e37 2016-09-08 martijn eargv[i+1] = tmpfile;
482 09a12e37 2016-09-08 martijn eargv[i+2] = NULL;
483 09a12e37 2016-09-08 martijn execvp(eargv[0], eargv);
484 09a12e37 2016-09-08 martijn err(1, "execvp failed");
485 09a12e37 2016-09-08 martijn default:
486 fe48b8a3 2020-12-18 martijn if (pledge("stdio cpath proc tty", NULL) == -1)
487 09a12e37 2016-09-08 martijn err(1, "pledge");
489 fe48b8a3 2020-12-18 martijn (void)setpgid(vipid, 0);
490 fe48b8a3 2020-12-18 martijn ttyfd = open("/dev/tty", O_RDWR);
491 fe48b8a3 2020-12-18 martijn if (ttyfd != -1)
492 fe48b8a3 2020-12-18 martijn (void)tcsetpgrp(ttyfd, vipid);
493 13ffdf3b 2017-09-24 martijn while ((ret = waitpid(vipid, &status, 0)) == -1 &&
494 13ffdf3b 2017-09-24 martijn errno == EINTR)
496 09a12e37 2016-09-08 martijn if (ret == -1)
497 09a12e37 2016-09-08 martijn err(1, "wait failed: Temporary file saved at %s",
498 09a12e37 2016-09-08 martijn tmpfile);
501 09a12e37 2016-09-08 martijn if (WEXITSTATUS(status) != 0) {
502 09a12e37 2016-09-08 martijn errx(1, "%s exited with status %d: Temporary file saved at %s",
503 09a12e37 2016-09-08 martijn eargv[0], WEXITSTATUS(status), tmpfile);
506 09a12e37 2016-09-08 martijn (void) lseek(tfd, 0, SEEK_SET);
507 09a12e37 2016-09-08 martijn if (ftruncate(ofd, 0) == -1)
508 09a12e37 2016-09-08 martijn err(1, "ftruncate failed: Temporary file saved at %s", tmpfile);
509 09a12e37 2016-09-08 martijn (void) lseek(ofd, 0, SEEK_SET);
510 09a12e37 2016-09-08 martijn if (!fcpy(ofd, tfd))
511 09a12e37 2016-09-08 martijn err(1, "restoring failed: Temporary file saved at %s", tmpfile);
512 09a12e37 2016-09-08 martijn if (unlink(tmpfile) == -1)
513 09a12e37 2016-09-08 martijn err(1, "unlink %s", tmpfile);
514 09a12e37 2016-09-08 martijn return 0;