From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 5/6] daemon: allow listening on well-known ports based on protocol
Date: Mon, 1 Aug 2022 21:24:46 +0000 [thread overview]
Message-ID: <20220801212447.270000-6-e@80x24.org> (raw)
In-Reply-To: <20220801212447.270000-1-e@80x24.org>
This allows admins to use "-l nntp://0.0.0.0/" to bind on port 119
without specifying ":119" on the CLI.
---
lib/PublicInbox/Daemon.pm | 30 ++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index 3264bb6c..ead5afc0 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -35,6 +35,9 @@ my ($uid, $gid);
my ($default_cert, $default_key);
my %KNOWN_TLS = (443 => 'https', 563 => 'nntps', 993 => 'imaps', 995 =>'pop3s');
my %KNOWN_STARTTLS = (110 => 'pop3', 119 => 'nntp', 143 => 'imap');
+my %SCHEME2PORT = map { $KNOWN_TLS{$_} => $_ + 0 } keys %KNOWN_TLS;
+for (keys %KNOWN_STARTTLS) { $SCHEME2PORT{$KNOWN_STARTTLS{$_}} = $_ + 0 }
+$SCHEME2PORT{http} = 80;
sub listener_opt ($) {
my ($str) = @_; # opt1=val1,opt2=val2 (opt may repeat for multi-value)
@@ -103,9 +106,10 @@ sub open_log_path ($$) { # my ($fh, $path) = @_; # $_[0] is modified
do_chown($_[1]);
}
-sub load_mod ($;$) {
- my ($scheme, $opt) = @_;
+sub load_mod ($;$$) {
+ my ($scheme, $opt, $addr) = @_;
my $modc = "PublicInbox::\U$scheme";
+ $modc =~ s/S\z//;
my $mod = $modc.'D';
eval "require $mod"; # IMAPD|HTTPD|NNTPD|POP3D
die $@ if $@;
@@ -200,11 +204,17 @@ EOF
foreach my $l (@cfg_listen) {
my $orig = $l;
my $scheme = '';
- if ($l =~ s!\A([^:]+)://!!) {
- $scheme = $1;
- } elsif ($l =~ /\A(?:\[[^\]]+\]|[^:]+):([0-9])+/) {
- my $s = $KNOWN_TLS{$1} // $KNOWN_STARTTLS{$1};
- $scheme = $s if defined $s;
+ my $port;
+ if ($l =~ s!\A([^:]+)://!!) { $scheme = $1 }
+ if ($l =~ /\A(?:\[[^\]]+\]|[^:]+):([0-9]+)/) {
+ $port = $1 + 0;
+ my $s = $KNOWN_TLS{$port} // $KNOWN_STARTTLS{$port};
+ $scheme //= $s if defined $s;
+ } elsif (index($l, '/') != 0) { # unix socket
+ $port //= $SCHEME2PORT{$scheme} if $scheme;
+ $port // die "no port in listen=$l\n";
+ $l =~ s!\A([^/]+)!$1:$port! or
+ die "unable to add port=$port to $l\n";
}
my $opt; # non-TLS options
if ($l =~ s!/?\?(.+)\z!!) {
@@ -215,8 +225,8 @@ EOF
} elsif ($scheme =~ /\A(?:https|imaps|nntps|pop3s)\z/) {
die "$orig specified w/o cert=\n";
}
- $scheme =~ /\A(http|imap|nntp|pop3)/ and
- $xnetd->{$l} = load_mod($1, $opt);
+ $scheme =~ /\A(?:http|imap|nntp|pop3)/ and
+ $xnetd->{$l} = load_mod($scheme, $opt, $l);
next if $listener_names->{$l}; # already inherited
my (%o, $sock_pkg);
@@ -263,7 +273,7 @@ EOF
for my $sockname (@inherited_names) {
$sockname =~ /:([0-9]+)\z/ or next;
if (my $scheme = $KNOWN_TLS{$1}) {
- $xnetd->{$sockname} = load_mod(substr($scheme, 0, -1));
+ $xnetd->{$sockname} = load_mod($scheme);
$tls_opt{"$scheme://$sockname"} ||= accept_tls_opt('');
} elsif (($scheme = $KNOWN_STARTTLS{$1})) {
$xnetd->{$sockname} = load_mod($scheme);
next prev parent reply other threads:[~2022-08-01 21:24 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-01 21:24 [PATCH 0/6] flesh out more -netd funcionality Eric Wong
2022-08-01 21:24 ` [PATCH 1/6] httpd: make internals slightly more generic Eric Wong
2022-08-01 21:24 ` [PATCH 2/6] daemon: support per-listener env, .psgi, out, err Eric Wong
2022-08-01 21:24 ` [PATCH 3/6] daemon: require absolute cert/key paths with --daemonize Eric Wong
2022-08-01 21:24 ` [PATCH 4/6] daemon: add diagnostics about inherited/bound listeners Eric Wong
2022-08-01 21:24 ` Eric Wong [this message]
2022-08-01 21:24 ` [PATCH 6/6] daemon: share FDs for identical log paths Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220801212447.270000-6-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).