1 |
matty |
10 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Entrypoint and utility functions |
4 |
matty |
30 |
Copyright (C) Matthew Chapman 1999-2001 |
5 |
matty |
10 |
|
6 |
|
|
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 |
8 |
|
|
the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
(at your option) any later version. |
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
GNU General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License |
17 |
|
|
along with this program; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
matty |
24 |
#include <stdlib.h> /* malloc realloc free */ |
22 |
matty |
30 |
#include <stdarg.h> /* va_list va_start va_end */ |
23 |
matty |
24 |
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */ |
24 |
|
|
#include <fcntl.h> /* open */ |
25 |
|
|
#include <pwd.h> /* getpwuid */ |
26 |
|
|
#include <sys/stat.h> /* stat */ |
27 |
|
|
#include <sys/time.h> /* gettimeofday */ |
28 |
|
|
#include <sys/times.h> /* times */ |
29 |
matty |
10 |
#include "rdesktop.h" |
30 |
|
|
|
31 |
|
|
char username[16]; |
32 |
|
|
char hostname[16]; |
33 |
matty |
30 |
int width; |
34 |
|
|
int height; |
35 |
matty |
10 |
int keylayout = 0x409; |
36 |
matty |
28 |
BOOL bitmap_compression = True; |
37 |
matty |
29 |
BOOL sendmotion = True; |
38 |
matty |
10 |
BOOL orders = True; |
39 |
|
|
BOOL licence = True; |
40 |
matty |
30 |
BOOL encryption = True; |
41 |
matty |
28 |
BOOL desktop_save = True; |
42 |
|
|
BOOL fullscreen = False; |
43 |
matty |
10 |
|
44 |
|
|
/* Display usage information */ |
45 |
matty |
25 |
static void |
46 |
|
|
usage(char *program) |
47 |
matty |
10 |
{ |
48 |
matty |
30 |
printf("Usage: %s [options] server\n", program); |
49 |
|
|
printf(" -u: user name\n"); |
50 |
|
|
printf(" -d: domain\n"); |
51 |
|
|
printf(" -s: shell\n"); |
52 |
|
|
printf(" -c: working directory\n"); |
53 |
|
|
printf(" -p: password (autologon)\n"); |
54 |
|
|
printf(" -n: client hostname\n"); |
55 |
|
|
printf(" -k: keyboard layout (hex)\n"); |
56 |
|
|
printf(" -g: desktop geometry (WxH)\n"); |
57 |
|
|
printf(" -f: full-screen mode\n"); |
58 |
|
|
printf(" -b: force bitmap updates\n"); |
59 |
|
|
printf(" -e: disable encryption (French TS)\n"); |
60 |
|
|
printf(" -m: do not send motion events\n"); |
61 |
|
|
printf(" -l: do not request licence\n\n"); |
62 |
matty |
10 |
} |
63 |
|
|
|
64 |
|
|
/* Client program */ |
65 |
matty |
25 |
int |
66 |
|
|
main(int argc, char *argv[]) |
67 |
matty |
10 |
{ |
68 |
matty |
30 |
char fullhostname[64]; |
69 |
matty |
21 |
char domain[16]; |
70 |
|
|
char password[16]; |
71 |
|
|
char shell[32]; |
72 |
|
|
char directory[32]; |
73 |
matty |
10 |
char title[32]; |
74 |
matty |
30 |
struct passwd *pw; |
75 |
|
|
char *server, *p; |
76 |
|
|
uint32 flags; |
77 |
matty |
10 |
int c; |
78 |
|
|
|
79 |
matty |
30 |
printf("rdesktop: A Remote Desktop Protocol client.\n"); |
80 |
|
|
printf("Version " VERSION ". Copyright (C) 1999-2001 Matt Chapman.\n"); |
81 |
|
|
printf("See http://www.rdesktop.org/ for more information.\n\n"); |
82 |
matty |
10 |
|
83 |
matty |
21 |
flags = RDP_LOGON_NORMAL; |
84 |
|
|
domain[0] = password[0] = shell[0] = directory[0] = 0; |
85 |
|
|
|
86 |
matty |
30 |
while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemlh?")) != -1) |
87 |
matty |
10 |
{ |
88 |
|
|
switch (c) |
89 |
|
|
{ |
90 |
|
|
case 'u': |
91 |
matty |
30 |
STRNCPY(username, optarg, sizeof(username)); |
92 |
matty |
10 |
break; |
93 |
|
|
|
94 |
matty |
21 |
case 'd': |
95 |
matty |
30 |
STRNCPY(domain, optarg, sizeof(domain)); |
96 |
matty |
21 |
break; |
97 |
|
|
|
98 |
|
|
case 's': |
99 |
matty |
30 |
STRNCPY(shell, optarg, sizeof(shell)); |
100 |
matty |
21 |
break; |
101 |
|
|
|
102 |
|
|
case 'c': |
103 |
matty |
30 |
STRNCPY(directory, optarg, sizeof(directory)); |
104 |
matty |
21 |
break; |
105 |
|
|
|
106 |
matty |
30 |
case 'p': |
107 |
|
|
STRNCPY(password, optarg, sizeof(password)); |
108 |
|
|
flags |= RDP_LOGON_AUTO; |
109 |
|
|
break; |
110 |
|
|
|
111 |
matty |
10 |
case 'n': |
112 |
matty |
30 |
STRNCPY(hostname, optarg, sizeof(hostname)); |
113 |
matty |
10 |
break; |
114 |
|
|
|
115 |
|
|
case 'k': |
116 |
|
|
keylayout = strtol(optarg, NULL, 16); |
117 |
matty |
28 |
if (keylayout == 0) |
118 |
matty |
30 |
{ |
119 |
|
|
error("invalid keyboard layout\n"); |
120 |
|
|
return 1; |
121 |
|
|
} |
122 |
matty |
10 |
break; |
123 |
|
|
|
124 |
matty |
30 |
case 'g': |
125 |
|
|
width = strtol(optarg, &p, 10); |
126 |
|
|
if (*p == 'x') |
127 |
|
|
height = strtol(p+1, NULL, 10); |
128 |
|
|
|
129 |
|
|
if ((width == 0) || (height == 0)) |
130 |
|
|
{ |
131 |
|
|
error("invalid geometry\n"); |
132 |
|
|
return 1; |
133 |
|
|
} |
134 |
matty |
10 |
break; |
135 |
|
|
|
136 |
matty |
30 |
case 'f': |
137 |
|
|
fullscreen = True; |
138 |
|
|
break; |
139 |
|
|
|
140 |
matty |
10 |
case 'b': |
141 |
|
|
orders = False; |
142 |
|
|
break; |
143 |
|
|
|
144 |
matty |
30 |
case 'e': |
145 |
|
|
encryption = False; |
146 |
matty |
10 |
break; |
147 |
|
|
|
148 |
matty |
30 |
case 'm': |
149 |
|
|
sendmotion = False; |
150 |
matty |
28 |
break; |
151 |
matty |
29 |
|
152 |
matty |
30 |
case 'l': |
153 |
|
|
licence = False; |
154 |
matty |
28 |
break; |
155 |
matty |
29 |
|
156 |
matty |
28 |
case 'h': |
157 |
matty |
10 |
case '?': |
158 |
|
|
default: |
159 |
|
|
usage(argv[0]); |
160 |
|
|
return 1; |
161 |
|
|
} |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
if (argc - optind < 1) |
165 |
|
|
{ |
166 |
|
|
usage(argv[0]); |
167 |
|
|
return 1; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
server = argv[optind]; |
171 |
|
|
|
172 |
|
|
if (username[0] == 0) |
173 |
|
|
{ |
174 |
|
|
pw = getpwuid(getuid()); |
175 |
|
|
if ((pw == NULL) || (pw->pw_name == NULL)) |
176 |
|
|
{ |
177 |
matty |
30 |
error("could not determine username, use -u\n"); |
178 |
matty |
10 |
return 1; |
179 |
|
|
} |
180 |
|
|
|
181 |
matty |
30 |
STRNCPY(username, pw->pw_name, sizeof(username)); |
182 |
matty |
10 |
} |
183 |
|
|
|
184 |
|
|
if (hostname[0] == 0) |
185 |
|
|
{ |
186 |
matty |
30 |
if (gethostname(fullhostname, sizeof(fullhostname)) == -1) |
187 |
matty |
10 |
{ |
188 |
matty |
30 |
error("could not determine local hostname, use -n\n"); |
189 |
matty |
10 |
return 1; |
190 |
|
|
} |
191 |
matty |
30 |
|
192 |
|
|
p = strchr(fullhostname, '.'); |
193 |
|
|
if (p != NULL) |
194 |
|
|
*p = 0; |
195 |
|
|
|
196 |
|
|
STRNCPY(hostname, fullhostname, sizeof(hostname)); |
197 |
matty |
10 |
} |
198 |
|
|
|
199 |
matty |
30 |
if (!strcmp(password, "-")) |
200 |
|
|
{ |
201 |
|
|
p = getpass("Password: "); |
202 |
|
|
if (p == NULL) |
203 |
|
|
{ |
204 |
|
|
error("failed to read password\n"); |
205 |
|
|
return 0; |
206 |
|
|
} |
207 |
|
|
STRNCPY(password, p, sizeof(password)); |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
if ((width == 0) || (height == 0)) |
211 |
|
|
{ |
212 |
|
|
width = 800; |
213 |
|
|
height = 600; |
214 |
|
|
} |
215 |
|
|
|
216 |
matty |
12 |
strcpy(title, "rdesktop - "); |
217 |
matty |
30 |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
218 |
matty |
12 |
|
219 |
matty |
10 |
if (ui_create_window(title)) |
220 |
|
|
{ |
221 |
matty |
28 |
if (!rdp_connect(server, flags, domain, password, shell, |
222 |
|
|
directory)) |
223 |
|
|
return 1; |
224 |
|
|
|
225 |
matty |
30 |
printf("Connection successful.\n"); |
226 |
matty |
10 |
rdp_main_loop(); |
227 |
matty |
33 |
printf("Disconnecting...\n"); |
228 |
matty |
10 |
ui_destroy_window(); |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
rdp_disconnect(); |
232 |
|
|
return 0; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
/* Generate a 32-byte random for the secure transport code. */ |
236 |
matty |
25 |
void |
237 |
|
|
generate_random(uint8 *random) |
238 |
matty |
10 |
{ |
239 |
|
|
struct stat st; |
240 |
matty |
22 |
struct tms tmsbuf; |
241 |
matty |
24 |
uint32 *r = (uint32 *) random; |
242 |
matty |
10 |
int fd; |
243 |
|
|
|
244 |
|
|
/* If we have a kernel random device, use it. */ |
245 |
matty |
30 |
if (((fd = open("/dev/urandom", O_RDONLY)) != -1) |
246 |
|
|
|| ((fd = open("/dev/random", O_RDONLY)) != -1)) |
247 |
matty |
10 |
{ |
248 |
|
|
read(fd, random, 32); |
249 |
|
|
close(fd); |
250 |
|
|
return; |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
/* Otherwise use whatever entropy we can gather - ideas welcome. */ |
254 |
|
|
r[0] = (getpid()) | (getppid() << 16); |
255 |
|
|
r[1] = (getuid()) | (getgid() << 16); |
256 |
matty |
24 |
r[2] = times(&tmsbuf); /* system uptime (clocks) */ |
257 |
|
|
gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */ |
258 |
matty |
10 |
stat("/tmp", &st); |
259 |
|
|
r[5] = st.st_atime; |
260 |
|
|
r[6] = st.st_mtime; |
261 |
|
|
r[7] = st.st_ctime; |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
/* malloc; exit if out of memory */ |
265 |
matty |
25 |
void * |
266 |
|
|
xmalloc(int size) |
267 |
matty |
10 |
{ |
268 |
|
|
void *mem = malloc(size); |
269 |
|
|
if (mem == NULL) |
270 |
|
|
{ |
271 |
matty |
30 |
error("xmalloc %d\n", size); |
272 |
matty |
10 |
exit(1); |
273 |
|
|
} |
274 |
|
|
return mem; |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
/* realloc; exit if out of memory */ |
278 |
matty |
25 |
void * |
279 |
|
|
xrealloc(void *oldmem, int size) |
280 |
matty |
10 |
{ |
281 |
|
|
void *mem = realloc(oldmem, size); |
282 |
|
|
if (mem == NULL) |
283 |
|
|
{ |
284 |
matty |
30 |
error("xrealloc %d\n", size); |
285 |
matty |
10 |
exit(1); |
286 |
|
|
} |
287 |
|
|
return mem; |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
/* free */ |
291 |
matty |
25 |
void |
292 |
|
|
xfree(void *mem) |
293 |
matty |
10 |
{ |
294 |
|
|
free(mem); |
295 |
|
|
} |
296 |
|
|
|
297 |
matty |
30 |
/* report an error */ |
298 |
matty |
25 |
void |
299 |
matty |
30 |
error(char *format, ...) |
300 |
|
|
{ |
301 |
|
|
va_list ap; |
302 |
|
|
|
303 |
|
|
fprintf(stderr, "ERROR: "); |
304 |
|
|
|
305 |
|
|
va_start(ap, format); |
306 |
|
|
vfprintf(stderr, format, ap); |
307 |
|
|
va_end(ap); |
308 |
|
|
} |
309 |
|
|
|
310 |
|
|
/* report an unimplemented protocol feature */ |
311 |
|
|
void |
312 |
|
|
unimpl(char *format, ...) |
313 |
|
|
{ |
314 |
|
|
va_list ap; |
315 |
|
|
|
316 |
|
|
fprintf(stderr, "NOT IMPLEMENTED: "); |
317 |
|
|
|
318 |
|
|
va_start(ap, format); |
319 |
|
|
vfprintf(stderr, format, ap); |
320 |
|
|
va_end(ap); |
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
/* produce a hex dump */ |
324 |
|
|
void |
325 |
matty |
25 |
hexdump(unsigned char *p, unsigned int len) |
326 |
matty |
10 |
{ |
327 |
|
|
unsigned char *line = p; |
328 |
|
|
unsigned int thisline, offset = 0; |
329 |
|
|
int i; |
330 |
|
|
|
331 |
|
|
while (offset < len) |
332 |
|
|
{ |
333 |
matty |
30 |
printf("%04x ", offset); |
334 |
matty |
10 |
thisline = len - offset; |
335 |
|
|
if (thisline > 16) |
336 |
|
|
thisline = 16; |
337 |
|
|
|
338 |
|
|
for (i = 0; i < thisline; i++) |
339 |
matty |
30 |
printf("%02x ", line[i]); |
340 |
matty |
10 |
|
341 |
matty |
30 |
for (; i < 16; i++) |
342 |
|
|
printf(" "); |
343 |
|
|
|
344 |
matty |
10 |
for (i = 0; i < thisline; i++) |
345 |
matty |
30 |
printf("%c", |
346 |
matty |
24 |
(line[i] >= 0x20 |
347 |
|
|
&& line[i] < 0x7f) ? line[i] : '.'); |
348 |
matty |
10 |
|
349 |
matty |
30 |
printf("\n"); |
350 |
matty |
10 |
offset += thisline; |
351 |
|
|
line += thisline; |
352 |
|
|
} |
353 |
|
|
} |