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 379f5b04e9SDavid Malone #if 0 389b50d902SRodney W. Grimes #ifndef lint 399f5b04e9SDavid Malone static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95"; 404c86f3adSPhilippe Charnier #endif 419f5b04e9SDavid Malone #endif 429f5b04e9SDavid Malone 439f5b04e9SDavid Malone #include <sys/cdefs.h> 449f5b04e9SDavid Malone __FBSDID("$FreeBSD$"); 459b50d902SRodney W. Grimes 46a716ad66SWarner Losh #include <sys/param.h> 479b50d902SRodney W. Grimes #include <sys/socket.h> 484c86f3adSPhilippe Charnier #include <sys/uio.h> 494c86f3adSPhilippe Charnier #include <ctype.h> 509b50d902SRodney W. Grimes #include <db.h> 51b14d8277SPhilippe Charnier #include <err.h> 524c86f3adSPhilippe Charnier #include <netdb.h> 539b50d902SRodney W. Grimes #include <pwd.h> 549b50d902SRodney W. Grimes #include <stdio.h> 557a19d1bbSDima Dorfman #include <stdlib.h> 569b50d902SRodney W. Grimes #include <string.h> 574c86f3adSPhilippe Charnier #include <unistd.h> 584c86f3adSPhilippe Charnier #include <utmp.h> 599b50d902SRodney W. Grimes #include "finger.h" 609b50d902SRodney W. Grimes 61e45ccde7SGarrett Wollman extern int lflag; /* XXX finger.h? */ 62e45ccde7SGarrett Wollman extern int Tflag; /* XXX finger.h? */ 633daa8471SHajimu UMEMOTO extern sa_family_t family; 64e45ccde7SGarrett Wollman 65e45ccde7SGarrett Wollman static void cleanup(int sig);; 66e45ccde7SGarrett Wollman static int do_protocol(const char *name, const struct addrinfo *ai); 67e45ccde7SGarrett Wollman static void trying(const struct addrinfo *ai); 68dd5288f3SDavid E. O'Brien 69dd5288f3SDavid E. O'Brien void 709b50d902SRodney W. Grimes netfinger(name) 719b50d902SRodney W. Grimes char *name; 729b50d902SRodney W. Grimes { 73e45ccde7SGarrett Wollman int error, multi; 74e45ccde7SGarrett Wollman char *host; 75e45ccde7SGarrett Wollman struct addrinfo *ai, *ai0; 76e45ccde7SGarrett Wollman static struct addrinfo hint; 779b50d902SRodney W. Grimes 78e45ccde7SGarrett Wollman host = strrchr(name, '@'); 79e45ccde7SGarrett Wollman if (host == 0) 809b50d902SRodney W. Grimes return; 81b14d8277SPhilippe Charnier *host++ = '\0'; 82dd5288f3SDavid E. O'Brien signal(SIGALRM, cleanup); 83e45ccde7SGarrett Wollman alarm(TIME_LIMIT); 84e45ccde7SGarrett Wollman 85e45ccde7SGarrett Wollman hint.ai_flags = AI_CANONNAME; 863daa8471SHajimu UMEMOTO hint.ai_family = family; 87e45ccde7SGarrett Wollman hint.ai_socktype = SOCK_STREAM; 88e45ccde7SGarrett Wollman 89e45ccde7SGarrett Wollman error = getaddrinfo(host, "finger", &hint, &ai0); 90e45ccde7SGarrett Wollman if (error) { 91e45ccde7SGarrett Wollman warnx("%s: %s", host, gai_strerror(error)); 929b50d902SRodney W. Grimes return; 939b50d902SRodney W. Grimes } 949b50d902SRodney W. Grimes 95e45ccde7SGarrett Wollman multi = (ai0->ai_next) != 0; 962804330fSGarrett Wollman 972804330fSGarrett Wollman /* ai_canonname may not be filled in if the user specified an IP. */ 982804330fSGarrett Wollman if (ai0->ai_canonname == 0) 992804330fSGarrett Wollman printf("[%s]\n", host); 1002804330fSGarrett Wollman else 101e45ccde7SGarrett Wollman printf("[%s]\n", ai0->ai_canonname); 1021e474c62SGarrett Wollman 103e45ccde7SGarrett Wollman for (ai = ai0; ai != 0; ai = ai->ai_next) { 104e45ccde7SGarrett Wollman if (multi) 105e45ccde7SGarrett Wollman trying(ai); 106e45ccde7SGarrett Wollman 107e45ccde7SGarrett Wollman error = do_protocol(name, ai); 108e45ccde7SGarrett Wollman if (!error) 109e45ccde7SGarrett Wollman break; 110e45ccde7SGarrett Wollman } 111e45ccde7SGarrett Wollman alarm(0); 112e45ccde7SGarrett Wollman freeaddrinfo(ai0); 113e45ccde7SGarrett Wollman } 114e45ccde7SGarrett Wollman 115e45ccde7SGarrett Wollman static int 116e45ccde7SGarrett Wollman do_protocol(const char *name, const struct addrinfo *ai) 117e45ccde7SGarrett Wollman { 1181e832bf8SRuslan Ermilov int cnt, line_len, s; 1194e030ba6SMark Murray FILE *fp; 1204e030ba6SMark Murray int c, lastc; 121e45ccde7SGarrett Wollman struct iovec iov[3]; 122e45ccde7SGarrett Wollman struct msghdr msg; 1234e030ba6SMark Murray static char slash_w[] = "/W "; 1244e030ba6SMark Murray static char neteol[] = "\n\r"; 125e45ccde7SGarrett Wollman 126e45ccde7SGarrett Wollman s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 127e45ccde7SGarrett Wollman if (s < 0) { 128e45ccde7SGarrett Wollman warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype, 129e45ccde7SGarrett Wollman ai->ai_protocol); 130e45ccde7SGarrett Wollman return -1; 131e45ccde7SGarrett Wollman } 132e45ccde7SGarrett Wollman 133e45ccde7SGarrett Wollman msg.msg_name = (void *)ai->ai_addr; 134e45ccde7SGarrett Wollman msg.msg_namelen = ai->ai_addrlen; 1351e474c62SGarrett Wollman msg.msg_iov = iov; 1361e474c62SGarrett Wollman msg.msg_iovlen = 0; 1371e474c62SGarrett Wollman msg.msg_control = 0; 1381e474c62SGarrett Wollman msg.msg_controllen = 0; 139a32cbefaSGarrett Wollman msg.msg_flags = 0; 1409b50d902SRodney W. Grimes 1419b50d902SRodney W. Grimes /* -l flag for remote fingerd */ 1421e474c62SGarrett Wollman if (lflag) { 1434e030ba6SMark Murray iov[msg.msg_iovlen].iov_base = slash_w; 1441e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 3; 1451e474c62SGarrett Wollman } 1469b50d902SRodney W. Grimes /* send the name followed by <CR><LF> */ 1474e030ba6SMark Murray iov[msg.msg_iovlen].iov_base = strdup(name); 1481e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = strlen(name); 1494e030ba6SMark Murray iov[msg.msg_iovlen].iov_base = neteol; 1501e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 2; 1511e474c62SGarrett Wollman 152e45ccde7SGarrett Wollman /* 153e45ccde7SGarrett Wollman * -T disables data-on-SYN: compatibility option to finger broken 154e45ccde7SGarrett Wollman * hosts. Also, the implicit-open API is broken on IPv6, so do 155e45ccde7SGarrett Wollman * the explicit connect there, too. 156e45ccde7SGarrett Wollman */ 157e45ccde7SGarrett Wollman if ((Tflag || ai->ai_addr->sa_family == AF_INET6) 158e45ccde7SGarrett Wollman && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 159e45ccde7SGarrett Wollman warn("connect"); 160e45ccde7SGarrett Wollman close(s); 161e45ccde7SGarrett Wollman return -1; 16203801815SAndras Olah } 16303801815SAndras Olah 164a32cbefaSGarrett Wollman if (sendmsg(s, &msg, 0) < 0) { 165e45ccde7SGarrett Wollman warn("sendmsg"); 1661e474c62SGarrett Wollman close(s); 167e45ccde7SGarrett Wollman return -1; 1681e474c62SGarrett Wollman } 1699b50d902SRodney W. Grimes 1709b50d902SRodney W. Grimes /* 1719b50d902SRodney W. Grimes * Read from the remote system; once we're connected, we assume some 1729b50d902SRodney W. Grimes * data. If none arrives, we hang until the user interrupts. 1739b50d902SRodney W. Grimes * 1749b50d902SRodney W. Grimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1759b50d902SRodney W. Grimes * a newline; if followed by a newline character, only output one 1769b50d902SRodney W. Grimes * newline. 1779b50d902SRodney W. Grimes * 1789b50d902SRodney W. Grimes * Otherwise, all high bits are stripped; if it isn't printable and 1799b50d902SRodney W. Grimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1809b50d902SRodney W. Grimes * character with bit 7 set is printable. 1819b50d902SRodney W. Grimes */ 182df3f5d9dSPeter Wemm lastc = 0; 183df3f5d9dSPeter Wemm if ((fp = fdopen(s, "r")) != NULL) { 184dd5288f3SDavid E. O'Brien cnt = 0; 185dd5288f3SDavid E. O'Brien line_len = 0; 1869b50d902SRodney W. Grimes while ((c = getc(fp)) != EOF) { 187dd5288f3SDavid E. O'Brien if (++cnt > OUTPUT_MAX) { 188dd5288f3SDavid E. O'Brien printf("\n\n Output truncated at %d bytes...\n", 189dd5288f3SDavid E. O'Brien cnt - 1); 190dd5288f3SDavid E. O'Brien break; 191dd5288f3SDavid E. O'Brien } 1929b50d902SRodney W. Grimes if (c == 0x0d) { 1939b50d902SRodney W. Grimes if (lastc == '\r') /* ^M^M - skip dupes */ 1949b50d902SRodney W. Grimes continue; 1959b50d902SRodney W. Grimes c = '\n'; 1969b50d902SRodney W. Grimes lastc = '\r'; 1979b50d902SRodney W. Grimes } else { 198a0a47889SAndrey A. Chernov if (!isprint(c) && !isspace(c)) { 199a0a47889SAndrey A. Chernov c &= 0x7f; 2009b50d902SRodney W. Grimes c |= 0x40; 201a0a47889SAndrey A. Chernov } 2029b50d902SRodney W. Grimes if (lastc != '\r' || c != '\n') 2039b50d902SRodney W. Grimes lastc = c; 2049b50d902SRodney W. Grimes else { 2059b50d902SRodney W. Grimes lastc = '\n'; 2069b50d902SRodney W. Grimes continue; 2079b50d902SRodney W. Grimes } 2089b50d902SRodney W. Grimes } 2099b50d902SRodney W. Grimes putchar(c); 210dd5288f3SDavid E. O'Brien if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) { 211dd5288f3SDavid E. O'Brien putchar('\\'); 212dd5288f3SDavid E. O'Brien putchar('\n'); 213dd5288f3SDavid E. O'Brien lastc = '\r'; 214dd5288f3SDavid E. O'Brien } 215dd5288f3SDavid E. O'Brien if (lastc == '\n' || lastc == '\r') 216dd5288f3SDavid E. O'Brien line_len = 0; 2179b50d902SRodney W. Grimes } 2181e474c62SGarrett Wollman if (ferror(fp)) { 2191e474c62SGarrett Wollman /* 2201e474c62SGarrett Wollman * Assume that whatever it was set errno... 2211e474c62SGarrett Wollman */ 222e45ccde7SGarrett Wollman warn("reading from network"); 2231e474c62SGarrett Wollman } 224e45ccde7SGarrett Wollman if (lastc != '\n') 225e45ccde7SGarrett Wollman putchar('\n'); 226e45ccde7SGarrett Wollman 227e45ccde7SGarrett Wollman fclose(fp); 2289b50d902SRodney W. Grimes } 229e45ccde7SGarrett Wollman return 0; 2301e474c62SGarrett Wollman } 231e45ccde7SGarrett Wollman 232e45ccde7SGarrett Wollman static void 233e45ccde7SGarrett Wollman trying(const struct addrinfo *ai) 234e45ccde7SGarrett Wollman { 235e45ccde7SGarrett Wollman char buf[NI_MAXHOST]; 236e45ccde7SGarrett Wollman 237e45ccde7SGarrett Wollman if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf, 238e45ccde7SGarrett Wollman (char *)0, 0, NI_NUMERICHOST) != 0) 239e45ccde7SGarrett Wollman return; /* XXX can't happen */ 240e45ccde7SGarrett Wollman 241e45ccde7SGarrett Wollman printf("Trying %s...\n", buf); 242e45ccde7SGarrett Wollman } 243e45ccde7SGarrett Wollman 244e45ccde7SGarrett Wollman void 2454e030ba6SMark Murray cleanup(int sig __unused) 246e45ccde7SGarrett Wollman { 247e45ccde7SGarrett Wollman #define ERRSTR "Timed out.\n" 248e45ccde7SGarrett Wollman write(STDERR_FILENO, ERRSTR, sizeof ERRSTR); 249e45ccde7SGarrett Wollman exit(1); 250e45ccde7SGarrett Wollman } 251e45ccde7SGarrett Wollman 252