18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
*/ |
20 |
|
|
21 |
|
#include <sys/types.h> |
22 |
|
#include <sys/stat.h> |
23 |
|
#include <unistd.h> |
24 |
|
#include <fcntl.h> |
25 |
|
#include <errno.h> |
26 |
#include "rdesktop.h" |
#include "rdesktop.h" |
27 |
|
|
28 |
#ifdef WITH_OPENSSL |
#ifdef WITH_OPENSSL |
39 |
|
|
40 |
BOOL licence_issued = False; |
BOOL licence_issued = False; |
41 |
|
|
42 |
|
|
43 |
|
int |
44 |
|
load_licence(unsigned char **data) |
45 |
|
{ |
46 |
|
char *path; |
47 |
|
char *home; |
48 |
|
struct stat st; |
49 |
|
int fd; |
50 |
|
|
51 |
|
home = getenv("HOME"); |
52 |
|
if (home == NULL) |
53 |
|
return -1; |
54 |
|
|
55 |
|
path = xmalloc(strlen(home) + strlen(hostname) + 20); |
56 |
|
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); |
57 |
|
|
58 |
|
fd = open(path, O_RDONLY); |
59 |
|
if (fd == -1) |
60 |
|
return -1; |
61 |
|
|
62 |
|
if (fstat(fd, &st)) |
63 |
|
return -1; |
64 |
|
|
65 |
|
*data = xmalloc(st.st_size); |
66 |
|
return read(fd, *data, st.st_size); |
67 |
|
} |
68 |
|
|
69 |
|
void |
70 |
|
save_licence(unsigned char *data, int length) |
71 |
|
{ |
72 |
|
char *fpath; /* file path for licence */ |
73 |
|
char *fname, *fnamewrk; /* file name for licence .inkl path. */ |
74 |
|
char *home; |
75 |
|
uint32 y; |
76 |
|
struct flock fnfl; |
77 |
|
int fnfd, fnwrkfd, i, wlen; |
78 |
|
struct stream s, *s_ptr; |
79 |
|
uint32 len; |
80 |
|
|
81 |
|
/* Construct a stream, so that we can use macros to extract the |
82 |
|
* licence. |
83 |
|
*/ |
84 |
|
s_ptr = &s; |
85 |
|
s_ptr->p = data; |
86 |
|
/* Skip first two bytes */ |
87 |
|
in_uint16(s_ptr, len); |
88 |
|
|
89 |
|
/* Skip three strings */ |
90 |
|
for (i = 0; i < 3; i++) |
91 |
|
{ |
92 |
|
in_uint32(s_ptr, len); |
93 |
|
s_ptr->p += len; |
94 |
|
/* Make sure that we won't be past the end of data after |
95 |
|
* reading the next length value |
96 |
|
*/ |
97 |
|
if ((s_ptr->p) + 4 > data + length) |
98 |
|
{ |
99 |
|
printf("Error in parsing licence key.\n"); |
100 |
|
printf("Strings %d end value %x > supplied length (%x)\n", i, |
101 |
|
(unsigned int)s_ptr->p, |
102 |
|
(unsigned int)data + length); |
103 |
|
return; |
104 |
|
} |
105 |
|
} |
106 |
|
in_uint32(s_ptr, len); |
107 |
|
if (s_ptr->p + len > data + length) |
108 |
|
{ |
109 |
|
printf("Error in parsing licence key.\n"); |
110 |
|
printf("End of licence %x > supplied length (%x)\n", |
111 |
|
(unsigned int)s_ptr->p + len, |
112 |
|
(unsigned int)data + length); |
113 |
|
return; |
114 |
|
} |
115 |
|
|
116 |
|
home = getenv("HOME"); |
117 |
|
if (home == NULL) |
118 |
|
return; |
119 |
|
|
120 |
|
/* set and create the directory -- if it doesn't exist. */ |
121 |
|
fpath = xmalloc(strlen(home) + 11); |
122 |
|
STRNCPY(fpath, home, strlen(home) + 1); |
123 |
|
|
124 |
|
sprintf(fpath, "%s/.rdesktop", fpath); |
125 |
|
if (mkdir(fpath, 0700) == -1 && errno != EEXIST) |
126 |
|
{ |
127 |
|
perror("mkdir"); |
128 |
|
exit(1); |
129 |
|
} |
130 |
|
|
131 |
|
/* set the real licence filename, and put a write lock on it. */ |
132 |
|
fname = xmalloc(strlen(fpath) + strlen(hostname) + 10); |
133 |
|
sprintf(fname, "%s/licence.%s", fpath, hostname); |
134 |
|
fnfd = open(fname, O_RDONLY); |
135 |
|
if (fnfd != -1) |
136 |
|
{ |
137 |
|
fnfl.l_type = F_WRLCK; |
138 |
|
fnfl.l_whence = SEEK_SET; |
139 |
|
fnfl.l_start = 0; |
140 |
|
fnfl.l_len = 1; |
141 |
|
fcntl(fnfd, F_SETLK, &fnfl); |
142 |
|
} |
143 |
|
|
144 |
|
/* create a temporary licence file */ |
145 |
|
fnamewrk = xmalloc(strlen(fname) + 12); |
146 |
|
for (y = 0;; y++) |
147 |
|
{ |
148 |
|
sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int)y); |
149 |
|
fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600); |
150 |
|
if (fnwrkfd == -1) |
151 |
|
{ |
152 |
|
if (errno == EINTR || errno == EEXIST) |
153 |
|
continue; |
154 |
|
perror("create"); |
155 |
|
exit(1); |
156 |
|
} |
157 |
|
break; |
158 |
|
} |
159 |
|
/* write to the licence file */ |
160 |
|
for (y = 0; y < len;) |
161 |
|
{ |
162 |
|
do |
163 |
|
{ |
164 |
|
wlen = write(fnwrkfd, s_ptr->p + y, len - y); |
165 |
|
} |
166 |
|
while (wlen == -1 && errno == EINTR); |
167 |
|
if (wlen < 1) |
168 |
|
{ |
169 |
|
perror("write"); |
170 |
|
unlink(fnamewrk); |
171 |
|
exit(1); |
172 |
|
} |
173 |
|
y += wlen; |
174 |
|
} |
175 |
|
|
176 |
|
/* close the file and rename it to fname */ |
177 |
|
if (close(fnwrkfd) == -1) |
178 |
|
{ |
179 |
|
perror("close"); |
180 |
|
unlink(fnamewrk); |
181 |
|
exit(1); |
182 |
|
} |
183 |
|
if (rename(fnamewrk, fname) == -1) |
184 |
|
{ |
185 |
|
perror("rename"); |
186 |
|
unlink(fnamewrk); |
187 |
|
exit(1); |
188 |
|
} |
189 |
|
/* close the file lock on fname */ |
190 |
|
if (fnfd != -1) |
191 |
|
{ |
192 |
|
fnfl.l_type = F_UNLCK; |
193 |
|
fnfl.l_whence = SEEK_SET; |
194 |
|
fnfl.l_start = 0; |
195 |
|
fnfl.l_len = 1; |
196 |
|
fcntl(fnfd, F_SETLK, &fnfl); |
197 |
|
close(fnfd); |
198 |
|
} |
199 |
|
|
200 |
|
} |
201 |
|
|
202 |
/* Generate a session key and RC4 keys, given client and server randoms */ |
/* Generate a session key and RC4 keys, given client and server randoms */ |
203 |
static void |
static void |
204 |
licence_generate_keys(uint8 * client_key, uint8 * server_key, uint8 * client_rsa) |
licence_generate_keys(uint8 * client_key, uint8 * server_key, uint8 * client_rsa) |
224 |
strncpy((char *) (hwid + 4), hostname, LICENCE_HWID_SIZE - 4); |
strncpy((char *) (hwid + 4), hostname, LICENCE_HWID_SIZE - 4); |
225 |
} |
} |
226 |
|
|
|
#ifdef SAVE_LICENCE |
|
227 |
/* Present an existing licence to the server */ |
/* Present an existing licence to the server */ |
228 |
static void |
static void |
229 |
licence_present(uint8 * client_random, uint8 * rsa_data, |
licence_present(uint8 * client_random, uint8 * rsa_data, |
263 |
s_mark_end(s); |
s_mark_end(s); |
264 |
sec_send(s, sec_flags); |
sec_send(s, sec_flags); |
265 |
} |
} |
|
#endif |
|
266 |
|
|
267 |
/* Send a licence request packet */ |
/* Send a licence request packet */ |
268 |
static void |
static void |
307 |
{ |
{ |
308 |
uint8 null_data[SEC_MODULUS_SIZE]; |
uint8 null_data[SEC_MODULUS_SIZE]; |
309 |
uint8 *server_random; |
uint8 *server_random; |
|
#ifdef SAVE_LICENCE |
|
310 |
uint8 signature[LICENCE_SIGNATURE_SIZE]; |
uint8 signature[LICENCE_SIGNATURE_SIZE]; |
311 |
uint8 hwid[LICENCE_HWID_SIZE]; |
uint8 hwid[LICENCE_HWID_SIZE]; |
312 |
uint8 *licence_data; |
uint8 *licence_data; |
313 |
int licence_size; |
int licence_size; |
314 |
RC4_KEY crypt_key; |
RC4_KEY crypt_key; |
|
#endif |
|
315 |
|
|
316 |
/* Retrieve the server random from the incoming packet */ |
/* Retrieve the server random from the incoming packet */ |
317 |
in_uint8p(s, server_random, SEC_RANDOM_SIZE); |
in_uint8p(s, server_random, SEC_RANDOM_SIZE); |
321 |
memset(null_data, 0, sizeof(null_data)); |
memset(null_data, 0, sizeof(null_data)); |
322 |
licence_generate_keys(null_data, server_random, null_data); |
licence_generate_keys(null_data, server_random, null_data); |
323 |
|
|
|
#ifdef SAVE_LICENCE |
|
324 |
licence_size = load_licence(&licence_data); |
licence_size = load_licence(&licence_data); |
325 |
if (licence_size != -1) |
if (licence_size != -1) |
326 |
{ |
{ |
336 |
xfree(licence_data); |
xfree(licence_data); |
337 |
return; |
return; |
338 |
} |
} |
|
#endif |
|
339 |
|
|
340 |
licence_send_request(null_data, null_data, username, hostname); |
licence_send_request(null_data, null_data, username, hostname); |
341 |
} |
} |
441 |
return; |
return; |
442 |
|
|
443 |
licence_issued = True; |
licence_issued = True; |
|
|
|
|
#ifdef SAVE_LICENCE |
|
444 |
save_licence(s->p, length - 2); |
save_licence(s->p, length - 2); |
|
#endif |
|
445 |
} |
} |
446 |
|
|
447 |
/* Process a licence packet */ |
/* Process a licence packet */ |
477 |
unimpl("licence tag 0x%x\n", tag); |
unimpl("licence tag 0x%x\n", tag); |
478 |
} |
} |
479 |
} |
} |
480 |
|
|