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