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"; 404c86f3adSPhilippe Charnier #endif 41b14d8277SPhilippe Charnier static const char rcsid[] = 42c3aac50fSPeter Wemm "$FreeBSD$"; 439b50d902SRodney W. Grimes #endif /* not lint */ 449b50d902SRodney W. Grimes 45a716ad66SWarner Losh #include <sys/param.h> 469b50d902SRodney W. Grimes #include <sys/socket.h> 474c86f3adSPhilippe Charnier #include <sys/uio.h> 484c86f3adSPhilippe Charnier #include <ctype.h> 499b50d902SRodney W. Grimes #include <db.h> 50b14d8277SPhilippe Charnier #include <err.h> 514c86f3adSPhilippe Charnier #include <netdb.h> 529b50d902SRodney W. Grimes #include <pwd.h> 539b50d902SRodney W. Grimes #include <stdio.h> 549b50d902SRodney W. Grimes #include <string.h> 554c86f3adSPhilippe Charnier #include <unistd.h> 564c86f3adSPhilippe Charnier #include <utmp.h> 579b50d902SRodney W. Grimes #include "finger.h" 589b50d902SRodney W. Grimes 59e45ccde7SGarrett Wollman extern int lflag; /* XXX finger.h? */ 60e45ccde7SGarrett Wollman extern int Tflag; /* XXX finger.h? */ 61e45ccde7SGarrett Wollman 62e45ccde7SGarrett Wollman static void cleanup(int sig);; 63e45ccde7SGarrett Wollman static int do_protocol(const char *name, const struct addrinfo *ai); 64e45ccde7SGarrett Wollman static void trying(const struct addrinfo *ai); 65dd5288f3SDavid E. O'Brien 66dd5288f3SDavid E. O'Brien void 679b50d902SRodney W. Grimes netfinger(name) 689b50d902SRodney W. Grimes char *name; 699b50d902SRodney W. Grimes { 70e45ccde7SGarrett Wollman int error, multi; 71e45ccde7SGarrett Wollman char *host; 72e45ccde7SGarrett Wollman struct addrinfo *ai, *ai0; 73e45ccde7SGarrett Wollman static struct addrinfo hint; 749b50d902SRodney W. Grimes 75e45ccde7SGarrett Wollman host = strrchr(name, '@'); 76e45ccde7SGarrett Wollman if (host == 0) 779b50d902SRodney W. Grimes return; 78b14d8277SPhilippe Charnier *host++ = '\0'; 79dd5288f3SDavid E. O'Brien signal(SIGALRM, cleanup); 80e45ccde7SGarrett Wollman alarm(TIME_LIMIT); 81e45ccde7SGarrett Wollman 82e45ccde7SGarrett Wollman hint.ai_flags = AI_CANONNAME; 83e45ccde7SGarrett Wollman hint.ai_family = PF_UNSPEC; 84e45ccde7SGarrett Wollman hint.ai_socktype = SOCK_STREAM; 85e45ccde7SGarrett Wollman 86e45ccde7SGarrett Wollman error = getaddrinfo(host, "finger", &hint, &ai0); 87e45ccde7SGarrett Wollman if (error) { 88e45ccde7SGarrett Wollman warnx("%s: %s", host, gai_strerror(error)); 899b50d902SRodney W. Grimes return; 909b50d902SRodney W. Grimes } 919b50d902SRodney W. Grimes 92e45ccde7SGarrett Wollman multi = (ai0->ai_next) != 0; 932804330fSGarrett Wollman 942804330fSGarrett Wollman /* ai_canonname may not be filled in if the user specified an IP. */ 952804330fSGarrett Wollman if (ai0->ai_canonname == 0) 962804330fSGarrett Wollman printf("[%s]\n", host); 972804330fSGarrett Wollman else 98e45ccde7SGarrett Wollman printf("[%s]\n", ai0->ai_canonname); 991e474c62SGarrett Wollman 100e45ccde7SGarrett Wollman for (ai = ai0; ai != 0; ai = ai->ai_next) { 101e45ccde7SGarrett Wollman if (multi) 102e45ccde7SGarrett Wollman trying(ai); 103e45ccde7SGarrett Wollman 104e45ccde7SGarrett Wollman error = do_protocol(name, ai); 105e45ccde7SGarrett Wollman if (!error) 106e45ccde7SGarrett Wollman break; 107e45ccde7SGarrett Wollman } 108e45ccde7SGarrett Wollman alarm(0); 109e45ccde7SGarrett Wollman freeaddrinfo(ai0); 110e45ccde7SGarrett Wollman } 111e45ccde7SGarrett Wollman 112e45ccde7SGarrett Wollman static int 113e45ccde7SGarrett Wollman do_protocol(const char *name, const struct addrinfo *ai) 114e45ccde7SGarrett Wollman { 1151e832bf8SRuslan Ermilov int cnt, line_len, s; 116e45ccde7SGarrett Wollman register FILE *fp; 117e45ccde7SGarrett Wollman register int c, lastc; 118e45ccde7SGarrett Wollman struct iovec iov[3]; 119e45ccde7SGarrett Wollman struct msghdr msg; 120e45ccde7SGarrett Wollman 121e45ccde7SGarrett Wollman s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 122e45ccde7SGarrett Wollman if (s < 0) { 123e45ccde7SGarrett Wollman warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype, 124e45ccde7SGarrett Wollman ai->ai_protocol); 125e45ccde7SGarrett Wollman return -1; 126e45ccde7SGarrett Wollman } 127e45ccde7SGarrett Wollman 128e45ccde7SGarrett Wollman msg.msg_name = (void *)ai->ai_addr; 129e45ccde7SGarrett Wollman msg.msg_namelen = ai->ai_addrlen; 1301e474c62SGarrett Wollman msg.msg_iov = iov; 1311e474c62SGarrett Wollman msg.msg_iovlen = 0; 1321e474c62SGarrett Wollman msg.msg_control = 0; 1331e474c62SGarrett Wollman msg.msg_controllen = 0; 134a32cbefaSGarrett Wollman msg.msg_flags = 0; 1359b50d902SRodney W. Grimes 1369b50d902SRodney W. Grimes /* -l flag for remote fingerd */ 1371e474c62SGarrett Wollman if (lflag) { 1381e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "/W "; 1391e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 3; 1401e474c62SGarrett Wollman } 1419b50d902SRodney W. Grimes /* send the name followed by <CR><LF> */ 1421e832bf8SRuslan Ermilov iov[msg.msg_iovlen].iov_base = (char *)name; 1431e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = strlen(name); 1441e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "\r\n"; 1451e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 2; 1461e474c62SGarrett Wollman 147e45ccde7SGarrett Wollman /* 148e45ccde7SGarrett Wollman * -T disables data-on-SYN: compatibility option to finger broken 149e45ccde7SGarrett Wollman * hosts. Also, the implicit-open API is broken on IPv6, so do 150e45ccde7SGarrett Wollman * the explicit connect there, too. 151e45ccde7SGarrett Wollman */ 152e45ccde7SGarrett Wollman if ((Tflag || ai->ai_addr->sa_family == AF_INET6) 153e45ccde7SGarrett Wollman && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 154e45ccde7SGarrett Wollman warn("connect"); 155e45ccde7SGarrett Wollman close(s); 156e45ccde7SGarrett Wollman return -1; 15703801815SAndras Olah } 15803801815SAndras Olah 159a32cbefaSGarrett Wollman if (sendmsg(s, &msg, 0) < 0) { 160e45ccde7SGarrett Wollman warn("sendmsg"); 1611e474c62SGarrett Wollman close(s); 162e45ccde7SGarrett Wollman return -1; 1631e474c62SGarrett Wollman } 1649b50d902SRodney W. Grimes 1659b50d902SRodney W. Grimes /* 1669b50d902SRodney W. Grimes * Read from the remote system; once we're connected, we assume some 1679b50d902SRodney W. Grimes * data. If none arrives, we hang until the user interrupts. 1689b50d902SRodney W. Grimes * 1699b50d902SRodney W. Grimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1709b50d902SRodney W. Grimes * a newline; if followed by a newline character, only output one 1719b50d902SRodney W. Grimes * newline. 1729b50d902SRodney W. Grimes * 1739b50d902SRodney W. Grimes * Otherwise, all high bits are stripped; if it isn't printable and 1749b50d902SRodney W. Grimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1759b50d902SRodney W. Grimes * character with bit 7 set is printable. 1769b50d902SRodney W. Grimes */ 177df3f5d9dSPeter Wemm lastc = 0; 178df3f5d9dSPeter Wemm if ((fp = fdopen(s, "r")) != NULL) { 179dd5288f3SDavid E. O'Brien cnt = 0; 180dd5288f3SDavid E. O'Brien line_len = 0; 1819b50d902SRodney W. Grimes while ((c = getc(fp)) != EOF) { 182dd5288f3SDavid E. O'Brien if (++cnt > OUTPUT_MAX) { 183dd5288f3SDavid E. O'Brien printf("\n\n Output truncated at %d bytes...\n", 184dd5288f3SDavid E. O'Brien cnt - 1); 185dd5288f3SDavid E. O'Brien break; 186dd5288f3SDavid E. O'Brien } 1879b50d902SRodney W. Grimes if (c == 0x0d) { 1889b50d902SRodney W. Grimes if (lastc == '\r') /* ^M^M - skip dupes */ 1899b50d902SRodney W. Grimes continue; 1909b50d902SRodney W. Grimes c = '\n'; 1919b50d902SRodney W. Grimes lastc = '\r'; 1929b50d902SRodney W. Grimes } else { 193a0a47889SAndrey A. Chernov if (!isprint(c) && !isspace(c)) { 194a0a47889SAndrey A. Chernov c &= 0x7f; 1959b50d902SRodney W. Grimes c |= 0x40; 196a0a47889SAndrey A. Chernov } 1979b50d902SRodney W. Grimes if (lastc != '\r' || c != '\n') 1989b50d902SRodney W. Grimes lastc = c; 1999b50d902SRodney W. Grimes else { 2009b50d902SRodney W. Grimes lastc = '\n'; 2019b50d902SRodney W. Grimes continue; 2029b50d902SRodney W. Grimes } 2039b50d902SRodney W. Grimes } 2049b50d902SRodney W. Grimes putchar(c); 205dd5288f3SDavid E. O'Brien if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) { 206dd5288f3SDavid E. O'Brien putchar('\\'); 207dd5288f3SDavid E. O'Brien putchar('\n'); 208dd5288f3SDavid E. O'Brien lastc = '\r'; 209dd5288f3SDavid E. O'Brien } 210dd5288f3SDavid E. O'Brien if (lastc == '\n' || lastc == '\r') 211dd5288f3SDavid E. O'Brien line_len = 0; 2129b50d902SRodney W. Grimes } 2131e474c62SGarrett Wollman if (ferror(fp)) { 2141e474c62SGarrett Wollman /* 2151e474c62SGarrett Wollman * Assume that whatever it was set errno... 2161e474c62SGarrett Wollman */ 217e45ccde7SGarrett Wollman warn("reading from network"); 2181e474c62SGarrett Wollman } 219e45ccde7SGarrett Wollman if (lastc != '\n') 220e45ccde7SGarrett Wollman putchar('\n'); 221e45ccde7SGarrett Wollman 222e45ccde7SGarrett Wollman fclose(fp); 2239b50d902SRodney W. Grimes } 224e45ccde7SGarrett Wollman return 0; 2251e474c62SGarrett Wollman } 226e45ccde7SGarrett Wollman 227e45ccde7SGarrett Wollman static void 228e45ccde7SGarrett Wollman trying(const struct addrinfo *ai) 229e45ccde7SGarrett Wollman { 230e45ccde7SGarrett Wollman char buf[NI_MAXHOST]; 231e45ccde7SGarrett Wollman 232e45ccde7SGarrett Wollman if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf, 233e45ccde7SGarrett Wollman (char *)0, 0, NI_NUMERICHOST) != 0) 234e45ccde7SGarrett Wollman return; /* XXX can't happen */ 235e45ccde7SGarrett Wollman 236e45ccde7SGarrett Wollman printf("Trying %s...\n", buf); 237e45ccde7SGarrett Wollman } 238e45ccde7SGarrett Wollman 239e45ccde7SGarrett Wollman void 240e45ccde7SGarrett Wollman cleanup(int sig) 241e45ccde7SGarrett Wollman { 242e45ccde7SGarrett Wollman #define ERRSTR "Timed out.\n" 243e45ccde7SGarrett Wollman write(STDERR_FILENO, ERRSTR, sizeof ERRSTR); 244e45ccde7SGarrett Wollman exit(1); 245e45ccde7SGarrett Wollman } 246e45ccde7SGarrett Wollman 247