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[] = 42c3aac50fSPeter Wemm "$FreeBSD$"; 43b14d8277SPhilippe Charnier #endif 449b50d902SRodney W. Grimes #endif /* not lint */ 459b50d902SRodney W. Grimes 469b50d902SRodney W. Grimes #include <sys/types.h> 47a716ad66SWarner Losh #include <sys/param.h> 489b50d902SRodney W. Grimes #include <sys/socket.h> 499b50d902SRodney W. Grimes #include <netinet/in.h> 509b50d902SRodney W. Grimes #include <arpa/inet.h> 519b50d902SRodney W. Grimes #include <netdb.h> 529b50d902SRodney W. Grimes #include <db.h> 53b14d8277SPhilippe Charnier #include <err.h> 549b50d902SRodney W. Grimes #include <unistd.h> 559b50d902SRodney W. Grimes #include <pwd.h> 569b50d902SRodney W. Grimes #include <utmp.h> 579b50d902SRodney W. Grimes #include <stdio.h> 589b50d902SRodney W. Grimes #include <ctype.h> 599b50d902SRodney W. Grimes #include <string.h> 601e474c62SGarrett Wollman #include <sys/uio.h> 61dd5288f3SDavid E. O'Brien #include <signal.h> 62dd5288f3SDavid E. O'Brien #include <limits.h> 639b50d902SRodney W. Grimes #include "finger.h" 649b50d902SRodney W. Grimes 65e45ccde7SGarrett Wollman extern int lflag; /* XXX finger.h? */ 66e45ccde7SGarrett Wollman extern int Tflag; /* XXX finger.h? */ 67e45ccde7SGarrett Wollman 68e45ccde7SGarrett Wollman static void cleanup(int sig);; 69e45ccde7SGarrett Wollman static int do_protocol(const char *name, const struct addrinfo *ai); 70e45ccde7SGarrett Wollman static void trying(const struct addrinfo *ai); 71dd5288f3SDavid E. O'Brien 72dd5288f3SDavid E. O'Brien void 739b50d902SRodney W. Grimes netfinger(name) 749b50d902SRodney W. Grimes char *name; 759b50d902SRodney W. Grimes { 76e45ccde7SGarrett Wollman int error, multi; 77e45ccde7SGarrett Wollman char *host; 78e45ccde7SGarrett Wollman struct addrinfo *ai, *ai0; 79e45ccde7SGarrett Wollman static struct addrinfo hint; 809b50d902SRodney W. Grimes 81e45ccde7SGarrett Wollman host = strrchr(name, '@'); 82e45ccde7SGarrett Wollman if (host == 0) 839b50d902SRodney W. Grimes return; 84b14d8277SPhilippe Charnier *host++ = '\0'; 85dd5288f3SDavid E. O'Brien signal(SIGALRM, cleanup); 86e45ccde7SGarrett Wollman alarm(TIME_LIMIT); 87e45ccde7SGarrett Wollman 88e45ccde7SGarrett Wollman hint.ai_flags = AI_CANONNAME; 89e45ccde7SGarrett Wollman hint.ai_family = PF_UNSPEC; 90e45ccde7SGarrett Wollman hint.ai_socktype = SOCK_STREAM; 91e45ccde7SGarrett Wollman 92e45ccde7SGarrett Wollman error = getaddrinfo(host, "finger", &hint, &ai0); 93e45ccde7SGarrett Wollman if (error) { 94e45ccde7SGarrett Wollman warnx("%s: %s", host, gai_strerror(error)); 959b50d902SRodney W. Grimes return; 969b50d902SRodney W. Grimes } 979b50d902SRodney W. Grimes 98e45ccde7SGarrett Wollman multi = (ai0->ai_next) != 0; 992804330fSGarrett Wollman 1002804330fSGarrett Wollman /* ai_canonname may not be filled in if the user specified an IP. */ 1012804330fSGarrett Wollman if (ai0->ai_canonname == 0) 1022804330fSGarrett Wollman printf("[%s]\n", host); 1032804330fSGarrett Wollman else 104e45ccde7SGarrett Wollman printf("[%s]\n", ai0->ai_canonname); 1051e474c62SGarrett Wollman 106e45ccde7SGarrett Wollman for (ai = ai0; ai != 0; ai = ai->ai_next) { 107e45ccde7SGarrett Wollman if (multi) 108e45ccde7SGarrett Wollman trying(ai); 109e45ccde7SGarrett Wollman 110e45ccde7SGarrett Wollman error = do_protocol(name, ai); 111e45ccde7SGarrett Wollman if (!error) 112e45ccde7SGarrett Wollman break; 113e45ccde7SGarrett Wollman } 114e45ccde7SGarrett Wollman alarm(0); 115e45ccde7SGarrett Wollman freeaddrinfo(ai0); 116e45ccde7SGarrett Wollman } 117e45ccde7SGarrett Wollman 118e45ccde7SGarrett Wollman static int 119e45ccde7SGarrett Wollman do_protocol(const char *name, const struct addrinfo *ai) 120e45ccde7SGarrett Wollman { 1211e832bf8SRuslan Ermilov int cnt, line_len, s; 122e45ccde7SGarrett Wollman register FILE *fp; 123e45ccde7SGarrett Wollman register int c, lastc; 124e45ccde7SGarrett Wollman struct iovec iov[3]; 125e45ccde7SGarrett Wollman struct msghdr msg; 126e45ccde7SGarrett Wollman 127e45ccde7SGarrett Wollman s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 128e45ccde7SGarrett Wollman if (s < 0) { 129e45ccde7SGarrett Wollman warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype, 130e45ccde7SGarrett Wollman ai->ai_protocol); 131e45ccde7SGarrett Wollman return -1; 132e45ccde7SGarrett Wollman } 133e45ccde7SGarrett Wollman 134e45ccde7SGarrett Wollman msg.msg_name = (void *)ai->ai_addr; 135e45ccde7SGarrett Wollman msg.msg_namelen = ai->ai_addrlen; 1361e474c62SGarrett Wollman msg.msg_iov = iov; 1371e474c62SGarrett Wollman msg.msg_iovlen = 0; 1381e474c62SGarrett Wollman msg.msg_control = 0; 1391e474c62SGarrett Wollman msg.msg_controllen = 0; 140a32cbefaSGarrett Wollman msg.msg_flags = 0; 1419b50d902SRodney W. Grimes 1429b50d902SRodney W. Grimes /* -l flag for remote fingerd */ 1431e474c62SGarrett Wollman if (lflag) { 1441e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "/W "; 1451e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 3; 1461e474c62SGarrett Wollman } 1479b50d902SRodney W. Grimes /* send the name followed by <CR><LF> */ 1481e832bf8SRuslan Ermilov iov[msg.msg_iovlen].iov_base = (char *)name; 1491e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = strlen(name); 1501e474c62SGarrett Wollman iov[msg.msg_iovlen].iov_base = "\r\n"; 1511e474c62SGarrett Wollman iov[msg.msg_iovlen++].iov_len = 2; 1521e474c62SGarrett Wollman 153e45ccde7SGarrett Wollman /* 154e45ccde7SGarrett Wollman * -T disables data-on-SYN: compatibility option to finger broken 155e45ccde7SGarrett Wollman * hosts. Also, the implicit-open API is broken on IPv6, so do 156e45ccde7SGarrett Wollman * the explicit connect there, too. 157e45ccde7SGarrett Wollman */ 158e45ccde7SGarrett Wollman if ((Tflag || ai->ai_addr->sa_family == AF_INET6) 159e45ccde7SGarrett Wollman && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 160e45ccde7SGarrett Wollman warn("connect"); 161e45ccde7SGarrett Wollman close(s); 162e45ccde7SGarrett Wollman return -1; 16303801815SAndras Olah } 16403801815SAndras Olah 165a32cbefaSGarrett Wollman if (sendmsg(s, &msg, 0) < 0) { 166e45ccde7SGarrett Wollman warn("sendmsg"); 1671e474c62SGarrett Wollman close(s); 168e45ccde7SGarrett Wollman return -1; 1691e474c62SGarrett Wollman } 1709b50d902SRodney W. Grimes 1719b50d902SRodney W. Grimes /* 1729b50d902SRodney W. Grimes * Read from the remote system; once we're connected, we assume some 1739b50d902SRodney W. Grimes * data. If none arrives, we hang until the user interrupts. 1749b50d902SRodney W. Grimes * 1759b50d902SRodney W. Grimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1769b50d902SRodney W. Grimes * a newline; if followed by a newline character, only output one 1779b50d902SRodney W. Grimes * newline. 1789b50d902SRodney W. Grimes * 1799b50d902SRodney W. Grimes * Otherwise, all high bits are stripped; if it isn't printable and 1809b50d902SRodney W. Grimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1819b50d902SRodney W. Grimes * character with bit 7 set is printable. 1829b50d902SRodney W. Grimes */ 183df3f5d9dSPeter Wemm lastc = 0; 184df3f5d9dSPeter Wemm if ((fp = fdopen(s, "r")) != NULL) { 185dd5288f3SDavid E. O'Brien cnt = 0; 186dd5288f3SDavid E. O'Brien line_len = 0; 1879b50d902SRodney W. Grimes while ((c = getc(fp)) != EOF) { 188dd5288f3SDavid E. O'Brien if (++cnt > OUTPUT_MAX) { 189dd5288f3SDavid E. O'Brien printf("\n\n Output truncated at %d bytes...\n", 190dd5288f3SDavid E. O'Brien cnt - 1); 191dd5288f3SDavid E. O'Brien break; 192dd5288f3SDavid E. O'Brien } 1939b50d902SRodney W. Grimes if (c == 0x0d) { 1949b50d902SRodney W. Grimes if (lastc == '\r') /* ^M^M - skip dupes */ 1959b50d902SRodney W. Grimes continue; 1969b50d902SRodney W. Grimes c = '\n'; 1979b50d902SRodney W. Grimes lastc = '\r'; 1989b50d902SRodney W. Grimes } else { 199a0a47889SAndrey A. Chernov if (!isprint(c) && !isspace(c)) { 200a0a47889SAndrey A. Chernov c &= 0x7f; 2019b50d902SRodney W. Grimes c |= 0x40; 202a0a47889SAndrey A. Chernov } 2039b50d902SRodney W. Grimes if (lastc != '\r' || c != '\n') 2049b50d902SRodney W. Grimes lastc = c; 2059b50d902SRodney W. Grimes else { 2069b50d902SRodney W. Grimes lastc = '\n'; 2079b50d902SRodney W. Grimes continue; 2089b50d902SRodney W. Grimes } 2099b50d902SRodney W. Grimes } 2109b50d902SRodney W. Grimes putchar(c); 211dd5288f3SDavid E. O'Brien if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) { 212dd5288f3SDavid E. O'Brien putchar('\\'); 213dd5288f3SDavid E. O'Brien putchar('\n'); 214dd5288f3SDavid E. O'Brien lastc = '\r'; 215dd5288f3SDavid E. O'Brien } 216dd5288f3SDavid E. O'Brien if (lastc == '\n' || lastc == '\r') 217dd5288f3SDavid E. O'Brien line_len = 0; 2189b50d902SRodney W. Grimes } 2191e474c62SGarrett Wollman if (ferror(fp)) { 2201e474c62SGarrett Wollman /* 2211e474c62SGarrett Wollman * Assume that whatever it was set errno... 2221e474c62SGarrett Wollman */ 223e45ccde7SGarrett Wollman warn("reading from network"); 2241e474c62SGarrett Wollman } 225e45ccde7SGarrett Wollman if (lastc != '\n') 226e45ccde7SGarrett Wollman putchar('\n'); 227e45ccde7SGarrett Wollman 228e45ccde7SGarrett Wollman fclose(fp); 2299b50d902SRodney W. Grimes } 230e45ccde7SGarrett Wollman return 0; 2311e474c62SGarrett Wollman } 232e45ccde7SGarrett Wollman 233e45ccde7SGarrett Wollman static void 234e45ccde7SGarrett Wollman trying(const struct addrinfo *ai) 235e45ccde7SGarrett Wollman { 236e45ccde7SGarrett Wollman char buf[NI_MAXHOST]; 237e45ccde7SGarrett Wollman 238e45ccde7SGarrett Wollman if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf, 239e45ccde7SGarrett Wollman (char *)0, 0, NI_NUMERICHOST) != 0) 240e45ccde7SGarrett Wollman return; /* XXX can't happen */ 241e45ccde7SGarrett Wollman 242e45ccde7SGarrett Wollman printf("Trying %s...\n", buf); 243e45ccde7SGarrett Wollman } 244e45ccde7SGarrett Wollman 245e45ccde7SGarrett Wollman void 246e45ccde7SGarrett Wollman cleanup(int sig) 247e45ccde7SGarrett Wollman { 248e45ccde7SGarrett Wollman #define ERRSTR "Timed out.\n" 249e45ccde7SGarrett Wollman write(STDERR_FILENO, ERRSTR, sizeof ERRSTR); 250e45ccde7SGarrett Wollman exit(1); 251e45ccde7SGarrett Wollman } 252e45ccde7SGarrett Wollman 253