1 /* $OpenBSD: net.c,v 1.7 1999/12/11 10:05:04 itojun Exp $ */ 2 3 /* 4 * Copyright (c) 1989 The Regents of the University of California. 5 * 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 #ifndef lint 40 /*static char sccsid[] = "from: @(#)net.c 5.5 (Berkeley) 6/1/90";*/ 41 static char rcsid[] = "$OpenBSD: net.c,v 1.7 1999/12/11 10:05:04 itojun Exp $"; 42 #endif /* not lint */ 43 44 #include <sys/types.h> 45 #include <sys/socket.h> 46 #include <netinet/in.h> 47 #include <arpa/inet.h> 48 #include <netdb.h> 49 #include <stdio.h> 50 #include <string.h> 51 #include <ctype.h> 52 #include <unistd.h> 53 #include <err.h> 54 #include "finger.h" 55 #include "extern.h" 56 57 void 58 netfinger(name) 59 char *name; 60 { 61 FILE *fp; 62 int c, lastc; 63 int s; 64 char *host; 65 struct addrinfo hints, *res0, *res; 66 int error; 67 char hbuf[NI_MAXHOST]; 68 69 lastc = 0; 70 if (!(host = strrchr(name, '@'))) 71 return; 72 *host++ = '\0'; 73 memset(&hints, 0, sizeof(hints)); 74 hints.ai_family = PF_UNSPEC; 75 hints.ai_socktype = SOCK_STREAM; 76 error = getaddrinfo(host, "finger", &hints, &res0); 77 if (error) { 78 warnx("%s", gai_strerror(error)); 79 return; 80 } 81 82 s = -1; 83 for (res = res0; res; res = res->ai_next) { 84 if ((s = socket(res->ai_family, res->ai_socktype, 85 res->ai_protocol)) < 0) { 86 continue; 87 } 88 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { 89 (void)close(s); 90 s = -1; 91 continue; 92 } 93 94 break; 95 } 96 97 if (s < 0) { 98 perror("finger"); 99 freeaddrinfo(res0); 100 return; 101 } 102 103 /* have network connection; identify the host connected with */ 104 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), 105 NULL, 0, NI_NUMERICHOST) != 0) { 106 strcpy(hbuf, "(invalid)"); 107 } 108 (void)printf("[%s/%s]\n", host, hbuf); 109 110 freeaddrinfo(res0); 111 112 /* -l flag for remote fingerd */ 113 if (lflag) 114 write(s, "/W ", 3); 115 /* send the name followed by <CR><LF> */ 116 (void)write(s, name, strlen(name)); 117 (void)write(s, "\r\n", 2); 118 119 /* 120 * Read from the remote system; once we're connected, we assume some 121 * data. If none arrives, we hang until the user interrupts. 122 * 123 * If we see a <CR> or a <CR> with the high bit set, treat it as 124 * a newline; if followed by a newline character, only output one 125 * newline. 126 * 127 * Otherwise, all high bits are stripped; if it isn't printable and 128 * it isn't a space, we can simply set the 7th bit. Every ASCII 129 * character with bit 7 set is printable. 130 */ 131 if ((fp = fdopen(s, "r")) != NULL) 132 while ((c = getc(fp)) != EOF) { 133 c &= 0x7f; 134 if (c == '\r') { 135 if (lastc == '\r') 136 continue; 137 c = '\n'; 138 lastc = '\r'; 139 } else { 140 if (!isprint(c) && !isspace(c)) 141 c |= 0x40; 142 if (lastc != '\r' || c != '\n') 143 lastc = c; 144 else { 145 lastc = '\n'; 146 continue; 147 } 148 } 149 putchar(c); 150 } 151 if (lastc != '\n') 152 putchar('\n'); 153 (void)fclose(fp); 154 } 155