net.c revision 87628
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 87628 2001-12-10 21:13:08Z dwmalone $"); 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? */ 6346662Sobrien 6466199Swollmanstatic void cleanup(int sig);; 6566199Swollmanstatic int do_protocol(const char *name, const struct addrinfo *ai); 6666199Swollmanstatic void trying(const struct addrinfo *ai); 6766199Swollman 6846662Sobrienvoid 691590Srgrimesnetfinger(name) 701590Srgrimes char *name; 711590Srgrimes{ 7266199Swollman int error, multi; 7366199Swollman char *host; 7466199Swollman struct addrinfo *ai, *ai0; 7566199Swollman static struct addrinfo hint; 761590Srgrimes 7766199Swollman host = strrchr(name, '@'); 7866199Swollman if (host == 0) 791590Srgrimes return; 8027169Scharnier *host++ = '\0'; 8146662Sobrien signal(SIGALRM, cleanup); 8266199Swollman alarm(TIME_LIMIT); 8366199Swollman 8466199Swollman hint.ai_flags = AI_CANONNAME; 8566199Swollman hint.ai_family = PF_UNSPEC; 8666199Swollman hint.ai_socktype = SOCK_STREAM; 8766199Swollman 8866199Swollman error = getaddrinfo(host, "finger", &hint, &ai0); 8966199Swollman if (error) { 9066199Swollman warnx("%s: %s", host, gai_strerror(error)); 911590Srgrimes return; 921590Srgrimes } 9366199Swollman 9466199Swollman multi = (ai0->ai_next) != 0; 9566199Swollman 9666315Swollman /* ai_canonname may not be filled in if the user specified an IP. */ 9766315Swollman if (ai0->ai_canonname == 0) 9866315Swollman printf("[%s]\n", host); 9966315Swollman else 10066315Swollman printf("[%s]\n", ai0->ai_canonname); 10166315Swollman 10266199Swollman for (ai = ai0; ai != 0; ai = ai->ai_next) { 10366199Swollman if (multi) 10466199Swollman trying(ai); 10566199Swollman 10666199Swollman error = do_protocol(name, ai); 10766199Swollman if (!error) 10866199Swollman break; 1091590Srgrimes } 11066199Swollman alarm(0); 11166199Swollman freeaddrinfo(ai0); 11266199Swollman} 11366199Swollman 11466199Swollmanstatic int 11566199Swollmando_protocol(const char *name, const struct addrinfo *ai) 11666199Swollman{ 11767467Sru int cnt, line_len, s; 11887229Smarkm FILE *fp; 11987229Smarkm int c, lastc; 12066199Swollman struct iovec iov[3]; 12166199Swollman struct msghdr msg; 12287229Smarkm static char slash_w[] = "/W "; 12387229Smarkm static char neteol[] = "\n\r"; 12466199Swollman 12566199Swollman s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 12666199Swollman if (s < 0) { 12766199Swollman warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype, 12866199Swollman ai->ai_protocol); 12966199Swollman return -1; 1301590Srgrimes } 1311590Srgrimes 13266199Swollman msg.msg_name = (void *)ai->ai_addr; 13366199Swollman msg.msg_namelen = ai->ai_addrlen; 13412909Swollman msg.msg_iov = iov; 13512909Swollman msg.msg_iovlen = 0; 13612909Swollman msg.msg_control = 0; 13712909Swollman msg.msg_controllen = 0; 13827835Swollman msg.msg_flags = 0; 13912909Swollman 1401590Srgrimes /* -l flag for remote fingerd */ 14112909Swollman if (lflag) { 14287229Smarkm iov[msg.msg_iovlen].iov_base = slash_w; 14312909Swollman iov[msg.msg_iovlen++].iov_len = 3; 14412909Swollman } 1451590Srgrimes /* send the name followed by <CR><LF> */ 14687229Smarkm iov[msg.msg_iovlen].iov_base = strdup(name); 14712909Swollman iov[msg.msg_iovlen++].iov_len = strlen(name); 14887229Smarkm iov[msg.msg_iovlen].iov_base = neteol; 14912909Swollman iov[msg.msg_iovlen++].iov_len = 2; 1501590Srgrimes 15166199Swollman /* 15266199Swollman * -T disables data-on-SYN: compatibility option to finger broken 15366199Swollman * hosts. Also, the implicit-open API is broken on IPv6, so do 15466199Swollman * the explicit connect there, too. 15566199Swollman */ 15666199Swollman if ((Tflag || ai->ai_addr->sa_family == AF_INET6) 15766199Swollman && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { 15866199Swollman warn("connect"); 15966199Swollman close(s); 16066199Swollman return -1; 16114631Solah } 16214631Solah 16327835Swollman if (sendmsg(s, &msg, 0) < 0) { 16466199Swollman warn("sendmsg"); 16512909Swollman close(s); 16666199Swollman return -1; 16712909Swollman } 16812909Swollman 1691590Srgrimes /* 1701590Srgrimes * Read from the remote system; once we're connected, we assume some 1711590Srgrimes * data. If none arrives, we hang until the user interrupts. 1721590Srgrimes * 1731590Srgrimes * If we see a <CR> or a <CR> with the high bit set, treat it as 1741590Srgrimes * a newline; if followed by a newline character, only output one 1751590Srgrimes * newline. 1761590Srgrimes * 1771590Srgrimes * Otherwise, all high bits are stripped; if it isn't printable and 1781590Srgrimes * it isn't a space, we can simply set the 7th bit. Every ASCII 1791590Srgrimes * character with bit 7 set is printable. 1808874Srgrimes */ 18123693Speter lastc = 0; 18223693Speter if ((fp = fdopen(s, "r")) != NULL) { 18346662Sobrien cnt = 0; 18446662Sobrien line_len = 0; 1851590Srgrimes while ((c = getc(fp)) != EOF) { 18646662Sobrien if (++cnt > OUTPUT_MAX) { 18746662Sobrien printf("\n\n Output truncated at %d bytes...\n", 18846662Sobrien cnt - 1); 18946662Sobrien break; 19046662Sobrien } 1911590Srgrimes if (c == 0x0d) { 1921590Srgrimes if (lastc == '\r') /* ^M^M - skip dupes */ 1931590Srgrimes continue; 1941590Srgrimes c = '\n'; 1951590Srgrimes lastc = '\r'; 1961590Srgrimes } else { 19714472Sache if (!isprint(c) && !isspace(c)) { 19814472Sache c &= 0x7f; 1991590Srgrimes c |= 0x40; 20014472Sache } 2011590Srgrimes if (lastc != '\r' || c != '\n') 2021590Srgrimes lastc = c; 2031590Srgrimes else { 2041590Srgrimes lastc = '\n'; 2051590Srgrimes continue; 2061590Srgrimes } 2071590Srgrimes } 2081590Srgrimes putchar(c); 20946662Sobrien if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) { 21046662Sobrien putchar('\\'); 21146662Sobrien putchar('\n'); 21246662Sobrien lastc = '\r'; 21346662Sobrien } 21446662Sobrien if (lastc == '\n' || lastc == '\r') 21546662Sobrien line_len = 0; 2161590Srgrimes } 21712909Swollman if (ferror(fp)) { 21812909Swollman /* 21912909Swollman * Assume that whatever it was set errno... 22012909Swollman */ 22166199Swollman warn("reading from network"); 22212909Swollman } 22366199Swollman if (lastc != '\n') 22466199Swollman putchar('\n'); 22566199Swollman 22666199Swollman fclose(fp); 22712909Swollman } 22866199Swollman return 0; 2291590Srgrimes} 23066199Swollman 23166199Swollmanstatic void 23266199Swollmantrying(const struct addrinfo *ai) 23366199Swollman{ 23466199Swollman char buf[NI_MAXHOST]; 23566199Swollman 23666199Swollman if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf, 23766199Swollman (char *)0, 0, NI_NUMERICHOST) != 0) 23866199Swollman return; /* XXX can't happen */ 23966199Swollman 24066199Swollman printf("Trying %s...\n", buf); 24166199Swollman} 24266199Swollman 24366199Swollmanvoid 24487229Smarkmcleanup(int sig __unused) 24566199Swollman{ 24666199Swollman#define ERRSTR "Timed out.\n" 24766199Swollman write(STDERR_FILENO, ERRSTR, sizeof ERRSTR); 24866199Swollman exit(1); 24966199Swollman} 25066199Swollman 251