BSD 4 release
[unix-history] / usr / src / cmd / su.c
index 11ca6fe..48de85b 100644 (file)
@@ -1,25 +1,28 @@
+static char *sccsid = "@(#)su.c        4.1 (Berkeley) 10/1/80";
 #include <stdio.h>
 #include <pwd.h>
 
 struct passwd *pwd,*getpwnam();
 char   *crypt();
 char   *getpass();
 #include <stdio.h>
 #include <pwd.h>
 
 struct passwd *pwd,*getpwnam();
 char   *crypt();
 char   *getpass();
-char   **environ;
 
 main(argc,argv)
 int    argc;
 char   **argv;
 {
 
 main(argc,argv)
 int    argc;
 char   **argv;
 {
-       register char **p;
        char *nptr;
        char *password;
        int badsw = 0;
        char *shell = "/bin/sh";
        char *nptr;
        char *password;
        int badsw = 0;
        char *shell = "/bin/sh";
+       int niced = 0;
 
        if(argc > 1)
                nptr = argv[1];
 
        if(argc > 1)
                nptr = argv[1];
-       else
+       else {
                nptr = "root";
                nptr = "root";
+               nice(-4);
+               niced = -4;
+       }
        if((pwd=getpwnam(nptr)) == NULL) {
                printf("Unknown id: %s\n",nptr);
                exit(1);
        if((pwd=getpwnam(nptr)) == NULL) {
                printf("Unknown id: %s\n",nptr);
                exit(1);
@@ -28,23 +31,74 @@ char        **argv;
                goto ok;
        password = getpass("Password:");
        if(badsw || (strcmp(pwd->pw_passwd, crypt(password, pwd->pw_passwd)) != 0)) {
                goto ok;
        password = getpass("Password:");
        if(badsw || (strcmp(pwd->pw_passwd, crypt(password, pwd->pw_passwd)) != 0)) {
+bad:
                printf("Sorry\n");
                printf("Sorry\n");
+               if(pwd->pw_uid == 0) {
+                       FILE *console = fopen("/dev/console", "w");
+                       if (console != NULL) {
+                               fprintf(console, "BADSU: %s %s\r\n", getlogin(), ttyname(2));
+                               fclose(console);
+                       }
+               }
                exit(2);
        }
                exit(2);
        }
-
 ok:
        endpwent();
 ok:
        endpwent();
+       if(pwd->pw_uid == 0) {
+               FILE *console = fopen("/dev/console", "w");
+               if (console != NULL) {
+                       fprintf(console, "SU: %s %s\r\n", getlogin(), ttyname(2));
+                       fclose(console);
+               }
+       }
        setgid(pwd->pw_gid);
        setuid(pwd->pw_uid);
        if (pwd->pw_shell && *pwd->pw_shell)
                shell = pwd->pw_shell;
        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;
-               }
-       }
+       homeis(pwd->pw_dir);
+       shellis(shell);
+       nice(-niced);
        execl(shell, "su", 0);
        printf("No shell\n");
        exit(3);
 }
        execl(shell, "su", 0);
        printf("No shell\n");
        exit(3);
 }
+
+char   **environ;
+
+homeis(hp)
+       char *hp;
+{
+       register char *cp, *dp;
+       register char **ep = environ;
+       static char homebuf[128];
+
+       while (dp = *ep++) {
+               for (cp = "HOME"; *cp == *dp && *cp; cp++, dp++)
+                       continue;
+               if (*cp == 0 && (*dp == '=' || *dp == 0)) {
+                       strcpy(homebuf, "HOME=");
+                       strcat(homebuf, hp);
+                       *--ep = homebuf;
+                       return;
+               }
+       }
+}
+
+shellis(sp)
+       char *sp;
+{
+       register char *cp, *dp;
+       register char **ep = environ;
+       static char shellbuf[128];
+
+       while (dp = *ep++) {
+               for (cp = "SHELL"; *cp == *dp && *cp; cp++, dp++)
+                       continue;
+               if (*cp == 0 && (*dp == '=' || *dp == 0)) {
+                       strcpy(shellbuf, "SHELL=");
+                       strcat(shellbuf, sp);
+                       *--ep = shellbuf;
+                       return;
+               }
+       }
+}