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 * 4. Neither the name of the University nor the names of its contributors 179b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 189b50d902SRodney W. Grimes * without specific prior written permission. 199b50d902SRodney W. Grimes * 209b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 219b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 229b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 239b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 249b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 259b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 269b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 279b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 289b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 299b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 309b50d902SRodney W. Grimes * SUCH DAMAGE. 319b50d902SRodney W. Grimes */ 329b50d902SRodney W. Grimes 339f5b04e9SDavid Malone #if 0 349b50d902SRodney W. Grimes #ifndef lint 359f5b04e9SDavid Malone static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95"; 364c86f3adSPhilippe Charnier #endif 379f5b04e9SDavid Malone #endif 389f5b04e9SDavid Malone 399f5b04e9SDavid Malone #include <sys/cdefs.h> 409f5b04e9SDavid Malone __FBSDID("$FreeBSD$"); 419b50d902SRodney W. Grimes 42a716ad66SWarner Losh #include <sys/param.h> 439b50d902SRodney W. Grimes #include <sys/socket.h> 444c86f3adSPhilippe Charnier #include <sys/uio.h> 454c86f3adSPhilippe Charnier #include <ctype.h> 469b50d902SRodney W. Grimes #include <db.h> 47b14d8277SPhilippe Charnier #include <err.h> 484c86f3adSPhilippe Charnier #include <netdb.h> 499b50d902SRodney W. Grimes #include <pwd.h> 509b50d902SRodney W. Grimes #include <stdio.h> 517a19d1bbSDima Dorfman #include <stdlib.h> 529b50d902SRodney W. Grimes #include <string.h> 534c86f3adSPhilippe Charnier #include <unistd.h> 54550dcc59SEd Schouten #include <utmpx.h> 559b50d902SRodney W. Grimes #include "finger.h" 569b50d902SRodney W. Grimes 57a7526044SStefan Farfeleder static void cleanup(int sig); 58e45ccde7SGarrett Wollman static int do_protocol(const char *name, const struct addrinfo *ai); 59e45ccde7SGarrett Wollman static void trying(const struct addrinfo *ai); 60dd5288f3SDavid E. O'Brien 61dd5288f3SDavid E. O'Brien void 62f4ac32deSDavid Malone netfinger(char *name) 639b50d902SRodney W. Grimes { 64e45ccde7SGarrett Wollman int error, multi; 65e45ccde7SGarrett Wollman char *host; 66e45ccde7SGarrett Wollman struct addrinfo *ai, *ai0; 67e45ccde7SGarrett Wollman static struct addrinfo hint; 689b50d902SRodney W. Grimes 69e45ccde7SGarrett Wollman host = strrchr(name, '@'); 70e45ccde7SGarrett Wollman if (host == 0) 719b50d902SRodney W. Grimes return; 72b14d8277SPhilippe Charnier *host++ = '\0'; 73dd5288f3SDavid E. O'Brien signal(SIGALRM, cleanup); 74e45ccde7SGarrett Wollman alarm(TIME_LIMIT); 75e45ccde7SGarrett Wollman 76e45ccde7SGarrett Wollman hint.ai_flags = AI_CANONNAME; 773daa8471SHajimu UMEMOTO hint.ai_family = family; 78e45ccde7SGarrett Wollman hint.ai_socktype = SOCK_STREAM; 79e45ccde7SGarrett Wollman 80e45ccde7SGarrett Wollman error = getaddrinfo(host, "finger", &hint, &ai0); 81e45ccde7SGarrett Wollman if (error) { 82e45ccde7SGarrett Wollman warnx("%s: %s", host, gai_strerror(error)); 839b50d902SRodney W. Grimes return; 849b50d902SRodney W. Grimes } 859b50d902SRodney W. Grimes 86e45ccde7SGarrett Wollman multi = (ai0->ai_next) != 0; 872804330fSGarrett Wollman 882804330fSGarrett Wollman /* ai_canonname may not be filled in if the user specified an IP. */ 892804330fSGarrett Wollman if (ai0->ai_canonname == 0) 902804330fSGarrett Wollman printf("[%s]\n", host); 912804330fSGarrett Wollman else 92e45ccde7SGarrett Wollman printf("[%s]\n", ai0->ai_canonname); 931e474c62SGarrett Wollman 94e45ccde7SGarrett Wollman for (ai = ai0; ai != 0; ai = ai->ai_next) { 95e45ccde7SGarrett Wollman if (multi) 96e45ccde7SGarrett Wollman trying(ai); 97e45ccde7SGarrett Wollman 98e45ccde7SGarrett Wollman error = do_protocol(name, ai); 99e45ccde7SGarrett Wollman if (!error) 100e45ccde7SGarrett Wollman break; 101e45ccde7SGarrett Wollman } 102e45ccde7SGarrett Wollman alarm(0); 103e45ccde7SGarrett Wollman freeaddrinfo(ai0); 104e45ccde7SGarrett Wollman } 105e45ccde7SGarrett Wollman 106e45ccde7SGarrett Wollman static int 107e45ccde7SGarrett Wollman do_protocol(const char *name, const struct addrinfo *ai) 108e45ccde7SGarrett Wollman { 1091e832bf8SRuslan Ermilov int cnt, line_len, s; 1104e030ba6SMark Murray FILE *fp; 1114e030ba6SMark Murray int c, lastc; 112e45ccde7SGarrett Wollman struct iovec iov[3]; 113e45ccde7SGarrett Wollman struct msghdr msg; 1144e030ba6SMark Murray static char slash_w[] = "/W "; 1158c9589bdSPeter Pentchev static char neteol[] = "\r\n"; 116e45ccde7SGarrett Wollman 117e45ccde7SGarrett Wollman s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 118e45ccde7SGarrett Wollman if (s < 0) { 119e45ccde7SGarrett Wollman warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype, 120e45ccde7SGarrett Wollman ai->ai_protocol); 121e45ccde7SGarrett Wollman return -1; 122e45ccde7SGarrett Wollman } 123e45ccde7SGarrett Wollman 124e45ccde7SGarrett Wollman msg.msg_name = (void *)ai->ai_addr; 125e45ccde7SGarrett Wollman msg.msg_namelen = ai->ai_addrlen; 1261e474c62SGarrett Wollman msg.msg_iov = iov; 1271e474c62SGarrett Wollman msg.msg_iovlen = 0; 1281e474c62SGarrett Wollman msg.msg_control = 0; 1291e474c62SGarrett Wollman msg.msg_controllen = 0; 130a32cbefaSGarrett Wollman msg.msg_flags = 0; 1319b50d902SRodney W. Grimes 1329b50d902SRodney W. Grimes /* -l flag for remote fingerd */ 1331e474c62SGarrett Wollman if (lflag) { 1344e030ba6SMark Murray iov[msg.msg_iovlen].iov_base = slash_w; 1351e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 3; 1361e474c62SGarrett Wollman } 1379b50d902SRodney W. Grimes /* send the name followed by <CR><LF> */ 1384e030ba6SMark Murray iov[msg.msg_iovlen].iov_base = strdup(name); 1391e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = strlen(name); 1404e030ba6SMark Murray iov[msg.msg_iovlen].iov_base = neteol; 1411e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 2; 1421e474c62SGarrett Wollman 143f8871cc8SDag-Erling Smørgrav if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 144e45ccde7SGarrett Wollman warn("connect"); 145e45ccde7SGarrett Wollman close(s); 146e45ccde7SGarrett Wollman return -1; 14703801815SAndras Olah } 14803801815SAndras Olah 149a32cbefaSGarrett Wollman if (sendmsg(s, &msg, 0) < 0) { 150e45ccde7SGarrett Wollman warn("sendmsg"); 1511e474c62SGarrett Wollman close(s); 152e45ccde7SGarrett Wollman return -1; 1531e474c62SGarrett Wollman } 1549b50d902SRodney W. Grimes 1559b50d902SRodney W. Grimes /* 1569b50d902SRodney W. Grimes * Read from the remote system; once we're connected, we assume some 1579b50d902SRodney W. Grimes * data. If none arrives, we hang until the user interrupts. 1589b50d902SRodney W. Grimes * 1599b50d902SRodney W. Grimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1609b50d902SRodney W. Grimes * a newline; if followed by a newline character, only output one 1619b50d902SRodney W. Grimes * newline. 1629b50d902SRodney W. Grimes * 1639b50d902SRodney W. Grimes * Otherwise, all high bits are stripped; if it isn't printable and 1649b50d902SRodney W. Grimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1659b50d902SRodney W. Grimes * character with bit 7 set is printable. 1669b50d902SRodney W. Grimes */ 167df3f5d9dSPeter Wemm lastc = 0; 168df3f5d9dSPeter Wemm if ((fp = fdopen(s, "r")) != NULL) { 169dd5288f3SDavid E. O'Brien cnt = 0; 170dd5288f3SDavid E. O'Brien line_len = 0; 1719b50d902SRodney W. Grimes while ((c = getc(fp)) != EOF) { 172dd5288f3SDavid E. O'Brien if (++cnt > OUTPUT_MAX) { 173dd5288f3SDavid E. O'Brien printf("\n\n Output truncated at %d bytes...\n", 174dd5288f3SDavid E. O'Brien cnt - 1); 175dd5288f3SDavid E. O'Brien break; 176dd5288f3SDavid E. O'Brien } 1779b50d902SRodney W. Grimes if (c == 0x0d) { 1789b50d902SRodney W. Grimes if (lastc == '\r') /* ^M^M - skip dupes */ 1799b50d902SRodney W. Grimes continue; 1809b50d902SRodney W. Grimes c = '\n'; 1819b50d902SRodney W. Grimes lastc = '\r'; 1829b50d902SRodney W. Grimes } else { 183a0a47889SAndrey A. Chernov if (!isprint(c) && !isspace(c)) { 184a0a47889SAndrey A. Chernov c &= 0x7f; 1859b50d902SRodney W. Grimes c |= 0x40; 186a0a47889SAndrey A. Chernov } 1879b50d902SRodney W. Grimes if (lastc != '\r' || c != '\n') 1889b50d902SRodney W. Grimes lastc = c; 1899b50d902SRodney W. Grimes else { 1909b50d902SRodney W. Grimes lastc = '\n'; 1919b50d902SRodney W. Grimes continue; 1929b50d902SRodney W. Grimes } 1939b50d902SRodney W. Grimes } 1949b50d902SRodney W. Grimes putchar(c); 195dd5288f3SDavid E. O'Brien if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) { 196dd5288f3SDavid E. O'Brien putchar('\\'); 197dd5288f3SDavid E. O'Brien putchar('\n'); 198dd5288f3SDavid E. O'Brien lastc = '\r'; 199dd5288f3SDavid E. O'Brien } 200dd5288f3SDavid E. O'Brien if (lastc == '\n' || lastc == '\r') 201dd5288f3SDavid E. O'Brien line_len = 0; 2029b50d902SRodney W. Grimes } 2031e474c62SGarrett Wollman if (ferror(fp)) { 2041e474c62SGarrett Wollman /* 2051e474c62SGarrett Wollman * Assume that whatever it was set errno... 2061e474c62SGarrett Wollman */ 207e45ccde7SGarrett Wollman warn("reading from network"); 2081e474c62SGarrett Wollman } 209e45ccde7SGarrett Wollman if (lastc != '\n') 210e45ccde7SGarrett Wollman putchar('\n'); 211e45ccde7SGarrett Wollman 212e45ccde7SGarrett Wollman fclose(fp); 2139b50d902SRodney W. Grimes } 214e45ccde7SGarrett Wollman return 0; 2151e474c62SGarrett Wollman } 216e45ccde7SGarrett Wollman 217e45ccde7SGarrett Wollman static void 218e45ccde7SGarrett Wollman trying(const struct addrinfo *ai) 219e45ccde7SGarrett Wollman { 220e45ccde7SGarrett Wollman char buf[NI_MAXHOST]; 221e45ccde7SGarrett Wollman 222e45ccde7SGarrett Wollman if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf, 223e45ccde7SGarrett Wollman (char *)0, 0, NI_NUMERICHOST) != 0) 224e45ccde7SGarrett Wollman return; /* XXX can't happen */ 225e45ccde7SGarrett Wollman 226e45ccde7SGarrett Wollman printf("Trying %s...\n", buf); 227e45ccde7SGarrett Wollman } 228e45ccde7SGarrett Wollman 229*cde9717bSXin LI static void 2304e030ba6SMark Murray cleanup(int sig __unused) 231e45ccde7SGarrett Wollman { 232e45ccde7SGarrett Wollman #define ERRSTR "Timed out.\n" 233e45ccde7SGarrett Wollman write(STDERR_FILENO, ERRSTR, sizeof ERRSTR); 234e45ccde7SGarrett Wollman exit(1); 235e45ccde7SGarrett Wollman } 236e45ccde7SGarrett Wollman 237