-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudo.c
More file actions
37 lines (33 loc) · 852 Bytes
/
sudo.c
File metadata and controls
37 lines (33 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int sudouid(uid_t uid) {
struct passwd *pwd = getpwuid(uid);
if (pwd == NULL)
return 0;
struct group *grp = getgrnam("sudo");
if (grp == NULL)
return 0;
for (size_t i = 0; grp->gr_mem[i]; i++)
if (strcmp(grp->gr_mem[i], pwd->pw_name) == 0)
return 1;
return 0;
}
int main(int argc, char **argv) {
if (argc <= 1) {
fprintf(stderr, "usage: %s <command>\n", argv[0]);
return EXIT_FAILURE;
}
uid_t uid = getuid();
if (uid != 0 && !sudouid(uid)) {
fprintf(stderr, "(%s: user not in 'sudo' group)\n", argv[0]);
return EXIT_FAILURE;
}
execvp(argv[1], &argv[1]);
fprintf(stderr, "(%s: cannot exec %s: %s)\n", argv[0], argv[1], strerror(errno));
abort();
}