26 |
#include <sys/stat.h> /* stat */ |
#include <sys/stat.h> /* stat */ |
27 |
#include <sys/time.h> /* gettimeofday */ |
#include <sys/time.h> /* gettimeofday */ |
28 |
#include <sys/times.h> /* times */ |
#include <sys/times.h> /* times */ |
29 |
|
#include <errno.h> |
30 |
#include "rdesktop.h" |
#include "rdesktop.h" |
31 |
|
|
32 |
#ifdef EGD_SOCKET |
#ifdef EGD_SOCKET |
514 |
line += thisline; |
line += thisline; |
515 |
} |
} |
516 |
} |
} |
517 |
|
|
518 |
|
|
519 |
|
int |
520 |
|
load_licence(unsigned char **data) |
521 |
|
{ |
522 |
|
char *path; |
523 |
|
char *home; |
524 |
|
struct stat st; |
525 |
|
int fd; |
526 |
|
|
527 |
|
home = getenv("HOME"); |
528 |
|
if (home == NULL) |
529 |
|
return -1; |
530 |
|
|
531 |
|
path = xmalloc(strlen(home) + strlen(hostname) + 20); |
532 |
|
sprintf(path, "%s/.rdesktop/licence.%s", home, hostname); |
533 |
|
|
534 |
|
fd = open(path, O_RDONLY); |
535 |
|
if (fd == -1) |
536 |
|
return -1; |
537 |
|
|
538 |
|
if (fstat(fd, &st)) |
539 |
|
return -1; |
540 |
|
|
541 |
|
*data = xmalloc(st.st_size); |
542 |
|
return read(fd, *data, st.st_size); |
543 |
|
} |
544 |
|
|
545 |
|
void |
546 |
|
save_licence(unsigned char *data, int length) |
547 |
|
{ |
548 |
|
char *fpath; /* file path for licence */ |
549 |
|
char *fname, *fnamewrk; /* file name for licence .inkl path. */ |
550 |
|
char *home; |
551 |
|
uint32 y; |
552 |
|
struct flock fnfl; |
553 |
|
int fnfd, fnwrkfd, i, wlen; |
554 |
|
struct stream s, *s_ptr; |
555 |
|
uint32 len; |
556 |
|
|
557 |
|
/* Construct a stream, so that we can use macros to extract the |
558 |
|
* licence. |
559 |
|
*/ |
560 |
|
s_ptr = &s; |
561 |
|
s_ptr->p = data; |
562 |
|
/* Skip first two bytes */ |
563 |
|
in_uint16(s_ptr, len); |
564 |
|
|
565 |
|
/* Skip three strings */ |
566 |
|
for (i = 0; i < 3; i++) |
567 |
|
{ |
568 |
|
in_uint32(s_ptr, len); |
569 |
|
s_ptr->p += len; |
570 |
|
/* Make sure that we won't be past the end of data after |
571 |
|
* reading the next length value |
572 |
|
*/ |
573 |
|
if ((s_ptr->p) + 4 > data + length) |
574 |
|
{ |
575 |
|
printf("Error in parsing licence key.\n"); |
576 |
|
printf("Strings %d end value %x > supplied length (%x)\n", i, |
577 |
|
(unsigned int) s_ptr->p, (unsigned int) data + length); |
578 |
|
return; |
579 |
|
} |
580 |
|
} |
581 |
|
in_uint32(s_ptr, len); |
582 |
|
if (s_ptr->p + len > data + length) |
583 |
|
{ |
584 |
|
printf("Error in parsing licence key.\n"); |
585 |
|
printf("End of licence %x > supplied length (%x)\n", |
586 |
|
(unsigned int) s_ptr->p + len, (unsigned int) data + length); |
587 |
|
return; |
588 |
|
} |
589 |
|
|
590 |
|
home = getenv("HOME"); |
591 |
|
if (home == NULL) |
592 |
|
return; |
593 |
|
|
594 |
|
/* set and create the directory -- if it doesn't exist. */ |
595 |
|
fpath = xmalloc(strlen(home) + 11); |
596 |
|
STRNCPY(fpath, home, strlen(home) + 1); |
597 |
|
|
598 |
|
sprintf(fpath, "%s/.rdesktop", fpath); |
599 |
|
if (mkdir(fpath, 0700) == -1 && errno != EEXIST) |
600 |
|
{ |
601 |
|
perror("mkdir"); |
602 |
|
exit(1); |
603 |
|
} |
604 |
|
|
605 |
|
/* set the real licence filename, and put a write lock on it. */ |
606 |
|
fname = xmalloc(strlen(fpath) + strlen(hostname) + 10); |
607 |
|
sprintf(fname, "%s/licence.%s", fpath, hostname); |
608 |
|
fnfd = open(fname, O_RDONLY); |
609 |
|
if (fnfd != -1) |
610 |
|
{ |
611 |
|
fnfl.l_type = F_WRLCK; |
612 |
|
fnfl.l_whence = SEEK_SET; |
613 |
|
fnfl.l_start = 0; |
614 |
|
fnfl.l_len = 1; |
615 |
|
fcntl(fnfd, F_SETLK, &fnfl); |
616 |
|
} |
617 |
|
|
618 |
|
/* create a temporary licence file */ |
619 |
|
fnamewrk = xmalloc(strlen(fname) + 12); |
620 |
|
for (y = 0;; y++) |
621 |
|
{ |
622 |
|
sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int) y); |
623 |
|
fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600); |
624 |
|
if (fnwrkfd == -1) |
625 |
|
{ |
626 |
|
if (errno == EINTR || errno == EEXIST) |
627 |
|
continue; |
628 |
|
perror("create"); |
629 |
|
exit(1); |
630 |
|
} |
631 |
|
break; |
632 |
|
} |
633 |
|
/* write to the licence file */ |
634 |
|
for (y = 0; y < len;) |
635 |
|
{ |
636 |
|
do |
637 |
|
{ |
638 |
|
wlen = write(fnwrkfd, s_ptr->p + y, len - y); |
639 |
|
} |
640 |
|
while (wlen == -1 && errno == EINTR); |
641 |
|
if (wlen < 1) |
642 |
|
{ |
643 |
|
perror("write"); |
644 |
|
unlink(fnamewrk); |
645 |
|
exit(1); |
646 |
|
} |
647 |
|
y += wlen; |
648 |
|
} |
649 |
|
|
650 |
|
/* close the file and rename it to fname */ |
651 |
|
if (close(fnwrkfd) == -1) |
652 |
|
{ |
653 |
|
perror("close"); |
654 |
|
unlink(fnamewrk); |
655 |
|
exit(1); |
656 |
|
} |
657 |
|
if (rename(fnamewrk, fname) == -1) |
658 |
|
{ |
659 |
|
perror("rename"); |
660 |
|
unlink(fnamewrk); |
661 |
|
exit(1); |
662 |
|
} |
663 |
|
/* close the file lock on fname */ |
664 |
|
if (fnfd != -1) |
665 |
|
{ |
666 |
|
fnfl.l_type = F_UNLCK; |
667 |
|
fnfl.l_whence = SEEK_SET; |
668 |
|
fnfl.l_start = 0; |
669 |
|
fnfl.l_len = 1; |
670 |
|
fcntl(fnfd, F_SETLK, &fnfl); |
671 |
|
close(fnfd); |
672 |
|
} |
673 |
|
|
674 |
|
} |