1 | /* -*- c-basic-offset: 8 -*-
|
---|
2 | rdesktop: A Remote Desktop Protocol client.
|
---|
3 | Protocol services - ISO layer
|
---|
4 | Copyright (C) Matthew Chapman 1999-2007
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Sun GPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
23 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
24 | * the General Public License version 2 (GPLv2) at this time for any software where
|
---|
25 | * a choice of GPL license versions is made available with the language indicating
|
---|
26 | * that GPLv2 or any later version may be used, or where a choice of which version
|
---|
27 | * of the GPL is applied is otherwise unspecified.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include "rdesktop.h"
|
---|
31 |
|
---|
32 | /* Send a self-contained ISO PDU */
|
---|
33 | static void
|
---|
34 | iso_send_msg(uint8 code)
|
---|
35 | {
|
---|
36 | STREAM s;
|
---|
37 |
|
---|
38 | s = tcp_init(11);
|
---|
39 |
|
---|
40 | out_uint8(s, 3); /* version */
|
---|
41 | out_uint8(s, 0); /* reserved */
|
---|
42 | out_uint16_be(s, 11); /* length */
|
---|
43 |
|
---|
44 | out_uint8(s, 6); /* hdrlen */
|
---|
45 | out_uint8(s, code);
|
---|
46 | out_uint16(s, 0); /* dst_ref */
|
---|
47 | out_uint16(s, 0); /* src_ref */
|
---|
48 | out_uint8(s, 0); /* class */
|
---|
49 |
|
---|
50 | s_mark_end(s);
|
---|
51 | tcp_send(s);
|
---|
52 | }
|
---|
53 |
|
---|
54 | static void
|
---|
55 | iso_send_connection_request(char *username)
|
---|
56 | {
|
---|
57 | STREAM s;
|
---|
58 | int length = 30 + strlen(username);
|
---|
59 |
|
---|
60 | s = tcp_init(length);
|
---|
61 |
|
---|
62 | out_uint8(s, 3); /* version */
|
---|
63 | out_uint8(s, 0); /* reserved */
|
---|
64 | out_uint16_be(s, length); /* length */
|
---|
65 |
|
---|
66 | out_uint8(s, length - 5); /* hdrlen */
|
---|
67 | out_uint8(s, ISO_PDU_CR);
|
---|
68 | out_uint16(s, 0); /* dst_ref */
|
---|
69 | out_uint16(s, 0); /* src_ref */
|
---|
70 | out_uint8(s, 0); /* class */
|
---|
71 |
|
---|
72 | out_uint8p(s, "Cookie: mstshash=", strlen("Cookie: mstshash="));
|
---|
73 | out_uint8p(s, username, strlen(username));
|
---|
74 |
|
---|
75 | out_uint8(s, 0x0d); /* Unknown */
|
---|
76 | out_uint8(s, 0x0a); /* Unknown */
|
---|
77 |
|
---|
78 | s_mark_end(s);
|
---|
79 | tcp_send(s);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* Receive a message on the ISO layer, return code */
|
---|
83 | static STREAM
|
---|
84 | iso_recv_msg(uint8 * code, uint8 * rdpver)
|
---|
85 | {
|
---|
86 | STREAM s;
|
---|
87 | uint16 length;
|
---|
88 | uint8 version;
|
---|
89 |
|
---|
90 | s = tcp_recv(NULL, 4);
|
---|
91 | if (s == NULL)
|
---|
92 | return NULL;
|
---|
93 | in_uint8(s, version);
|
---|
94 | if (rdpver != NULL)
|
---|
95 | *rdpver = version;
|
---|
96 | if (version == 3)
|
---|
97 | {
|
---|
98 | in_uint8s(s, 1); /* pad */
|
---|
99 | in_uint16_be(s, length);
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | in_uint8(s, length);
|
---|
104 | if (length & 0x80)
|
---|
105 | {
|
---|
106 | length &= ~0x80;
|
---|
107 | next_be(s, length);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | if (length < 4)
|
---|
111 | {
|
---|
112 | error("Bad packet header\n");
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 | s = tcp_recv(s, length - 4);
|
---|
116 | if (s == NULL)
|
---|
117 | return NULL;
|
---|
118 | if (version != 3)
|
---|
119 | return s;
|
---|
120 | in_uint8s(s, 1); /* hdrlen */
|
---|
121 | in_uint8(s, *code);
|
---|
122 | if (*code == ISO_PDU_DT)
|
---|
123 | {
|
---|
124 | in_uint8s(s, 1); /* eot */
|
---|
125 | return s;
|
---|
126 | }
|
---|
127 | in_uint8s(s, 5); /* dst_ref, src_ref, class */
|
---|
128 | return s;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* Initialise ISO transport data packet */
|
---|
132 | STREAM
|
---|
133 | iso_init(int length)
|
---|
134 | {
|
---|
135 | STREAM s;
|
---|
136 |
|
---|
137 | s = tcp_init(length + 7);
|
---|
138 | s_push_layer(s, iso_hdr, 7);
|
---|
139 |
|
---|
140 | return s;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* Send an ISO data PDU */
|
---|
144 | void
|
---|
145 | iso_send(STREAM s)
|
---|
146 | {
|
---|
147 | uint16 length;
|
---|
148 |
|
---|
149 | s_pop_layer(s, iso_hdr);
|
---|
150 | length = s->end - s->p;
|
---|
151 |
|
---|
152 | out_uint8(s, 3); /* version */
|
---|
153 | out_uint8(s, 0); /* reserved */
|
---|
154 | out_uint16_be(s, length);
|
---|
155 |
|
---|
156 | out_uint8(s, 2); /* hdrlen */
|
---|
157 | out_uint8(s, ISO_PDU_DT); /* code */
|
---|
158 | out_uint8(s, 0x80); /* eot */
|
---|
159 |
|
---|
160 | tcp_send(s);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /* Receive ISO transport data packet */
|
---|
164 | STREAM
|
---|
165 | iso_recv(uint8 * rdpver)
|
---|
166 | {
|
---|
167 | STREAM s;
|
---|
168 | uint8 code = 0;
|
---|
169 |
|
---|
170 | s = iso_recv_msg(&code, rdpver);
|
---|
171 | if (s == NULL)
|
---|
172 | return NULL;
|
---|
173 | if (rdpver != NULL)
|
---|
174 | if (*rdpver != 3)
|
---|
175 | return s;
|
---|
176 | if (code != ISO_PDU_DT)
|
---|
177 | {
|
---|
178 | error("expected DT, got 0x%x\n", code);
|
---|
179 | return NULL;
|
---|
180 | }
|
---|
181 | return s;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /* Establish a connection up to the ISO layer */
|
---|
185 | RD_BOOL
|
---|
186 | iso_connect(char *server, char *username)
|
---|
187 | {
|
---|
188 | uint8 code = 0;
|
---|
189 |
|
---|
190 | if (!tcp_connect(server))
|
---|
191 | return False;
|
---|
192 |
|
---|
193 | iso_send_connection_request(username);
|
---|
194 |
|
---|
195 | if (iso_recv_msg(&code, NULL) == NULL)
|
---|
196 | return False;
|
---|
197 |
|
---|
198 | if (code != ISO_PDU_CC)
|
---|
199 | {
|
---|
200 | error("expected CC, got 0x%x\n", code);
|
---|
201 | tcp_disconnect();
|
---|
202 | return False;
|
---|
203 | }
|
---|
204 |
|
---|
205 | return True;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /* Establish a reconnection up to the ISO layer */
|
---|
209 | RD_BOOL
|
---|
210 | iso_reconnect(char *server)
|
---|
211 | {
|
---|
212 | uint8 code = 0;
|
---|
213 |
|
---|
214 | if (!tcp_connect(server))
|
---|
215 | return False;
|
---|
216 |
|
---|
217 | iso_send_msg(ISO_PDU_CR);
|
---|
218 |
|
---|
219 | if (iso_recv_msg(&code, NULL) == NULL)
|
---|
220 | return False;
|
---|
221 |
|
---|
222 | if (code != ISO_PDU_CC)
|
---|
223 | {
|
---|
224 | error("expected CC, got 0x%x\n", code);
|
---|
225 | tcp_disconnect();
|
---|
226 | return False;
|
---|
227 | }
|
---|
228 |
|
---|
229 | return True;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* Disconnect from the ISO layer */
|
---|
233 | void
|
---|
234 | iso_disconnect(void)
|
---|
235 | {
|
---|
236 | iso_send_msg(ISO_PDU_DR);
|
---|
237 | tcp_disconnect();
|
---|
238 | }
|
---|
239 |
|
---|
240 | /* reset the state to support reconnecting */
|
---|
241 | void
|
---|
242 | iso_reset_state(void)
|
---|
243 | {
|
---|
244 | tcp_reset_state();
|
---|
245 | }
|
---|