Diffstat (limited to 'noncore/net/opierdesktop/rdesktop.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/opierdesktop/rdesktop.cpp | 313 |
1 files changed, 313 insertions, 0 deletions
diff --git a/noncore/net/opierdesktop/rdesktop.cpp b/noncore/net/opierdesktop/rdesktop.cpp new file mode 100644 index 0000000..1a9a087 --- a/dev/null +++ b/noncore/net/opierdesktop/rdesktop.cpp | |||
@@ -0,0 +1,313 @@ | |||
1 | /* | ||
2 | rdesktop: A Remote Desktop Protocol client. | ||
3 | Entrypoint and utility functions | ||
4 | Copyright (C) Matthew Chapman 1999-2003 | ||
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 <stdarg.h> /* va_list va_start va_end */ | ||
22 | #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */ | ||
23 | #include <fcntl.h> /* open */ | ||
24 | #include <pwd.h> /* getpwuid */ | ||
25 | //#include <termios.h> /* tcgetattr tcsetattr */ | ||
26 | #include <sys/stat.h> /* stat */ | ||
27 | #include <sys/time.h> /* gettimeofday */ | ||
28 | #include <sys/times.h> /* times */ | ||
29 | #include <errno.h> | ||
30 | #include "rdesktop.h" | ||
31 | |||
32 | #ifdef EGD_SOCKET | ||
33 | #include <sys/socket.h> /* socket connect */ | ||
34 | #include <sys/un.h> /* sockaddr_un */ | ||
35 | #endif | ||
36 | |||
37 | //#ifdef WITH_OPENSSL | ||
38 | #include <openssl/md5.h> | ||
39 | //#else | ||
40 | //#include "crypto/md5.h" | ||
41 | //#endif | ||
42 | |||
43 | /*extern "C" */void ui_main_loop(void); | ||
44 | |||
45 | int g_argc; | ||
46 | char ** g_argv; | ||
47 | |||
48 | char title[32] = ""; | ||
49 | char username[16] = "j"; | ||
50 | char hostname[16] = "sharp"; | ||
51 | char keymapname[16]; | ||
52 | int keylayout = 0x409; /* Defaults to US keyboard layout */ | ||
53 | int g_width = 800; /* If width or height are reset to zero, the geometry will | ||
54 | be fetched from _NET_WORKAREA */ | ||
55 | int g_height = 600; | ||
56 | int tcp_port_rdp = TCP_PORT_RDP; | ||
57 | int server_bpp = 8; | ||
58 | int win_button_size = 0;/* If zero, disable single app mode */ | ||
59 | BOOL bitmap_compression = True; | ||
60 | BOOL sendmotion = True; | ||
61 | BOOL orders = True; | ||
62 | BOOL encryption = True; | ||
63 | BOOL desktop_save = True; | ||
64 | BOOL fullscreen = False; | ||
65 | BOOL grab_keyboard = True; | ||
66 | BOOL hide_decorations = False; | ||
67 | extern BOOL owncolmap; | ||
68 | |||
69 | /* report an unimplemented protocol feature */ | ||
70 | void | ||
71 | unimpl(char *format, ...) | ||
72 | { | ||
73 | } | ||
74 | |||
75 | /* malloc; exit if out of memory */ | ||
76 | void * | ||
77 | xmalloc(int size) | ||
78 | { | ||
79 | void *mem = malloc(size); | ||
80 | if (mem == NULL) | ||
81 | { | ||
82 | error("xmalloc %d\n", size); | ||
83 | exit(1); | ||
84 | } | ||
85 | return mem; | ||
86 | } | ||
87 | |||
88 | /* realloc; exit if out of memory */ | ||
89 | void * | ||
90 | xrealloc(void *oldmem, int size) | ||
91 | { | ||
92 | void *mem = realloc(oldmem, size); | ||
93 | if (mem == NULL) | ||
94 | { | ||
95 | error("xrealloc %d\n", size); | ||
96 | exit(1); | ||
97 | } | ||
98 | return mem; | ||
99 | } | ||
100 | |||
101 | /* free */ | ||
102 | void | ||
103 | xfree(void *mem) | ||
104 | { | ||
105 | free(mem); | ||
106 | } | ||
107 | |||
108 | /* report an error */ | ||
109 | void | ||
110 | error(char *format, ...) | ||
111 | { | ||
112 | } | ||
113 | |||
114 | /* report a warning */ | ||
115 | void | ||
116 | warning(char *format, ...) | ||
117 | { | ||
118 | } | ||
119 | |||
120 | /* Generate a 32-byte random for the secure transport code. */ | ||
121 | void | ||
122 | generate_random(uint8 * random) | ||
123 | { | ||
124 | struct stat st; | ||
125 | struct tms tmsbuf; | ||
126 | MD5_CTX md5; | ||
127 | uint32 *r; | ||
128 | int fd, n; | ||
129 | |||
130 | /* If we have a kernel random device, try that first */ | ||
131 | if (((fd = open("/dev/urandom", O_RDONLY)) != -1) | ||
132 | || ((fd = open("/dev/random", O_RDONLY)) != -1)) | ||
133 | { | ||
134 | n = read(fd, random, 32); | ||
135 | close(fd); | ||
136 | if (n == 32) | ||
137 | return; | ||
138 | } | ||
139 | /* Otherwise use whatever entropy we can gather - ideas welcome. */ | ||
140 | r = (uint32 *) random; | ||
141 | r[0] = (getpid()) | (getppid() << 16); | ||
142 | r[1] = (getuid()) | (getgid() << 16); | ||
143 | r[2] = times(&tmsbuf);/* system uptime (clocks) */ | ||
144 | gettimeofday((struct timeval *) &r[3], NULL);/* sec and usec */ | ||
145 | stat("/tmp", &st); | ||
146 | r[5] = st.st_atime; | ||
147 | r[6] = st.st_mtime; | ||
148 | r[7] = st.st_ctime; | ||
149 | |||
150 | /* Hash both halves with MD5 to obscure possible patterns */ | ||
151 | MD5_Init(&md5); | ||
152 | MD5_Update(&md5, random, 16); | ||
153 | MD5_Final(random, &md5); | ||
154 | MD5_Update(&md5, random + 16, 16); | ||
155 | MD5_Final(random + 16, &md5); | ||
156 | } | ||
157 | |||
158 | int | ||
159 | load_licence(unsigned char **data) | ||
160 | { | ||
161 | char *path; | ||
162 | char *home; | ||
163 | struct stat st; | ||
164 | int fd; | ||
165 | |||
166 | home = getenv("HOME"); | ||
167 | if (home == NULL) | ||
168 | return -1; | ||
169 | |||
170 | path = (char*)xmalloc(strlen(home) + strlen(hostname) + 20); | ||
171 | sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); | ||
172 | |||
173 | fd = open(path, O_RDONLY); | ||
174 | if (fd == -1) | ||
175 | return -1; | ||
176 | |||
177 | if (fstat(fd, &st)) | ||
178 | return -1; | ||
179 | |||
180 | *data = (unsigned char*)xmalloc(st.st_size); | ||
181 | return read(fd, *data, st.st_size); | ||
182 | } | ||
183 | |||
184 | void | ||
185 | save_licence(unsigned char *data, int length) | ||
186 | { | ||
187 | char *fpath; /* file path for licence */ | ||
188 | char *fname, *fnamewrk;/* file name for licence .inkl path. */ | ||
189 | char *home; | ||
190 | uint32 y; | ||
191 | struct flock fnfl; | ||
192 | int fnfd, fnwrkfd, i, wlen; | ||
193 | struct stream s, *s_ptr; | ||
194 | uint32 len; | ||
195 | |||
196 | /* Construct a stream, so that we can use macros to extract the | ||
197 | * licence. | ||
198 | */ | ||
199 | s_ptr = &s; | ||
200 | s_ptr->p = data; | ||
201 | /* Skip first two bytes */ | ||
202 | in_uint16(s_ptr, len); | ||
203 | |||
204 | /* Skip three strings */ | ||
205 | for (i = 0; i < 3; i++) | ||
206 | { | ||
207 | in_uint32(s_ptr, len); | ||
208 | s_ptr->p += len; | ||
209 | /* Make sure that we won't be past the end of data after | ||
210 | * reading the next length value | ||
211 | */ | ||
212 | if ((s_ptr->p) + 4 > data + length) | ||
213 | { | ||
214 | printf("Error in parsing licence key.\n"); | ||
215 | printf("Strings %d end value %x > supplied length (%x)\n", i, | ||
216 | (unsigned int) s_ptr->p, (unsigned int) data + length); | ||
217 | return; | ||
218 | } | ||
219 | } | ||
220 | in_uint32(s_ptr, len); | ||
221 | if (s_ptr->p + len > data + length) | ||
222 | { | ||
223 | printf("Error in parsing licence key.\n"); | ||
224 | printf("End of licence %x > supplied length (%x)\n", | ||
225 | (unsigned int) s_ptr->p + len, (unsigned int) data + length); | ||
226 | return; | ||
227 | } | ||
228 | |||
229 | home = getenv("HOME"); | ||
230 | if (home == NULL) | ||
231 | return; | ||
232 | |||
233 | /* set and create the directory -- if it doesn't exist. */ | ||
234 | fpath = (char*)xmalloc(strlen(home) + 11); | ||
235 | STRNCPY(fpath, home, strlen(home) + 1); | ||
236 | |||
237 | sprintf(fpath, "%s/.rdesktop", fpath); | ||
238 | if (mkdir(fpath, 0700) == -1 && errno != EEXIST) | ||
239 | { | ||
240 | perror("mkdir"); | ||
241 | exit(1); | ||
242 | } | ||
243 | |||
244 | /* set the real licence filename, and put a write lock on it. */ | ||
245 | fname = (char*)xmalloc(strlen(fpath) + strlen(hostname) + 10); | ||
246 | sprintf(fname, "%s/licence.%s", fpath, hostname); | ||
247 | fnfd = open(fname, O_RDONLY); | ||
248 | if (fnfd != -1) | ||
249 | { | ||
250 | fnfl.l_type = F_WRLCK; | ||
251 | fnfl.l_whence = SEEK_SET; | ||
252 | fnfl.l_start = 0; | ||
253 | fnfl.l_len = 1; | ||
254 | fcntl(fnfd, F_SETLK, &fnfl); | ||
255 | } | ||
256 | |||
257 | /* create a temporary licence file */ | ||
258 | fnamewrk = (char*)xmalloc(strlen(fname) + 12); | ||
259 | for (y = 0;; y++) | ||
260 | { | ||
261 | sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int) y); | ||
262 | fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600); | ||
263 | if (fnwrkfd == -1) | ||
264 | { | ||
265 | if (errno == EINTR || errno == EEXIST) | ||
266 | continue; | ||
267 | perror("create"); | ||
268 | exit(1); | ||
269 | } | ||
270 | break; | ||
271 | } | ||
272 | /* write to the licence file */ | ||
273 | for (y = 0; y < len;) | ||
274 | { | ||
275 | do | ||
276 | { | ||
277 | wlen = write(fnwrkfd, s_ptr->p + y, len - y); | ||
278 | } | ||
279 | while (wlen == -1 && errno == EINTR); | ||
280 | if (wlen < 1) | ||
281 | { | ||
282 | perror("write"); | ||
283 | unlink(fnamewrk); | ||
284 | exit(1); | ||
285 | } | ||
286 | y += wlen; | ||
287 | } | ||
288 | |||
289 | /* close the file and rename it to fname */ | ||
290 | if (close(fnwrkfd) == -1) | ||
291 | { | ||
292 | perror("close"); | ||
293 | unlink(fnamewrk); | ||
294 | exit(1); | ||
295 | } | ||
296 | if (rename(fnamewrk, fname) == -1) | ||
297 | { | ||
298 | perror("rename"); | ||
299 | unlink(fnamewrk); | ||
300 | exit(1); | ||
301 | } | ||
302 | /* close the file lock on fname */ | ||
303 | if (fnfd != -1) | ||
304 | { | ||
305 | fnfl.l_type = F_UNLCK; | ||
306 | fnfl.l_whence = SEEK_SET; | ||
307 | fnfl.l_start = 0; | ||
308 | fnfl.l_len = 1; | ||
309 | fcntl(fnfd, F_SETLK, &fnfl); | ||
310 | close(fnfd); | ||
311 | } | ||
312 | |||
313 | } | ||