]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - libexec/fingerd/fingerd.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)fingerd.c   8.1 (Berkeley) 6/4/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <netinet/tcp.h>
53 #include <arpa/inet.h>
54 #include <errno.h>
55
56 #include <unistd.h>
57 #include <syslog.h>
58 #include <libutil.h>
59 #include <netdb.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include "pathnames.h"
64
65 void logerr(const char *, ...) __printflike(1, 2) __dead2;
66
67 int
68 main(int argc, char *argv[])
69 {
70         FILE *fp;
71         int ch;
72         char *lp;
73         struct sockaddr_storage ss;
74         socklen_t sval;
75         int p[2], debug, kflag, logging, pflag, secure;
76 #define ENTRIES 50
77         char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
78         char rhost[MAXHOSTNAMELEN];
79
80         prog = _PATH_FINGER;
81         debug = logging = kflag = pflag = secure = 0;
82         openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
83         opterr = 0;
84         while ((ch = getopt(argc, argv, "dklp:s")) != -1)
85                 switch (ch) {
86                 case 'd':
87                         debug = 1;
88                         break;
89                 case 'k':
90                         kflag = 1;
91                         break;
92                 case 'l':
93                         logging = 1;
94                         break;
95                 case 'p':
96                         prog = optarg;
97                         pflag = 1;
98                         break;
99                 case 's':
100                         secure = 1;
101                         break;
102                 case '?':
103                 default:
104                         logerr("illegal option -- %c", optopt);
105                 }
106
107         /*
108          * Enable server-side Transaction TCP.
109          */
110         if (!debug) {
111                 int one = 1;
112                 if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one, 
113                                sizeof one) < 0) {
114                         logerr("setsockopt(TCP_NOPUSH) failed: %m");
115                 }
116         }
117
118         if (!fgets(line, sizeof(line), stdin))
119                 exit(1);
120
121         if (!debug && (logging || pflag)) {
122                 sval = sizeof(ss);
123                 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
124                         logerr("getpeername: %s", strerror(errno));
125                 realhostname_sa(rhost, sizeof rhost - 1,
126                                 (struct sockaddr *)&ss, sval);
127                 rhost[sizeof(rhost) - 1] = '\0';
128                 if (pflag)
129                         setenv("FINGERD_REMOTE_HOST", rhost, 1);
130         }
131
132         if (logging) {
133                 char *t;
134                 char *end;
135
136                 end = memchr(line, 0, sizeof(line));
137                 if (end == NULL) {
138                         if ((t = malloc(sizeof(line) + 1)) == NULL)
139                                 logerr("malloc: %s", strerror(errno));
140                         memcpy(t, line, sizeof(line));
141                         t[sizeof(line)] = 0;
142                 } else {
143                         if ((t = strdup(line)) == NULL)
144                                 logerr("strdup: %s", strerror(errno));
145                 }
146                 for (end = t; *end; end++)
147                         if (*end == '\n' || *end == '\r')
148                                 *end = ' ';
149                 syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t);
150         }
151
152         comp = &av[2];
153         av[3] = "--";
154         if (kflag)
155                 *comp-- = "-k";
156         for (lp = line, ap = &av[4];;) {
157                 *ap = strtok(lp, " \t\r\n");
158                 if (!*ap) {
159                         if (secure && ap == &av[4]) {
160                                 puts("must provide username\r\n");
161                                 exit(1);
162                         }
163                         break;
164                 }
165                 if (secure && strchr(*ap, '@')) {
166                         puts("forwarding service denied\r\n");
167                         exit(1);
168                 }
169
170                 /* RFC742: "/[Ww]" == "-l" */
171                 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
172                         *comp-- = "-l";
173                 }
174                 else if (++ap == av + ENTRIES) {
175                         *ap = NULL;
176                         break;
177                 }
178                 lp = NULL;
179         }
180
181         if ((lp = strrchr(prog, '/')) != NULL)
182                 *comp = ++lp;
183         else
184                 *comp = prog;
185         if (pipe(p) < 0)
186                 logerr("pipe: %s", strerror(errno));
187
188         if (debug) {
189                 fprintf(stderr, "%s", prog);
190                 for (ap = comp; *ap != NULL; ++ap)
191                         fprintf(stderr, " %s", *ap);
192                 fprintf(stderr, "\n");
193         }
194
195         switch(vfork()) {
196         case 0:
197                 (void)close(p[0]);
198                 if (p[1] != STDOUT_FILENO) {
199                         (void)dup2(p[1], STDOUT_FILENO);
200                         (void)close(p[1]);
201                 }
202                 dup2(STDOUT_FILENO, STDERR_FILENO);
203
204                 execv(prog, comp);
205                 write(STDERR_FILENO, prog, strlen(prog));
206 #define MSG ": cannot execute\n"
207                 write(STDERR_FILENO, MSG, strlen(MSG));
208 #undef MSG
209                 _exit(1);
210         case -1:
211                 logerr("fork: %s", strerror(errno));
212         }
213         (void)close(p[1]);
214         if (!(fp = fdopen(p[0], "r")))
215                 logerr("fdopen: %s", strerror(errno));
216         while ((ch = getc(fp)) != EOF) {
217                 if (ch == '\n')
218                         putchar('\r');
219                 putchar(ch);
220         }
221         exit(0);
222 }
223
224 #include <stdarg.h>
225
226 void
227 logerr(const char *fmt, ...)
228 {
229         va_list ap;
230         va_start(ap, fmt);
231         (void)vsyslog(LOG_ERR, fmt, ap);
232         va_end(ap);
233         exit(1);
234         /* NOTREACHED */
235 }