/[rdesktop]/sourceforge.net/trunk/rdesktop/licence.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/licence.c

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

revision 159 by matthewc, Sun Sep 15 12:36:55 2002 UTC revision 320 by astrand, Mon Feb 10 13:05:40 2003 UTC
# Line 1  Line 1 
1  /*  /*
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     RDP licensing negotiation     RDP licensing negotiation
4     Copyright (C) Matthew Chapman 1999-2001     Copyright (C) Matthew Chapman 1999-2002
5        
6     This program is free software; you can redistribute it and/or modify     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     it under the terms of the GNU General Public License as published by
# Line 18  Line 18 
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */  */
20    
21    #include <sys/types.h>
22    #include <sys/stat.h>
23    #include <unistd.h>
24    #include <fcntl.h>
25    #include <errno.h>
26  #include "rdesktop.h"  #include "rdesktop.h"
27    
28    #ifdef WITH_OPENSSL
29    #include <openssl/rc4.h>
30    #else
31  #include "crypto/rc4.h"  #include "crypto/rc4.h"
32    #endif
33    
34  extern char username[16];  extern char username[16];
35  extern char hostname[16];  extern char hostname[16];
 extern BOOL licence;  
36    
37  static uint8 licence_key[16];  static uint8 licence_key[16];
38  static uint8 licence_sign_key[16];  static uint8 licence_sign_key[16];
39    
40  BOOL licence_issued = False;  BOOL licence_issued = False;
41    
42    
43    static int
44    load_licence(unsigned char **data)
45    {
46            char *path;
47            char *home;
48            struct stat st;
49            int fd;
50    
51            home = getenv("HOME");
52            if (home == NULL)
53                    return -1;
54    
55            path = xmalloc(strlen(home) + strlen(hostname) + 20);
56            sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
57    
58            fd = open(path, O_RDONLY);
59            if (fd == -1)
60                    return -1;
61    
62            if (fstat(fd, &st))
63                    return -1;
64    
65            *data = xmalloc(st.st_size);
66            return read(fd, *data, st.st_size);
67    }
68    
69    static void
70    save_licence(unsigned char *data, int length)
71    {
72            char *fpath;            /* file path for licence */
73            char *fname, *fnamewrk; /* file name for licence .inkl path. */
74            char *home;
75            uint32 y;
76            struct flock fnfl;
77            int fnfd, fnwrkfd, i, wlen;
78            struct stream s, *s_ptr;
79            uint32 len;
80    
81            /* Construct a stream, so that we can use macros to extract the
82             * licence.
83             */
84            s_ptr = &s;
85            s_ptr->p = data;
86            /* Skip first two bytes */
87            in_uint16(s_ptr, len);
88    
89            /* Skip three strings */
90            for (i = 0; i < 3; i++)
91            {
92                    in_uint32(s_ptr, len);
93                    s_ptr->p += len;
94                    /* Make sure that we won't be past the end of data after
95                     * reading the next length value
96                     */
97                    if ((s_ptr->p) + 4 > data + length)
98                    {
99                            printf("Error in parsing licence key.\n");
100                            printf("Strings %d end value %x > supplied length (%x)\n", i,
101                                   (unsigned int) s_ptr->p, (unsigned int) data + length);
102                            return;
103                    }
104            }
105            in_uint32(s_ptr, len);
106            if (s_ptr->p + len > data + length)
107            {
108                    printf("Error in parsing licence key.\n");
109                    printf("End of licence %x > supplied length (%x)\n",
110                           (unsigned int) s_ptr->p + len, (unsigned int) data + length);
111                    return;
112            }
113    
114            home = getenv("HOME");
115            if (home == NULL)
116                    return;
117    
118            /* set and create the directory -- if it doesn't exist. */
119            fpath = xmalloc(strlen(home) + 11);
120            STRNCPY(fpath, home, strlen(home) + 1);
121    
122            sprintf(fpath, "%s/.rdesktop", fpath);
123            if (mkdir(fpath, 0700) == -1 && errno != EEXIST)
124            {
125                    perror("mkdir");
126                    exit(1);
127            }
128    
129            /* set the real licence filename, and put a write lock on it. */
130            fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);
131            sprintf(fname, "%s/licence.%s", fpath, hostname);
132            fnfd = open(fname, O_RDONLY);
133            if (fnfd != -1)
134            {
135                    fnfl.l_type = F_WRLCK;
136                    fnfl.l_whence = SEEK_SET;
137                    fnfl.l_start = 0;
138                    fnfl.l_len = 1;
139                    fcntl(fnfd, F_SETLK, &fnfl);
140            }
141    
142            /* create a temporary licence file */
143            fnamewrk = xmalloc(strlen(fname) + 12);
144            for (y = 0;; y++)
145            {
146                    sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int) y);
147                    fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600);
148                    if (fnwrkfd == -1)
149                    {
150                            if (errno == EINTR || errno == EEXIST)
151                                    continue;
152                            perror("create");
153                            exit(1);
154                    }
155                    break;
156            }
157            /* write to the licence file */
158            for (y = 0; y < len;)
159            {
160                    do
161                    {
162                            wlen = write(fnwrkfd, s_ptr->p + y, len - y);
163                    }
164                    while (wlen == -1 && errno == EINTR);
165                    if (wlen < 1)
166                    {
167                            perror("write");
168                            unlink(fnamewrk);
169                            exit(1);
170                    }
171                    y += wlen;
172            }
173    
174            /* close the file and rename it to fname */
175            if (close(fnwrkfd) == -1)
176            {
177                    perror("close");
178                    unlink(fnamewrk);
179                    exit(1);
180            }
181            if (rename(fnamewrk, fname) == -1)
182            {
183                    perror("rename");
184                    unlink(fnamewrk);
185                    exit(1);
186            }
187            /* close the file lock on fname */
188            if (fnfd != -1)
189            {
190                    fnfl.l_type = F_UNLCK;
191                    fnfl.l_whence = SEEK_SET;
192                    fnfl.l_start = 0;
193                    fnfl.l_len = 1;
194                    fcntl(fnfd, F_SETLK, &fnfl);
195                    close(fnfd);
196            }
197    
198    }
199    
200  /* Generate a session key and RC4 keys, given client and server randoms */  /* Generate a session key and RC4 keys, given client and server randoms */
201  static void  static void
202  licence_generate_keys(uint8 * client_key, uint8 * server_key, uint8 * client_rsa)  licence_generate_keys(uint8 * client_key, uint8 * server_key, uint8 * client_rsa)
# Line 55  licence_generate_hwid(uint8 * hwid) Line 222  licence_generate_hwid(uint8 * hwid)
222          strncpy((char *) (hwid + 4), hostname, LICENCE_HWID_SIZE - 4);          strncpy((char *) (hwid + 4), hostname, LICENCE_HWID_SIZE - 4);
223  }  }
224    
 #ifdef SAVE_LICENCE  
225  /* Present an existing licence to the server */  /* Present an existing licence to the server */
226  static void  static void
227  licence_present(uint8 * client_random, uint8 * rsa_data,  licence_present(uint8 * client_random, uint8 * rsa_data,
# Line 95  licence_present(uint8 * client_random, u Line 261  licence_present(uint8 * client_random, u
261          s_mark_end(s);          s_mark_end(s);
262          sec_send(s, sec_flags);          sec_send(s, sec_flags);
263  }  }
 #endif  
264    
265  /* Send a licence request packet */  /* Send a licence request packet */
266  static void  static void
# Line 139  static void Line 304  static void
304  licence_process_demand(STREAM s)  licence_process_demand(STREAM s)
305  {  {
306          uint8 null_data[SEC_MODULUS_SIZE];          uint8 null_data[SEC_MODULUS_SIZE];
         uint8 hwid[LICENCE_HWID_SIZE];  
         uint8 signature[LICENCE_SIGNATURE_SIZE];  
307          uint8 *server_random;          uint8 *server_random;
308            uint8 signature[LICENCE_SIGNATURE_SIZE];
309            uint8 hwid[LICENCE_HWID_SIZE];
310          uint8 *licence_data;          uint8 *licence_data;
311          int licence_size;          int licence_size;
312          RC4_KEY crypt_key;          RC4_KEY crypt_key;
# Line 154  licence_process_demand(STREAM s) Line 319  licence_process_demand(STREAM s)
319          memset(null_data, 0, sizeof(null_data));          memset(null_data, 0, sizeof(null_data));
320          licence_generate_keys(null_data, server_random, null_data);          licence_generate_keys(null_data, server_random, null_data);
321    
 #ifdef SAVE_LICENCE  
322          licence_size = load_licence(&licence_data);          licence_size = load_licence(&licence_data);
323          if (licence_size != -1)          if (licence_size != -1)
324          {          {
# Line 170  licence_process_demand(STREAM s) Line 334  licence_process_demand(STREAM s)
334                  xfree(licence_data);                  xfree(licence_data);
335                  return;                  return;
336          }          }
 #endif  
337    
338          licence_send_request(null_data, null_data, username, hostname);          licence_send_request(null_data, null_data, username, hostname);
339  }  }
# Line 248  licence_process_authreq(STREAM s) Line 411  licence_process_authreq(STREAM s)
411          memcpy(sealed_buffer + LICENCE_TOKEN_SIZE, hwid, LICENCE_HWID_SIZE);          memcpy(sealed_buffer + LICENCE_TOKEN_SIZE, hwid, LICENCE_HWID_SIZE);
412          sec_sign(out_sig, 16, licence_sign_key, 16, sealed_buffer, sizeof(sealed_buffer));          sec_sign(out_sig, 16, licence_sign_key, 16, sealed_buffer, sizeof(sealed_buffer));
413    
         /* Deliberately break signature if licencing disabled */  
         if (!licence)  
                 memset(out_sig, 0, sizeof(out_sig));  
   
414          /* Now encrypt the HWID */          /* Now encrypt the HWID */
415          RC4_set_key(&crypt_key, 16, licence_key);          RC4_set_key(&crypt_key, 16, licence_key);
416          RC4(&crypt_key, LICENCE_HWID_SIZE, hwid, crypt_hwid);          RC4(&crypt_key, LICENCE_HWID_SIZE, hwid, crypt_hwid);
# Line 280  licence_process_issue(STREAM s) Line 439  licence_process_issue(STREAM s)
439                  return;                  return;
440    
441          licence_issued = True;          licence_issued = True;
   
 #ifdef SAVE_LICENCE  
442          save_licence(s->p, length - 2);          save_licence(s->p, length - 2);
 #endif  
443  }  }
444    
445  /* Process a licence packet */  /* Process a licence packet */

Legend:
Removed from v.159  
changed lines
  Added in v.320

  ViewVC Help
Powered by ViewVC 1.1.26