diff options
-rw-r--r-- | main.c | 45 |
1 files changed, 22 insertions, 23 deletions
@@ -33,7 +33,7 @@ long int udp_start_port = 0; | |||
33 | long int udp_end_port = 0; | 33 | long int udp_end_port = 0; |
34 | 34 | ||
35 | /* Directory with config and tox save */ | 35 | /* Directory with config and tox save */ |
36 | char *config_path = "/etc/tuntox/"; | 36 | char *config_path = "/etc/tuntox"; |
37 | 37 | ||
38 | /* Limit hostname and port in server */ | 38 | /* Limit hostname and port in server */ |
39 | int tunnel_target_whitelist_size = 0; | 39 | int tunnel_target_whitelist_size = 0; |
@@ -705,38 +705,42 @@ int send_tunnel_request_packet(char *remote_host, int remote_port, int friend_nu | |||
705 | 705 | ||
706 | /* End proto */ | 706 | /* End proto */ |
707 | 707 | ||
708 | void path_append(char *target, char *prefix, char *suffix) | ||
709 | { | ||
710 | size_t prefix_len = strlen(prefix); | ||
711 | size_t suffix_len = strlen(suffix); | ||
712 | if (prefix_len + suffix_len + 1 > PATH_MAX) return; | ||
713 | memcpy(target, prefix, prefix_len); | ||
714 | memcpy(target + prefix_len, suffix, suffix_len); | ||
715 | *(target + prefix_len + suffix_len) = 0; | ||
716 | } | ||
717 | |||
708 | /* Save tox identity to a file */ | 718 | /* Save tox identity to a file */ |
709 | static void write_save(Tox *tox) | 719 | static void write_save(Tox *tox) |
710 | { | 720 | { |
711 | void *data; | 721 | void *data; |
712 | uint32_t size; | 722 | uint32_t size; |
713 | uint8_t path_tmp[512], path_real[512], *p; | 723 | char path_tmp[PATH_MAX], path_real[PATH_MAX]; |
714 | FILE *file; | 724 | FILE *file; |
715 | 725 | ||
716 | size = tox_get_savedata_size(tox); | 726 | size = tox_get_savedata_size(tox); |
717 | data = malloc(size); | 727 | data = malloc(size); |
718 | tox_get_savedata(tox, data); | 728 | tox_get_savedata(tox, data); |
719 | 729 | ||
720 | strncpy((char *)path_real, config_path, sizeof(path_real)); | 730 | path_append(path_real, config_path, "/tox_save"); |
721 | 731 | path_append(path_tmp, path_real, ".tmp"); | |
722 | p = path_real + strlen((char *)path_real); | ||
723 | memcpy(p, "tox_save", sizeof("tox_save")); | ||
724 | 732 | ||
725 | unsigned int path_len = (p - path_real) + sizeof("tox_save"); | 733 | file = fopen(path_tmp, "wb"); |
726 | memcpy(path_tmp, path_real, path_len); | 734 | if (file) { |
727 | memcpy(path_tmp + (path_len - 1), ".tmp", sizeof(".tmp")); | ||
728 | |||
729 | file = fopen((char*)path_tmp, "wb"); | ||
730 | if(file) { | ||
731 | fwrite(data, size, 1, file); | 735 | fwrite(data, size, 1, file); |
732 | fflush(file); | 736 | fflush(file); |
733 | fclose(file); | 737 | fclose(file); |
734 | if (rename((char*)path_tmp, (char*)path_real) != 0) { | 738 | if (rename(path_tmp, path_real) != 0) { |
735 | log_printf(L_WARNING, "Failed to rename file. %s to %s deleting and trying again\n", path_tmp, path_real); | 739 | log_printf(L_WARNING, "Failed to rename file. %s to %s deleting and trying again\n", path_tmp, path_real); |
736 | if(remove((const char *)path_real) < 0) { | 740 | if (remove(path_real) < 0) { |
737 | log_printf(L_WARNING, "Failed to remove old save file %s\n", path_real); | 741 | log_printf(L_WARNING, "Failed to remove old save file %s\n", path_real); |
738 | } | 742 | } |
739 | if (rename((char*)path_tmp, (char*)path_real) != 0) { | 743 | if (rename(path_tmp, path_real) != 0) { |
740 | log_printf(L_WARNING, "Saving Failed\n"); | 744 | log_printf(L_WARNING, "Saving Failed\n"); |
741 | } else { | 745 | } else { |
742 | log_printf(L_DEBUG, "Saved data\n"); | 746 | log_printf(L_DEBUG, "Saved data\n"); |
@@ -758,15 +762,10 @@ static size_t load_save(uint8_t **out_data) | |||
758 | { | 762 | { |
759 | void *data; | 763 | void *data; |
760 | uint32_t size; | 764 | uint32_t size; |
761 | uint8_t path_real[PATH_MAX], *p; | 765 | char f[PATH_MAX]; |
762 | |||
763 | strncpy((char *)path_real, config_path, sizeof(path_real)); | ||
764 | |||
765 | p = path_real + strlen((char *)path_real); | ||
766 | char basename[] = "/tox_save"; | ||
767 | memcpy(p, "/tox_save", sizeof(basename)); | ||
768 | 766 | ||
769 | data = file_raw((char *)path_real, &size); | 767 | path_append(f, config_path, "/tox_save"); |
768 | data = file_raw(f, &size); | ||
770 | 769 | ||
771 | if(data) | 770 | if(data) |
772 | { | 771 | { |