| 1 | |
| 2 | * abuild-rmtemp
|
| 3 | * Copyright (c) 2017 Kaarle Ritvanen
|
| 4 | * Distributed under GPL-2
|
| 5 |
|
| 6 |
|
| 7 | #define _XOPEN_SOURCE 700
|
| 8 | #include <err.h>
|
| 9 | #include <errno.h>
|
| 10 | #include <ftw.h>
|
| 11 | #include <pwd.h>
|
| 12 | #include <stdio.h>
|
| 13 | #include <stdlib.h>
|
| 14 | #include <string.h>
|
| 15 | #include <sys/stat.h>
|
| 16 | #include <sys/types.h>
|
| 17 | #include <unistd.h>
|
| 18 |
|
| 19 | #define PREFIX "/var/tmp/abuild."
|
| 20 |
|
| 21 | static void fail() {
|
| 22 | errx(1, "%s", strerror(errno));
|
| 23 | }
|
| 24 |
|
| 25 | static int handler(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
|
| 26 | return remove(fpath);
|
| 27 | }
|
| 28 |
|
| 29 | int main(int argc, char **argv) {
|
| 30 | if (argc < 2) return 0;
|
| 31 |
|
| 32 | if (getuid()) {
|
| 33 | argv[0] = "-abuild-rmtemp";
|
| 34 | execv("/usr/bin/abuild-sudo", argv);
|
| 35 | }
|
| 36 |
|
| 37 | if (strncmp(argv[1], PREFIX, strlen(PREFIX)) || \
|
| 38 | strchr(argv[1] + strlen(PREFIX), '/'))
|
| 39 | errx(1, "Invalid path: %s", argv[1]);
|
| 40 |
|
| 41 | struct stat s;
|
| 42 | if (lstat(argv[1], &s)) fail();
|
| 43 | struct passwd *p = getpwnam(getenv("USER"));
|
| 44 | if (!p) errx(1, "Incorrect user");
|
| 45 | if (s.st_uid != p->pw_uid) errx(1, "Permission denied");
|
| 46 |
|
| 47 | if (nftw(argv[1], handler, 512, FTW_DEPTH|FTW_PHYS)) fail();
|
| 48 |
|
| 49 | return 0;
|
| 50 | }
|