1 |
/* -*- c-basic-offset: 8 -*- |
/* -*- 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-2005 |
Copyright (C) Matthew Chapman 1999-2008 |
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 |
45 |
#include <sys/un.h> /* sockaddr_un */ |
#include <sys/un.h> /* sockaddr_un */ |
46 |
#endif |
#endif |
47 |
|
|
48 |
#include <openssl/md5.h> |
#include "ssl.h" |
49 |
|
|
50 |
|
/* DJ globals begin */ |
51 |
|
unsigned long long g_time_last_write = 0; |
52 |
|
unsigned long long g_time_last_change = 0; |
53 |
|
uint32 g_pixels_changed; |
54 |
|
uint8 * g_bitmap_data = 0; |
55 |
|
uint8 * g_bitmap_data_last_write = 0; |
56 |
|
/* DJ globals end */ |
57 |
|
|
58 |
char g_title[64] = ""; |
char g_title[64] = ""; |
59 |
char g_username[64]; |
char *g_username; |
60 |
char g_hostname[16]; |
char g_hostname[16]; |
61 |
char g_keymapname[PATH_MAX] = ""; |
char g_keymapname[PATH_MAX] = ""; |
62 |
int g_keylayout = 0x409; /* Defaults to US keyboard layout */ |
unsigned int g_keylayout = 0x409; /* Defaults to US keyboard layout */ |
63 |
int g_keyboard_type = 0x4; /* Defaults to US keyboard layout */ |
int g_keyboard_type = 0x4; /* Defaults to US keyboard layout */ |
64 |
int g_keyboard_subtype = 0x0; /* Defaults to US keyboard layout */ |
int g_keyboard_subtype = 0x0; /* Defaults to US keyboard layout */ |
65 |
int g_keyboard_functionkeys = 0xc; /* Defaults to US keyboard layout */ |
int g_keyboard_functionkeys = 0xc; /* Defaults to US keyboard layout */ |
77 |
2 xpos neg, |
2 xpos neg, |
78 |
4 ypos neg */ |
4 ypos neg */ |
79 |
extern int g_tcp_port_rdp; |
extern int g_tcp_port_rdp; |
80 |
int g_server_bpp = 8; |
int g_server_depth = -1; |
81 |
int g_win_button_size = 0; /* If zero, disable single app mode */ |
int g_win_button_size = 0; /* If zero, disable single app mode */ |
82 |
BOOL g_bitmap_compression = True; |
RD_BOOL g_bitmap_compression = True; |
83 |
BOOL g_sendmotion = True; |
RD_BOOL g_sendmotion = True; |
84 |
BOOL g_bitmap_cache = True; |
RD_BOOL g_bitmap_cache = True; |
85 |
BOOL g_bitmap_cache_persist_enable = False; |
RD_BOOL g_bitmap_cache_persist_enable = False; |
86 |
BOOL g_bitmap_cache_precache = True; |
RD_BOOL g_bitmap_cache_precache = True; |
87 |
BOOL g_encryption = True; |
RD_BOOL g_encryption = True; |
88 |
BOOL packet_encryption = True; |
RD_BOOL g_packet_encryption = True; |
89 |
BOOL g_desktop_save = True; /* desktop save order */ |
RD_BOOL g_desktop_save = True; /* desktop save order */ |
90 |
BOOL g_polygon_ellipse_orders = True; /* polygon / ellipse orders */ |
RD_BOOL g_polygon_ellipse_orders = True; /* polygon / ellipse orders */ |
91 |
BOOL g_fullscreen = False; |
RD_BOOL g_fullscreen = False; |
92 |
BOOL g_grab_keyboard = True; |
RD_BOOL g_grab_keyboard = True; |
93 |
BOOL g_hide_decorations = False; |
RD_BOOL g_hide_decorations = False; |
94 |
BOOL g_use_rdp5 = True; |
RD_BOOL g_use_rdp5 = True; |
95 |
BOOL g_console_session = False; |
RD_BOOL g_rdpclip = True; |
96 |
BOOL g_numlock_sync = False; |
RD_BOOL g_console_session = False; |
97 |
BOOL g_owncolmap = False; |
RD_BOOL g_numlock_sync = False; |
98 |
BOOL g_ownbackstore = True; /* We can't rely on external BackingStore */ |
RD_BOOL g_lspci_enabled = False; |
99 |
|
RD_BOOL g_owncolmap = False; |
100 |
|
RD_BOOL g_ownbackstore = True; /* We can't rely on external BackingStore */ |
101 |
|
RD_BOOL g_seamless_rdp = False; |
102 |
uint32 g_embed_wnd; |
uint32 g_embed_wnd; |
103 |
uint32 g_rdp5_performanceflags = |
uint32 g_rdp5_performanceflags = |
104 |
RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS; |
RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS; |
105 |
/* Session Directory redirection */ |
/* Session Directory redirection */ |
106 |
BOOL g_redirect = False; |
RD_BOOL g_redirect = False; |
107 |
char g_redirect_server[64]; |
char g_redirect_server[64]; |
108 |
char g_redirect_domain[16]; |
char g_redirect_domain[16]; |
109 |
char g_redirect_password[64]; |
char g_redirect_password[64]; |
110 |
char g_redirect_username[64]; |
char *g_redirect_username; |
111 |
char g_redirect_cookie[128]; |
char g_redirect_cookie[128]; |
112 |
uint32 g_redirect_flags = 0; |
uint32 g_redirect_flags = 0; |
113 |
|
|
114 |
#ifdef WITH_RDPSND |
#ifdef WITH_RDPSND |
115 |
BOOL g_rdpsnd = False; |
RD_BOOL g_rdpsnd = False; |
116 |
#endif |
#endif |
117 |
|
|
118 |
#ifdef HAVE_ICONV |
#ifdef HAVE_ICONV |
135 |
usage(char *program) |
usage(char *program) |
136 |
{ |
{ |
137 |
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n"); |
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n"); |
138 |
fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2005 Matt Chapman.\n"); |
fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2008 Matthew Chapman.\n"); |
139 |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n"); |
140 |
|
|
141 |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
fprintf(stderr, "Usage: %s [options] server[:port]\n", program); |
156 |
#ifdef HAVE_ICONV |
#ifdef HAVE_ICONV |
157 |
fprintf(stderr, " -L: local codepage\n"); |
fprintf(stderr, " -L: local codepage\n"); |
158 |
#endif |
#endif |
159 |
|
fprintf(stderr, " -A: enable SeamlessRDP mode\n"); |
160 |
fprintf(stderr, " -B: use BackingStore of X-server (if available)\n"); |
fprintf(stderr, " -B: use BackingStore of X-server (if available)\n"); |
161 |
fprintf(stderr, " -e: disable encryption (French TS)\n"); |
fprintf(stderr, " -e: disable encryption (French TS)\n"); |
162 |
fprintf(stderr, " -E: disable encryption from client to server\n"); |
fprintf(stderr, " -E: disable encryption from client to server\n"); |
187 |
fprintf(stderr, " '-r printer:mydeskjet': enable printer redirection\n"); |
fprintf(stderr, " '-r printer:mydeskjet': enable printer redirection\n"); |
188 |
fprintf(stderr, |
fprintf(stderr, |
189 |
" or mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n"); |
" or mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n"); |
190 |
fprintf(stderr, " '-r sound:[local|off|remote]': enable sound redirection\n"); |
#ifdef WITH_RDPSND |
191 |
|
fprintf(stderr, |
192 |
|
" '-r sound:[local[:driver[:device]]|off|remote]': enable sound redirection\n"); |
193 |
fprintf(stderr, " remote would leave sound on server\n"); |
fprintf(stderr, " remote would leave sound on server\n"); |
194 |
|
fprintf(stderr, " available drivers for 'local':\n"); |
195 |
|
rdpsnd_show_help(); |
196 |
|
#endif |
197 |
|
fprintf(stderr, |
198 |
|
" '-r clipboard:[off|PRIMARYCLIPBOARD|CLIPBOARD]': enable clipboard\n"); |
199 |
|
fprintf(stderr, " redirection.\n"); |
200 |
|
fprintf(stderr, |
201 |
|
" 'PRIMARYCLIPBOARD' looks at both PRIMARY and CLIPBOARD\n"); |
202 |
|
fprintf(stderr, " when sending data to server.\n"); |
203 |
|
fprintf(stderr, " 'CLIPBOARD' looks at only CLIPBOARD.\n"); |
204 |
|
#ifdef WITH_SCARD |
205 |
|
fprintf(stderr, " '-r scard[:\"Scard Name\"=\"Alias Name[;Vendor Name]\"[,...]]\n"); |
206 |
|
fprintf(stderr, " example: -r scard:\"eToken PRO 00 00\"=\"AKS ifdh 0\"\n"); |
207 |
|
fprintf(stderr, |
208 |
|
" \"eToken PRO 00 00\" -> Device in Linux/Unix enviroment\n"); |
209 |
|
fprintf(stderr, |
210 |
|
" \"AKS ifdh 0\" -> Device shown in Windows enviroment \n"); |
211 |
|
fprintf(stderr, " example: -r scard:\"eToken PRO 00 00\"=\"AKS ifdh 0;AKS\"\n"); |
212 |
|
fprintf(stderr, |
213 |
|
" \"eToken PRO 00 00\" -> Device in Linux/Unix enviroment\n"); |
214 |
|
fprintf(stderr, |
215 |
|
" \"AKS ifdh 0\" -> Device shown in Windows enviroment \n"); |
216 |
|
fprintf(stderr, |
217 |
|
" \"AKS\" -> Device vendor name \n"); |
218 |
|
#endif |
219 |
fprintf(stderr, " -0: attach to console\n"); |
fprintf(stderr, " -0: attach to console\n"); |
220 |
fprintf(stderr, " -4: use RDP version 4\n"); |
fprintf(stderr, " -4: use RDP version 4\n"); |
221 |
fprintf(stderr, " -5: use RDP version 5 (default)\n"); |
fprintf(stderr, " -5: use RDP version 5 (default)\n"); |
327 |
rdp_reset_state(); |
rdp_reset_state(); |
328 |
} |
} |
329 |
|
|
330 |
static BOOL |
static RD_BOOL |
331 |
read_password(char *password, int size) |
read_password(char *password, int size) |
332 |
{ |
{ |
333 |
struct termios tios; |
struct termios tios; |
334 |
BOOL ret = False; |
RD_BOOL ret = False; |
335 |
int istty = 0; |
int istty = 0; |
336 |
char *p; |
char *p; |
337 |
|
|
419 |
char fullhostname[64]; |
char fullhostname[64]; |
420 |
char domain[16]; |
char domain[16]; |
421 |
char password[64]; |
char password[64]; |
422 |
char shell[128]; |
char shell[256]; |
423 |
char directory[32]; |
char directory[256]; |
424 |
BOOL prompt_password, deactivated; |
RD_BOOL prompt_password, deactivated; |
425 |
struct passwd *pw; |
struct passwd *pw; |
426 |
uint32 flags, ext_disc_reason = 0; |
uint32 flags, ext_disc_reason = 0; |
427 |
char *p; |
char *p; |
428 |
int c; |
int c; |
429 |
char *locale = NULL; |
char *locale = NULL; |
430 |
int username_option = 0; |
int username_option = 0; |
431 |
|
RD_BOOL geometry_option = False; |
432 |
int run_count = 0; /* Session Directory support */ |
int run_count = 0; /* Session Directory support */ |
433 |
BOOL continue_connect = True; /* Session Directory support */ |
RD_BOOL continue_connect = True; /* Session Directory support */ |
434 |
|
#ifdef WITH_RDPSND |
435 |
|
char *rdpsnd_optarg = NULL; |
436 |
|
#endif |
437 |
|
|
438 |
#ifdef HAVE_LOCALE_H |
#ifdef HAVE_LOCALE_H |
439 |
/* Set locale according to environment */ |
/* Set locale according to environment */ |
458 |
#endif |
#endif |
459 |
|
|
460 |
while ((c = getopt(argc, argv, |
while ((c = getopt(argc, argv, |
461 |
VNCOPT "u:L:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1) |
VNCOPT "Au:L:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1) |
462 |
{ |
{ |
463 |
switch (c) |
switch (c) |
464 |
{ |
{ |
476 |
break; |
break; |
477 |
#endif |
#endif |
478 |
|
|
479 |
|
case 'A': |
480 |
|
g_seamless_rdp = True; |
481 |
|
break; |
482 |
|
|
483 |
case 'u': |
case 'u': |
484 |
STRNCPY(g_username, optarg, sizeof(g_username)); |
g_username = (char *) xmalloc(strlen(optarg) + 1); |
485 |
|
STRNCPY(g_username, optarg, strlen(optarg) + 1); |
486 |
username_option = 1; |
username_option = 1; |
487 |
break; |
break; |
488 |
|
|
531 |
break; |
break; |
532 |
|
|
533 |
case 'g': |
case 'g': |
534 |
|
geometry_option = True; |
535 |
g_fullscreen = False; |
g_fullscreen = False; |
536 |
if (!strcmp(optarg, "workarea")) |
if (!strcmp(optarg, "workarea")) |
537 |
{ |
{ |
591 |
g_encryption = False; |
g_encryption = False; |
592 |
break; |
break; |
593 |
case 'E': |
case 'E': |
594 |
packet_encryption = False; |
g_packet_encryption = False; |
595 |
break; |
break; |
596 |
case 'm': |
case 'm': |
597 |
g_sendmotion = False; |
g_sendmotion = False; |
639 |
break; |
break; |
640 |
|
|
641 |
case 'a': |
case 'a': |
642 |
g_server_bpp = strtol(optarg, NULL, 10); |
g_server_depth = strtol(optarg, NULL, 10); |
643 |
if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15 |
if (g_server_depth != 8 && |
644 |
&& g_server_bpp != 24) |
g_server_depth != 16 && |
645 |
|
g_server_depth != 15 && g_server_depth != 24 |
646 |
|
&& g_server_depth != 32) |
647 |
{ |
{ |
648 |
error("invalid server bpp\n"); |
error("Invalid server colour depth.\n"); |
649 |
return 1; |
return 1; |
650 |
} |
} |
651 |
break; |
break; |
688 |
|
|
689 |
if (*optarg == ':') |
if (*optarg == ':') |
690 |
{ |
{ |
691 |
*optarg++; |
optarg++; |
692 |
while ((p = next_arg(optarg, ','))) |
while ((p = next_arg(optarg, ','))) |
693 |
{ |
{ |
694 |
if (str_startswith(optarg, "remote")) |
if (str_startswith(optarg, "remote")) |
696 |
|
|
697 |
if (str_startswith(optarg, "local")) |
if (str_startswith(optarg, "local")) |
698 |
#ifdef WITH_RDPSND |
#ifdef WITH_RDPSND |
699 |
|
{ |
700 |
|
rdpsnd_optarg = |
701 |
|
next_arg(optarg, ':'); |
702 |
g_rdpsnd = True; |
g_rdpsnd = True; |
703 |
|
} |
704 |
|
|
705 |
#else |
#else |
706 |
warning("Not compiled with sound support\n"); |
warning("Not compiled with sound support\n"); |
707 |
#endif |
#endif |
734 |
{ |
{ |
735 |
serial_enum_devices(&g_num_devices, optarg + 7); |
serial_enum_devices(&g_num_devices, optarg + 7); |
736 |
} |
} |
737 |
|
else if (str_startswith(optarg, "lspci")) |
738 |
|
{ |
739 |
|
g_lspci_enabled = True; |
740 |
|
} |
741 |
else if (str_startswith(optarg, "lptport")) |
else if (str_startswith(optarg, "lptport")) |
742 |
{ |
{ |
743 |
parallel_enum_devices(&g_num_devices, optarg + 7); |
parallel_enum_devices(&g_num_devices, optarg + 7); |
751 |
g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1); |
g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1); |
752 |
strcpy(g_rdpdr_clientname, optarg + 11); |
strcpy(g_rdpdr_clientname, optarg + 11); |
753 |
} |
} |
754 |
|
else if (str_startswith(optarg, "clipboard")) |
755 |
|
{ |
756 |
|
optarg += 9; |
757 |
|
|
758 |
|
if (*optarg == ':') |
759 |
|
{ |
760 |
|
optarg++; |
761 |
|
|
762 |
|
if (str_startswith(optarg, "off")) |
763 |
|
g_rdpclip = False; |
764 |
|
else |
765 |
|
cliprdr_set_mode(optarg); |
766 |
|
} |
767 |
|
else |
768 |
|
g_rdpclip = True; |
769 |
|
} |
770 |
|
else if (strncmp("scard", optarg, 5) == 0) |
771 |
|
{ |
772 |
|
#ifdef WITH_SCARD |
773 |
|
scard_enum_devices(&g_num_devices, optarg + 5); |
774 |
|
#else |
775 |
|
warning("Not compiled with smartcard support\n"); |
776 |
|
#endif |
777 |
|
} |
778 |
else |
else |
779 |
{ |
{ |
780 |
warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n"); |
warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound, clipboard, scard\n"); |
781 |
} |
} |
782 |
break; |
break; |
783 |
|
|
810 |
STRNCPY(server, argv[optind], sizeof(server)); |
STRNCPY(server, argv[optind], sizeof(server)); |
811 |
parse_server_and_port(server); |
parse_server_and_port(server); |
812 |
|
|
813 |
|
if (g_seamless_rdp) |
814 |
|
{ |
815 |
|
if (g_win_button_size) |
816 |
|
{ |
817 |
|
error("You cannot use -S and -A at the same time\n"); |
818 |
|
return 1; |
819 |
|
} |
820 |
|
g_rdp5_performanceflags &= ~RDP5_NO_FULLWINDOWDRAG; |
821 |
|
if (geometry_option) |
822 |
|
{ |
823 |
|
error("You cannot use -g and -A at the same time\n"); |
824 |
|
return 1; |
825 |
|
} |
826 |
|
if (g_fullscreen) |
827 |
|
{ |
828 |
|
error("You cannot use -f and -A at the same time\n"); |
829 |
|
return 1; |
830 |
|
} |
831 |
|
if (g_hide_decorations) |
832 |
|
{ |
833 |
|
error("You cannot use -D and -A at the same time\n"); |
834 |
|
return 1; |
835 |
|
} |
836 |
|
if (g_embed_wnd) |
837 |
|
{ |
838 |
|
error("You cannot use -X and -A at the same time\n"); |
839 |
|
return 1; |
840 |
|
} |
841 |
|
if (!g_use_rdp5) |
842 |
|
{ |
843 |
|
error("You cannot use -4 and -A at the same time\n"); |
844 |
|
return 1; |
845 |
|
} |
846 |
|
g_width = -100; |
847 |
|
g_grab_keyboard = False; |
848 |
|
} |
849 |
|
|
850 |
if (!username_option) |
if (!username_option) |
851 |
{ |
{ |
852 |
pw = getpwuid(getuid()); |
pw = getpwuid(getuid()); |
855 |
error("could not determine username, use -u\n"); |
error("could not determine username, use -u\n"); |
856 |
return 1; |
return 1; |
857 |
} |
} |
858 |
|
/* +1 for trailing \0 */ |
859 |
STRNCPY(g_username, pw->pw_name, sizeof(g_username)); |
int pwlen = strlen(pw->pw_name) + 1; |
860 |
|
g_username = (char *) xmalloc(pwlen); |
861 |
|
STRNCPY(g_username, pw->pw_name, pwlen); |
862 |
} |
} |
863 |
|
|
864 |
#ifdef HAVE_ICONV |
#ifdef HAVE_ICONV |
924 |
|
|
925 |
#ifdef WITH_RDPSND |
#ifdef WITH_RDPSND |
926 |
if (g_rdpsnd) |
if (g_rdpsnd) |
927 |
rdpsnd_init(); |
{ |
928 |
|
if (!rdpsnd_init(rdpsnd_optarg)) |
929 |
|
{ |
930 |
|
warning("Initializing sound-support failed!\n"); |
931 |
|
} |
932 |
|
} |
933 |
#endif |
#endif |
934 |
|
|
935 |
|
if (g_lspci_enabled) |
936 |
|
lspci_init(); |
937 |
|
|
938 |
rdpdr_init(); |
rdpdr_init(); |
939 |
|
|
940 |
while (run_count < 2 && continue_connect) /* add support for Session Directory; only reconnect once */ |
while (run_count < 2 && continue_connect) /* add support for Session Directory; only reconnect once */ |
950 |
|
|
951 |
/* By setting encryption to False here, we have an encrypted login |
/* By setting encryption to False here, we have an encrypted login |
952 |
packet but unencrypted transfer of other packets */ |
packet but unencrypted transfer of other packets */ |
953 |
if (!packet_encryption) |
if (!g_packet_encryption) |
954 |
g_encryption = False; |
g_encryption = False; |
955 |
|
|
956 |
|
|
973 |
rdesktop_reset_state(); |
rdesktop_reset_state(); |
974 |
|
|
975 |
STRNCPY(domain, g_redirect_domain, sizeof(domain)); |
STRNCPY(domain, g_redirect_domain, sizeof(domain)); |
976 |
|
xfree(g_username); |
977 |
|
g_username = (char *) xmalloc(strlen(g_redirect_username) + 1); |
978 |
STRNCPY(g_username, g_redirect_username, sizeof(g_username)); |
STRNCPY(g_username, g_redirect_username, sizeof(g_username)); |
979 |
STRNCPY(password, g_redirect_password, sizeof(password)); |
STRNCPY(password, g_redirect_password, sizeof(password)); |
980 |
STRNCPY(server, g_redirect_server, sizeof(server)); |
STRNCPY(server, g_redirect_server, sizeof(server)); |
1019 |
} |
} |
1020 |
|
|
1021 |
#endif |
#endif |
1022 |
|
if (g_redirect_username) |
1023 |
|
xfree(g_redirect_username); |
1024 |
|
|
1025 |
|
xfree(g_username); |
1026 |
} |
} |
1027 |
|
|
1028 |
#ifdef EGD_SOCKET |
#ifdef EGD_SOCKET |
1029 |
/* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */ |
/* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */ |
1030 |
static BOOL |
static RD_BOOL |
1031 |
generate_random_egd(uint8 * buf) |
generate_random_egd(uint8 * buf) |
1032 |
{ |
{ |
1033 |
struct sockaddr_un addr; |
struct sockaddr_un addr; |
1034 |
BOOL ret = False; |
RD_BOOL ret = False; |
1035 |
int fd; |
int fd; |
1036 |
|
|
1037 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
1069 |
{ |
{ |
1070 |
struct stat st; |
struct stat st; |
1071 |
struct tms tmsbuf; |
struct tms tmsbuf; |
1072 |
MD5_CTX md5; |
SSL_MD5 md5; |
1073 |
uint32 *r; |
uint32 *r; |
1074 |
int fd, n; |
int fd, n; |
1075 |
|
|
1101 |
r[7] = st.st_ctime; |
r[7] = st.st_ctime; |
1102 |
|
|
1103 |
/* Hash both halves with MD5 to obscure possible patterns */ |
/* Hash both halves with MD5 to obscure possible patterns */ |
1104 |
MD5_Init(&md5); |
ssl_md5_init(&md5); |
1105 |
MD5_Update(&md5, random, 16); |
ssl_md5_update(&md5, random, 16); |
1106 |
MD5_Final(random, &md5); |
ssl_md5_final(&md5, random); |
1107 |
MD5_Update(&md5, random + 16, 16); |
ssl_md5_update(&md5, random + 16, 16); |
1108 |
MD5_Final(random + 16, &md5); |
ssl_md5_final(&md5, random + 16); |
1109 |
} |
} |
1110 |
|
|
1111 |
/* malloc; exit if out of memory */ |
/* malloc; exit if out of memory */ |
1121 |
return mem; |
return mem; |
1122 |
} |
} |
1123 |
|
|
1124 |
|
/* Exit on NULL pointer. Use to verify result from XGetImage etc */ |
1125 |
|
void |
1126 |
|
exit_if_null(void *ptr) |
1127 |
|
{ |
1128 |
|
if (ptr == NULL) |
1129 |
|
{ |
1130 |
|
error("unexpected null pointer. Out of memory?\n"); |
1131 |
|
exit(1); |
1132 |
|
} |
1133 |
|
} |
1134 |
|
|
1135 |
/* strdup */ |
/* strdup */ |
1136 |
char * |
char * |
1137 |
xstrdup(const char *s) |
xstrdup(const char *s) |
1147 |
|
|
1148 |
/* realloc; exit if out of memory */ |
/* realloc; exit if out of memory */ |
1149 |
void * |
void * |
1150 |
xrealloc(void *oldmem, int size) |
xrealloc(void *oldmem, size_t size) |
1151 |
{ |
{ |
1152 |
void *mem; |
void *mem; |
1153 |
|
|
1154 |
if (size < 1) |
if (size == 0) |
1155 |
size = 1; |
size = 1; |
1156 |
mem = realloc(oldmem, size); |
mem = realloc(oldmem, size); |
1157 |
if (mem == NULL) |
if (mem == NULL) |
1158 |
{ |
{ |
1159 |
error("xrealloc %d\n", size); |
error("xrealloc %ld\n", size); |
1160 |
exit(1); |
exit(1); |
1161 |
} |
} |
1162 |
return mem; |
return mem; |
1280 |
while (*(mvp + 1) != (char) 0x00) |
while (*(mvp + 1) != (char) 0x00) |
1281 |
{ |
{ |
1282 |
*mvp = *(mvp + 1); |
*mvp = *(mvp + 1); |
1283 |
*mvp++; |
mvp++; |
1284 |
} |
} |
1285 |
*mvp = (char) 0x00; |
*mvp = (char) 0x00; |
1286 |
p = nextval; |
p = nextval; |
1318 |
} |
} |
1319 |
|
|
1320 |
|
|
1321 |
BOOL |
RD_BOOL |
1322 |
str_startswith(const char *s, const char *prefix) |
str_startswith(const char *s, const char *prefix) |
1323 |
{ |
{ |
1324 |
return (strncmp(s, prefix, strlen(prefix)) == 0); |
return (strncmp(s, prefix, strlen(prefix)) == 0); |
1325 |
} |
} |
1326 |
|
|
1327 |
|
|
1328 |
|
/* Split input into lines, and call linehandler for each |
1329 |
|
line. Incomplete lines are saved in the rest variable, which should |
1330 |
|
initially point to NULL. When linehandler returns False, stop and |
1331 |
|
return False. Otherwise, return True. */ |
1332 |
|
RD_BOOL |
1333 |
|
str_handle_lines(const char *input, char **rest, str_handle_lines_t linehandler, void *data) |
1334 |
|
{ |
1335 |
|
char *buf, *p; |
1336 |
|
char *oldrest; |
1337 |
|
size_t inputlen; |
1338 |
|
size_t buflen; |
1339 |
|
size_t restlen = 0; |
1340 |
|
RD_BOOL ret = True; |
1341 |
|
|
1342 |
|
/* Copy data to buffer */ |
1343 |
|
inputlen = strlen(input); |
1344 |
|
if (*rest) |
1345 |
|
restlen = strlen(*rest); |
1346 |
|
buflen = restlen + inputlen + 1; |
1347 |
|
buf = (char *) xmalloc(buflen); |
1348 |
|
buf[0] = '\0'; |
1349 |
|
if (*rest) |
1350 |
|
STRNCPY(buf, *rest, buflen); |
1351 |
|
strncat(buf, input, inputlen); |
1352 |
|
p = buf; |
1353 |
|
|
1354 |
|
while (1) |
1355 |
|
{ |
1356 |
|
char *newline = strchr(p, '\n'); |
1357 |
|
if (newline) |
1358 |
|
{ |
1359 |
|
*newline = '\0'; |
1360 |
|
if (!linehandler(p, data)) |
1361 |
|
{ |
1362 |
|
p = newline + 1; |
1363 |
|
ret = False; |
1364 |
|
break; |
1365 |
|
} |
1366 |
|
p = newline + 1; |
1367 |
|
} |
1368 |
|
else |
1369 |
|
{ |
1370 |
|
break; |
1371 |
|
|
1372 |
|
} |
1373 |
|
} |
1374 |
|
|
1375 |
|
/* Save in rest */ |
1376 |
|
oldrest = *rest; |
1377 |
|
restlen = buf + buflen - p; |
1378 |
|
*rest = (char *) xmalloc(restlen); |
1379 |
|
STRNCPY((*rest), p, restlen); |
1380 |
|
xfree(oldrest); |
1381 |
|
|
1382 |
|
xfree(buf); |
1383 |
|
return ret; |
1384 |
|
} |
1385 |
|
|
1386 |
|
/* Execute the program specified by argv. For each line in |
1387 |
|
stdout/stderr output, call linehandler. Returns false on failure. */ |
1388 |
|
RD_BOOL |
1389 |
|
subprocess(char *const argv[], str_handle_lines_t linehandler, void *data) |
1390 |
|
{ |
1391 |
|
pid_t child; |
1392 |
|
int fd[2]; |
1393 |
|
int n = 1; |
1394 |
|
char output[256]; |
1395 |
|
char *rest = NULL; |
1396 |
|
|
1397 |
|
if (pipe(fd) < 0) |
1398 |
|
{ |
1399 |
|
perror("pipe"); |
1400 |
|
return False; |
1401 |
|
} |
1402 |
|
|
1403 |
|
if ((child = fork()) < 0) |
1404 |
|
{ |
1405 |
|
perror("fork"); |
1406 |
|
return False; |
1407 |
|
} |
1408 |
|
|
1409 |
|
/* Child */ |
1410 |
|
if (child == 0) |
1411 |
|
{ |
1412 |
|
/* Close read end */ |
1413 |
|
close(fd[0]); |
1414 |
|
|
1415 |
|
/* Redirect stdout and stderr to pipe */ |
1416 |
|
dup2(fd[1], 1); |
1417 |
|
dup2(fd[1], 2); |
1418 |
|
|
1419 |
|
/* Execute */ |
1420 |
|
execvp(argv[0], argv); |
1421 |
|
perror("Error executing child"); |
1422 |
|
_exit(128); |
1423 |
|
} |
1424 |
|
|
1425 |
|
/* Parent. Close write end. */ |
1426 |
|
close(fd[1]); |
1427 |
|
while (n > 0) |
1428 |
|
{ |
1429 |
|
n = read(fd[0], output, 255); |
1430 |
|
output[n] = '\0'; |
1431 |
|
str_handle_lines(output, &rest, linehandler, data); |
1432 |
|
} |
1433 |
|
xfree(rest); |
1434 |
|
|
1435 |
|
return True; |
1436 |
|
} |
1437 |
|
|
1438 |
|
|
1439 |
/* not all clibs got ltoa */ |
/* not all clibs got ltoa */ |
1440 |
#define LTOA_BUFSIZE (sizeof(long) * 8 + 1) |
#define LTOA_BUFSIZE (sizeof(long) * 8 + 1) |
1441 |
|
|
1551 |
} |
} |
1552 |
|
|
1553 |
/* Create the bitmap cache directory */ |
/* Create the bitmap cache directory */ |
1554 |
BOOL |
RD_BOOL |
1555 |
rd_pstcache_mkdir(void) |
rd_pstcache_mkdir(void) |
1556 |
{ |
{ |
1557 |
char *home; |
char *home; |
1628 |
} |
} |
1629 |
|
|
1630 |
/* do a write lock on a file */ |
/* do a write lock on a file */ |
1631 |
BOOL |
RD_BOOL |
1632 |
rd_lock_file(int fd, int start, int len) |
rd_lock_file(int fd, int start, int len) |
1633 |
{ |
{ |
1634 |
struct flock lock; |
struct flock lock; |