/[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 848 - (hide annotations)
Sun Mar 13 03:29:19 2005 UTC (19 years, 2 months ago) by jsorg71
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 26342 byte(s)
added var for new polygon and ellipse orders

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

  ViewVC Help
Powered by ViewVC 1.1.26