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