/[rdesktop]/jpeg/rdesktop/trunk/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 /jpeg/rdesktop/trunk/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 968 - (hide annotations)
Thu Aug 4 11:19:06 2005 UTC (18 years, 9 months ago) by astrand
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 27453 byte(s)
Need to include limits.h for PATH_MAX.

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

  ViewVC Help
Powered by ViewVC 1.1.26