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 38df3f5d9dSPeter Wemm static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95"; 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; 6103801815SAndras Olah extern int Tflag; 629b50d902SRodney W. Grimes register FILE *fp; 639b50d902SRodney W. Grimes register int c, lastc; 649b50d902SRodney W. Grimes struct in_addr defaddr; 659b50d902SRodney W. Grimes struct hostent *hp, def; 669b50d902SRodney W. Grimes struct servent *sp; 679b50d902SRodney W. Grimes struct sockaddr_in sin; 689b50d902SRodney W. Grimes int s; 699b50d902SRodney W. Grimes char *alist[1], *host; 701e474c62SGarrett Wollman struct iovec iov[3]; 711e474c62SGarrett Wollman struct msghdr msg; 729b50d902SRodney W. Grimes 739b50d902SRodney W. Grimes if (!(host = rindex(name, '@'))) 749b50d902SRodney W. Grimes return; 759b50d902SRodney W. Grimes *host++ = NULL; 769b50d902SRodney W. Grimes if (isdigit(*host) && (defaddr.s_addr = inet_addr(host)) != -1) { 779b50d902SRodney W. Grimes def.h_name = host; 789b50d902SRodney W. Grimes def.h_addr_list = alist; 799b50d902SRodney W. Grimes def.h_addr = (char *)&defaddr; 809b50d902SRodney W. Grimes def.h_length = sizeof(struct in_addr); 819b50d902SRodney W. Grimes def.h_addrtype = AF_INET; 829b50d902SRodney W. Grimes def.h_aliases = 0; 839b50d902SRodney W. Grimes hp = &def; 849b50d902SRodney W. Grimes } else if (!(hp = gethostbyname(host))) { 859b50d902SRodney W. Grimes (void)fprintf(stderr, 869b50d902SRodney W. Grimes "finger: unknown host: %s\n", host); 879b50d902SRodney W. Grimes return; 889b50d902SRodney W. Grimes } 899b50d902SRodney W. Grimes if (!(sp = getservbyname("finger", "tcp"))) { 909b50d902SRodney W. Grimes (void)fprintf(stderr, "finger: tcp/finger: unknown service\n"); 919b50d902SRodney W. Grimes return; 929b50d902SRodney W. Grimes } 939b50d902SRodney W. Grimes sin.sin_family = hp->h_addrtype; 949b50d902SRodney W. Grimes bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 959b50d902SRodney W. Grimes sin.sin_port = sp->s_port; 969b50d902SRodney W. Grimes if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { 979b50d902SRodney W. Grimes perror("finger: socket"); 989b50d902SRodney W. Grimes return; 999b50d902SRodney W. Grimes } 1009b50d902SRodney W. Grimes 1019b50d902SRodney W. Grimes /* have network connection; identify the host connected with */ 1029b50d902SRodney W. Grimes (void)printf("[%s]\n", hp->h_name); 1031e474c62SGarrett Wollman 1041e474c62SGarrett Wollman msg.msg_name = (void *)&sin; 1051e474c62SGarrett Wollman msg.msg_namelen = sizeof sin; 1061e474c62SGarrett Wollman msg.msg_iov = iov; 1071e474c62SGarrett Wollman msg.msg_iovlen = 0; 1081e474c62SGarrett Wollman msg.msg_control = 0; 1091e474c62SGarrett Wollman msg.msg_controllen = 0; 1101e474c62SGarrett Wollman msg.msg_flags = MSG_EOF; 1119b50d902SRodney W. Grimes 1129b50d902SRodney W. Grimes /* -l flag for remote fingerd */ 1131e474c62SGarrett Wollman if (lflag) { 1141e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "/W "; 1151e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 3; 1161e474c62SGarrett Wollman } 1179b50d902SRodney W. Grimes /* send the name followed by <CR><LF> */ 1181e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = name; 1191e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = strlen(name); 1201e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "\r\n"; 1211e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 2; 1221e474c62SGarrett Wollman 12303801815SAndras Olah /* -T disables T/TCP: compatibility option to finger broken hosts */ 12403801815SAndras Olah if (Tflag && connect(s, (struct sockaddr *)&sin, sizeof (sin))) { 12503801815SAndras Olah perror("finger: connect"); 12603801815SAndras Olah return; 12703801815SAndras Olah } 12803801815SAndras Olah 1291e474c62SGarrett Wollman if (sendmsg(s, &msg, MSG_EOF) < 0) { 1301e474c62SGarrett Wollman perror("finger: sendmsg"); 1311e474c62SGarrett Wollman close(s); 1321e474c62SGarrett Wollman return; 1331e474c62SGarrett Wollman } 1349b50d902SRodney W. Grimes 1359b50d902SRodney W. Grimes /* 1369b50d902SRodney W. Grimes * Read from the remote system; once we're connected, we assume some 1379b50d902SRodney W. Grimes * data. If none arrives, we hang until the user interrupts. 1389b50d902SRodney W. Grimes * 1399b50d902SRodney W. Grimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1409b50d902SRodney W. Grimes * a newline; if followed by a newline character, only output one 1419b50d902SRodney W. Grimes * newline. 1429b50d902SRodney W. Grimes * 1439b50d902SRodney W. Grimes * Otherwise, all high bits are stripped; if it isn't printable and 1449b50d902SRodney W. Grimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1459b50d902SRodney W. Grimes * character with bit 7 set is printable. 1469b50d902SRodney W. Grimes */ 147df3f5d9dSPeter Wemm lastc = 0; 148df3f5d9dSPeter Wemm if ((fp = fdopen(s, "r")) != NULL) { 1499b50d902SRodney W. Grimes while ((c = getc(fp)) != EOF) { 1509b50d902SRodney W. Grimes if (c == 0x0d) { 1519b50d902SRodney W. Grimes if (lastc == '\r') /* ^M^M - skip dupes */ 1529b50d902SRodney W. Grimes continue; 1539b50d902SRodney W. Grimes c = '\n'; 1549b50d902SRodney W. Grimes lastc = '\r'; 1559b50d902SRodney W. Grimes } else { 156a0a47889SAndrey A. Chernov if (!isprint(c) && !isspace(c)) { 157a0a47889SAndrey A. Chernov c &= 0x7f; 1589b50d902SRodney W. Grimes c |= 0x40; 159a0a47889SAndrey A. Chernov } 1609b50d902SRodney W. Grimes if (lastc != '\r' || c != '\n') 1619b50d902SRodney W. Grimes lastc = c; 1629b50d902SRodney W. Grimes else { 1639b50d902SRodney W. Grimes lastc = '\n'; 1649b50d902SRodney W. Grimes continue; 1659b50d902SRodney W. Grimes } 1669b50d902SRodney W. Grimes } 1679b50d902SRodney W. Grimes putchar(c); 1689b50d902SRodney W. Grimes } 1699b50d902SRodney W. Grimes if (lastc != '\n') 1709b50d902SRodney W. Grimes putchar('\n'); 1711e474c62SGarrett Wollman 1721e474c62SGarrett Wollman if (ferror(fp)) { 1731e474c62SGarrett Wollman /* 1741e474c62SGarrett Wollman * Assume that whatever it was set errno... 1751e474c62SGarrett Wollman */ 1761e474c62SGarrett Wollman perror("finger: read"); 1771e474c62SGarrett Wollman } 1789b50d902SRodney W. Grimes (void)fclose(fp); 1799b50d902SRodney W. Grimes } 1801e474c62SGarrett Wollman } 181