Drop main() prototype. Syncs with NetBSD-8
[minix.git] / libexec / fingerd / fingerd.c
blob9b7e7c64583a836536165a69c1c5d2c856190c95
1 /* $NetBSD: fingerd.c,v 1.27 2012/03/15 02:02:21 joerg Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "from: @(#)fingerd.c 8.1 (Berkeley) 6/4/93";
41 #else
42 __RCSID("$NetBSD: fingerd.c,v 1.27 2012/03/15 02:02:21 joerg Exp $");
43 #endif
44 #endif /* not lint */
46 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <errno.h>
52 #include <unistd.h>
53 #include <syslog.h>
54 #include <netdb.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include "pathnames.h"
61 __dead static void my_err(const char *, ...) __printflike(1, 2);
63 int
64 main(int argc, char *argv[])
66 FILE *fp;
67 int ch, ac = 2;
68 char *lp = NULL /* XXX gcc */;
69 struct sockaddr_storage ss;
70 int p[2], logging, no_forward, user_required, short_list;
71 socklen_t sval;
72 #define ENTRIES 50
73 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog, *s;
74 #if 0
75 const char *av[ENTRIES + 1], **comp;
76 const char *prog;
77 #endif
78 char hostbuf[MAXHOSTNAMELEN];
80 prog = __UNCONST(_PATH_FINGER);
81 logging = no_forward = user_required = short_list = 0;
82 openlog("fingerd", LOG_PID, LOG_DAEMON);
83 opterr = 0;
84 while ((ch = getopt(argc, argv, "gsluShmpP:8")) != -1) {
85 switch (ch) {
86 case 'l':
87 logging = 1;
88 break;
89 case 'P':
90 prog = optarg;
91 break;
92 case 's':
93 no_forward = 1;
94 break;
95 case 'u':
96 user_required = 1;
97 break;
98 case 'S':
99 short_list = 1;
100 av[ac++] = __UNCONST("-s");
101 break;
102 case 'h':
103 av[ac++] = __UNCONST("-h");
104 break;
105 case 'm':
106 av[ac++] = __UNCONST("-m");
107 break;
108 case 'p':
109 av[ac++] = __UNCONST("-p");
110 break;
111 case 'g':
112 av[ac++] = __UNCONST("-g");
113 break;
114 case '8':
115 av[ac++] = __UNCONST("-8");
116 break;
117 case '?':
118 default:
119 my_err("illegal option -- %c", optopt);
121 if (ac >= ENTRIES)
122 my_err("Too many options provided");
126 if (logging) {
127 sval = sizeof(ss);
128 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
129 my_err("getpeername: %s", strerror(errno));
130 (void)getnameinfo((struct sockaddr *)&ss, sval,
131 hostbuf, sizeof(hostbuf), NULL, 0, 0);
132 lp = hostbuf;
135 if (!fgets(line, sizeof(line), stdin)) {
136 if (logging)
137 syslog(LOG_NOTICE, "query from %s", lp);
138 exit(1);
140 while ((s = strrchr(line, '\n')) != NULL ||
141 (s = strrchr(line, '\r')) != NULL)
142 *s = '\0';
144 if (logging) {
145 if (*line == '\0')
146 syslog(LOG_NOTICE, "query from %s", lp);
147 else
148 syslog(LOG_NOTICE, "query from %s: %s", lp, line);
151 if (ac >= ENTRIES)
152 my_err("Too many options provided");
153 av[ac++] = __UNCONST("--");
154 comp = &av[1];
155 for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
156 if ((*ap = strtok(lp, " \t\r\n")) == NULL)
157 break;
158 lp = NULL;
159 if (no_forward && strchr(*ap, '@')) {
160 (void) puts("forwarding service denied\r\n");
161 exit(1);
164 ch = strlen(*ap);
165 while ((*ap)[ch-1] == '@')
166 (*ap)[--ch] = '\0';
167 if (**ap == '\0')
168 continue;
170 /* RFC1196: "/[Ww]" == "-l" */
171 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
172 if (!short_list) {
173 av[1] = __UNCONST("-l");
174 comp = &av[0];
176 } else {
177 ap++;
178 ac++;
181 av[ENTRIES - 1] = NULL;
183 if ((lp = strrchr(prog, '/')))
184 *comp = ++lp;
185 else
186 *comp = prog;
188 if (user_required) {
189 for (ap = comp + 1; strcmp("--", *(ap++)); );
190 if (*ap == NULL) {
191 (void) puts("must provide username\r\n");
192 exit(1);
196 if (pipe(p) < 0)
197 my_err("pipe: %s", strerror(errno));
199 switch(fork()) {
200 case 0:
201 (void) close(p[0]);
202 if (p[1] != 1) {
203 (void) dup2(p[1], 1);
204 (void) close(p[1]);
206 execv(prog, comp);
207 my_err("execv: %s: %s", prog, strerror(errno));
208 _exit(1);
209 case -1:
210 my_err("fork: %s", strerror(errno));
212 (void) close(p[1]);
213 if (!(fp = fdopen(p[0], "r")))
214 my_err("fdopen: %s", strerror(errno));
215 while ((ch = getc(fp)) != EOF) {
216 if (ch == '\n')
217 putchar('\r');
218 putchar(ch);
220 exit(0);
223 static void
224 my_err(const char *fmt, ...)
226 va_list ap;
228 va_start(ap, fmt);
229 (void) vsyslog(LOG_ERR, fmt, ap);
230 va_end(ap);
231 exit(1);
232 /* NOTREACHED */