commit ff2e11638f03fb259b0d698996e998bd7a431974
Author: sin <s...@2f30.org>
Date:   Thu Oct 17 23:02:55 2013 +0100

    Add initial su(1)

diff --git a/Makefile b/Makefile
index 7dd2704..d9ff21e 100644
--- a/Makefile
+++ b/Makefile
@@ -36,6 +36,7 @@ SRC = \
        pivot_root.c        \
        ps.c                \
        rmmod.c             \
+       su.c                \
        swapoff.c           \
        swapon.c            \
        truncate.c          \
diff --git a/config.mk b/config.mk
index 5c62469..34d1efe 100644
--- a/config.mk
+++ b/config.mk
@@ -10,10 +10,10 @@ MANPREFIX = $(PREFIX)/share/man
 LD = $(CC)
 CPPFLAGS = -D_BSD_SOURCE -D_GNU_SOURCE
 CFLAGS   = -g -ansi -Wall -Wno-long-long $(CPPFLAGS)
-LDFLAGS  = -g
+LDFLAGS  = -g -lcrypt
 
 #CC = tcc
 #LD = $(CC)
 #CPPFLAGS = -D_BSD_SOURCE -D_GNU_SOURCE
 #CFLAGS   = -Os -Wall $(CPPFLAGS)
-#LDFLAGS  =
+#LDFLAGS  = -lcrypt
diff --git a/su.c b/su.c
new file mode 100644
index 0000000..d2e28e8
--- /dev/null
+++ b/su.c
@@ -0,0 +1,94 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <pwd.h>
+#include <grp.h>
+#include <shadow.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "util.h"
+
+static void
+usage(void)
+{
+       eprintf("usage: %s [username]
", argv0);
+}
+
+int
+main(int argc, char **argv)
+{
+       char *usr, *pass, *cryptpass;
+       char **newargv;
+       struct spwd *spw;
+       struct passwd *pw;
+       uid_t uid;
+       int i;
+
+       ARGBEGIN {
+       default:
+               usage();
+       } ARGEND;
+
+       if (argc < 1)
+               usr = "root";
+       else if (argc == 1)
+               usr = argv[0];
+       else
+               usage();
+
+       uid = getuid();
+
+       spw = getspnam(usr);
+       if (!spw)
+               eprintf("getspnam: %s:", usr);
+
+       switch (spw->sp_pwdp[0]) {
+       case '!':
+       case '*':
+               enprintf(EXIT_FAILURE, "Denied
");
+       case '$':
+               break;
+       default:
+               enprintf(EXIT_FAILURE, "Invalid shadow record
");
+       }
+
+       if (uid) {
+               pass = getpass("Password: ");
+               if (!pass)
+                       eprintf("getpass:");
+       }
+
+       cryptpass = crypt(pass, spw->sp_pwdp);
+       for (i = 0; pass[i]; i++)
+               pass[i] = '

Reply via email to