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