1 |
/* |
/* -*- c-basic-offset: 8 -*- |
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-2003 |
Copyright (C) Matthew Chapman 1999-2003 |
21 |
#include <stdarg.h> /* va_list va_start va_end */ |
#include <stdarg.h> /* va_list va_start va_end */ |
22 |
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */ |
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */ |
23 |
#include <fcntl.h> /* open */ |
#include <fcntl.h> /* open */ |
|
#include <errno.h> /* save licence uses it. */ |
|
24 |
#include <pwd.h> /* getpwuid */ |
#include <pwd.h> /* getpwuid */ |
25 |
#include <termios.h> /* tcgetattr tcsetattr */ |
#include <termios.h> /* tcgetattr tcsetattr */ |
26 |
#include <sys/stat.h> /* stat */ |
#include <sys/stat.h> /* stat */ |
27 |
#include <sys/time.h> /* gettimeofday */ |
#include <sys/time.h> /* gettimeofday */ |
28 |
#include <sys/times.h> /* times */ |
#include <sys/times.h> /* times */ |
29 |
|
#include <ctype.h> /* toupper */ |
30 |
|
#include <errno.h> |
31 |
#include "rdesktop.h" |
#include "rdesktop.h" |
32 |
|
|
33 |
#ifdef EGD_SOCKET |
#ifdef EGD_SOCKET |
41 |
#include "crypto/md5.h" |
#include "crypto/md5.h" |
42 |
#endif |
#endif |
43 |
|
|
44 |
char title[32] = ""; |
char g_title[64] = ""; |
45 |
char username[16]; |
char g_username[64]; |
46 |
char hostname[16]; |
char hostname[16]; |
47 |
char keymapname[16]; |
char keymapname[16]; |
48 |
int keylayout = 0x409; /* Defaults to US keyboard layout */ |
int keylayout = 0x409; /* Defaults to US keyboard layout */ |
49 |
int width = 800; /* If width or height are reset to zero, the geometry will |
|
50 |
be fetched from _NET_WORKAREA */ |
int g_width = 800; /* width is special: If 0, the |
51 |
int height = 600; |
geometry will be fetched from |
52 |
|
_NET_WORKAREA. If negative, |
53 |
|
absolute value specifies the |
54 |
|
percent of the whole screen. */ |
55 |
|
int g_height = 600; |
56 |
int tcp_port_rdp = TCP_PORT_RDP; |
int tcp_port_rdp = TCP_PORT_RDP; |
57 |
int server_bpp = 8; |
int g_server_bpp = 8; |
58 |
BOOL bitmap_compression = True; |
int g_win_button_size = 0; /* If zero, disable single app mode */ |
59 |
BOOL sendmotion = True; |
BOOL g_bitmap_compression = True; |
60 |
BOOL orders = True; |
BOOL g_sendmotion = True; |
61 |
BOOL encryption = True; |
BOOL g_orders = True; |
62 |
BOOL desktop_save = True; |
BOOL g_encryption = True; |
63 |
BOOL fullscreen = False; |
BOOL packet_encryption = True; |
64 |
BOOL grab_keyboard = True; |
BOOL g_desktop_save = True; |
65 |
BOOL hide_decorations = False; |
BOOL g_fullscreen = False; |
66 |
extern BOOL owncolmap; |
BOOL g_grab_keyboard = True; |
67 |
|
BOOL g_hide_decorations = False; |
68 |
|
BOOL g_use_rdp5 = True; |
69 |
|
BOOL g_console_session = False; |
70 |
|
BOOL g_numlock_sync = False; |
71 |
|
extern BOOL g_owncolmap; |
72 |
|
|
73 |
|
#ifdef WITH_RDPSND |
74 |
|
BOOL g_rdpsnd = False; |
75 |
|
#endif |
76 |
|
|
77 |
|
extern RDPDR_DEVICE g_rdpdr_device[]; |
78 |
|
extern uint32 g_num_devices; |
79 |
|
|
80 |
|
#ifdef RDP2VNC |
81 |
|
extern int rfb_port; |
82 |
|
extern int defer_time; |
83 |
|
void |
84 |
|
rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password, |
85 |
|
char *shell, char *directory); |
86 |
|
#endif |
87 |
/* Display usage information */ |
/* Display usage information */ |
88 |
static void |
static void |
89 |
usage(char *program) |
usage(char *program) |
93 |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
94 |
|
|
95 |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
96 |
|
#ifdef RDP2VNC |
97 |
|
fprintf(stderr, " -V: vnc port\n"); |
98 |
|
fprintf(stderr, " -Q: defer time (ms)\n"); |
99 |
|
#endif |
100 |
fprintf(stderr, " -u: user name\n"); |
fprintf(stderr, " -u: user name\n"); |
101 |
fprintf(stderr, " -d: domain\n"); |
fprintf(stderr, " -d: domain\n"); |
102 |
fprintf(stderr, " -s: shell\n"); |
fprintf(stderr, " -s: shell\n"); |
103 |
fprintf(stderr, " -c: working directory\n"); |
fprintf(stderr, " -c: working directory\n"); |
104 |
fprintf(stderr, " -p: password (- to prompt)\n"); |
fprintf(stderr, " -p: password (- to prompt)\n"); |
105 |
fprintf(stderr, " -n: client hostname\n"); |
fprintf(stderr, " -n: client hostname\n"); |
106 |
fprintf(stderr, " -k: keyboard layout on terminal server (us,sv,gr,etc.)\n"); |
fprintf(stderr, " -k: keyboard layout on server (en-us, de, sv, etc.)\n"); |
107 |
fprintf(stderr, " -g: desktop geometry (WxH)\n"); |
fprintf(stderr, " -g: desktop geometry (WxH)\n"); |
108 |
fprintf(stderr, " -f: full-screen mode\n"); |
fprintf(stderr, " -f: full-screen mode\n"); |
109 |
fprintf(stderr, " -b: force bitmap updates\n"); |
fprintf(stderr, " -b: force bitmap updates\n"); |
110 |
fprintf(stderr, " -e: disable encryption (French TS)\n"); |
fprintf(stderr, " -e: disable encryption (French TS)\n"); |
111 |
|
fprintf(stderr, " -E: disable encryption from client to server\n"); |
112 |
fprintf(stderr, " -m: do not send motion events\n"); |
fprintf(stderr, " -m: do not send motion events\n"); |
113 |
fprintf(stderr, " -C: use private colour map\n"); |
fprintf(stderr, " -C: use private colour map\n"); |
114 |
|
fprintf(stderr, " -D: hide window manager decorations\n"); |
115 |
fprintf(stderr, " -K: keep window manager key bindings\n"); |
fprintf(stderr, " -K: keep window manager key bindings\n"); |
116 |
|
fprintf(stderr, " -S: caption button size (single application mode)\n"); |
117 |
fprintf(stderr, " -T: window title\n"); |
fprintf(stderr, " -T: window title\n"); |
118 |
fprintf(stderr, " -D: hide window manager decorations\n"); |
fprintf(stderr, " -N: enable numlock syncronization\n"); |
119 |
fprintf(stderr, " -a: server bpp\n"); |
fprintf(stderr, " -a: connection colour depth\n"); |
120 |
|
fprintf(stderr, " -r: enable specified device redirection (this flag can be repeated)\n"); |
121 |
|
fprintf(stderr, " '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n"); |
122 |
|
fprintf(stderr, " or COM1=/dev/ttyS0,COM2=/dev/ttyS1\n"); |
123 |
|
fprintf(stderr, " '-r disk:A=/mnt/floppy': enable redirection of /mnt/floppy to A:\n"); |
124 |
|
fprintf(stderr, " or A=/mnt/floppy,D=/mnt/cdrom'\n"); |
125 |
|
fprintf(stderr, " '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n"); |
126 |
|
fprintf(stderr, " or LPT1=/dev/lp0,LPT2=/dev/lp1\n"); |
127 |
|
fprintf(stderr, " '-r printer:mydeskjet': enable printer redirection\n"); |
128 |
|
fprintf(stderr, " or mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n"); |
129 |
|
fprintf(stderr, " '-r sound': enable sound redirection\n"); |
130 |
|
fprintf(stderr, " -0: attach to console\n"); |
131 |
|
fprintf(stderr, " -4: use RDP version 4\n"); |
132 |
|
fprintf(stderr, " -5: use RDP version 5 (default)\n"); |
133 |
} |
} |
134 |
|
|
135 |
static BOOL |
static BOOL |
168 |
return ret; |
return ret; |
169 |
} |
} |
170 |
|
|
171 |
|
static void |
172 |
|
parse_server_and_port(char *server) |
173 |
|
{ |
174 |
|
char *p; |
175 |
|
#ifdef IPv6 |
176 |
|
int addr_colons; |
177 |
|
#endif |
178 |
|
|
179 |
|
#ifdef IPv6 |
180 |
|
p = server; |
181 |
|
addr_colons = 0; |
182 |
|
while (*p) |
183 |
|
if (*p++ == ':') |
184 |
|
addr_colons++; |
185 |
|
if (addr_colons >= 2) |
186 |
|
{ |
187 |
|
/* numeric IPv6 style address format - [1:2:3::4]:port */ |
188 |
|
p = strchr(server, ']'); |
189 |
|
if (*server == '[' && p != NULL) |
190 |
|
{ |
191 |
|
if (*(p + 1) == ':' && *(p + 2) != '\0') |
192 |
|
tcp_port_rdp = strtol(p + 2, NULL, 10); |
193 |
|
/* remove the port number and brackets from the address */ |
194 |
|
*p = '\0'; |
195 |
|
strncpy(server, server + 1, strlen(server)); |
196 |
|
} |
197 |
|
} |
198 |
|
else |
199 |
|
{ |
200 |
|
/* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */ |
201 |
|
p = strchr(server, ':'); |
202 |
|
if (p != NULL) |
203 |
|
{ |
204 |
|
tcp_port_rdp = strtol(p + 1, NULL, 10); |
205 |
|
*p = 0; |
206 |
|
} |
207 |
|
} |
208 |
|
#else /* no IPv6 support */ |
209 |
|
p = strchr(server, ':'); |
210 |
|
if (p != NULL) |
211 |
|
{ |
212 |
|
tcp_port_rdp = strtol(p + 1, NULL, 10); |
213 |
|
*p = 0; |
214 |
|
} |
215 |
|
#endif /* IPv6 */ |
216 |
|
|
217 |
|
} |
218 |
|
|
219 |
/* Client program */ |
/* Client program */ |
220 |
int |
int |
221 |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
223 |
char server[64]; |
char server[64]; |
224 |
char fullhostname[64]; |
char fullhostname[64]; |
225 |
char domain[16]; |
char domain[16]; |
226 |
char password[16]; |
char password[64]; |
227 |
char shell[128]; |
char shell[128]; |
228 |
char directory[32]; |
char directory[32]; |
229 |
BOOL prompt_password; |
BOOL prompt_password, rdp_retval = False; |
230 |
struct passwd *pw; |
struct passwd *pw; |
231 |
uint32 flags; |
uint32 flags; |
232 |
char *p; |
char *p; |
233 |
int c; |
int c; |
234 |
|
|
235 |
int username_option = 0; |
int username_option = 0; |
236 |
|
|
237 |
flags = RDP_LOGON_NORMAL; |
flags = RDP_LOGON_NORMAL; |
239 |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
240 |
strcpy(keymapname, "en-us"); |
strcpy(keymapname, "en-us"); |
241 |
|
|
242 |
while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:a:fbemCKT:Dh?")) != -1) |
g_num_devices = 0; |
243 |
|
|
244 |
|
#ifdef RDP2VNC |
245 |
|
#define VNCOPT "V:Q:" |
246 |
|
#else |
247 |
|
#define VNCOPT |
248 |
|
#endif |
249 |
|
|
250 |
|
while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbeEmCDKS:T:Na:r:045h?")) != -1) |
251 |
{ |
{ |
252 |
switch (c) |
switch (c) |
253 |
{ |
{ |
254 |
|
#ifdef RDP2VNC |
255 |
|
case 'V': |
256 |
|
rfb_port = strtol(optarg, NULL, 10); |
257 |
|
if (rfb_port < 100) |
258 |
|
rfb_port += 5900; |
259 |
|
break; |
260 |
|
|
261 |
|
case 'Q': |
262 |
|
defer_time = strtol(optarg, NULL, 10); |
263 |
|
if (defer_time < 0) |
264 |
|
defer_time = 0; |
265 |
|
break; |
266 |
|
#endif |
267 |
|
|
268 |
case 'u': |
case 'u': |
269 |
STRNCPY(username, optarg, sizeof(username)); |
STRNCPY(g_username, optarg, sizeof(g_username)); |
270 |
username_option = 1; |
username_option = 1; |
271 |
break; |
break; |
272 |
|
|
307 |
break; |
break; |
308 |
|
|
309 |
case 'g': |
case 'g': |
310 |
|
g_fullscreen = False; |
311 |
if (!strcmp(optarg, "workarea")) |
if (!strcmp(optarg, "workarea")) |
312 |
{ |
{ |
313 |
width = height = 0; |
g_width = g_height = 0; |
314 |
break; |
break; |
315 |
} |
} |
316 |
|
|
317 |
width = strtol(optarg, &p, 10); |
g_width = strtol(optarg, &p, 10); |
318 |
|
if (g_width <= 0) |
319 |
|
{ |
320 |
|
error("invalid geometry\n"); |
321 |
|
return 1; |
322 |
|
} |
323 |
|
|
324 |
if (*p == 'x') |
if (*p == 'x') |
325 |
height = strtol(p + 1, NULL, 10); |
g_height = strtol(p + 1, NULL, 10); |
326 |
|
|
327 |
if ((width == 0) || (height == 0)) |
if (g_height <= 0) |
328 |
{ |
{ |
329 |
error("invalid geometry\n"); |
error("invalid geometry\n"); |
330 |
return 1; |
return 1; |
331 |
} |
} |
332 |
|
|
333 |
|
if (*p == '%') |
334 |
|
g_width = -g_width; |
335 |
|
|
336 |
break; |
break; |
337 |
|
|
338 |
case 'f': |
case 'f': |
339 |
fullscreen = True; |
g_fullscreen = True; |
340 |
break; |
break; |
341 |
|
|
342 |
case 'b': |
case 'b': |
343 |
orders = False; |
g_orders = False; |
344 |
break; |
break; |
345 |
|
|
346 |
case 'e': |
case 'e': |
347 |
encryption = False; |
g_encryption = False; |
348 |
|
break; |
349 |
|
case 'E': |
350 |
|
packet_encryption = False; |
351 |
break; |
break; |
|
|
|
352 |
case 'm': |
case 'm': |
353 |
sendmotion = False; |
g_sendmotion = False; |
354 |
break; |
break; |
355 |
|
|
356 |
case 'C': |
case 'C': |
357 |
owncolmap = True; |
g_owncolmap = True; |
358 |
|
break; |
359 |
|
|
360 |
|
case 'D': |
361 |
|
g_hide_decorations = True; |
362 |
break; |
break; |
363 |
|
|
364 |
case 'K': |
case 'K': |
365 |
grab_keyboard = False; |
g_grab_keyboard = False; |
366 |
|
break; |
367 |
|
|
368 |
|
case 'S': |
369 |
|
if (!strcmp(optarg, "standard")) |
370 |
|
{ |
371 |
|
g_win_button_size = 18; |
372 |
|
break; |
373 |
|
} |
374 |
|
|
375 |
|
g_win_button_size = strtol(optarg, &p, 10); |
376 |
|
|
377 |
|
if (*p) |
378 |
|
{ |
379 |
|
error("invalid button size\n"); |
380 |
|
return 1; |
381 |
|
} |
382 |
|
|
383 |
break; |
break; |
384 |
|
|
385 |
case 'T': |
case 'T': |
386 |
STRNCPY(title, optarg, sizeof(title)); |
STRNCPY(g_title, optarg, sizeof(g_title)); |
387 |
break; |
break; |
388 |
|
|
389 |
case 'D': |
case 'N': |
390 |
hide_decorations = True; |
g_numlock_sync = True; |
391 |
break; |
break; |
392 |
|
|
393 |
case 'a': |
case 'a': |
394 |
server_bpp = strtol(optarg, NULL, 10); |
g_server_bpp = strtol(optarg, NULL, 10); |
395 |
if (server_bpp != 8 && server_bpp != 16 && server_bpp != 15 && server_bpp != 24) |
if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15 |
396 |
|
&& g_server_bpp != 24) |
397 |
{ |
{ |
398 |
error("invalid server bpp\n"); |
error("invalid server bpp\n"); |
399 |
return 1; |
return 1; |
400 |
} |
} |
401 |
break; |
break; |
402 |
|
|
403 |
|
case 'r': |
404 |
|
|
405 |
|
if (strncmp("sound", optarg, 5) == 0) |
406 |
|
{ |
407 |
|
#ifdef WITH_RDPSND |
408 |
|
g_rdpsnd = True; |
409 |
|
#else |
410 |
|
warning("Not compiled with sound support"); |
411 |
|
#endif |
412 |
|
} |
413 |
|
else if (strncmp("disk", optarg, 4) == 0) |
414 |
|
{ |
415 |
|
/* -r disk:h:=/mnt/floppy */ |
416 |
|
disk_enum_devices(&g_num_devices, optarg + 4); |
417 |
|
} |
418 |
|
else if (strncmp("comport", optarg, 7) == 0) |
419 |
|
{ |
420 |
|
serial_enum_devices(&g_num_devices, optarg + 7); |
421 |
|
} |
422 |
|
else if (strncmp("lptport", optarg, 7) == 0) |
423 |
|
{ |
424 |
|
parallel_enum_devices(&g_num_devices, optarg + 7); |
425 |
|
} |
426 |
|
else if (strncmp("printer", optarg, 7) == 0) |
427 |
|
{ |
428 |
|
printer_enum_devices(&g_num_devices, optarg + 7); |
429 |
|
} |
430 |
|
else |
431 |
|
{ |
432 |
|
warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n"); |
433 |
|
} |
434 |
|
break; |
435 |
|
|
436 |
|
case '0': |
437 |
|
g_console_session = True; |
438 |
|
break; |
439 |
|
|
440 |
|
case '4': |
441 |
|
g_use_rdp5 = False; |
442 |
|
break; |
443 |
|
|
444 |
|
case '5': |
445 |
|
g_use_rdp5 = True; |
446 |
|
break; |
447 |
|
|
448 |
case 'h': |
case 'h': |
449 |
case '?': |
case '?': |
450 |
default: |
default: |
460 |
} |
} |
461 |
|
|
462 |
STRNCPY(server, argv[optind], sizeof(server)); |
STRNCPY(server, argv[optind], sizeof(server)); |
463 |
p = strchr(server, ':'); |
parse_server_and_port(server); |
|
if (p != NULL) |
|
|
{ |
|
|
tcp_port_rdp = strtol(p + 1, NULL, 10); |
|
|
*p = 0; |
|
|
} |
|
464 |
|
|
465 |
if (!username_option) |
if (!username_option) |
466 |
{ |
{ |
471 |
return 1; |
return 1; |
472 |
} |
} |
473 |
|
|
474 |
STRNCPY(username, pw->pw_name, sizeof(username)); |
STRNCPY(g_username, pw->pw_name, sizeof(g_username)); |
475 |
} |
} |
476 |
|
|
477 |
if (hostname[0] == 0) |
if (hostname[0] == 0) |
492 |
if (prompt_password && read_password(password, sizeof(password))) |
if (prompt_password && read_password(password, sizeof(password))) |
493 |
flags |= RDP_LOGON_AUTO; |
flags |= RDP_LOGON_AUTO; |
494 |
|
|
495 |
if (title[0] == 0) |
if (g_title[0] == 0) |
496 |
{ |
{ |
497 |
strcpy(title, "rdesktop - "); |
strcpy(g_title, "rdesktop - "); |
498 |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - ")); |
499 |
} |
} |
500 |
|
|
501 |
|
#ifdef RDP2VNC |
502 |
|
rdp2vnc_connect(server, flags, domain, password, shell, directory); |
503 |
|
return 0; |
504 |
|
#else |
505 |
|
|
506 |
if (!ui_init()) |
if (!ui_init()) |
507 |
return 1; |
return 1; |
508 |
|
|
509 |
|
#ifdef WITH_RDPSND |
510 |
|
if (g_rdpsnd) |
511 |
|
rdpsnd_init(); |
512 |
|
#endif |
513 |
|
rdpdr_init(); |
514 |
|
|
515 |
if (!rdp_connect(server, flags, domain, password, shell, directory)) |
if (!rdp_connect(server, flags, domain, password, shell, directory)) |
516 |
return 1; |
return 1; |
517 |
|
|
518 |
|
/* By setting encryption to False here, we have an encrypted login |
519 |
|
packet but unencrypted transfer of other packets */ |
520 |
|
if (!packet_encryption) |
521 |
|
g_encryption = False; |
522 |
|
|
523 |
|
|
524 |
DEBUG(("Connection successful.\n")); |
DEBUG(("Connection successful.\n")); |
525 |
memset(password, 0, sizeof(password)); |
memset(password, 0, sizeof(password)); |
526 |
|
|
527 |
if (ui_create_window()) |
if (ui_create_window()) |
528 |
{ |
{ |
529 |
rdp_main_loop(); |
rdp_retval = rdp_main_loop(); |
530 |
ui_destroy_window(); |
ui_destroy_window(); |
531 |
} |
} |
532 |
|
|
533 |
DEBUG(("Disconnecting...\n")); |
DEBUG(("Disconnecting...\n")); |
534 |
rdp_disconnect(); |
rdp_disconnect(); |
535 |
ui_deinit(); |
ui_deinit(); |
536 |
return 0; |
|
537 |
|
if (True == rdp_retval) |
538 |
|
return 0; |
539 |
|
else |
540 |
|
return 2; |
541 |
|
|
542 |
|
#endif |
543 |
|
|
544 |
} |
} |
545 |
|
|
546 |
#ifdef EGD_SOCKET |
#ifdef EGD_SOCKET |
703 |
hexdump(unsigned char *p, unsigned int len) |
hexdump(unsigned char *p, unsigned int len) |
704 |
{ |
{ |
705 |
unsigned char *line = p; |
unsigned char *line = p; |
706 |
unsigned int thisline, offset = 0; |
int i, thisline, offset = 0; |
|
int i; |
|
707 |
|
|
708 |
while (offset < len) |
while (offset < len) |
709 |
{ |
{ |
727 |
} |
} |
728 |
} |
} |
729 |
|
|
730 |
#ifdef SAVE_LICENCE |
/* |
731 |
|
input: src is the string we look in for needle. |
732 |
|
Needle may be escaped by a backslash, in |
733 |
|
that case we ignore that particular needle. |
734 |
|
return value: returns next src pointer, for |
735 |
|
succesive executions, like in a while loop |
736 |
|
if retval is 0, then there are no more args. |
737 |
|
pitfalls: |
738 |
|
src is modified. 0x00 chars are inserted to |
739 |
|
terminate strings. |
740 |
|
return val, points on the next val chr after ins |
741 |
|
0x00 |
742 |
|
|
743 |
|
example usage: |
744 |
|
while( (pos = next_arg( optarg, ',')) ){ |
745 |
|
printf("%s\n",optarg); |
746 |
|
optarg=pos; |
747 |
|
} |
748 |
|
|
749 |
|
*/ |
750 |
|
char * |
751 |
|
next_arg(char *src, char needle) |
752 |
|
{ |
753 |
|
char *nextval; |
754 |
|
char *p; |
755 |
|
char *mvp = 0; |
756 |
|
|
757 |
|
/* EOS */ |
758 |
|
if (*src == (char) 0x00) |
759 |
|
return 0; |
760 |
|
|
761 |
|
p = src; |
762 |
|
/* skip escaped needles */ |
763 |
|
while ((nextval = strchr(p, needle))) |
764 |
|
{ |
765 |
|
mvp = nextval - 1; |
766 |
|
/* found backslashed needle */ |
767 |
|
if (*mvp == '\\' && (mvp > src)) |
768 |
|
{ |
769 |
|
/* move string one to the left */ |
770 |
|
while (*(mvp + 1) != (char) 0x00) |
771 |
|
{ |
772 |
|
*mvp = *(mvp + 1); |
773 |
|
*mvp++; |
774 |
|
} |
775 |
|
*mvp = (char) 0x00; |
776 |
|
p = nextval; |
777 |
|
} |
778 |
|
else |
779 |
|
{ |
780 |
|
p = nextval + 1; |
781 |
|
break; |
782 |
|
} |
783 |
|
|
784 |
|
} |
785 |
|
|
786 |
|
/* more args available */ |
787 |
|
if (nextval) |
788 |
|
{ |
789 |
|
*nextval = (char) 0x00; |
790 |
|
return ++nextval; |
791 |
|
} |
792 |
|
|
793 |
|
/* no more args after this, jump to EOS */ |
794 |
|
nextval = src + strlen(src); |
795 |
|
return nextval; |
796 |
|
} |
797 |
|
|
798 |
|
|
799 |
|
void |
800 |
|
toupper_str(char *p) |
801 |
|
{ |
802 |
|
while (*p) |
803 |
|
{ |
804 |
|
if ((*p >= 'a') && (*p <= 'z')) |
805 |
|
*p = toupper((int) *p); |
806 |
|
p++; |
807 |
|
} |
808 |
|
} |
809 |
|
|
810 |
|
|
811 |
|
/* not all clibs got ltoa */ |
812 |
|
#define LTOA_BUFSIZE (sizeof(long) * 8 + 1) |
813 |
|
|
814 |
|
char * |
815 |
|
ltoa(long N, int base) |
816 |
|
{ |
817 |
|
static char ret[LTOA_BUFSIZE]; |
818 |
|
|
819 |
|
char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf); |
820 |
|
|
821 |
|
register int divrem; |
822 |
|
|
823 |
|
if (base < 36 || 2 > base) |
824 |
|
base = 10; |
825 |
|
|
826 |
|
if (N < 0) |
827 |
|
{ |
828 |
|
*head++ = '-'; |
829 |
|
N = -N; |
830 |
|
} |
831 |
|
|
832 |
|
tail = buf + sizeof(buf); |
833 |
|
*--tail = 0; |
834 |
|
|
835 |
|
do |
836 |
|
{ |
837 |
|
divrem = N % base; |
838 |
|
*--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10; |
839 |
|
N /= base; |
840 |
|
} |
841 |
|
while (N); |
842 |
|
|
843 |
|
strcpy(head, tail); |
844 |
|
return ret; |
845 |
|
} |
846 |
|
|
847 |
|
|
848 |
int |
int |
849 |
load_licence(unsigned char **data) |
load_licence(unsigned char **data) |
850 |
{ |
{ |
851 |
char *path; |
char *home, *path; |
|
char *home; |
|
852 |
struct stat st; |
struct stat st; |
853 |
int fd; |
int fd, length; |
854 |
|
|
855 |
home = getenv("HOME"); |
home = getenv("HOME"); |
856 |
if (home == NULL) |
if (home == NULL) |
857 |
return -1; |
return -1; |
858 |
|
|
859 |
path = xmalloc(strlen(home) + strlen(hostname) + 20); |
path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence.")); |
860 |
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); |
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); |
861 |
|
|
862 |
fd = open(path, O_RDONLY); |
fd = open(path, O_RDONLY); |
866 |
if (fstat(fd, &st)) |
if (fstat(fd, &st)) |
867 |
return -1; |
return -1; |
868 |
|
|
869 |
*data = xmalloc(st.st_size); |
*data = (uint8 *) xmalloc(st.st_size); |
870 |
return read(fd, *data, st.st_size); |
length = read(fd, *data, st.st_size); |
871 |
|
close(fd); |
872 |
|
xfree(path); |
873 |
|
return length; |
874 |
} |
} |
875 |
|
|
876 |
void |
void |
877 |
save_licence(unsigned char *data, int length) |
save_licence(unsigned char *data, int length) |
878 |
{ |
{ |
879 |
char *fpath; /* file path for licence */ |
char *home, *path, *tmppath; |
880 |
char *fname, *fnamewrk; /* file name for licence .inkl path. */ |
int fd; |
|
char *home; |
|
|
uint32 y; |
|
|
struct flock fnfl; |
|
|
int fnfd, fnwrkfd, i, wlen; |
|
|
struct stream s, *s_ptr; |
|
|
uint32 len; |
|
|
|
|
|
/* Construct a stream, so that we can use macros to extract the |
|
|
* licence. |
|
|
*/ |
|
|
s_ptr = &s; |
|
|
s_ptr->p = data; |
|
|
/* Skip first two bytes */ |
|
|
in_uint16(s_ptr, len); |
|
|
|
|
|
/* Skip three strings */ |
|
|
for (i = 0; i < 3; i++) |
|
|
{ |
|
|
in_uint32(s_ptr, len); |
|
|
s_ptr->p += len; |
|
|
/* Make sure that we won't be past the end of data after |
|
|
* reading the next length value |
|
|
*/ |
|
|
if ((s_ptr->p) + 4 > data + length) |
|
|
{ |
|
|
printf("Error in parsing licence key.\n"); |
|
|
printf("Strings %d end value %x > supplied length (%x)\n", |
|
|
i, s_ptr->p, data + length); |
|
|
return; |
|
|
} |
|
|
} |
|
|
in_uint32(s_ptr, len); |
|
|
if (s_ptr->p + len > data + length) |
|
|
{ |
|
|
printf("Error in parsing licence key.\n"); |
|
|
printf("End of licence %x > supplied length (%x)\n", s_ptr->p + len, data + length); |
|
|
return; |
|
|
} |
|
881 |
|
|
882 |
home = getenv("HOME"); |
home = getenv("HOME"); |
883 |
if (home == NULL) |
if (home == NULL) |
884 |
return; |
return; |
885 |
|
|
886 |
/* set and create the directory -- if it doesn't exist. */ |
path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence.")); |
|
fpath = xmalloc(strlen(home) + 11); |
|
|
STRNCPY(fpath, home, strlen(home) + 1); |
|
887 |
|
|
888 |
sprintf(fpath, "%s/.rdesktop", fpath); |
sprintf(path, "%s/.rdesktop", home); |
889 |
if (mkdir(fpath, 0700) == -1 && errno != EEXIST) |
if ((mkdir(path, 0700) == -1) && errno != EEXIST) |
890 |
{ |
{ |
891 |
perror("mkdir"); |
perror(path); |
892 |
exit(1); |
return; |
893 |
} |
} |
894 |
|
|
895 |
/* set the real licence filename, and put a write lock on it. */ |
/* write licence to licence.hostname.new, then atomically rename to licence.hostname */ |
896 |
fname = xmalloc(strlen(fpath) + strlen(hostname) + 10); |
|
897 |
sprintf(fname, "%s/licence.%s", fpath, hostname); |
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); |
898 |
fnfd = open(fname, O_RDONLY); |
tmppath = (char *) xmalloc(strlen(path) + sizeof(".new")); |
899 |
if (fnfd != -1) |
strcpy(tmppath, path); |
900 |
{ |
strcat(tmppath, ".new"); |
|
fnfl.l_type = F_WRLCK; |
|
|
fnfl.l_whence = SEEK_SET; |
|
|
fnfl.l_start = 0; |
|
|
fnfl.l_len = 1; |
|
|
fcntl(fnfd, F_SETLK, &fnfl); |
|
|
} |
|
|
|
|
|
/* create a temporary licence file */ |
|
|
fnamewrk = xmalloc(strlen(fname) + 12); |
|
|
for (y = 0;; y++) |
|
|
{ |
|
|
sprintf(fnamewrk, "%s.%lu", fname, y); |
|
|
fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600); |
|
|
if (fnwrkfd == -1) |
|
|
{ |
|
|
if (errno == EINTR || errno == EEXIST) |
|
|
continue; |
|
|
perror("create"); |
|
|
exit(1); |
|
|
} |
|
|
break; |
|
|
} |
|
|
/* write to the licence file */ |
|
|
for (y = 0; y < len;) |
|
|
{ |
|
|
do |
|
|
{ |
|
|
wlen = write(fnwrkfd, s_ptr->p + y, len - y); |
|
|
} |
|
|
while (wlen == -1 && errno == EINTR); |
|
|
if (wlen < 1) |
|
|
{ |
|
|
perror("write"); |
|
|
unlink(fnamewrk); |
|
|
exit(1); |
|
|
} |
|
|
y += wlen; |
|
|
} |
|
901 |
|
|
902 |
/* close the file and rename it to fname */ |
fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
903 |
if (close(fnwrkfd) == -1) |
if (fd == -1) |
904 |
{ |
{ |
905 |
perror("close"); |
perror(tmppath); |
906 |
unlink(fnamewrk); |
return; |
|
exit(1); |
|
907 |
} |
} |
908 |
if (rename(fnamewrk, fname) == -1) |
|
909 |
|
if (write(fd, data, length) != length) |
910 |
{ |
{ |
911 |
perror("rename"); |
perror(tmppath); |
912 |
unlink(fnamewrk); |
unlink(tmppath); |
|
exit(1); |
|
913 |
} |
} |
914 |
/* close the file lock on fname */ |
else if (rename(tmppath, path) == -1) |
|
if (fnfd != -1) |
|
915 |
{ |
{ |
916 |
fnfl.l_type = F_UNLCK; |
perror(path); |
917 |
fnfl.l_whence = SEEK_SET; |
unlink(tmppath); |
|
fnfl.l_start = 0; |
|
|
fnfl.l_len = 1; |
|
|
fcntl(fnfd, F_SETLK, &fnfl); |
|
|
close(fnfd); |
|
918 |
} |
} |
919 |
|
|
920 |
|
close(fd); |
921 |
|
xfree(tmppath); |
922 |
|
xfree(path); |
923 |
} |
} |
|
#endif |
|