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