/[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 886 - (hide annotations)
Sat Apr 16 11:42:34 2005 UTC (19 years, 1 month ago) by stargo
File MIME type: text/plain
File size: 27098 byte(s)
Enable rdp-compression for all bpps.
Many thanks to Vahur Sinijärv <vahur.sinijarv@cydonia.ee> for
finding the fix.

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

  ViewVC Help
Powered by ViewVC 1.1.26