/[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 646 - (hide annotations)
Fri Apr 2 15:34:38 2004 UTC (20 years, 1 month ago) by forsberg
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 21271 byte(s)
Made it possible to set the client name displayed to the right of the
redirected disks (in Windows explorer) using -r:clientname=<client name>.

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

  ViewVC Help
Powered by ViewVC 1.1.26