net.c revision 1.20 1 1.20 agc /* $NetBSD: net.c,v 1.20 2003/08/07 11:13:45 agc Exp $ */
2 1.8 tls
3 1.1 cgd /*
4 1.10 mrg * Copyright (c) 1989, 1993
5 1.10 mrg * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.20 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.10 mrg #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.10 mrg #if 0
38 1.10 mrg static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95";
39 1.10 mrg #else
40 1.20 agc __RCSID("$NetBSD: net.c,v 1.20 2003/08/07 11:13:45 agc Exp $");
41 1.10 mrg #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/types.h>
45 1.1 cgd #include <sys/socket.h>
46 1.10 mrg
47 1.1 cgd #include <netinet/in.h>
48 1.10 mrg
49 1.5 mycroft #include <arpa/inet.h>
50 1.10 mrg
51 1.1 cgd #include <netdb.h>
52 1.15 christos #include <time.h>
53 1.10 mrg #include <db.h>
54 1.10 mrg #include <unistd.h>
55 1.10 mrg #include <pwd.h>
56 1.1 cgd #include <stdio.h>
57 1.4 cgd #include <string.h>
58 1.1 cgd #include <ctype.h>
59 1.7 lukem #include <unistd.h>
60 1.13 itojun #include <err.h>
61 1.15 christos
62 1.15 christos #include "utmpentry.h"
63 1.10 mrg
64 1.7 lukem #include "finger.h"
65 1.7 lukem #include "extern.h"
66 1.1 cgd
67 1.7 lukem void
68 1.1 cgd netfinger(name)
69 1.1 cgd char *name;
70 1.1 cgd {
71 1.7 lukem FILE *fp;
72 1.7 lukem int c, lastc;
73 1.1 cgd int s;
74 1.7 lukem char *host;
75 1.13 itojun struct addrinfo hints, *res, *res0;
76 1.13 itojun int error;
77 1.13 itojun char *emsg = NULL;
78 1.1 cgd
79 1.7 lukem lastc = 0;
80 1.7 lukem if (!(host = strrchr(name, '@')))
81 1.1 cgd return;
82 1.9 pk *host++ = '\0';
83 1.13 itojun memset(&hints, 0, sizeof(hints));
84 1.13 itojun hints.ai_family = PF_UNSPEC;
85 1.13 itojun hints.ai_socktype = SOCK_STREAM;
86 1.13 itojun hints.ai_flags = AI_CANONNAME;
87 1.13 itojun error = getaddrinfo(host, "finger", &hints, &res0);
88 1.13 itojun if (error) {
89 1.13 itojun warnx("%s: %s", gai_strerror(error), host);
90 1.13 itojun return;
91 1.13 itojun }
92 1.13 itojun
93 1.13 itojun s = -1;
94 1.13 itojun for (res = res0; res; res = res->ai_next) {
95 1.13 itojun s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
96 1.13 itojun if (s < 0) {
97 1.13 itojun emsg = "socket";
98 1.13 itojun continue;
99 1.13 itojun }
100 1.13 itojun
101 1.13 itojun if (connect(s, res->ai_addr, res->ai_addrlen)) {
102 1.13 itojun close(s);
103 1.13 itojun s = -1;
104 1.13 itojun emsg = "connect";
105 1.13 itojun continue;
106 1.1 cgd }
107 1.13 itojun
108 1.13 itojun break;
109 1.1 cgd }
110 1.13 itojun if (s < 0) {
111 1.13 itojun if (emsg != NULL)
112 1.14 itojun warn("%s", emsg);
113 1.1 cgd return;
114 1.1 cgd }
115 1.1 cgd
116 1.1 cgd /* have network connection; identify the host connected with */
117 1.13 itojun (void)printf("[%s]\n", res0->ai_canonname ? res0->ai_canonname : host);
118 1.1 cgd
119 1.1 cgd /* -l flag for remote fingerd */
120 1.1 cgd if (lflag)
121 1.1 cgd write(s, "/W ", 3);
122 1.1 cgd /* send the name followed by <CR><LF> */
123 1.1 cgd (void)write(s, name, strlen(name));
124 1.1 cgd (void)write(s, "\r\n", 2);
125 1.1 cgd
126 1.1 cgd /*
127 1.1 cgd * Read from the remote system; once we're connected, we assume some
128 1.1 cgd * data. If none arrives, we hang until the user interrupts.
129 1.1 cgd *
130 1.18 kim * If we see a <CR> followed by a newline character, only output
131 1.18 kim * one newline.
132 1.1 cgd *
133 1.18 kim * If a character isn't printable and it isn't a space, we strip the
134 1.18 kim * 8th bit and set the 7th bit. Every ASCII character with bit 7 set
135 1.18 kim * is printable.
136 1.7 lukem */
137 1.7 lukem if ((fp = fdopen(s, "r")) != NULL)
138 1.1 cgd while ((c = getc(fp)) != EOF) {
139 1.3 mycroft if (c == '\r') {
140 1.10 mrg if (lastc == '\r') /* ^M^M - skip dupes */
141 1.3 mycroft continue;
142 1.1 cgd c = '\n';
143 1.1 cgd lastc = '\r';
144 1.1 cgd } else {
145 1.19 kim if (!(eightflag || isprint(c) || isspace(c))) {
146 1.18 kim c &= 0x7f;
147 1.1 cgd c |= 0x40;
148 1.18 kim }
149 1.1 cgd if (lastc != '\r' || c != '\n')
150 1.1 cgd lastc = c;
151 1.1 cgd else {
152 1.1 cgd lastc = '\n';
153 1.1 cgd continue;
154 1.1 cgd }
155 1.1 cgd }
156 1.1 cgd putchar(c);
157 1.1 cgd }
158 1.1 cgd if (lastc != '\n')
159 1.1 cgd putchar('\n');
160 1.1 cgd (void)fclose(fp);
161 1.1 cgd }
162