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