Annotation of researchv9/ipc/src/libipc/ipclogin.c, revision 1.1.1.1

1.1       root        1: #include <sgtty.h>
                      2: #include <stdio.h>
                      3: #include <ipc.h>
                      4: #include "defs.h"
                      5: #include "libc.h"
                      6: 
                      7: /* this better be larger than either login names or passwords */
                      8: #define PWDSIZE 16
                      9: 
                     10: char *_ipcgetpass();
                     11: 
                     12: /*
                     13:  * log a user into a remote system in the v9 style
                     14:  *
                     15:  * First attempt at a valid userid came from ipcopen(),
                     16:  * who sent it as part of the setup request.  Remote
                     17:  * system responds to that with either "OK" or "NO".
                     18:  * If the latter, we must send a string containing
                     19:  * "userid,password<newline>" until either we receive
                     20:  * the OK response or our user here gives up.
                     21:  */
                     22: ipclogin(fd)
                     23: {
                     24: #      define BFS 2*PWDSIZE + 3        /* <log>,<pswd>\n\0 */
                     25:        int     cc;
                     26:        struct sgttyb echo, noecho;
                     27:        char    buf[BFS];
                     28:        char    *rbuf;
                     29:        FILE    *tty;
                     30: 
                     31:        cc = read(fd, buf, 2) ;
                     32:        if (cc <= 0) {
                     33:                errstr = "ipclogin can't read remote system";
                     34:                return(-1);
                     35:        }
                     36:        if (buf[0] == 'O' && buf[1] == 'K')
                     37:                return fd ;
                     38:        tty = fopen("/dev/tty", "r+");
                     39:        if (tty == NULL) {
                     40:                errstr = "ipclogin can't open /dev/tty";
                     41:                return -1;
                     42:        }
                     43:        if (ioctl(fileno(tty), TIOCGETP, &echo) < 0) {
                     44:                errstr = "ipclogin can't log in";
                     45:                return(-1);
                     46:        }
                     47:        noecho = echo;
                     48:        noecho.sg_flags &= ~ECHO;
                     49:        write (fileno(tty), "please ", 7) ;
                     50:        while (1) {
                     51:                rbuf = _ipcgetpass (tty, "login: ");
                     52:                if (rbuf == 0) {
                     53:                        cc = -1;
                     54:                        break;
                     55:                }
                     56:                strcpy (buf, rbuf);
                     57:                strcat (buf, ",");
                     58: 
                     59:                ioctl(fileno(tty), TIOCSETP, &noecho) ;
                     60:                rbuf = _ipcgetpass (tty, "Password:");
                     61:                ioctl(fileno(tty), TIOCSETP, &echo) ;
                     62:                write(fileno(tty), "\n", 1);
                     63:                if (rbuf == 0) {
                     64:                        cc = -2;
                     65:                        break;
                     66:                }
                     67:                strcat (buf, rbuf);
                     68:                strcat (buf, "\n");
                     69: 
                     70:                write(fd, buf, strlen (buf)) ;
                     71:                cc = read(fd, buf, 2) ;
                     72:                if (cc <= 0)
                     73:                        break ;
                     74:                if (buf[0] == 'O' && buf[1] == 'K')
                     75:                        break ;
                     76:        }
                     77:        fclose(tty);
                     78:        if (cc <= 0) {
                     79:                close(fd);
                     80:                errstr = "ipclogin can't log in";
                     81:                return -1;
                     82:        }
                     83:        return fd;
                     84: }
                     85: 
                     86: /*
                     87:  * log a user into a remote system in the Berkeley style
                     88:  *
                     89:  * Send a string to the other side of the form
                     90:  *     "<NULL>local_user_name<NULL>remote_user_name<NULL>terminal_type<NULL>"
                     91:  * The response is either
                     92:  *     "<NULL>"                to indicate success or
                     93:  *     "<non_0_byte>string"    where `string' is the error message
                     94:  */
                     95: ipcrogin(fd, opt)
                     96:        int fd;
                     97:        char *opt;
                     98: {
                     99:        char buf[32];
                    100:        char *me=getlogin();
                    101:        int len;
                    102: 
                    103:        len = me!=NULL ? strlen(me) : 0;
                    104:        write(fd, "", 1);
                    105:        write(fd, me, len+1);
                    106:        write(fd, me, len+1);
                    107:        write(fd, opt, strlen(opt)+1);
                    108:        if(read(fd, buf, 1) != 1)
                    109:                return -1;
                    110:        if(buf[0] != 0){
                    111:                while(read(fd, buf, 1) == 1){
                    112:                        write(2, buf, 1);
                    113:                        if(buf[0] == '\n')
                    114:                                break;
                    115:                }
                    116:                return -1;
                    117:        }
                    118:        return 0;
                    119: }
                    120: 
                    121: char *
                    122: _ipcgetpass(tty, prompt)
                    123: FILE *tty;
                    124: char * prompt;
                    125: {
                    126:        char c;
                    127:        char *cp;
                    128:        static char buf[PWDSIZE + 1];
                    129: 
                    130:        write (fileno(tty), prompt, strlen (prompt));
                    131: 
                    132:        cp = buf;
                    133:        while ((c = getc(tty)) != '\n' && c != EOF && c != '\04') {
                    134:                if (cp < &buf[PWDSIZE])
                    135:                        *cp++ = c;
                    136:        }
                    137:        *cp = '\0';
                    138: 
                    139:        if (buf[0] == '\0' && (c == EOF || c== '\04'))
                    140:                return ((char *)0);
                    141:        if (buf[0] == '~' && buf[1] == '.')
                    142:                return ((char *)0);
                    143: 
                    144:        return (buf);
                    145: }

unix.superglobalmegacorp.com

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