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.postinst495
1 files changed, 495 insertions, 0 deletions
diff --git a/debian/openssh-server.postinst b/debian/openssh-server.postinst
new file mode 100644
index 000000000..d415f3f0d
--- /dev/null
+++ b/debian/openssh-server.postinst
@@ -0,0 +1,495 @@
1#!/bin/sh -e
2
3action="$1"
4oldversion="$2"
5
6. /usr/share/debconf/confmodule
7db_version 2.0
8
9umask 022
10
11if [ "$action" != configure ]
12 then
13 exit 0
14fi
15
16
17fix_doc_symlink() {
18 if [ ! -L /usr/share/doc/openssh-server ] && \
19 dpkg --compare-versions "$oldversion" lt-nl 1:4.1p1-5; then
20 rm -rf /usr/share/doc/openssh-server
21 ln -s openssh-client /usr/share/doc/openssh-server
22 fi
23}
24
25check_idea_key() {
26 # check for old host_key files using IDEA, which openssh does not
27 # support
28 if [ -f /etc/ssh/ssh_host_key ] ; then
29 cp -a /etc/ssh/ssh_host_key /etc/ssh/ssh_host_key.check_idea
30 if ssh-keygen -p -N '' -f /etc/ssh/ssh_host_key.check_idea 2>&1 | \
31 grep -q 'unknown cipher' 2>/dev/null; then
32 mv /etc/ssh/ssh_host_key /etc/ssh/ssh_host_key.old
33 mv /etc/ssh/ssh_host_key.pub /etc/ssh/ssh_host_key.pub.old
34 fi
35 rm -f /etc/ssh/ssh_host_key.check_idea
36 fi
37}
38
39
40get_config_option() {
41 option="$1"
42
43 [ -f /etc/ssh/sshd_config ] || return
44
45 # TODO: actually only one '=' allowed after option
46 perl -lne 's/\s+/ /g; print if s/^\s*'"$option"'[[:space:]=]+//i' \
47 /etc/ssh/sshd_config
48}
49
50
51set_config_option() {
52 option="$1"
53 value="$2"
54
55 perl -le '
56 $option = $ARGV[0]; $value = $ARGV[1]; $done = 0;
57 while (<STDIN>) {
58 chomp;
59 (my $match = $_) =~ s/\s+/ /g;
60 if ($match =~ s/^\s*\Q$option\E\s+.*/$option $value/) {
61 $_ = $match;
62 $done = 1;
63 }
64 print;
65 }
66 print "$option $value" unless $done;' \
67 "$option" "$value" \
68 < /etc/ssh/sshd_config > /etc/ssh/sshd_config.dpkg-new
69 chown --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
70 chmod --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
71 mv /etc/ssh/sshd_config.dpkg-new /etc/ssh/sshd_config
72}
73
74
75disable_config_option() {
76 option="$1"
77
78 value="$(get_config_option "$option")"
79 [ "$value" ] || return 0
80
81 perl -le '
82 $option = $ARGV[0];
83 while (<STDIN>) {
84 chomp;
85 (my $match = $_) =~ s/\s+/ /g;
86 # TODO: actually only one "=" allowed after option
87 if ($match =~ s/^(\s*\Q$option\E[[:space:]=]+.*)/#$1/i) {
88 $_ = $match;
89 }
90 print;
91 }' \
92 "$option" \
93 < /etc/ssh/sshd_config > /etc/ssh/sshd_config.dpkg-new
94 chown --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
95 chmod --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
96 mv /etc/ssh/sshd_config.dpkg-new /etc/ssh/sshd_config
97}
98
99
100rename_config_option() {
101 oldoption="$1"
102 newoption="$2"
103
104 value="$(get_config_option "$oldoption")"
105 [ "$value" ] || return 0
106
107 perl -le '
108 $oldoption = $ARGV[0]; $newoption = $ARGV[1];
109 while (<STDIN>) {
110 chomp;
111 (my $match = $_) =~ s/\s+/ /g;
112 # TODO: actually only one "=" allowed after option
113 if ($match =~ s/^(\s*)\Q$oldoption\E([[:space:]=]+)/$1$newoption$2/i) {
114 $_ = $match;
115 }
116 print;
117 }' \
118 "$oldoption" "$newoption" \
119 < /etc/ssh/sshd_config > /etc/ssh/sshd_config.dpkg-new
120 chown --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
121 chmod --reference /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-new
122 mv /etc/ssh/sshd_config.dpkg-new /etc/ssh/sshd_config
123}
124
125
126remove_obsolete_gssapi() {
127 disable_config_option GSSAPINoMICAuthentication
128 disable_config_option GSSUseSessionCCache
129 disable_config_option GSSAPIUseSessionCredCache
130}
131
132
133host_keys_required() {
134 hostkeys="$(get_config_option HostKey)"
135 if [ "$hostkeys" ]; then
136 echo "$hostkeys"
137 else
138 # No HostKey directives at all, so the server picks some
139 # defaults depending on the setting of Protocol.
140 protocol="$(get_config_option Protocol)"
141 [ "$protocol" ] || protocol=1,2
142 if echo "$protocol" | grep 1 >/dev/null; then
143 echo /etc/ssh/ssh_host_key
144 fi
145 if echo "$protocol" | grep 2 >/dev/null; then
146 echo /etc/ssh/ssh_host_rsa_key
147 echo /etc/ssh/ssh_host_dsa_key
148 fi
149 fi
150}
151
152
153create_key() {
154 msg="$1"
155 shift
156 hostkeys="$1"
157 shift
158 file="$1"
159 shift
160
161 if echo "$hostkeys" | grep -x "$file" >/dev/null && \
162 [ ! -f "$file" ] ; then
163 echo -n $msg
164 ssh-keygen -q -f "$file" -N '' "$@"
165 echo
166 if which restorecon >/dev/null 2>&1; then
167 restorecon "$file.pub"
168 fi
169 fi
170}
171
172
173create_keys() {
174 hostkeys="$(host_keys_required)"
175
176 create_key "Creating SSH1 key; this may take some time ..." \
177 "$hostkeys" /etc/ssh/ssh_host_key -t rsa1
178
179 create_key "Creating SSH2 RSA key; this may take some time ..." \
180 "$hostkeys" /etc/ssh/ssh_host_rsa_key -t rsa
181 create_key "Creating SSH2 DSA key; this may take some time ..." \
182 "$hostkeys" /etc/ssh/ssh_host_dsa_key -t dsa
183}
184
185
186vulnerable_host_keys() {
187 # If the admin has explicitly put the vulnerable keys back, we
188 # assume they can look after themselves.
189 db_fget ssh/vulnerable_host_keys seen
190 if [ "$RET" = true ]; then
191 return 0
192 fi
193
194 hostkeys="$(host_keys_required)"
195 vulnerable=
196 for hostkey in $hostkeys; do
197 [ -f "$hostkey" ] || continue
198 if ssh-vulnkey -q "$hostkey"; then
199 vulnerable="${vulnerable:+$vulnerable }$hostkey"
200 fi
201 done
202 if [ "$vulnerable" ]; then
203 db_subst ssh/vulnerable_host_keys HOST_KEYS "$vulnerable"
204 db_input critical ssh/vulnerable_host_keys || true
205 db_go
206 for hostkey in $vulnerable; do
207 mv "$hostkey" "$hostkey.broken" || true
208 mv "$hostkey.pub" "$hostkey.pub.broken" || true
209 done
210 create_keys
211 fi
212}
213
214
215check_password_auth() {
216 passwordauth="$(get_config_option PasswordAuthentication)"
217 crauth="$(get_config_option ChallengeResponseAuthentication)"
218 if [ "$passwordauth" = no ] && \
219 ([ -z "$crauth" ] || [ "$crauth" = yes ]); then
220 db_get ssh/disable_cr_auth
221 if [ "$RET" = true ]; then
222 set_config_option ChallengeResponseAuthentication no
223 fi
224 fi
225}
226
227
228move_subsystem_sftp() {
229 subsystem_sftp="$(get_config_option 'Subsystem sftp')"
230 if [ "$subsystem_sftp" = /usr/lib/sftp-server ] || \
231 [ "$subsystem_sftp" = /usr/libexec/sftp-server ]; then
232 set_config_option 'Subsystem sftp' /usr/lib/openssh/sftp-server
233 fi
234}
235
236
237fix_loglevel_silent() {
238 if [ "$(get_config_option LogLevel)" = SILENT ]; then
239 set_config_option LogLevel QUIET
240 fi
241}
242
243
244create_sshdconfig() {
245 if [ -e /etc/ssh/sshd_config ] ; then
246 # Upgrade an existing sshd configuration.
247
248 if (dpkg --compare-versions "$oldversion" lt-nl 1:3.8p1-1 && \
249 ! grep -iq ^UsePAM /etc/ssh/sshd_config) || \
250 grep -Eiq '^(PAMAuthenticationViaKbdInt|RhostsAuthentication)' \
251 /etc/ssh/sshd_config ; then
252 # Upgrade from pre-3.7: UsePAM needed to maintain standard
253 # Debian configuration.
254 # Note that --compare-versions is sadly not reliable enough
255 # here due to the package split of ssh into openssh-client
256 # and openssh-server. The extra grep for some deprecated
257 # options should with any luck be a good enough heuristic.
258 echo -n 'Upgrading sshd_config (old version in .dpkg-old) ...'
259 cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-old
260 perl -pe 's/^(PAMAuthenticationViaKbdInt|RhostsAuthentication)\b/#$1/i' \
261 /etc/ssh/sshd_config > /etc/ssh/sshd_config.dpkg-new
262 echo >> /etc/ssh/sshd_config.dpkg-new
263 echo 'UsePAM yes' >> /etc/ssh/sshd_config.dpkg-new
264 chown --reference /etc/ssh/sshd_config \
265 /etc/ssh/sshd_config.dpkg-new
266 chmod --reference /etc/ssh/sshd_config \
267 /etc/ssh/sshd_config.dpkg-new
268 mv /etc/ssh/sshd_config.dpkg-new /etc/ssh/sshd_config
269 echo
270 fi
271
272 # An empty version means we're upgrading from before the
273 # package split, so check.
274 if dpkg --compare-versions "$oldversion" lt 1:3.8.1p1-11; then
275 check_password_auth
276 fi
277
278 # libexecdir changed, so fix up 'Subsystem sftp'.
279 if dpkg --compare-versions "$oldversion" lt 1:4.1p1-1; then
280 move_subsystem_sftp
281 fi
282
283 # Remove obsolete GSSAPI options.
284 if dpkg --compare-versions "$oldversion" lt 1:4.3p2-8; then
285 remove_obsolete_gssapi
286 fi
287
288 # This option was renamed in 3.8p1, but we never took care
289 # of adjusting the configuration file until now.
290 if dpkg --compare-versions "$oldversion" lt 1:4.7p1-8; then
291 rename_config_option KeepAlive TCPKeepAlive
292 fi
293
294 # 'LogLevel SILENT' is now equivalent to QUIET.
295 if dpkg --compare-versions "$oldversion" lt 1:5.4p1-1; then
296 fix_loglevel_silent
297 fi
298
299 return 0
300 fi
301
302 #Preserve old sshd_config before generating a new one
303 if [ -e /etc/ssh/sshd_config ] ; then
304 mv /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-old
305 fi
306
307 cat <<EOF > /etc/ssh/sshd_config
308# Package generated configuration file
309# See the sshd_config(5) manpage for details
310
311# What ports, IPs and protocols we listen for
312Port 22
313# Use these options to restrict which interfaces/protocols sshd will bind to
314#ListenAddress ::
315#ListenAddress 0.0.0.0
316Protocol 2
317# HostKeys for protocol version 2
318HostKey /etc/ssh/ssh_host_rsa_key
319HostKey /etc/ssh/ssh_host_dsa_key
320#Privilege Separation is turned on for security
321UsePrivilegeSeparation yes
322
323# Lifetime and size of ephemeral version 1 server key
324KeyRegenerationInterval 3600
325ServerKeyBits 768
326
327# Logging
328SyslogFacility AUTH
329LogLevel INFO
330
331# Authentication:
332LoginGraceTime 120
333PermitRootLogin yes
334StrictModes yes
335
336RSAAuthentication yes
337PubkeyAuthentication yes
338#AuthorizedKeysFile %h/.ssh/authorized_keys
339
340# Don't read the user's ~/.rhosts and ~/.shosts files
341IgnoreRhosts yes
342# For this to work you will also need host keys in /etc/ssh_known_hosts
343RhostsRSAAuthentication no
344# similar for protocol version 2
345HostbasedAuthentication no
346# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
347#IgnoreUserKnownHosts yes
348
349# To enable empty passwords, change to yes (NOT RECOMMENDED)
350PermitEmptyPasswords no
351
352# Change to yes to enable challenge-response passwords (beware issues with
353# some PAM modules and threads)
354ChallengeResponseAuthentication no
355
356# Change to no to disable tunnelled clear text passwords
357#PasswordAuthentication yes
358
359# Kerberos options
360#KerberosAuthentication no
361#KerberosGetAFSToken no
362#KerberosOrLocalPasswd yes
363#KerberosTicketCleanup yes
364
365# GSSAPI options
366#GSSAPIAuthentication no
367#GSSAPICleanupCredentials yes
368
369X11Forwarding yes
370X11DisplayOffset 10
371PrintMotd no
372PrintLastLog yes
373TCPKeepAlive yes
374#UseLogin no
375
376#MaxStartups 10:30:60
377#Banner /etc/issue.net
378
379# Allow client to pass locale environment variables
380AcceptEnv LANG LC_*
381
382Subsystem sftp /usr/lib/openssh/sftp-server
383
384# Set this to 'yes' to enable PAM authentication, account processing,
385# and session processing. If this is enabled, PAM authentication will
386# be allowed through the ChallengeResponseAuthentication and
387# PasswordAuthentication. Depending on your PAM configuration,
388# PAM authentication via ChallengeResponseAuthentication may bypass
389# the setting of "PermitRootLogin without-password".
390# If you just want the PAM account and session checks to run without
391# PAM authentication, then enable this but set PasswordAuthentication
392# and ChallengeResponseAuthentication to 'no'.
393UsePAM yes
394EOF
395}
396
397fix_statoverride() {
398# Remove an erronous override for sshd (we should have overridden ssh)
399 if [ -x /usr/sbin/dpkg-statoverride ]; then
400 if dpkg-statoverride --list /usr/sbin/sshd >/dev/null ; then
401 dpkg-statoverride --remove /usr/sbin/sshd
402 fi
403 fi
404}
405
406fix_sshd_shell() {
407 if getent passwd sshd | grep -q ':/bin/false$'; then
408 usermod -s /usr/sbin/nologin sshd || true
409 fi
410}
411
412setup_sshd_user() {
413 if ! getent passwd sshd >/dev/null; then
414 adduser --quiet --system --no-create-home --home /var/run/sshd --shell /usr/sbin/nologin sshd
415 fi
416}
417
418fix_conffile_permissions() {
419 # Clean up after executable /etc/default/ssh in 1:3.5p1-5. dpkg
420 # doesn't do this for us; see bug #192981.
421 chmod 644 /etc/default/ssh
422}
423
424remove_old_init_links() {
425 # Yes, this only works with the SysV init script layout. I know.
426 # The important thing is that it doesn't actually *break* with
427 # file-rc ...
428 if [ -e /etc/rc2.d/S20ssh ]; then
429 update-rc.d -f ssh remove >/dev/null 2>&1
430 fi
431 rm -f /etc/rc0.d/K??ssh /etc/rc1.d/K??ssh /etc/rc6.d/K??ssh
432}
433
434setup_init() {
435 if [ -x /etc/init.d/ssh ]; then
436 update-rc.d ssh start 16 2 3 4 5 . >/dev/null
437 if [ -x /usr/sbin/invoke-rc.d ]; then
438 invoke-rc.d ssh restart
439 else
440 /etc/init.d/ssh restart
441 fi
442 fi
443}
444
445commit_transfer_conffile () {
446 CONFFILE="$1"
447 if [ -e "$CONFFILE.moved-by-preinst" ]; then
448 rm -f "$CONFFILE.moved-by-preinst"
449 fi
450}
451
452commit_mv_conffile () {
453 OLDCONFFILE="$1"
454 NEWCONFFILE="$2"
455
456 if [ -e "$OLDCONFFILE.moving" ]; then
457 echo "Preserving user changes to $NEWCONFFILE ..."
458 mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
459 mv -f "$OLDCONFFILE.moving" "$NEWCONFFILE"
460 elif [ -e "$OLDCONFFILE.dpkg-old" ]; then
461 rm -f "$OLDCONFFILE.dpkg-old"
462 fi
463}
464
465
466fix_doc_symlink
467create_sshdconfig
468check_idea_key
469create_keys
470vulnerable_host_keys
471fix_statoverride
472if dpkg --compare-versions "$2" lt 1:4.3p2-3; then
473 fix_sshd_shell
474fi
475setup_sshd_user
476if dpkg --compare-versions "$2" lt 1:3.6.1p2-2; then
477 fix_conffile_permissions
478fi
479if dpkg --compare-versions "$2" lt 1:5.2p1-1; then
480 remove_old_init_links
481fi
482setup_init
483commit_transfer_conffile /etc/default/ssh
484commit_transfer_conffile /etc/init.d/ssh
485commit_transfer_conffile /etc/pam.d/ssh
486commit_mv_conffile /etc/pam.d/ssh /etc/pam.d/sshd
487# Renamed to /etc/ssh/moduli in 2.9.9 (!)
488if dpkg --compare-versions "$2" lt 1:4.7p1-1; then
489 rm -f /etc/ssh/primes
490fi
491
492
493db_stop
494
495exit 0