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