libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
entry.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 <array>
27#include <cstddef>
28#include <ctime>
29#include <map>
30#include <memory>
31#include <optional>
32#include <string>
33#include <vector>
34
35#include "binary.hh"
36#include "libkeepass/export.hh"
37#include "secure.hh"
38#include "security.hh"
39#include "totp.hh"
40#include "util.hh"
41
42namespace keepass {
43
44class Group;
45class Icon;
46
55class LIBKEEPASS_API Entry final {
56public:
63 private:
64 std::string name_;
65 std::shared_ptr<Binary> binary_;
66
67 public:
69 const std::string& name() const { return name_; }
70
72 void set_name(const std::string& name) { name_ = name; }
73
75 std::shared_ptr<Binary> binary() const { return binary_; }
76
78 void set_binary(std::shared_ptr<Binary> binary) { binary_ = std::move(binary); }
79
81
85 std::string ToJson(bool include_data = false) const;
86
88 bool operator==(const Attachment& other) const {
89 return name_ == other.name_ && indirect_equal(binary_, other.binary_);
90 }
91
93 bool operator!=(const Attachment& other) const { return !(*this == other); }
94 };
95
99 class AutoType final {
100 public:
104 class Association final {
105 private:
106 std::string window_;
107 std::string sequence_;
108
109 public:
111 Association(std::string window, std::string sequence)
112 : window_(std::move(window)), sequence_(std::move(sequence)) {}
113
115 const std::string& window() const { return window_; }
116
118 const std::string& sequence() const { return sequence_; }
119
121 bool operator==(const Association& other) const {
122 return window_ == other.window_ && sequence_ == other.sequence_;
123 }
124
126 bool operator!=(const Association& other) const { return !(*this == other); }
127 };
128
129 private:
130 bool enabled_ = false;
131 uint32_t obfuscation_ = 0;
132 std::string sequence_;
133 std::vector<Association> associations_;
134
135 public:
137 bool enabled() const { return enabled_; }
138
140 void set_enabled(bool enabled) { enabled_ = enabled; }
141
143 uint32_t obfuscation() const { return obfuscation_; }
144
146 void set_obfuscation(bool obfuscation) { obfuscation_ = obfuscation; }
147
149 const std::string& sequence() const { return sequence_; }
150
152 void set_sequence(const std::string& sequence) { sequence_ = sequence; }
153
155 const std::vector<Association>& associations() const { return associations_; }
156
158 void AddAssociation(const std::string& window, const std::string& sequence) {
159 associations_.emplace_back(window, sequence);
160 }
161
163 bool operator==(const AutoType& other) const {
164 return enabled_ == other.enabled_ && obfuscation_ == other.obfuscation_ &&
165 sequence_ == other.sequence_ && associations_ == other.associations_;
166 }
167
169 bool operator!=(const AutoType& other) const { return !(*this == other); }
170 };
171
175 class Field final {
176 private:
177 std::string key_;
179
180 public:
182 Field(std::string key, protect<secure_string> value)
183 : key_(std::move(key)), value_(std::move(value)) {}
184
186 Field(const Field& other) {
187 key_ = other.key_;
188 value_ = other.value_;
189 }
190
192 Field(Field&& other) noexcept {
193 key_ = std::move(other.key_);
194 value_ = std::move(other.value_);
195 }
196
198 const std::string& key() const { return key_; }
199
201 const protect<secure_string>& value() const { return value_; }
202
204 Field& operator=(const Field& other) = default;
205
207 Field& operator=(Field&& other) noexcept {
208 key_ = std::move(other.key_);
209 value_ = std::move(other.value_);
210 return *this;
211 }
212
214 bool operator==(const Field& other) const {
215 return key_ == other.key_ && value_ == other.value_;
216 }
217
219 bool operator!=(const Field& other) const { return !(*this == other); }
220 };
221
222private:
223 std::array<uint8_t, 16> uuid_;
224 uint32_t icon_ = 0;
225 std::weak_ptr<Icon> custom_icon_;
226 std::weak_ptr<Group> parent_;
229 std::string override_url_;
230 protect<secure_string> username_;
231 protect<secure_string> password_;
233 std::string tags_;
234 bool quality_check_ = true;
235 std::optional<std::array<uint8_t, 16>> previous_parent_group_;
236 std::time_t creation_time_ = 0;
237 std::time_t modification_time_ = 0;
238 std::time_t access_time_ = 0;
239 std::time_t expiry_time_ = 0;
240 std::time_t move_time_ = 0;
241 bool expires_ = false;
242 uint32_t usage_count_ = 0;
243 std::string bg_color_;
244 std::string fg_color_;
245 AutoType auto_type_;
246 std::vector<std::shared_ptr<Attachment>> attachments_;
247 std::vector<std::shared_ptr<Entry>> history_;
248 std::vector<Field> custom_fields_;
249
250public:
253
255 const std::array<uint8_t, 16>& uuid() const { return uuid_; }
256
258 void set_uuid(const std::array<uint8_t, 16>& uuid) { uuid_ = uuid; }
259
261 uint32_t icon() const { return icon_; }
262
264 void set_icon(const uint32_t& icon) { icon_ = icon; }
265
267 std::weak_ptr<Icon> custom_icon() const { return custom_icon_; }
268
270 void set_custom_icon(std::weak_ptr<Icon> icon) { custom_icon_ = std::move(icon); }
271
273 const protect<secure_string>& title() const { return title_; }
274
276 void set_title(const protect<secure_string>& title) { title_ = title; }
277
279 const protect<secure_string>& url() const { return url_; }
280
282 void set_url(const protect<secure_string>& url) { url_ = url; }
283
285 const std::string& override_url() const { return override_url_; }
286
288 void set_override_url(const std::string& url) { override_url_ = url; }
289
291 const protect<secure_string>& username() const { return username_; }
292
294 void set_username(const protect<secure_string>& username) { username_ = username; }
295
297 const protect<secure_string>& password() const { return password_; }
298
300 void set_password(const protect<secure_string>& password) { password_ = password; }
301
303 const protect<secure_string>& notes() const { return notes_; }
304
306 void set_notes(const protect<secure_string>& notes) { notes_ = notes; }
307
309 const std::string& tags() const { return tags_; }
310
312 void set_tags(const std::string& tags) { tags_ = tags; }
313
315 bool quality_check() const { return quality_check_; }
316
318 void set_quality_check(bool quality_check) { quality_check_ = quality_check; }
319
321 const std::optional<std::array<uint8_t, 16>>& previous_parent_group() const {
322 return previous_parent_group_;
323 }
324
327 void set_previous_parent_group(const std::optional<std::array<uint8_t, 16>>& uuid) {
328 previous_parent_group_ = uuid;
329 }
330
332 std::time_t creation_time() const { return creation_time_; }
333
335 void set_creation_time(const std::time_t& time) { creation_time_ = time; }
336
338 std::time_t modification_time() const { return modification_time_; }
339
341 void set_modification_time(const std::time_t& time) { modification_time_ = time; }
342
344 std::time_t access_time() const { return access_time_; }
345
347 void set_access_time(const std::time_t& time) { access_time_ = time; }
348
350 std::time_t expiry_time() const { return expiry_time_; }
351
353 void set_expiry_time(const std::time_t& time) { expiry_time_ = time; }
354
356 std::time_t move_time() const { return move_time_; }
357
359 void set_move_time(const std::time_t& time) { move_time_ = time; }
360
362 bool expires() const { return expires_; }
363
365 void set_expires(bool expires) { expires_ = expires; }
366
368 uint32_t usage_count() const { return usage_count_; }
369
371 void set_usage_count(uint32_t usage_count) { usage_count_ = usage_count; }
372
374 const std::string& bg_color() const { return bg_color_; }
375
377 void set_bg_color(const std::string& bg_color) { bg_color_ = bg_color; }
378
380 const std::string& fg_color() const { return fg_color_; }
381
383 void set_fg_color(const std::string& fg_color) { fg_color_ = fg_color; }
384
386 AutoType& auto_type() { return auto_type_; }
387
389 const std::vector<std::shared_ptr<Attachment>>& attachments() const { return attachments_; }
390
393
399 const std::vector<std::shared_ptr<Entry>>& history() const { return history_; }
400
402 const std::vector<Field>& custom_fields() const { return custom_fields_; }
403
405 std::weak_ptr<Group> parent() const { return parent_; }
406
408 void set_parent(std::weak_ptr<Group> parent) { parent_ = std::move(parent); }
409
411 std::string path() const;
412
414 std::map<std::string, std::string> custom_properties() const;
415
417
421 std::string GetString(const std::string& key) const;
422
424 bool HasString(const std::string& key) const;
425
427 void set_custom_property(const std::string& key, const std::string& value);
428
430 void delete_custom_property(const std::string& key);
431
433 void set_binary_property(const std::string& name, const std::vector<uint8_t>& data);
434
436 std::vector<uint8_t> get_binary_property(const std::string& name) const;
437
439
444 std::shared_ptr<Binary> FindAttachment(const std::string& name) const;
445
447 std::vector<std::string> attachment_names() const;
448
450 bool HasAttachment(const std::string& name) const;
451
453 void delete_binary_property(const std::string& name);
454
457 std::optional<TotpConfig> totp() const;
458
460
464 std::string totp_code(std::time_t time = 0) const;
465
468
471
473
478 void delete_old_history(std::size_t max_items);
479
482
487 void delete_old_history(std::size_t max_items, std::size_t max_bytes);
488
490 void DeleteOldHistory(std::size_t max_items) { delete_old_history(max_items); }
491
493 void touch(bool modify = false);
494
496 void AddAttachment(const std::shared_ptr<Attachment>& attachment);
497
499 bool HasAttachment() const;
500
502 void AddHistoryEntry(const std::shared_ptr<Entry>& entry);
503
505 void AddCustomField(std::string& key, const protect<secure_string>& value);
506
509
511 bool IsMetaEntry() const;
512
514
518 std::string ToJson(bool include_attachments = false) const;
519
521 bool operator==(const Entry& other) const;
522
524 bool operator!=(const Entry& other) const;
525};
526
527} // namespace keepass
Type representing a binary blob stored in a database.
A file attachment of an entry.
Definition entry.hh:62
std::string ToJson(bool include_data=false) const
Serializes the attachment to a JSON fragment.
void set_binary(std::shared_ptr< Binary > binary)
Sets the attachment binary payload.
Definition entry.hh:78
std::shared_ptr< Binary > binary() const
Returns the attachment binary payload.
Definition entry.hh:75
bool operator!=(const Attachment &other) const
Inequality comparison.
Definition entry.hh:93
void set_name(const std::string &name)
Sets the attachment name.
Definition entry.hh:72
bool operator==(const Attachment &other) const
Equality comparison.
Definition entry.hh:88
const std::string & name() const
Returns the attachment name.
Definition entry.hh:69
Association between a target window and a keystroke sequence.
Definition entry.hh:104
const std::string & sequence() const
Returns the keystroke sequence sent to the window.
Definition entry.hh:118
bool operator==(const Association &other) const
Equality comparison.
Definition entry.hh:121
Association(std::string window, std::string sequence)
Creates an association for the given window and sequence.
Definition entry.hh:111
bool operator!=(const Association &other) const
Inequality comparison.
Definition entry.hh:126
const std::string & window() const
Returns the target window title.
Definition entry.hh:115
Keystroke-sending (auto-type) configuration for an entry.
Definition entry.hh:99
void set_obfuscation(bool obfuscation)
Sets the obfuscation level.
Definition entry.hh:146
bool operator==(const AutoType &other) const
Equality comparison.
Definition entry.hh:163
uint32_t obfuscation() const
Returns the obfuscation level.
Definition entry.hh:143
const std::string & sequence() const
Returns the default keystroke sequence.
Definition entry.hh:149
bool operator!=(const AutoType &other) const
Inequality comparison.
Definition entry.hh:169
void set_sequence(const std::string &sequence)
Sets the default keystroke sequence.
Definition entry.hh:152
void set_enabled(bool enabled)
Sets whether auto-type is enabled.
Definition entry.hh:140
const std::vector< Association > & associations() const
Returns the window/sequence associations.
Definition entry.hh:155
void AddAssociation(const std::string &window, const std::string &sequence)
Adds an association between a window and a keystroke sequence.
Definition entry.hh:158
bool enabled() const
Returns whether auto-type is enabled.
Definition entry.hh:137
An arbitrary custom string (key/value) field.
Definition entry.hh:175
Field(Field &&other) noexcept
Move constructor.
Definition entry.hh:192
Field(std::string key, protect< secure_string > value)
Creates a field with the given key and protected value.
Definition entry.hh:182
Field & operator=(Field &&other) noexcept
Move assignment.
Definition entry.hh:207
bool operator!=(const Field &other) const
Inequality comparison.
Definition entry.hh:219
Field & operator=(const Field &other)=default
Copy assignment.
const protect< secure_string > & value() const
Returns the field value.
Definition entry.hh:201
const std::string & key() const
Returns the field key.
Definition entry.hh:198
bool operator==(const Field &other) const
Equality comparison.
Definition entry.hh:214
Field(const Field &other)
Copy constructor.
Definition entry.hh:186
A single KeePass entry (a username/password record).
Definition entry.hh:55
std::weak_ptr< Group > parent() const
Returns the parent group (empty if this entry is not attached).
Definition entry.hh:405
void set_icon(const uint32_t &icon)
Sets the index of the stock icon.
Definition entry.hh:264
const std::vector< std::shared_ptr< Attachment > > & attachments() const
Returns the list of attachments.
Definition entry.hh:389
void DeleteOldHistory(std::size_t max_items)
Alias for delete_old_history(std::size_t) matching the KeePass API.
Definition entry.hh:490
const std::vector< Field > & custom_fields() const
Returns the list of custom fields.
Definition entry.hh:402
const std::vector< std::shared_ptr< Entry > > & history() const
Definition entry.hh:399
void save_history()
Saves a copy of the current state into the entry history.
std::weak_ptr< Icon > custom_icon() const
Returns a weak reference to the custom icon (if any).
Definition entry.hh:267
Entry()
Creates an empty entry.
void set_uuid(const std::array< uint8_t, 16 > &uuid)
Sets the entry's unique identifier (UUID).
Definition entry.hh:258
void set_move_time(const std::time_t &time)
Sets the last move time.
Definition entry.hh:359
void set_username(const protect< secure_string > &username)
Sets the username.
Definition entry.hh:294
const protect< secure_string > & url() const
Returns the URL.
Definition entry.hh:279
void set_expires(bool expires)
Sets whether the entry expires.
Definition entry.hh:365
std::optional< TotpConfig > totp() const
uint32_t icon() const
Returns the index of the stock icon.
Definition entry.hh:261
bool HasAttachment() const
Returns whether the entry has at least one attachment.
const protect< secure_string > & title() const
Returns the entry title.
Definition entry.hh:273
bool operator!=(const Entry &other) const
Inequality comparison.
const protect< secure_string > & password() const
Returns the password.
Definition entry.hh:297
const std::string & override_url() const
Returns the override URL.
Definition entry.hh:285
std::map< std::string, std::string > custom_properties() const
Returns the custom fields as a key/value map.
const protect< secure_string > & username() const
Returns the username.
Definition entry.hh:291
void touch(bool modify=false)
Updates the last access time (and optionally the modification time).
void AddHistoryEntry(const std::shared_ptr< Entry > &entry)
Adds a previous version of this entry to its history.
const protect< secure_string > & notes() const
Returns the notes.
Definition entry.hh:303
bool operator==(const Entry &other) const
Equality comparison.
std::time_t creation_time() const
Returns the creation time.
Definition entry.hh:332
std::vector< uint8_t > get_binary_property(const std::string &name) const
Returns the data of the binary property with the given name.
bool HasAttachment(const std::string &name) const
Returns whether an attachment with the given name exists.
bool HasString(const std::string &key) const
Returns whether the given string field exists.
void set_url(const protect< secure_string > &url)
Sets the URL.
Definition entry.hh:282
void set_title(const protect< secure_string > &title)
Sets the entry title.
Definition entry.hh:276
uint32_t usage_count() const
Returns the usage count.
Definition entry.hh:368
std::string GetString(const std::string &key) const
Returns the value of the given string field.
std::time_t expiry_time() const
Returns the expiry time.
Definition entry.hh:350
void set_fg_color(const std::string &fg_color)
Sets the foreground color.
Definition entry.hh:383
void set_override_url(const std::string &url)
Sets the override URL.
Definition entry.hh:288
void set_usage_count(uint32_t usage_count)
Sets the usage count.
Definition entry.hh:371
void set_parent(std::weak_ptr< Group > parent)
Sets the parent group.
Definition entry.hh:408
void set_access_time(const std::time_t &time)
Sets the last access time.
Definition entry.hh:347
void set_creation_time(const std::time_t &time)
Sets the creation time.
Definition entry.hh:335
std::time_t access_time() const
Returns the last access time.
Definition entry.hh:344
const std::string & fg_color() const
Returns the foreground color.
Definition entry.hh:380
bool HasNonDefaultAutoTypeSettings() const
Returns whether the auto-type settings differ from the defaults.
std::time_t move_time() const
Returns the last move time.
Definition entry.hh:356
void delete_old_history(std::size_t max_items, std::size_t max_bytes)
void set_custom_property(const std::string &key, const std::string &value)
Sets a custom field, creating it if it does not exist.
bool expires() const
Returns whether the entry expires.
Definition entry.hh:362
std::vector< std::string > attachment_names() const
Returns the names of all attachments in attachment order.
void delete_binary_property(const std::string &name)
Removes the binary property with the given name.
void set_custom_icon(std::weak_ptr< Icon > icon)
Sets the custom icon.
Definition entry.hh:270
const std::optional< std::array< uint8_t, 16 > > & previous_parent_group() const
Returns the UUID of the previous parent group (KDBX 4.1); empty if unset.
Definition entry.hh:321
bool IsMetaEntry() const
Returns whether the entry is a KeePassX metadata entry.
std::string path() const
Returns the full path of this entry including its title.
std::time_t modification_time() const
Returns the last modification time.
Definition entry.hh:338
void set_binary_property(const std::string &name, const std::vector< uint8_t > &data)
Sets (or replaces) a binary property (attachment) of this entry.
void set_quality_check(bool quality_check)
Sets whether the password quality warning is enabled for this entry.
Definition entry.hh:318
void set_previous_parent_group(const std::optional< std::array< uint8_t, 16 > > &uuid)
Definition entry.hh:327
void AddCustomField(std::string &key, const protect< secure_string > &value)
Adds a custom field with the given key and value.
bool quality_check() const
Returns whether the password quality warning is enabled for this entry.
Definition entry.hh:315
void set_password(const protect< secure_string > &password)
Sets the password.
Definition entry.hh:300
void set_tags(const std::string &tags)
Sets the tags (space-separated).
Definition entry.hh:312
std::string totp_code(std::time_t time=0) const
Generates the current TOTP code from the entry's custom fields.
void delete_history()
Clears the entry history.
void set_modification_time(const std::time_t &time)
Sets the last modification time.
Definition entry.hh:341
std::string ToJson(bool include_attachments=false) const
Serializes the entry to a JSON object.
void set_expiry_time(const std::time_t &time)
Sets the expiry time.
Definition entry.hh:353
void set_notes(const protect< secure_string > &notes)
Sets the notes.
Definition entry.hh:306
AutoType & auto_type()
Returns the auto-type configuration.
Definition entry.hh:386
void AddAttachment(const std::shared_ptr< Attachment > &attachment)
Adds an attachment to the entry.
std::shared_ptr< Binary > FindAttachment(const std::string &name) const
Returns the attachment object with the given name, or null if absent.
const std::string & bg_color() const
Returns the background color.
Definition entry.hh:374
void delete_old_history(std::size_t max_items)
Removes the oldest history items, keeping at most max_items.
void delete_custom_property(const std::string &key)
Removes a custom field.
const std::array< uint8_t, 16 > & uuid() const
Returns the entry's unique identifier (UUID).
Definition entry.hh:255
void set_bg_color(const std::string &bg_color)
Sets the background color.
Definition entry.hh:377
const std::string & tags() const
Returns the tags (space-separated).
Definition entry.hh:309
Wrapper that marks a value as sensitive and tracks the protection flag of a KeePass field.
Definition security.hh:41
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
Secure memory handling: zeroization, locked allocations and sensitive value containers.
Wrapper type for protecting sensitive KeePass field values.
Helpers for the KeePass TOTP custom fields and time-based one-time password generation.
Utility functions for clamp, comparison, traversal, and time formatting.
bool indirect_equal(const std::vector< T > &v0, const std::vector< T > &v1)
Definition util.hh:59