initial commie

noproxy
Alessandro Mauri 4 years ago
commit e891693f6f
  1. 1
      README.md
  2. 25
      makefile
  3. 57
      us.c

@ -0,0 +1 @@
# US - User Switcher

@ -0,0 +1,25 @@
CC ?= gcc
CFLAGS = -Wall -pedantic --std=c99 -O2
PREFIX = /usr/local
MANPREFIX = ${PREFIX}/share/man
us: us.c
dbg:
gcc -O0 -g us.c -o us
install: us
mkdir -p ${DESTDIR}${PREFIX}/bin
cp -f us ${DESTDIR}${PREFIX}/bin/us
chown root:root ${DESTDIR}${PREFIX}/bin/us
chmod 4755 ${DESTDIR}${PREFIX}/bin/us
# mkdir -p ${DESTDIR}${MANPREFIX}/man1
# cp -f us.1 ${DESTDIR}${MANPREFIX}/man1/us.1
# chmod 644 ${DESTDIR}${MANPREFIX}/man1/us.1
uninstall:
rm -f ${DESTDIR}${PREFIX}/bin/us
# ${DESTDIR}${MANPREFIX}/man1/us.1
clean:
rm -f us us-dbg

57
us.c

@ -0,0 +1,57 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
static void usage (void);
static int perm_set (uid_t);
int main (int argc, char *argv[])
{
if (argc < 2) {
usage();
exit(1);
}
uid_t ruid = getuid();
// Copy argv and argc
int c_argc = argc - 1;
char **c_argv = malloc((c_argc + 1) * sizeof(char *));
if (!c_argv) {
fprintf(stderr, "malloc: %s\n", strerror(errno));
goto fail_end;
}
for (int i = 0; i < c_argc; i++)
c_argv[i] = strdup(argv[i+1]);
c_argc[c_argv] = NULL;
if (perm_set(0) == -1) { // 0 = root
fprintf(stderr, "perm_set: %s\n", strerror(errno));
goto fail_end;
}
if (execvp(*c_argv, c_argv) == -1) // execvp searches in path
fprintf(stderr, "execv: %s\n", strerror(errno));
// if exec fails reset the permissions
if (perm_set(ruid) == -1) { // 0 = root
fprintf(stderr, "perm_set: %s\n", strerror(errno));
goto fail_end;
}
fail_end:
return 0;
}
static inline void usage (void)
{
printf("usage: us [command]\n");
}
static inline int perm_set (uid_t id)
{
return seteuid(id);
}
Loading…
Cancel
Save