GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/finger/net.c Lines: 0 49 0.0 %
Date: 2016-12-06 Branches: 0 40 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: net.c,v 1.13 2015/01/16 06:40:07 deraadt Exp $	*/
2
3
/*
4
 * Copyright (c) 1989 The Regents of the University of California.
5
 * 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. Neither the name of the University nor the names of its contributors
19
 *    may be used to endorse or promote products derived from this software
20
 *    without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
 * SUCH DAMAGE.
33
 */
34
35
#include <sys/socket.h>
36
#include <netinet/in.h>
37
#include <arpa/inet.h>
38
#include <netdb.h>
39
#include <stdio.h>
40
#include <string.h>
41
#include <ctype.h>
42
#include <unistd.h>
43
#include <err.h>
44
#include "finger.h"
45
#include "extern.h"
46
47
void
48
netfinger(name)
49
	char *name;
50
{
51
	FILE *fp;
52
	int c, lastc;
53
	int s;
54
	char *host;
55
	struct addrinfo hints, *res0, *res;
56
	int error;
57
	char hbuf[NI_MAXHOST];
58
59
	lastc = 0;
60
	if (!(host = strrchr(name, '@')))
61
		return;
62
	*host++ = '\0';
63
	memset(&hints, 0, sizeof(hints));
64
	hints.ai_family = PF_UNSPEC;
65
	hints.ai_socktype = SOCK_STREAM;
66
	error = getaddrinfo(host, "finger", &hints, &res0);
67
	if (error) {
68
		warnx("%s", gai_strerror(error));
69
		return;
70
	}
71
72
	s = -1;
73
	for (res = res0; res; res = res->ai_next) {
74
		if ((s = socket(res->ai_family, res->ai_socktype,
75
				res->ai_protocol)) < 0) {
76
			continue;
77
		}
78
		if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
79
			(void)close(s);
80
			s = -1;
81
			continue;
82
		}
83
84
		break;
85
	}
86
87
	if (s < 0) {
88
		perror("finger");
89
		freeaddrinfo(res0);
90
		return;
91
	}
92
93
	/* have network connection; identify the host connected with */
94
	if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
95
	    NULL, 0, NI_NUMERICHOST) != 0) {
96
		strlcpy(hbuf, "(invalid)", sizeof hbuf);
97
	}
98
	(void)printf("[%s/%s]\n", host, hbuf);
99
100
	freeaddrinfo(res0);
101
102
	/* -l flag for remote fingerd  */
103
	if (lflag)
104
		write(s, "/W ", 3);
105
	/* send the name followed by <CR><LF> */
106
	(void)write(s, name, strlen(name));
107
	(void)write(s, "\r\n", 2);
108
109
	/*
110
	 * Read from the remote system; once we're connected, we assume some
111
	 * data.  If none arrives, we hang until the user interrupts.
112
	 *
113
	 * If we see a <CR> or a <CR> with the high bit set, treat it as
114
	 * a newline; if followed by a newline character, only output one
115
	 * newline.
116
	 *
117
	 * Otherwise, all high bits are stripped; if it isn't printable and
118
	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
119
	 * character with bit 7 set is printable.
120
	 */
121
	if ((fp = fdopen(s, "r")) != NULL)
122
		while ((c = getc(fp)) != EOF) {
123
			c &= 0x7f;
124
			if (c == '\r') {
125
				if (lastc == '\r')
126
					continue;
127
				c = '\n';
128
				lastc = '\r';
129
			} else {
130
				if (!isprint(c) && !isspace(c))
131
					c |= 0x40;
132
				if (lastc != '\r' || c != '\n')
133
					lastc = c;
134
				else {
135
					lastc = '\n';
136
					continue;
137
				}
138
			}
139
			putchar(c);
140
		}
141
	if (lastc != '\n')
142
		putchar('\n');
143
	(void)fclose(fp);
144
}