3 changed files with 86 additions and 3 deletions
@ -0,0 +1,81 @@
|
||||
// relogin.c, version 0.1.0
|
||||
// OMKOV coreutils relogin
|
||||
// Copyright (C) 2022, Jakob Wakeling
|
||||
// MIT Licence
|
||||
|
||||
#include "util/error.h" |
||||
#include "util/optget.h" |
||||
|
||||
#include <grp.h> |
||||
#include <pwd.h> |
||||
#include <sys/types.h> |
||||
#include <unistd.h> |
||||
|
||||
#include <errno.h> |
||||
#include <stdarg.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <stdnoreturn.h> |
||||
#include <string.h> |
||||
|
||||
#define VERSION "0.1.0" |
||||
|
||||
static struct lop lops[] = { |
||||
{ "help", ARG_NUL, 256 }, |
||||
{ "version", ARG_NUL, 257 }, |
||||
{ NULL, 0, 0 } |
||||
}; |
||||
|
||||
static inline int relogin(const char *user, char *av[]); |
||||
|
||||
static void hlp(void); |
||||
static void ver(void); |
||||
|
||||
int main(int ac, char *av[]) { A0 = av[0]; |
||||
struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; |
||||
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
||||
case 256: { hlp(); return 0; } |
||||
case 257: { ver(); return 0; } |
||||
default: { return 1; } |
||||
} |
||||
|
||||
if (opt.ind + 0 >= ac) { error(1, "missing user operand"); } |
||||
if (opt.ind + 1 >= ac) { error(1, "missing command operand"); } |
||||
|
||||
if (relogin(av[1], &av[2])) { error(1, "%s: %s\n", av[1], serr()); } |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static inline int relogin(const char *user, char *av[]) { |
||||
struct passwd *pw; |
||||
if ((pw = getpwnam(user)) == NULL) { error(1, "%s", serr()); } |
||||
|
||||
if (initgroups(pw->pw_name, pw->pw_gid) == -1) { error(1, "%s", serr()); } |
||||
if (setgid(pw->pw_gid) == -1) { error(1, "%s", serr()); } |
||||
if (setuid(pw->pw_uid) == -1) { error(1, "%s", serr()); } |
||||
|
||||
setenv("HOME", pw->pw_dir, 1); setenv("LOGNAME", pw->pw_name, 1); |
||||
setenv("USER", pw->pw_name, 1); unsetenv("MAIL"); |
||||
|
||||
unsetenv("DOAS_USER"); unsetenv("SUDO_UID"); unsetenv("SUDO_GID"); |
||||
unsetenv("SUDO_USER"); unsetenv("SUDO_COMMAND"); |
||||
|
||||
if (execvp(av[0], &av[0]) == -1) { error(1, "%s", serr()); } |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static void hlp(void) { |
||||
puts("relogin - invoke a command as a specified user\n"); |
||||
puts("usage: relogin user command [argument...]\n"); |
||||
puts("options:"); |
||||
puts(" --help Display help information"); |
||||
puts(" --version Display version information"); |
||||
} |
||||
|
||||
static void ver() { |
||||
puts("OMKOV coreutils relogin, version " VERSION); |
||||
puts("Copyright (C) 2022, Jakob Wakeling"); |
||||
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
||||
} |
Loading…
Reference in new issue