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 · internal/sshdconfig/sshdconfig.go

Line attribution on main · 217 line(s)

09610c0f
Keenan Nemetz
16d ago
1 // Package sshdconfig renders the sshd drop-in that puts the system sshd in front
2 // of Vole.
3 //
883ffcc9
Keenan Nemetz
12d ago
4 // It exists so one function decides what the files say. Two are shipped:
5 // packaging/sshd/vole-linux.conf, installed as /usr/share/vole/sshd/vole.conf,
6 // and packaging/sshd/vole-darwin.conf for macOS. A test compares each with
7 // Render(LinuxDefaults()) and Render(DarwinDefaults()), so the Match block, the
8 // account sshd runs, the binary path, and the config path cannot drift from what
9 // `vole ssh-authorized-keys` is given. The platform changes the commands in the
10 // header, the paths, and the accounts; the Match block is the same on both.
09610c0f
Keenan Nemetz
16d ago
11 package sshdconfig
12
13 import (
14 "fmt"
15 "strings"
16 )
17
883ffcc9
Keenan Nemetz
12d ago
18 // Options are the things the drop-in names.
09610c0f
Keenan Nemetz
16d ago
19 type Options struct {
883ffcc9
Keenan Nemetz
12d ago
20 // Platform selects the commands and defaults in the header: "linux" or
21 // "darwin". It is the platform sshd runs on, not the one that rendered the
22 // file.
23 Platform string
24 // Destination is where the operator will put this file; the header's Include
25 // line names it.
26 Destination string
09610c0f
Keenan Nemetz
16d ago
27 // User is the operating-system account sshd runs the forced command as, and
28 // the account in generated clone URLs (ssh_user).
29 User string
883ffcc9
Keenan Nemetz
12d ago
30 // ServiceUser is the account sshd runs AuthorizedKeysCommand as. It reads the
31 // roster and mints a session token, so it is the account Vole itself runs as.
32 ServiceUser string
33 // Shell is the login shell of User: the wrapper that recognises the one
34 // command sshd is allowed to run.
35 Shell string
36 // Socket is the front door's Unix socket, printed as ssh_socket.
37 Socket string
09610c0f
Keenan Nemetz
16d ago
38 // Vole is the absolute path to the binary sshd runs. It is the same binary
39 // whose ssh-exec becomes the forced command, so the two cannot disagree.
40 Vole string
41 // Config is the absolute path to vole.toml, handed to ssh-authorized-keys.
42 Config string
43 }
44
883ffcc9
Keenan Nemetz
12d ago
45 // LinuxDefaults is what packaging/sshd/vole-linux.conf is rendered from: the
46 // account and paths the Linux package installs. An operator who changes ssh_user
47 // changes the Match line in their copy of the file, and the header says so.
48 func LinuxDefaults() Options {
49 return defaultsFor("linux")
50 }
51
52 // DarwinDefaults is what packaging/sshd/vole-darwin.conf is rendered from: a
53 // /usr/local install, which is where a system install puts its files. Apple
54 // Silicon Homebrew uses /opt/homebrew instead; the header tells the reader to
55 // substitute the prefix it prints.
56 func DarwinDefaults() Options {
57 return defaultsFor("darwin")
58 }
59
60 // defaultsFor is the fallback for every field an Options leaves empty. The two
61 // platforms differ in where a package puts its files and in which service
62 // manager owns sshd and Vole.
63 func defaultsFor(platform string) Options {
64 if platform == "darwin" {
65 return Options{
66 Platform: "darwin",
67 Destination: "/etc/ssh/vole.conf",
68 User: "git",
69 ServiceUser: "vole",
70 Shell: "/usr/local/lib/vole/ssh-shell",
71 Socket: "/usr/local/var/vole/ssh.sock",
72 Vole: "/usr/local/bin/vole",
73 Config: "/usr/local/etc/vole/vole.toml",
74 }
75 }
09610c0f
Keenan Nemetz
16d ago
76 return Options{
883ffcc9
Keenan Nemetz
12d ago
77 Platform: "linux",
78 Destination: "/usr/share/vole/sshd/vole.conf",
79 User: "git",
80 ServiceUser: "vole",
81 Shell: "/usr/lib/vole/ssh-shell",
82 Socket: "/run/vole/ssh.sock",
83 Vole: "/usr/bin/vole",
84 Config: "/etc/vole/vole.toml",
09610c0f
Keenan Nemetz
16d ago
85 }
86 }
87
883ffcc9
Keenan Nemetz
12d ago
88 // filled replaces every empty field with the default for its platform.
89 func (o Options) filled() Options {
90 d := defaultsFor(o.Platform)
91 if o.Platform == "" {
92 o.Platform = d.Platform
93 }
94 if o.Destination == "" {
95 o.Destination = d.Destination
96 }
97 if o.User == "" {
98 o.User = d.User
99 }
100 if o.ServiceUser == "" {
101 o.ServiceUser = d.ServiceUser
102 }
103 if o.Shell == "" {
104 o.Shell = d.Shell
105 }
106 if o.Socket == "" {
107 o.Socket = d.Socket
108 }
109 if o.Vole == "" {
110 o.Vole = d.Vole
111 }
112 if o.Config == "" {
113 o.Config = d.Config
114 }
115 return o
116 }
117
09610c0f
Keenan Nemetz
16d ago
118 // Render is the whole drop-in, comments included.
119 func Render(o Options) string {
883ffcc9
Keenan Nemetz
12d ago
120 o = o.filled()
09610c0f
Keenan Nemetz
16d ago
121 var b strings.Builder
122 fmt.Fprintf(&b, `# The system sshd as Vole's front door.
123 #
883ffcc9
Keenan Nemetz
12d ago
124 # This file is rendered, not installed: Vole never edits /etc/ssh. To enable the
125 # front door:
09610c0f
Keenan Nemetz
16d ago
126 #
127 # 1. Append this line at the END of /etc/ssh/sshd_config. It must be last: a
128 # Match block applies to everything after it, so an Include placed above
129 # other settings would silently scope them to the matched user.
130 #
883ffcc9
Keenan Nemetz
12d ago
131 # Include %s
132 #
133 `, o.Destination)
134 if o.Platform == "darwin" {
135 fmt.Fprint(&b, `# 2. sudo sshd -t && sudo launchctl kickstart -k system/com.openssh.sshd
09610c0f
Keenan Nemetz
16d ago
136 #
883ffcc9
Keenan Nemetz
12d ago
137 # Remote Login must be on: sudo systemsetup -setremotelogin on
09610c0f
Keenan Nemetz
16d ago
138 #
883ffcc9
Keenan Nemetz
12d ago
139 `)
140 } else {
141 fmt.Fprint(&b, `# 2. sshd -t && systemctl reload ssh # the unit is sshd on RHEL
142 #
143 `)
144 }
145 fmt.Fprintf(&b, `# 3. In %s set:
09610c0f
Keenan Nemetz
16d ago
146 #
147 # ssh_listen = "socket"
148 # ssh_addr = "0.0.0.0:22"
883ffcc9
Keenan Nemetz
12d ago
149 # ssh_socket = %q
09610c0f
Keenan Nemetz
16d ago
150 # ssh_public_host_key_file = "/etc/ssh/ssh_host_ed25519_key.pub"
151 #
152 # ssh_addr is what generated clone URLs carry: port 22 is omitted, so the
153 # URL is ssh://git@host/repo.git.
154 #
883ffcc9
Keenan Nemetz
12d ago
155 `, o.Config, o.Socket)
156 if o.Platform == "darwin" {
157 fmt.Fprint(&b, `# 4. Restart vole serve the way it was started: brew services restart vole,
158 # or the launchd job or shell that runs it.
159 #
160 `)
161 } else {
162 fmt.Fprint(&b, `# 4. systemctl restart vole
163 #
164 `)
165 }
166 fmt.Fprintf(&b, `# If this instance uses a different ssh_user, change the Match line below to
167 # name it, and `)
168 if o.Platform == "darwin" {
169 fmt.Fprintf(&b, `give that account this file as its shell:
09610c0f
Keenan Nemetz
16d ago
170 #
883ffcc9
Keenan Nemetz
12d ago
171 # sudo sysadminctl -addUser %s -shell %s ...
09610c0f
Keenan Nemetz
16d ago
172 #
883ffcc9
Keenan Nemetz
12d ago
173 `, o.User, o.Shell)
174 } else {
175 fmt.Fprintf(&b, `create that account with this file as its shell:
09610c0f
Keenan Nemetz
16d ago
176 #
883ffcc9
Keenan Nemetz
12d ago
177 # useradd --system --shell %s ...
178 #
179 `, o.Shell)
180 }
181 if o.Platform == "darwin" {
182 if strings.HasPrefix(o.Vole, "/usr/local/") {
183 fmt.Fprint(&b, `# The command below runs /usr/local/bin/vole with
184 # /usr/local/etc/vole/vole.toml, the paths of a system install. A Homebrew
185 # install uses the prefix "brew --prefix" prints (/opt/homebrew on Apple
186 # Silicon): change both paths to match, or sshd will run a binary that is not
187 # there.
188 #
189 `)
190 }
191 fmt.Fprintf(&b, `# AuthorizedKeysCommandUser below names %s: the account that runs
192 # vole serve. Give the launchd job that name, or change this line to the account
193 # it uses, because sshd runs the roster lookup as it.
194 #
195 `, o.ServiceUser)
196 } else {
197 fmt.Fprint(&b, `# AuthorizedKeysCommandUser is the service account.
198 #
199 `)
200 }
201 fmt.Fprintf(&b, `# That account reads the roster and mints a session token. The forced command
202 # runs as %s, which has no access to the instance's data directory.
203 # AuthorizedKeysFile none means sshd never reads an authorized_keys file on this
204 # machine: the only keys it accepts are the ones Vole answers for.
09610c0f
Keenan Nemetz
16d ago
205
206 Match User %s
207 AuthorizedKeysFile none
208 AuthorizedKeysCommand %s ssh-authorized-keys --config %s --fingerprint %%f --type %%t --key %%k --connection %%C
883ffcc9
Keenan Nemetz
12d ago
209 AuthorizedKeysCommandUser %s
09610c0f
Keenan Nemetz
16d ago
210 PasswordAuthentication no
211 KbdInteractiveAuthentication no
212 DisableForwarding yes
213 PermitTTY no
214 PermitUserRC no
883ffcc9
Keenan Nemetz
12d ago
215 `, o.User, o.User, o.Vole, o.Config, o.ServiceUser)
09610c0f
Keenan Nemetz
16d ago
216 return b.String()
217 }