]> git.sesse.net Git - rdpsrv/blob - rdp.c
eafd3d0da7fae6e2506951723366e64fcd9c8355
[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 static 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 static 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
152 rdp_get_logon_info(STREAM s)
153 {
154         uint32 flags;
155         int len_domain, len_user, len_password, len_program, len_directory;
156         char domain[256], user[256], password[256], program[256], directory[256]; // FIXME
157         
158         in_uint32_le(s, flags);  // unknown
159         in_uint32_le(s, flags);
160
161         if (flags & RDP_LOGON_BLOB) 
162                 error("got RDP5-style logon packet, can't handle this yet");
163
164         printf("logon flags: %x\n", flags);
165
166         in_uint16_le(s, len_domain);
167         in_uint16_le(s, len_user);
168         in_uint16_le(s, len_password);
169         in_uint16_le(s, len_program);
170         in_uint16_le(s, len_directory);
171         rdp_in_unistr(s, domain, len_domain);
172         rdp_in_unistr(s, user, len_user);
173         rdp_in_unistr(s, password, len_password);
174         rdp_in_unistr(s, program, len_program);
175         rdp_in_unistr(s, directory, len_directory);
176
177         printf("domain='%s'\n", domain);
178         printf("user='%s'\n", user);
179         printf("password='%s'\n", password);
180         printf("program='%s'\n", program);
181         printf("directory='%s'\n", directory);
182
183         if (!s_check_end(s))
184                 error("Unknown data at end of logon packet!");
185 }
186
187 /* Send a control PDU */
188 static void
189 rdp_send_control(uint16 action)
190 {
191         STREAM s;
192
193         s = rdp_init_data(8);
194
195         out_uint16_le(s, action);
196         out_uint16(s, 0);       /* userid */
197         out_uint32(s, 0);       /* control id */
198
199         s_mark_end(s);
200         rdp_send_data(s, RDP_DATA_PDU_CONTROL);
201 }
202
203 /* Send a synchronisation PDU */
204 static void
205 rdp_send_synchronise(void)
206 {
207         STREAM s;
208
209         s = rdp_init_data(4);
210
211         out_uint16_le(s, 1);    /* type */
212         out_uint16_le(s, 1002);
213
214         s_mark_end(s);
215         rdp_send_data(s, RDP_DATA_PDU_SYNCHRONISE);
216 }
217
218 /* Send a single input event */
219 void
220 rdp_send_input(uint32 time, uint16 message_type, uint16 device_flags, uint16 param1, uint16 param2)
221 {
222         STREAM s;
223
224         s = rdp_init_data(16);
225
226         out_uint16_le(s, 1);    /* number of events */
227         out_uint16(s, 0);       /* pad */
228
229         out_uint32_le(s, time);
230         out_uint16_le(s, message_type);
231         out_uint16_le(s, device_flags);
232         out_uint16_le(s, param1);
233         out_uint16_le(s, param2);
234
235         s_mark_end(s);
236         rdp_send_data(s, RDP_DATA_PDU_INPUT);
237 }
238
239 /* Disconnect from the RDP layer */
240 void
241 rdp_disconnect(void)
242 {
243         sec_disconnect();
244 }