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 * 4. Neither the name of the University nor the names of its contributors
171590Srgrimes *    may be used to endorse or promote products derived from this software
181590Srgrimes *    without specific prior written permission.
191590Srgrimes *
201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301590Srgrimes * SUCH DAMAGE.
311590Srgrimes */
321590Srgrimes
3387628Sdwmalone#if 0
3487628Sdwmalone#ifndef lint
3587628Sdwmalonestatic char sccsid[] = "@(#)net.c	8.4 (Berkeley) 4/28/95";
3687628Sdwmalone#endif
3787628Sdwmalone#endif
3887628Sdwmalone
3987229Smarkm#include <sys/cdefs.h>
4087229Smarkm__FBSDID("$FreeBSD$");
4187229Smarkm
4236792Simp#include <sys/param.h>
431590Srgrimes#include <sys/socket.h>
4472109Scharnier#include <sys/uio.h>
4572109Scharnier#include <ctype.h>
461590Srgrimes#include <db.h>
4727169Scharnier#include <err.h>
4872109Scharnier#include <netdb.h>
491590Srgrimes#include <pwd.h>
501590Srgrimes#include <stdio.h>
5178718Sdd#include <stdlib.h>
521590Srgrimes#include <string.h>
5372109Scharnier#include <unistd.h>
54202191Sed#include <utmpx.h>
551590Srgrimes#include "finger.h"
561590Srgrimes
57129302Sstefanfstatic void cleanup(int sig);
5866199Swollmanstatic int do_protocol(const char *name, const struct addrinfo *ai);
5966199Swollmanstatic void trying(const struct addrinfo *ai);
6066199Swollman
6146662Sobrienvoid
62102944Sdwmalonenetfinger(char *name)
631590Srgrimes{
6466199Swollman	int error, multi;
6566199Swollman	char *host;
6666199Swollman	struct addrinfo *ai, *ai0;
6766199Swollman	static struct addrinfo hint;
681590Srgrimes
6966199Swollman	host = strrchr(name, '@');
7066199Swollman	if (host == 0)
711590Srgrimes		return;
7227169Scharnier	*host++ = '\0';
7346662Sobrien	signal(SIGALRM, cleanup);
7466199Swollman	alarm(TIME_LIMIT);
7566199Swollman
7666199Swollman	hint.ai_flags = AI_CANONNAME;
77100521Sume	hint.ai_family = family;
7866199Swollman	hint.ai_socktype = SOCK_STREAM;
7966199Swollman
8066199Swollman	error = getaddrinfo(host, "finger", &hint, &ai0);
8166199Swollman	if (error) {
8266199Swollman		warnx("%s: %s", host, gai_strerror(error));
831590Srgrimes		return;
841590Srgrimes	}
8566199Swollman
8666199Swollman	multi = (ai0->ai_next) != 0;
8766199Swollman
8866315Swollman	/* ai_canonname may not be filled in if the user specified an IP. */
8966315Swollman	if (ai0->ai_canonname == 0)
9066315Swollman		printf("[%s]\n", host);
9166315Swollman	else
9266315Swollman		printf("[%s]\n", ai0->ai_canonname);
9366315Swollman
9466199Swollman	for (ai = ai0; ai != 0; ai = ai->ai_next) {
9566199Swollman		if (multi)
9666199Swollman			trying(ai);
9766199Swollman
9866199Swollman		error = do_protocol(name, ai);
9966199Swollman		if (!error)
10066199Swollman			break;
1011590Srgrimes	}
10266199Swollman	alarm(0);
10366199Swollman	freeaddrinfo(ai0);
10466199Swollman}
10566199Swollman
10666199Swollmanstatic int
10766199Swollmando_protocol(const char *name, const struct addrinfo *ai)
10866199Swollman{
10967467Sru	int cnt, line_len, s;
11087229Smarkm	FILE *fp;
11187229Smarkm	int c, lastc;
11266199Swollman	struct iovec iov[3];
11366199Swollman	struct msghdr msg;
11487229Smarkm	static char slash_w[] = "/W ";
115107528Sroam	static char neteol[] = "\r\n";
11666199Swollman
11766199Swollman	s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
11866199Swollman	if (s < 0) {
11966199Swollman		warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
12066199Swollman		     ai->ai_protocol);
12166199Swollman		return -1;
1221590Srgrimes	}
1231590Srgrimes
12466199Swollman	msg.msg_name = (void *)ai->ai_addr;
12566199Swollman	msg.msg_namelen = ai->ai_addrlen;
12612909Swollman	msg.msg_iov = iov;
12712909Swollman	msg.msg_iovlen = 0;
12812909Swollman	msg.msg_control = 0;
12912909Swollman	msg.msg_controllen = 0;
13027835Swollman	msg.msg_flags = 0;
13112909Swollman
1321590Srgrimes	/* -l flag for remote fingerd  */
13312909Swollman	if (lflag) {
13487229Smarkm		iov[msg.msg_iovlen].iov_base = slash_w;
13512909Swollman		iov[msg.msg_iovlen++].iov_len = 3;
13612909Swollman	}
1371590Srgrimes	/* send the name followed by <CR><LF> */
13887229Smarkm	iov[msg.msg_iovlen].iov_base = strdup(name);
13912909Swollman	iov[msg.msg_iovlen++].iov_len = strlen(name);
14087229Smarkm	iov[msg.msg_iovlen].iov_base = neteol;
14112909Swollman	iov[msg.msg_iovlen++].iov_len = 2;
1421590Srgrimes
143168632Sdes	if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
14466199Swollman		warn("connect");
14566199Swollman		close(s);
14666199Swollman		return -1;
14714631Solah	}
14814631Solah
14927835Swollman	if (sendmsg(s, &msg, 0) < 0) {
15066199Swollman		warn("sendmsg");
15112909Swollman		close(s);
15266199Swollman		return -1;
15312909Swollman	}
15412909Swollman
1551590Srgrimes	/*
1561590Srgrimes	 * Read from the remote system; once we're connected, we assume some
1571590Srgrimes	 * data.  If none arrives, we hang until the user interrupts.
1581590Srgrimes	 *
1591590Srgrimes	 * If we see a <CR> or a <CR> with the high bit set, treat it as
1601590Srgrimes	 * a newline; if followed by a newline character, only output one
1611590Srgrimes	 * newline.
1621590Srgrimes	 *
1631590Srgrimes	 * Otherwise, all high bits are stripped; if it isn't printable and
1641590Srgrimes	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
1651590Srgrimes	 * character with bit 7 set is printable.
1668874Srgrimes	 */
16723693Speter	lastc = 0;
16823693Speter	if ((fp = fdopen(s, "r")) != NULL) {
16946662Sobrien		cnt = 0;
17046662Sobrien		line_len = 0;
1711590Srgrimes		while ((c = getc(fp)) != EOF) {
17246662Sobrien			if (++cnt > OUTPUT_MAX) {
17346662Sobrien				printf("\n\n Output truncated at %d bytes...\n",
17446662Sobrien					cnt - 1);
17546662Sobrien				break;
17646662Sobrien			}
1771590Srgrimes			if (c == 0x0d) {
1781590Srgrimes				if (lastc == '\r')	/* ^M^M - skip dupes */
1791590Srgrimes					continue;
1801590Srgrimes				c = '\n';
1811590Srgrimes				lastc = '\r';
1821590Srgrimes			} else {
18314472Sache				if (!isprint(c) && !isspace(c)) {
18414472Sache					c &= 0x7f;
1851590Srgrimes					c |= 0x40;
18614472Sache				}
1871590Srgrimes				if (lastc != '\r' || c != '\n')
1881590Srgrimes					lastc = c;
1891590Srgrimes				else {
1901590Srgrimes					lastc = '\n';
1911590Srgrimes					continue;
1921590Srgrimes				}
1931590Srgrimes			}
1941590Srgrimes			putchar(c);
19546662Sobrien			if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
19646662Sobrien				putchar('\\');
19746662Sobrien				putchar('\n');
19846662Sobrien				lastc = '\r';
19946662Sobrien			}
20046662Sobrien			if (lastc == '\n' || lastc == '\r')
20146662Sobrien				line_len = 0;
2021590Srgrimes		}
20312909Swollman		if (ferror(fp)) {
20412909Swollman			/*
20512909Swollman			 * Assume that whatever it was set errno...
20612909Swollman			 */
20766199Swollman			warn("reading from network");
20812909Swollman		}
20966199Swollman		if (lastc != '\n')
21066199Swollman			putchar('\n');
21166199Swollman
21266199Swollman		fclose(fp);
21312909Swollman	}
21466199Swollman	return 0;
2151590Srgrimes}
21666199Swollman
21766199Swollmanstatic void
21866199Swollmantrying(const struct addrinfo *ai)
21966199Swollman{
22066199Swollman	char buf[NI_MAXHOST];
22166199Swollman
22266199Swollman	if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf,
22366199Swollman			(char *)0, 0, NI_NUMERICHOST) != 0)
22466199Swollman		return;		/* XXX can't happen */
22566199Swollman
22666199Swollman	printf("Trying %s...\n", buf);
22766199Swollman}
22866199Swollman
229223374Sdelphijstatic void
23087229Smarkmcleanup(int sig __unused)
23166199Swollman{
23266199Swollman#define	ERRSTR	"Timed out.\n"
23366199Swollman	write(STDERR_FILENO, ERRSTR, sizeof ERRSTR);
23466199Swollman	exit(1);
23566199Swollman}
23666199Swollman
237