File: getsecrets_linux_kwallet.cc

package info (click to toggle)
signalbackup-tools 20250313.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,752 kB
  • sloc: cpp: 47,042; sh: 477; ansic: 399; ruby: 19; makefile: 3
file content (173 lines) | stat: -rw-r--r-- 5,766 bytes parent folder | download
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
/*
  Copyright (C) 2024  Selwin van Dijk

  This file is part of signalbackup-tools.

  signalbackup-tools 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.

  signalbackup-tools 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 signalbackup-tools.  If not, see <https://www.gnu.org/licenses/>.
*/

#if !defined(_WIN32) && !defined(__MINGW64__) && (!defined(__APPLE__) || !defined(__MACH__))

#if defined WITHOUT_DBUS

#include "desktopdatabase.ih"

void DesktopDatabase::getSecrets_linux_Kwallet(int /*version*/, std::set<std::string> */*secrets*/) const
{
  Logger::error       ("Found encrypted sqlcipher key in config file. To decrypt this key");
  Logger::error_indent("a secret must be retrieved from your keyring through dbus, but");
  Logger::error_indent("this program was explicitly compiled without dbus.");
  return;
}

#else

#include "desktopdatabase.ih"
#include "../dbuscon/dbuscon.h"

void DesktopDatabase::getSecrets_linux_Kwallet(int version, std::set<std::string> *secrets) const
{
  if (d_dbus_verbose) [[unlikely]]
    Logger::message("Getting secret from Kwallet through DBUS (version ", version, ")");

  if (!secrets)
    return;

  DBusCon dbuscon(d_dbus_verbose);
  if (!dbuscon.ok())
  {
    Logger::error("Failed to connect to dbus session");
    return;
  }

  std::string destination("org.kde.kwalletd" + std::to_string(version));
  std::string path("/modules/kwalletd" + std::to_string(version));
  std::string interface("org.kde.KWallet");

  /* GET WALLET */
  if (d_dbus_verbose) Logger::message("[networkWallet]");
  dbuscon.callMethod(destination.c_str(),
                     path.c_str(),
                     interface.c_str(),
                     "networkWallet");
  std::string walletname = dbuscon.get<std::string>("s", 0);
  if (walletname.empty())
  {
    Logger::error("Failed to get wallet name");
    return;
  }
  if (d_dbus_verbose) Logger::message(" *** Wallet name: ", walletname);

  // ON KDE THE 'open' METHOD SEEMS TO BLOCK FOR PASSWORD PROMPT BY ITSELF...
  // /* Register to wait for opening wallet */
  // if (!matchSignal("member='walletOpened'"))
  //   Logger::message("WARN: Failed to register for signal");

  /* OPEN WALLET */
  if (d_dbus_verbose) Logger::message("[open]");
  dbuscon.callMethod(destination.c_str(),
                     path.c_str(),
                     interface.c_str(),
                     "open",
                     {walletname, int64_t{0}, "signalbackup-tools"});
  int32_t handle = dbuscon.get<int32_t>("i", 0 - 1);
  if (handle < 0)
  {
    Logger::error("Failed to open wallet");
    return;
  }
  if (d_dbus_verbose) Logger::message(" *** Handle: ", handle);



  /* GET FOLDERS */
  if (d_dbus_verbose) Logger::message("[folderList]");
  dbuscon.callMethod(destination.c_str(),
                     path.c_str(),
                     interface.c_str(),
                     "folderList",
                     {handle, "signalbackup-tools"});
  std::vector<std::string> folders = dbuscon.get<std::vector<std::string>>("as", 0);
  if (folders.empty())
  {
    Logger::error("Failed to get any folders from wallet");
    return;
  }

  for (auto const &folder : folders)
  {
#if __cpp_lib_string_contains >= 202011L
    if ((folder.contains("Chrome") || folder.contains("Chromium")) &&
        (folder.contains("Safe Storage") || folder.contains("Keys")))
#else
    if ((folder.find("Chrome") != std::string::npos || folder.find("Chromium") != std::string::npos) &&
        (folder.find("Safe Storage") != std::string::npos || folder.find("Keys") != std::string::npos))
#endif
    {
      /* GET PASSWORD */
      if (d_dbus_verbose) Logger::message("[passwordList]");
      dbuscon.callMethod(destination.c_str(),
                         path.c_str(),
                         interface.c_str(),
                         "passwordList",
                         {handle, folder, "signalbackup-tools"});
      /*
        The password list returns a dict (dicts are always (in) an array as per dbus spec)
        the signature is a{sv} -> the v in our case is a string again, pretty much a map<std::string, std::string>,

        The value we want seems to have the key "Chrom[e|ium] Safe Storage"...
      */
      std::map<std::string, std::string> passwordmap = dbuscon.get<std::map<std::string, std::string>>("a{sv}", 0);

      if (passwordmap.empty())
      {
        Logger::error("Failed to get password map");
        return;
      }

      for (auto const &e : passwordmap)
        if (e.first == "Chromium Safe Storage" || e.first == "Chrome Safe Storage")
        {
          if (d_dbus_verbose) [[unlikely]]
            Logger::message(" *** SECRET: ", e.second);
          secrets->insert(e.second);
        }
    }
  }


  /* CLOSE WALLET */
  if (d_dbus_verbose) Logger::message("[close (wallet)]");
  dbuscon.callMethod(destination.c_str(),
                     path.c_str(),
                     interface.c_str(),
                     "close",
                     {walletname, false});

  /* CLOSE SESSION */
  if (d_dbus_verbose) Logger::message("[close (session)]");
  dbuscon.callMethod(destination.c_str(),
                     path.c_str(),
                     interface.c_str(),
                     "close",
                     {handle, false, "signalbackup-tools"});



  return;
}

#endif

#endif