summaryrefslogtreecommitdiff
path: root/debian/patches
diff options
context:
space:
mode:
Diffstat (limited to 'debian/patches')
-rw-r--r--debian/patches/authorized-keys-man-symlink.patch26
-rw-r--r--debian/patches/conch-old-privkey-format.patch68
-rw-r--r--debian/patches/debian-banner.patch162
-rw-r--r--debian/patches/debian-config.patch270
-rw-r--r--debian/patches/dnssec-sshfp.patch94
-rw-r--r--debian/patches/doc-hash-tab-completion.patch28
-rw-r--r--debian/patches/gnome-ssh-askpass2-icon.patch26
-rw-r--r--debian/patches/gssapi.patch4002
-rw-r--r--debian/patches/keepalive-extensions.patch135
-rw-r--r--debian/patches/mention-ssh-keygen-on-keychange.patch44
-rw-r--r--debian/patches/no-openssl-version-status.patch62
-rw-r--r--debian/patches/openbsd-docs.patch148
-rw-r--r--debian/patches/package-versioning.patch47
-rw-r--r--debian/patches/restore-authorized_keys2.patch35
-rw-r--r--debian/patches/restore-tcp-wrappers.patch172
-rw-r--r--debian/patches/revert-ipqos-defaults.patch93
-rw-r--r--debian/patches/revert-x32-sandbox-breakage.patch39
-rw-r--r--debian/patches/scp-quoting.patch41
-rw-r--r--debian/patches/selinux-role.patch472
-rw-r--r--debian/patches/series27
-rw-r--r--debian/patches/shell-path.patch39
-rw-r--r--debian/patches/ssh-agent-setgid.patch40
-rw-r--r--debian/patches/ssh-argv0.patch31
-rw-r--r--debian/patches/ssh-copy-id-heredoc-syntax.patch37
-rw-r--r--debian/patches/ssh-vulnkey-compat.patch42
-rw-r--r--debian/patches/syslog-level-silent.patch47
-rw-r--r--debian/patches/systemd-readiness.patch84
-rw-r--r--debian/patches/user-group-modes.patch210
28 files changed, 6521 insertions, 0 deletions
diff --git a/debian/patches/authorized-keys-man-symlink.patch b/debian/patches/authorized-keys-man-symlink.patch
new file mode 100644
index 000000000..2680fc739
--- /dev/null
+++ b/debian/patches/authorized-keys-man-symlink.patch
@@ -0,0 +1,26 @@
1From 27ced5f6a3c5dec6e0a78ae138d3db56d49953bd Mon Sep 17 00:00:00 2001
2From: Tomas Pospisek <tpo_deb@sourcepole.ch>
3Date: Sun, 9 Feb 2014 16:10:07 +0000
4Subject: Install authorized_keys(5) as a symlink to sshd(8)
5
6Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1720
7Bug-Debian: http://bugs.debian.org/441817
8Last-Update: 2013-09-14
9
10Patch-Name: authorized-keys-man-symlink.patch
11---
12 Makefile.in | 1 +
13 1 file changed, 1 insertion(+)
14
15diff --git a/Makefile.in b/Makefile.in
16index 56759c388..73e56aaac 100644
17--- a/Makefile.in
18+++ b/Makefile.in
19@@ -408,6 +408,7 @@ install-files:
20 $(INSTALL) -m 644 sshd_config.5.out $(DESTDIR)$(mandir)/$(mansubdir)5/sshd_config.5
21 $(INSTALL) -m 644 ssh_config.5.out $(DESTDIR)$(mandir)/$(mansubdir)5/ssh_config.5
22 $(INSTALL) -m 644 sshd.8.out $(DESTDIR)$(mandir)/$(mansubdir)8/sshd.8
23+ ln -s ../$(mansubdir)8/sshd.8 $(DESTDIR)$(mandir)/$(mansubdir)5/authorized_keys.5
24 $(INSTALL) -m 644 sftp.1.out $(DESTDIR)$(mandir)/$(mansubdir)1/sftp.1
25 $(INSTALL) -m 644 sftp-server.8.out $(DESTDIR)$(mandir)/$(mansubdir)8/sftp-server.8
26 $(INSTALL) -m 644 ssh-keysign.8.out $(DESTDIR)$(mandir)/$(mansubdir)8/ssh-keysign.8
diff --git a/debian/patches/conch-old-privkey-format.patch b/debian/patches/conch-old-privkey-format.patch
new file mode 100644
index 000000000..c7063cece
--- /dev/null
+++ b/debian/patches/conch-old-privkey-format.patch
@@ -0,0 +1,68 @@
1From a73fcc8bab768900ca16d3121303941511b28d45 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Thu, 30 Aug 2018 00:58:56 +0100
4Subject: Work around conch interoperability failure
5
6Twisted Conch fails to read private keys in the new format
7(https://twistedmatrix.com/trac/ticket/9515). Work around this until it
8can be fixed in Twisted.
9
10Forwarded: not-needed
11Last-Update: 2019-10-09
12
13Patch-Name: conch-old-privkey-format.patch
14---
15 regress/Makefile | 2 +-
16 regress/conch-ciphers.sh | 2 +-
17 regress/test-exec.sh | 12 ++++++++++++
18 3 files changed, 14 insertions(+), 2 deletions(-)
19
20diff --git a/regress/Makefile b/regress/Makefile
21index 8b4ed9de3..f50d189bb 100644
22--- a/regress/Makefile
23+++ b/regress/Makefile
24@@ -122,7 +122,7 @@ CLEANFILES= *.core actual agent-key.* authorized_keys_${USERNAME} \
25 rsa_ssh2_crnl.prv scp-ssh-wrapper.exe \
26 scp-ssh-wrapper.scp setuid-allowed sftp-server.log \
27 sftp-server.sh sftp.log ssh-log-wrapper.sh ssh.log \
28- ssh-rsa_oldfmt \
29+ ssh-rsa_oldfmt ssh-rsa_oldfmt.pub \
30 ssh_config ssh_config.* ssh_proxy ssh_proxy_bak \
31 ssh_proxy_envpass sshd.log sshd_config sshd_config_minimal \
32 sshd_config.* sshd_proxy sshd_proxy.* sshd_proxy_bak \
33diff --git a/regress/conch-ciphers.sh b/regress/conch-ciphers.sh
34index 6678813a2..6ff5da20b 100644
35--- a/regress/conch-ciphers.sh
36+++ b/regress/conch-ciphers.sh
37@@ -16,7 +16,7 @@ for c in aes256-ctr aes256-cbc aes192-ctr aes192-cbc aes128-ctr aes128-cbc \
38 rm -f ${COPY}
39 # XXX the 2nd "cat" seems to be needed because of buggy FD handling
40 # in conch
41- ${CONCH} --identity $OBJ/ssh-rsa --port $PORT --user $USER -e none \
42+ ${CONCH} --identity $OBJ/ssh-rsa_oldfmt --port $PORT --user $USER -e none \
43 --known-hosts $OBJ/known_hosts --notty --noagent --nox11 -n \
44 127.0.0.1 "cat ${DATA}" 2>/dev/null | cat > ${COPY}
45 if [ $? -ne 0 ]; then
46diff --git a/regress/test-exec.sh b/regress/test-exec.sh
47index 5dc975d07..d8491b2be 100644
48--- a/regress/test-exec.sh
49+++ b/regress/test-exec.sh
50@@ -587,6 +587,18 @@ REGRESS_INTEROP_CONCH=no
51 if test -x "$CONCH" ; then
52 REGRESS_INTEROP_CONCH=yes
53 fi
54+case "$SCRIPT" in
55+*conch*) ;;
56+*) REGRESS_INTEROP_CONCH=no
57+esac
58+
59+if test "$REGRESS_INTEROP_CONCH" = "yes" ; then
60+ # Convert rsa key to old format to work around
61+ # https://twistedmatrix.com/trac/ticket/9515
62+ cp $OBJ/ssh-rsa $OBJ/ssh-rsa_oldfmt
63+ cp $OBJ/ssh-rsa.pub $OBJ/ssh-rsa_oldfmt.pub
64+ ${SSHKEYGEN} -p -N '' -m PEM -f $OBJ/ssh-rsa_oldfmt >/dev/null
65+fi
66
67 # If PuTTY is present and we are running a PuTTY test, prepare keys and
68 # configuration
diff --git a/debian/patches/debian-banner.patch b/debian/patches/debian-banner.patch
new file mode 100644
index 000000000..82cc37c1b
--- /dev/null
+++ b/debian/patches/debian-banner.patch
@@ -0,0 +1,162 @@
1From 6353ee79cc71ef33a0a34d2d769a5fe327f6260d Mon Sep 17 00:00:00 2001
2From: Kees Cook <kees@debian.org>
3Date: Sun, 9 Feb 2014 16:10:06 +0000
4Subject: Add DebianBanner server configuration option
5
6Setting this to "no" causes sshd to omit the Debian revision from its
7initial protocol handshake, for those scared by package-versioning.patch.
8
9Bug-Debian: http://bugs.debian.org/562048
10Forwarded: not-needed
11Last-Update: 2020-06-07
12
13Patch-Name: debian-banner.patch
14---
15 kex.c | 5 +++--
16 kex.h | 2 +-
17 servconf.c | 9 +++++++++
18 servconf.h | 2 ++
19 sshconnect.c | 2 +-
20 sshd.c | 2 +-
21 sshd_config.5 | 5 +++++
22 7 files changed, 22 insertions(+), 5 deletions(-)
23
24diff --git a/kex.c b/kex.c
25index ce7bb5b3b..763c45536 100644
26--- a/kex.c
27+++ b/kex.c
28@@ -1225,7 +1225,7 @@ send_error(struct ssh *ssh, char *msg)
29 */
30 int
31 kex_exchange_identification(struct ssh *ssh, int timeout_ms,
32- const char *version_addendum)
33+ int debian_banner, const char *version_addendum)
34 {
35 int remote_major, remote_minor, mismatch, oerrno = 0;
36 size_t len, i, n;
37@@ -1243,7 +1243,8 @@ kex_exchange_identification(struct ssh *ssh, int timeout_ms,
38 if (version_addendum != NULL && *version_addendum == '\0')
39 version_addendum = NULL;
40 if ((r = sshbuf_putf(our_version, "SSH-%d.%d-%.100s%s%s\r\n",
41- PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_RELEASE,
42+ PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2,
43+ debian_banner ? SSH_RELEASE : SSH_RELEASE_MINIMUM,
44 version_addendum == NULL ? "" : " ",
45 version_addendum == NULL ? "" : version_addendum)) != 0) {
46 oerrno = errno;
47diff --git a/kex.h b/kex.h
48index fe7141414..938dca03b 100644
49--- a/kex.h
50+++ b/kex.h
51@@ -194,7 +194,7 @@ char *kex_names_cat(const char *, const char *);
52 int kex_assemble_names(char **, const char *, const char *);
53 int kex_gss_names_valid(const char *);
54
55-int kex_exchange_identification(struct ssh *, int, const char *);
56+int kex_exchange_identification(struct ssh *, int, int, const char *);
57
58 struct kex *kex_new(void);
59 int kex_ready(struct ssh *, char *[PROPOSAL_MAX]);
60diff --git a/servconf.c b/servconf.c
61index 21abe41ac..f9eb778d6 100644
62--- a/servconf.c
63+++ b/servconf.c
64@@ -195,6 +195,7 @@ initialize_server_options(ServerOptions *options)
65 options->fingerprint_hash = -1;
66 options->disable_forwarding = -1;
67 options->expose_userauth_info = -1;
68+ options->debian_banner = -1;
69 }
70
71 /* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
72@@ -469,6 +470,8 @@ fill_default_server_options(ServerOptions *options)
73 options->expose_userauth_info = 0;
74 if (options->sk_provider == NULL)
75 options->sk_provider = xstrdup("internal");
76+ if (options->debian_banner == -1)
77+ options->debian_banner = 1;
78
79 assemble_algorithms(options);
80
81@@ -548,6 +551,7 @@ typedef enum {
82 sStreamLocalBindMask, sStreamLocalBindUnlink,
83 sAllowStreamLocalForwarding, sFingerprintHash, sDisableForwarding,
84 sExposeAuthInfo, sRDomain, sPubkeyAuthOptions, sSecurityKeyProvider,
85+ sDebianBanner,
86 sDeprecated, sIgnore, sUnsupported
87 } ServerOpCodes;
88
89@@ -712,6 +716,7 @@ static struct {
90 { "rdomain", sRDomain, SSHCFG_ALL },
91 { "casignaturealgorithms", sCASignatureAlgorithms, SSHCFG_ALL },
92 { "securitykeyprovider", sSecurityKeyProvider, SSHCFG_GLOBAL },
93+ { "debianbanner", sDebianBanner, SSHCFG_GLOBAL },
94 { NULL, sBadOption, 0 }
95 };
96
97@@ -2402,6 +2407,10 @@ process_server_config_line_depth(ServerOptions *options, char *line,
98 *charptr = xstrdup(arg);
99 break;
100
101+ case sDebianBanner:
102+ intptr = &options->debian_banner;
103+ goto parse_flag;
104+
105 case sDeprecated:
106 case sIgnore:
107 case sUnsupported:
108diff --git a/servconf.h b/servconf.h
109index f10908e5b..4afdf24d0 100644
110--- a/servconf.h
111+++ b/servconf.h
112@@ -227,6 +227,8 @@ typedef struct {
113 int expose_userauth_info;
114 u_int64_t timing_secret;
115 char *sk_provider;
116+
117+ int debian_banner;
118 } ServerOptions;
119
120 /* Information about the incoming connection as used by Match */
121diff --git a/sshconnect.c b/sshconnect.c
122index 3ae20b74e..bab3916d8 100644
123--- a/sshconnect.c
124+++ b/sshconnect.c
125@@ -1296,7 +1296,7 @@ ssh_login(struct ssh *ssh, Sensitive *sensitive, const char *orighost,
126 lowercase(host);
127
128 /* Exchange protocol version identification strings with the server. */
129- if ((r = kex_exchange_identification(ssh, timeout_ms, NULL)) != 0)
130+ if ((r = kex_exchange_identification(ssh, timeout_ms, 1, NULL)) != 0)
131 sshpkt_fatal(ssh, r, "banner exchange");
132
133 /* Put the connection into non-blocking mode. */
134diff --git a/sshd.c b/sshd.c
135index 38d281ab4..50f2726bf 100644
136--- a/sshd.c
137+++ b/sshd.c
138@@ -2232,7 +2232,7 @@ main(int ac, char **av)
139 if (!debug_flag)
140 alarm(options.login_grace_time);
141
142- if ((r = kex_exchange_identification(ssh, -1,
143+ if ((r = kex_exchange_identification(ssh, -1, options.debian_banner,
144 options.version_addendum)) != 0)
145 sshpkt_fatal(ssh, r, "banner exchange");
146
147diff --git a/sshd_config.5 b/sshd_config.5
148index 6457620bb..33dc0c675 100644
149--- a/sshd_config.5
150+++ b/sshd_config.5
151@@ -540,6 +540,11 @@ or
152 .Cm no .
153 The default is
154 .Cm yes .
155+.It Cm DebianBanner
156+Specifies whether the distribution-specified extra version suffix is
157+included during initial protocol handshake.
158+The default is
159+.Cm yes .
160 .It Cm DenyGroups
161 This keyword can be followed by a list of group name patterns, separated
162 by spaces.
diff --git a/debian/patches/debian-config.patch b/debian/patches/debian-config.patch
new file mode 100644
index 000000000..aa370e52f
--- /dev/null
+++ b/debian/patches/debian-config.patch
@@ -0,0 +1,270 @@
1From a0c9f82b05d33f3e2cf8e5442cee47c09d1a1dd8 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Sun, 9 Feb 2014 16:10:18 +0000
4Subject: Various Debian-specific configuration changes
5
6ssh: Enable ForwardX11Trusted, returning to earlier semantics which cause
7fewer problems with existing setups (http://bugs.debian.org/237021).
8
9ssh: Set 'SendEnv LANG LC_*' by default (http://bugs.debian.org/264024).
10
11ssh: Enable HashKnownHosts by default to try to limit the spread of ssh
12worms.
13
14ssh: Enable GSSAPIAuthentication by default.
15
16ssh: Include /etc/ssh/ssh_config.d/*.conf.
17
18sshd: Enable PAM, disable ChallengeResponseAuthentication, and disable
19PrintMotd.
20
21sshd: Enable X11Forwarding.
22
23sshd: Set 'AcceptEnv LANG LC_*' by default.
24
25sshd: Change sftp subsystem path to /usr/lib/openssh/sftp-server.
26
27sshd: Include /etc/ssh/sshd_config.d/*.conf.
28
29Document all of this.
30
31Author: Russ Allbery <rra@debian.org>
32Forwarded: not-needed
33Last-Update: 2020-10-18
34
35Patch-Name: debian-config.patch
36---
37 readconf.c | 2 +-
38 ssh.1 | 24 ++++++++++++++++++++++++
39 ssh_config | 8 +++++++-
40 ssh_config.5 | 26 +++++++++++++++++++++++++-
41 sshd_config | 18 ++++++++++++------
42 sshd_config.5 | 29 +++++++++++++++++++++++++++++
43 6 files changed, 98 insertions(+), 9 deletions(-)
44
45diff --git a/readconf.c b/readconf.c
46index f4f273c96..e676b6be6 100644
47--- a/readconf.c
48+++ b/readconf.c
49@@ -2153,7 +2153,7 @@ fill_default_options(Options * options)
50 if (options->forward_x11 == -1)
51 options->forward_x11 = 0;
52 if (options->forward_x11_trusted == -1)
53- options->forward_x11_trusted = 0;
54+ options->forward_x11_trusted = 1;
55 if (options->forward_x11_timeout == -1)
56 options->forward_x11_timeout = 1200;
57 /*
58diff --git a/ssh.1 b/ssh.1
59index 76ddd89b5..ad48fc8c8 100644
60--- a/ssh.1
61+++ b/ssh.1
62@@ -812,6 +812,16 @@ directive in
63 .Xr ssh_config 5
64 for more information.
65 .Pp
66+(Debian-specific: X11 forwarding is not subjected to X11 SECURITY extension
67+restrictions by default, because too many programs currently crash in this
68+mode.
69+Set the
70+.Cm ForwardX11Trusted
71+option to
72+.Dq no
73+to restore the upstream behaviour.
74+This may change in future depending on client-side improvements.)
75+.Pp
76 .It Fl x
77 Disables X11 forwarding.
78 .Pp
79@@ -820,6 +830,20 @@ Enables trusted X11 forwarding.
80 Trusted X11 forwardings are not subjected to the X11 SECURITY extension
81 controls.
82 .Pp
83+(Debian-specific: In the default configuration, this option is equivalent to
84+.Fl X ,
85+since
86+.Cm ForwardX11Trusted
87+defaults to
88+.Dq yes
89+as described above.
90+Set the
91+.Cm ForwardX11Trusted
92+option to
93+.Dq no
94+to restore the upstream behaviour.
95+This may change in future depending on client-side improvements.)
96+.Pp
97 .It Fl y
98 Send log information using the
99 .Xr syslog 3
100diff --git a/ssh_config b/ssh_config
101index 52aae8692..09a17cf18 100644
102--- a/ssh_config
103+++ b/ssh_config
104@@ -17,9 +17,12 @@
105 # list of available options, their meanings and defaults, please see the
106 # ssh_config(5) man page.
107
108-# Host *
109+Include /etc/ssh/ssh_config.d/*.conf
110+
111+Host *
112 # ForwardAgent no
113 # ForwardX11 no
114+# ForwardX11Trusted yes
115 # PasswordAuthentication yes
116 # HostbasedAuthentication no
117 # GSSAPIAuthentication no
118@@ -46,3 +49,6 @@
119 # ProxyCommand ssh -q -W %h:%p gateway.example.com
120 # RekeyLimit 1G 1h
121 # UserKnownHostsFile ~/.ssh/known_hosts.d/%k
122+ SendEnv LANG LC_*
123+ HashKnownHosts yes
124+ GSSAPIAuthentication yes
125diff --git a/ssh_config.5 b/ssh_config.5
126index 96ca7a5df..6d6c59521 100644
127--- a/ssh_config.5
128+++ b/ssh_config.5
129@@ -71,6 +71,29 @@ Since the first obtained value for each parameter is used, more
130 host-specific declarations should be given near the beginning of the
131 file, and general defaults at the end.
132 .Pp
133+Note that the Debian
134+.Ic openssh-client
135+package sets several options as standard in
136+.Pa /etc/ssh/ssh_config
137+which are not the default in
138+.Xr ssh 1 :
139+.Pp
140+.Bl -bullet -offset indent -compact
141+.It
142+.Cm Include /etc/ssh/ssh_config.d/*.conf
143+.It
144+.Cm SendEnv No LANG LC_*
145+.It
146+.Cm HashKnownHosts No yes
147+.It
148+.Cm GSSAPIAuthentication No yes
149+.El
150+.Pp
151+.Pa /etc/ssh/ssh_config.d/*.conf
152+files are included at the start of the system-wide configuration file, so
153+options set there will override those in
154+.Pa /etc/ssh/ssh_config.
155+.Pp
156 The file contains keyword-argument pairs, one per line.
157 Lines starting with
158 .Ql #
159@@ -742,11 +765,12 @@ elapsed.
160 .It Cm ForwardX11Trusted
161 If this option is set to
162 .Cm yes ,
163+(the Debian-specific default),
164 remote X11 clients will have full access to the original X11 display.
165 .Pp
166 If this option is set to
167 .Cm no
168-(the default),
169+(the upstream default),
170 remote X11 clients will be considered untrusted and prevented
171 from stealing or tampering with data belonging to trusted X11
172 clients.
173diff --git a/sshd_config b/sshd_config
174index 2c48105f8..459c1b230 100644
175--- a/sshd_config
176+++ b/sshd_config
177@@ -10,6 +10,8 @@
178 # possible, but leave them commented. Uncommented options override the
179 # default value.
180
181+Include /etc/ssh/sshd_config.d/*.conf
182+
183 #Port 22
184 #AddressFamily any
185 #ListenAddress 0.0.0.0
186@@ -57,8 +59,9 @@ AuthorizedKeysFile .ssh/authorized_keys
187 #PasswordAuthentication yes
188 #PermitEmptyPasswords no
189
190-# Change to no to disable s/key passwords
191-#ChallengeResponseAuthentication yes
192+# Change to yes to enable challenge-response passwords (beware issues with
193+# some PAM modules and threads)
194+ChallengeResponseAuthentication no
195
196 # Kerberos options
197 #KerberosAuthentication no
198@@ -81,16 +84,16 @@ AuthorizedKeysFile .ssh/authorized_keys
199 # If you just want the PAM account and session checks to run without
200 # PAM authentication, then enable this but set PasswordAuthentication
201 # and ChallengeResponseAuthentication to 'no'.
202-#UsePAM no
203+UsePAM yes
204
205 #AllowAgentForwarding yes
206 #AllowTcpForwarding yes
207 #GatewayPorts no
208-#X11Forwarding no
209+X11Forwarding yes
210 #X11DisplayOffset 10
211 #X11UseLocalhost yes
212 #PermitTTY yes
213-#PrintMotd yes
214+PrintMotd no
215 #PrintLastLog yes
216 #TCPKeepAlive yes
217 #PermitUserEnvironment no
218@@ -107,8 +110,11 @@ AuthorizedKeysFile .ssh/authorized_keys
219 # no default banner path
220 #Banner none
221
222+# Allow client to pass locale environment variables
223+AcceptEnv LANG LC_*
224+
225 # override default of no subsystems
226-Subsystem sftp /usr/libexec/sftp-server
227+Subsystem sftp /usr/lib/openssh/sftp-server
228
229 # Example of overriding settings on a per-user basis
230 #Match User anoncvs
231diff --git a/sshd_config.5 b/sshd_config.5
232index 32ae46476..472001dd1 100644
233--- a/sshd_config.5
234+++ b/sshd_config.5
235@@ -56,6 +56,35 @@ Arguments may optionally be enclosed in double quotes
236 .Pq \&"
237 in order to represent arguments containing spaces.
238 .Pp
239+Note that the Debian
240+.Ic openssh-server
241+package sets several options as standard in
242+.Pa /etc/ssh/sshd_config
243+which are not the default in
244+.Xr sshd 8 :
245+.Pp
246+.Bl -bullet -offset indent -compact
247+.It
248+.Cm Include /etc/ssh/sshd_config.d/*.conf
249+.It
250+.Cm ChallengeResponseAuthentication No no
251+.It
252+.Cm X11Forwarding No yes
253+.It
254+.Cm PrintMotd No no
255+.It
256+.Cm AcceptEnv No LANG LC_*
257+.It
258+.Cm Subsystem No sftp /usr/lib/openssh/sftp-server
259+.It
260+.Cm UsePAM No yes
261+.El
262+.Pp
263+.Pa /etc/ssh/sshd_config.d/*.conf
264+files are included at the start of the configuration file, so options set
265+there will override those in
266+.Pa /etc/ssh/sshd_config.
267+.Pp
268 The possible
269 keywords and their meanings are as follows (note that
270 keywords are case-insensitive and arguments are case-sensitive):
diff --git a/debian/patches/dnssec-sshfp.patch b/debian/patches/dnssec-sshfp.patch
new file mode 100644
index 000000000..23ecc0d3d
--- /dev/null
+++ b/debian/patches/dnssec-sshfp.patch
@@ -0,0 +1,94 @@
1From 78a7702d88713e854550a05fa9b8670f219d9bf9 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Sun, 9 Feb 2014 16:10:01 +0000
4Subject: Force use of DNSSEC even if "options edns0" isn't in resolv.conf
5
6This allows SSHFP DNS records to be verified if glibc 2.11 is installed.
7
8Origin: vendor, https://cvs.fedoraproject.org/viewvc/F-12/openssh/openssh-5.2p1-edns.patch?revision=1.1&view=markup
9Bug: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=572049
10Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=572049
11Last-Update: 2010-04-06
12
13Patch-Name: dnssec-sshfp.patch
14---
15 dns.c | 14 +++++++++++++-
16 openbsd-compat/getrrsetbyname.c | 10 +++++-----
17 openbsd-compat/getrrsetbyname.h | 3 +++
18 3 files changed, 21 insertions(+), 6 deletions(-)
19
20diff --git a/dns.c b/dns.c
21index e4f9bf830..9c9fe6413 100644
22--- a/dns.c
23+++ b/dns.c
24@@ -210,6 +210,7 @@ verify_host_key_dns(const char *hostname, struct sockaddr *address,
25 {
26 u_int counter;
27 int result;
28+ unsigned int rrset_flags = 0;
29 struct rrsetinfo *fingerprints = NULL;
30
31 u_int8_t hostkey_algorithm;
32@@ -233,8 +234,19 @@ verify_host_key_dns(const char *hostname, struct sockaddr *address,
33 return -1;
34 }
35
36+ /*
37+ * Original getrrsetbyname function, found on OpenBSD for example,
38+ * doesn't accept any flag and prerequisite for obtaining AD bit in
39+ * DNS response is set by "options edns0" in resolv.conf.
40+ *
41+ * Our version is more clever and use RRSET_FORCE_EDNS0 flag.
42+ */
43+#ifndef HAVE_GETRRSETBYNAME
44+ rrset_flags |= RRSET_FORCE_EDNS0;
45+#endif
46 result = getrrsetbyname(hostname, DNS_RDATACLASS_IN,
47- DNS_RDATATYPE_SSHFP, 0, &fingerprints);
48+ DNS_RDATATYPE_SSHFP, rrset_flags, &fingerprints);
49+
50 if (result) {
51 verbose("DNS lookup error: %s", dns_result_totext(result));
52 return -1;
53diff --git a/openbsd-compat/getrrsetbyname.c b/openbsd-compat/getrrsetbyname.c
54index dc6fe0533..e061a290a 100644
55--- a/openbsd-compat/getrrsetbyname.c
56+++ b/openbsd-compat/getrrsetbyname.c
57@@ -209,8 +209,8 @@ getrrsetbyname(const char *hostname, unsigned int rdclass,
58 goto fail;
59 }
60
61- /* don't allow flags yet, unimplemented */
62- if (flags) {
63+ /* Allow RRSET_FORCE_EDNS0 flag only. */
64+ if ((flags & !RRSET_FORCE_EDNS0) != 0) {
65 result = ERRSET_INVAL;
66 goto fail;
67 }
68@@ -226,9 +226,9 @@ getrrsetbyname(const char *hostname, unsigned int rdclass,
69 #endif /* DEBUG */
70
71 #ifdef RES_USE_DNSSEC
72- /* turn on DNSSEC if EDNS0 is configured */
73- if (_resp->options & RES_USE_EDNS0)
74- _resp->options |= RES_USE_DNSSEC;
75+ /* turn on DNSSEC if required */
76+ if (flags & RRSET_FORCE_EDNS0)
77+ _resp->options |= (RES_USE_EDNS0|RES_USE_DNSSEC);
78 #endif /* RES_USE_DNSEC */
79
80 /* make query */
81diff --git a/openbsd-compat/getrrsetbyname.h b/openbsd-compat/getrrsetbyname.h
82index 1283f5506..dbbc85a2a 100644
83--- a/openbsd-compat/getrrsetbyname.h
84+++ b/openbsd-compat/getrrsetbyname.h
85@@ -72,6 +72,9 @@
86 #ifndef RRSET_VALIDATED
87 # define RRSET_VALIDATED 1
88 #endif
89+#ifndef RRSET_FORCE_EDNS0
90+# define RRSET_FORCE_EDNS0 0x0001
91+#endif
92
93 /*
94 * Return codes for getrrsetbyname()
diff --git a/debian/patches/doc-hash-tab-completion.patch b/debian/patches/doc-hash-tab-completion.patch
new file mode 100644
index 000000000..3e96f3b8e
--- /dev/null
+++ b/debian/patches/doc-hash-tab-completion.patch
@@ -0,0 +1,28 @@
1From 5fca8a730171f96a72007118c0d35cf4a09359f8 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Sun, 9 Feb 2014 16:10:11 +0000
4Subject: Document that HashKnownHosts may break tab-completion
5
6Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1727
7Bug-Debian: http://bugs.debian.org/430154
8Last-Update: 2013-09-14
9
10Patch-Name: doc-hash-tab-completion.patch
11---
12 ssh_config.5 | 3 +++
13 1 file changed, 3 insertions(+)
14
15diff --git a/ssh_config.5 b/ssh_config.5
16index 190e1d927..96ca7a5df 100644
17--- a/ssh_config.5
18+++ b/ssh_config.5
19@@ -861,6 +861,9 @@ Note that existing names and addresses in known hosts files
20 will not be converted automatically,
21 but may be manually hashed using
22 .Xr ssh-keygen 1 .
23+Use of this option may break facilities such as tab-completion that rely
24+on being able to read unhashed host names from
25+.Pa ~/.ssh/known_hosts .
26 .It Cm HostbasedAuthentication
27 Specifies whether to try rhosts based authentication with public key
28 authentication.
diff --git a/debian/patches/gnome-ssh-askpass2-icon.patch b/debian/patches/gnome-ssh-askpass2-icon.patch
new file mode 100644
index 000000000..d7d0bed64
--- /dev/null
+++ b/debian/patches/gnome-ssh-askpass2-icon.patch
@@ -0,0 +1,26 @@
1From c26f6f9c7051b9ab2ac13d1d227e6d39527839cc Mon Sep 17 00:00:00 2001
2From: Vincent Untz <vuntz@ubuntu.com>
3Date: Sun, 9 Feb 2014 16:10:16 +0000
4Subject: Give the ssh-askpass-gnome window a default icon
5
6Bug-Ubuntu: https://bugs.launchpad.net/bugs/27152
7Last-Update: 2010-02-28
8
9Patch-Name: gnome-ssh-askpass2-icon.patch
10---
11 contrib/gnome-ssh-askpass2.c | 2 ++
12 1 file changed, 2 insertions(+)
13
14diff --git a/contrib/gnome-ssh-askpass2.c b/contrib/gnome-ssh-askpass2.c
15index f7912727c..bf8c92c8f 100644
16--- a/contrib/gnome-ssh-askpass2.c
17+++ b/contrib/gnome-ssh-askpass2.c
18@@ -322,6 +322,8 @@ main(int argc, char **argv)
19
20 gtk_init(&argc, &argv);
21
22+ gtk_window_set_default_icon_from_file ("/usr/share/pixmaps/ssh-askpass-gnome.png", NULL);
23+
24 if (argc > 1) {
25 message = g_strjoinv(" ", argv + 1);
26 } else {
diff --git a/debian/patches/gssapi.patch b/debian/patches/gssapi.patch
new file mode 100644
index 000000000..d779eacb6
--- /dev/null
+++ b/debian/patches/gssapi.patch
@@ -0,0 +1,4002 @@
1From d1b7918f9bce6e997c7952ac795e18d09192b2a6 Mon Sep 17 00:00:00 2001
2From: Simon Wilkinson <simon@sxw.org.uk>
3Date: Sun, 9 Feb 2014 16:09:48 +0000
4Subject: GSSAPI key exchange support
5
6This patch has been rejected upstream: "None of the OpenSSH developers are
7in favour of adding this, and this situation has not changed for several
8years. This is not a slight on Simon's patch, which is of fine quality, but
9just that a) we don't trust GSSAPI implementations that much and b) we don't
10like adding new KEX since they are pre-auth attack surface. This one is
11particularly scary, since it requires hooks out to typically root-owned
12system resources."
13
14However, quite a lot of people rely on this in Debian, and it's better to
15have it merged into the main openssh package rather than having separate
16-krb5 packages (as we used to have). It seems to have a generally good
17security history.
18
19Author: Simon Wilkinson <simon@sxw.org.uk>
20Author: Colin Watson <cjwatson@debian.org>
21Author: Jakub Jelen <jjelen@redhat.com>
22Origin: other, https://github.com/openssh-gsskex/openssh-gsskex/commits/debian/master
23Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1242
24Last-Updated: 2020-06-07
25
26Patch-Name: gssapi.patch
27---
28 Makefile.in | 3 +-
29 README.md | 33 +++
30 auth.c | 96 +-------
31 auth2-gss.c | 56 ++++-
32 auth2.c | 2 +
33 canohost.c | 93 ++++++++
34 canohost.h | 3 +
35 clientloop.c | 15 +-
36 configure.ac | 24 ++
37 gss-genr.c | 300 +++++++++++++++++++++++-
38 gss-serv-krb5.c | 85 ++++++-
39 gss-serv.c | 186 +++++++++++++--
40 kex.c | 66 +++++-
41 kex.h | 29 +++
42 kexdh.c | 10 +
43 kexgen.c | 2 +-
44 kexgssc.c | 606 ++++++++++++++++++++++++++++++++++++++++++++++++
45 kexgsss.c | 474 +++++++++++++++++++++++++++++++++++++
46 monitor.c | 139 ++++++++++-
47 monitor.h | 2 +
48 monitor_wrap.c | 57 ++++-
49 monitor_wrap.h | 4 +-
50 readconf.c | 70 ++++++
51 readconf.h | 6 +
52 servconf.c | 47 ++++
53 servconf.h | 3 +
54 session.c | 10 +-
55 ssh-gss.h | 54 ++++-
56 ssh.1 | 8 +
57 ssh.c | 6 +-
58 ssh_config | 2 +
59 ssh_config.5 | 57 +++++
60 sshconnect2.c | 154 +++++++++++-
61 sshd.c | 62 ++++-
62 sshd_config | 2 +
63 sshd_config.5 | 30 +++
64 sshkey.c | 3 +-
65 sshkey.h | 1 +
66 38 files changed, 2640 insertions(+), 160 deletions(-)
67 create mode 100644 kexgssc.c
68 create mode 100644 kexgsss.c
69
70diff --git a/Makefile.in b/Makefile.in
71index acfb919da..56759c388 100644
72--- a/Makefile.in
73+++ b/Makefile.in
74@@ -107,6 +107,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
75 kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
76 kexgexc.o kexgexs.o \
77 sntrup4591761.o kexsntrup4591761x25519.o kexgen.o \
78+ kexgssc.o \
79 sftp-realpath.o platform-pledge.o platform-tracing.o platform-misc.o \
80 sshbuf-io.o
81
82@@ -123,7 +124,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passwd.o \
83 auth-bsdauth.o auth2-hostbased.o auth2-kbdint.o \
84 auth2-none.o auth2-passwd.o auth2-pubkey.o \
85 monitor.o monitor_wrap.o auth-krb5.o \
86- auth2-gss.o gss-serv.o gss-serv-krb5.o \
87+ auth2-gss.o gss-serv.o gss-serv-krb5.o kexgsss.o \
88 loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \
89 sftp-server.o sftp-common.o \
90 sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \
91diff --git a/README.md b/README.md
92index 28fb43d2a..5b73d24c0 100644
93--- a/README.md
94+++ b/README.md
95@@ -1,3 +1,36 @@
96+Portable OpenSSH with GSSAPI Key Exchange patches
97+=================================================
98+
99+Currently, there are two branches with gssapi key exchange related
100+patches:
101+
102+ * fedora/master: Changes that are shipped in Fedora
103+ * debian/master: Changes that are shipped in Debian
104+
105+The target is to converge to a shared repository with single master
106+branch from where we could build releases for both OSes.
107+
108+
109+What is in:
110+
111+ * The original patch implementing missing parts of RFC4462 by Simon Wilkinson
112+ adapted to the current OpenSSH versions and with several fixes
113+ * New methods for GSSAPI Kex from IETF draft [1] from Jakub Jelen
114+
115+
116+Missing kerberos-related parts:
117+
118+ * .k5login and .kusers support available in Fedora [2] [3].
119+ * Improved handling of kerberos ccache location [4]
120+
121+
122+[1] https://tools.ietf.org/html/draft-ietf-curdle-gss-keyex-sha2-08
123+[2] https://src.fedoraproject.org/rpms/openssh/blob/master/f/openssh-6.6p1-kuserok.patch
124+[3] https://src.fedoraproject.org/rpms/openssh/blob/master/f/openssh-6.6p1-GSSAPIEnablek5users.patch
125+[4] https://bugzilla.mindrot.org/show_bug.cgi?id=2775
126+
127+-------------------------------------------------------------------------------
128+
129 # Portable OpenSSH
130
131 [![Fuzzing Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/openssh.svg)](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:openssh)
132diff --git a/auth.c b/auth.c
133index 9a5498b66..3d31ec860 100644
134--- a/auth.c
135+++ b/auth.c
136@@ -400,7 +400,8 @@ auth_root_allowed(struct ssh *ssh, const char *method)
137 case PERMIT_NO_PASSWD:
138 if (strcmp(method, "publickey") == 0 ||
139 strcmp(method, "hostbased") == 0 ||
140- strcmp(method, "gssapi-with-mic") == 0)
141+ strcmp(method, "gssapi-with-mic") == 0 ||
142+ strcmp(method, "gssapi-keyex") == 0)
143 return 1;
144 break;
145 case PERMIT_FORCED_ONLY:
146@@ -724,99 +725,6 @@ fakepw(void)
147 return (&fake);
148 }
149
150-/*
151- * Returns the remote DNS hostname as a string. The returned string must not
152- * be freed. NB. this will usually trigger a DNS query the first time it is
153- * called.
154- * This function does additional checks on the hostname to mitigate some
155- * attacks on legacy rhosts-style authentication.
156- * XXX is RhostsRSAAuthentication vulnerable to these?
157- * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
158- */
159-
160-static char *
161-remote_hostname(struct ssh *ssh)
162-{
163- struct sockaddr_storage from;
164- socklen_t fromlen;
165- struct addrinfo hints, *ai, *aitop;
166- char name[NI_MAXHOST], ntop2[NI_MAXHOST];
167- const char *ntop = ssh_remote_ipaddr(ssh);
168-
169- /* Get IP address of client. */
170- fromlen = sizeof(from);
171- memset(&from, 0, sizeof(from));
172- if (getpeername(ssh_packet_get_connection_in(ssh),
173- (struct sockaddr *)&from, &fromlen) == -1) {
174- debug("getpeername failed: %.100s", strerror(errno));
175- return xstrdup(ntop);
176- }
177-
178- ipv64_normalise_mapped(&from, &fromlen);
179- if (from.ss_family == AF_INET6)
180- fromlen = sizeof(struct sockaddr_in6);
181-
182- debug3("Trying to reverse map address %.100s.", ntop);
183- /* Map the IP address to a host name. */
184- if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
185- NULL, 0, NI_NAMEREQD) != 0) {
186- /* Host name not found. Use ip address. */
187- return xstrdup(ntop);
188- }
189-
190- /*
191- * if reverse lookup result looks like a numeric hostname,
192- * someone is trying to trick us by PTR record like following:
193- * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
194- */
195- memset(&hints, 0, sizeof(hints));
196- hints.ai_socktype = SOCK_DGRAM; /*dummy*/
197- hints.ai_flags = AI_NUMERICHOST;
198- if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
199- logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
200- name, ntop);
201- freeaddrinfo(ai);
202- return xstrdup(ntop);
203- }
204-
205- /* Names are stored in lowercase. */
206- lowercase(name);
207-
208- /*
209- * Map it back to an IP address and check that the given
210- * address actually is an address of this host. This is
211- * necessary because anyone with access to a name server can
212- * define arbitrary names for an IP address. Mapping from
213- * name to IP address can be trusted better (but can still be
214- * fooled if the intruder has access to the name server of
215- * the domain).
216- */
217- memset(&hints, 0, sizeof(hints));
218- hints.ai_family = from.ss_family;
219- hints.ai_socktype = SOCK_STREAM;
220- if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
221- logit("reverse mapping checking getaddrinfo for %.700s "
222- "[%s] failed.", name, ntop);
223- return xstrdup(ntop);
224- }
225- /* Look for the address from the list of addresses. */
226- for (ai = aitop; ai; ai = ai->ai_next) {
227- if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
228- sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
229- (strcmp(ntop, ntop2) == 0))
230- break;
231- }
232- freeaddrinfo(aitop);
233- /* If we reached the end of the list, the address was not there. */
234- if (ai == NULL) {
235- /* Address not found for the host name. */
236- logit("Address %.100s maps to %.600s, but this does not "
237- "map back to the address.", ntop, name);
238- return xstrdup(ntop);
239- }
240- return xstrdup(name);
241-}
242-
243 /*
244 * Return the canonical name of the host in the other side of the current
245 * connection. The host name is cached, so it is efficient to call this
246diff --git a/auth2-gss.c b/auth2-gss.c
247index 9351e0428..d6446c0cf 100644
248--- a/auth2-gss.c
249+++ b/auth2-gss.c
250@@ -1,7 +1,7 @@
251 /* $OpenBSD: auth2-gss.c,v 1.29 2018/07/31 03:10:27 djm Exp $ */
252
253 /*
254- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
255+ * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
256 *
257 * Redistribution and use in source and binary forms, with or without
258 * modification, are permitted provided that the following conditions
259@@ -54,6 +54,48 @@ static int input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh);
260 static int input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh);
261 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
262
263+/*
264+ * The 'gssapi_keyex' userauth mechanism.
265+ */
266+static int
267+userauth_gsskeyex(struct ssh *ssh)
268+{
269+ Authctxt *authctxt = ssh->authctxt;
270+ int r, authenticated = 0;
271+ struct sshbuf *b = NULL;
272+ gss_buffer_desc mic, gssbuf;
273+ u_char *p;
274+ size_t len;
275+
276+ if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
277+ (r = sshpkt_get_end(ssh)) != 0)
278+ fatal("%s: %s", __func__, ssh_err(r));
279+
280+ if ((b = sshbuf_new()) == NULL)
281+ fatal("%s: sshbuf_new failed", __func__);
282+
283+ mic.value = p;
284+ mic.length = len;
285+
286+ ssh_gssapi_buildmic(b, authctxt->user, authctxt->service,
287+ "gssapi-keyex");
288+
289+ if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
290+ fatal("%s: sshbuf_mutable_ptr failed", __func__);
291+ gssbuf.length = sshbuf_len(b);
292+
293+ /* gss_kex_context is NULL with privsep, so we can't check it here */
294+ if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context,
295+ &gssbuf, &mic))))
296+ authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
297+ authctxt->pw, 1));
298+
299+ sshbuf_free(b);
300+ free(mic.value);
301+
302+ return (authenticated);
303+}
304+
305 /*
306 * We only support those mechanisms that we know about (ie ones that we know
307 * how to check local user kuserok and the like)
308@@ -260,7 +302,8 @@ input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh)
309 if ((r = sshpkt_get_end(ssh)) != 0)
310 fatal("%s: %s", __func__, ssh_err(r));
311
312- authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
313+ authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
314+ authctxt->pw, 1));
315
316 if ((!use_privsep || mm_is_monitor()) &&
317 (displayname = ssh_gssapi_displayname()) != NULL)
318@@ -306,7 +349,8 @@ input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh)
319 gssbuf.length = sshbuf_len(b);
320
321 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
322- authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
323+ authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
324+ authctxt->pw, 0));
325 else
326 logit("GSSAPI MIC check failed");
327
328@@ -326,6 +370,12 @@ input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh)
329 return 0;
330 }
331
332+Authmethod method_gsskeyex = {
333+ "gssapi-keyex",
334+ userauth_gsskeyex,
335+ &options.gss_authentication
336+};
337+
338 Authmethod method_gssapi = {
339 "gssapi-with-mic",
340 userauth_gssapi,
341diff --git a/auth2.c b/auth2.c
342index 242a7adbe..9fa1404b3 100644
343--- a/auth2.c
344+++ b/auth2.c
345@@ -73,6 +73,7 @@ extern Authmethod method_passwd;
346 extern Authmethod method_kbdint;
347 extern Authmethod method_hostbased;
348 #ifdef GSSAPI
349+extern Authmethod method_gsskeyex;
350 extern Authmethod method_gssapi;
351 #endif
352
353@@ -80,6 +81,7 @@ Authmethod *authmethods[] = {
354 &method_none,
355 &method_pubkey,
356 #ifdef GSSAPI
357+ &method_gsskeyex,
358 &method_gssapi,
359 #endif
360 &method_passwd,
361diff --git a/canohost.c b/canohost.c
362index abea9c6e6..8e81b5193 100644
363--- a/canohost.c
364+++ b/canohost.c
365@@ -35,6 +35,99 @@
366 #include "canohost.h"
367 #include "misc.h"
368
369+/*
370+ * Returns the remote DNS hostname as a string. The returned string must not
371+ * be freed. NB. this will usually trigger a DNS query the first time it is
372+ * called.
373+ * This function does additional checks on the hostname to mitigate some
374+ * attacks on legacy rhosts-style authentication.
375+ * XXX is RhostsRSAAuthentication vulnerable to these?
376+ * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
377+ */
378+
379+char *
380+remote_hostname(struct ssh *ssh)
381+{
382+ struct sockaddr_storage from;
383+ socklen_t fromlen;
384+ struct addrinfo hints, *ai, *aitop;
385+ char name[NI_MAXHOST], ntop2[NI_MAXHOST];
386+ const char *ntop = ssh_remote_ipaddr(ssh);
387+
388+ /* Get IP address of client. */
389+ fromlen = sizeof(from);
390+ memset(&from, 0, sizeof(from));
391+ if (getpeername(ssh_packet_get_connection_in(ssh),
392+ (struct sockaddr *)&from, &fromlen) == -1) {
393+ debug("getpeername failed: %.100s", strerror(errno));
394+ return xstrdup(ntop);
395+ }
396+
397+ ipv64_normalise_mapped(&from, &fromlen);
398+ if (from.ss_family == AF_INET6)
399+ fromlen = sizeof(struct sockaddr_in6);
400+
401+ debug3("Trying to reverse map address %.100s.", ntop);
402+ /* Map the IP address to a host name. */
403+ if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
404+ NULL, 0, NI_NAMEREQD) != 0) {
405+ /* Host name not found. Use ip address. */
406+ return xstrdup(ntop);
407+ }
408+
409+ /*
410+ * if reverse lookup result looks like a numeric hostname,
411+ * someone is trying to trick us by PTR record like following:
412+ * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
413+ */
414+ memset(&hints, 0, sizeof(hints));
415+ hints.ai_socktype = SOCK_DGRAM; /*dummy*/
416+ hints.ai_flags = AI_NUMERICHOST;
417+ if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
418+ logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
419+ name, ntop);
420+ freeaddrinfo(ai);
421+ return xstrdup(ntop);
422+ }
423+
424+ /* Names are stored in lowercase. */
425+ lowercase(name);
426+
427+ /*
428+ * Map it back to an IP address and check that the given
429+ * address actually is an address of this host. This is
430+ * necessary because anyone with access to a name server can
431+ * define arbitrary names for an IP address. Mapping from
432+ * name to IP address can be trusted better (but can still be
433+ * fooled if the intruder has access to the name server of
434+ * the domain).
435+ */
436+ memset(&hints, 0, sizeof(hints));
437+ hints.ai_family = from.ss_family;
438+ hints.ai_socktype = SOCK_STREAM;
439+ if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
440+ logit("reverse mapping checking getaddrinfo for %.700s "
441+ "[%s] failed.", name, ntop);
442+ return xstrdup(ntop);
443+ }
444+ /* Look for the address from the list of addresses. */
445+ for (ai = aitop; ai; ai = ai->ai_next) {
446+ if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
447+ sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
448+ (strcmp(ntop, ntop2) == 0))
449+ break;
450+ }
451+ freeaddrinfo(aitop);
452+ /* If we reached the end of the list, the address was not there. */
453+ if (ai == NULL) {
454+ /* Address not found for the host name. */
455+ logit("Address %.100s maps to %.600s, but this does not "
456+ "map back to the address.", ntop, name);
457+ return xstrdup(ntop);
458+ }
459+ return xstrdup(name);
460+}
461+
462 void
463 ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
464 {
465diff --git a/canohost.h b/canohost.h
466index 26d62855a..0cadc9f18 100644
467--- a/canohost.h
468+++ b/canohost.h
469@@ -15,6 +15,9 @@
470 #ifndef _CANOHOST_H
471 #define _CANOHOST_H
472
473+struct ssh;
474+
475+char *remote_hostname(struct ssh *);
476 char *get_peer_ipaddr(int);
477 int get_peer_port(int);
478 char *get_local_ipaddr(int);
479diff --git a/clientloop.c b/clientloop.c
480index 60b46d161..2cebea29f 100644
481--- a/clientloop.c
482+++ b/clientloop.c
483@@ -112,6 +112,10 @@
484 #include "ssherr.h"
485 #include "hostfile.h"
486
487+#ifdef GSSAPI
488+#include "ssh-gss.h"
489+#endif
490+
491 /* import options */
492 extern Options options;
493
494@@ -1368,9 +1372,18 @@ client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
495 break;
496
497 /* Do channel operations unless rekeying in progress. */
498- if (!ssh_packet_is_rekeying(ssh))
499+ if (!ssh_packet_is_rekeying(ssh)) {
500 channel_after_select(ssh, readset, writeset);
501
502+#ifdef GSSAPI
503+ if (options.gss_renewal_rekey &&
504+ ssh_gssapi_credentials_updated(NULL)) {
505+ debug("credentials updated - forcing rekey");
506+ need_rekeying = 1;
507+ }
508+#endif
509+ }
510+
511 /* Buffer input from the connection. */
512 client_process_net_input(ssh, readset);
513
514diff --git a/configure.ac b/configure.ac
515index 7005a503e..c8a96deb4 100644
516--- a/configure.ac
517+++ b/configure.ac
518@@ -679,6 +679,30 @@ main() { if (NSVersionOfRunTimeLibrary("System") >= (60 << 16))
519 [Use tunnel device compatibility to OpenBSD])
520 AC_DEFINE([SSH_TUN_PREPEND_AF], [1],
521 [Prepend the address family to IP tunnel traffic])
522+ AC_MSG_CHECKING([if we have the Security Authorization Session API])
523+ AC_TRY_COMPILE([#include <Security/AuthSession.h>],
524+ [SessionCreate(0, 0);],
525+ [ac_cv_use_security_session_api="yes"
526+ AC_DEFINE([USE_SECURITY_SESSION_API], [1],
527+ [platform has the Security Authorization Session API])
528+ LIBS="$LIBS -framework Security"
529+ AC_MSG_RESULT([yes])],
530+ [ac_cv_use_security_session_api="no"
531+ AC_MSG_RESULT([no])])
532+ AC_MSG_CHECKING([if we have an in-memory credentials cache])
533+ AC_TRY_COMPILE(
534+ [#include <Kerberos/Kerberos.h>],
535+ [cc_context_t c;
536+ (void) cc_initialize (&c, 0, NULL, NULL);],
537+ [AC_DEFINE([USE_CCAPI], [1],
538+ [platform uses an in-memory credentials cache])
539+ LIBS="$LIBS -framework Security"
540+ AC_MSG_RESULT([yes])
541+ if test "x$ac_cv_use_security_session_api" = "xno"; then
542+ AC_MSG_ERROR([*** Need a security framework to use the credentials cache API ***])
543+ fi],
544+ [AC_MSG_RESULT([no])]
545+ )
546 m4_pattern_allow([AU_IPv])
547 AC_CHECK_DECL([AU_IPv4], [],
548 AC_DEFINE([AU_IPv4], [0], [System only supports IPv4 audit records])
549diff --git a/gss-genr.c b/gss-genr.c
550index d56257b4a..763a63ffa 100644
551--- a/gss-genr.c
552+++ b/gss-genr.c
553@@ -1,7 +1,7 @@
554 /* $OpenBSD: gss-genr.c,v 1.26 2018/07/10 09:13:30 djm Exp $ */
555
556 /*
557- * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
558+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
559 *
560 * Redistribution and use in source and binary forms, with or without
561 * modification, are permitted provided that the following conditions
562@@ -41,12 +41,36 @@
563 #include "sshbuf.h"
564 #include "log.h"
565 #include "ssh2.h"
566+#include "cipher.h"
567+#include "sshkey.h"
568+#include "kex.h"
569+#include "digest.h"
570+#include "packet.h"
571
572 #include "ssh-gss.h"
573
574 extern u_char *session_id2;
575 extern u_int session_id2_len;
576
577+typedef struct {
578+ char *encoded;
579+ gss_OID oid;
580+} ssh_gss_kex_mapping;
581+
582+/*
583+ * XXX - It would be nice to find a more elegant way of handling the
584+ * XXX passing of the key exchange context to the userauth routines
585+ */
586+
587+Gssctxt *gss_kex_context = NULL;
588+
589+static ssh_gss_kex_mapping *gss_enc2oid = NULL;
590+
591+int
592+ssh_gssapi_oid_table_ok(void) {
593+ return (gss_enc2oid != NULL);
594+}
595+
596 /* sshbuf_get for gss_buffer_desc */
597 int
598 ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g)
599@@ -62,6 +86,162 @@ ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g)
600 return 0;
601 }
602
603+/* sshpkt_get of gss_buffer_desc */
604+int
605+ssh_gssapi_sshpkt_get_buffer_desc(struct ssh *ssh, gss_buffer_desc *g)
606+{
607+ int r;
608+ u_char *p;
609+ size_t len;
610+
611+ if ((r = sshpkt_get_string(ssh, &p, &len)) != 0)
612+ return r;
613+ g->value = p;
614+ g->length = len;
615+ return 0;
616+}
617+
618+/*
619+ * Return a list of the gss-group1-sha1 mechanisms supported by this program
620+ *
621+ * We test mechanisms to ensure that we can use them, to avoid starting
622+ * a key exchange with a bad mechanism
623+ */
624+
625+char *
626+ssh_gssapi_client_mechanisms(const char *host, const char *client,
627+ const char *kex) {
628+ gss_OID_set gss_supported = NULL;
629+ OM_uint32 min_status;
630+
631+ if (GSS_ERROR(gss_indicate_mechs(&min_status, &gss_supported)))
632+ return NULL;
633+
634+ return ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism,
635+ host, client, kex);
636+}
637+
638+char *
639+ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check,
640+ const char *host, const char *client, const char *kex) {
641+ struct sshbuf *buf = NULL;
642+ size_t i;
643+ int r = SSH_ERR_ALLOC_FAIL;
644+ int oidpos, enclen;
645+ char *mechs, *encoded;
646+ u_char digest[SSH_DIGEST_MAX_LENGTH];
647+ char deroid[2];
648+ struct ssh_digest_ctx *md = NULL;
649+ char *s, *cp, *p;
650+
651+ if (gss_enc2oid != NULL) {
652+ for (i = 0; gss_enc2oid[i].encoded != NULL; i++)
653+ free(gss_enc2oid[i].encoded);
654+ free(gss_enc2oid);
655+ }
656+
657+ gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping) *
658+ (gss_supported->count + 1));
659+
660+ if ((buf = sshbuf_new()) == NULL)
661+ fatal("%s: sshbuf_new failed", __func__);
662+
663+ oidpos = 0;
664+ s = cp = xstrdup(kex);
665+ for (i = 0; i < gss_supported->count; i++) {
666+ if (gss_supported->elements[i].length < 128 &&
667+ (*check)(NULL, &(gss_supported->elements[i]), host, client)) {
668+
669+ deroid[0] = SSH_GSS_OIDTYPE;
670+ deroid[1] = gss_supported->elements[i].length;
671+
672+ if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
673+ (r = ssh_digest_update(md, deroid, 2)) != 0 ||
674+ (r = ssh_digest_update(md,
675+ gss_supported->elements[i].elements,
676+ gss_supported->elements[i].length)) != 0 ||
677+ (r = ssh_digest_final(md, digest, sizeof(digest))) != 0)
678+ fatal("%s: digest failed: %s", __func__,
679+ ssh_err(r));
680+ ssh_digest_free(md);
681+ md = NULL;
682+
683+ encoded = xmalloc(ssh_digest_bytes(SSH_DIGEST_MD5)
684+ * 2);
685+ enclen = __b64_ntop(digest,
686+ ssh_digest_bytes(SSH_DIGEST_MD5), encoded,
687+ ssh_digest_bytes(SSH_DIGEST_MD5) * 2);
688+
689+ cp = strncpy(s, kex, strlen(kex));
690+ for ((p = strsep(&cp, ",")); p && *p != '\0';
691+ (p = strsep(&cp, ","))) {
692+ if (sshbuf_len(buf) != 0 &&
693+ (r = sshbuf_put_u8(buf, ',')) != 0)
694+ fatal("%s: sshbuf_put_u8 error: %s",
695+ __func__, ssh_err(r));
696+ if ((r = sshbuf_put(buf, p, strlen(p))) != 0 ||
697+ (r = sshbuf_put(buf, encoded, enclen)) != 0)
698+ fatal("%s: sshbuf_put error: %s",
699+ __func__, ssh_err(r));
700+ }
701+
702+ gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]);
703+ gss_enc2oid[oidpos].encoded = encoded;
704+ oidpos++;
705+ }
706+ }
707+ free(s);
708+ gss_enc2oid[oidpos].oid = NULL;
709+ gss_enc2oid[oidpos].encoded = NULL;
710+
711+ if ((mechs = sshbuf_dup_string(buf)) == NULL)
712+ fatal("%s: sshbuf_dup_string failed", __func__);
713+
714+ sshbuf_free(buf);
715+
716+ if (strlen(mechs) == 0) {
717+ free(mechs);
718+ mechs = NULL;
719+ }
720+
721+ return (mechs);
722+}
723+
724+gss_OID
725+ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int kex_type) {
726+ int i = 0;
727+
728+#define SKIP_KEX_NAME(type) \
729+ case type: \
730+ if (strlen(name) < sizeof(type##_ID)) \
731+ return GSS_C_NO_OID; \
732+ name += sizeof(type##_ID) - 1; \
733+ break;
734+
735+ switch (kex_type) {
736+ SKIP_KEX_NAME(KEX_GSS_GRP1_SHA1)
737+ SKIP_KEX_NAME(KEX_GSS_GRP14_SHA1)
738+ SKIP_KEX_NAME(KEX_GSS_GRP14_SHA256)
739+ SKIP_KEX_NAME(KEX_GSS_GRP16_SHA512)
740+ SKIP_KEX_NAME(KEX_GSS_GEX_SHA1)
741+ SKIP_KEX_NAME(KEX_GSS_NISTP256_SHA256)
742+ SKIP_KEX_NAME(KEX_GSS_C25519_SHA256)
743+ default:
744+ return GSS_C_NO_OID;
745+ }
746+
747+#undef SKIP_KEX_NAME
748+
749+ while (gss_enc2oid[i].encoded != NULL &&
750+ strcmp(name, gss_enc2oid[i].encoded) != 0)
751+ i++;
752+
753+ if (gss_enc2oid[i].oid != NULL && ctx != NULL)
754+ ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid);
755+
756+ return gss_enc2oid[i].oid;
757+}
758+
759 /* Check that the OID in a data stream matches that in the context */
760 int
761 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
762@@ -218,7 +398,7 @@ ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
763 }
764
765 ctx->major = gss_init_sec_context(&ctx->minor,
766- GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
767+ ctx->client_creds, &ctx->context, ctx->name, ctx->oid,
768 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
769 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
770
771@@ -247,9 +427,43 @@ ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
772 return (ctx->major);
773 }
774
775+OM_uint32
776+ssh_gssapi_client_identity(Gssctxt *ctx, const char *name)
777+{
778+ gss_buffer_desc gssbuf;
779+ gss_name_t gssname;
780+ OM_uint32 status;
781+ gss_OID_set oidset;
782+
783+ gssbuf.value = (void *) name;
784+ gssbuf.length = strlen(gssbuf.value);
785+
786+ gss_create_empty_oid_set(&status, &oidset);
787+ gss_add_oid_set_member(&status, ctx->oid, &oidset);
788+
789+ ctx->major = gss_import_name(&ctx->minor, &gssbuf,
790+ GSS_C_NT_USER_NAME, &gssname);
791+
792+ if (!ctx->major)
793+ ctx->major = gss_acquire_cred(&ctx->minor,
794+ gssname, 0, oidset, GSS_C_INITIATE,
795+ &ctx->client_creds, NULL, NULL);
796+
797+ gss_release_name(&status, &gssname);
798+ gss_release_oid_set(&status, &oidset);
799+
800+ if (ctx->major)
801+ ssh_gssapi_error(ctx);
802+
803+ return(ctx->major);
804+}
805+
806 OM_uint32
807 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
808 {
809+ if (ctx == NULL)
810+ return -1;
811+
812 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
813 GSS_C_QOP_DEFAULT, buffer, hash)))
814 ssh_gssapi_error(ctx);
815@@ -257,6 +471,19 @@ ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
816 return (ctx->major);
817 }
818
819+/* Priviledged when used by server */
820+OM_uint32
821+ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
822+{
823+ if (ctx == NULL)
824+ return -1;
825+
826+ ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
827+ gssbuf, gssmic, NULL);
828+
829+ return (ctx->major);
830+}
831+
832 void
833 ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
834 const char *context)
835@@ -273,11 +500,16 @@ ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
836 }
837
838 int
839-ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
840+ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host,
841+ const char *client)
842 {
843 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
844 OM_uint32 major, minor;
845 gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
846+ Gssctxt *intctx = NULL;
847+
848+ if (ctx == NULL)
849+ ctx = &intctx;
850
851 /* RFC 4462 says we MUST NOT do SPNEGO */
852 if (oid->length == spnego_oid.length &&
853@@ -287,6 +519,10 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
854 ssh_gssapi_build_ctx(ctx);
855 ssh_gssapi_set_oid(*ctx, oid);
856 major = ssh_gssapi_import_name(*ctx, host);
857+
858+ if (!GSS_ERROR(major) && client)
859+ major = ssh_gssapi_client_identity(*ctx, client);
860+
861 if (!GSS_ERROR(major)) {
862 major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
863 NULL);
864@@ -296,10 +532,66 @@ ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
865 GSS_C_NO_BUFFER);
866 }
867
868- if (GSS_ERROR(major))
869+ if (GSS_ERROR(major) || intctx != NULL)
870 ssh_gssapi_delete_ctx(ctx);
871
872 return (!GSS_ERROR(major));
873 }
874
875+int
876+ssh_gssapi_credentials_updated(Gssctxt *ctxt) {
877+ static gss_name_t saved_name = GSS_C_NO_NAME;
878+ static OM_uint32 saved_lifetime = 0;
879+ static gss_OID saved_mech = GSS_C_NO_OID;
880+ static gss_name_t name;
881+ static OM_uint32 last_call = 0;
882+ OM_uint32 lifetime, now, major, minor;
883+ int equal;
884+
885+ now = time(NULL);
886+
887+ if (ctxt) {
888+ debug("Rekey has happened - updating saved versions");
889+
890+ if (saved_name != GSS_C_NO_NAME)
891+ gss_release_name(&minor, &saved_name);
892+
893+ major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
894+ &saved_name, &saved_lifetime, NULL, NULL);
895+
896+ if (!GSS_ERROR(major)) {
897+ saved_mech = ctxt->oid;
898+ saved_lifetime+= now;
899+ } else {
900+ /* Handle the error */
901+ }
902+ return 0;
903+ }
904+
905+ if (now - last_call < 10)
906+ return 0;
907+
908+ last_call = now;
909+
910+ if (saved_mech == GSS_C_NO_OID)
911+ return 0;
912+
913+ major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
914+ &name, &lifetime, NULL, NULL);
915+ if (major == GSS_S_CREDENTIALS_EXPIRED)
916+ return 0;
917+ else if (GSS_ERROR(major))
918+ return 0;
919+
920+ major = gss_compare_name(&minor, saved_name, name, &equal);
921+ gss_release_name(&minor, &name);
922+ if (GSS_ERROR(major))
923+ return 0;
924+
925+ if (equal && (saved_lifetime < lifetime + now - 10))
926+ return 1;
927+
928+ return 0;
929+}
930+
931 #endif /* GSSAPI */
932diff --git a/gss-serv-krb5.c b/gss-serv-krb5.c
933index a151bc1e4..ef9beb67c 100644
934--- a/gss-serv-krb5.c
935+++ b/gss-serv-krb5.c
936@@ -1,7 +1,7 @@
937 /* $OpenBSD: gss-serv-krb5.c,v 1.9 2018/07/09 21:37:55 markus Exp $ */
938
939 /*
940- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
941+ * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
942 *
943 * Redistribution and use in source and binary forms, with or without
944 * modification, are permitted provided that the following conditions
945@@ -120,8 +120,8 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
946 krb5_error_code problem;
947 krb5_principal princ;
948 OM_uint32 maj_status, min_status;
949- int len;
950 const char *errmsg;
951+ const char *new_ccname;
952
953 if (client->creds == NULL) {
954 debug("No credentials stored");
955@@ -180,11 +180,16 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
956 return;
957 }
958
959- client->store.filename = xstrdup(krb5_cc_get_name(krb_context, ccache));
960+ new_ccname = krb5_cc_get_name(krb_context, ccache);
961+
962 client->store.envvar = "KRB5CCNAME";
963- len = strlen(client->store.filename) + 6;
964- client->store.envval = xmalloc(len);
965- snprintf(client->store.envval, len, "FILE:%s", client->store.filename);
966+#ifdef USE_CCAPI
967+ xasprintf(&client->store.envval, "API:%s", new_ccname);
968+ client->store.filename = NULL;
969+#else
970+ xasprintf(&client->store.envval, "FILE:%s", new_ccname);
971+ client->store.filename = xstrdup(new_ccname);
972+#endif
973
974 #ifdef USE_PAM
975 if (options.use_pam)
976@@ -196,6 +201,71 @@ ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
977 return;
978 }
979
980+int
981+ssh_gssapi_krb5_updatecreds(ssh_gssapi_ccache *store,
982+ ssh_gssapi_client *client)
983+{
984+ krb5_ccache ccache = NULL;
985+ krb5_principal principal = NULL;
986+ char *name = NULL;
987+ krb5_error_code problem;
988+ OM_uint32 maj_status, min_status;
989+
990+ if ((problem = krb5_cc_resolve(krb_context, store->envval, &ccache))) {
991+ logit("krb5_cc_resolve(): %.100s",
992+ krb5_get_err_text(krb_context, problem));
993+ return 0;
994+ }
995+
996+ /* Find out who the principal in this cache is */
997+ if ((problem = krb5_cc_get_principal(krb_context, ccache,
998+ &principal))) {
999+ logit("krb5_cc_get_principal(): %.100s",
1000+ krb5_get_err_text(krb_context, problem));
1001+ krb5_cc_close(krb_context, ccache);
1002+ return 0;
1003+ }
1004+
1005+ if ((problem = krb5_unparse_name(krb_context, principal, &name))) {
1006+ logit("krb5_unparse_name(): %.100s",
1007+ krb5_get_err_text(krb_context, problem));
1008+ krb5_free_principal(krb_context, principal);
1009+ krb5_cc_close(krb_context, ccache);
1010+ return 0;
1011+ }
1012+
1013+
1014+ if (strcmp(name,client->exportedname.value)!=0) {
1015+ debug("Name in local credentials cache differs. Not storing");
1016+ krb5_free_principal(krb_context, principal);
1017+ krb5_cc_close(krb_context, ccache);
1018+ krb5_free_unparsed_name(krb_context, name);
1019+ return 0;
1020+ }
1021+ krb5_free_unparsed_name(krb_context, name);
1022+
1023+ /* Name matches, so lets get on with it! */
1024+
1025+ if ((problem = krb5_cc_initialize(krb_context, ccache, principal))) {
1026+ logit("krb5_cc_initialize(): %.100s",
1027+ krb5_get_err_text(krb_context, problem));
1028+ krb5_free_principal(krb_context, principal);
1029+ krb5_cc_close(krb_context, ccache);
1030+ return 0;
1031+ }
1032+
1033+ krb5_free_principal(krb_context, principal);
1034+
1035+ if ((maj_status = gss_krb5_copy_ccache(&min_status, client->creds,
1036+ ccache))) {
1037+ logit("gss_krb5_copy_ccache() failed. Sorry!");
1038+ krb5_cc_close(krb_context, ccache);
1039+ return 0;
1040+ }
1041+
1042+ return 1;
1043+}
1044+
1045 ssh_gssapi_mech gssapi_kerberos_mech = {
1046 "toWM5Slw5Ew8Mqkay+al2g==",
1047 "Kerberos",
1048@@ -203,7 +273,8 @@ ssh_gssapi_mech gssapi_kerberos_mech = {
1049 NULL,
1050 &ssh_gssapi_krb5_userok,
1051 NULL,
1052- &ssh_gssapi_krb5_storecreds
1053+ &ssh_gssapi_krb5_storecreds,
1054+ &ssh_gssapi_krb5_updatecreds
1055 };
1056
1057 #endif /* KRB5 */
1058diff --git a/gss-serv.c b/gss-serv.c
1059index b5d4bb2d1..55f4d4bda 100644
1060--- a/gss-serv.c
1061+++ b/gss-serv.c
1062@@ -1,7 +1,7 @@
1063 /* $OpenBSD: gss-serv.c,v 1.32 2020/03/13 03:17:07 djm Exp $ */
1064
1065 /*
1066- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
1067+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
1068 *
1069 * Redistribution and use in source and binary forms, with or without
1070 * modification, are permitted provided that the following conditions
1071@@ -44,17 +44,19 @@
1072 #include "session.h"
1073 #include "misc.h"
1074 #include "servconf.h"
1075+#include "uidswap.h"
1076
1077 #include "ssh-gss.h"
1078+#include "monitor_wrap.h"
1079
1080 extern ServerOptions options;
1081
1082 static ssh_gssapi_client gssapi_client =
1083- { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
1084- GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}};
1085+ { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER, GSS_C_NO_CREDENTIAL,
1086+ GSS_C_NO_NAME, NULL, {NULL, NULL, NULL, NULL, NULL}, 0, 0};
1087
1088 ssh_gssapi_mech gssapi_null_mech =
1089- { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
1090+ { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
1091
1092 #ifdef KRB5
1093 extern ssh_gssapi_mech gssapi_kerberos_mech;
1094@@ -140,6 +142,29 @@ ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
1095 return (ssh_gssapi_acquire_cred(*ctx));
1096 }
1097
1098+/* Unprivileged */
1099+char *
1100+ssh_gssapi_server_mechanisms(void) {
1101+ if (supported_oids == NULL)
1102+ ssh_gssapi_prepare_supported_oids();
1103+ return (ssh_gssapi_kex_mechs(supported_oids,
1104+ &ssh_gssapi_server_check_mech, NULL, NULL,
1105+ options.gss_kex_algorithms));
1106+}
1107+
1108+/* Unprivileged */
1109+int
1110+ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
1111+ const char *dummy) {
1112+ Gssctxt *ctx = NULL;
1113+ int res;
1114+
1115+ res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
1116+ ssh_gssapi_delete_ctx(&ctx);
1117+
1118+ return (res);
1119+}
1120+
1121 /* Unprivileged */
1122 void
1123 ssh_gssapi_supported_oids(gss_OID_set *oidset)
1124@@ -150,7 +175,9 @@ ssh_gssapi_supported_oids(gss_OID_set *oidset)
1125 gss_OID_set supported;
1126
1127 gss_create_empty_oid_set(&min_status, oidset);
1128- gss_indicate_mechs(&min_status, &supported);
1129+
1130+ if (GSS_ERROR(gss_indicate_mechs(&min_status, &supported)))
1131+ return;
1132
1133 while (supported_mechs[i]->name != NULL) {
1134 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
1135@@ -276,8 +303,48 @@ OM_uint32
1136 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1137 {
1138 int i = 0;
1139+ int equal = 0;
1140+ gss_name_t new_name = GSS_C_NO_NAME;
1141+ gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
1142+
1143+ if (options.gss_store_rekey && client->used && ctx->client_creds) {
1144+ if (client->mech->oid.length != ctx->oid->length ||
1145+ (memcmp(client->mech->oid.elements,
1146+ ctx->oid->elements, ctx->oid->length) !=0)) {
1147+ debug("Rekeyed credentials have different mechanism");
1148+ return GSS_S_COMPLETE;
1149+ }
1150+
1151+ if ((ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
1152+ ctx->client_creds, ctx->oid, &new_name,
1153+ NULL, NULL, NULL))) {
1154+ ssh_gssapi_error(ctx);
1155+ return (ctx->major);
1156+ }
1157+
1158+ ctx->major = gss_compare_name(&ctx->minor, client->name,
1159+ new_name, &equal);
1160+
1161+ if (GSS_ERROR(ctx->major)) {
1162+ ssh_gssapi_error(ctx);
1163+ return (ctx->major);
1164+ }
1165+
1166+ if (!equal) {
1167+ debug("Rekeyed credentials have different name");
1168+ return GSS_S_COMPLETE;
1169+ }
1170
1171- gss_buffer_desc ename;
1172+ debug("Marking rekeyed credentials for export");
1173+
1174+ gss_release_name(&ctx->minor, &client->name);
1175+ gss_release_cred(&ctx->minor, &client->creds);
1176+ client->name = new_name;
1177+ client->creds = ctx->client_creds;
1178+ ctx->client_creds = GSS_C_NO_CREDENTIAL;
1179+ client->updated = 1;
1180+ return GSS_S_COMPLETE;
1181+ }
1182
1183 client->mech = NULL;
1184
1185@@ -292,6 +359,13 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1186 if (client->mech == NULL)
1187 return GSS_S_FAILURE;
1188
1189+ if (ctx->client_creds &&
1190+ (ctx->major = gss_inquire_cred_by_mech(&ctx->minor,
1191+ ctx->client_creds, ctx->oid, &client->name, NULL, NULL, NULL))) {
1192+ ssh_gssapi_error(ctx);
1193+ return (ctx->major);
1194+ }
1195+
1196 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
1197 &client->displayname, NULL))) {
1198 ssh_gssapi_error(ctx);
1199@@ -309,6 +383,8 @@ ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
1200 return (ctx->major);
1201 }
1202
1203+ gss_release_buffer(&ctx->minor, &ename);
1204+
1205 /* We can't copy this structure, so we just move the pointer to it */
1206 client->creds = ctx->client_creds;
1207 ctx->client_creds = GSS_C_NO_CREDENTIAL;
1208@@ -356,19 +432,23 @@ ssh_gssapi_do_child(char ***envp, u_int *envsizep)
1209
1210 /* Privileged */
1211 int
1212-ssh_gssapi_userok(char *user)
1213+ssh_gssapi_userok(char *user, struct passwd *pw, int kex)
1214 {
1215 OM_uint32 lmin;
1216
1217+ (void) kex; /* used in privilege separation */
1218+
1219 if (gssapi_client.exportedname.length == 0 ||
1220 gssapi_client.exportedname.value == NULL) {
1221 debug("No suitable client data");
1222 return 0;
1223 }
1224 if (gssapi_client.mech && gssapi_client.mech->userok)
1225- if ((*gssapi_client.mech->userok)(&gssapi_client, user))
1226+ if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
1227+ gssapi_client.used = 1;
1228+ gssapi_client.store.owner = pw;
1229 return 1;
1230- else {
1231+ } else {
1232 /* Destroy delegated credentials if userok fails */
1233 gss_release_buffer(&lmin, &gssapi_client.displayname);
1234 gss_release_buffer(&lmin, &gssapi_client.exportedname);
1235@@ -382,14 +462,90 @@ ssh_gssapi_userok(char *user)
1236 return (0);
1237 }
1238
1239-/* Privileged */
1240-OM_uint32
1241-ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1242+/* These bits are only used for rekeying. The unpriviledged child is running
1243+ * as the user, the monitor is root.
1244+ *
1245+ * In the child, we want to :
1246+ * *) Ask the monitor to store our credentials into the store we specify
1247+ * *) If it succeeds, maybe do a PAM update
1248+ */
1249+
1250+/* Stuff for PAM */
1251+
1252+#ifdef USE_PAM
1253+static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg,
1254+ struct pam_response **resp, void *data)
1255 {
1256- ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
1257- gssbuf, gssmic, NULL);
1258+ return (PAM_CONV_ERR);
1259+}
1260+#endif
1261
1262- return (ctx->major);
1263+void
1264+ssh_gssapi_rekey_creds(void) {
1265+ int ok;
1266+#ifdef USE_PAM
1267+ int ret;
1268+ pam_handle_t *pamh = NULL;
1269+ struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
1270+ char *envstr;
1271+#endif
1272+
1273+ if (gssapi_client.store.filename == NULL &&
1274+ gssapi_client.store.envval == NULL &&
1275+ gssapi_client.store.envvar == NULL)
1276+ return;
1277+
1278+ ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
1279+
1280+ if (!ok)
1281+ return;
1282+
1283+ debug("Rekeyed credentials stored successfully");
1284+
1285+ /* Actually managing to play with the ssh pam stack from here will
1286+ * be next to impossible. In any case, we may want different options
1287+ * for rekeying. So, use our own :)
1288+ */
1289+#ifdef USE_PAM
1290+ if (!use_privsep) {
1291+ debug("Not even going to try and do PAM with privsep disabled");
1292+ return;
1293+ }
1294+
1295+ ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
1296+ &pamconv, &pamh);
1297+ if (ret)
1298+ return;
1299+
1300+ xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar,
1301+ gssapi_client.store.envval);
1302+
1303+ ret = pam_putenv(pamh, envstr);
1304+ if (!ret)
1305+ pam_setcred(pamh, PAM_REINITIALIZE_CRED);
1306+ pam_end(pamh, PAM_SUCCESS);
1307+#endif
1308+}
1309+
1310+int
1311+ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
1312+ int ok = 0;
1313+
1314+ /* Check we've got credentials to store */
1315+ if (!gssapi_client.updated)
1316+ return 0;
1317+
1318+ gssapi_client.updated = 0;
1319+
1320+ temporarily_use_uid(gssapi_client.store.owner);
1321+ if (gssapi_client.mech && gssapi_client.mech->updatecreds)
1322+ ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
1323+ else
1324+ debug("No update function for this mechanism");
1325+
1326+ restore_uid();
1327+
1328+ return ok;
1329 }
1330
1331 /* Privileged */
1332diff --git a/kex.c b/kex.c
1333index aecb9394d..751cfc710 100644
1334--- a/kex.c
1335+++ b/kex.c
1336@@ -57,11 +57,16 @@
1337 #include "misc.h"
1338 #include "dispatch.h"
1339 #include "monitor.h"
1340+#include "xmalloc.h"
1341
1342 #include "ssherr.h"
1343 #include "sshbuf.h"
1344 #include "digest.h"
1345
1346+#ifdef GSSAPI
1347+#include "ssh-gss.h"
1348+#endif
1349+
1350 /* prototype */
1351 static int kex_choose_conf(struct ssh *);
1352 static int kex_input_newkeys(int, u_int32_t, struct ssh *);
1353@@ -115,15 +120,28 @@ static const struct kexalg kexalgs[] = {
1354 #endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */
1355 { NULL, 0, -1, -1},
1356 };
1357+static const struct kexalg gss_kexalgs[] = {
1358+#ifdef GSSAPI
1359+ { KEX_GSS_GEX_SHA1_ID, KEX_GSS_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
1360+ { KEX_GSS_GRP1_SHA1_ID, KEX_GSS_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
1361+ { KEX_GSS_GRP14_SHA1_ID, KEX_GSS_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
1362+ { KEX_GSS_GRP14_SHA256_ID, KEX_GSS_GRP14_SHA256, 0, SSH_DIGEST_SHA256 },
1363+ { KEX_GSS_GRP16_SHA512_ID, KEX_GSS_GRP16_SHA512, 0, SSH_DIGEST_SHA512 },
1364+ { KEX_GSS_NISTP256_SHA256_ID, KEX_GSS_NISTP256_SHA256,
1365+ NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
1366+ { KEX_GSS_C25519_SHA256_ID, KEX_GSS_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
1367+#endif
1368+ { NULL, 0, -1, -1},
1369+};
1370
1371-char *
1372-kex_alg_list(char sep)
1373+static char *
1374+kex_alg_list_internal(char sep, const struct kexalg *algs)
1375 {
1376 char *ret = NULL, *tmp;
1377 size_t nlen, rlen = 0;
1378 const struct kexalg *k;
1379
1380- for (k = kexalgs; k->name != NULL; k++) {
1381+ for (k = algs; k->name != NULL; k++) {
1382 if (ret != NULL)
1383 ret[rlen++] = sep;
1384 nlen = strlen(k->name);
1385@@ -138,6 +156,18 @@ kex_alg_list(char sep)
1386 return ret;
1387 }
1388
1389+char *
1390+kex_alg_list(char sep)
1391+{
1392+ return kex_alg_list_internal(sep, kexalgs);
1393+}
1394+
1395+char *
1396+kex_gss_alg_list(char sep)
1397+{
1398+ return kex_alg_list_internal(sep, gss_kexalgs);
1399+}
1400+
1401 static const struct kexalg *
1402 kex_alg_by_name(const char *name)
1403 {
1404@@ -147,6 +177,10 @@ kex_alg_by_name(const char *name)
1405 if (strcmp(k->name, name) == 0)
1406 return k;
1407 }
1408+ for (k = gss_kexalgs; k->name != NULL; k++) {
1409+ if (strncmp(k->name, name, strlen(k->name)) == 0)
1410+ return k;
1411+ }
1412 return NULL;
1413 }
1414
1415@@ -315,6 +349,29 @@ kex_assemble_names(char **listp, const char *def, const char *all)
1416 return r;
1417 }
1418
1419+/* Validate GSS KEX method name list */
1420+int
1421+kex_gss_names_valid(const char *names)
1422+{
1423+ char *s, *cp, *p;
1424+
1425+ if (names == NULL || *names == '\0')
1426+ return 0;
1427+ s = cp = xstrdup(names);
1428+ for ((p = strsep(&cp, ",")); p && *p != '\0';
1429+ (p = strsep(&cp, ","))) {
1430+ if (strncmp(p, "gss-", 4) != 0
1431+ || kex_alg_by_name(p) == NULL) {
1432+ error("Unsupported KEX algorithm \"%.100s\"", p);
1433+ free(s);
1434+ return 0;
1435+ }
1436+ }
1437+ debug3("gss kex names ok: [%s]", names);
1438+ free(s);
1439+ return 1;
1440+}
1441+
1442 /* put algorithm proposal into buffer */
1443 int
1444 kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
1445@@ -697,6 +754,9 @@ kex_free(struct kex *kex)
1446 sshbuf_free(kex->server_version);
1447 sshbuf_free(kex->client_pub);
1448 free(kex->session_id);
1449+#ifdef GSSAPI
1450+ free(kex->gss_host);
1451+#endif /* GSSAPI */
1452 free(kex->failed_choice);
1453 free(kex->hostkey_alg);
1454 free(kex->name);
1455diff --git a/kex.h b/kex.h
1456index a5ae6ac05..fe7141414 100644
1457--- a/kex.h
1458+++ b/kex.h
1459@@ -102,6 +102,15 @@ enum kex_exchange {
1460 KEX_ECDH_SHA2,
1461 KEX_C25519_SHA256,
1462 KEX_KEM_SNTRUP4591761X25519_SHA512,
1463+#ifdef GSSAPI
1464+ KEX_GSS_GRP1_SHA1,
1465+ KEX_GSS_GRP14_SHA1,
1466+ KEX_GSS_GRP14_SHA256,
1467+ KEX_GSS_GRP16_SHA512,
1468+ KEX_GSS_GEX_SHA1,
1469+ KEX_GSS_NISTP256_SHA256,
1470+ KEX_GSS_C25519_SHA256,
1471+#endif
1472 KEX_MAX
1473 };
1474
1475@@ -153,6 +162,12 @@ struct kex {
1476 u_int flags;
1477 int hash_alg;
1478 int ec_nid;
1479+#ifdef GSSAPI
1480+ int gss_deleg_creds;
1481+ int gss_trust_dns;
1482+ char *gss_host;
1483+ char *gss_client;
1484+#endif
1485 char *failed_choice;
1486 int (*verify_host_key)(struct sshkey *, struct ssh *);
1487 struct sshkey *(*load_host_public_key)(int, int, struct ssh *);
1488@@ -174,8 +189,10 @@ struct kex {
1489
1490 int kex_names_valid(const char *);
1491 char *kex_alg_list(char);
1492+char *kex_gss_alg_list(char);
1493 char *kex_names_cat(const char *, const char *);
1494 int kex_assemble_names(char **, const char *, const char *);
1495+int kex_gss_names_valid(const char *);
1496
1497 int kex_exchange_identification(struct ssh *, int, const char *);
1498
1499@@ -202,6 +219,12 @@ int kexgex_client(struct ssh *);
1500 int kexgex_server(struct ssh *);
1501 int kex_gen_client(struct ssh *);
1502 int kex_gen_server(struct ssh *);
1503+#if defined(GSSAPI) && defined(WITH_OPENSSL)
1504+int kexgssgex_client(struct ssh *);
1505+int kexgssgex_server(struct ssh *);
1506+int kexgss_client(struct ssh *);
1507+int kexgss_server(struct ssh *);
1508+#endif
1509
1510 int kex_dh_keypair(struct kex *);
1511 int kex_dh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
1512@@ -234,6 +257,12 @@ int kexgex_hash(int, const struct sshbuf *, const struct sshbuf *,
1513 const BIGNUM *, const u_char *, size_t,
1514 u_char *, size_t *);
1515
1516+int kex_gen_hash(int hash_alg, const struct sshbuf *client_version,
1517+ const struct sshbuf *server_version, const struct sshbuf *client_kexinit,
1518+ const struct sshbuf *server_kexinit, const struct sshbuf *server_host_key_blob,
1519+ const struct sshbuf *client_pub, const struct sshbuf *server_pub,
1520+ const struct sshbuf *shared_secret, u_char *hash, size_t *hashlen);
1521+
1522 void kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
1523 __attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
1524 __attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
1525diff --git a/kexdh.c b/kexdh.c
1526index 6e0159f9f..d024a8b9a 100644
1527--- a/kexdh.c
1528+++ b/kexdh.c
1529@@ -49,13 +49,23 @@ kex_dh_keygen(struct kex *kex)
1530 {
1531 switch (kex->kex_type) {
1532 case KEX_DH_GRP1_SHA1:
1533+#ifdef GSSAPI
1534+ case KEX_GSS_GRP1_SHA1:
1535+#endif
1536 kex->dh = dh_new_group1();
1537 break;
1538 case KEX_DH_GRP14_SHA1:
1539 case KEX_DH_GRP14_SHA256:
1540+#ifdef GSSAPI
1541+ case KEX_GSS_GRP14_SHA1:
1542+ case KEX_GSS_GRP14_SHA256:
1543+#endif
1544 kex->dh = dh_new_group14();
1545 break;
1546 case KEX_DH_GRP16_SHA512:
1547+#ifdef GSSAPI
1548+ case KEX_GSS_GRP16_SHA512:
1549+#endif
1550 kex->dh = dh_new_group16();
1551 break;
1552 case KEX_DH_GRP18_SHA512:
1553diff --git a/kexgen.c b/kexgen.c
1554index 69348b964..c0e8c2f44 100644
1555--- a/kexgen.c
1556+++ b/kexgen.c
1557@@ -44,7 +44,7 @@
1558 static int input_kex_gen_init(int, u_int32_t, struct ssh *);
1559 static int input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh);
1560
1561-static int
1562+int
1563 kex_gen_hash(
1564 int hash_alg,
1565 const struct sshbuf *client_version,
1566diff --git a/kexgssc.c b/kexgssc.c
1567new file mode 100644
1568index 000000000..f6e1405eb
1569--- /dev/null
1570+++ b/kexgssc.c
1571@@ -0,0 +1,606 @@
1572+/*
1573+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
1574+ *
1575+ * Redistribution and use in source and binary forms, with or without
1576+ * modification, are permitted provided that the following conditions
1577+ * are met:
1578+ * 1. Redistributions of source code must retain the above copyright
1579+ * notice, this list of conditions and the following disclaimer.
1580+ * 2. Redistributions in binary form must reproduce the above copyright
1581+ * notice, this list of conditions and the following disclaimer in the
1582+ * documentation and/or other materials provided with the distribution.
1583+ *
1584+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
1585+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1586+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1587+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1588+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1589+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1590+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1591+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1592+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1593+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1594+ */
1595+
1596+#include "includes.h"
1597+
1598+#if defined(GSSAPI) && defined(WITH_OPENSSL)
1599+
1600+#include "includes.h"
1601+
1602+#include <openssl/crypto.h>
1603+#include <openssl/bn.h>
1604+
1605+#include <string.h>
1606+
1607+#include "xmalloc.h"
1608+#include "sshbuf.h"
1609+#include "ssh2.h"
1610+#include "sshkey.h"
1611+#include "cipher.h"
1612+#include "kex.h"
1613+#include "log.h"
1614+#include "packet.h"
1615+#include "dh.h"
1616+#include "digest.h"
1617+#include "ssherr.h"
1618+
1619+#include "ssh-gss.h"
1620+
1621+int
1622+kexgss_client(struct ssh *ssh)
1623+{
1624+ struct kex *kex = ssh->kex;
1625+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER,
1626+ recv_tok = GSS_C_EMPTY_BUFFER,
1627+ gssbuf, msg_tok = GSS_C_EMPTY_BUFFER, *token_ptr;
1628+ Gssctxt *ctxt;
1629+ OM_uint32 maj_status, min_status, ret_flags;
1630+ struct sshbuf *server_blob = NULL;
1631+ struct sshbuf *shared_secret = NULL;
1632+ struct sshbuf *server_host_key_blob = NULL;
1633+ struct sshbuf *empty = NULL;
1634+ u_char *msg;
1635+ int type = 0;
1636+ int first = 1;
1637+ u_char hash[SSH_DIGEST_MAX_LENGTH];
1638+ size_t hashlen;
1639+ u_char c;
1640+ int r;
1641+
1642+ /* Initialise our GSSAPI world */
1643+ ssh_gssapi_build_ctx(&ctxt);
1644+ if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type)
1645+ == GSS_C_NO_OID)
1646+ fatal("Couldn't identify host exchange");
1647+
1648+ if (ssh_gssapi_import_name(ctxt, kex->gss_host))
1649+ fatal("Couldn't import hostname");
1650+
1651+ if (kex->gss_client &&
1652+ ssh_gssapi_client_identity(ctxt, kex->gss_client))
1653+ fatal("Couldn't acquire client credentials");
1654+
1655+ /* Step 1 */
1656+ switch (kex->kex_type) {
1657+ case KEX_GSS_GRP1_SHA1:
1658+ case KEX_GSS_GRP14_SHA1:
1659+ case KEX_GSS_GRP14_SHA256:
1660+ case KEX_GSS_GRP16_SHA512:
1661+ r = kex_dh_keypair(kex);
1662+ break;
1663+ case KEX_GSS_NISTP256_SHA256:
1664+ r = kex_ecdh_keypair(kex);
1665+ break;
1666+ case KEX_GSS_C25519_SHA256:
1667+ r = kex_c25519_keypair(kex);
1668+ break;
1669+ default:
1670+ fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
1671+ }
1672+ if (r != 0)
1673+ return r;
1674+
1675+ token_ptr = GSS_C_NO_BUFFER;
1676+
1677+ do {
1678+ debug("Calling gss_init_sec_context");
1679+
1680+ maj_status = ssh_gssapi_init_ctx(ctxt,
1681+ kex->gss_deleg_creds, token_ptr, &send_tok,
1682+ &ret_flags);
1683+
1684+ if (GSS_ERROR(maj_status)) {
1685+ /* XXX Useles code: Missing send? */
1686+ if (send_tok.length != 0) {
1687+ if ((r = sshpkt_start(ssh,
1688+ SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
1689+ (r = sshpkt_put_string(ssh, send_tok.value,
1690+ send_tok.length)) != 0)
1691+ fatal("sshpkt failed: %s", ssh_err(r));
1692+ }
1693+ fatal("gss_init_context failed");
1694+ }
1695+
1696+ /* If we've got an old receive buffer get rid of it */
1697+ if (token_ptr != GSS_C_NO_BUFFER)
1698+ gss_release_buffer(&min_status, &recv_tok);
1699+
1700+ if (maj_status == GSS_S_COMPLETE) {
1701+ /* If mutual state flag is not true, kex fails */
1702+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
1703+ fatal("Mutual authentication failed");
1704+
1705+ /* If integ avail flag is not true kex fails */
1706+ if (!(ret_flags & GSS_C_INTEG_FLAG))
1707+ fatal("Integrity check failed");
1708+ }
1709+
1710+ /*
1711+ * If we have data to send, then the last message that we
1712+ * received cannot have been a 'complete'.
1713+ */
1714+ if (send_tok.length != 0) {
1715+ if (first) {
1716+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_INIT)) != 0 ||
1717+ (r = sshpkt_put_string(ssh, send_tok.value,
1718+ send_tok.length)) != 0 ||
1719+ (r = sshpkt_put_stringb(ssh, kex->client_pub)) != 0)
1720+ fatal("failed to construct packet: %s", ssh_err(r));
1721+ first = 0;
1722+ } else {
1723+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
1724+ (r = sshpkt_put_string(ssh, send_tok.value,
1725+ send_tok.length)) != 0)
1726+ fatal("failed to construct packet: %s", ssh_err(r));
1727+ }
1728+ if ((r = sshpkt_send(ssh)) != 0)
1729+ fatal("failed to send packet: %s", ssh_err(r));
1730+ gss_release_buffer(&min_status, &send_tok);
1731+
1732+ /* If we've sent them data, they should reply */
1733+ do {
1734+ type = ssh_packet_read(ssh);
1735+ if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
1736+ debug("Received KEXGSS_HOSTKEY");
1737+ if (server_host_key_blob)
1738+ fatal("Server host key received more than once");
1739+ if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
1740+ fatal("Failed to read server host key: %s", ssh_err(r));
1741+ }
1742+ } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
1743+
1744+ switch (type) {
1745+ case SSH2_MSG_KEXGSS_CONTINUE:
1746+ debug("Received GSSAPI_CONTINUE");
1747+ if (maj_status == GSS_S_COMPLETE)
1748+ fatal("GSSAPI Continue received from server when complete");
1749+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
1750+ &recv_tok)) != 0 ||
1751+ (r = sshpkt_get_end(ssh)) != 0)
1752+ fatal("Failed to read token: %s", ssh_err(r));
1753+ break;
1754+ case SSH2_MSG_KEXGSS_COMPLETE:
1755+ debug("Received GSSAPI_COMPLETE");
1756+ if (msg_tok.value != NULL)
1757+ fatal("Received GSSAPI_COMPLETE twice?");
1758+ if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 ||
1759+ (r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
1760+ &msg_tok)) != 0)
1761+ fatal("Failed to read message: %s", ssh_err(r));
1762+
1763+ /* Is there a token included? */
1764+ if ((r = sshpkt_get_u8(ssh, &c)) != 0)
1765+ fatal("sshpkt failed: %s", ssh_err(r));
1766+ if (c) {
1767+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(
1768+ ssh, &recv_tok)) != 0)
1769+ fatal("Failed to read token: %s", ssh_err(r));
1770+ /* If we're already complete - protocol error */
1771+ if (maj_status == GSS_S_COMPLETE)
1772+ sshpkt_disconnect(ssh, "Protocol error: received token when complete");
1773+ } else {
1774+ /* No token included */
1775+ if (maj_status != GSS_S_COMPLETE)
1776+ sshpkt_disconnect(ssh, "Protocol error: did not receive final token");
1777+ }
1778+ if ((r = sshpkt_get_end(ssh)) != 0) {
1779+ fatal("Expecting end of packet.");
1780+ }
1781+ break;
1782+ case SSH2_MSG_KEXGSS_ERROR:
1783+ debug("Received Error");
1784+ if ((r = sshpkt_get_u32(ssh, &maj_status)) != 0 ||
1785+ (r = sshpkt_get_u32(ssh, &min_status)) != 0 ||
1786+ (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
1787+ (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* lang tag */
1788+ (r = sshpkt_get_end(ssh)) != 0)
1789+ fatal("sshpkt_get failed: %s", ssh_err(r));
1790+ fatal("GSSAPI Error: \n%.400s", msg);
1791+ default:
1792+ sshpkt_disconnect(ssh, "Protocol error: didn't expect packet type %d",
1793+ type);
1794+ }
1795+ token_ptr = &recv_tok;
1796+ } else {
1797+ /* No data, and not complete */
1798+ if (maj_status != GSS_S_COMPLETE)
1799+ fatal("Not complete, and no token output");
1800+ }
1801+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
1802+
1803+ /*
1804+ * We _must_ have received a COMPLETE message in reply from the
1805+ * server, which will have set server_blob and msg_tok
1806+ */
1807+
1808+ if (type != SSH2_MSG_KEXGSS_COMPLETE)
1809+ fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
1810+
1811+ /* compute shared secret */
1812+ switch (kex->kex_type) {
1813+ case KEX_GSS_GRP1_SHA1:
1814+ case KEX_GSS_GRP14_SHA1:
1815+ case KEX_GSS_GRP14_SHA256:
1816+ case KEX_GSS_GRP16_SHA512:
1817+ r = kex_dh_dec(kex, server_blob, &shared_secret);
1818+ break;
1819+ case KEX_GSS_C25519_SHA256:
1820+ if (sshbuf_ptr(server_blob)[sshbuf_len(server_blob)] & 0x80)
1821+ fatal("The received key has MSB of last octet set!");
1822+ r = kex_c25519_dec(kex, server_blob, &shared_secret);
1823+ break;
1824+ case KEX_GSS_NISTP256_SHA256:
1825+ if (sshbuf_len(server_blob) != 65)
1826+ fatal("The received NIST-P256 key did not match"
1827+ "expected length (expected 65, got %zu)", sshbuf_len(server_blob));
1828+
1829+ if (sshbuf_ptr(server_blob)[0] != POINT_CONVERSION_UNCOMPRESSED)
1830+ fatal("The received NIST-P256 key does not have first octet 0x04");
1831+
1832+ r = kex_ecdh_dec(kex, server_blob, &shared_secret);
1833+ break;
1834+ default:
1835+ r = SSH_ERR_INVALID_ARGUMENT;
1836+ break;
1837+ }
1838+ if (r != 0)
1839+ goto out;
1840+
1841+ if ((empty = sshbuf_new()) == NULL) {
1842+ r = SSH_ERR_ALLOC_FAIL;
1843+ goto out;
1844+ }
1845+
1846+ hashlen = sizeof(hash);
1847+ if ((r = kex_gen_hash(
1848+ kex->hash_alg,
1849+ kex->client_version,
1850+ kex->server_version,
1851+ kex->my,
1852+ kex->peer,
1853+ (server_host_key_blob ? server_host_key_blob : empty),
1854+ kex->client_pub,
1855+ server_blob,
1856+ shared_secret,
1857+ hash, &hashlen)) != 0)
1858+ fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
1859+
1860+ gssbuf.value = hash;
1861+ gssbuf.length = hashlen;
1862+
1863+ /* Verify that the hash matches the MIC we just got. */
1864+ if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
1865+ sshpkt_disconnect(ssh, "Hash's MIC didn't verify");
1866+
1867+ gss_release_buffer(&min_status, &msg_tok);
1868+
1869+ if (kex->gss_deleg_creds)
1870+ ssh_gssapi_credentials_updated(ctxt);
1871+
1872+ if (gss_kex_context == NULL)
1873+ gss_kex_context = ctxt;
1874+ else
1875+ ssh_gssapi_delete_ctx(&ctxt);
1876+
1877+ if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
1878+ r = kex_send_newkeys(ssh);
1879+
1880+out:
1881+ explicit_bzero(hash, sizeof(hash));
1882+ explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
1883+ sshbuf_free(empty);
1884+ sshbuf_free(server_host_key_blob);
1885+ sshbuf_free(server_blob);
1886+ sshbuf_free(shared_secret);
1887+ sshbuf_free(kex->client_pub);
1888+ kex->client_pub = NULL;
1889+ return r;
1890+}
1891+
1892+int
1893+kexgssgex_client(struct ssh *ssh)
1894+{
1895+ struct kex *kex = ssh->kex;
1896+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER,
1897+ recv_tok = GSS_C_EMPTY_BUFFER, gssbuf,
1898+ msg_tok = GSS_C_EMPTY_BUFFER, *token_ptr;
1899+ Gssctxt *ctxt;
1900+ OM_uint32 maj_status, min_status, ret_flags;
1901+ struct sshbuf *shared_secret = NULL;
1902+ BIGNUM *p = NULL;
1903+ BIGNUM *g = NULL;
1904+ struct sshbuf *buf = NULL;
1905+ struct sshbuf *server_host_key_blob = NULL;
1906+ struct sshbuf *server_blob = NULL;
1907+ BIGNUM *dh_server_pub = NULL;
1908+ u_char *msg;
1909+ int type = 0;
1910+ int first = 1;
1911+ u_char hash[SSH_DIGEST_MAX_LENGTH];
1912+ size_t hashlen;
1913+ const BIGNUM *pub_key, *dh_p, *dh_g;
1914+ int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
1915+ struct sshbuf *empty = NULL;
1916+ u_char c;
1917+ int r;
1918+
1919+ /* Initialise our GSSAPI world */
1920+ ssh_gssapi_build_ctx(&ctxt);
1921+ if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type)
1922+ == GSS_C_NO_OID)
1923+ fatal("Couldn't identify host exchange");
1924+
1925+ if (ssh_gssapi_import_name(ctxt, kex->gss_host))
1926+ fatal("Couldn't import hostname");
1927+
1928+ if (kex->gss_client &&
1929+ ssh_gssapi_client_identity(ctxt, kex->gss_client))
1930+ fatal("Couldn't acquire client credentials");
1931+
1932+ debug("Doing group exchange");
1933+ nbits = dh_estimate(kex->dh_need * 8);
1934+
1935+ kex->min = DH_GRP_MIN;
1936+ kex->max = DH_GRP_MAX;
1937+ kex->nbits = nbits;
1938+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_GROUPREQ)) != 0 ||
1939+ (r = sshpkt_put_u32(ssh, min)) != 0 ||
1940+ (r = sshpkt_put_u32(ssh, nbits)) != 0 ||
1941+ (r = sshpkt_put_u32(ssh, max)) != 0 ||
1942+ (r = sshpkt_send(ssh)) != 0)
1943+ fatal("Failed to construct a packet: %s", ssh_err(r));
1944+
1945+ if ((r = ssh_packet_read_expect(ssh, SSH2_MSG_KEXGSS_GROUP)) != 0)
1946+ fatal("Error: %s", ssh_err(r));
1947+
1948+ if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 ||
1949+ (r = sshpkt_get_bignum2(ssh, &g)) != 0 ||
1950+ (r = sshpkt_get_end(ssh)) != 0)
1951+ fatal("shpkt_get_bignum2 failed: %s", ssh_err(r));
1952+
1953+ if (BN_num_bits(p) < min || BN_num_bits(p) > max)
1954+ fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
1955+ min, BN_num_bits(p), max);
1956+
1957+ if ((kex->dh = dh_new_group(g, p)) == NULL)
1958+ fatal("dn_new_group() failed");
1959+ p = g = NULL; /* belong to kex->dh now */
1960+
1961+ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
1962+ goto out;
1963+ DH_get0_key(kex->dh, &pub_key, NULL);
1964+
1965+ token_ptr = GSS_C_NO_BUFFER;
1966+
1967+ do {
1968+ /* Step 2 - call GSS_Init_sec_context() */
1969+ debug("Calling gss_init_sec_context");
1970+
1971+ maj_status = ssh_gssapi_init_ctx(ctxt,
1972+ kex->gss_deleg_creds, token_ptr, &send_tok,
1973+ &ret_flags);
1974+
1975+ if (GSS_ERROR(maj_status)) {
1976+ /* XXX Useles code: Missing send? */
1977+ if (send_tok.length != 0) {
1978+ if ((r = sshpkt_start(ssh,
1979+ SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
1980+ (r = sshpkt_put_string(ssh, send_tok.value,
1981+ send_tok.length)) != 0)
1982+ fatal("sshpkt failed: %s", ssh_err(r));
1983+ }
1984+ fatal("gss_init_context failed");
1985+ }
1986+
1987+ /* If we've got an old receive buffer get rid of it */
1988+ if (token_ptr != GSS_C_NO_BUFFER)
1989+ gss_release_buffer(&min_status, &recv_tok);
1990+
1991+ if (maj_status == GSS_S_COMPLETE) {
1992+ /* If mutual state flag is not true, kex fails */
1993+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
1994+ fatal("Mutual authentication failed");
1995+
1996+ /* If integ avail flag is not true kex fails */
1997+ if (!(ret_flags & GSS_C_INTEG_FLAG))
1998+ fatal("Integrity check failed");
1999+ }
2000+
2001+ /*
2002+ * If we have data to send, then the last message that we
2003+ * received cannot have been a 'complete'.
2004+ */
2005+ if (send_tok.length != 0) {
2006+ if (first) {
2007+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_INIT)) != 0 ||
2008+ (r = sshpkt_put_string(ssh, send_tok.value,
2009+ send_tok.length)) != 0 ||
2010+ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0)
2011+ fatal("sshpkt failed: %s", ssh_err(r));
2012+ first = 0;
2013+ } else {
2014+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2015+ (r = sshpkt_put_string(ssh,send_tok.value,
2016+ send_tok.length)) != 0)
2017+ fatal("sshpkt failed: %s", ssh_err(r));
2018+ }
2019+ if ((r = sshpkt_send(ssh)) != 0)
2020+ fatal("sshpkt_send failed: %s", ssh_err(r));
2021+ gss_release_buffer(&min_status, &send_tok);
2022+
2023+ /* If we've sent them data, they should reply */
2024+ do {
2025+ type = ssh_packet_read(ssh);
2026+ if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
2027+ debug("Received KEXGSS_HOSTKEY");
2028+ if (server_host_key_blob)
2029+ fatal("Server host key received more than once");
2030+ if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
2031+ fatal("sshpkt failed: %s", ssh_err(r));
2032+ }
2033+ } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
2034+
2035+ switch (type) {
2036+ case SSH2_MSG_KEXGSS_CONTINUE:
2037+ debug("Received GSSAPI_CONTINUE");
2038+ if (maj_status == GSS_S_COMPLETE)
2039+ fatal("GSSAPI Continue received from server when complete");
2040+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2041+ &recv_tok)) != 0 ||
2042+ (r = sshpkt_get_end(ssh)) != 0)
2043+ fatal("sshpkt failed: %s", ssh_err(r));
2044+ break;
2045+ case SSH2_MSG_KEXGSS_COMPLETE:
2046+ debug("Received GSSAPI_COMPLETE");
2047+ if (msg_tok.value != NULL)
2048+ fatal("Received GSSAPI_COMPLETE twice?");
2049+ if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 ||
2050+ (r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2051+ &msg_tok)) != 0)
2052+ fatal("sshpkt failed: %s", ssh_err(r));
2053+
2054+ /* Is there a token included? */
2055+ if ((r = sshpkt_get_u8(ssh, &c)) != 0)
2056+ fatal("sshpkt failed: %s", ssh_err(r));
2057+ if (c) {
2058+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(
2059+ ssh, &recv_tok)) != 0 ||
2060+ (r = sshpkt_get_end(ssh)) != 0)
2061+ fatal("sshpkt failed: %s", ssh_err(r));
2062+ /* If we're already complete - protocol error */
2063+ if (maj_status == GSS_S_COMPLETE)
2064+ sshpkt_disconnect(ssh, "Protocol error: received token when complete");
2065+ } else {
2066+ /* No token included */
2067+ if (maj_status != GSS_S_COMPLETE)
2068+ sshpkt_disconnect(ssh, "Protocol error: did not receive final token");
2069+ }
2070+ break;
2071+ case SSH2_MSG_KEXGSS_ERROR:
2072+ debug("Received Error");
2073+ if ((r = sshpkt_get_u32(ssh, &maj_status)) != 0 ||
2074+ (r = sshpkt_get_u32(ssh, &min_status)) != 0 ||
2075+ (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
2076+ (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* lang tag */
2077+ (r = sshpkt_get_end(ssh)) != 0)
2078+ fatal("sshpkt failed: %s", ssh_err(r));
2079+ fatal("GSSAPI Error: \n%.400s", msg);
2080+ default:
2081+ sshpkt_disconnect(ssh, "Protocol error: didn't expect packet type %d",
2082+ type);
2083+ }
2084+ token_ptr = &recv_tok;
2085+ } else {
2086+ /* No data, and not complete */
2087+ if (maj_status != GSS_S_COMPLETE)
2088+ fatal("Not complete, and no token output");
2089+ }
2090+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
2091+
2092+ /*
2093+ * We _must_ have received a COMPLETE message in reply from the
2094+ * server, which will have set dh_server_pub and msg_tok
2095+ */
2096+
2097+ if (type != SSH2_MSG_KEXGSS_COMPLETE)
2098+ fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
2099+
2100+ /* 7. C verifies that the key Q_S is valid */
2101+ /* 8. C computes shared secret */
2102+ if ((buf = sshbuf_new()) == NULL ||
2103+ (r = sshbuf_put_stringb(buf, server_blob)) != 0 ||
2104+ (r = sshbuf_get_bignum2(buf, &dh_server_pub)) != 0)
2105+ goto out;
2106+ sshbuf_free(buf);
2107+ buf = NULL;
2108+
2109+ if ((shared_secret = sshbuf_new()) == NULL) {
2110+ r = SSH_ERR_ALLOC_FAIL;
2111+ goto out;
2112+ }
2113+
2114+ if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0)
2115+ goto out;
2116+ if ((empty = sshbuf_new()) == NULL) {
2117+ r = SSH_ERR_ALLOC_FAIL;
2118+ goto out;
2119+ }
2120+
2121+ DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
2122+ hashlen = sizeof(hash);
2123+ if ((r = kexgex_hash(
2124+ kex->hash_alg,
2125+ kex->client_version,
2126+ kex->server_version,
2127+ kex->my,
2128+ kex->peer,
2129+ (server_host_key_blob ? server_host_key_blob : empty),
2130+ kex->min, kex->nbits, kex->max,
2131+ dh_p, dh_g,
2132+ pub_key,
2133+ dh_server_pub,
2134+ sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
2135+ hash, &hashlen)) != 0)
2136+ fatal("Failed to calculate hash: %s", ssh_err(r));
2137+
2138+ gssbuf.value = hash;
2139+ gssbuf.length = hashlen;
2140+
2141+ /* Verify that the hash matches the MIC we just got. */
2142+ if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
2143+ sshpkt_disconnect(ssh, "Hash's MIC didn't verify");
2144+
2145+ gss_release_buffer(&min_status, &msg_tok);
2146+
2147+ /* save session id */
2148+ if (kex->session_id == NULL) {
2149+ kex->session_id_len = hashlen;
2150+ kex->session_id = xmalloc(kex->session_id_len);
2151+ memcpy(kex->session_id, hash, kex->session_id_len);
2152+ }
2153+
2154+ if (kex->gss_deleg_creds)
2155+ ssh_gssapi_credentials_updated(ctxt);
2156+
2157+ if (gss_kex_context == NULL)
2158+ gss_kex_context = ctxt;
2159+ else
2160+ ssh_gssapi_delete_ctx(&ctxt);
2161+
2162+ /* Finally derive the keys and send them */
2163+ if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
2164+ r = kex_send_newkeys(ssh);
2165+out:
2166+ sshbuf_free(buf);
2167+ sshbuf_free(server_blob);
2168+ sshbuf_free(empty);
2169+ explicit_bzero(hash, sizeof(hash));
2170+ DH_free(kex->dh);
2171+ kex->dh = NULL;
2172+ BN_clear_free(dh_server_pub);
2173+ sshbuf_free(shared_secret);
2174+ sshbuf_free(server_host_key_blob);
2175+ return r;
2176+}
2177+#endif /* defined(GSSAPI) && defined(WITH_OPENSSL) */
2178diff --git a/kexgsss.c b/kexgsss.c
2179new file mode 100644
2180index 000000000..60bc02deb
2181--- /dev/null
2182+++ b/kexgsss.c
2183@@ -0,0 +1,474 @@
2184+/*
2185+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
2186+ *
2187+ * Redistribution and use in source and binary forms, with or without
2188+ * modification, are permitted provided that the following conditions
2189+ * are met:
2190+ * 1. Redistributions of source code must retain the above copyright
2191+ * notice, this list of conditions and the following disclaimer.
2192+ * 2. Redistributions in binary form must reproduce the above copyright
2193+ * notice, this list of conditions and the following disclaimer in the
2194+ * documentation and/or other materials provided with the distribution.
2195+ *
2196+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
2197+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2198+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2199+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2200+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2201+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2202+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2203+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2204+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2205+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2206+ */
2207+
2208+#include "includes.h"
2209+
2210+#if defined(GSSAPI) && defined(WITH_OPENSSL)
2211+
2212+#include <string.h>
2213+
2214+#include <openssl/crypto.h>
2215+#include <openssl/bn.h>
2216+
2217+#include "xmalloc.h"
2218+#include "sshbuf.h"
2219+#include "ssh2.h"
2220+#include "sshkey.h"
2221+#include "cipher.h"
2222+#include "kex.h"
2223+#include "log.h"
2224+#include "packet.h"
2225+#include "dh.h"
2226+#include "ssh-gss.h"
2227+#include "monitor_wrap.h"
2228+#include "misc.h" /* servconf.h needs misc.h for struct ForwardOptions */
2229+#include "servconf.h"
2230+#include "ssh-gss.h"
2231+#include "digest.h"
2232+#include "ssherr.h"
2233+
2234+extern ServerOptions options;
2235+
2236+int
2237+kexgss_server(struct ssh *ssh)
2238+{
2239+ struct kex *kex = ssh->kex;
2240+ OM_uint32 maj_status, min_status;
2241+
2242+ /*
2243+ * Some GSSAPI implementations use the input value of ret_flags (an
2244+ * output variable) as a means of triggering mechanism specific
2245+ * features. Initializing it to zero avoids inadvertently
2246+ * activating this non-standard behaviour.
2247+ */
2248+
2249+ OM_uint32 ret_flags = 0;
2250+ gss_buffer_desc gssbuf, recv_tok, msg_tok;
2251+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
2252+ Gssctxt *ctxt = NULL;
2253+ struct sshbuf *shared_secret = NULL;
2254+ struct sshbuf *client_pubkey = NULL;
2255+ struct sshbuf *server_pubkey = NULL;
2256+ struct sshbuf *empty = sshbuf_new();
2257+ int type = 0;
2258+ gss_OID oid;
2259+ char *mechs;
2260+ u_char hash[SSH_DIGEST_MAX_LENGTH];
2261+ size_t hashlen;
2262+ int r;
2263+
2264+ /* Initialise GSSAPI */
2265+
2266+ /* If we're rekeying, privsep means that some of the private structures
2267+ * in the GSSAPI code are no longer available. This kludges them back
2268+ * into life
2269+ */
2270+ if (!ssh_gssapi_oid_table_ok()) {
2271+ mechs = ssh_gssapi_server_mechanisms();
2272+ free(mechs);
2273+ }
2274+
2275+ debug2("%s: Identifying %s", __func__, kex->name);
2276+ oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type);
2277+ if (oid == GSS_C_NO_OID)
2278+ fatal("Unknown gssapi mechanism");
2279+
2280+ debug2("%s: Acquiring credentials", __func__);
2281+
2282+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
2283+ fatal("Unable to acquire credentials for the server");
2284+
2285+ do {
2286+ debug("Wait SSH2_MSG_KEXGSS_INIT");
2287+ type = ssh_packet_read(ssh);
2288+ switch(type) {
2289+ case SSH2_MSG_KEXGSS_INIT:
2290+ if (client_pubkey != NULL)
2291+ fatal("Received KEXGSS_INIT after initialising");
2292+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2293+ &recv_tok)) != 0 ||
2294+ (r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 ||
2295+ (r = sshpkt_get_end(ssh)) != 0)
2296+ fatal("sshpkt failed: %s", ssh_err(r));
2297+
2298+ switch (kex->kex_type) {
2299+ case KEX_GSS_GRP1_SHA1:
2300+ case KEX_GSS_GRP14_SHA1:
2301+ case KEX_GSS_GRP14_SHA256:
2302+ case KEX_GSS_GRP16_SHA512:
2303+ r = kex_dh_enc(kex, client_pubkey, &server_pubkey,
2304+ &shared_secret);
2305+ break;
2306+ case KEX_GSS_NISTP256_SHA256:
2307+ r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey,
2308+ &shared_secret);
2309+ break;
2310+ case KEX_GSS_C25519_SHA256:
2311+ r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
2312+ &shared_secret);
2313+ break;
2314+ default:
2315+ fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
2316+ }
2317+ if (r != 0)
2318+ goto out;
2319+
2320+ /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
2321+ break;
2322+ case SSH2_MSG_KEXGSS_CONTINUE:
2323+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2324+ &recv_tok)) != 0 ||
2325+ (r = sshpkt_get_end(ssh)) != 0)
2326+ fatal("sshpkt failed: %s", ssh_err(r));
2327+ break;
2328+ default:
2329+ sshpkt_disconnect(ssh,
2330+ "Protocol error: didn't expect packet type %d",
2331+ type);
2332+ }
2333+
2334+ maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok,
2335+ &send_tok, &ret_flags));
2336+
2337+ gss_release_buffer(&min_status, &recv_tok);
2338+
2339+ if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
2340+ fatal("Zero length token output when incomplete");
2341+
2342+ if (client_pubkey == NULL)
2343+ fatal("No client public key");
2344+
2345+ if (maj_status & GSS_S_CONTINUE_NEEDED) {
2346+ debug("Sending GSSAPI_CONTINUE");
2347+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2348+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 ||
2349+ (r = sshpkt_send(ssh)) != 0)
2350+ fatal("sshpkt failed: %s", ssh_err(r));
2351+ gss_release_buffer(&min_status, &send_tok);
2352+ }
2353+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
2354+
2355+ if (GSS_ERROR(maj_status)) {
2356+ if (send_tok.length > 0) {
2357+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2358+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 ||
2359+ (r = sshpkt_send(ssh)) != 0)
2360+ fatal("sshpkt failed: %s", ssh_err(r));
2361+ }
2362+ fatal("accept_ctx died");
2363+ }
2364+
2365+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
2366+ fatal("Mutual Authentication flag wasn't set");
2367+
2368+ if (!(ret_flags & GSS_C_INTEG_FLAG))
2369+ fatal("Integrity flag wasn't set");
2370+
2371+ hashlen = sizeof(hash);
2372+ if ((r = kex_gen_hash(
2373+ kex->hash_alg,
2374+ kex->client_version,
2375+ kex->server_version,
2376+ kex->peer,
2377+ kex->my,
2378+ empty,
2379+ client_pubkey,
2380+ server_pubkey,
2381+ shared_secret,
2382+ hash, &hashlen)) != 0)
2383+ goto out;
2384+
2385+ gssbuf.value = hash;
2386+ gssbuf.length = hashlen;
2387+
2388+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt, &gssbuf, &msg_tok))))
2389+ fatal("Couldn't get MIC");
2390+
2391+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_COMPLETE)) != 0 ||
2392+ (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
2393+ (r = sshpkt_put_string(ssh, msg_tok.value, msg_tok.length)) != 0)
2394+ fatal("sshpkt failed: %s", ssh_err(r));
2395+
2396+ if (send_tok.length != 0) {
2397+ if ((r = sshpkt_put_u8(ssh, 1)) != 0 || /* true */
2398+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0)
2399+ fatal("sshpkt failed: %s", ssh_err(r));
2400+ } else {
2401+ if ((r = sshpkt_put_u8(ssh, 0)) != 0) /* false */
2402+ fatal("sshpkt failed: %s", ssh_err(r));
2403+ }
2404+ if ((r = sshpkt_send(ssh)) != 0)
2405+ fatal("sshpkt_send failed: %s", ssh_err(r));
2406+
2407+ gss_release_buffer(&min_status, &send_tok);
2408+ gss_release_buffer(&min_status, &msg_tok);
2409+
2410+ if (gss_kex_context == NULL)
2411+ gss_kex_context = ctxt;
2412+ else
2413+ ssh_gssapi_delete_ctx(&ctxt);
2414+
2415+ if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
2416+ r = kex_send_newkeys(ssh);
2417+
2418+ /* If this was a rekey, then save out any delegated credentials we
2419+ * just exchanged. */
2420+ if (options.gss_store_rekey)
2421+ ssh_gssapi_rekey_creds();
2422+out:
2423+ sshbuf_free(empty);
2424+ explicit_bzero(hash, sizeof(hash));
2425+ sshbuf_free(shared_secret);
2426+ sshbuf_free(client_pubkey);
2427+ sshbuf_free(server_pubkey);
2428+ return r;
2429+}
2430+
2431+int
2432+kexgssgex_server(struct ssh *ssh)
2433+{
2434+ struct kex *kex = ssh->kex;
2435+ OM_uint32 maj_status, min_status;
2436+
2437+ /*
2438+ * Some GSSAPI implementations use the input value of ret_flags (an
2439+ * output variable) as a means of triggering mechanism specific
2440+ * features. Initializing it to zero avoids inadvertently
2441+ * activating this non-standard behaviour.
2442+ */
2443+
2444+ OM_uint32 ret_flags = 0;
2445+ gss_buffer_desc gssbuf, recv_tok, msg_tok;
2446+ gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
2447+ Gssctxt *ctxt = NULL;
2448+ struct sshbuf *shared_secret = NULL;
2449+ int type = 0;
2450+ gss_OID oid;
2451+ char *mechs;
2452+ u_char hash[SSH_DIGEST_MAX_LENGTH];
2453+ size_t hashlen;
2454+ BIGNUM *dh_client_pub = NULL;
2455+ const BIGNUM *pub_key, *dh_p, *dh_g;
2456+ int min = -1, max = -1, nbits = -1;
2457+ int cmin = -1, cmax = -1; /* client proposal */
2458+ struct sshbuf *empty = sshbuf_new();
2459+ int r;
2460+
2461+ /* Initialise GSSAPI */
2462+
2463+ /* If we're rekeying, privsep means that some of the private structures
2464+ * in the GSSAPI code are no longer available. This kludges them back
2465+ * into life
2466+ */
2467+ if (!ssh_gssapi_oid_table_ok())
2468+ if ((mechs = ssh_gssapi_server_mechanisms()))
2469+ free(mechs);
2470+
2471+ debug2("%s: Identifying %s", __func__, kex->name);
2472+ oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type);
2473+ if (oid == GSS_C_NO_OID)
2474+ fatal("Unknown gssapi mechanism");
2475+
2476+ debug2("%s: Acquiring credentials", __func__);
2477+
2478+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid))))
2479+ fatal("Unable to acquire credentials for the server");
2480+
2481+ /* 5. S generates an ephemeral key pair (do the allocations early) */
2482+ debug("Doing group exchange");
2483+ ssh_packet_read_expect(ssh, SSH2_MSG_KEXGSS_GROUPREQ);
2484+ /* store client proposal to provide valid signature */
2485+ if ((r = sshpkt_get_u32(ssh, &cmin)) != 0 ||
2486+ (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
2487+ (r = sshpkt_get_u32(ssh, &cmax)) != 0 ||
2488+ (r = sshpkt_get_end(ssh)) != 0)
2489+ fatal("sshpkt failed: %s", ssh_err(r));
2490+ kex->nbits = nbits;
2491+ kex->min = cmin;
2492+ kex->max = cmax;
2493+ min = MAX(DH_GRP_MIN, cmin);
2494+ max = MIN(DH_GRP_MAX, cmax);
2495+ nbits = MAXIMUM(DH_GRP_MIN, nbits);
2496+ nbits = MINIMUM(DH_GRP_MAX, nbits);
2497+ if (max < min || nbits < min || max < nbits)
2498+ fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
2499+ min, nbits, max);
2500+ kex->dh = PRIVSEP(choose_dh(min, nbits, max));
2501+ if (kex->dh == NULL) {
2502+ sshpkt_disconnect(ssh, "Protocol error: no matching group found");
2503+ fatal("Protocol error: no matching group found");
2504+ }
2505+
2506+ DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
2507+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_GROUP)) != 0 ||
2508+ (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
2509+ (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
2510+ (r = sshpkt_send(ssh)) != 0)
2511+ fatal("sshpkt failed: %s", ssh_err(r));
2512+
2513+ if ((r = ssh_packet_write_wait(ssh)) != 0)
2514+ fatal("ssh_packet_write_wait: %s", ssh_err(r));
2515+
2516+ /* Compute our exchange value in parallel with the client */
2517+ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
2518+ goto out;
2519+
2520+ do {
2521+ debug("Wait SSH2_MSG_GSSAPI_INIT");
2522+ type = ssh_packet_read(ssh);
2523+ switch(type) {
2524+ case SSH2_MSG_KEXGSS_INIT:
2525+ if (dh_client_pub != NULL)
2526+ fatal("Received KEXGSS_INIT after initialising");
2527+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2528+ &recv_tok)) != 0 ||
2529+ (r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
2530+ (r = sshpkt_get_end(ssh)) != 0)
2531+ fatal("sshpkt failed: %s", ssh_err(r));
2532+
2533+ /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
2534+ break;
2535+ case SSH2_MSG_KEXGSS_CONTINUE:
2536+ if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh,
2537+ &recv_tok)) != 0 ||
2538+ (r = sshpkt_get_end(ssh)) != 0)
2539+ fatal("sshpkt failed: %s", ssh_err(r));
2540+ break;
2541+ default:
2542+ sshpkt_disconnect(ssh,
2543+ "Protocol error: didn't expect packet type %d",
2544+ type);
2545+ }
2546+
2547+ maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok,
2548+ &send_tok, &ret_flags));
2549+
2550+ gss_release_buffer(&min_status, &recv_tok);
2551+
2552+ if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
2553+ fatal("Zero length token output when incomplete");
2554+
2555+ if (dh_client_pub == NULL)
2556+ fatal("No client public key");
2557+
2558+ if (maj_status & GSS_S_CONTINUE_NEEDED) {
2559+ debug("Sending GSSAPI_CONTINUE");
2560+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2561+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 ||
2562+ (r = sshpkt_send(ssh)) != 0)
2563+ fatal("sshpkt failed: %s", ssh_err(r));
2564+ gss_release_buffer(&min_status, &send_tok);
2565+ }
2566+ } while (maj_status & GSS_S_CONTINUE_NEEDED);
2567+
2568+ if (GSS_ERROR(maj_status)) {
2569+ if (send_tok.length > 0) {
2570+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 ||
2571+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 ||
2572+ (r = sshpkt_send(ssh)) != 0)
2573+ fatal("sshpkt failed: %s", ssh_err(r));
2574+ }
2575+ fatal("accept_ctx died");
2576+ }
2577+
2578+ if (!(ret_flags & GSS_C_MUTUAL_FLAG))
2579+ fatal("Mutual Authentication flag wasn't set");
2580+
2581+ if (!(ret_flags & GSS_C_INTEG_FLAG))
2582+ fatal("Integrity flag wasn't set");
2583+
2584+ /* calculate shared secret */
2585+ if ((shared_secret = sshbuf_new()) == NULL) {
2586+ r = SSH_ERR_ALLOC_FAIL;
2587+ goto out;
2588+ }
2589+ if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0)
2590+ goto out;
2591+
2592+ DH_get0_key(kex->dh, &pub_key, NULL);
2593+ DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
2594+ hashlen = sizeof(hash);
2595+ if ((r = kexgex_hash(
2596+ kex->hash_alg,
2597+ kex->client_version,
2598+ kex->server_version,
2599+ kex->peer,
2600+ kex->my,
2601+ empty,
2602+ cmin, nbits, cmax,
2603+ dh_p, dh_g,
2604+ dh_client_pub,
2605+ pub_key,
2606+ sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
2607+ hash, &hashlen)) != 0)
2608+ fatal("kexgex_hash failed: %s", ssh_err(r));
2609+
2610+ gssbuf.value = hash;
2611+ gssbuf.length = hashlen;
2612+
2613+ if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt, &gssbuf, &msg_tok))))
2614+ fatal("Couldn't get MIC");
2615+
2616+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_COMPLETE)) != 0 ||
2617+ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
2618+ (r = sshpkt_put_string(ssh, msg_tok.value, msg_tok.length)) != 0)
2619+ fatal("sshpkt failed: %s", ssh_err(r));
2620+
2621+ if (send_tok.length != 0) {
2622+ if ((r = sshpkt_put_u8(ssh, 1)) != 0 || /* true */
2623+ (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0)
2624+ fatal("sshpkt failed: %s", ssh_err(r));
2625+ } else {
2626+ if ((r = sshpkt_put_u8(ssh, 0)) != 0) /* false */
2627+ fatal("sshpkt failed: %s", ssh_err(r));
2628+ }
2629+ if ((r = sshpkt_send(ssh)) != 0)
2630+ fatal("sshpkt failed: %s", ssh_err(r));
2631+
2632+ gss_release_buffer(&min_status, &send_tok);
2633+ gss_release_buffer(&min_status, &msg_tok);
2634+
2635+ if (gss_kex_context == NULL)
2636+ gss_kex_context = ctxt;
2637+ else
2638+ ssh_gssapi_delete_ctx(&ctxt);
2639+
2640+ /* Finally derive the keys and send them */
2641+ if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
2642+ r = kex_send_newkeys(ssh);
2643+
2644+ /* If this was a rekey, then save out any delegated credentials we
2645+ * just exchanged. */
2646+ if (options.gss_store_rekey)
2647+ ssh_gssapi_rekey_creds();
2648+out:
2649+ sshbuf_free(empty);
2650+ explicit_bzero(hash, sizeof(hash));
2651+ DH_free(kex->dh);
2652+ kex->dh = NULL;
2653+ BN_clear_free(dh_client_pub);
2654+ sshbuf_free(shared_secret);
2655+ return r;
2656+}
2657+#endif /* defined(GSSAPI) && defined(WITH_OPENSSL) */
2658diff --git a/monitor.c b/monitor.c
2659index 4cf79dfc9..11868952b 100644
2660--- a/monitor.c
2661+++ b/monitor.c
2662@@ -148,6 +148,8 @@ int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
2663 int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
2664 int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
2665 int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
2666+int mm_answer_gss_sign(struct ssh *, int, struct sshbuf *);
2667+int mm_answer_gss_updatecreds(struct ssh *, int, struct sshbuf *);
2668 #endif
2669
2670 #ifdef SSH_AUDIT_EVENTS
2671@@ -220,11 +222,18 @@ struct mon_table mon_dispatch_proto20[] = {
2672 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
2673 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
2674 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
2675+ {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
2676 #endif
2677 {0, 0, NULL}
2678 };
2679
2680 struct mon_table mon_dispatch_postauth20[] = {
2681+#ifdef GSSAPI
2682+ {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
2683+ {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
2684+ {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
2685+ {MONITOR_REQ_GSSUPCREDS, 0, mm_answer_gss_updatecreds},
2686+#endif
2687 #ifdef WITH_OPENSSL
2688 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
2689 #endif
2690@@ -293,6 +302,10 @@ monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
2691 /* Permit requests for moduli and signatures */
2692 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
2693 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
2694+#ifdef GSSAPI
2695+ /* and for the GSSAPI key exchange */
2696+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
2697+#endif
2698
2699 /* The first few requests do not require asynchronous access */
2700 while (!authenticated) {
2701@@ -406,6 +419,10 @@ monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
2702 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
2703 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
2704 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
2705+#ifdef GSSAPI
2706+ /* and for the GSSAPI key exchange */
2707+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
2708+#endif
2709
2710 if (auth_opts->permit_pty_flag) {
2711 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
2712@@ -1725,6 +1742,17 @@ monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
2713 # ifdef OPENSSL_HAS_ECC
2714 kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
2715 # endif
2716+# ifdef GSSAPI
2717+ if (options.gss_keyex) {
2718+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
2719+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
2720+ kex->kex[KEX_GSS_GRP14_SHA256] = kexgss_server;
2721+ kex->kex[KEX_GSS_GRP16_SHA512] = kexgss_server;
2722+ kex->kex[KEX_GSS_GEX_SHA1] = kexgssgex_server;
2723+ kex->kex[KEX_GSS_NISTP256_SHA256] = kexgss_server;
2724+ kex->kex[KEX_GSS_C25519_SHA256] = kexgss_server;
2725+ }
2726+# endif
2727 #endif /* WITH_OPENSSL */
2728 kex->kex[KEX_C25519_SHA256] = kex_gen_server;
2729 kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
2730@@ -1818,8 +1846,8 @@ mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
2731 u_char *p;
2732 int r;
2733
2734- if (!options.gss_authentication)
2735- fatal("%s: GSSAPI authentication not enabled", __func__);
2736+ if (!options.gss_authentication && !options.gss_keyex)
2737+ fatal("%s: GSSAPI not enabled", __func__);
2738
2739 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
2740 fatal("%s: buffer error: %s", __func__, ssh_err(r));
2741@@ -1851,8 +1879,8 @@ mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
2742 OM_uint32 flags = 0; /* GSI needs this */
2743 int r;
2744
2745- if (!options.gss_authentication)
2746- fatal("%s: GSSAPI authentication not enabled", __func__);
2747+ if (!options.gss_authentication && !options.gss_keyex)
2748+ fatal("%s: GSSAPI not enabled", __func__);
2749
2750 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
2751 fatal("%s: buffer error: %s", __func__, ssh_err(r));
2752@@ -1872,6 +1900,7 @@ mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
2753 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2754 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2755 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2756+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
2757 }
2758 return (0);
2759 }
2760@@ -1883,8 +1912,8 @@ mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
2761 OM_uint32 ret;
2762 int r;
2763
2764- if (!options.gss_authentication)
2765- fatal("%s: GSSAPI authentication not enabled", __func__);
2766+ if (!options.gss_authentication && !options.gss_keyex)
2767+ fatal("%s: GSSAPI not enabled", __func__);
2768
2769 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
2770 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
2771@@ -1910,13 +1939,17 @@ mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
2772 int
2773 mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
2774 {
2775- int r, authenticated;
2776+ int r, authenticated, kex;
2777 const char *displayname;
2778
2779- if (!options.gss_authentication)
2780- fatal("%s: GSSAPI authentication not enabled", __func__);
2781+ if (!options.gss_authentication && !options.gss_keyex)
2782+ fatal("%s: GSSAPI not enabled", __func__);
2783
2784- authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2785+ if ((r = sshbuf_get_u32(m, &kex)) != 0)
2786+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2787+
2788+ authenticated = authctxt->valid &&
2789+ ssh_gssapi_userok(authctxt->user, authctxt->pw, kex);
2790
2791 sshbuf_reset(m);
2792 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
2793@@ -1925,7 +1958,11 @@ mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
2794 debug3("%s: sending result %d", __func__, authenticated);
2795 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2796
2797- auth_method = "gssapi-with-mic";
2798+ if (kex) {
2799+ auth_method = "gssapi-keyex";
2800+ } else {
2801+ auth_method = "gssapi-with-mic";
2802+ }
2803
2804 if ((displayname = ssh_gssapi_displayname()) != NULL)
2805 auth2_record_info(authctxt, "%s", displayname);
2806@@ -1933,5 +1970,85 @@ mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
2807 /* Monitor loop will terminate if authenticated */
2808 return (authenticated);
2809 }
2810+
2811+int
2812+mm_answer_gss_sign(struct ssh *ssh, int socket, struct sshbuf *m)
2813+{
2814+ gss_buffer_desc data;
2815+ gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2816+ OM_uint32 major, minor;
2817+ size_t len;
2818+ u_char *p = NULL;
2819+ int r;
2820+
2821+ if (!options.gss_authentication && !options.gss_keyex)
2822+ fatal("%s: GSSAPI not enabled", __func__);
2823+
2824+ if ((r = sshbuf_get_string(m, &p, &len)) != 0)
2825+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2826+ data.value = p;
2827+ data.length = len;
2828+ /* Lengths of SHA-1, SHA-256 and SHA-512 hashes that are used */
2829+ if (data.length != 20 && data.length != 32 && data.length != 64)
2830+ fatal("%s: data length incorrect: %d", __func__,
2831+ (int) data.length);
2832+
2833+ /* Save the session ID on the first time around */
2834+ if (session_id2_len == 0) {
2835+ session_id2_len = data.length;
2836+ session_id2 = xmalloc(session_id2_len);
2837+ memcpy(session_id2, data.value, session_id2_len);
2838+ }
2839+ major = ssh_gssapi_sign(gsscontext, &data, &hash);
2840+
2841+ free(data.value);
2842+
2843+ sshbuf_reset(m);
2844+
2845+ if ((r = sshbuf_put_u32(m, major)) != 0 ||
2846+ (r = sshbuf_put_string(m, hash.value, hash.length)) != 0)
2847+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2848+
2849+ mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2850+
2851+ gss_release_buffer(&minor, &hash);
2852+
2853+ /* Turn on getpwnam permissions */
2854+ monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2855+
2856+ /* And credential updating, for when rekeying */
2857+ monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
2858+
2859+ return (0);
2860+}
2861+
2862+int
2863+mm_answer_gss_updatecreds(struct ssh *ssh, int socket, struct sshbuf *m) {
2864+ ssh_gssapi_ccache store;
2865+ int r, ok;
2866+
2867+ if (!options.gss_authentication && !options.gss_keyex)
2868+ fatal("%s: GSSAPI not enabled", __func__);
2869+
2870+ if ((r = sshbuf_get_string(m, (u_char **)&store.filename, NULL)) != 0 ||
2871+ (r = sshbuf_get_string(m, (u_char **)&store.envvar, NULL)) != 0 ||
2872+ (r = sshbuf_get_string(m, (u_char **)&store.envval, NULL)) != 0)
2873+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2874+
2875+ ok = ssh_gssapi_update_creds(&store);
2876+
2877+ free(store.filename);
2878+ free(store.envvar);
2879+ free(store.envval);
2880+
2881+ sshbuf_reset(m);
2882+ if ((r = sshbuf_put_u32(m, ok)) != 0)
2883+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2884+
2885+ mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
2886+
2887+ return(0);
2888+}
2889+
2890 #endif /* GSSAPI */
2891
2892diff --git a/monitor.h b/monitor.h
2893index 683e5e071..2b1a2d590 100644
2894--- a/monitor.h
2895+++ b/monitor.h
2896@@ -63,6 +63,8 @@ enum monitor_reqtype {
2897 MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
2898 MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
2899
2900+ MONITOR_REQ_GSSSIGN = 150, MONITOR_ANS_GSSSIGN = 151,
2901+ MONITOR_REQ_GSSUPCREDS = 152, MONITOR_ANS_GSSUPCREDS = 153,
2902 };
2903
2904 struct ssh;
2905diff --git a/monitor_wrap.c b/monitor_wrap.c
2906index 5e38d83eb..0e78cd006 100644
2907--- a/monitor_wrap.c
2908+++ b/monitor_wrap.c
2909@@ -993,13 +993,15 @@ mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
2910 }
2911
2912 int
2913-mm_ssh_gssapi_userok(char *user)
2914+mm_ssh_gssapi_userok(char *user, struct passwd *pw, int kex)
2915 {
2916 struct sshbuf *m;
2917 int r, authenticated = 0;
2918
2919 if ((m = sshbuf_new()) == NULL)
2920 fatal("%s: sshbuf_new failed", __func__);
2921+ if ((r = sshbuf_put_u32(m, kex)) != 0)
2922+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2923
2924 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
2925 mm_request_receive_expect(pmonitor->m_recvfd,
2926@@ -1012,4 +1014,57 @@ mm_ssh_gssapi_userok(char *user)
2927 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
2928 return (authenticated);
2929 }
2930+
2931+OM_uint32
2932+mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash)
2933+{
2934+ struct sshbuf *m;
2935+ OM_uint32 major;
2936+ int r;
2937+
2938+ if ((m = sshbuf_new()) == NULL)
2939+ fatal("%s: sshbuf_new failed", __func__);
2940+ if ((r = sshbuf_put_string(m, data->value, data->length)) != 0)
2941+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2942+
2943+ mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, m);
2944+ mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, m);
2945+
2946+ if ((r = sshbuf_get_u32(m, &major)) != 0 ||
2947+ (r = ssh_gssapi_get_buffer_desc(m, hash)) != 0)
2948+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2949+
2950+ sshbuf_free(m);
2951+
2952+ return (major);
2953+}
2954+
2955+int
2956+mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
2957+{
2958+ struct sshbuf *m;
2959+ int r, ok;
2960+
2961+ if ((m = sshbuf_new()) == NULL)
2962+ fatal("%s: sshbuf_new failed", __func__);
2963+
2964+ if ((r = sshbuf_put_cstring(m,
2965+ store->filename ? store->filename : "")) != 0 ||
2966+ (r = sshbuf_put_cstring(m,
2967+ store->envvar ? store->envvar : "")) != 0 ||
2968+ (r = sshbuf_put_cstring(m,
2969+ store->envval ? store->envval : "")) != 0)
2970+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2971+
2972+ mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUPCREDS, m);
2973+ mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUPCREDS, m);
2974+
2975+ if ((r = sshbuf_get_u32(m, &ok)) != 0)
2976+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
2977+
2978+ sshbuf_free(m);
2979+
2980+ return (ok);
2981+}
2982+
2983 #endif /* GSSAPI */
2984diff --git a/monitor_wrap.h b/monitor_wrap.h
2985index 0db38c206..75aef1c74 100644
2986--- a/monitor_wrap.h
2987+++ b/monitor_wrap.h
2988@@ -65,8 +65,10 @@ int mm_sshkey_verify(const struct sshkey *, const u_char *, size_t,
2989 OM_uint32 mm_ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
2990 OM_uint32 mm_ssh_gssapi_accept_ctx(Gssctxt *,
2991 gss_buffer_desc *, gss_buffer_desc *, OM_uint32 *);
2992-int mm_ssh_gssapi_userok(char *user);
2993+int mm_ssh_gssapi_userok(char *user, struct passwd *, int kex);
2994 OM_uint32 mm_ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
2995+OM_uint32 mm_ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
2996+int mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *);
2997 #endif
2998
2999 #ifdef USE_PAM
3000diff --git a/readconf.c b/readconf.c
3001index 554efd7c9..57dae55d1 100644
3002--- a/readconf.c
3003+++ b/readconf.c
3004@@ -67,6 +67,7 @@
3005 #include "uidswap.h"
3006 #include "myproposal.h"
3007 #include "digest.h"
3008+#include "ssh-gss.h"
3009
3010 /* Format of the configuration file:
3011
3012@@ -160,6 +161,8 @@ typedef enum {
3013 oClearAllForwardings, oNoHostAuthenticationForLocalhost,
3014 oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
3015 oAddressFamily, oGssAuthentication, oGssDelegateCreds,
3016+ oGssTrustDns, oGssKeyEx, oGssClientIdentity, oGssRenewalRekey,
3017+ oGssServerIdentity, oGssKexAlgorithms,
3018 oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
3019 oSendEnv, oSetEnv, oControlPath, oControlMaster, oControlPersist,
3020 oHashKnownHosts,
3021@@ -204,10 +207,22 @@ static struct {
3022 /* Sometimes-unsupported options */
3023 #if defined(GSSAPI)
3024 { "gssapiauthentication", oGssAuthentication },
3025+ { "gssapikeyexchange", oGssKeyEx },
3026 { "gssapidelegatecredentials", oGssDelegateCreds },
3027+ { "gssapitrustdns", oGssTrustDns },
3028+ { "gssapiclientidentity", oGssClientIdentity },
3029+ { "gssapiserveridentity", oGssServerIdentity },
3030+ { "gssapirenewalforcesrekey", oGssRenewalRekey },
3031+ { "gssapikexalgorithms", oGssKexAlgorithms },
3032 # else
3033 { "gssapiauthentication", oUnsupported },
3034+ { "gssapikeyexchange", oUnsupported },
3035 { "gssapidelegatecredentials", oUnsupported },
3036+ { "gssapitrustdns", oUnsupported },
3037+ { "gssapiclientidentity", oUnsupported },
3038+ { "gssapiserveridentity", oUnsupported },
3039+ { "gssapirenewalforcesrekey", oUnsupported },
3040+ { "gssapikexalgorithms", oUnsupported },
3041 #endif
3042 #ifdef ENABLE_PKCS11
3043 { "pkcs11provider", oPKCS11Provider },
3044@@ -1068,10 +1083,42 @@ parse_time:
3045 intptr = &options->gss_authentication;
3046 goto parse_flag;
3047
3048+ case oGssKeyEx:
3049+ intptr = &options->gss_keyex;
3050+ goto parse_flag;
3051+
3052 case oGssDelegateCreds:
3053 intptr = &options->gss_deleg_creds;
3054 goto parse_flag;
3055
3056+ case oGssTrustDns:
3057+ intptr = &options->gss_trust_dns;
3058+ goto parse_flag;
3059+
3060+ case oGssClientIdentity:
3061+ charptr = &options->gss_client_identity;
3062+ goto parse_string;
3063+
3064+ case oGssServerIdentity:
3065+ charptr = &options->gss_server_identity;
3066+ goto parse_string;
3067+
3068+ case oGssRenewalRekey:
3069+ intptr = &options->gss_renewal_rekey;
3070+ goto parse_flag;
3071+
3072+ case oGssKexAlgorithms:
3073+ arg = strdelim(&s);
3074+ if (!arg || *arg == '\0')
3075+ fatal("%.200s line %d: Missing argument.",
3076+ filename, linenum);
3077+ if (!kex_gss_names_valid(arg))
3078+ fatal("%.200s line %d: Bad GSSAPI KexAlgorithms '%s'.",
3079+ filename, linenum, arg ? arg : "<NONE>");
3080+ if (*activep && options->gss_kex_algorithms == NULL)
3081+ options->gss_kex_algorithms = xstrdup(arg);
3082+ break;
3083+
3084 case oBatchMode:
3085 intptr = &options->batch_mode;
3086 goto parse_flag;
3087@@ -1976,7 +2023,13 @@ initialize_options(Options * options)
3088 options->pubkey_authentication = -1;
3089 options->challenge_response_authentication = -1;
3090 options->gss_authentication = -1;
3091+ options->gss_keyex = -1;
3092 options->gss_deleg_creds = -1;
3093+ options->gss_trust_dns = -1;
3094+ options->gss_renewal_rekey = -1;
3095+ options->gss_client_identity = NULL;
3096+ options->gss_server_identity = NULL;
3097+ options->gss_kex_algorithms = NULL;
3098 options->password_authentication = -1;
3099 options->kbd_interactive_authentication = -1;
3100 options->kbd_interactive_devices = NULL;
3101@@ -2125,8 +2178,18 @@ fill_default_options(Options * options)
3102 options->challenge_response_authentication = 1;
3103 if (options->gss_authentication == -1)
3104 options->gss_authentication = 0;
3105+ if (options->gss_keyex == -1)
3106+ options->gss_keyex = 0;
3107 if (options->gss_deleg_creds == -1)
3108 options->gss_deleg_creds = 0;
3109+ if (options->gss_trust_dns == -1)
3110+ options->gss_trust_dns = 0;
3111+ if (options->gss_renewal_rekey == -1)
3112+ options->gss_renewal_rekey = 0;
3113+#ifdef GSSAPI
3114+ if (options->gss_kex_algorithms == NULL)
3115+ options->gss_kex_algorithms = strdup(GSS_KEX_DEFAULT_KEX);
3116+#endif
3117 if (options->password_authentication == -1)
3118 options->password_authentication = 1;
3119 if (options->kbd_interactive_authentication == -1)
3120@@ -2776,7 +2839,14 @@ dump_client_config(Options *o, const char *host)
3121 dump_cfg_fmtint(oGatewayPorts, o->fwd_opts.gateway_ports);
3122 #ifdef GSSAPI
3123 dump_cfg_fmtint(oGssAuthentication, o->gss_authentication);
3124+ dump_cfg_fmtint(oGssKeyEx, o->gss_keyex);
3125 dump_cfg_fmtint(oGssDelegateCreds, o->gss_deleg_creds);
3126+ dump_cfg_fmtint(oGssTrustDns, o->gss_trust_dns);
3127+ dump_cfg_fmtint(oGssRenewalRekey, o->gss_renewal_rekey);
3128+ dump_cfg_string(oGssClientIdentity, o->gss_client_identity);
3129+ dump_cfg_string(oGssServerIdentity, o->gss_server_identity);
3130+ dump_cfg_string(oGssKexAlgorithms, o->gss_kex_algorithms ?
3131+ o->gss_kex_algorithms : GSS_KEX_DEFAULT_KEX);
3132 #endif /* GSSAPI */
3133 dump_cfg_fmtint(oHashKnownHosts, o->hash_known_hosts);
3134 dump_cfg_fmtint(oHostbasedAuthentication, o->hostbased_authentication);
3135diff --git a/readconf.h b/readconf.h
3136index d6a15550d..3803eeddf 100644
3137--- a/readconf.h
3138+++ b/readconf.h
3139@@ -41,7 +41,13 @@ typedef struct {
3140 int challenge_response_authentication;
3141 /* Try S/Key or TIS, authentication. */
3142 int gss_authentication; /* Try GSS authentication */
3143+ int gss_keyex; /* Try GSS key exchange */
3144 int gss_deleg_creds; /* Delegate GSS credentials */
3145+ int gss_trust_dns; /* Trust DNS for GSS canonicalization */
3146+ int gss_renewal_rekey; /* Credential renewal forces rekey */
3147+ char *gss_client_identity; /* Principal to initiate GSSAPI with */
3148+ char *gss_server_identity; /* GSSAPI target principal */
3149+ char *gss_kex_algorithms; /* GSSAPI kex methods to be offered by client. */
3150 int password_authentication; /* Try password
3151 * authentication. */
3152 int kbd_interactive_authentication; /* Try keyboard-interactive auth. */
3153diff --git a/servconf.c b/servconf.c
3154index f08e37477..ded8f4a87 100644
3155--- a/servconf.c
3156+++ b/servconf.c
3157@@ -70,6 +70,7 @@
3158 #include "auth.h"
3159 #include "myproposal.h"
3160 #include "digest.h"
3161+#include "ssh-gss.h"
3162
3163 static void add_listen_addr(ServerOptions *, const char *,
3164 const char *, int);
3165@@ -134,8 +135,11 @@ initialize_server_options(ServerOptions *options)
3166 options->kerberos_ticket_cleanup = -1;
3167 options->kerberos_get_afs_token = -1;
3168 options->gss_authentication=-1;
3169+ options->gss_keyex = -1;
3170 options->gss_cleanup_creds = -1;
3171 options->gss_strict_acceptor = -1;
3172+ options->gss_store_rekey = -1;
3173+ options->gss_kex_algorithms = NULL;
3174 options->password_authentication = -1;
3175 options->kbd_interactive_authentication = -1;
3176 options->challenge_response_authentication = -1;
3177@@ -376,10 +380,18 @@ fill_default_server_options(ServerOptions *options)
3178 options->kerberos_get_afs_token = 0;
3179 if (options->gss_authentication == -1)
3180 options->gss_authentication = 0;
3181+ if (options->gss_keyex == -1)
3182+ options->gss_keyex = 0;
3183 if (options->gss_cleanup_creds == -1)
3184 options->gss_cleanup_creds = 1;
3185 if (options->gss_strict_acceptor == -1)
3186 options->gss_strict_acceptor = 1;
3187+ if (options->gss_store_rekey == -1)
3188+ options->gss_store_rekey = 0;
3189+#ifdef GSSAPI
3190+ if (options->gss_kex_algorithms == NULL)
3191+ options->gss_kex_algorithms = strdup(GSS_KEX_DEFAULT_KEX);
3192+#endif
3193 if (options->password_authentication == -1)
3194 options->password_authentication = 1;
3195 if (options->kbd_interactive_authentication == -1)
3196@@ -523,6 +535,7 @@ typedef enum {
3197 sHostKeyAlgorithms,
3198 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
3199 sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
3200+ sGssKeyEx, sGssKexAlgorithms, sGssStoreRekey,
3201 sAcceptEnv, sSetEnv, sPermitTunnel,
3202 sMatch, sPermitOpen, sPermitListen, sForceCommand, sChrootDirectory,
3203 sUsePrivilegeSeparation, sAllowAgentForwarding,
3204@@ -600,12 +613,22 @@ static struct {
3205 #ifdef GSSAPI
3206 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
3207 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
3208+ { "gssapicleanupcreds", sGssCleanupCreds, SSHCFG_GLOBAL },
3209 { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
3210+ { "gssapikeyexchange", sGssKeyEx, SSHCFG_GLOBAL },
3211+ { "gssapistorecredentialsonrekey", sGssStoreRekey, SSHCFG_GLOBAL },
3212+ { "gssapikexalgorithms", sGssKexAlgorithms, SSHCFG_GLOBAL },
3213 #else
3214 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
3215 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
3216+ { "gssapicleanupcreds", sUnsupported, SSHCFG_GLOBAL },
3217 { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
3218+ { "gssapikeyexchange", sUnsupported, SSHCFG_GLOBAL },
3219+ { "gssapistorecredentialsonrekey", sUnsupported, SSHCFG_GLOBAL },
3220+ { "gssapikexalgorithms", sUnsupported, SSHCFG_GLOBAL },
3221 #endif
3222+ { "gssusesessionccache", sUnsupported, SSHCFG_GLOBAL },
3223+ { "gssapiusesessioncredcache", sUnsupported, SSHCFG_GLOBAL },
3224 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
3225 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
3226 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
3227@@ -1557,6 +1580,10 @@ process_server_config_line_depth(ServerOptions *options, char *line,
3228 intptr = &options->gss_authentication;
3229 goto parse_flag;
3230
3231+ case sGssKeyEx:
3232+ intptr = &options->gss_keyex;
3233+ goto parse_flag;
3234+
3235 case sGssCleanupCreds:
3236 intptr = &options->gss_cleanup_creds;
3237 goto parse_flag;
3238@@ -1565,6 +1592,22 @@ process_server_config_line_depth(ServerOptions *options, char *line,
3239 intptr = &options->gss_strict_acceptor;
3240 goto parse_flag;
3241
3242+ case sGssStoreRekey:
3243+ intptr = &options->gss_store_rekey;
3244+ goto parse_flag;
3245+
3246+ case sGssKexAlgorithms:
3247+ arg = strdelim(&cp);
3248+ if (!arg || *arg == '\0')
3249+ fatal("%.200s line %d: Missing argument.",
3250+ filename, linenum);
3251+ if (!kex_gss_names_valid(arg))
3252+ fatal("%.200s line %d: Bad GSSAPI KexAlgorithms '%s'.",
3253+ filename, linenum, arg ? arg : "<NONE>");
3254+ if (*activep && options->gss_kex_algorithms == NULL)
3255+ options->gss_kex_algorithms = xstrdup(arg);
3256+ break;
3257+
3258 case sPasswordAuthentication:
3259 intptr = &options->password_authentication;
3260 goto parse_flag;
3261@@ -2808,6 +2851,10 @@ dump_config(ServerOptions *o)
3262 #ifdef GSSAPI
3263 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
3264 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
3265+ dump_cfg_fmtint(sGssKeyEx, o->gss_keyex);
3266+ dump_cfg_fmtint(sGssStrictAcceptor, o->gss_strict_acceptor);
3267+ dump_cfg_fmtint(sGssStoreRekey, o->gss_store_rekey);
3268+ dump_cfg_string(sGssKexAlgorithms, o->gss_kex_algorithms);
3269 #endif
3270 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
3271 dump_cfg_fmtint(sKbdInteractiveAuthentication,
3272diff --git a/servconf.h b/servconf.h
3273index 1df8f3db8..f10908e5b 100644
3274--- a/servconf.h
3275+++ b/servconf.h
3276@@ -138,8 +138,11 @@ typedef struct {
3277 int kerberos_get_afs_token; /* If true, try to get AFS token if
3278 * authenticated with Kerberos. */
3279 int gss_authentication; /* If true, permit GSSAPI authentication */
3280+ int gss_keyex; /* If true, permit GSSAPI key exchange */
3281 int gss_cleanup_creds; /* If true, destroy cred cache on logout */
3282 int gss_strict_acceptor; /* If true, restrict the GSSAPI acceptor name */
3283+ int gss_store_rekey;
3284+ char *gss_kex_algorithms; /* GSSAPI kex methods to be offered by client. */
3285 int password_authentication; /* If true, permit password
3286 * authentication. */
3287 int kbd_interactive_authentication; /* If true, permit */
3288diff --git a/session.c b/session.c
3289index 27ca8a104..857f17b3c 100644
3290--- a/session.c
3291+++ b/session.c
3292@@ -2685,13 +2685,19 @@ do_cleanup(struct ssh *ssh, Authctxt *authctxt)
3293
3294 #ifdef KRB5
3295 if (options.kerberos_ticket_cleanup &&
3296- authctxt->krb5_ctx)
3297+ authctxt->krb5_ctx) {
3298+ temporarily_use_uid(authctxt->pw);
3299 krb5_cleanup_proc(authctxt);
3300+ restore_uid();
3301+ }
3302 #endif
3303
3304 #ifdef GSSAPI
3305- if (options.gss_cleanup_creds)
3306+ if (options.gss_cleanup_creds) {
3307+ temporarily_use_uid(authctxt->pw);
3308 ssh_gssapi_cleanup_creds();
3309+ restore_uid();
3310+ }
3311 #endif
3312
3313 /* remove agent socket */
3314diff --git a/ssh-gss.h b/ssh-gss.h
3315index 36180d07a..50d80bbca 100644
3316--- a/ssh-gss.h
3317+++ b/ssh-gss.h
3318@@ -1,6 +1,6 @@
3319 /* $OpenBSD: ssh-gss.h,v 1.14 2018/07/10 09:13:30 djm Exp $ */
3320 /*
3321- * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
3322+ * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
3323 *
3324 * Redistribution and use in source and binary forms, with or without
3325 * modification, are permitted provided that the following conditions
3326@@ -61,10 +61,34 @@
3327
3328 #define SSH_GSS_OIDTYPE 0x06
3329
3330+#define SSH2_MSG_KEXGSS_INIT 30
3331+#define SSH2_MSG_KEXGSS_CONTINUE 31
3332+#define SSH2_MSG_KEXGSS_COMPLETE 32
3333+#define SSH2_MSG_KEXGSS_HOSTKEY 33
3334+#define SSH2_MSG_KEXGSS_ERROR 34
3335+#define SSH2_MSG_KEXGSS_GROUPREQ 40
3336+#define SSH2_MSG_KEXGSS_GROUP 41
3337+#define KEX_GSS_GRP1_SHA1_ID "gss-group1-sha1-"
3338+#define KEX_GSS_GRP14_SHA1_ID "gss-group14-sha1-"
3339+#define KEX_GSS_GRP14_SHA256_ID "gss-group14-sha256-"
3340+#define KEX_GSS_GRP16_SHA512_ID "gss-group16-sha512-"
3341+#define KEX_GSS_GEX_SHA1_ID "gss-gex-sha1-"
3342+#define KEX_GSS_NISTP256_SHA256_ID "gss-nistp256-sha256-"
3343+#define KEX_GSS_C25519_SHA256_ID "gss-curve25519-sha256-"
3344+
3345+#define GSS_KEX_DEFAULT_KEX \
3346+ KEX_GSS_GRP14_SHA256_ID "," \
3347+ KEX_GSS_GRP16_SHA512_ID "," \
3348+ KEX_GSS_NISTP256_SHA256_ID "," \
3349+ KEX_GSS_C25519_SHA256_ID "," \
3350+ KEX_GSS_GRP14_SHA1_ID "," \
3351+ KEX_GSS_GEX_SHA1_ID
3352+
3353 typedef struct {
3354 char *filename;
3355 char *envvar;
3356 char *envval;
3357+ struct passwd *owner;
3358 void *data;
3359 } ssh_gssapi_ccache;
3360
3361@@ -72,8 +96,11 @@ typedef struct {
3362 gss_buffer_desc displayname;
3363 gss_buffer_desc exportedname;
3364 gss_cred_id_t creds;
3365+ gss_name_t name;
3366 struct ssh_gssapi_mech_struct *mech;
3367 ssh_gssapi_ccache store;
3368+ int used;
3369+ int updated;
3370 } ssh_gssapi_client;
3371
3372 typedef struct ssh_gssapi_mech_struct {
3373@@ -84,6 +111,7 @@ typedef struct ssh_gssapi_mech_struct {
3374 int (*userok) (ssh_gssapi_client *, char *);
3375 int (*localname) (ssh_gssapi_client *, char **);
3376 void (*storecreds) (ssh_gssapi_client *);
3377+ int (*updatecreds) (ssh_gssapi_ccache *, ssh_gssapi_client *);
3378 } ssh_gssapi_mech;
3379
3380 typedef struct {
3381@@ -94,10 +122,11 @@ typedef struct {
3382 gss_OID oid; /* client */
3383 gss_cred_id_t creds; /* server */
3384 gss_name_t client; /* server */
3385- gss_cred_id_t client_creds; /* server */
3386+ gss_cred_id_t client_creds; /* both */
3387 } Gssctxt;
3388
3389 extern ssh_gssapi_mech *supported_mechs[];
3390+extern Gssctxt *gss_kex_context;
3391
3392 int ssh_gssapi_check_oid(Gssctxt *, void *, size_t);
3393 void ssh_gssapi_set_oid_data(Gssctxt *, void *, size_t);
3394@@ -109,6 +138,7 @@ OM_uint32 ssh_gssapi_test_oid_supported(OM_uint32 *, gss_OID, int *);
3395
3396 struct sshbuf;
3397 int ssh_gssapi_get_buffer_desc(struct sshbuf *, gss_buffer_desc *);
3398+int ssh_gssapi_sshpkt_get_buffer_desc(struct ssh *, gss_buffer_desc *);
3399
3400 OM_uint32 ssh_gssapi_import_name(Gssctxt *, const char *);
3401 OM_uint32 ssh_gssapi_init_ctx(Gssctxt *, int,
3402@@ -123,17 +153,33 @@ void ssh_gssapi_delete_ctx(Gssctxt **);
3403 OM_uint32 ssh_gssapi_sign(Gssctxt *, gss_buffer_t, gss_buffer_t);
3404 void ssh_gssapi_buildmic(struct sshbuf *, const char *,
3405 const char *, const char *);
3406-int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *);
3407+int ssh_gssapi_check_mechanism(Gssctxt **, gss_OID, const char *, const char *);
3408+OM_uint32 ssh_gssapi_client_identity(Gssctxt *, const char *);
3409+int ssh_gssapi_credentials_updated(Gssctxt *);
3410
3411 /* In the server */
3412+typedef int ssh_gssapi_check_fn(Gssctxt **, gss_OID, const char *,
3413+ const char *);
3414+char *ssh_gssapi_client_mechanisms(const char *, const char *, const char *);
3415+char *ssh_gssapi_kex_mechs(gss_OID_set, ssh_gssapi_check_fn *, const char *,
3416+ const char *, const char *);
3417+gss_OID ssh_gssapi_id_kex(Gssctxt *, char *, int);
3418+int ssh_gssapi_server_check_mech(Gssctxt **,gss_OID, const char *,
3419+ const char *);
3420 OM_uint32 ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
3421-int ssh_gssapi_userok(char *name);
3422+int ssh_gssapi_userok(char *name, struct passwd *, int kex);
3423 OM_uint32 ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
3424 void ssh_gssapi_do_child(char ***, u_int *);
3425 void ssh_gssapi_cleanup_creds(void);
3426 void ssh_gssapi_storecreds(void);
3427 const char *ssh_gssapi_displayname(void);
3428
3429+char *ssh_gssapi_server_mechanisms(void);
3430+int ssh_gssapi_oid_table_ok(void);
3431+
3432+int ssh_gssapi_update_creds(ssh_gssapi_ccache *store);
3433+void ssh_gssapi_rekey_creds(void);
3434+
3435 #endif /* GSSAPI */
3436
3437 #endif /* _SSH_GSS_H */
3438diff --git a/ssh.1 b/ssh.1
3439index 555317887..be8e964f0 100644
3440--- a/ssh.1
3441+++ b/ssh.1
3442@@ -506,7 +506,13 @@ For full details of the options listed below, and their possible values, see
3443 .It GatewayPorts
3444 .It GlobalKnownHostsFile
3445 .It GSSAPIAuthentication
3446+.It GSSAPIKeyExchange
3447+.It GSSAPIClientIdentity
3448 .It GSSAPIDelegateCredentials
3449+.It GSSAPIKexAlgorithms
3450+.It GSSAPIRenewalForcesRekey
3451+.It GSSAPIServerIdentity
3452+.It GSSAPITrustDns
3453 .It HashKnownHosts
3454 .It Host
3455 .It HostbasedAuthentication
3456@@ -582,6 +588,8 @@ flag),
3457 (supported message integrity codes),
3458 .Ar kex
3459 (key exchange algorithms),
3460+.Ar kex-gss
3461+(GSSAPI key exchange algorithms),
3462 .Ar key
3463 (key types),
3464 .Ar key-cert
3465diff --git a/ssh.c b/ssh.c
3466index f34ca0d71..bb98a7e2d 100644
3467--- a/ssh.c
3468+++ b/ssh.c
3469@@ -801,6 +801,8 @@ main(int ac, char **av)
3470 else if (strcmp(optarg, "kex") == 0 ||
3471 strcasecmp(optarg, "KexAlgorithms") == 0)
3472 cp = kex_alg_list('\n');
3473+ else if (strcmp(optarg, "kex-gss") == 0)
3474+ cp = kex_gss_alg_list('\n');
3475 else if (strcmp(optarg, "key") == 0)
3476 cp = sshkey_alg_list(0, 0, 0, '\n');
3477 else if (strcmp(optarg, "key-cert") == 0)
3478@@ -826,8 +828,8 @@ main(int ac, char **av)
3479 } else if (strcmp(optarg, "help") == 0) {
3480 cp = xstrdup(
3481 "cipher\ncipher-auth\ncompression\nkex\n"
3482- "key\nkey-cert\nkey-plain\nkey-sig\nmac\n"
3483- "protocol-version\nsig");
3484+ "kex-gss\nkey\nkey-cert\nkey-plain\n"
3485+ "key-sig\nmac\nprotocol-version\nsig");
3486 }
3487 if (cp == NULL)
3488 fatal("Unsupported query \"%s\"", optarg);
3489diff --git a/ssh_config b/ssh_config
3490index 842ea866c..52aae8692 100644
3491--- a/ssh_config
3492+++ b/ssh_config
3493@@ -24,6 +24,8 @@
3494 # HostbasedAuthentication no
3495 # GSSAPIAuthentication no
3496 # GSSAPIDelegateCredentials no
3497+# GSSAPIKeyExchange no
3498+# GSSAPITrustDNS no
3499 # BatchMode no
3500 # CheckHostIP yes
3501 # AddressFamily any
3502diff --git a/ssh_config.5 b/ssh_config.5
3503index 6be1f1aa2..bd86d000c 100644
3504--- a/ssh_config.5
3505+++ b/ssh_config.5
3506@@ -779,10 +779,67 @@ The default is
3507 Specifies whether user authentication based on GSSAPI is allowed.
3508 The default is
3509 .Cm no .
3510+.It Cm GSSAPIClientIdentity
3511+If set, specifies the GSSAPI client identity that ssh should use when
3512+connecting to the server. The default is unset, which means that the default
3513+identity will be used.
3514 .It Cm GSSAPIDelegateCredentials
3515 Forward (delegate) credentials to the server.
3516 The default is
3517 .Cm no .
3518+.It Cm GSSAPIKeyExchange
3519+Specifies whether key exchange based on GSSAPI may be used. When using
3520+GSSAPI key exchange the server need not have a host key.
3521+The default is
3522+.Dq no .
3523+.It Cm GSSAPIRenewalForcesRekey
3524+If set to
3525+.Dq yes
3526+then renewal of the client's GSSAPI credentials will force the rekeying of the
3527+ssh connection. With a compatible server, this will delegate the renewed
3528+credentials to a session on the server.
3529+.Pp
3530+Checks are made to ensure that credentials are only propagated when the new
3531+credentials match the old ones on the originating client and where the
3532+receiving server still has the old set in its cache.
3533+.Pp
3534+The default is
3535+.Dq no .
3536+.Pp
3537+For this to work
3538+.Cm GSSAPIKeyExchange
3539+needs to be enabled in the server and also used by the client.
3540+.It Cm GSSAPIServerIdentity
3541+If set, specifies the GSSAPI server identity that ssh should expect when
3542+connecting to the server. The default is unset, which means that the
3543+expected GSSAPI server identity will be determined from the target
3544+hostname.
3545+.It Cm GSSAPITrustDns
3546+Set to
3547+.Dq yes
3548+to indicate that the DNS is trusted to securely canonicalize
3549+the name of the host being connected to. If
3550+.Dq no ,
3551+the hostname entered on the
3552+command line will be passed untouched to the GSSAPI library.
3553+The default is
3554+.Dq no .
3555+.It Cm GSSAPIKexAlgorithms
3556+The list of key exchange algorithms that are offered for GSSAPI
3557+key exchange. Possible values are
3558+.Bd -literal -offset 3n
3559+gss-gex-sha1-,
3560+gss-group1-sha1-,
3561+gss-group14-sha1-,
3562+gss-group14-sha256-,
3563+gss-group16-sha512-,
3564+gss-nistp256-sha256-,
3565+gss-curve25519-sha256-
3566+.Ed
3567+.Pp
3568+The default is
3569+.Dq gss-group14-sha256-,gss-group16-sha512-,gss-nistp256-sha256-,gss-curve25519-sha256-,gss-gex-sha1-,gss-group14-sha1- .
3570+This option only applies to connections using GSSAPI.
3571 .It Cm HashKnownHosts
3572 Indicates that
3573 .Xr ssh 1
3574diff --git a/sshconnect2.c b/sshconnect2.c
3575index f64aae66a..c47fc31a6 100644
3576--- a/sshconnect2.c
3577+++ b/sshconnect2.c
3578@@ -80,8 +80,6 @@
3579 #endif
3580
3581 /* import */
3582-extern char *client_version_string;
3583-extern char *server_version_string;
3584 extern Options options;
3585
3586 /*
3587@@ -210,6 +208,11 @@ ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
3588 char *s, *all_key;
3589 int r, use_known_hosts_order = 0;
3590
3591+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3592+ char *orig = NULL, *gss = NULL;
3593+ char *gss_host = NULL;
3594+#endif
3595+
3596 xxx_host = host;
3597 xxx_hostaddr = hostaddr;
3598
3599@@ -253,6 +256,41 @@ ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
3600 compat_pkalg_proposal(options.hostkeyalgorithms);
3601 }
3602
3603+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3604+ if (options.gss_keyex) {
3605+ /* Add the GSSAPI mechanisms currently supported on this
3606+ * client to the key exchange algorithm proposal */
3607+ orig = myproposal[PROPOSAL_KEX_ALGS];
3608+
3609+ if (options.gss_server_identity) {
3610+ gss_host = xstrdup(options.gss_server_identity);
3611+ } else if (options.gss_trust_dns) {
3612+ gss_host = remote_hostname(ssh);
3613+ /* Fall back to specified host if we are using proxy command
3614+ * and can not use DNS on that socket */
3615+ if (strcmp(gss_host, "UNKNOWN") == 0) {
3616+ gss_host = xstrdup(host);
3617+ }
3618+ } else {
3619+ gss_host = xstrdup(host);
3620+ }
3621+
3622+ gss = ssh_gssapi_client_mechanisms(gss_host,
3623+ options.gss_client_identity, options.gss_kex_algorithms);
3624+ if (gss) {
3625+ debug("Offering GSSAPI proposal: %s", gss);
3626+ xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
3627+ "%s,%s", gss, orig);
3628+
3629+ /* If we've got GSSAPI algorithms, then we also support the
3630+ * 'null' hostkey, as a last resort */
3631+ orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
3632+ xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
3633+ "%s,null", orig);
3634+ }
3635+ }
3636+#endif
3637+
3638 if (options.rekey_limit || options.rekey_interval)
3639 ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
3640 options.rekey_interval);
3641@@ -271,16 +309,46 @@ ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
3642 # ifdef OPENSSL_HAS_ECC
3643 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
3644 # endif
3645-#endif
3646+# ifdef GSSAPI
3647+ if (options.gss_keyex) {
3648+ ssh->kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
3649+ ssh->kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
3650+ ssh->kex->kex[KEX_GSS_GRP14_SHA256] = kexgss_client;
3651+ ssh->kex->kex[KEX_GSS_GRP16_SHA512] = kexgss_client;
3652+ ssh->kex->kex[KEX_GSS_GEX_SHA1] = kexgssgex_client;
3653+ ssh->kex->kex[KEX_GSS_NISTP256_SHA256] = kexgss_client;
3654+ ssh->kex->kex[KEX_GSS_C25519_SHA256] = kexgss_client;
3655+ }
3656+# endif
3657+#endif /* WITH_OPENSSL */
3658 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
3659 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
3660 ssh->kex->verify_host_key=&verify_host_key_callback;
3661
3662+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3663+ if (options.gss_keyex) {
3664+ ssh->kex->gss_deleg_creds = options.gss_deleg_creds;
3665+ ssh->kex->gss_trust_dns = options.gss_trust_dns;
3666+ ssh->kex->gss_client = options.gss_client_identity;
3667+ ssh->kex->gss_host = gss_host;
3668+ }
3669+#endif
3670+
3671 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
3672
3673 /* remove ext-info from the KEX proposals for rekeying */
3674 myproposal[PROPOSAL_KEX_ALGS] =
3675 compat_kex_proposal(options.kex_algorithms);
3676+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3677+ /* repair myproposal after it was crumpled by the */
3678+ /* ext-info removal above */
3679+ if (gss) {
3680+ orig = myproposal[PROPOSAL_KEX_ALGS];
3681+ xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
3682+ "%s,%s", gss, orig);
3683+ free(gss);
3684+ }
3685+#endif
3686 if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
3687 fatal("kex_prop2buf: %s", ssh_err(r));
3688
3689@@ -377,6 +445,7 @@ static int input_gssapi_response(int type, u_int32_t, struct ssh *);
3690 static int input_gssapi_token(int type, u_int32_t, struct ssh *);
3691 static int input_gssapi_error(int, u_int32_t, struct ssh *);
3692 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
3693+static int userauth_gsskeyex(struct ssh *);
3694 #endif
3695
3696 void userauth(struct ssh *, char *);
3697@@ -393,6 +462,11 @@ static char *authmethods_get(void);
3698
3699 Authmethod authmethods[] = {
3700 #ifdef GSSAPI
3701+ {"gssapi-keyex",
3702+ userauth_gsskeyex,
3703+ NULL,
3704+ &options.gss_keyex,
3705+ NULL},
3706 {"gssapi-with-mic",
3707 userauth_gssapi,
3708 userauth_gssapi_cleanup,
3709@@ -763,12 +837,31 @@ userauth_gssapi(struct ssh *ssh)
3710 OM_uint32 min;
3711 int r, ok = 0;
3712 gss_OID mech = NULL;
3713+ char *gss_host;
3714+
3715+ if (options.gss_server_identity) {
3716+ gss_host = xstrdup(options.gss_server_identity);
3717+ } else if (options.gss_trust_dns) {
3718+ gss_host = remote_hostname(ssh);
3719+ /* Fall back to specified host if we are using proxy command
3720+ * and can not use DNS on that socket */
3721+ if (strcmp(gss_host, "UNKNOWN") == 0) {
3722+ gss_host = authctxt->host;
3723+ }
3724+ } else {
3725+ gss_host = xstrdup(authctxt->host);
3726+ }
3727
3728 /* Try one GSSAPI method at a time, rather than sending them all at
3729 * once. */
3730
3731 if (authctxt->gss_supported_mechs == NULL)
3732- gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
3733+ if (GSS_ERROR(gss_indicate_mechs(&min,
3734+ &authctxt->gss_supported_mechs))) {
3735+ authctxt->gss_supported_mechs = NULL;
3736+ free(gss_host);
3737+ return 0;
3738+ }
3739
3740 /* Check to see whether the mechanism is usable before we offer it */
3741 while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
3742@@ -777,13 +870,15 @@ userauth_gssapi(struct ssh *ssh)
3743 elements[authctxt->mech_tried];
3744 /* My DER encoding requires length<128 */
3745 if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
3746- mech, authctxt->host)) {
3747+ mech, gss_host, options.gss_client_identity)) {
3748 ok = 1; /* Mechanism works */
3749 } else {
3750 authctxt->mech_tried++;
3751 }
3752 }
3753
3754+ free(gss_host);
3755+
3756 if (!ok || mech == NULL)
3757 return 0;
3758
3759@@ -1023,6 +1118,55 @@ input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
3760 free(lang);
3761 return r;
3762 }
3763+
3764+int
3765+userauth_gsskeyex(struct ssh *ssh)
3766+{
3767+ struct sshbuf *b = NULL;
3768+ Authctxt *authctxt = ssh->authctxt;
3769+ gss_buffer_desc gssbuf;
3770+ gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
3771+ OM_uint32 ms;
3772+ int r;
3773+
3774+ static int attempt = 0;
3775+ if (attempt++ >= 1)
3776+ return (0);
3777+
3778+ if (gss_kex_context == NULL) {
3779+ debug("No valid Key exchange context");
3780+ return (0);
3781+ }
3782+
3783+ if ((b = sshbuf_new()) == NULL)
3784+ fatal("%s: sshbuf_new failed", __func__);
3785+
3786+ ssh_gssapi_buildmic(b, authctxt->server_user, authctxt->service,
3787+ "gssapi-keyex");
3788+
3789+ if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
3790+ fatal("%s: sshbuf_mutable_ptr failed", __func__);
3791+ gssbuf.length = sshbuf_len(b);
3792+
3793+ if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
3794+ sshbuf_free(b);
3795+ return (0);
3796+ }
3797+
3798+ if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
3799+ (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
3800+ (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
3801+ (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
3802+ (r = sshpkt_put_string(ssh, mic.value, mic.length)) != 0 ||
3803+ (r = sshpkt_send(ssh)) != 0)
3804+ fatal("%s: %s", __func__, ssh_err(r));
3805+
3806+ sshbuf_free(b);
3807+ gss_release_buffer(&ms, &mic);
3808+
3809+ return (1);
3810+}
3811+
3812 #endif /* GSSAPI */
3813
3814 static int
3815diff --git a/sshd.c b/sshd.c
3816index 8aa7f3df6..8c5d5822e 100644
3817--- a/sshd.c
3818+++ b/sshd.c
3819@@ -816,8 +816,8 @@ notify_hostkeys(struct ssh *ssh)
3820 }
3821 debug3("%s: sent %u hostkeys", __func__, nkeys);
3822 if (nkeys == 0)
3823- fatal("%s: no hostkeys", __func__);
3824- if ((r = sshpkt_send(ssh)) != 0)
3825+ debug3("%s: no hostkeys", __func__);
3826+ else if ((r = sshpkt_send(ssh)) != 0)
3827 sshpkt_fatal(ssh, r, "%s: send", __func__);
3828 sshbuf_free(buf);
3829 }
3830@@ -1901,7 +1901,8 @@ main(int ac, char **av)
3831 free(fp);
3832 }
3833 accumulate_host_timing_secret(cfg, NULL);
3834- if (!sensitive_data.have_ssh2_key) {
3835+ /* The GSSAPI key exchange can run without a host key */
3836+ if (!sensitive_data.have_ssh2_key && !options.gss_keyex) {
3837 logit("sshd: no hostkeys available -- exiting.");
3838 exit(1);
3839 }
3840@@ -2393,6 +2394,48 @@ do_ssh2_kex(struct ssh *ssh)
3841 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
3842 list_hostkey_types());
3843
3844+#if defined(GSSAPI) && defined(WITH_OPENSSL)
3845+ {
3846+ char *orig;
3847+ char *gss = NULL;
3848+ char *newstr = NULL;
3849+ orig = myproposal[PROPOSAL_KEX_ALGS];
3850+
3851+ /*
3852+ * If we don't have a host key, then there's no point advertising
3853+ * the other key exchange algorithms
3854+ */
3855+
3856+ if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
3857+ orig = NULL;
3858+
3859+ if (options.gss_keyex)
3860+ gss = ssh_gssapi_server_mechanisms();
3861+ else
3862+ gss = NULL;
3863+
3864+ if (gss && orig)
3865+ xasprintf(&newstr, "%s,%s", gss, orig);
3866+ else if (gss)
3867+ newstr = gss;
3868+ else if (orig)
3869+ newstr = orig;
3870+
3871+ /*
3872+ * If we've got GSSAPI mechanisms, then we've got the 'null' host
3873+ * key alg, but we can't tell people about it unless its the only
3874+ * host key algorithm we support
3875+ */
3876+ if (gss && (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS])) == 0)
3877+ myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = "null";
3878+
3879+ if (newstr)
3880+ myproposal[PROPOSAL_KEX_ALGS] = newstr;
3881+ else
3882+ fatal("No supported key exchange algorithms");
3883+ }
3884+#endif
3885+
3886 /* start key exchange */
3887 if ((r = kex_setup(ssh, myproposal)) != 0)
3888 fatal("kex_setup: %s", ssh_err(r));
3889@@ -2408,7 +2451,18 @@ do_ssh2_kex(struct ssh *ssh)
3890 # ifdef OPENSSL_HAS_ECC
3891 kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
3892 # endif
3893-#endif
3894+# ifdef GSSAPI
3895+ if (options.gss_keyex) {
3896+ kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
3897+ kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
3898+ kex->kex[KEX_GSS_GRP14_SHA256] = kexgss_server;
3899+ kex->kex[KEX_GSS_GRP16_SHA512] = kexgss_server;
3900+ kex->kex[KEX_GSS_GEX_SHA1] = kexgssgex_server;
3901+ kex->kex[KEX_GSS_NISTP256_SHA256] = kexgss_server;
3902+ kex->kex[KEX_GSS_C25519_SHA256] = kexgss_server;
3903+ }
3904+# endif
3905+#endif /* WITH_OPENSSL */
3906 kex->kex[KEX_C25519_SHA256] = kex_gen_server;
3907 kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
3908 kex->load_host_public_key=&get_hostkey_public_by_type;
3909diff --git a/sshd_config b/sshd_config
3910index 19b7c91a1..2c48105f8 100644
3911--- a/sshd_config
3912+++ b/sshd_config
3913@@ -69,6 +69,8 @@ AuthorizedKeysFile .ssh/authorized_keys
3914 # GSSAPI options
3915 #GSSAPIAuthentication no
3916 #GSSAPICleanupCredentials yes
3917+#GSSAPIStrictAcceptorCheck yes
3918+#GSSAPIKeyExchange no
3919
3920 # Set this to 'yes' to enable PAM authentication, account processing,
3921 # and session processing. If this is enabled, PAM authentication will
3922diff --git a/sshd_config.5 b/sshd_config.5
3923index 6fa421cae..eabbe9e73 100644
3924--- a/sshd_config.5
3925+++ b/sshd_config.5
3926@@ -644,6 +644,11 @@ Specifies whether to automatically destroy the user's credentials cache
3927 on logout.
3928 The default is
3929 .Cm yes .
3930+.It Cm GSSAPIKeyExchange
3931+Specifies whether key exchange based on GSSAPI is allowed. GSSAPI key exchange
3932+doesn't rely on ssh keys to verify host identity.
3933+The default is
3934+.Cm no .
3935 .It Cm GSSAPIStrictAcceptorCheck
3936 Determines whether to be strict about the identity of the GSSAPI acceptor
3937 a client authenticates against.
3938@@ -658,6 +663,31 @@ machine's default store.
3939 This facility is provided to assist with operation on multi homed machines.
3940 The default is
3941 .Cm yes .
3942+.It Cm GSSAPIStoreCredentialsOnRekey
3943+Controls whether the user's GSSAPI credentials should be updated following a
3944+successful connection rekeying. This option can be used to accepted renewed
3945+or updated credentials from a compatible client. The default is
3946+.Dq no .
3947+.Pp
3948+For this to work
3949+.Cm GSSAPIKeyExchange
3950+needs to be enabled in the server and also used by the client.
3951+.It Cm GSSAPIKexAlgorithms
3952+The list of key exchange algorithms that are accepted by GSSAPI
3953+key exchange. Possible values are
3954+.Bd -literal -offset 3n
3955+gss-gex-sha1-,
3956+gss-group1-sha1-,
3957+gss-group14-sha1-,
3958+gss-group14-sha256-,
3959+gss-group16-sha512-,
3960+gss-nistp256-sha256-,
3961+gss-curve25519-sha256-
3962+.Ed
3963+.Pp
3964+The default is
3965+.Dq gss-group14-sha256-,gss-group16-sha512-,gss-nistp256-sha256-,gss-curve25519-sha256-,gss-gex-sha1-,gss-group14-sha1- .
3966+This option only applies to connections using GSSAPI.
3967 .It Cm HostbasedAcceptedKeyTypes
3968 Specifies the key types that will be accepted for hostbased authentication
3969 as a list of comma-separated patterns.
3970diff --git a/sshkey.c b/sshkey.c
3971index ac451f1a8..b88282e19 100644
3972--- a/sshkey.c
3973+++ b/sshkey.c
3974@@ -156,6 +156,7 @@ static const struct keytype keytypes[] = {
3975 KEY_ECDSA_SK_CERT, NID_X9_62_prime256v1, 1, 0 },
3976 # endif /* OPENSSL_HAS_ECC */
3977 #endif /* WITH_OPENSSL */
3978+ { "null", "null", NULL, KEY_NULL, 0, 0, 0 },
3979 { NULL, NULL, NULL, -1, -1, 0, 0 }
3980 };
3981
3982@@ -257,7 +258,7 @@ sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep)
3983 const struct keytype *kt;
3984
3985 for (kt = keytypes; kt->type != -1; kt++) {
3986- if (kt->name == NULL)
3987+ if (kt->name == NULL || kt->type == KEY_NULL)
3988 continue;
3989 if (!include_sigonly && kt->sigonly)
3990 continue;
3991diff --git a/sshkey.h b/sshkey.h
3992index 2d8b62497..dc1c10597 100644
3993--- a/sshkey.h
3994+++ b/sshkey.h
3995@@ -69,6 +69,7 @@ enum sshkey_types {
3996 KEY_ECDSA_SK_CERT,
3997 KEY_ED25519_SK,
3998 KEY_ED25519_SK_CERT,
3999+ KEY_NULL,
4000 KEY_UNSPEC
4001 };
4002
diff --git a/debian/patches/keepalive-extensions.patch b/debian/patches/keepalive-extensions.patch
new file mode 100644
index 000000000..c9bc83267
--- /dev/null
+++ b/debian/patches/keepalive-extensions.patch
@@ -0,0 +1,135 @@
1From 164d1c9f11309d38273ac64e30eda2baa3733f78 Mon Sep 17 00:00:00 2001
2From: Richard Kettlewell <rjk@greenend.org.uk>
3Date: Sun, 9 Feb 2014 16:09:52 +0000
4Subject: Various keepalive extensions
5
6Add compatibility aliases for ProtocolKeepAlives and SetupTimeOut, supported
7in previous versions of Debian's OpenSSH package but since superseded by
8ServerAliveInterval. (We're probably stuck with this bit for
9compatibility.)
10
11In batch mode, default ServerAliveInterval to five minutes.
12
13Adjust documentation to match and to give some more advice on use of
14keepalives.
15
16Author: Ian Jackson <ian@chiark.greenend.org.uk>
17Author: Matthew Vernon <matthew@debian.org>
18Author: Colin Watson <cjwatson@debian.org>
19Last-Update: 2020-02-21
20
21Patch-Name: keepalive-extensions.patch
22---
23 readconf.c | 14 ++++++++++++--
24 ssh_config.5 | 21 +++++++++++++++++++--
25 sshd_config.5 | 3 +++
26 3 files changed, 34 insertions(+), 4 deletions(-)
27
28diff --git a/readconf.c b/readconf.c
29index b069333fa..3d0a812b3 100644
30--- a/readconf.c
31+++ b/readconf.c
32@@ -176,6 +176,7 @@ typedef enum {
33 oFingerprintHash, oUpdateHostkeys, oHostbasedKeyTypes,
34 oPubkeyAcceptedKeyTypes, oCASignatureAlgorithms, oProxyJump,
35 oSecurityKeyProvider,
36+ oProtocolKeepAlives, oSetupTimeOut,
37 oIgnore, oIgnoredUnknownOption, oDeprecated, oUnsupported
38 } OpCodes;
39
40@@ -326,6 +327,8 @@ static struct {
41 { "ignoreunknown", oIgnoreUnknown },
42 { "proxyjump", oProxyJump },
43 { "securitykeyprovider", oSecurityKeyProvider },
44+ { "protocolkeepalives", oProtocolKeepAlives },
45+ { "setuptimeout", oSetupTimeOut },
46
47 { NULL, oBadOption }
48 };
49@@ -1534,6 +1537,8 @@ parse_keytypes:
50 goto parse_flag;
51
52 case oServerAliveInterval:
53+ case oProtocolKeepAlives: /* Debian-specific compatibility alias */
54+ case oSetupTimeOut: /* Debian-specific compatibility alias */
55 intptr = &options->server_alive_interval;
56 goto parse_time;
57
58@@ -2266,8 +2271,13 @@ fill_default_options(Options * options)
59 options->rekey_interval = 0;
60 if (options->verify_host_key_dns == -1)
61 options->verify_host_key_dns = 0;
62- if (options->server_alive_interval == -1)
63- options->server_alive_interval = 0;
64+ if (options->server_alive_interval == -1) {
65+ /* in batch mode, default is 5mins */
66+ if (options->batch_mode == 1)
67+ options->server_alive_interval = 300;
68+ else
69+ options->server_alive_interval = 0;
70+ }
71 if (options->server_alive_count_max == -1)
72 options->server_alive_count_max = 3;
73 if (options->control_master == -1)
74diff --git a/ssh_config.5 b/ssh_config.5
75index bd86d000c..3ceb800ba 100644
76--- a/ssh_config.5
77+++ b/ssh_config.5
78@@ -275,9 +275,13 @@ If set to
79 .Cm yes ,
80 user interaction such as password prompts and host key confirmation requests
81 will be disabled.
82+In addition, the
83+.Cm ServerAliveInterval
84+option will be set to 300 seconds by default (Debian-specific).
85 This option is useful in scripts and other batch jobs where no user
86 is present to interact with
87-.Xr ssh 1 .
88+.Xr ssh 1 ,
89+and where it is desirable to detect a broken network swiftly.
90 The argument must be
91 .Cm yes
92 or
93@@ -1624,7 +1628,14 @@ from the server,
94 will send a message through the encrypted
95 channel to request a response from the server.
96 The default
97-is 0, indicating that these messages will not be sent to the server.
98+is 0, indicating that these messages will not be sent to the server,
99+or 300 if the
100+.Cm BatchMode
101+option is set (Debian-specific).
102+.Cm ProtocolKeepAlives
103+and
104+.Cm SetupTimeOut
105+are Debian-specific compatibility aliases for this option.
106 .It Cm SetEnv
107 Directly specify one or more environment variables and their contents to
108 be sent to the server.
109@@ -1704,6 +1715,12 @@ Specifies whether the system should send TCP keepalive messages to the
110 other side.
111 If they are sent, death of the connection or crash of one
112 of the machines will be properly noticed.
113+This option only uses TCP keepalives (as opposed to using ssh level
114+keepalives), so takes a long time to notice when the connection dies.
115+As such, you probably want
116+the
117+.Cm ServerAliveInterval
118+option as well.
119 However, this means that
120 connections will die if the route is down temporarily, and some people
121 find it annoying.
122diff --git a/sshd_config.5 b/sshd_config.5
123index eabbe9e73..6457620bb 100644
124--- a/sshd_config.5
125+++ b/sshd_config.5
126@@ -1691,6 +1691,9 @@ This avoids infinitely hanging sessions.
127 .Pp
128 To disable TCP keepalive messages, the value should be set to
129 .Cm no .
130+.Pp
131+This option was formerly called
132+.Cm KeepAlive .
133 .It Cm TrustedUserCAKeys
134 Specifies a file containing public keys of certificate authorities that are
135 trusted to sign user certificates for authentication, or
diff --git a/debian/patches/mention-ssh-keygen-on-keychange.patch b/debian/patches/mention-ssh-keygen-on-keychange.patch
new file mode 100644
index 000000000..cb227f839
--- /dev/null
+++ b/debian/patches/mention-ssh-keygen-on-keychange.patch
@@ -0,0 +1,44 @@
1From c8da63c601b5d44fd233548385809c9c3a2fa0b8 Mon Sep 17 00:00:00 2001
2From: Scott Moser <smoser@ubuntu.com>
3Date: Sun, 9 Feb 2014 16:10:03 +0000
4Subject: Mention ssh-keygen in ssh fingerprint changed warning
5
6Author: Chris Lamb <lamby@debian.org>
7Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1843
8Bug-Ubuntu: https://bugs.launchpad.net/bugs/686607
9Last-Update: 2017-08-22
10
11Patch-Name: mention-ssh-keygen-on-keychange.patch
12---
13 sshconnect.c | 9 ++++++++-
14 1 file changed, 8 insertions(+), 1 deletion(-)
15
16diff --git a/sshconnect.c b/sshconnect.c
17index 5f8c81b84..3ae20b74e 100644
18--- a/sshconnect.c
19+++ b/sshconnect.c
20@@ -994,9 +994,13 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
21 error("%s. This could either mean that", key_msg);
22 error("DNS SPOOFING is happening or the IP address for the host");
23 error("and its host key have changed at the same time.");
24- if (ip_status != HOST_NEW)
25+ if (ip_status != HOST_NEW) {
26 error("Offending key for IP in %s:%lu",
27 ip_found->file, ip_found->line);
28+ error(" remove with:");
29+ error(" ssh-keygen -f \"%s\" -R \"%s\"",
30+ ip_found->file, ip);
31+ }
32 }
33 /* The host key has changed. */
34 warn_changed_key(host_key);
35@@ -1005,6 +1009,9 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
36 error("Offending %s key in %s:%lu",
37 sshkey_type(host_found->key),
38 host_found->file, host_found->line);
39+ error(" remove with:");
40+ error(" ssh-keygen -f \"%s\" -R \"%s\"",
41+ host_found->file, host);
42
43 /*
44 * If strict host key checking is in use, the user will have
diff --git a/debian/patches/no-openssl-version-status.patch b/debian/patches/no-openssl-version-status.patch
new file mode 100644
index 000000000..e383375c6
--- /dev/null
+++ b/debian/patches/no-openssl-version-status.patch
@@ -0,0 +1,62 @@
1From cf3ffd6a25d425bed33dd698f92e64953d9769eb Mon Sep 17 00:00:00 2001
2From: Kurt Roeckx <kurt@roeckx.be>
3Date: Sun, 9 Feb 2014 16:10:14 +0000
4Subject: Don't check the status field of the OpenSSL version
5
6There is no reason to check the version of OpenSSL (in Debian). If it's
7not compatible the soname will change. OpenSSH seems to want to do a
8check for the soname based on the version number, but wants to keep the
9status of the release the same. Remove that check on the status since
10it doesn't tell you anything about how compatible that version is.
11
12Author: Colin Watson <cjwatson@debian.org>
13Bug-Debian: https://bugs.debian.org/93581
14Bug-Debian: https://bugs.debian.org/664383
15Bug-Debian: https://bugs.debian.org/732940
16Forwarded: not-needed
17Last-Update: 2014-10-07
18
19Patch-Name: no-openssl-version-status.patch
20---
21 openbsd-compat/openssl-compat.c | 6 +++---
22 openbsd-compat/regress/opensslvertest.c | 1 +
23 2 files changed, 4 insertions(+), 3 deletions(-)
24
25diff --git a/openbsd-compat/openssl-compat.c b/openbsd-compat/openssl-compat.c
26index a37ca61bf..c1749210d 100644
27--- a/openbsd-compat/openssl-compat.c
28+++ b/openbsd-compat/openssl-compat.c
29@@ -34,7 +34,7 @@
30 /*
31 * OpenSSL version numbers: MNNFFPPS: major minor fix patch status
32 * We match major, minor, fix and status (not patch) for <1.0.0.
33- * After that, we acceptable compatible fix versions (so we
34+ * After that, we accept compatible fix and status versions (so we
35 * allow 1.0.1 to work with 1.0.0). Going backwards is only allowed
36 * within a patch series.
37 */
38@@ -55,10 +55,10 @@ ssh_compatible_openssl(long headerver, long libver)
39 }
40
41 /*
42- * For versions >= 1.0.0, major,minor,status must match and library
43+ * For versions >= 1.0.0, major,minor must match and library
44 * fix version must be equal to or newer than the header.
45 */
46- mask = 0xfff0000fL; /* major,minor,status */
47+ mask = 0xfff00000L; /* major,minor */
48 hfix = (headerver & 0x000ff000) >> 12;
49 lfix = (libver & 0x000ff000) >> 12;
50 if ( (headerver & mask) == (libver & mask) && lfix >= hfix)
51diff --git a/openbsd-compat/regress/opensslvertest.c b/openbsd-compat/regress/opensslvertest.c
52index 5d019b598..58474873d 100644
53--- a/openbsd-compat/regress/opensslvertest.c
54+++ b/openbsd-compat/regress/opensslvertest.c
55@@ -35,6 +35,7 @@ struct version_test {
56
57 /* built with 1.0.1b release headers */
58 { 0x1000101fL, 0x1000101fL, 1},/* exact match */
59+ { 0x1000101fL, 0x10001010L, 1}, /* different status: ok */
60 { 0x1000101fL, 0x1000102fL, 1}, /* newer library patch version: ok */
61 { 0x1000101fL, 0x1000100fL, 1}, /* older library patch version: ok */
62 { 0x1000101fL, 0x1000201fL, 1}, /* newer library fix version: ok */
diff --git a/debian/patches/openbsd-docs.patch b/debian/patches/openbsd-docs.patch
new file mode 100644
index 000000000..64405d578
--- /dev/null
+++ b/debian/patches/openbsd-docs.patch
@@ -0,0 +1,148 @@
1From 6bcbfca92b58917dba48b696dd63529fa5dcbb82 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Sun, 9 Feb 2014 16:10:09 +0000
4Subject: Adjust various OpenBSD-specific references in manual pages
5
6No single bug reference for this patch, but history includes:
7 http://bugs.debian.org/154434 (login.conf(5))
8 http://bugs.debian.org/513417 (/etc/rc)
9 http://bugs.debian.org/530692 (ssl(8))
10 https://bugs.launchpad.net/bugs/456660 (ssl(8))
11
12Forwarded: not-needed
13Last-Update: 2017-10-04
14
15Patch-Name: openbsd-docs.patch
16---
17 moduli.5 | 4 ++--
18 ssh-keygen.1 | 12 ++++--------
19 ssh.1 | 4 ++++
20 sshd.8 | 5 ++---
21 sshd_config.5 | 3 +--
22 5 files changed, 13 insertions(+), 15 deletions(-)
23
24diff --git a/moduli.5 b/moduli.5
25index ef0de0850..149846c8c 100644
26--- a/moduli.5
27+++ b/moduli.5
28@@ -21,7 +21,7 @@
29 .Nd Diffie-Hellman moduli
30 .Sh DESCRIPTION
31 The
32-.Pa /etc/moduli
33+.Pa /etc/ssh/moduli
34 file contains prime numbers and generators for use by
35 .Xr sshd 8
36 in the Diffie-Hellman Group Exchange key exchange method.
37@@ -110,7 +110,7 @@ first estimates the size of the modulus required to produce enough
38 Diffie-Hellman output to sufficiently key the selected symmetric cipher.
39 .Xr sshd 8
40 then randomly selects a modulus from
41-.Fa /etc/moduli
42+.Fa /etc/ssh/moduli
43 that best meets the size requirement.
44 .Sh SEE ALSO
45 .Xr ssh-keygen 1 ,
46diff --git a/ssh-keygen.1 b/ssh-keygen.1
47index 3ae596caa..836174fb6 100644
48--- a/ssh-keygen.1
49+++ b/ssh-keygen.1
50@@ -202,9 +202,7 @@ key in
51 .Pa ~/.ssh/id_ed25519_sk
52 or
53 .Pa ~/.ssh/id_rsa .
54-Additionally, the system administrator may use this to generate host keys,
55-as seen in
56-.Pa /etc/rc .
57+Additionally, the system administrator may use this to generate host keys.
58 .Pp
59 Normally this program generates the key and asks for a file in which
60 to store the private key.
61@@ -269,9 +267,7 @@ If
62 .Fl f
63 has also been specified, its argument is used as a prefix to the
64 default path for the resulting host key files.
65-This is used by
66-.Pa /etc/rc
67-to generate new host keys.
68+This is used by system administration scripts to generate new host keys.
69 .It Fl a Ar rounds
70 When saving a private key, this option specifies the number of KDF
71 (key derivation function) rounds used.
72@@ -804,7 +800,7 @@ option.
73 Valid generator values are 2, 3, and 5.
74 .Pp
75 Screened DH groups may be installed in
76-.Pa /etc/moduli .
77+.Pa /etc/ssh/moduli .
78 It is important that this file contains moduli of a range of bit lengths and
79 that both ends of a connection share common moduli.
80 .Pp
81@@ -1185,7 +1181,7 @@ on all machines
82 where the user wishes to log in using public key authentication.
83 There is no need to keep the contents of this file secret.
84 .Pp
85-.It Pa /etc/moduli
86+.It Pa /etc/ssh/moduli
87 Contains Diffie-Hellman groups used for DH-GEX.
88 The file format is described in
89 .Xr moduli 5 .
90diff --git a/ssh.1 b/ssh.1
91index 5d613076c..1880c032d 100644
92--- a/ssh.1
93+++ b/ssh.1
94@@ -890,6 +890,10 @@ implements public key authentication protocol automatically,
95 using one of the DSA, ECDSA, Ed25519 or RSA algorithms.
96 The HISTORY section of
97 .Xr ssl 8
98+(on non-OpenBSD systems, see
99+.nh
100+http://www.openbsd.org/cgi\-bin/man.cgi?query=ssl&sektion=8#HISTORY)
101+.hy
102 contains a brief discussion of the DSA and RSA algorithms.
103 .Pp
104 The file
105diff --git a/sshd.8 b/sshd.8
106index 97d547ffa..7895a6a94 100644
107--- a/sshd.8
108+++ b/sshd.8
109@@ -65,7 +65,7 @@ over an insecure network.
110 .Nm
111 listens for connections from clients.
112 It is normally started at boot from
113-.Pa /etc/rc .
114+.Pa /etc/init.d/ssh .
115 It forks a new
116 daemon for each incoming connection.
117 The forked daemons handle
118@@ -911,7 +911,7 @@ This file is for host-based authentication (see
119 .Xr ssh 1 ) .
120 It should only be writable by root.
121 .Pp
122-.It Pa /etc/moduli
123+.It Pa /etc/ssh/moduli
124 Contains Diffie-Hellman groups used for the "Diffie-Hellman Group Exchange"
125 key exchange method.
126 The file format is described in
127@@ -1009,7 +1009,6 @@ The content of this file is not sensitive; it can be world-readable.
128 .Xr ssh-keyscan 1 ,
129 .Xr chroot 2 ,
130 .Xr hosts_access 5 ,
131-.Xr login.conf 5 ,
132 .Xr moduli 5 ,
133 .Xr sshd_config 5 ,
134 .Xr inetd 8 ,
135diff --git a/sshd_config.5 b/sshd_config.5
136index 33dc0c675..32ae46476 100644
137--- a/sshd_config.5
138+++ b/sshd_config.5
139@@ -385,8 +385,7 @@ Certificates signed using other algorithms will not be accepted for
140 public key or host-based authentication.
141 .It Cm ChallengeResponseAuthentication
142 Specifies whether challenge-response authentication is allowed (e.g. via
143-PAM or through authentication styles supported in
144-.Xr login.conf 5 )
145+PAM).
146 The default is
147 .Cm yes .
148 .It Cm ChrootDirectory
diff --git a/debian/patches/package-versioning.patch b/debian/patches/package-versioning.patch
new file mode 100644
index 000000000..daa1473db
--- /dev/null
+++ b/debian/patches/package-versioning.patch
@@ -0,0 +1,47 @@
1From 707144d399b9fc959a4f6be3fd8e239c208c88ff Mon Sep 17 00:00:00 2001
2From: Matthew Vernon <matthew@debian.org>
3Date: Sun, 9 Feb 2014 16:10:05 +0000
4Subject: Include the Debian version in our identification
5
6This makes it easier to audit networks for versions patched against security
7vulnerabilities. It has little detrimental effect, as attackers will
8generally just try attacks rather than bothering to scan for
9vulnerable-looking version strings. (However, see debian-banner.patch.)
10
11Forwarded: not-needed
12Last-Update: 2019-06-05
13
14Patch-Name: package-versioning.patch
15---
16 kex.c | 2 +-
17 version.h | 7 ++++++-
18 2 files changed, 7 insertions(+), 2 deletions(-)
19
20diff --git a/kex.c b/kex.c
21index 751cfc710..ce7bb5b3b 100644
22--- a/kex.c
23+++ b/kex.c
24@@ -1243,7 +1243,7 @@ kex_exchange_identification(struct ssh *ssh, int timeout_ms,
25 if (version_addendum != NULL && *version_addendum == '\0')
26 version_addendum = NULL;
27 if ((r = sshbuf_putf(our_version, "SSH-%d.%d-%.100s%s%s\r\n",
28- PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION,
29+ PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_RELEASE,
30 version_addendum == NULL ? "" : " ",
31 version_addendum == NULL ? "" : version_addendum)) != 0) {
32 oerrno = errno;
33diff --git a/version.h b/version.h
34index c2f9c55bb..480cd59e1 100644
35--- a/version.h
36+++ b/version.h
37@@ -3,4 +3,9 @@
38 #define SSH_VERSION "OpenSSH_8.4"
39
40 #define SSH_PORTABLE "p1"
41-#define SSH_RELEASE SSH_VERSION SSH_PORTABLE
42+#define SSH_RELEASE_MINIMUM SSH_VERSION SSH_PORTABLE
43+#ifdef SSH_EXTRAVERSION
44+#define SSH_RELEASE SSH_RELEASE_MINIMUM " " SSH_EXTRAVERSION
45+#else
46+#define SSH_RELEASE SSH_RELEASE_MINIMUM
47+#endif
diff --git a/debian/patches/restore-authorized_keys2.patch b/debian/patches/restore-authorized_keys2.patch
new file mode 100644
index 000000000..a1f52056f
--- /dev/null
+++ b/debian/patches/restore-authorized_keys2.patch
@@ -0,0 +1,35 @@
1From 8dc9bb0d9cf53a35d6003623f1e7c91326d79875 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Sun, 5 Mar 2017 02:02:11 +0000
4Subject: Restore reading authorized_keys2 by default
5
6Upstream seems to intend to gradually phase this out, so don't assume
7that this will remain the default forever. However, we were late in
8adopting the upstream sshd_config changes, so it makes sense to extend
9the grace period.
10
11Bug-Debian: https://bugs.debian.org/852320
12Forwarded: not-needed
13Last-Update: 2017-03-05
14
15Patch-Name: restore-authorized_keys2.patch
16---
17 sshd_config | 5 ++---
18 1 file changed, 2 insertions(+), 3 deletions(-)
19
20diff --git a/sshd_config b/sshd_config
21index 459c1b230..dc0db5706 100644
22--- a/sshd_config
23+++ b/sshd_config
24@@ -38,9 +38,8 @@ Include /etc/ssh/sshd_config.d/*.conf
25
26 #PubkeyAuthentication yes
27
28-# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
29-# but this is overridden so installations will only check .ssh/authorized_keys
30-AuthorizedKeysFile .ssh/authorized_keys
31+# Expect .ssh/authorized_keys2 to be disregarded by default in future.
32+#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
33
34 #AuthorizedPrincipalsFile none
35
diff --git a/debian/patches/restore-tcp-wrappers.patch b/debian/patches/restore-tcp-wrappers.patch
new file mode 100644
index 000000000..7388fadff
--- /dev/null
+++ b/debian/patches/restore-tcp-wrappers.patch
@@ -0,0 +1,172 @@
1From 6806b85f30244d186206004386a9faddc16b8738 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Tue, 7 Oct 2014 13:22:41 +0100
4Subject: Restore TCP wrappers support
5
6Support for TCP wrappers was dropped in OpenSSH 6.7. See this message
7and thread:
8
9 https://lists.mindrot.org/pipermail/openssh-unix-dev/2014-April/032497.html
10
11It is true that this reduces preauth attack surface in sshd. On the
12other hand, this support seems to be quite widely used, and abruptly
13dropping it (from the perspective of users who don't read
14openssh-unix-dev) could easily cause more serious problems in practice.
15
16It's not entirely clear what the right long-term answer for Debian is,
17but it at least probably doesn't involve dropping this feature shortly
18before a freeze.
19
20Forwarded: not-needed
21Last-Update: 2019-06-05
22
23Patch-Name: restore-tcp-wrappers.patch
24---
25 configure.ac | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++
26 sshd.8 | 7 +++++++
27 sshd.c | 25 +++++++++++++++++++++++
28 3 files changed, 89 insertions(+)
29
30diff --git a/configure.ac b/configure.ac
31index c8a96deb4..bb435ec1f 100644
32--- a/configure.ac
33+++ b/configure.ac
34@@ -1571,6 +1571,62 @@ else
35 AC_MSG_RESULT([no])
36 fi
37
38+# Check whether user wants TCP wrappers support
39+TCPW_MSG="no"
40+AC_ARG_WITH([tcp-wrappers],
41+ [ --with-tcp-wrappers[[=PATH]] Enable tcpwrappers support (optionally in PATH)],
42+ [
43+ if test "x$withval" != "xno" ; then
44+ saved_LIBS="$LIBS"
45+ saved_LDFLAGS="$LDFLAGS"
46+ saved_CPPFLAGS="$CPPFLAGS"
47+ if test -n "${withval}" && \
48+ test "x${withval}" != "xyes"; then
49+ if test -d "${withval}/lib"; then
50+ if test -n "${need_dash_r}"; then
51+ LDFLAGS="-L${withval}/lib -R${withval}/lib ${LDFLAGS}"
52+ else
53+ LDFLAGS="-L${withval}/lib ${LDFLAGS}"
54+ fi
55+ else
56+ if test -n "${need_dash_r}"; then
57+ LDFLAGS="-L${withval} -R${withval} ${LDFLAGS}"
58+ else
59+ LDFLAGS="-L${withval} ${LDFLAGS}"
60+ fi
61+ fi
62+ if test -d "${withval}/include"; then
63+ CPPFLAGS="-I${withval}/include ${CPPFLAGS}"
64+ else
65+ CPPFLAGS="-I${withval} ${CPPFLAGS}"
66+ fi
67+ fi
68+ LIBS="-lwrap $LIBS"
69+ AC_MSG_CHECKING([for libwrap])
70+ AC_LINK_IFELSE([AC_LANG_PROGRAM([[
71+#include <sys/types.h>
72+#include <sys/socket.h>
73+#include <netinet/in.h>
74+#include <tcpd.h>
75+int deny_severity = 0, allow_severity = 0;
76+ ]], [[
77+ hosts_access(0);
78+ ]])], [
79+ AC_MSG_RESULT([yes])
80+ AC_DEFINE([LIBWRAP], [1],
81+ [Define if you want
82+ TCP Wrappers support])
83+ SSHDLIBS="$SSHDLIBS -lwrap"
84+ TCPW_MSG="yes"
85+ ], [
86+ AC_MSG_ERROR([*** libwrap missing])
87+
88+ ])
89+ LIBS="$saved_LIBS"
90+ fi
91+ ]
92+)
93+
94 # Check whether user wants to use ldns
95 LDNS_MSG="no"
96 AC_ARG_WITH(ldns,
97@@ -5536,6 +5592,7 @@ echo " PAM support: $PAM_MSG"
98 echo " OSF SIA support: $SIA_MSG"
99 echo " KerberosV support: $KRB5_MSG"
100 echo " SELinux support: $SELINUX_MSG"
101+echo " TCP Wrappers support: $TCPW_MSG"
102 echo " MD5 password support: $MD5_MSG"
103 echo " libedit support: $LIBEDIT_MSG"
104 echo " libldns support: $LDNS_MSG"
105diff --git a/sshd.8 b/sshd.8
106index b2fad56d3..97d547ffa 100644
107--- a/sshd.8
108+++ b/sshd.8
109@@ -900,6 +900,12 @@ the user's home directory becomes accessible.
110 This file should be writable only by the user, and need not be
111 readable by anyone else.
112 .Pp
113+.It Pa /etc/hosts.allow
114+.It Pa /etc/hosts.deny
115+Access controls that should be enforced by tcp-wrappers are defined here.
116+Further details are described in
117+.Xr hosts_access 5 .
118+.Pp
119 .It Pa /etc/hosts.equiv
120 This file is for host-based authentication (see
121 .Xr ssh 1 ) .
122@@ -1002,6 +1008,7 @@ The content of this file is not sensitive; it can be world-readable.
123 .Xr ssh-keygen 1 ,
124 .Xr ssh-keyscan 1 ,
125 .Xr chroot 2 ,
126+.Xr hosts_access 5 ,
127 .Xr login.conf 5 ,
128 .Xr moduli 5 ,
129 .Xr sshd_config 5 ,
130diff --git a/sshd.c b/sshd.c
131index 8c5d5822e..a50ec3584 100644
132--- a/sshd.c
133+++ b/sshd.c
134@@ -124,6 +124,13 @@
135 #include "ssherr.h"
136 #include "sk-api.h"
137
138+#ifdef LIBWRAP
139+#include <tcpd.h>
140+#include <syslog.h>
141+int allow_severity;
142+int deny_severity;
143+#endif /* LIBWRAP */
144+
145 /* Re-exec fds */
146 #define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
147 #define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
148@@ -2183,6 +2190,24 @@ main(int ac, char **av)
149 #ifdef SSH_AUDIT_EVENTS
150 audit_connection_from(remote_ip, remote_port);
151 #endif
152+#ifdef LIBWRAP
153+ allow_severity = options.log_facility|LOG_INFO;
154+ deny_severity = options.log_facility|LOG_WARNING;
155+ /* Check whether logins are denied from this host. */
156+ if (ssh_packet_connection_is_on_socket(ssh)) {
157+ struct request_info req;
158+
159+ request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
160+ fromhost(&req);
161+
162+ if (!hosts_access(&req)) {
163+ debug("Connection refused by tcp wrapper");
164+ refuse(&req);
165+ /* NOTREACHED */
166+ fatal("libwrap refuse returns");
167+ }
168+ }
169+#endif /* LIBWRAP */
170
171 rdomain = ssh_packet_rdomain_in(ssh);
172
diff --git a/debian/patches/revert-ipqos-defaults.patch b/debian/patches/revert-ipqos-defaults.patch
new file mode 100644
index 000000000..b84cef134
--- /dev/null
+++ b/debian/patches/revert-ipqos-defaults.patch
@@ -0,0 +1,93 @@
1From 3728919292c05983372954d27426f7d966813139 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Mon, 8 Apr 2019 10:46:29 +0100
4Subject: Revert "upstream: Update default IPQoS in ssh(1), sshd(8) to DSCP
5 AF21 for"
6
7This reverts commit 5ee8448ad7c306f05a9f56769f95336a8269f379.
8
9The IPQoS default changes have some unfortunate interactions with
10iptables (see https://bugs.debian.org/923880) and VMware, so I'm
11temporarily reverting them until those have been fixed.
12
13Bug-Debian: https://bugs.debian.org/923879
14Bug-Debian: https://bugs.debian.org/926229
15Bug-Ubuntu: https://bugs.launchpad.net/bugs/1822370
16Last-Update: 2019-04-08
17
18Patch-Name: revert-ipqos-defaults.patch
19---
20 readconf.c | 4 ++--
21 servconf.c | 4 ++--
22 ssh_config.5 | 6 ++----
23 sshd_config.5 | 6 ++----
24 4 files changed, 8 insertions(+), 12 deletions(-)
25
26diff --git a/readconf.c b/readconf.c
27index e676b6be6..c60df5602 100644
28--- a/readconf.c
29+++ b/readconf.c
30@@ -2298,9 +2298,9 @@ fill_default_options(Options * options)
31 if (options->visual_host_key == -1)
32 options->visual_host_key = 0;
33 if (options->ip_qos_interactive == -1)
34- options->ip_qos_interactive = IPTOS_DSCP_AF21;
35+ options->ip_qos_interactive = IPTOS_LOWDELAY;
36 if (options->ip_qos_bulk == -1)
37- options->ip_qos_bulk = IPTOS_DSCP_CS1;
38+ options->ip_qos_bulk = IPTOS_THROUGHPUT;
39 if (options->request_tty == -1)
40 options->request_tty = REQUEST_TTY_AUTO;
41 if (options->proxy_use_fdpass == -1)
42diff --git a/servconf.c b/servconf.c
43index f9eb778d6..98afcfcec 100644
44--- a/servconf.c
45+++ b/servconf.c
46@@ -453,9 +453,9 @@ fill_default_server_options(ServerOptions *options)
47 if (options->permit_tun == -1)
48 options->permit_tun = SSH_TUNMODE_NO;
49 if (options->ip_qos_interactive == -1)
50- options->ip_qos_interactive = IPTOS_DSCP_AF21;
51+ options->ip_qos_interactive = IPTOS_LOWDELAY;
52 if (options->ip_qos_bulk == -1)
53- options->ip_qos_bulk = IPTOS_DSCP_CS1;
54+ options->ip_qos_bulk = IPTOS_THROUGHPUT;
55 if (options->version_addendum == NULL)
56 options->version_addendum = xstrdup("");
57 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
58diff --git a/ssh_config.5 b/ssh_config.5
59index 6d6c59521..080d289a7 100644
60--- a/ssh_config.5
61+++ b/ssh_config.5
62@@ -1156,11 +1156,9 @@ If one argument is specified, it is used as the packet class unconditionally.
63 If two values are specified, the first is automatically selected for
64 interactive sessions and the second for non-interactive sessions.
65 The default is
66-.Cm af21
67-(Low-Latency Data)
68+.Cm lowdelay
69 for interactive sessions and
70-.Cm cs1
71-(Lower Effort)
72+.Cm throughput
73 for non-interactive sessions.
74 .It Cm KbdInteractiveAuthentication
75 Specifies whether to use keyboard-interactive authentication.
76diff --git a/sshd_config.5 b/sshd_config.5
77index 472001dd1..a555e7ec3 100644
78--- a/sshd_config.5
79+++ b/sshd_config.5
80@@ -925,11 +925,9 @@ If one argument is specified, it is used as the packet class unconditionally.
81 If two values are specified, the first is automatically selected for
82 interactive sessions and the second for non-interactive sessions.
83 The default is
84-.Cm af21
85-(Low-Latency Data)
86+.Cm lowdelay
87 for interactive sessions and
88-.Cm cs1
89-(Lower Effort)
90+.Cm throughput
91 for non-interactive sessions.
92 .It Cm KbdInteractiveAuthentication
93 Specifies whether to allow keyboard-interactive authentication.
diff --git a/debian/patches/revert-x32-sandbox-breakage.patch b/debian/patches/revert-x32-sandbox-breakage.patch
new file mode 100644
index 000000000..32cff92d7
--- /dev/null
+++ b/debian/patches/revert-x32-sandbox-breakage.patch
@@ -0,0 +1,39 @@
1From 67434174b3d64b352a794275f77489ebf1575849 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Mon, 26 Oct 2020 17:36:22 +0000
4Subject: Revert "detect Linux/X32 systems"
5
6This reverts commit 5b56bd0affea7b02b540bdbc4d1d271b0e4fc885. The bug
7reporter wasn't actually using x32, but rather an ordinary 32-bit
8userspace on a 64-bit kernel; this patch broke the seccomp sandbox on
9the actual x32 architecture.
10
11Patch-Name: revert-x32-sandbox-breakage.patch
12---
13 configure.ac | 6 ------
14 1 file changed, 6 deletions(-)
15
16diff --git a/configure.ac b/configure.ac
17index 5944299fa..15fc0d653 100644
18--- a/configure.ac
19+++ b/configure.ac
20@@ -521,8 +521,6 @@ SPP_MSG="no"
21 # the --with-solaris-privs option and --with-sandbox=solaris).
22 SOLARIS_PRIVS="no"
23
24-AC_CHECK_SIZEOF([size_t])
25-
26 # Check for some target-specific stuff
27 case "$host" in
28 *-*-aix*)
29@@ -891,10 +889,6 @@ main() { if (NSVersionOfRunTimeLibrary("System") >= (60 << 16))
30 case "$host" in
31 x86_64-*)
32 seccomp_audit_arch=AUDIT_ARCH_X86_64
33- # X32: AMD64 instructions in 32bit address space.
34- if test "x$ac_cv_sizeof_size_t" = "x4" ; then
35- seccomp_audit_arch=AUDIT_ARCH_I386
36- fi
37 ;;
38 i*86-*)
39 seccomp_audit_arch=AUDIT_ARCH_I386
diff --git a/debian/patches/scp-quoting.patch b/debian/patches/scp-quoting.patch
new file mode 100644
index 000000000..604e831b3
--- /dev/null
+++ b/debian/patches/scp-quoting.patch
@@ -0,0 +1,41 @@
1From 94f06f8888f2e11267120eeebdb931d95bbfb7fd Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?Nicolas=20Valc=C3=A1rcel?= <nvalcarcel@ubuntu.com>
3Date: Sun, 9 Feb 2014 16:09:59 +0000
4Subject: Adjust scp quoting in verbose mode
5
6Tweak scp's reporting of filenames in verbose mode to be a bit less
7confusing with spaces.
8
9This should be revised to mimic real shell quoting.
10
11Bug-Ubuntu: https://bugs.launchpad.net/bugs/89945
12Last-Update: 2010-02-27
13
14Patch-Name: scp-quoting.patch
15---
16 scp.c | 12 ++++++++++--
17 1 file changed, 10 insertions(+), 2 deletions(-)
18
19diff --git a/scp.c b/scp.c
20index 6ae17061d..2d1b8e9b9 100644
21--- a/scp.c
22+++ b/scp.c
23@@ -201,8 +201,16 @@ do_local_cmd(arglist *a)
24
25 if (verbose_mode) {
26 fprintf(stderr, "Executing:");
27- for (i = 0; i < a->num; i++)
28- fmprintf(stderr, " %s", a->list[i]);
29+ for (i = 0; i < a->num; i++) {
30+ if (i == 0)
31+ fmprintf(stderr, " %s", a->list[i]);
32+ else
33+ /*
34+ * TODO: misbehaves if a->list[i] contains a
35+ * single quote
36+ */
37+ fmprintf(stderr, " '%s'", a->list[i]);
38+ }
39 fprintf(stderr, "\n");
40 }
41 if ((pid = fork()) == -1)
diff --git a/debian/patches/selinux-role.patch b/debian/patches/selinux-role.patch
new file mode 100644
index 000000000..3161999a9
--- /dev/null
+++ b/debian/patches/selinux-role.patch
@@ -0,0 +1,472 @@
1From c574865182e2c5dfa183b577f49ac602d16df5c0 Mon Sep 17 00:00:00 2001
2From: Manoj Srivastava <srivasta@debian.org>
3Date: Sun, 9 Feb 2014 16:09:49 +0000
4Subject: Handle SELinux authorisation roles
5
6Rejected upstream due to discomfort with magic usernames; a better approach
7will need an SSH protocol change. In the meantime, this came from Debian's
8SELinux maintainer, so we'll keep it until we have something better.
9
10Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1641
11Bug-Debian: http://bugs.debian.org/394795
12Last-Update: 2020-10-18
13
14Patch-Name: selinux-role.patch
15---
16 auth.h | 1 +
17 auth2.c | 10 ++++++++--
18 monitor.c | 37 +++++++++++++++++++++++++++++++++----
19 monitor.h | 2 ++
20 monitor_wrap.c | 27 ++++++++++++++++++++++++---
21 monitor_wrap.h | 3 ++-
22 openbsd-compat/port-linux.c | 21 ++++++++++++++-------
23 openbsd-compat/port-linux.h | 4 ++--
24 platform.c | 4 ++--
25 platform.h | 2 +-
26 session.c | 10 +++++-----
27 session.h | 2 +-
28 sshd.c | 2 +-
29 sshpty.c | 4 ++--
30 sshpty.h | 2 +-
31 15 files changed, 99 insertions(+), 32 deletions(-)
32
33diff --git a/auth.h b/auth.h
34index becc672b5..5da9fe75f 100644
35--- a/auth.h
36+++ b/auth.h
37@@ -63,6 +63,7 @@ struct Authctxt {
38 char *service;
39 struct passwd *pw; /* set if 'valid' */
40 char *style;
41+ char *role;
42
43 /* Method lists for multiple authentication */
44 char **auth_methods; /* modified from server config */
45diff --git a/auth2.c b/auth2.c
46index 9fa1404b3..d8363bdba 100644
47--- a/auth2.c
48+++ b/auth2.c
49@@ -265,7 +265,7 @@ input_userauth_request(int type, u_int32_t seq, struct ssh *ssh)
50 {
51 Authctxt *authctxt = ssh->authctxt;
52 Authmethod *m = NULL;
53- char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
54+ char *user = NULL, *service = NULL, *method = NULL, *style = NULL, *role = NULL;
55 int r, authenticated = 0;
56 double tstart = monotime_double();
57
58@@ -279,8 +279,13 @@ input_userauth_request(int type, u_int32_t seq, struct ssh *ssh)
59 debug("userauth-request for user %s service %s method %s", user, service, method);
60 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
61
62+ if ((role = strchr(user, '/')) != NULL)
63+ *role++ = 0;
64+
65 if ((style = strchr(user, ':')) != NULL)
66 *style++ = 0;
67+ else if (role && (style = strchr(role, ':')) != NULL)
68+ *style++ = '\0';
69
70 if (authctxt->attempt++ == 0) {
71 /* setup auth context */
72@@ -307,8 +312,9 @@ input_userauth_request(int type, u_int32_t seq, struct ssh *ssh)
73 use_privsep ? " [net]" : "");
74 authctxt->service = xstrdup(service);
75 authctxt->style = style ? xstrdup(style) : NULL;
76+ authctxt->role = role ? xstrdup(role) : NULL;
77 if (use_privsep)
78- mm_inform_authserv(service, style);
79+ mm_inform_authserv(service, style, role);
80 userauth_banner(ssh);
81 if (auth2_setup_methods_lists(authctxt) != 0)
82 ssh_packet_disconnect(ssh,
83diff --git a/monitor.c b/monitor.c
84index 11868952b..98362948f 100644
85--- a/monitor.c
86+++ b/monitor.c
87@@ -118,6 +118,7 @@ int mm_answer_sign(struct ssh *, int, struct sshbuf *);
88 int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *);
89 int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *);
90 int mm_answer_authserv(struct ssh *, int, struct sshbuf *);
91+int mm_answer_authrole(struct ssh *, int, struct sshbuf *);
92 int mm_answer_authpassword(struct ssh *, int, struct sshbuf *);
93 int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *);
94 int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *);
95@@ -198,6 +199,7 @@ struct mon_table mon_dispatch_proto20[] = {
96 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
97 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
98 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
99+ {MONITOR_REQ_AUTHROLE, MON_ONCE, mm_answer_authrole},
100 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
101 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
102 #ifdef USE_PAM
103@@ -820,6 +822,7 @@ mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
104
105 /* Allow service/style information on the auth context */
106 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
107+ monitor_permit(mon_dispatch, MONITOR_REQ_AUTHROLE, 1);
108 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
109
110 #ifdef USE_PAM
111@@ -853,16 +856,42 @@ mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
112 monitor_permit_authentications(1);
113
114 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
115- (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
116+ (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0 ||
117+ (r = sshbuf_get_cstring(m, &authctxt->role, NULL)) != 0)
118 fatal("%s: buffer error: %s", __func__, ssh_err(r));
119- debug3("%s: service=%s, style=%s",
120- __func__, authctxt->service, authctxt->style);
121+ debug3("%s: service=%s, style=%s, role=%s",
122+ __func__, authctxt->service, authctxt->style, authctxt->role);
123
124 if (strlen(authctxt->style) == 0) {
125 free(authctxt->style);
126 authctxt->style = NULL;
127 }
128
129+ if (strlen(authctxt->role) == 0) {
130+ free(authctxt->role);
131+ authctxt->role = NULL;
132+ }
133+
134+ return (0);
135+}
136+
137+int
138+mm_answer_authrole(struct ssh *ssh, int sock, struct sshbuf *m)
139+{
140+ int r;
141+
142+ monitor_permit_authentications(1);
143+
144+ if ((r = sshbuf_get_cstring(m, &authctxt->role, NULL)) != 0)
145+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
146+ debug3("%s: role=%s",
147+ __func__, authctxt->role);
148+
149+ if (strlen(authctxt->role) == 0) {
150+ free(authctxt->role);
151+ authctxt->role = NULL;
152+ }
153+
154 return (0);
155 }
156
157@@ -1566,7 +1595,7 @@ mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
158 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
159 if (res == 0)
160 goto error;
161- pty_setowner(authctxt->pw, s->tty);
162+ pty_setowner(authctxt->pw, s->tty, authctxt->role);
163
164 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
165 (r = sshbuf_put_cstring(m, s->tty)) != 0)
166diff --git a/monitor.h b/monitor.h
167index 2b1a2d590..4d87284aa 100644
168--- a/monitor.h
169+++ b/monitor.h
170@@ -65,6 +65,8 @@ enum monitor_reqtype {
171
172 MONITOR_REQ_GSSSIGN = 150, MONITOR_ANS_GSSSIGN = 151,
173 MONITOR_REQ_GSSUPCREDS = 152, MONITOR_ANS_GSSUPCREDS = 153,
174+
175+ MONITOR_REQ_AUTHROLE = 154,
176 };
177
178 struct ssh;
179diff --git a/monitor_wrap.c b/monitor_wrap.c
180index 0e78cd006..d41d3949d 100644
181--- a/monitor_wrap.c
182+++ b/monitor_wrap.c
183@@ -364,10 +364,10 @@ mm_auth2_read_banner(void)
184 return (banner);
185 }
186
187-/* Inform the privileged process about service and style */
188+/* Inform the privileged process about service, style, and role */
189
190 void
191-mm_inform_authserv(char *service, char *style)
192+mm_inform_authserv(char *service, char *style, char *role)
193 {
194 struct sshbuf *m;
195 int r;
196@@ -377,7 +377,8 @@ mm_inform_authserv(char *service, char *style)
197 if ((m = sshbuf_new()) == NULL)
198 fatal("%s: sshbuf_new failed", __func__);
199 if ((r = sshbuf_put_cstring(m, service)) != 0 ||
200- (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
201+ (r = sshbuf_put_cstring(m, style ? style : "")) != 0 ||
202+ (r = sshbuf_put_cstring(m, role ? role : "")) != 0)
203 fatal("%s: buffer error: %s", __func__, ssh_err(r));
204
205 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
206@@ -385,6 +386,26 @@ mm_inform_authserv(char *service, char *style)
207 sshbuf_free(m);
208 }
209
210+/* Inform the privileged process about role */
211+
212+void
213+mm_inform_authrole(char *role)
214+{
215+ struct sshbuf *m;
216+ int r;
217+
218+ debug3("%s entering", __func__);
219+
220+ if ((m = sshbuf_new()) == NULL)
221+ fatal("%s: sshbuf_new failed", __func__);
222+ if ((r = sshbuf_put_cstring(m, role ? role : "")) != 0)
223+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
224+
225+ mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHROLE, m);
226+
227+ sshbuf_free(m);
228+}
229+
230 /* Do the password authentication */
231 int
232 mm_auth_password(struct ssh *ssh, char *password)
233diff --git a/monitor_wrap.h b/monitor_wrap.h
234index 75aef1c74..c39e5dd8b 100644
235--- a/monitor_wrap.h
236+++ b/monitor_wrap.h
237@@ -48,7 +48,8 @@ DH *mm_choose_dh(int, int, int);
238 int mm_sshkey_sign(struct ssh *, struct sshkey *, u_char **, size_t *,
239 const u_char *, size_t, const char *, const char *,
240 const char *, u_int compat);
241-void mm_inform_authserv(char *, char *);
242+void mm_inform_authserv(char *, char *, char *);
243+void mm_inform_authrole(char *);
244 struct passwd *mm_getpwnamallow(struct ssh *, const char *);
245 char *mm_auth2_read_banner(void);
246 int mm_auth_password(struct ssh *, char *);
247diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c
248index f46094faf..56f1d2c1e 100644
249--- a/openbsd-compat/port-linux.c
250+++ b/openbsd-compat/port-linux.c
251@@ -56,7 +56,7 @@ ssh_selinux_enabled(void)
252
253 /* Return the default security context for the given username */
254 static security_context_t
255-ssh_selinux_getctxbyname(char *pwname)
256+ssh_selinux_getctxbyname(char *pwname, const char *role)
257 {
258 security_context_t sc = NULL;
259 char *sename = NULL, *lvl = NULL;
260@@ -71,9 +71,16 @@ ssh_selinux_getctxbyname(char *pwname)
261 #endif
262
263 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
264- r = get_default_context_with_level(sename, lvl, NULL, &sc);
265+ if (role != NULL && role[0])
266+ r = get_default_context_with_rolelevel(sename, role, lvl, NULL,
267+ &sc);
268+ else
269+ r = get_default_context_with_level(sename, lvl, NULL, &sc);
270 #else
271- r = get_default_context(sename, NULL, &sc);
272+ if (role != NULL && role[0])
273+ r = get_default_context_with_role(sename, role, NULL, &sc);
274+ else
275+ r = get_default_context(sename, NULL, &sc);
276 #endif
277
278 if (r != 0) {
279@@ -103,7 +110,7 @@ ssh_selinux_getctxbyname(char *pwname)
280
281 /* Set the execution context to the default for the specified user */
282 void
283-ssh_selinux_setup_exec_context(char *pwname)
284+ssh_selinux_setup_exec_context(char *pwname, const char *role)
285 {
286 security_context_t user_ctx = NULL;
287
288@@ -112,7 +119,7 @@ ssh_selinux_setup_exec_context(char *pwname)
289
290 debug3("%s: setting execution context", __func__);
291
292- user_ctx = ssh_selinux_getctxbyname(pwname);
293+ user_ctx = ssh_selinux_getctxbyname(pwname, role);
294 if (setexeccon(user_ctx) != 0) {
295 switch (security_getenforce()) {
296 case -1:
297@@ -134,7 +141,7 @@ ssh_selinux_setup_exec_context(char *pwname)
298
299 /* Set the TTY context for the specified user */
300 void
301-ssh_selinux_setup_pty(char *pwname, const char *tty)
302+ssh_selinux_setup_pty(char *pwname, const char *tty, const char *role)
303 {
304 security_context_t new_tty_ctx = NULL;
305 security_context_t user_ctx = NULL;
306@@ -146,7 +153,7 @@ ssh_selinux_setup_pty(char *pwname, const char *tty)
307
308 debug3("%s: setting TTY context on %s", __func__, tty);
309
310- user_ctx = ssh_selinux_getctxbyname(pwname);
311+ user_ctx = ssh_selinux_getctxbyname(pwname, role);
312
313 /* XXX: should these calls fatal() upon failure in enforcing mode? */
314
315diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h
316index 3c22a854d..c88129428 100644
317--- a/openbsd-compat/port-linux.h
318+++ b/openbsd-compat/port-linux.h
319@@ -19,8 +19,8 @@
320
321 #ifdef WITH_SELINUX
322 int ssh_selinux_enabled(void);
323-void ssh_selinux_setup_pty(char *, const char *);
324-void ssh_selinux_setup_exec_context(char *);
325+void ssh_selinux_setup_pty(char *, const char *, const char *);
326+void ssh_selinux_setup_exec_context(char *, const char *);
327 void ssh_selinux_change_context(const char *);
328 void ssh_selinux_setfscreatecon(const char *);
329 #endif
330diff --git a/platform.c b/platform.c
331index 44ba71dc5..2defe9425 100644
332--- a/platform.c
333+++ b/platform.c
334@@ -143,7 +143,7 @@ platform_setusercontext(struct passwd *pw)
335 * called if sshd is running as root.
336 */
337 void
338-platform_setusercontext_post_groups(struct passwd *pw)
339+platform_setusercontext_post_groups(struct passwd *pw, const char *role)
340 {
341 #if !defined(HAVE_LOGIN_CAP) && defined(USE_PAM)
342 /*
343@@ -184,7 +184,7 @@ platform_setusercontext_post_groups(struct passwd *pw)
344 }
345 #endif /* HAVE_SETPCRED */
346 #ifdef WITH_SELINUX
347- ssh_selinux_setup_exec_context(pw->pw_name);
348+ ssh_selinux_setup_exec_context(pw->pw_name, role);
349 #endif
350 }
351
352diff --git a/platform.h b/platform.h
353index ea4f9c584..60d72ffe7 100644
354--- a/platform.h
355+++ b/platform.h
356@@ -25,7 +25,7 @@ void platform_post_fork_parent(pid_t child_pid);
357 void platform_post_fork_child(void);
358 int platform_privileged_uidswap(void);
359 void platform_setusercontext(struct passwd *);
360-void platform_setusercontext_post_groups(struct passwd *);
361+void platform_setusercontext_post_groups(struct passwd *, const char *);
362 char *platform_get_krb5_client(const char *);
363 char *platform_krb5_get_principal_name(const char *);
364 int platform_sys_dir_uid(uid_t);
365diff --git a/session.c b/session.c
366index 857f17b3c..b1796a803 100644
367--- a/session.c
368+++ b/session.c
369@@ -1364,7 +1364,7 @@ safely_chroot(const char *path, uid_t uid)
370
371 /* Set login name, uid, gid, and groups. */
372 void
373-do_setusercontext(struct passwd *pw)
374+do_setusercontext(struct passwd *pw, const char *role)
375 {
376 char uidstr[32], *chroot_path, *tmp;
377
378@@ -1392,7 +1392,7 @@ do_setusercontext(struct passwd *pw)
379 endgrent();
380 #endif
381
382- platform_setusercontext_post_groups(pw);
383+ platform_setusercontext_post_groups(pw, role);
384
385 if (!in_chroot && options.chroot_directory != NULL &&
386 strcasecmp(options.chroot_directory, "none") != 0) {
387@@ -1536,7 +1536,7 @@ do_child(struct ssh *ssh, Session *s, const char *command)
388
389 /* Force a password change */
390 if (s->authctxt->force_pwchange) {
391- do_setusercontext(pw);
392+ do_setusercontext(pw, s->authctxt->role);
393 child_close_fds(ssh);
394 do_pwchange(s);
395 exit(1);
396@@ -1554,7 +1554,7 @@ do_child(struct ssh *ssh, Session *s, const char *command)
397 /* When PAM is enabled we rely on it to do the nologin check */
398 if (!options.use_pam)
399 do_nologin(pw);
400- do_setusercontext(pw);
401+ do_setusercontext(pw, s->authctxt->role);
402 /*
403 * PAM session modules in do_setusercontext may have
404 * generated messages, so if this in an interactive
405@@ -1953,7 +1953,7 @@ session_pty_req(struct ssh *ssh, Session *s)
406 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
407
408 if (!use_privsep)
409- pty_setowner(s->pw, s->tty);
410+ pty_setowner(s->pw, s->tty, s->authctxt->role);
411
412 /* Set window size from the packet. */
413 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
414diff --git a/session.h b/session.h
415index ce59dabd9..675c91146 100644
416--- a/session.h
417+++ b/session.h
418@@ -77,7 +77,7 @@ void session_pty_cleanup2(Session *);
419 Session *session_new(void);
420 Session *session_by_tty(char *);
421 void session_close(struct ssh *, Session *);
422-void do_setusercontext(struct passwd *);
423+void do_setusercontext(struct passwd *, const char *);
424
425 const char *session_get_remote_name_or_ip(struct ssh *, u_int, int);
426
427diff --git a/sshd.c b/sshd.c
428index a50ec3584..38d281ab4 100644
429--- a/sshd.c
430+++ b/sshd.c
431@@ -594,7 +594,7 @@ privsep_postauth(struct ssh *ssh, Authctxt *authctxt)
432 reseed_prngs();
433
434 /* Drop privileges */
435- do_setusercontext(authctxt->pw);
436+ do_setusercontext(authctxt->pw, authctxt->role);
437
438 skip:
439 /* It is safe now to apply the key state */
440diff --git a/sshpty.c b/sshpty.c
441index bce09e255..308449b37 100644
442--- a/sshpty.c
443+++ b/sshpty.c
444@@ -162,7 +162,7 @@ pty_change_window_size(int ptyfd, u_int row, u_int col,
445 }
446
447 void
448-pty_setowner(struct passwd *pw, const char *tty)
449+pty_setowner(struct passwd *pw, const char *tty, const char *role)
450 {
451 struct group *grp;
452 gid_t gid;
453@@ -186,7 +186,7 @@ pty_setowner(struct passwd *pw, const char *tty)
454 strerror(errno));
455
456 #ifdef WITH_SELINUX
457- ssh_selinux_setup_pty(pw->pw_name, tty);
458+ ssh_selinux_setup_pty(pw->pw_name, tty, role);
459 #endif
460
461 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
462diff --git a/sshpty.h b/sshpty.h
463index 9ec7e9a15..de7e000ae 100644
464--- a/sshpty.h
465+++ b/sshpty.h
466@@ -24,5 +24,5 @@ int pty_allocate(int *, int *, char *, size_t);
467 void pty_release(const char *);
468 void pty_make_controlling_tty(int *, const char *);
469 void pty_change_window_size(int, u_int, u_int, u_int, u_int);
470-void pty_setowner(struct passwd *, const char *);
471+void pty_setowner(struct passwd *, const char *, const char *);
472 void disconnect_controlling_tty(void);
diff --git a/debian/patches/series b/debian/patches/series
new file mode 100644
index 000000000..5b00428bc
--- /dev/null
+++ b/debian/patches/series
@@ -0,0 +1,27 @@
1gssapi.patch
2restore-tcp-wrappers.patch
3selinux-role.patch
4ssh-vulnkey-compat.patch
5keepalive-extensions.patch
6syslog-level-silent.patch
7user-group-modes.patch
8scp-quoting.patch
9shell-path.patch
10dnssec-sshfp.patch
11mention-ssh-keygen-on-keychange.patch
12package-versioning.patch
13debian-banner.patch
14authorized-keys-man-symlink.patch
15openbsd-docs.patch
16ssh-argv0.patch
17doc-hash-tab-completion.patch
18ssh-agent-setgid.patch
19no-openssl-version-status.patch
20gnome-ssh-askpass2-icon.patch
21systemd-readiness.patch
22debian-config.patch
23restore-authorized_keys2.patch
24conch-old-privkey-format.patch
25revert-ipqos-defaults.patch
26revert-x32-sandbox-breakage.patch
27ssh-copy-id-heredoc-syntax.patch
diff --git a/debian/patches/shell-path.patch b/debian/patches/shell-path.patch
new file mode 100644
index 000000000..503b08dda
--- /dev/null
+++ b/debian/patches/shell-path.patch
@@ -0,0 +1,39 @@
1From a7d2f23b7b86f97749856482233cdc9dd970d1d3 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Sun, 9 Feb 2014 16:10:00 +0000
4Subject: Look for $SHELL on the path for ProxyCommand/LocalCommand
5
6There's some debate on the upstream bug about whether POSIX requires this.
7I (Colin Watson) agree with Vincent and think it does.
8
9Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1494
10Bug-Debian: http://bugs.debian.org/492728
11Last-Update: 2020-02-21
12
13Patch-Name: shell-path.patch
14---
15 sshconnect.c | 4 ++--
16 1 file changed, 2 insertions(+), 2 deletions(-)
17
18diff --git a/sshconnect.c b/sshconnect.c
19index 9ec0618a9..5f8c81b84 100644
20--- a/sshconnect.c
21+++ b/sshconnect.c
22@@ -263,7 +263,7 @@ ssh_proxy_connect(struct ssh *ssh, const char *host, const char *host_arg,
23 /* Execute the proxy command. Note that we gave up any
24 extra privileges above. */
25 ssh_signal(SIGPIPE, SIG_DFL);
26- execv(argv[0], argv);
27+ execvp(argv[0], argv);
28 perror(argv[0]);
29 exit(1);
30 }
31@@ -1392,7 +1392,7 @@ ssh_local_cmd(const char *args)
32 if (pid == 0) {
33 ssh_signal(SIGPIPE, SIG_DFL);
34 debug3("Executing %s -c \"%s\"", shell, args);
35- execl(shell, shell, "-c", args, (char *)NULL);
36+ execlp(shell, shell, "-c", args, (char *)NULL);
37 error("Couldn't execute %s -c \"%s\": %s",
38 shell, args, strerror(errno));
39 _exit(1);
diff --git a/debian/patches/ssh-agent-setgid.patch b/debian/patches/ssh-agent-setgid.patch
new file mode 100644
index 000000000..5d7a6c0fb
--- /dev/null
+++ b/debian/patches/ssh-agent-setgid.patch
@@ -0,0 +1,40 @@
1From 7a305ed4a0cba43d0d1bc6ebf5737521a0854a9d Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Sun, 9 Feb 2014 16:10:13 +0000
4Subject: Document consequences of ssh-agent being setgid in ssh-agent(1)
5
6Bug-Debian: http://bugs.debian.org/711623
7Forwarded: no
8Last-Update: 2020-02-21
9
10Patch-Name: ssh-agent-setgid.patch
11---
12 ssh-agent.1 | 15 +++++++++++++++
13 1 file changed, 15 insertions(+)
14
15diff --git a/ssh-agent.1 b/ssh-agent.1
16index 2cf46160b..272da79b3 100644
17--- a/ssh-agent.1
18+++ b/ssh-agent.1
19@@ -206,6 +206,21 @@ socket and stores its pathname in this variable.
20 It is accessible only to the current user,
21 but is easily abused by root or another instance of the same user.
22 .El
23+.Pp
24+In Debian,
25+.Nm
26+is installed with the set-group-id bit set, to prevent
27+.Xr ptrace 2
28+attacks retrieving private key material.
29+This has the side-effect of causing the run-time linker to remove certain
30+environment variables which might have security implications for set-id
31+programs, including
32+.Ev LD_PRELOAD ,
33+.Ev LD_LIBRARY_PATH ,
34+and
35+.Ev TMPDIR .
36+If you need to set any of these environment variables, you will need to do
37+so in the program executed by ssh-agent.
38 .Sh FILES
39 .Bl -tag -width Ds
40 .It Pa $TMPDIR/ssh-XXXXXXXXXX/agent.<ppid>
diff --git a/debian/patches/ssh-argv0.patch b/debian/patches/ssh-argv0.patch
new file mode 100644
index 000000000..12f8c1b90
--- /dev/null
+++ b/debian/patches/ssh-argv0.patch
@@ -0,0 +1,31 @@
1From 0e71b467fd84b0972c6aa2762d93af1c3defc0dc Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Sun, 9 Feb 2014 16:10:10 +0000
4Subject: ssh(1): Refer to ssh-argv0(1)
5
6Old versions of OpenSSH (up to 2.5 or thereabouts) allowed creating symlinks
7to ssh with the name of the host you want to connect to. Debian ships an
8ssh-argv0 script restoring this feature; this patch refers to its manual
9page from ssh(1).
10
11Bug-Debian: http://bugs.debian.org/111341
12Forwarded: not-needed
13Last-Update: 2013-09-14
14
15Patch-Name: ssh-argv0.patch
16---
17 ssh.1 | 1 +
18 1 file changed, 1 insertion(+)
19
20diff --git a/ssh.1 b/ssh.1
21index 1880c032d..76ddd89b5 100644
22--- a/ssh.1
23+++ b/ssh.1
24@@ -1632,6 +1632,7 @@ if an error occurred.
25 .Xr sftp 1 ,
26 .Xr ssh-add 1 ,
27 .Xr ssh-agent 1 ,
28+.Xr ssh-argv0 1 ,
29 .Xr ssh-keygen 1 ,
30 .Xr ssh-keyscan 1 ,
31 .Xr tun 4 ,
diff --git a/debian/patches/ssh-copy-id-heredoc-syntax.patch b/debian/patches/ssh-copy-id-heredoc-syntax.patch
new file mode 100644
index 000000000..e0259670a
--- /dev/null
+++ b/debian/patches/ssh-copy-id-heredoc-syntax.patch
@@ -0,0 +1,37 @@
1From 27cf2f667b46a99f4469f41bcb8e004834a3d34f Mon Sep 17 00:00:00 2001
2From: Oleg <Fallmay@users.noreply.github.com>
3Date: Thu, 1 Oct 2020 12:09:08 +0300
4Subject: Fix `EOF: command not found` error in ssh-copy-id
5
6Origin: upstream, https://anongit.mindrot.org/openssh.git/commit/?id=d9e727dcc04a52caaac87543ea1d230e9e6b5604
7Bug: https://github.com/openssh/openssh-portable/pull/206
8Bug-Debian: https://bugs.debian.org/975540
9Bug-Debian: https://bugs.debian.org/976242
10Last-Update: 2020-12-02
11
12Patch-Name: ssh-copy-id-heredoc-syntax.patch
13---
14 contrib/ssh-copy-id | 3 ++-
15 1 file changed, 2 insertions(+), 1 deletion(-)
16
17diff --git a/contrib/ssh-copy-id b/contrib/ssh-copy-id
18index 392f64f94..a76907717 100644
19--- a/contrib/ssh-copy-id
20+++ b/contrib/ssh-copy-id
21@@ -247,7 +247,7 @@ installkeys_sh() {
22 # the -z `tail ...` checks for a trailing newline. The echo adds one if was missing
23 # the cat adds the keys we're getting via STDIN
24 # and if available restorecon is used to restore the SELinux context
25- INSTALLKEYS_SH=$(tr '\t\n' ' ' <<-EOF)
26+ INSTALLKEYS_SH=$(tr '\t\n' ' ' <<-EOF
27 cd;
28 umask 077;
29 mkdir -p $(dirname "${AUTH_KEY_FILE}") &&
30@@ -258,6 +258,7 @@ installkeys_sh() {
31 restorecon -F .ssh ${AUTH_KEY_FILE};
32 fi
33 EOF
34+ )
35
36 # to defend against quirky remote shells: use 'exec sh -c' to get POSIX;
37 printf "exec sh -c '%s'" "${INSTALLKEYS_SH}"
diff --git a/debian/patches/ssh-vulnkey-compat.patch b/debian/patches/ssh-vulnkey-compat.patch
new file mode 100644
index 000000000..f4bedfd7b
--- /dev/null
+++ b/debian/patches/ssh-vulnkey-compat.patch
@@ -0,0 +1,42 @@
1From 61b4d4c07d19cd0816ab5d48da81a75f7adbdf24 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@ubuntu.com>
3Date: Sun, 9 Feb 2014 16:09:50 +0000
4Subject: Accept obsolete ssh-vulnkey configuration options
5
6These options were used as part of Debian's response to CVE-2008-0166.
7Nearly six years later, we no longer need to continue carrying the bulk
8of that patch, but we do need to avoid failing when the associated
9configuration options are still present.
10
11Last-Update: 2014-02-09
12
13Patch-Name: ssh-vulnkey-compat.patch
14---
15 readconf.c | 1 +
16 servconf.c | 1 +
17 2 files changed, 2 insertions(+)
18
19diff --git a/readconf.c b/readconf.c
20index 57dae55d1..b069333fa 100644
21--- a/readconf.c
22+++ b/readconf.c
23@@ -191,6 +191,7 @@ static struct {
24 { "fallbacktorsh", oDeprecated },
25 { "globalknownhostsfile2", oDeprecated },
26 { "rhostsauthentication", oDeprecated },
27+ { "useblacklistedkeys", oDeprecated },
28 { "userknownhostsfile2", oDeprecated },
29 { "useroaming", oDeprecated },
30 { "usersh", oDeprecated },
31diff --git a/servconf.c b/servconf.c
32index ded8f4a87..21abe41ac 100644
33--- a/servconf.c
34+++ b/servconf.c
35@@ -649,6 +649,7 @@ static struct {
36 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
37 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
38 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
39+ { "permitblacklistedkeys", sDeprecated, SSHCFG_GLOBAL },
40 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
41 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
42 { "uselogin", sDeprecated, SSHCFG_GLOBAL },
diff --git a/debian/patches/syslog-level-silent.patch b/debian/patches/syslog-level-silent.patch
new file mode 100644
index 000000000..d6215dea6
--- /dev/null
+++ b/debian/patches/syslog-level-silent.patch
@@ -0,0 +1,47 @@
1From 33a5f7aadea15899586710c615408045eaaecebd Mon Sep 17 00:00:00 2001
2From: Natalie Amery <nmamery@chiark.greenend.org.uk>
3Date: Sun, 9 Feb 2014 16:09:54 +0000
4Subject: "LogLevel SILENT" compatibility
5
6"LogLevel SILENT" (-qq) was introduced in Debian openssh 1:3.0.1p1-1 to
7match the behaviour of non-free SSH, in which -q does not suppress fatal
8errors. However, this was unintentionally broken in 1:4.6p1-2 and nobody
9complained, so we've dropped most of it. The parts that remain are basic
10configuration file compatibility, and an adjustment to "Pseudo-terminal will
11not be allocated ..." which should be split out into a separate patch.
12
13Author: Matthew Vernon <matthew@debian.org>
14Author: Colin Watson <cjwatson@debian.org>
15Last-Update: 2013-09-14
16
17Patch-Name: syslog-level-silent.patch
18---
19 log.c | 1 +
20 ssh.c | 2 +-
21 2 files changed, 2 insertions(+), 1 deletion(-)
22
23diff --git a/log.c b/log.c
24index 6b1a7a314..5ebae1480 100644
25--- a/log.c
26+++ b/log.c
27@@ -93,6 +93,7 @@ static struct {
28 LogLevel val;
29 } log_levels[] =
30 {
31+ { "SILENT", SYSLOG_LEVEL_QUIET }, /* compatibility */
32 { "QUIET", SYSLOG_LEVEL_QUIET },
33 { "FATAL", SYSLOG_LEVEL_FATAL },
34 { "ERROR", SYSLOG_LEVEL_ERROR },
35diff --git a/ssh.c b/ssh.c
36index bb98a7e2d..aa15b8a1f 100644
37--- a/ssh.c
38+++ b/ssh.c
39@@ -1373,7 +1373,7 @@ main(int ac, char **av)
40 /* Do not allocate a tty if stdin is not a tty. */
41 if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
42 options.request_tty != REQUEST_TTY_FORCE) {
43- if (tty_flag)
44+ if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
45 logit("Pseudo-terminal will not be allocated because "
46 "stdin is not a terminal.");
47 tty_flag = 0;
diff --git a/debian/patches/systemd-readiness.patch b/debian/patches/systemd-readiness.patch
new file mode 100644
index 000000000..37e98c1dc
--- /dev/null
+++ b/debian/patches/systemd-readiness.patch
@@ -0,0 +1,84 @@
1From e8453621b2a26f8d6afec405ff60201749b01e5e Mon Sep 17 00:00:00 2001
2From: Michael Biebl <biebl@debian.org>
3Date: Mon, 21 Dec 2015 16:08:47 +0000
4Subject: Add systemd readiness notification support
5
6Bug-Debian: https://bugs.debian.org/778913
7Forwarded: no
8Last-Update: 2017-08-22
9
10Patch-Name: systemd-readiness.patch
11---
12 configure.ac | 24 ++++++++++++++++++++++++
13 sshd.c | 9 +++++++++
14 2 files changed, 33 insertions(+)
15
16diff --git a/configure.ac b/configure.ac
17index bb435ec1f..5944299fa 100644
18--- a/configure.ac
19+++ b/configure.ac
20@@ -4785,6 +4785,29 @@ AC_ARG_WITH([kerberos5],
21 AC_SUBST([GSSLIBS])
22 AC_SUBST([K5LIBS])
23
24+# Check whether user wants systemd support
25+SYSTEMD_MSG="no"
26+AC_ARG_WITH(systemd,
27+ [ --with-systemd Enable systemd support],
28+ [ if test "x$withval" != "xno" ; then
29+ AC_PATH_TOOL([PKGCONFIG], [pkg-config], [no])
30+ if test "$PKGCONFIG" != "no"; then
31+ AC_MSG_CHECKING([for libsystemd])
32+ if $PKGCONFIG --exists libsystemd; then
33+ SYSTEMD_CFLAGS=`$PKGCONFIG --cflags libsystemd`
34+ SYSTEMD_LIBS=`$PKGCONFIG --libs libsystemd`
35+ CPPFLAGS="$CPPFLAGS $SYSTEMD_CFLAGS"
36+ SSHDLIBS="$SSHDLIBS $SYSTEMD_LIBS"
37+ AC_MSG_RESULT([yes])
38+ AC_DEFINE(HAVE_SYSTEMD, 1, [Define if you want systemd support.])
39+ SYSTEMD_MSG="yes"
40+ else
41+ AC_MSG_RESULT([no])
42+ fi
43+ fi
44+ fi ]
45+)
46+
47 # Looking for programs, paths and files
48
49 PRIVSEP_PATH=/var/empty
50@@ -5599,6 +5622,7 @@ echo " libldns support: $LDNS_MSG"
51 echo " Solaris process contract support: $SPC_MSG"
52 echo " Solaris project support: $SP_MSG"
53 echo " Solaris privilege support: $SPP_MSG"
54+echo " systemd support: $SYSTEMD_MSG"
55 echo " IP address in \$DISPLAY hack: $DISPLAY_HACK_MSG"
56 echo " Translate v4 in v6 hack: $IPV4_IN6_HACK_MSG"
57 echo " BSD Auth support: $BSD_AUTH_MSG"
58diff --git a/sshd.c b/sshd.c
59index 50f2726bf..fb9b7b7fb 100644
60--- a/sshd.c
61+++ b/sshd.c
62@@ -85,6 +85,10 @@
63 #include <prot.h>
64 #endif
65
66+#ifdef HAVE_SYSTEMD
67+#include <systemd/sd-daemon.h>
68+#endif
69+
70 #include "xmalloc.h"
71 #include "ssh.h"
72 #include "ssh2.h"
73@@ -2076,6 +2080,11 @@ main(int ac, char **av)
74 }
75 }
76
77+#ifdef HAVE_SYSTEMD
78+ /* Signal systemd that we are ready to accept connections */
79+ sd_notify(0, "READY=1");
80+#endif
81+
82 /* Accept a connection and return in a forked child */
83 server_accept_loop(&sock_in, &sock_out,
84 &newsock, config_s);
diff --git a/debian/patches/user-group-modes.patch b/debian/patches/user-group-modes.patch
new file mode 100644
index 000000000..8f5a8a383
--- /dev/null
+++ b/debian/patches/user-group-modes.patch
@@ -0,0 +1,210 @@
1From d08cd2b0cfbedf3ccd2ec3adaef850b8d9a87e85 Mon Sep 17 00:00:00 2001
2From: Colin Watson <cjwatson@debian.org>
3Date: Sun, 9 Feb 2014 16:09:58 +0000
4Subject: Allow harmless group-writability
5
6Allow secure files (~/.ssh/config, ~/.ssh/authorized_keys, etc.) to be
7group-writable, provided that the group in question contains only the file's
8owner. Rejected upstream for IMO incorrect reasons (e.g. a misunderstanding
9about the contents of gr->gr_mem). Given that per-user groups and umask 002
10are the default setup in Debian (for good reasons - this makes operating in
11setgid directories with other groups much easier), we need to permit this by
12default.
13
14Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1060
15Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=314347
16Last-Update: 2019-10-09
17
18Patch-Name: user-group-modes.patch
19---
20 auth-rhosts.c | 6 ++----
21 auth.c | 3 +--
22 misc.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++-----
23 misc.h | 2 ++
24 readconf.c | 3 +--
25 ssh.1 | 2 ++
26 ssh_config.5 | 2 ++
27 7 files changed, 63 insertions(+), 13 deletions(-)
28
29diff --git a/auth-rhosts.c b/auth-rhosts.c
30index e81321b49..3bcc73766 100644
31--- a/auth-rhosts.c
32+++ b/auth-rhosts.c
33@@ -260,8 +260,7 @@ auth_rhosts2(struct passwd *pw, const char *client_user, const char *hostname,
34 return 0;
35 }
36 if (options.strict_modes &&
37- ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
38- (st.st_mode & 022) != 0)) {
39+ !secure_permissions(&st, pw->pw_uid)) {
40 logit("Rhosts authentication refused for %.100s: "
41 "bad ownership or modes for home directory.", pw->pw_name);
42 auth_debug_add("Rhosts authentication refused for %.100s: "
43@@ -287,8 +286,7 @@ auth_rhosts2(struct passwd *pw, const char *client_user, const char *hostname,
44 * allowing access to their account by anyone.
45 */
46 if (options.strict_modes &&
47- ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
48- (st.st_mode & 022) != 0)) {
49+ !secure_permissions(&st, pw->pw_uid)) {
50 logit("Rhosts authentication refused for %.100s: bad modes for %.200s",
51 pw->pw_name, buf);
52 auth_debug_add("Bad file modes for %.200s", buf);
53diff --git a/auth.c b/auth.c
54index 3d31ec860..4152d9c44 100644
55--- a/auth.c
56+++ b/auth.c
57@@ -474,8 +474,7 @@ check_key_in_hostfiles(struct passwd *pw, struct sshkey *key, const char *host,
58 user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
59 if (options.strict_modes &&
60 (stat(user_hostfile, &st) == 0) &&
61- ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
62- (st.st_mode & 022) != 0)) {
63+ !secure_permissions(&st, pw->pw_uid)) {
64 logit("Authentication refused for %.100s: "
65 "bad owner or modes for %.200s",
66 pw->pw_name, user_hostfile);
67diff --git a/misc.c b/misc.c
68index 4623b5755..c75a795c2 100644
69--- a/misc.c
70+++ b/misc.c
71@@ -55,8 +55,9 @@
72 #include <netdb.h>
73 #ifdef HAVE_PATHS_H
74 # include <paths.h>
75-#include <pwd.h>
76 #endif
77+#include <pwd.h>
78+#include <grp.h>
79 #ifdef SSH_TUN_OPENBSD
80 #include <net/if.h>
81 #endif
82@@ -1271,6 +1272,55 @@ percent_dollar_expand(const char *string, ...)
83 return ret;
84 }
85
86+int
87+secure_permissions(struct stat *st, uid_t uid)
88+{
89+ if (!platform_sys_dir_uid(st->st_uid) && st->st_uid != uid)
90+ return 0;
91+ if ((st->st_mode & 002) != 0)
92+ return 0;
93+ if ((st->st_mode & 020) != 0) {
94+ /* If the file is group-writable, the group in question must
95+ * have exactly one member, namely the file's owner.
96+ * (Zero-member groups are typically used by setgid
97+ * binaries, and are unlikely to be suitable.)
98+ */
99+ struct passwd *pw;
100+ struct group *gr;
101+ int members = 0;
102+
103+ gr = getgrgid(st->st_gid);
104+ if (!gr)
105+ return 0;
106+
107+ /* Check primary group memberships. */
108+ while ((pw = getpwent()) != NULL) {
109+ if (pw->pw_gid == gr->gr_gid) {
110+ ++members;
111+ if (pw->pw_uid != uid)
112+ return 0;
113+ }
114+ }
115+ endpwent();
116+
117+ pw = getpwuid(st->st_uid);
118+ if (!pw)
119+ return 0;
120+
121+ /* Check supplementary group memberships. */
122+ if (gr->gr_mem[0]) {
123+ ++members;
124+ if (strcmp(pw->pw_name, gr->gr_mem[0]) ||
125+ gr->gr_mem[1])
126+ return 0;
127+ }
128+
129+ if (!members)
130+ return 0;
131+ }
132+ return 1;
133+}
134+
135 int
136 tun_open(int tun, int mode, char **ifname)
137 {
138@@ -2056,8 +2106,7 @@ safe_path(const char *name, struct stat *stp, const char *pw_dir,
139 snprintf(err, errlen, "%s is not a regular file", buf);
140 return -1;
141 }
142- if ((!platform_sys_dir_uid(stp->st_uid) && stp->st_uid != uid) ||
143- (stp->st_mode & 022) != 0) {
144+ if (!secure_permissions(stp, uid)) {
145 snprintf(err, errlen, "bad ownership or modes for file %s",
146 buf);
147 return -1;
148@@ -2072,8 +2121,7 @@ safe_path(const char *name, struct stat *stp, const char *pw_dir,
149 strlcpy(buf, cp, sizeof(buf));
150
151 if (stat(buf, &st) == -1 ||
152- (!platform_sys_dir_uid(st.st_uid) && st.st_uid != uid) ||
153- (st.st_mode & 022) != 0) {
154+ !secure_permissions(&st, uid)) {
155 snprintf(err, errlen,
156 "bad ownership or modes for directory %s", buf);
157 return -1;
158diff --git a/misc.h b/misc.h
159index ab94a79c0..b34c798e7 100644
160--- a/misc.h
161+++ b/misc.h
162@@ -192,6 +192,8 @@ struct notifier_ctx *notify_start(int, const char *, ...)
163 __attribute__((format(printf, 2, 3)));
164 void notify_complete(struct notifier_ctx *);
165
166+int secure_permissions(struct stat *st, uid_t uid);
167+
168 #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
169 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
170 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
171diff --git a/readconf.c b/readconf.c
172index 3d0a812b3..f4f273c96 100644
173--- a/readconf.c
174+++ b/readconf.c
175@@ -1967,8 +1967,7 @@ read_config_file_depth(const char *filename, struct passwd *pw,
176
177 if (fstat(fileno(f), &sb) == -1)
178 fatal("fstat %s: %s", filename, strerror(errno));
179- if (((sb.st_uid != 0 && sb.st_uid != getuid()) ||
180- (sb.st_mode & 022) != 0))
181+ if (!secure_permissions(&sb, getuid()))
182 fatal("Bad owner or permissions on %s", filename);
183 }
184
185diff --git a/ssh.1 b/ssh.1
186index be8e964f0..5d613076c 100644
187--- a/ssh.1
188+++ b/ssh.1
189@@ -1528,6 +1528,8 @@ The file format and configuration options are described in
190 .Xr ssh_config 5 .
191 Because of the potential for abuse, this file must have strict permissions:
192 read/write for the user, and not writable by others.
193+It may be group-writable provided that the group in question contains only
194+the user.
195 .Pp
196 .It Pa ~/.ssh/environment
197 Contains additional definitions for environment variables; see
198diff --git a/ssh_config.5 b/ssh_config.5
199index 3ceb800ba..190e1d927 100644
200--- a/ssh_config.5
201+++ b/ssh_config.5
202@@ -2010,6 +2010,8 @@ The format of this file is described above.
203 This file is used by the SSH client.
204 Because of the potential for abuse, this file must have strict permissions:
205 read/write for the user, and not writable by others.
206+It may be group-writable provided that the group in question contains only
207+the user.
208 .It Pa /etc/ssh/ssh_config
209 Systemwide configuration file.
210 This file provides defaults for those