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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 651 - (hide annotations)
Thu Apr 15 20:12:42 2004 UTC (20 years, 1 month ago) by astrand
File MIME type: text/plain
File size: 21316 byte(s)
Indent fixes

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 matty 10 char hostname[16];
47 matthewc 38 char keymapname[16];
48 astrand 66 int 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 58 int tcp_port_rdp = 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 437 BOOL g_orders = True;
62     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 matthewc 211 static BOOL
152     read_password(char *password, int size)
153     {
154     struct termios tios;
155     BOOL ret = False;
156     int istty = 0;
157     char *p;
158    
159     if (tcgetattr(STDIN_FILENO, &tios) == 0)
160     {
161     fprintf(stderr, "Password: ");
162     tios.c_lflag &= ~ECHO;
163     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
164     istty = 1;
165     }
166    
167     if (fgets(password, size, stdin) != NULL)
168     {
169     ret = True;
170    
171     /* strip final newline */
172     p = strchr(password, '\n');
173     if (p != NULL)
174     *p = 0;
175     }
176    
177     if (istty)
178     {
179     tios.c_lflag |= ECHO;
180     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
181     fprintf(stderr, "\n");
182     }
183    
184     return ret;
185     }
186    
187 astrand 444 static void
188     parse_server_and_port(char *server)
189     {
190     char *p;
191     #ifdef IPv6
192     int addr_colons;
193     #endif
194    
195     #ifdef IPv6
196     p = server;
197     addr_colons = 0;
198     while (*p)
199     if (*p++ == ':')
200     addr_colons++;
201     if (addr_colons >= 2)
202     {
203     /* numeric IPv6 style address format - [1:2:3::4]:port */
204     p = strchr(server, ']');
205     if (*server == '[' && p != NULL)
206     {
207     if (*(p + 1) == ':' && *(p + 2) != '\0')
208     tcp_port_rdp = strtol(p + 2, NULL, 10);
209     /* remove the port number and brackets from the address */
210     *p = '\0';
211     strncpy(server, server + 1, strlen(server));
212     }
213     }
214     else
215     {
216     /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
217     p = strchr(server, ':');
218     if (p != NULL)
219     {
220     tcp_port_rdp = strtol(p + 1, NULL, 10);
221     *p = 0;
222     }
223     }
224     #else /* no IPv6 support */
225     p = strchr(server, ':');
226     if (p != NULL)
227     {
228     tcp_port_rdp = strtol(p + 1, NULL, 10);
229     *p = 0;
230     }
231     #endif /* IPv6 */
232    
233     }
234    
235 matty 10 /* Client program */
236 matty 25 int
237     main(int argc, char *argv[])
238 matty 10 {
239 matthewc 222 char server[64];
240 matty 30 char fullhostname[64];
241 matty 21 char domain[16];
242 astrand 479 char password[64];
243 astrand 238 char shell[128];
244 matty 21 char directory[32];
245 forsberg 424 BOOL prompt_password, rdp_retval = False;
246 matty 30 struct passwd *pw;
247     uint32 flags;
248 matthewc 222 char *p;
249 matty 10 int c;
250 n-ki 569
251 astrand 289 int username_option = 0;
252 matty 10
253 matty 21 flags = RDP_LOGON_NORMAL;
254 matthewc 211 prompt_password = False;
255 matty 21 domain[0] = password[0] = shell[0] = directory[0] = 0;
256 matthewc 240 strcpy(keymapname, "en-us");
257 stargo 636 g_embed_wnd = 0;
258 matty 21
259 n-ki 569 g_num_devices = 0;
260    
261 astrand 333 #ifdef RDP2VNC
262 forsberg 471 #define VNCOPT "V:Q:"
263 astrand 333 #else
264     #define VNCOPT
265     #endif
266    
267 stargo 637 while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbBeEmCDKS:T:NX:a:x:r:045h?")) != -1)
268 matty 10 {
269     switch (c)
270     {
271 astrand 333 #ifdef RDP2VNC
272     case 'V':
273     rfb_port = strtol(optarg, NULL, 10);
274     if (rfb_port < 100)
275     rfb_port += 5900;
276     break;
277    
278 forsberg 471 case 'Q':
279 astrand 333 defer_time = strtol(optarg, NULL, 10);
280     if (defer_time < 0)
281     defer_time = 0;
282     break;
283     #endif
284    
285 matty 10 case 'u':
286 jsorg71 437 STRNCPY(g_username, optarg, sizeof(g_username));
287 astrand 289 username_option = 1;
288 matty 10 break;
289    
290 matty 21 case 'd':
291 matty 30 STRNCPY(domain, optarg, sizeof(domain));
292 matty 21 break;
293    
294     case 's':
295 matty 30 STRNCPY(shell, optarg, sizeof(shell));
296 matty 21 break;
297    
298     case 'c':
299 matty 30 STRNCPY(directory, optarg, sizeof(directory));
300 matty 21 break;
301    
302 matty 30 case 'p':
303 matthewc 211 if ((optarg[0] == '-') && (optarg[1] == 0))
304     {
305     prompt_password = True;
306     break;
307     }
308    
309 matty 30 STRNCPY(password, optarg, sizeof(password));
310     flags |= RDP_LOGON_AUTO;
311 matthewc 211
312     /* try to overwrite argument so it won't appear in ps */
313 n-ki 171 p = optarg;
314     while (*p)
315     *(p++) = 'X';
316 matty 30 break;
317    
318 matty 10 case 'n':
319 matty 30 STRNCPY(hostname, optarg, sizeof(hostname));
320 matty 10 break;
321    
322     case 'k':
323 astrand 82 STRNCPY(keymapname, optarg, sizeof(keymapname));
324 matty 10 break;
325    
326 matty 30 case 'g':
327 astrand 547 g_fullscreen = False;
328 astrand 263 if (!strcmp(optarg, "workarea"))
329     {
330 jsorg71 447 g_width = g_height = 0;
331 astrand 263 break;
332     }
333    
334 jsorg71 447 g_width = strtol(optarg, &p, 10);
335 astrand 500 if (g_width <= 0)
336     {
337     error("invalid geometry\n");
338     return 1;
339     }
340    
341 matty 30 if (*p == 'x')
342 jsorg71 447 g_height = strtol(p + 1, NULL, 10);
343 matty 30
344 astrand 500 if (g_height <= 0)
345 matty 30 {
346     error("invalid geometry\n");
347     return 1;
348     }
349 astrand 500
350     if (*p == '%')
351     g_width = -g_width;
352    
353 matty 10 break;
354    
355 matty 30 case 'f':
356 jsorg71 447 g_fullscreen = True;
357 matty 30 break;
358    
359 matty 10 case 'b':
360 jsorg71 437 g_orders = False;
361 matty 10 break;
362    
363 stargo 609 case 'B':
364     g_ownbackstore = False;
365     break;
366    
367 matty 30 case 'e':
368 jsorg71 437 g_encryption = False;
369 matty 10 break;
370 astrand 435 case 'E':
371 forsberg 427 packet_encryption = False;
372     break;
373 matty 30 case 'm':
374 jsorg71 447 g_sendmotion = False;
375 matty 28 break;
376 matty 29
377 n-ki 279 case 'C':
378 jsorg71 450 g_owncolmap = True;
379 n-ki 279 break;
380    
381 matthewc 520 case 'D':
382     g_hide_decorations = True;
383     break;
384    
385 astrand 76 case 'K':
386 jsorg71 450 g_grab_keyboard = False;
387 astrand 76 break;
388    
389 matthewc 520 case 'S':
390     if (!strcmp(optarg, "standard"))
391     {
392     g_win_button_size = 18;
393     break;
394     }
395    
396     g_win_button_size = strtol(optarg, &p, 10);
397    
398     if (*p)
399     {
400     error("invalid button size\n");
401     return 1;
402     }
403    
404     break;
405    
406 matthewc 223 case 'T':
407 jsorg71 450 STRNCPY(g_title, optarg, sizeof(g_title));
408 astrand 107 break;
409    
410 astrand 552 case 'N':
411     g_numlock_sync = True;
412     break;
413    
414 stargo 636 case 'X':
415     g_embed_wnd = strtol(optarg, NULL, 10);
416     break;
417 astrand 651
418 jsorg71 309 case 'a':
419 jsorg71 438 g_server_bpp = strtol(optarg, NULL, 10);
420     if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
421     && g_server_bpp != 24)
422 jsorg71 309 {
423     error("invalid server bpp\n");
424     return 1;
425     }
426     break;
427    
428 stargo 637 case 'x':
429 astrand 651
430 stargo 637 if (strncmp("modem", optarg, 1) == 0)
431     {
432 astrand 651 g_rdp5_performanceflags =
433     RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG |
434     RDP5_NO_MENUANIMATIONS | RDP5_NO_THEMING;
435 stargo 637 }
436     else if (strncmp("broadband", optarg, 1) == 0)
437     {
438     g_rdp5_performanceflags = RDP5_NO_WALLPAPER;
439     }
440     else if (strncmp("lan", optarg, 1) == 0)
441     {
442     g_rdp5_performanceflags = RDP5_DISABLE_NOTHING;
443     }
444     else
445     {
446     g_rdp5_performanceflags = strtol(optarg, NULL, 16);
447     }
448     break;
449 astrand 651
450 matthewc 520 case 'r':
451 n-ki 569
452     if (strncmp("sound", optarg, 5) == 0)
453     {
454 n-ki 629 optarg += 5;
455    
456     if (*optarg == ':')
457 n-ki 628 {
458 n-ki 629 *optarg++;
459     while ((p = next_arg(optarg, ',')))
460     {
461     if (strncmp("remote", optarg, 6) == 0)
462     flags |= RDP_LOGON_LEAVE_AUDIO;
463 n-ki 628
464 n-ki 630 if (strncmp("local", optarg, 5) == 0)
465 stargo 501 #ifdef WITH_RDPSND
466 n-ki 629 g_rdpsnd = True;
467 matthewc 520 #else
468 n-ki 629 warning("Not compiled with sound support");
469 matthewc 520 #endif
470 n-ki 629
471     if (strncmp("off", optarg, 3) == 0)
472     g_rdpsnd = False;
473    
474     optarg = p;
475 n-ki 628 }
476     }
477     else
478     {
479     #ifdef WITH_RDPSND
480     g_rdpsnd = True;
481     #else
482     warning("Not compiled with sound support");
483     #endif
484     }
485 n-ki 569 }
486     else if (strncmp("disk", optarg, 4) == 0)
487     {
488     /* -r disk:h:=/mnt/floppy */
489     disk_enum_devices(&g_num_devices, optarg + 4);
490     }
491     else if (strncmp("comport", optarg, 7) == 0)
492     {
493     serial_enum_devices(&g_num_devices, optarg + 7);
494     }
495     else if (strncmp("lptport", optarg, 7) == 0)
496     {
497     parallel_enum_devices(&g_num_devices, optarg + 7);
498     }
499     else if (strncmp("printer", optarg, 7) == 0)
500     {
501     printer_enum_devices(&g_num_devices, optarg + 7);
502     }
503 forsberg 646 else if (strncmp("clientname", optarg, 7) == 0)
504     {
505 astrand 651 g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1);
506 forsberg 646 strcpy(g_rdpdr_clientname, optarg + 11);
507     }
508 n-ki 569 else
509     {
510     warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n");
511     }
512 stargo 501 break;
513 matthewc 520
514 matthewc 482 case '0':
515     g_console_session = True;
516     break;
517    
518 astrand 458 case '4':
519     g_use_rdp5 = False;
520     break;
521    
522 forsberg 350 case '5':
523 jsorg71 438 g_use_rdp5 = True;
524 forsberg 350 break;
525 astrand 458
526 matty 28 case 'h':
527 matty 10 case '?':
528     default:
529     usage(argv[0]);
530     return 1;
531     }
532     }
533    
534 stargo 610 if (argc - optind != 1)
535 matty 10 {
536     usage(argv[0]);
537     return 1;
538     }
539    
540 matthewc 222 STRNCPY(server, argv[optind], sizeof(server));
541 astrand 444 parse_server_and_port(server);
542 matty 10
543 astrand 289 if (!username_option)
544 matty 10 {
545     pw = getpwuid(getuid());
546     if ((pw == NULL) || (pw->pw_name == NULL))
547     {
548 matty 30 error("could not determine username, use -u\n");
549 matty 10 return 1;
550     }
551    
552 jsorg71 437 STRNCPY(g_username, pw->pw_name, sizeof(g_username));
553 matty 10 }
554    
555     if (hostname[0] == 0)
556     {
557 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
558 matty 10 {
559 matty 30 error("could not determine local hostname, use -n\n");
560 matty 10 return 1;
561     }
562 matty 30
563     p = strchr(fullhostname, '.');
564     if (p != NULL)
565     *p = 0;
566    
567     STRNCPY(hostname, fullhostname, sizeof(hostname));
568 matty 10 }
569    
570 matthewc 211 if (prompt_password && read_password(password, sizeof(password)))
571     flags |= RDP_LOGON_AUTO;
572 matty 30
573 jsorg71 450 if (g_title[0] == 0)
574 astrand 107 {
575 jsorg71 450 strcpy(g_title, "rdesktop - ");
576     strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
577 astrand 107 }
578 matty 12
579 astrand 333 #ifdef RDP2VNC
580     rdp2vnc_connect(server, flags, domain, password, shell, directory);
581 forsberg 424 return 0;
582 astrand 333 #else
583    
584 astrand 82 if (!ui_init())
585     return 1;
586 astrand 66
587 matthewc 474 #ifdef WITH_RDPSND
588 stargo 501 if (g_rdpsnd)
589     rdpsnd_init();
590 matthewc 474 #endif
591 n-ki 569 rdpdr_init();
592 forsberg 416
593 matthewc 53 if (!rdp_connect(server, flags, domain, password, shell, directory))
594     return 1;
595    
596 forsberg 427 /* By setting encryption to False here, we have an encrypted login
597     packet but unencrypted transfer of other packets */
598 astrand 435 if (!packet_encryption)
599 jsorg71 437 g_encryption = False;
600 forsberg 427
601    
602 matthewc 122 DEBUG(("Connection successful.\n"));
603 matthewc 211 memset(password, 0, sizeof(password));
604 matthewc 53
605 jsorg71 100 if (ui_create_window())
606 matty 10 {
607 forsberg 424 rdp_retval = rdp_main_loop();
608 matty 10 ui_destroy_window();
609     }
610    
611 matthewc 122 DEBUG(("Disconnecting...\n"));
612 matthewc 53 rdp_disconnect();
613 matthewc 188 ui_deinit();
614 astrand 333
615 astrand 435 if (True == rdp_retval)
616 forsberg 424 return 0;
617     else
618     return 2;
619    
620 astrand 333 #endif
621    
622 matty 10 }
623    
624 matthewc 220 #ifdef EGD_SOCKET
625     /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
626     static BOOL
627     generate_random_egd(uint8 * buf)
628     {
629     struct sockaddr_un addr;
630     BOOL ret = False;
631     int fd;
632    
633     fd = socket(AF_UNIX, SOCK_STREAM, 0);
634     if (fd == -1)
635     return False;
636    
637     addr.sun_family = AF_UNIX;
638     memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
639 astrand 259 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
640 matthewc 220 goto err;
641    
642     /* PRNGD and EGD use a simple communications protocol */
643 astrand 259 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
644     buf[1] = 32; /* Number of requested random bytes */
645 matthewc 220 if (write(fd, buf, 2) != 2)
646     goto err;
647    
648 astrand 259 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
649 matthewc 220 goto err;
650    
651     if (read(fd, buf, 32) != 32)
652     goto err;
653    
654     ret = True;
655    
656 astrand 259 err:
657 matthewc 220 close(fd);
658     return ret;
659     }
660     #endif
661    
662 matty 10 /* Generate a 32-byte random for the secure transport code. */
663 matty 25 void
664 astrand 64 generate_random(uint8 * random)
665 matty 10 {
666     struct stat st;
667 matty 22 struct tms tmsbuf;
668 matthewc 220 MD5_CTX md5;
669     uint32 *r;
670     int fd, n;
671 matty 10
672 matthewc 220 /* If we have a kernel random device, try that first */
673 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
674     || ((fd = open("/dev/random", O_RDONLY)) != -1))
675 matty 10 {
676 matthewc 220 n = read(fd, random, 32);
677 matty 10 close(fd);
678 matthewc 220 if (n == 32)
679     return;
680 matty 10 }
681    
682 matthewc 220 #ifdef EGD_SOCKET
683     /* As a second preference use an EGD */
684     if (generate_random_egd(random))
685     return;
686     #endif
687    
688 matty 10 /* Otherwise use whatever entropy we can gather - ideas welcome. */
689 astrand 259 r = (uint32 *) random;
690 matty 10 r[0] = (getpid()) | (getppid() << 16);
691     r[1] = (getuid()) | (getgid() << 16);
692 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
693     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
694 matty 10 stat("/tmp", &st);
695     r[5] = st.st_atime;
696     r[6] = st.st_mtime;
697     r[7] = st.st_ctime;
698 matthewc 220
699     /* Hash both halves with MD5 to obscure possible patterns */
700     MD5_Init(&md5);
701 astrand 259 MD5_Update(&md5, random, 16);
702 matthewc 220 MD5_Final(random, &md5);
703 astrand 259 MD5_Update(&md5, random + 16, 16);
704     MD5_Final(random + 16, &md5);
705 matty 10 }
706    
707     /* malloc; exit if out of memory */
708 matty 25 void *
709     xmalloc(int size)
710 matty 10 {
711     void *mem = malloc(size);
712     if (mem == NULL)
713     {
714 matty 30 error("xmalloc %d\n", size);
715 matty 10 exit(1);
716     }
717     return mem;
718     }
719    
720     /* realloc; exit if out of memory */
721 matty 25 void *
722     xrealloc(void *oldmem, int size)
723 matty 10 {
724     void *mem = realloc(oldmem, size);
725     if (mem == NULL)
726     {
727 matty 30 error("xrealloc %d\n", size);
728 matty 10 exit(1);
729     }
730     return mem;
731     }
732    
733     /* free */
734 matty 25 void
735     xfree(void *mem)
736 matty 10 {
737     free(mem);
738     }
739    
740 matty 30 /* report an error */
741 matty 25 void
742 matty 30 error(char *format, ...)
743     {
744     va_list ap;
745    
746     fprintf(stderr, "ERROR: ");
747    
748     va_start(ap, format);
749     vfprintf(stderr, format, ap);
750     va_end(ap);
751     }
752    
753 matthewc 297 /* report a warning */
754     void
755     warning(char *format, ...)
756     {
757     va_list ap;
758    
759     fprintf(stderr, "WARNING: ");
760    
761     va_start(ap, format);
762     vfprintf(stderr, format, ap);
763     va_end(ap);
764     }
765    
766 matty 30 /* report an unimplemented protocol feature */
767     void
768     unimpl(char *format, ...)
769     {
770     va_list ap;
771    
772     fprintf(stderr, "NOT IMPLEMENTED: ");
773    
774     va_start(ap, format);
775     vfprintf(stderr, format, ap);
776     va_end(ap);
777     }
778    
779     /* produce a hex dump */
780     void
781 n-ki 569 hexdump(unsigned char *p, unsigned int len)
782 matty 10 {
783     unsigned char *line = p;
784 jsorg71 376 int i, thisline, offset = 0;
785 matty 10
786     while (offset < len)
787     {
788 matthewc 169 printf("%04x ", offset);
789 matty 10 thisline = len - offset;
790     if (thisline > 16)
791     thisline = 16;
792    
793     for (i = 0; i < thisline; i++)
794 matthewc 169 printf("%02x ", line[i]);
795 matty 10
796 matty 30 for (; i < 16; i++)
797 matthewc 169 printf(" ");
798 matty 30
799 matty 10 for (i = 0; i < thisline; i++)
800 matthewc 169 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
801 matty 10
802 matthewc 169 printf("\n");
803 matty 10 offset += thisline;
804     line += thisline;
805     }
806     }
807 astrand 325
808 n-ki 569 /*
809 n-ki 583 input: src is the string we look in for needle.
810     Needle may be escaped by a backslash, in
811     that case we ignore that particular needle.
812 n-ki 569 return value: returns next src pointer, for
813     succesive executions, like in a while loop
814     if retval is 0, then there are no more args.
815     pitfalls:
816     src is modified. 0x00 chars are inserted to
817     terminate strings.
818     return val, points on the next val chr after ins
819     0x00
820 astrand 325
821 n-ki 569 example usage:
822     while( (pos = next_arg( optarg, ',')) ){
823     printf("%s\n",optarg);
824     optarg=pos;
825     }
826    
827     */
828     char *
829     next_arg(char *src, char needle)
830     {
831     char *nextval;
832 n-ki 571 char *p;
833 n-ki 575 char *mvp = 0;
834 n-ki 569
835 n-ki 575 /* EOS */
836 n-ki 569 if (*src == (char) 0x00)
837     return 0;
838    
839 n-ki 571 p = src;
840 n-ki 575 /* skip escaped needles */
841 astrand 580 while ((nextval = strchr(p, needle)))
842 n-ki 571 {
843 n-ki 575 mvp = nextval - 1;
844     /* found backslashed needle */
845 astrand 580 if (*mvp == '\\' && (mvp > src))
846 n-ki 575 {
847     /* move string one to the left */
848 astrand 580 while (*(mvp + 1) != (char) 0x00)
849 n-ki 575 {
850 astrand 580 *mvp = *(mvp + 1);
851 n-ki 575 *mvp++;
852     }
853 astrand 580 *mvp = (char) 0x00;
854 n-ki 575 p = nextval;
855     }
856     else
857     {
858 astrand 580 p = nextval + 1;
859 n-ki 571 break;
860 n-ki 575 }
861    
862 n-ki 571 }
863    
864 n-ki 575 /* more args available */
865 n-ki 569 if (nextval)
866     {
867     *nextval = (char) 0x00;
868     return ++nextval;
869     }
870    
871 n-ki 575 /* no more args after this, jump to EOS */
872 n-ki 569 nextval = src + strlen(src);
873     return nextval;
874     }
875    
876    
877 stargo 570 void
878 astrand 580 toupper_str(char *p)
879 n-ki 569 {
880 astrand 580 while (*p)
881     {
882     if ((*p >= 'a') && (*p <= 'z'))
883 stargo 570 *p = toupper((int) *p);
884 n-ki 569 p++;
885     }
886     }
887    
888    
889     /* not all clibs got ltoa */
890     #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
891    
892     char *
893 stargo 603 l_to_a(long N, int base)
894 n-ki 569 {
895     static char ret[LTOA_BUFSIZE];
896    
897 n-ki 583 char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf);
898 n-ki 569
899 n-ki 583 register int divrem;
900    
901     if (base < 36 || 2 > base)
902 n-ki 569 base = 10;
903    
904 n-ki 583 if (N < 0)
905 n-ki 569 {
906     *head++ = '-';
907 n-ki 583 N = -N;
908 n-ki 569 }
909    
910 n-ki 583 tail = buf + sizeof(buf);
911     *--tail = 0;
912    
913     do
914 n-ki 569 {
915 n-ki 583 divrem = N % base;
916     *--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10;
917     N /= base;
918 n-ki 569 }
919 n-ki 583 while (N);
920 n-ki 569
921 n-ki 583 strcpy(head, tail);
922 n-ki 569 return ret;
923     }
924    
925    
926 astrand 325 int
927     load_licence(unsigned char **data)
928     {
929 matthewc 367 char *home, *path;
930 astrand 325 struct stat st;
931 matthewc 367 int fd, length;
932 astrand 325
933     home = getenv("HOME");
934     if (home == NULL)
935     return -1;
936    
937 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
938 astrand 325 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
939    
940     fd = open(path, O_RDONLY);
941     if (fd == -1)
942     return -1;
943    
944     if (fstat(fd, &st))
945     return -1;
946    
947 forsberg 416 *data = (uint8 *) xmalloc(st.st_size);
948 matthewc 367 length = read(fd, *data, st.st_size);
949     close(fd);
950     xfree(path);
951     return length;
952 astrand 325 }
953    
954     void
955     save_licence(unsigned char *data, int length)
956     {
957 matthewc 367 char *home, *path, *tmppath;
958     int fd;
959 astrand 325
960     home = getenv("HOME");
961     if (home == NULL)
962     return;
963    
964 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
965 astrand 325
966 matthewc 367 sprintf(path, "%s/.rdesktop", home);
967     if ((mkdir(path, 0700) == -1) && errno != EEXIST)
968 astrand 325 {
969 matthewc 367 perror(path);
970     return;
971 astrand 325 }
972    
973 matthewc 367 /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
974 astrand 325
975 matthewc 367 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
976 forsberg 416 tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
977 matthewc 367 strcpy(tmppath, path);
978     strcat(tmppath, ".new");
979    
980 forsberg 416 fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
981 matthewc 367 if (fd == -1)
982 astrand 325 {
983 matthewc 367 perror(tmppath);
984     return;
985 astrand 325 }
986    
987 matthewc 367 if (write(fd, data, length) != length)
988 astrand 325 {
989 matthewc 367 perror(tmppath);
990     unlink(tmppath);
991 astrand 325 }
992 matthewc 367 else if (rename(tmppath, path) == -1)
993 astrand 325 {
994 matthewc 367 perror(path);
995     unlink(tmppath);
996 astrand 325 }
997    
998 matthewc 367 close(fd);
999     xfree(tmppath);
1000     xfree(path);
1001 astrand 325 }

  ViewVC Help
Powered by ViewVC 1.1.26