NCNetworkingSync.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. //
  2. // NCNetworkingSync.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/10/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. #import "NCNetworkingSync.h"
  9. #import "CCUtility.h"
  10. #import "CCCertificate.h"
  11. #import "NCBridgeSwift.h"
  12. @implementation NCNetworkingSync
  13. + (NCNetworkingSync *)sharedManager {
  14. static NCNetworkingSync *sharedManager;
  15. @synchronized(self)
  16. {
  17. if (!sharedManager) {
  18. sharedManager = [NCNetworkingSync new];
  19. }
  20. return sharedManager;
  21. }
  22. }
  23. #pragma --------------------------------------------------------------------------------------------
  24. #pragma mark ============================
  25. #pragma --------------------------------------------------------------------------------------------
  26. - (NSError *)uploadFile:(NSString *)localFilePathName remoteFilePathName:(NSString *)remoteFilePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  27. {
  28. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  29. __block NSError *returnError = nil;
  30. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  31. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  32. [communication setUserAgent:[CCUtility getUserAgent]];
  33. [communication uploadFileSession:localFilePathName toDestiny:remoteFilePathName onCommunication:communication progress:^(NSProgress *progress) {
  34. // Progress
  35. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  36. dispatch_semaphore_signal(semaphore);
  37. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  38. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  39. returnError = [self getError:httpResponse error:error descriptionDefault:@"_error_upload_file_"];
  40. dispatch_semaphore_signal(semaphore);
  41. } failureBeforeRequest:^(NSError *error) {
  42. returnError = error;
  43. dispatch_semaphore_signal(semaphore);
  44. }];
  45. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  46. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  47. return returnError;
  48. }
  49. - (NSError *)checkServer:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  50. {
  51. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  52. __block NSError *returnError = nil;
  53. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  54. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  55. [communication setUserAgent:[CCUtility getUserAgent]];
  56. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  57. dispatch_semaphore_signal(semaphore);
  58. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  59. returnError = [self getError:response error:error descriptionDefault:@"_error_check_server_"];
  60. dispatch_semaphore_signal(semaphore);
  61. }];
  62. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  63. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  64. return returnError;
  65. }
  66. - (NSError *)readFile:(NSString *)filePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password items:(NSArray **)items
  67. {
  68. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  69. __block NSError *returnError = nil;
  70. __block NSArray *returnItems = nil;
  71. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  72. [communication setCredentialsWithUser: user andUserID: userID andPassword: password];
  73. [communication setUserAgent:[CCUtility getUserAgent]];
  74. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  75. returnItems = items;
  76. dispatch_semaphore_signal(semaphore);
  77. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  78. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  79. dispatch_semaphore_signal(semaphore);
  80. }];
  81. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  82. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  83. *items = returnItems;
  84. return returnError;
  85. }
  86. - (NSError *)readFolder:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password items:(NSArray **)items
  87. {
  88. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  89. __block NSError *returnError = nil;
  90. __block NSArray *returnItems = nil;
  91. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  92. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  93. [communication setUserAgent:[CCUtility getUserAgent]];
  94. [communication readFolder:serverUrl depth:0 withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  95. returnItems = items;
  96. dispatch_semaphore_signal(semaphore);
  97. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  98. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  99. dispatch_semaphore_signal(semaphore);
  100. }];
  101. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  102. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  103. *items = returnItems;
  104. return returnError;
  105. }
  106. - (NSError *)createFolderAutomaticUpload:(NSString *)folderPathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url encrypted:(BOOL)encrypted fileID:(NSString **)fileID
  107. {
  108. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  109. __block NSError *returnError = nil;
  110. __block NSString *returnFileID = nil;
  111. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  112. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  113. [communication setUserAgent:[CCUtility getUserAgent]];
  114. [communication readFile:folderPathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  115. dispatch_semaphore_signal(semaphore);
  116. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  117. [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  118. NSDictionary *fields = [response allHeaderFields];
  119. returnFileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  120. if (encrypted) {
  121. // MARK
  122. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:returnFileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  123. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] directoryID:nil];
  124. dispatch_semaphore_signal(semaphore);
  125. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  126. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_mark_folder_"];
  127. dispatch_semaphore_signal(semaphore);
  128. }];
  129. } else {
  130. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] directoryID:nil];
  131. dispatch_semaphore_signal(semaphore);
  132. }
  133. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  134. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  135. dispatch_semaphore_signal(semaphore);
  136. } errorBeforeRequest:^(NSError *error) {
  137. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:response.description forKey:NSLocalizedDescriptionKey]];
  138. dispatch_semaphore_signal(semaphore);
  139. }];
  140. }];
  141. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  142. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  143. *fileID = returnFileID;
  144. return returnError;
  145. }
  146. #pragma --------------------------------------------------------------------------------------------
  147. #pragma mark ===== E2EE End-to-End Encryption =====
  148. #pragma --------------------------------------------------------------------------------------------
  149. // E2EE
  150. - (NSError *)markEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl
  151. {
  152. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  153. __block NSError *returnError = nil;
  154. __block NSString *token = [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl];
  155. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  156. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  157. [communication setUserAgent:[CCUtility getUserAgent]];
  158. // Read Folder
  159. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  160. if (items.count > 1) {
  161. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_directory_not_empty_", nil) forKey:NSLocalizedDescriptionKey]];
  162. dispatch_semaphore_signal(semaphore);
  163. return;
  164. }
  165. // LOCK
  166. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  167. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:token];
  168. // REMOVE METADATA
  169. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  170. NSLog(@"[LOG] Found metadata and delete");
  171. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  172. NSLog(@"[LOG] %@", [NSString stringWithFormat:@"Remove metadata error %d", (int)response.statusCode]);
  173. }];
  174. // MARK
  175. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  176. // UNLOCK
  177. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  178. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:@""];
  179. dispatch_semaphore_signal(semaphore);
  180. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  181. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  182. dispatch_semaphore_signal(semaphore);
  183. }];
  184. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  185. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_mark_folder_"];
  186. dispatch_semaphore_signal(semaphore);
  187. }];
  188. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  189. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  190. dispatch_semaphore_signal(semaphore);
  191. }];
  192. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  193. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  194. dispatch_semaphore_signal(semaphore);
  195. }];
  196. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  197. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  198. return returnError;
  199. }
  200. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl
  201. {
  202. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  203. __block NSError *returnError = nil;
  204. __block NSString *token = [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl];
  205. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  206. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  207. [communication setUserAgent:[CCUtility getUserAgent]];
  208. // Read Folder
  209. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  210. if (items.count > 1) {
  211. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_directory_not_empty_", nil) forKey:NSLocalizedDescriptionKey]];
  212. dispatch_semaphore_signal(semaphore);
  213. return;
  214. }
  215. // LOCK
  216. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  217. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:token];
  218. // DELETE METADATA
  219. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  220. NSLog(@"[LOG] Found metadata and delete");
  221. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  222. NSLog(@"[LOG] %@", [NSString stringWithFormat:@"Remove metadata error %d", (int)response.statusCode]);
  223. }];
  224. // DELETE MARK
  225. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  226. // UNLOCK
  227. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  228. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:@""];
  229. dispatch_semaphore_signal(semaphore);
  230. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  231. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  232. dispatch_semaphore_signal(semaphore);
  233. }];
  234. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  235. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_mark_folder_"];
  236. dispatch_semaphore_signal(semaphore);
  237. }];
  238. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  239. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  240. dispatch_semaphore_signal(semaphore);
  241. }];
  242. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  243. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  244. dispatch_semaphore_signal(semaphore);
  245. }];
  246. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  247. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  248. return returnError;
  249. }
  250. - (NSError *)getEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString **)metadata
  251. {
  252. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  253. __block NSError *returnError = nil;
  254. __block NSString *returnMetadata = nil;
  255. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  256. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  257. [communication setUserAgent:[CCUtility getUserAgent]];
  258. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  259. returnMetadata = encryptedMetadata;
  260. dispatch_semaphore_signal(semaphore);
  261. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  262. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_get_metadata_"];
  263. dispatch_semaphore_signal(semaphore);
  264. }];
  265. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  266. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  267. *metadata = returnMetadata;
  268. return returnError;
  269. }
  270. - (NSError *)deleteEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  271. {
  272. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  273. __block NSError *returnError = nil;
  274. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  275. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  276. [communication setUserAgent:[CCUtility getUserAgent]];
  277. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  278. dispatch_semaphore_signal(semaphore);
  279. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  280. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_metadata_"];
  281. dispatch_semaphore_signal(semaphore);
  282. }];
  283. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  284. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  285. return returnError;
  286. }
  287. - (NSError *)storeEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url serverUrl:(NSString *)serverUrl fileID:(NSString *)fileID metadata:(NSString *)metadata
  288. {
  289. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  290. __block NSError *returnError = nil;
  291. __block NSString *token = [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl];
  292. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  293. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  294. [communication setUserAgent:[CCUtility getUserAgent]];
  295. // LOCK
  296. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  297. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:token];
  298. // STORE METADATA
  299. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  300. dispatch_semaphore_signal(semaphore);
  301. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  302. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_store_metadata_"];
  303. dispatch_semaphore_signal(semaphore);
  304. }];
  305. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  306. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  307. dispatch_semaphore_signal(semaphore);
  308. }];
  309. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  310. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  311. return returnError;
  312. }
  313. - (NSError *)updateEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url serverUrl:(NSString *)serverUrl fileID:(NSString *)fileID metadata:(NSString *)metadata
  314. {
  315. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  316. __block NSError *returnError = nil;
  317. __block NSString *token = [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl];
  318. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  319. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  320. [communication setUserAgent:[CCUtility getUserAgent]];
  321. // LOCK
  322. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  323. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:token];
  324. // UPDATA METADATA
  325. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  326. dispatch_semaphore_signal(semaphore);
  327. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  328. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_update_metadata_"];
  329. dispatch_semaphore_signal(semaphore);
  330. }];
  331. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  332. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  333. dispatch_semaphore_signal(semaphore);
  334. }];
  335. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  336. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  337. return returnError;
  338. }
  339. - (NSError *)lockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url serverUrl:(NSString *)serverUrl fileID:(NSString *)fileID
  340. {
  341. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  342. __block NSError *returnError = nil;
  343. __block NSString *token = [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl];
  344. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  345. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  346. [communication setUserAgent:[CCUtility getUserAgent]];
  347. // LOCK
  348. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  349. // Write DB token
  350. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:token];
  351. dispatch_semaphore_signal(semaphore);
  352. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  353. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  354. dispatch_semaphore_signal(semaphore);
  355. }];
  356. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  357. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  358. return returnError;
  359. }
  360. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url serverUrl:(NSString *)serverUrl fileID:(NSString *)fileID token:(NSString *)token
  361. {
  362. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  363. __block NSError *returnError = nil;
  364. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  365. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  366. [communication setUserAgent:[CCUtility getUserAgent]];
  367. // UNLOCK
  368. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  369. // Write DB token ""
  370. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:@""];
  371. dispatch_semaphore_signal(semaphore);
  372. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  373. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  374. dispatch_semaphore_signal(semaphore);
  375. }];
  376. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  377. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  378. return returnError;
  379. }
  380. - (NSError *)sendEndToEndMetadataOnServerUrl:(NSString *)serverUrl account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileNameRename:(NSString *)fileName fileNameNewRename:(NSString *)fileNameNew
  381. {
  382. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", account, serverUrl]];
  383. NSString *metadata;
  384. NSError *error;
  385. // Enabled E2E
  386. if ([CCUtility isEndToEndEnabled:account] == NO)
  387. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_not_enabled_", nil) forKey:NSLocalizedDescriptionKey]];
  388. // get Metadata for select updateEndToEndMetadata or storeEndToEndMetadata
  389. error = [[NCNetworkingSync sharedManager] getEndToEndMetadata:user userID:userID password:password url:url fileID:directory.fileID metadata:&metadata];
  390. if (error.code != 404 && error != nil) {
  391. return error;
  392. }
  393. // Rename
  394. if (fileName && fileNameNew)
  395. [[NCManageDatabase sharedInstance] renameFileE2eEncryptionWithServerUrl:serverUrl fileNameIdentifier:fileName newFileName:fileNameNew newFileNamePath:[CCUtility returnFileNamePathFromFileName:fileNameNew serverUrl:serverUrl activeUrl:url]];
  396. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", account, serverUrl]];
  397. if (!tableE2eEncryption)
  398. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_record_not_found_", nil) forKey:NSLocalizedDescriptionKey]];
  399. NSString *e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  400. if (!e2eMetadataJSON)
  401. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  402. // send Metadata
  403. if (error == nil)
  404. error = [[NCNetworkingSync sharedManager] updateEndToEndMetadata:user userID:userID password:password url:url serverUrl:serverUrl fileID:directory.fileID metadata:e2eMetadataJSON];
  405. else if (error.code == 404)
  406. error = [[NCNetworkingSync sharedManager] storeEndToEndMetadata:user userID:userID password:password url:url serverUrl:serverUrl fileID:directory.fileID metadata:e2eMetadataJSON];
  407. return error;
  408. }
  409. - (NSError *)rebuildAndSendEndToEndMetadataOnServerUrl:(NSString *)serverUrl account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  410. {
  411. NSError *error;
  412. NSString *e2eMetadataJSON;
  413. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", account, serverUrl]];
  414. if (directory.e2eEncrypted == NO)
  415. return nil;
  416. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", account, serverUrl]];
  417. if (tableE2eEncryption) {
  418. e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  419. if (!e2eMetadataJSON)
  420. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  421. error = [[NCNetworkingSync sharedManager] updateEndToEndMetadata:user userID:userID password:password url:url serverUrl:serverUrl fileID:directory.fileID metadata:e2eMetadataJSON];
  422. } else {
  423. [[NCNetworkingSync sharedManager] deleteEndToEndMetadata:user userID:userID password:password url:url fileID:directory.fileID];
  424. }
  425. return error;
  426. }
  427. - (NSError *)getError:(NSHTTPURLResponse *)response error:(NSError *)error descriptionDefault:(NSString *)descriptionDefault
  428. {
  429. NSInteger errorCode = response.statusCode;
  430. NSString *errorDescription = response.description;
  431. if (errorDescription == nil || errorCode == 0) {
  432. errorCode = error.code;
  433. errorDescription = error.description;
  434. if (errorDescription == nil) errorDescription = NSLocalizedString(descriptionDefault, @"");
  435. }
  436. if (errorDescription.length >= 200) {
  437. errorDescription = [errorDescription substringToIndex:200];
  438. errorDescription = [errorDescription stringByAppendingString:@" ..."];
  439. }
  440. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:errorCode userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:NSLocalizedDescriptionKey]];
  441. }
  442. @end