/[rdesktop]/sourceforge.net/branches/seamlessrdp-branch/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/branches/seamlessrdp-branch/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 547 - (hide annotations)
Mon Nov 10 15:09:49 2003 UTC (20 years, 6 months ago) by astrand
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 15694 byte(s)
When mixing -f and -g, the last option should take precedence.

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 547 g_fullscreen = False;
293 astrand 263 if (!strcmp(optarg, "workarea"))
294     {
295 jsorg71 447 g_width = g_height = 0;
296 astrand 263 break;
297     }
298    
299 jsorg71 447 g_width = strtol(optarg, &p, 10);
300 astrand 500 if (g_width <= 0)
301     {
302     error("invalid geometry\n");
303     return 1;
304     }
305    
306 matty 30 if (*p == 'x')
307 jsorg71 447 g_height = strtol(p + 1, NULL, 10);
308 matty 30
309 astrand 500 if (g_height <= 0)
310 matty 30 {
311     error("invalid geometry\n");
312     return 1;
313     }
314 astrand 500
315     if (*p == '%')
316     g_width = -g_width;
317    
318 matty 10 break;
319    
320 matty 30 case 'f':
321 jsorg71 447 g_fullscreen = True;
322 matty 30 break;
323    
324 matty 10 case 'b':
325 jsorg71 437 g_orders = False;
326 matty 10 break;
327    
328 matty 30 case 'e':
329 jsorg71 437 g_encryption = False;
330 matty 10 break;
331 astrand 435 case 'E':
332 forsberg 427 packet_encryption = False;
333     break;
334 matty 30 case 'm':
335 jsorg71 447 g_sendmotion = False;
336 matty 28 break;
337 matty 29
338 n-ki 279 case 'C':
339 jsorg71 450 g_owncolmap = True;
340 n-ki 279 break;
341    
342 matthewc 520 case 'D':
343     g_hide_decorations = True;
344     break;
345    
346 astrand 76 case 'K':
347 jsorg71 450 g_grab_keyboard = False;
348 astrand 76 break;
349    
350 matthewc 520 case 'S':
351     if (!strcmp(optarg, "standard"))
352     {
353     g_win_button_size = 18;
354     break;
355     }
356    
357     g_win_button_size = strtol(optarg, &p, 10);
358    
359     if (*p)
360     {
361     error("invalid button size\n");
362     return 1;
363     }
364    
365     break;
366    
367 matthewc 223 case 'T':
368 jsorg71 450 STRNCPY(g_title, optarg, sizeof(g_title));
369 astrand 107 break;
370    
371 jsorg71 309 case 'a':
372 jsorg71 438 g_server_bpp = strtol(optarg, NULL, 10);
373     if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
374     && g_server_bpp != 24)
375 jsorg71 309 {
376     error("invalid server bpp\n");
377     return 1;
378     }
379     break;
380    
381 matthewc 520 case 'r':
382     if (!strcmp(optarg, "sound"))
383 stargo 501 #ifdef WITH_RDPSND
384 matthewc 520 g_rdpsnd = True;
385     #else
386     warning("Not compiled with sound support");
387     #endif
388 stargo 501 break;
389 matthewc 520
390 matthewc 482 case '0':
391     g_console_session = True;
392     break;
393    
394 astrand 458 case '4':
395     g_use_rdp5 = False;
396     break;
397    
398 forsberg 350 case '5':
399 jsorg71 438 g_use_rdp5 = True;
400 forsberg 350 break;
401 astrand 458
402 matty 28 case 'h':
403 matty 10 case '?':
404     default:
405     usage(argv[0]);
406     return 1;
407     }
408     }
409    
410     if (argc - optind < 1)
411     {
412     usage(argv[0]);
413     return 1;
414     }
415    
416 matthewc 222 STRNCPY(server, argv[optind], sizeof(server));
417 astrand 444 parse_server_and_port(server);
418 matty 10
419 astrand 289 if (!username_option)
420 matty 10 {
421     pw = getpwuid(getuid());
422     if ((pw == NULL) || (pw->pw_name == NULL))
423     {
424 matty 30 error("could not determine username, use -u\n");
425 matty 10 return 1;
426     }
427    
428 jsorg71 437 STRNCPY(g_username, pw->pw_name, sizeof(g_username));
429 matty 10 }
430    
431     if (hostname[0] == 0)
432     {
433 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
434 matty 10 {
435 matty 30 error("could not determine local hostname, use -n\n");
436 matty 10 return 1;
437     }
438 matty 30
439     p = strchr(fullhostname, '.');
440     if (p != NULL)
441     *p = 0;
442    
443     STRNCPY(hostname, fullhostname, sizeof(hostname));
444 matty 10 }
445    
446 matthewc 211 if (prompt_password && read_password(password, sizeof(password)))
447     flags |= RDP_LOGON_AUTO;
448 matty 30
449 jsorg71 450 if (g_title[0] == 0)
450 astrand 107 {
451 jsorg71 450 strcpy(g_title, "rdesktop - ");
452     strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
453 astrand 107 }
454 matty 12
455 astrand 333 #ifdef RDP2VNC
456     rdp2vnc_connect(server, flags, domain, password, shell, directory);
457 forsberg 424 return 0;
458 astrand 333 #else
459    
460 astrand 82 if (!ui_init())
461     return 1;
462 astrand 66
463 matthewc 474 #ifdef WITH_RDPSND
464 stargo 501 if (g_rdpsnd)
465     rdpsnd_init();
466 matthewc 474 #endif
467 matthewc 432 /* rdpdr_init(); */
468 forsberg 416
469 matthewc 53 if (!rdp_connect(server, flags, domain, password, shell, directory))
470     return 1;
471    
472 forsberg 427 /* By setting encryption to False here, we have an encrypted login
473     packet but unencrypted transfer of other packets */
474 astrand 435 if (!packet_encryption)
475 jsorg71 437 g_encryption = False;
476 forsberg 427
477    
478 matthewc 122 DEBUG(("Connection successful.\n"));
479 matthewc 211 memset(password, 0, sizeof(password));
480 matthewc 53
481 jsorg71 100 if (ui_create_window())
482 matty 10 {
483 forsberg 424 rdp_retval = rdp_main_loop();
484 matty 10 ui_destroy_window();
485     }
486    
487 matthewc 122 DEBUG(("Disconnecting...\n"));
488 matthewc 53 rdp_disconnect();
489 matthewc 188 ui_deinit();
490 astrand 333
491 astrand 435 if (True == rdp_retval)
492 forsberg 424 return 0;
493     else
494     return 2;
495    
496 astrand 333 #endif
497    
498 matty 10 }
499    
500 matthewc 220 #ifdef EGD_SOCKET
501     /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
502     static BOOL
503     generate_random_egd(uint8 * buf)
504     {
505     struct sockaddr_un addr;
506     BOOL ret = False;
507     int fd;
508    
509     fd = socket(AF_UNIX, SOCK_STREAM, 0);
510     if (fd == -1)
511     return False;
512    
513     addr.sun_family = AF_UNIX;
514     memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
515 astrand 259 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
516 matthewc 220 goto err;
517    
518     /* PRNGD and EGD use a simple communications protocol */
519 astrand 259 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
520     buf[1] = 32; /* Number of requested random bytes */
521 matthewc 220 if (write(fd, buf, 2) != 2)
522     goto err;
523    
524 astrand 259 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
525 matthewc 220 goto err;
526    
527     if (read(fd, buf, 32) != 32)
528     goto err;
529    
530     ret = True;
531    
532 astrand 259 err:
533 matthewc 220 close(fd);
534     return ret;
535     }
536     #endif
537    
538 matty 10 /* Generate a 32-byte random for the secure transport code. */
539 matty 25 void
540 astrand 64 generate_random(uint8 * random)
541 matty 10 {
542     struct stat st;
543 matty 22 struct tms tmsbuf;
544 matthewc 220 MD5_CTX md5;
545     uint32 *r;
546     int fd, n;
547 matty 10
548 matthewc 220 /* If we have a kernel random device, try that first */
549 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
550     || ((fd = open("/dev/random", O_RDONLY)) != -1))
551 matty 10 {
552 matthewc 220 n = read(fd, random, 32);
553 matty 10 close(fd);
554 matthewc 220 if (n == 32)
555     return;
556 matty 10 }
557    
558 matthewc 220 #ifdef EGD_SOCKET
559     /* As a second preference use an EGD */
560     if (generate_random_egd(random))
561     return;
562     #endif
563    
564 matty 10 /* Otherwise use whatever entropy we can gather - ideas welcome. */
565 astrand 259 r = (uint32 *) random;
566 matty 10 r[0] = (getpid()) | (getppid() << 16);
567     r[1] = (getuid()) | (getgid() << 16);
568 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
569     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
570 matty 10 stat("/tmp", &st);
571     r[5] = st.st_atime;
572     r[6] = st.st_mtime;
573     r[7] = st.st_ctime;
574 matthewc 220
575     /* Hash both halves with MD5 to obscure possible patterns */
576     MD5_Init(&md5);
577 astrand 259 MD5_Update(&md5, random, 16);
578 matthewc 220 MD5_Final(random, &md5);
579 astrand 259 MD5_Update(&md5, random + 16, 16);
580     MD5_Final(random + 16, &md5);
581 matty 10 }
582    
583     /* malloc; exit if out of memory */
584 matty 25 void *
585     xmalloc(int size)
586 matty 10 {
587     void *mem = malloc(size);
588     if (mem == NULL)
589     {
590 matty 30 error("xmalloc %d\n", size);
591 matty 10 exit(1);
592     }
593     return mem;
594     }
595    
596     /* realloc; exit if out of memory */
597 matty 25 void *
598     xrealloc(void *oldmem, int size)
599 matty 10 {
600     void *mem = realloc(oldmem, size);
601     if (mem == NULL)
602     {
603 matty 30 error("xrealloc %d\n", size);
604 matty 10 exit(1);
605     }
606     return mem;
607     }
608    
609     /* free */
610 matty 25 void
611     xfree(void *mem)
612 matty 10 {
613     free(mem);
614     }
615    
616 matty 30 /* report an error */
617 matty 25 void
618 matty 30 error(char *format, ...)
619     {
620     va_list ap;
621    
622     fprintf(stderr, "ERROR: ");
623    
624     va_start(ap, format);
625     vfprintf(stderr, format, ap);
626     va_end(ap);
627     }
628    
629 matthewc 297 /* report a warning */
630     void
631     warning(char *format, ...)
632     {
633     va_list ap;
634    
635     fprintf(stderr, "WARNING: ");
636    
637     va_start(ap, format);
638     vfprintf(stderr, format, ap);
639     va_end(ap);
640     }
641    
642 matty 30 /* report an unimplemented protocol feature */
643     void
644     unimpl(char *format, ...)
645     {
646     va_list ap;
647    
648     fprintf(stderr, "NOT IMPLEMENTED: ");
649    
650     va_start(ap, format);
651     vfprintf(stderr, format, ap);
652     va_end(ap);
653     }
654    
655     /* produce a hex dump */
656     void
657 jsorg71 376 hexdump(unsigned char *p, int len)
658 matty 10 {
659     unsigned char *line = p;
660 jsorg71 376 int i, thisline, offset = 0;
661 matty 10
662     while (offset < len)
663     {
664 matthewc 169 printf("%04x ", offset);
665 matty 10 thisline = len - offset;
666     if (thisline > 16)
667     thisline = 16;
668    
669     for (i = 0; i < thisline; i++)
670 matthewc 169 printf("%02x ", line[i]);
671 matty 10
672 matty 30 for (; i < 16; i++)
673 matthewc 169 printf(" ");
674 matty 30
675 matty 10 for (i = 0; i < thisline; i++)
676 matthewc 169 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
677 matty 10
678 matthewc 169 printf("\n");
679 matty 10 offset += thisline;
680     line += thisline;
681     }
682     }
683 astrand 325
684    
685     int
686     load_licence(unsigned char **data)
687     {
688 matthewc 367 char *home, *path;
689 astrand 325 struct stat st;
690 matthewc 367 int fd, length;
691 astrand 325
692     home = getenv("HOME");
693     if (home == NULL)
694     return -1;
695    
696 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
697 astrand 325 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
698    
699     fd = open(path, O_RDONLY);
700     if (fd == -1)
701     return -1;
702    
703     if (fstat(fd, &st))
704     return -1;
705    
706 forsberg 416 *data = (uint8 *) xmalloc(st.st_size);
707 matthewc 367 length = read(fd, *data, st.st_size);
708     close(fd);
709     xfree(path);
710     return length;
711 astrand 325 }
712    
713     void
714     save_licence(unsigned char *data, int length)
715     {
716 matthewc 367 char *home, *path, *tmppath;
717     int fd;
718 astrand 325
719     home = getenv("HOME");
720     if (home == NULL)
721     return;
722    
723 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
724 astrand 325
725 matthewc 367 sprintf(path, "%s/.rdesktop", home);
726     if ((mkdir(path, 0700) == -1) && errno != EEXIST)
727 astrand 325 {
728 matthewc 367 perror(path);
729     return;
730 astrand 325 }
731    
732 matthewc 367 /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
733 astrand 325
734 matthewc 367 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
735 forsberg 416 tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
736 matthewc 367 strcpy(tmppath, path);
737     strcat(tmppath, ".new");
738    
739 forsberg 416 fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
740 matthewc 367 if (fd == -1)
741 astrand 325 {
742 matthewc 367 perror(tmppath);
743     return;
744 astrand 325 }
745    
746 matthewc 367 if (write(fd, data, length) != length)
747 astrand 325 {
748 matthewc 367 perror(tmppath);
749     unlink(tmppath);
750 astrand 325 }
751 matthewc 367 else if (rename(tmppath, path) == -1)
752 astrand 325 {
753 matthewc 367 perror(path);
754     unlink(tmppath);
755 astrand 325 }
756    
757 matthewc 367 close(fd);
758     xfree(tmppath);
759     xfree(path);
760 astrand 325 }

  ViewVC Help
Powered by ViewVC 1.1.26