1 |
/* |
/* -*- c-basic-offset: 8 -*- |
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; /* If width or height are reset to zero, the geometry will |
int width = 800; /* If width or height are reset to zero, the geometry will |
49 |
be fetched from _NET_WORKAREA */ |
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; |
59 |
BOOL fullscreen = False; |
BOOL fullscreen = False; |
60 |
BOOL grab_keyboard = True; |
BOOL grab_keyboard = True; |
61 |
BOOL hide_decorations = False; |
BOOL hide_decorations = False; |
62 |
|
BOOL use_rdp5 = False; |
63 |
extern BOOL owncolmap; |
extern BOOL owncolmap; |
64 |
|
|
65 |
|
#ifdef RDP2VNC |
66 |
|
extern int rfb_port; |
67 |
|
extern int defer_time; |
68 |
|
void |
69 |
|
rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password, |
70 |
|
char *shell, char *directory); |
71 |
|
#endif |
72 |
/* Display usage information */ |
/* Display usage information */ |
73 |
static void |
static void |
74 |
usage(char *program) |
usage(char *program) |
75 |
{ |
{ |
76 |
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n"); |
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n"); |
77 |
fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n"); |
fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n"); |
78 |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
79 |
|
|
80 |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
81 |
|
#ifdef RDP2VNC |
82 |
|
fprintf(stderr, " -V: vnc port\n"); |
83 |
|
fprintf(stderr, " -E: defer time (ms)\n"); |
84 |
|
#endif |
85 |
fprintf(stderr, " -u: user name\n"); |
fprintf(stderr, " -u: user name\n"); |
86 |
fprintf(stderr, " -d: domain\n"); |
fprintf(stderr, " -d: domain\n"); |
87 |
fprintf(stderr, " -s: shell\n"); |
fprintf(stderr, " -s: shell\n"); |
88 |
|
fprintf(stderr, " -S: caption button size (single application mode)\n"); |
89 |
fprintf(stderr, " -c: working directory\n"); |
fprintf(stderr, " -c: working directory\n"); |
90 |
fprintf(stderr, " -p: password (- to prompt)\n"); |
fprintf(stderr, " -p: password (- to prompt)\n"); |
91 |
fprintf(stderr, " -n: client hostname\n"); |
fprintf(stderr, " -n: client hostname\n"); |
99 |
fprintf(stderr, " -K: keep window manager key bindings\n"); |
fprintf(stderr, " -K: keep window manager key bindings\n"); |
100 |
fprintf(stderr, " -T: window title\n"); |
fprintf(stderr, " -T: window title\n"); |
101 |
fprintf(stderr, " -D: hide window manager decorations\n"); |
fprintf(stderr, " -D: hide window manager decorations\n"); |
102 |
|
fprintf(stderr, " -a: server bpp\n"); |
103 |
|
fprintf(stderr, " -5: Use RDP5 (EXPERIMENTAL!)\n"); |
104 |
} |
} |
105 |
|
|
106 |
static BOOL |
static BOOL |
154 |
uint32 flags; |
uint32 flags; |
155 |
char *p; |
char *p; |
156 |
int c; |
int c; |
157 |
|
int username_option = 0; |
158 |
|
|
159 |
flags = RDP_LOGON_NORMAL; |
flags = RDP_LOGON_NORMAL; |
160 |
prompt_password = False; |
prompt_password = False; |
161 |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
162 |
strcpy(keymapname, "en-us"); |
strcpy(keymapname, "en-us"); |
163 |
|
|
164 |
while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemCKT:Dh?")) != -1) |
#ifdef RDP2VNC |
165 |
|
#define VNCOPT "V:E:" |
166 |
|
#else |
167 |
|
#define VNCOPT |
168 |
|
#endif |
169 |
|
|
170 |
|
while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbemCKT:Dh?54")) != -1) |
171 |
{ |
{ |
172 |
switch (c) |
switch (c) |
173 |
{ |
{ |
174 |
|
#ifdef RDP2VNC |
175 |
|
case 'V': |
176 |
|
rfb_port = strtol(optarg, NULL, 10); |
177 |
|
if (rfb_port < 100) |
178 |
|
rfb_port += 5900; |
179 |
|
break; |
180 |
|
|
181 |
|
case 'E': |
182 |
|
defer_time = strtol(optarg, NULL, 10); |
183 |
|
if (defer_time < 0) |
184 |
|
defer_time = 0; |
185 |
|
break; |
186 |
|
#endif |
187 |
|
|
188 |
case 'u': |
case 'u': |
189 |
STRNCPY(username, optarg, sizeof(username)); |
STRNCPY(username, optarg, sizeof(username)); |
190 |
|
username_option = 1; |
191 |
break; |
break; |
192 |
|
|
193 |
case 'd': |
case 'd': |
198 |
STRNCPY(shell, optarg, sizeof(shell)); |
STRNCPY(shell, optarg, sizeof(shell)); |
199 |
break; |
break; |
200 |
|
|
201 |
|
case 'S': |
202 |
|
if (!strcmp(optarg, "standard")) |
203 |
|
{ |
204 |
|
win_button_size = 18; |
205 |
|
break; |
206 |
|
} |
207 |
|
|
208 |
|
win_button_size = strtol(optarg, &p, 10); |
209 |
|
|
210 |
|
if (*p) |
211 |
|
{ |
212 |
|
error("invalid button size\n"); |
213 |
|
return 1; |
214 |
|
} |
215 |
|
|
216 |
|
break; |
217 |
|
|
218 |
case 'c': |
case 'c': |
219 |
STRNCPY(directory, optarg, sizeof(directory)); |
STRNCPY(directory, optarg, sizeof(directory)); |
220 |
break; |
break; |
293 |
hide_decorations = True; |
hide_decorations = True; |
294 |
break; |
break; |
295 |
|
|
296 |
|
case 'a': |
297 |
|
server_bpp = strtol(optarg, NULL, 10); |
298 |
|
if (server_bpp != 8 && server_bpp != 16 && server_bpp != 15 |
299 |
|
&& server_bpp != 24) |
300 |
|
{ |
301 |
|
error("invalid server bpp\n"); |
302 |
|
return 1; |
303 |
|
} |
304 |
|
break; |
305 |
|
|
306 |
|
case '5': |
307 |
|
use_rdp5 = True; |
308 |
|
break; |
309 |
case 'h': |
case 'h': |
310 |
case '?': |
case '?': |
311 |
default: |
default: |
328 |
*p = 0; |
*p = 0; |
329 |
} |
} |
330 |
|
|
331 |
if (username[0] == 0) |
if (!username_option) |
332 |
{ |
{ |
333 |
pw = getpwuid(getuid()); |
pw = getpwuid(getuid()); |
334 |
if ((pw == NULL) || (pw->pw_name == NULL)) |
if ((pw == NULL) || (pw->pw_name == NULL)) |
364 |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
365 |
} |
} |
366 |
|
|
367 |
|
#ifdef RDP2VNC |
368 |
|
rdp2vnc_connect(server, flags, domain, password, shell, directory); |
369 |
|
#else |
370 |
|
|
371 |
if (!ui_init()) |
if (!ui_init()) |
372 |
return 1; |
return 1; |
373 |
|
|
386 |
DEBUG(("Disconnecting...\n")); |
DEBUG(("Disconnecting...\n")); |
387 |
rdp_disconnect(); |
rdp_disconnect(); |
388 |
ui_deinit(); |
ui_deinit(); |
389 |
|
|
390 |
|
#endif |
391 |
|
|
392 |
return 0; |
return 0; |
393 |
} |
} |
394 |
|
|
521 |
va_end(ap); |
va_end(ap); |
522 |
} |
} |
523 |
|
|
524 |
|
/* report a warning */ |
525 |
|
void |
526 |
|
warning(char *format, ...) |
527 |
|
{ |
528 |
|
va_list ap; |
529 |
|
|
530 |
|
fprintf(stderr, "WARNING: "); |
531 |
|
|
532 |
|
va_start(ap, format); |
533 |
|
vfprintf(stderr, format, ap); |
534 |
|
va_end(ap); |
535 |
|
} |
536 |
|
|
537 |
/* report an unimplemented protocol feature */ |
/* report an unimplemented protocol feature */ |
538 |
void |
void |
539 |
unimpl(char *format, ...) |
unimpl(char *format, ...) |
577 |
} |
} |
578 |
} |
} |
579 |
|
|
580 |
#ifdef SAVE_LICENCE |
|
581 |
int |
int |
582 |
load_licence(unsigned char **data) |
load_licence(unsigned char **data) |
583 |
{ |
{ |
584 |
char path[PATH_MAX]; |
char *path; |
585 |
char *home; |
char *home; |
586 |
struct stat st; |
struct stat st; |
587 |
int fd; |
int fd; |
590 |
if (home == NULL) |
if (home == NULL) |
591 |
return -1; |
return -1; |
592 |
|
|
593 |
STRNCPY(path, home, sizeof(path)); |
path = xmalloc(strlen(home) + strlen(hostname) + 20); |
594 |
strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1); |
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); |
595 |
|
|
596 |
fd = open(path, O_RDONLY); |
fd = open(path, O_RDONLY); |
597 |
if (fd == -1) |
if (fd == -1) |
607 |
void |
void |
608 |
save_licence(unsigned char *data, int length) |
save_licence(unsigned char *data, int length) |
609 |
{ |
{ |
610 |
char path[PATH_MAX]; |
char *fpath; /* file path for licence */ |
611 |
|
char *fname, *fnamewrk; /* file name for licence .inkl path. */ |
612 |
char *home; |
char *home; |
613 |
int fd; |
uint32 y; |
614 |
|
struct flock fnfl; |
615 |
|
int fnfd, fnwrkfd, i, wlen; |
616 |
|
struct stream s, *s_ptr; |
617 |
|
uint32 len; |
618 |
|
|
619 |
|
/* Construct a stream, so that we can use macros to extract the |
620 |
|
* licence. |
621 |
|
*/ |
622 |
|
s_ptr = &s; |
623 |
|
s_ptr->p = data; |
624 |
|
/* Skip first two bytes */ |
625 |
|
in_uint16_le(s_ptr, len); |
626 |
|
|
627 |
|
/* Skip three strings */ |
628 |
|
for (i = 0; i < 3; i++) |
629 |
|
{ |
630 |
|
in_uint32_le(s_ptr, len); |
631 |
|
s_ptr->p += len; |
632 |
|
/* Make sure that we won't be past the end of data after |
633 |
|
* reading the next length value |
634 |
|
*/ |
635 |
|
if ((s_ptr->p) + 4 > data + length) |
636 |
|
{ |
637 |
|
printf("Error in parsing licence key.\n"); |
638 |
|
printf("Strings %d end value %x > supplied length (%x)\n", i, |
639 |
|
(unsigned int) s_ptr->p, (unsigned int) data + length); |
640 |
|
return; |
641 |
|
} |
642 |
|
} |
643 |
|
in_uint32_le(s_ptr, len); |
644 |
|
if (s_ptr->p + len > data + length) |
645 |
|
{ |
646 |
|
printf("Error in parsing licence key.\n"); |
647 |
|
printf("End of licence %x > supplied length (%x)\n", |
648 |
|
(unsigned int) s_ptr->p + len, (unsigned int) data + length); |
649 |
|
return; |
650 |
|
} |
651 |
|
|
652 |
home = getenv("HOME"); |
home = getenv("HOME"); |
653 |
if (home == NULL) |
if (home == NULL) |
654 |
return; |
return; |
655 |
|
|
656 |
STRNCPY(path, home, sizeof(path)); |
/* set and create the directory -- if it doesn't exist. */ |
657 |
strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1); |
fpath = xmalloc(strlen(home) + 11); |
658 |
mkdir(path, 0700); |
STRNCPY(fpath, home, strlen(home) + 1); |
659 |
|
|
660 |
strncat(path, "/licence", sizeof(path) - strlen(path) - 1); |
sprintf(fpath, "%s/.rdesktop", fpath); |
661 |
|
if (mkdir(fpath, 0700) == -1 && errno != EEXIST) |
662 |
|
{ |
663 |
|
perror("mkdir"); |
664 |
|
exit(1); |
665 |
|
} |
666 |
|
|
667 |
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
/* set the real licence filename, and put a write lock on it. */ |
668 |
if (fd == -1) |
fname = xmalloc(strlen(fpath) + strlen(hostname) + 10); |
669 |
|
sprintf(fname, "%s/licence.%s", fpath, hostname); |
670 |
|
fnfd = open(fname, O_RDONLY); |
671 |
|
if (fnfd != -1) |
672 |
|
{ |
673 |
|
fnfl.l_type = F_WRLCK; |
674 |
|
fnfl.l_whence = SEEK_SET; |
675 |
|
fnfl.l_start = 0; |
676 |
|
fnfl.l_len = 1; |
677 |
|
fcntl(fnfd, F_SETLK, &fnfl); |
678 |
|
} |
679 |
|
|
680 |
|
/* create a temporary licence file */ |
681 |
|
fnamewrk = xmalloc(strlen(fname) + 12); |
682 |
|
for (y = 0;; y++) |
683 |
|
{ |
684 |
|
sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int) y); |
685 |
|
fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600); |
686 |
|
if (fnwrkfd == -1) |
687 |
|
{ |
688 |
|
if (errno == EINTR || errno == EEXIST) |
689 |
|
continue; |
690 |
|
perror("create"); |
691 |
|
exit(1); |
692 |
|
} |
693 |
|
break; |
694 |
|
} |
695 |
|
/* write to the licence file */ |
696 |
|
for (y = 0; y < len;) |
697 |
{ |
{ |
698 |
perror("open"); |
do |
699 |
return; |
{ |
700 |
|
wlen = write(fnwrkfd, s_ptr->p + y, len - y); |
701 |
|
} |
702 |
|
while (wlen == -1 && errno == EINTR); |
703 |
|
if (wlen < 1) |
704 |
|
{ |
705 |
|
perror("write"); |
706 |
|
unlink(fnamewrk); |
707 |
|
exit(1); |
708 |
|
} |
709 |
|
y += wlen; |
710 |
|
} |
711 |
|
|
712 |
|
/* close the file and rename it to fname */ |
713 |
|
if (close(fnwrkfd) == -1) |
714 |
|
{ |
715 |
|
perror("close"); |
716 |
|
unlink(fnamewrk); |
717 |
|
exit(1); |
718 |
|
} |
719 |
|
if (rename(fnamewrk, fname) == -1) |
720 |
|
{ |
721 |
|
perror("rename"); |
722 |
|
unlink(fnamewrk); |
723 |
|
exit(1); |
724 |
|
} |
725 |
|
/* close the file lock on fname */ |
726 |
|
if (fnfd != -1) |
727 |
|
{ |
728 |
|
fnfl.l_type = F_UNLCK; |
729 |
|
fnfl.l_whence = SEEK_SET; |
730 |
|
fnfl.l_start = 0; |
731 |
|
fnfl.l_len = 1; |
732 |
|
fcntl(fnfd, F_SETLK, &fnfl); |
733 |
|
close(fnfd); |
734 |
} |
} |
735 |
|
|
|
write(fd, data, length); |
|
|
close(fd); |
|
736 |
} |
} |
|
#endif |
|