projects/aports·blob v2.13.0 · refs

81 lines · 1.5 KiB

1
2#include <sys/types.h>
3
4#include <err.h>
5#include <grp.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10
11#ifndef ABUILD_GROUP
12#define ABUILD_GROUP "abuild"
13#endif
14
15static const char* valid_cmds[] = {
16 "/bin/adduser",
17 "/bin/addgroup",
18 "/sbin/apk",
19 NULL
20};
21
22const char *get_command_path(const char *cmd)
23{
24 const char *p;
25 int i;
26 for (i = 0; valid_cmds[i] != NULL; i++) {
27 p = strrchr(valid_cmds[i], '/') + 1;
28 if (strcmp(p, cmd) == 0)
29 return valid_cmds[i];
30 }
31 return NULL;
32}
33
34int is_in_group(gid_t group)
35{
36 int ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
37 gid_t *buf = malloc(ngroups_max * sizeof(gid_t));
38 int ngroups, ret = 0;
39 int i;
40 if (buf == NULL) {
41 perror("malloc");
42 return 0;
43 }
44 ngroups = getgroups(ngroups_max, buf);
45 for (i = 0; i < ngroups; i++) {
46 if (buf[i] == group)
47 break;
48 }
49 free(buf);
50 return i < ngroups;
51}
52
53
54int main(int argc, const char *argv[])
55{
56 struct group *grent;
57 const char *cmd;
58 const char *path;
59
60 grent = getgrnam(ABUILD_GROUP);
61 if (grent == NULL)
62 errx(1, "%s: Group not found", ABUILD_GROUP);
63
64 if (!is_in_group(grent->gr_gid))
65 errx(1, "Not a member of group %s\n", ABUILD_GROUP);
66
67 cmd = strrchr(argv[0], '-');
68 if (cmd == NULL)
69 errx(1, "Calling command has no '-'");
70 cmd++;
71
72 path = get_command_path(cmd);
73 if (path == NULL)
74 errx(1, "%s: Not a valid subcommand", cmd);
75
76 argv[0] = path;
77 /* set our uid to root soo bbsuid --install works */
78 setuid(0);
79 execv(path, (char * const*)argv);
80 return 1;
81}