fingerd.c revision 1.14 1 /* $NetBSD: fingerd.c,v 1.14 2002/04/09 00:55:15 thorpej 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.14 2002/04/09 00:55:15 thorpej 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 <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include "pathnames.h"
64
65 void err __P((const char *, ...));
66 int main __P((int, char *[]));
67
68 int
69 main(argc, argv)
70 int argc;
71 char *argv[];
72 {
73 register FILE *fp;
74 register int ch, ac = 2;
75 register char *lp = NULL /* XXX gcc */;
76 struct sockaddr_storage ss;
77 int p[2], logging, no_forward, user_required, short_list, sval;
78 #define ENTRIES 50
79 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog, *s;
80 char hostbuf[MAXHOSTNAMELEN];
81
82 prog = _PATH_FINGER;
83 logging = no_forward = user_required = short_list = 0;
84 openlog("fingerd", LOG_PID, LOG_DAEMON);
85 opterr = 0;
86 while ((ch = getopt(argc, argv, "gsluShmpP:")) != -1)
87 switch (ch) {
88 case 'l':
89 logging = 1;
90 break;
91 case 'P':
92 prog = optarg;
93 break;
94 case 's':
95 no_forward = 1;
96 break;
97 case 'u':
98 user_required = 1;
99 break;
100 case 'S':
101 short_list = 1;
102 av[ac++] = "-s";
103 break;
104 case 'h':
105 av[ac++] = "-h";
106 break;
107 case 'm':
108 av[ac++] = "-m";
109 break;
110 case 'p':
111 av[ac++] = "-p";
112 break;
113 case 'g':
114 av[ac++] = "-g";
115 break;
116 case '?':
117 default:
118 err("illegal option -- %c", ch);
119 }
120
121
122 if (logging) {
123 sval = sizeof(ss);
124 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
125 err("getpeername: %s", strerror(errno));
126 (void)getnameinfo((struct sockaddr *)&ss, sval,
127 hostbuf, sizeof(hostbuf), NULL, 0, 0);
128 lp = hostbuf;
129 }
130
131 if (!fgets(line, sizeof(line), stdin)) {
132 if (logging)
133 syslog(LOG_NOTICE, "query from %s", lp);
134 exit(1);
135 }
136 while ((s = strrchr(line, '\n')) != NULL ||
137 (s = strrchr(line, '\r')) != NULL)
138 *s = '\0';
139
140 if (logging) {
141 if (*line == '\0')
142 syslog(LOG_NOTICE, "query from %s", lp);
143 else
144 syslog(LOG_NOTICE, "query from %s: %s", lp, line);
145 }
146
147 av[ac++] = "--";
148 comp = &av[1];
149 for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
150 if ((*ap = strtok(lp, " \t\r\n")) == NULL)
151 break;
152 lp = NULL;
153 if (no_forward && strchr(*ap, '@')) {
154 (void) puts("fowarding service denied\r\n");
155 exit(1);
156 }
157
158 ch = strlen(*ap);
159 while ((*ap)[ch-1] == '@')
160 (*ap)[--ch] = '\0';
161 if (**ap == '\0')
162 continue;
163
164 /* RFC1196: "/[Ww]" == "-l" */
165 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
166 if (!short_list) {
167 av[1] = "-l";
168 comp = &av[0];
169 }
170 } else {
171 ap++;
172 ac++;
173 }
174 }
175 av[ENTRIES - 1] = NULL;
176
177 if ((lp = strrchr(prog, '/')))
178 *comp = ++lp;
179 else
180 *comp = prog;
181
182 if (user_required) {
183 for (ap = comp + 1; strcmp("--", *(ap++)); );
184 if (*ap == NULL) {
185 (void) puts("must provide username\r\n");
186 exit(1);
187 }
188 }
189
190 if (pipe(p) < 0)
191 err("pipe: %s", strerror(errno));
192
193 switch(fork()) {
194 case 0:
195 (void) close(p[0]);
196 if (p[1] != 1) {
197 (void) dup2(p[1], 1);
198 (void) close(p[1]);
199 }
200 execv(prog, comp);
201 err("execv: %s: %s", prog, strerror(errno));
202 _exit(1);
203 case -1:
204 err("fork: %s", strerror(errno));
205 }
206 (void) close(p[1]);
207 if (!(fp = fdopen(p[0], "r")))
208 err("fdopen: %s", strerror(errno));
209 while ((ch = getc(fp)) != EOF) {
210 if (ch == '\n')
211 putchar('\r');
212 putchar(ch);
213 }
214 exit(0);
215 }
216
217 #if __STDC__
218 #include <stdarg.h>
219 #else
220 #include <varargs.h>
221 #endif
222
223 void
224 #if __STDC__
225 err(const char *fmt, ...)
226 #else
227 err(fmt, va_alist)
228 char *fmt;
229 va_dcl
230 #endif
231 {
232 va_list ap;
233 #if __STDC__
234 va_start(ap, fmt);
235 #else
236 va_start(ap);
237 #endif
238 (void) vsyslog(LOG_ERR, fmt, ap);
239 va_end(ap);
240 exit(1);
241 /* NOTREACHED */
242 }
243