diff options
Diffstat (limited to 'debian/openssh-server.postinst')
-rw-r--r-- | debian/openssh-server.postinst | 330 |
1 files changed, 330 insertions, 0 deletions
diff --git a/debian/openssh-server.postinst b/debian/openssh-server.postinst new file mode 100644 index 000000000..ef1412ca7 --- /dev/null +++ b/debian/openssh-server.postinst | |||
@@ -0,0 +1,330 @@ | |||
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 | check_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 | |||
40 | get_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 | |||
51 | set_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 | |||
75 | host_keys_required() { | ||
76 | hostkeys="$(get_config_option HostKey)" | ||
77 | if [ "$hostkeys" ]; then | ||
78 | echo "$hostkeys" | ||
79 | else | ||
80 | # No HostKey directives at all, so the server picks some | ||
81 | # defaults depending on the setting of Protocol. | ||
82 | protocol="$(get_config_option Protocol)" | ||
83 | [ "$protocol" ] || protocol=1,2 | ||
84 | if echo "$protocol" | grep 1 >/dev/null; then | ||
85 | echo /etc/ssh/ssh_host_key | ||
86 | fi | ||
87 | if echo "$protocol" | grep 2 >/dev/null; then | ||
88 | echo /etc/ssh/ssh_host_rsa_key | ||
89 | echo /etc/ssh/ssh_host_dsa_key | ||
90 | fi | ||
91 | fi | ||
92 | } | ||
93 | |||
94 | |||
95 | create_key() { | ||
96 | msg="$1" | ||
97 | shift | ||
98 | hostkeys="$1" | ||
99 | shift | ||
100 | file="$1" | ||
101 | shift | ||
102 | |||
103 | if echo "$hostkeys" | grep -x "$file" >/dev/null && \ | ||
104 | [ ! -f "$file" ] ; then | ||
105 | echo -n $msg | ||
106 | ssh-keygen -q -f "$file" -N '' "$@" | ||
107 | echo | ||
108 | if type restorecon >/dev/null 2>&1; then | ||
109 | restorecon "$file.pub" | ||
110 | fi | ||
111 | fi | ||
112 | } | ||
113 | |||
114 | |||
115 | create_keys() { | ||
116 | hostkeys="$(host_keys_required)" | ||
117 | |||
118 | create_key "Creating SSH1 key; this may take some time ..." \ | ||
119 | "$hostkeys" /etc/ssh/ssh_host_key -t rsa1 | ||
120 | |||
121 | create_key "Creating SSH2 RSA key; this may take some time ..." \ | ||
122 | "$hostkeys" /etc/ssh/ssh_host_rsa_key -t rsa | ||
123 | create_key "Creating SSH2 DSA key; this may take some time ..." \ | ||
124 | "$hostkeys" /etc/ssh/ssh_host_dsa_key -t dsa | ||
125 | } | ||
126 | |||
127 | |||
128 | check_password_auth() { | ||
129 | passwordauth="$(get_config_option PasswordAuthentication)" | ||
130 | crauth="$(get_config_option ChallengeResponseAuthentication)" | ||
131 | if [ "$passwordauth" = no ] && \ | ||
132 | ([ -z "$crauth" ] || [ "$crauth" = yes ]); then | ||
133 | db_get ssh/disable_cr_auth | ||
134 | if [ "$RET" = true ]; then | ||
135 | set_config_option ChallengeResponseAuthentication no | ||
136 | fi | ||
137 | fi | ||
138 | } | ||
139 | |||
140 | |||
141 | move_subsystem_sftp() { | ||
142 | subsystem_sftp="$(get_config_option 'Subsystem sftp')" | ||
143 | if [ "$subsystem_sftp" = /usr/lib/sftp-server ] || \ | ||
144 | [ "$subsystem_sftp" = /usr/libexec/sftp-server ]; then | ||
145 | set_config_option 'Subsystem sftp' /usr/lib/openssh/sftp-server | ||
146 | fi | ||
147 | } | ||
148 | |||
149 | |||
150 | create_sshdconfig() { | ||
151 | if [ -e /etc/ssh/sshd_config ] ; then | ||
152 | if dpkg --compare-versions "$oldversion" lt-nl 1:1.3 ; then | ||
153 | db_get ssh/new_config | ||
154 | if [ "$RET" = "false" ] ; then return 0; fi | ||
155 | else | ||
156 | # Upgrade sshd configuration from a sane version. | ||
157 | |||
158 | if (dpkg --compare-versions "$oldversion" lt-nl 1:3.8p1-1 && \ | ||
159 | ! grep -iq ^UsePAM /etc/ssh/sshd_config) || \ | ||
160 | grep -Eiq '^(PAMAuthenticationViaKbdInt|RhostsAuthentication)' \ | ||
161 | /etc/ssh/sshd_config ; then | ||
162 | # Upgrade from pre-3.7: UsePAM needed to maintain standard | ||
163 | # Debian configuration. | ||
164 | # Note that --compare-versions is sadly not reliable enough | ||
165 | # here due to the package split of ssh into openssh-client | ||
166 | # and openssh-server. The extra grep for some deprecated | ||
167 | # options should with any luck be a good enough heuristic. | ||
168 | echo -n 'Upgrading sshd_config (old version in .dpkg-old) ...' | ||
169 | cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-old | ||
170 | perl -pe 's/^(PAMAuthenticationViaKbdInt|RhostsAuthentication)\b/#$1/i' \ | ||
171 | /etc/ssh/sshd_config > /etc/ssh/sshd_config.dpkg-new | ||
172 | echo >> /etc/ssh/sshd_config.dpkg-new | ||
173 | echo 'UsePAM yes' >> /etc/ssh/sshd_config.dpkg-new | ||
174 | chown --reference /etc/ssh/sshd_config \ | ||
175 | /etc/ssh/sshd_config.dpkg-new | ||
176 | chmod --reference /etc/ssh/sshd_config \ | ||
177 | /etc/ssh/sshd_config.dpkg-new | ||
178 | mv /etc/ssh/sshd_config.dpkg-new /etc/ssh/sshd_config | ||
179 | echo | ||
180 | fi | ||
181 | |||
182 | # An empty version means we're upgrading from before the | ||
183 | # package split, so check. | ||
184 | if dpkg --compare-versions "$oldversion" lt 1:3.8.1p1-11; then | ||
185 | check_password_auth | ||
186 | fi | ||
187 | |||
188 | # libexecdir changed, so fix up 'Subsystem sftp'. | ||
189 | if dpkg --compare-versions "$oldversion" lt 1:4.1p1-1; then | ||
190 | move_subsystem_sftp | ||
191 | fi | ||
192 | |||
193 | return 0 | ||
194 | fi | ||
195 | fi | ||
196 | |||
197 | #Preserve old sshd_config before generating a new one | ||
198 | if [ -e /etc/ssh/sshd_config ] ; then | ||
199 | mv /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-old | ||
200 | fi | ||
201 | |||
202 | cat <<EOF > /etc/ssh/sshd_config | ||
203 | # Package generated configuration file | ||
204 | # See the sshd(8) manpage for details | ||
205 | |||
206 | # What ports, IPs and protocols we listen for | ||
207 | Port 22 | ||
208 | # Use these options to restrict which interfaces/protocols sshd will bind to | ||
209 | #ListenAddress :: | ||
210 | #ListenAddress 0.0.0.0 | ||
211 | Protocol 2 | ||
212 | # HostKeys for protocol version 2 | ||
213 | HostKey /etc/ssh/ssh_host_rsa_key | ||
214 | HostKey /etc/ssh/ssh_host_dsa_key | ||
215 | #Privilege Separation is turned on for security | ||
216 | UsePrivilegeSeparation yes | ||
217 | |||
218 | # Lifetime and size of ephemeral version 1 server key | ||
219 | KeyRegenerationInterval 3600 | ||
220 | ServerKeyBits 768 | ||
221 | |||
222 | # Logging | ||
223 | SyslogFacility AUTH | ||
224 | LogLevel INFO | ||
225 | |||
226 | # Authentication: | ||
227 | LoginGraceTime 120 | ||
228 | PermitRootLogin yes | ||
229 | StrictModes yes | ||
230 | |||
231 | RSAAuthentication yes | ||
232 | PubkeyAuthentication yes | ||
233 | #AuthorizedKeysFile %h/.ssh/authorized_keys | ||
234 | |||
235 | # Don't read the user's ~/.rhosts and ~/.shosts files | ||
236 | IgnoreRhosts yes | ||
237 | # For this to work you will also need host keys in /etc/ssh_known_hosts | ||
238 | RhostsRSAAuthentication no | ||
239 | # similar for protocol version 2 | ||
240 | HostbasedAuthentication no | ||
241 | # Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication | ||
242 | #IgnoreUserKnownHosts yes | ||
243 | |||
244 | # To enable empty passwords, change to yes (NOT RECOMMENDED) | ||
245 | PermitEmptyPasswords no | ||
246 | |||
247 | # Change to yes to enable challenge-response passwords (beware issues with | ||
248 | # some PAM modules and threads) | ||
249 | ChallengeResponseAuthentication no | ||
250 | |||
251 | # Change to no to disable tunnelled clear text passwords | ||
252 | #PasswordAuthentication yes | ||
253 | |||
254 | |||
255 | # To change Kerberos options | ||
256 | #KerberosAuthentication no | ||
257 | #KerberosOrLocalPasswd yes | ||
258 | #AFSTokenPassing no | ||
259 | #KerberosTicketCleanup no | ||
260 | |||
261 | # Kerberos TGT Passing does only work with the AFS kaserver | ||
262 | #KerberosTgtPassing yes | ||
263 | |||
264 | X11Forwarding no | ||
265 | X11DisplayOffset 10 | ||
266 | PrintMotd no | ||
267 | PrintLastLog yes | ||
268 | KeepAlive yes | ||
269 | #UseLogin no | ||
270 | |||
271 | #MaxStartups 10:30:60 | ||
272 | #Banner /etc/issue.net | ||
273 | |||
274 | # Allow client to pass locale environment variables | ||
275 | AcceptEnv LANG LC_* | ||
276 | |||
277 | Subsystem sftp /usr/lib/openssh/sftp-server | ||
278 | |||
279 | UsePAM yes | ||
280 | EOF | ||
281 | } | ||
282 | |||
283 | fix_statoverride() { | ||
284 | # Remove an erronous override for sshd (we should have overridden ssh) | ||
285 | if [ -x /usr/sbin/dpkg-statoverride ]; then | ||
286 | if dpkg-statoverride --list /usr/sbin/sshd >/dev/null ; then | ||
287 | dpkg-statoverride --remove /usr/sbin/sshd | ||
288 | fi | ||
289 | fi | ||
290 | } | ||
291 | |||
292 | setup_sshd_user() { | ||
293 | if ! getent passwd sshd >/dev/null; then | ||
294 | adduser --quiet --system --no-create-home --home /var/run/sshd sshd | ||
295 | fi | ||
296 | } | ||
297 | |||
298 | fix_conffile_permissions() { | ||
299 | # Clean up after executable /etc/default/ssh in 1:3.5p1-5. dpkg | ||
300 | # doesn't do this for us; see bug #192981. | ||
301 | chmod 644 /etc/default/ssh | ||
302 | } | ||
303 | |||
304 | setup_init() { | ||
305 | if [ -x /etc/init.d/ssh ]; then | ||
306 | update-rc.d ssh defaults >/dev/null | ||
307 | if [ -x /usr/sbin/invoke-rc.d ]; then | ||
308 | invoke-rc.d ssh restart | ||
309 | else | ||
310 | /etc/init.d/ssh restart | ||
311 | fi | ||
312 | fi | ||
313 | } | ||
314 | |||
315 | |||
316 | fix_doc_symlink | ||
317 | create_sshdconfig | ||
318 | check_idea_key | ||
319 | create_keys | ||
320 | fix_statoverride | ||
321 | setup_sshd_user | ||
322 | if dpkg --compare-versions "$2" lt 1:3.6.1p2-2; then | ||
323 | fix_conffile_permissions | ||
324 | fi | ||
325 | setup_init | ||
326 | |||
327 | |||
328 | db_stop | ||
329 | |||
330 | exit 0 | ||