/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 424 - (hide annotations)
Thu Jun 19 07:29:53 2003 UTC (20 years, 11 months ago) by forsberg
File MIME type: text/plain
File size: 13753 byte(s)
Make rdesktop return 0 only if a RDP_PDU_DISCONNECT was received,
otherwise return 2 (except for usage errors, where it still returns
1).

Documented exit codes of rdesktop in doc/exit_codes.txt.

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

  ViewVC Help
Powered by ViewVC 1.1.26