| 1 |
/* |
| 2 |
* Copyright (c) 1998-2001, Robert O'Callahan |
| 3 |
* (C) 2004- TeraTerm Project |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* 3. The name of the author may not be used to endorse or promote products |
| 16 |
* derived from this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 19 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include "x11util.h" |
| 31 |
|
| 32 |
#include <openssl/rand.h> |
| 33 |
#include "util.h" |
| 34 |
#include <stdlib.h> |
| 35 |
|
| 36 |
typedef struct { |
| 37 |
PTInstVar pvar; |
| 38 |
X11AuthData *auth_data; |
| 39 |
|
| 40 |
unsigned char *init_buf; |
| 41 |
int init_buf_len; |
| 42 |
int init_data_len; |
| 43 |
} X11UnspoofingFilterClosure; |
| 44 |
|
| 45 |
void parse_DISPLAY_str(char *name_buf, int name_buf_len, |
| 46 |
int *port, int *screen, char *DISPLAY) |
| 47 |
{ |
| 48 |
strncpy_s(name_buf, name_buf_len, "localhost", _TRUNCATE); |
| 49 |
*port = 6000; |
| 50 |
*screen = 0; |
| 51 |
|
| 52 |
if (DISPLAY != NULL) { |
| 53 |
int i; |
| 54 |
|
| 55 |
for (i = 0; DISPLAY[i] != 0 && DISPLAY[i] != ':'; i++) { |
| 56 |
} |
| 57 |
|
| 58 |
if (i > 0) { |
| 59 |
char c = DISPLAY[i]; |
| 60 |
DISPLAY[i] = 0; |
| 61 |
strncpy_s(name_buf, name_buf_len, DISPLAY, _TRUNCATE); |
| 62 |
DISPLAY[i] = c; |
| 63 |
} |
| 64 |
|
| 65 |
if (DISPLAY[i] == ':') { |
| 66 |
*port = atoi(&DISPLAY[++i]) + 6000; |
| 67 |
while (DISPLAY[i] >= '0' && DISPLAY[i] <= '9') { |
| 68 |
i++; |
| 69 |
} |
| 70 |
if (DISPLAY[i] == '.' && DISPLAY[i+1] != 0) { |
| 71 |
*screen = atoi(&DISPLAY[++i]); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
void X11_get_DISPLAY_info(PTInstVar pvar, char *name_buf, int name_buf_len, |
| 78 |
int *port, int *screen) |
| 79 |
{ |
| 80 |
char *DISPLAY = getenv("DISPLAY"); |
| 81 |
|
| 82 |
if (pvar->settings.X11Display[0] != 0) { |
| 83 |
parse_DISPLAY_str(name_buf, name_buf_len, port, screen, pvar->settings.X11Display); |
| 84 |
} |
| 85 |
else { |
| 86 |
parse_DISPLAY_str(name_buf, name_buf_len, port, screen, DISPLAY); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
X11AuthData *X11_load_local_auth_data(int screen_num) |
| 92 |
{ |
| 93 |
X11AuthData *auth_data = |
| 94 |
(X11AuthData *) malloc(sizeof(X11AuthData)); |
| 95 |
char *local_auth_data_str; |
| 96 |
|
| 97 |
auth_data->local_protocol = getenv("TTSSH_XAUTH_PROTOCOL_NAME"); |
| 98 |
|
| 99 |
local_auth_data_str = getenv("TTSSH_XAUTH_PROTOCOL_DATA"); |
| 100 |
if (local_auth_data_str == NULL) { |
| 101 |
auth_data->local_data_len = 0; |
| 102 |
auth_data->local_data = NULL; |
| 103 |
} else { |
| 104 |
int str_len = strlen(local_auth_data_str); |
| 105 |
int i; |
| 106 |
|
| 107 |
auth_data->local_data_len = (str_len + 1) / 2; |
| 108 |
auth_data->local_data = malloc(auth_data->local_data_len); |
| 109 |
|
| 110 |
if (auth_data->local_data_len * 2 > str_len) { |
| 111 |
char buf[2] = { local_auth_data_str[0], 0 }; |
| 112 |
|
| 113 |
auth_data->local_data[0] = |
| 114 |
(unsigned char) strtol(buf, NULL, 16); |
| 115 |
i = 1; |
| 116 |
} else { |
| 117 |
i = 0; |
| 118 |
} |
| 119 |
|
| 120 |
for (; i < str_len; i += 2) { |
| 121 |
char buf[3] = |
| 122 |
{ local_auth_data_str[i], local_auth_data_str[i + 1], 0 }; |
| 123 |
|
| 124 |
auth_data->local_data[(i + 1) / 2] = |
| 125 |
(unsigned char) strtol(buf, NULL, 16); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
auth_data->spoofed_protocol = _strdup("MIT-MAGIC-COOKIE-1"); |
| 130 |
auth_data->spoofed_data_len = 16; |
| 131 |
auth_data->spoofed_data = malloc(auth_data->spoofed_data_len); |
| 132 |
RAND_bytes(auth_data->spoofed_data, auth_data->spoofed_data_len); |
| 133 |
|
| 134 |
return auth_data; |
| 135 |
} |
| 136 |
|
| 137 |
void X11_dispose_auth_data(X11AuthData *auth_data) |
| 138 |
{ |
| 139 |
SecureZeroMemory(auth_data->local_data, auth_data->local_data_len); |
| 140 |
free(auth_data->local_data); |
| 141 |
free(auth_data->spoofed_protocol); |
| 142 |
SecureZeroMemory(auth_data->spoofed_data, auth_data->spoofed_data_len); |
| 143 |
free(auth_data->spoofed_data); |
| 144 |
free(auth_data); |
| 145 |
} |
| 146 |
|
| 147 |
void *X11_init_unspoofing_filter(PTInstVar pvar, |
| 148 |
X11AuthData *auth_data) |
| 149 |
{ |
| 150 |
X11UnspoofingFilterClosure *closure = |
| 151 |
malloc(sizeof(X11UnspoofingFilterClosure)); |
| 152 |
|
| 153 |
closure->pvar = pvar; |
| 154 |
closure->auth_data = auth_data; |
| 155 |
|
| 156 |
closure->init_data_len = 0; |
| 157 |
buf_create(&closure->init_buf, &closure->init_buf_len); |
| 158 |
|
| 159 |
return closure; |
| 160 |
} |
| 161 |
|
| 162 |
#define MERGE_NEED_MORE 0 |
| 163 |
#define MERGE_GOT_GOOD_DATA 1 |
| 164 |
#define MERGE_GOT_BAD_DATA 2 |
| 165 |
|
| 166 |
static int merge_into_X11_init_packet(X11UnspoofingFilterClosure * |
| 167 |
closure, int length, |
| 168 |
unsigned char *buf) |
| 169 |
{ |
| 170 |
buf_ensure_size_growing(&closure->init_buf, &closure->init_buf_len, |
| 171 |
closure->init_data_len + length); |
| 172 |
memcpy(closure->init_buf + closure->init_data_len, buf, length); |
| 173 |
closure->init_data_len += length; |
| 174 |
|
| 175 |
if (closure->init_data_len < 12) { |
| 176 |
return MERGE_NEED_MORE; |
| 177 |
} else { |
| 178 |
int name_len; |
| 179 |
int data_len; |
| 180 |
int padded_name_len; |
| 181 |
int padded_data_len; |
| 182 |
|
| 183 |
switch (closure->init_buf[0]) { |
| 184 |
case 0x42: /* MSB first */ |
| 185 |
name_len = (closure->init_buf[6] << 8) | closure->init_buf[7]; |
| 186 |
data_len = (closure->init_buf[8] << 8) | closure->init_buf[9]; |
| 187 |
break; |
| 188 |
case 0x6C: /* LSB first */ |
| 189 |
name_len = (closure->init_buf[7] << 8) | closure->init_buf[6]; |
| 190 |
data_len = (closure->init_buf[9] << 8) | closure->init_buf[8]; |
| 191 |
break; |
| 192 |
default: |
| 193 |
return MERGE_GOT_BAD_DATA; |
| 194 |
} |
| 195 |
|
| 196 |
padded_name_len = (name_len + 3) & ~0x3; |
| 197 |
padded_data_len = (data_len + 3) & ~0x3; |
| 198 |
|
| 199 |
if (closure->init_data_len < |
| 200 |
12 + padded_name_len + padded_data_len) { |
| 201 |
return MERGE_NEED_MORE; |
| 202 |
} else if (name_len == |
| 203 |
(int) strlen(closure->auth_data->spoofed_protocol) |
| 204 |
&& memcmp(closure->init_buf + 12, |
| 205 |
closure->auth_data->spoofed_protocol, |
| 206 |
name_len) == 0 |
| 207 |
&& data_len == closure->auth_data->spoofed_data_len |
| 208 |
&& memcmp(closure->init_buf + 12 + padded_name_len, |
| 209 |
closure->auth_data->spoofed_data, |
| 210 |
data_len) == 0) { |
| 211 |
return MERGE_GOT_GOOD_DATA; |
| 212 |
} else { |
| 213 |
return MERGE_GOT_BAD_DATA; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
static void insert_real_X11_auth_data(X11UnspoofingFilterClosure * |
| 219 |
closure, int *length, |
| 220 |
unsigned char **buf) |
| 221 |
{ |
| 222 |
int name_len = closure->auth_data->local_protocol == NULL |
| 223 |
? 0 : strlen(closure->auth_data->local_protocol); |
| 224 |
int data_len = closure->auth_data->local_data_len; |
| 225 |
int padded_name_len = (name_len + 3) & ~0x3; |
| 226 |
int padded_data_len = (data_len + 3) & ~0x3; |
| 227 |
|
| 228 |
*length = 12 + padded_name_len + padded_data_len; |
| 229 |
buf_ensure_size(&closure->init_buf, &closure->init_buf_len, *length); |
| 230 |
*buf = closure->init_buf; |
| 231 |
|
| 232 |
switch (closure->init_buf[0]) { |
| 233 |
case 0x42: /* MSB first */ |
| 234 |
closure->init_buf[6] = name_len >> 8; |
| 235 |
closure->init_buf[7] = name_len & 0xFF; |
| 236 |
closure->init_buf[8] = data_len >> 8; |
| 237 |
closure->init_buf[9] = data_len & 0xFF; |
| 238 |
break; |
| 239 |
case 0x6C: /* LSB first */ |
| 240 |
closure->init_buf[7] = name_len >> 8; |
| 241 |
closure->init_buf[6] = name_len & 0xFF; |
| 242 |
closure->init_buf[9] = data_len >> 8; |
| 243 |
closure->init_buf[8] = data_len & 0xFF; |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
memcpy(*buf + 12, closure->auth_data->local_protocol, name_len); |
| 248 |
memcpy(*buf + 12 + padded_name_len, closure->auth_data->local_data, |
| 249 |
data_len); |
| 250 |
} |
| 251 |
|
| 252 |
FwdFilterResult X11_unspoofing_filter(void *void_closure, FwdFilterEvent event, int *length, unsigned char **buf) |
| 253 |
{ |
| 254 |
X11UnspoofingFilterClosure *closure = |
| 255 |
(X11UnspoofingFilterClosure *) void_closure; |
| 256 |
|
| 257 |
switch (event) { |
| 258 |
case FWD_FILTER_CLEANUP: |
| 259 |
buf_destroy(&closure->init_buf, &closure->init_buf_len); |
| 260 |
free(closure); |
| 261 |
return FWD_FILTER_REMOVE; |
| 262 |
|
| 263 |
case FWD_FILTER_FROM_SERVER: |
| 264 |
switch (merge_into_X11_init_packet(closure, *length, *buf)) { |
| 265 |
case MERGE_NEED_MORE: |
| 266 |
*length = 0; |
| 267 |
return FWD_FILTER_RETAIN; |
| 268 |
case MERGE_GOT_GOOD_DATA: |
| 269 |
insert_real_X11_auth_data(closure, length, buf); |
| 270 |
return FWD_FILTER_REMOVE; |
| 271 |
default: |
| 272 |
case MERGE_GOT_BAD_DATA: |
| 273 |
UTIL_get_lang_msg("MSG_X_AUTH_ERROR", closure->pvar, |
| 274 |
"Remote X application sent incorrect authentication data.\n" |
| 275 |
"Its X session is being cancelled."); |
| 276 |
notify_nonfatal_error(closure->pvar, closure->pvar->UIMsg); |
| 277 |
*length = 0; |
| 278 |
return FWD_FILTER_CLOSECHANNEL; |
| 279 |
} |
| 280 |
break; |
| 281 |
} |
| 282 |
|
| 283 |
return FWD_FILTER_RETAIN; |
| 284 |
} |