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-2002 |
Copyright (C) Matthew Chapman 1999-2003 |
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 |
22 |
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */ |
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */ |
23 |
#include <fcntl.h> /* open */ |
#include <fcntl.h> /* open */ |
24 |
#include <pwd.h> /* getpwuid */ |
#include <pwd.h> /* getpwuid */ |
|
#include <limits.h> /* PATH_MAX */ |
|
25 |
#include <termios.h> /* tcgetattr tcsetattr */ |
#include <termios.h> /* tcgetattr tcsetattr */ |
26 |
#include <sys/stat.h> /* stat */ |
#include <sys/stat.h> /* stat */ |
27 |
#include <sys/time.h> /* gettimeofday */ |
#include <sys/time.h> /* gettimeofday */ |
28 |
#include <sys/times.h> /* times */ |
#include <sys/times.h> /* times */ |
29 |
|
#include <errno.h> |
30 |
#include "rdesktop.h" |
#include "rdesktop.h" |
31 |
|
|
32 |
#ifdef EGD_SOCKET |
#ifdef EGD_SOCKET |
45 |
char hostname[16]; |
char hostname[16]; |
46 |
char keymapname[16]; |
char keymapname[16]; |
47 |
int keylayout = 0x409; /* Defaults to US keyboard layout */ |
int keylayout = 0x409; /* Defaults to US keyboard layout */ |
48 |
int width = 800; |
int width = 800; /* If width or height are reset to zero, the geometry will |
49 |
|
be fetched from _NET_WORKAREA */ |
50 |
int height = 600; |
int height = 600; |
51 |
int tcp_port_rdp = TCP_PORT_RDP; |
int tcp_port_rdp = TCP_PORT_RDP; |
52 |
|
int server_bpp = 8; |
53 |
|
int win_button_size = 0; /* If zero, disable single app mode */ |
54 |
BOOL bitmap_compression = True; |
BOOL bitmap_compression = True; |
55 |
BOOL sendmotion = True; |
BOOL sendmotion = True; |
56 |
BOOL orders = True; |
BOOL orders = True; |
58 |
BOOL desktop_save = True; |
BOOL desktop_save = True; |
59 |
BOOL fullscreen = False; |
BOOL fullscreen = False; |
60 |
BOOL grab_keyboard = True; |
BOOL grab_keyboard = True; |
61 |
|
BOOL hide_decorations = False; |
62 |
|
extern BOOL owncolmap; |
63 |
|
|
64 |
|
#ifdef RDP2VNC |
65 |
|
extern int rfb_port; |
66 |
|
extern int defer_time; |
67 |
|
void |
68 |
|
rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password, |
69 |
|
char *shell, char *directory); |
70 |
|
#endif |
71 |
/* Display usage information */ |
/* Display usage information */ |
72 |
static void |
static void |
73 |
usage(char *program) |
usage(char *program) |
74 |
{ |
{ |
75 |
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n"); |
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n"); |
76 |
fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n"); |
fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n"); |
77 |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
78 |
|
|
79 |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
80 |
|
#ifdef RDP2VNC |
81 |
|
fprintf(stderr, " -V: vnc port\n"); |
82 |
|
fprintf(stderr, " -E: defer time (ms)\n"); |
83 |
|
#endif |
84 |
fprintf(stderr, " -u: user name\n"); |
fprintf(stderr, " -u: user name\n"); |
85 |
fprintf(stderr, " -d: domain\n"); |
fprintf(stderr, " -d: domain\n"); |
86 |
fprintf(stderr, " -s: shell\n"); |
fprintf(stderr, " -s: shell\n"); |
87 |
|
fprintf(stderr, " -S: caption button size (single application mode)\n"); |
88 |
fprintf(stderr, " -c: working directory\n"); |
fprintf(stderr, " -c: working directory\n"); |
89 |
fprintf(stderr, " -p: password (- to prompt)\n"); |
fprintf(stderr, " -p: password (- to prompt)\n"); |
90 |
fprintf(stderr, " -n: client hostname\n"); |
fprintf(stderr, " -n: client hostname\n"); |
94 |
fprintf(stderr, " -b: force bitmap updates\n"); |
fprintf(stderr, " -b: force bitmap updates\n"); |
95 |
fprintf(stderr, " -e: disable encryption (French TS)\n"); |
fprintf(stderr, " -e: disable encryption (French TS)\n"); |
96 |
fprintf(stderr, " -m: do not send motion events\n"); |
fprintf(stderr, " -m: do not send motion events\n"); |
97 |
|
fprintf(stderr, " -C: use private colour map\n"); |
98 |
fprintf(stderr, " -K: keep window manager key bindings\n"); |
fprintf(stderr, " -K: keep window manager key bindings\n"); |
99 |
fprintf(stderr, " -T: window title\n"); |
fprintf(stderr, " -T: window title\n"); |
100 |
|
fprintf(stderr, " -D: hide window manager decorations\n"); |
101 |
|
fprintf(stderr, " -a: server bpp\n"); |
102 |
} |
} |
103 |
|
|
104 |
static BOOL |
static BOOL |
152 |
uint32 flags; |
uint32 flags; |
153 |
char *p; |
char *p; |
154 |
int c; |
int c; |
155 |
|
int username_option = 0; |
156 |
|
|
157 |
flags = RDP_LOGON_NORMAL; |
flags = RDP_LOGON_NORMAL; |
158 |
prompt_password = False; |
prompt_password = False; |
159 |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
160 |
strcpy(keymapname, "us"); |
strcpy(keymapname, "en-us"); |
161 |
|
|
162 |
|
#ifdef RDP2VNC |
163 |
|
#define VNCOPT "V:E:" |
164 |
|
#else |
165 |
|
#define VNCOPT |
166 |
|
#endif |
167 |
|
|
168 |
while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemKT:h?")) != -1) |
while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbemCKT:Dh?")) != -1) |
169 |
{ |
{ |
170 |
switch (c) |
switch (c) |
171 |
{ |
{ |
172 |
|
#ifdef RDP2VNC |
173 |
|
case 'V': |
174 |
|
rfb_port = strtol(optarg, NULL, 10); |
175 |
|
if (rfb_port < 100) |
176 |
|
rfb_port += 5900; |
177 |
|
break; |
178 |
|
|
179 |
|
case 'E': |
180 |
|
defer_time = strtol(optarg, NULL, 10); |
181 |
|
if (defer_time < 0) |
182 |
|
defer_time = 0; |
183 |
|
break; |
184 |
|
#endif |
185 |
|
|
186 |
case 'u': |
case 'u': |
187 |
STRNCPY(username, optarg, sizeof(username)); |
STRNCPY(username, optarg, sizeof(username)); |
188 |
|
username_option = 1; |
189 |
break; |
break; |
190 |
|
|
191 |
case 'd': |
case 'd': |
196 |
STRNCPY(shell, optarg, sizeof(shell)); |
STRNCPY(shell, optarg, sizeof(shell)); |
197 |
break; |
break; |
198 |
|
|
199 |
|
case 'S': |
200 |
|
if (!strcmp(optarg, "standard")) |
201 |
|
{ |
202 |
|
win_button_size = 18; |
203 |
|
break; |
204 |
|
} |
205 |
|
|
206 |
|
win_button_size = strtol(optarg, &p, 10); |
207 |
|
|
208 |
|
if (*p) |
209 |
|
{ |
210 |
|
error("invalid button size\n"); |
211 |
|
return 1; |
212 |
|
} |
213 |
|
|
214 |
|
break; |
215 |
|
|
216 |
case 'c': |
case 'c': |
217 |
STRNCPY(directory, optarg, sizeof(directory)); |
STRNCPY(directory, optarg, sizeof(directory)); |
218 |
break; |
break; |
242 |
break; |
break; |
243 |
|
|
244 |
case 'g': |
case 'g': |
245 |
|
if (!strcmp(optarg, "workarea")) |
246 |
|
{ |
247 |
|
width = height = 0; |
248 |
|
break; |
249 |
|
} |
250 |
|
|
251 |
width = strtol(optarg, &p, 10); |
width = strtol(optarg, &p, 10); |
252 |
if (*p == 'x') |
if (*p == 'x') |
253 |
height = strtol(p + 1, NULL, 10); |
height = strtol(p + 1, NULL, 10); |
275 |
sendmotion = False; |
sendmotion = False; |
276 |
break; |
break; |
277 |
|
|
278 |
|
case 'C': |
279 |
|
owncolmap = True; |
280 |
|
break; |
281 |
|
|
282 |
case 'K': |
case 'K': |
283 |
grab_keyboard = False; |
grab_keyboard = False; |
284 |
break; |
break; |
287 |
STRNCPY(title, optarg, sizeof(title)); |
STRNCPY(title, optarg, sizeof(title)); |
288 |
break; |
break; |
289 |
|
|
290 |
|
case 'D': |
291 |
|
hide_decorations = True; |
292 |
|
break; |
293 |
|
|
294 |
|
case 'a': |
295 |
|
server_bpp = strtol(optarg, NULL, 10); |
296 |
|
if (server_bpp != 8 && server_bpp != 16 && server_bpp != 15 |
297 |
|
&& server_bpp != 24) |
298 |
|
{ |
299 |
|
error("invalid server bpp\n"); |
300 |
|
return 1; |
301 |
|
} |
302 |
|
break; |
303 |
|
|
304 |
case 'h': |
case 'h': |
305 |
case '?': |
case '?': |
306 |
default: |
default: |
323 |
*p = 0; |
*p = 0; |
324 |
} |
} |
325 |
|
|
326 |
if (username[0] == 0) |
if (!username_option) |
327 |
{ |
{ |
328 |
pw = getpwuid(getuid()); |
pw = getpwuid(getuid()); |
329 |
if ((pw == NULL) || (pw->pw_name == NULL)) |
if ((pw == NULL) || (pw->pw_name == NULL)) |
359 |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
360 |
} |
} |
361 |
|
|
362 |
|
#ifdef RDP2VNC |
363 |
|
rdp2vnc_connect(server, flags, domain, password, shell, directory); |
364 |
|
#else |
365 |
|
|
366 |
if (!ui_init()) |
if (!ui_init()) |
367 |
return 1; |
return 1; |
368 |
|
|
381 |
DEBUG(("Disconnecting...\n")); |
DEBUG(("Disconnecting...\n")); |
382 |
rdp_disconnect(); |
rdp_disconnect(); |
383 |
ui_deinit(); |
ui_deinit(); |
384 |
|
|
385 |
|
#endif |
386 |
|
|
387 |
return 0; |
return 0; |
388 |
} |
} |
389 |
|
|
402 |
|
|
403 |
addr.sun_family = AF_UNIX; |
addr.sun_family = AF_UNIX; |
404 |
memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET)); |
memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET)); |
405 |
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) |
if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) |
406 |
goto err; |
goto err; |
407 |
|
|
408 |
/* PRNGD and EGD use a simple communications protocol */ |
/* PRNGD and EGD use a simple communications protocol */ |
409 |
buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */ |
buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */ |
410 |
buf[1] = 32; /* Number of requested random bytes */ |
buf[1] = 32; /* Number of requested random bytes */ |
411 |
if (write(fd, buf, 2) != 2) |
if (write(fd, buf, 2) != 2) |
412 |
goto err; |
goto err; |
413 |
|
|
414 |
if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */ |
if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */ |
415 |
goto err; |
goto err; |
416 |
|
|
417 |
if (read(fd, buf, 32) != 32) |
if (read(fd, buf, 32) != 32) |
419 |
|
|
420 |
ret = True; |
ret = True; |
421 |
|
|
422 |
err: |
err: |
423 |
close(fd); |
close(fd); |
424 |
return ret; |
return ret; |
425 |
} |
} |
452 |
#endif |
#endif |
453 |
|
|
454 |
/* Otherwise use whatever entropy we can gather - ideas welcome. */ |
/* Otherwise use whatever entropy we can gather - ideas welcome. */ |
455 |
r = (uint32 *)random; |
r = (uint32 *) random; |
456 |
r[0] = (getpid()) | (getppid() << 16); |
r[0] = (getpid()) | (getppid() << 16); |
457 |
r[1] = (getuid()) | (getgid() << 16); |
r[1] = (getuid()) | (getgid() << 16); |
458 |
r[2] = times(&tmsbuf); /* system uptime (clocks) */ |
r[2] = times(&tmsbuf); /* system uptime (clocks) */ |
464 |
|
|
465 |
/* Hash both halves with MD5 to obscure possible patterns */ |
/* Hash both halves with MD5 to obscure possible patterns */ |
466 |
MD5_Init(&md5); |
MD5_Init(&md5); |
467 |
MD5_Update(&md5, random, 16); |
MD5_Update(&md5, random, 16); |
468 |
MD5_Final(random, &md5); |
MD5_Final(random, &md5); |
469 |
MD5_Update(&md5, random+16, 16); |
MD5_Update(&md5, random + 16, 16); |
470 |
MD5_Final(random+16, &md5); |
MD5_Final(random + 16, &md5); |
471 |
} |
} |
472 |
|
|
473 |
/* malloc; exit if out of memory */ |
/* malloc; exit if out of memory */ |
516 |
va_end(ap); |
va_end(ap); |
517 |
} |
} |
518 |
|
|
519 |
|
/* report a warning */ |
520 |
|
void |
521 |
|
warning(char *format, ...) |
522 |
|
{ |
523 |
|
va_list ap; |
524 |
|
|
525 |
|
fprintf(stderr, "WARNING: "); |
526 |
|
|
527 |
|
va_start(ap, format); |
528 |
|
vfprintf(stderr, format, ap); |
529 |
|
va_end(ap); |
530 |
|
} |
531 |
|
|
532 |
/* report an unimplemented protocol feature */ |
/* report an unimplemented protocol feature */ |
533 |
void |
void |
534 |
unimpl(char *format, ...) |
unimpl(char *format, ...) |
572 |
} |
} |
573 |
} |
} |
574 |
|
|
575 |
#ifdef SAVE_LICENCE |
|
576 |
int |
int |
577 |
load_licence(unsigned char **data) |
load_licence(unsigned char **data) |
578 |
{ |
{ |
579 |
char path[PATH_MAX]; |
char *path; |
580 |
char *home; |
char *home; |
581 |
struct stat st; |
struct stat st; |
582 |
int fd; |
int fd; |
585 |
if (home == NULL) |
if (home == NULL) |
586 |
return -1; |
return -1; |
587 |
|
|
588 |
STRNCPY(path, home, sizeof(path)); |
path = xmalloc(strlen(home) + strlen(hostname) + 20); |
589 |
strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1); |
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); |
590 |
|
|
591 |
fd = open(path, O_RDONLY); |
fd = open(path, O_RDONLY); |
592 |
if (fd == -1) |
if (fd == -1) |
602 |
void |
void |
603 |
save_licence(unsigned char *data, int length) |
save_licence(unsigned char *data, int length) |
604 |
{ |
{ |
605 |
char path[PATH_MAX]; |
char *fpath; /* file path for licence */ |
606 |
|
char *fname, *fnamewrk; /* file name for licence .inkl path. */ |
607 |
char *home; |
char *home; |
608 |
int fd; |
uint32 y; |
609 |
|
struct flock fnfl; |
610 |
|
int fnfd, fnwrkfd, i, wlen; |
611 |
|
struct stream s, *s_ptr; |
612 |
|
uint32 len; |
613 |
|
|
614 |
|
/* Construct a stream, so that we can use macros to extract the |
615 |
|
* licence. |
616 |
|
*/ |
617 |
|
s_ptr = &s; |
618 |
|
s_ptr->p = data; |
619 |
|
/* Skip first two bytes */ |
620 |
|
in_uint16(s_ptr, len); |
621 |
|
|
622 |
|
/* Skip three strings */ |
623 |
|
for (i = 0; i < 3; i++) |
624 |
|
{ |
625 |
|
in_uint32(s_ptr, len); |
626 |
|
s_ptr->p += len; |
627 |
|
/* Make sure that we won't be past the end of data after |
628 |
|
* reading the next length value |
629 |
|
*/ |
630 |
|
if ((s_ptr->p) + 4 > data + length) |
631 |
|
{ |
632 |
|
printf("Error in parsing licence key.\n"); |
633 |
|
printf("Strings %d end value %x > supplied length (%x)\n", i, |
634 |
|
(unsigned int) s_ptr->p, (unsigned int) data + length); |
635 |
|
return; |
636 |
|
} |
637 |
|
} |
638 |
|
in_uint32(s_ptr, len); |
639 |
|
if (s_ptr->p + len > data + length) |
640 |
|
{ |
641 |
|
printf("Error in parsing licence key.\n"); |
642 |
|
printf("End of licence %x > supplied length (%x)\n", |
643 |
|
(unsigned int) s_ptr->p + len, (unsigned int) data + length); |
644 |
|
return; |
645 |
|
} |
646 |
|
|
647 |
home = getenv("HOME"); |
home = getenv("HOME"); |
648 |
if (home == NULL) |
if (home == NULL) |
649 |
return; |
return; |
650 |
|
|
651 |
STRNCPY(path, home, sizeof(path)); |
/* set and create the directory -- if it doesn't exist. */ |
652 |
strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1); |
fpath = xmalloc(strlen(home) + 11); |
653 |
mkdir(path, 0700); |
STRNCPY(fpath, home, strlen(home) + 1); |
654 |
|
|
655 |
strncat(path, "/licence", sizeof(path) - strlen(path) - 1); |
sprintf(fpath, "%s/.rdesktop", fpath); |
656 |
|
if (mkdir(fpath, 0700) == -1 && errno != EEXIST) |
657 |
|
{ |
658 |
|
perror("mkdir"); |
659 |
|
exit(1); |
660 |
|
} |
661 |
|
|
662 |
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
/* set the real licence filename, and put a write lock on it. */ |
663 |
if (fd == -1) |
fname = xmalloc(strlen(fpath) + strlen(hostname) + 10); |
664 |
|
sprintf(fname, "%s/licence.%s", fpath, hostname); |
665 |
|
fnfd = open(fname, O_RDONLY); |
666 |
|
if (fnfd != -1) |
667 |
|
{ |
668 |
|
fnfl.l_type = F_WRLCK; |
669 |
|
fnfl.l_whence = SEEK_SET; |
670 |
|
fnfl.l_start = 0; |
671 |
|
fnfl.l_len = 1; |
672 |
|
fcntl(fnfd, F_SETLK, &fnfl); |
673 |
|
} |
674 |
|
|
675 |
|
/* create a temporary licence file */ |
676 |
|
fnamewrk = xmalloc(strlen(fname) + 12); |
677 |
|
for (y = 0;; y++) |
678 |
|
{ |
679 |
|
sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int) y); |
680 |
|
fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600); |
681 |
|
if (fnwrkfd == -1) |
682 |
|
{ |
683 |
|
if (errno == EINTR || errno == EEXIST) |
684 |
|
continue; |
685 |
|
perror("create"); |
686 |
|
exit(1); |
687 |
|
} |
688 |
|
break; |
689 |
|
} |
690 |
|
/* write to the licence file */ |
691 |
|
for (y = 0; y < len;) |
692 |
{ |
{ |
693 |
perror("open"); |
do |
694 |
return; |
{ |
695 |
|
wlen = write(fnwrkfd, s_ptr->p + y, len - y); |
696 |
|
} |
697 |
|
while (wlen == -1 && errno == EINTR); |
698 |
|
if (wlen < 1) |
699 |
|
{ |
700 |
|
perror("write"); |
701 |
|
unlink(fnamewrk); |
702 |
|
exit(1); |
703 |
|
} |
704 |
|
y += wlen; |
705 |
|
} |
706 |
|
|
707 |
|
/* close the file and rename it to fname */ |
708 |
|
if (close(fnwrkfd) == -1) |
709 |
|
{ |
710 |
|
perror("close"); |
711 |
|
unlink(fnamewrk); |
712 |
|
exit(1); |
713 |
|
} |
714 |
|
if (rename(fnamewrk, fname) == -1) |
715 |
|
{ |
716 |
|
perror("rename"); |
717 |
|
unlink(fnamewrk); |
718 |
|
exit(1); |
719 |
|
} |
720 |
|
/* close the file lock on fname */ |
721 |
|
if (fnfd != -1) |
722 |
|
{ |
723 |
|
fnfl.l_type = F_UNLCK; |
724 |
|
fnfl.l_whence = SEEK_SET; |
725 |
|
fnfl.l_start = 0; |
726 |
|
fnfl.l_len = 1; |
727 |
|
fcntl(fnfd, F_SETLK, &fnfl); |
728 |
|
close(fnfd); |
729 |
} |
} |
730 |
|
|
|
write(fd, data, length); |
|
|
close(fd); |
|
731 |
} |
} |
|
#endif |
|