summaryrefslogtreecommitdiffstats
path: root/src/userpref.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/userpref.c')
-rw-r--r--src/userpref.c58
1 files changed, 46 insertions, 12 deletions
diff --git a/src/userpref.c b/src/userpref.c
index 9b7a0a8..2f4e55b 100644
--- a/src/userpref.c
+++ b/src/userpref.c
@@ -193,6 +193,8 @@ static int config_write(const char *cfgfile, plist_t dict)
193 fprintf(fd, "\n[Global]\nHostID=%s\n", hostidstr); 193 fprintf(fd, "\n[Global]\nHostID=%s\n", hostidstr);
194 fclose(fd); 194 fclose(fd);
195 res = 0; 195 res = 0;
196 } else {
197 debug_info("could not open '%s' for writing: %s", cfgfile, strerror(errno));
196 } 198 }
197 free(hostidstr); 199 free(hostidstr);
198 } 200 }
@@ -234,6 +236,7 @@ static int config_read(const char *cfgfile, plist_t *dict)
234 int res = -1; 236 int res = -1;
235 FILE *fd = fopen(cfgfile, "rb"); 237 FILE *fd = fopen(cfgfile, "rb");
236 if (!fd) { 238 if (!fd) {
239 debug_info("could not open '%s' for reading: %s", cfgfile, strerror(errno));
237 return -1; 240 return -1;
238 } 241 }
239 242
@@ -565,8 +568,12 @@ userpref_error_t userpref_set_device_public_key(const char *uuid, key_data_t pub
565 568
566 /* store file */ 569 /* store file */
567 FILE *pFile = fopen(pem, "wb"); 570 FILE *pFile = fopen(pem, "wb");
568 fwrite(public_key.data, 1, public_key.size, pFile); 571 if (pFile) {
569 fclose(pFile); 572 fwrite(public_key.data, 1, public_key.size, pFile);
573 fclose(pFile);
574 } else {
575 debug_info("could not open '%s' for writing: %s", pem, strerror(errno));
576 }
570 free(pem); 577 free(pem);
571 578
572 return USERPREF_E_SUCCESS; 579 return USERPREF_E_SUCCESS;
@@ -678,6 +685,7 @@ static userpref_error_t userpref_gen_keys_and_cert(void)
678 key_data_t host_key_pem = { NULL, 0 }; 685 key_data_t host_key_pem = { NULL, 0 };
679 key_data_t host_cert_pem = { NULL, 0 }; 686 key_data_t host_cert_pem = { NULL, 0 };
680 687
688 debug_info("Generating keys and certificates");
681#ifdef HAVE_OPENSSL 689#ifdef HAVE_OPENSSL
682 RSA* root_keypair = RSA_generate_key(2048, 65537, NULL, NULL); 690 RSA* root_keypair = RSA_generate_key(2048, 65537, NULL, NULL);
683 RSA* host_keypair = RSA_generate_key(2048, 65537, NULL, NULL); 691 RSA* host_keypair = RSA_generate_key(2048, 65537, NULL, NULL);
@@ -1035,6 +1043,7 @@ userpref_error_t userpref_get_certs_as_pem(key_data_t *pem_root_cert, key_data_t
1035 pem_host_cert->size = 0; 1043 pem_host_cert->size = 0;
1036 } 1044 }
1037 } 1045 }
1046 debug_info("configuration invalid");
1038 return USERPREF_E_INVALID_CONF; 1047 return USERPREF_E_INVALID_CONF;
1039} 1048}
1040 1049
@@ -1055,9 +1064,14 @@ userpref_error_t userpref_set_keys_and_certs(key_data_t * root_key, key_data_t *
1055 FILE *pFile; 1064 FILE *pFile;
1056 char *pem; 1065 char *pem;
1057 const char *config_path; 1066 const char *config_path;
1067 userpref_error_t ret = USERPREF_E_SUCCESS;
1068
1069 debug_info("saving keys and certs");
1058 1070
1059 if (!root_key || !host_key || !root_cert || !host_cert) 1071 if (!root_key || !host_key || !root_cert || !host_cert) {
1072 debug_info("missing key or cert (root_key=%p, host_key=%p, root=cert=%p, host_cert=%p", root_key, host_key, root_cert, host_cert);
1060 return USERPREF_E_INVALID_ARG; 1073 return USERPREF_E_INVALID_ARG;
1074 }
1061 1075
1062 /* Make sure config directory exists */ 1076 /* Make sure config directory exists */
1063 userpref_create_config_dir(); 1077 userpref_create_config_dir();
@@ -1070,8 +1084,13 @@ userpref_error_t userpref_set_keys_and_certs(key_data_t * root_key, key_data_t *
1070 strcat(pem, DIR_SEP_S); 1084 strcat(pem, DIR_SEP_S);
1071 strcat(pem, LIBIMOBILEDEVICE_ROOT_PRIVKEY); 1085 strcat(pem, LIBIMOBILEDEVICE_ROOT_PRIVKEY);
1072 pFile = fopen(pem, "wb"); 1086 pFile = fopen(pem, "wb");
1073 fwrite(root_key->data, 1, root_key->size, pFile); 1087 if (pFile) {
1074 fclose(pFile); 1088 fwrite(root_key->data, 1, root_key->size, pFile);
1089 fclose(pFile);
1090 } else {
1091 debug_info("could not open '%s' for writing: %s", pem, strerror(errno));
1092 ret = USERPREF_E_WRITE_ERROR;
1093 }
1075 free(pem); 1094 free(pem);
1076 1095
1077 pem = (char*)malloc(strlen(config_path)+1+strlen(LIBIMOBILEDEVICE_HOST_PRIVKEY)+1); 1096 pem = (char*)malloc(strlen(config_path)+1+strlen(LIBIMOBILEDEVICE_HOST_PRIVKEY)+1);
@@ -1079,8 +1098,13 @@ userpref_error_t userpref_set_keys_and_certs(key_data_t * root_key, key_data_t *
1079 strcat(pem, DIR_SEP_S); 1098 strcat(pem, DIR_SEP_S);
1080 strcat(pem, LIBIMOBILEDEVICE_HOST_PRIVKEY); 1099 strcat(pem, LIBIMOBILEDEVICE_HOST_PRIVKEY);
1081 pFile = fopen(pem, "wb"); 1100 pFile = fopen(pem, "wb");
1082 fwrite(host_key->data, 1, host_key->size, pFile); 1101 if (pFile) {
1083 fclose(pFile); 1102 fwrite(host_key->data, 1, host_key->size, pFile);
1103 fclose(pFile);
1104 } else {
1105 debug_info("could not open '%s' for writing: %s", pem, strerror(errno));
1106 ret = USERPREF_E_WRITE_ERROR;
1107 }
1084 free(pem); 1108 free(pem);
1085 1109
1086 pem = (char*)malloc(strlen(config_path)+1+strlen(LIBIMOBILEDEVICE_ROOT_CERTIF)+1); 1110 pem = (char*)malloc(strlen(config_path)+1+strlen(LIBIMOBILEDEVICE_ROOT_CERTIF)+1);
@@ -1088,8 +1112,13 @@ userpref_error_t userpref_set_keys_and_certs(key_data_t * root_key, key_data_t *
1088 strcat(pem, DIR_SEP_S); 1112 strcat(pem, DIR_SEP_S);
1089 strcat(pem, LIBIMOBILEDEVICE_ROOT_CERTIF); 1113 strcat(pem, LIBIMOBILEDEVICE_ROOT_CERTIF);
1090 pFile = fopen(pem, "wb"); 1114 pFile = fopen(pem, "wb");
1091 fwrite(root_cert->data, 1, root_cert->size, pFile); 1115 if (pFile) {
1092 fclose(pFile); 1116 fwrite(root_cert->data, 1, root_cert->size, pFile);
1117 fclose(pFile);
1118 } else {
1119 debug_info("could not open '%s' for writing: %s", pem, strerror(errno));
1120 ret = USERPREF_E_WRITE_ERROR;
1121 }
1093 free(pem); 1122 free(pem);
1094 1123
1095 pem = (char*)malloc(strlen(config_path)+1+strlen(LIBIMOBILEDEVICE_HOST_CERTIF)+1); 1124 pem = (char*)malloc(strlen(config_path)+1+strlen(LIBIMOBILEDEVICE_HOST_CERTIF)+1);
@@ -1097,9 +1126,14 @@ userpref_error_t userpref_set_keys_and_certs(key_data_t * root_key, key_data_t *
1097 strcat(pem, DIR_SEP_S); 1126 strcat(pem, DIR_SEP_S);
1098 strcat(pem, LIBIMOBILEDEVICE_HOST_CERTIF); 1127 strcat(pem, LIBIMOBILEDEVICE_HOST_CERTIF);
1099 pFile = fopen(pem, "wb"); 1128 pFile = fopen(pem, "wb");
1100 fwrite(host_cert->data, 1, host_cert->size, pFile); 1129 if (pFile) {
1101 fclose(pFile); 1130 fwrite(host_cert->data, 1, host_cert->size, pFile);
1131 fclose(pFile);
1132 } else {
1133 debug_info("could not open '%s' for writing: %s", pem, strerror(errno));
1134 ret = USERPREF_E_WRITE_ERROR;
1135 }
1102 free(pem); 1136 free(pem);
1103 1137
1104 return USERPREF_E_SUCCESS; 1138 return ret;
1105} 1139}