/[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 889 - (hide annotations)
Sun Apr 17 23:14:20 2005 UTC (19 years, 1 month ago) by jdmeijer
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 27069 byte(s)
Add alternative mppc decompression code with 64kB history buffer from Vahur Sinijärv, and reenable rdp5 (persistent) bitmap cache 2 (64x64)

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

  ViewVC Help
Powered by ViewVC 1.1.26