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 38b14d8277SPhilippe Charnier #if 0 39df3f5d9dSPeter Wemm static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95"; 40b14d8277SPhilippe Charnier #else 41b14d8277SPhilippe Charnier static const char rcsid[] = 42a716ad66SWarner Losh "$Id: net.c,v 1.9 1997/08/01 20:10:44 wollman Exp $"; 43b14d8277SPhilippe Charnier #endif 449b50d902SRodney W. Grimes #endif /* not lint */ 459b50d902SRodney W. Grimes 469b50d902SRodney W. Grimes #include <sys/types.h> 47a716ad66SWarner Losh #include <sys/param.h> 489b50d902SRodney W. Grimes #include <sys/socket.h> 499b50d902SRodney W. Grimes #include <netinet/in.h> 509b50d902SRodney W. Grimes #include <arpa/inet.h> 519b50d902SRodney W. Grimes #include <netdb.h> 529b50d902SRodney W. Grimes #include <db.h> 53b14d8277SPhilippe Charnier #include <err.h> 549b50d902SRodney W. Grimes #include <unistd.h> 559b50d902SRodney W. Grimes #include <pwd.h> 569b50d902SRodney W. Grimes #include <utmp.h> 579b50d902SRodney W. Grimes #include <stdio.h> 589b50d902SRodney W. Grimes #include <ctype.h> 599b50d902SRodney W. Grimes #include <string.h> 601e474c62SGarrett Wollman #include <sys/uio.h> 619b50d902SRodney W. Grimes #include "finger.h" 629b50d902SRodney W. Grimes 639b50d902SRodney W. Grimes void 649b50d902SRodney W. Grimes netfinger(name) 659b50d902SRodney W. Grimes char *name; 669b50d902SRodney W. Grimes { 679b50d902SRodney W. Grimes extern int lflag; 6803801815SAndras Olah extern int Tflag; 699b50d902SRodney W. Grimes register FILE *fp; 709b50d902SRodney W. Grimes register int c, lastc; 719b50d902SRodney W. Grimes struct in_addr defaddr; 729b50d902SRodney W. Grimes struct hostent *hp, def; 739b50d902SRodney W. Grimes struct servent *sp; 749b50d902SRodney W. Grimes struct sockaddr_in sin; 759b50d902SRodney W. Grimes int s; 769b50d902SRodney W. Grimes char *alist[1], *host; 771e474c62SGarrett Wollman struct iovec iov[3]; 781e474c62SGarrett Wollman struct msghdr msg; 799b50d902SRodney W. Grimes 809b50d902SRodney W. Grimes if (!(host = rindex(name, '@'))) 819b50d902SRodney W. Grimes return; 82b14d8277SPhilippe Charnier *host++ = '\0'; 839b50d902SRodney W. Grimes if (isdigit(*host) && (defaddr.s_addr = inet_addr(host)) != -1) { 849b50d902SRodney W. Grimes def.h_name = host; 859b50d902SRodney W. Grimes def.h_addr_list = alist; 869b50d902SRodney W. Grimes def.h_addr = (char *)&defaddr; 879b50d902SRodney W. Grimes def.h_length = sizeof(struct in_addr); 889b50d902SRodney W. Grimes def.h_addrtype = AF_INET; 899b50d902SRodney W. Grimes def.h_aliases = 0; 909b50d902SRodney W. Grimes hp = &def; 919b50d902SRodney W. Grimes } else if (!(hp = gethostbyname(host))) { 92b14d8277SPhilippe Charnier warnx("unknown host: %s", host); 939b50d902SRodney W. Grimes return; 949b50d902SRodney W. Grimes } 959b50d902SRodney W. Grimes if (!(sp = getservbyname("finger", "tcp"))) { 96b14d8277SPhilippe Charnier warnx("tcp/finger: unknown service"); 979b50d902SRodney W. Grimes return; 989b50d902SRodney W. Grimes } 999b50d902SRodney W. Grimes sin.sin_family = hp->h_addrtype; 100a716ad66SWarner Losh bcopy(hp->h_addr, (char *)&sin.sin_addr, MIN(hp->h_length,sizeof(sin.sin_addr))); 1019b50d902SRodney W. Grimes sin.sin_port = sp->s_port; 1029b50d902SRodney W. Grimes if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { 1039b50d902SRodney W. Grimes perror("finger: socket"); 1049b50d902SRodney W. Grimes return; 1059b50d902SRodney W. Grimes } 1069b50d902SRodney W. Grimes 1079b50d902SRodney W. Grimes /* have network connection; identify the host connected with */ 1089b50d902SRodney W. Grimes (void)printf("[%s]\n", hp->h_name); 1091e474c62SGarrett Wollman 1101e474c62SGarrett Wollman msg.msg_name = (void *)&sin; 1111e474c62SGarrett Wollman msg.msg_namelen = sizeof sin; 1121e474c62SGarrett Wollman msg.msg_iov = iov; 1131e474c62SGarrett Wollman msg.msg_iovlen = 0; 1141e474c62SGarrett Wollman msg.msg_control = 0; 1151e474c62SGarrett Wollman msg.msg_controllen = 0; 116a32cbefaSGarrett Wollman msg.msg_flags = 0; 1179b50d902SRodney W. Grimes 1189b50d902SRodney W. Grimes /* -l flag for remote fingerd */ 1191e474c62SGarrett Wollman if (lflag) { 1201e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "/W "; 1211e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 3; 1221e474c62SGarrett Wollman } 1239b50d902SRodney W. Grimes /* send the name followed by <CR><LF> */ 1241e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = name; 1251e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = strlen(name); 1261e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "\r\n"; 1271e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 2; 1281e474c62SGarrett Wollman 12903801815SAndras Olah /* -T disables T/TCP: compatibility option to finger broken hosts */ 13003801815SAndras Olah if (Tflag && connect(s, (struct sockaddr *)&sin, sizeof (sin))) { 13103801815SAndras Olah perror("finger: connect"); 13203801815SAndras Olah return; 13303801815SAndras Olah } 13403801815SAndras Olah 135a32cbefaSGarrett Wollman if (sendmsg(s, &msg, 0) < 0) { 1361e474c62SGarrett Wollman perror("finger: sendmsg"); 1371e474c62SGarrett Wollman close(s); 1381e474c62SGarrett Wollman return; 1391e474c62SGarrett Wollman } 1409b50d902SRodney W. Grimes 1419b50d902SRodney W. Grimes /* 1429b50d902SRodney W. Grimes * Read from the remote system; once we're connected, we assume some 1439b50d902SRodney W. Grimes * data. If none arrives, we hang until the user interrupts. 1449b50d902SRodney W. Grimes * 1459b50d902SRodney W. Grimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1469b50d902SRodney W. Grimes * a newline; if followed by a newline character, only output one 1479b50d902SRodney W. Grimes * newline. 1489b50d902SRodney W. Grimes * 1499b50d902SRodney W. Grimes * Otherwise, all high bits are stripped; if it isn't printable and 1509b50d902SRodney W. Grimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1519b50d902SRodney W. Grimes * character with bit 7 set is printable. 1529b50d902SRodney W. Grimes */ 153df3f5d9dSPeter Wemm lastc = 0; 154df3f5d9dSPeter Wemm if ((fp = fdopen(s, "r")) != NULL) { 1559b50d902SRodney W. Grimes while ((c = getc(fp)) != EOF) { 1569b50d902SRodney W. Grimes if (c == 0x0d) { 1579b50d902SRodney W. Grimes if (lastc == '\r') /* ^M^M - skip dupes */ 1589b50d902SRodney W. Grimes continue; 1599b50d902SRodney W. Grimes c = '\n'; 1609b50d902SRodney W. Grimes lastc = '\r'; 1619b50d902SRodney W. Grimes } else { 162a0a47889SAndrey A. Chernov if (!isprint(c) && !isspace(c)) { 163a0a47889SAndrey A. Chernov c &= 0x7f; 1649b50d902SRodney W. Grimes c |= 0x40; 165a0a47889SAndrey A. Chernov } 1669b50d902SRodney W. Grimes if (lastc != '\r' || c != '\n') 1679b50d902SRodney W. Grimes lastc = c; 1689b50d902SRodney W. Grimes else { 1699b50d902SRodney W. Grimes lastc = '\n'; 1709b50d902SRodney W. Grimes continue; 1719b50d902SRodney W. Grimes } 1729b50d902SRodney W. Grimes } 1739b50d902SRodney W. Grimes putchar(c); 1749b50d902SRodney W. Grimes } 1759b50d902SRodney W. Grimes if (lastc != '\n') 1769b50d902SRodney W. Grimes putchar('\n'); 1771e474c62SGarrett Wollman 1781e474c62SGarrett Wollman if (ferror(fp)) { 1791e474c62SGarrett Wollman /* 1801e474c62SGarrett Wollman * Assume that whatever it was set errno... 1811e474c62SGarrett Wollman */ 1821e474c62SGarrett Wollman perror("finger: read"); 1831e474c62SGarrett Wollman } 1849b50d902SRodney W. Grimes (void)fclose(fp); 1859b50d902SRodney W. Grimes } 1861e474c62SGarrett Wollman } 187