/[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 637 - (hide annotations)
Mon Mar 15 14:49:12 2004 UTC (20 years, 2 months ago) by stargo
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 20921 byte(s)
RDP5 Experience patch by Jeroen Meijer <jeroen@oldambt7.com>

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

  ViewVC Help
Powered by ViewVC 1.1.26