fingerd.c revision 1.4 1 /* $NetBSD: fingerd.c,v 1.4 1997/01/20 21:01:30 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 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1983, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "from: @(#)fingerd.c 8.1 (Berkeley) 6/4/93";
45 #else
46 static char rcsid[] = "$NetBSD: fingerd.c,v 1.4 1997/01/20 21:01:30 thorpej Exp $";
47 #endif
48 #endif /* not lint */
49
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <errno.h>
55
56 #include <unistd.h>
57 #include <syslog.h>
58 #include <netdb.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <strings.h>
62 #include "pathnames.h"
63
64 void err __P((const char *, ...));
65
66 int
67 main(argc, argv)
68 int argc;
69 char *argv[];
70 {
71 register FILE *fp;
72 register int ch, ac = 2;
73 register char *lp;
74 struct hostent *hp;
75 struct sockaddr_in sin;
76 int p[2], logging, secure, user_required, short_list, sval;
77 #define ENTRIES 50
78 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
79
80 prog = _PATH_FINGER;
81 logging = secure = user_required = short_list = 0;
82 openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
83 opterr = 0;
84 while ((ch = getopt(argc, argv, "sluSmpP:")) != EOF)
85 switch (ch) {
86 case 'l':
87 logging = 1;
88 break;
89 case 'P':
90 prog = optarg;
91 break;
92 case 's':
93 secure = 1;
94 break;
95 case 'u':
96 user_required = 1;
97 break;
98 case 'S':
99 short_list = 1;
100 av[ac++] = "-s";
101 break;
102 case 'm':
103 av[ac++] = "-m";
104 break;
105 case 'p':
106 av[ac++] = "-p";
107 break;
108 case '?':
109 default:
110 err("illegal option -- %c", ch);
111 }
112
113 if (logging) {
114 sval = sizeof(sin);
115 if (getpeername(0, (struct sockaddr *)&sin, &sval) < 0)
116 err("getpeername: %s", strerror(errno));
117 if ((hp = gethostbyaddr((char *)&sin.sin_addr.s_addr,
118 sizeof(sin.sin_addr.s_addr), AF_INET)))
119 lp = hp->h_name;
120 else
121 lp = inet_ntoa(sin.sin_addr);
122 syslog(LOG_NOTICE, "query from %s", lp);
123 }
124
125 if (!fgets(line, sizeof(line), stdin))
126 exit(1);
127
128 av[ac++] = "--";
129 comp = &av[1];
130 for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
131 if ((*ap = strtok(lp, " \t\r\n")) == NULL)
132 break;
133 lp = NULL;
134 if (secure && strchr(*ap, '@')) {
135 (void) puts("fowarding service denied\r\n");
136 exit(1);
137 }
138
139 ch = strlen(*ap);
140 while ((*ap)[ch-1] == '@')
141 (*ap)[--ch] = '\0';
142 if (**ap == '\0')
143 continue;
144
145 /* RFC1196: "/[Ww]" == "-l" */
146 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
147 if (!short_list) {
148 av[1] = "-l";
149 comp = &av[0];
150 }
151 } else {
152 ap++;
153 ac++;
154 }
155 }
156 av[ENTRIES - 1] = NULL;
157
158 if ((lp = strrchr(prog, '/')))
159 *comp = ++lp;
160 else
161 *comp = prog;
162
163 if (user_required) {
164 for (ap = comp + 1; strcmp("--", *(ap++)); );
165 if (*ap == NULL) {
166 (void) puts("must provide username\r\n");
167 exit(1);
168 }
169 }
170
171 if (pipe(p) < 0)
172 err("pipe: %s", strerror(errno));
173
174 switch(vfork()) {
175 case 0:
176 (void) close(p[0]);
177 if (p[1] != 1) {
178 (void) dup2(p[1], 1);
179 (void) close(p[1]);
180 }
181 execv(prog, comp);
182 err("execv: %s: %s", prog, strerror(errno));
183 _exit(1);
184 case -1:
185 err("fork: %s", strerror(errno));
186 }
187 (void) close(p[1]);
188 if (!(fp = fdopen(p[0], "r")))
189 err("fdopen: %s", strerror(errno));
190 while ((ch = getc(fp)) != EOF) {
191 if (ch == '\n')
192 putchar('\r');
193 putchar(ch);
194 }
195 exit(0);
196 }
197
198 #if __STDC__
199 #include <stdarg.h>
200 #else
201 #include <varargs.h>
202 #endif
203
204 void
205 #if __STDC__
206 err(const char *fmt, ...)
207 #else
208 err(fmt, va_alist)
209 char *fmt;
210 va_dcl
211 #endif
212 {
213 va_list ap;
214 #if __STDC__
215 va_start(ap, fmt);
216 #else
217 va_start(ap);
218 #endif
219 (void) vsyslog(LOG_ERR, fmt, ap);
220 va_end(ap);
221 exit(1);
222 /* NOTREACHED */
223 }
224