tomcat-11: fix mediator version and license
[oi-userland.git] / components / network / hpn-ssh / patches / 0018-Per-session-xauthfile.patch
blobed5164def32791be7c18ec715d947678096d3e2d
1 This patch is to fix a X11 connection failure when a user's home directory
2 is read-only.
4 Oracle contributed back this fix to the OpenSSH upstream community. For
5 more information, see https://bugzilla.mindrot.org/show_bug.cgi?id=2440
6 In the future, if this fix is accepted by the upsteam in a later release, we
7 will remove this patch when we upgrade to that release.
9 --- hpn-ssh-hpn-18.4.2/session.c.orig
10 +++ hpn-ssh-hpn-18.4.2/session.c
11 @@ -62,6 +62,10 @@
12 #include <unistd.h>
13 #include <limits.h>
15 +#ifdef PER_SESSION_XAUTHFILE
16 +#include <libgen.h>
17 +#endif
19 #include "openbsd-compat/sys-queue.h"
20 #include "xmalloc.h"
21 #include "ssh.h"
22 @@ -129,6 +133,11 @@
24 static int session_pty_req(struct ssh *, Session *);
26 +#ifdef PER_SESSION_XAUTHFILE
27 +void session_xauthfile_cleanup(Session *);
28 +void cleanup_all_session_xauthfile();
29 +#endif
31 /* import */
32 extern ServerOptions options;
33 extern char *__progname;
34 @@ -1104,6 +1113,11 @@
35 auth_sock_name);
38 +#ifdef PER_SESSION_XAUTHFILE
39 + if (s->auth_file != NULL)
40 + child_set_env(&env, &envsize, "XAUTHORITY", s->auth_file);
41 +#endif
43 /* Set custom environment options from pubkey authentication. */
44 if (options.permit_user_env) {
45 for (n = 0 ; n < auth_opts->nenv; n++) {
46 @@ -2006,6 +2020,11 @@
47 int r, success;
48 u_char single_connection = 0;
50 +#ifdef PER_SESSION_XAUTHFILE
51 + int fd;
52 + char xauthdir[] = "/tmp/ssh-xauth-XXXXXX";
53 +#endif
55 if (s->auth_proto != NULL || s->auth_data != NULL) {
56 error("session_x11_req: session %d: "
57 "x11 forwarding already active", s->self);
58 @@ -2020,19 +2039,82 @@
60 s->single_connection = single_connection;
62 - if (xauth_valid_string(s->auth_proto) &&
63 - xauth_valid_string(s->auth_data))
64 - success = session_setup_x11fwd(ssh, s);
65 - else {
66 + if (!xauth_valid_string(s->auth_proto) ||
67 + !xauth_valid_string(s->auth_data)) {
68 success = 0;
69 error("Invalid X11 forwarding data");
70 + goto out;
73 +#ifdef PER_SESSION_XAUTHFILE
74 + /*
75 + * Create per session X authority file in the /tmp directory.
76 + *
77 + * If mkdtemp() or open() fails then s->auth_file remains NULL which
78 + * means that we won't set XAUTHORITY variable in child's environment
79 + * and xauth(1) will use the default location for the authority file.
80 + */
81 + temporarily_use_uid(s->pw);
82 + if (mkdtemp(xauthdir) != NULL) {
83 + s->auth_file = xmalloc(MAXPATHLEN);
84 + if (snprintf(s->auth_file, MAXPATHLEN, "%s/xauthfile",
85 + xauthdir) >= MAXPATHLEN) {
86 + error("temporary X authority file name was too long "
87 + "for the buffer allocated");
88 + success = 0;
89 + restore_uid();
90 + goto out;
91 + }
92 + /*
93 + * we don't want that "creating new authority file" message to
94 + * be printed by xauth(1) so we must create that file
95 + * beforehand.
96 + */
97 + if ((fd = open(s->auth_file, O_CREAT | O_EXCL | O_RDONLY,
98 + S_IRUSR | S_IWUSR)) == -1) {
99 + error("failed to create the temporary X authority "
100 + "file %s: %.100s; will use the default one",
101 + s->auth_file, strerror(errno));
102 + free(s->auth_file);
103 + s->auth_file = NULL;
104 + if (rmdir(xauthdir) == -1) {
105 + error("cannot remove xauth directory "
106 + "%s: %.100s", xauthdir, strerror(errno));
108 + } else {
109 + if (close(fd) != 0) {
110 + error("close() failed on temporary X authority "
111 + "file: %s", strerror(errno));
112 + success = 0;
113 + restore_uid();
114 + goto out;
116 + debug("temporary X authority file %s created",
117 + s->auth_file);
118 + debug("session number = %d", s->self);
120 + } else {
121 + error("failed to create a directory for the temporary X "
122 + "authority file: %.100s; will use the default xauth file",
123 + strerror(errno));
125 + restore_uid();
126 +#endif
128 + success = session_setup_x11fwd(ssh, s);
130 +out:
131 if (!success) {
132 free(s->auth_proto);
133 free(s->auth_data);
134 s->auth_proto = NULL;
135 s->auth_data = NULL;
136 +#ifdef PER_SESSION_XAUTHFILE
137 + free(s->auth_file);
138 + s->auth_file = NULL;
139 +#endif
142 return success;
145 @@ -2312,6 +2394,51 @@
146 PRIVSEP(session_pty_cleanup2(s));
149 +#ifdef PER_SESSION_XAUTHFILE
151 + * We use a different temporary X authority file per session so we should
152 + * remove those files when cleanup_exit() is called.
153 + */
154 +void
155 +session_xauthfile_cleanup(Session *s)
157 + if (s == NULL || s->auth_file == NULL) {
158 + return;
161 + debug("session_xauthfile_cleanup: session %d removing %s", s->self,
162 + s->auth_file);
164 + if (unlink(s->auth_file) == -1) {
165 + error("session_xauthfile_cleanup: cannot remove xauth file "
166 + "%s: %.100s", s->auth_file, strerror(errno));
167 + return;
170 + /* dirname() will modify s->auth_file but that's ok */
171 + if (rmdir(dirname(s->auth_file)) == -1) {
172 + error("session_xauthfile_cleanup: "
173 + "cannot remove xauth directory %s: %.100s",
174 + s->auth_file, strerror(errno));
175 + return;
177 + free(s->auth_file);
178 + s->auth_file = NULL;
182 + * This is called by do_cleanup() when cleanup_exit() is called.
183 + */
184 +void
185 +cleanup_all_session_xauthfile()
187 + int i;
188 + for (i = 0; i < sessions_nalloc; i++) {
189 + session_xauthfile_cleanup(&sessions[i]);
192 +#endif
194 static char *
195 sig2name(int sig)
197 @@ -2459,6 +2586,9 @@
198 free(s->auth_display);
199 free(s->auth_data);
200 free(s->auth_proto);
201 +#ifdef PER_SESSION_XAUTHFILE
202 + session_xauthfile_cleanup(s);
203 +#endif
204 free(s->subsys);
205 if (s->env != NULL) {
206 for (i = 0; i < s->num_env; i++) {
207 @@ -2714,6 +2844,10 @@
208 auth_info_file = NULL;
211 +#ifdef PER_SESSION_XAUTHFILE
212 + cleanup_all_session_xauthfile();
213 +#endif
216 * Cleanup ptys/utmp only if privsep is disabled,
217 * or if running in monitor.
218 --- hpn-ssh-hpn-18.4.2/session.h.orig
219 +++ hpn-ssh-hpn-18.4.2/session.h
220 @@ -50,6 +50,9 @@
221 char *auth_display;
222 char *auth_proto;
223 char *auth_data;
224 +#ifdef PER_SESSION_XAUTHFILE
225 + char *auth_file; /* xauth(1) authority file */
226 +#endif
227 int single_connection;
229 int chanid;