/[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

Diff of /sourceforge.net/trunk/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 365 by matthewc, Wed Apr 16 08:19:15 2003 UTC revision 367 by matthewc, Wed Apr 16 13:04:15 2003 UTC
# Line 581  hexdump(unsigned char *p, unsigned int l Line 581  hexdump(unsigned char *p, unsigned int l
581  int  int
582  load_licence(unsigned char **data)  load_licence(unsigned char **data)
583  {  {
584          char *path;          char *home, *path;
         char *home;  
585          struct stat st;          struct stat st;
586          int fd;          int fd, length;
587    
588          home = getenv("HOME");          home = getenv("HOME");
589          if (home == NULL)          if (home == NULL)
590                  return -1;                  return -1;
591    
592          path = xmalloc(strlen(home) + strlen(hostname) + 20);          path = xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
593          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
594    
595          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
# Line 601  load_licence(unsigned char **data) Line 600  load_licence(unsigned char **data)
600                  return -1;                  return -1;
601    
602          *data = xmalloc(st.st_size);          *data = xmalloc(st.st_size);
603          return read(fd, *data, st.st_size);          length = read(fd, *data, st.st_size);
604            close(fd);
605            xfree(path);
606            return length;
607  }  }
608    
609  void  void
610  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
611  {  {
612          char *fpath;            /* file path for licence */          char *home, *path, *tmppath;
613          char *fname, *fnamewrk; /* file name for licence .inkl path. */          int fd;
         char *home;  
         uint32 y;  
         struct flock fnfl;  
         int fnfd, fnwrkfd, i, wlen;  
         struct stream s, *s_ptr;  
         uint32 len;  
   
         /* Construct a stream, so that we can use macros to extract the  
          * licence.  
          */  
         s_ptr = &s;  
         s_ptr->p = data;  
         /* Skip first two bytes */  
         in_uint16_le(s_ptr, len);  
   
         /* Skip three strings */  
         for (i = 0; i < 3; i++)  
         {  
                 in_uint32_le(s_ptr, len);  
                 s_ptr->p += len;  
                 /* Make sure that we won't be past the end of data after  
                  * reading the next length value  
                  */  
                 if ((s_ptr->p) + 4 > data + length)  
                 {  
                         printf("Error in parsing licence key.\n");  
                         printf("Strings %d end value %x > supplied length (%x)\n", i,  
                                (unsigned int) s_ptr->p, (unsigned int) data + length);  
                         return;  
                 }  
         }  
         in_uint32_le(s_ptr, len);  
         if (s_ptr->p + len > data + length)  
         {  
                 printf("Error in parsing licence key.\n");  
                 printf("End of licence %x > supplied length (%x)\n",  
                        (unsigned int) s_ptr->p + len, (unsigned int) data + length);  
                 return;  
         }  
614    
615          home = getenv("HOME");          home = getenv("HOME");
616          if (home == NULL)          if (home == NULL)
617                  return;                  return;
618    
619          /* set and create the directory -- if it doesn't exist. */          path = xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
         fpath = xmalloc(strlen(home) + 11);  
         STRNCPY(fpath, home, strlen(home) + 1);  
620    
621          sprintf(fpath, "%s/.rdesktop", fpath);          sprintf(path, "%s/.rdesktop", home);
622          if (mkdir(fpath, 0700) == -1 && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
623          {          {
624                  perror("mkdir");                  perror(path);
625                  exit(1);                  return;
626          }          }
627    
628          /* set the real licence filename, and put a write lock on it. */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
         fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);  
         sprintf(fname, "%s/licence.%s", fpath, hostname);  
         fnfd = open(fname, O_RDONLY);  
         if (fnfd != -1)  
         {  
                 fnfl.l_type = F_WRLCK;  
                 fnfl.l_whence = SEEK_SET;  
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
         }  
629    
630          /* create a temporary licence file */          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
631          fnamewrk = xmalloc(strlen(fname) + 12);          tmppath = xmalloc(strlen(path) + sizeof(".new"));
632          for (y = 0;; y++)          strcpy(tmppath, path);
633          {          strcat(tmppath, ".new");
                 sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int) y);  
                 fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600);  
                 if (fnwrkfd == -1)  
                 {  
                         if (errno == EINTR || errno == EEXIST)  
                                 continue;  
                         perror("create");  
                         exit(1);  
                 }  
                 break;  
         }  
         /* write to the licence file */  
         for (y = 0; y < len;)  
         {  
                 do  
                 {  
                         wlen = write(fnwrkfd, s_ptr->p + y, len - y);  
                 }  
                 while (wlen == -1 && errno == EINTR);  
                 if (wlen < 1)  
                 {  
                         perror("write");  
                         unlink(fnamewrk);  
                         exit(1);  
                 }  
                 y += wlen;  
         }  
634    
635          /* close the file and rename it to fname */          fd = open(tmppath, O_WRONLY|O_CREAT|O_TRUNC, 0600);
636          if (close(fnwrkfd) == -1)          if (fd == -1)
637          {          {
638                  perror("close");                  perror(tmppath);
639                  unlink(fnamewrk);                  return;
                 exit(1);  
640          }          }
641          if (rename(fnamewrk, fname) == -1)  
642            if (write(fd, data, length) != length)
643          {          {
644                  perror("rename");                  perror(tmppath);
645                  unlink(fnamewrk);                  unlink(tmppath);
                 exit(1);  
646          }          }
647          /* close the file lock on fname */          else if (rename(tmppath, path) == -1)
         if (fnfd != -1)  
648          {          {
649                  fnfl.l_type = F_UNLCK;                  perror(path);
650                  fnfl.l_whence = SEEK_SET;                  unlink(tmppath);
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
                 close(fnfd);  
651          }          }
652    
653            close(fd);
654            xfree(tmppath);
655            xfree(path);
656  }  }

Legend:
Removed from v.365  
changed lines
  Added in v.367

  ViewVC Help
Powered by ViewVC 1.1.26