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