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. 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.
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
33 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)fingerd.c 8.1 (Berkeley) 6/4/93
35 * $FreeBSD: src/libexec/fingerd/fingerd.c,v 1.16.2.3 2002/04/03 09:05:23 mike Exp $
36 * $DragonFly: src/libexec/fingerd/fingerd.c,v 1.3 2003/11/14 03:54:29 dillon Exp $
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
54 #include "pathnames.h"
56 void logerr (const char *, ...);
66 struct sockaddr_storage ss
;
67 int p
[2], logging
, secure
, sval
;
69 char **ap
, *av
[ENTRIES
+ 1], **comp
, line
[1024], *prog
;
70 char rhost
[MAXHOSTNAMELEN
];
74 openlog("fingerd", LOG_PID
| LOG_CONS
, LOG_DAEMON
);
76 while ((ch
= getopt(argc
, argv
, "slp:")) != -1)
89 logerr("illegal option -- %c", optopt
);
93 * Enable server-side Transaction TCP.
97 if (setsockopt(STDOUT_FILENO
, IPPROTO_TCP
, TCP_NOPUSH
, &one
,
99 logerr("setsockopt(TCP_NOPUSH) failed: %m");
103 if (!fgets(line
, sizeof(line
), stdin
))
110 end
= memchr(line
, 0, sizeof(line
));
112 if ((t
= malloc(sizeof(line
) + 1)) == NULL
)
113 logerr("malloc: %s", strerror(errno
));
114 memcpy(t
, line
, sizeof(line
));
117 if ((t
= strdup(line
)) == NULL
)
118 logerr("strdup: %s", strerror(errno
));
120 for (end
= t
; *end
; end
++)
121 if (*end
== '\n' || *end
== '\r')
124 if (getpeername(0, (struct sockaddr
*)&ss
, &sval
) < 0)
125 logerr("getpeername: %s", strerror(errno
));
126 realhostname_sa(rhost
, sizeof rhost
- 1,
127 (struct sockaddr
*)&ss
, sval
);
128 rhost
[sizeof(rhost
) - 1] = '\0';
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
, '/'))
165 logerr("pipe: %s", strerror(errno
));
175 logerr("execv: %s: %s", prog
, strerror(errno
));
178 logerr("fork: %s", strerror(errno
));
181 if (!(fp
= fdopen(p
[0], "r")))
182 logerr("fdopen: %s", strerror(errno
));
183 while ((ch
= getc(fp
)) != EOF
) {
199 logerr(const char *fmt
, ...)
201 logerr(fmt
, va_alist
)
212 (void)vsyslog(LOG_ERR
, fmt
, ap
);