1 |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Entrypoint and utility functions |
4 |
Copyright (C) Matthew Chapman 1999-2001 |
5 |
|
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 |
#include <stdlib.h> /* malloc realloc free */ |
22 |
#include <stdarg.h> /* va_list va_start va_end */ |
23 |
#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 |
#include "rdesktop.h" |
30 |
|
31 |
char username[16]; |
32 |
char hostname[16]; |
33 |
int width; |
34 |
int height; |
35 |
int keylayout = 0x409; |
36 |
BOOL bitmap_compression = True; |
37 |
BOOL sendmotion = True; |
38 |
BOOL orders = True; |
39 |
BOOL licence = True; |
40 |
BOOL encryption = True; |
41 |
BOOL desktop_save = True; |
42 |
BOOL fullscreen = False; |
43 |
|
44 |
/* Display usage information */ |
45 |
static void |
46 |
usage(char *program) |
47 |
{ |
48 |
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 |
} |
63 |
|
64 |
/* Client program */ |
65 |
int |
66 |
main(int argc, char *argv[]) |
67 |
{ |
68 |
char fullhostname[64]; |
69 |
char domain[16]; |
70 |
char password[16]; |
71 |
char shell[32]; |
72 |
char directory[32]; |
73 |
char title[32]; |
74 |
struct passwd *pw; |
75 |
char *server, *p; |
76 |
uint32 flags; |
77 |
int c; |
78 |
|
79 |
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 |
|
83 |
flags = RDP_LOGON_NORMAL; |
84 |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
85 |
|
86 |
while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemlh?")) != -1) |
87 |
{ |
88 |
switch (c) |
89 |
{ |
90 |
case 'u': |
91 |
STRNCPY(username, optarg, sizeof(username)); |
92 |
break; |
93 |
|
94 |
case 'd': |
95 |
STRNCPY(domain, optarg, sizeof(domain)); |
96 |
break; |
97 |
|
98 |
case 's': |
99 |
STRNCPY(shell, optarg, sizeof(shell)); |
100 |
break; |
101 |
|
102 |
case 'c': |
103 |
STRNCPY(directory, optarg, sizeof(directory)); |
104 |
break; |
105 |
|
106 |
case 'p': |
107 |
STRNCPY(password, optarg, sizeof(password)); |
108 |
flags |= RDP_LOGON_AUTO; |
109 |
break; |
110 |
|
111 |
case 'n': |
112 |
STRNCPY(hostname, optarg, sizeof(hostname)); |
113 |
break; |
114 |
|
115 |
case 'k': |
116 |
keylayout = strtol(optarg, NULL, 16); |
117 |
if (keylayout == 0) |
118 |
{ |
119 |
error("invalid keyboard layout\n"); |
120 |
return 1; |
121 |
} |
122 |
break; |
123 |
|
124 |
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 |
break; |
135 |
|
136 |
case 'f': |
137 |
fullscreen = True; |
138 |
break; |
139 |
|
140 |
case 'b': |
141 |
orders = False; |
142 |
break; |
143 |
|
144 |
case 'e': |
145 |
encryption = False; |
146 |
break; |
147 |
|
148 |
case 'm': |
149 |
sendmotion = False; |
150 |
break; |
151 |
|
152 |
case 'l': |
153 |
licence = False; |
154 |
break; |
155 |
|
156 |
case 'h': |
157 |
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 |
error("could not determine username, use -u\n"); |
178 |
return 1; |
179 |
} |
180 |
|
181 |
STRNCPY(username, pw->pw_name, sizeof(username)); |
182 |
} |
183 |
|
184 |
if (hostname[0] == 0) |
185 |
{ |
186 |
if (gethostname(fullhostname, sizeof(fullhostname)) == -1) |
187 |
{ |
188 |
error("could not determine local hostname, use -n\n"); |
189 |
return 1; |
190 |
} |
191 |
|
192 |
p = strchr(fullhostname, '.'); |
193 |
if (p != NULL) |
194 |
*p = 0; |
195 |
|
196 |
STRNCPY(hostname, fullhostname, sizeof(hostname)); |
197 |
} |
198 |
|
199 |
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 |
strcpy(title, "rdesktop - "); |
217 |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
218 |
|
219 |
if (ui_create_window(title)) |
220 |
{ |
221 |
if (!rdp_connect(server, flags, domain, password, shell, |
222 |
directory)) |
223 |
return 1; |
224 |
|
225 |
printf("Connection successful.\n"); |
226 |
rdp_main_loop(); |
227 |
ui_destroy_window(); |
228 |
} |
229 |
|
230 |
rdp_disconnect(); |
231 |
return 0; |
232 |
} |
233 |
|
234 |
/* Generate a 32-byte random for the secure transport code. */ |
235 |
void |
236 |
generate_random(uint8 *random) |
237 |
{ |
238 |
struct stat st; |
239 |
struct tms tmsbuf; |
240 |
uint32 *r = (uint32 *) random; |
241 |
int fd; |
242 |
|
243 |
/* If we have a kernel random device, use it. */ |
244 |
if (((fd = open("/dev/urandom", O_RDONLY)) != -1) |
245 |
|| ((fd = open("/dev/random", O_RDONLY)) != -1)) |
246 |
{ |
247 |
read(fd, random, 32); |
248 |
close(fd); |
249 |
return; |
250 |
} |
251 |
|
252 |
/* Otherwise use whatever entropy we can gather - ideas welcome. */ |
253 |
r[0] = (getpid()) | (getppid() << 16); |
254 |
r[1] = (getuid()) | (getgid() << 16); |
255 |
r[2] = times(&tmsbuf); /* system uptime (clocks) */ |
256 |
gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */ |
257 |
stat("/tmp", &st); |
258 |
r[5] = st.st_atime; |
259 |
r[6] = st.st_mtime; |
260 |
r[7] = st.st_ctime; |
261 |
} |
262 |
|
263 |
/* malloc; exit if out of memory */ |
264 |
void * |
265 |
xmalloc(int size) |
266 |
{ |
267 |
void *mem = malloc(size); |
268 |
if (mem == NULL) |
269 |
{ |
270 |
error("xmalloc %d\n", size); |
271 |
exit(1); |
272 |
} |
273 |
return mem; |
274 |
} |
275 |
|
276 |
/* realloc; exit if out of memory */ |
277 |
void * |
278 |
xrealloc(void *oldmem, int size) |
279 |
{ |
280 |
void *mem = realloc(oldmem, size); |
281 |
if (mem == NULL) |
282 |
{ |
283 |
error("xrealloc %d\n", size); |
284 |
exit(1); |
285 |
} |
286 |
return mem; |
287 |
} |
288 |
|
289 |
/* free */ |
290 |
void |
291 |
xfree(void *mem) |
292 |
{ |
293 |
free(mem); |
294 |
} |
295 |
|
296 |
/* report an error */ |
297 |
void |
298 |
error(char *format, ...) |
299 |
{ |
300 |
va_list ap; |
301 |
|
302 |
fprintf(stderr, "ERROR: "); |
303 |
|
304 |
va_start(ap, format); |
305 |
vfprintf(stderr, format, ap); |
306 |
va_end(ap); |
307 |
} |
308 |
|
309 |
/* report an unimplemented protocol feature */ |
310 |
void |
311 |
unimpl(char *format, ...) |
312 |
{ |
313 |
va_list ap; |
314 |
|
315 |
fprintf(stderr, "NOT IMPLEMENTED: "); |
316 |
|
317 |
va_start(ap, format); |
318 |
vfprintf(stderr, format, ap); |
319 |
va_end(ap); |
320 |
} |
321 |
|
322 |
/* produce a hex dump */ |
323 |
void |
324 |
hexdump(unsigned char *p, unsigned int len) |
325 |
{ |
326 |
unsigned char *line = p; |
327 |
unsigned int thisline, offset = 0; |
328 |
int i; |
329 |
|
330 |
while (offset < len) |
331 |
{ |
332 |
printf("%04x ", offset); |
333 |
thisline = len - offset; |
334 |
if (thisline > 16) |
335 |
thisline = 16; |
336 |
|
337 |
for (i = 0; i < thisline; i++) |
338 |
printf("%02x ", line[i]); |
339 |
|
340 |
for (; i < 16; i++) |
341 |
printf(" "); |
342 |
|
343 |
for (i = 0; i < thisline; i++) |
344 |
printf("%c", |
345 |
(line[i] >= 0x20 |
346 |
&& line[i] < 0x7f) ? line[i] : '.'); |
347 |
|
348 |
printf("\n"); |
349 |
offset += thisline; |
350 |
line += thisline; |
351 |
} |
352 |
} |