2 79c9b1b2 2016-09-08 martijn * Copyright (c) 2016 Martijn van Duren <vias@imperialat.at>
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 09a12e37 2016-09-08 martijn #include <string.h>
30 09a12e37 2016-09-08 martijn #include <stdio.h>
31 09a12e37 2016-09-08 martijn #include <stdlib.h>
32 09a12e37 2016-09-08 martijn #include <err.h>
33 09a12e37 2016-09-08 martijn #include <unistd.h>
34 09a12e37 2016-09-08 martijn #include <pwd.h>
35 09a12e37 2016-09-08 martijn #include <grp.h>
36 09a12e37 2016-09-08 martijn #include <syslog.h>
37 09a12e37 2016-09-08 martijn #include <errno.h>
39 09a12e37 2016-09-08 martijn #include "vias.h"
41 09a12e37 2016-09-08 martijn static void __dead
42 09a12e37 2016-09-08 martijn usage(void)
44 09a12e37 2016-09-08 martijn fprintf(stderr, "usage: vias [-a style] [-C config] "
45 09a12e37 2016-09-08 martijn "file [editor args]\n");
50 09a12e37 2016-09-08 martijn arraylen(const char **arr)
52 09a12e37 2016-09-08 martijn size_t cnt = 0;
54 09a12e37 2016-09-08 martijn while (*arr) {
58 09a12e37 2016-09-08 martijn return cnt;
61 09a12e37 2016-09-08 martijn static int
62 09a12e37 2016-09-08 martijn parseuid(const char *s, uid_t *uid)
64 09a12e37 2016-09-08 martijn struct passwd *pw;
65 09a12e37 2016-09-08 martijn const char *errstr;
67 09a12e37 2016-09-08 martijn if ((pw = getpwnam(s)) != NULL) {
68 09a12e37 2016-09-08 martijn *uid = pw->pw_uid;
69 09a12e37 2016-09-08 martijn return 0;
71 09a12e37 2016-09-08 martijn *uid = strtonum(s, 0, UID_MAX, &errstr);
72 09a12e37 2016-09-08 martijn if (errstr)
73 09a12e37 2016-09-08 martijn return -1;
74 09a12e37 2016-09-08 martijn return 0;
77 09a12e37 2016-09-08 martijn static int
78 09a12e37 2016-09-08 martijn uidcheck(const char *s, uid_t desired)
80 09a12e37 2016-09-08 martijn uid_t uid;
82 09a12e37 2016-09-08 martijn if (parseuid(s, &uid) != 0)
83 09a12e37 2016-09-08 martijn return -1;
84 09a12e37 2016-09-08 martijn if (uid != desired)
85 09a12e37 2016-09-08 martijn return -1;
86 09a12e37 2016-09-08 martijn return 0;
89 09a12e37 2016-09-08 martijn static int
90 09a12e37 2016-09-08 martijn parsegid(const char *s, gid_t *gid)
92 09a12e37 2016-09-08 martijn struct group *gr;
93 09a12e37 2016-09-08 martijn const char *errstr;
95 09a12e37 2016-09-08 martijn if ((gr = getgrnam(s)) != NULL) {
96 09a12e37 2016-09-08 martijn *gid = gr->gr_gid;
97 09a12e37 2016-09-08 martijn return 0;
99 09a12e37 2016-09-08 martijn *gid = strtonum(s, 0, GID_MAX, &errstr);
100 09a12e37 2016-09-08 martijn if (errstr)
101 09a12e37 2016-09-08 martijn return -1;
102 09a12e37 2016-09-08 martijn return 0;
105 09a12e37 2016-09-08 martijn static int
106 09a12e37 2016-09-08 martijn open_nosym(const char *file)
108 09a12e37 2016-09-08 martijn static int dep = 0;
109 09a12e37 2016-09-08 martijn struct stat sb;
110 09a12e37 2016-09-08 martijn char dir[PATH_MAX];
111 09a12e37 2016-09-08 martijn int fd, pfd;
114 09a12e37 2016-09-08 martijn (void) strlcpy(dir, dirname(file), sizeof(dir));
116 09a12e37 2016-09-08 martijn if (dir[1] == '\0') {
118 09a12e37 2016-09-08 martijn if ((pfd = open("/", O_CLOEXEC)) == -1)
119 09a12e37 2016-09-08 martijn return -1;
120 09a12e37 2016-09-08 martijn } else {
121 09a12e37 2016-09-08 martijn pfd = open_nosym(dir);
123 09a12e37 2016-09-08 martijn if (pfd == -1)
124 09a12e37 2016-09-08 martijn return -1;
128 09a12e37 2016-09-08 martijn if (dep) {
129 09a12e37 2016-09-08 martijn fd = openat(pfd, basename(file),
130 09a12e37 2016-09-08 martijn O_NOFOLLOW | O_CLOEXEC);
131 09a12e37 2016-09-08 martijn } else {
132 09a12e37 2016-09-08 martijn fd = openat(pfd, basename(file),
133 09a12e37 2016-09-08 martijn O_RDWR | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0777);
135 09a12e37 2016-09-08 martijn } while (fd == -1 && errno == EINTR);
136 09a12e37 2016-09-08 martijn close(pfd);
137 09a12e37 2016-09-08 martijn if (fd == -1)
138 09a12e37 2016-09-08 martijn return -1;
140 09a12e37 2016-09-08 martijn if (fstat(fd, &sb) == -1)
141 09a12e37 2016-09-08 martijn err(1, "fstat");
143 09a12e37 2016-09-08 martijn * This should already have been checked in parse.y.
144 09a12e37 2016-09-08 martijn * It's only here to test for race-conditions
146 09a12e37 2016-09-08 martijn if (S_ISLNK(sb.st_mode))
147 09a12e37 2016-09-08 martijn errx(1, "Symbolic links not allowed");
148 09a12e37 2016-09-08 martijn if (dep && !S_ISDIR(sb.st_mode))
149 09a12e37 2016-09-08 martijn errc(1, ENOTDIR, NULL);
150 09a12e37 2016-09-08 martijn if (!dep && !S_ISREG(sb.st_mode))
151 09a12e37 2016-09-08 martijn errx(1, "File is not a regular file");
153 09a12e37 2016-09-08 martijn return fd;
156 09a12e37 2016-09-08 martijn static int
157 09a12e37 2016-09-08 martijn match(uid_t uid, gid_t *groups, int ngroups, const char *file, struct rule *r)
160 09a12e37 2016-09-08 martijn int flen;
162 09a12e37 2016-09-08 martijn if (r->ident[0] == ':') {
163 09a12e37 2016-09-08 martijn gid_t rgid;
164 09a12e37 2016-09-08 martijn if (parsegid(r->ident + 1, &rgid) == -1)
165 09a12e37 2016-09-08 martijn return 0;
166 09a12e37 2016-09-08 martijn for (i = 0; i < ngroups; i++) {
167 09a12e37 2016-09-08 martijn if (rgid == groups[i])
170 09a12e37 2016-09-08 martijn if (i == ngroups)
171 09a12e37 2016-09-08 martijn return 0;
172 09a12e37 2016-09-08 martijn } else {
173 09a12e37 2016-09-08 martijn if (uidcheck(r->ident, uid) != 0)
174 09a12e37 2016-09-08 martijn return 0;
176 09a12e37 2016-09-08 martijn if (r->files != NULL) {
177 09a12e37 2016-09-08 martijn for (i = 0; r->files[i] != NULL; i++) {
178 09a12e37 2016-09-08 martijn flen = strlen(r->files[i]);
179 09a12e37 2016-09-08 martijn /* Allow access to the entire directory tree */
180 09a12e37 2016-09-08 martijn if (r->files[i][flen-1] == '/') {
181 09a12e37 2016-09-08 martijn if (!strncmp(r->files[i], file, flen))
183 09a12e37 2016-09-08 martijn } else {
184 09a12e37 2016-09-08 martijn if (!strcmp(r->files[i], file))
188 09a12e37 2016-09-08 martijn if (r->files[i] == NULL)
189 09a12e37 2016-09-08 martijn return 0;
191 09a12e37 2016-09-08 martijn return 1;
194 09a12e37 2016-09-08 martijn static int
195 09a12e37 2016-09-08 martijn permit(uid_t uid, gid_t *groups, int ngroups, struct rule **lastr,
196 09a12e37 2016-09-08 martijn const char *file)
199 09a12e37 2016-09-08 martijn int fd = -1, pfd = -1;
200 09a12e37 2016-09-08 martijn uid_t suid = -1;
201 09a12e37 2016-09-08 martijn struct rule *r;
202 09a12e37 2016-09-08 martijn char *rfile;
204 09a12e37 2016-09-08 martijn if ((rfile = realpath(file, NULL)) == NULL)
205 09a12e37 2016-09-08 martijn err(1, "Unable to open %s", file);
207 09a12e37 2016-09-08 martijn *lastr = NULL;
208 09a12e37 2016-09-08 martijn for (i = 0; i < nrules; i++) {
209 09a12e37 2016-09-08 martijn r = rules[i];
210 09a12e37 2016-09-08 martijn if (match(uid, groups, ngroups, rfile, r)) {
211 09a12e37 2016-09-08 martijn /* Try to open the file, so we know the rule is really permitted */
212 09a12e37 2016-09-08 martijn if (r->target) {
213 09a12e37 2016-09-08 martijn if (parseuid(r->target, &suid) == -1)
214 09a12e37 2016-09-08 martijn err(1, "getpwnam");
215 09a12e37 2016-09-08 martijn if (seteuid(suid) == -1)
216 09a12e37 2016-09-08 martijn err(1, "seteuid");
218 09a12e37 2016-09-08 martijn if ((fd = open_nosym(rfile)) != -1) {
219 09a12e37 2016-09-08 martijn if (pfd != -1)
220 09a12e37 2016-09-08 martijn if (close(pfd) == -1)
221 09a12e37 2016-09-08 martijn err(1, "close");
222 09a12e37 2016-09-08 martijn pfd = fd;
223 09a12e37 2016-09-08 martijn *lastr = rules[i];
224 09a12e37 2016-09-08 martijn } else if (errno != EPERM) {
225 09a12e37 2016-09-08 martijn err(1, "open %s", file);
227 09a12e37 2016-09-08 martijn if (r->target && seteuid(0))
228 09a12e37 2016-09-08 martijn err(1, "seteuid");
231 09a12e37 2016-09-08 martijn if (*lastr == NULL || (*lastr)->action != PERMIT) {
232 95866557 2017-09-25 martijn if (fd != -1 && close(fd) == -1)
233 76b35bc9 2017-09-25 martijn err(1, "close");
234 09a12e37 2016-09-08 martijn return -1;
237 09a12e37 2016-09-08 martijn return fd;
240 09a12e37 2016-09-08 martijn static void
241 09a12e37 2016-09-08 martijn parseconfig(const char *filename, int checkperms)
243 09a12e37 2016-09-08 martijn extern FILE *yyfp;
244 09a12e37 2016-09-08 martijn extern int yyparse(void);
245 09a12e37 2016-09-08 martijn struct stat sb;
247 09a12e37 2016-09-08 martijn yyfp = fopen(filename, "r");
248 09a12e37 2016-09-08 martijn if (!yyfp)
249 09a12e37 2016-09-08 martijn err(1, checkperms ? "vias is not enabled, %s" :
250 09a12e37 2016-09-08 martijn "could not open config file %s", filename);
252 09a12e37 2016-09-08 martijn if (checkperms) {
253 09a12e37 2016-09-08 martijn if (fstat(fileno(yyfp), &sb) != 0)
254 09a12e37 2016-09-08 martijn err(1, "fstat(\"%s\")", filename);
255 09a12e37 2016-09-08 martijn if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
256 09a12e37 2016-09-08 martijn errx(1, "%s is writable by group or other", filename);
257 09a12e37 2016-09-08 martijn if (sb.st_uid != 0)
258 09a12e37 2016-09-08 martijn errx(1, "%s is not owned by root", filename);
261 09a12e37 2016-09-08 martijn yyparse();
262 09a12e37 2016-09-08 martijn fclose(yyfp);
263 09a12e37 2016-09-08 martijn if (parse_errors)
264 09a12e37 2016-09-08 martijn exit(1);
267 09a12e37 2016-09-08 martijn static void __dead
268 09a12e37 2016-09-08 martijn checkconfig(const char *confpath, uid_t uid, gid_t *groups, int ngroups,
269 09a12e37 2016-09-08 martijn const char *file)
271 09a12e37 2016-09-08 martijn struct rule *rule;
273 09a12e37 2016-09-08 martijn parseconfig(confpath, 0);
274 09a12e37 2016-09-08 martijn if (!file)
275 09a12e37 2016-09-08 martijn exit(0);
277 09a12e37 2016-09-08 martijn if (permit(uid, groups, ngroups, &rule, file) != -1) {
278 09a12e37 2016-09-08 martijn printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
279 09a12e37 2016-09-08 martijn exit(0);
281 09a12e37 2016-09-08 martijn printf("deny\n");
282 09a12e37 2016-09-08 martijn exit(1);
285 09a12e37 2016-09-08 martijn static void
286 09a12e37 2016-09-08 martijn authuser(char *myname, char *login_style, int persist)
288 09a12e37 2016-09-08 martijn char *challenge = NULL, *response, rbuf[1024], cbuf[128];
289 09a12e37 2016-09-08 martijn auth_session_t *as;
290 09a12e37 2016-09-08 martijn int fd = -1;
292 09a12e37 2016-09-08 martijn if (persist)
293 09a12e37 2016-09-08 martijn fd = open("/dev/tty", O_RDWR);
294 09a12e37 2016-09-08 martijn if (fd != -1) {
295 09a12e37 2016-09-08 martijn if (ioctl(fd, TIOCCHKVERAUTH) == 0)
296 09a12e37 2016-09-08 martijn goto good;
299 09a12e37 2016-09-08 martijn if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
300 09a12e37 2016-09-08 martijn &challenge)))
301 09a12e37 2016-09-08 martijn errx(1, "Authorization failed");
302 09a12e37 2016-09-08 martijn if (!challenge) {
303 09a12e37 2016-09-08 martijn char host[HOST_NAME_MAX + 1];
304 09a12e37 2016-09-08 martijn if (gethostname(host, sizeof(host)))
305 09a12e37 2016-09-08 martijn snprintf(host, sizeof(host), "?");
306 09a12e37 2016-09-08 martijn snprintf(cbuf, sizeof(cbuf),
307 09a12e37 2016-09-08 martijn "\rvias (%.32s@%.32s) password: ", myname, host);
308 09a12e37 2016-09-08 martijn challenge = cbuf;
310 09a12e37 2016-09-08 martijn response = readpassphrase(challenge, rbuf, sizeof(rbuf),
311 09a12e37 2016-09-08 martijn RPP_REQUIRE_TTY);
312 09a12e37 2016-09-08 martijn if (response == NULL && errno == ENOTTY) {
313 09a12e37 2016-09-08 martijn syslog(LOG_AUTHPRIV | LOG_NOTICE,
314 09a12e37 2016-09-08 martijn "tty required for %s", myname);
315 09a12e37 2016-09-08 martijn errx(1, "a tty is required");
317 09a12e37 2016-09-08 martijn if (!auth_userresponse(as, response, 0)) {
318 09a12e37 2016-09-08 martijn syslog(LOG_AUTHPRIV | LOG_NOTICE,
319 09a12e37 2016-09-08 martijn "failed auth for %s", myname);
320 09a12e37 2016-09-08 martijn errc(1, EPERM, NULL);
322 09a12e37 2016-09-08 martijn explicit_bzero(rbuf, sizeof(rbuf));
324 09a12e37 2016-09-08 martijn if (fd != -1) {
325 09a12e37 2016-09-08 martijn int secs = 5 * 60;
326 09a12e37 2016-09-08 martijn ioctl(fd, TIOCSETVERAUTH, &secs);
327 09a12e37 2016-09-08 martijn close(fd);
330 09a12e37 2016-09-08 martijn static int
331 09a12e37 2016-09-08 martijn fcpy(int dfd, int sfd)
333 09a12e37 2016-09-08 martijn unsigned char buf[4096];
337 09a12e37 2016-09-08 martijn while ((r = read(sfd, buf, sizeof(buf))) > 0) {
338 09a12e37 2016-09-08 martijn if (write(dfd, buf, r) != r)
339 09a12e37 2016-09-08 martijn return 0;
341 09a12e37 2016-09-08 martijn } while (r == -1 && errno == EINTR);
342 09a12e37 2016-09-08 martijn if (r == -1)
343 09a12e37 2016-09-08 martijn return 0;
344 09a12e37 2016-09-08 martijn return 1;
348 09a12e37 2016-09-08 martijn main(int argc, char **argv)
350 09a12e37 2016-09-08 martijn const char *confpath = NULL;
351 09a12e37 2016-09-08 martijn char tmpfile[] = "/tmp/vias.XXXXXX";
352 09a12e37 2016-09-08 martijn char myname[_PW_NAME_LEN + 1];
353 09a12e37 2016-09-08 martijn struct passwd *pw;
354 09a12e37 2016-09-08 martijn struct rule *rule;
355 09a12e37 2016-09-08 martijn uid_t uid;
356 09a12e37 2016-09-08 martijn gid_t groups[NGROUPS_MAX + 1];
357 09a12e37 2016-09-08 martijn int ngroups;
358 09a12e37 2016-09-08 martijn int i, ch;
359 09a12e37 2016-09-08 martijn int ofd, tfd;
360 09a12e37 2016-09-08 martijn char cwdpath[PATH_MAX];
361 09a12e37 2016-09-08 martijn const char *cwd;
362 09a12e37 2016-09-08 martijn char *login_style = NULL;
363 09a12e37 2016-09-08 martijn char *file;
364 09a12e37 2016-09-08 martijn char **eargv;
365 09a12e37 2016-09-08 martijn int status;
366 13ffdf3b 2017-09-24 martijn pid_t ret, vipid;
368 09a12e37 2016-09-08 martijn setprogname("vias");
370 09a12e37 2016-09-08 martijn closefrom(STDERR_FILENO + 1);
372 09a12e37 2016-09-08 martijn uid = getuid();
374 09a12e37 2016-09-08 martijn while ((ch = getopt(argc, argv, "a:C:")) != -1) {
375 09a12e37 2016-09-08 martijn switch (ch) {
376 09a12e37 2016-09-08 martijn case 'a':
377 09a12e37 2016-09-08 martijn login_style = optarg;
379 09a12e37 2016-09-08 martijn case 'C':
380 09a12e37 2016-09-08 martijn confpath = optarg;
382 09a12e37 2016-09-08 martijn default:
383 09a12e37 2016-09-08 martijn usage();
386 09a12e37 2016-09-08 martijn argv += optind;
387 09a12e37 2016-09-08 martijn argc -= optind;
389 347af3ca 2017-09-25 martijn if (argc == 0 && confpath == NULL)
390 09a12e37 2016-09-08 martijn usage();
391 09a12e37 2016-09-08 martijn file = argv[0];
395 09a12e37 2016-09-08 martijn pw = getpwuid(uid);
396 09a12e37 2016-09-08 martijn if (!pw)
397 09a12e37 2016-09-08 martijn err(1, "getpwuid failed");
398 09a12e37 2016-09-08 martijn if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
399 09a12e37 2016-09-08 martijn errx(1, "pw_name too long");
400 09a12e37 2016-09-08 martijn ngroups = getgroups(NGROUPS_MAX, groups);
401 09a12e37 2016-09-08 martijn if (ngroups == -1)
402 09a12e37 2016-09-08 martijn err(1, "can't get groups");
403 09a12e37 2016-09-08 martijn groups[ngroups++] = getgid();
405 09a12e37 2016-09-08 martijn if (confpath)
406 09a12e37 2016-09-08 martijn checkconfig(confpath, uid, groups, ngroups, file);
408 332482f3 2017-09-25 martijn if (geteuid())
409 332482f3 2017-09-25 martijn errx(1, "not installed setuid");
411 09a12e37 2016-09-08 martijn parseconfig("/etc/vias.conf", 1);
413 73964625 2017-09-24 martijn if (setuid(0) == -1)
414 73964625 2017-09-24 martijn err(1, "setuid");
415 09a12e37 2016-09-08 martijn if ((ofd = permit(uid, groups, ngroups, &rule, file)) == -1) {
416 09a12e37 2016-09-08 martijn syslog(LOG_AUTHPRIV | LOG_NOTICE,
417 09a12e37 2016-09-08 martijn "failed edit for %s: %s", myname, file);
418 08393614 2017-09-25 martijn errc(1, EPERM, "%s", file);
421 73964625 2017-09-24 martijn if (setreuid(uid, 0) == -1)
422 73964625 2017-09-24 martijn err(1, "setreuid failed");
424 09a12e37 2016-09-08 martijn if (!(rule->options & NOPASS))
425 09a12e37 2016-09-08 martijn authuser(myname, login_style, rule->options & PERSIST);
427 09a12e37 2016-09-08 martijn if (pledge("stdio rpath wpath cpath exec proc id", NULL) == -1)
428 09a12e37 2016-09-08 martijn err(1, "pledge");
430 09a12e37 2016-09-08 martijn if ((setuid(uid)) == -1)
431 09a12e37 2016-09-08 martijn err(1, "setuid failed");
433 09a12e37 2016-09-08 martijn if (pledge("stdio rpath wpath cpath exec proc", NULL) == -1)
434 09a12e37 2016-09-08 martijn err(1, "pledge");
436 09a12e37 2016-09-08 martijn if ((tfd = mkstemp(tmpfile)) == -1)
437 09a12e37 2016-09-08 martijn err(1, "mkstemp failed");
439 09a12e37 2016-09-08 martijn if (pledge("stdio rpath cpath exec proc", NULL) == -1)
440 09a12e37 2016-09-08 martijn err(1, "pledge");
442 09a12e37 2016-09-08 martijn if (!fcpy(tfd, ofd)) {
443 09a12e37 2016-09-08 martijn unlink(tmpfile);
444 09a12e37 2016-09-08 martijn err(1, "temp copy failed");
447 09a12e37 2016-09-08 martijn if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
448 09a12e37 2016-09-08 martijn cwd = "(failed)";
450 09a12e37 2016-09-08 martijn cwd = cwdpath;
452 09a12e37 2016-09-08 martijn if (pledge("stdio cpath exec proc", NULL) == -1)
453 09a12e37 2016-09-08 martijn err(1, "pledge");
455 09a12e37 2016-09-08 martijn syslog(LOG_AUTHPRIV | LOG_INFO, "%s edited %s as %s from %s",
456 09a12e37 2016-09-08 martijn myname, file, pw->pw_name, cwd);
458 09a12e37 2016-09-08 martijn if ((eargv = reallocarray(NULL, arraylen((const char **) argv) + 2,
459 09a12e37 2016-09-08 martijn sizeof(*eargv))) == NULL) {
460 09a12e37 2016-09-08 martijn unlink(tmpfile);
461 09a12e37 2016-09-08 martijn err(1, NULL);
463 09a12e37 2016-09-08 martijn eargv[0] = getenv("EDITOR");
464 09a12e37 2016-09-08 martijn if (eargv[0] == NULL || *(eargv[0]) == '\0')
465 09a12e37 2016-09-08 martijn eargv[0] = "vi";
467 13ffdf3b 2017-09-24 martijn switch ((vipid = fork())) {
468 09a12e37 2016-09-08 martijn case -1:
469 09a12e37 2016-09-08 martijn unlink(tmpfile);
470 09a12e37 2016-09-08 martijn err(1, "fork failed");
472 09a12e37 2016-09-08 martijn if (pledge("stdio exec", NULL) == -1)
473 09a12e37 2016-09-08 martijn err(1, "pledge");
475 09a12e37 2016-09-08 martijn for (i = 0; argv[i] != NULL; i++)
476 09a12e37 2016-09-08 martijn eargv[i+1] = argv[i];
477 09a12e37 2016-09-08 martijn eargv[i+1] = tmpfile;
478 09a12e37 2016-09-08 martijn eargv[i+2] = NULL;
479 09a12e37 2016-09-08 martijn execvp(eargv[0], eargv);
480 09a12e37 2016-09-08 martijn err(1, "execvp failed");
481 09a12e37 2016-09-08 martijn default:
482 09a12e37 2016-09-08 martijn if (pledge("stdio cpath", NULL) == -1)
483 09a12e37 2016-09-08 martijn err(1, "pledge");
485 13ffdf3b 2017-09-24 martijn while ((ret = waitpid(vipid, &status, 0)) == -1 &&
486 13ffdf3b 2017-09-24 martijn errno == EINTR)
488 09a12e37 2016-09-08 martijn if (ret == -1)
489 09a12e37 2016-09-08 martijn err(1, "wait failed: Temporary file saved at %s",
490 09a12e37 2016-09-08 martijn tmpfile);
493 09a12e37 2016-09-08 martijn if (WEXITSTATUS(status) != 0) {
494 09a12e37 2016-09-08 martijn errx(1, "%s exited with status %d: Temporary file saved at %s",
495 09a12e37 2016-09-08 martijn eargv[0], WEXITSTATUS(status), tmpfile);
498 09a12e37 2016-09-08 martijn (void) lseek(tfd, 0, SEEK_SET);
499 09a12e37 2016-09-08 martijn if (ftruncate(ofd, 0) == -1)
500 09a12e37 2016-09-08 martijn err(1, "ftruncate failed: Temporary file saved at %s", tmpfile);
501 09a12e37 2016-09-08 martijn (void) lseek(ofd, 0, SEEK_SET);
502 09a12e37 2016-09-08 martijn if (!fcpy(ofd, tfd))
503 09a12e37 2016-09-08 martijn err(1, "restoring failed: Temporary file saved at %s", tmpfile);
504 09a12e37 2016-09-08 martijn if (unlink(tmpfile) == -1)
505 09a12e37 2016-09-08 martijn err(1, "unlink %s", tmpfile);
506 09a12e37 2016-09-08 martijn return 0;