1 /* $NetBSD: net.c,v 1.10 1997/10/19 08:13:42 mrg 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.10 1997/10/19 08:13:42 mrg 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 65 #include "finger.h" 66 #include "extern.h" 67 68 void 69 netfinger(name) 70 char *name; 71 { 72 FILE *fp; 73 int c, lastc; 74 struct hostent *hp; 75 struct servent *sp; 76 struct sockaddr_in sin; 77 int s; 78 char *host; 79 80 lastc = 0; 81 if (!(host = strrchr(name, '@'))) 82 return; 83 *host++ = '\0'; 84 if (inet_aton(host, &sin.sin_addr) == 0) { 85 hp = gethostbyname(host); 86 if (hp == 0) { 87 (void)fprintf(stderr, 88 "finger: unknown host: %s\n", host); 89 return; 90 } 91 sin.sin_family = hp->h_addrtype; 92 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 93 host = hp->h_name; 94 } else 95 sin.sin_family = AF_INET; 96 if (!(sp = getservbyname("finger", "tcp"))) { 97 (void)fprintf(stderr, "finger: tcp/finger: unknown service\n"); 98 return; 99 } 100 sin.sin_port = sp->s_port; 101 if ((s = socket(sin.sin_family, SOCK_STREAM, 0)) < 0) { 102 perror("finger: socket"); 103 return; 104 } 105 106 /* have network connection; identify the host connected with */ 107 (void)printf("[%s]\n", host); 108 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 109 perror("finger: connect"); 110 (void)close(s); 111 return; 112 } 113 114 /* -l flag for remote fingerd */ 115 if (lflag) 116 write(s, "/W ", 3); 117 /* send the name followed by <CR><LF> */ 118 (void)write(s, name, strlen(name)); 119 (void)write(s, "\r\n", 2); 120 121 /* 122 * Read from the remote system; once we're connected, we assume some 123 * data. If none arrives, we hang until the user interrupts. 124 * 125 * If we see a <CR> or a <CR> with the high bit set, treat it as 126 * a newline; if followed by a newline character, only output one 127 * newline. 128 * 129 * Otherwise, all high bits are stripped; if it isn't printable and 130 * it isn't a space, we can simply set the 7th bit. Every ASCII 131 * character with bit 7 set is printable. 132 */ 133 if ((fp = fdopen(s, "r")) != NULL) 134 while ((c = getc(fp)) != EOF) { 135 c &= 0x7f; 136 if (c == '\r') { 137 if (lastc == '\r') /* ^M^M - skip dupes */ 138 continue; 139 c = '\n'; 140 lastc = '\r'; 141 } else { 142 if (!isprint(c) && !isspace(c)) 143 c |= 0x40; 144 if (lastc != '\r' || c != '\n') 145 lastc = c; 146 else { 147 lastc = '\n'; 148 continue; 149 } 150 } 151 putchar(c); 152 } 153 if (lastc != '\n') 154 putchar('\n'); 155 putchar('\n'); 156 (void)fclose(fp); 157 } 158