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