/[rdesktop]/sourceforge.net/trunk/rdesktop/rdesktop.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 39 - (show annotations)
Fri Apr 5 07:57:43 2002 UTC (22 years, 1 month ago) by matthewc
File MIME type: text/plain
File size: 8066 byte(s)
Code to save and load licence (not quite working yet - server still seems
to reissue the licence).
Small clean-up in xkeymap.

1 /*
2 rdesktop: A Remote Desktop Protocol client.
3 Entrypoint and utility functions
4 Copyright (C) Matthew Chapman 1999-2001
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 <stdarg.h> /* va_list va_start va_end */
23 #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
24 #include <fcntl.h> /* open */
25 #include <pwd.h> /* getpwuid */
26 #include <limits.h> /* PATH_MAX */
27 #include <sys/stat.h> /* stat */
28 #include <sys/time.h> /* gettimeofday */
29 #include <sys/times.h> /* times */
30 #include "rdesktop.h"
31
32 char username[16];
33 char hostname[16];
34 char keymapname[16];
35 int keylayout;
36 int width;
37 int height;
38 BOOL bitmap_compression = True;
39 BOOL sendmotion = True;
40 BOOL orders = True;
41 BOOL licence = True;
42 BOOL encryption = True;
43 BOOL desktop_save = True;
44 BOOL fullscreen = False;
45
46 /* Display usage information */
47 static void
48 usage(char *program)
49 {
50 printf("Usage: %s [options] server\n", program);
51 printf(" -u: user name\n");
52 printf(" -d: domain\n");
53 printf(" -s: shell\n");
54 printf(" -c: working directory\n");
55 printf(" -p: password (autologon)\n");
56 printf(" -n: client hostname\n");
57 printf(" -k: keyboard layout\n");
58 printf(" -g: desktop geometry (WxH)\n");
59 printf(" -f: full-screen mode\n");
60 printf(" -b: force bitmap updates\n");
61 printf(" -e: disable encryption (French TS)\n");
62 printf(" -m: do not send motion events\n");
63 printf(" -l: do not request licence\n\n");
64 }
65
66 /* Client program */
67 int
68 main(int argc, char *argv[])
69 {
70 char fullhostname[64];
71 char domain[16];
72 char password[16];
73 char shell[32];
74 char directory[32];
75 char title[32];
76 struct passwd *pw;
77 char *server, *p;
78 uint32 flags;
79 int c;
80
81 printf("rdesktop: A Remote Desktop Protocol client.\n");
82 printf("Version " VERSION ". Copyright (C) 1999-2001 Matt Chapman.\n");
83 printf("See http://www.rdesktop.org/ for more information.\n\n");
84
85 flags = RDP_LOGON_NORMAL;
86 domain[0] = password[0] = shell[0] = directory[0] = 0;
87 strcpy(keymapname, "us");
88
89 while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemlh?")) != -1)
90 {
91 switch (c)
92 {
93 case 'u':
94 STRNCPY(username, optarg, sizeof(username));
95 break;
96
97 case 'd':
98 STRNCPY(domain, optarg, sizeof(domain));
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 'p':
110 STRNCPY(password, optarg, sizeof(password));
111 flags |= RDP_LOGON_AUTO;
112 break;
113
114 case 'n':
115 STRNCPY(hostname, optarg, sizeof(hostname));
116 break;
117
118 case 'k':
119 STRNCPY(keymapname, optarg, sizeof(keymapname));
120 break;
121
122 case 'g':
123 width = strtol(optarg, &p, 10);
124 if (*p == 'x')
125 height = strtol(p+1, NULL, 10);
126
127 if ((width == 0) || (height == 0))
128 {
129 error("invalid geometry\n");
130 return 1;
131 }
132 break;
133
134 case 'f':
135 fullscreen = True;
136 break;
137
138 case 'b':
139 orders = False;
140 break;
141
142 case 'e':
143 encryption = False;
144 break;
145
146 case 'm':
147 sendmotion = False;
148 break;
149
150 case 'l':
151 licence = False;
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 error("could not determine username, use -u\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(fullhostname, sizeof(fullhostname)) == -1)
185 {
186 error("could not determine local hostname, use -n\n");
187 return 1;
188 }
189
190 p = strchr(fullhostname, '.');
191 if (p != NULL)
192 *p = 0;
193
194 STRNCPY(hostname, fullhostname, sizeof(hostname));
195 }
196
197 if (!strcmp(password, "-"))
198 {
199 p = getpass("Password: ");
200 if (p == NULL)
201 {
202 error("failed to read password\n");
203 return 0;
204 }
205 STRNCPY(password, p, sizeof(password));
206 }
207
208 if ((width == 0) || (height == 0))
209 {
210 width = 800;
211 height = 600;
212 }
213
214 strcpy(title, "rdesktop - ");
215 strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
216
217 if (ui_create_window(title))
218 {
219 if (!rdp_connect(server, flags, domain, password, shell,
220 directory))
221 return 1;
222
223 printf("Connection successful.\n");
224 rdp_main_loop();
225 printf("Disconnecting...\n");
226 ui_destroy_window();
227 rdp_disconnect();
228 }
229
230 return 0;
231 }
232
233 /* Generate a 32-byte random for the secure transport code. */
234 void
235 generate_random(uint8 *random)
236 {
237 struct stat st;
238 struct tms tmsbuf;
239 uint32 *r = (uint32 *) random;
240 int fd;
241
242 /* If we have a kernel random device, use it. */
243 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
244 || ((fd = open("/dev/random", O_RDONLY)) != -1))
245 {
246 read(fd, random, 32);
247 close(fd);
248 return;
249 }
250
251 /* Otherwise use whatever entropy we can gather - ideas welcome. */
252 r[0] = (getpid()) | (getppid() << 16);
253 r[1] = (getuid()) | (getgid() << 16);
254 r[2] = times(&tmsbuf); /* system uptime (clocks) */
255 gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
256 stat("/tmp", &st);
257 r[5] = st.st_atime;
258 r[6] = st.st_mtime;
259 r[7] = st.st_ctime;
260 }
261
262 /* malloc; exit if out of memory */
263 void *
264 xmalloc(int size)
265 {
266 void *mem = malloc(size);
267 if (mem == NULL)
268 {
269 error("xmalloc %d\n", size);
270 exit(1);
271 }
272 return mem;
273 }
274
275 /* realloc; exit if out of memory */
276 void *
277 xrealloc(void *oldmem, int size)
278 {
279 void *mem = realloc(oldmem, size);
280 if (mem == NULL)
281 {
282 error("xrealloc %d\n", size);
283 exit(1);
284 }
285 return mem;
286 }
287
288 /* free */
289 void
290 xfree(void *mem)
291 {
292 free(mem);
293 }
294
295 /* report an error */
296 void
297 error(char *format, ...)
298 {
299 va_list ap;
300
301 fprintf(stderr, "ERROR: ");
302
303 va_start(ap, format);
304 vfprintf(stderr, format, ap);
305 va_end(ap);
306 }
307
308 /* report an unimplemented protocol feature */
309 void
310 unimpl(char *format, ...)
311 {
312 va_list ap;
313
314 fprintf(stderr, "NOT IMPLEMENTED: ");
315
316 va_start(ap, format);
317 vfprintf(stderr, format, ap);
318 va_end(ap);
319 }
320
321 /* produce a hex dump */
322 void
323 hexdump(unsigned char *p, unsigned int len)
324 {
325 unsigned char *line = p;
326 unsigned int thisline, offset = 0;
327 int i;
328
329 while (offset < len)
330 {
331 printf("%04x ", offset);
332 thisline = len - offset;
333 if (thisline > 16)
334 thisline = 16;
335
336 for (i = 0; i < thisline; i++)
337 printf("%02x ", line[i]);
338
339 for (; i < 16; i++)
340 printf(" ");
341
342 for (i = 0; i < thisline; i++)
343 printf("%c",
344 (line[i] >= 0x20
345 && line[i] < 0x7f) ? line[i] : '.');
346
347 printf("\n");
348 offset += thisline;
349 line += thisline;
350 }
351 }
352
353 int
354 load_licence(unsigned char **data)
355 {
356 char path[PATH_MAX];
357 char *home;
358 struct stat st;
359 int fd;
360
361 home = getenv("HOME");
362 if (home == NULL)
363 return -1;
364
365 STRNCPY(path, home, sizeof(path));
366 strncat(path, "/.rdesktop/licence", sizeof(path)-strlen(path)-1);
367
368 fd = open(path, O_RDONLY);
369 if (fd == -1)
370 return -1;
371
372 if (fstat(fd, &st))
373 return -1;
374
375 *data = xmalloc(st.st_size);
376 return read(fd, *data, st.st_size);
377 }
378
379 void
380 save_licence(unsigned char *data, int length)
381 {
382 char path[PATH_MAX];
383 char *home;
384 int fd;
385
386 home = getenv("HOME");
387 if (home == NULL)
388 return;
389
390 STRNCPY(path, home, sizeof(path));
391 strncat(path, "/.rdesktop", sizeof(path)-strlen(path)-1);
392 mkdir(path, 0700);
393
394 strncat(path, "/licence", sizeof(path)-strlen(path)-1);
395
396 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0600);
397 if (fd == -1)
398 {
399 perror("open");
400 return;
401 }
402
403 write(fd, data, length);
404 close(fd);
405 }
406

  ViewVC Help
Powered by ViewVC 1.1.26