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