/[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 520 - (hide annotations)
Tue Oct 28 05:07:00 2003 UTC (20 years, 6 months ago) by matthewc
File MIME type: text/plain
File size: 15668 byte(s)
-A -> -r sound
Update option listing and documentation for this and some other options.

1 forsberg 350 /* -*- c-basic-offset: 8 -*-
2 matty 10 rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions
4 matthewc 304 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 astrand 325 #include <errno.h>
30 matty 10 #include "rdesktop.h"
31    
32 matthewc 220 #ifdef EGD_SOCKET
33     #include <sys/socket.h> /* socket connect */
34     #include <sys/un.h> /* sockaddr_un */
35     #endif
36    
37     #ifdef WITH_OPENSSL
38     #include <openssl/md5.h>
39     #else
40     #include "crypto/md5.h"
41     #endif
42    
43 astrand 479 char g_title[64] = "";
44     char g_username[64];
45 matty 10 char hostname[16];
46 matthewc 38 char keymapname[16];
47 astrand 66 int keylayout = 0x409; /* Defaults to US keyboard layout */
48 astrand 500
49     int g_width = 800; /* width is special: If 0, the
50     geometry will be fetched from
51     _NET_WORKAREA. If negative,
52     absolute value specifies the
53     percent of the whole screen. */
54 jsorg71 447 int g_height = 600;
55 jsorg71 58 int tcp_port_rdp = TCP_PORT_RDP;
56 jsorg71 438 int g_server_bpp = 8;
57 jsorg71 450 int g_win_button_size = 0; /* If zero, disable single app mode */
58 jsorg71 437 BOOL g_bitmap_compression = True;
59 jsorg71 447 BOOL g_sendmotion = True;
60 jsorg71 437 BOOL g_orders = True;
61     BOOL g_encryption = True;
62 forsberg 427 BOOL packet_encryption = True;
63 jsorg71 438 BOOL g_desktop_save = True;
64 jsorg71 447 BOOL g_fullscreen = False;
65 jsorg71 450 BOOL g_grab_keyboard = True;
66     BOOL g_hide_decorations = False;
67 astrand 458 BOOL g_use_rdp5 = True;
68 matthewc 482 BOOL g_console_session = False;
69 jsorg71 450 extern BOOL g_owncolmap;
70 matty 10
71 stargo 501 #ifdef WITH_RDPSND
72 matthewc 520 BOOL g_rdpsnd = False;
73 stargo 501 #endif
74    
75 astrand 333 #ifdef RDP2VNC
76     extern int rfb_port;
77     extern int defer_time;
78     void
79     rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
80     char *shell, char *directory);
81     #endif
82 matty 10 /* Display usage information */
83 matty 25 static void
84     usage(char *program)
85 matty 10 {
86 matthewc 122 fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
87 matthewc 304 fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
88 matthewc 122 fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
89    
90 matthewc 222 fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
91 astrand 333 #ifdef RDP2VNC
92     fprintf(stderr, " -V: vnc port\n");
93 forsberg 471 fprintf(stderr, " -Q: defer time (ms)\n");
94 astrand 333 #endif
95 astrand 111 fprintf(stderr, " -u: user name\n");
96     fprintf(stderr, " -d: domain\n");
97     fprintf(stderr, " -s: shell\n");
98     fprintf(stderr, " -c: working directory\n");
99 matthewc 211 fprintf(stderr, " -p: password (- to prompt)\n");
100 astrand 111 fprintf(stderr, " -n: client hostname\n");
101 matthewc 520 fprintf(stderr, " -k: keyboard layout on server (en-us, de, sv, etc.)\n");
102 astrand 111 fprintf(stderr, " -g: desktop geometry (WxH)\n");
103     fprintf(stderr, " -f: full-screen mode\n");
104     fprintf(stderr, " -b: force bitmap updates\n");
105     fprintf(stderr, " -e: disable encryption (French TS)\n");
106 forsberg 471 fprintf(stderr, " -E: disable encryption from client to server\n");
107 astrand 111 fprintf(stderr, " -m: do not send motion events\n");
108 n-ki 279 fprintf(stderr, " -C: use private colour map\n");
109 matthewc 520 fprintf(stderr, " -D: hide window manager decorations\n");
110 astrand 111 fprintf(stderr, " -K: keep window manager key bindings\n");
111 matthewc 520 fprintf(stderr, " -S: caption button size (single application mode)\n");
112 matthewc 223 fprintf(stderr, " -T: window title\n");
113 matthewc 520 fprintf(stderr, " -a: connection colour depth\n");
114     fprintf(stderr, " -r: enable specified device redirection (currently: sound)\n");
115 matthewc 482 fprintf(stderr, " -0: attach to console\n");
116     fprintf(stderr, " -4: use RDP version 4\n");
117     fprintf(stderr, " -5: use RDP version 5 (default)\n");
118 matty 10 }
119    
120 matthewc 211 static BOOL
121     read_password(char *password, int size)
122     {
123     struct termios tios;
124     BOOL ret = False;
125     int istty = 0;
126     char *p;
127    
128     if (tcgetattr(STDIN_FILENO, &tios) == 0)
129     {
130     fprintf(stderr, "Password: ");
131     tios.c_lflag &= ~ECHO;
132     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
133     istty = 1;
134     }
135    
136     if (fgets(password, size, stdin) != NULL)
137     {
138     ret = True;
139    
140     /* strip final newline */
141     p = strchr(password, '\n');
142     if (p != NULL)
143     *p = 0;
144     }
145    
146     if (istty)
147     {
148     tios.c_lflag |= ECHO;
149     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
150     fprintf(stderr, "\n");
151     }
152    
153     return ret;
154     }
155    
156 astrand 444 static void
157     parse_server_and_port(char *server)
158     {
159     char *p;
160     #ifdef IPv6
161     int addr_colons;
162     #endif
163    
164     #ifdef IPv6
165     p = server;
166     addr_colons = 0;
167     while (*p)
168     if (*p++ == ':')
169     addr_colons++;
170     if (addr_colons >= 2)
171     {
172     /* numeric IPv6 style address format - [1:2:3::4]:port */
173     p = strchr(server, ']');
174     if (*server == '[' && p != NULL)
175     {
176     if (*(p + 1) == ':' && *(p + 2) != '\0')
177     tcp_port_rdp = strtol(p + 2, NULL, 10);
178     /* remove the port number and brackets from the address */
179     *p = '\0';
180     strncpy(server, server + 1, strlen(server));
181     }
182     }
183     else
184     {
185     /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
186     p = strchr(server, ':');
187     if (p != NULL)
188     {
189     tcp_port_rdp = strtol(p + 1, NULL, 10);
190     *p = 0;
191     }
192     }
193     #else /* no IPv6 support */
194     p = strchr(server, ':');
195     if (p != NULL)
196     {
197     tcp_port_rdp = strtol(p + 1, NULL, 10);
198     *p = 0;
199     }
200     #endif /* IPv6 */
201    
202     }
203    
204 matty 10 /* Client program */
205 matty 25 int
206     main(int argc, char *argv[])
207 matty 10 {
208 matthewc 222 char server[64];
209 matty 30 char fullhostname[64];
210 matty 21 char domain[16];
211 astrand 479 char password[64];
212 astrand 238 char shell[128];
213 matty 21 char directory[32];
214 forsberg 424 BOOL prompt_password, rdp_retval = False;
215 matty 30 struct passwd *pw;
216     uint32 flags;
217 matthewc 222 char *p;
218 matty 10 int c;
219 astrand 289 int username_option = 0;
220 matty 10
221 matty 21 flags = RDP_LOGON_NORMAL;
222 matthewc 211 prompt_password = False;
223 matty 21 domain[0] = password[0] = shell[0] = directory[0] = 0;
224 matthewc 240 strcpy(keymapname, "en-us");
225 matty 21
226 astrand 333 #ifdef RDP2VNC
227 forsberg 471 #define VNCOPT "V:Q:"
228 astrand 333 #else
229     #define VNCOPT
230     #endif
231    
232 matthewc 520 while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbeEmCDKS:T:a:r:045h?")) != -1)
233 matty 10 {
234     switch (c)
235     {
236 astrand 333 #ifdef RDP2VNC
237     case 'V':
238     rfb_port = strtol(optarg, NULL, 10);
239     if (rfb_port < 100)
240     rfb_port += 5900;
241     break;
242    
243 forsberg 471 case 'Q':
244 astrand 333 defer_time = strtol(optarg, NULL, 10);
245     if (defer_time < 0)
246     defer_time = 0;
247     break;
248     #endif
249    
250 matty 10 case 'u':
251 jsorg71 437 STRNCPY(g_username, optarg, sizeof(g_username));
252 astrand 289 username_option = 1;
253 matty 10 break;
254    
255 matty 21 case 'd':
256 matty 30 STRNCPY(domain, optarg, sizeof(domain));
257 matty 21 break;
258    
259     case 's':
260 matty 30 STRNCPY(shell, optarg, sizeof(shell));
261 matty 21 break;
262    
263     case 'c':
264 matty 30 STRNCPY(directory, optarg, sizeof(directory));
265 matty 21 break;
266    
267 matty 30 case 'p':
268 matthewc 211 if ((optarg[0] == '-') && (optarg[1] == 0))
269     {
270     prompt_password = True;
271     break;
272     }
273    
274 matty 30 STRNCPY(password, optarg, sizeof(password));
275     flags |= RDP_LOGON_AUTO;
276 matthewc 211
277     /* try to overwrite argument so it won't appear in ps */
278 n-ki 171 p = optarg;
279     while (*p)
280     *(p++) = 'X';
281 matty 30 break;
282    
283 matty 10 case 'n':
284 matty 30 STRNCPY(hostname, optarg, sizeof(hostname));
285 matty 10 break;
286    
287     case 'k':
288 astrand 82 STRNCPY(keymapname, optarg, sizeof(keymapname));
289 matty 10 break;
290    
291 matty 30 case 'g':
292 astrand 263 if (!strcmp(optarg, "workarea"))
293     {
294 jsorg71 447 g_width = g_height = 0;
295 astrand 263 break;
296     }
297    
298 jsorg71 447 g_width = strtol(optarg, &p, 10);
299 astrand 500 if (g_width <= 0)
300     {
301     error("invalid geometry\n");
302     return 1;
303     }
304    
305 matty 30 if (*p == 'x')
306 jsorg71 447 g_height = strtol(p + 1, NULL, 10);
307 matty 30
308 astrand 500 if (g_height <= 0)
309 matty 30 {
310     error("invalid geometry\n");
311     return 1;
312     }
313 astrand 500
314     if (*p == '%')
315     g_width = -g_width;
316    
317 matty 10 break;
318    
319 matty 30 case 'f':
320 jsorg71 447 g_fullscreen = True;
321 matty 30 break;
322    
323 matty 10 case 'b':
324 jsorg71 437 g_orders = False;
325 matty 10 break;
326    
327 matty 30 case 'e':
328 jsorg71 437 g_encryption = False;
329 matty 10 break;
330 astrand 435 case 'E':
331 forsberg 427 packet_encryption = False;
332     break;
333 matty 30 case 'm':
334 jsorg71 447 g_sendmotion = False;
335 matty 28 break;
336 matty 29
337 n-ki 279 case 'C':
338 jsorg71 450 g_owncolmap = True;
339 n-ki 279 break;
340    
341 matthewc 520 case 'D':
342     g_hide_decorations = True;
343     break;
344    
345 astrand 76 case 'K':
346 jsorg71 450 g_grab_keyboard = False;
347 astrand 76 break;
348    
349 matthewc 520 case 'S':
350     if (!strcmp(optarg, "standard"))
351     {
352     g_win_button_size = 18;
353     break;
354     }
355    
356     g_win_button_size = strtol(optarg, &p, 10);
357    
358     if (*p)
359     {
360     error("invalid button size\n");
361     return 1;
362     }
363    
364     break;
365    
366 matthewc 223 case 'T':
367 jsorg71 450 STRNCPY(g_title, optarg, sizeof(g_title));
368 astrand 107 break;
369    
370 jsorg71 309 case 'a':
371 jsorg71 438 g_server_bpp = strtol(optarg, NULL, 10);
372     if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
373     && g_server_bpp != 24)
374 jsorg71 309 {
375     error("invalid server bpp\n");
376     return 1;
377     }
378     break;
379    
380 matthewc 520 case 'r':
381     if (!strcmp(optarg, "sound"))
382 stargo 501 #ifdef WITH_RDPSND
383 matthewc 520 g_rdpsnd = True;
384     #else
385     warning("Not compiled with sound support");
386     #endif
387 stargo 501 break;
388 matthewc 520
389 matthewc 482 case '0':
390     g_console_session = True;
391     break;
392    
393 astrand 458 case '4':
394     g_use_rdp5 = False;
395     break;
396    
397 forsberg 350 case '5':
398 jsorg71 438 g_use_rdp5 = True;
399 forsberg 350 break;
400 astrand 458
401 matty 28 case 'h':
402 matty 10 case '?':
403     default:
404     usage(argv[0]);
405     return 1;
406     }
407     }
408    
409     if (argc - optind < 1)
410     {
411     usage(argv[0]);
412     return 1;
413     }
414    
415 matthewc 222 STRNCPY(server, argv[optind], sizeof(server));
416 astrand 444 parse_server_and_port(server);
417 matty 10
418 astrand 289 if (!username_option)
419 matty 10 {
420     pw = getpwuid(getuid());
421     if ((pw == NULL) || (pw->pw_name == NULL))
422     {
423 matty 30 error("could not determine username, use -u\n");
424 matty 10 return 1;
425     }
426    
427 jsorg71 437 STRNCPY(g_username, pw->pw_name, sizeof(g_username));
428 matty 10 }
429    
430     if (hostname[0] == 0)
431     {
432 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
433 matty 10 {
434 matty 30 error("could not determine local hostname, use -n\n");
435 matty 10 return 1;
436     }
437 matty 30
438     p = strchr(fullhostname, '.');
439     if (p != NULL)
440     *p = 0;
441    
442     STRNCPY(hostname, fullhostname, sizeof(hostname));
443 matty 10 }
444    
445 matthewc 211 if (prompt_password && read_password(password, sizeof(password)))
446     flags |= RDP_LOGON_AUTO;
447 matty 30
448 jsorg71 450 if (g_title[0] == 0)
449 astrand 107 {
450 jsorg71 450 strcpy(g_title, "rdesktop - ");
451     strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
452 astrand 107 }
453 matty 12
454 astrand 333 #ifdef RDP2VNC
455     rdp2vnc_connect(server, flags, domain, password, shell, directory);
456 forsberg 424 return 0;
457 astrand 333 #else
458    
459 astrand 82 if (!ui_init())
460     return 1;
461 astrand 66
462 matthewc 474 #ifdef WITH_RDPSND
463 stargo 501 if (g_rdpsnd)
464     rdpsnd_init();
465 matthewc 474 #endif
466 matthewc 432 /* rdpdr_init(); */
467 forsberg 416
468 matthewc 53 if (!rdp_connect(server, flags, domain, password, shell, directory))
469     return 1;
470    
471 forsberg 427 /* By setting encryption to False here, we have an encrypted login
472     packet but unencrypted transfer of other packets */
473 astrand 435 if (!packet_encryption)
474 jsorg71 437 g_encryption = False;
475 forsberg 427
476    
477 matthewc 122 DEBUG(("Connection successful.\n"));
478 matthewc 211 memset(password, 0, sizeof(password));
479 matthewc 53
480 jsorg71 100 if (ui_create_window())
481 matty 10 {
482 forsberg 424 rdp_retval = rdp_main_loop();
483 matty 10 ui_destroy_window();
484     }
485    
486 matthewc 122 DEBUG(("Disconnecting...\n"));
487 matthewc 53 rdp_disconnect();
488 matthewc 188 ui_deinit();
489 astrand 333
490 astrand 435 if (True == rdp_retval)
491 forsberg 424 return 0;
492     else
493     return 2;
494    
495 astrand 333 #endif
496    
497 matty 10 }
498    
499 matthewc 220 #ifdef EGD_SOCKET
500     /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
501     static BOOL
502     generate_random_egd(uint8 * buf)
503     {
504     struct sockaddr_un addr;
505     BOOL ret = False;
506     int fd;
507    
508     fd = socket(AF_UNIX, SOCK_STREAM, 0);
509     if (fd == -1)
510     return False;
511    
512     addr.sun_family = AF_UNIX;
513     memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
514 astrand 259 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
515 matthewc 220 goto err;
516    
517     /* PRNGD and EGD use a simple communications protocol */
518 astrand 259 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
519     buf[1] = 32; /* Number of requested random bytes */
520 matthewc 220 if (write(fd, buf, 2) != 2)
521     goto err;
522    
523 astrand 259 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
524 matthewc 220 goto err;
525    
526     if (read(fd, buf, 32) != 32)
527     goto err;
528    
529     ret = True;
530    
531 astrand 259 err:
532 matthewc 220 close(fd);
533     return ret;
534     }
535     #endif
536    
537 matty 10 /* Generate a 32-byte random for the secure transport code. */
538 matty 25 void
539 astrand 64 generate_random(uint8 * random)
540 matty 10 {
541     struct stat st;
542 matty 22 struct tms tmsbuf;
543 matthewc 220 MD5_CTX md5;
544     uint32 *r;
545     int fd, n;
546 matty 10
547 matthewc 220 /* If we have a kernel random device, try that first */
548 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
549     || ((fd = open("/dev/random", O_RDONLY)) != -1))
550 matty 10 {
551 matthewc 220 n = read(fd, random, 32);
552 matty 10 close(fd);
553 matthewc 220 if (n == 32)
554     return;
555 matty 10 }
556    
557 matthewc 220 #ifdef EGD_SOCKET
558     /* As a second preference use an EGD */
559     if (generate_random_egd(random))
560     return;
561     #endif
562    
563 matty 10 /* Otherwise use whatever entropy we can gather - ideas welcome. */
564 astrand 259 r = (uint32 *) random;
565 matty 10 r[0] = (getpid()) | (getppid() << 16);
566     r[1] = (getuid()) | (getgid() << 16);
567 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
568     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
569 matty 10 stat("/tmp", &st);
570     r[5] = st.st_atime;
571     r[6] = st.st_mtime;
572     r[7] = st.st_ctime;
573 matthewc 220
574     /* Hash both halves with MD5 to obscure possible patterns */
575     MD5_Init(&md5);
576 astrand 259 MD5_Update(&md5, random, 16);
577 matthewc 220 MD5_Final(random, &md5);
578 astrand 259 MD5_Update(&md5, random + 16, 16);
579     MD5_Final(random + 16, &md5);
580 matty 10 }
581    
582     /* malloc; exit if out of memory */
583 matty 25 void *
584     xmalloc(int size)
585 matty 10 {
586     void *mem = malloc(size);
587     if (mem == NULL)
588     {
589 matty 30 error("xmalloc %d\n", size);
590 matty 10 exit(1);
591     }
592     return mem;
593     }
594    
595     /* realloc; exit if out of memory */
596 matty 25 void *
597     xrealloc(void *oldmem, int size)
598 matty 10 {
599     void *mem = realloc(oldmem, size);
600     if (mem == NULL)
601     {
602 matty 30 error("xrealloc %d\n", size);
603 matty 10 exit(1);
604     }
605     return mem;
606     }
607    
608     /* free */
609 matty 25 void
610     xfree(void *mem)
611 matty 10 {
612     free(mem);
613     }
614    
615 matty 30 /* report an error */
616 matty 25 void
617 matty 30 error(char *format, ...)
618     {
619     va_list ap;
620    
621     fprintf(stderr, "ERROR: ");
622    
623     va_start(ap, format);
624     vfprintf(stderr, format, ap);
625     va_end(ap);
626     }
627    
628 matthewc 297 /* report a warning */
629     void
630     warning(char *format, ...)
631     {
632     va_list ap;
633    
634     fprintf(stderr, "WARNING: ");
635    
636     va_start(ap, format);
637     vfprintf(stderr, format, ap);
638     va_end(ap);
639     }
640    
641 matty 30 /* report an unimplemented protocol feature */
642     void
643     unimpl(char *format, ...)
644     {
645     va_list ap;
646    
647     fprintf(stderr, "NOT IMPLEMENTED: ");
648    
649     va_start(ap, format);
650     vfprintf(stderr, format, ap);
651     va_end(ap);
652     }
653    
654     /* produce a hex dump */
655     void
656 jsorg71 376 hexdump(unsigned char *p, int len)
657 matty 10 {
658     unsigned char *line = p;
659 jsorg71 376 int i, thisline, offset = 0;
660 matty 10
661     while (offset < len)
662     {
663 matthewc 169 printf("%04x ", offset);
664 matty 10 thisline = len - offset;
665     if (thisline > 16)
666     thisline = 16;
667    
668     for (i = 0; i < thisline; i++)
669 matthewc 169 printf("%02x ", line[i]);
670 matty 10
671 matty 30 for (; i < 16; i++)
672 matthewc 169 printf(" ");
673 matty 30
674 matty 10 for (i = 0; i < thisline; i++)
675 matthewc 169 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
676 matty 10
677 matthewc 169 printf("\n");
678 matty 10 offset += thisline;
679     line += thisline;
680     }
681     }
682 astrand 325
683    
684     int
685     load_licence(unsigned char **data)
686     {
687 matthewc 367 char *home, *path;
688 astrand 325 struct stat st;
689 matthewc 367 int fd, length;
690 astrand 325
691     home = getenv("HOME");
692     if (home == NULL)
693     return -1;
694    
695 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
696 astrand 325 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
697    
698     fd = open(path, O_RDONLY);
699     if (fd == -1)
700     return -1;
701    
702     if (fstat(fd, &st))
703     return -1;
704    
705 forsberg 416 *data = (uint8 *) xmalloc(st.st_size);
706 matthewc 367 length = read(fd, *data, st.st_size);
707     close(fd);
708     xfree(path);
709     return length;
710 astrand 325 }
711    
712     void
713     save_licence(unsigned char *data, int length)
714     {
715 matthewc 367 char *home, *path, *tmppath;
716     int fd;
717 astrand 325
718     home = getenv("HOME");
719     if (home == NULL)
720     return;
721    
722 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
723 astrand 325
724 matthewc 367 sprintf(path, "%s/.rdesktop", home);
725     if ((mkdir(path, 0700) == -1) && errno != EEXIST)
726 astrand 325 {
727 matthewc 367 perror(path);
728     return;
729 astrand 325 }
730    
731 matthewc 367 /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
732 astrand 325
733 matthewc 367 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
734 forsberg 416 tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
735 matthewc 367 strcpy(tmppath, path);
736     strcat(tmppath, ".new");
737    
738 forsberg 416 fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
739 matthewc 367 if (fd == -1)
740 astrand 325 {
741 matthewc 367 perror(tmppath);
742     return;
743 astrand 325 }
744    
745 matthewc 367 if (write(fd, data, length) != length)
746 astrand 325 {
747 matthewc 367 perror(tmppath);
748     unlink(tmppath);
749 astrand 325 }
750 matthewc 367 else if (rename(tmppath, path) == -1)
751 astrand 325 {
752 matthewc 367 perror(path);
753     unlink(tmppath);
754 astrand 325 }
755    
756 matthewc 367 close(fd);
757     xfree(tmppath);
758     xfree(path);
759 astrand 325 }

  ViewVC Help
Powered by ViewVC 1.1.26