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 |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include <stdlib.h> /* malloc realloc free */ |
#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 */ |
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */ |
24 |
#include <fcntl.h> /* open */ |
#include <fcntl.h> /* open */ |
25 |
#include <pwd.h> /* getpwuid */ |
#include <pwd.h> /* getpwuid */ |
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 |
|
int height; |
37 |
BOOL bitmap_compression = True; |
BOOL bitmap_compression = True; |
38 |
BOOL motion = True; |
BOOL sendmotion = True; |
39 |
BOOL orders = True; |
BOOL orders = True; |
40 |
BOOL licence = True; |
BOOL licence = True; |
41 |
BOOL use_encryption = True; |
BOOL encryption = True; |
42 |
BOOL desktop_save = True; |
BOOL desktop_save = True; |
|
BOOL grab_keyboard = True; |
|
43 |
BOOL fullscreen = False; |
BOOL fullscreen = False; |
|
int private_colormap = False; |
|
44 |
|
|
45 |
/* Display usage information */ |
/* Display usage information */ |
46 |
static void |
static void |
47 |
usage(char *program) |
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 |
int |
67 |
main(int argc, char *argv[]) |
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 |
printf("Version " VERSION ". Copyright (C) 1999-2001 Matt Chapman.\n"); |
82 |
". Copyright (C) 1999-2000 Matt Chapman.\n"); |
printf("See http://www.rdesktop.org/ for more information.\n\n"); |
|
STATUS("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; |
107 |
|
|
108 |
|
case 'p': |
109 |
|
STRNCPY(password, optarg, sizeof(password)); |
110 |
|
flags |= RDP_LOGON_AUTO; |
111 |
break; |
break; |
112 |
|
|
113 |
case 'n': |
case 'n': |
114 |
strncpy(hostname, optarg, sizeof(hostname)); |
STRNCPY(hostname, optarg, sizeof(hostname)); |
115 |
|
break; |
116 |
|
|
117 |
|
case 'k': |
118 |
|
STRNCPY(keymapname, optarg, sizeof(keymapname)); |
119 |
break; |
break; |
120 |
|
|
121 |
case 'g': |
case 'g': |
122 |
|
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 |
char *tgem = 0; |
error("invalid geometry\n"); |
129 |
width = strtol(optarg, NULL, 10); |
return 1; |
|
tgem = strchr(optarg, 'x'); |
|
|
if ((tgem == 0) || (strlen(tgem) < 2)) |
|
|
{ |
|
|
ERROR |
|
|
("-g: invalid parameter. Syntax example: -g 1024x768\n"); |
|
|
exit(1); |
|
|
} |
|
|
height = strtol(tgem + 1, NULL, 10); |
|
130 |
} |
} |
131 |
break; |
break; |
132 |
|
|
133 |
case 'k': |
case 'f': |
134 |
keylayout = strtol(optarg, NULL, 16); |
fullscreen = True; |
|
/* keylayout = find_keyb_code(optarg); */ |
|
|
if (keylayout == 0) |
|
|
return 0; |
|
|
break; |
|
|
|
|
|
case 'm': |
|
|
motion = False; |
|
135 |
break; |
break; |
136 |
|
|
137 |
case 'b': |
case 'b': |
138 |
orders = False; |
orders = False; |
139 |
break; |
break; |
140 |
|
|
|
case 'l': |
|
|
licence = False; |
|
|
break; |
|
|
|
|
141 |
case 'e': |
case 'e': |
142 |
use_encryption = False; |
encryption = False; |
143 |
break; |
break; |
144 |
case 'K': |
|
145 |
grab_keyboard = False; |
case 'm': |
146 |
break; |
sendmotion = False; |
|
case 'F': |
|
|
fullscreen = True; |
|
147 |
break; |
break; |
148 |
case 'v': |
|
149 |
private_colormap = True; |
case 'l': |
150 |
|
licence = False; |
151 |
break; |
break; |
152 |
|
|
153 |
case 'h': |
case 'h': |
154 |
case '?': |
case '?': |
155 |
default: |
default: |
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 (!strcmp(password, "-")) |
197 |
|
{ |
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 |
|
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 |
{ |
{ |
219 |
directory)) |
directory)) |
220 |
return 1; |
return 1; |
221 |
|
|
222 |
STATUS("Connection successful.\n"); |
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 |
|
|
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); |
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; |
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; |
291 |
free(mem); |
free(mem); |
292 |
} |
} |
293 |
|
|
294 |
/* Produce a hex dump */ |
/* report an error */ |
295 |
|
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 |
void |
322 |
hexdump(unsigned char *p, unsigned int len) |
hexdump(unsigned char *p, unsigned int len) |
323 |
{ |
{ |
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]) for (; i < 16; i++) |
printf("%02x ", line[i]); |
337 |
STATUS(" "); |
|
338 |
|
for (; i < 16; i++) |
339 |
|
printf(" "); |
340 |
|
|
341 |
for (i = 0; i < thisline; i++) |
for (i = 0; i < thisline; i++) |
342 |
STATUS("%c", |
printf("%c", |
343 |
(line[i] >= 0x20 |
(line[i] >= 0x20 |
344 |
&& line[i] < 0x7f) ? line[i] : '.'); |
&& line[i] < 0x7f) ? line[i] : '.'); |
345 |
|
|
346 |
STATUS("\n"); |
printf("\n"); |
347 |
offset += thisline; |
offset += thisline; |
348 |
line += thisline; |
line += thisline; |
349 |
} |
} |