fingerd.c revision 1.17 1 /* $NetBSD: fingerd.c,v 1.17 2002/09/10 03:29:02 kim Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT(
39 "@(#) Copyright (c) 1983, 1993\n\
40 The Regents of the University of California. All rights reserved.\n");
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "from: @(#)fingerd.c 8.1 (Berkeley) 6/4/93";
46 #else
47 __RCSID("$NetBSD: fingerd.c,v 1.17 2002/09/10 03:29:02 kim Exp $");
48 #endif
49 #endif /* not lint */
50
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <errno.h>
56
57 #include <unistd.h>
58 #include <syslog.h>
59 #include <netdb.h>
60 #include <stdarg.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include "pathnames.h"
65
66 void err __P((const char *, ...));
67 int main __P((int, char *[]));
68
69 int
70 main(argc, argv)
71 int argc;
72 char *argv[];
73 {
74 register FILE *fp;
75 register int ch, ac = 2;
76 register char *lp = NULL /* XXX gcc */;
77 struct sockaddr_storage ss;
78 int p[2], logging, no_forward, user_required, short_list, sval;
79 #define ENTRIES 50
80 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog, *s;
81 char hostbuf[MAXHOSTNAMELEN];
82
83 prog = _PATH_FINGER;
84 logging = no_forward = user_required = short_list = 0;
85 openlog("fingerd", LOG_PID, LOG_DAEMON);
86 opterr = 0;
87 while ((ch = getopt(argc, argv, "gsluShmpP:i")) != -1)
88 switch (ch) {
89 case 'l':
90 logging = 1;
91 break;
92 case 'P':
93 prog = optarg;
94 break;
95 case 's':
96 no_forward = 1;
97 break;
98 case 'u':
99 user_required = 1;
100 break;
101 case 'i':
102 /*
103 * This is a hack to enable single-byte 8-bit
104 * characters in the output of the default
105 * finger program. The character set is not
106 * communicated to the network client, and the
107 * exact value does not matter much as long
108 * as it enables as many 8-bit characters as
109 * possible.
110 */
111 (void) putenv("LC_CTYPE=en_US.ISO8859-15");
112 break;
113 case 'S':
114 short_list = 1;
115 av[ac++] = "-s";
116 break;
117 case 'h':
118 av[ac++] = "-h";
119 break;
120 case 'm':
121 av[ac++] = "-m";
122 break;
123 case 'p':
124 av[ac++] = "-p";
125 break;
126 case 'g':
127 av[ac++] = "-g";
128 break;
129 case '?':
130 default:
131 err("illegal option -- %c", ch);
132 }
133
134
135 if (logging) {
136 sval = sizeof(ss);
137 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
138 err("getpeername: %s", strerror(errno));
139 (void)getnameinfo((struct sockaddr *)&ss, sval,
140 hostbuf, sizeof(hostbuf), NULL, 0, 0);
141 lp = hostbuf;
142 }
143
144 if (!fgets(line, sizeof(line), stdin)) {
145 if (logging)
146 syslog(LOG_NOTICE, "query from %s", lp);
147 exit(1);
148 }
149 while ((s = strrchr(line, '\n')) != NULL ||
150 (s = strrchr(line, '\r')) != NULL)
151 *s = '\0';
152
153 if (logging) {
154 if (*line == '\0')
155 syslog(LOG_NOTICE, "query from %s", lp);
156 else
157 syslog(LOG_NOTICE, "query from %s: %s", lp, line);
158 }
159
160 av[ac++] = "--";
161 comp = &av[1];
162 for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
163 if ((*ap = strtok(lp, " \t\r\n")) == NULL)
164 break;
165 lp = NULL;
166 if (no_forward && strchr(*ap, '@')) {
167 (void) puts("forwarding service denied\r\n");
168 exit(1);
169 }
170
171 ch = strlen(*ap);
172 while ((*ap)[ch-1] == '@')
173 (*ap)[--ch] = '\0';
174 if (**ap == '\0')
175 continue;
176
177 /* RFC1196: "/[Ww]" == "-l" */
178 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
179 if (!short_list) {
180 av[1] = "-l";
181 comp = &av[0];
182 }
183 } else {
184 ap++;
185 ac++;
186 }
187 }
188 av[ENTRIES - 1] = NULL;
189
190 if ((lp = strrchr(prog, '/')))
191 *comp = ++lp;
192 else
193 *comp = prog;
194
195 if (user_required) {
196 for (ap = comp + 1; strcmp("--", *(ap++)); );
197 if (*ap == NULL) {
198 (void) puts("must provide username\r\n");
199 exit(1);
200 }
201 }
202
203 if (pipe(p) < 0)
204 err("pipe: %s", strerror(errno));
205
206 switch(fork()) {
207 case 0:
208 (void) close(p[0]);
209 if (p[1] != 1) {
210 (void) dup2(p[1], 1);
211 (void) close(p[1]);
212 }
213 execv(prog, comp);
214 err("execv: %s: %s", prog, strerror(errno));
215 _exit(1);
216 case -1:
217 err("fork: %s", strerror(errno));
218 }
219 (void) close(p[1]);
220 if (!(fp = fdopen(p[0], "r")))
221 err("fdopen: %s", strerror(errno));
222 while ((ch = getc(fp)) != EOF) {
223 if (ch == '\n')
224 putchar('\r');
225 putchar(ch);
226 }
227 exit(0);
228 }
229
230 void
231 err(const char *fmt, ...)
232 {
233 va_list ap;
234
235 va_start(ap, fmt);
236 (void) vsyslog(LOG_ERR, fmt, ap);
237 va_end(ap);
238 exit(1);
239 /* NOTREACHED */
240 }
241