1 /*
2    rdesktop: A Remote Desktop Protocol client.
3    Master include file
4    Copyright (C) Matthew Chapman 1999-2005
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 along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 
21 #ifndef WIN32_LEAN_AND_MEAN
22 #define WIN32_LEAN_AND_MEAN
23 #endif
24 
25 #include <windows.h>
26 #include <winsock2.h>
27 #include <cchannel.h>
28 
29 #if 0
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <dirent.h>
34 #include <sys/time.h>
35 #ifdef HAVE_SYS_SELECT_H
36 #include <sys/select.h>
37 #else
38 #include <sys/types.h>
39 #include <unistd.h>
40 #endif
41 #include <limits.h>		/* PATH_MAX */
42 
43 /* FIXME FIXME */
44 #include <windows.h>
45 #include <X11/Xlib.h>
46 #include <X11/Xatom.h>
47 /* FIXME FIXME */
48 #endif
49 
50 // TODO
51 #include <openssl/rc4.h>
52 #include <openssl/md5.h>
53 #include <openssl/sha.h>
54 #include <openssl/bn.h>
55 #include <openssl/x509v3.h>
56 
57 #define VERSION "1.4.1"
58 
59 #ifdef WITH_DEBUG
60 #define DEBUG(args)	printf args;
61 #else
62 #define DEBUG(args)
63 #endif
64 
65 #ifdef WITH_DEBUG_KBD
66 #define DEBUG_KBD(args) printf args;
67 #else
68 #define DEBUG_KBD(args)
69 #endif
70 
71 #ifdef WITH_DEBUG_RDP5
72 #define DEBUG_RDP5(args) printf args;
73 #else
74 #define DEBUG_RDP5(args)
75 #endif
76 
77 #ifdef WITH_DEBUG_CLIPBOARD
78 #define DEBUG_CLIPBOARD(args) printf args;
79 #else
80 #define DEBUG_CLIPBOARD(args)
81 #endif
82 
83 #ifdef WITH_DEBUG_CHANNEL
84 #define DEBUG_CHANNEL(args) printf args;
85 #else
86 #define DEBUG_CHANNEL(args)
87 #endif
88 
89 #define STRNCPY(dst,src,n)	{ strncpy(dst,src,n-1); dst[n-1] = 0; }
90 
91 #ifndef MIN
92 #define MIN(x,y)		(((x) < (y)) ? (x) : (y))
93 #endif
94 
95 #ifndef MAX
96 #define MAX(x,y)		(((x) > (y)) ? (x) : (y))
97 #endif
98 
99 /* timeval macros */
100 #ifndef timerisset
101 #define timerisset(tvp)\
102          ((tvp)->tv_sec || (tvp)->tv_usec)
103 #endif
104 #ifndef timercmp
105 #define timercmp(tvp, uvp, cmp)\
106         ((tvp)->tv_sec cmp (uvp)->tv_sec ||\
107         (tvp)->tv_sec == (uvp)->tv_sec &&\
108         (tvp)->tv_usec cmp (uvp)->tv_usec)
109 #endif
110 #ifndef timerclear
111 #define timerclear(tvp)\
112         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
113 #endif
114 
115 /* If configure does not define the endianess, try
116    to find it out */
117 #if !defined(L_ENDIAN) && !defined(B_ENDIAN)
118 #if __BYTE_ORDER == __LITTLE_ENDIAN
119 #define L_ENDIAN
120 #elif __BYTE_ORDER == __BIG_ENDIAN
121 #define B_ENDIAN
122 #else
123 #error Unknown endianness. Edit rdesktop.h.
124 #endif
125 #endif /* B_ENDIAN, L_ENDIAN from configure */
126 
127 /* No need for alignment on x86 and amd64 */
128 #if !defined(NEED_ALIGN)
129 #if !(defined(__x86__) || defined(__x86_64__) || \
130       defined(__AMD64__) || defined(_M_IX86) || \
131       defined(__i386__))
132 #define NEED_ALIGN
133 #endif
134 #endif
135 
136 struct rdpclient;
137 typedef struct rdpclient RDPCLIENT;
138 
139 #include "parse.h"
140 #include "constants.h"
141 #include "types.h"
142 #include "orders.h"
143 
144 #if 0
145 /* Used to store incoming io request, until they are ready to be completed */
146 /* using a linked list ensures that they are processed in the right order, */
147 /* if multiple ios are being done on the same fd */
148 struct async_iorequest
149 {
150 	uint32 fd, major, minor, offset, device, id, length, partial_len;
151 	long timeout,		/* Total timeout */
152 	  itv_timeout;		/* Interval timeout (between serial characters) */
153 	uint8 *buffer;
154 	DEVICE_FNS *fns;
155 
156 	struct async_iorequest *next;	/* next element in list */
157 };
158 #endif
159 
160 struct bmpcache_entry
161 {
162 	HBITMAP bitmap;
163 	sint16 previous;
164 	sint16 next;
165 };
166 
167 /* holds the whole state of the RDP client */
168 struct rdpclient
169 {
170 	/* channels.c */
171 	CHANNEL_DEF channel_defs[CHANNEL_MAX_COUNT];
172 	unsigned int num_channels;
173 
174 	/* licence.c */
175 	char * licence_username;
176 	char licence_hostname[MAX_COMPUTERNAME_LENGTH + 1];
177 	BOOL licence_issued;
178 
179 	/* mcs.c */
180 	uint16 mcs_userid;
181 
182 	/* mppc.c */
183 	RDPCOMP mppc_dict;
184 
185 	/* pstcache.c */
186 	int pstcache_fd[8];
187 	int pstcache_Bpp;
188 	BOOL pstcache_enumerated;
189 
190 	/* rdesktop.c */
191 	int disconnect_reason;
192 	unsigned int keylayout;
193 	int keyboard_type;
194 	int keyboard_subtype;
195 	int keyboard_functionkeys;
196 
197 	int width;
198 	int height;
199 	int server_depth;
200 	BOOL bitmap_compression;
201 	BOOL bitmap_cache;
202 	BOOL bitmap_cache_persist_enable;
203 	BOOL bitmap_cache_precache;
204 	BOOL encryption;
205 	BOOL packet_encryption;
206 	BOOL desktop_save;	/* desktop save order */
207 	BOOL polygon_ellipse_orders;	/* polygon / ellipse orders */
208 	BOOL use_rdp5;
209 	BOOL console_session;
210 	uint32 rdp5_performanceflags;
211 
212 	/* Session Directory redirection */
213 	BOOL redirect;
214 	wchar_t * redirect_server;
215 	wchar_t * redirect_domain;
216 	wchar_t * redirect_password;
217 	wchar_t * redirect_username;
218 	char * redirect_cookie;
219 	uint32 redirect_flags;
220 
221 	/* rdp.c */
222 	uint8 *next_packet;
223 	uint32 rdp_shareid;
224 
225 	/* secure.c */
226 	uint16 server_rdp_version;
227 
228 	/* tcp.c */
229 	int tcp_port_rdp;
230 
231 	/* cache.c */
232 	struct cache_
233 	{
234 		struct bmpcache_entry bmpcache[3][0xa00];
235 		HBITMAP volatile_bc[3];
236 
237 		int bmpcache_lru[3];
238 		int bmpcache_mru[3];
239 		int bmpcache_count[3];
240 
241 		FONTGLYPH fontcache[12][256];
242 		DATABLOB textcache[256];
243 		uint8 deskcache[0x38400 * 4];
244 		HCURSOR cursorcache[0x20];
245 	}
246 	cache;
247 
248 	/* licence.c */
249 	struct licence_
250 	{
251 		uint8 key[16];
252 		uint8 sign_key[16];
253 	}
254 	licence;
255 
256 	/* orders.c */
257 	struct orders_
258 	{
259 		RDP_ORDER_STATE order_state;
260 	}
261 	orders;
262 
263 	/* rdp.c */
264 	struct rdp_
265 	{
266 		int current_status;
267 
268 #if WITH_DEBUG
269 		uint32 packetno;
270 #endif
271 
272 #ifdef HAVE_ICONV
273 		BOOL iconv_works;
274 #endif
275 	}
276 	rdp;
277 
278 	/* secure.c */
279 	struct secure_
280 	{
281 		int rc4_key_len;
282 		RC4_KEY rc4_decrypt_key;
283 		RC4_KEY rc4_encrypt_key;
284 		RSA *server_public_key;
285 		uint32 server_public_key_len;
286 
287 		uint8 sign_key[16];
288 		uint8 decrypt_key[16];
289 		uint8 encrypt_key[16];
290 		uint8 decrypt_update_key[16];
291 		uint8 encrypt_update_key[16];
292 		uint8 crypted_random[SEC_MAX_MODULUS_SIZE];
293 
294 		/* These values must be available to reset state - Session Directory */
295 		int encrypt_use_count;
296 		int decrypt_use_count;
297 	}
298 	secure;
299 
300 	/* tcp.c */
301 	struct tcp_
302 	{
303 		SOCKET sock;
304 		struct stream in;
305 		struct stream out;
306 		long connection_timeout;
307 	}
308 	tcp;
309 };
310 
311 #ifndef MAKE_PROTO
312 #include "proto.h"
313 #endif
314