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-2002 |
Copyright (C) Matthew Chapman 1999-2003 |
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 |
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 */ |
24 |
#include <pwd.h> /* getpwuid */ |
#include <pwd.h> /* getpwuid */ |
|
#include <limits.h> /* PATH_MAX */ |
|
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 <errno.h> |
30 |
#include "rdesktop.h" |
#include "rdesktop.h" |
31 |
|
|
32 |
#ifdef EGD_SOCKET |
#ifdef EGD_SOCKET |
40 |
#include "crypto/md5.h" |
#include "crypto/md5.h" |
41 |
#endif |
#endif |
42 |
|
|
43 |
char title[32] = ""; |
char g_title[32] = ""; |
44 |
char username[16]; |
char g_username[16]; |
45 |
char hostname[16]; |
char hostname[16]; |
46 |
char keymapname[16]; |
char keymapname[16]; |
47 |
int keylayout = 0x409; /* Defaults to US keyboard layout */ |
int keylayout = 0x409; /* Defaults to US keyboard layout */ |
48 |
int width = 800; |
int g_width = 800; /* If width or height are reset to zero, the geometry will |
49 |
int height = 600; |
be fetched from _NET_WORKAREA */ |
50 |
|
int g_height = 600; |
51 |
int tcp_port_rdp = TCP_PORT_RDP; |
int tcp_port_rdp = TCP_PORT_RDP; |
52 |
BOOL bitmap_compression = True; |
int g_server_bpp = 8; |
53 |
BOOL sendmotion = True; |
int g_win_button_size = 0; /* If zero, disable single app mode */ |
54 |
BOOL orders = True; |
BOOL g_bitmap_compression = True; |
55 |
BOOL encryption = True; |
BOOL g_sendmotion = True; |
56 |
BOOL desktop_save = True; |
BOOL g_orders = True; |
57 |
BOOL fullscreen = False; |
BOOL g_encryption = True; |
58 |
BOOL grab_keyboard = True; |
BOOL packet_encryption = True; |
59 |
|
BOOL g_desktop_save = True; |
60 |
|
BOOL g_fullscreen = False; |
61 |
|
BOOL g_grab_keyboard = True; |
62 |
|
BOOL g_hide_decorations = False; |
63 |
|
BOOL g_use_rdp5 = True; |
64 |
|
extern BOOL g_owncolmap; |
65 |
|
|
66 |
|
#ifdef RDP2VNC |
67 |
|
extern int rfb_port; |
68 |
|
extern int defer_time; |
69 |
|
void |
70 |
|
rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password, |
71 |
|
char *shell, char *directory); |
72 |
|
#endif |
73 |
/* Display usage information */ |
/* Display usage information */ |
74 |
static void |
static void |
75 |
usage(char *program) |
usage(char *program) |
76 |
{ |
{ |
77 |
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n"); |
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n"); |
78 |
fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n"); |
fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n"); |
79 |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
80 |
|
|
81 |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
82 |
|
#ifdef RDP2VNC |
83 |
|
fprintf(stderr, " -V: vnc port\n"); |
84 |
|
fprintf(stderr, " -Q: defer time (ms)\n"); |
85 |
|
#endif |
86 |
fprintf(stderr, " -u: user name\n"); |
fprintf(stderr, " -u: user name\n"); |
87 |
fprintf(stderr, " -d: domain\n"); |
fprintf(stderr, " -d: domain\n"); |
88 |
fprintf(stderr, " -s: shell\n"); |
fprintf(stderr, " -s: shell\n"); |
89 |
|
fprintf(stderr, " -S: caption button size (single application mode)\n"); |
90 |
fprintf(stderr, " -c: working directory\n"); |
fprintf(stderr, " -c: working directory\n"); |
91 |
fprintf(stderr, " -p: password (- to prompt)\n"); |
fprintf(stderr, " -p: password (- to prompt)\n"); |
92 |
fprintf(stderr, " -n: client hostname\n"); |
fprintf(stderr, " -n: client hostname\n"); |
95 |
fprintf(stderr, " -f: full-screen mode\n"); |
fprintf(stderr, " -f: full-screen mode\n"); |
96 |
fprintf(stderr, " -b: force bitmap updates\n"); |
fprintf(stderr, " -b: force bitmap updates\n"); |
97 |
fprintf(stderr, " -e: disable encryption (French TS)\n"); |
fprintf(stderr, " -e: disable encryption (French TS)\n"); |
98 |
|
fprintf(stderr, " -E: disable encryption from client to server\n"); |
99 |
fprintf(stderr, " -m: do not send motion events\n"); |
fprintf(stderr, " -m: do not send motion events\n"); |
100 |
|
fprintf(stderr, " -C: use private colour map\n"); |
101 |
fprintf(stderr, " -K: keep window manager key bindings\n"); |
fprintf(stderr, " -K: keep window manager key bindings\n"); |
102 |
fprintf(stderr, " -T: window title\n"); |
fprintf(stderr, " -T: window title\n"); |
103 |
|
fprintf(stderr, " -D: hide window manager decorations\n"); |
104 |
|
fprintf(stderr, " -a: server bpp\n"); |
105 |
|
fprintf(stderr, " -4: Use RDP version 4\n"); |
106 |
|
fprintf(stderr, " -5: Use RDP version 5 (default)\n"); |
107 |
} |
} |
108 |
|
|
109 |
static BOOL |
static BOOL |
142 |
return ret; |
return ret; |
143 |
} |
} |
144 |
|
|
145 |
|
static void |
146 |
|
parse_server_and_port(char *server) |
147 |
|
{ |
148 |
|
char *p; |
149 |
|
#ifdef IPv6 |
150 |
|
int addr_colons; |
151 |
|
#endif |
152 |
|
|
153 |
|
#ifdef IPv6 |
154 |
|
p = server; |
155 |
|
addr_colons = 0; |
156 |
|
while (*p) |
157 |
|
if (*p++ == ':') |
158 |
|
addr_colons++; |
159 |
|
if (addr_colons >= 2) |
160 |
|
{ |
161 |
|
/* numeric IPv6 style address format - [1:2:3::4]:port */ |
162 |
|
p = strchr(server, ']'); |
163 |
|
if (*server == '[' && p != NULL) |
164 |
|
{ |
165 |
|
if (*(p + 1) == ':' && *(p + 2) != '\0') |
166 |
|
tcp_port_rdp = strtol(p + 2, NULL, 10); |
167 |
|
/* remove the port number and brackets from the address */ |
168 |
|
*p = '\0'; |
169 |
|
strncpy(server, server + 1, strlen(server)); |
170 |
|
} |
171 |
|
} |
172 |
|
else |
173 |
|
{ |
174 |
|
/* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */ |
175 |
|
p = strchr(server, ':'); |
176 |
|
if (p != NULL) |
177 |
|
{ |
178 |
|
tcp_port_rdp = strtol(p + 1, NULL, 10); |
179 |
|
*p = 0; |
180 |
|
} |
181 |
|
} |
182 |
|
#else /* no IPv6 support */ |
183 |
|
p = strchr(server, ':'); |
184 |
|
if (p != NULL) |
185 |
|
{ |
186 |
|
tcp_port_rdp = strtol(p + 1, NULL, 10); |
187 |
|
*p = 0; |
188 |
|
} |
189 |
|
#endif /* IPv6 */ |
190 |
|
|
191 |
|
} |
192 |
|
|
193 |
/* Client program */ |
/* Client program */ |
194 |
int |
int |
195 |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
200 |
char password[16]; |
char password[16]; |
201 |
char shell[128]; |
char shell[128]; |
202 |
char directory[32]; |
char directory[32]; |
203 |
BOOL prompt_password; |
BOOL prompt_password, rdp_retval = False; |
204 |
struct passwd *pw; |
struct passwd *pw; |
205 |
uint32 flags; |
uint32 flags; |
206 |
char *p; |
char *p; |
207 |
int c; |
int c; |
208 |
|
int username_option = 0; |
209 |
|
|
210 |
flags = RDP_LOGON_NORMAL; |
flags = RDP_LOGON_NORMAL; |
211 |
prompt_password = False; |
prompt_password = False; |
212 |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
213 |
strcpy(keymapname, "us"); |
strcpy(keymapname, "en-us"); |
214 |
|
|
215 |
while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemKT:h?")) != -1) |
#ifdef RDP2VNC |
216 |
|
#define VNCOPT "V:Q:" |
217 |
|
#else |
218 |
|
#define VNCOPT |
219 |
|
#endif |
220 |
|
|
221 |
|
while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbeEmCKT:Dh?54")) != -1) |
222 |
{ |
{ |
223 |
switch (c) |
switch (c) |
224 |
{ |
{ |
225 |
|
#ifdef RDP2VNC |
226 |
|
case 'V': |
227 |
|
rfb_port = strtol(optarg, NULL, 10); |
228 |
|
if (rfb_port < 100) |
229 |
|
rfb_port += 5900; |
230 |
|
break; |
231 |
|
|
232 |
|
case 'Q': |
233 |
|
defer_time = strtol(optarg, NULL, 10); |
234 |
|
if (defer_time < 0) |
235 |
|
defer_time = 0; |
236 |
|
break; |
237 |
|
#endif |
238 |
|
|
239 |
case 'u': |
case 'u': |
240 |
STRNCPY(username, optarg, sizeof(username)); |
STRNCPY(g_username, optarg, sizeof(g_username)); |
241 |
|
username_option = 1; |
242 |
break; |
break; |
243 |
|
|
244 |
case 'd': |
case 'd': |
249 |
STRNCPY(shell, optarg, sizeof(shell)); |
STRNCPY(shell, optarg, sizeof(shell)); |
250 |
break; |
break; |
251 |
|
|
252 |
|
case 'S': |
253 |
|
if (!strcmp(optarg, "standard")) |
254 |
|
{ |
255 |
|
g_win_button_size = 18; |
256 |
|
break; |
257 |
|
} |
258 |
|
|
259 |
|
g_win_button_size = strtol(optarg, &p, 10); |
260 |
|
|
261 |
|
if (*p) |
262 |
|
{ |
263 |
|
error("invalid button size\n"); |
264 |
|
return 1; |
265 |
|
} |
266 |
|
|
267 |
|
break; |
268 |
|
|
269 |
case 'c': |
case 'c': |
270 |
STRNCPY(directory, optarg, sizeof(directory)); |
STRNCPY(directory, optarg, sizeof(directory)); |
271 |
break; |
break; |
295 |
break; |
break; |
296 |
|
|
297 |
case 'g': |
case 'g': |
298 |
width = strtol(optarg, &p, 10); |
if (!strcmp(optarg, "workarea")) |
299 |
|
{ |
300 |
|
g_width = g_height = 0; |
301 |
|
break; |
302 |
|
} |
303 |
|
|
304 |
|
g_width = strtol(optarg, &p, 10); |
305 |
if (*p == 'x') |
if (*p == 'x') |
306 |
height = strtol(p + 1, NULL, 10); |
g_height = strtol(p + 1, NULL, 10); |
307 |
|
|
308 |
if ((width == 0) || (height == 0)) |
if ((g_width == 0) || (g_height == 0)) |
309 |
{ |
{ |
310 |
error("invalid geometry\n"); |
error("invalid geometry\n"); |
311 |
return 1; |
return 1; |
313 |
break; |
break; |
314 |
|
|
315 |
case 'f': |
case 'f': |
316 |
fullscreen = True; |
g_fullscreen = True; |
317 |
break; |
break; |
318 |
|
|
319 |
case 'b': |
case 'b': |
320 |
orders = False; |
g_orders = False; |
321 |
break; |
break; |
322 |
|
|
323 |
case 'e': |
case 'e': |
324 |
encryption = False; |
g_encryption = False; |
325 |
|
break; |
326 |
|
case 'E': |
327 |
|
packet_encryption = False; |
328 |
break; |
break; |
|
|
|
329 |
case 'm': |
case 'm': |
330 |
sendmotion = False; |
g_sendmotion = False; |
331 |
|
break; |
332 |
|
|
333 |
|
case 'C': |
334 |
|
g_owncolmap = True; |
335 |
break; |
break; |
336 |
|
|
337 |
case 'K': |
case 'K': |
338 |
grab_keyboard = False; |
g_grab_keyboard = False; |
339 |
break; |
break; |
340 |
|
|
341 |
case 'T': |
case 'T': |
342 |
STRNCPY(title, optarg, sizeof(title)); |
STRNCPY(g_title, optarg, sizeof(g_title)); |
343 |
|
break; |
344 |
|
|
345 |
|
case 'D': |
346 |
|
g_hide_decorations = True; |
347 |
|
break; |
348 |
|
|
349 |
|
case 'a': |
350 |
|
g_server_bpp = strtol(optarg, NULL, 10); |
351 |
|
if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15 |
352 |
|
&& g_server_bpp != 24) |
353 |
|
{ |
354 |
|
error("invalid server bpp\n"); |
355 |
|
return 1; |
356 |
|
} |
357 |
|
break; |
358 |
|
|
359 |
|
case '4': |
360 |
|
g_use_rdp5 = False; |
361 |
|
break; |
362 |
|
|
363 |
|
case '5': |
364 |
|
g_use_rdp5 = True; |
365 |
break; |
break; |
366 |
|
|
367 |
case 'h': |
case 'h': |
379 |
} |
} |
380 |
|
|
381 |
STRNCPY(server, argv[optind], sizeof(server)); |
STRNCPY(server, argv[optind], sizeof(server)); |
382 |
p = strchr(server, ':'); |
parse_server_and_port(server); |
|
if (p != NULL) |
|
|
{ |
|
|
tcp_port_rdp = strtol(p + 1, NULL, 10); |
|
|
*p = 0; |
|
|
} |
|
383 |
|
|
384 |
if (username[0] == 0) |
if (!username_option) |
385 |
{ |
{ |
386 |
pw = getpwuid(getuid()); |
pw = getpwuid(getuid()); |
387 |
if ((pw == NULL) || (pw->pw_name == NULL)) |
if ((pw == NULL) || (pw->pw_name == NULL)) |
390 |
return 1; |
return 1; |
391 |
} |
} |
392 |
|
|
393 |
STRNCPY(username, pw->pw_name, sizeof(username)); |
STRNCPY(g_username, pw->pw_name, sizeof(g_username)); |
394 |
} |
} |
395 |
|
|
396 |
if (hostname[0] == 0) |
if (hostname[0] == 0) |
411 |
if (prompt_password && read_password(password, sizeof(password))) |
if (prompt_password && read_password(password, sizeof(password))) |
412 |
flags |= RDP_LOGON_AUTO; |
flags |= RDP_LOGON_AUTO; |
413 |
|
|
414 |
if (title[0] == 0) |
if (g_title[0] == 0) |
415 |
{ |
{ |
416 |
strcpy(title, "rdesktop - "); |
strcpy(g_title, "rdesktop - "); |
417 |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - ")); |
418 |
} |
} |
419 |
|
|
420 |
|
#ifdef RDP2VNC |
421 |
|
rdp2vnc_connect(server, flags, domain, password, shell, directory); |
422 |
|
return 0; |
423 |
|
#else |
424 |
|
|
425 |
if (!ui_init()) |
if (!ui_init()) |
426 |
return 1; |
return 1; |
427 |
|
|
428 |
|
/* rdpsnd_init(); */ |
429 |
|
/* rdpdr_init(); */ |
430 |
|
|
431 |
if (!rdp_connect(server, flags, domain, password, shell, directory)) |
if (!rdp_connect(server, flags, domain, password, shell, directory)) |
432 |
return 1; |
return 1; |
433 |
|
|
434 |
|
/* By setting encryption to False here, we have an encrypted login |
435 |
|
packet but unencrypted transfer of other packets */ |
436 |
|
if (!packet_encryption) |
437 |
|
g_encryption = False; |
438 |
|
|
439 |
|
|
440 |
DEBUG(("Connection successful.\n")); |
DEBUG(("Connection successful.\n")); |
441 |
memset(password, 0, sizeof(password)); |
memset(password, 0, sizeof(password)); |
442 |
|
|
443 |
if (ui_create_window()) |
if (ui_create_window()) |
444 |
{ |
{ |
445 |
rdp_main_loop(); |
rdp_retval = rdp_main_loop(); |
446 |
ui_destroy_window(); |
ui_destroy_window(); |
447 |
} |
} |
448 |
|
|
449 |
DEBUG(("Disconnecting...\n")); |
DEBUG(("Disconnecting...\n")); |
450 |
rdp_disconnect(); |
rdp_disconnect(); |
451 |
ui_deinit(); |
ui_deinit(); |
452 |
return 0; |
|
453 |
|
if (True == rdp_retval) |
454 |
|
return 0; |
455 |
|
else |
456 |
|
return 2; |
457 |
|
|
458 |
|
#endif |
459 |
|
|
460 |
} |
} |
461 |
|
|
462 |
#ifdef EGD_SOCKET |
#ifdef EGD_SOCKET |
474 |
|
|
475 |
addr.sun_family = AF_UNIX; |
addr.sun_family = AF_UNIX; |
476 |
memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET)); |
memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET)); |
477 |
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) |
if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) |
478 |
goto err; |
goto err; |
479 |
|
|
480 |
/* PRNGD and EGD use a simple communications protocol */ |
/* PRNGD and EGD use a simple communications protocol */ |
481 |
buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */ |
buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */ |
482 |
buf[1] = 32; /* Number of requested random bytes */ |
buf[1] = 32; /* Number of requested random bytes */ |
483 |
if (write(fd, buf, 2) != 2) |
if (write(fd, buf, 2) != 2) |
484 |
goto err; |
goto err; |
485 |
|
|
486 |
if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */ |
if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */ |
487 |
goto err; |
goto err; |
488 |
|
|
489 |
if (read(fd, buf, 32) != 32) |
if (read(fd, buf, 32) != 32) |
491 |
|
|
492 |
ret = True; |
ret = True; |
493 |
|
|
494 |
err: |
err: |
495 |
close(fd); |
close(fd); |
496 |
return ret; |
return ret; |
497 |
} |
} |
524 |
#endif |
#endif |
525 |
|
|
526 |
/* Otherwise use whatever entropy we can gather - ideas welcome. */ |
/* Otherwise use whatever entropy we can gather - ideas welcome. */ |
527 |
r = (uint32 *)random; |
r = (uint32 *) random; |
528 |
r[0] = (getpid()) | (getppid() << 16); |
r[0] = (getpid()) | (getppid() << 16); |
529 |
r[1] = (getuid()) | (getgid() << 16); |
r[1] = (getuid()) | (getgid() << 16); |
530 |
r[2] = times(&tmsbuf); /* system uptime (clocks) */ |
r[2] = times(&tmsbuf); /* system uptime (clocks) */ |
536 |
|
|
537 |
/* Hash both halves with MD5 to obscure possible patterns */ |
/* Hash both halves with MD5 to obscure possible patterns */ |
538 |
MD5_Init(&md5); |
MD5_Init(&md5); |
539 |
MD5_Update(&md5, random, 16); |
MD5_Update(&md5, random, 16); |
540 |
MD5_Final(random, &md5); |
MD5_Final(random, &md5); |
541 |
MD5_Update(&md5, random+16, 16); |
MD5_Update(&md5, random + 16, 16); |
542 |
MD5_Final(random+16, &md5); |
MD5_Final(random + 16, &md5); |
543 |
} |
} |
544 |
|
|
545 |
/* malloc; exit if out of memory */ |
/* malloc; exit if out of memory */ |
588 |
va_end(ap); |
va_end(ap); |
589 |
} |
} |
590 |
|
|
591 |
|
/* report a warning */ |
592 |
|
void |
593 |
|
warning(char *format, ...) |
594 |
|
{ |
595 |
|
va_list ap; |
596 |
|
|
597 |
|
fprintf(stderr, "WARNING: "); |
598 |
|
|
599 |
|
va_start(ap, format); |
600 |
|
vfprintf(stderr, format, ap); |
601 |
|
va_end(ap); |
602 |
|
} |
603 |
|
|
604 |
/* report an unimplemented protocol feature */ |
/* report an unimplemented protocol feature */ |
605 |
void |
void |
606 |
unimpl(char *format, ...) |
unimpl(char *format, ...) |
616 |
|
|
617 |
/* produce a hex dump */ |
/* produce a hex dump */ |
618 |
void |
void |
619 |
hexdump(unsigned char *p, unsigned int len) |
hexdump(unsigned char *p, int len) |
620 |
{ |
{ |
621 |
unsigned char *line = p; |
unsigned char *line = p; |
622 |
unsigned int thisline, offset = 0; |
int i, thisline, offset = 0; |
|
int i; |
|
623 |
|
|
624 |
while (offset < len) |
while (offset < len) |
625 |
{ |
{ |
643 |
} |
} |
644 |
} |
} |
645 |
|
|
646 |
#ifdef SAVE_LICENCE |
|
647 |
int |
int |
648 |
load_licence(unsigned char **data) |
load_licence(unsigned char **data) |
649 |
{ |
{ |
650 |
char path[PATH_MAX]; |
char *home, *path; |
|
char *home; |
|
651 |
struct stat st; |
struct stat st; |
652 |
int fd; |
int fd, length; |
653 |
|
|
654 |
home = getenv("HOME"); |
home = getenv("HOME"); |
655 |
if (home == NULL) |
if (home == NULL) |
656 |
return -1; |
return -1; |
657 |
|
|
658 |
STRNCPY(path, home, sizeof(path)); |
path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence.")); |
659 |
strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1); |
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); |
660 |
|
|
661 |
fd = open(path, O_RDONLY); |
fd = open(path, O_RDONLY); |
662 |
if (fd == -1) |
if (fd == -1) |
665 |
if (fstat(fd, &st)) |
if (fstat(fd, &st)) |
666 |
return -1; |
return -1; |
667 |
|
|
668 |
*data = xmalloc(st.st_size); |
*data = (uint8 *) xmalloc(st.st_size); |
669 |
return read(fd, *data, st.st_size); |
length = read(fd, *data, st.st_size); |
670 |
|
close(fd); |
671 |
|
xfree(path); |
672 |
|
return length; |
673 |
} |
} |
674 |
|
|
675 |
void |
void |
676 |
save_licence(unsigned char *data, int length) |
save_licence(unsigned char *data, int length) |
677 |
{ |
{ |
678 |
char path[PATH_MAX]; |
char *home, *path, *tmppath; |
|
char *home; |
|
679 |
int fd; |
int fd; |
680 |
|
|
681 |
home = getenv("HOME"); |
home = getenv("HOME"); |
682 |
if (home == NULL) |
if (home == NULL) |
683 |
return; |
return; |
684 |
|
|
685 |
STRNCPY(path, home, sizeof(path)); |
path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence.")); |
686 |
strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1); |
|
687 |
mkdir(path, 0700); |
sprintf(path, "%s/.rdesktop", home); |
688 |
|
if ((mkdir(path, 0700) == -1) && errno != EEXIST) |
689 |
|
{ |
690 |
|
perror(path); |
691 |
|
return; |
692 |
|
} |
693 |
|
|
694 |
|
/* write licence to licence.hostname.new, then atomically rename to licence.hostname */ |
695 |
|
|
696 |
strncat(path, "/licence", sizeof(path) - strlen(path) - 1); |
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); |
697 |
|
tmppath = (char *) xmalloc(strlen(path) + sizeof(".new")); |
698 |
|
strcpy(tmppath, path); |
699 |
|
strcat(tmppath, ".new"); |
700 |
|
|
701 |
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
702 |
if (fd == -1) |
if (fd == -1) |
703 |
{ |
{ |
704 |
perror("open"); |
perror(tmppath); |
705 |
return; |
return; |
706 |
} |
} |
707 |
|
|
708 |
write(fd, data, length); |
if (write(fd, data, length) != length) |
709 |
|
{ |
710 |
|
perror(tmppath); |
711 |
|
unlink(tmppath); |
712 |
|
} |
713 |
|
else if (rename(tmppath, path) == -1) |
714 |
|
{ |
715 |
|
perror(path); |
716 |
|
unlink(tmppath); |
717 |
|
} |
718 |
|
|
719 |
close(fd); |
close(fd); |
720 |
|
xfree(tmppath); |
721 |
|
xfree(path); |
722 |
} |
} |
|
#endif |
|