void save_licence(unsigned char *data, int length) { uint8 ho[20], hi[16]; char *home, path[PATH_MAX], tmppath[PATH_MAX], hash[41]; int fd; home = getenv("HOME"); if (home == NULL) return; snprintf(path, PATH_MAX, "%s" RDESKTOP_LICENSE_STORE, home); path[sizeof(path) - 1] = '\0'; if (utils_mkdir_p(path, 0700) == -1) { perror(path); return; } memset(hi, 0, sizeof(hi)); snprintf((char *) hi, 16, "%s", g_hostname); sec_hash_sha1_16(ho, hi, g_static_rdesktop_salt_16); sec_hash_to_string(hash, sizeof(hash), ho, sizeof(ho)); /* write licence to {sha1}.cal.new, then atomically rename to {sha1}.cal */ snprintf(path, PATH_MAX, "%s" RDESKTOP_LICENSE_STORE "/%s.cal", home, hash); path[sizeof(path) - 1] = '\0'; snprintf(tmppath, PATH_MAX, "%s.new", path); path[sizeof(path) - 1] = '\0'; fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600); if (fd == -1) { perror(tmppath); return; } if (write(fd, data, length) != length) { perror(tmppath); unlink(tmppath); } else if (rename(tmppath, path) == -1) { perror(path); unlink(tmppath); } close(fd); }
int load_licence(unsigned char **data) { uint8 ho[20], hi[16]; char *home, path[PATH_MAX], hash[41]; struct stat st; int fd, length; home = getenv("HOME"); if (home == NULL) return -1; memset(hi, 0, sizeof(hi)); snprintf((char *) hi, 16, "%s", g_hostname); sec_hash_sha1_16(ho, hi, g_static_rdesktop_salt_16); sec_hash_to_string(hash, sizeof(hash), ho, sizeof(ho)); snprintf(path, PATH_MAX, "%s" RDESKTOP_LICENSE_STORE "/%s.cal", home, hash); path[sizeof(path) - 1] = '\0'; fd = open(path, O_RDONLY); if (fd == -1) { /* fallback to try reading old license file */ snprintf(path, PATH_MAX, "%s/.rdesktop/license.%s", home, g_hostname); path[sizeof(path) - 1] = '\0'; if ((fd = open(path, O_RDONLY)) == -1) return -1; } if (fstat(fd, &st)) { close(fd); return -1; } *data = (uint8 *) xmalloc(st.st_size); length = read(fd, *data, st.st_size); close(fd); return length; }
static void _ctrl_create_hash(const char *user, const char *domain, const char *host, char *hash, size_t hsize) { RDSSL_SHA1 sha1; uint8 out[20], delim; uint16 version; uint32 flags; /* version\0user\0domain\0host\0flags */ flags = 0; delim = '\0'; version = 0x0100; if (g_seamless_rdp) flags = CTRL_HASH_FLAG_SEAMLESS; rdssl_sha1_init(&sha1); rdssl_sha1_update(&sha1, (uint8 *) & version, sizeof(version)); rdssl_sha1_update(&sha1, &delim, 1); if (user) rdssl_sha1_update(&sha1, (uint8 *) user, strlen(user)); rdssl_sha1_update(&sha1, &delim, 1); if (domain) rdssl_sha1_update(&sha1, (uint8 *) domain, strlen(domain)); rdssl_sha1_update(&sha1, &delim, 1); if (host) rdssl_sha1_update(&sha1, (uint8 *) host, strlen(host)); rdssl_sha1_update(&sha1, &delim, 1); rdssl_sha1_update(&sha1, (uint8 *) & flags, sizeof(flags)); rdssl_sha1_final(&sha1, out); sec_hash_to_string(hash, hsize, out, sizeof(out)); }