Research V7 development
authorDennis Ritchie <dmr@research.uucp>
Sat, 5 May 1979 08:17:04 +0000 (03:17 -0500)
committerDennis Ritchie <dmr@research.uucp>
Sat, 5 May 1979 08:17:04 +0000 (03:17 -0500)
Work on file usr/src/cmd/su.c

Co-Authored-By: Ken Thompson <ken@research.uucp>
Synthesized-from: v7

usr/src/cmd/su.c [new file with mode: 0644]

diff --git a/usr/src/cmd/su.c b/usr/src/cmd/su.c
new file mode 100644 (file)
index 0000000..11ca6fe
--- /dev/null
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <pwd.h>
+
+struct passwd *pwd,*getpwnam();
+char   *crypt();
+char   *getpass();
+char   **environ;
+
+main(argc,argv)
+int    argc;
+char   **argv;
+{
+       register char **p;
+       char *nptr;
+       char *password;
+       int badsw = 0;
+       char *shell = "/bin/sh";
+
+       if(argc > 1)
+               nptr = argv[1];
+       else
+               nptr = "root";
+       if((pwd=getpwnam(nptr)) == NULL) {
+               printf("Unknown id: %s\n",nptr);
+               exit(1);
+       }
+       if(pwd->pw_passwd[0] == '\0' || getuid() == 0)
+               goto ok;
+       password = getpass("Password:");
+       if(badsw || (strcmp(pwd->pw_passwd, crypt(password, pwd->pw_passwd)) != 0)) {
+               printf("Sorry\n");
+               exit(2);
+       }
+
+ok:
+       endpwent();
+       setgid(pwd->pw_gid);
+       setuid(pwd->pw_uid);
+       if (pwd->pw_shell && *pwd->pw_shell)
+               shell = pwd->pw_shell;
+       for (p=environ; *p; p++) {
+               if (strncmp("PS1=", *p, 4) == 0) {
+                       *p = "PS1=# ";
+                       break;
+               }
+       }
+       execl(shell, "su", 0);
+       printf("No shell\n");
+       exit(3);
+}