1 /* $NetBSD: net.c,v 1.14 2000/07/07 15:13:22 itojun Exp $ */ 2 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95"; 43 #else 44 __RCSID("$NetBSD: net.c,v 1.14 2000/07/07 15:13:22 itojun Exp $"); 45 #endif 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/socket.h> 50 51 #include <netinet/in.h> 52 53 #include <arpa/inet.h> 54 55 #include <netdb.h> 56 #include <db.h> 57 #include <unistd.h> 58 #include <pwd.h> 59 #include <utmp.h> 60 #include <stdio.h> 61 #include <string.h> 62 #include <ctype.h> 63 #include <unistd.h> 64 #include <err.h> 65 66 #include "finger.h" 67 #include "extern.h" 68 69 void 70 netfinger(name) 71 char *name; 72 { 73 FILE *fp; 74 int c, lastc; 75 int s; 76 char *host; 77 struct addrinfo hints, *res, *res0; 78 int error; 79 char *emsg = NULL; 80 81 lastc = 0; 82 if (!(host = strrchr(name, '@'))) 83 return; 84 *host++ = '\0'; 85 memset(&hints, 0, sizeof(hints)); 86 hints.ai_family = PF_UNSPEC; 87 hints.ai_socktype = SOCK_STREAM; 88 hints.ai_flags = AI_CANONNAME; 89 error = getaddrinfo(host, "finger", &hints, &res0); 90 if (error) { 91 warnx("%s: %s", gai_strerror(error), host); 92 return; 93 } 94 95 s = -1; 96 for (res = res0; res; res = res->ai_next) { 97 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 98 if (s < 0) { 99 emsg = "socket"; 100 continue; 101 } 102 103 if (connect(s, res->ai_addr, res->ai_addrlen)) { 104 close(s); 105 s = -1; 106 emsg = "connect"; 107 continue; 108 } 109 110 break; 111 } 112 if (s < 0) { 113 if (emsg != NULL) 114 warn("%s", emsg); 115 return; 116 } 117 118 /* have network connection; identify the host connected with */ 119 (void)printf("[%s]\n", res0->ai_canonname ? res0->ai_canonname : host); 120 121 /* -l flag for remote fingerd */ 122 if (lflag) 123 write(s, "/W ", 3); 124 /* send the name followed by <CR><LF> */ 125 (void)write(s, name, strlen(name)); 126 (void)write(s, "\r\n", 2); 127 128 /* 129 * Read from the remote system; once we're connected, we assume some 130 * data. If none arrives, we hang until the user interrupts. 131 * 132 * If we see a <CR> or a <CR> with the high bit set, treat it as 133 * a newline; if followed by a newline character, only output one 134 * newline. 135 * 136 * Otherwise, all high bits are stripped; if it isn't printable and 137 * it isn't a space, we can simply set the 7th bit. Every ASCII 138 * character with bit 7 set is printable. 139 */ 140 if ((fp = fdopen(s, "r")) != NULL) 141 while ((c = getc(fp)) != EOF) { 142 c &= 0x7f; 143 if (c == '\r') { 144 if (lastc == '\r') /* ^M^M - skip dupes */ 145 continue; 146 c = '\n'; 147 lastc = '\r'; 148 } else { 149 if (!isprint(c) && !isspace(c)) 150 c |= 0x40; 151 if (lastc != '\r' || c != '\n') 152 lastc = c; 153 else { 154 lastc = '\n'; 155 continue; 156 } 157 } 158 putchar(c); 159 } 160 if (lastc != '\n') 161 putchar('\n'); 162 (void)fclose(fp); 163 } 164