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 #if 0 36 #ifndef lint 37 static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95"; 38 #endif 39 #endif 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/socket.h> 46 #include <sys/uio.h> 47 #include <wctype.h> 48 #include <db.h> 49 #include <err.h> 50 #include <netdb.h> 51 #include <pwd.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 #include <utmpx.h> 57 #include <wchar.h> 58 #include "finger.h" 59 60 static void cleanup(int sig); 61 static int do_protocol(const char *name, const struct addrinfo *ai); 62 static void trying(const struct addrinfo *ai); 63 64 void 65 netfinger(char *name) 66 { 67 int error, multi; 68 char *host; 69 struct addrinfo *ai, *ai0; 70 static struct addrinfo hint; 71 72 host = strrchr(name, '@'); 73 if (host == NULL) 74 return; 75 *host++ = '\0'; 76 signal(SIGALRM, cleanup); 77 alarm(TIME_LIMIT); 78 79 hint.ai_flags = AI_CANONNAME; 80 hint.ai_family = family; 81 hint.ai_socktype = SOCK_STREAM; 82 83 error = getaddrinfo(host, "finger", &hint, &ai0); 84 if (error) { 85 warnx("%s: %s", host, gai_strerror(error)); 86 return; 87 } 88 89 multi = (ai0->ai_next) != 0; 90 91 /* ai_canonname may not be filled in if the user specified an IP. */ 92 if (ai0->ai_canonname == 0) 93 printf("[%s]\n", host); 94 else 95 printf("[%s]\n", ai0->ai_canonname); 96 97 for (ai = ai0; ai != NULL; ai = ai->ai_next) { 98 if (multi) 99 trying(ai); 100 101 error = do_protocol(name, ai); 102 if (!error) 103 break; 104 } 105 alarm(0); 106 freeaddrinfo(ai0); 107 } 108 109 static int 110 do_protocol(const char *name, const struct addrinfo *ai) 111 { 112 int cnt, line_len, s; 113 FILE *fp; 114 wint_t c, lastc; 115 struct iovec iov[3]; 116 struct msghdr msg; 117 static char slash_w[] = "/W "; 118 static char neteol[] = "\r\n"; 119 120 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 121 if (s < 0) { 122 warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype, 123 ai->ai_protocol); 124 return -1; 125 } 126 127 msg.msg_name = (void *)ai->ai_addr; 128 msg.msg_namelen = ai->ai_addrlen; 129 msg.msg_iov = iov; 130 msg.msg_iovlen = 0; 131 msg.msg_control = 0; 132 msg.msg_controllen = 0; 133 msg.msg_flags = 0; 134 135 /* -l flag for remote fingerd */ 136 if (lflag) { 137 iov[msg.msg_iovlen].iov_base = slash_w; 138 iov[msg.msg_iovlen++].iov_len = 3; 139 } 140 /* send the name followed by <CR><LF> */ 141 iov[msg.msg_iovlen].iov_base = strdup(name); 142 iov[msg.msg_iovlen++].iov_len = strlen(name); 143 iov[msg.msg_iovlen].iov_base = neteol; 144 iov[msg.msg_iovlen++].iov_len = 2; 145 146 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 147 warn("connect"); 148 close(s); 149 return -1; 150 } 151 152 if (sendmsg(s, &msg, 0) < 0) { 153 warn("sendmsg"); 154 close(s); 155 return -1; 156 } 157 158 /* 159 * Read from the remote system; once we're connected, we assume some 160 * data. If none arrives, we hang until the user interrupts. 161 * 162 * If we see a <CR> or a <CR> with the high bit set, treat it as 163 * a newline; if followed by a newline character, only output one 164 * newline. 165 * 166 * Otherwise, all high bits are stripped; if it isn't printable and 167 * it isn't a space, we can simply set the 7th bit. Every ASCII 168 * character with bit 7 set is printable. 169 */ 170 lastc = 0; 171 if ((fp = fdopen(s, "r")) != NULL) { 172 cnt = 0; 173 line_len = 0; 174 while ((c = getwc(fp)) != EOF) { 175 if (++cnt > OUTPUT_MAX) { 176 printf("\n\n Output truncated at %d bytes...\n", 177 cnt - 1); 178 break; 179 } 180 if (c == 0x0d) { 181 if (lastc == '\r') /* ^M^M - skip dupes */ 182 continue; 183 c = '\n'; 184 lastc = '\r'; 185 } else { 186 if (!iswprint(c) && !iswspace(c)) { 187 c &= 0x7f; 188 c |= 0x40; 189 } 190 if (lastc != '\r' || c != '\n') 191 lastc = c; 192 else { 193 lastc = '\n'; 194 continue; 195 } 196 } 197 putwchar(c); 198 if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) { 199 putchar('\\'); 200 putchar('\n'); 201 lastc = '\r'; 202 } 203 if (lastc == '\n' || lastc == '\r') 204 line_len = 0; 205 } 206 if (ferror(fp)) { 207 /* 208 * Assume that whatever it was set errno... 209 */ 210 warn("reading from network"); 211 } 212 if (lastc != L'\n') 213 putchar('\n'); 214 215 fclose(fp); 216 } 217 return 0; 218 } 219 220 static void 221 trying(const struct addrinfo *ai) 222 { 223 char buf[NI_MAXHOST]; 224 225 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf, 226 (char *)0, 0, NI_NUMERICHOST) != 0) 227 return; /* XXX can't happen */ 228 229 printf("Trying %s...\n", buf); 230 } 231 232 static void 233 cleanup(int sig __unused) 234 { 235 #define ERRSTR "Timed out.\n" 236 write(STDERR_FILENO, ERRSTR, sizeof ERRSTR); 237 exit(1); 238 } 239 240