xref: /netbsd-src/usr.bin/finger/net.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: net.c,v 1.9 1997/05/17 19:42:25 pk 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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 /*static char sccsid[] = "from: @(#)net.c	5.5 (Berkeley) 6/1/90";*/
41 static char rcsid[] = "$NetBSD: net.c,v 1.9 1997/05/17 19:42:25 pk Exp $";
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <netdb.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <ctype.h>
52 #include <unistd.h>
53 #include "finger.h"
54 #include "extern.h"
55 
56 void
57 netfinger(name)
58 	char *name;
59 {
60 	FILE *fp;
61 	int c, lastc;
62 	struct hostent *hp;
63 	struct servent *sp;
64 	struct sockaddr_in sin;
65 	int s;
66 	char *host;
67 
68 	lastc = 0;
69 	if (!(host = strrchr(name, '@')))
70 		return;
71 	*host++ = '\0';
72 	if (inet_aton(host, &sin.sin_addr) == 0) {
73 		hp = gethostbyname(host);
74 		if (hp == 0) {
75 			(void)fprintf(stderr,
76 			    "finger: unknown host: %s\n", host);
77 			return;
78 		}
79 		sin.sin_family = hp->h_addrtype;
80 		bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
81 		host = hp->h_name;
82 	} else
83 		sin.sin_family = AF_INET;
84 	if (!(sp = getservbyname("finger", "tcp"))) {
85 		(void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
86 		return;
87 	}
88 	sin.sin_port = sp->s_port;
89 	if ((s = socket(sin.sin_family, SOCK_STREAM, 0)) < 0) {
90 		perror("finger: socket");
91 		return;
92 	}
93 
94 	/* have network connection; identify the host connected with */
95 	(void)printf("[%s]\n", host);
96 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
97 		perror("finger: connect");
98 		(void)close(s);
99 		return;
100 	}
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 }
145