2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)fingerd.c 8.1 (Berkeley) 6/4/93
30 * $FreeBSD: src/libexec/fingerd/fingerd.c,v 1.26 2008/08/04 01:25:48 cperciva Exp $
31 * $DragonFly: src/libexec/fingerd/fingerd.c,v 1.3 2003/11/14 03:54:29 dillon Exp $
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netinet/tcp.h>
39 #include <arpa/inet.h>
49 #include "pathnames.h"
51 void logerr(const char *, ...) __printflike(1, 2) __dead2;
54 main(int argc, char *argv[])
59 struct sockaddr_storage ss;
61 int p[2], logging, pflag, secure;
63 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
64 char rhost[MAXHOSTNAMELEN];
67 logging = pflag = secure = 0;
68 openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
70 while ((ch = getopt(argc, argv, "lp:s")) != -1)
84 logerr("illegal option -- %c", optopt);
88 * Enable server-side Transaction TCP.
92 if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one,
94 logerr("setsockopt(TCP_NOPUSH) failed: %m");
98 if (!fgets(line, sizeof(line), stdin))
101 if (logging || pflag) {
103 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
104 logerr("getpeername: %s", strerror(errno));
105 realhostname_sa(rhost, sizeof rhost - 1,
106 (struct sockaddr *)&ss, sval);
107 rhost[sizeof(rhost) - 1] = '\0';
109 setenv("FINGERD_REMOTE_HOST", rhost, 1);
116 end = memchr(line, 0, sizeof(line));
118 if ((t = malloc(sizeof(line) + 1)) == NULL)
119 logerr("malloc: %s", strerror(errno));
120 memcpy(t, line, sizeof(line));
123 if ((t = strdup(line)) == NULL)
124 logerr("strdup: %s", strerror(errno));
126 for (end = t; *end; end++)
127 if (*end == '\n' || *end == '\r')
129 syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t);
134 for (lp = line, ap = &av[3];;) {
135 *ap = strtok(lp, " \t\r\n");
137 if (secure && ap == &av[3]) {
138 puts("must provide username\r\n");
143 if (secure && strchr(*ap, '@')) {
144 puts("forwarding service denied\r\n");
148 /* RFC742: "/[Ww]" == "-l" */
149 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
153 else if (++ap == av + ENTRIES) {
160 if ((lp = strrchr(prog, '/')) != NULL)
165 logerr("pipe: %s", strerror(errno));
170 if (p[1] != STDOUT_FILENO) {
171 dup2(p[1], STDOUT_FILENO);
174 dup2(STDOUT_FILENO, STDERR_FILENO);
177 write(STDERR_FILENO, prog, strlen(prog));
178 #define MSG ": cannot execute\n"
179 write(STDERR_FILENO, MSG, strlen(MSG));
183 logerr("fork: %s", strerror(errno));
186 if (!(fp = fdopen(p[0], "r")))
187 logerr("fdopen: %s", strerror(errno));
188 while ((ch = getc(fp)) != EOF) {
199 logerr(const char *fmt, ...)
203 vsyslog(LOG_ERR, fmt, ap);