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