1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
* Standalone POP server: accepts connections, checks the anti-flood limits,
* logs and starts the actual POP sessions.
*/
#include "params.h"
#if POP_STANDALONE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <time.h>
#include <errno.h>
#include <netdb.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#if DAEMON_LIBWRAP
#include <tcpd.h>
int allow_severity = SYSLOG_PRI_LO;
int deny_severity = SYSLOG_PRI_HI;
#endif
/*
* These are defined in pop_root.c.
*/
extern int log_error(char *s);
extern int do_pop_startup(void);
extern int do_pop_session(void);
extern int af;
extern int dual_stack;
typedef volatile sig_atomic_t va_int;
struct ip_addr {
sa_family_t af;
socklen_t len;
union {
struct in_addr sin_addr;
struct in6_addr sin6_addr;
} a;
};
/*
* Active POP sessions. Those that were started within the last MIN_DELAY
* seconds are also considered active (regardless of their actual state),
* to allow for limiting the logging rate without throwing away critical
* information about sessions that we could have allowed to proceed.
*/
static struct {
struct ip_addr addr; /* Source IP address */
volatile int pid; /* PID of the server, or 0 for none */
clock_t start; /* When the server was started */
clock_t log; /* When we've last logged a failure */
} sessions[MAX_SESSIONS];
static va_int child_blocked; /* We use blocking to avoid races */
static va_int child_pending; /* Are any dead children waiting? */
/*
* SIGCHLD handler.
*/
static void handle_child(int signum)
{
int saved_errno;
int pid;
int i;
saved_errno = errno;
if (child_blocked)
child_pending = 1;
else {
child_pending = 0;
while ((pid = waitpid(0, NULL, WNOHANG)) > 0)
for (i = 0; i < MAX_SESSIONS; i++)
if (sessions[i].pid == pid) {
sessions[i].pid = 0;
break;
}
}
signal(SIGCHLD, handle_child);
errno = saved_errno;
}
#if DAEMON_LIBWRAP
static void check_access(int sock)
{
struct request_info request;
request_init(&request,
RQ_DAEMON, DAEMON_LIBWRAP_IDENT,
RQ_FILE, sock,
0);
fromhost(&request);
if (!hosts_access(&request)) {
/* refuse() shouldn't return... */
refuse(&request);
/* ...but just in case */
exit(1);
}
}
#endif
static void save_ip_addr(struct ip_addr *to,
const struct sockaddr *from)
{
to->af = from->sa_family;
if (to->af == AF_INET6) {
to->a.sin6_addr = ((struct sockaddr_in6 *) from)->sin6_addr;
to->len = sizeof to->a.sin6_addr;
} else {
to->a.sin_addr = ((struct sockaddr_in *) from)->sin_addr;
to->len = sizeof to->a.sin_addr;
}
}
static int cmp_ip_addr(const struct ip_addr *a,
const struct ip_addr *b)
{
return memcmp(a, b, ((size_t)&((struct ip_addr *)(0))->a) + a->len);
}
#if POP_OPTIONS
int do_standalone(int foreground)
{
#else
int main(void)
{
int foreground = 0;
#endif
int off = 0;
int on = 1;
int sock, new;
struct sockaddr_storage addr;
socklen_t addrlen;
struct ip_addr peer;
int pid;
struct tms buf;
clock_t min_delay, now, log;
int i, j, n;
struct addrinfo hints, *res;
char hbuf[NI_MAXHOST];
char sbuf[NI_MAXSERV];
int error;
if (do_pop_startup()) return 1;
snprintf(sbuf, sizeof(sbuf), "%u", DAEMON_PORT);
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = af;
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(NULL, sbuf, &hints, &res);
if (error)
return log_error("getaddrinfo");
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock < 0) {
freeaddrinfo(res);
return log_error("socket");
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(void *)&on, sizeof(on))) {
freeaddrinfo(res);
return log_error("setsockopt");
}
#ifdef IPV6_V6ONLY
if (res->ai_family == AF_INET6 &&
(af == AF_INET6 || dual_stack) &&
setsockopt(sock, IPPROTO_IPV6,
IPV6_V6ONLY, (void *)(dual_stack ? &off : &on), sizeof(on))) {
freeaddrinfo(res);
return log_error("setsockopt");
}
#endif
if (bind(sock, res->ai_addr, res->ai_addrlen)) {
freeaddrinfo(res);
return log_error("bind");
}
freeaddrinfo(res);
if (listen(sock, MAX_BACKLOG))
return log_error("listen");
chdir("/");
if (!foreground) {
setsid();
switch (fork()) {
case -1:
return log_error("fork");
case 0:
break;
default:
return 0;
}
setsid();
}
#if defined(_SC_CLK_TCK) || !defined(CLK_TCK)
min_delay = MIN_DELAY * sysconf(_SC_CLK_TCK);
#else
min_delay = MIN_DELAY * CLK_TCK;
#endif
child_blocked = 1;
child_pending = 0;
signal(SIGCHLD, handle_child);
memset((void *)sessions, 0, sizeof(sessions));
log = 0;
new = 0;
while (1) {
child_blocked = 0;
if (child_pending) raise(SIGCHLD);
if (new > 0)
if (close(new)) return log_error("close");
addrlen = sizeof(addr);
new = accept(sock, (struct sockaddr *)&addr, &addrlen);
/*
* I wish there were a portable way to classify errno's... In this case,
* it appears to be better to risk eating up the CPU on a fatal error
* rather than risk terminating the entire service because of a minor
* temporary error having to do with one particular connection attempt.
*/
if (new < 0) continue;
error = getnameinfo((struct sockaddr *)&addr, addrlen,
hbuf, sizeof(hbuf),
NULL, 0, NI_NUMERICHOST);
if (error) {
log_error("getnameinfo");
/* If rendering the numerical address failed,
* no good can come of accepting it! */
continue;
}
save_ip_addr(&peer, (struct sockaddr *) &addr);
now = times(&buf);
if (!now) now = 1;
child_blocked = 1;
j = -1; n = 0;
for (i = 0; i < MAX_SESSIONS; i++) {
if (sessions[i].start > now)
sessions[i].start = 0;
if (sessions[i].pid ||
(sessions[i].start &&
now - sessions[i].start < min_delay)) {
if (cmp_ip_addr(&sessions[i].addr, &peer) == 0)
if (++n >= MAX_SESSIONS_PER_SOURCE)
break;
} else
if (j < 0) j = i;
}
if (n >= MAX_SESSIONS_PER_SOURCE) {
if (!sessions[i].log ||
now < sessions[i].log ||
now - sessions[i].log >= min_delay) {
syslog(SYSLOG_PRI_HI,
"%s: per source limit reached",
hbuf);
sessions[i].log = now;
}
continue;
}
if (j < 0) {
if (!log ||
now < log || now - log >= min_delay) {
syslog(SYSLOG_PRI_HI,
"%s: sessions limit reached",
hbuf);
log = now;
}
continue;
}
switch ((pid = fork())) {
case -1:
syslog(SYSLOG_PRI_ERROR, "%s: fork: %m", hbuf);
break;
case 0:
if (close(sock)) return log_error("close");
#if DAEMON_LIBWRAP
check_access(new);
#endif
syslog(SYSLOG_PRI_LO, "Session from %s",
hbuf);
if (dup2(new, 0) < 0) return log_error("dup2");
if (dup2(new, 1) < 0) return log_error("dup2");
if (dup2(new, 2) < 0) return log_error("dup2");
if (close(new)) return log_error("close");
return do_pop_session();
default:
sessions[j].addr = peer;
sessions[j].pid = pid;
sessions[j].start = now;
sessions[j].log = 0;
}
}
}
#endif
|