NCNetworkingSync.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. @implementation NCNetworkingSync
  12. + (NCNetworkingSync *)sharedManager {
  13. static NCNetworkingSync *sharedManager;
  14. @synchronized(self)
  15. {
  16. if (!sharedManager) {
  17. sharedManager = [NCNetworkingSync new];
  18. }
  19. return sharedManager;
  20. }
  21. }
  22. #pragma --------------------------------------------------------------------------------------------
  23. #pragma mark ============================
  24. #pragma --------------------------------------------------------------------------------------------
  25. - (NSError *)uploadFile:(NSString *)localFilePathName remoteFilePathName:(NSString *)remoteFilePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  26. {
  27. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  28. __block NSError *returnError = nil;
  29. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  30. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  31. [communication setUserAgent:[CCUtility getUserAgent]];
  32. [communication uploadFileSession:localFilePathName toDestiny:remoteFilePathName onCommunication:communication progress:^(NSProgress *progress) {
  33. // Progress
  34. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  35. dispatch_semaphore_signal(semaphore);
  36. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  37. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  38. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:httpResponse.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Upload file error" forKey:NSLocalizedDescriptionKey]];
  39. dispatch_semaphore_signal(semaphore);
  40. } failureBeforeRequest:^(NSError *error) {
  41. returnError = error;
  42. dispatch_semaphore_signal(semaphore);
  43. }];
  44. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  45. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  46. return returnError;
  47. }
  48. - (NSError *)checkServer:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  49. {
  50. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  51. __block NSError *returnError = nil;
  52. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  53. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  54. [communication setUserAgent:[CCUtility getUserAgent]];
  55. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  56. dispatch_semaphore_signal(semaphore);
  57. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  58. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Check server error" forKey:NSLocalizedDescriptionKey]];
  59. dispatch_semaphore_signal(semaphore);
  60. }];
  61. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  62. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  63. return returnError;
  64. }
  65. - (NSError *)readFile:(NSString *)filePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  66. {
  67. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  68. __block NSError *returnError = nil;
  69. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  70. [communication setCredentialsWithUser: user andUserID: userID andPassword: password];
  71. [communication setUserAgent:[CCUtility getUserAgent]];
  72. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  73. dispatch_semaphore_signal(semaphore);
  74. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  75. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Read file error" forKey:NSLocalizedDescriptionKey]];
  76. dispatch_semaphore_signal(semaphore);
  77. }];
  78. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  79. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  80. return returnError;
  81. }
  82. #pragma --------------------------------------------------------------------------------------------
  83. #pragma mark ===== End-to-End Encryption =====
  84. #pragma --------------------------------------------------------------------------------------------
  85. - (NSError *)markEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  86. {
  87. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  88. __block NSError *returnError = nil;
  89. __block NSString *returnToken = nil;
  90. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  91. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  92. [communication setUserAgent:[CCUtility getUserAgent]];
  93. // LOCK
  94. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  95. returnToken = token;
  96. // MARK
  97. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  98. // UNLOCK
  99. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  100. returnToken = nil;
  101. dispatch_semaphore_signal(semaphore);
  102. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  103. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  104. dispatch_semaphore_signal(semaphore);
  105. }];
  106. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  107. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  108. dispatch_semaphore_signal(semaphore);
  109. }];
  110. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  111. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  112. dispatch_semaphore_signal(semaphore);
  113. }];
  114. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  115. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  116. *token = returnToken;
  117. return returnError;
  118. }
  119. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  120. {
  121. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  122. __block NSError *returnError = nil;
  123. __block NSString *returnToken = nil;
  124. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  125. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  126. [communication setUserAgent:[CCUtility getUserAgent]];
  127. // LOCK
  128. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  129. returnToken = token;
  130. // REMOVE METADATA
  131. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  132. // DELETE MARK
  133. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  134. // UNLOCK
  135. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  136. returnToken = nil;
  137. dispatch_semaphore_signal(semaphore);
  138. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  139. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  140. dispatch_semaphore_signal(semaphore);
  141. }];
  142. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  143. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  144. dispatch_semaphore_signal(semaphore);
  145. }];
  146. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  147. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  148. dispatch_semaphore_signal(semaphore);
  149. }];
  150. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  151. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  152. dispatch_semaphore_signal(semaphore);
  153. }];
  154. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  155. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  156. *token = returnToken;
  157. return returnError;
  158. }
  159. - (NSError *)getEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString **)metadata
  160. {
  161. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  162. __block NSError *returnError = nil;
  163. __block NSString *returnMetadata = nil;
  164. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  165. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  166. [communication setUserAgent:[CCUtility getUserAgent]];
  167. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  168. returnMetadata = encryptedMetadata;
  169. dispatch_semaphore_signal(semaphore);
  170. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  171. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  172. dispatch_semaphore_signal(semaphore);
  173. }];
  174. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  175. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  176. *metadata = returnMetadata;
  177. return returnError;
  178. }
  179. - (NSError *)storeEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  180. {
  181. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  182. __block NSError *returnError = nil;
  183. __block NSString *returnToken = nil;
  184. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  185. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  186. [communication setUserAgent:[CCUtility getUserAgent]];
  187. // LOCK
  188. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  189. returnToken = token;
  190. // STORE METADATA
  191. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  192. // UNLOCK
  193. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  194. returnToken = nil;
  195. dispatch_semaphore_signal(semaphore);
  196. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  197. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  198. dispatch_semaphore_signal(semaphore);
  199. }];
  200. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  201. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  202. dispatch_semaphore_signal(semaphore);
  203. }];
  204. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  205. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  206. dispatch_semaphore_signal(semaphore);
  207. }];
  208. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  209. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  210. *token = returnToken;
  211. return returnError;
  212. }
  213. - (NSError *)updateEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  214. {
  215. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  216. __block NSError *returnError = nil;
  217. __block NSString *returnToken = nil;
  218. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  219. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  220. [communication setUserAgent:[CCUtility getUserAgent]];
  221. // LOCK
  222. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  223. returnToken = token;
  224. // UPDATA METADATA
  225. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  226. // UNLOCK
  227. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  228. returnToken = nil;
  229. dispatch_semaphore_signal(semaphore);
  230. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  231. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  232. dispatch_semaphore_signal(semaphore);
  233. }];
  234. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  235. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  236. dispatch_semaphore_signal(semaphore);
  237. }];
  238. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  239. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  240. dispatch_semaphore_signal(semaphore);
  241. }];
  242. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  243. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  244. *token = returnToken;
  245. return returnError;
  246. }
  247. - (NSError *)lockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  248. {
  249. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  250. __block NSError *returnError = nil;
  251. __block NSString *returnToken = nil;
  252. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  253. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  254. [communication setUserAgent:[CCUtility getUserAgent]];
  255. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  256. returnToken = token;
  257. dispatch_semaphore_signal(semaphore);
  258. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  259. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  260. dispatch_semaphore_signal(semaphore);
  261. }];
  262. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  263. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  264. *token = returnToken;
  265. return returnError;
  266. }
  267. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString *)token
  268. {
  269. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  270. __block NSError *returnError= nil;
  271. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  272. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  273. [communication setUserAgent:[CCUtility getUserAgent]];
  274. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  275. dispatch_semaphore_signal(semaphore);
  276. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  277. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  278. dispatch_semaphore_signal(semaphore);
  279. }];
  280. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  281. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  282. return returnError;
  283. }
  284. @end