fingerd.c revision 1.5 1 1.5 mrg /* $NetBSD: fingerd.c,v 1.5 1997/09/09 02:40:43 mrg 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.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.3 explorer static char copyright[] =
38 1.3 explorer "@(#) Copyright (c) 1983, 1993\n\
39 1.3 explorer The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.4 thorpej #if 0
44 1.4 thorpej static char sccsid[] = "from: @(#)fingerd.c 8.1 (Berkeley) 6/4/93";
45 1.4 thorpej #else
46 1.5 mrg static char rcsid[] = "$NetBSD: fingerd.c,v 1.5 1997/09/09 02:40:43 mrg Exp $";
47 1.4 thorpej #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.3 explorer #include <sys/types.h>
51 1.3 explorer #include <sys/socket.h>
52 1.3 explorer #include <netinet/in.h>
53 1.3 explorer #include <arpa/inet.h>
54 1.3 explorer #include <errno.h>
55 1.3 explorer
56 1.3 explorer #include <unistd.h>
57 1.3 explorer #include <syslog.h>
58 1.3 explorer #include <netdb.h>
59 1.1 cgd #include <stdio.h>
60 1.3 explorer #include <stdlib.h>
61 1.3 explorer #include <strings.h>
62 1.1 cgd #include "pathnames.h"
63 1.1 cgd
64 1.3 explorer void err __P((const char *, ...));
65 1.3 explorer
66 1.3 explorer int
67 1.3 explorer main(argc, argv)
68 1.3 explorer int argc;
69 1.3 explorer char *argv[];
70 1.1 cgd {
71 1.1 cgd register FILE *fp;
72 1.3 explorer register int ch, ac = 2;
73 1.1 cgd register char *lp;
74 1.3 explorer struct hostent *hp;
75 1.3 explorer struct sockaddr_in sin;
76 1.3 explorer int p[2], logging, secure, user_required, short_list, sval;
77 1.1 cgd #define ENTRIES 50
78 1.3 explorer char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
79 1.1 cgd
80 1.3 explorer prog = _PATH_FINGER;
81 1.3 explorer logging = secure = user_required = short_list = 0;
82 1.3 explorer openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
83 1.3 explorer opterr = 0;
84 1.5 mrg while ((ch = getopt(argc, argv, "gsluSmpP:")) != EOF)
85 1.3 explorer switch (ch) {
86 1.3 explorer case 'l':
87 1.3 explorer logging = 1;
88 1.3 explorer break;
89 1.3 explorer case 'P':
90 1.3 explorer prog = optarg;
91 1.3 explorer break;
92 1.3 explorer case 's':
93 1.3 explorer secure = 1;
94 1.3 explorer break;
95 1.3 explorer case 'u':
96 1.3 explorer user_required = 1;
97 1.3 explorer break;
98 1.3 explorer case 'S':
99 1.3 explorer short_list = 1;
100 1.3 explorer av[ac++] = "-s";
101 1.3 explorer break;
102 1.3 explorer case 'm':
103 1.3 explorer av[ac++] = "-m";
104 1.3 explorer break;
105 1.3 explorer case 'p':
106 1.3 explorer av[ac++] = "-p";
107 1.5 mrg break;
108 1.5 mrg case 'g':
109 1.5 mrg av[ac++] = "-g";
110 1.3 explorer break;
111 1.3 explorer case '?':
112 1.3 explorer default:
113 1.3 explorer err("illegal option -- %c", ch);
114 1.3 explorer }
115 1.1 cgd
116 1.3 explorer if (logging) {
117 1.3 explorer sval = sizeof(sin);
118 1.3 explorer if (getpeername(0, (struct sockaddr *)&sin, &sval) < 0)
119 1.3 explorer err("getpeername: %s", strerror(errno));
120 1.3 explorer if ((hp = gethostbyaddr((char *)&sin.sin_addr.s_addr,
121 1.3 explorer sizeof(sin.sin_addr.s_addr), AF_INET)))
122 1.3 explorer lp = hp->h_name;
123 1.3 explorer else
124 1.3 explorer lp = inet_ntoa(sin.sin_addr);
125 1.3 explorer syslog(LOG_NOTICE, "query from %s", lp);
126 1.3 explorer }
127 1.1 cgd
128 1.1 cgd if (!fgets(line, sizeof(line), stdin))
129 1.1 cgd exit(1);
130 1.3 explorer
131 1.3 explorer av[ac++] = "--";
132 1.3 explorer comp = &av[1];
133 1.3 explorer for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
134 1.3 explorer if ((*ap = strtok(lp, " \t\r\n")) == NULL)
135 1.1 cgd break;
136 1.1 cgd lp = NULL;
137 1.3 explorer if (secure && strchr(*ap, '@')) {
138 1.3 explorer (void) puts("fowarding service denied\r\n");
139 1.3 explorer exit(1);
140 1.3 explorer }
141 1.3 explorer
142 1.3 explorer ch = strlen(*ap);
143 1.3 explorer while ((*ap)[ch-1] == '@')
144 1.3 explorer (*ap)[--ch] = '\0';
145 1.3 explorer if (**ap == '\0')
146 1.3 explorer continue;
147 1.3 explorer
148 1.3 explorer /* RFC1196: "/[Ww]" == "-l" */
149 1.3 explorer if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
150 1.3 explorer if (!short_list) {
151 1.3 explorer av[1] = "-l";
152 1.3 explorer comp = &av[0];
153 1.3 explorer }
154 1.3 explorer } else {
155 1.3 explorer ap++;
156 1.3 explorer ac++;
157 1.3 explorer }
158 1.3 explorer }
159 1.3 explorer av[ENTRIES - 1] = NULL;
160 1.3 explorer
161 1.3 explorer if ((lp = strrchr(prog, '/')))
162 1.3 explorer *comp = ++lp;
163 1.3 explorer else
164 1.3 explorer *comp = prog;
165 1.3 explorer
166 1.3 explorer if (user_required) {
167 1.3 explorer for (ap = comp + 1; strcmp("--", *(ap++)); );
168 1.3 explorer if (*ap == NULL) {
169 1.3 explorer (void) puts("must provide username\r\n");
170 1.3 explorer exit(1);
171 1.3 explorer }
172 1.1 cgd }
173 1.1 cgd
174 1.1 cgd if (pipe(p) < 0)
175 1.3 explorer err("pipe: %s", strerror(errno));
176 1.1 cgd
177 1.3 explorer switch(vfork()) {
178 1.1 cgd case 0:
179 1.3 explorer (void) close(p[0]);
180 1.1 cgd if (p[1] != 1) {
181 1.3 explorer (void) dup2(p[1], 1);
182 1.3 explorer (void) close(p[1]);
183 1.1 cgd }
184 1.3 explorer execv(prog, comp);
185 1.3 explorer err("execv: %s: %s", prog, strerror(errno));
186 1.1 cgd _exit(1);
187 1.1 cgd case -1:
188 1.3 explorer err("fork: %s", strerror(errno));
189 1.1 cgd }
190 1.3 explorer (void) close(p[1]);
191 1.1 cgd if (!(fp = fdopen(p[0], "r")))
192 1.3 explorer err("fdopen: %s", strerror(errno));
193 1.1 cgd while ((ch = getc(fp)) != EOF) {
194 1.1 cgd if (ch == '\n')
195 1.1 cgd putchar('\r');
196 1.1 cgd putchar(ch);
197 1.1 cgd }
198 1.1 cgd exit(0);
199 1.1 cgd }
200 1.1 cgd
201 1.3 explorer #if __STDC__
202 1.3 explorer #include <stdarg.h>
203 1.3 explorer #else
204 1.3 explorer #include <varargs.h>
205 1.3 explorer #endif
206 1.3 explorer
207 1.3 explorer void
208 1.3 explorer #if __STDC__
209 1.3 explorer err(const char *fmt, ...)
210 1.3 explorer #else
211 1.3 explorer err(fmt, va_alist)
212 1.3 explorer char *fmt;
213 1.3 explorer va_dcl
214 1.3 explorer #endif
215 1.1 cgd {
216 1.3 explorer va_list ap;
217 1.3 explorer #if __STDC__
218 1.3 explorer va_start(ap, fmt);
219 1.3 explorer #else
220 1.3 explorer va_start(ap);
221 1.3 explorer #endif
222 1.3 explorer (void) vsyslog(LOG_ERR, fmt, ap);
223 1.3 explorer va_end(ap);
224 1.1 cgd exit(1);
225 1.3 explorer /* NOTREACHED */
226 1.1 cgd }
227