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