File: KeyFolderXml1.cs

package info (click to toggle)
keepass2 2.57%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 14,516 kB
  • sloc: cs: 120,930; xml: 6,271; cpp: 322; sh: 53; makefile: 49
file content (186 lines) | stat: -rw-r--r-- 5,110 bytes parent folder | download | duplicates (2)
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
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2024 Dominik Reichl <dominik.reichl@t-online.de>

  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 2 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, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;

using KeePass.Resources;
using KeePass.Util;

using KeePassLib;
using KeePassLib.Interfaces;
using KeePassLib.Utility;

namespace KeePass.DataExchange.Formats
{
	// 1.22+
	internal sealed class KeyFolderXml1 : FileFormatProvider
	{
		public override bool SupportsImport { get { return true; } }
		public override bool SupportsExport { get { return false; } }

		public override string FormatName { get { return "Key Folder XML"; } }
		public override string DefaultExtension { get { return "xml"; } }
		public override string ApplicationGroup { get { return KPRes.PasswordManagers; } }

		public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
		{
			using(StreamReader sr = new StreamReader(sInput, Encoding.UTF8, true))
			{
				XmlDocument d = XmlUtilEx.CreateXmlDocument();
				d.LoadXml(sr.ReadToEnd());

				ImportGroup(d.DocumentElement, pwStorage.RootGroup, pwStorage);
			}
		}

		private static void ImportGroup(XmlNode xnFolder, PwGroup pg, PwDatabase pd)
		{
			foreach(XmlNode xn in xnFolder.ChildNodes)
			{
				switch(xn.Name)
				{
					case "name":
						pg.Name = XmlUtil.SafeInnerText(xn);
						break;

					case "folder":
						PwGroup pgSub = new PwGroup(true, true);
						pg.AddGroup(pgSub, true);
						ImportGroup(xn, pgSub, pd);
						break;

					case "item":
						ImportEntry(xn, pg, pd);
						break;

					default:
						Debug.Assert(false); // Unknown node
						break;
				}
			}
		}

		private static void ImportEntry(XmlNode xnItem, PwGroup pg, PwDatabase pd)
		{
			PwEntry pe = new PwEntry(true, true);
			pg.AddEntry(pe, true);

			bool bFirstUrl = true;
			DateTime? odt;

			foreach(XmlNode xn in xnItem.ChildNodes)
			{
				switch(xn.Name)
				{
					case "name":
						ImportUtil.AppendToField(pe, PwDefs.TitleField,
							XmlUtil.SafeInnerText(xn), pd);
						break;

					case "username":
						ImportUtil.AppendToField(pe, PwDefs.UserNameField,
							XmlUtil.SafeInnerText(xn), pd);
						break;

					case "password":
						ImportUtil.AppendToField(pe, PwDefs.PasswordField,
							XmlUtil.SafeInnerText(xn), pd);
						break;

					case "link":
						string strType = GetChildValue(xn, "type");
						string strName = GetChildValue(xn, "name");
						string strUrl = GetChildValue(xn, "url");
						if(string.IsNullOrEmpty(strUrl)) continue;

						if(bFirstUrl)
						{
							strName = PwDefs.UrlField;
							bFirstUrl = false;
						}

						if(strType == "mail") strUrl = "mailto:" + strUrl;

						ImportUtil.CreateFieldWithIndex(pe.Strings, strName,
							strUrl, pd, false);
						break;

					case "note":
						ImportUtil.AppendToField(pe, PwDefs.NotesField,
							XmlUtil.SafeInnerText(xn), pd);
						break;

					case "created":
						odt = TryParseDate(xn);
						if(odt.HasValue) pe.CreationTime = odt.Value;
						break;

					case "changed":
						odt = TryParseDate(xn);
						if(odt.HasValue) pe.LastModificationTime = odt.Value;
						break;

					case "expire":
						odt = TryParseDate(xn);
						if(odt.HasValue)
						{
							pe.Expires = true;
							pe.ExpiryTime = odt.Value;
						}
						break;

					default:
						Debug.Assert(false); // Unknown node
						break;
				}
			}
		}

		private static string GetChildValue(XmlNode xn, string strChildName)
		{
			XmlNode xnChild = XmlUtil.FindMultiChild(xn.ChildNodes, strChildName, 0);
			if(xnChild == null) { Debug.Assert(false); return string.Empty; }
			return XmlUtil.SafeInnerText(xnChild);
		}

		private static DateTime? TryParseDate(XmlNode xn)
		{
			string str = XmlUtil.SafeInnerText(xn);
			if(string.IsNullOrEmpty(str)) { Debug.Assert(false); return null; }

			DateTime dt;
			if(!DateTime.TryParseExact(str, @"dd'.'MM'.'yyyy",
				CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out dt))
			{
				Debug.Assert(false);
				if(!StrUtil.TryParseDateTime(str, out dt))
					return null;
			}

			return TimeUtil.ToUtc(dt, false);
		}
	}
}