1 /* $NetBSD: net.c,v 1.19 2002/09/12 01:31:41 kim 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.19 2002/09/12 01:31:41 kim 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 <time.h> 57 #include <db.h> 58 #include <unistd.h> 59 #include <pwd.h> 60 #include <stdio.h> 61 #include <string.h> 62 #include <ctype.h> 63 #include <unistd.h> 64 #include <err.h> 65 66 #include "utmpentry.h" 67 68 #include "finger.h" 69 #include "extern.h" 70 71 void 72 netfinger(name) 73 char *name; 74 { 75 FILE *fp; 76 int c, lastc; 77 int s; 78 char *host; 79 struct addrinfo hints, *res, *res0; 80 int error; 81 char *emsg = NULL; 82 83 lastc = 0; 84 if (!(host = strrchr(name, '@'))) 85 return; 86 *host++ = '\0'; 87 memset(&hints, 0, sizeof(hints)); 88 hints.ai_family = PF_UNSPEC; 89 hints.ai_socktype = SOCK_STREAM; 90 hints.ai_flags = AI_CANONNAME; 91 error = getaddrinfo(host, "finger", &hints, &res0); 92 if (error) { 93 warnx("%s: %s", gai_strerror(error), host); 94 return; 95 } 96 97 s = -1; 98 for (res = res0; res; res = res->ai_next) { 99 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 100 if (s < 0) { 101 emsg = "socket"; 102 continue; 103 } 104 105 if (connect(s, res->ai_addr, res->ai_addrlen)) { 106 close(s); 107 s = -1; 108 emsg = "connect"; 109 continue; 110 } 111 112 break; 113 } 114 if (s < 0) { 115 if (emsg != NULL) 116 warn("%s", emsg); 117 return; 118 } 119 120 /* have network connection; identify the host connected with */ 121 (void)printf("[%s]\n", res0->ai_canonname ? res0->ai_canonname : host); 122 123 /* -l flag for remote fingerd */ 124 if (lflag) 125 write(s, "/W ", 3); 126 /* send the name followed by <CR><LF> */ 127 (void)write(s, name, strlen(name)); 128 (void)write(s, "\r\n", 2); 129 130 /* 131 * Read from the remote system; once we're connected, we assume some 132 * data. If none arrives, we hang until the user interrupts. 133 * 134 * If we see a <CR> followed by a newline character, only output 135 * one newline. 136 * 137 * If a character isn't printable and it isn't a space, we strip the 138 * 8th bit and set the 7th bit. Every ASCII character with bit 7 set 139 * is printable. 140 */ 141 if ((fp = fdopen(s, "r")) != NULL) 142 while ((c = getc(fp)) != EOF) { 143 if (c == '\r') { 144 if (lastc == '\r') /* ^M^M - skip dupes */ 145 continue; 146 c = '\n'; 147 lastc = '\r'; 148 } else { 149 if (!(eightflag || isprint(c) || isspace(c))) { 150 c &= 0x7f; 151 c |= 0x40; 152 } 153 if (lastc != '\r' || c != '\n') 154 lastc = c; 155 else { 156 lastc = '\n'; 157 continue; 158 } 159 } 160 putchar(c); 161 } 162 if (lastc != '\n') 163 putchar('\n'); 164 (void)fclose(fp); 165 } 166