NCNetworkingSync.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 *)createFolder:(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. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  155. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  156. [communication setUserAgent:[CCUtility getUserAgent]];
  157. [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl completion:^(NSString * _Nullable tokenDatabase) {
  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:tokenDatabase 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. }];
  197. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  198. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  199. return returnError;
  200. }
  201. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl
  202. {
  203. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  204. __block NSError *returnError = nil;
  205. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  206. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  207. [communication setUserAgent:[CCUtility getUserAgent]];
  208. [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl completion:^(NSString * _Nullable tokenDatabase) {
  209. // Read Folder
  210. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  211. if (items.count > 1) {
  212. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_directory_not_empty_", nil) forKey:NSLocalizedDescriptionKey]];
  213. dispatch_semaphore_signal(semaphore);
  214. return;
  215. }
  216. // LOCK
  217. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:tokenDatabase onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  218. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:token];
  219. // DELETE METADATA
  220. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  221. NSLog(@"[LOG] Found metadata and delete");
  222. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  223. NSLog(@"[LOG] %@", [NSString stringWithFormat:@"Remove metadata error %d", (int)response.statusCode]);
  224. }];
  225. // DELETE MARK
  226. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  227. // UNLOCK
  228. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  229. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:@""];
  230. dispatch_semaphore_signal(semaphore);
  231. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  232. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  233. dispatch_semaphore_signal(semaphore);
  234. }];
  235. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  236. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_mark_folder_"];
  237. dispatch_semaphore_signal(semaphore);
  238. }];
  239. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  240. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  241. dispatch_semaphore_signal(semaphore);
  242. }];
  243. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  244. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  245. dispatch_semaphore_signal(semaphore);
  246. }];
  247. }];
  248. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  249. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  250. return returnError;
  251. }
  252. - (NSError *)getEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString **)metadata
  253. {
  254. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  255. __block NSError *returnError = nil;
  256. __block NSString *returnMetadata = nil;
  257. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  258. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  259. [communication setUserAgent:[CCUtility getUserAgent]];
  260. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  261. returnMetadata = encryptedMetadata;
  262. dispatch_semaphore_signal(semaphore);
  263. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  264. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_get_metadata_"];
  265. dispatch_semaphore_signal(semaphore);
  266. }];
  267. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  268. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  269. *metadata = returnMetadata;
  270. return returnError;
  271. }
  272. - (NSError *)deleteEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  273. {
  274. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  275. __block NSError *returnError = nil;
  276. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  277. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  278. [communication setUserAgent:[CCUtility getUserAgent]];
  279. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  280. dispatch_semaphore_signal(semaphore);
  281. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  282. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_metadata_"];
  283. dispatch_semaphore_signal(semaphore);
  284. }];
  285. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  286. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  287. return returnError;
  288. }
  289. - (NSError *)storeEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url serverUrl:(NSString *)serverUrl fileID:(NSString *)fileID metadata:(NSString *)metadata
  290. {
  291. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  292. __block NSError *returnError = nil;
  293. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  294. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  295. [communication setUserAgent:[CCUtility getUserAgent]];
  296. [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl completion:^(NSString * _Nullable tokenDatabase) {
  297. // LOCK
  298. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:tokenDatabase onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  299. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:token];
  300. // STORE METADATA
  301. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  302. dispatch_semaphore_signal(semaphore);
  303. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  304. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_store_metadata_"];
  305. dispatch_semaphore_signal(semaphore);
  306. }];
  307. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  308. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  309. dispatch_semaphore_signal(semaphore);
  310. }];
  311. }];
  312. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  313. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  314. return returnError;
  315. }
  316. - (NSError *)updateEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url serverUrl:(NSString *)serverUrl fileID:(NSString *)fileID metadata:(NSString *)metadata
  317. {
  318. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  319. __block NSError *returnError = nil;
  320. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  321. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  322. [communication setUserAgent:[CCUtility getUserAgent]];
  323. [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl completion:^(NSString * _Nullable tokenDatabase) {
  324. // LOCK
  325. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:tokenDatabase onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  326. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:token];
  327. // UPDATA METADATA
  328. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  329. dispatch_semaphore_signal(semaphore);
  330. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  331. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_update_metadata_"];
  332. dispatch_semaphore_signal(semaphore);
  333. }];
  334. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  335. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  336. dispatch_semaphore_signal(semaphore);
  337. }];
  338. }];
  339. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  340. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  341. return returnError;
  342. }
  343. - (NSError *)lockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url serverUrl:(NSString *)serverUrl fileID:(NSString *)fileID
  344. {
  345. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  346. __block NSError *returnError = nil;
  347. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  348. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  349. [communication setUserAgent:[CCUtility getUserAgent]];
  350. [[NCManageDatabase sharedInstance] getDirectoryE2ETokenLockWithServerUrl:serverUrl completion:^(NSString * _Nullable tokenDatabase) {
  351. // LOCK
  352. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:tokenDatabase onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  353. // Write DB token
  354. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:token];
  355. dispatch_semaphore_signal(semaphore);
  356. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  357. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  358. dispatch_semaphore_signal(semaphore);
  359. }];
  360. }];
  361. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  362. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  363. return returnError;
  364. }
  365. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url serverUrl:(NSString *)serverUrl fileID:(NSString *)fileID token:(NSString *)token
  366. {
  367. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  368. __block NSError *returnError = nil;
  369. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  370. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  371. [communication setUserAgent:[CCUtility getUserAgent]];
  372. // UNLOCK
  373. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  374. // Write DB token ""
  375. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithServerUrl:serverUrl token:@""];
  376. dispatch_semaphore_signal(semaphore);
  377. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  378. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  379. dispatch_semaphore_signal(semaphore);
  380. }];
  381. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  382. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  383. return returnError;
  384. }
  385. - (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
  386. {
  387. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", account, serverUrl]];
  388. NSString *metadata;
  389. NSError *error;
  390. // Enabled E2E
  391. if ([CCUtility isEndToEndEnabled:account] == NO)
  392. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_not_enabled_", nil) forKey:NSLocalizedDescriptionKey]];
  393. // get Metadata for select updateEndToEndMetadata or storeEndToEndMetadata
  394. error = [[NCNetworkingSync sharedManager] getEndToEndMetadata:user userID:userID password:password url:url fileID:directory.fileID metadata:&metadata];
  395. if (error.code != 404 && error != nil) {
  396. return error;
  397. }
  398. // Rename
  399. if (fileName && fileNameNew)
  400. [[NCManageDatabase sharedInstance] renameFileE2eEncryptionWithServerUrl:serverUrl fileNameIdentifier:fileName newFileName:fileNameNew newFileNamePath:[CCUtility returnFileNamePathFromFileName:fileNameNew serverUrl:serverUrl activeUrl:url]];
  401. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", account, serverUrl]];
  402. if (!tableE2eEncryption)
  403. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_record_not_found_", nil) forKey:NSLocalizedDescriptionKey]];
  404. NSString *e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  405. if (!e2eMetadataJSON)
  406. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  407. // send Metadata
  408. if (error == nil)
  409. error = [[NCNetworkingSync sharedManager] updateEndToEndMetadata:user userID:userID password:password url:url serverUrl:serverUrl fileID:directory.fileID metadata:e2eMetadataJSON];
  410. else if (error.code == 404)
  411. error = [[NCNetworkingSync sharedManager] storeEndToEndMetadata:user userID:userID password:password url:url serverUrl:serverUrl fileID:directory.fileID metadata:e2eMetadataJSON];
  412. return error;
  413. }
  414. - (NSError *)rebuildAndSendEndToEndMetadataOnServerUrl:(NSString *)serverUrl account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  415. {
  416. NSError *error;
  417. NSString *e2eMetadataJSON;
  418. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", account, serverUrl]];
  419. if (directory.e2eEncrypted == NO)
  420. return nil;
  421. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", account, serverUrl]];
  422. if (tableE2eEncryption) {
  423. e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  424. if (!e2eMetadataJSON)
  425. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  426. error = [[NCNetworkingSync sharedManager] updateEndToEndMetadata:user userID:userID password:password url:url serverUrl:serverUrl fileID:directory.fileID metadata:e2eMetadataJSON];
  427. } else {
  428. [[NCNetworkingSync sharedManager] deleteEndToEndMetadata:user userID:userID password:password url:url fileID:directory.fileID];
  429. }
  430. return error;
  431. }
  432. - (NSError *)getError:(NSHTTPURLResponse *)response error:(NSError *)error descriptionDefault:(NSString *)descriptionDefault
  433. {
  434. NSInteger errorCode = response.statusCode;
  435. NSString *errorDescription = response.description;
  436. if (errorDescription == nil || errorCode == 0) {
  437. errorCode = error.code;
  438. errorDescription = error.description;
  439. if (errorDescription == nil) errorDescription = NSLocalizedString(descriptionDefault, @"");
  440. }
  441. if (errorDescription.length >= 200) {
  442. errorDescription = [errorDescription substringToIndex:200];
  443. errorDescription = [errorDescription stringByAppendingString:@" ..."];
  444. }
  445. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:errorCode userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:NSLocalizedDescriptionKey]];
  446. }
  447. @end