libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
metadata.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
25#pragma once
26#include <ctime>
27#include <memory>
28#include <optional>
29#include <string>
30#include <vector>
31
32#include "binary.hh"
33#include "icon.hh"
34#include "libkeepass/export.hh"
35#include "temporal.hh"
36
37namespace keepass {
38
39class Group;
40
50public:
54 class MemoryProtection final {
55 private:
56 bool title_ = false;
57 bool username_ = false;
58 bool password_ = true;
59 bool url_ = false;
60 bool notes_ = false;
61
62 public:
64 bool title() const { return title_; }
65
67 void set_title(bool title) { title_ = title; }
68
70 bool username() const { return username_; }
71
73 void set_username(bool username) { username_ = username; }
74
76 bool password() const { return password_; }
77
79 void set_password(bool password) { password_ = password; }
80
82 bool url() const { return url_; }
83
85 void set_url(bool url) { url_ = url; }
86
88 bool notes() const { return notes_; }
89
91 void set_notes(bool notes) { notes_ = notes; }
92 };
93
97 class Field final {
98 private:
99 std::string key_;
100 std::string value_;
101 std::optional<std::time_t> last_modification_time_;
102
103 public:
105 Field(std::string key, std::string value) : key_(std::move(key)), value_(std::move(value)) {}
106
108 Field(const Field& other)
109 : key_(other.key_), value_(other.value_),
110 last_modification_time_(other.last_modification_time_) {}
111
113 Field(Field&& other) noexcept
114 : key_(std::move(other.key_)), value_(std::move(other.value_)),
115 last_modification_time_(other.last_modification_time_) {}
116
118 const std::string& key() const { return key_; }
119
121 const std::string& value() const { return value_; }
122
124 const std::optional<std::time_t>& last_modification_time() const {
125 return last_modification_time_;
126 }
127
130 void set_last_modification_time(std::optional<std::time_t> time) {
131 last_modification_time_ = time;
132 }
133
135 Field& operator=(const Field& other) = default;
136
138 Field& operator=(Field&& other) noexcept {
139 key_ = std::move(other.key_);
140 value_ = std::move(other.value_);
141 last_modification_time_ = other.last_modification_time_;
142 return *this;
143 }
144 };
145
154 class DeletedObject final {
155 private:
156 std::array<uint8_t, 16> uuid_;
157 std::time_t deletion_time_ = 0;
158
159 public:
161 DeletedObject(const std::array<uint8_t, 16>& uuid, std::time_t deletion_time)
162 : uuid_(uuid), deletion_time_(deletion_time) {}
163
165 const std::array<uint8_t, 16>& uuid() const { return uuid_; }
166
168 void set_uuid(const std::array<uint8_t, 16>& uuid) { uuid_ = uuid; }
169
171 std::time_t deletion_time() const { return deletion_time_; }
172
174 void set_deletion_time(std::time_t time) { deletion_time_ = time; }
175
177 bool operator==(const DeletedObject& other) const {
178 return uuid_ == other.uuid_ && deletion_time_ == other.deletion_time_;
179 }
180
182 bool operator!=(const DeletedObject& other) const { return !(*this == other); }
183 };
184
185private:
186 std::string generator_;
187 temporal<std::string> database_name_;
188 temporal<std::string> database_desc_;
189 temporal<std::string> default_username_;
190 uint32_t maintenance_hist_days_ = 365;
191 std::string database_color_;
192 std::time_t master_key_changed_ = 0;
193 int64_t master_key_change_rec_ = -1;
194 int64_t master_key_change_force_ = -1;
195 MemoryProtection memory_protection_;
196 std::shared_ptr<Group> recycle_bin_;
197 std::time_t recycle_bin_changed_;
198 std::shared_ptr<Group> entry_templates_;
199 std::time_t entry_templates_changed_;
200 int32_t history_max_items_ = -1;
201 int64_t history_max_size_ = -1;
202 std::weak_ptr<Group> last_selected_group_;
203 std::weak_ptr<Group> last_visible_group_;
204
205 std::vector<std::shared_ptr<Binary>> binaries_;
206 std::vector<std::shared_ptr<Icon>> icons_;
207 std::vector<Field> fields_;
208 std::vector<DeletedObject> deleted_objects_;
209
210public:
212 const std::string& generator() const { return generator_; }
213
215 void set_generator(const std::string& generator) { generator_ = generator; }
216
218 const temporal<std::string>& database_name() const { return database_name_; }
219
221 void set_database_name(const temporal<std::string>& name) { database_name_ = name; }
222
224 const temporal<std::string>& database_desc() const { return database_desc_; }
225
227 void set_database_desc(const temporal<std::string>& desc) { database_desc_ = desc; }
228
230 const temporal<std::string>& default_username() const { return default_username_; }
231
233 void set_default_username(const temporal<std::string>& username) { default_username_ = username; }
234
236 uint32_t maintenance_hist_days() const { return maintenance_hist_days_; }
237
239 void set_maintenance_hist_days(uint32_t days) { maintenance_hist_days_ = days; }
240
242 const std::string& database_color() const { return database_color_; }
243
245 void set_database_color(const std::string& color) { database_color_ = color; }
246
248 std::time_t master_key_changed() const { return master_key_changed_; }
249
251 void set_master_key_changed(std::time_t time) { master_key_changed_ = time; }
252
254 int64_t master_key_change_rec() const { return master_key_change_rec_; }
255
257 void set_master_key_change_rec(int64_t rec) { master_key_change_rec_ = rec; }
258
260 int64_t master_key_change_force() const { return master_key_change_force_; }
261
263 void set_master_key_change_force(int64_t force) { master_key_change_force_ = force; }
264
266 MemoryProtection& memory_protection() { return memory_protection_; }
267
269 std::shared_ptr<Group> recycle_bin() const { return recycle_bin_; }
270
272 void set_recycle_bin(std::shared_ptr<Group> bin) { recycle_bin_ = std::move(bin); }
273
275 std::time_t recycle_bin_changed() const { return recycle_bin_changed_; }
276
278 void set_recycle_bin_changed(std::time_t time) { recycle_bin_changed_ = time; }
279
281 std::shared_ptr<Group> entry_templates() const { return entry_templates_; }
282
284 void set_entry_templates(std::shared_ptr<Group> entry_templates) {
285 entry_templates_ = std::move(entry_templates);
286 }
287
289 std::time_t entry_templates_changed() const { return entry_templates_changed_; }
290
292 void set_entry_templates_changed(std::time_t time) { entry_templates_changed_ = time; }
293
295 int32_t history_max_items() const { return history_max_items_; }
296
298 void set_history_max_items(int32_t max) { history_max_items_ = max; }
299
301 int64_t history_max_size() const { return history_max_size_; }
302
304 void set_history_max_size(int64_t max) { history_max_size_ = max; }
305
307 std::weak_ptr<Group> last_selected_group() const { return last_selected_group_; }
308
310 void set_last_selected_group(std::weak_ptr<Group> group) {
311 last_selected_group_ = std::move(group);
312 }
313
315 std::weak_ptr<Group> last_visible_group() const { return last_visible_group_; }
316
318 void set_last_visible_group(std::weak_ptr<Group> group) {
319 last_visible_group_ = std::move(group);
320 }
321
323 const std::vector<std::shared_ptr<Binary>>& binaries() const { return binaries_; }
324
326 const std::vector<std::shared_ptr<Icon>>& icons() const { return icons_; }
327
329 const std::vector<Field>& fields() const { return fields_; }
330
332 const std::vector<DeletedObject>& deleted_objects() const { return deleted_objects_; }
333
335 void AddBinary(const std::shared_ptr<Binary>& binary);
336
338 void AddIcon(const std::shared_ptr<Icon>& icon);
339
341 void AddField(const std::string& key, const std::string& value);
342
344 void AddField(const Field& field);
345
347 void AddDeletedObject(const DeletedObject& object) { deleted_objects_.push_back(object); }
348};
349
350} // namespace keepass
Type representing a binary blob stored in a database.
A tombstone for a deleted group, entry or icon.
Definition metadata.hh:154
void set_deletion_time(std::time_t time)
Sets the deletion time.
Definition metadata.hh:174
bool operator!=(const DeletedObject &other) const
Inequality comparison.
Definition metadata.hh:182
bool operator==(const DeletedObject &other) const
Equality comparison.
Definition metadata.hh:177
const std::array< uint8_t, 16 > & uuid() const
Returns the UUID of the deleted object.
Definition metadata.hh:165
DeletedObject(const std::array< uint8_t, 16 > &uuid, std::time_t deletion_time)
Creates a deletion tombstone with the given UUID and deletion time.
Definition metadata.hh:161
void set_uuid(const std::array< uint8_t, 16 > &uuid)
Sets the UUID of the deleted object.
Definition metadata.hh:168
std::time_t deletion_time() const
Returns the deletion time.
Definition metadata.hh:171
An arbitrary metadata key/value field.
Definition metadata.hh:97
Field & operator=(const Field &other)=default
Copy assignment.
const std::string & key() const
Returns the field key.
Definition metadata.hh:118
void set_last_modification_time(std::optional< std::time_t > time)
Definition metadata.hh:130
Field(std::string key, std::string value)
Creates a metadata field with the given key and value.
Definition metadata.hh:105
Field(Field &&other) noexcept
Move constructor.
Definition metadata.hh:113
const std::optional< std::time_t > & last_modification_time() const
Returns the field last modification time (KDBX 4.1); empty if unset.
Definition metadata.hh:124
Field & operator=(Field &&other) noexcept
Move assignment.
Definition metadata.hh:138
const std::string & value() const
Returns the field value.
Definition metadata.hh:121
Field(const Field &other)
Copy constructor.
Definition metadata.hh:108
Flags which fields get cleared from memory when read or written.
Definition metadata.hh:54
void set_username(bool username)
Sets whether the username is memory-protected.
Definition metadata.hh:73
void set_url(bool url)
Sets whether the URL is memory-protected.
Definition metadata.hh:85
bool title() const
Returns whether the title is memory-protected.
Definition metadata.hh:64
void set_notes(bool notes)
Sets whether the notes are memory-protected.
Definition metadata.hh:91
bool password() const
Returns whether the password is memory-protected.
Definition metadata.hh:76
bool url() const
Returns whether the URL is memory-protected.
Definition metadata.hh:82
void set_title(bool title)
Sets whether the title is memory-protected.
Definition metadata.hh:67
bool username() const
Returns whether the username is memory-protected.
Definition metadata.hh:70
bool notes() const
Returns whether the notes are memory-protected.
Definition metadata.hh:88
void set_password(bool password)
Sets whether the password is memory-protected.
Definition metadata.hh:79
Metadata describing a KeePass database.
Definition metadata.hh:49
void set_master_key_change_rec(int64_t rec)
Sets days before a master key change is recommended.
Definition metadata.hh:257
void AddField(const Field &field)
Adds a metadata field with an explicit last modification time (KDBX 4.1).
std::shared_ptr< Group > entry_templates() const
Returns the entry templates group.
Definition metadata.hh:281
void set_entry_templates_changed(std::time_t time)
Sets when the entry templates were last changed.
Definition metadata.hh:292
void set_recycle_bin(std::shared_ptr< Group > bin)
Sets the recycle bin group.
Definition metadata.hh:272
const std::vector< std::shared_ptr< Binary > > & binaries() const
Returns the list of custom binaries.
Definition metadata.hh:323
MemoryProtection & memory_protection()
Returns the memory protection settings.
Definition metadata.hh:266
const std::string & generator() const
Returns the name of the application that generated the database.
Definition metadata.hh:212
const temporal< std::string > & database_desc() const
Returns the database description (with change timestamp).
Definition metadata.hh:224
std::weak_ptr< Group > last_visible_group() const
Returns the last visible group.
Definition metadata.hh:315
const std::string & database_color() const
Returns the database color.
Definition metadata.hh:242
int64_t master_key_change_rec() const
Returns days before a master key change is recommended.
Definition metadata.hh:254
void set_maintenance_hist_days(uint32_t days)
Sets the number of days to keep maintenance history.
Definition metadata.hh:239
void set_last_visible_group(std::weak_ptr< Group > group)
Sets the last visible group.
Definition metadata.hh:318
void set_history_max_items(int32_t max)
Sets the maximum number of history items kept per entry.
Definition metadata.hh:298
int64_t history_max_size() const
Returns the maximum total size of an entry's history.
Definition metadata.hh:301
void set_default_username(const temporal< std::string > &username)
Sets the default username used for new entries.
Definition metadata.hh:233
std::time_t master_key_changed() const
Returns the time the master key was last changed.
Definition metadata.hh:248
void AddDeletedObject(const DeletedObject &object)
Adds a deletion tombstone to the metadata.
Definition metadata.hh:347
void set_database_desc(const temporal< std::string > &desc)
Sets the database description.
Definition metadata.hh:227
const std::vector< Field > & fields() const
Returns the list of arbitrary metadata fields.
Definition metadata.hh:329
void AddIcon(const std::shared_ptr< Icon > &icon)
Adds a custom icon to the metadata.
const std::vector< DeletedObject > & deleted_objects() const
Returns the list of deletion tombstones (deleted groups/entries/icons).
Definition metadata.hh:332
void set_history_max_size(int64_t max)
Sets the maximum total size of an entry's history.
Definition metadata.hh:304
void set_database_color(const std::string &color)
Sets the database color.
Definition metadata.hh:245
void set_last_selected_group(std::weak_ptr< Group > group)
Sets the last selected group.
Definition metadata.hh:310
const temporal< std::string > & database_name() const
Returns the database name (with change timestamp).
Definition metadata.hh:218
int32_t history_max_items() const
Returns the maximum number of history items kept per entry.
Definition metadata.hh:295
std::time_t recycle_bin_changed() const
Returns when the recycle bin was last changed.
Definition metadata.hh:275
void AddBinary(const std::shared_ptr< Binary > &binary)
Adds a custom binary to the metadata.
uint32_t maintenance_hist_days() const
Returns the number of days to keep maintenance history.
Definition metadata.hh:236
void set_database_name(const temporal< std::string > &name)
Sets the database name.
Definition metadata.hh:221
const std::vector< std::shared_ptr< Icon > > & icons() const
Returns the list of custom icons.
Definition metadata.hh:326
void AddField(const std::string &key, const std::string &value)
Adds an arbitrary metadata field.
void set_generator(const std::string &generator)
Sets the name of the application that generated the database.
Definition metadata.hh:215
const temporal< std::string > & default_username() const
Returns the default username used for new entries.
Definition metadata.hh:230
int64_t master_key_change_force() const
Returns days before a master key change is forced.
Definition metadata.hh:260
std::weak_ptr< Group > last_selected_group() const
Returns the last selected group.
Definition metadata.hh:307
void set_master_key_changed(std::time_t time)
Sets the time the master key was last changed.
Definition metadata.hh:251
std::shared_ptr< Group > recycle_bin() const
Returns the recycle bin group.
Definition metadata.hh:269
std::time_t entry_templates_changed() const
Returns when the entry templates were last changed.
Definition metadata.hh:289
void set_entry_templates(std::shared_ptr< Group > entry_templates)
Sets the entry templates group.
Definition metadata.hh:284
void set_master_key_change_force(int64_t force)
Sets days before a master key change is forced.
Definition metadata.hh:263
void set_recycle_bin_changed(std::time_t time)
Sets when the recycle bin was last changed.
Definition metadata.hh:278
Template class for keeping track of when a variable is modified.
Definition temporal.hh:35
Visibility/export macro for the shared library.
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43
Type representing a custom database icon.
Template class for tracking modification timestamps of values.