1 | /* -*- c-basic-offset: 8 -*-
|
---|
2 | rdesktop: A Remote Desktop Protocol client.
|
---|
3 | Protocol services - ISO layer
|
---|
4 | Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
|
---|
5 | Copyright 2005-2011 Peter Astrand <astrand@cendio.se> for Cendio AB
|
---|
6 |
|
---|
7 | This program is free software: you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation, either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Oracle 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, Oracle 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, RD_BOOL reconnect)
|
---|
187 | {
|
---|
188 | uint8 code = 0;
|
---|
189 |
|
---|
190 | if (!tcp_connect(server))
|
---|
191 | return False;
|
---|
192 |
|
---|
193 | if (reconnect)
|
---|
194 | {
|
---|
195 | iso_send_msg(ISO_PDU_CR);
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | iso_send_connection_request(username);
|
---|
200 | }
|
---|
201 |
|
---|
202 | if (iso_recv_msg(&code, NULL) == NULL)
|
---|
203 | return False;
|
---|
204 |
|
---|
205 | if (code != ISO_PDU_CC)
|
---|
206 | {
|
---|
207 | error("expected CC, got 0x%x\n", code);
|
---|
208 | tcp_disconnect();
|
---|
209 | return False;
|
---|
210 | }
|
---|
211 |
|
---|
212 | return True;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /* Disconnect from the ISO layer */
|
---|
216 | void
|
---|
217 | iso_disconnect(void)
|
---|
218 | {
|
---|
219 | iso_send_msg(ISO_PDU_DR);
|
---|
220 | tcp_disconnect();
|
---|
221 | }
|
---|
222 |
|
---|
223 | /* reset the state to support reconnecting */
|
---|
224 | void
|
---|
225 | iso_reset_state(void)
|
---|
226 | {
|
---|
227 | tcp_reset_state();
|
---|
228 | }
|
---|