xref: /freebsd/usr.bin/finger/net.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if 0
36 #endif
37 
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/uio.h>
42 #include <wctype.h>
43 #include <db.h>
44 #include <err.h>
45 #include <netdb.h>
46 #include <pwd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <utmpx.h>
52 #include <wchar.h>
53 #include "finger.h"
54 
55 static void cleanup(int sig);
56 static int do_protocol(const char *name, const struct addrinfo *ai);
57 static void trying(const struct addrinfo *ai);
58 
59 void
60 netfinger(char *name)
61 {
62 	int error, multi;
63 	char *host;
64 	struct addrinfo *ai, *ai0;
65 	static struct addrinfo hint;
66 
67 	host = strrchr(name, '@');
68 	if (host == NULL)
69 		return;
70 	*host++ = '\0';
71 	signal(SIGALRM, cleanup);
72 	alarm(TIME_LIMIT);
73 
74 	hint.ai_flags = AI_CANONNAME;
75 	hint.ai_family = family;
76 	hint.ai_socktype = SOCK_STREAM;
77 
78 	error = getaddrinfo(host, "finger", &hint, &ai0);
79 	if (error) {
80 		warnx("%s: %s", host, gai_strerror(error));
81 		return;
82 	}
83 
84 	multi = (ai0->ai_next) != 0;
85 
86 	/* ai_canonname may not be filled in if the user specified an IP. */
87 	if (ai0->ai_canonname == 0)
88 		printf("[%s]\n", host);
89 	else
90 		printf("[%s]\n", ai0->ai_canonname);
91 
92 	for (ai = ai0; ai != NULL; ai = ai->ai_next) {
93 		if (multi)
94 			trying(ai);
95 
96 		error = do_protocol(name, ai);
97 		if (!error)
98 			break;
99 	}
100 	alarm(0);
101 	freeaddrinfo(ai0);
102 }
103 
104 static int
105 do_protocol(const char *name, const struct addrinfo *ai)
106 {
107 	int cnt, line_len, s;
108 	FILE *fp;
109 	wint_t c, lastc;
110 	struct iovec iov[3];
111 	struct msghdr msg;
112 	static char slash_w[] = "/W ";
113 	static char neteol[] = "\r\n";
114 
115 	s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
116 	if (s < 0) {
117 		warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
118 		     ai->ai_protocol);
119 		return -1;
120 	}
121 
122 	msg.msg_name = (void *)ai->ai_addr;
123 	msg.msg_namelen = ai->ai_addrlen;
124 	msg.msg_iov = iov;
125 	msg.msg_iovlen = 0;
126 	msg.msg_control = 0;
127 	msg.msg_controllen = 0;
128 	msg.msg_flags = 0;
129 
130 	/* -l flag for remote fingerd  */
131 	if (lflag) {
132 		iov[msg.msg_iovlen].iov_base = slash_w;
133 		iov[msg.msg_iovlen++].iov_len = 3;
134 	}
135 	/* send the name followed by <CR><LF> */
136 	iov[msg.msg_iovlen].iov_base = strdup(name);
137 	iov[msg.msg_iovlen++].iov_len = strlen(name);
138 	iov[msg.msg_iovlen].iov_base = neteol;
139 	iov[msg.msg_iovlen++].iov_len = 2;
140 
141 	if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
142 		warn("connect");
143 		close(s);
144 		return -1;
145 	}
146 
147 	if (sendmsg(s, &msg, 0) < 0) {
148 		warn("sendmsg");
149 		close(s);
150 		return -1;
151 	}
152 
153 	/*
154 	 * Read from the remote system; once we're connected, we assume some
155 	 * data.  If none arrives, we hang until the user interrupts.
156 	 *
157 	 * If we see a <CR> or a <CR> with the high bit set, treat it as
158 	 * a newline; if followed by a newline character, only output one
159 	 * newline.
160 	 *
161 	 * Otherwise, all high bits are stripped; if it isn't printable and
162 	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
163 	 * character with bit 7 set is printable.
164 	 */
165 	lastc = 0;
166 	if ((fp = fdopen(s, "r")) != NULL) {
167 		cnt = 0;
168 		line_len = 0;
169 		while ((c = getwc(fp)) != EOF) {
170 			if (++cnt > OUTPUT_MAX) {
171 				printf("\n\n Output truncated at %d bytes...\n",
172 					cnt - 1);
173 				break;
174 			}
175 			if (c == 0x0d) {
176 				if (lastc == '\r')	/* ^M^M - skip dupes */
177 					continue;
178 				c = '\n';
179 				lastc = '\r';
180 			} else {
181 				if (!iswprint(c) && !iswspace(c)) {
182 					c &= 0x7f;
183 					c |= 0x40;
184 				}
185 				if (lastc != '\r' || c != '\n')
186 					lastc = c;
187 				else {
188 					lastc = '\n';
189 					continue;
190 				}
191 			}
192 			putwchar(c);
193 			if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
194 				putchar('\\');
195 				putchar('\n');
196 				lastc = '\r';
197 			}
198 			if (lastc == '\n' || lastc == '\r')
199 				line_len = 0;
200 		}
201 		if (ferror(fp)) {
202 			/*
203 			 * Assume that whatever it was set errno...
204 			 */
205 			warn("reading from network");
206 		}
207 		if (lastc != L'\n')
208 			putchar('\n');
209 
210 		fclose(fp);
211 	}
212 	return 0;
213 }
214 
215 static void
216 trying(const struct addrinfo *ai)
217 {
218 	char buf[NI_MAXHOST];
219 
220 	if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf,
221 			(char *)0, 0, NI_NUMERICHOST) != 0)
222 		return;		/* XXX can't happen */
223 
224 	printf("Trying %s...\n", buf);
225 }
226 
227 static void
228 cleanup(int sig __unused)
229 {
230 #define	ERRSTR	"Timed out.\n"
231 	write(STDERR_FILENO, ERRSTR, sizeof ERRSTR);
232 	exit(1);
233 }
234 
235