net.c revision 168632
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 3787628Sdwmalone#if 0 3887628Sdwmalone#ifndef lint 3987628Sdwmalonestatic char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95"; 4087628Sdwmalone#endif 4187628Sdwmalone#endif 4287628Sdwmalone 4387229Smarkm#include <sys/cdefs.h> 4487229Smarkm__FBSDID("$FreeBSD: head/usr.bin/finger/net.c 168632 2007-04-11 19:11:54Z des $"); 4587229Smarkm 4636792Simp#include <sys/param.h> 471590Srgrimes#include <sys/socket.h> 4872109Scharnier#include <sys/uio.h> 4972109Scharnier#include <ctype.h> 501590Srgrimes#include <db.h> 5127169Scharnier#include <err.h> 5272109Scharnier#include <netdb.h> 531590Srgrimes#include <pwd.h> 541590Srgrimes#include <stdio.h> 5578718Sdd#include <stdlib.h> 561590Srgrimes#include <string.h> 5772109Scharnier#include <unistd.h> 5872109Scharnier#include <utmp.h> 591590Srgrimes#include "finger.h" 601590Srgrimes 61129302Sstefanfstatic void cleanup(int sig); 6266199Swollmanstatic int do_protocol(const char *name, const struct addrinfo *ai); 6366199Swollmanstatic void trying(const struct addrinfo *ai); 6466199Swollman 6546662Sobrienvoid 66102944Sdwmalonenetfinger(char *name) 671590Srgrimes{ 6866199Swollman int error, multi; 6966199Swollman char *host; 7066199Swollman struct addrinfo *ai, *ai0; 7166199Swollman static struct addrinfo hint; 721590Srgrimes 7366199Swollman host = strrchr(name, '@'); 7466199Swollman if (host == 0) 751590Srgrimes return; 7627169Scharnier *host++ = '\0'; 7746662Sobrien signal(SIGALRM, cleanup); 7866199Swollman alarm(TIME_LIMIT); 7966199Swollman 8066199Swollman hint.ai_flags = AI_CANONNAME; 81100521Sume hint.ai_family = family; 8266199Swollman hint.ai_socktype = SOCK_STREAM; 8366199Swollman 8466199Swollman error = getaddrinfo(host, "finger", &hint, &ai0); 8566199Swollman if (error) { 8666199Swollman warnx("%s: %s", host, gai_strerror(error)); 871590Srgrimes return; 881590Srgrimes } 8966199Swollman 9066199Swollman multi = (ai0->ai_next) != 0; 9166199Swollman 9266315Swollman /* ai_canonname may not be filled in if the user specified an IP. */ 9366315Swollman if (ai0->ai_canonname == 0) 9466315Swollman printf("[%s]\n", host); 9566315Swollman else 9666315Swollman printf("[%s]\n", ai0->ai_canonname); 9766315Swollman 9866199Swollman for (ai = ai0; ai != 0; ai = ai->ai_next) { 9966199Swollman if (multi) 10066199Swollman trying(ai); 10166199Swollman 10266199Swollman error = do_protocol(name, ai); 10366199Swollman if (!error) 10466199Swollman break; 1051590Srgrimes } 10666199Swollman alarm(0); 10766199Swollman freeaddrinfo(ai0); 10866199Swollman} 10966199Swollman 11066199Swollmanstatic int 11166199Swollmando_protocol(const char *name, const struct addrinfo *ai) 11266199Swollman{ 11367467Sru int cnt, line_len, s; 11487229Smarkm FILE *fp; 11587229Smarkm int c, lastc; 11666199Swollman struct iovec iov[3]; 11766199Swollman struct msghdr msg; 11887229Smarkm static char slash_w[] = "/W "; 119107528Sroam static char neteol[] = "\r\n"; 12066199Swollman 12166199Swollman s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 12266199Swollman if (s < 0) { 12366199Swollman warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype, 12466199Swollman ai->ai_protocol); 12566199Swollman return -1; 1261590Srgrimes } 1271590Srgrimes 12866199Swollman msg.msg_name = (void *)ai->ai_addr; 12966199Swollman msg.msg_namelen = ai->ai_addrlen; 13012909Swollman msg.msg_iov = iov; 13112909Swollman msg.msg_iovlen = 0; 13212909Swollman msg.msg_control = 0; 13312909Swollman msg.msg_controllen = 0; 13427835Swollman msg.msg_flags = 0; 13512909Swollman 1361590Srgrimes /* -l flag for remote fingerd */ 13712909Swollman if (lflag) { 13887229Smarkm iov[msg.msg_iovlen].iov_base = slash_w; 13912909Swollman iov[msg.msg_iovlen++].iov_len = 3; 14012909Swollman } 1411590Srgrimes /* send the name followed by <CR><LF> */ 14287229Smarkm iov[msg.msg_iovlen].iov_base = strdup(name); 14312909Swollman iov[msg.msg_iovlen++].iov_len = strlen(name); 14487229Smarkm iov[msg.msg_iovlen].iov_base = neteol; 14512909Swollman iov[msg.msg_iovlen++].iov_len = 2; 1461590Srgrimes 147168632Sdes if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 14866199Swollman warn("connect"); 14966199Swollman close(s); 15066199Swollman return -1; 15114631Solah } 15214631Solah 15327835Swollman if (sendmsg(s, &msg, 0) < 0) { 15466199Swollman warn("sendmsg"); 15512909Swollman close(s); 15666199Swollman return -1; 15712909Swollman } 15812909Swollman 1591590Srgrimes /* 1601590Srgrimes * Read from the remote system; once we're connected, we assume some 1611590Srgrimes * data. If none arrives, we hang until the user interrupts. 1621590Srgrimes * 1631590Srgrimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1641590Srgrimes * a newline; if followed by a newline character, only output one 1651590Srgrimes * newline. 1661590Srgrimes * 1671590Srgrimes * Otherwise, all high bits are stripped; if it isn't printable and 1681590Srgrimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1691590Srgrimes * character with bit 7 set is printable. 1708874Srgrimes */ 17123693Speter lastc = 0; 17223693Speter if ((fp = fdopen(s, "r")) != NULL) { 17346662Sobrien cnt = 0; 17446662Sobrien line_len = 0; 1751590Srgrimes while ((c = getc(fp)) != EOF) { 17646662Sobrien if (++cnt > OUTPUT_MAX) { 17746662Sobrien printf("\n\n Output truncated at %d bytes...\n", 17846662Sobrien cnt - 1); 17946662Sobrien break; 18046662Sobrien } 1811590Srgrimes if (c == 0x0d) { 1821590Srgrimes if (lastc == '\r') /* ^M^M - skip dupes */ 1831590Srgrimes continue; 1841590Srgrimes c = '\n'; 1851590Srgrimes lastc = '\r'; 1861590Srgrimes } else { 18714472Sache if (!isprint(c) && !isspace(c)) { 18814472Sache c &= 0x7f; 1891590Srgrimes c |= 0x40; 19014472Sache } 1911590Srgrimes if (lastc != '\r' || c != '\n') 1921590Srgrimes lastc = c; 1931590Srgrimes else { 1941590Srgrimes lastc = '\n'; 1951590Srgrimes continue; 1961590Srgrimes } 1971590Srgrimes } 1981590Srgrimes putchar(c); 19946662Sobrien if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) { 20046662Sobrien putchar('\\'); 20146662Sobrien putchar('\n'); 20246662Sobrien lastc = '\r'; 20346662Sobrien } 20446662Sobrien if (lastc == '\n' || lastc == '\r') 20546662Sobrien line_len = 0; 2061590Srgrimes } 20712909Swollman if (ferror(fp)) { 20812909Swollman /* 20912909Swollman * Assume that whatever it was set errno... 21012909Swollman */ 21166199Swollman warn("reading from network"); 21212909Swollman } 21366199Swollman if (lastc != '\n') 21466199Swollman putchar('\n'); 21566199Swollman 21666199Swollman fclose(fp); 21712909Swollman } 21866199Swollman return 0; 2191590Srgrimes} 22066199Swollman 22166199Swollmanstatic void 22266199Swollmantrying(const struct addrinfo *ai) 22366199Swollman{ 22466199Swollman char buf[NI_MAXHOST]; 22566199Swollman 22666199Swollman if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf, 22766199Swollman (char *)0, 0, NI_NUMERICHOST) != 0) 22866199Swollman return; /* XXX can't happen */ 22966199Swollman 23066199Swollman printf("Trying %s...\n", buf); 23166199Swollman} 23266199Swollman 23366199Swollmanvoid 23487229Smarkmcleanup(int sig __unused) 23566199Swollman{ 23666199Swollman#define ERRSTR "Timed out.\n" 23766199Swollman write(STDERR_FILENO, ERRSTR, sizeof ERRSTR); 23866199Swollman exit(1); 23966199Swollman} 24066199Swollman 241