Annotation of src/usr.bin/finger/net.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD$ */
! 2:
1.1 deraadt 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";*/
1.2 ! deraadt 41: static char rcsid[] = "$OpenBSD: net.c,v 1.1.1.1 1995/10/18 08:45:14 deraadt Exp $";
1.1 deraadt 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:
53: netfinger(name)
54: char *name;
55: {
56: extern int lflag;
57: register FILE *fp;
58: register int c, lastc;
59: struct hostent *hp, def;
60: struct servent *sp;
61: struct sockaddr_in sin;
62: int s;
63: char *alist[1], *host, *rindex();
64:
65: if (!(host = rindex(name, '@')))
66: return;
67: *host++ = NULL;
68: if (inet_aton(host, &sin.sin_addr) == 0) {
69: hp = gethostbyname(host);
70: if (hp == 0) {
71: (void)fprintf(stderr,
72: "finger: unknown host: %s\n", host);
73: return;
74: }
75: sin.sin_family = hp->h_addrtype;
76: bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
77: host = hp->h_name;
78: } else
79: sin.sin_family = AF_INET;
80: if (!(sp = getservbyname("finger", "tcp"))) {
81: (void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
82: return;
83: }
84: sin.sin_port = sp->s_port;
85: if ((s = socket(sin.sin_family, SOCK_STREAM, 0)) < 0) {
86: perror("finger: socket");
87: return;
88: }
89:
90: /* have network connection; identify the host connected with */
91: (void)printf("[%s]\n", host);
92: if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
93: perror("finger: connect");
94: (void)close(s);
95: return;
96: }
97:
98: /* -l flag for remote fingerd */
99: if (lflag)
100: write(s, "/W ", 3);
101: /* send the name followed by <CR><LF> */
102: (void)write(s, name, strlen(name));
103: (void)write(s, "\r\n", 2);
104:
105: /*
106: * Read from the remote system; once we're connected, we assume some
107: * data. If none arrives, we hang until the user interrupts.
108: *
109: * If we see a <CR> or a <CR> with the high bit set, treat it as
110: * a newline; if followed by a newline character, only output one
111: * newline.
112: *
113: * Otherwise, all high bits are stripped; if it isn't printable and
114: * it isn't a space, we can simply set the 7th bit. Every ASCII
115: * character with bit 7 set is printable.
116: */
117: if (fp = fdopen(s, "r"))
118: while ((c = getc(fp)) != EOF) {
119: c &= 0x7f;
120: if (c == '\r') {
121: if (lastc == '\r')
122: continue;
123: c = '\n';
124: lastc = '\r';
125: } else {
126: if (!isprint(c) && !isspace(c))
127: c |= 0x40;
128: if (lastc != '\r' || c != '\n')
129: lastc = c;
130: else {
131: lastc = '\n';
132: continue;
133: }
134: }
135: putchar(c);
136: }
137: if (lastc != '\n')
138: putchar('\n');
139: (void)fclose(fp);
140: }