/[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 773 - (hide annotations)
Sat Sep 11 22:57:42 2004 UTC (19 years, 8 months ago) by jsorg71
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 25894 byte(s)
added option for rdp compression

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

  ViewVC Help
Powered by ViewVC 1.1.26