]> git.cameronkatri.com Git - apple_cmds.git/blob - adv_cmds/finger/net.c
Create README.md
[apple_cmds.git] / adv_cmds / finger / net.c
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #if 0
38 #ifndef lint
39 static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95";
40 #endif
41 #endif
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD: src/usr.bin/finger/net.c,v 1.23 2004/05/16 22:08:15 stefanf Exp $");
45
46 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <sys/uio.h>
49 #include <ctype.h>
50 #include <db.h>
51 #include <err.h>
52 #include <netdb.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <utmpx.h>
59 #include "finger.h"
60
61 static void cleanup(int sig);
62 static int do_protocol(const char *name, const struct addrinfo *ai);
63 static void trying(const struct addrinfo *ai);
64
65 void
66 netfinger(char *name)
67 {
68 int error, multi;
69 char *host;
70 struct addrinfo *ai, *ai0;
71 static struct addrinfo hint;
72
73 host = strrchr(name, '@');
74 if (host == 0)
75 return;
76 *host++ = '\0';
77 signal(SIGALRM, cleanup);
78 alarm(TIME_LIMIT);
79
80 hint.ai_flags = AI_CANONNAME;
81 hint.ai_family = family;
82 hint.ai_socktype = SOCK_STREAM;
83
84 error = getaddrinfo(host, "finger", &hint, &ai0);
85 if (error) {
86 warnx("%s: %s", host, gai_strerror(error));
87 return;
88 }
89
90 multi = (ai0->ai_next) != 0;
91
92 /* ai_canonname may not be filled in if the user specified an IP. */
93 if (ai0->ai_canonname == 0)
94 printf("[%s]\n", host);
95 else
96 printf("[%s]\n", ai0->ai_canonname);
97
98 for (ai = ai0; ai != 0; ai = ai->ai_next) {
99 if (multi)
100 trying(ai);
101
102 error = do_protocol(name, ai);
103 if (!error)
104 break;
105 }
106 alarm(0);
107 freeaddrinfo(ai0);
108 }
109
110 static int
111 do_protocol(const char *name, const struct addrinfo *ai)
112 {
113 int cnt, line_len, s;
114 FILE *fp;
115 int c, lastc;
116 struct iovec iov[3];
117 struct msghdr msg;
118 static char slash_w[] = "/W ";
119 static char neteol[] = "\r\n";
120
121 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
122 if (s < 0) {
123 warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
124 ai->ai_protocol);
125 return -1;
126 }
127
128 msg.msg_name = (void *)ai->ai_addr;
129 msg.msg_namelen = ai->ai_addrlen;
130 msg.msg_iov = iov;
131 msg.msg_iovlen = 0;
132 msg.msg_control = 0;
133 msg.msg_controllen = 0;
134 msg.msg_flags = 0;
135
136 /* -l flag for remote fingerd */
137 if (lflag) {
138 iov[msg.msg_iovlen].iov_base = slash_w;
139 iov[msg.msg_iovlen++].iov_len = 3;
140 }
141 /* send the name followed by <CR><LF> */
142 iov[msg.msg_iovlen].iov_base = strdup(name);
143 iov[msg.msg_iovlen++].iov_len = strlen(name);
144 iov[msg.msg_iovlen].iov_base = neteol;
145 iov[msg.msg_iovlen++].iov_len = 2;
146
147 #ifdef __APPLE__
148 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
149 #else
150 /*
151 * -T disables data-on-SYN: compatibility option to finger broken
152 * hosts. Also, the implicit-open API is broken on IPv6, so do
153 * the explicit connect there, too.
154 */
155 if ((Tflag || ai->ai_addr->sa_family == AF_INET6)
156 && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
157 #endif
158 warn("connect");
159 close(s);
160 return -1;
161 }
162
163 if (sendmsg(s, &msg, 0) < 0) {
164 warn("sendmsg");
165 close(s);
166 return -1;
167 }
168
169 /*
170 * Read from the remote system; once we're connected, we assume some
171 * data. If none arrives, we hang until the user interrupts.
172 *
173 * If we see a <CR> or a <CR> with the high bit set, treat it as
174 * a newline; if followed by a newline character, only output one
175 * newline.
176 *
177 * Otherwise, all high bits are stripped; if it isn't printable and
178 * it isn't a space, we can simply set the 7th bit. Every ASCII
179 * character with bit 7 set is printable.
180 */
181 lastc = 0;
182 if ((fp = fdopen(s, "r")) != NULL) {
183 cnt = 0;
184 line_len = 0;
185 while ((c = getc(fp)) != EOF) {
186 if (++cnt > OUTPUT_MAX) {
187 printf("\n\n Output truncated at %d bytes...\n",
188 cnt - 1);
189 break;
190 }
191 if (c == 0x0d) {
192 if (lastc == '\r') /* ^M^M - skip dupes */
193 continue;
194 c = '\n';
195 lastc = '\r';
196 } else {
197 if (!isprint(c) && !isspace(c)) {
198 c &= 0x7f;
199 c |= 0x40;
200 }
201 if (lastc != '\r' || c != '\n')
202 lastc = c;
203 else {
204 lastc = '\n';
205 continue;
206 }
207 }
208 putchar(c);
209 if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
210 putchar('\\');
211 putchar('\n');
212 lastc = '\r';
213 }
214 if (lastc == '\n' || lastc == '\r')
215 line_len = 0;
216 }
217 if (ferror(fp)) {
218 /*
219 * Assume that whatever it was set errno...
220 */
221 warn("reading from network");
222 }
223 if (lastc != '\n')
224 putchar('\n');
225
226 fclose(fp);
227 }
228 return 0;
229 }
230
231 static void
232 trying(const struct addrinfo *ai)
233 {
234 char buf[NI_MAXHOST];
235
236 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf,
237 (char *)0, 0, NI_NUMERICHOST) != 0)
238 return; /* XXX can't happen */
239
240 printf("Trying %s...\n", buf);
241 }
242
243 void
244 cleanup(int sig __unused)
245 {
246 #define ERRSTR "Timed out.\n"
247 write(STDERR_FILENO, ERRSTR, sizeof ERRSTR);
248 exit(1);
249 }
250