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