NCNetworkingSync.m 34 KB

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