1 |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Entrypoint and utility functions |
4 |
Copyright (C) Matthew Chapman 1999-2000 |
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 <unistd.h> /* read close getuid getgid getpid getppid gethostname */ |
23 |
#include <fcntl.h> /* open */ |
24 |
#include <pwd.h> /* getpwuid */ |
25 |
#include <sys/stat.h> /* stat */ |
26 |
#include <sys/time.h> /* gettimeofday */ |
27 |
#include <sys/times.h> /* times */ |
28 |
#include "rdesktop.h" |
29 |
|
30 |
char username[16]; |
31 |
char hostname[16]; |
32 |
int width = 800; |
33 |
int height = 600; |
34 |
int keylayout = 0x409; |
35 |
BOOL bitmap_compression = True; |
36 |
BOOL sendmotion = True; |
37 |
BOOL orders = True; |
38 |
BOOL licence = True; |
39 |
BOOL use_encryption = True; |
40 |
BOOL desktop_save = True; |
41 |
BOOL fullscreen = False; |
42 |
|
43 |
/* Display usage information */ |
44 |
static void |
45 |
usage(char *program) |
46 |
{ |
47 |
STATUS("Usage: %s [options] server\n", program); |
48 |
STATUS(" -u: user name\n"); |
49 |
STATUS(" -d: domain\n"); |
50 |
STATUS(" -s: shell\n"); |
51 |
STATUS(" -c: working directory\n"); |
52 |
STATUS(" -p: password (autologon)\n"); |
53 |
STATUS(" -n: client hostname\n"); |
54 |
STATUS(" -w: desktop width\n"); |
55 |
STATUS(" -h: desktop height\n"); |
56 |
STATUS(" -k: keyboard layout (hex)\n"); |
57 |
STATUS(" -b: force bitmap updates\n"); |
58 |
STATUS(" -m: do not send motion events\n"); |
59 |
STATUS(" -l: do not request licence\n\n"); |
60 |
} |
61 |
|
62 |
/* Client program */ |
63 |
int |
64 |
main(int argc, char *argv[]) |
65 |
{ |
66 |
struct passwd *pw; |
67 |
char *server; |
68 |
uint32 flags; |
69 |
char domain[16]; |
70 |
char password[16]; |
71 |
char shell[32]; |
72 |
char directory[32]; |
73 |
char title[32]; |
74 |
int c; |
75 |
|
76 |
STATUS("rdesktop: A Remote Desktop Protocol client.\n"); |
77 |
STATUS("Version " VERSION |
78 |
". Copyright (C) 1999-2000 Matt Chapman.\n"); |
79 |
STATUS("See http://www.rdesktop.org/ for more information.\n\n"); |
80 |
|
81 |
flags = RDP_LOGON_NORMAL; |
82 |
domain[0] = password[0] = shell[0] = directory[0] = 0; |
83 |
|
84 |
while ((c = getopt(argc, argv, "u:d:s:c:p:n:g:k:mbleKFVh?")) != -1) |
85 |
{ |
86 |
switch (c) |
87 |
{ |
88 |
case 'u': |
89 |
strncpy(username, optarg, sizeof(username)); |
90 |
break; |
91 |
|
92 |
case 'd': |
93 |
strncpy(domain, optarg, sizeof(domain)); |
94 |
break; |
95 |
|
96 |
case 'p': |
97 |
flags |= RDP_LOGON_AUTO; |
98 |
strncpy(password, optarg, sizeof(password)); |
99 |
break; |
100 |
|
101 |
case 's': |
102 |
strncpy(shell, optarg, sizeof(shell)); |
103 |
break; |
104 |
|
105 |
case 'c': |
106 |
strncpy(directory, optarg, sizeof(directory)); |
107 |
break; |
108 |
|
109 |
case 'n': |
110 |
strncpy(hostname, optarg, sizeof(hostname)); |
111 |
break; |
112 |
case 'g': |
113 |
{ |
114 |
char *tgem = 0; |
115 |
width = strtol(optarg, NULL, 10); |
116 |
tgem = strchr(optarg, 'x'); |
117 |
if ((tgem == 0) || (strlen(tgem) < 2)) |
118 |
{ |
119 |
ERROR |
120 |
("-g: invalid parameter. Syntax example: -g 1024x768\n"); |
121 |
exit(1); |
122 |
} |
123 |
height = strtol(tgem + 1, NULL, 10); |
124 |
} |
125 |
break; |
126 |
|
127 |
case 'k': |
128 |
keylayout = strtol(optarg, NULL, 16); |
129 |
/* keylayout = find_keyb_code(optarg); */ |
130 |
if (keylayout == 0) |
131 |
return 0; |
132 |
break; |
133 |
|
134 |
case 'm': |
135 |
sendmotion = False; |
136 |
break; |
137 |
|
138 |
case 'b': |
139 |
orders = False; |
140 |
break; |
141 |
|
142 |
case 'l': |
143 |
licence = False; |
144 |
break; |
145 |
|
146 |
case 'e': |
147 |
use_encryption = False; |
148 |
break; |
149 |
|
150 |
case 'F': |
151 |
fullscreen = True; |
152 |
break; |
153 |
|
154 |
case 'h': |
155 |
case '?': |
156 |
default: |
157 |
usage(argv[0]); |
158 |
return 1; |
159 |
} |
160 |
} |
161 |
|
162 |
if (argc - optind < 1) |
163 |
{ |
164 |
usage(argv[0]); |
165 |
return 1; |
166 |
} |
167 |
|
168 |
server = argv[optind]; |
169 |
|
170 |
if (username[0] == 0) |
171 |
{ |
172 |
pw = getpwuid(getuid()); |
173 |
if ((pw == NULL) || (pw->pw_name == NULL)) |
174 |
{ |
175 |
STATUS("Could not determine user name.\n"); |
176 |
return 1; |
177 |
} |
178 |
|
179 |
strncpy(username, pw->pw_name, sizeof(username)); |
180 |
} |
181 |
|
182 |
if (hostname[0] == 0) |
183 |
{ |
184 |
if (gethostname(hostname, sizeof(hostname)) == -1) |
185 |
{ |
186 |
STATUS("Could not determine host name.\n"); |
187 |
return 1; |
188 |
} |
189 |
} |
190 |
|
191 |
strcpy(title, "rdesktop - "); |
192 |
strncat(title, server, sizeof(title)); |
193 |
|
194 |
if (ui_create_window(title)) |
195 |
{ |
196 |
if (!rdp_connect(server, flags, domain, password, shell, |
197 |
directory)) |
198 |
return 1; |
199 |
|
200 |
STATUS("Connection successful.\n"); |
201 |
rdp_main_loop(); |
202 |
ui_destroy_window(); |
203 |
} |
204 |
|
205 |
rdp_disconnect(); |
206 |
return 0; |
207 |
} |
208 |
|
209 |
/* Generate a 32-byte random for the secure transport code. */ |
210 |
void |
211 |
generate_random(uint8 *random) |
212 |
{ |
213 |
struct stat st; |
214 |
struct tms tmsbuf; |
215 |
uint32 *r = (uint32 *) random; |
216 |
int fd; |
217 |
|
218 |
/* If we have a kernel random device, use it. */ |
219 |
if ((fd = open("/dev/urandom", O_RDONLY)) != -1) |
220 |
{ |
221 |
read(fd, random, 32); |
222 |
close(fd); |
223 |
return; |
224 |
} |
225 |
|
226 |
/* Otherwise use whatever entropy we can gather - ideas welcome. */ |
227 |
r[0] = (getpid()) | (getppid() << 16); |
228 |
r[1] = (getuid()) | (getgid() << 16); |
229 |
r[2] = times(&tmsbuf); /* system uptime (clocks) */ |
230 |
gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */ |
231 |
stat("/tmp", &st); |
232 |
r[5] = st.st_atime; |
233 |
r[6] = st.st_mtime; |
234 |
r[7] = st.st_ctime; |
235 |
} |
236 |
|
237 |
/* malloc; exit if out of memory */ |
238 |
void * |
239 |
xmalloc(int size) |
240 |
{ |
241 |
void *mem = malloc(size); |
242 |
if (mem == NULL) |
243 |
{ |
244 |
ERROR("xmalloc %d\n", size); |
245 |
exit(1); |
246 |
} |
247 |
return mem; |
248 |
} |
249 |
|
250 |
/* realloc; exit if out of memory */ |
251 |
void * |
252 |
xrealloc(void *oldmem, int size) |
253 |
{ |
254 |
void *mem = realloc(oldmem, size); |
255 |
if (mem == NULL) |
256 |
{ |
257 |
ERROR("xrealloc %d\n", size); |
258 |
exit(1); |
259 |
} |
260 |
return mem; |
261 |
} |
262 |
|
263 |
/* free */ |
264 |
void |
265 |
xfree(void *mem) |
266 |
{ |
267 |
free(mem); |
268 |
} |
269 |
|
270 |
/* Produce a hex dump */ |
271 |
void |
272 |
hexdump(unsigned char *p, unsigned int len) |
273 |
{ |
274 |
unsigned char *line = p; |
275 |
unsigned int thisline, offset = 0; |
276 |
int i; |
277 |
|
278 |
while (offset < len) |
279 |
{ |
280 |
STATUS("%04x ", offset); |
281 |
thisline = len - offset; |
282 |
if (thisline > 16) |
283 |
thisline = 16; |
284 |
|
285 |
for (i = 0; i < thisline; i++) |
286 |
STATUS("%02x ", line[i]) for (; i < 16; i++) |
287 |
STATUS(" "); |
288 |
|
289 |
for (i = 0; i < thisline; i++) |
290 |
STATUS("%c", |
291 |
(line[i] >= 0x20 |
292 |
&& line[i] < 0x7f) ? line[i] : '.'); |
293 |
|
294 |
STATUS("\n"); |
295 |
offset += thisline; |
296 |
line += thisline; |
297 |
} |
298 |
} |