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