/[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 974 - (hide annotations)
Thu Aug 4 12:50:15 2005 UTC (18 years, 9 months ago) by astrand
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 27859 byte(s)
Created a common xstrdup function.

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 973 #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 973 char keymapname[PATH_MAX] = "";
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 973 char *locale = NULL;
378 astrand 289 int username_option = 0;
379 matty 10
380 astrand 973 #ifdef HAVE_LOCALE_H
381 astrand 962 /* Set locale according to environment */
382     locale = setlocale(LC_ALL, "");
383     if (locale)
384     {
385 astrand 974 locale = xstrdup(locale);
386 astrand 962 }
387    
388 astrand 973 #endif
389 matty 21 flags = RDP_LOGON_NORMAL;
390 matthewc 211 prompt_password = False;
391 matty 21 domain[0] = password[0] = shell[0] = directory[0] = 0;
392 stargo 636 g_embed_wnd = 0;
393 matty 21
394 n-ki 569 g_num_devices = 0;
395    
396 astrand 333 #ifdef RDP2VNC
397 forsberg 471 #define VNCOPT "V:Q:"
398 astrand 333 #else
399     #define VNCOPT
400     #endif
401    
402 jsorg71 725 while ((c = getopt(argc, argv,
403 stargo 855 VNCOPT "u:L:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1)
404 matty 10 {
405     switch (c)
406     {
407 astrand 333 #ifdef RDP2VNC
408     case 'V':
409     rfb_port = strtol(optarg, NULL, 10);
410     if (rfb_port < 100)
411     rfb_port += 5900;
412     break;
413    
414 forsberg 471 case 'Q':
415 astrand 333 defer_time = strtol(optarg, NULL, 10);
416     if (defer_time < 0)
417     defer_time = 0;
418     break;
419     #endif
420    
421 matty 10 case 'u':
422 jsorg71 437 STRNCPY(g_username, optarg, sizeof(g_username));
423 astrand 289 username_option = 1;
424 matty 10 break;
425    
426 stargo 855 case 'L':
427     #ifdef HAVE_ICONV
428     STRNCPY(g_codepage, optarg, sizeof(g_codepage));
429     #else
430     error("iconv support not available\n");
431     #endif
432     break;
433    
434 matty 21 case 'd':
435 matty 30 STRNCPY(domain, optarg, sizeof(domain));
436 matty 21 break;
437    
438     case 's':
439 matty 30 STRNCPY(shell, optarg, sizeof(shell));
440 matty 21 break;
441    
442     case 'c':
443 matty 30 STRNCPY(directory, optarg, sizeof(directory));
444 matty 21 break;
445    
446 matty 30 case 'p':
447 matthewc 211 if ((optarg[0] == '-') && (optarg[1] == 0))
448     {
449     prompt_password = True;
450     break;
451     }
452    
453 matty 30 STRNCPY(password, optarg, sizeof(password));
454     flags |= RDP_LOGON_AUTO;
455 matthewc 211
456     /* try to overwrite argument so it won't appear in ps */
457 n-ki 171 p = optarg;
458     while (*p)
459     *(p++) = 'X';
460 matty 30 break;
461    
462 matty 10 case 'n':
463 jsorg71 710 STRNCPY(g_hostname, optarg, sizeof(g_hostname));
464 matty 10 break;
465    
466     case 'k':
467 astrand 82 STRNCPY(keymapname, optarg, sizeof(keymapname));
468 matty 10 break;
469    
470 matty 30 case 'g':
471 astrand 547 g_fullscreen = False;
472 astrand 263 if (!strcmp(optarg, "workarea"))
473     {
474 jsorg71 447 g_width = g_height = 0;
475 astrand 263 break;
476     }
477    
478 jsorg71 447 g_width = strtol(optarg, &p, 10);
479 astrand 500 if (g_width <= 0)
480     {
481     error("invalid geometry\n");
482     return 1;
483     }
484    
485 matty 30 if (*p == 'x')
486 stargo 800 g_height = strtol(p + 1, &p, 10);
487 matty 30
488 astrand 500 if (g_height <= 0)
489 matty 30 {
490     error("invalid geometry\n");
491     return 1;
492     }
493 astrand 500
494     if (*p == '%')
495 stargo 800 {
496 astrand 500 g_width = -g_width;
497 stargo 800 p++;
498     }
499 astrand 500
500 stargo 800 if (*p == '+' || *p == '-')
501 stargo 867 {
502     g_pos |= (*p == '-') ? 2 : 1;
503 stargo 800 g_xpos = strtol(p, &p, 10);
504 astrand 801
505 stargo 867 }
506 stargo 800 if (*p == '+' || *p == '-')
507 stargo 867 {
508     g_pos |= (*p == '-') ? 4 : 1;
509 stargo 800 g_ypos = strtol(p, NULL, 10);
510 stargo 867 }
511 stargo 800
512 matty 10 break;
513    
514 matty 30 case 'f':
515 jsorg71 447 g_fullscreen = True;
516 matty 30 break;
517    
518 matty 10 case 'b':
519 jsorg71 678 g_bitmap_cache = False;
520 matty 10 break;
521    
522 stargo 609 case 'B':
523     g_ownbackstore = False;
524     break;
525    
526 matty 30 case 'e':
527 jsorg71 437 g_encryption = False;
528 matty 10 break;
529 astrand 435 case 'E':
530 forsberg 427 packet_encryption = False;
531     break;
532 matty 30 case 'm':
533 jsorg71 447 g_sendmotion = False;
534 matty 28 break;
535 matty 29
536 n-ki 279 case 'C':
537 jsorg71 450 g_owncolmap = True;
538 n-ki 279 break;
539    
540 matthewc 520 case 'D':
541     g_hide_decorations = True;
542     break;
543    
544 astrand 76 case 'K':
545 jsorg71 450 g_grab_keyboard = False;
546 astrand 76 break;
547    
548 matthewc 520 case 'S':
549     if (!strcmp(optarg, "standard"))
550     {
551     g_win_button_size = 18;
552     break;
553     }
554    
555     g_win_button_size = strtol(optarg, &p, 10);
556    
557     if (*p)
558     {
559     error("invalid button size\n");
560     return 1;
561     }
562    
563     break;
564    
565 matthewc 223 case 'T':
566 jsorg71 450 STRNCPY(g_title, optarg, sizeof(g_title));
567 astrand 107 break;
568    
569 astrand 552 case 'N':
570     g_numlock_sync = True;
571     break;
572    
573 stargo 636 case 'X':
574 stargo 871 g_embed_wnd = strtol(optarg, NULL, 0);
575 stargo 636 break;
576 astrand 651
577 jsorg71 309 case 'a':
578 jsorg71 438 g_server_bpp = strtol(optarg, NULL, 10);
579     if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
580     && g_server_bpp != 24)
581 jsorg71 309 {
582     error("invalid server bpp\n");
583     return 1;
584     }
585     break;
586    
587 jsorg71 773 case 'z':
588     DEBUG(("rdp compression enabled\n"));
589 jdmeijer 889 flags |= (RDP_LOGON_COMPRESSION | RDP_LOGON_COMPRESSION2);
590 jsorg71 773 break;
591    
592 stargo 637 case 'x':
593     if (strncmp("modem", optarg, 1) == 0)
594     {
595 astrand 651 g_rdp5_performanceflags =
596     RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG |
597     RDP5_NO_MENUANIMATIONS | RDP5_NO_THEMING;
598 stargo 637 }
599     else if (strncmp("broadband", optarg, 1) == 0)
600     {
601     g_rdp5_performanceflags = RDP5_NO_WALLPAPER;
602     }
603     else if (strncmp("lan", optarg, 1) == 0)
604     {
605     g_rdp5_performanceflags = RDP5_DISABLE_NOTHING;
606     }
607     else
608     {
609     g_rdp5_performanceflags = strtol(optarg, NULL, 16);
610     }
611     break;
612 astrand 651
613 jsorg71 725 case 'P':
614     g_bitmap_cache_persist_enable = True;
615     break;
616    
617 matthewc 520 case 'r':
618 n-ki 569
619     if (strncmp("sound", optarg, 5) == 0)
620     {
621 n-ki 629 optarg += 5;
622    
623     if (*optarg == ':')
624 n-ki 628 {
625 n-ki 629 *optarg++;
626     while ((p = next_arg(optarg, ',')))
627     {
628     if (strncmp("remote", optarg, 6) == 0)
629     flags |= RDP_LOGON_LEAVE_AUDIO;
630 n-ki 628
631 n-ki 630 if (strncmp("local", optarg, 5) == 0)
632 stargo 501 #ifdef WITH_RDPSND
633 n-ki 629 g_rdpsnd = True;
634 matthewc 520 #else
635 stargo 739 warning("Not compiled with sound support\n");
636 matthewc 520 #endif
637 n-ki 629
638     if (strncmp("off", optarg, 3) == 0)
639 stargo 685 #ifdef WITH_RDPSND
640 n-ki 629 g_rdpsnd = False;
641 stargo 685 #else
642 stargo 739 warning("Not compiled with sound support\n");
643 stargo 685 #endif
644 n-ki 629
645     optarg = p;
646 n-ki 628 }
647     }
648     else
649     {
650     #ifdef WITH_RDPSND
651     g_rdpsnd = True;
652     #else
653 stargo 739 warning("Not compiled with sound support\n");
654 n-ki 628 #endif
655     }
656 n-ki 569 }
657     else if (strncmp("disk", optarg, 4) == 0)
658     {
659     /* -r disk:h:=/mnt/floppy */
660     disk_enum_devices(&g_num_devices, optarg + 4);
661     }
662     else if (strncmp("comport", optarg, 7) == 0)
663     {
664     serial_enum_devices(&g_num_devices, optarg + 7);
665     }
666     else if (strncmp("lptport", optarg, 7) == 0)
667     {
668     parallel_enum_devices(&g_num_devices, optarg + 7);
669     }
670     else if (strncmp("printer", optarg, 7) == 0)
671     {
672     printer_enum_devices(&g_num_devices, optarg + 7);
673     }
674 forsberg 646 else if (strncmp("clientname", optarg, 7) == 0)
675     {
676 astrand 651 g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1);
677 forsberg 646 strcpy(g_rdpdr_clientname, optarg + 11);
678     }
679 n-ki 569 else
680     {
681     warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n");
682     }
683 stargo 501 break;
684 matthewc 520
685 matthewc 482 case '0':
686     g_console_session = True;
687     break;
688    
689 astrand 458 case '4':
690     g_use_rdp5 = False;
691     break;
692    
693 forsberg 350 case '5':
694 jsorg71 438 g_use_rdp5 = True;
695 forsberg 350 break;
696 astrand 458
697 matty 28 case 'h':
698 matty 10 case '?':
699     default:
700     usage(argv[0]);
701     return 1;
702     }
703     }
704    
705 stargo 610 if (argc - optind != 1)
706 matty 10 {
707     usage(argv[0]);
708     return 1;
709     }
710    
711 matthewc 222 STRNCPY(server, argv[optind], sizeof(server));
712 astrand 444 parse_server_and_port(server);
713 matty 10
714 astrand 289 if (!username_option)
715 matty 10 {
716     pw = getpwuid(getuid());
717     if ((pw == NULL) || (pw->pw_name == NULL))
718     {
719 matty 30 error("could not determine username, use -u\n");
720 matty 10 return 1;
721     }
722    
723 jsorg71 437 STRNCPY(g_username, pw->pw_name, sizeof(g_username));
724 matty 10 }
725    
726 stargo 855 #ifdef HAVE_ICONV
727     if (g_codepage[0] == 0)
728     {
729     if (setlocale(LC_CTYPE, ""))
730     {
731     STRNCPY(g_codepage, nl_langinfo(CODESET), sizeof(g_codepage));
732     }
733     else
734     {
735     STRNCPY(g_codepage, DEFAULT_CODEPAGE, sizeof(g_codepage));
736     }
737     }
738     #endif
739 stargo 861
740 jsorg71 710 if (g_hostname[0] == 0)
741 matty 10 {
742 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
743 matty 10 {
744 matty 30 error("could not determine local hostname, use -n\n");
745 matty 10 return 1;
746     }
747 matty 30
748     p = strchr(fullhostname, '.');
749     if (p != NULL)
750     *p = 0;
751    
752 jsorg71 710 STRNCPY(g_hostname, fullhostname, sizeof(g_hostname));
753 matty 10 }
754    
755 astrand 973 if (keymapname[0] == 0)
756     {
757     if (locale && xkeymap_from_locale(locale))
758     {
759     fprintf(stderr, "Autoselected keyboard map %s\n", keymapname);
760     }
761     else
762     {
763     STRNCPY(keymapname, "en-us", sizeof(keymapname));
764     }
765     }
766     if (locale)
767     xfree(locale);
768    
769    
770 matthewc 211 if (prompt_password && read_password(password, sizeof(password)))
771     flags |= RDP_LOGON_AUTO;
772 matty 30
773 jsorg71 450 if (g_title[0] == 0)
774 astrand 107 {
775 jsorg71 450 strcpy(g_title, "rdesktop - ");
776     strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
777 astrand 107 }
778 matty 12
779 astrand 333 #ifdef RDP2VNC
780     rdp2vnc_connect(server, flags, domain, password, shell, directory);
781 forsberg 424 return 0;
782 astrand 333 #else
783    
784 astrand 82 if (!ui_init())
785     return 1;
786 astrand 66
787 matthewc 474 #ifdef WITH_RDPSND
788 stargo 501 if (g_rdpsnd)
789     rdpsnd_init();
790 matthewc 474 #endif
791 n-ki 569 rdpdr_init();
792 forsberg 416
793 matthewc 53 if (!rdp_connect(server, flags, domain, password, shell, directory))
794     return 1;
795    
796 forsberg 427 /* By setting encryption to False here, we have an encrypted login
797     packet but unencrypted transfer of other packets */
798 astrand 435 if (!packet_encryption)
799 jsorg71 437 g_encryption = False;
800 forsberg 427
801    
802 matthewc 122 DEBUG(("Connection successful.\n"));
803 matthewc 211 memset(password, 0, sizeof(password));
804 matthewc 53
805 jsorg71 100 if (ui_create_window())
806 matty 10 {
807 astrand 676 rdp_main_loop(&deactivated, &ext_disc_reason);
808 matty 10 ui_destroy_window();
809     }
810    
811 matthewc 122 DEBUG(("Disconnecting...\n"));
812 jsorg71 730 rdp_disconnect();
813 jsorg71 725 cache_save_state();
814 matthewc 188 ui_deinit();
815 astrand 333
816 astrand 676 if (ext_disc_reason >= 2)
817     print_disconnect_reason(ext_disc_reason);
818    
819     if (deactivated)
820     {
821     /* clean disconnect */
822 forsberg 424 return 0;
823 astrand 676 }
824 forsberg 424 else
825 astrand 676 {
826     if (ext_disc_reason == exDiscReasonAPIInitiatedDisconnect
827     || ext_disc_reason == exDiscReasonAPIInitiatedLogoff)
828     {
829     /* not so clean disconnect, but nothing to worry about */
830     return 0;
831     }
832     else
833     {
834     /* return error */
835     return 2;
836     }
837     }
838 forsberg 424
839 astrand 333 #endif
840    
841 matty 10 }
842    
843 matthewc 220 #ifdef EGD_SOCKET
844     /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
845     static BOOL
846     generate_random_egd(uint8 * buf)
847     {
848     struct sockaddr_un addr;
849     BOOL ret = False;
850     int fd;
851    
852     fd = socket(AF_UNIX, SOCK_STREAM, 0);
853     if (fd == -1)
854     return False;
855    
856     addr.sun_family = AF_UNIX;
857     memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
858 astrand 259 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
859 matthewc 220 goto err;
860    
861     /* PRNGD and EGD use a simple communications protocol */
862 astrand 259 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
863     buf[1] = 32; /* Number of requested random bytes */
864 matthewc 220 if (write(fd, buf, 2) != 2)
865     goto err;
866    
867 astrand 259 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
868 matthewc 220 goto err;
869    
870     if (read(fd, buf, 32) != 32)
871     goto err;
872    
873     ret = True;
874    
875 astrand 259 err:
876 matthewc 220 close(fd);
877     return ret;
878     }
879     #endif
880    
881 matty 10 /* Generate a 32-byte random for the secure transport code. */
882 matty 25 void
883 astrand 64 generate_random(uint8 * random)
884 matty 10 {
885     struct stat st;
886 matty 22 struct tms tmsbuf;
887 matthewc 220 MD5_CTX md5;
888     uint32 *r;
889     int fd, n;
890 matty 10
891 matthewc 220 /* If we have a kernel random device, try that first */
892 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
893     || ((fd = open("/dev/random", O_RDONLY)) != -1))
894 matty 10 {
895 matthewc 220 n = read(fd, random, 32);
896 matty 10 close(fd);
897 matthewc 220 if (n == 32)
898     return;
899 matty 10 }
900    
901 matthewc 220 #ifdef EGD_SOCKET
902     /* As a second preference use an EGD */
903     if (generate_random_egd(random))
904     return;
905     #endif
906    
907 matty 10 /* Otherwise use whatever entropy we can gather - ideas welcome. */
908 astrand 259 r = (uint32 *) random;
909 matty 10 r[0] = (getpid()) | (getppid() << 16);
910     r[1] = (getuid()) | (getgid() << 16);
911 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
912     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
913 matty 10 stat("/tmp", &st);
914     r[5] = st.st_atime;
915     r[6] = st.st_mtime;
916     r[7] = st.st_ctime;
917 matthewc 220
918     /* Hash both halves with MD5 to obscure possible patterns */
919     MD5_Init(&md5);
920 astrand 259 MD5_Update(&md5, random, 16);
921 matthewc 220 MD5_Final(random, &md5);
922 astrand 259 MD5_Update(&md5, random + 16, 16);
923     MD5_Final(random + 16, &md5);
924 matty 10 }
925    
926     /* malloc; exit if out of memory */
927 matty 25 void *
928     xmalloc(int size)
929 matty 10 {
930     void *mem = malloc(size);
931     if (mem == NULL)
932     {
933 matty 30 error("xmalloc %d\n", size);
934 matty 10 exit(1);
935     }
936     return mem;
937     }
938    
939 astrand 974 /* strdup */
940     char *
941     xstrdup(const char *s)
942     {
943     char *mem = strdup(s);
944     if (mem == NULL)
945     {
946     perror("strdup");
947     exit(1);
948     }
949     return mem;
950     }
951    
952 matty 10 /* realloc; exit if out of memory */
953 matty 25 void *
954     xrealloc(void *oldmem, int size)
955 matty 10 {
956 astrand 782 void *mem;
957 jsorg71 773
958     if (size < 1)
959     size = 1;
960     mem = realloc(oldmem, size);
961 matty 10 if (mem == NULL)
962     {
963 matty 30 error("xrealloc %d\n", size);
964 matty 10 exit(1);
965     }
966     return mem;
967     }
968    
969     /* free */
970 matty 25 void
971     xfree(void *mem)
972 matty 10 {
973     free(mem);
974     }
975    
976 matty 30 /* report an error */
977 matty 25 void
978 matty 30 error(char *format, ...)
979     {
980     va_list ap;
981    
982     fprintf(stderr, "ERROR: ");
983    
984     va_start(ap, format);
985     vfprintf(stderr, format, ap);
986     va_end(ap);
987     }
988    
989 matthewc 297 /* report a warning */
990     void
991     warning(char *format, ...)
992     {
993     va_list ap;
994    
995     fprintf(stderr, "WARNING: ");
996    
997     va_start(ap, format);
998     vfprintf(stderr, format, ap);
999     va_end(ap);
1000     }
1001    
1002 matty 30 /* report an unimplemented protocol feature */
1003     void
1004     unimpl(char *format, ...)
1005     {
1006     va_list ap;
1007    
1008     fprintf(stderr, "NOT IMPLEMENTED: ");
1009    
1010     va_start(ap, format);
1011     vfprintf(stderr, format, ap);
1012     va_end(ap);
1013     }
1014    
1015     /* produce a hex dump */
1016     void
1017 n-ki 569 hexdump(unsigned char *p, unsigned int len)
1018 matty 10 {
1019     unsigned char *line = p;
1020 jsorg71 376 int i, thisline, offset = 0;
1021 matty 10
1022     while (offset < len)
1023     {
1024 matthewc 169 printf("%04x ", offset);
1025 matty 10 thisline = len - offset;
1026     if (thisline > 16)
1027     thisline = 16;
1028    
1029     for (i = 0; i < thisline; i++)
1030 matthewc 169 printf("%02x ", line[i]);
1031 matty 10
1032 matty 30 for (; i < 16; i++)
1033 matthewc 169 printf(" ");
1034 matty 30
1035 matty 10 for (i = 0; i < thisline; i++)
1036 matthewc 169 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
1037 matty 10
1038 matthewc 169 printf("\n");
1039 matty 10 offset += thisline;
1040     line += thisline;
1041     }
1042     }
1043 astrand 325
1044 n-ki 569 /*
1045 n-ki 583 input: src is the string we look in for needle.
1046     Needle may be escaped by a backslash, in
1047     that case we ignore that particular needle.
1048 n-ki 569 return value: returns next src pointer, for
1049     succesive executions, like in a while loop
1050     if retval is 0, then there are no more args.
1051     pitfalls:
1052     src is modified. 0x00 chars are inserted to
1053     terminate strings.
1054     return val, points on the next val chr after ins
1055     0x00
1056 astrand 325
1057 n-ki 569 example usage:
1058     while( (pos = next_arg( optarg, ',')) ){
1059     printf("%s\n",optarg);
1060     optarg=pos;
1061     }
1062    
1063     */
1064     char *
1065     next_arg(char *src, char needle)
1066     {
1067     char *nextval;
1068 n-ki 571 char *p;
1069 n-ki 575 char *mvp = 0;
1070 n-ki 569
1071 n-ki 575 /* EOS */
1072 n-ki 569 if (*src == (char) 0x00)
1073     return 0;
1074    
1075 n-ki 571 p = src;
1076 n-ki 575 /* skip escaped needles */
1077 astrand 580 while ((nextval = strchr(p, needle)))
1078 n-ki 571 {
1079 n-ki 575 mvp = nextval - 1;
1080     /* found backslashed needle */
1081 astrand 580 if (*mvp == '\\' && (mvp > src))
1082 n-ki 575 {
1083     /* move string one to the left */
1084 astrand 580 while (*(mvp + 1) != (char) 0x00)
1085 n-ki 575 {
1086 astrand 580 *mvp = *(mvp + 1);
1087 n-ki 575 *mvp++;
1088     }
1089 astrand 580 *mvp = (char) 0x00;
1090 n-ki 575 p = nextval;
1091     }
1092     else
1093     {
1094 astrand 580 p = nextval + 1;
1095 n-ki 571 break;
1096 n-ki 575 }
1097    
1098 n-ki 571 }
1099    
1100 n-ki 575 /* more args available */
1101 n-ki 569 if (nextval)
1102     {
1103     *nextval = (char) 0x00;
1104     return ++nextval;
1105     }
1106    
1107 n-ki 575 /* no more args after this, jump to EOS */
1108 n-ki 569 nextval = src + strlen(src);
1109     return nextval;
1110     }
1111    
1112    
1113 stargo 570 void
1114 astrand 580 toupper_str(char *p)
1115 n-ki 569 {
1116 astrand 580 while (*p)
1117     {
1118     if ((*p >= 'a') && (*p <= 'z'))
1119 stargo 570 *p = toupper((int) *p);
1120 n-ki 569 p++;
1121     }
1122     }
1123    
1124    
1125     /* not all clibs got ltoa */
1126     #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
1127    
1128     char *
1129 stargo 603 l_to_a(long N, int base)
1130 n-ki 569 {
1131     static char ret[LTOA_BUFSIZE];
1132    
1133 n-ki 583 char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf);
1134 n-ki 569
1135 n-ki 583 register int divrem;
1136    
1137     if (base < 36 || 2 > base)
1138 n-ki 569 base = 10;
1139    
1140 n-ki 583 if (N < 0)
1141 n-ki 569 {
1142     *head++ = '-';
1143 n-ki 583 N = -N;
1144 n-ki 569 }
1145    
1146 n-ki 583 tail = buf + sizeof(buf);
1147     *--tail = 0;
1148    
1149     do
1150 n-ki 569 {
1151 n-ki 583 divrem = N % base;
1152     *--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10;
1153     N /= base;
1154 n-ki 569 }
1155 n-ki 583 while (N);
1156 n-ki 569
1157 n-ki 583 strcpy(head, tail);
1158 n-ki 569 return ret;
1159     }
1160    
1161    
1162 astrand 325 int
1163     load_licence(unsigned char **data)
1164     {
1165 matthewc 367 char *home, *path;
1166 astrand 325 struct stat st;
1167 matthewc 367 int fd, length;
1168 astrand 325
1169     home = getenv("HOME");
1170     if (home == NULL)
1171     return -1;
1172    
1173 jsorg71 710 path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1174     sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1175 astrand 325
1176     fd = open(path, O_RDONLY);
1177     if (fd == -1)
1178     return -1;
1179    
1180     if (fstat(fd, &st))
1181     return -1;
1182    
1183 forsberg 416 *data = (uint8 *) xmalloc(st.st_size);
1184 matthewc 367 length = read(fd, *data, st.st_size);
1185     close(fd);
1186     xfree(path);
1187     return length;
1188 astrand 325 }
1189    
1190     void
1191     save_licence(unsigned char *data, int length)
1192     {
1193 matthewc 367 char *home, *path, *tmppath;
1194     int fd;
1195 astrand 325
1196     home = getenv("HOME");
1197     if (home == NULL)
1198     return;
1199    
1200 jsorg71 710 path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1201 astrand 325
1202 matthewc 367 sprintf(path, "%s/.rdesktop", home);
1203     if ((mkdir(path, 0700) == -1) && errno != EEXIST)
1204 astrand 325 {
1205 matthewc 367 perror(path);
1206     return;
1207 astrand 325 }
1208    
1209 matthewc 367 /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
1210 astrand 325
1211 jsorg71 710 sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1212 forsberg 416 tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
1213 matthewc 367 strcpy(tmppath, path);
1214     strcat(tmppath, ".new");
1215    
1216 forsberg 416 fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1217 matthewc 367 if (fd == -1)
1218 astrand 325 {
1219 matthewc 367 perror(tmppath);
1220     return;
1221 astrand 325 }
1222    
1223 matthewc 367 if (write(fd, data, length) != length)
1224 astrand 325 {
1225 matthewc 367 perror(tmppath);
1226     unlink(tmppath);
1227 astrand 325 }
1228 matthewc 367 else if (rename(tmppath, path) == -1)
1229 astrand 325 {
1230 matthewc 367 perror(path);
1231     unlink(tmppath);
1232 astrand 325 }
1233    
1234 matthewc 367 close(fd);
1235     xfree(tmppath);
1236     xfree(path);
1237 astrand 325 }
1238 jsorg71 725
1239     /* Create the bitmap cache directory */
1240     BOOL
1241     rd_pstcache_mkdir(void)
1242     {
1243     char *home;
1244     char bmpcache_dir[256];
1245    
1246     home = getenv("HOME");
1247    
1248     if (home == NULL)
1249     return False;
1250    
1251     sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop");
1252    
1253     if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1254     {
1255     perror(bmpcache_dir);
1256     return False;
1257     }
1258    
1259     sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop/cache");
1260    
1261     if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1262     {
1263     perror(bmpcache_dir);
1264     return False;
1265     }
1266    
1267     return True;
1268     }
1269    
1270     /* open a file in the .rdesktop directory */
1271     int
1272     rd_open_file(char *filename)
1273     {
1274     char *home;
1275     char fn[256];
1276 astrand 738 int fd;
1277 jsorg71 725
1278     home = getenv("HOME");
1279     if (home == NULL)
1280     return -1;
1281     sprintf(fn, "%s/.rdesktop/%s", home, filename);
1282     fd = open(fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1283     if (fd == -1)
1284     perror(fn);
1285     return fd;
1286     }
1287    
1288     /* close file */
1289     void
1290     rd_close_file(int fd)
1291     {
1292 astrand 738 close(fd);
1293 jsorg71 725 }
1294    
1295     /* read from file*/
1296     int
1297     rd_read_file(int fd, void *ptr, int len)
1298     {
1299 astrand 738 return read(fd, ptr, len);
1300 jsorg71 725 }
1301    
1302     /* write to file */
1303     int
1304 astrand 738 rd_write_file(int fd, void *ptr, int len)
1305 jsorg71 725 {
1306 astrand 738 return write(fd, ptr, len);
1307 jsorg71 725 }
1308    
1309     /* move file pointer */
1310     int
1311     rd_lseek_file(int fd, int offset)
1312     {
1313 astrand 738 return lseek(fd, offset, SEEK_SET);
1314 jsorg71 725 }
1315    
1316     /* do a write lock on a file */
1317     BOOL
1318     rd_lock_file(int fd, int start, int len)
1319     {
1320     struct flock lock;
1321    
1322     lock.l_type = F_WRLCK;
1323     lock.l_whence = SEEK_SET;
1324     lock.l_start = start;
1325     lock.l_len = len;
1326     if (fcntl(fd, F_SETLK, &lock) == -1)
1327     return False;
1328     return True;
1329     }

  ViewVC Help
Powered by ViewVC 1.1.26