/[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 710 - (hide annotations)
Tue Jun 15 22:17:08 2004 UTC (19 years, 11 months ago) by jsorg71
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 24036 byte(s)
proceed more global vars with g_

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

  ViewVC Help
Powered by ViewVC 1.1.26