summaryrefslogtreecommitdiff
path: root/debian/openssh-server.postinst
diff options
context:
space:
mode:
Diffstat (limited to 'debian/openssh-server.postinst')
-rw-r--r--debian/openssh-server.postinst327
1 files changed, 327 insertions, 0 deletions
diff --git a/debian/openssh-server.postinst b/debian/openssh-server.postinst
new file mode 100644
index 000000000..2c6c53d88
--- /dev/null
+++ b/debian/openssh-server.postinst
@@ -0,0 +1,327 @@
1#!/bin/sh
2set -e
3
4. /usr/share/debconf/confmodule
5db_version 2.0
6
7action="$1"
8oldversion="$2"
9
10umask 022
11
12
13get_config_option() {
14 option="$1"
15
16 [ -f /etc/ssh/sshd_config ] || return
17
18 # TODO: actually only one '=' allowed after option
19 perl -lne '
20 s/[[:space:]]+/ /g; s/[[:space:]]+$//;
21 print if s/^[[:space:]]*'"$option"'[[:space:]=]+//i' \
22 /etc/ssh/sshd_config
23}
24
25
26set_config_option() {
27 option="$1"
28 value="$2"
29
30 perl -le '
31 $option = $ARGV[0]; $value = $ARGV[1]; $done = 0;
32 while (<STDIN>) {
33 chomp;
34 (my $match = $_) =~ s/\s+/ /g;
35 if ($match =~ s/^\s*\Q$option\E\s+.*/$option $value/) {
36 $_ = $match;
37 $done = 1;
38 }
39 print;
40 }
41 print "$option $value" unless $done;' \
42 "$option" "$value" \
43 < /etc/ssh/sshd_config > /etc/ssh/sshd_config.dpkg-new
44 chown --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
45 chmod --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
46 mv /etc/ssh/sshd_config.dpkg-new /etc/ssh/sshd_config
47}
48
49
50rename_config_option() {
51 oldoption="$1"
52 newoption="$2"
53
54 value="$(get_config_option "$oldoption")"
55 [ "$value" ] || return 0
56
57 perl -le '
58 $oldoption = $ARGV[0]; $newoption = $ARGV[1];
59 while (<STDIN>) {
60 chomp;
61 (my $match = $_) =~ s/\s+/ /g;
62 # TODO: actually only one "=" allowed after option
63 if ($match =~ s/^(\s*)\Q$oldoption\E([[:space:]=]+)/$1$newoption$2/i) {
64 $_ = $match;
65 }
66 print;
67 }' \
68 "$oldoption" "$newoption" \
69 < /etc/ssh/sshd_config > /etc/ssh/sshd_config.dpkg-new
70 chown --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
71 chmod --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
72 mv /etc/ssh/sshd_config.dpkg-new /etc/ssh/sshd_config
73}
74
75
76host_keys_required() {
77 hostkeys="$(get_config_option HostKey)"
78 if [ "$hostkeys" ]; then
79 echo "$hostkeys"
80 else
81 # No HostKey directives at all, so the server picks some
82 # defaults depending on the setting of Protocol.
83 protocol="$(get_config_option Protocol)"
84 [ "$protocol" ] || protocol=1,2
85 if echo "$protocol" | grep 1 >/dev/null; then
86 echo /etc/ssh/ssh_host_key
87 fi
88 if echo "$protocol" | grep 2 >/dev/null; then
89 echo /etc/ssh/ssh_host_rsa_key
90 echo /etc/ssh/ssh_host_dsa_key
91 echo /etc/ssh/ssh_host_ecdsa_key
92 echo /etc/ssh/ssh_host_ed25519_key
93 fi
94 fi
95}
96
97
98create_key() {
99 msg="$1"
100 shift
101 hostkeys="$1"
102 shift
103 file="$1"
104 shift
105
106 if echo "$hostkeys" | grep -x "$file" >/dev/null && \
107 [ ! -f "$file" ] ; then
108 echo -n $msg
109 ssh-keygen -q -f "$file" -N '' "$@"
110 echo
111 if which restorecon >/dev/null 2>&1; then
112 restorecon "$file" "$file.pub"
113 fi
114 ssh-keygen -l -f "$file.pub"
115 fi
116}
117
118
119create_keys() {
120 hostkeys="$(host_keys_required)"
121
122 create_key "Creating SSH1 key; this may take some time ..." \
123 "$hostkeys" /etc/ssh/ssh_host_key -t rsa1
124
125 create_key "Creating SSH2 RSA key; this may take some time ..." \
126 "$hostkeys" /etc/ssh/ssh_host_rsa_key -t rsa
127 create_key "Creating SSH2 DSA key; this may take some time ..." \
128 "$hostkeys" /etc/ssh/ssh_host_dsa_key -t dsa
129 create_key "Creating SSH2 ECDSA key; this may take some time ..." \
130 "$hostkeys" /etc/ssh/ssh_host_ecdsa_key -t ecdsa
131 create_key "Creating SSH2 ED25519 key; this may take some time ..." \
132 "$hostkeys" /etc/ssh/ssh_host_ed25519_key -t ed25519
133}
134
135
136fix_loglevel_silent() {
137 if [ "$(get_config_option LogLevel)" = SILENT ]; then
138 set_config_option LogLevel QUIET
139 fi
140}
141
142
143update_server_key_bits() {
144 if [ "$(get_config_option ServerKeyBits)" = 768 ]; then
145 set_config_option ServerKeyBits 1024
146 fi
147}
148
149
150create_sshdconfig() {
151 if [ -e /etc/ssh/sshd_config ] ; then
152 # Upgrade an existing sshd configuration.
153
154 # This option was renamed in 3.8p1, but we never took care
155 # of adjusting the configuration file until now.
156 if dpkg --compare-versions "$oldversion" lt 1:4.7p1-8; then
157 rename_config_option KeepAlive TCPKeepAlive
158 fi
159
160 # 'LogLevel SILENT' is now equivalent to QUIET.
161 if dpkg --compare-versions "$oldversion" lt 1:5.4p1-1; then
162 fix_loglevel_silent
163 fi
164
165 # Changed upstream in 5.1p1, but we forgot to update the
166 # package-generated configuration file until now.
167 if dpkg --compare-versions "$oldversion" lt 1:6.4p1-2; then
168 update_server_key_bits
169 fi
170
171 return 0
172 fi
173
174 cat <<EOF > /etc/ssh/sshd_config
175# Package generated configuration file
176# See the sshd_config(5) manpage for details
177
178# What ports, IPs and protocols we listen for
179Port 22
180# Use these options to restrict which interfaces/protocols sshd will bind to
181#ListenAddress ::
182#ListenAddress 0.0.0.0
183Protocol 2
184# HostKeys for protocol version 2
185HostKey /etc/ssh/ssh_host_rsa_key
186HostKey /etc/ssh/ssh_host_dsa_key
187HostKey /etc/ssh/ssh_host_ecdsa_key
188HostKey /etc/ssh/ssh_host_ed25519_key
189#Privilege Separation is turned on for security
190UsePrivilegeSeparation yes
191
192# Lifetime and size of ephemeral version 1 server key
193KeyRegenerationInterval 3600
194ServerKeyBits 1024
195
196# Logging
197SyslogFacility AUTH
198LogLevel INFO
199
200# Authentication:
201LoginGraceTime 120
202PermitRootLogin prohibit-password
203StrictModes yes
204
205RSAAuthentication yes
206PubkeyAuthentication yes
207#AuthorizedKeysFile %h/.ssh/authorized_keys
208
209# Don't read the user's ~/.rhosts and ~/.shosts files
210IgnoreRhosts yes
211# For this to work you will also need host keys in /etc/ssh_known_hosts
212RhostsRSAAuthentication no
213# similar for protocol version 2
214HostbasedAuthentication no
215# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
216#IgnoreUserKnownHosts yes
217
218# To enable empty passwords, change to yes (NOT RECOMMENDED)
219PermitEmptyPasswords no
220
221# Change to yes to enable challenge-response passwords (beware issues with
222# some PAM modules and threads)
223ChallengeResponseAuthentication no
224
225# Change to no to disable tunnelled clear text passwords
226#PasswordAuthentication yes
227
228# Kerberos options
229#KerberosAuthentication no
230#KerberosGetAFSToken no
231#KerberosOrLocalPasswd yes
232#KerberosTicketCleanup yes
233
234# GSSAPI options
235#GSSAPIAuthentication no
236#GSSAPICleanupCredentials yes
237
238X11Forwarding yes
239X11DisplayOffset 10
240PrintMotd no
241PrintLastLog yes
242TCPKeepAlive yes
243#UseLogin no
244
245#MaxStartups 10:30:60
246#Banner /etc/issue.net
247
248# Allow client to pass locale environment variables
249AcceptEnv LANG LC_*
250
251Subsystem sftp /usr/lib/openssh/sftp-server
252
253# Set this to 'yes' to enable PAM authentication, account processing,
254# and session processing. If this is enabled, PAM authentication will
255# be allowed through the ChallengeResponseAuthentication and
256# PasswordAuthentication. Depending on your PAM configuration,
257# PAM authentication via ChallengeResponseAuthentication may bypass
258# the setting of "PermitRootLogin without-password".
259# If you just want the PAM account and session checks to run without
260# PAM authentication, then enable this but set PasswordAuthentication
261# and ChallengeResponseAuthentication to 'no'.
262UsePAM yes
263EOF
264}
265
266fix_statoverride() {
267# Remove an erronous override for sshd (we should have overridden ssh)
268 if dpkg-statoverride --list /usr/sbin/sshd >/dev/null; then
269 dpkg-statoverride --remove /usr/sbin/sshd
270 fi
271}
272
273setup_sshd_user() {
274 if ! getent passwd sshd >/dev/null; then
275 adduser --quiet --system --no-create-home --home /var/run/sshd --shell /usr/sbin/nologin sshd
276 fi
277}
278
279if [ "$action" = configure ]; then
280 create_sshdconfig
281 create_keys
282 fix_statoverride
283 setup_sshd_user
284 # Renamed to /etc/ssh/moduli in 2.9.9 (!)
285 if dpkg --compare-versions "$2" lt-nl 1:4.7p1-1; then
286 rm -f /etc/ssh/primes
287 fi
288 if dpkg --compare-versions "$2" lt-nl 1:5.5p1-6; then
289 rm -f /var/run/sshd/.placeholder
290 fi
291 if dpkg --compare-versions "$2" lt-nl 1:6.2p2-3 && \
292 which initctl >/dev/null && initctl version 2>/dev/null | grep -q upstart && \
293 ! status ssh 2>/dev/null | grep -q ' start/'; then
294 # We must stop the sysvinit-controlled sshd before we can
295 # restart it under Upstart.
296 start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/sshd.pid || true
297 fi
298 if dpkg --compare-versions "$2" lt-nl 1:6.5p1-2 && \
299 deb-systemd-helper debian-installed ssh.socket && \
300 deb-systemd-helper --quiet was-enabled ssh.service && \
301 deb-systemd-helper --quiet was-enabled ssh.socket; then
302 # 1:6.5p1-1 mistakenly left both ssh.service and ssh.socket
303 # enabled.
304 deb-systemd-helper disable ssh.socket >/dev/null || true
305 fi
306 if dpkg --compare-versions "$2" lt-nl 1:6.5p1-3 && \
307 [ -d /run/systemd/system ]; then
308 # We must stop the sysvinit-controlled sshd before we can
309 # restart it under systemd.
310 start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/sshd.pid --exec /usr/sbin/sshd || true
311 fi
312 if dpkg --compare-versions "$2" lt-nl 1:6.6p1-1 && \
313 [ "$(get_config_option PermitRootLogin)" = yes ] &&
314 db_get openssh-server/permit-root-login && [ "$RET" = true ]; then
315 set_config_option PermitRootLogin prohibit-password
316 fi
317 if dpkg --compare-versions "$2" lt-nl 1:7.1p1-1 && \
318 [ "$(get_config_option PermitRootLogin)" = without-password ]; then
319 set_config_option PermitRootLogin prohibit-password
320 fi
321fi
322
323#DEBHELPER#
324
325db_stop
326
327exit 0