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; |
int width; |
34 |
int height = 600; |
int height; |
35 |
int keylayout = 0x409; |
int keylayout = 0x409; |
36 |
BOOL motion = True; |
BOOL bitmap_compression = True; |
37 |
|
BOOL sendmotion = True; |
38 |
BOOL orders = True; |
BOOL orders = True; |
39 |
BOOL licence = True; |
BOOL licence = True; |
40 |
|
BOOL encryption = True; |
41 |
|
BOOL desktop_save = True; |
42 |
|
BOOL fullscreen = False; |
43 |
|
|
44 |
/* Display usage information */ |
/* Display usage information */ |
45 |
static void |
static void |
46 |
usage(char *program) |
usage(char *program) |
47 |
{ |
{ |
48 |
STATUS("Usage: %s [options] server\n", program); |
printf("Usage: %s [options] server\n", program); |
49 |
STATUS(" -u: user name\n"); |
printf(" -u: user name\n"); |
50 |
STATUS(" -d: domain\n"); |
printf(" -d: domain\n"); |
51 |
STATUS(" -s: shell\n"); |
printf(" -s: shell\n"); |
52 |
STATUS(" -c: working directory\n"); |
printf(" -c: working directory\n"); |
53 |
STATUS(" -p: password (autologon)\n"); |
printf(" -p: password (autologon)\n"); |
54 |
STATUS(" -n: client hostname\n"); |
printf(" -n: client hostname\n"); |
55 |
STATUS(" -w: desktop width\n"); |
printf(" -k: keyboard layout (hex)\n"); |
56 |
STATUS(" -h: desktop height\n"); |
printf(" -g: desktop geometry (WxH)\n"); |
57 |
STATUS(" -k: keyboard layout (hex)\n"); |
printf(" -f: full-screen mode\n"); |
58 |
STATUS(" -b: force bitmap updates\n"); |
printf(" -b: force bitmap updates\n"); |
59 |
STATUS(" -m: do not send motion events\n"); |
printf(" -e: disable encryption (French TS)\n"); |
60 |
STATUS(" -l: do not request licence\n\n"); |
printf(" -m: do not send motion events\n"); |
61 |
|
printf(" -l: do not request licence\n\n"); |
62 |
} |
} |
63 |
|
|
64 |
/* Client program */ |
/* Client program */ |
65 |
int |
int |
66 |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
67 |
{ |
{ |
68 |
struct passwd *pw; |
char fullhostname[64]; |
|
char *server; |
|
|
uint32 flags; |
|
69 |
char domain[16]; |
char domain[16]; |
70 |
char password[16]; |
char password[16]; |
71 |
char shell[32]; |
char shell[32]; |
72 |
char directory[32]; |
char directory[32]; |
73 |
char title[32]; |
char title[32]; |
74 |
|
struct passwd *pw; |
75 |
|
char *server, *p; |
76 |
|
uint32 flags; |
77 |
int c; |
int c; |
78 |
|
|
79 |
STATUS("rdesktop: A Remote Desktop Protocol client.\n"); |
printf("rdesktop: A Remote Desktop Protocol client.\n"); |
80 |
STATUS("Version " VERSION |
printf("Version " VERSION ". Copyright (C) 1999-2001 Matt Chapman.\n"); |
81 |
". 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"); |
|
82 |
|
|
83 |
flags = RDP_LOGON_NORMAL; |
flags = RDP_LOGON_NORMAL; |
84 |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
85 |
|
|
86 |
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) |
87 |
{ |
{ |
88 |
switch (c) |
switch (c) |
89 |
{ |
{ |
90 |
case 'u': |
case 'u': |
91 |
strncpy(username, optarg, sizeof(username)); |
STRNCPY(username, optarg, sizeof(username)); |
92 |
break; |
break; |
93 |
|
|
94 |
case 'd': |
case 'd': |
95 |
strncpy(domain, optarg, sizeof(domain)); |
STRNCPY(domain, optarg, sizeof(domain)); |
|
break; |
|
|
|
|
|
case 'p': |
|
|
flags |= RDP_LOGON_AUTO; |
|
|
strncpy(password, optarg, sizeof(password)); |
|
96 |
break; |
break; |
97 |
|
|
98 |
case 's': |
case 's': |
99 |
strncpy(shell, optarg, sizeof(shell)); |
STRNCPY(shell, optarg, sizeof(shell)); |
100 |
break; |
break; |
101 |
|
|
102 |
case 'c': |
case 'c': |
103 |
strncpy(directory, optarg, sizeof(directory)); |
STRNCPY(directory, optarg, sizeof(directory)); |
|
break; |
|
|
|
|
|
case 'n': |
|
|
strncpy(hostname, optarg, sizeof(hostname)); |
|
104 |
break; |
break; |
105 |
|
|
106 |
case 'w': |
case 'p': |
107 |
width = strtol(optarg, NULL, 10); |
STRNCPY(password, optarg, sizeof(password)); |
108 |
|
flags |= RDP_LOGON_AUTO; |
109 |
break; |
break; |
110 |
|
|
111 |
case 'h': |
case 'n': |
112 |
height = strtol(optarg, NULL, 10); |
STRNCPY(hostname, optarg, sizeof(hostname)); |
113 |
break; |
break; |
114 |
|
|
115 |
case 'k': |
case 'k': |
116 |
keylayout = strtol(optarg, NULL, 16); |
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; |
break; |
135 |
|
|
136 |
case 'm': |
case 'f': |
137 |
motion = False; |
fullscreen = True; |
138 |
break; |
break; |
139 |
|
|
140 |
case 'b': |
case 'b': |
141 |
orders = False; |
orders = False; |
142 |
break; |
break; |
143 |
|
|
144 |
|
case 'e': |
145 |
|
encryption = False; |
146 |
|
break; |
147 |
|
|
148 |
|
case 'm': |
149 |
|
sendmotion = False; |
150 |
|
break; |
151 |
|
|
152 |
case 'l': |
case 'l': |
153 |
licence = False; |
licence = False; |
154 |
break; |
break; |
155 |
|
|
156 |
|
case 'h': |
157 |
case '?': |
case '?': |
158 |
default: |
default: |
159 |
usage(argv[0]); |
usage(argv[0]); |
174 |
pw = getpwuid(getuid()); |
pw = getpwuid(getuid()); |
175 |
if ((pw == NULL) || (pw->pw_name == NULL)) |
if ((pw == NULL) || (pw->pw_name == NULL)) |
176 |
{ |
{ |
177 |
STATUS("Could not determine user name.\n"); |
error("could not determine username, use -u\n"); |
178 |
return 1; |
return 1; |
179 |
} |
} |
180 |
|
|
181 |
strncpy(username, pw->pw_name, sizeof(username)); |
STRNCPY(username, pw->pw_name, sizeof(username)); |
182 |
} |
} |
183 |
|
|
184 |
if (hostname[0] == 0) |
if (hostname[0] == 0) |
185 |
{ |
{ |
186 |
if (gethostname(hostname, sizeof(hostname)) == -1) |
if (gethostname(fullhostname, sizeof(fullhostname)) == -1) |
187 |
{ |
{ |
188 |
STATUS("Could not determine host name.\n"); |
error("could not determine local hostname, use -n\n"); |
189 |
return 1; |
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 (!rdp_connect(server, flags, domain, password, shell, directory)) |
if (!strcmp(password, "-")) |
200 |
return 1; |
{ |
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 |
STATUS("Connection successful.\n"); |
if ((width == 0) || (height == 0)) |
211 |
|
{ |
212 |
|
width = 800; |
213 |
|
height = 600; |
214 |
|
} |
215 |
|
|
216 |
strcpy(title, "rdesktop - "); |
strcpy(title, "rdesktop - "); |
217 |
strncat(title, server, sizeof(title)); |
strncat(title, server, sizeof(title) - sizeof("rdesktop - ")); |
218 |
|
|
219 |
if (ui_create_window(title)) |
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(); |
rdp_main_loop(); |
227 |
|
printf("Disconnecting...\n"); |
228 |
ui_destroy_window(); |
ui_destroy_window(); |
229 |
} |
} |
230 |
|
|
242 |
int fd; |
int fd; |
243 |
|
|
244 |
/* If we have a kernel random device, use it. */ |
/* If we have a kernel random device, use it. */ |
245 |
if ((fd = open("/dev/urandom", O_RDONLY)) != -1) |
if (((fd = open("/dev/urandom", O_RDONLY)) != -1) |
246 |
|
|| ((fd = open("/dev/random", O_RDONLY)) != -1)) |
247 |
{ |
{ |
248 |
read(fd, random, 32); |
read(fd, random, 32); |
249 |
close(fd); |
close(fd); |
268 |
void *mem = malloc(size); |
void *mem = malloc(size); |
269 |
if (mem == NULL) |
if (mem == NULL) |
270 |
{ |
{ |
271 |
ERROR("xmalloc %d\n", size); |
error("xmalloc %d\n", size); |
272 |
exit(1); |
exit(1); |
273 |
} |
} |
274 |
return mem; |
return mem; |
281 |
void *mem = realloc(oldmem, size); |
void *mem = realloc(oldmem, size); |
282 |
if (mem == NULL) |
if (mem == NULL) |
283 |
{ |
{ |
284 |
ERROR("xrealloc %d\n", size); |
error("xrealloc %d\n", size); |
285 |
exit(1); |
exit(1); |
286 |
} |
} |
287 |
return mem; |
return mem; |
294 |
free(mem); |
free(mem); |
295 |
} |
} |
296 |
|
|
297 |
/* Produce a hex dump */ |
/* report an error */ |
298 |
|
void |
299 |
|
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 |
void |
325 |
hexdump(unsigned char *p, unsigned int len) |
hexdump(unsigned char *p, unsigned int len) |
326 |
{ |
{ |
330 |
|
|
331 |
while (offset < len) |
while (offset < len) |
332 |
{ |
{ |
333 |
STATUS("%04x ", offset); |
printf("%04x ", offset); |
334 |
thisline = len - offset; |
thisline = len - offset; |
335 |
if (thisline > 16) |
if (thisline > 16) |
336 |
thisline = 16; |
thisline = 16; |
337 |
|
|
338 |
for (i = 0; i < thisline; i++) |
for (i = 0; i < thisline; i++) |
339 |
STATUS("%02x ", line[i]) for (; i < 16; i++) |
printf("%02x ", line[i]); |
340 |
STATUS(" "); |
|
341 |
|
for (; i < 16; i++) |
342 |
|
printf(" "); |
343 |
|
|
344 |
for (i = 0; i < thisline; i++) |
for (i = 0; i < thisline; i++) |
345 |
STATUS("%c", |
printf("%c", |
346 |
(line[i] >= 0x20 |
(line[i] >= 0x20 |
347 |
&& line[i] < 0x7f) ? line[i] : '.'); |
&& line[i] < 0x7f) ? line[i] : '.'); |
348 |
|
|
349 |
STATUS("\n"); |
printf("\n"); |
350 |
offset += thisline; |
offset += thisline; |
351 |
line += thisline; |
line += thisline; |
352 |
} |
} |