/[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 636 - (hide annotations)
Sat Mar 13 12:08:18 2004 UTC (20 years, 2 months ago) by stargo
File MIME type: text/plain
File size: 20215 byte(s)
XEmbed-patch by Arend van Beelen jr. <arend@auton.nl>

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

  ViewVC Help
Powered by ViewVC 1.1.26