/[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 630 - (hide annotations)
Fri Mar 5 06:39:00 2004 UTC (20 years, 2 months ago) by n-ki
File MIME type: text/plain
File size: 20022 byte(s)
change to -r sound:local instead of -r sound:on

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

  ViewVC Help
Powered by ViewVC 1.1.26