Annotation of researchv10no/cmd/odist/sysv/connect.c, revision 1.1.1.1

1.1       root        1: /* usage: connect hostname (answer|send|notice) (in|out) program [ args ... ] */
                      2: /* TODO: should librarify the bit for opening an rsh-authenticated connection.  */
                      3: 
                      4: #include <sys/types.h>
                      5: #include <sys/socket.h>
                      6: #include <netinet/in.h>
                      7: #include <netdb.h>
                      8: #include <pwd.h>
                      9: #include <stdio.h>
                     10: #include <string.h>
                     11: #include "../paths.h"
                     12: 
                     13: int
                     14: main(int argc, char *argv[])
                     15: {
                     16:        int fd;
                     17:        struct hostent *host;
                     18:        struct servent *serv;
                     19:        struct passwd *pw;
                     20:        int lport;
                     21:        struct sockaddr_in sin;
                     22:        char byte;
                     23: 
                     24:        if (argc < 5)
                     25:                exit(1);
                     26: 
                     27:        host = gethostbyname(argv[1]);
                     28:        if (!host) {
                     29:                fprintf(stderr, "connect: can't get host '%s'\n", argv[1]);
                     30:                exit(1);
                     31:        }
                     32: 
                     33:        serv = getservbyname("dist", "tcp");
                     34:        if (!serv) {
                     35:                fprintf(stderr, "connect: can't get 'dist' port number\n");
                     36:                exit(1);
                     37:        }
                     38: 
                     39:        pw = getpwuid(getuid());        /* we assume unique uid/name map */
                     40:        if (!pw) {
                     41:                fprintf(stderr, "connect: can't get user name\n");
                     42:                exit(1);
                     43:        }
                     44: 
                     45:        chdir(SDIR);
                     46: 
                     47:        lport = 1023;
                     48:        fd = rresvport(&lport);
                     49:        if (fd < 0) {
                     50:                perror("connect: getting socket");
                     51:                exit(1);
                     52:        }
                     53: 
                     54:        sin.sin_family = host->h_addrtype;
                     55:        memcpy(&sin.sin_addr, host->h_addr, host->h_length);
                     56:        sin.sin_port = serv->s_port;
                     57:        if (connect(fd, &sin, sizeof sin) < 0) {
                     58:                perror("connect: opening connection");
                     59:                exit(1);
                     60:        }
                     61: 
                     62:        write(fd, "", 1);       /* a NUL byte */
                     63:        write(fd, pw->pw_name, strlen(pw->pw_name) + 1);
                     64:        write(fd, pw->pw_name, strlen(pw->pw_name) + 1);
                     65:        if (read(fd, &byte, 1) != 1 || byte != '\0') {
                     66:                fprintf(stderr, "connect: rsh authentication failed\n");
                     67:                exit(1);
                     68:        }
                     69: 
                     70:        write(fd, argv[2], strlen(argv[2]) + 1);
                     71: 
                     72:        if (strchr(argv[3], 'i'))
                     73:                dup2(fd, 0);
                     74:        if (strchr(argv[3], 'o'))
                     75:                dup2(fd, 1);
                     76:        close(fd);
                     77:        execv(argv[4], &argv[4]);
                     78: }

unix.superglobalmegacorp.com

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