]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/fingerd/fingerd.c
This commit was generated by cvs2svn to compensate for changes in r90075,
[FreeBSD/FreeBSD.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 <strings.h>
63 #include "pathnames.h"
64
65 void logerr __P((const char *, ...));
66
67 int
68 main(argc, argv)
69         int argc;
70         char *argv[];
71 {
72         register FILE *fp;
73         register int ch;
74         register char *lp;
75         struct sockaddr_storage ss;
76         int p[2], logging, secure, sval;
77 #define ENTRIES 50
78         char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
79         char rhost[MAXHOSTNAMELEN];
80
81         prog = _PATH_FINGER;
82         logging = secure = 0;
83         openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
84         opterr = 0;
85         while ((ch = getopt(argc, argv, "slp:")) != -1)
86                 switch (ch) {
87                 case 'l':
88                         logging = 1;
89                         break;
90                 case 'p':
91                         prog = optarg;
92                         break;
93                 case 's':
94                         secure = 1;
95                         break;
96                 case '?':
97                 default:
98                         logerr("illegal option -- %c", optopt);
99                 }
100
101         /*
102          * Enable server-side Transaction TCP.
103          */
104         {
105 #if     !defined(__alpha__) /* XXX FIXME */
106                 int one = 1;
107                 if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one, 
108                                sizeof one) < 0) {
109                         logerr("setsockopt(TCP_NOPUSH) failed: %m");
110                 }
111 #endif
112         }
113
114         if (!fgets(line, sizeof(line), stdin))
115                 exit(1);
116
117         if (logging) {
118                 char *t;
119                 char *end;
120
121                 end = memchr(line, 0, sizeof(line));
122                 if (end == NULL) {
123                         if ((t = malloc(sizeof(line) + 1)) == NULL)
124                                 logerr("malloc: %s", strerror(errno));
125                         memcpy(t, line, sizeof(line));
126                         t[sizeof(line)] = 0;
127                 } else {
128                         if ((t = strdup(line)) == NULL)
129                                 logerr("strdup: %s", strerror(errno));
130                 }
131                 for (end = t; *end; end++)
132                         if (*end == '\n' || *end == '\r')
133                                 *end = ' ';
134                 sval = sizeof(ss);
135                 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
136                         logerr("getpeername: %s", strerror(errno));
137                 realhostname_sa(rhost, sizeof rhost - 1,
138                                 (struct sockaddr *)&ss, sval);
139                 rhost[sizeof(rhost) - 1] = '\0';
140                 syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t);
141         }
142
143         comp = &av[1];
144         av[2] = "--";
145         for (lp = line, ap = &av[3];;) {
146                 *ap = strtok(lp, " \t\r\n");
147                 if (!*ap) {
148                         if (secure && ap == &av[3]) {
149                                 puts("must provide username\r\n");
150                                 exit(1);
151                         }
152                         break;
153                 }
154                 if (secure && strchr(*ap, '@')) {
155                         puts("forwarding service denied\r\n");
156                         exit(1);
157                 }
158
159                 /* RFC742: "/[Ww]" == "-l" */
160                 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
161                         av[1] = "-l";
162                         comp = &av[0];
163                 }
164                 else if (++ap == av + ENTRIES) {
165                         *ap = NULL;
166                         break;
167                 }
168                 lp = NULL;
169         }
170
171         if (lp = strrchr(prog, '/'))
172                 *comp = ++lp;
173         else
174                 *comp = prog;
175         if (pipe(p) < 0)
176                 logerr("pipe: %s", strerror(errno));
177
178         switch(vfork()) {
179         case 0:
180                 (void)close(p[0]);
181                 if (p[1] != 1) {
182                         (void)dup2(p[1], 1);
183                         (void)close(p[1]);
184                 }
185                 execv(prog, comp);
186                 logerr("execv: %s: %s", prog, strerror(errno));
187                 _exit(1);
188         case -1:
189                 logerr("fork: %s", strerror(errno));
190         }
191         (void)close(p[1]);
192         if (!(fp = fdopen(p[0], "r")))
193                 logerr("fdopen: %s", strerror(errno));
194         while ((ch = getc(fp)) != EOF) {
195                 if (ch == '\n')
196                         putchar('\r');
197                 putchar(ch);
198         }
199         exit(0);
200 }
201
202 #if __STDC__
203 #include <stdarg.h>
204 #else
205 #include <varargs.h>
206 #endif
207
208 void
209 #if __STDC__
210 logerr(const char *fmt, ...)
211 #else
212 logerr(fmt, va_alist)
213         char *fmt;
214         va_dcl
215 #endif
216 {
217         va_list ap;
218 #if __STDC__
219         va_start(ap, fmt);
220 #else
221         va_start(ap);
222 #endif
223         (void)vsyslog(LOG_ERR, fmt, ap);
224         va_end(ap);
225         exit(1);
226         /* NOTREACHED */
227 }