net.c revision 72109
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1989, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef lint
3827169Scharnier#if 0
3923693Speterstatic char sccsid[] = "@(#)net.c	8.4 (Berkeley) 4/28/95";
4072109Scharnier#endif
4127169Scharnierstatic const char rcsid[] =
4250477Speter  "$FreeBSD: head/usr.bin/finger/net.c 72109 2001-02-06 20:13:48Z charnier $";
431590Srgrimes#endif /* not lint */
441590Srgrimes
4536792Simp#include <sys/param.h>
461590Srgrimes#include <sys/socket.h>
4772109Scharnier#include <sys/uio.h>
4872109Scharnier#include <ctype.h>
491590Srgrimes#include <db.h>
5027169Scharnier#include <err.h>
5172109Scharnier#include <netdb.h>
521590Srgrimes#include <pwd.h>
531590Srgrimes#include <stdio.h>
541590Srgrimes#include <string.h>
5572109Scharnier#include <unistd.h>
5672109Scharnier#include <utmp.h>
571590Srgrimes#include "finger.h"
581590Srgrimes
5966199Swollmanextern int lflag;		/* XXX finger.h? */
6066199Swollmanextern int Tflag;		/* XXX finger.h? */
6146662Sobrien
6266199Swollmanstatic void cleanup(int sig);;
6366199Swollmanstatic int do_protocol(const char *name, const struct addrinfo *ai);
6466199Swollmanstatic void trying(const struct addrinfo *ai);
6566199Swollman
6646662Sobrienvoid
671590Srgrimesnetfinger(name)
681590Srgrimes	char *name;
691590Srgrimes{
7066199Swollman	int error, multi;
7166199Swollman	char *host;
7266199Swollman	struct addrinfo *ai, *ai0;
7366199Swollman	static struct addrinfo hint;
741590Srgrimes
7566199Swollman	host = strrchr(name, '@');
7666199Swollman	if (host == 0)
771590Srgrimes		return;
7827169Scharnier	*host++ = '\0';
7946662Sobrien	signal(SIGALRM, cleanup);
8066199Swollman	alarm(TIME_LIMIT);
8166199Swollman
8266199Swollman	hint.ai_flags = AI_CANONNAME;
8366199Swollman	hint.ai_family = PF_UNSPEC;
8466199Swollman	hint.ai_socktype = SOCK_STREAM;
8566199Swollman
8666199Swollman	error = getaddrinfo(host, "finger", &hint, &ai0);
8766199Swollman	if (error) {
8866199Swollman		warnx("%s: %s", host, gai_strerror(error));
891590Srgrimes		return;
901590Srgrimes	}
9166199Swollman
9266199Swollman	multi = (ai0->ai_next) != 0;
9366199Swollman
9466315Swollman	/* ai_canonname may not be filled in if the user specified an IP. */
9566315Swollman	if (ai0->ai_canonname == 0)
9666315Swollman		printf("[%s]\n", host);
9766315Swollman	else
9866315Swollman		printf("[%s]\n", ai0->ai_canonname);
9966315Swollman
10066199Swollman	for (ai = ai0; ai != 0; ai = ai->ai_next) {
10166199Swollman		if (multi)
10266199Swollman			trying(ai);
10366199Swollman
10466199Swollman		error = do_protocol(name, ai);
10566199Swollman		if (!error)
10666199Swollman			break;
1071590Srgrimes	}
10866199Swollman	alarm(0);
10966199Swollman	freeaddrinfo(ai0);
11066199Swollman}
11166199Swollman
11266199Swollmanstatic int
11366199Swollmando_protocol(const char *name, const struct addrinfo *ai)
11466199Swollman{
11567467Sru	int cnt, line_len, s;
11666199Swollman	register FILE *fp;
11766199Swollman	register int c, lastc;
11866199Swollman	struct iovec iov[3];
11966199Swollman	struct msghdr msg;
12066199Swollman
12166199Swollman	s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
12266199Swollman	if (s < 0) {
12366199Swollman		warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
12466199Swollman		     ai->ai_protocol);
12566199Swollman		return -1;
1261590Srgrimes	}
1271590Srgrimes
12866199Swollman	msg.msg_name = (void *)ai->ai_addr;
12966199Swollman	msg.msg_namelen = ai->ai_addrlen;
13012909Swollman	msg.msg_iov = iov;
13112909Swollman	msg.msg_iovlen = 0;
13212909Swollman	msg.msg_control = 0;
13312909Swollman	msg.msg_controllen = 0;
13427835Swollman	msg.msg_flags = 0;
13512909Swollman
1361590Srgrimes	/* -l flag for remote fingerd  */
13712909Swollman	if (lflag) {
13812909Swollman		iov[msg.msg_iovlen].iov_base = "/W ";
13912909Swollman		iov[msg.msg_iovlen++].iov_len = 3;
14012909Swollman	}
1411590Srgrimes	/* send the name followed by <CR><LF> */
14267467Sru	iov[msg.msg_iovlen].iov_base = (char *)name;
14312909Swollman	iov[msg.msg_iovlen++].iov_len = strlen(name);
14412909Swollman	iov[msg.msg_iovlen].iov_base = "\r\n";
14512909Swollman	iov[msg.msg_iovlen++].iov_len = 2;
1461590Srgrimes
14766199Swollman	/*
14866199Swollman	 * -T disables data-on-SYN: compatibility option to finger broken
14966199Swollman	 * hosts.  Also, the implicit-open API is broken on IPv6, so do
15066199Swollman	 * the explicit connect there, too.
15166199Swollman	 */
15266199Swollman	if ((Tflag || ai->ai_addr->sa_family == AF_INET6)
15366199Swollman	    && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
15466199Swollman		warn("connect");
15566199Swollman		close(s);
15666199Swollman		return -1;
15714631Solah	}
15814631Solah
15927835Swollman	if (sendmsg(s, &msg, 0) < 0) {
16066199Swollman		warn("sendmsg");
16112909Swollman		close(s);
16266199Swollman		return -1;
16312909Swollman	}
16412909Swollman
1651590Srgrimes	/*
1661590Srgrimes	 * Read from the remote system; once we're connected, we assume some
1671590Srgrimes	 * data.  If none arrives, we hang until the user interrupts.
1681590Srgrimes	 *
1691590Srgrimes	 * If we see a <CR> or a <CR> with the high bit set, treat it as
1701590Srgrimes	 * a newline; if followed by a newline character, only output one
1711590Srgrimes	 * newline.
1721590Srgrimes	 *
1731590Srgrimes	 * Otherwise, all high bits are stripped; if it isn't printable and
1741590Srgrimes	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
1751590Srgrimes	 * character with bit 7 set is printable.
1768874Srgrimes	 */
17723693Speter	lastc = 0;
17823693Speter	if ((fp = fdopen(s, "r")) != NULL) {
17946662Sobrien		cnt = 0;
18046662Sobrien		line_len = 0;
1811590Srgrimes		while ((c = getc(fp)) != EOF) {
18246662Sobrien			if (++cnt > OUTPUT_MAX) {
18346662Sobrien				printf("\n\n Output truncated at %d bytes...\n",
18446662Sobrien					cnt - 1);
18546662Sobrien				break;
18646662Sobrien			}
1871590Srgrimes			if (c == 0x0d) {
1881590Srgrimes				if (lastc == '\r')	/* ^M^M - skip dupes */
1891590Srgrimes					continue;
1901590Srgrimes				c = '\n';
1911590Srgrimes				lastc = '\r';
1921590Srgrimes			} else {
19314472Sache				if (!isprint(c) && !isspace(c)) {
19414472Sache					c &= 0x7f;
1951590Srgrimes					c |= 0x40;
19614472Sache				}
1971590Srgrimes				if (lastc != '\r' || c != '\n')
1981590Srgrimes					lastc = c;
1991590Srgrimes				else {
2001590Srgrimes					lastc = '\n';
2011590Srgrimes					continue;
2021590Srgrimes				}
2031590Srgrimes			}
2041590Srgrimes			putchar(c);
20546662Sobrien			if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
20646662Sobrien				putchar('\\');
20746662Sobrien				putchar('\n');
20846662Sobrien				lastc = '\r';
20946662Sobrien			}
21046662Sobrien			if (lastc == '\n' || lastc == '\r')
21146662Sobrien				line_len = 0;
2121590Srgrimes		}
21312909Swollman		if (ferror(fp)) {
21412909Swollman			/*
21512909Swollman			 * Assume that whatever it was set errno...
21612909Swollman			 */
21766199Swollman			warn("reading from network");
21812909Swollman		}
21966199Swollman		if (lastc != '\n')
22066199Swollman			putchar('\n');
22166199Swollman
22266199Swollman		fclose(fp);
22312909Swollman	}
22466199Swollman	return 0;
2251590Srgrimes}
22666199Swollman
22766199Swollmanstatic void
22866199Swollmantrying(const struct addrinfo *ai)
22966199Swollman{
23066199Swollman	char buf[NI_MAXHOST];
23166199Swollman
23266199Swollman	if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf,
23366199Swollman			(char *)0, 0, NI_NUMERICHOST) != 0)
23466199Swollman		return;		/* XXX can't happen */
23566199Swollman
23666199Swollman	printf("Trying %s...\n", buf);
23766199Swollman}
23866199Swollman
23966199Swollmanvoid
24066199Swollmancleanup(int sig)
24166199Swollman{
24266199Swollman#define	ERRSTR	"Timed out.\n"
24366199Swollman	write(STDERR_FILENO, ERRSTR, sizeof ERRSTR);
24466199Swollman	exit(1);
24566199Swollman}
24666199Swollman
247