/[rdesktop]/sourceforge.net/branches/seamlessrdp-branch/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/branches/seamlessrdp-branch/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 825 - (hide annotations)
Wed Mar 2 10:16:31 2005 UTC (19 years, 2 months ago) by stargo
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 26248 byte(s)
don't allow rdp-compression for depth > 8

1 forsberg 350 /* -*- c-basic-offset: 8 -*-
2 matty 10 rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions
4 n-ki 569 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 stargo 570 #include <ctype.h> /* toupper */
30 astrand 325 #include <errno.h>
31 matty 10 #include "rdesktop.h"
32    
33 matthewc 220 #ifdef EGD_SOCKET
34     #include <sys/socket.h> /* socket connect */
35     #include <sys/un.h> /* sockaddr_un */
36     #endif
37    
38     #include <openssl/md5.h>
39    
40 astrand 479 char g_title[64] = "";
41     char g_username[64];
42 jsorg71 710 char g_hostname[16];
43 matthewc 38 char keymapname[16];
44 jsorg71 710 int g_keylayout = 0x409; /* Defaults to US keyboard layout */
45 astrand 500
46     int g_width = 800; /* width is special: If 0, the
47     geometry will be fetched from
48     _NET_WORKAREA. If negative,
49     absolute value specifies the
50     percent of the whole screen. */
51 jsorg71 447 int g_height = 600;
52 stargo 800 int g_xpos = 0;
53     int g_ypos = 0;
54 jsorg71 710 extern int g_tcp_port_rdp;
55 jsorg71 438 int g_server_bpp = 8;
56 jsorg71 450 int g_win_button_size = 0; /* If zero, disable single app mode */
57 jsorg71 437 BOOL g_bitmap_compression = True;
58 jsorg71 447 BOOL g_sendmotion = True;
59 jsorg71 678 BOOL g_bitmap_cache = True;
60 jsorg71 725 BOOL g_bitmap_cache_persist_enable = False;
61     BOOL g_bitmap_cache_precache = True;
62 jsorg71 437 BOOL g_encryption = True;
63 forsberg 427 BOOL packet_encryption = True;
64 jsorg71 438 BOOL g_desktop_save = True;
65 jsorg71 447 BOOL g_fullscreen = False;
66 jsorg71 450 BOOL g_grab_keyboard = True;
67     BOOL g_hide_decorations = False;
68 astrand 458 BOOL g_use_rdp5 = True;
69 matthewc 482 BOOL g_console_session = False;
70 astrand 552 BOOL g_numlock_sync = False;
71 stargo 648 BOOL g_owncolmap = False;
72 astrand 651 BOOL g_ownbackstore = True; /* We can't rely on external BackingStore */
73 stargo 648 uint32 g_embed_wnd;
74 astrand 651 uint32 g_rdp5_performanceflags =
75     RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS;
76 matty 10
77 stargo 501 #ifdef WITH_RDPSND
78 matthewc 520 BOOL g_rdpsnd = False;
79 stargo 501 #endif
80    
81 n-ki 569 extern RDPDR_DEVICE g_rdpdr_device[];
82     extern uint32 g_num_devices;
83 astrand 651 extern char *g_rdpdr_clientname;
84 n-ki 569
85 astrand 333 #ifdef RDP2VNC
86     extern int rfb_port;
87     extern int defer_time;
88     void
89     rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
90     char *shell, char *directory);
91     #endif
92 matty 10 /* Display usage information */
93 matty 25 static void
94     usage(char *program)
95 matty 10 {
96 matthewc 122 fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
97 matthewc 304 fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
98 matthewc 122 fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
99    
100 matthewc 222 fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
101 astrand 333 #ifdef RDP2VNC
102     fprintf(stderr, " -V: vnc port\n");
103 forsberg 471 fprintf(stderr, " -Q: defer time (ms)\n");
104 astrand 333 #endif
105 astrand 111 fprintf(stderr, " -u: user name\n");
106     fprintf(stderr, " -d: domain\n");
107     fprintf(stderr, " -s: shell\n");
108     fprintf(stderr, " -c: working directory\n");
109 matthewc 211 fprintf(stderr, " -p: password (- to prompt)\n");
110 astrand 111 fprintf(stderr, " -n: client hostname\n");
111 matthewc 520 fprintf(stderr, " -k: keyboard layout on server (en-us, de, sv, etc.)\n");
112 astrand 111 fprintf(stderr, " -g: desktop geometry (WxH)\n");
113     fprintf(stderr, " -f: full-screen mode\n");
114     fprintf(stderr, " -b: force bitmap updates\n");
115 stargo 609 fprintf(stderr, " -B: use BackingStore of X-server (if available)\n");
116 astrand 111 fprintf(stderr, " -e: disable encryption (French TS)\n");
117 forsberg 471 fprintf(stderr, " -E: disable encryption from client to server\n");
118 astrand 111 fprintf(stderr, " -m: do not send motion events\n");
119 n-ki 279 fprintf(stderr, " -C: use private colour map\n");
120 matthewc 520 fprintf(stderr, " -D: hide window manager decorations\n");
121 astrand 111 fprintf(stderr, " -K: keep window manager key bindings\n");
122 matthewc 520 fprintf(stderr, " -S: caption button size (single application mode)\n");
123 matthewc 223 fprintf(stderr, " -T: window title\n");
124 n-ki 569 fprintf(stderr, " -N: enable numlock syncronization\n");
125 stargo 636 fprintf(stderr, " -X: embed into another window with a given id.\n");
126 matthewc 520 fprintf(stderr, " -a: connection colour depth\n");
127 jsorg71 773 fprintf(stderr, " -z: enable rdp compression\n");
128 jsorg71 725 fprintf(stderr, " -x: RDP5 experience (m[odem 28.8], b[roadband], l[an] or hex nr.)\n");
129     fprintf(stderr, " -P: use persistent bitmap caching\n");
130 n-ki 569 fprintf(stderr, " -r: enable specified device redirection (this flag can be repeated)\n");
131 astrand 608 fprintf(stderr,
132     " '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");
133 n-ki 578 fprintf(stderr, " or COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");
134 astrand 608 fprintf(stderr,
135 astrand 747 " '-r disk:floppy=/mnt/floppy': enable redirection of /mnt/floppy to 'floppy' share\n");
136     fprintf(stderr, " or 'floppy=/mnt/floppy,cdrom=/mnt/cdrom'\n");
137 forsberg 646 fprintf(stderr, " '-r clientname=<client name>': Set the client name displayed\n");
138     fprintf(stderr, " for redirected disks\n");
139 astrand 608 fprintf(stderr,
140     " '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n");
141 n-ki 578 fprintf(stderr, " or LPT1=/dev/lp0,LPT2=/dev/lp1\n");
142 n-ki 569 fprintf(stderr, " '-r printer:mydeskjet': enable printer redirection\n");
143 astrand 608 fprintf(stderr,
144 n-ki 628 " or mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");
145 n-ki 630 fprintf(stderr, " '-r sound:[local|off|remote]': enable sound redirection\n");
146 n-ki 628 fprintf(stderr, " remote would leave sound on server\n");
147 matthewc 482 fprintf(stderr, " -0: attach to console\n");
148     fprintf(stderr, " -4: use RDP version 4\n");
149     fprintf(stderr, " -5: use RDP version 5 (default)\n");
150 matty 10 }
151    
152 astrand 676 void
153     print_disconnect_reason(uint16 reason)
154     {
155     char *text;
156    
157     switch (reason)
158     {
159     case exDiscReasonNoInfo:
160     text = "No information available";
161     break;
162    
163     case exDiscReasonAPIInitiatedDisconnect:
164     text = "Server initiated disconnect";
165     break;
166    
167     case exDiscReasonAPIInitiatedLogoff:
168     text = "Server initiated logoff";
169     break;
170    
171     case exDiscReasonServerIdleTimeout:
172     text = "Server idle timeout reached";
173     break;
174    
175     case exDiscReasonServerLogonTimeout:
176     text = "Server logon timeout reached";
177     break;
178    
179     case exDiscReasonReplacedByOtherConnection:
180     text = "The session was replaced";
181     break;
182    
183     case exDiscReasonOutOfMemory:
184     text = "The server is out of memory";
185     break;
186    
187     case exDiscReasonServerDeniedConnection:
188     text = "The server denied the connection";
189     break;
190    
191     case exDiscReasonServerDeniedConnectionFips:
192     text = "The server denied the connection for security reason";
193     break;
194    
195     case exDiscReasonLicenseInternal:
196     text = "Internal licensing error";
197     break;
198    
199     case exDiscReasonLicenseNoLicenseServer:
200     text = "No license server available";
201     break;
202    
203     case exDiscReasonLicenseNoLicense:
204     text = "No valid license available";
205     break;
206    
207     case exDiscReasonLicenseErrClientMsg:
208     text = "Invalid licensing message";
209     break;
210    
211     case exDiscReasonLicenseHwidDoesntMatchLicense:
212     text = "Hardware id doesn't match software license";
213     break;
214    
215     case exDiscReasonLicenseErrClientLicense:
216     text = "Client license error";
217     break;
218    
219     case exDiscReasonLicenseCantFinishProtocol:
220     text = "Network error during licensing protocol";
221     break;
222    
223     case exDiscReasonLicenseClientEndedProtocol:
224     text = "Licensing protocol was not completed";
225     break;
226    
227     case exDiscReasonLicenseErrClientEncryption:
228     text = "Incorrect client license enryption";
229     break;
230    
231     case exDiscReasonLicenseCantUpgradeLicense:
232     text = "Can't upgrade license";
233     break;
234    
235     case exDiscReasonLicenseNoRemoteConnections:
236     text = "The server is not licensed to accept remote connections";
237     break;
238    
239     default:
240     if (reason > 0x1000 && reason < 0x7fff)
241     {
242     text = "Internal protocol error";
243     }
244     else
245     {
246     text = "Unknown reason";
247     }
248     }
249     fprintf(stderr, "disconnect: %s.\n", text);
250     }
251    
252 matthewc 211 static BOOL
253     read_password(char *password, int size)
254     {
255     struct termios tios;
256     BOOL ret = False;
257     int istty = 0;
258     char *p;
259    
260     if (tcgetattr(STDIN_FILENO, &tios) == 0)
261     {
262     fprintf(stderr, "Password: ");
263     tios.c_lflag &= ~ECHO;
264     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
265     istty = 1;
266     }
267    
268     if (fgets(password, size, stdin) != NULL)
269     {
270     ret = True;
271    
272     /* strip final newline */
273     p = strchr(password, '\n');
274     if (p != NULL)
275     *p = 0;
276     }
277    
278     if (istty)
279     {
280     tios.c_lflag |= ECHO;
281     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
282     fprintf(stderr, "\n");
283     }
284    
285     return ret;
286     }
287    
288 astrand 444 static void
289     parse_server_and_port(char *server)
290     {
291     char *p;
292     #ifdef IPv6
293     int addr_colons;
294     #endif
295    
296     #ifdef IPv6
297     p = server;
298     addr_colons = 0;
299     while (*p)
300     if (*p++ == ':')
301     addr_colons++;
302     if (addr_colons >= 2)
303     {
304     /* numeric IPv6 style address format - [1:2:3::4]:port */
305     p = strchr(server, ']');
306     if (*server == '[' && p != NULL)
307     {
308     if (*(p + 1) == ':' && *(p + 2) != '\0')
309 jsorg71 710 g_tcp_port_rdp = strtol(p + 2, NULL, 10);
310 astrand 444 /* remove the port number and brackets from the address */
311     *p = '\0';
312     strncpy(server, server + 1, strlen(server));
313     }
314     }
315     else
316     {
317     /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
318     p = strchr(server, ':');
319     if (p != NULL)
320     {
321 jsorg71 710 g_tcp_port_rdp = strtol(p + 1, NULL, 10);
322 astrand 444 *p = 0;
323     }
324     }
325     #else /* no IPv6 support */
326     p = strchr(server, ':');
327     if (p != NULL)
328     {
329 jsorg71 710 g_tcp_port_rdp = strtol(p + 1, NULL, 10);
330 astrand 444 *p = 0;
331     }
332     #endif /* IPv6 */
333    
334     }
335    
336 matty 10 /* Client program */
337 matty 25 int
338     main(int argc, char *argv[])
339 matty 10 {
340 matthewc 222 char server[64];
341 matty 30 char fullhostname[64];
342 matty 21 char domain[16];
343 astrand 479 char password[64];
344 astrand 238 char shell[128];
345 matty 21 char directory[32];
346 astrand 676 BOOL prompt_password, deactivated;
347 matty 30 struct passwd *pw;
348 astrand 676 uint32 flags, ext_disc_reason = 0;
349 matthewc 222 char *p;
350 matty 10 int c;
351 n-ki 569
352 astrand 289 int username_option = 0;
353 matty 10
354 matty 21 flags = RDP_LOGON_NORMAL;
355 matthewc 211 prompt_password = False;
356 matty 21 domain[0] = password[0] = shell[0] = directory[0] = 0;
357 matthewc 240 strcpy(keymapname, "en-us");
358 stargo 636 g_embed_wnd = 0;
359 matty 21
360 n-ki 569 g_num_devices = 0;
361    
362 astrand 333 #ifdef RDP2VNC
363 forsberg 471 #define VNCOPT "V:Q:"
364 astrand 333 #else
365     #define VNCOPT
366     #endif
367    
368 jsorg71 725 while ((c = getopt(argc, argv,
369 jsorg71 773 VNCOPT "u:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1)
370 matty 10 {
371     switch (c)
372     {
373 astrand 333 #ifdef RDP2VNC
374     case 'V':
375     rfb_port = strtol(optarg, NULL, 10);
376     if (rfb_port < 100)
377     rfb_port += 5900;
378     break;
379    
380 forsberg 471 case 'Q':
381 astrand 333 defer_time = strtol(optarg, NULL, 10);
382     if (defer_time < 0)
383     defer_time = 0;
384     break;
385     #endif
386    
387 matty 10 case 'u':
388 jsorg71 437 STRNCPY(g_username, optarg, sizeof(g_username));
389 astrand 289 username_option = 1;
390 matty 10 break;
391    
392 matty 21 case 'd':
393 matty 30 STRNCPY(domain, optarg, sizeof(domain));
394 matty 21 break;
395    
396     case 's':
397 matty 30 STRNCPY(shell, optarg, sizeof(shell));
398 matty 21 break;
399    
400     case 'c':
401 matty 30 STRNCPY(directory, optarg, sizeof(directory));
402 matty 21 break;
403    
404 matty 30 case 'p':
405 matthewc 211 if ((optarg[0] == '-') && (optarg[1] == 0))
406     {
407     prompt_password = True;
408     break;
409     }
410    
411 matty 30 STRNCPY(password, optarg, sizeof(password));
412     flags |= RDP_LOGON_AUTO;
413 matthewc 211
414     /* try to overwrite argument so it won't appear in ps */
415 n-ki 171 p = optarg;
416     while (*p)
417     *(p++) = 'X';
418 matty 30 break;
419    
420 matty 10 case 'n':
421 jsorg71 710 STRNCPY(g_hostname, optarg, sizeof(g_hostname));
422 matty 10 break;
423    
424     case 'k':
425 astrand 82 STRNCPY(keymapname, optarg, sizeof(keymapname));
426 matty 10 break;
427    
428 matty 30 case 'g':
429 astrand 547 g_fullscreen = False;
430 astrand 263 if (!strcmp(optarg, "workarea"))
431     {
432 jsorg71 447 g_width = g_height = 0;
433 astrand 263 break;
434     }
435    
436 jsorg71 447 g_width = strtol(optarg, &p, 10);
437 astrand 500 if (g_width <= 0)
438     {
439     error("invalid geometry\n");
440     return 1;
441     }
442    
443 matty 30 if (*p == 'x')
444 stargo 800 g_height = strtol(p + 1, &p, 10);
445 matty 30
446 astrand 500 if (g_height <= 0)
447 matty 30 {
448     error("invalid geometry\n");
449     return 1;
450     }
451 astrand 500
452     if (*p == '%')
453 stargo 800 {
454 astrand 500 g_width = -g_width;
455 stargo 800 p++;
456     }
457 astrand 500
458 stargo 800 if (*p == '+' || *p == '-')
459     g_xpos = strtol(p, &p, 10);
460 astrand 801
461 stargo 800 if (*p == '+' || *p == '-')
462     g_ypos = strtol(p, NULL, 10);
463    
464 matty 10 break;
465    
466 matty 30 case 'f':
467 jsorg71 447 g_fullscreen = True;
468 matty 30 break;
469    
470 matty 10 case 'b':
471 jsorg71 678 g_bitmap_cache = False;
472 matty 10 break;
473    
474 stargo 609 case 'B':
475     g_ownbackstore = False;
476     break;
477    
478 matty 30 case 'e':
479 jsorg71 437 g_encryption = False;
480 matty 10 break;
481 astrand 435 case 'E':
482 forsberg 427 packet_encryption = False;
483     break;
484 matty 30 case 'm':
485 jsorg71 447 g_sendmotion = False;
486 matty 28 break;
487 matty 29
488 n-ki 279 case 'C':
489 jsorg71 450 g_owncolmap = True;
490 n-ki 279 break;
491    
492 matthewc 520 case 'D':
493     g_hide_decorations = True;
494     break;
495    
496 astrand 76 case 'K':
497 jsorg71 450 g_grab_keyboard = False;
498 astrand 76 break;
499    
500 matthewc 520 case 'S':
501     if (!strcmp(optarg, "standard"))
502     {
503     g_win_button_size = 18;
504     break;
505     }
506    
507     g_win_button_size = strtol(optarg, &p, 10);
508    
509     if (*p)
510     {
511     error("invalid button size\n");
512     return 1;
513     }
514    
515     break;
516    
517 matthewc 223 case 'T':
518 jsorg71 450 STRNCPY(g_title, optarg, sizeof(g_title));
519 astrand 107 break;
520    
521 astrand 552 case 'N':
522     g_numlock_sync = True;
523     break;
524    
525 stargo 636 case 'X':
526     g_embed_wnd = strtol(optarg, NULL, 10);
527     break;
528 astrand 651
529 jsorg71 309 case 'a':
530 jsorg71 438 g_server_bpp = strtol(optarg, NULL, 10);
531     if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
532     && g_server_bpp != 24)
533 jsorg71 309 {
534     error("invalid server bpp\n");
535     return 1;
536     }
537     break;
538    
539 jsorg71 773 case 'z':
540     DEBUG(("rdp compression enabled\n"));
541     flags |= RDP_COMPRESSION;
542     break;
543    
544 stargo 637 case 'x':
545     if (strncmp("modem", optarg, 1) == 0)
546     {
547 astrand 651 g_rdp5_performanceflags =
548     RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG |
549     RDP5_NO_MENUANIMATIONS | RDP5_NO_THEMING;
550 stargo 637 }
551     else if (strncmp("broadband", optarg, 1) == 0)
552     {
553     g_rdp5_performanceflags = RDP5_NO_WALLPAPER;
554     }
555     else if (strncmp("lan", optarg, 1) == 0)
556     {
557     g_rdp5_performanceflags = RDP5_DISABLE_NOTHING;
558     }
559     else
560     {
561     g_rdp5_performanceflags = strtol(optarg, NULL, 16);
562     }
563     break;
564 astrand 651
565 jsorg71 725 case 'P':
566     g_bitmap_cache_persist_enable = True;
567     break;
568    
569 matthewc 520 case 'r':
570 n-ki 569
571     if (strncmp("sound", optarg, 5) == 0)
572     {
573 n-ki 629 optarg += 5;
574    
575     if (*optarg == ':')
576 n-ki 628 {
577 n-ki 629 *optarg++;
578     while ((p = next_arg(optarg, ',')))
579     {
580     if (strncmp("remote", optarg, 6) == 0)
581     flags |= RDP_LOGON_LEAVE_AUDIO;
582 n-ki 628
583 n-ki 630 if (strncmp("local", optarg, 5) == 0)
584 stargo 501 #ifdef WITH_RDPSND
585 n-ki 629 g_rdpsnd = True;
586 matthewc 520 #else
587 stargo 739 warning("Not compiled with sound support\n");
588 matthewc 520 #endif
589 n-ki 629
590     if (strncmp("off", optarg, 3) == 0)
591 stargo 685 #ifdef WITH_RDPSND
592 n-ki 629 g_rdpsnd = False;
593 stargo 685 #else
594 stargo 739 warning("Not compiled with sound support\n");
595 stargo 685 #endif
596 n-ki 629
597     optarg = p;
598 n-ki 628 }
599     }
600     else
601     {
602     #ifdef WITH_RDPSND
603     g_rdpsnd = True;
604     #else
605 stargo 739 warning("Not compiled with sound support\n");
606 n-ki 628 #endif
607     }
608 n-ki 569 }
609     else if (strncmp("disk", optarg, 4) == 0)
610     {
611     /* -r disk:h:=/mnt/floppy */
612     disk_enum_devices(&g_num_devices, optarg + 4);
613     }
614     else if (strncmp("comport", optarg, 7) == 0)
615     {
616     serial_enum_devices(&g_num_devices, optarg + 7);
617     }
618     else if (strncmp("lptport", optarg, 7) == 0)
619     {
620     parallel_enum_devices(&g_num_devices, optarg + 7);
621     }
622     else if (strncmp("printer", optarg, 7) == 0)
623     {
624     printer_enum_devices(&g_num_devices, optarg + 7);
625     }
626 forsberg 646 else if (strncmp("clientname", optarg, 7) == 0)
627     {
628 astrand 651 g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1);
629 forsberg 646 strcpy(g_rdpdr_clientname, optarg + 11);
630     }
631 n-ki 569 else
632     {
633     warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n");
634     }
635 stargo 501 break;
636 matthewc 520
637 matthewc 482 case '0':
638     g_console_session = True;
639     break;
640    
641 astrand 458 case '4':
642     g_use_rdp5 = False;
643     break;
644    
645 forsberg 350 case '5':
646 jsorg71 438 g_use_rdp5 = True;
647 forsberg 350 break;
648 astrand 458
649 matty 28 case 'h':
650 matty 10 case '?':
651     default:
652     usage(argv[0]);
653     return 1;
654     }
655     }
656    
657 stargo 610 if (argc - optind != 1)
658 matty 10 {
659     usage(argv[0]);
660     return 1;
661     }
662    
663 matthewc 222 STRNCPY(server, argv[optind], sizeof(server));
664 astrand 444 parse_server_and_port(server);
665 matty 10
666 astrand 289 if (!username_option)
667 matty 10 {
668     pw = getpwuid(getuid());
669     if ((pw == NULL) || (pw->pw_name == NULL))
670     {
671 matty 30 error("could not determine username, use -u\n");
672 matty 10 return 1;
673     }
674    
675 jsorg71 437 STRNCPY(g_username, pw->pw_name, sizeof(g_username));
676 matty 10 }
677    
678 jsorg71 710 if (g_hostname[0] == 0)
679 matty 10 {
680 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
681 matty 10 {
682 matty 30 error("could not determine local hostname, use -n\n");
683 matty 10 return 1;
684     }
685 matty 30
686     p = strchr(fullhostname, '.');
687     if (p != NULL)
688     *p = 0;
689    
690 jsorg71 710 STRNCPY(g_hostname, fullhostname, sizeof(g_hostname));
691 matty 10 }
692    
693 stargo 825 if ((flags & RDP_COMPRESSION) && (g_server_bpp > 8))
694     {
695     warning("rdp compression not supported for bpp > 8, compression disabled\n");
696     flags ^= RDP_COMPRESSION;
697     }
698    
699 matthewc 211 if (prompt_password && read_password(password, sizeof(password)))
700     flags |= RDP_LOGON_AUTO;
701 matty 30
702 jsorg71 450 if (g_title[0] == 0)
703 astrand 107 {
704 jsorg71 450 strcpy(g_title, "rdesktop - ");
705     strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
706 astrand 107 }
707 matty 12
708 astrand 333 #ifdef RDP2VNC
709     rdp2vnc_connect(server, flags, domain, password, shell, directory);
710 forsberg 424 return 0;
711 astrand 333 #else
712    
713 astrand 82 if (!ui_init())
714     return 1;
715 astrand 66
716 matthewc 474 #ifdef WITH_RDPSND
717 stargo 501 if (g_rdpsnd)
718     rdpsnd_init();
719 matthewc 474 #endif
720 n-ki 569 rdpdr_init();
721 forsberg 416
722 matthewc 53 if (!rdp_connect(server, flags, domain, password, shell, directory))
723     return 1;
724    
725 forsberg 427 /* By setting encryption to False here, we have an encrypted login
726     packet but unencrypted transfer of other packets */
727 astrand 435 if (!packet_encryption)
728 jsorg71 437 g_encryption = False;
729 forsberg 427
730    
731 matthewc 122 DEBUG(("Connection successful.\n"));
732 matthewc 211 memset(password, 0, sizeof(password));
733 matthewc 53
734 jsorg71 100 if (ui_create_window())
735 matty 10 {
736 astrand 676 rdp_main_loop(&deactivated, &ext_disc_reason);
737 matty 10 ui_destroy_window();
738     }
739    
740 matthewc 122 DEBUG(("Disconnecting...\n"));
741 jsorg71 730 rdp_disconnect();
742 jsorg71 725 cache_save_state();
743 matthewc 188 ui_deinit();
744 astrand 333
745 astrand 676 if (ext_disc_reason >= 2)
746     print_disconnect_reason(ext_disc_reason);
747    
748     if (deactivated)
749     {
750     /* clean disconnect */
751 forsberg 424 return 0;
752 astrand 676 }
753 forsberg 424 else
754 astrand 676 {
755     if (ext_disc_reason == exDiscReasonAPIInitiatedDisconnect
756     || ext_disc_reason == exDiscReasonAPIInitiatedLogoff)
757     {
758     /* not so clean disconnect, but nothing to worry about */
759     return 0;
760     }
761     else
762     {
763     /* return error */
764     return 2;
765     }
766     }
767 forsberg 424
768 astrand 333 #endif
769    
770 matty 10 }
771    
772 matthewc 220 #ifdef EGD_SOCKET
773     /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
774     static BOOL
775     generate_random_egd(uint8 * buf)
776     {
777     struct sockaddr_un addr;
778     BOOL ret = False;
779     int fd;
780    
781     fd = socket(AF_UNIX, SOCK_STREAM, 0);
782     if (fd == -1)
783     return False;
784    
785     addr.sun_family = AF_UNIX;
786     memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
787 astrand 259 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
788 matthewc 220 goto err;
789    
790     /* PRNGD and EGD use a simple communications protocol */
791 astrand 259 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
792     buf[1] = 32; /* Number of requested random bytes */
793 matthewc 220 if (write(fd, buf, 2) != 2)
794     goto err;
795    
796 astrand 259 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
797 matthewc 220 goto err;
798    
799     if (read(fd, buf, 32) != 32)
800     goto err;
801    
802     ret = True;
803    
804 astrand 259 err:
805 matthewc 220 close(fd);
806     return ret;
807     }
808     #endif
809    
810 matty 10 /* Generate a 32-byte random for the secure transport code. */
811 matty 25 void
812 astrand 64 generate_random(uint8 * random)
813 matty 10 {
814     struct stat st;
815 matty 22 struct tms tmsbuf;
816 matthewc 220 MD5_CTX md5;
817     uint32 *r;
818     int fd, n;
819 matty 10
820 matthewc 220 /* If we have a kernel random device, try that first */
821 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
822     || ((fd = open("/dev/random", O_RDONLY)) != -1))
823 matty 10 {
824 matthewc 220 n = read(fd, random, 32);
825 matty 10 close(fd);
826 matthewc 220 if (n == 32)
827     return;
828 matty 10 }
829    
830 matthewc 220 #ifdef EGD_SOCKET
831     /* As a second preference use an EGD */
832     if (generate_random_egd(random))
833     return;
834     #endif
835    
836 matty 10 /* Otherwise use whatever entropy we can gather - ideas welcome. */
837 astrand 259 r = (uint32 *) random;
838 matty 10 r[0] = (getpid()) | (getppid() << 16);
839     r[1] = (getuid()) | (getgid() << 16);
840 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
841     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
842 matty 10 stat("/tmp", &st);
843     r[5] = st.st_atime;
844     r[6] = st.st_mtime;
845     r[7] = st.st_ctime;
846 matthewc 220
847     /* Hash both halves with MD5 to obscure possible patterns */
848     MD5_Init(&md5);
849 astrand 259 MD5_Update(&md5, random, 16);
850 matthewc 220 MD5_Final(random, &md5);
851 astrand 259 MD5_Update(&md5, random + 16, 16);
852     MD5_Final(random + 16, &md5);
853 matty 10 }
854    
855     /* malloc; exit if out of memory */
856 matty 25 void *
857     xmalloc(int size)
858 matty 10 {
859     void *mem = malloc(size);
860     if (mem == NULL)
861     {
862 matty 30 error("xmalloc %d\n", size);
863 matty 10 exit(1);
864     }
865     return mem;
866     }
867    
868     /* realloc; exit if out of memory */
869 matty 25 void *
870     xrealloc(void *oldmem, int size)
871 matty 10 {
872 astrand 782 void *mem;
873 jsorg71 773
874     if (size < 1)
875     size = 1;
876     mem = realloc(oldmem, size);
877 matty 10 if (mem == NULL)
878     {
879 matty 30 error("xrealloc %d\n", size);
880 matty 10 exit(1);
881     }
882     return mem;
883     }
884    
885     /* free */
886 matty 25 void
887     xfree(void *mem)
888 matty 10 {
889     free(mem);
890     }
891    
892 matty 30 /* report an error */
893 matty 25 void
894 matty 30 error(char *format, ...)
895     {
896     va_list ap;
897    
898     fprintf(stderr, "ERROR: ");
899    
900     va_start(ap, format);
901     vfprintf(stderr, format, ap);
902     va_end(ap);
903     }
904    
905 matthewc 297 /* report a warning */
906     void
907     warning(char *format, ...)
908     {
909     va_list ap;
910    
911     fprintf(stderr, "WARNING: ");
912    
913     va_start(ap, format);
914     vfprintf(stderr, format, ap);
915     va_end(ap);
916     }
917    
918 matty 30 /* report an unimplemented protocol feature */
919     void
920     unimpl(char *format, ...)
921     {
922     va_list ap;
923    
924     fprintf(stderr, "NOT IMPLEMENTED: ");
925    
926     va_start(ap, format);
927     vfprintf(stderr, format, ap);
928     va_end(ap);
929     }
930    
931     /* produce a hex dump */
932     void
933 n-ki 569 hexdump(unsigned char *p, unsigned int len)
934 matty 10 {
935     unsigned char *line = p;
936 jsorg71 376 int i, thisline, offset = 0;
937 matty 10
938     while (offset < len)
939     {
940 matthewc 169 printf("%04x ", offset);
941 matty 10 thisline = len - offset;
942     if (thisline > 16)
943     thisline = 16;
944    
945     for (i = 0; i < thisline; i++)
946 matthewc 169 printf("%02x ", line[i]);
947 matty 10
948 matty 30 for (; i < 16; i++)
949 matthewc 169 printf(" ");
950 matty 30
951 matty 10 for (i = 0; i < thisline; i++)
952 matthewc 169 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
953 matty 10
954 matthewc 169 printf("\n");
955 matty 10 offset += thisline;
956     line += thisline;
957     }
958     }
959 astrand 325
960 n-ki 569 /*
961 n-ki 583 input: src is the string we look in for needle.
962     Needle may be escaped by a backslash, in
963     that case we ignore that particular needle.
964 n-ki 569 return value: returns next src pointer, for
965     succesive executions, like in a while loop
966     if retval is 0, then there are no more args.
967     pitfalls:
968     src is modified. 0x00 chars are inserted to
969     terminate strings.
970     return val, points on the next val chr after ins
971     0x00
972 astrand 325
973 n-ki 569 example usage:
974     while( (pos = next_arg( optarg, ',')) ){
975     printf("%s\n",optarg);
976     optarg=pos;
977     }
978    
979     */
980     char *
981     next_arg(char *src, char needle)
982     {
983     char *nextval;
984 n-ki 571 char *p;
985 n-ki 575 char *mvp = 0;
986 n-ki 569
987 n-ki 575 /* EOS */
988 n-ki 569 if (*src == (char) 0x00)
989     return 0;
990    
991 n-ki 571 p = src;
992 n-ki 575 /* skip escaped needles */
993 astrand 580 while ((nextval = strchr(p, needle)))
994 n-ki 571 {
995 n-ki 575 mvp = nextval - 1;
996     /* found backslashed needle */
997 astrand 580 if (*mvp == '\\' && (mvp > src))
998 n-ki 575 {
999     /* move string one to the left */
1000 astrand 580 while (*(mvp + 1) != (char) 0x00)
1001 n-ki 575 {
1002 astrand 580 *mvp = *(mvp + 1);
1003 n-ki 575 *mvp++;
1004     }
1005 astrand 580 *mvp = (char) 0x00;
1006 n-ki 575 p = nextval;
1007     }
1008     else
1009     {
1010 astrand 580 p = nextval + 1;
1011 n-ki 571 break;
1012 n-ki 575 }
1013    
1014 n-ki 571 }
1015    
1016 n-ki 575 /* more args available */
1017 n-ki 569 if (nextval)
1018     {
1019     *nextval = (char) 0x00;
1020     return ++nextval;
1021     }
1022    
1023 n-ki 575 /* no more args after this, jump to EOS */
1024 n-ki 569 nextval = src + strlen(src);
1025     return nextval;
1026     }
1027    
1028    
1029 stargo 570 void
1030 astrand 580 toupper_str(char *p)
1031 n-ki 569 {
1032 astrand 580 while (*p)
1033     {
1034     if ((*p >= 'a') && (*p <= 'z'))
1035 stargo 570 *p = toupper((int) *p);
1036 n-ki 569 p++;
1037     }
1038     }
1039    
1040    
1041     /* not all clibs got ltoa */
1042     #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
1043    
1044     char *
1045 stargo 603 l_to_a(long N, int base)
1046 n-ki 569 {
1047     static char ret[LTOA_BUFSIZE];
1048    
1049 n-ki 583 char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf);
1050 n-ki 569
1051 n-ki 583 register int divrem;
1052    
1053     if (base < 36 || 2 > base)
1054 n-ki 569 base = 10;
1055    
1056 n-ki 583 if (N < 0)
1057 n-ki 569 {
1058     *head++ = '-';
1059 n-ki 583 N = -N;
1060 n-ki 569 }
1061    
1062 n-ki 583 tail = buf + sizeof(buf);
1063     *--tail = 0;
1064    
1065     do
1066 n-ki 569 {
1067 n-ki 583 divrem = N % base;
1068     *--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10;
1069     N /= base;
1070 n-ki 569 }
1071 n-ki 583 while (N);
1072 n-ki 569
1073 n-ki 583 strcpy(head, tail);
1074 n-ki 569 return ret;
1075     }
1076    
1077    
1078 astrand 325 int
1079     load_licence(unsigned char **data)
1080     {
1081 matthewc 367 char *home, *path;
1082 astrand 325 struct stat st;
1083 matthewc 367 int fd, length;
1084 astrand 325
1085     home = getenv("HOME");
1086     if (home == NULL)
1087     return -1;
1088    
1089 jsorg71 710 path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1090     sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1091 astrand 325
1092     fd = open(path, O_RDONLY);
1093     if (fd == -1)
1094     return -1;
1095    
1096     if (fstat(fd, &st))
1097     return -1;
1098    
1099 forsberg 416 *data = (uint8 *) xmalloc(st.st_size);
1100 matthewc 367 length = read(fd, *data, st.st_size);
1101     close(fd);
1102     xfree(path);
1103     return length;
1104 astrand 325 }
1105    
1106     void
1107     save_licence(unsigned char *data, int length)
1108     {
1109 matthewc 367 char *home, *path, *tmppath;
1110     int fd;
1111 astrand 325
1112     home = getenv("HOME");
1113     if (home == NULL)
1114     return;
1115    
1116 jsorg71 710 path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1117 astrand 325
1118 matthewc 367 sprintf(path, "%s/.rdesktop", home);
1119     if ((mkdir(path, 0700) == -1) && errno != EEXIST)
1120 astrand 325 {
1121 matthewc 367 perror(path);
1122     return;
1123 astrand 325 }
1124    
1125 matthewc 367 /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
1126 astrand 325
1127 jsorg71 710 sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1128 forsberg 416 tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
1129 matthewc 367 strcpy(tmppath, path);
1130     strcat(tmppath, ".new");
1131    
1132 forsberg 416 fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1133 matthewc 367 if (fd == -1)
1134 astrand 325 {
1135 matthewc 367 perror(tmppath);
1136     return;
1137 astrand 325 }
1138    
1139 matthewc 367 if (write(fd, data, length) != length)
1140 astrand 325 {
1141 matthewc 367 perror(tmppath);
1142     unlink(tmppath);
1143 astrand 325 }
1144 matthewc 367 else if (rename(tmppath, path) == -1)
1145 astrand 325 {
1146 matthewc 367 perror(path);
1147     unlink(tmppath);
1148 astrand 325 }
1149    
1150 matthewc 367 close(fd);
1151     xfree(tmppath);
1152     xfree(path);
1153 astrand 325 }
1154 jsorg71 725
1155     /* Create the bitmap cache directory */
1156     BOOL
1157     rd_pstcache_mkdir(void)
1158     {
1159     char *home;
1160     char bmpcache_dir[256];
1161    
1162     home = getenv("HOME");
1163    
1164     if (home == NULL)
1165     return False;
1166    
1167     sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop");
1168    
1169     if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1170     {
1171     perror(bmpcache_dir);
1172     return False;
1173     }
1174    
1175     sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop/cache");
1176    
1177     if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1178     {
1179     perror(bmpcache_dir);
1180     return False;
1181     }
1182    
1183     return True;
1184     }
1185    
1186     /* open a file in the .rdesktop directory */
1187     int
1188     rd_open_file(char *filename)
1189     {
1190     char *home;
1191     char fn[256];
1192 astrand 738 int fd;
1193 jsorg71 725
1194     home = getenv("HOME");
1195     if (home == NULL)
1196     return -1;
1197     sprintf(fn, "%s/.rdesktop/%s", home, filename);
1198     fd = open(fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1199     if (fd == -1)
1200     perror(fn);
1201     return fd;
1202     }
1203    
1204     /* close file */
1205     void
1206     rd_close_file(int fd)
1207     {
1208 astrand 738 close(fd);
1209 jsorg71 725 }
1210    
1211     /* read from file*/
1212     int
1213     rd_read_file(int fd, void *ptr, int len)
1214     {
1215 astrand 738 return read(fd, ptr, len);
1216 jsorg71 725 }
1217    
1218     /* write to file */
1219     int
1220 astrand 738 rd_write_file(int fd, void *ptr, int len)
1221 jsorg71 725 {
1222 astrand 738 return write(fd, ptr, len);
1223 jsorg71 725 }
1224    
1225     /* move file pointer */
1226     int
1227     rd_lseek_file(int fd, int offset)
1228     {
1229 astrand 738 return lseek(fd, offset, SEEK_SET);
1230 jsorg71 725 }
1231    
1232     /* do a write lock on a file */
1233     BOOL
1234     rd_lock_file(int fd, int start, int len)
1235     {
1236     struct flock lock;
1237    
1238     lock.l_type = F_WRLCK;
1239     lock.l_whence = SEEK_SET;
1240     lock.l_start = start;
1241     lock.l_len = len;
1242     if (fcntl(fd, F_SETLK, &lock) == -1)
1243     return False;
1244     return True;
1245     }

  ViewVC Help
Powered by ViewVC 1.1.26