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