Annotation of 3BSD/cmd/login.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * login [ name ]
        !             3:  */
        !             4: 
        !             5: #include <sys/types.h>
        !             6: #include <sgtty.h>
        !             7: #include <utmp.h>
        !             8: #include <signal.h>
        !             9: #include <pwd.h>
        !            10: #include <stdio.h>
        !            11: #include <sys/stat.h>
        !            12: #include <lastlog.h>
        !            13: #define SCPYN(a, b)    strncpy(a, b, sizeof(a))
        !            14: 
        !            15: char   maildir[30] =   "/usr/spool/mail/";
        !            16: char   lastlog[] =     "/usr/adm/lastlog";
        !            17: struct passwd nouser = {"", "nope"};
        !            18: struct sgttyb ttyb;
        !            19: struct utmp utmp;
        !            20: char   minusnam[16] = "-";
        !            21: char   homedir[64] = "HOME=";
        !            22: char   shell[64] = "SHELL=";
        !            23: char   term[64] = "TERM=";
        !            24: char   *envinit[] = {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, 0};
        !            25: struct passwd *pwd;
        !            26: 
        !            27: struct passwd *getpwnam();
        !            28: char   *strcat();
        !            29: int    setpwent();
        !            30: char   *ttyname();
        !            31: char   *crypt();
        !            32: char   *getpass();
        !            33: char   *rindex();
        !            34: char   *stypeof();
        !            35: extern char **environ;
        !            36: 
        !            37: main(argc, argv)
        !            38: char **argv;
        !            39: {
        !            40:        register char *namep;
        !            41:        int t, f, c;
        !            42:        char *ttyn;
        !            43: 
        !            44:        alarm(60);
        !            45:        signal(SIGQUIT, SIG_IGN);
        !            46:        signal(SIGINT, SIG_IGN);
        !            47:        nice(-100);
        !            48:        nice(20);
        !            49:        nice(0);
        !            50:        gtty(0, &ttyb);
        !            51:        ttyb.sg_erase = '#';
        !            52:        ttyb.sg_kill = '@';
        !            53:        stty(0, &ttyb);
        !            54:        for (t=3; t<20; t++)
        !            55:                close(t);
        !            56:        ttyn = ttyname(0);
        !            57:        if (ttyn==0)
        !            58:                ttyn = "/dev/tty??";
        !            59: 
        !            60:     loop:
        !            61:        SCPYN(utmp.ut_name, "");
        !            62:        if (argc>1) {
        !            63:                SCPYN(utmp.ut_name, argv[1]);
        !            64:                argc = 0;
        !            65:        }
        !            66:        while (utmp.ut_name[0] == '\0') {
        !            67:                namep = utmp.ut_name;
        !            68:                printf("login: ");
        !            69:                while ((c = getchar()) != '\n') {
        !            70:                        if(c == ' ')
        !            71:                                c = '_';
        !            72:                        if (c == EOF)
        !            73:                                exit(0);
        !            74:                        if (namep < utmp.ut_name+8)
        !            75:                                *namep++ = c;
        !            76:                }
        !            77:        }
        !            78:        setpwent();
        !            79:        if ((pwd = getpwnam(utmp.ut_name)) == NULL)
        !            80:                pwd = &nouser;
        !            81:        endpwent();
        !            82:        if (*pwd->pw_passwd != '\0') {
        !            83:                namep = crypt(getpass("Password:"),pwd->pw_passwd);
        !            84:                if (strcmp(namep, pwd->pw_passwd)) {
        !            85: bad:
        !            86:                        printf("Login incorrect\n");
        !            87:                        if (ttyn[8] == 'd') {
        !            88:                                FILE *console = fopen("/dev/console", "w");
        !            89:                                if (console != NULL) {
        !            90:                                        fprintf(console, "\r\nBADDIALUP %s %s\r\n", ttyn+5, utmp.ut_name);
        !            91:                                        fclose(console);
        !            92:                                }
        !            93:                        }
        !            94:                        goto loop;
        !            95:                }
        !            96:        }
        !            97: /*
        !            98:        if (pwd->pw_uid == 0 && ttyn[5] != 'c')
        !            99:                goto bad;
        !           100: */
        !           101:        if (ttyn[8] == 'd') {
        !           102:                FILE *console = fopen("/dev/console", "w");
        !           103:                if (console != NULL) {
        !           104:                        fprintf(console, "\r\nDIALUP %s %s\r\n", ttyn+5, pwd->pw_name);
        !           105:                        fclose(console);
        !           106:                }
        !           107:        }
        !           108:        if((f = open(lastlog, 2)) >= 0) {
        !           109:                struct lastlog ll;
        !           110: 
        !           111:                lseek(f, pwd->pw_uid * sizeof (struct lastlog), 0);
        !           112:                if (read(f, (char *) &ll, sizeof ll) == sizeof ll && ll.ll_time != 0) {
        !           113:                        register char *ep = (char *) ctime(&ll.ll_time);
        !           114:                        printf("Last login: ");
        !           115:                        ep[24 - 5] = 0;
        !           116:                        printf("%s on %.8s\n", ep, ll.ll_line);
        !           117:                }
        !           118:                lseek(f, pwd->pw_uid * sizeof (struct lastlog), 0);
        !           119:                time(&ll.ll_time);
        !           120:                strcpyn(ll.ll_line, ttyn+5, 8);
        !           121:                write(f, (char *) &ll, sizeof ll);
        !           122:        }
        !           123:        if(chdir(pwd->pw_dir) < 0) {
        !           124:                printf("No directory\n");
        !           125:                goto loop;
        !           126:        }
        !           127:        time(&utmp.ut_time);
        !           128:        t = ttyslot();
        !           129:        if (t>0 && (f = open("/etc/utmp", 1)) >= 0) {
        !           130:                lseek(f, (long)(t*sizeof(utmp)), 0);
        !           131:                SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
        !           132:                write(f, (char *)&utmp, sizeof(utmp));
        !           133:                close(f);
        !           134:        }
        !           135:        if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
        !           136:                lseek(f, 0L, 2);
        !           137:                write(f, (char *)&utmp, sizeof(utmp));
        !           138:                close(f);
        !           139:        }
        !           140:        chown(ttyn, pwd->pw_uid, pwd->pw_gid);
        !           141:        setgid(pwd->pw_gid);
        !           142:        setuid(pwd->pw_uid);
        !           143:        if (*pwd->pw_shell == '\0')
        !           144:                pwd->pw_shell = "/bin/sh";
        !           145:        environ = envinit;
        !           146:        strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
        !           147:        strncat(shell, pwd->pw_shell, sizeof(shell)-7);
        !           148:        strncat(term, stypeof(ttyn), sizeof(term)-6);
        !           149:        if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
        !           150:                namep = pwd->pw_shell;
        !           151:        else
        !           152:                namep++;
        !           153:        strcat(minusnam, namep);
        !           154:        alarm(0);
        !           155:        umask(022);
        !           156:        showmotd();
        !           157:        strcat(maildir, pwd->pw_name);
        !           158:        if(access(maildir,4)==0) {
        !           159:                struct stat statb;
        !           160:                stat(maildir, &statb);
        !           161:                if (statb.st_size)
        !           162:                        printf("You have mail.\n");
        !           163:        }
        !           164:        signal(SIGQUIT, SIG_DFL);
        !           165:        signal(SIGINT, SIG_DFL);
        !           166:        execlp(pwd->pw_shell, minusnam, 0);
        !           167:        printf("No shell\n");
        !           168:        exit(0);
        !           169: }
        !           170: 
        !           171: int    stopmotd;
        !           172: catch()
        !           173: {
        !           174:        signal(SIGINT, SIG_IGN);
        !           175:        stopmotd++;
        !           176: }
        !           177: 
        !           178: showmotd()
        !           179: {
        !           180:        FILE *mf;
        !           181:        register c;
        !           182: 
        !           183:        signal(SIGINT, catch);
        !           184:        if((mf = fopen("/etc/motd","r")) != NULL) {
        !           185:                while((c = getc(mf)) != EOF && stopmotd == 0)
        !           186:                        putchar(c);
        !           187:                fclose(mf);
        !           188:        }
        !           189:        signal(SIGINT, SIG_IGN);
        !           190: }
        !           191: 
        !           192: #define UNKNOWN "su"
        !           193: 
        !           194: char *
        !           195: stypeof(ttyid)
        !           196: char   *ttyid;
        !           197: {
        !           198:        static char     typebuf[16];
        !           199:        char            buf[50];
        !           200:        register FILE   *f;
        !           201:        register char   *p, *t, *q;
        !           202: 
        !           203:        if (ttyid == NULL)
        !           204:                return (UNKNOWN);
        !           205:        f = fopen("/etc/ttytype", "r");
        !           206:        if (f == NULL)
        !           207:                return (UNKNOWN);
        !           208:        /* split off end of name */
        !           209:        for (p = q = ttyid; *p != 0; p++)
        !           210:                if (*p == '/')
        !           211:                        q = p + 1;
        !           212: 
        !           213:        /* scan the file */
        !           214:        while (fgets(buf, sizeof buf, f) != NULL)
        !           215:        {
        !           216:                for (t=buf; *t!=' '; t++)
        !           217:                        ;
        !           218:                *t++ = 0;
        !           219:                for (p=t; *p>' '; p++)
        !           220:                        ;
        !           221:                *p = 0;
        !           222:                if (strcmp(q,t)==0) {
        !           223:                        strcpy(typebuf, buf);
        !           224:                        fclose(f);
        !           225:                        return (typebuf);
        !           226:                }
        !           227:        }
        !           228:        fclose (f);
        !           229:        return (UNKNOWN);
        !           230: }

unix.superglobalmegacorp.com

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