net.c revision 66315
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";
4027169Scharnier#else
4127169Scharnierstatic const char rcsid[] =
4250477Speter  "$FreeBSD: head/usr.bin/finger/net.c 66315 2000-09-24 01:54:49Z wollman $";
4327169Scharnier#endif
441590Srgrimes#endif /* not lint */
451590Srgrimes
461590Srgrimes#include <sys/types.h>
4736792Simp#include <sys/param.h>
481590Srgrimes#include <sys/socket.h>
491590Srgrimes#include <netinet/in.h>
501590Srgrimes#include <arpa/inet.h>
511590Srgrimes#include <netdb.h>
521590Srgrimes#include <db.h>
5327169Scharnier#include <err.h>
541590Srgrimes#include <unistd.h>
551590Srgrimes#include <pwd.h>
561590Srgrimes#include <utmp.h>
571590Srgrimes#include <stdio.h>
581590Srgrimes#include <ctype.h>
591590Srgrimes#include <string.h>
6012909Swollman#include <sys/uio.h>
6146662Sobrien#include <signal.h>
6246662Sobrien#include <limits.h>
631590Srgrimes#include "finger.h"
641590Srgrimes
6566199Swollmanextern int lflag;		/* XXX finger.h? */
6666199Swollmanextern int Tflag;		/* XXX finger.h? */
6746662Sobrien
6866199Swollmanstatic void cleanup(int sig);;
6966199Swollmanstatic int do_protocol(const char *name, const struct addrinfo *ai);
7066199Swollmanstatic void trying(const struct addrinfo *ai);
7166199Swollman
7246662Sobrienvoid
731590Srgrimesnetfinger(name)
741590Srgrimes	char *name;
751590Srgrimes{
7666199Swollman	int error, multi;
7766199Swollman	char *host;
7866199Swollman	struct addrinfo *ai, *ai0;
7966199Swollman	static struct addrinfo hint;
801590Srgrimes
8166199Swollman	host = strrchr(name, '@');
8266199Swollman	if (host == 0)
831590Srgrimes		return;
8427169Scharnier	*host++ = '\0';
8546662Sobrien	signal(SIGALRM, cleanup);
8666199Swollman	alarm(TIME_LIMIT);
8766199Swollman
8866199Swollman	hint.ai_flags = AI_CANONNAME;
8966199Swollman	hint.ai_family = PF_UNSPEC;
9066199Swollman	hint.ai_socktype = SOCK_STREAM;
9166199Swollman
9266199Swollman	error = getaddrinfo(host, "finger", &hint, &ai0);
9366199Swollman	if (error) {
9466199Swollman		warnx("%s: %s", host, gai_strerror(error));
951590Srgrimes		return;
961590Srgrimes	}
9766199Swollman
9866199Swollman	multi = (ai0->ai_next) != 0;
9966199Swollman
10066315Swollman	/* ai_canonname may not be filled in if the user specified an IP. */
10166315Swollman	if (ai0->ai_canonname == 0)
10266315Swollman		printf("[%s]\n", host);
10366315Swollman	else
10466315Swollman		printf("[%s]\n", ai0->ai_canonname);
10566315Swollman
10666199Swollman	for (ai = ai0; ai != 0; ai = ai->ai_next) {
10766199Swollman		if (multi)
10866199Swollman			trying(ai);
10966199Swollman
11066199Swollman		error = do_protocol(name, ai);
11166199Swollman		if (!error)
11266199Swollman			break;
1131590Srgrimes	}
11466199Swollman	alarm(0);
11566199Swollman	freeaddrinfo(ai0);
11666199Swollman}
11766199Swollman
11866199Swollmanstatic int
11966199Swollmando_protocol(const char *name, const struct addrinfo *ai)
12066199Swollman{
12166199Swollman	int cnt, error, line_len, s;
12266199Swollman	register FILE *fp;
12366199Swollman	register int c, lastc;
12466199Swollman	struct iovec iov[3];
12566199Swollman	struct msghdr msg;
12666199Swollman
12766199Swollman	s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
12866199Swollman	if (s < 0) {
12966199Swollman		warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
13066199Swollman		     ai->ai_protocol);
13166199Swollman		return -1;
1321590Srgrimes	}
1331590Srgrimes
13466199Swollman	msg.msg_name = (void *)ai->ai_addr;
13566199Swollman	msg.msg_namelen = ai->ai_addrlen;
13612909Swollman	msg.msg_iov = iov;
13712909Swollman	msg.msg_iovlen = 0;
13812909Swollman	msg.msg_control = 0;
13912909Swollman	msg.msg_controllen = 0;
14027835Swollman	msg.msg_flags = 0;
14112909Swollman
1421590Srgrimes	/* -l flag for remote fingerd  */
14312909Swollman	if (lflag) {
14412909Swollman		iov[msg.msg_iovlen].iov_base = "/W ";
14512909Swollman		iov[msg.msg_iovlen++].iov_len = 3;
14612909Swollman	}
1471590Srgrimes	/* send the name followed by <CR><LF> */
14812909Swollman	iov[msg.msg_iovlen].iov_base = name;
14912909Swollman	iov[msg.msg_iovlen++].iov_len = strlen(name);
15012909Swollman	iov[msg.msg_iovlen].iov_base = "\r\n";
15112909Swollman	iov[msg.msg_iovlen++].iov_len = 2;
1521590Srgrimes
15366199Swollman	/*
15466199Swollman	 * -T disables data-on-SYN: compatibility option to finger broken
15566199Swollman	 * hosts.  Also, the implicit-open API is broken on IPv6, so do
15666199Swollman	 * the explicit connect there, too.
15766199Swollman	 */
15866199Swollman	if ((Tflag || ai->ai_addr->sa_family == AF_INET6)
15966199Swollman	    && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
16066199Swollman		warn("connect");
16166199Swollman		close(s);
16266199Swollman		return -1;
16314631Solah	}
16414631Solah
16527835Swollman	if (sendmsg(s, &msg, 0) < 0) {
16666199Swollman		warn("sendmsg");
16712909Swollman		close(s);
16866199Swollman		return -1;
16912909Swollman	}
17012909Swollman
1711590Srgrimes	/*
1721590Srgrimes	 * Read from the remote system; once we're connected, we assume some
1731590Srgrimes	 * data.  If none arrives, we hang until the user interrupts.
1741590Srgrimes	 *
1751590Srgrimes	 * If we see a <CR> or a <CR> with the high bit set, treat it as
1761590Srgrimes	 * a newline; if followed by a newline character, only output one
1771590Srgrimes	 * newline.
1781590Srgrimes	 *
1791590Srgrimes	 * Otherwise, all high bits are stripped; if it isn't printable and
1801590Srgrimes	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
1811590Srgrimes	 * character with bit 7 set is printable.
1828874Srgrimes	 */
18323693Speter	lastc = 0;
18423693Speter	if ((fp = fdopen(s, "r")) != NULL) {
18546662Sobrien		cnt = 0;
18646662Sobrien		line_len = 0;
1871590Srgrimes		while ((c = getc(fp)) != EOF) {
18846662Sobrien			if (++cnt > OUTPUT_MAX) {
18946662Sobrien				printf("\n\n Output truncated at %d bytes...\n",
19046662Sobrien					cnt - 1);
19146662Sobrien				break;
19246662Sobrien			}
1931590Srgrimes			if (c == 0x0d) {
1941590Srgrimes				if (lastc == '\r')	/* ^M^M - skip dupes */
1951590Srgrimes					continue;
1961590Srgrimes				c = '\n';
1971590Srgrimes				lastc = '\r';
1981590Srgrimes			} else {
19914472Sache				if (!isprint(c) && !isspace(c)) {
20014472Sache					c &= 0x7f;
2011590Srgrimes					c |= 0x40;
20214472Sache				}
2031590Srgrimes				if (lastc != '\r' || c != '\n')
2041590Srgrimes					lastc = c;
2051590Srgrimes				else {
2061590Srgrimes					lastc = '\n';
2071590Srgrimes					continue;
2081590Srgrimes				}
2091590Srgrimes			}
2101590Srgrimes			putchar(c);
21146662Sobrien			if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
21246662Sobrien				putchar('\\');
21346662Sobrien				putchar('\n');
21446662Sobrien				lastc = '\r';
21546662Sobrien			}
21646662Sobrien			if (lastc == '\n' || lastc == '\r')
21746662Sobrien				line_len = 0;
2181590Srgrimes		}
21912909Swollman		if (ferror(fp)) {
22012909Swollman			/*
22112909Swollman			 * Assume that whatever it was set errno...
22212909Swollman			 */
22366199Swollman			warn("reading from network");
22412909Swollman		}
22566199Swollman		if (lastc != '\n')
22666199Swollman			putchar('\n');
22766199Swollman
22866199Swollman		fclose(fp);
22912909Swollman	}
23066199Swollman	return 0;
2311590Srgrimes}
23266199Swollman
23366199Swollmanstatic void
23466199Swollmantrying(const struct addrinfo *ai)
23566199Swollman{
23666199Swollman	char buf[NI_MAXHOST];
23766199Swollman
23866199Swollman	if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf,
23966199Swollman			(char *)0, 0, NI_NUMERICHOST) != 0)
24066199Swollman		return;		/* XXX can't happen */
24166199Swollman
24266199Swollman	printf("Trying %s...\n", buf);
24366199Swollman}
24466199Swollman
24566199Swollmanvoid
24666199Swollmancleanup(int sig)
24766199Swollman{
24866199Swollman#define	ERRSTR	"Timed out.\n"
24966199Swollman	write(STDERR_FILENO, ERRSTR, sizeof ERRSTR);
25066199Swollman	exit(1);
25166199Swollman}
25266199Swollman
253