Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / libexec / fingerd / fingerd.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)fingerd.c        8.1 (Berkeley) 6/4/93
35  * $FreeBSD: src/libexec/fingerd/fingerd.c,v 1.16.2.3 2002/04/03 09:05:23 mike Exp $
36  * $DragonFly: src/libexec/fingerd/fingerd.c,v 1.3 2003/11/14 03:54:29 dillon Exp $
37  */
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
45 #include <errno.h>
46
47 #include <unistd.h>
48 #include <syslog.h>
49 #include <libutil.h>
50 #include <netdb.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <strings.h>
54 #include "pathnames.h"
55
56 void logerr (const char *, ...);
57
58 int
59 main(argc, argv)
60         int argc;
61         char *argv[];
62 {
63         register FILE *fp;
64         register int ch;
65         register char *lp;
66         struct sockaddr_storage ss;
67         int p[2], logging, secure, sval;
68 #define ENTRIES 50
69         char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
70         char rhost[MAXHOSTNAMELEN];
71
72         prog = _PATH_FINGER;
73         logging = secure = 0;
74         openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
75         opterr = 0;
76         while ((ch = getopt(argc, argv, "slp:")) != -1)
77                 switch (ch) {
78                 case 'l':
79                         logging = 1;
80                         break;
81                 case 'p':
82                         prog = optarg;
83                         break;
84                 case 's':
85                         secure = 1;
86                         break;
87                 case '?':
88                 default:
89                         logerr("illegal option -- %c", optopt);
90                 }
91
92         /*
93          * Enable server-side Transaction TCP.
94          */
95         {
96                 int one = 1;
97                 if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one, 
98                                sizeof one) < 0) {
99                         logerr("setsockopt(TCP_NOPUSH) failed: %m");
100                 }
101         }
102
103         if (!fgets(line, sizeof(line), stdin))
104                 exit(1);
105
106         if (logging) {
107                 char *t;
108                 char *end;
109
110                 end = memchr(line, 0, sizeof(line));
111                 if (end == NULL) {
112                         if ((t = malloc(sizeof(line) + 1)) == NULL)
113                                 logerr("malloc: %s", strerror(errno));
114                         memcpy(t, line, sizeof(line));
115                         t[sizeof(line)] = 0;
116                 } else {
117                         if ((t = strdup(line)) == NULL)
118                                 logerr("strdup: %s", strerror(errno));
119                 }
120                 for (end = t; *end; end++)
121                         if (*end == '\n' || *end == '\r')
122                                 *end = ' ';
123                 sval = sizeof(ss);
124                 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
125                         logerr("getpeername: %s", strerror(errno));
126                 realhostname_sa(rhost, sizeof rhost - 1,
127                                 (struct sockaddr *)&ss, sval);
128                 rhost[sizeof(rhost) - 1] = '\0';
129                 syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t);
130         }
131
132         comp = &av[1];
133         av[2] = "--";
134         for (lp = line, ap = &av[3];;) {
135                 *ap = strtok(lp, " \t\r\n");
136                 if (!*ap) {
137                         if (secure && ap == &av[3]) {
138                                 puts("must provide username\r\n");
139                                 exit(1);
140                         }
141                         break;
142                 }
143                 if (secure && strchr(*ap, '@')) {
144                         puts("forwarding service denied\r\n");
145                         exit(1);
146                 }
147
148                 /* RFC742: "/[Ww]" == "-l" */
149                 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
150                         av[1] = "-l";
151                         comp = &av[0];
152                 }
153                 else if (++ap == av + ENTRIES) {
154                         *ap = NULL;
155                         break;
156                 }
157                 lp = NULL;
158         }
159
160         if (lp = strrchr(prog, '/'))
161                 *comp = ++lp;
162         else
163                 *comp = prog;
164         if (pipe(p) < 0)
165                 logerr("pipe: %s", strerror(errno));
166
167         switch(vfork()) {
168         case 0:
169                 (void)close(p[0]);
170                 if (p[1] != 1) {
171                         (void)dup2(p[1], 1);
172                         (void)close(p[1]);
173                 }
174                 execv(prog, comp);
175                 logerr("execv: %s: %s", prog, strerror(errno));
176                 _exit(1);
177         case -1:
178                 logerr("fork: %s", strerror(errno));
179         }
180         (void)close(p[1]);
181         if (!(fp = fdopen(p[0], "r")))
182                 logerr("fdopen: %s", strerror(errno));
183         while ((ch = getc(fp)) != EOF) {
184                 if (ch == '\n')
185                         putchar('\r');
186                 putchar(ch);
187         }
188         exit(0);
189 }
190
191 #if __STDC__
192 #include <stdarg.h>
193 #else
194 #include <varargs.h>
195 #endif
196
197 void
198 #if __STDC__
199 logerr(const char *fmt, ...)
200 #else
201 logerr(fmt, va_alist)
202         char *fmt;
203         va_dcl
204 #endif
205 {
206         va_list ap;
207 #if __STDC__
208         va_start(ap, fmt);
209 #else
210         va_start(ap);
211 #endif
212         (void)vsyslog(LOG_ERR, fmt, ap);
213         va_end(ap);
214         exit(1);
215         /* NOTREACHED */
216 }