net.c revision 100521
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 100521 2002-07-22 17:19:54Z ume $"); 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 6166199Swollmanextern int lflag; /* XXX finger.h? */ 6266199Swollmanextern int Tflag; /* XXX finger.h? */ 63100521Sumeextern sa_family_t family; 6446662Sobrien 6566199Swollmanstatic void cleanup(int sig);; 6666199Swollmanstatic int do_protocol(const char *name, const struct addrinfo *ai); 6766199Swollmanstatic void trying(const struct addrinfo *ai); 6866199Swollman 6946662Sobrienvoid 701590Srgrimesnetfinger(name) 711590Srgrimes char *name; 721590Srgrimes{ 7366199Swollman int error, multi; 7466199Swollman char *host; 7566199Swollman struct addrinfo *ai, *ai0; 7666199Swollman static struct addrinfo hint; 771590Srgrimes 7866199Swollman host = strrchr(name, '@'); 7966199Swollman if (host == 0) 801590Srgrimes return; 8127169Scharnier *host++ = '\0'; 8246662Sobrien signal(SIGALRM, cleanup); 8366199Swollman alarm(TIME_LIMIT); 8466199Swollman 8566199Swollman hint.ai_flags = AI_CANONNAME; 86100521Sume hint.ai_family = family; 8766199Swollman hint.ai_socktype = SOCK_STREAM; 8866199Swollman 8966199Swollman error = getaddrinfo(host, "finger", &hint, &ai0); 9066199Swollman if (error) { 9166199Swollman warnx("%s: %s", host, gai_strerror(error)); 921590Srgrimes return; 931590Srgrimes } 9466199Swollman 9566199Swollman multi = (ai0->ai_next) != 0; 9666199Swollman 9766315Swollman /* ai_canonname may not be filled in if the user specified an IP. */ 9866315Swollman if (ai0->ai_canonname == 0) 9966315Swollman printf("[%s]\n", host); 10066315Swollman else 10166315Swollman printf("[%s]\n", ai0->ai_canonname); 10266315Swollman 10366199Swollman for (ai = ai0; ai != 0; ai = ai->ai_next) { 10466199Swollman if (multi) 10566199Swollman trying(ai); 10666199Swollman 10766199Swollman error = do_protocol(name, ai); 10866199Swollman if (!error) 10966199Swollman break; 1101590Srgrimes } 11166199Swollman alarm(0); 11266199Swollman freeaddrinfo(ai0); 11366199Swollman} 11466199Swollman 11566199Swollmanstatic int 11666199Swollmando_protocol(const char *name, const struct addrinfo *ai) 11766199Swollman{ 11867467Sru int cnt, line_len, s; 11987229Smarkm FILE *fp; 12087229Smarkm int c, lastc; 12166199Swollman struct iovec iov[3]; 12266199Swollman struct msghdr msg; 12387229Smarkm static char slash_w[] = "/W "; 12487229Smarkm static char neteol[] = "\n\r"; 12566199Swollman 12666199Swollman s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 12766199Swollman if (s < 0) { 12866199Swollman warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype, 12966199Swollman ai->ai_protocol); 13066199Swollman return -1; 1311590Srgrimes } 1321590Srgrimes 13366199Swollman msg.msg_name = (void *)ai->ai_addr; 13466199Swollman msg.msg_namelen = ai->ai_addrlen; 13512909Swollman msg.msg_iov = iov; 13612909Swollman msg.msg_iovlen = 0; 13712909Swollman msg.msg_control = 0; 13812909Swollman msg.msg_controllen = 0; 13927835Swollman msg.msg_flags = 0; 14012909Swollman 1411590Srgrimes /* -l flag for remote fingerd */ 14212909Swollman if (lflag) { 14387229Smarkm iov[msg.msg_iovlen].iov_base = slash_w; 14412909Swollman iov[msg.msg_iovlen++].iov_len = 3; 14512909Swollman } 1461590Srgrimes /* send the name followed by <CR><LF> */ 14787229Smarkm iov[msg.msg_iovlen].iov_base = strdup(name); 14812909Swollman iov[msg.msg_iovlen++].iov_len = strlen(name); 14987229Smarkm iov[msg.msg_iovlen].iov_base = neteol; 15012909Swollman iov[msg.msg_iovlen++].iov_len = 2; 1511590Srgrimes 15266199Swollman /* 15366199Swollman * -T disables data-on-SYN: compatibility option to finger broken 15466199Swollman * hosts. Also, the implicit-open API is broken on IPv6, so do 15566199Swollman * the explicit connect there, too. 15666199Swollman */ 15766199Swollman if ((Tflag || ai->ai_addr->sa_family == AF_INET6) 15866199Swollman && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 15966199Swollman warn("connect"); 16066199Swollman close(s); 16166199Swollman return -1; 16214631Solah } 16314631Solah 16427835Swollman if (sendmsg(s, &msg, 0) < 0) { 16566199Swollman warn("sendmsg"); 16612909Swollman close(s); 16766199Swollman return -1; 16812909Swollman } 16912909Swollman 1701590Srgrimes /* 1711590Srgrimes * Read from the remote system; once we're connected, we assume some 1721590Srgrimes * data. If none arrives, we hang until the user interrupts. 1731590Srgrimes * 1741590Srgrimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1751590Srgrimes * a newline; if followed by a newline character, only output one 1761590Srgrimes * newline. 1771590Srgrimes * 1781590Srgrimes * Otherwise, all high bits are stripped; if it isn't printable and 1791590Srgrimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1801590Srgrimes * character with bit 7 set is printable. 1818874Srgrimes */ 18223693Speter lastc = 0; 18323693Speter if ((fp = fdopen(s, "r")) != NULL) { 18446662Sobrien cnt = 0; 18546662Sobrien line_len = 0; 1861590Srgrimes while ((c = getc(fp)) != EOF) { 18746662Sobrien if (++cnt > OUTPUT_MAX) { 18846662Sobrien printf("\n\n Output truncated at %d bytes...\n", 18946662Sobrien cnt - 1); 19046662Sobrien break; 19146662Sobrien } 1921590Srgrimes if (c == 0x0d) { 1931590Srgrimes if (lastc == '\r') /* ^M^M - skip dupes */ 1941590Srgrimes continue; 1951590Srgrimes c = '\n'; 1961590Srgrimes lastc = '\r'; 1971590Srgrimes } else { 19814472Sache if (!isprint(c) && !isspace(c)) { 19914472Sache c &= 0x7f; 2001590Srgrimes c |= 0x40; 20114472Sache } 2021590Srgrimes if (lastc != '\r' || c != '\n') 2031590Srgrimes lastc = c; 2041590Srgrimes else { 2051590Srgrimes lastc = '\n'; 2061590Srgrimes continue; 2071590Srgrimes } 2081590Srgrimes } 2091590Srgrimes putchar(c); 21046662Sobrien if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) { 21146662Sobrien putchar('\\'); 21246662Sobrien putchar('\n'); 21346662Sobrien lastc = '\r'; 21446662Sobrien } 21546662Sobrien if (lastc == '\n' || lastc == '\r') 21646662Sobrien line_len = 0; 2171590Srgrimes } 21812909Swollman if (ferror(fp)) { 21912909Swollman /* 22012909Swollman * Assume that whatever it was set errno... 22112909Swollman */ 22266199Swollman warn("reading from network"); 22312909Swollman } 22466199Swollman if (lastc != '\n') 22566199Swollman putchar('\n'); 22666199Swollman 22766199Swollman fclose(fp); 22812909Swollman } 22966199Swollman return 0; 2301590Srgrimes} 23166199Swollman 23266199Swollmanstatic void 23366199Swollmantrying(const struct addrinfo *ai) 23466199Swollman{ 23566199Swollman char buf[NI_MAXHOST]; 23666199Swollman 23766199Swollman if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf, 23866199Swollman (char *)0, 0, NI_NUMERICHOST) != 0) 23966199Swollman return; /* XXX can't happen */ 24066199Swollman 24166199Swollman printf("Trying %s...\n", buf); 24266199Swollman} 24366199Swollman 24466199Swollmanvoid 24587229Smarkmcleanup(int sig __unused) 24666199Swollman{ 24766199Swollman#define ERRSTR "Timed out.\n" 24866199Swollman write(STDERR_FILENO, ERRSTR, sizeof ERRSTR); 24966199Swollman exit(1); 25066199Swollman} 25166199Swollman 252