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