This repository has been archived by the owner on Jan 25, 2020. It is now read-only.
/
DatabaseManager.m
183 lines (147 loc) · 7.21 KB
/
DatabaseManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright 2011-2012 Jason Rush and John Flanagan. All rights reserved.
*
* This program 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.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import "DatabaseManager.h"
#import "MiniKeePassAppDelegate.h"
#import "SFHFKeychainUtils.h"
#import "PasswordViewController.h"
#import "AppSettings.h"
@implementation DatabaseManager
@synthesize selectedFilename;
@synthesize animated;
static DatabaseManager *sharedInstance;
+ (void)initialize {
static BOOL initialized = NO;
if (!initialized) {
initialized = YES;
sharedInstance = [[DatabaseManager alloc] init];
}
}
+ (DatabaseManager*)sharedInstance {
return sharedInstance;
}
- (void)dealloc {
[selectedFilename release];
[super dealloc];
}
- (void)openDatabaseDocument:(NSString*)filename animated:(BOOL)newAnimated {
BOOL databaseLoaded = NO;
self.selectedFilename = filename;
self.animated = newAnimated;
// Get the application delegate
MiniKeePassAppDelegate *appDelegate = (MiniKeePassAppDelegate*)[[UIApplication sharedApplication] delegate];
// Get the documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Load the password and keyfile from the keychain
NSString *password = [SFHFKeychainUtils getPasswordForUsername:selectedFilename andServiceName:@"com.jflan.MiniKeePass.passwords" error:nil];
NSString *keyFile = [SFHFKeychainUtils getPasswordForUsername:selectedFilename andServiceName:@"com.jflan.MiniKeePass.keyfiles" error:nil];
// Try and load the database with the cached password from the keychain
if (password != nil || keyFile != nil) {
// Get the absolute path to the database
NSString *path = [documentsDirectory stringByAppendingPathComponent:selectedFilename];
// Get the absolute path to the keyfile
NSString *keyFilePath = nil;
if (keyFile != nil) {
keyFilePath = [documentsDirectory stringByAppendingPathComponent:keyFile];
}
// Load the database
DatabaseDocument *dd = [[DatabaseDocument alloc] init];
@try {
[dd open:path password:password keyFile:keyFilePath];
databaseLoaded = YES;
// Set the database document in the application delegate
appDelegate.databaseDocument = dd;
} @catch (NSException * exception) {
// Ignore
}
[dd release];
}
// Prompt the user for the password if we haven't loaded the database yet
if (!databaseLoaded) {
// Prompt the user for a password
PasswordViewController *passwordViewController = [[PasswordViewController alloc] initWithFilename:filename];
passwordViewController.delegate = self;
// Create a defult keyfile name from the database name
keyFile = [[filename stringByDeletingPathExtension] stringByAppendingPathExtension:@"key"];
// Select the keyfile if it's in the list
NSInteger index = [passwordViewController.keyFileCell.choices indexOfObject:keyFile];
if (index != NSNotFound) {
passwordViewController.keyFileCell.selectedIndex = index;
} else {
passwordViewController.keyFileCell.selectedIndex = 0;
}
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:passwordViewController];
[appDelegate.window.rootViewController presentModalViewController:navigationController animated:animated];
[navigationController release];
[passwordViewController release];
}
}
- (void)loadDatabaseDocument:(DatabaseDocument*)databaseDocument {
// Set the database document in the application delegate
MiniKeePassAppDelegate *appDelegate = (MiniKeePassAppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.databaseDocument = databaseDocument;
[databaseDocument release];
}
- (void)formViewController:(FormViewController *)controller button:(FormViewControllerButton)button {
PasswordViewController *passwordViewController = (PasswordViewController*)controller;
BOOL shouldDismiss = YES;
// Check if the OK button was pressed
if (button == FormViewControllerButtonOk) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:selectedFilename];
// Get the password
NSString *password = passwordViewController.passwordTextField.text;
if ([password isEqualToString:@""]) {
password = nil;
}
// Get the keyfile
NSString *keyFile = [passwordViewController.keyFileCell getSelectedItem];
if ([keyFile isEqualToString:NSLocalizedString(@"None", nil)]) {
keyFile = nil;
}
// Get the absolute path to the keyfile
NSString *keyFilePath = nil;
if (keyFile != nil) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
keyFilePath = [documentsDirectory stringByAppendingPathComponent:keyFile];
}
// Load the database
DatabaseDocument *dd = [[DatabaseDocument alloc] init];
@try {
// Open the database
[dd open:path password:password keyFile:keyFilePath];
// Store the password in the keychain
if ([[AppSettings sharedInstance] rememberPasswordsEnabled]) {
NSError *error;
[SFHFKeychainUtils storeUsername:selectedFilename andPassword:password forServiceName:@"com.jflan.MiniKeePass.passwords" updateExisting:YES error:&error];
[SFHFKeychainUtils storeUsername:selectedFilename andPassword:keyFile forServiceName:@"com.jflan.MiniKeePass.keyfiles" updateExisting:YES error:&error];
}
// Load the database after a short delay so the push animation is visible
[self performSelector:@selector(loadDatabaseDocument:) withObject:dd afterDelay:0.01];
} @catch (NSException *exception) {
shouldDismiss = NO;
[passwordViewController showErrorMessage:exception.reason];
[dd release];
}
}
if (shouldDismiss) {
[passwordViewController dismissModalViewControllerAnimated:YES];
}
}
@end