1: /*
   2: **  Sendmail
   3: **  Copyright (c) 1983  Eric P. Allman
   4: **  Berkeley, California
   5: **
   6: **  Copyright (c) 1983 Regents of the University of California.
   7: **  All rights reserved.  The Berkeley software License Agreement
   8: **  specifies the terms and conditions for redistribution.
   9: */
  10: 
  11: #ifndef lint
  12: char copyright[] =
  13: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  14:  All rights reserved.\n";
  15: #endif not lint
  16: 
  17: #ifndef lint
  18: static char SccsId[] = "@(#)mconnect.c	5.2 (Berkeley) 7/13/85";
  19: #endif not lint
  20: 
  21: # include <stdio.h>
  22: # include <signal.h>
  23: # include <ctype.h>
  24: # include <sgtty.h>
  25: # include <sys/types.h>
  26: # include <sys/socket.h>
  27: # include <netinet/in.h>
  28: # include <netdb.h>
  29: 
  30: struct sockaddr_in  SendmailAddress;
  31: struct sgttyb       TtyBuf;
  32: 
  33: main(argc, argv)
  34:     int argc;
  35:     char **argv;
  36: {
  37:     register int s;
  38:     char *host;
  39:     register int i;
  40:     int pid;
  41:     struct servent *sp;
  42:     int raw = 0;
  43:     char buf[1000];
  44:     register char *p;
  45:     extern char *index();
  46:     register FILE *f;
  47:     extern u_long inet_addr();
  48:     extern finis();
  49: 
  50:     (void) gtty(0, &TtyBuf);
  51:     (void) signal(SIGINT, finis);
  52:     s = socket(AF_INET, SOCK_STREAM, 0, 0);
  53:     if (s < 0)
  54:     {
  55:         perror("socket");
  56:         exit(-1);
  57:     }
  58: 
  59:     sp = getservbyname("smtp", "tcp");
  60:     if (sp != NULL)
  61:         SendmailAddress.sin_port = sp->s_port;
  62: 
  63:     while (--argc > 0)
  64:     {
  65:         register char *p = *++argv;
  66: 
  67:         if (*p == '-')
  68:         {
  69:             switch (*++p)
  70:             {
  71:               case 'h':     /* host */
  72:                 break;
  73: 
  74:               case 'p':     /* port */
  75:                 SendmailAddress.sin_port = htons(atoi(*++argv));
  76:                 argc--;
  77:                 break;
  78: 
  79:               case 'r':     /* raw connection */
  80:                 raw = 1;
  81:                 TtyBuf.sg_flags &= ~CRMOD;
  82:                 stty(0, &TtyBuf);
  83:                 TtyBuf.sg_flags |= CRMOD;
  84:                 break;
  85:             }
  86:         }
  87:         else if (host == NULL)
  88:             host = p;
  89:     }
  90:     if (host == NULL)
  91:         host = "localhost";
  92: 
  93:     if (isdigit(*host))
  94:         SendmailAddress.sin_addr.s_addr = inet_addr(host);
  95:     else
  96:     {
  97:         register struct hostent *hp = gethostbyname(host);
  98: 
  99:         if (hp == NULL)
 100:         {
 101:             fprintf(stderr, "mconnect: unknown host %s\r\n", host);
 102:             finis();
 103:         }
 104:         bcopy(hp->h_addr, &SendmailAddress.sin_addr, hp->h_length);
 105:     }
 106:     SendmailAddress.sin_family = AF_INET;
 107:     printf("connecting to host %s (0x%x), port 0x%x\r\n", host,
 108:            SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
 109:     if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
 110:     {
 111:         perror("connect");
 112:         exit(-1);
 113:     }
 114: 
 115:     /* good connection, fork both sides */
 116:     printf("connection open\n");
 117:     pid = fork();
 118:     if (pid < 0)
 119:     {
 120:         perror("fork");
 121:         exit(-1);
 122:     }
 123:     if (pid == 0)
 124:     {
 125:         /* child -- standard input to sendmail */
 126:         int c;
 127: 
 128:         f = fdopen(s, "w");
 129:         while ((c = fgetc(stdin)) >= 0)
 130:         {
 131:             if (!raw && c == '\n')
 132:                 fputc('\r', f);
 133:             fputc(c, f);
 134:             if (c == '\n')
 135:                 fflush(f);
 136:         }
 137:     }
 138:     else
 139:     {
 140:         /* parent -- sendmail to standard output */
 141:         f = fdopen(s, "r");
 142:         while (fgets(buf, sizeof buf, f) != NULL)
 143:         {
 144:             fputs(buf, stdout);
 145:             fflush(stdout);
 146:         }
 147:     }
 148:     finis();
 149: }
 150: 
 151: finis()
 152: {
 153:     stty(0, &TtyBuf);
 154:     exit(0);
 155: }

Defined functions

finis defined in line 151; used 5 times
main defined in line 33; never used

Defined variables

SccsId defined in line 18; never used
SendmailAddress defined in line 30; used 9 times
TtyBuf defined in line 31; used 5 times
copyright defined in line 12; never used
Last modified: 1987-02-18
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2606
Valid CSS Valid XHTML 1.0 Strict