projects/aports·blob v2.23.0 · refs

100 lines · 2.1 KiB

1/* abuild-sudo.c - limited root privileges for users in "abuild" group
2 *
3 * Copyright (C) 2012 Natanael Copa <[email protected]>
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation. See http://www.gnu.org/ for details.
9 */
10
11#include <sys/types.h>
12
13#include <err.h>
14#include <grp.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#ifndef ABUILD_GROUP
21#define ABUILD_GROUP "abuild"
22#endif
23
24static const char* valid_cmds[] = {
25 "/bin/adduser",
26 "/usr/sbin/adduser",
27 "/bin/addgroup",
28 "/usr/sbin/addgroup",
29 "/sbin/apk",
30 NULL
31};
32
33const char *get_command_path(const char *cmd)
34{
35 const char *p;
36 int i;
37 for (i = 0; valid_cmds[i] != NULL; i++) {
38 if (access(valid_cmds[i], F_OK) == -1)
39 continue;
40 p = strrchr(valid_cmds[i], '/') + 1;
41 if (strcmp(p, cmd) == 0)
42 return valid_cmds[i];
43 }
44 return NULL;
45}
46
47int is_in_group(gid_t group)
48{
49 int ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
50 gid_t *buf = malloc(ngroups_max * sizeof(gid_t));
51 int ngroups, ret = 0;
52 int i;
53 if (buf == NULL) {
54 perror("malloc");
55 return 0;
56 }
57 ngroups = getgroups(ngroups_max, buf);
58 for (i = 0; i < ngroups; i++) {
59 if (buf[i] == group)
60 break;
61 }
62 free(buf);
63 return i < ngroups;
64}
65
66int main(int argc, const char *argv[])
67{
68 struct group *grent;
69 const char *cmd;
70 const char *path;
71 int i;
72
73 grent = getgrnam(ABUILD_GROUP);
74 if (grent == NULL)
75 errx(1, "%s: Group not found", ABUILD_GROUP);
76
77 if (!is_in_group(grent->gr_gid))
78 errx(1, "Not a member of group %s\n", ABUILD_GROUP);
79
80 cmd = strrchr(argv[0], '-');
81 if (cmd == NULL)
82 errx(1, "Calling command has no '-'");
83 cmd++;
84
85 path = get_command_path(cmd);
86 if (path == NULL)
87 errx(1, "%s: Not a valid subcommand", cmd);
88
89 /* we dont allow --allow-untrusted option */
90 for (i = 1; i < argc; i++)
91 if (strcmp(argv[i], "--allow-untrusted") == 0)
92 errx(1, "%s: not allowed option", "--allow-untrusted");
93
94 argv[0] = path;
95 /* set our uid to root so bbsuid --install works */
96 setuid(0);
97 execv(path, (char * const*)argv);
98 perror(path);
99 return 1;
100}