Develop and Download Open Source Software

Browse Subversion Repository

Contents of /ttssh2/trunk/ttxssh/x11util.c

Parent Directory Parent Directory | Revision Log Revision Log


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

SourceForge.JP is a Japanese version of SourceForge.net. For developments that are not related to Japan, we recommend you to use SourceForge.net.