2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2012-05-07 20:18:01 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2012-05-07 20:18:01 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2012-05-07 20:18:01 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
2012-05-07 20:18:01 +00:00
|
|
|
|
|
|
|
#import "MPAppDelegate_Store.h"
|
2017-09-20 16:43:03 +00:00
|
|
|
#import "mpw-marshal.h"
|
2017-08-13 01:57:47 +00:00
|
|
|
#import "mpw-util.h"
|
2012-05-07 20:18:01 +00:00
|
|
|
|
2013-04-07 23:05:51 +00:00
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
#define STORE_OPTIONS NSPersistentStoreFileProtectionKey : NSFileProtectionComplete,
|
|
|
|
#else
|
|
|
|
#define STORE_OPTIONS
|
|
|
|
#endif
|
|
|
|
|
2014-10-30 01:55:18 +00:00
|
|
|
#define MPMigrationLevelLocalStoreKey @"MPMigrationLevelLocalStoreKey"
|
2013-05-10 15:13:55 +00:00
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
typedef NS_ENUM( NSInteger, MPStoreMigrationLevel ) {
|
|
|
|
MPStoreMigrationLevelV1,
|
|
|
|
MPStoreMigrationLevelV2,
|
|
|
|
MPStoreMigrationLevelV3,
|
|
|
|
MPStoreMigrationLevelCurrent = MPStoreMigrationLevelV3,
|
2013-05-10 15:13:55 +00:00
|
|
|
};
|
2013-05-02 15:19:34 +00:00
|
|
|
|
2013-04-20 18:11:19 +00:00
|
|
|
@implementation MPAppDelegate_Shared(Store)
|
2012-05-07 20:18:01 +00:00
|
|
|
|
2014-10-28 04:53:16 +00:00
|
|
|
PearlAssociatedObjectProperty( NSOperationQueue *, StoreQueue, storeQueue );
|
2014-09-15 05:00:23 +00:00
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
PearlAssociatedObjectProperty( NSManagedObjectContext*, PrivateManagedObjectContext, privateManagedObjectContext );
|
|
|
|
|
|
|
|
PearlAssociatedObjectProperty( NSManagedObjectContext*, MainManagedObjectContext, mainManagedObjectContext );
|
2013-01-17 05:37:20 +00:00
|
|
|
|
2014-09-27 20:30:17 +00:00
|
|
|
PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
|
|
|
|
|
2012-05-09 17:34:00 +00:00
|
|
|
#pragma mark - Core Data setup
|
2012-05-09 08:11:34 +00:00
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
+ (NSManagedObjectContext *)managedObjectContextForMainThreadIfReady {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
NSAssert( [[NSThread currentThread] isMainThread], @"Can only access main MOC from the main thread." );
|
2013-01-31 05:42:32 +00:00
|
|
|
NSManagedObjectContext *mainManagedObjectContext = [[self get] mainManagedObjectContextIfReady];
|
2013-06-16 16:39:52 +00:00
|
|
|
if (!mainManagedObjectContext || ![[NSThread currentThread] isMainThread])
|
2013-04-24 00:38:56 +00:00
|
|
|
return nil;
|
2013-01-31 05:42:32 +00:00
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
return mainManagedObjectContext;
|
2013-01-31 05:42:32 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
+ (BOOL)managedObjectContextForMainThreadPerformBlock:(void ( ^ )(NSManagedObjectContext *mainContext))mocBlock {
|
2014-03-20 11:15:37 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
NSManagedObjectContext *mainManagedObjectContext = [[self get] mainManagedObjectContextIfReady];
|
2014-03-20 11:15:37 +00:00
|
|
|
if (!mainManagedObjectContext)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
[mainManagedObjectContext performBlock:^{
|
2014-10-23 01:17:02 +00:00
|
|
|
@try {
|
|
|
|
mocBlock( mainManagedObjectContext );
|
2014-10-28 04:53:16 +00:00
|
|
|
}
|
2015-11-01 04:19:47 +00:00
|
|
|
@catch (id exception) {
|
2014-10-23 01:17:02 +00:00
|
|
|
err( @"While performing managed block:\n%@", [exception fullDescription] );
|
|
|
|
}
|
2014-03-20 11:15:37 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
+ (BOOL)managedObjectContextForMainThreadPerformBlockAndWait:(void ( ^ )(NSManagedObjectContext *mainContext))mocBlock {
|
2014-03-20 11:15:37 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
NSManagedObjectContext *mainManagedObjectContext = [[self get] mainManagedObjectContextIfReady];
|
2014-03-20 11:15:37 +00:00
|
|
|
if (!mainManagedObjectContext)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
[mainManagedObjectContext performBlockAndWait:^{
|
2014-10-23 01:17:02 +00:00
|
|
|
@try {
|
|
|
|
mocBlock( mainManagedObjectContext );
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
err( @"While performing managed block:\n%@", [exception fullDescription] );
|
|
|
|
}
|
2014-03-20 11:15:37 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
+ (BOOL)managedObjectContextPerformBlock:(void ( ^ )(NSManagedObjectContext *context))mocBlock {
|
2013-01-31 05:42:32 +00:00
|
|
|
|
2014-04-15 04:26:13 +00:00
|
|
|
NSManagedObjectContext *privateManagedObjectContextIfReady = [[self get] privateManagedObjectContextIfReady];
|
|
|
|
if (!privateManagedObjectContextIfReady)
|
2013-01-31 05:42:32 +00:00
|
|
|
return NO;
|
|
|
|
|
|
|
|
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
|
2014-04-15 04:26:13 +00:00
|
|
|
moc.parentContext = privateManagedObjectContextIfReady;
|
2013-01-31 05:42:32 +00:00
|
|
|
[moc performBlock:^{
|
2014-10-23 01:17:02 +00:00
|
|
|
@try {
|
|
|
|
mocBlock( moc );
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
err( @"While performing managed block:\n%@", [exception fullDescription] );
|
|
|
|
}
|
2013-01-31 05:42:32 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
+ (BOOL)managedObjectContextPerformBlockAndWait:(void ( ^ )(NSManagedObjectContext *context))mocBlock {
|
2013-02-08 23:11:24 +00:00
|
|
|
|
2014-04-15 04:26:13 +00:00
|
|
|
NSManagedObjectContext *privateManagedObjectContextIfReady = [[self get] privateManagedObjectContextIfReady];
|
|
|
|
if (!privateManagedObjectContextIfReady)
|
2013-02-08 23:11:24 +00:00
|
|
|
return NO;
|
|
|
|
|
|
|
|
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
|
2014-04-15 04:26:13 +00:00
|
|
|
moc.parentContext = privateManagedObjectContextIfReady;
|
2013-02-08 23:11:24 +00:00
|
|
|
[moc performBlockAndWait:^{
|
2014-10-23 01:17:02 +00:00
|
|
|
@try {
|
|
|
|
mocBlock( moc );
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
err( @"While performing managed block:\n%@", [exception fullDescription] );
|
|
|
|
}
|
2013-02-08 23:11:24 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2018-02-11 04:29:55 +00:00
|
|
|
- (id)managedObjectContextChanged:(void ( ^ )(NSDictionary<NSManagedObjectID *, NSString *> *affectedObjects))changedBlock {
|
2017-04-29 19:01:24 +00:00
|
|
|
|
2018-02-11 04:29:55 +00:00
|
|
|
NSManagedObjectContext *privateManagedObjectContextIfReady = [self privateManagedObjectContextIfReady];
|
2017-04-29 19:01:24 +00:00
|
|
|
if (!privateManagedObjectContextIfReady)
|
|
|
|
return nil;
|
|
|
|
return PearlAddNotificationObserver( NSManagedObjectContextObjectsDidChangeNotification, privateManagedObjectContextIfReady, nil,
|
|
|
|
^(id host, NSNotification *note) {
|
|
|
|
NSMutableDictionary *affectedObjects = [NSMutableDictionary new];
|
|
|
|
for (NSManagedObject *object in note.userInfo[NSInsertedObjectsKey])
|
|
|
|
affectedObjects[object.objectID] = NSInsertedObjectsKey;
|
|
|
|
for (NSManagedObject *object in note.userInfo[NSUpdatedObjectsKey])
|
|
|
|
affectedObjects[object.objectID] = NSUpdatedObjectsKey;
|
|
|
|
for (NSManagedObject *object in note.userInfo[NSDeletedObjectsKey])
|
|
|
|
affectedObjects[object.objectID] = NSDeletedObjectsKey;
|
|
|
|
changedBlock( affectedObjects );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2013-01-31 05:42:32 +00:00
|
|
|
- (NSManagedObjectContext *)mainManagedObjectContextIfReady {
|
2013-04-03 23:25:15 +00:00
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
[self loadStore];
|
2013-04-05 04:31:05 +00:00
|
|
|
return self.mainManagedObjectContext;
|
2012-05-07 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 05:42:32 +00:00
|
|
|
- (NSManagedObjectContext *)privateManagedObjectContextIfReady {
|
2012-08-04 08:16:58 +00:00
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
[self loadStore];
|
2013-04-05 04:31:05 +00:00
|
|
|
return self.privateManagedObjectContext;
|
2012-05-07 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (NSURL *)localStoreURL {
|
|
|
|
|
|
|
|
NSURL *applicationSupportURL = [[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory
|
|
|
|
inDomains:NSUserDomainMask] lastObject];
|
|
|
|
return [[[applicationSupportURL
|
|
|
|
URLByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier isDirectory:YES]
|
2014-10-22 23:52:18 +00:00
|
|
|
URLByAppendingPathComponent:@"MasterPassword" isDirectory:NO]
|
2014-09-15 05:00:23 +00:00
|
|
|
URLByAppendingPathExtension:@"sqlite"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadStore {
|
|
|
|
|
2014-10-28 04:53:16 +00:00
|
|
|
static dispatch_once_t once = 0;
|
|
|
|
dispatch_once( &once, ^{
|
|
|
|
(self.storeQueue = [NSOperationQueue new]).maxConcurrentOperationCount = 1;
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Do nothing if already fully set up, otherwise (re-)load the store.
|
|
|
|
if (self.storeCoordinator && self.mainManagedObjectContext && self.privateManagedObjectContext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
[self.storeQueue addOperationWithBlock:^{
|
2014-09-15 05:00:23 +00:00
|
|
|
// Do nothing if already fully set up, otherwise (re-)load the store.
|
2014-10-28 04:53:16 +00:00
|
|
|
if (self.storeCoordinator && self.mainManagedObjectContext && self.privateManagedObjectContext)
|
2014-09-15 05:00:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Unregister any existing observers and contexts.
|
2014-10-28 04:53:16 +00:00
|
|
|
PearlRemoveNotificationObserversFrom( self.mainManagedObjectContext );
|
2014-09-15 05:00:23 +00:00
|
|
|
[self.mainManagedObjectContext performBlockAndWait:^{
|
|
|
|
[self.mainManagedObjectContext reset];
|
|
|
|
self.mainManagedObjectContext = nil;
|
|
|
|
}];
|
|
|
|
[self.privateManagedObjectContext performBlockAndWait:^{
|
|
|
|
[self.privateManagedObjectContext reset];
|
|
|
|
self.privateManagedObjectContext = nil;
|
|
|
|
}];
|
|
|
|
|
2014-09-27 20:30:17 +00:00
|
|
|
// Don't load when the store is corrupted.
|
|
|
|
if ([self.storeCorrupted boolValue])
|
|
|
|
return;
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
// Check if migration is necessary.
|
|
|
|
[self migrateStore];
|
|
|
|
|
2014-10-28 04:53:16 +00:00
|
|
|
// Install managed object contexts and observers.
|
|
|
|
self.privateManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
|
|
|
|
[self.privateManagedObjectContext performBlockAndWait:^{
|
|
|
|
self.privateManagedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
|
|
|
|
self.privateManagedObjectContext.persistentStoreCoordinator = self.storeCoordinator;
|
|
|
|
}];
|
|
|
|
|
|
|
|
self.mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
|
|
|
|
self.mainManagedObjectContext.parentContext = self.privateManagedObjectContext;
|
2018-02-11 04:43:42 +00:00
|
|
|
if (@available(iOS 10.0, macOS 10.12, *))
|
2017-04-29 19:01:24 +00:00
|
|
|
self.mainManagedObjectContext.automaticallyMergesChangesFromParent = YES;
|
|
|
|
else
|
|
|
|
// When privateManagedObjectContext is saved, import the changes into mainManagedObjectContext.
|
|
|
|
PearlAddNotificationObserverTo( self.mainManagedObjectContext, NSManagedObjectContextDidSaveNotification,
|
|
|
|
self.privateManagedObjectContext, nil, ^(NSManagedObjectContext *mainContext, NSNotification *note) {
|
|
|
|
[mainContext performBlock:^{
|
|
|
|
@try {
|
|
|
|
[mainContext mergeChangesFromContextDidSaveNotification:note];
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
err( @"While merging changes:\n%@", [exception fullDescription] );
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
} );
|
2014-10-28 04:53:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Create a new store coordinator.
|
2014-09-15 05:00:23 +00:00
|
|
|
NSError *error = nil;
|
2014-09-16 11:53:31 +00:00
|
|
|
NSURL *localStoreURL = [self localStoreURL];
|
|
|
|
if (![[NSFileManager defaultManager] createDirectoryAtURL:[localStoreURL URLByDeletingLastPathComponent]
|
|
|
|
withIntermediateDirectories:YES attributes:nil error:&error]) {
|
2017-04-30 03:03:50 +00:00
|
|
|
MPError( error, @"Couldn't create our application support directory." );
|
2014-09-16 11:53:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-28 04:53:16 +00:00
|
|
|
if (![self.storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self localStoreURL]
|
|
|
|
options:@{
|
2017-04-01 04:30:25 +00:00
|
|
|
NSMigratePersistentStoresAutomaticallyOption: @YES,
|
|
|
|
NSInferMappingModelAutomaticallyOption : @YES,
|
2014-10-28 04:53:16 +00:00
|
|
|
STORE_OPTIONS
|
|
|
|
} error:&error]) {
|
2017-04-30 03:03:50 +00:00
|
|
|
MPError( error, @"Failed to open store." );
|
2014-09-27 20:30:17 +00:00
|
|
|
self.storeCorrupted = @YES;
|
2014-09-22 03:11:05 +00:00
|
|
|
[self handleCoordinatorError:error];
|
|
|
|
return;
|
|
|
|
}
|
2014-09-27 20:30:17 +00:00
|
|
|
self.storeCorrupted = @NO;
|
2014-09-15 05:00:23 +00:00
|
|
|
|
2013-04-02 02:03:28 +00:00
|
|
|
#if TARGET_OS_IPHONE
|
2015-09-23 04:31:33 +00:00
|
|
|
PearlAddNotificationObserver( UIApplicationWillResignActiveNotification, UIApp, [NSOperationQueue mainQueue],
|
2017-04-30 03:03:50 +00:00
|
|
|
^(MPAppDelegate_Shared *self, NSNotification *note) {
|
|
|
|
[self.mainManagedObjectContext saveToStore];
|
|
|
|
} );
|
2013-04-02 02:03:28 +00:00
|
|
|
#else
|
2015-09-23 04:31:33 +00:00
|
|
|
PearlAddNotificationObserver( NSApplicationWillResignActiveNotification, NSApp, [NSOperationQueue mainQueue],
|
|
|
|
^(MPAppDelegate_Shared *self, NSNotification *note) {
|
2017-04-30 03:03:50 +00:00
|
|
|
[self.mainManagedObjectContext saveToStore];
|
|
|
|
} );
|
|
|
|
#endif
|
2013-04-02 02:03:28 +00:00
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
// Perform a data sanity check on the newly loaded store to find and fix any issues.
|
|
|
|
if ([[MPConfig get].checkInconsistency boolValue])
|
|
|
|
[MPAppDelegate_Shared managedObjectContextPerformBlockAndWait:^(NSManagedObjectContext *context) {
|
|
|
|
[self findAndFixInconsistenciesSaveInContext:context];
|
|
|
|
}];
|
2014-10-28 04:53:16 +00:00
|
|
|
}];
|
2013-04-02 02:03:28 +00:00
|
|
|
}
|
2012-05-07 20:18:01 +00:00
|
|
|
|
2014-09-22 03:11:05 +00:00
|
|
|
- (void)deleteAndResetStore {
|
|
|
|
|
|
|
|
@synchronized (self) {
|
|
|
|
// Unregister any existing observers and contexts.
|
2014-10-28 04:53:16 +00:00
|
|
|
PearlRemoveNotificationObserversFrom( self.mainManagedObjectContext );
|
2014-09-22 03:11:05 +00:00
|
|
|
[self.mainManagedObjectContext performBlockAndWait:^{
|
|
|
|
[self.mainManagedObjectContext reset];
|
|
|
|
self.mainManagedObjectContext = nil;
|
|
|
|
}];
|
|
|
|
[self.privateManagedObjectContext performBlockAndWait:^{
|
|
|
|
[self.privateManagedObjectContext reset];
|
|
|
|
self.privateManagedObjectContext = nil;
|
|
|
|
}];
|
|
|
|
NSError *error = nil;
|
2014-10-28 04:53:16 +00:00
|
|
|
for (NSPersistentStore *store in self.storeCoordinator.persistentStores) {
|
|
|
|
if (![self.storeCoordinator removePersistentStore:store error:&error])
|
2017-04-30 03:03:50 +00:00
|
|
|
MPError( error, @"Couldn't remove persistence store from coordinator." );
|
2014-09-22 03:11:05 +00:00
|
|
|
}
|
|
|
|
if (![[NSFileManager defaultManager] removeItemAtURL:self.localStoreURL error:&error])
|
2017-04-30 03:03:50 +00:00
|
|
|
MPError( error, @"Couldn't remove persistence store at URL %@.", self.localStoreURL );
|
2014-09-22 03:11:05 +00:00
|
|
|
|
|
|
|
[self loadStore];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
- (MPFixableResult)findAndFixInconsistenciesSaveInContext:(NSManagedObjectContext *)context {
|
|
|
|
|
|
|
|
NSError *error = nil;
|
|
|
|
NSFetchRequest *fetchRequest = [NSFetchRequest new];
|
|
|
|
fetchRequest.fetchBatchSize = 50;
|
|
|
|
|
|
|
|
MPFixableResult result = MPFixableResultNoProblems;
|
|
|
|
for (NSEntityDescription *entity in [context.persistentStoreCoordinator.managedObjectModel entities])
|
|
|
|
if (class_conformsToProtocol( NSClassFromString( entity.managedObjectClassName ), @protocol(MPFixable) )) {
|
|
|
|
fetchRequest.entity = entity;
|
|
|
|
NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];
|
|
|
|
if (!objects) {
|
2017-04-30 03:03:50 +00:00
|
|
|
MPError( error, @"Failed to fetch %@ objects.", entity );
|
2014-04-26 18:03:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (NSManagedObject<MPFixable> *object in objects)
|
|
|
|
result = MPApplyFix( result, ^MPFixableResult {
|
|
|
|
return [object findAndFixInconsistenciesInContext:context];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == MPFixableResultNoProblems)
|
|
|
|
inf( @"Sanity check found no problems in store." );
|
|
|
|
|
|
|
|
else {
|
|
|
|
[context saveToStore];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:MPFoundInconsistenciesNotification object:nil userInfo:@{
|
2017-04-01 04:30:25 +00:00
|
|
|
MPInconsistenciesFixResultUserKey: @(result)
|
2014-04-26 18:03:44 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (void)migrateStore {
|
2013-04-07 23:05:51 +00:00
|
|
|
|
2017-04-30 03:03:50 +00:00
|
|
|
MPStoreMigrationLevel migrationLevel = (MPStoreMigrationLevel)
|
|
|
|
[[NSUserDefaults standardUserDefaults] integerForKey:MPMigrationLevelLocalStoreKey];
|
2014-09-15 05:00:23 +00:00
|
|
|
if (migrationLevel >= MPStoreMigrationLevelCurrent)
|
2014-04-26 18:03:44 +00:00
|
|
|
// Local store up-to-date.
|
2013-05-10 15:13:55 +00:00
|
|
|
return;
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
inf( @"Local store migration level: %d (current %d)", (signed)migrationLevel, (signed)MPStoreMigrationLevelCurrent );
|
2014-10-30 04:18:01 +00:00
|
|
|
if (migrationLevel <= MPStoreMigrationLevelV1 && ![self migrateV1LocalStore]) {
|
2014-04-26 18:03:44 +00:00
|
|
|
inf( @"Failed to migrate old V1 to new local store." );
|
2013-09-21 14:34:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-30 04:18:01 +00:00
|
|
|
if (migrationLevel <= MPStoreMigrationLevelV2 && ![self migrateV2LocalStore]) {
|
2014-09-15 05:00:23 +00:00
|
|
|
inf( @"Failed to migrate old V2 to new local store." );
|
2013-05-02 15:19:34 +00:00
|
|
|
return;
|
2013-05-10 15:13:55 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 01:55:18 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] setInteger:MPStoreMigrationLevelCurrent forKey:MPMigrationLevelLocalStoreKey];
|
2014-09-15 05:00:23 +00:00
|
|
|
inf( @"Successfully migrated old to new local store." );
|
2014-10-24 04:35:05 +00:00
|
|
|
if (![[NSUserDefaults standardUserDefaults] synchronize])
|
|
|
|
wrn( @"Couldn't synchronize after store migration." );
|
2013-05-10 15:13:55 +00:00
|
|
|
}
|
|
|
|
|
2013-08-31 16:29:56 +00:00
|
|
|
- (BOOL)migrateV1LocalStore {
|
2013-05-10 15:13:55 +00:00
|
|
|
|
2013-04-07 23:05:51 +00:00
|
|
|
NSURL *applicationFilesDirectory = [[[NSFileManager defaultManager]
|
|
|
|
URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
|
|
|
|
NSURL *oldLocalStoreURL = [[applicationFilesDirectory
|
|
|
|
URLByAppendingPathComponent:@"MasterPassword" isDirectory:NO] URLByAppendingPathExtension:@"sqlite"];
|
2014-06-25 18:14:56 +00:00
|
|
|
if (![[NSFileManager defaultManager] fileExistsAtPath:oldLocalStoreURL.path isDirectory:NULL]) {
|
2014-04-26 18:03:44 +00:00
|
|
|
inf( @"No V1 local store to migrate." );
|
2013-08-31 16:29:56 +00:00
|
|
|
return YES;
|
2013-04-07 23:05:51 +00:00
|
|
|
}
|
2013-05-10 15:13:55 +00:00
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
inf( @"Migrating V1 local store" );
|
2014-09-15 05:00:23 +00:00
|
|
|
NSURL *newLocalStoreURL = [self localStoreURL];
|
2014-10-30 04:18:01 +00:00
|
|
|
if (![[NSFileManager defaultManager] fileExistsAtPath:newLocalStoreURL.path isDirectory:NULL]) {
|
|
|
|
inf( @"New local store already exists." );
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
NSError *error = nil;
|
2014-10-22 23:52:18 +00:00
|
|
|
if (![NSPersistentStore migrateStore:oldLocalStoreURL withOptions:@{ STORE_OPTIONS }
|
|
|
|
toStore:newLocalStoreURL withOptions:@{ STORE_OPTIONS }
|
2017-05-01 22:32:52 +00:00
|
|
|
error:&error]) {
|
2017-04-30 03:03:50 +00:00
|
|
|
MPError( error, @"Couldn't migrate the old store to the new location." );
|
2013-08-31 16:29:56 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2013-04-07 23:05:51 +00:00
|
|
|
|
2013-08-31 16:29:56 +00:00
|
|
|
return YES;
|
2013-04-07 23:05:51 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (BOOL)migrateV2LocalStore {
|
2013-04-07 23:05:51 +00:00
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
NSURL *applicationSupportURL = [[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory
|
|
|
|
inDomains:NSUserDomainMask] lastObject];
|
|
|
|
NSURL *oldLocalStoreURL;
|
|
|
|
// On iOS, each app is in a sandbox so we don't need to app-scope this directory.
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
oldLocalStoreURL = [[applicationSupportURL
|
|
|
|
URLByAppendingPathComponent:@"UbiquityStore" isDirectory:NO]
|
|
|
|
URLByAppendingPathExtension:@"sqlite"];
|
|
|
|
#else
|
|
|
|
// The directory is shared between all apps on the system so we need to scope it for the running app.
|
|
|
|
oldLocalStoreURL = [[[applicationSupportURL
|
|
|
|
URLByAppendingPathComponent:[NSRunningApplication currentApplication].bundleIdentifier isDirectory:YES]
|
|
|
|
URLByAppendingPathComponent:@"UbiquityStore" isDirectory:NO]
|
|
|
|
URLByAppendingPathExtension:@"sqlite"];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (![[NSFileManager defaultManager] fileExistsAtPath:oldLocalStoreURL.path isDirectory:NULL]) {
|
|
|
|
inf( @"No V2 local store to migrate." );
|
2013-08-31 16:29:56 +00:00
|
|
|
return YES;
|
2013-05-02 15:19:34 +00:00
|
|
|
}
|
2013-04-07 23:05:51 +00:00
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
inf( @"Migrating V2 local store" );
|
|
|
|
NSURL *newLocalStoreURL = [self localStoreURL];
|
2014-10-30 04:18:01 +00:00
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath:newLocalStoreURL.path isDirectory:NULL]) {
|
|
|
|
inf( @"New local store already exists." );
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
NSError *error = nil;
|
2014-10-22 23:52:18 +00:00
|
|
|
if (![NSPersistentStore migrateStore:oldLocalStoreURL withOptions:@{
|
2017-04-01 04:30:25 +00:00
|
|
|
NSMigratePersistentStoresAutomaticallyOption: @YES,
|
|
|
|
NSInferMappingModelAutomaticallyOption : @YES,
|
2014-10-22 23:52:18 +00:00
|
|
|
STORE_OPTIONS
|
|
|
|
} toStore:newLocalStoreURL withOptions:@{
|
2017-04-01 04:30:25 +00:00
|
|
|
NSMigratePersistentStoresAutomaticallyOption: @YES,
|
|
|
|
NSInferMappingModelAutomaticallyOption : @YES,
|
2014-10-22 23:52:18 +00:00
|
|
|
STORE_OPTIONS
|
2017-05-01 22:32:52 +00:00
|
|
|
} error:&error]) {
|
2017-04-30 03:03:50 +00:00
|
|
|
MPError( error, @"Couldn't migrate the old store to the new location." );
|
2013-08-31 16:29:56 +00:00
|
|
|
return NO;
|
2014-09-15 05:00:23 +00:00
|
|
|
}
|
2013-04-07 23:05:51 +00:00
|
|
|
|
2013-08-31 16:29:56 +00:00
|
|
|
return YES;
|
2013-04-07 23:05:51 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 22:32:52 +00:00
|
|
|
//- (BOOL)migrateV3LocalStore {
|
|
|
|
//
|
|
|
|
// inf( @"Migrating V3 local store" );
|
|
|
|
// NSURL *localStoreURL = [self localStoreURL];
|
|
|
|
// if (![[NSFileManager defaultManager] fileExistsAtPath:localStoreURL.path isDirectory:NULL]) {
|
|
|
|
// inf( @"No V3 local store to migrate." );
|
|
|
|
// return YES;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// NSError *error = nil;
|
|
|
|
// NSDictionary<NSString *, id> *metadata = [NSPersistentStore metadataForPersistentStoreWithURL:localStoreURL error:&error];
|
|
|
|
// if (!metadata) {
|
|
|
|
// MPError( error, @"Couldn't inspect metadata for store: %@", localStoreURL );
|
|
|
|
// return NO;
|
|
|
|
// }
|
|
|
|
// NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:
|
|
|
|
// [NSManagedObjectModel mergedModelFromBundles:nil forStoreMetadata:metadata]];
|
|
|
|
// if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil
|
|
|
|
// URL:localStoreURL options:@{ STORE_OPTIONS }
|
|
|
|
// error:&error]) {
|
|
|
|
// MPError( error, @"Couldn't open V3 local store to migrate." );
|
|
|
|
// return NO;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
|
|
|
|
// [context performBlockAndWait:^{
|
|
|
|
// context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
|
|
|
|
// context.persistentStoreCoordinator = coordinator;
|
|
|
|
// for (MPStoredSiteEntity *storedSite in [[MPStoredSiteEntity fetchRequest] execute:&error]) {
|
|
|
|
// id contentObject = [storedSite valueForKey:@"contentObject"];
|
|
|
|
// if ([contentObject isKindOfClass:[NSData class]])
|
|
|
|
// storedSite.contentObject = contentObject;
|
|
|
|
// }
|
|
|
|
// }];
|
|
|
|
//
|
|
|
|
// return YES;
|
|
|
|
//}
|
|
|
|
|
2013-05-07 04:45:06 +00:00
|
|
|
#pragma mark - Utilities
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (void)addSiteNamed:(NSString *)siteName completion:(void ( ^ )(MPSiteEntity *site, NSManagedObjectContext *context))completion {
|
2013-05-07 04:45:06 +00:00
|
|
|
|
|
|
|
if (![siteName length]) {
|
2014-07-21 03:54:32 +00:00
|
|
|
completion( nil, nil );
|
2013-05-07 04:45:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
[MPAppDelegate_Shared managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
MPUserEntity *activeUser = [self activeUserInContext:context];
|
2014-04-26 18:03:44 +00:00
|
|
|
NSAssert( activeUser, @"Missing user." );
|
2014-04-07 03:34:18 +00:00
|
|
|
if (!activeUser) {
|
2014-07-21 03:54:32 +00:00
|
|
|
completion( nil, nil );
|
2013-09-13 12:14:58 +00:00
|
|
|
return;
|
2014-04-07 03:34:18 +00:00
|
|
|
}
|
2013-05-07 04:45:06 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
MPResultType type = activeUser.defaultType;
|
2015-02-28 15:01:41 +00:00
|
|
|
id<MPAlgorithm> algorithm = MPAlgorithmDefault;
|
|
|
|
Class entityType = [algorithm classOfType:type];
|
2013-05-07 04:45:06 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
MPSiteEntity *site = (MPSiteEntity *)[entityType insertNewObjectInContext:context];
|
2014-09-21 14:39:09 +00:00
|
|
|
site.name = siteName;
|
|
|
|
site.user = activeUser;
|
|
|
|
site.type = type;
|
|
|
|
site.lastUsed = [NSDate date];
|
2015-02-28 15:01:41 +00:00
|
|
|
site.algorithm = algorithm;
|
2013-05-07 04:45:06 +00:00
|
|
|
|
2014-04-15 05:07:46 +00:00
|
|
|
[context saveToStore];
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
completion( site, context );
|
2013-05-07 04:45:06 +00:00
|
|
|
}];
|
|
|
|
}
|
2012-05-09 17:34:00 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
- (MPSiteEntity *)changeSite:(MPSiteEntity *)site saveInContext:(NSManagedObjectContext *)context toType:(MPResultType)type {
|
2014-04-07 03:34:18 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
if (site.type == type)
|
|
|
|
return site;
|
2013-06-15 05:39:24 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
if ([site.algorithm classOfType:type] == site.typeClass) {
|
|
|
|
site.type = type;
|
2014-04-07 03:34:18 +00:00
|
|
|
[context saveToStore];
|
|
|
|
}
|
2013-06-15 05:39:24 +00:00
|
|
|
|
|
|
|
else {
|
2014-09-21 14:39:09 +00:00
|
|
|
// Type requires a different class of site. Recreate the site.
|
2015-02-28 15:01:41 +00:00
|
|
|
Class entityType = [site.algorithm classOfType:type];
|
|
|
|
MPSiteEntity *newSite = (MPSiteEntity *)[entityType insertNewObjectInContext:context];
|
2014-09-21 14:39:09 +00:00
|
|
|
newSite.type = type;
|
|
|
|
newSite.name = site.name;
|
|
|
|
newSite.user = site.user;
|
|
|
|
newSite.uses = site.uses;
|
|
|
|
newSite.lastUsed = site.lastUsed;
|
2015-02-28 15:01:41 +00:00
|
|
|
newSite.algorithm = site.algorithm;
|
2014-09-21 14:39:09 +00:00
|
|
|
newSite.loginName = site.loginName;
|
2013-06-15 05:39:24 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
[context deleteObject:site];
|
2014-04-15 04:26:13 +00:00
|
|
|
[context saveToStore];
|
|
|
|
|
2017-04-30 03:52:57 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.permanentObjectID];
|
2014-09-21 14:39:09 +00:00
|
|
|
site = newSite;
|
2013-06-15 05:39:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 03:52:57 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.permanentObjectID];
|
2014-09-21 14:39:09 +00:00
|
|
|
return site;
|
2013-06-15 05:39:24 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
- (void)importSites:(NSString *)importData
|
|
|
|
askImportPassword:(NSString *( ^ )(NSString *userName))importPassword
|
|
|
|
askUserPassword:(NSString *( ^ )(NSString *userName))userPassword
|
|
|
|
result:(void ( ^ )(NSError *error))resultBlock {
|
2012-08-19 07:34:49 +00:00
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
NSAssert( ![[NSThread currentThread] isMainThread], @"This method should not be invoked from the main thread." );
|
2013-05-16 02:42:21 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
if ([MPAppDelegate_Shared managedObjectContextPerformBlockAndWait:^(NSManagedObjectContext *context) {
|
2017-08-13 01:57:47 +00:00
|
|
|
NSError *error = [self importSites:importData askImportPassword:importPassword askUserPassword:userPassword
|
|
|
|
saveInContext:context];
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
resultBlock( error );
|
|
|
|
} );
|
2013-05-16 02:42:21 +00:00
|
|
|
}])
|
|
|
|
break;
|
|
|
|
usleep( (useconds_t)(USEC_PER_SEC * 0.2) );
|
|
|
|
} while (YES);
|
|
|
|
}
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
- (NSError *)importSites:(NSString *)importData
|
|
|
|
askImportPassword:(NSString *( ^ )(NSString *userName))askImportPassword
|
|
|
|
askUserPassword:(NSString *( ^ )(NSString *userName))askUserPassword
|
|
|
|
saveInContext:(NSManagedObjectContext *)context {
|
|
|
|
|
|
|
|
// Read metadata for the import file.
|
2017-09-22 19:20:14 +00:00
|
|
|
MPMarshalInfo *info = mpw_marshal_read_info( importData.UTF8String );
|
2017-09-20 14:48:04 +00:00
|
|
|
if (info->format == MPMarshalFormatNone)
|
|
|
|
return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshalCode userInfo:@{
|
|
|
|
@"type" : @(MPMarshalErrorFormat),
|
2017-08-13 01:57:47 +00:00
|
|
|
NSLocalizedDescriptionKey: @"This is not a Master Password import file.",
|
|
|
|
}]), @"While importing sites." );
|
|
|
|
|
|
|
|
// Get master password for import file.
|
|
|
|
MPKey *importKey;
|
|
|
|
NSString *importMasterPassword;
|
|
|
|
do {
|
|
|
|
importMasterPassword = askImportPassword( @(info->fullName) );
|
|
|
|
if (!importMasterPassword) {
|
|
|
|
inf( @"Import cancelled." );
|
2017-08-23 04:01:23 +00:00
|
|
|
mpw_marshal_info_free( &info );
|
2017-08-13 01:57:47 +00:00
|
|
|
return MPError( ([NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]), @"" );
|
2013-01-31 05:42:32 +00:00
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
importKey = [[MPKey alloc] initForFullName:@(info->fullName) withMasterPassword:importMasterPassword];
|
|
|
|
} while ([[[importKey keyIDForAlgorithm:MPAlgorithmForVersion( info->algorithm )] encodeHex]
|
|
|
|
caseInsensitiveCompare:@(info->keyID)] != NSOrderedSame);
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
// Parse import data.
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalError importError = { .type = MPMarshalSuccess };
|
2017-09-22 19:20:14 +00:00
|
|
|
MPMarshalledUser *importUser = mpw_marshal_read( importData.UTF8String, info->format, importMasterPassword.UTF8String, &importError );
|
2017-08-23 04:01:23 +00:00
|
|
|
mpw_marshal_info_free( &info );
|
2017-08-13 01:57:47 +00:00
|
|
|
|
|
|
|
@try {
|
2017-09-20 14:48:04 +00:00
|
|
|
if (!importUser || importError.type != MPMarshalSuccess)
|
|
|
|
return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshalCode userInfo:@{
|
2017-08-13 01:57:47 +00:00
|
|
|
@"type" : @(importError.type),
|
|
|
|
NSLocalizedDescriptionKey: @(importError.description),
|
|
|
|
}]), @"While importing sites." );
|
|
|
|
|
|
|
|
// Find an existing user to update.
|
|
|
|
NSError *error = nil;
|
|
|
|
NSFetchRequest *userFetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass( [MPUserEntity class] )];
|
|
|
|
userFetchRequest.predicate = [NSPredicate predicateWithFormat:@"name == %@", @(importUser->fullName)];
|
|
|
|
NSArray *users = [context executeFetchRequest:userFetchRequest error:&error];
|
|
|
|
if (!users)
|
|
|
|
return MPError( error, @"While looking for user: %@.", @(importUser->fullName) );
|
|
|
|
if ([users count] > 1)
|
|
|
|
return MPMakeError( @"While looking for user: %@, found more than one: %zu",
|
|
|
|
@(importUser->fullName), (size_t)[users count] );
|
|
|
|
|
|
|
|
// Get master key for user.
|
|
|
|
MPUserEntity *user = [users lastObject];
|
|
|
|
MPKey *userKey = importKey;
|
|
|
|
while (user && ![[userKey keyIDForAlgorithm:user.algorithm] isEqualToData:user.keyID]) {
|
|
|
|
NSString *userMasterPassword = askUserPassword( user.name );
|
|
|
|
if (!userMasterPassword) {
|
|
|
|
inf( @"Import cancelled." );
|
|
|
|
return MPError( ([NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]), @"" );
|
2012-06-22 14:52:33 +00:00
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
userKey = [[MPKey alloc] initForFullName:@(importUser->fullName) withMasterPassword:userMasterPassword];
|
2014-08-23 05:39:47 +00:00
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
// Update or create user.
|
|
|
|
if (!user) {
|
|
|
|
user = [MPUserEntity insertNewObjectInContext:context];
|
|
|
|
user.name = @(importUser->fullName);
|
|
|
|
}
|
|
|
|
user.algorithm = MPAlgorithmForVersion( importUser->algorithm );
|
|
|
|
user.keyID = [userKey keyIDForAlgorithm:user.algorithm];
|
|
|
|
user.avatar = importUser->avatar;
|
|
|
|
user.defaultType = importUser->defaultType;
|
|
|
|
user.lastUsed = [NSDate dateWithTimeIntervalSince1970:MAX( user.lastUsed.timeIntervalSince1970, importUser->lastUsed )];
|
|
|
|
dbg( @"Importing user: %@", [user debugDescription] );
|
|
|
|
|
|
|
|
// Update or create sites.
|
|
|
|
for (size_t s = 0; s < importUser->sites_count; ++s) {
|
|
|
|
MPMarshalledSite *importSite = &importUser->sites[s];
|
|
|
|
|
|
|
|
// Find an existing site to update.
|
|
|
|
NSFetchRequest *siteFetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass( [MPSiteEntity class] )];
|
|
|
|
siteFetchRequest.predicate = [NSPredicate predicateWithFormat:@"name == %@ AND user == %@", @(importSite->name), user];
|
2014-09-21 14:39:09 +00:00
|
|
|
NSArray *existingSites = [context executeFetchRequest:siteFetchRequest error:&error];
|
2017-08-13 01:57:47 +00:00
|
|
|
if (!existingSites)
|
|
|
|
return MPError( error, @"Lookup of existing sites failed for site: %@, user: %@", @(importSite->name), user.userID );
|
|
|
|
if ([existingSites count])
|
|
|
|
// Update existing site.
|
|
|
|
for (MPSiteEntity *site in existingSites) {
|
|
|
|
[self importSite:importSite protectedByKey:importKey intoSite:site usingKey:userKey];
|
|
|
|
dbg( @"Updated site: %@", [site debugDescription] );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Create new site.
|
|
|
|
id<MPAlgorithm> algorithm = MPAlgorithmForVersion( importSite->algorithm );
|
|
|
|
Class entityType = [algorithm classOfType:importSite->type];
|
|
|
|
if (!entityType)
|
|
|
|
return MPMakeError( @"Invalid site type in import file: %@ has type %lu", @(importSite->name), (long)importSite->type );
|
|
|
|
|
|
|
|
MPSiteEntity *site = (MPSiteEntity *)[entityType insertNewObjectInContext:context];
|
|
|
|
site.user = user;
|
|
|
|
|
|
|
|
[self importSite:importSite protectedByKey:importKey intoSite:site usingKey:userKey];
|
|
|
|
dbg( @"Created site: %@", [site debugDescription] );
|
2013-10-31 03:37:12 +00:00
|
|
|
}
|
2012-06-04 09:27:02 +00:00
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
if (![context saveToStore])
|
|
|
|
return MPMakeError( @"Failed saving imported changes." );
|
2013-01-31 05:42:32 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
inf( @"Import completed successfully." );
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:MPSitesImportedNotification object:nil userInfo:@{
|
|
|
|
MPSitesImportedNotificationUserKey: user
|
|
|
|
}];
|
|
|
|
return nil;
|
2013-01-31 05:42:32 +00:00
|
|
|
}
|
2017-08-13 01:57:47 +00:00
|
|
|
@finally {
|
2017-08-23 04:01:23 +00:00
|
|
|
mpw_marshal_free( &importUser );
|
2013-01-31 05:42:32 +00:00
|
|
|
}
|
2012-05-09 08:11:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
- (void)importSite:(const MPMarshalledSite *)importSite protectedByKey:(MPKey *)importKey intoSite:(MPSiteEntity *)site
|
|
|
|
usingKey:(MPKey *)userKey {
|
|
|
|
|
|
|
|
site.name = @(importSite->name);
|
|
|
|
if (importSite->content)
|
|
|
|
[site.algorithm importPassword:@(importSite->content) protectedByKey:importKey intoSite:site usingKey:userKey];
|
|
|
|
site.type = importSite->type;
|
|
|
|
if ([site isKindOfClass:[MPGeneratedSiteEntity class]])
|
|
|
|
((MPGeneratedSiteEntity *)site).counter = importSite->counter;
|
|
|
|
site.algorithm = MPAlgorithmForVersion( importSite->algorithm );
|
2017-08-22 22:18:24 +00:00
|
|
|
site.loginName = importSite->loginContent? @(importSite->loginContent): nil;
|
2017-08-28 23:37:51 +00:00
|
|
|
site.loginGenerated = importSite->loginType & MPResultTypeClassTemplate;
|
2017-08-13 01:57:47 +00:00
|
|
|
site.url = importSite->url? @(importSite->url): nil;
|
|
|
|
site.uses = importSite->uses;
|
|
|
|
site.lastUsed = [NSDate dateWithTimeIntervalSince1970:importSite->lastUsed];
|
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
- (void)exportSitesRevealPasswords:(BOOL)revealPasswords
|
|
|
|
askExportPassword:(NSString *( ^ )(NSString *userName))askImportPassword
|
|
|
|
result:(void ( ^ )(NSString *mpsites, NSError *error))resultBlock {
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
[MPAppDelegate_Shared managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
MPUserEntity *user = [self activeUserInContext:context];
|
|
|
|
NSString *masterPassword = askImportPassword( user.name );
|
|
|
|
|
|
|
|
inf( @"Exporting sites, %@, for user: %@", revealPasswords? @"revealing passwords": @"omitting passwords", user.userID );
|
2017-09-22 19:20:14 +00:00
|
|
|
MPMarshalledUser *exportUser = mpw_marshal_user( user.name.UTF8String, masterPassword.UTF8String, user.algorithm.version );
|
2017-08-13 01:57:47 +00:00
|
|
|
exportUser->redacted = !revealPasswords;
|
|
|
|
exportUser->avatar = (unsigned int)user.avatar;
|
|
|
|
exportUser->defaultType = user.defaultType;
|
|
|
|
exportUser->lastUsed = (time_t)user.lastUsed.timeIntervalSince1970;
|
|
|
|
|
|
|
|
for (MPSiteEntity *site in user.sites) {
|
|
|
|
MPCounterValue counter = MPCounterValueInitial;
|
|
|
|
if ([site isKindOfClass:[MPGeneratedSiteEntity class]])
|
|
|
|
counter = ((MPGeneratedSiteEntity *)site).counter;
|
|
|
|
NSString *content = revealPasswords
|
|
|
|
? [site.algorithm exportPasswordForSite:site usingKey:self.key]
|
|
|
|
: [site.algorithm resolvePasswordForSite:site usingKey:self.key];
|
|
|
|
|
2017-09-22 19:20:14 +00:00
|
|
|
MPMarshalledSite *exportSite = mpw_marshal_site( exportUser,
|
2017-08-13 01:57:47 +00:00
|
|
|
site.name.UTF8String, site.type, counter, site.algorithm.version );
|
|
|
|
exportSite->content = content.UTF8String;
|
2017-08-22 22:18:24 +00:00
|
|
|
exportSite->loginContent = site.loginName.UTF8String;
|
2017-08-28 23:37:51 +00:00
|
|
|
exportSite->loginType = site.loginGenerated? MPResultTypeTemplateName: MPResultTypeStatefulPersonal;
|
2017-08-13 01:57:47 +00:00
|
|
|
exportSite->url = site.url.UTF8String;
|
|
|
|
exportSite->uses = (unsigned int)site.uses;
|
|
|
|
exportSite->lastUsed = (time_t)site.lastUsed.timeIntervalSince1970;
|
|
|
|
|
|
|
|
for (MPSiteQuestionEntity *siteQuestion in site.questions)
|
|
|
|
mpw_marshal_question( exportSite, siteQuestion.keyword.UTF8String );
|
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
char *export = NULL;
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalError exportError = (MPMarshalError){ .type= MPMarshalSuccess };
|
2017-09-22 19:20:14 +00:00
|
|
|
mpw_marshal_write( &export, MPMarshalFormatFlat, exportUser, &exportError );
|
2017-08-13 01:57:47 +00:00
|
|
|
NSString *mpsites = nil;
|
2017-09-20 14:48:04 +00:00
|
|
|
if (export && exportError.type == MPMarshalSuccess)
|
2017-08-13 01:57:47 +00:00
|
|
|
mpsites = [NSString stringWithCString:export encoding:NSUTF8StringEncoding];
|
2017-08-28 23:37:51 +00:00
|
|
|
mpw_free_string( &export );
|
2017-08-13 01:57:47 +00:00
|
|
|
|
2017-09-20 14:48:04 +00:00
|
|
|
resultBlock( mpsites, exportError.type == MPMarshalSuccess? nil:
|
|
|
|
[NSError errorWithDomain:MPErrorDomain code:MPErrorMarshalCode userInfo:@{
|
2017-08-13 01:57:47 +00:00
|
|
|
@"type" : @(exportError.type),
|
|
|
|
NSLocalizedDescriptionKey: @(exportError.description),
|
|
|
|
}] );
|
|
|
|
}];
|
2012-05-09 08:11:34 +00:00
|
|
|
}
|
2012-05-07 20:18:01 +00:00
|
|
|
|
|
|
|
@end
|