/[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 609 - (hide annotations)
Mon Feb 16 20:28:09 2004 UTC (20 years, 3 months ago) by stargo
File MIME type: text/plain
File size: 19434 byte(s)
Make internal backing-store the default && small fix for it

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

  ViewVC Help
Powered by ViewVC 1.1.26