Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / 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.12.2.4 2002/12/16 08:41:28 roam 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 <utmp.h>
59 #include "finger.h"
60
61 extern int lflag;               /* XXX finger.h? */
62 extern int Tflag;               /* XXX finger.h? */
63 extern sa_family_t family;
64
65 static void cleanup(int sig);;
66 static int do_protocol(const char *name, const struct addrinfo *ai);
67 static void trying(const struct addrinfo *ai);
68
69 void
70 netfinger(name)
71         char *name;
72 {
73         int error, multi;
74         char *host;
75         struct addrinfo *ai, *ai0;
76         static struct addrinfo hint;
77
78         host = strrchr(name, '@');
79         if (host == 0)
80                 return;
81         *host++ = '\0';
82         signal(SIGALRM, cleanup);
83         alarm(TIME_LIMIT);
84
85         hint.ai_flags = AI_CANONNAME;
86         hint.ai_family = family;
87         hint.ai_socktype = SOCK_STREAM;
88
89         error = getaddrinfo(host, "finger", &hint, &ai0);
90         if (error) {
91                 warnx("%s: %s", host, gai_strerror(error));
92                 return;
93         }
94
95         multi = (ai0->ai_next) != 0;
96
97         /* ai_canonname may not be filled in if the user specified an IP. */
98         if (ai0->ai_canonname == 0)
99                 printf("[%s]\n", host);
100         else
101                 printf("[%s]\n", ai0->ai_canonname);
102
103         for (ai = ai0; ai != 0; ai = ai->ai_next) {
104                 if (multi)
105                         trying(ai);
106
107                 error = do_protocol(name, ai);
108                 if (!error)
109                         break;
110         }
111         alarm(0);
112         freeaddrinfo(ai0);
113 }
114
115 static int
116 do_protocol(const char *name, const struct addrinfo *ai)
117 {
118         int cnt, line_len, s;
119         FILE *fp;
120         int c, lastc;
121         struct iovec iov[3];
122         struct msghdr msg;
123         static char slash_w[] = "/W ";
124         static char neteol[] = "\r\n";
125
126         s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
127         if (s < 0) {
128                 warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
129                      ai->ai_protocol);
130                 return -1;
131         }
132
133         msg.msg_name = (void *)ai->ai_addr;
134         msg.msg_namelen = ai->ai_addrlen;
135         msg.msg_iov = iov;
136         msg.msg_iovlen = 0;
137         msg.msg_control = 0;
138         msg.msg_controllen = 0;
139         msg.msg_flags = 0;
140
141         /* -l flag for remote fingerd  */
142         if (lflag) {
143                 iov[msg.msg_iovlen].iov_base = slash_w;
144                 iov[msg.msg_iovlen++].iov_len = 3;
145         }
146         /* send the name followed by <CR><LF> */
147         iov[msg.msg_iovlen].iov_base = strdup(name);
148         iov[msg.msg_iovlen++].iov_len = strlen(name);
149         iov[msg.msg_iovlen].iov_base = neteol;
150         iov[msg.msg_iovlen++].iov_len = 2;
151
152         /*
153          * -T disables data-on-SYN: compatibility option to finger broken
154          * hosts.  Also, the implicit-open API is broken on IPv6, so do
155          * the explicit connect there, too.
156          */
157         if ((Tflag || ai->ai_addr->sa_family == AF_INET6)
158             && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
159                 warn("connect");
160                 close(s);
161                 return -1;
162         }
163
164         if (sendmsg(s, &msg, 0) < 0) {
165                 warn("sendmsg");
166                 close(s);
167                 return -1;
168         }
169
170         /*
171          * Read from the remote system; once we're connected, we assume some
172          * data.  If none arrives, we hang until the user interrupts.
173          *
174          * If we see a <CR> or a <CR> with the high bit set, treat it as
175          * a newline; if followed by a newline character, only output one
176          * newline.
177          *
178          * Otherwise, all high bits are stripped; if it isn't printable and
179          * it isn't a space, we can simply set the 7th bit.  Every ASCII
180          * character with bit 7 set is printable.
181          */
182         lastc = 0;
183         if ((fp = fdopen(s, "r")) != NULL) {
184                 cnt = 0;
185                 line_len = 0;
186                 while ((c = getc(fp)) != EOF) {
187                         if (++cnt > OUTPUT_MAX) {
188                                 printf("\n\n Output truncated at %d bytes...\n",
189                                         cnt - 1);
190                                 break;
191                         }
192                         if (c == 0x0d) {
193                                 if (lastc == '\r')      /* ^M^M - skip dupes */
194                                         continue;
195                                 c = '\n';
196                                 lastc = '\r';
197                         } else {
198                                 if (!isprint(c) && !isspace(c)) {
199                                         c &= 0x7f;
200                                         c |= 0x40;
201                                 }
202                                 if (lastc != '\r' || c != '\n')
203                                         lastc = c;
204                                 else {
205                                         lastc = '\n';
206                                         continue;
207                                 }
208                         }
209                         putchar(c);
210                         if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
211                                 putchar('\\');
212                                 putchar('\n');
213                                 lastc = '\r';
214                         }
215                         if (lastc == '\n' || lastc == '\r')
216                                 line_len = 0;
217                 }
218                 if (ferror(fp)) {
219                         /*
220                          * Assume that whatever it was set errno...
221                          */
222                         warn("reading from network");
223                 }
224                 if (lastc != '\n')
225                         putchar('\n');
226
227                 fclose(fp);
228         }
229         return 0;
230 }
231
232 static void
233 trying(const struct addrinfo *ai)
234 {
235         char buf[NI_MAXHOST];
236
237         if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf,
238                         (char *)0, 0, NI_NUMERICHOST) != 0)
239                 return;         /* XXX can't happen */
240
241         printf("Trying %s...\n", buf);
242 }
243
244 void
245 cleanup(int sig __unused)
246 {
247 #define ERRSTR  "Timed out.\n"
248         write(STDERR_FILENO, ERRSTR, sizeof ERRSTR);
249         exit(1);
250 }
251