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