xref: /titanic_51/usr/src/cmd/sendmail/aux/mconnect.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * mconnect.c - A program to test out SMTP connections.
30*7c478bd9Sstevel@tonic-gate  * Usage: mconnect [host]
31*7c478bd9Sstevel@tonic-gate  *  ... SMTP dialog
32*7c478bd9Sstevel@tonic-gate  *  ^C or ^D or QUIT
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <signal.h>
38*7c478bd9Sstevel@tonic-gate #include <ctype.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <strings.h>
41*7c478bd9Sstevel@tonic-gate #include <sgtty.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
45*7c478bd9Sstevel@tonic-gate #include <unistd.h>
46*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
47*7c478bd9Sstevel@tonic-gate #include <netdb.h>
48*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
49*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
50*7c478bd9Sstevel@tonic-gate #include <errno.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate union bigsockaddr
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	struct sockaddr		sa;	/* general version */
55*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in	sin;	/* INET family */
56*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	sin6;	/* INET/IPv6 */
57*7c478bd9Sstevel@tonic-gate };
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static struct sgttyb TtyBuf;
60*7c478bd9Sstevel@tonic-gate static int raw = 0;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
63*7c478bd9Sstevel@tonic-gate static void
64*7c478bd9Sstevel@tonic-gate finis(sig)
65*7c478bd9Sstevel@tonic-gate 	int sig;
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	if (raw)
68*7c478bd9Sstevel@tonic-gate 		(void) ioctl(0, TIOCSETP, &TtyBuf);
69*7c478bd9Sstevel@tonic-gate 	exit(0);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate int
73*7c478bd9Sstevel@tonic-gate main(argc, argv)
74*7c478bd9Sstevel@tonic-gate 	int argc;
75*7c478bd9Sstevel@tonic-gate 	char **argv;
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	union bigsockaddr SendmailAddress;
78*7c478bd9Sstevel@tonic-gate 	register int s;
79*7c478bd9Sstevel@tonic-gate 	char *host = NULL;
80*7c478bd9Sstevel@tonic-gate 	int pid;
81*7c478bd9Sstevel@tonic-gate 	int on = 1;
82*7c478bd9Sstevel@tonic-gate 	struct servent *sp;
83*7c478bd9Sstevel@tonic-gate 	char buf[1000];
84*7c478bd9Sstevel@tonic-gate 	register FILE *f;
85*7c478bd9Sstevel@tonic-gate 	register struct hostent *hp;
86*7c478bd9Sstevel@tonic-gate 	in_port_t port = 0;
87*7c478bd9Sstevel@tonic-gate 	int err;
88*7c478bd9Sstevel@tonic-gate 	char buf6[INET6_ADDRSTRLEN];
89*7c478bd9Sstevel@tonic-gate 	int addr_num = 0;
90*7c478bd9Sstevel@tonic-gate 	int addrlen;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	(void) ioctl(0, TIOCGETP, &TtyBuf);
93*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, finis);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	while (--argc > 0)
96*7c478bd9Sstevel@tonic-gate 	{
97*7c478bd9Sstevel@tonic-gate 		register char *p;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 		p = *++argv;
100*7c478bd9Sstevel@tonic-gate 		if (*p == '-')
101*7c478bd9Sstevel@tonic-gate 		{
102*7c478bd9Sstevel@tonic-gate 			switch (*++p)
103*7c478bd9Sstevel@tonic-gate 			{
104*7c478bd9Sstevel@tonic-gate 			    case 'h':		/* host */
105*7c478bd9Sstevel@tonic-gate 				break;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 			    case 'p':		/* port */
108*7c478bd9Sstevel@tonic-gate 				port = htons(atoi(*++argv));
109*7c478bd9Sstevel@tonic-gate 				argc--;
110*7c478bd9Sstevel@tonic-gate 				break;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 			    case 'r':		/* raw connection */
113*7c478bd9Sstevel@tonic-gate 				raw = 1;
114*7c478bd9Sstevel@tonic-gate 				break;
115*7c478bd9Sstevel@tonic-gate 			}
116*7c478bd9Sstevel@tonic-gate 		} else if (host == NULL)
117*7c478bd9Sstevel@tonic-gate 			host = p;
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 	if (host == NULL)
120*7c478bd9Sstevel@tonic-gate 		host = "localhost";
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	bzero(&SendmailAddress, sizeof (SendmailAddress));
123*7c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(host, AF_INET6, AI_DEFAULT|AI_ALL, &err);
124*7c478bd9Sstevel@tonic-gate 	if (hp == NULL)
125*7c478bd9Sstevel@tonic-gate 	{
126*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "mconnect: unknown host %s\r\n", host);
127*7c478bd9Sstevel@tonic-gate 		exit(0);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (port == 0) {
131*7c478bd9Sstevel@tonic-gate 		sp = getservbyname("smtp", "tcp");
132*7c478bd9Sstevel@tonic-gate 		if (sp != NULL)
133*7c478bd9Sstevel@tonic-gate 			port = sp->s_port;
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	for (;;) {
137*7c478bd9Sstevel@tonic-gate 		bcopy(hp->h_addr_list[addr_num],
138*7c478bd9Sstevel@tonic-gate 		    &SendmailAddress.sin6.sin6_addr, IN6ADDRSZ);
139*7c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&SendmailAddress.sin6.sin6_addr)) {
140*7c478bd9Sstevel@tonic-gate 			SendmailAddress.sa.sa_family = AF_INET;
141*7c478bd9Sstevel@tonic-gate 			SendmailAddress.sin.sin_port = port;
142*7c478bd9Sstevel@tonic-gate 			bcopy(&hp->h_addr_list[addr_num][IN6ADDRSZ - INADDRSZ],
143*7c478bd9Sstevel@tonic-gate 				&SendmailAddress.sin.sin_addr, INADDRSZ);
144*7c478bd9Sstevel@tonic-gate 			addrlen = sizeof (struct sockaddr_in);
145*7c478bd9Sstevel@tonic-gate 		} else {
146*7c478bd9Sstevel@tonic-gate 			SendmailAddress.sa.sa_family = AF_INET6;
147*7c478bd9Sstevel@tonic-gate 			SendmailAddress.sin6.sin6_port = port;
148*7c478bd9Sstevel@tonic-gate 			addrlen = sizeof (struct sockaddr_in6);
149*7c478bd9Sstevel@tonic-gate 		}
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 		s = socket(SendmailAddress.sa.sa_family, SOCK_STREAM, 0);
152*7c478bd9Sstevel@tonic-gate 		if (s < 0)
153*7c478bd9Sstevel@tonic-gate 		{
154*7c478bd9Sstevel@tonic-gate 			perror("socket");
155*7c478bd9Sstevel@tonic-gate 			exit(-1);
156*7c478bd9Sstevel@tonic-gate 		}
157*7c478bd9Sstevel@tonic-gate 		(void) setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
158*7c478bd9Sstevel@tonic-gate 		    sizeof (on));
159*7c478bd9Sstevel@tonic-gate 		if (SendmailAddress.sa.sa_family == AF_INET)
160*7c478bd9Sstevel@tonic-gate 			(void) printf("connecting to host %s (%s), port %d\r\n",
161*7c478bd9Sstevel@tonic-gate 				host, inet_ntoa(SendmailAddress.sin.sin_addr),
162*7c478bd9Sstevel@tonic-gate 				ntohs(SendmailAddress.sin.sin_port));
163*7c478bd9Sstevel@tonic-gate 		else
164*7c478bd9Sstevel@tonic-gate 			(void) printf("connecting to host %s (%s), port %d\r\n",
165*7c478bd9Sstevel@tonic-gate 				host, inet_ntop(AF_INET6,
166*7c478bd9Sstevel@tonic-gate 					SendmailAddress.sin6.sin6_addr.s6_addr,
167*7c478bd9Sstevel@tonic-gate 					buf6, sizeof (buf6)),
168*7c478bd9Sstevel@tonic-gate 				ntohs(SendmailAddress.sin6.sin6_port));
169*7c478bd9Sstevel@tonic-gate 		if (connect(s, (struct sockaddr *)&SendmailAddress,
170*7c478bd9Sstevel@tonic-gate 				addrlen) >= 0)
171*7c478bd9Sstevel@tonic-gate 			break;
172*7c478bd9Sstevel@tonic-gate 		if (hp->h_addr_list[++addr_num] != NULL) {
173*7c478bd9Sstevel@tonic-gate 			(void) printf("connect failed (%s), next address ...\n",
174*7c478bd9Sstevel@tonic-gate 				strerror(errno));
175*7c478bd9Sstevel@tonic-gate 			bcopy(hp->h_addr_list[addr_num],
176*7c478bd9Sstevel@tonic-gate 				&SendmailAddress.sin6.sin6_addr, IN6ADDRSZ);
177*7c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(
178*7c478bd9Sstevel@tonic-gate 			    &SendmailAddress.sin6.sin6_addr)) {
179*7c478bd9Sstevel@tonic-gate 				SendmailAddress.sa.sa_family = AF_INET;
180*7c478bd9Sstevel@tonic-gate 				bcopy(&hp->h_addr_list[addr_num]
181*7c478bd9Sstevel@tonic-gate 				    [IN6ADDRSZ - INADDRSZ],
182*7c478bd9Sstevel@tonic-gate 					&SendmailAddress.sin.sin_addr,
183*7c478bd9Sstevel@tonic-gate 					INADDRSZ);
184*7c478bd9Sstevel@tonic-gate 				addrlen = sizeof (struct sockaddr_in);
185*7c478bd9Sstevel@tonic-gate 			} else {
186*7c478bd9Sstevel@tonic-gate 				SendmailAddress.sa.sa_family = AF_INET6;
187*7c478bd9Sstevel@tonic-gate 				addrlen = sizeof (struct sockaddr_in6);
188*7c478bd9Sstevel@tonic-gate 			}
189*7c478bd9Sstevel@tonic-gate 			continue;
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 		perror("connect");
192*7c478bd9Sstevel@tonic-gate 		exit(-1);
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	if (raw) {
196*7c478bd9Sstevel@tonic-gate 		TtyBuf.sg_flags &= ~CRMOD;
197*7c478bd9Sstevel@tonic-gate 		(void) ioctl(0, TIOCSETP, &TtyBuf);
198*7c478bd9Sstevel@tonic-gate 		TtyBuf.sg_flags |= CRMOD;
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	/* good connection, fork both sides */
202*7c478bd9Sstevel@tonic-gate 	(void) printf("connection open\n");
203*7c478bd9Sstevel@tonic-gate 	pid = fork();
204*7c478bd9Sstevel@tonic-gate 	if (pid < 0)
205*7c478bd9Sstevel@tonic-gate 	{
206*7c478bd9Sstevel@tonic-gate 		perror("fork");
207*7c478bd9Sstevel@tonic-gate 		exit(-1);
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 	if (pid == 0)
210*7c478bd9Sstevel@tonic-gate 	{
211*7c478bd9Sstevel@tonic-gate 		/* child -- standard input to sendmail */
212*7c478bd9Sstevel@tonic-gate 		int c;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 		f = fdopen(s, "w");
215*7c478bd9Sstevel@tonic-gate 		while ((c = fgetc(stdin)) >= 0)
216*7c478bd9Sstevel@tonic-gate 		{
217*7c478bd9Sstevel@tonic-gate 			if (!raw && c == '\n')
218*7c478bd9Sstevel@tonic-gate 				(void) fputc('\r', f);
219*7c478bd9Sstevel@tonic-gate 			(void) fputc(c, f);
220*7c478bd9Sstevel@tonic-gate 			if (c == '\n')
221*7c478bd9Sstevel@tonic-gate 				(void) fflush(f);
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 		(void) shutdown(s, 1);
224*7c478bd9Sstevel@tonic-gate 		(void) sleep(10);
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 	else
227*7c478bd9Sstevel@tonic-gate 	{
228*7c478bd9Sstevel@tonic-gate 		/* parent -- sendmail to standard output */
229*7c478bd9Sstevel@tonic-gate 		f = fdopen(s, "r");
230*7c478bd9Sstevel@tonic-gate 		while (fgets(buf, sizeof (buf), f) != NULL)
231*7c478bd9Sstevel@tonic-gate 		{
232*7c478bd9Sstevel@tonic-gate 			(void) fputs(buf, stdout);
233*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 		(void) kill(pid, SIGTERM);
236*7c478bd9Sstevel@tonic-gate 	}
237*7c478bd9Sstevel@tonic-gate 	if (raw)
238*7c478bd9Sstevel@tonic-gate 		(void) ioctl(0, TIOCSETP, &TtyBuf);
239*7c478bd9Sstevel@tonic-gate 	return (0);
240*7c478bd9Sstevel@tonic-gate }
241