-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
187 lines (155 loc) · 5.13 KB
/
Program.cs
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
184
185
186
187
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using KeePassLib;
using KeePassLib.Serialization;
using KeePassLib.Interfaces;
using KeePassLib.Keys;
using CommandLine;
using CommandLine.Text;
namespace wr2kpsx
{
class MainClass
{
public static void Main(string[] args)
{
(new MainClass()).Run(args);
}
private void Run(string[] args)
{
var options = new Options();
if (!CommandLine.Parser.Default.ParseArguments(args, options))
{
return;
}
PrintMsg("Start importing: " + options.InputFile);
try
{
this.verbose = options.Verbose;
this.importBookmarks = options.ImportBookmarks;
ImportXML(options.InputFile, options.KeePassPwd);
}
catch (Exception ex)
{
PrintMsg(ex.Message);
Environment.Exit(1);
return;
}
}
private void PrintMsg(string msg)
{
if (this.verbose)
{
Console.WriteLine(msg);
}
}
void ImportXML(string xmlFile, string pwd)
{
// Create a new Keepass database.
var kdbxPath = xmlFile + ".kdbx";
var ioConnInfo = new IOConnectionInfo { Path = kdbxPath };
var compKey = new CompositeKey();
compKey.AddUserKey(new KcpPassword(pwd));
var kpdb = new KeePassLib.PwDatabase();
kpdb.New(ioConnInfo, compKey);
PrintMsg("Importing to: " + kdbxPath);
try
{
// Read the XML file.
XDocument xdoc = XDocument.Load(xmlFile);
///////////////////////////////////////////////////////////////////////////
PrintMsg("Logins...");
var logins = from lgn in xdoc.Descendants("login")
select new {
Name = lgn.Attribute("name").Value,
Url = lgn.Attribute("site").Value,
User = lgn.Attribute("user").Value,
Pwd = lgn.Attribute("password").Value,
User2 = lgn.Attribute("user2").Value,
Pwd2 = lgn.Attribute("password2").Value,
Notes = lgn.Value
};
int doubleUserAccount = 0;
var accountsGroup = kpdb.RootGroup.FindCreateGroup("Accounts", true);
foreach (var lgn in logins)
{
var login = new PwEntry(true, true);
login.Strings.Set("Title", new KeePassLib.Security.ProtectedString(true, lgn.Name));
login.Strings.Set("Notes", new KeePassLib.Security.ProtectedString(true, lgn.Notes));
login.Strings.Set("URL", new KeePassLib.Security.ProtectedString(true, lgn.Url));
login.Strings.Set("UserName", new KeePassLib.Security.ProtectedString(true, lgn.User));
login.Strings.Set("Password", new KeePassLib.Security.ProtectedString(true, lgn.Pwd));
accountsGroup.AddEntry(login, true);
if (!String.IsNullOrWhiteSpace(lgn.User2) ||
!String.IsNullOrWhiteSpace(lgn.Pwd2))
{
doubleUserAccount++;
}
}
PrintMsg(String.Format("{0} accounts imported, {1} accounts with 2 credentials", logins.Count(), doubleUserAccount));
///////////////////////////////////////////////////////////////////////////
PrintMsg("Notes...");
var safeNotes = from sn in xdoc.Descendants("note")
select new {
Name = sn.Attribute("name").Value,
Text = sn.Value
};
var notesGroup = kpdb.RootGroup.FindCreateGroup("Notes", true);
foreach (var sn in safeNotes)
{
var note = new PwEntry(true, true);
note.Strings.Set("Title", new KeePassLib.Security.ProtectedString(true, sn.Name));
note.Strings.Set("Notes", new KeePassLib.Security.ProtectedString(true, sn.Text));
notesGroup.AddEntry(note, true);
}
PrintMsg(String.Format("{0} notes imported", safeNotes.Count()));
///////////////////////////////////////////////////////////////////////////
if (this.importBookmarks)
{
PrintMsg("Bookmarks...");
var bookmarks = from bm in xdoc.Descendants("bookmark")
select new {
Url = bm.Attribute("url").Value,
Name = bm.Value
};
var bookmarksGroup = kpdb.RootGroup.FindCreateGroup("Bookmarks", true);
foreach (var b in bookmarks)
{
var bookmark = new PwEntry(true, true);
bookmark.Strings.Set("Title", new KeePassLib.Security.ProtectedString(true, b.Name));
bookmark.Strings.Set("URL", new KeePassLib.Security.ProtectedString(true, b.Url));
bookmarksGroup.AddEntry(bookmark, true);
}
PrintMsg(String.Format("{0} bookmarks imported", bookmarks.Count()));
}
}
finally
{
kpdb.Save(null);
kpdb.Close();
kpdb = null;
}
}
private bool verbose = true;
private bool importBookmarks = false;
}
internal class Options
{
[Option('r', "read", Required = true, HelpText = "Input XML file to be imported.")]
public string InputFile { get; set; }
[Option('p', "password", Required = false, HelpText = "Set password on kbdx database.")]
public string KeePassPwd { get; set; }
[Option('b', "bookmakrs", DefaultValue = false, HelpText = "Import bookmarks.")]
public bool ImportBookmarks { get; set; }
[Option('v', "verbose", DefaultValue = true, HelpText = "Prints all messages to standard output.")]
public bool Verbose { get; set; }
[ParserState]
public IParserState LastParserState { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
}