xref: /freebsd/usr.bin/finger/net.c (revision 1e474c62ca4b3be27403e423eab170f7e10f566a)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)net.c	8.3 (Berkeley) 1/2/94";
399b50d902SRodney W. Grimes #endif /* not lint */
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes #include <sys/types.h>
429b50d902SRodney W. Grimes #include <sys/socket.h>
439b50d902SRodney W. Grimes #include <netinet/in.h>
449b50d902SRodney W. Grimes #include <arpa/inet.h>
459b50d902SRodney W. Grimes #include <netdb.h>
469b50d902SRodney W. Grimes #include <db.h>
479b50d902SRodney W. Grimes #include <unistd.h>
489b50d902SRodney W. Grimes #include <pwd.h>
499b50d902SRodney W. Grimes #include <utmp.h>
509b50d902SRodney W. Grimes #include <stdio.h>
519b50d902SRodney W. Grimes #include <ctype.h>
529b50d902SRodney W. Grimes #include <string.h>
531e474c62SGarrett Wollman #include <sys/uio.h>
549b50d902SRodney W. Grimes #include "finger.h"
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes void
579b50d902SRodney W. Grimes netfinger(name)
589b50d902SRodney W. Grimes 	char *name;
599b50d902SRodney W. Grimes {
609b50d902SRodney W. Grimes 	extern int lflag;
619b50d902SRodney W. Grimes 	register FILE *fp;
629b50d902SRodney W. Grimes 	register int c, lastc;
639b50d902SRodney W. Grimes 	struct in_addr defaddr;
649b50d902SRodney W. Grimes 	struct hostent *hp, def;
659b50d902SRodney W. Grimes 	struct servent *sp;
669b50d902SRodney W. Grimes 	struct sockaddr_in sin;
679b50d902SRodney W. Grimes 	int s;
689b50d902SRodney W. Grimes 	char *alist[1], *host;
691e474c62SGarrett Wollman 	struct iovec iov[3];
701e474c62SGarrett Wollman 	struct msghdr msg;
719b50d902SRodney W. Grimes 
729b50d902SRodney W. Grimes 	if (!(host = rindex(name, '@')))
739b50d902SRodney W. Grimes 		return;
749b50d902SRodney W. Grimes 	*host++ = NULL;
759b50d902SRodney W. Grimes 	if (isdigit(*host) && (defaddr.s_addr = inet_addr(host)) != -1) {
769b50d902SRodney W. Grimes 		def.h_name = host;
779b50d902SRodney W. Grimes 		def.h_addr_list = alist;
789b50d902SRodney W. Grimes 		def.h_addr = (char *)&defaddr;
799b50d902SRodney W. Grimes 		def.h_length = sizeof(struct in_addr);
809b50d902SRodney W. Grimes 		def.h_addrtype = AF_INET;
819b50d902SRodney W. Grimes 		def.h_aliases = 0;
829b50d902SRodney W. Grimes 		hp = &def;
839b50d902SRodney W. Grimes 	} else if (!(hp = gethostbyname(host))) {
849b50d902SRodney W. Grimes 		(void)fprintf(stderr,
859b50d902SRodney W. Grimes 		    "finger: unknown host: %s\n", host);
869b50d902SRodney W. Grimes 		return;
879b50d902SRodney W. Grimes 	}
889b50d902SRodney W. Grimes 	if (!(sp = getservbyname("finger", "tcp"))) {
899b50d902SRodney W. Grimes 		(void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
909b50d902SRodney W. Grimes 		return;
919b50d902SRodney W. Grimes 	}
929b50d902SRodney W. Grimes 	sin.sin_family = hp->h_addrtype;
939b50d902SRodney W. Grimes 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
949b50d902SRodney W. Grimes 	sin.sin_port = sp->s_port;
959b50d902SRodney W. Grimes 	if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
969b50d902SRodney W. Grimes 		perror("finger: socket");
979b50d902SRodney W. Grimes 		return;
989b50d902SRodney W. Grimes 	}
999b50d902SRodney W. Grimes 
1009b50d902SRodney W. Grimes 	/* have network connection; identify the host connected with */
1019b50d902SRodney W. Grimes 	(void)printf("[%s]\n", hp->h_name);
1021e474c62SGarrett Wollman 
1031e474c62SGarrett Wollman 	msg.msg_name = (void *)&sin;
1041e474c62SGarrett Wollman 	msg.msg_namelen = sizeof sin;
1051e474c62SGarrett Wollman 	msg.msg_iov = iov;
1061e474c62SGarrett Wollman 	msg.msg_iovlen = 0;
1071e474c62SGarrett Wollman 	msg.msg_control = 0;
1081e474c62SGarrett Wollman 	msg.msg_controllen = 0;
1091e474c62SGarrett Wollman 	msg.msg_flags = MSG_EOF;
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes 	/* -l flag for remote fingerd  */
1121e474c62SGarrett Wollman 	if (lflag) {
1131e474c62SGarrett Wollman 		iov[msg.msg_iovlen].iov_base = "/W ";
1141e474c62SGarrett Wollman 		iov[msg.msg_iovlen++].iov_len = 3;
1151e474c62SGarrett Wollman 	}
1169b50d902SRodney W. Grimes 	/* send the name followed by <CR><LF> */
1171e474c62SGarrett Wollman 	iov[msg.msg_iovlen].iov_base = name;
1181e474c62SGarrett Wollman 	iov[msg.msg_iovlen++].iov_len = strlen(name);
1191e474c62SGarrett Wollman 	iov[msg.msg_iovlen].iov_base = "\r\n";
1201e474c62SGarrett Wollman 	iov[msg.msg_iovlen++].iov_len = 2;
1211e474c62SGarrett Wollman 
1221e474c62SGarrett Wollman 	if (sendmsg(s, &msg, MSG_EOF) < 0) {
1231e474c62SGarrett Wollman 		perror("finger: sendmsg");
1241e474c62SGarrett Wollman 		close(s);
1251e474c62SGarrett Wollman 		return;
1261e474c62SGarrett Wollman 	}
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes 	/*
1299b50d902SRodney W. Grimes 	 * Read from the remote system; once we're connected, we assume some
1309b50d902SRodney W. Grimes 	 * data.  If none arrives, we hang until the user interrupts.
1319b50d902SRodney W. Grimes 	 *
1329b50d902SRodney W. Grimes 	 * If we see a <CR> or a <CR> with the high bit set, treat it as
1339b50d902SRodney W. Grimes 	 * a newline; if followed by a newline character, only output one
1349b50d902SRodney W. Grimes 	 * newline.
1359b50d902SRodney W. Grimes 	 *
1369b50d902SRodney W. Grimes 	 * Otherwise, all high bits are stripped; if it isn't printable and
1379b50d902SRodney W. Grimes 	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
1389b50d902SRodney W. Grimes 	 * character with bit 7 set is printable.
1399b50d902SRodney W. Grimes 	 */
1401e474c62SGarrett Wollman 	if (fp = fdopen(s, "r")) {
1411e474c62SGarrett Wollman 		int lastc = '\n';
1421e474c62SGarrett Wollman 
1439b50d902SRodney W. Grimes 		while ((c = getc(fp)) != EOF) {
1449b50d902SRodney W. Grimes 			c &= 0x7f;
1459b50d902SRodney W. Grimes 			if (c == 0x0d) {
1469b50d902SRodney W. Grimes 				if (lastc == '\r')	/* ^M^M - skip dupes */
1479b50d902SRodney W. Grimes 					continue;
1489b50d902SRodney W. Grimes 				c = '\n';
1499b50d902SRodney W. Grimes 				lastc = '\r';
1509b50d902SRodney W. Grimes 			} else {
1519b50d902SRodney W. Grimes 				if (!isprint(c) && !isspace(c))
1529b50d902SRodney W. Grimes 					c |= 0x40;
1539b50d902SRodney W. Grimes 				if (lastc != '\r' || c != '\n')
1549b50d902SRodney W. Grimes 					lastc = c;
1559b50d902SRodney W. Grimes 				else {
1569b50d902SRodney W. Grimes 					lastc = '\n';
1579b50d902SRodney W. Grimes 					continue;
1589b50d902SRodney W. Grimes 				}
1599b50d902SRodney W. Grimes 			}
1609b50d902SRodney W. Grimes 			putchar(c);
1619b50d902SRodney W. Grimes 		}
1629b50d902SRodney W. Grimes 		if (lastc != '\n')
1639b50d902SRodney W. Grimes 			putchar('\n');
1641e474c62SGarrett Wollman 
1651e474c62SGarrett Wollman 		if (ferror(fp)) {
1661e474c62SGarrett Wollman 			/*
1671e474c62SGarrett Wollman 			 * Assume that whatever it was set errno...
1681e474c62SGarrett Wollman 			 */
1691e474c62SGarrett Wollman 			perror("finger: read");
1701e474c62SGarrett Wollman 		}
1719b50d902SRodney W. Grimes 		(void)fclose(fp);
1729b50d902SRodney W. Grimes 	}
1731e474c62SGarrett Wollman }
174