/[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 500 - (hide annotations)
Wed Oct 15 14:32:43 2003 UTC (20 years, 7 months ago) by astrand
File MIME type: text/plain
File size: 15343 byte(s)
Support for specifying geometry in terms of percent of whole screen

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

  ViewVC Help
Powered by ViewVC 1.1.26