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