net.c revision 27169
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 371590Srgrimes#ifndef lint 3827169Scharnier#if 0 3923693Speterstatic char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95"; 4027169Scharnier#else 4127169Scharnierstatic const char rcsid[] = 4227169Scharnier "$Id$"; 4327169Scharnier#endif 441590Srgrimes#endif /* not lint */ 451590Srgrimes 461590Srgrimes#include <sys/types.h> 471590Srgrimes#include <sys/socket.h> 481590Srgrimes#include <netinet/in.h> 491590Srgrimes#include <arpa/inet.h> 501590Srgrimes#include <netdb.h> 511590Srgrimes#include <db.h> 5227169Scharnier#include <err.h> 531590Srgrimes#include <unistd.h> 541590Srgrimes#include <pwd.h> 551590Srgrimes#include <utmp.h> 561590Srgrimes#include <stdio.h> 571590Srgrimes#include <ctype.h> 581590Srgrimes#include <string.h> 5912909Swollman#include <sys/uio.h> 601590Srgrimes#include "finger.h" 611590Srgrimes 621590Srgrimesvoid 631590Srgrimesnetfinger(name) 641590Srgrimes char *name; 651590Srgrimes{ 661590Srgrimes extern int lflag; 6714631Solah extern int Tflag; 681590Srgrimes register FILE *fp; 691590Srgrimes register int c, lastc; 701590Srgrimes struct in_addr defaddr; 711590Srgrimes struct hostent *hp, def; 721590Srgrimes struct servent *sp; 731590Srgrimes struct sockaddr_in sin; 741590Srgrimes int s; 751590Srgrimes char *alist[1], *host; 7612909Swollman struct iovec iov[3]; 7712909Swollman struct msghdr msg; 781590Srgrimes 791590Srgrimes if (!(host = rindex(name, '@'))) 801590Srgrimes return; 8127169Scharnier *host++ = '\0'; 821590Srgrimes if (isdigit(*host) && (defaddr.s_addr = inet_addr(host)) != -1) { 831590Srgrimes def.h_name = host; 841590Srgrimes def.h_addr_list = alist; 851590Srgrimes def.h_addr = (char *)&defaddr; 861590Srgrimes def.h_length = sizeof(struct in_addr); 871590Srgrimes def.h_addrtype = AF_INET; 881590Srgrimes def.h_aliases = 0; 891590Srgrimes hp = &def; 901590Srgrimes } else if (!(hp = gethostbyname(host))) { 9127169Scharnier warnx("unknown host: %s", host); 921590Srgrimes return; 931590Srgrimes } 941590Srgrimes if (!(sp = getservbyname("finger", "tcp"))) { 9527169Scharnier warnx("tcp/finger: unknown service"); 961590Srgrimes return; 971590Srgrimes } 981590Srgrimes sin.sin_family = hp->h_addrtype; 991590Srgrimes bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 1001590Srgrimes sin.sin_port = sp->s_port; 1011590Srgrimes if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { 1021590Srgrimes perror("finger: socket"); 1031590Srgrimes return; 1041590Srgrimes } 1051590Srgrimes 1061590Srgrimes /* have network connection; identify the host connected with */ 1071590Srgrimes (void)printf("[%s]\n", hp->h_name); 1081590Srgrimes 10912909Swollman msg.msg_name = (void *)&sin; 11012909Swollman msg.msg_namelen = sizeof sin; 11112909Swollman msg.msg_iov = iov; 11212909Swollman msg.msg_iovlen = 0; 11312909Swollman msg.msg_control = 0; 11412909Swollman msg.msg_controllen = 0; 11512909Swollman msg.msg_flags = MSG_EOF; 11612909Swollman 1171590Srgrimes /* -l flag for remote fingerd */ 11812909Swollman if (lflag) { 11912909Swollman iov[msg.msg_iovlen].iov_base = "/W "; 12012909Swollman iov[msg.msg_iovlen++].iov_len = 3; 12112909Swollman } 1221590Srgrimes /* send the name followed by <CR><LF> */ 12312909Swollman iov[msg.msg_iovlen].iov_base = name; 12412909Swollman iov[msg.msg_iovlen++].iov_len = strlen(name); 12512909Swollman iov[msg.msg_iovlen].iov_base = "\r\n"; 12612909Swollman iov[msg.msg_iovlen++].iov_len = 2; 1271590Srgrimes 12814631Solah /* -T disables T/TCP: compatibility option to finger broken hosts */ 12914631Solah if (Tflag && connect(s, (struct sockaddr *)&sin, sizeof (sin))) { 13014631Solah perror("finger: connect"); 13114631Solah return; 13214631Solah } 13314631Solah 13412909Swollman if (sendmsg(s, &msg, MSG_EOF) < 0) { 13512909Swollman perror("finger: sendmsg"); 13612909Swollman close(s); 13712909Swollman return; 13812909Swollman } 13912909Swollman 1401590Srgrimes /* 1411590Srgrimes * Read from the remote system; once we're connected, we assume some 1421590Srgrimes * data. If none arrives, we hang until the user interrupts. 1431590Srgrimes * 1441590Srgrimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1451590Srgrimes * a newline; if followed by a newline character, only output one 1461590Srgrimes * newline. 1471590Srgrimes * 1481590Srgrimes * Otherwise, all high bits are stripped; if it isn't printable and 1491590Srgrimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1501590Srgrimes * character with bit 7 set is printable. 1518874Srgrimes */ 15223693Speter lastc = 0; 15323693Speter if ((fp = fdopen(s, "r")) != NULL) { 1541590Srgrimes while ((c = getc(fp)) != EOF) { 1551590Srgrimes if (c == 0x0d) { 1561590Srgrimes if (lastc == '\r') /* ^M^M - skip dupes */ 1571590Srgrimes continue; 1581590Srgrimes c = '\n'; 1591590Srgrimes lastc = '\r'; 1601590Srgrimes } else { 16114472Sache if (!isprint(c) && !isspace(c)) { 16214472Sache c &= 0x7f; 1631590Srgrimes c |= 0x40; 16414472Sache } 1651590Srgrimes if (lastc != '\r' || c != '\n') 1661590Srgrimes lastc = c; 1671590Srgrimes else { 1681590Srgrimes lastc = '\n'; 1691590Srgrimes continue; 1701590Srgrimes } 1711590Srgrimes } 1721590Srgrimes putchar(c); 1731590Srgrimes } 17412909Swollman if (lastc != '\n') 17512909Swollman putchar('\n'); 17612909Swollman 17712909Swollman if (ferror(fp)) { 17812909Swollman /* 17912909Swollman * Assume that whatever it was set errno... 18012909Swollman */ 18112909Swollman perror("finger: read"); 18212909Swollman } 18312909Swollman (void)fclose(fp); 18412909Swollman } 1851590Srgrimes} 186