1 /* $NetBSD: fingerd.c,v 1.27 2012/03/15 02:02:21 joerg Exp $ */
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
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "from: @(#)fingerd.c 8.1 (Berkeley) 6/4/93";
42 __RCSID("$NetBSD: fingerd.c,v 1.27 2012/03/15 02:02:21 joerg Exp $");
46 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
59 #include "pathnames.h"
61 __dead
static void my_err(const char *, ...) __printflike(1, 2);
64 main(int argc
, char *argv
[])
68 char *lp
= NULL
/* XXX gcc */;
69 struct sockaddr_storage ss
;
70 int p
[2], logging
, no_forward
, user_required
, short_list
;
73 char **ap
, *av
[ENTRIES
+ 1], **comp
, line
[1024], *prog
, *s
;
75 const char *av
[ENTRIES
+ 1], **comp
;
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
);
84 while ((ch
= getopt(argc
, argv
, "gsluShmpP:8")) != -1) {
100 av
[ac
++] = __UNCONST("-s");
103 av
[ac
++] = __UNCONST("-h");
106 av
[ac
++] = __UNCONST("-m");
109 av
[ac
++] = __UNCONST("-p");
112 av
[ac
++] = __UNCONST("-g");
115 av
[ac
++] = __UNCONST("-8");
119 my_err("illegal option -- %c", optopt
);
122 my_err("Too many options provided");
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);
135 if (!fgets(line
, sizeof(line
), stdin
)) {
137 syslog(LOG_NOTICE
, "query from %s", lp
);
140 while ((s
= strrchr(line
, '\n')) != NULL
||
141 (s
= strrchr(line
, '\r')) != NULL
)
146 syslog(LOG_NOTICE
, "query from %s", lp
);
148 syslog(LOG_NOTICE
, "query from %s: %s", lp
, line
);
152 my_err("Too many options provided");
153 av
[ac
++] = __UNCONST("--");
155 for (lp
= line
, ap
= &av
[ac
]; ac
< ENTRIES
;) {
156 if ((*ap
= strtok(lp
, " \t\r\n")) == NULL
)
159 if (no_forward
&& strchr(*ap
, '@')) {
160 (void) puts("forwarding service denied\r\n");
165 while ((*ap
)[ch
-1] == '@')
170 /* RFC1196: "/[Ww]" == "-l" */
171 if ((*ap
)[0] == '/' && ((*ap
)[1] == 'W' || (*ap
)[1] == 'w')) {
173 av
[1] = __UNCONST("-l");
181 av
[ENTRIES
- 1] = NULL
;
183 if ((lp
= strrchr(prog
, '/')))
189 for (ap
= comp
+ 1; strcmp("--", *(ap
++)); );
191 (void) puts("must provide username\r\n");
197 my_err("pipe: %s", strerror(errno
));
203 (void) dup2(p
[1], 1);
207 my_err("execv: %s: %s", prog
, strerror(errno
));
210 my_err("fork: %s", strerror(errno
));
213 if (!(fp
= fdopen(p
[0], "r")))
214 my_err("fdopen: %s", strerror(errno
));
215 while ((ch
= getc(fp
)) != EOF
) {
224 my_err(const char *fmt
, ...)
229 (void) vsyslog(LOG_ERR
, fmt
, ap
);