1 |
matty |
10 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Entrypoint and utility functions |
4 |
matty |
30 |
Copyright (C) Matthew Chapman 1999-2001 |
5 |
matty |
10 |
|
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 |
matty |
24 |
#include <stdlib.h> /* malloc realloc free */ |
22 |
matty |
30 |
#include <stdarg.h> /* va_list va_start va_end */ |
23 |
matty |
24 |
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */ |
24 |
|
|
#include <fcntl.h> /* open */ |
25 |
|
|
#include <pwd.h> /* getpwuid */ |
26 |
matthewc |
39 |
#include <limits.h> /* PATH_MAX */ |
27 |
matty |
24 |
#include <sys/stat.h> /* stat */ |
28 |
|
|
#include <sys/time.h> /* gettimeofday */ |
29 |
|
|
#include <sys/times.h> /* times */ |
30 |
matty |
10 |
#include "rdesktop.h" |
31 |
|
|
|
32 |
|
|
char username[16]; |
33 |
|
|
char hostname[16]; |
34 |
matthewc |
38 |
char keymapname[16]; |
35 |
astrand |
66 |
int keylayout = 0x409; /* Defaults to US keyboard layout */ |
36 |
matty |
30 |
int width; |
37 |
|
|
int height; |
38 |
jsorg71 |
58 |
int tcp_port_rdp = TCP_PORT_RDP; |
39 |
matty |
28 |
BOOL bitmap_compression = True; |
40 |
matty |
29 |
BOOL sendmotion = True; |
41 |
matty |
10 |
BOOL orders = True; |
42 |
|
|
BOOL licence = True; |
43 |
matty |
30 |
BOOL encryption = True; |
44 |
matty |
28 |
BOOL desktop_save = True; |
45 |
|
|
BOOL fullscreen = False; |
46 |
astrand |
76 |
BOOL grab_keyboard = True; |
47 |
matty |
10 |
|
48 |
|
|
/* Display usage information */ |
49 |
matty |
25 |
static void |
50 |
|
|
usage(char *program) |
51 |
matty |
10 |
{ |
52 |
matty |
30 |
printf("Usage: %s [options] server\n", program); |
53 |
|
|
printf(" -u: user name\n"); |
54 |
|
|
printf(" -d: domain\n"); |
55 |
|
|
printf(" -s: shell\n"); |
56 |
|
|
printf(" -c: working directory\n"); |
57 |
|
|
printf(" -p: password (autologon)\n"); |
58 |
|
|
printf(" -n: client hostname\n"); |
59 |
astrand |
66 |
printf(" -k: keyboard layout on terminal server (us,sv,gr etc.)\n"); |
60 |
matty |
30 |
printf(" -g: desktop geometry (WxH)\n"); |
61 |
|
|
printf(" -f: full-screen mode\n"); |
62 |
|
|
printf(" -b: force bitmap updates\n"); |
63 |
|
|
printf(" -e: disable encryption (French TS)\n"); |
64 |
|
|
printf(" -m: do not send motion events\n"); |
65 |
jsorg71 |
58 |
printf(" -l: do not request licence\n"); |
66 |
astrand |
79 |
printf(" -t: rdp tcp port\n"); |
67 |
astrand |
76 |
printf(" -K: keep window manager key bindings\n"); |
68 |
matty |
10 |
} |
69 |
|
|
|
70 |
|
|
/* Client program */ |
71 |
matty |
25 |
int |
72 |
|
|
main(int argc, char *argv[]) |
73 |
matty |
10 |
{ |
74 |
matty |
30 |
char fullhostname[64]; |
75 |
matty |
21 |
char domain[16]; |
76 |
|
|
char password[16]; |
77 |
|
|
char shell[32]; |
78 |
|
|
char directory[32]; |
79 |
matty |
10 |
char title[32]; |
80 |
matty |
30 |
struct passwd *pw; |
81 |
|
|
char *server, *p; |
82 |
|
|
uint32 flags; |
83 |
matty |
10 |
int c; |
84 |
|
|
|
85 |
matty |
30 |
printf("rdesktop: A Remote Desktop Protocol client.\n"); |
86 |
astrand |
64 |
printf("Version " VERSION |
87 |
|
|
". Copyright (C) 1999-2001 Matt Chapman.\n"); |
88 |
matty |
30 |
printf("See http://www.rdesktop.org/ for more information.\n\n"); |
89 |
matty |
10 |
|
90 |
matty |
21 |
flags = RDP_LOGON_NORMAL; |
91 |
|
|
domain[0] = password[0] = shell[0] = directory[0] = 0; |
92 |
matthewc |
38 |
strcpy(keymapname, "us"); |
93 |
matty |
21 |
|
94 |
astrand |
76 |
while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:t:fbemlKh?")) != -1) |
95 |
matty |
10 |
{ |
96 |
|
|
switch (c) |
97 |
|
|
{ |
98 |
|
|
case 'u': |
99 |
matty |
30 |
STRNCPY(username, optarg, sizeof(username)); |
100 |
matty |
10 |
break; |
101 |
|
|
|
102 |
matty |
21 |
case 'd': |
103 |
matty |
30 |
STRNCPY(domain, optarg, sizeof(domain)); |
104 |
matty |
21 |
break; |
105 |
|
|
|
106 |
|
|
case 's': |
107 |
matty |
30 |
STRNCPY(shell, optarg, sizeof(shell)); |
108 |
matty |
21 |
break; |
109 |
|
|
|
110 |
|
|
case 'c': |
111 |
matty |
30 |
STRNCPY(directory, optarg, sizeof(directory)); |
112 |
matty |
21 |
break; |
113 |
|
|
|
114 |
matty |
30 |
case 'p': |
115 |
|
|
STRNCPY(password, optarg, sizeof(password)); |
116 |
|
|
flags |= RDP_LOGON_AUTO; |
117 |
|
|
break; |
118 |
|
|
|
119 |
matty |
10 |
case 'n': |
120 |
matty |
30 |
STRNCPY(hostname, optarg, sizeof(hostname)); |
121 |
matty |
10 |
break; |
122 |
|
|
|
123 |
|
|
case 'k': |
124 |
astrand |
64 |
STRNCPY(keymapname, optarg, |
125 |
|
|
sizeof(keymapname)); |
126 |
matty |
10 |
break; |
127 |
|
|
|
128 |
matty |
30 |
case 'g': |
129 |
|
|
width = strtol(optarg, &p, 10); |
130 |
|
|
if (*p == 'x') |
131 |
astrand |
64 |
height = strtol(p + 1, NULL, 10); |
132 |
matty |
30 |
|
133 |
|
|
if ((width == 0) || (height == 0)) |
134 |
|
|
{ |
135 |
|
|
error("invalid geometry\n"); |
136 |
|
|
return 1; |
137 |
|
|
} |
138 |
matty |
10 |
break; |
139 |
|
|
|
140 |
matty |
30 |
case 'f': |
141 |
|
|
fullscreen = True; |
142 |
|
|
break; |
143 |
|
|
|
144 |
matty |
10 |
case 'b': |
145 |
|
|
orders = False; |
146 |
|
|
break; |
147 |
|
|
|
148 |
matty |
30 |
case 'e': |
149 |
|
|
encryption = False; |
150 |
matty |
10 |
break; |
151 |
|
|
|
152 |
matty |
30 |
case 'm': |
153 |
|
|
sendmotion = False; |
154 |
matty |
28 |
break; |
155 |
matty |
29 |
|
156 |
matty |
30 |
case 'l': |
157 |
|
|
licence = False; |
158 |
matty |
28 |
break; |
159 |
matty |
29 |
|
160 |
jsorg71 |
58 |
case 't': |
161 |
|
|
tcp_port_rdp = strtol(optarg, NULL, 10); |
162 |
|
|
break; |
163 |
|
|
|
164 |
astrand |
76 |
case 'K': |
165 |
|
|
grab_keyboard = False; |
166 |
|
|
break; |
167 |
|
|
|
168 |
matty |
28 |
case 'h': |
169 |
matty |
10 |
case '?': |
170 |
|
|
default: |
171 |
|
|
usage(argv[0]); |
172 |
|
|
return 1; |
173 |
|
|
} |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
if (argc - optind < 1) |
177 |
|
|
{ |
178 |
|
|
usage(argv[0]); |
179 |
|
|
return 1; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
server = argv[optind]; |
183 |
|
|
|
184 |
|
|
if (username[0] == 0) |
185 |
|
|
{ |
186 |
|
|
pw = getpwuid(getuid()); |
187 |
|
|
if ((pw == NULL) || (pw->pw_name == NULL)) |
188 |
|
|
{ |
189 |
matty |
30 |
error("could not determine username, use -u\n"); |
190 |
matty |
10 |
return 1; |
191 |
|
|
} |
192 |
|
|
|
193 |
matty |
30 |
STRNCPY(username, pw->pw_name, sizeof(username)); |
194 |
matty |
10 |
} |
195 |
|
|
|
196 |
|
|
if (hostname[0] == 0) |
197 |
|
|
{ |
198 |
matty |
30 |
if (gethostname(fullhostname, sizeof(fullhostname)) == -1) |
199 |
matty |
10 |
{ |
200 |
matty |
30 |
error("could not determine local hostname, use -n\n"); |
201 |
matty |
10 |
return 1; |
202 |
|
|
} |
203 |
matty |
30 |
|
204 |
|
|
p = strchr(fullhostname, '.'); |
205 |
|
|
if (p != NULL) |
206 |
|
|
*p = 0; |
207 |
|
|
|
208 |
|
|
STRNCPY(hostname, fullhostname, sizeof(hostname)); |
209 |
matty |
10 |
} |
210 |
|
|
|
211 |
matty |
30 |
if (!strcmp(password, "-")) |
212 |
|
|
{ |
213 |
|
|
p = getpass("Password: "); |
214 |
|
|
if (p == NULL) |
215 |
|
|
{ |
216 |
|
|
error("failed to read password\n"); |
217 |
|
|
return 0; |
218 |
|
|
} |
219 |
|
|
STRNCPY(password, p, sizeof(password)); |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
if ((width == 0) || (height == 0)) |
223 |
|
|
{ |
224 |
|
|
width = 800; |
225 |
|
|
height = 600; |
226 |
|
|
} |
227 |
|
|
|
228 |
matty |
12 |
strcpy(title, "rdesktop - "); |
229 |
matty |
30 |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
230 |
matty |
12 |
|
231 |
astrand |
66 |
xkeymap_init1(); |
232 |
jsorg71 |
81 |
if(!ui_init()) |
233 |
|
|
return 1; |
234 |
astrand |
66 |
|
235 |
matthewc |
53 |
if (!rdp_connect(server, flags, domain, password, shell, directory)) |
236 |
|
|
return 1; |
237 |
|
|
|
238 |
|
|
printf("Connection successful.\n"); |
239 |
|
|
|
240 |
matty |
10 |
if (ui_create_window(title)) |
241 |
|
|
{ |
242 |
|
|
rdp_main_loop(); |
243 |
|
|
ui_destroy_window(); |
244 |
|
|
} |
245 |
|
|
|
246 |
matthewc |
53 |
printf("Disconnecting...\n"); |
247 |
|
|
rdp_disconnect(); |
248 |
matty |
10 |
return 0; |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
/* Generate a 32-byte random for the secure transport code. */ |
252 |
matty |
25 |
void |
253 |
astrand |
64 |
generate_random(uint8 * random) |
254 |
matty |
10 |
{ |
255 |
|
|
struct stat st; |
256 |
matty |
22 |
struct tms tmsbuf; |
257 |
matty |
24 |
uint32 *r = (uint32 *) random; |
258 |
matty |
10 |
int fd; |
259 |
|
|
|
260 |
|
|
/* If we have a kernel random device, use it. */ |
261 |
matty |
30 |
if (((fd = open("/dev/urandom", O_RDONLY)) != -1) |
262 |
|
|
|| ((fd = open("/dev/random", O_RDONLY)) != -1)) |
263 |
matty |
10 |
{ |
264 |
|
|
read(fd, random, 32); |
265 |
|
|
close(fd); |
266 |
|
|
return; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
/* Otherwise use whatever entropy we can gather - ideas welcome. */ |
270 |
|
|
r[0] = (getpid()) | (getppid() << 16); |
271 |
|
|
r[1] = (getuid()) | (getgid() << 16); |
272 |
matty |
24 |
r[2] = times(&tmsbuf); /* system uptime (clocks) */ |
273 |
|
|
gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */ |
274 |
matty |
10 |
stat("/tmp", &st); |
275 |
|
|
r[5] = st.st_atime; |
276 |
|
|
r[6] = st.st_mtime; |
277 |
|
|
r[7] = st.st_ctime; |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
/* malloc; exit if out of memory */ |
281 |
matty |
25 |
void * |
282 |
|
|
xmalloc(int size) |
283 |
matty |
10 |
{ |
284 |
|
|
void *mem = malloc(size); |
285 |
|
|
if (mem == NULL) |
286 |
|
|
{ |
287 |
matty |
30 |
error("xmalloc %d\n", size); |
288 |
matty |
10 |
exit(1); |
289 |
|
|
} |
290 |
|
|
return mem; |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
/* realloc; exit if out of memory */ |
294 |
matty |
25 |
void * |
295 |
|
|
xrealloc(void *oldmem, int size) |
296 |
matty |
10 |
{ |
297 |
|
|
void *mem = realloc(oldmem, size); |
298 |
|
|
if (mem == NULL) |
299 |
|
|
{ |
300 |
matty |
30 |
error("xrealloc %d\n", size); |
301 |
matty |
10 |
exit(1); |
302 |
|
|
} |
303 |
|
|
return mem; |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
/* free */ |
307 |
matty |
25 |
void |
308 |
|
|
xfree(void *mem) |
309 |
matty |
10 |
{ |
310 |
|
|
free(mem); |
311 |
|
|
} |
312 |
|
|
|
313 |
matty |
30 |
/* report an error */ |
314 |
matty |
25 |
void |
315 |
matty |
30 |
error(char *format, ...) |
316 |
|
|
{ |
317 |
|
|
va_list ap; |
318 |
|
|
|
319 |
|
|
fprintf(stderr, "ERROR: "); |
320 |
|
|
|
321 |
|
|
va_start(ap, format); |
322 |
|
|
vfprintf(stderr, format, ap); |
323 |
|
|
va_end(ap); |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
/* report an unimplemented protocol feature */ |
327 |
|
|
void |
328 |
|
|
unimpl(char *format, ...) |
329 |
|
|
{ |
330 |
|
|
va_list ap; |
331 |
|
|
|
332 |
|
|
fprintf(stderr, "NOT IMPLEMENTED: "); |
333 |
|
|
|
334 |
|
|
va_start(ap, format); |
335 |
|
|
vfprintf(stderr, format, ap); |
336 |
|
|
va_end(ap); |
337 |
|
|
} |
338 |
|
|
|
339 |
|
|
/* produce a hex dump */ |
340 |
|
|
void |
341 |
matty |
25 |
hexdump(unsigned char *p, unsigned int len) |
342 |
matty |
10 |
{ |
343 |
|
|
unsigned char *line = p; |
344 |
|
|
unsigned int thisline, offset = 0; |
345 |
|
|
int i; |
346 |
|
|
|
347 |
|
|
while (offset < len) |
348 |
|
|
{ |
349 |
matty |
30 |
printf("%04x ", offset); |
350 |
matty |
10 |
thisline = len - offset; |
351 |
|
|
if (thisline > 16) |
352 |
|
|
thisline = 16; |
353 |
|
|
|
354 |
|
|
for (i = 0; i < thisline; i++) |
355 |
matty |
30 |
printf("%02x ", line[i]); |
356 |
matty |
10 |
|
357 |
matty |
30 |
for (; i < 16; i++) |
358 |
astrand |
64 |
printf(" "); |
359 |
matty |
30 |
|
360 |
matty |
10 |
for (i = 0; i < thisline; i++) |
361 |
matty |
30 |
printf("%c", |
362 |
matty |
24 |
(line[i] >= 0x20 |
363 |
|
|
&& line[i] < 0x7f) ? line[i] : '.'); |
364 |
matty |
10 |
|
365 |
matty |
30 |
printf("\n"); |
366 |
matty |
10 |
offset += thisline; |
367 |
|
|
line += thisline; |
368 |
|
|
} |
369 |
|
|
} |
370 |
matthewc |
39 |
|
371 |
|
|
int |
372 |
|
|
load_licence(unsigned char **data) |
373 |
|
|
{ |
374 |
|
|
char path[PATH_MAX]; |
375 |
|
|
char *home; |
376 |
|
|
struct stat st; |
377 |
|
|
int fd; |
378 |
|
|
|
379 |
|
|
home = getenv("HOME"); |
380 |
|
|
if (home == NULL) |
381 |
|
|
return -1; |
382 |
|
|
|
383 |
|
|
STRNCPY(path, home, sizeof(path)); |
384 |
astrand |
64 |
strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1); |
385 |
matthewc |
39 |
|
386 |
|
|
fd = open(path, O_RDONLY); |
387 |
|
|
if (fd == -1) |
388 |
|
|
return -1; |
389 |
|
|
|
390 |
|
|
if (fstat(fd, &st)) |
391 |
|
|
return -1; |
392 |
|
|
|
393 |
|
|
*data = xmalloc(st.st_size); |
394 |
|
|
return read(fd, *data, st.st_size); |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
void |
398 |
|
|
save_licence(unsigned char *data, int length) |
399 |
|
|
{ |
400 |
|
|
char path[PATH_MAX]; |
401 |
|
|
char *home; |
402 |
|
|
int fd; |
403 |
|
|
|
404 |
|
|
home = getenv("HOME"); |
405 |
|
|
if (home == NULL) |
406 |
|
|
return; |
407 |
|
|
|
408 |
|
|
STRNCPY(path, home, sizeof(path)); |
409 |
astrand |
64 |
strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1); |
410 |
matthewc |
39 |
mkdir(path, 0700); |
411 |
|
|
|
412 |
astrand |
64 |
strncat(path, "/licence", sizeof(path) - strlen(path) - 1); |
413 |
matthewc |
39 |
|
414 |
astrand |
64 |
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
415 |
matthewc |
39 |
if (fd == -1) |
416 |
|
|
{ |
417 |
|
|
perror("open"); |
418 |
|
|
return; |
419 |
|
|
} |
420 |
|
|
|
421 |
|
|
write(fd, data, length); |
422 |
|
|
close(fd); |
423 |
|
|
} |