/[rdesktop]/jpeg/rdesktop/trunk/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

Annotation of /jpeg/rdesktop/trunk/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 263 - (hide annotations)
Mon Nov 18 18:12:49 2002 UTC (21 years, 6 months ago) by astrand
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 11132 byte(s)
Support for extended WM hint _NET_WORKAREA, via -g workarea

1 matty 10 /*
2     rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions
4 matthewc 122 Copyright (C) Matthew Chapman 1999-2002
5 jsorg71 100
6 matty 10 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 jsorg71 100
11 matty 10 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 jsorg71 100
16 matty 10 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 matty 30 #include <stdarg.h> /* va_list va_start va_end */
22 matty 24 #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
23     #include <fcntl.h> /* open */
24     #include <pwd.h> /* getpwuid */
25 matthewc 39 #include <limits.h> /* PATH_MAX */
26 matthewc 211 #include <termios.h> /* tcgetattr tcsetattr */
27 matty 24 #include <sys/stat.h> /* stat */
28     #include <sys/time.h> /* gettimeofday */
29     #include <sys/times.h> /* times */
30 matty 10 #include "rdesktop.h"
31    
32 matthewc 220 #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 astrand 107 char title[32] = "";
44 matty 10 char username[16];
45     char hostname[16];
46 matthewc 38 char keymapname[16];
47 astrand 66 int keylayout = 0x409; /* Defaults to US keyboard layout */
48 astrand 263 int width = 800; /* If width or height are reset to zero, the geometry will
49     be fetched from _NET_WORKAREA */
50 matthewc 160 int height = 600;
51 jsorg71 58 int tcp_port_rdp = TCP_PORT_RDP;
52 matty 28 BOOL bitmap_compression = True;
53 matty 29 BOOL sendmotion = True;
54 matty 10 BOOL orders = True;
55 matty 30 BOOL encryption = True;
56 matty 28 BOOL desktop_save = True;
57     BOOL fullscreen = False;
58 astrand 76 BOOL grab_keyboard = True;
59 astrand 262 BOOL hide_decorations = False;
60 matty 10
61     /* Display usage information */
62 matty 25 static void
63     usage(char *program)
64 matty 10 {
65 matthewc 122 fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
66     fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");
67     fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
68    
69 matthewc 222 fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
70 astrand 111 fprintf(stderr, " -u: user name\n");
71     fprintf(stderr, " -d: domain\n");
72     fprintf(stderr, " -s: shell\n");
73     fprintf(stderr, " -c: working directory\n");
74 matthewc 211 fprintf(stderr, " -p: password (- to prompt)\n");
75 astrand 111 fprintf(stderr, " -n: client hostname\n");
76 matthewc 223 fprintf(stderr, " -k: keyboard layout on terminal server (us,sv,gr,etc.)\n");
77 astrand 111 fprintf(stderr, " -g: desktop geometry (WxH)\n");
78     fprintf(stderr, " -f: full-screen mode\n");
79     fprintf(stderr, " -b: force bitmap updates\n");
80     fprintf(stderr, " -e: disable encryption (French TS)\n");
81     fprintf(stderr, " -m: do not send motion events\n");
82     fprintf(stderr, " -K: keep window manager key bindings\n");
83 matthewc 223 fprintf(stderr, " -T: window title\n");
84 astrand 262 fprintf(stderr, " -D: hide window manager decorations\n");
85 matty 10 }
86    
87 matthewc 211 static BOOL
88     read_password(char *password, int size)
89     {
90     struct termios tios;
91     BOOL ret = False;
92     int istty = 0;
93     char *p;
94    
95     if (tcgetattr(STDIN_FILENO, &tios) == 0)
96     {
97     fprintf(stderr, "Password: ");
98     tios.c_lflag &= ~ECHO;
99     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
100     istty = 1;
101     }
102    
103     if (fgets(password, size, stdin) != NULL)
104     {
105     ret = True;
106    
107     /* strip final newline */
108     p = strchr(password, '\n');
109     if (p != NULL)
110     *p = 0;
111     }
112    
113     if (istty)
114     {
115     tios.c_lflag |= ECHO;
116     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
117     fprintf(stderr, "\n");
118     }
119    
120     return ret;
121     }
122    
123 matty 10 /* Client program */
124 matty 25 int
125     main(int argc, char *argv[])
126 matty 10 {
127 matthewc 222 char server[64];
128 matty 30 char fullhostname[64];
129 matty 21 char domain[16];
130     char password[16];
131 astrand 238 char shell[128];
132 matty 21 char directory[32];
133 matthewc 211 BOOL prompt_password;
134 matty 30 struct passwd *pw;
135     uint32 flags;
136 matthewc 222 char *p;
137 matty 10 int c;
138    
139 matty 21 flags = RDP_LOGON_NORMAL;
140 matthewc 211 prompt_password = False;
141 matty 21 domain[0] = password[0] = shell[0] = directory[0] = 0;
142 matthewc 240 strcpy(keymapname, "en-us");
143 matty 21
144 astrand 262 while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemKT:Dh?")) != -1)
145 matty 10 {
146     switch (c)
147     {
148     case 'u':
149 matty 30 STRNCPY(username, optarg, sizeof(username));
150 matty 10 break;
151    
152 matty 21 case 'd':
153 matty 30 STRNCPY(domain, optarg, sizeof(domain));
154 matty 21 break;
155    
156     case 's':
157 matty 30 STRNCPY(shell, optarg, sizeof(shell));
158 matty 21 break;
159    
160     case 'c':
161 matty 30 STRNCPY(directory, optarg, sizeof(directory));
162 matty 21 break;
163    
164 matty 30 case 'p':
165 matthewc 211 if ((optarg[0] == '-') && (optarg[1] == 0))
166     {
167     prompt_password = True;
168     break;
169     }
170    
171 matty 30 STRNCPY(password, optarg, sizeof(password));
172     flags |= RDP_LOGON_AUTO;
173 matthewc 211
174     /* try to overwrite argument so it won't appear in ps */
175 n-ki 171 p = optarg;
176     while (*p)
177     *(p++) = 'X';
178 matty 30 break;
179    
180 matty 10 case 'n':
181 matty 30 STRNCPY(hostname, optarg, sizeof(hostname));
182 matty 10 break;
183    
184     case 'k':
185 astrand 82 STRNCPY(keymapname, optarg, sizeof(keymapname));
186 matty 10 break;
187    
188 matty 30 case 'g':
189 astrand 263 if (!strcmp(optarg, "workarea"))
190     {
191     width = height = 0;
192     break;
193     }
194    
195 matty 30 width = strtol(optarg, &p, 10);
196     if (*p == 'x')
197 astrand 64 height = strtol(p + 1, NULL, 10);
198 matty 30
199     if ((width == 0) || (height == 0))
200     {
201     error("invalid geometry\n");
202     return 1;
203     }
204 matty 10 break;
205    
206 matty 30 case 'f':
207     fullscreen = True;
208     break;
209    
210 matty 10 case 'b':
211     orders = False;
212     break;
213    
214 matty 30 case 'e':
215     encryption = False;
216 matty 10 break;
217    
218 matty 30 case 'm':
219     sendmotion = False;
220 matty 28 break;
221 matty 29
222 astrand 76 case 'K':
223     grab_keyboard = False;
224     break;
225    
226 matthewc 223 case 'T':
227 matthewc 222 STRNCPY(title, optarg, sizeof(title));
228 astrand 107 break;
229    
230 astrand 262 case 'D':
231     hide_decorations = True;
232     break;
233    
234 matty 28 case 'h':
235 matty 10 case '?':
236     default:
237     usage(argv[0]);
238     return 1;
239     }
240     }
241    
242     if (argc - optind < 1)
243     {
244     usage(argv[0]);
245     return 1;
246     }
247    
248 matthewc 222 STRNCPY(server, argv[optind], sizeof(server));
249     p = strchr(server, ':');
250     if (p != NULL)
251     {
252     tcp_port_rdp = strtol(p + 1, NULL, 10);
253     *p = 0;
254     }
255 matty 10
256     if (username[0] == 0)
257     {
258     pw = getpwuid(getuid());
259     if ((pw == NULL) || (pw->pw_name == NULL))
260     {
261 matty 30 error("could not determine username, use -u\n");
262 matty 10 return 1;
263     }
264    
265 matty 30 STRNCPY(username, pw->pw_name, sizeof(username));
266 matty 10 }
267    
268     if (hostname[0] == 0)
269     {
270 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
271 matty 10 {
272 matty 30 error("could not determine local hostname, use -n\n");
273 matty 10 return 1;
274     }
275 matty 30
276     p = strchr(fullhostname, '.');
277     if (p != NULL)
278     *p = 0;
279    
280     STRNCPY(hostname, fullhostname, sizeof(hostname));
281 matty 10 }
282    
283 matthewc 211 if (prompt_password && read_password(password, sizeof(password)))
284     flags |= RDP_LOGON_AUTO;
285 matty 30
286 matthewc 211 if (title[0] == 0)
287 astrand 107 {
288     strcpy(title, "rdesktop - ");
289     strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
290     }
291 matty 12
292 astrand 82 if (!ui_init())
293     return 1;
294 astrand 66
295 matthewc 53 if (!rdp_connect(server, flags, domain, password, shell, directory))
296     return 1;
297    
298 matthewc 122 DEBUG(("Connection successful.\n"));
299 matthewc 211 memset(password, 0, sizeof(password));
300 matthewc 53
301 jsorg71 100 if (ui_create_window())
302 matty 10 {
303     rdp_main_loop();
304     ui_destroy_window();
305     }
306    
307 matthewc 122 DEBUG(("Disconnecting...\n"));
308 matthewc 53 rdp_disconnect();
309 matthewc 188 ui_deinit();
310 matty 10 return 0;
311     }
312    
313 matthewc 220 #ifdef EGD_SOCKET
314     /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
315     static BOOL
316     generate_random_egd(uint8 * buf)
317     {
318     struct sockaddr_un addr;
319     BOOL ret = False;
320     int fd;
321    
322     fd = socket(AF_UNIX, SOCK_STREAM, 0);
323     if (fd == -1)
324     return False;
325    
326     addr.sun_family = AF_UNIX;
327     memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
328 astrand 259 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
329 matthewc 220 goto err;
330    
331     /* PRNGD and EGD use a simple communications protocol */
332 astrand 259 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
333     buf[1] = 32; /* Number of requested random bytes */
334 matthewc 220 if (write(fd, buf, 2) != 2)
335     goto err;
336    
337 astrand 259 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
338 matthewc 220 goto err;
339    
340     if (read(fd, buf, 32) != 32)
341     goto err;
342    
343     ret = True;
344    
345 astrand 259 err:
346 matthewc 220 close(fd);
347     return ret;
348     }
349     #endif
350    
351 matty 10 /* Generate a 32-byte random for the secure transport code. */
352 matty 25 void
353 astrand 64 generate_random(uint8 * random)
354 matty 10 {
355     struct stat st;
356 matty 22 struct tms tmsbuf;
357 matthewc 220 MD5_CTX md5;
358     uint32 *r;
359     int fd, n;
360 matty 10
361 matthewc 220 /* If we have a kernel random device, try that first */
362 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
363     || ((fd = open("/dev/random", O_RDONLY)) != -1))
364 matty 10 {
365 matthewc 220 n = read(fd, random, 32);
366 matty 10 close(fd);
367 matthewc 220 if (n == 32)
368     return;
369 matty 10 }
370    
371 matthewc 220 #ifdef EGD_SOCKET
372     /* As a second preference use an EGD */
373     if (generate_random_egd(random))
374     return;
375     #endif
376    
377 matty 10 /* Otherwise use whatever entropy we can gather - ideas welcome. */
378 astrand 259 r = (uint32 *) random;
379 matty 10 r[0] = (getpid()) | (getppid() << 16);
380     r[1] = (getuid()) | (getgid() << 16);
381 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
382     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
383 matty 10 stat("/tmp", &st);
384     r[5] = st.st_atime;
385     r[6] = st.st_mtime;
386     r[7] = st.st_ctime;
387 matthewc 220
388     /* Hash both halves with MD5 to obscure possible patterns */
389     MD5_Init(&md5);
390 astrand 259 MD5_Update(&md5, random, 16);
391 matthewc 220 MD5_Final(random, &md5);
392 astrand 259 MD5_Update(&md5, random + 16, 16);
393     MD5_Final(random + 16, &md5);
394 matty 10 }
395    
396     /* malloc; exit if out of memory */
397 matty 25 void *
398     xmalloc(int size)
399 matty 10 {
400     void *mem = malloc(size);
401     if (mem == NULL)
402     {
403 matty 30 error("xmalloc %d\n", size);
404 matty 10 exit(1);
405     }
406     return mem;
407     }
408    
409     /* realloc; exit if out of memory */
410 matty 25 void *
411     xrealloc(void *oldmem, int size)
412 matty 10 {
413     void *mem = realloc(oldmem, size);
414     if (mem == NULL)
415     {
416 matty 30 error("xrealloc %d\n", size);
417 matty 10 exit(1);
418     }
419     return mem;
420     }
421    
422     /* free */
423 matty 25 void
424     xfree(void *mem)
425 matty 10 {
426     free(mem);
427     }
428    
429 matty 30 /* report an error */
430 matty 25 void
431 matty 30 error(char *format, ...)
432     {
433     va_list ap;
434    
435     fprintf(stderr, "ERROR: ");
436    
437     va_start(ap, format);
438     vfprintf(stderr, format, ap);
439     va_end(ap);
440     }
441    
442     /* report an unimplemented protocol feature */
443     void
444     unimpl(char *format, ...)
445     {
446     va_list ap;
447    
448     fprintf(stderr, "NOT IMPLEMENTED: ");
449    
450     va_start(ap, format);
451     vfprintf(stderr, format, ap);
452     va_end(ap);
453     }
454    
455     /* produce a hex dump */
456     void
457 matty 25 hexdump(unsigned char *p, unsigned int len)
458 matty 10 {
459     unsigned char *line = p;
460     unsigned int thisline, offset = 0;
461     int i;
462    
463     while (offset < len)
464     {
465 matthewc 169 printf("%04x ", offset);
466 matty 10 thisline = len - offset;
467     if (thisline > 16)
468     thisline = 16;
469    
470     for (i = 0; i < thisline; i++)
471 matthewc 169 printf("%02x ", line[i]);
472 matty 10
473 matty 30 for (; i < 16; i++)
474 matthewc 169 printf(" ");
475 matty 30
476 matty 10 for (i = 0; i < thisline; i++)
477 matthewc 169 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
478 matty 10
479 matthewc 169 printf("\n");
480 matty 10 offset += thisline;
481     line += thisline;
482     }
483     }
484 matthewc 39
485 matthewc 159 #ifdef SAVE_LICENCE
486 matthewc 39 int
487     load_licence(unsigned char **data)
488     {
489     char path[PATH_MAX];
490     char *home;
491     struct stat st;
492     int fd;
493    
494     home = getenv("HOME");
495     if (home == NULL)
496     return -1;
497    
498     STRNCPY(path, home, sizeof(path));
499 astrand 64 strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1);
500 matthewc 39
501     fd = open(path, O_RDONLY);
502     if (fd == -1)
503     return -1;
504    
505     if (fstat(fd, &st))
506     return -1;
507    
508     *data = xmalloc(st.st_size);
509     return read(fd, *data, st.st_size);
510     }
511    
512     void
513     save_licence(unsigned char *data, int length)
514     {
515     char path[PATH_MAX];
516     char *home;
517     int fd;
518    
519     home = getenv("HOME");
520     if (home == NULL)
521     return;
522    
523     STRNCPY(path, home, sizeof(path));
524 astrand 64 strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1);
525 matthewc 39 mkdir(path, 0700);
526    
527 astrand 64 strncat(path, "/licence", sizeof(path) - strlen(path) - 1);
528 matthewc 39
529 astrand 64 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
530 matthewc 39 if (fd == -1)
531     {
532     perror("open");
533     return;
534     }
535    
536     write(fd, data, length);
537     close(fd);
538     }
539 matthewc 159 #endif

  ViewVC Help
Powered by ViewVC 1.1.26