Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Blame · server/socketactivation.go

Line attribution on main · 110 line(s)

54ccf66e
Keenan Nemetz
3d ago
1 package server
2
3 // Socket activation lets `vole serve` accept listeners the service manager
4 // already owns, so it can serve privileged ports without running as root. Two
5 // managers use the same contract:
6 //
7 // - systemd sets LISTEN_PID to the process id and LISTEN_FDS to a count of the
8 // file descriptors it passed, numbered from 3 upward.
9 // - launchd passes sockets named in the LaunchDaemon's Sockets dict; the
10 // matching file descriptors arrive the same way (3 upward), and the process
11 // sees LAUNCHD_SOCKET or the Listeners array naming them.
12 //
13 // Both are read, and both reduce to "up to two inherited listeners: HTTP first,
14 // then HTTPS". When they are present, serve uses them instead of binding
15 // http_addr and https_addr; when they are not, it binds exactly as a hand-run or
16 // containerized instance always has. The ACME cache, challenge handler, and TLS
17 // config do not care which descriptor is underneath them.
18
19 import (
20 "fmt"
21 "net"
22 "os"
23 "strconv"
24 "strings"
25 )
26
27 // activationEnv reads the two activation contracts and reports how many
28 // listeners the manager handed over, if any. A count of zero means the caller
29 // binds its own. It is split from the listener conversion so a test can supply a
30 // fake environment without launching launchd or systemd.
31 func activationEnv(getenv func(string) string, pid int) int {
32 owner := strings.TrimSpace(getenv("LISTEN_PID"))
33 if owner == "" {
34 // launchd does not reliably set LISTEN_PID; it names the managed sockets
35 // through LAUNCHD_SOCKET instead. When neither names an owner, accept the
36 // descriptors (systemd's sd_listen_fds does the same when LISTEN_PID is
37 // unset).
38 owner = strings.TrimSpace(getenv("LAUNCHD_SOCKET"))
39 }
40 if owner != "" && owner != strconv.Itoa(pid) {
41 // The descriptors belong to another process (or a stale value); do not
42 // touch fds we do not own.
43 return 0
44 }
45 n, err := strconv.Atoi(strings.TrimSpace(getenv("LISTEN_FDS")))
46 if err != nil || n < 0 {
47 return 0
48 }
49 return n
50 }
51
52 // listenName is the first descriptor systemd and launchd both number their
53 // sockets from.
54 const firstListenFD = 3
55
56 // inheritedListener turns one passed file descriptor into a net.Listener. The
57 // descriptor is consumed: on failure it is closed, and on success the caller
58 // owns the listener. It accepts any stream protocol (tcp under either contract)
59 // and reports the address for the startup log.
60 func inheritedListener(fd uintptr, name string) (net.Listener, error) {
61 file := os.NewFile(fd, name)
62 if file == nil {
63 return nil, fmt.Errorf("inherited fd %d is not a valid descriptor", fd)
64 }
65 ln, err := net.FileListener(file)
66 if err != nil {
67 file.Close()
68 return nil, fmt.Errorf("inherited fd %d is not a listener: %w", fd, err)
69 }
70 return ln, nil
71 }
72
73 // httpListeners returns the HTTP listener and, when ACME is on, the HTTPS
74 // listener. Inherited descriptors win over binding http_addr/https_addr, so a
75 // launchd or systemd unit that owns 80 and 443 is the only reason the process
76 // serves a privileged port without privileges.
77 func httpListeners(getenv func(string) string, pid int, httpAddr, httpsAddr string, wantTLS bool) (httpLn net.Listener, httpsLn net.Listener, err error) {
78 inherited := activationEnv(getenv, pid)
79 if inherited > 0 {
80 httpLn, err = inheritedListener(firstListenFD, "http")
81 if err != nil {
82 return nil, nil, err
83 }
84 if wantTLS {
85 if inherited < 2 {
86 httpLn.Close()
87 return nil, nil, fmt.Errorf("socket activation passed %d listener(s), need 2 for http and https", inherited)
88 }
89 httpsLn, err = inheritedListener(firstListenFD+1, "https")
90 if err != nil {
91 httpLn.Close()
92 return nil, nil, err
93 }
94 }
95 return httpLn, httpsLn, nil
96 }
97
98 httpLn, err = net.Listen("tcp", httpAddr)
99 if err != nil {
100 return nil, nil, err
101 }
102 if wantTLS {
103 httpsLn, err = net.Listen("tcp", httpsAddr)
104 if err != nil {
105 httpLn.Close()
106 return nil, nil, err
107 }
108 }
109 return httpLn, httpsLn, nil
110 }