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