1: /*
   2:  * Copyright (c) 1988 Regents of the University of California.
   3:  * All rights reserved.
   4:  *
   5:  * Redistribution and use in source and binary forms are permitted
   6:  * provided that this notice is preserved and that due credit is given
   7:  * to the University of California at Berkeley. The name of the University
   8:  * may not be used to endorse or promote products derived from this
   9:  * software without specific prior written permission. This software
  10:  * is provided ``as is'' without express or implied warranty.
  11:  *
  12:  *  Sendmail
  13:  *  Copyright (c) 1983  Eric P. Allman
  14:  *  Berkeley, California
  15:  */
  16: 
  17: #ifndef lint
  18: char copyright[] =
  19: "@(#) Copyright (c) 1988 Regents of the University of California.\n\
  20:  All rights reserved.\n";
  21: #endif /* not lint */
  22: 
  23: #ifndef lint
  24: static char sccsid[] = "@(#)mconnect.c	5.3 (Berkeley) 4/21/88";
  25: #endif /* not lint */
  26: 
  27: #include <stdio.h>
  28: #include <signal.h>
  29: #include <ctype.h>
  30: #include <sgtty.h>
  31: #include <sys/types.h>
  32: #include <sys/socket.h>
  33: #include <netinet/in.h>
  34: #include <netdb.h>
  35: 
  36: static struct sgttyb TtyBuf;
  37: 
  38: main(argc, argv)
  39:     int argc;
  40:     char **argv;
  41: {
  42:     extern char *optarg;
  43:     extern int optind;
  44:     register FILE *f;
  45:     register int s;
  46:     struct servent *sp;
  47:     struct sockaddr_in SendmailAddress;
  48:     int ch, raw;
  49:     char *host, buf[1000], *index();
  50:     u_long inet_addr();
  51:     int finis();
  52: 
  53:     raw = 0;
  54:     (void)gtty(0, &TtyBuf);
  55:     (void)signal(SIGINT, finis);
  56: 
  57:     if ((s = socket(AF_INET, SOCK_STREAM, 0, 0)) < 0) {
  58:         perror("socket");
  59:         exit(-1);
  60:     }
  61: 
  62:     sp = getservbyname("smtp", "tcp");
  63:     if (sp != NULL)
  64:         SendmailAddress.sin_port = sp->s_port;
  65: 
  66:     while ((ch = getopt(argc, argv, "hp:r")) != EOF)
  67:         switch((char)ch) {
  68:         case 'h':   /* host */
  69:             break;
  70:         case 'p':   /* port */
  71:             SendmailAddress.sin_port = htons(atoi(optarg));
  72:             break;
  73:         case 'r':   /* raw connection */
  74:             raw = 1;
  75:             TtyBuf.sg_flags &= ~CRMOD;
  76:             stty(0, &TtyBuf);
  77:             TtyBuf.sg_flags |= CRMOD;
  78:             break;
  79:         case '?':
  80:         default:
  81:             fputs("usage: mconnect [-hr] [-p port] [host]\n", stderr);
  82:             exit(-1);
  83:         }
  84:     argc -= optind;
  85:     argv += optind;
  86: 
  87:     host = argc ? *argv : "localhost";
  88: 
  89:     if (isdigit(*host))
  90:         SendmailAddress.sin_addr.s_addr = inet_addr(host);
  91:     else {
  92:         register struct hostent *hp = gethostbyname(host);
  93: 
  94:         if (hp == NULL) {
  95:             fprintf(stderr, "mconnect: unknown host %s\r\n", host);
  96:             finis();
  97:         }
  98:         bcopy(hp->h_addr, &SendmailAddress.sin_addr, hp->h_length);
  99:     }
 100:     SendmailAddress.sin_family = AF_INET;
 101:     printf("connecting to host %s (0x%lx), port 0x%x\r\n", host,
 102:            SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
 103:     if (connect(s, &SendmailAddress, sizeof(SendmailAddress), 0) < 0) {
 104:         perror("connect");
 105:         exit(-1);
 106:     }
 107: 
 108:     /* good connection, fork both sides */
 109:     puts("connection open");
 110:     switch(fork()) {
 111:     case -1:
 112:         perror("fork");
 113:         exit(-1);
 114:     case 0: {       /* child -- standard input to sendmail */
 115:         int c;
 116: 
 117:         f = fdopen(s, "w");
 118:         while ((c = fgetc(stdin)) >= 0) {
 119:             if (!raw && c == '\n')
 120:                 fputc('\r', f);
 121:             fputc(c, f);
 122:             if (c == '\n')
 123:                 (void)fflush(f);
 124:         }
 125:     }
 126:     default:        /* parent -- sendmail to standard output */
 127:         f = fdopen(s, "r");
 128:         while (fgets(buf, sizeof(buf), f) != NULL) {
 129:             fputs(buf, stdout);
 130:             (void)fflush(stdout);
 131:         }
 132:     }
 133:     finis();
 134: }
 135: 
 136: finis()
 137: {
 138:     stty(0, &TtyBuf);
 139:     exit(0);
 140: }

Defined functions

finis defined in line 136; used 4 times
main defined in line 38; never used

Defined variables

TtyBuf defined in line 36; used 5 times
copyright defined in line 18; never used
sccsid defined in line 24; never used
Last modified: 1988-04-22
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3537
Valid CSS Valid XHTML 1.0 Strict