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