| 1 |
/* |
| 2 |
Copyright (C) 2000-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <signal.h> |
| 22 |
#include <sys/types.h> |
| 23 |
#include <sys/socket.h> |
| 24 |
#include <netinet/in.h> |
| 25 |
#include <arpa/inet.h> |
| 26 |
#include <netdb.h> |
| 27 |
#include <string.h> |
| 28 |
#include <syslog.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <sys/wait.h> |
| 33 |
#include <errno.h> |
| 34 |
|
| 35 |
#ifdef HAVE_TCPD_H |
| 36 |
# include <tcpd.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <libinetutils.h> |
| 40 |
#include "attribute.h" |
| 41 |
|
| 42 |
int usefamily = AF_UNSPEC; /* Address family for daemon. */ |
| 43 |
|
| 44 |
static void reapchild (int); |
| 45 |
|
| 46 |
#ifndef DEFPORT |
| 47 |
# ifdef IPPORT_FTP |
| 48 |
# define DEFPORT IPPORT_FTP |
| 49 |
# else/* !IPPORT_FTP */ |
| 50 |
# define DEFPORT 21 |
| 51 |
# endif |
| 52 |
#endif /* !DEFPORT */ |
| 53 |
|
| 54 |
#ifdef WITH_WRAP |
| 55 |
|
| 56 |
# if !HAVE_DECL_HOSTS_CTL |
| 57 |
extern int hosts_ctl (char *, char *, char *, char *); |
| 58 |
# endif |
| 59 |
|
| 60 |
int allow_severity = LOG_INFO; |
| 61 |
int deny_severity = LOG_NOTICE; |
| 62 |
|
| 63 |
static int |
| 64 |
check_host (struct sockaddr *sa, socklen_t len) |
| 65 |
{ |
| 66 |
int err; |
| 67 |
char addr[INET6_ADDRSTRLEN]; |
| 68 |
char name[NI_MAXHOST]; |
| 69 |
|
| 70 |
if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6) |
| 71 |
return 1; |
| 72 |
|
| 73 |
(void) getnameinfo (sa, len, addr, sizeof (addr), NULL, 0, NI_NUMERICHOST); |
| 74 |
err = getnameinfo (sa, len, name, sizeof (name), NULL, 0, NI_NAMEREQD); |
| 75 |
if (!err) |
| 76 |
{ |
| 77 |
if (!hosts_ctl ("ftpd", name, addr, STRING_UNKNOWN)) |
| 78 |
{ |
| 79 |
syslog (deny_severity, "tcpwrappers rejected: %s [%s]", name, addr); |
| 80 |
return 0; |
| 81 |
} |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
if (!hosts_ctl ("ftpd", STRING_UNKNOWN, addr, STRING_UNKNOWN)) |
| 86 |
{ |
| 87 |
syslog (deny_severity, "tcpwrappers rejected: [%s]", addr); |
| 88 |
return 0; |
| 89 |
} |
| 90 |
} |
| 91 |
return (1); |
| 92 |
} |
| 93 |
#endif /* WITH_WRAP */ |
| 94 |
|
| 95 |
static void |
| 96 |
reapchild (int signo MAYBE_UNUSED) |
| 97 |
{ |
| 98 |
int save_errno = errno; |
| 99 |
|
| 100 |
while (waitpid (-1, NULL, WNOHANG) > 0) |
| 101 |
; |
| 102 |
errno = save_errno; |
| 103 |
} |
| 104 |
|
| 105 |
/* The parameter '*phis_addrlen' must be initiated |
| 106 |
with the space available at calling time. |
| 107 |
The size of used space will then be returned. |
| 108 |
*/ |
| 109 |
int |
| 110 |
server_mode (const char *pidfile, struct sockaddr *phis_addr, |
| 111 |
socklen_t *phis_addrlen, char *argv[]) |
| 112 |
{ |
| 113 |
int ctl_sock, fd; |
| 114 |
struct servent *sv; |
| 115 |
int port, err; |
| 116 |
char portstr[8]; |
| 117 |
socklen_t saved_addrlen = *phis_addrlen; |
| 118 |
struct addrinfo hints, *res, *ai; |
| 119 |
|
| 120 |
/* Become a daemon. */ |
| 121 |
if (daemon (1, 1) < 0) |
| 122 |
{ |
| 123 |
syslog (LOG_ERR, "failed to become a daemon"); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
signal (SIGCHLD, reapchild); |
| 127 |
|
| 128 |
/* Get port for ftp/tcp. */ |
| 129 |
sv = getservbyname ("ftp", "tcp"); |
| 130 |
port = (sv == NULL) ? DEFPORT : ntohs (sv->s_port); |
| 131 |
snprintf (portstr, sizeof (portstr), "%u", port); |
| 132 |
|
| 133 |
memset (&hints, 0, sizeof (hints)); |
| 134 |
hints.ai_socktype = SOCK_STREAM; |
| 135 |
hints.ai_flags = AI_PASSIVE; |
| 136 |
|
| 137 |
/* If an undetermined address family is passed, |
| 138 |
* we build a dual stacked listener from an AF_INET6 |
| 139 |
* socket by unsetting the sockopt IPV6_V6ONLY. |
| 140 |
* Otherwise the resolver often prefers AF_INET. |
| 141 |
*/ |
| 142 |
hints.ai_family = (usefamily != AF_UNSPEC) ? usefamily : AF_INET6; |
| 143 |
|
| 144 |
err = getaddrinfo (NULL, portstr, &hints, &res); |
| 145 |
if (err) |
| 146 |
{ |
| 147 |
syslog (LOG_ERR, "control socket: %s", gai_strerror (err)); |
| 148 |
return -1; |
| 149 |
} |
| 150 |
/* Attempt to open socket, bind and start listen. */ |
| 151 |
for (ai = res; ai; ai = ai->ai_next) |
| 152 |
{ |
| 153 |
ctl_sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 154 |
if (ctl_sock < 0) |
| 155 |
continue; |
| 156 |
|
| 157 |
/* Enable local address reuse. */ |
| 158 |
{ |
| 159 |
int on = 1; |
| 160 |
if (setsockopt (ctl_sock, SOL_SOCKET, SO_REUSEADDR, |
| 161 |
(char *) &on, sizeof (on)) < 0) |
| 162 |
syslog (LOG_ERR, "control setsockopt: %m"); |
| 163 |
} |
| 164 |
|
| 165 |
/* Upgrade to dual stacked socket. */ |
| 166 |
if (usefamily == AF_UNSPEC && ai->ai_family == AF_INET6) |
| 167 |
{ |
| 168 |
int off = 0; |
| 169 |
if (setsockopt (ctl_sock, IPPROTO_IPV6, IPV6_V6ONLY, |
| 170 |
(char *) &off, sizeof (off)) < 0) |
| 171 |
syslog (LOG_DEBUG, "setsockopt bindv6only: %m"); |
| 172 |
} |
| 173 |
|
| 174 |
if (bind (ctl_sock, ai->ai_addr, ai->ai_addrlen)) |
| 175 |
{ |
| 176 |
close (ctl_sock); |
| 177 |
ctl_sock = -1; |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
if (listen (ctl_sock, 32) < 0) |
| 182 |
{ |
| 183 |
close (ctl_sock); |
| 184 |
ctl_sock = -1; |
| 185 |
continue; |
| 186 |
} |
| 187 |
|
| 188 |
/* Accept the first choice! */ |
| 189 |
break; |
| 190 |
} |
| 191 |
|
| 192 |
if (res) |
| 193 |
freeaddrinfo (res); |
| 194 |
|
| 195 |
if (ai == NULL) |
| 196 |
{ |
| 197 |
syslog (LOG_ERR, "control socket: %m"); |
| 198 |
return -1; |
| 199 |
} |
| 200 |
|
| 201 |
/* Stash pid in pidfile. */ |
| 202 |
{ |
| 203 |
FILE *pid_fp = fopen (pidfile, "w"); |
| 204 |
if (pid_fp == NULL) |
| 205 |
syslog (LOG_ERR, "can't open %s: %m", PATH_FTPDPID); |
| 206 |
else |
| 207 |
{ |
| 208 |
fprintf (pid_fp, "%d\n", (int) getpid ()); |
| 209 |
fchmod (fileno (pid_fp), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 210 |
fclose (pid_fp); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/* Loop forever accepting connection requests and forking off |
| 215 |
children to handle them. */ |
| 216 |
while (1) |
| 217 |
{ |
| 218 |
*phis_addrlen = saved_addrlen; |
| 219 |
fd = accept (ctl_sock, phis_addr, phis_addrlen); |
| 220 |
if (fork () == 0) /* child */ |
| 221 |
{ |
| 222 |
dup2 (fd, 0); |
| 223 |
dup2 (fd, 1); |
| 224 |
close (ctl_sock); |
| 225 |
break; |
| 226 |
} |
| 227 |
close (fd); |
| 228 |
} |
| 229 |
|
| 230 |
#ifdef WITH_WRAP |
| 231 |
/* In the child. */ |
| 232 |
if (!check_host (phis_addr, *phis_addrlen)) |
| 233 |
return -1; |
| 234 |
#endif |
| 235 |
|
| 236 |
#ifndef HAVE_FORK |
| 237 |
_exit (execvp (argv[0], argv)); |
| 238 |
#else |
| 239 |
(void) argv; /* Silence warnings. */ |
| 240 |
#endif |
| 241 |
|
| 242 |
return fd; |
| 243 |
} |