projects/aports·blob v2.16.0 · refs

94 lines · 2.0 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
72 grent = getgrnam(ABUILD_GROUP);
73 if (grent == NULL)
74 errx(1, "%s: Group not found", ABUILD_GROUP);
75
76 if (!is_in_group(grent->gr_gid))
77 errx(1, "Not a member of group %s\n", ABUILD_GROUP);
78
79 cmd = strrchr(argv[0], '-');
80 if (cmd == NULL)
81 errx(1, "Calling command has no '-'");
82 cmd++;
83
84 path = get_command_path(cmd);
85 if (path == NULL)
86 errx(1, "%s: Not a valid subcommand", cmd);
87
88 argv[0] = path;
89 /* set our uid to root so bbsuid --install works */
90 setuid(0);
91 execv(path, (char * const*)argv);
92 perror(path);
93 return 1;
94}