Annotation of researchv10no/cmd/su.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include <pwd.h>
                      3: #include <time.h>
                      4: #include <signal.h>
                      5: 
                      6: struct passwd *pwd,*getpwnam();
                      7: char   *crypt();
                      8: char   *getpass();
                      9: char   **environ;
                     10: char *name = "root";
                     11: char *shell = "/bin/sh";
                     12: char *sulog = "/dev/console";
                     13: 
                     14: main(argc,argv)
                     15: int    argc;
                     16: char   **argv;
                     17: {
                     18:        register char **p;
                     19:        char *password;
                     20:        int newgid, newuid;
                     21:        if(argv[0][0] != '/'){
                     22:                fprintf(stderr, "su must be invoked with a full path\n");
                     23:                exit(1);
                     24:        }
                     25: 
                     26:        if(argc > 1)
                     27:                name = argv[1];
                     28:        if((pwd=getpwnam(name)) == NULL) {
                     29:                printf("Unknown id: %s\n",name);
                     30:                exit(1);
                     31:        }
                     32:        if(pwd->pw_passwd[0] != '\0' && getuid() != 0) {
                     33:                password = getpass("Password:");
                     34:                if(strcmp(pwd->pw_passwd,crypt(password,pwd->pw_passwd))!=0) {
                     35:                        logit(sulog,0);
                     36:                        printf("Sorry\n");
                     37:                        exit(2);
                     38:                }
                     39:        }
                     40: 
                     41:        newgid = pwd->pw_gid;
                     42:        newuid = pwd->pw_uid;
                     43:        logit(sulog,1);
                     44:        setgid(newgid);
                     45:        setuid(newuid);
                     46:        if (pwd->pw_shell && *pwd->pw_shell)
                     47:                shell = pwd->pw_shell;
                     48:        if (newuid == 0)
                     49:                for (p=environ; *p; p++) {
                     50:                        if (strncmp("PS1=", *p, 4) == 0)
                     51:                                *p = "PS1=# ";
                     52:                        else if (strncmp("PATH=", *p, 5) == 0)
                     53:                                *p = "PATH=/bin:/usr/bin:/etc";
                     54:                }
                     55:        execl(shell, "su", "-p", 0);
                     56:        perror(shell);
                     57:        exit(3);
                     58: }
                     59: logit(logfile,how)
                     60: char *logfile;
                     61: {
                     62:        int catch();
                     63:        long now, time();
                     64:        char *cuserid(), *strrchr();
                     65:        char *ttyn, *ttyname();
                     66:        FILE *logf = fopen(logfile,"a");
                     67:        struct tm *tmp, *localtime();
                     68: 
                     69:        if(logf == NULL) return;
                     70:        if((ttyn=ttyname(3))==NULL)
                     71:                ttyn="/dev/tty??";
                     72:        now = time((long *)0);
                     73:        tmp = localtime(&now);
                     74:        signal(SIGALRM, catch);
                     75:        alarm(5);
                     76:        fprintf(logf,"\r\nSU %.2d/%.2d %.2d:%.2d %c %s %s-%s\r\n",
                     77:                tmp->tm_mon+1,tmp->tm_mday,tmp->tm_hour,tmp->tm_min,
                     78:                how?'+':'-',(strrchr(ttyn,'/')+1),cuserid(),name);
                     79:        fclose(logf);
                     80:        alarm(0);
                     81: }
                     82: char *cuserid()
                     83: {
                     84:        static char s[20];
                     85:        struct passwd *getpwuid();
                     86:        int uid = getuid();
                     87:        struct passwd *pwd = getpwuid(uid);
                     88: 
                     89:        if(pwd && pwd->pw_name && *pwd->pw_name)
                     90:                return pwd->pw_name;
                     91:        sprintf(s, "%d", uid);
                     92:        return s;
                     93: }
                     94: catch(){}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.