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