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[] = 42b14d8277SPhilippe Charnier "$Id$"; 43b14d8277SPhilippe Charnier #endif 449b50d902SRodney W. Grimes #endif /* not lint */ 459b50d902SRodney W. Grimes 469b50d902SRodney W. Grimes #include <sys/types.h> 479b50d902SRodney W. Grimes #include <sys/socket.h> 489b50d902SRodney W. Grimes #include <netinet/in.h> 499b50d902SRodney W. Grimes #include <arpa/inet.h> 509b50d902SRodney W. Grimes #include <netdb.h> 519b50d902SRodney W. Grimes #include <db.h> 52b14d8277SPhilippe Charnier #include <err.h> 539b50d902SRodney W. Grimes #include <unistd.h> 549b50d902SRodney W. Grimes #include <pwd.h> 559b50d902SRodney W. Grimes #include <utmp.h> 569b50d902SRodney W. Grimes #include <stdio.h> 579b50d902SRodney W. Grimes #include <ctype.h> 589b50d902SRodney W. Grimes #include <string.h> 591e474c62SGarrett Wollman #include <sys/uio.h> 609b50d902SRodney W. Grimes #include "finger.h" 619b50d902SRodney W. Grimes 629b50d902SRodney W. Grimes void 639b50d902SRodney W. Grimes netfinger(name) 649b50d902SRodney W. Grimes char *name; 659b50d902SRodney W. Grimes { 669b50d902SRodney W. Grimes extern int lflag; 6703801815SAndras Olah extern int Tflag; 689b50d902SRodney W. Grimes register FILE *fp; 699b50d902SRodney W. Grimes register int c, lastc; 709b50d902SRodney W. Grimes struct in_addr defaddr; 719b50d902SRodney W. Grimes struct hostent *hp, def; 729b50d902SRodney W. Grimes struct servent *sp; 739b50d902SRodney W. Grimes struct sockaddr_in sin; 749b50d902SRodney W. Grimes int s; 759b50d902SRodney W. Grimes char *alist[1], *host; 761e474c62SGarrett Wollman struct iovec iov[3]; 771e474c62SGarrett Wollman struct msghdr msg; 789b50d902SRodney W. Grimes 799b50d902SRodney W. Grimes if (!(host = rindex(name, '@'))) 809b50d902SRodney W. Grimes return; 81b14d8277SPhilippe Charnier *host++ = '\0'; 829b50d902SRodney W. Grimes if (isdigit(*host) && (defaddr.s_addr = inet_addr(host)) != -1) { 839b50d902SRodney W. Grimes def.h_name = host; 849b50d902SRodney W. Grimes def.h_addr_list = alist; 859b50d902SRodney W. Grimes def.h_addr = (char *)&defaddr; 869b50d902SRodney W. Grimes def.h_length = sizeof(struct in_addr); 879b50d902SRodney W. Grimes def.h_addrtype = AF_INET; 889b50d902SRodney W. Grimes def.h_aliases = 0; 899b50d902SRodney W. Grimes hp = &def; 909b50d902SRodney W. Grimes } else if (!(hp = gethostbyname(host))) { 91b14d8277SPhilippe Charnier warnx("unknown host: %s", host); 929b50d902SRodney W. Grimes return; 939b50d902SRodney W. Grimes } 949b50d902SRodney W. Grimes if (!(sp = getservbyname("finger", "tcp"))) { 95b14d8277SPhilippe Charnier warnx("tcp/finger: unknown service"); 969b50d902SRodney W. Grimes return; 979b50d902SRodney W. Grimes } 989b50d902SRodney W. Grimes sin.sin_family = hp->h_addrtype; 999b50d902SRodney W. Grimes bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 1009b50d902SRodney W. Grimes sin.sin_port = sp->s_port; 1019b50d902SRodney W. Grimes if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { 1029b50d902SRodney W. Grimes perror("finger: socket"); 1039b50d902SRodney W. Grimes return; 1049b50d902SRodney W. Grimes } 1059b50d902SRodney W. Grimes 1069b50d902SRodney W. Grimes /* have network connection; identify the host connected with */ 1079b50d902SRodney W. Grimes (void)printf("[%s]\n", hp->h_name); 1081e474c62SGarrett Wollman 1091e474c62SGarrett Wollman msg.msg_name = (void *)&sin; 1101e474c62SGarrett Wollman msg.msg_namelen = sizeof sin; 1111e474c62SGarrett Wollman msg.msg_iov = iov; 1121e474c62SGarrett Wollman msg.msg_iovlen = 0; 1131e474c62SGarrett Wollman msg.msg_control = 0; 1141e474c62SGarrett Wollman msg.msg_controllen = 0; 1151e474c62SGarrett Wollman msg.msg_flags = MSG_EOF; 1169b50d902SRodney W. Grimes 1179b50d902SRodney W. Grimes /* -l flag for remote fingerd */ 1181e474c62SGarrett Wollman if (lflag) { 1191e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "/W "; 1201e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 3; 1211e474c62SGarrett Wollman } 1229b50d902SRodney W. Grimes /* send the name followed by <CR><LF> */ 1231e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = name; 1241e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = strlen(name); 1251e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "\r\n"; 1261e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 2; 1271e474c62SGarrett Wollman 12803801815SAndras Olah /* -T disables T/TCP: compatibility option to finger broken hosts */ 12903801815SAndras Olah if (Tflag && connect(s, (struct sockaddr *)&sin, sizeof (sin))) { 13003801815SAndras Olah perror("finger: connect"); 13103801815SAndras Olah return; 13203801815SAndras Olah } 13303801815SAndras Olah 1341e474c62SGarrett Wollman if (sendmsg(s, &msg, MSG_EOF) < 0) { 1351e474c62SGarrett Wollman perror("finger: sendmsg"); 1361e474c62SGarrett Wollman close(s); 1371e474c62SGarrett Wollman return; 1381e474c62SGarrett Wollman } 1399b50d902SRodney W. Grimes 1409b50d902SRodney W. Grimes /* 1419b50d902SRodney W. Grimes * Read from the remote system; once we're connected, we assume some 1429b50d902SRodney W. Grimes * data. If none arrives, we hang until the user interrupts. 1439b50d902SRodney W. Grimes * 1449b50d902SRodney W. Grimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1459b50d902SRodney W. Grimes * a newline; if followed by a newline character, only output one 1469b50d902SRodney W. Grimes * newline. 1479b50d902SRodney W. Grimes * 1489b50d902SRodney W. Grimes * Otherwise, all high bits are stripped; if it isn't printable and 1499b50d902SRodney W. Grimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1509b50d902SRodney W. Grimes * character with bit 7 set is printable. 1519b50d902SRodney W. Grimes */ 152df3f5d9dSPeter Wemm lastc = 0; 153df3f5d9dSPeter Wemm if ((fp = fdopen(s, "r")) != NULL) { 1549b50d902SRodney W. Grimes while ((c = getc(fp)) != EOF) { 1559b50d902SRodney W. Grimes if (c == 0x0d) { 1569b50d902SRodney W. Grimes if (lastc == '\r') /* ^M^M - skip dupes */ 1579b50d902SRodney W. Grimes continue; 1589b50d902SRodney W. Grimes c = '\n'; 1599b50d902SRodney W. Grimes lastc = '\r'; 1609b50d902SRodney W. Grimes } else { 161a0a47889SAndrey A. Chernov if (!isprint(c) && !isspace(c)) { 162a0a47889SAndrey A. Chernov c &= 0x7f; 1639b50d902SRodney W. Grimes c |= 0x40; 164a0a47889SAndrey A. Chernov } 1659b50d902SRodney W. Grimes if (lastc != '\r' || c != '\n') 1669b50d902SRodney W. Grimes lastc = c; 1679b50d902SRodney W. Grimes else { 1689b50d902SRodney W. Grimes lastc = '\n'; 1699b50d902SRodney W. Grimes continue; 1709b50d902SRodney W. Grimes } 1719b50d902SRodney W. Grimes } 1729b50d902SRodney W. Grimes putchar(c); 1739b50d902SRodney W. Grimes } 1749b50d902SRodney W. Grimes if (lastc != '\n') 1759b50d902SRodney W. Grimes putchar('\n'); 1761e474c62SGarrett Wollman 1771e474c62SGarrett Wollman if (ferror(fp)) { 1781e474c62SGarrett Wollman /* 1791e474c62SGarrett Wollman * Assume that whatever it was set errno... 1801e474c62SGarrett Wollman */ 1811e474c62SGarrett Wollman perror("finger: read"); 1821e474c62SGarrett Wollman } 1839b50d902SRodney W. Grimes (void)fclose(fp); 1849b50d902SRodney W. Grimes } 1851e474c62SGarrett Wollman } 186