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