]> git.sesse.net Git - rdpsrv/blob - rdp.c
Debug message cleanup.
[rdpsrv] / rdp.c
1 /* -*- c-basic-offset: 8 -*-
2    rdesktop: A Remote Desktop Protocol client.
3    Protocol services - RDP layer
4    Copyright (C) Matthew Chapman 1999-2002
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 #include <time.h>
22 #include "rdesktop.h"
23
24 extern uint16 g_mcs_userid;
25 extern char g_username[16];
26 extern BOOL g_bitmap_compression;
27 extern BOOL g_orders;
28 BOOL g_encryption = 0;
29 extern BOOL g_desktop_save;
30 extern BOOL g_use_rdp5;
31 extern uint16 g_server_rdp_version;
32 extern int g_server_bpp;
33
34 uint8 *g_next_packet;
35 uint32 g_rdp_shareid;
36
37 #if WITH_DEBUG
38 static uint32 g_packetno;
39 #endif
40
41 /* Receive an RDP packet */
42 STREAM
43 rdp_recv(uint8 * type)
44 {
45         static STREAM rdp_s;
46         uint16 length, pdu_type;
47
48         if ((rdp_s == NULL) || (g_next_packet >= rdp_s->end))
49         {
50                 rdp_s = sec_recv();
51                 if (rdp_s == NULL)
52                         return NULL;
53
54                 g_next_packet = rdp_s->p;
55         }
56         else
57         {
58                 rdp_s->p = g_next_packet;
59         }
60
61         in_uint16_le(rdp_s, length);
62         /* 32k packets are really 8, keepalive fix */
63         if (length == 0x8000)
64         {
65                 g_next_packet += 8;
66                 *type = 0;
67                 return rdp_s;
68         }
69         in_uint16_le(rdp_s, pdu_type);
70         in_uint8s(rdp_s, 2);    /* userid */
71         *type = pdu_type & 0xf;
72
73 #if WITH_DEBUG
74         DEBUG(("RDP packet #%d, (type %x, length %u)\n", ++g_packetno, *type, length));
75         hexdump(g_next_packet, length);
76 #endif /*  */
77
78         g_next_packet += length;
79         return rdp_s;
80 }
81
82 /* Initialise an RDP data packet */
83 STREAM
84 rdp_init_data(int maxlen)
85 {
86         STREAM s;
87
88         s = sec_init(g_encryption ? SEC_ENCRYPT : 0, maxlen + 18);
89         s_push_layer(s, rdp_hdr, 18);
90
91         return s;
92 }
93
94 /* Send an RDP data packet */
95 void
96 rdp_send_data(STREAM s, uint8 data_pdu_type)
97 {
98         uint16 length;
99
100         s_pop_layer(s, rdp_hdr);
101         length = s->end - s->p;
102
103         out_uint16_le(s, length);
104         out_uint16_le(s, (RDP_PDU_DATA | 0x10));
105         out_uint16_le(s, (g_mcs_userid + 1001));
106
107         out_uint32_le(s, g_rdp_shareid);
108         out_uint8(s, 0);        /* pad */
109         out_uint8(s, 1);        /* streamid */
110         out_uint16_le(s, (length - 14));
111         out_uint8(s, data_pdu_type);
112         out_uint8(s, 0);        /* compress_type */
113         out_uint16(s, 0);       /* compress_len */
114
115         sec_send(s, g_encryption ? SEC_ENCRYPT : 0);
116 }
117
118 /* Output a string in Unicode */
119 void
120 rdp_out_unistr(STREAM s, char *string, int len)
121 {
122         int i = 0, j = 0;
123
124         len += 2;
125
126         while (i < len)
127         {
128                 s->p[i++] = string[j++];
129                 s->p[i++] = 0;
130         }
131
132         s->p += len;
133 }
134
135 /* Input a string in Unicode (uuuuuuugly, use iconv later) */
136 void
137 rdp_in_unistr(STREAM s, char *string, int len)
138 {
139         int i = 0, j = 0;
140         len += 2;
141
142         while (i < len)
143         {
144                 string[j++] = s->p[i];
145                 i += 2;
146         }
147         
148         s->p += len;
149 }
150
151 void rdp_send_bitmap_update(unsigned x, unsigned y, unsigned width, unsigned height, unsigned char *data)
152 {
153         STREAM s;
154
155         int length = 10*2 + width*height*3 + 5;
156
157         s = rdp5_init(length, 1);
158
159         out_uint8(s, 1); // process bitmap update
160         out_uint16_le(s, 10*2 + width*height*3 + 2); // RDP5 chunk length
161         out_uint16_le(s, 10*2 + width*height*3);     // part length
162
163         out_uint16_le(s, 1); // one update
164         out_uint16_le(s, x); // left, top, right, bottom
165         out_uint16_le(s, y);
166         out_uint16_le(s, x+width);
167         out_uint16_le(s, y+height);
168         out_uint16_le(s, width); // width, height
169         out_uint16_le(s, height);
170         out_uint16_le(s, 24); // bpp
171         out_uint16_le(s, 0); // no compression
172         out_uint16_le(s, width*height*3); // bufsize
173
174         out_uint8p(s, data, width*height*3);
175         
176         s_mark_end(s);
177         rdp5_send(s, 1);
178 }
179
180 #define EXPECT16(value) { in_uint16_le(s, unknown); if (unknown != (value)) printf("Unknown value on code line %u; expected 0x%x, got 0x%x\n", __LINE__, (value), unknown); }
181 #define EXPECT32(value) { in_uint32_le(s, unknown); if (unknown != (value)) printf("Unknown value on code line %u; expected 0x%x, got 0x%x\n", __LINE__, (value), unknown); }
182
183 void
184 rdp_get_logon_info(STREAM s)
185 {
186         uint32 flags, unknown;
187         uint16 tzone_major, tzone_minor;
188         int len_domain, len_user, len_password, len_program, len_directory, len_ip, len_dll;
189         char domain[256], user[256], password[256], program[256], directory[256], ip[256], dll[256], tz1[256], tz2[256]; // FIXME
190         
191         in_uint32_le(s, unknown);
192         in_uint32_le(s, flags);
193         
194         printf("logon flags: %x\n", flags);
195
196         if (flags & RDP_LOGON_BLOB) { 
197                 printf("RDP5-style logon packet\n");
198
199                 in_uint16_le(s, len_domain);
200                 in_uint16_le(s, len_user);
201
202                 if (flags & RDP_LOGON_AUTO) {
203                         in_uint16_le(s, len_password);
204                 } else {
205                         strcpy(password, "<none>");
206                 }
207                 if (flags & RDP_LOGON_BLOB && !(flags & RDP_LOGON_AUTO)) {
208                         // is this too some kind of length?
209                         EXPECT16(0);
210                 }
211                 in_uint16_le(s, len_program);
212                 in_uint16_le(s, len_directory);
213                 
214                 rdp_in_unistr(s, domain, len_domain);
215                 rdp_in_unistr(s, user, len_user);
216
217                 if (flags & RDP_LOGON_AUTO)
218                         rdp_in_unistr(s, password, len_password);
219                 
220                 if (flags & RDP_LOGON_BLOB && !(flags & RDP_LOGON_AUTO))
221                         EXPECT16(0);
222                         
223                 rdp_in_unistr(s, program, len_program);
224                 rdp_in_unistr(s, directory, len_directory);
225
226                 EXPECT16(2);
227
228                 in_uint16_le(s, len_ip);
229                 rdp_in_unistr(s, ip, len_ip - 2);
230
231                 in_uint16_le(s, len_dll);
232                 rdp_in_unistr(s, dll, len_dll - 2);
233
234                 in_uint16_le(s, tzone_major);
235                 in_uint16_le(s, tzone_minor);
236
237                 // time zone names?
238                 rdp_in_unistr(s, tz1, 62);
239                 EXPECT32(0x0a0000);
240                 EXPECT32(0x050000);
241                 EXPECT32(3);
242                 EXPECT32(0);
243                 EXPECT32(0);
244
245                 rdp_in_unistr(s, tz2, 62);
246                 EXPECT32(0x030000);
247                 EXPECT32(0x050000);
248                 EXPECT32(2);
249                 EXPECT32(0);
250                 EXPECT32(0xffffffc4);
251                 EXPECT32(0xfffffffe);
252                 EXPECT32(0x0f);
253                 EXPECT32(0);
254
255                 printf("domain='%s'\n", domain);
256                 printf("user='%s'\n", user);
257                 printf("password='%s'\n", password);
258                 printf("program='%s'\n", program);
259                 printf("directory='%s'\n", directory);
260                 printf("ip='%s'\n", ip);
261                 printf("dll='%s'\n", dll);
262                 printf("timezone=%d:%02d\n", tzone_major, tzone_minor);
263                 printf("tz1='%64s'\n", tz1);
264                 printf("tz2='%64s'\n", tz2);
265         } else {
266                 in_uint16_le(s, len_domain);
267                 in_uint16_le(s, len_user);
268                 in_uint16_le(s, len_password);
269                 in_uint16_le(s, len_program);
270                 in_uint16_le(s, len_directory);
271                 rdp_in_unistr(s, domain, len_domain);
272                 rdp_in_unistr(s, user, len_user);
273                 rdp_in_unistr(s, password, len_password);
274                 rdp_in_unistr(s, program, len_program);
275                 rdp_in_unistr(s, directory, len_directory);
276
277                 printf("domain='%s'\n", domain);
278                 printf("user='%s'\n", user);
279                 printf("password='%s'\n", password);
280                 printf("program='%s'\n", program);
281                 printf("directory='%s'\n", directory);
282         }
283
284         if (!s_check_end(s)) {
285                 printf("Unknown data at end of logon packet!\n");
286         }
287 }
288
289 /* Send a control PDU */
290 static void
291 rdp_send_control(uint16 action)
292 {
293         STREAM s;
294
295         s = rdp_init_data(8);
296
297         out_uint16_le(s, action);
298         out_uint16(s, 0);       /* userid */
299         out_uint32(s, 0);       /* control id */
300
301         s_mark_end(s);
302         rdp_send_data(s, RDP_DATA_PDU_CONTROL);
303 }
304
305 /* Send a synchronisation PDU */
306 static void
307 rdp_send_synchronise(void)
308 {
309         STREAM s;
310
311         s = rdp_init_data(4);
312
313         out_uint16_le(s, 1);    /* type */
314         out_uint16_le(s, 1002);
315
316         s_mark_end(s);
317         rdp_send_data(s, RDP_DATA_PDU_SYNCHRONISE);
318 }
319
320 /* Receive a single input event */
321 void
322 rdp_recv_input(STREAM s, uint32 *time, uint16 *message_type, uint16 *device_flags, uint16 *param1, uint16 *param2)
323 {
324         in_uint32_le(s, *time);
325         in_uint16_le(s, *message_type);
326         in_uint16_le(s, *device_flags);
327         in_uint16_le(s, *param1);
328         in_uint16_le(s, *param2);
329 }
330
331 /* Disconnect from the RDP layer */
332 void
333 rdp_disconnect(void)
334 {
335         sec_disconnect();
336 }