File: ipv6.patch

package info (click to toggle)
popa3d 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: ansic: 2,334; sh: 106; makefile: 61
file content (386 lines) | stat: -rw-r--r-- 9,610 bytes parent folder | download
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
From: Andrew Bower <andrew@bower.uk>
Author: Jun-ichiro itojun Hagino
Origin: https://www.openwall.com/popa3d/contrib/popa3d-0.5.1-ipv6-patch-1.diff.gz
Bug-Debian: https://bugs.debian.org/1040606
Forwarded: https://github.com/openwall/popa3d/pull/3
Last-Update: 2025-08-23
Subject: Patch to support IPv6

Incidentally re-fixes use of reserved word true as variable name.
---
 popa3d.8     |  23 +++++++++++++
 standalone.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 startup.c    |  17 +++++++++-
 virtual.c    |  29 +++++++++++-----
 4 files changed, 148 insertions(+), 29 deletions(-)

diff --git a/popa3d.8 b/popa3d.8
index d7a0c3d..56a48b9 100644
--- a/popa3d.8
+++ b/popa3d.8
@@ -6,6 +6,8 @@ popa3d \- Post Office Protocol (POP3) server
 .RB [ -D ]
 .RB [ -F ]
 .RB [ -V ]
+.RB [ -4 ]
+.RB [ -6 ]
 .SH DESCRIPTION
 .B popa3d
 is a Post Office Protocol version 3 (POP3) server.
@@ -52,6 +54,27 @@ Like
 but staying in the foreground rather than becoming a daemon.
 This supports improved service integration with init systems.
 .TP
+.B -4
+When used with
+.BR -D
+or
+.BR -F ,
+listen on
+.BR 0.0.0.0 .
+.TP
+.B -6
+When used with
+.BR -D
+or
+.BR -F ,
+listen on
+.BR :: .
+If both
+.B -4
+and
+.B -6
+are combined, listen on a dual-stack socket.
+.TP
 .B -V
 Print version information and exit.
 .SH COMMANDS
diff --git a/standalone.c b/standalone.c
index 8fb8a2b..e3a9dd1 100644
--- a/standalone.c
+++ b/standalone.c
@@ -15,6 +15,7 @@
 #include <syslog.h>
 #include <time.h>
 #include <errno.h>
+#include <netdb.h>
 #include <sys/times.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -34,9 +35,20 @@ int deny_severity = SYSLOG_PRI_HI;
 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),
@@ -44,7 +56,7 @@ typedef volatile sig_atomic_t va_int;
  * information about sessions that we could have allowed to proceed.
  */
 static struct {
-	struct in_addr addr;		/* Source IP address */
+	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 */
@@ -102,6 +114,25 @@ static void check_access(int sock)
 }
 #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)
 {
@@ -110,30 +141,59 @@ int main(void)
 {
 	int foreground = 0;
 #endif
-	int true = 1;
+	int off = 0;
+	int on = 1;
 	int sock, new;
-	struct sockaddr_in addr;
+	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;
 
-	if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
+	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 *)&true, sizeof(true)))
+	    (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
 
-	memset(&addr, 0, sizeof(addr));
-	addr.sin_family = AF_INET;
-	addr.sin_addr.s_addr = inet_addr(DAEMON_ADDR);
-	addr.sin_port = htons(DAEMON_PORT);
-	if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)))
+	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");
@@ -190,6 +250,17 @@ int main(void)
  */
 		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;
 
@@ -202,9 +273,9 @@ int main(void)
 			if (sessions[i].pid ||
 			    (sessions[i].start &&
 			    now - sessions[i].start < min_delay)) {
-				if (sessions[i].addr.s_addr ==
-				    addr.sin_addr.s_addr)
-				if (++n >= MAX_SESSIONS_PER_SOURCE) break;
+				if (cmp_ip_addr(&sessions[i].addr, &peer) == 0)
+					if (++n >= MAX_SESSIONS_PER_SOURCE)
+						break;
 			} else
 			if (j < 0) j = i;
 		}
@@ -215,7 +286,7 @@ int main(void)
 			    now - sessions[i].log >= min_delay) {
 				syslog(SYSLOG_PRI_HI,
 					"%s: per source limit reached",
-					inet_ntoa(addr.sin_addr));
+					hbuf);
 				sessions[i].log = now;
 			}
 			continue;
@@ -226,7 +297,7 @@ int main(void)
 			    now < log || now - log >= min_delay) {
 				syslog(SYSLOG_PRI_HI,
 					"%s: sessions limit reached",
-					inet_ntoa(addr.sin_addr));
+					hbuf);
 				log = now;
 			}
 			continue;
@@ -234,8 +305,7 @@ int main(void)
 
 		switch ((pid = fork())) {
 		case -1:
-			syslog(SYSLOG_PRI_ERROR, "%s: fork: %m",
-				inet_ntoa(addr.sin_addr));
+			syslog(SYSLOG_PRI_ERROR, "%s: fork: %m", hbuf);
 			break;
 
 		case 0:
@@ -244,7 +314,7 @@ int main(void)
 			check_access(new);
 #endif
 			syslog(SYSLOG_PRI_LO, "Session from %s",
-				inet_ntoa(addr.sin_addr));
+				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");
@@ -252,7 +322,7 @@ int main(void)
 			return do_pop_session();
 
 		default:
-			sessions[j].addr = addr.sin_addr;
+			sessions[j].addr = peer;
 			sessions[j].pid = pid;
 			sessions[j].start = now;
 			sessions[j].log = 0;
diff --git a/startup.c b/startup.c
index a298015..3dc2630 100644
--- a/startup.c
+++ b/startup.c
@@ -6,6 +6,7 @@
 
 #if POP_OPTIONS
 
+#include <sys/socket.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -28,6 +29,9 @@ extern char *__progname;
 static char *progname;
 #endif
 
+int af = AF_UNSPEC;
+int dual_stack = 0;
+
 static void usage(void)
 {
 	fprintf(stderr, "Usage: %s [-D] [-F] [-V]\n", progname);
@@ -51,7 +55,7 @@ int main(int argc, char **argv)
 		progname = POP_SERVER;
 #endif
 
-	while ((c = getopt(argc, argv, "DFV")) != -1) {
+	while ((c = getopt(argc, argv, "DFV46")) != -1) {
 		switch (c) {
 		case 'F':
 			foreground++;
@@ -59,6 +63,17 @@ int main(int argc, char **argv)
 		case 'D':
 			standalone++;
 			break;
+		case '4':
+			if (af == AF_INET6)
+				dual_stack = 1;
+			else
+				af = AF_INET;
+			break;
+		case '6':
+			if (af == AF_INET)
+				dual_stack = 1;
+			af = AF_INET6;
+			break;
 
 		case 'V':
 			version();
diff --git a/virtual.c b/virtual.c
index bea0bb2..24a9697 100644
--- a/virtual.c
+++ b/virtual.c
@@ -6,7 +6,7 @@
 
 #if POP_VIRTUAL
 
-#define _XOPEN_SOURCE 4
+/* Inhibits NI_MAXHOST definition if present: #define _XOPEN_SOURCE 4 */
 #define _XOPEN_SOURCE_EXTENDED
 #define _XOPEN_VERSION 4
 #define _XPG4_2
@@ -22,6 +22,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <netdb.h>
 #include <arpa/inet.h>
 
 #ifndef NAME_MAX
@@ -42,18 +43,29 @@ int virtual_startup(void)
 
 static char *lookup(void)
 {
-	struct sockaddr_in sin;
+	struct sockaddr_storage ss;
 	socklen_t length;
-
-	length = sizeof(sin);
-	if (getsockname(0, (struct sockaddr *)&sin, &length)) {
-		if (errno == ENOTSOCK) return "";
+	int error;
+	static char hbuf[NI_MAXHOST];
+
+	length = sizeof(ss);
+	if (getsockname(0, (struct sockaddr *)&ss, &length)) {
+		if (errno == ENOTSOCK) {
+			hbuf[0] = '\0';
+			return hbuf;
+		}
 		log_error("getsockname");
 		return NULL;
 	}
-	if (length != sizeof(sin) || sin.sin_family != AF_INET) return NULL;
 
-	return inet_ntoa(sin.sin_addr);
+	error = getnameinfo((struct sockaddr *)&ss, length, hbuf, sizeof(hbuf),
+	    NULL, 0, NI_NUMERICHOST);
+	if (error) {
+		log_error("getnameinfo");
+		return NULL;
+	}
+
+	return hbuf;
 }
 
 static int is_valid_user(char *user)
@@ -118,7 +130,6 @@ struct passwd *virtual_userpass(char *user, char *pass, int *known)
 	}
 	free(pathname);
 
-	if (!(address = strdup(address))) return NULL;
 	virtual_domain = address;
 
 	pathname = concat(VIRTUAL_HOME_PATH, "/", address, "/",