1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/socket.h> 37 #include <sys/uio.h> 38 #include <wctype.h> 39 #include <db.h> 40 #include <err.h> 41 #include <netdb.h> 42 #include <pwd.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <utmpx.h> 48 #include <wchar.h> 49 #include "finger.h" 50 51 static void cleanup(int sig); 52 static int do_protocol(const char *name, const struct addrinfo *ai); 53 static void trying(const struct addrinfo *ai); 54 55 void 56 netfinger(char *name) 57 { 58 int error, multi; 59 char *host; 60 struct addrinfo *ai, *ai0; 61 static struct addrinfo hint; 62 63 host = strrchr(name, '@'); 64 if (host == NULL) 65 return; 66 *host++ = '\0'; 67 signal(SIGALRM, cleanup); 68 alarm(TIME_LIMIT); 69 70 hint.ai_flags = AI_CANONNAME; 71 hint.ai_family = family; 72 hint.ai_socktype = SOCK_STREAM; 73 74 error = getaddrinfo(host, "finger", &hint, &ai0); 75 if (error) { 76 warnx("%s: %s", host, gai_strerror(error)); 77 return; 78 } 79 80 multi = (ai0->ai_next) != 0; 81 82 /* ai_canonname may not be filled in if the user specified an IP. */ 83 if (ai0->ai_canonname == 0) 84 printf("[%s]\n", host); 85 else 86 printf("[%s]\n", ai0->ai_canonname); 87 88 for (ai = ai0; ai != NULL; ai = ai->ai_next) { 89 if (multi) 90 trying(ai); 91 92 error = do_protocol(name, ai); 93 if (!error) 94 break; 95 } 96 alarm(0); 97 freeaddrinfo(ai0); 98 } 99 100 static int 101 do_protocol(const char *name, const struct addrinfo *ai) 102 { 103 int cnt, line_len, s; 104 FILE *fp; 105 wint_t c, lastc; 106 struct iovec iov[3]; 107 struct msghdr msg; 108 static char slash_w[] = "/W "; 109 static char neteol[] = "\r\n"; 110 111 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 112 if (s < 0) { 113 warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype, 114 ai->ai_protocol); 115 return -1; 116 } 117 118 msg.msg_name = (void *)ai->ai_addr; 119 msg.msg_namelen = ai->ai_addrlen; 120 msg.msg_iov = iov; 121 msg.msg_iovlen = 0; 122 msg.msg_control = 0; 123 msg.msg_controllen = 0; 124 msg.msg_flags = 0; 125 126 /* -l flag for remote fingerd */ 127 if (lflag) { 128 iov[msg.msg_iovlen].iov_base = slash_w; 129 iov[msg.msg_iovlen++].iov_len = 3; 130 } 131 /* send the name followed by <CR><LF> */ 132 iov[msg.msg_iovlen].iov_base = strdup(name); 133 iov[msg.msg_iovlen++].iov_len = strlen(name); 134 iov[msg.msg_iovlen].iov_base = neteol; 135 iov[msg.msg_iovlen++].iov_len = 2; 136 137 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 138 warn("connect"); 139 close(s); 140 return -1; 141 } 142 143 if (sendmsg(s, &msg, 0) < 0) { 144 warn("sendmsg"); 145 close(s); 146 return -1; 147 } 148 149 /* 150 * Read from the remote system; once we're connected, we assume some 151 * data. If none arrives, we hang until the user interrupts. 152 * 153 * If we see a <CR> or a <CR> with the high bit set, treat it as 154 * a newline; if followed by a newline character, only output one 155 * newline. 156 * 157 * Otherwise, all high bits are stripped; if it isn't printable and 158 * it isn't a space, we can simply set the 7th bit. Every ASCII 159 * character with bit 7 set is printable. 160 */ 161 lastc = 0; 162 if ((fp = fdopen(s, "r")) != NULL) { 163 cnt = 0; 164 line_len = 0; 165 while ((c = getwc(fp)) != EOF) { 166 if (++cnt > OUTPUT_MAX) { 167 printf("\n\n Output truncated at %d bytes...\n", 168 cnt - 1); 169 break; 170 } 171 if (c == 0x0d) { 172 if (lastc == '\r') /* ^M^M - skip dupes */ 173 continue; 174 c = '\n'; 175 lastc = '\r'; 176 } else { 177 if (!iswprint(c) && !iswspace(c)) { 178 c &= 0x7f; 179 c |= 0x40; 180 } 181 if (lastc != '\r' || c != '\n') 182 lastc = c; 183 else { 184 lastc = '\n'; 185 continue; 186 } 187 } 188 putwchar(c); 189 if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) { 190 putchar('\\'); 191 putchar('\n'); 192 lastc = '\r'; 193 } 194 if (lastc == '\n' || lastc == '\r') 195 line_len = 0; 196 } 197 if (ferror(fp)) { 198 /* 199 * Assume that whatever it was set errno... 200 */ 201 warn("reading from network"); 202 } 203 if (lastc != L'\n') 204 putchar('\n'); 205 206 fclose(fp); 207 } 208 return 0; 209 } 210 211 static void 212 trying(const struct addrinfo *ai) 213 { 214 char buf[NI_MAXHOST]; 215 216 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf, 217 (char *)0, 0, NI_NUMERICHOST) != 0) 218 return; /* XXX can't happen */ 219 220 printf("Trying %s...\n", buf); 221 } 222 223 static void 224 cleanup(int sig __unused) 225 { 226 #define ERRSTR "Timed out.\n" 227 write(STDERR_FILENO, ERRSTR, sizeof ERRSTR); 228 exit(1); 229 } 230 231