60762e4 |
Jamozed |
2022-09-11 15:06:17 |
0
|
// relogin.c, version 0.1.0 |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
1
|
// OMKOV coreutils relogin |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
2
|
// Copyright (C) 2022, Jakob Wakeling |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
3
|
// MIT Licence |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
4
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
5
|
#include "util/error.h" |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
6
|
#include "util/optget.h" |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
7
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
8
|
#include <grp.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
9
|
#include <pwd.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
10
|
#include <sys/types.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
11
|
#include <unistd.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
12
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
13
|
#include <errno.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
14
|
#include <stdarg.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
15
|
#include <stdio.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
16
|
#include <stdlib.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
17
|
#include <stdnoreturn.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
18
|
#include <string.h> |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
19
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
20
|
#define VERSION "0.1.0" |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
21
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
22
|
static struct lop lops[] = { |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
23
|
{ "help", ARG_NUL, 256 }, |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
24
|
{ "version", ARG_NUL, 257 }, |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
25
|
{ NULL, 0, 0 } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
26
|
}; |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
27
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
28
|
static inline int relogin(const char *user, char *av[]); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
29
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
30
|
static void hlp(void); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
31
|
static void ver(void); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
32
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
33
|
int main(int ac, char *av[]) { A0 = av[0]; |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
34
|
struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
35
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
36
|
case 256: { hlp(); return 0; } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
37
|
case 257: { ver(); return 0; } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
38
|
default: { return 1; } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
39
|
} |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
40
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
41
|
if (opt.ind + 0 >= ac) { error(1, "missing user operand"); } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
42
|
if (opt.ind + 1 >= ac) { error(1, "missing command operand"); } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
43
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
44
|
if (relogin(av[1], &av[2])) { error(1, "%s: %s\n", av[1], serr()); } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
45
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
46
|
return 0; |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
47
|
} |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
48
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
49
|
static inline int relogin(const char *user, char *av[]) { |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
50
|
struct passwd *pw; |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
51
|
if ((pw = getpwnam(user)) == NULL) { error(1, "%s", serr()); } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
52
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
53
|
if (initgroups(pw->pw_name, pw->pw_gid) == -1) { error(1, "%s", serr()); } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
54
|
if (setgid(pw->pw_gid) == -1) { error(1, "%s", serr()); } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
55
|
if (setuid(pw->pw_uid) == -1) { error(1, "%s", serr()); } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
56
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
57
|
setenv("HOME", pw->pw_dir, 1); setenv("LOGNAME", pw->pw_name, 1); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
58
|
setenv("USER", pw->pw_name, 1); unsetenv("MAIL"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
59
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
60
|
unsetenv("DOAS_USER"); unsetenv("SUDO_UID"); unsetenv("SUDO_GID"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
61
|
unsetenv("SUDO_USER"); unsetenv("SUDO_COMMAND"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
62
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
63
|
if (execvp(av[0], &av[0]) == -1) { error(1, "%s", serr()); } |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
64
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
65
|
return 0; |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
66
|
} |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
67
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
68
|
static void hlp(void) { |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
69
|
puts("relogin - invoke a command as a specified user\n"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
70
|
puts("usage: relogin user command [argument...]\n"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
71
|
puts("options:"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
72
|
puts(" --help Display help information"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
73
|
puts(" --version Display version information"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
74
|
} |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
75
|
|
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
76
|
static void ver() { |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
77
|
puts("OMKOV coreutils relogin, version " VERSION); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
78
|
puts("Copyright (C) 2022, Jakob Wakeling"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
79
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
60762e4 |
Jamozed |
2022-09-11 15:06:17 |
80
|
} |
|
|
|
81
|
|