/[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 855 - (hide annotations)
Sun Mar 13 13:18:48 2005 UTC (19 years, 2 months ago) by stargo
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 26927 byte(s)
unicode support from Andy Igoshin <ai@vsu.ru>
Currently disabled, need HAVE_ICONV and HAVE_ICONV_H defined to be
used. This should be done with a new configure test.

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

  ViewVC Help
Powered by ViewVC 1.1.26