File: kwallet_dbus.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (420 lines) | stat: -rw-r--r-- 16,149 bytes parent folder | download | duplicates (5)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include "components/os_crypt/sync/kwallet_dbus.h"

#include <algorithm>
#include <memory>

#include "base/logging.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"

namespace {

// DBus service, path, and interface names for klauncher and kwalletd.
const char kKWalletDName[] = "kwalletd";
const char kKWalletD5Name[] = "kwalletd5";
const char kKWalletD6Name[] = "kwalletd6";
const char kKWalletServiceName[] = "org.kde.kwalletd";
const char kKWallet5ServiceName[] = "org.kde.kwalletd5";
const char kKWallet6ServiceName[] = "org.kde.kwalletd6";
const char kKWalletPath[] = "/modules/kwalletd";
const char kKWallet5Path[] = "/modules/kwalletd5";
const char kKWallet6Path[] = "/modules/kwalletd6";
const char kKWalletInterface[] = "org.kde.KWallet";
const char kKLauncherServiceName[] = "org.kde.klauncher";
const char kKLauncherPath[] = "/KLauncher";
const char kKLauncherInterface[] = "org.kde.KLauncher";

}  // namespace

KWalletDBus::KWalletDBus(base::nix::DesktopEnvironment desktop_env) {
  if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE6) {
    dbus_service_name_ = kKWallet6ServiceName;
    dbus_path_ = kKWallet6Path;
    kwalletd_name_ = kKWalletD6Name;
  } else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE5) {
    dbus_service_name_ = kKWallet5ServiceName;
    dbus_path_ = kKWallet5Path;
    kwalletd_name_ = kKWalletD5Name;
  } else {
    dbus_service_name_ = kKWalletServiceName;
    dbus_path_ = kKWalletPath;
    kwalletd_name_ = kKWalletDName;
  }
}

KWalletDBus::~KWalletDBus() = default;

dbus::Bus* KWalletDBus::GetSessionBus() {
  return session_bus_.get();
}

void KWalletDBus::SetSessionBus(scoped_refptr<dbus::Bus> session_bus) {
  session_bus_ = session_bus;
  kwallet_proxy_ = session_bus_->GetObjectProxy(dbus_service_name_,
                                                dbus::ObjectPath(dbus_path_));
}

bool KWalletDBus::StartKWalletd() {
  dbus::ObjectProxy* klauncher = session_bus_->GetObjectProxy(
      kKLauncherServiceName, dbus::ObjectPath(kKLauncherPath));

  dbus::MethodCall method_call(kKLauncherInterface,
                               "start_service_by_desktop_name");
  dbus::MessageWriter builder(&method_call);
  std::vector<std::string> empty;
  builder.AppendString(kwalletd_name_);  // serviceName
  builder.AppendArrayOfStrings(empty);   // urls
  builder.AppendArrayOfStrings(empty);   // envs
  builder.AppendString(std::string());   // startup_id
  builder.AppendBool(false);             // blind
  std::unique_ptr<dbus::Response> response(
      klauncher
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting klauncher to start " << kwalletd_name_;
    return false;
  }
  dbus::MessageReader reader(response.get());
  int32_t ret = -1;
  std::string dbus_name;
  std::string error;
  int32_t pid = -1;
  if (!reader.PopInt32(&ret) || !reader.PopString(&dbus_name) ||
      !reader.PopString(&error) || !reader.PopInt32(&pid)) {
    LOG(ERROR) << "Error reading response from klauncher to start "
               << kwalletd_name_ << ": " << response->ToString();
    return false;
  }
  if (!error.empty() || ret) {
    LOG(ERROR) << "Error launching " << kwalletd_name_ << ": error '" << error
               << "' (code " << ret << ")";
    return false;
  }

  return true;
}

KWalletDBus::Error KWalletDBus::IsEnabled(bool* enabled) {
  dbus::MethodCall method_call(kKWalletInterface, "isEnabled");
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (isEnabled)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  if (!reader.PopBool(enabled)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (isEnabled): " << response->ToString();
    return CANNOT_READ;
  }
  // Not enabled? Don't use KWallet. But also don't warn here.
  if (!enabled) {
    VLOG(1) << kwalletd_name_ << " reports that KWallet is not enabled.";
  }

  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::NetworkWallet(std::string* wallet_name) {
  // Get the wallet name.
  dbus::MethodCall method_call(kKWalletInterface, "networkWallet");
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (networkWallet)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  if (!reader.PopString(wallet_name)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (networkWallet): " << response->ToString();
    return CANNOT_READ;
  }

  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::Open(const std::string& wallet_name,
                                     const std::string& app_name,
                                     int* handle_ptr) {
  dbus::MethodCall method_call(kKWalletInterface, "open");
  dbus::MessageWriter builder(&method_call);
  builder.AppendString(wallet_name);  // wallet
  builder.AppendInt64(0);             // wid
  builder.AppendString(app_name);     // appid
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (open)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  if (!reader.PopInt32(handle_ptr)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (open): " << response->ToString();
    return CANNOT_READ;
  }
  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::HasEntry(const int wallet_handle,
                                         const std::string& folder_name,
                                         const std::string& key,
                                         const std::string& app_name,
                                         bool* has_entry) {
  dbus::MethodCall method_call(kKWalletInterface, "hasEntry");
  dbus::MessageWriter builder(&method_call);
  builder.AppendInt32(wallet_handle);  // handle
  builder.AppendString(folder_name);   // folder
  builder.AppendString(key);           // key
  builder.AppendString(app_name);      // appid
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (hasEntry)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  if (!reader.PopBool(has_entry)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (hasEntry): " << response->ToString();
    return CANNOT_READ;
  }
  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::EntryType(const int wallet_handle,
                                          const std::string& folder_name,
                                          const std::string& key,
                                          const std::string& app_name,
                                          Type* entry_type) {
  constexpr char kMethodName[] = "entryType";

  dbus::MethodCall method_call(kKWalletInterface, kMethodName);
  dbus::MessageWriter builder(&method_call);
  builder.AppendInt32(wallet_handle);  // handle
  builder.AppendString(folder_name);   // folder
  builder.AppendString(key);           // key
  builder.AppendString(app_name);      // appid
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (" << kMethodName
               << ")";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  int entry_type_raw;
  if (!reader.PopInt32(&entry_type_raw)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_ << " ("
               << kMethodName << "): " << response->ToString();
    return CANNOT_READ;
  }
  *entry_type = static_cast<Type>(entry_type_raw);
  if (*entry_type < Type::kUnknown || *entry_type > Type::kMaxValue) {
    *entry_type = Type::kUnknown;
  }
  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::RemoveEntry(const int wallet_handle,
                                            const std::string& folder_name,
                                            const std::string& key,
                                            const std::string& app_name,
                                            int* return_code_ptr) {
  dbus::MethodCall method_call(kKWalletInterface, "removeEntry");
  dbus::MessageWriter builder(&method_call);
  builder.AppendInt32(wallet_handle);  // handle
  builder.AppendString(folder_name);   // folder
  builder.AppendString(key);           // key
  builder.AppendString(app_name);      // appid
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (removeEntry)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  if (!reader.PopInt32(return_code_ptr)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (removeEntry): " << response->ToString();
    return CANNOT_READ;
  }
  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::HasFolder(const int handle,
                                          const std::string& folder_name,
                                          const std::string& app_name,
                                          bool* has_folder_ptr) {
  dbus::MethodCall method_call(kKWalletInterface, "hasFolder");
  dbus::MessageWriter builder(&method_call);
  builder.AppendInt32(handle);        // handle
  builder.AppendString(folder_name);  // folder
  builder.AppendString(app_name);     // appid
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (hasFolder)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  if (!reader.PopBool(has_folder_ptr)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (hasFolder): " << response->ToString();
    return CANNOT_READ;
  }
  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::CreateFolder(const int handle,
                                             const std::string& folder_name,
                                             const std::string& app_name,
                                             bool* success_ptr) {
  dbus::MethodCall method_call(kKWalletInterface, "createFolder");
  dbus::MessageWriter builder(&method_call);
  builder.AppendInt32(handle);        // handle
  builder.AppendString(folder_name);  // folder
  builder.AppendString(app_name);     // appid
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (createFolder)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  if (!reader.PopBool(success_ptr)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (createFolder): " << response->ToString();
    return CANNOT_READ;
  }
  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::WritePassword(const int handle,
                                              const std::string& folder_name,
                                              const std::string& key,
                                              const std::string& password,
                                              const std::string& app_name,
                                              bool* const write_success_ptr) {
  dbus::MethodCall method_call(kKWalletInterface, "writePassword");
  dbus::MessageWriter builder(&method_call);
  builder.AppendInt32(handle);
  builder.AppendString(folder_name);
  builder.AppendString(key);
  builder.AppendString(password);
  builder.AppendString(app_name);
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (writePassword)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  int return_code;
  if (!reader.PopInt32(&return_code)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (writePassword): " << response->ToString();
    return CANNOT_READ;
  }
  *write_success_ptr = return_code == 0;
  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::ReadPassword(
    const int handle,
    const std::string& folder_name,
    const std::string& key,
    const std::string& app_name,
    std::optional<std::string>* const password_ptr) {
  dbus::MethodCall method_call(kKWalletInterface, "readPassword");
  dbus::MessageWriter builder(&method_call);
  builder.AppendInt32(handle);
  builder.AppendString(folder_name);
  builder.AppendString(key);
  builder.AppendString(app_name);
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (readPassword)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  std::string password;
  if (!reader.PopString(&password)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (readPassword): " << response->ToString();
    *password_ptr = std::nullopt;
    return CANNOT_READ;
  }
  *password_ptr = std::move(password);
  return SUCCESS;
}

KWalletDBus::Error KWalletDBus::Close(const int handle,
                                      const bool force,
                                      const std::string& app_name,
                                      bool* success_ptr) {
  dbus::MethodCall method_call(kKWalletInterface, "close");
  dbus::MessageWriter builder(&method_call);
  builder.AppendInt32(handle);
  builder.AppendBool(force);
  builder.AppendString(app_name);
  std::unique_ptr<dbus::Response> response(
      kwallet_proxy_
          ->CallMethodAndBlock(&method_call,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
          .value_or(nullptr));
  if (!response) {
    LOG(ERROR) << "Error contacting " << kwalletd_name_ << " (close)";
    return CANNOT_CONTACT;
  }
  dbus::MessageReader reader(response.get());
  int return_code = 1;
  if (!reader.PopInt32(&return_code)) {
    LOG(ERROR) << "Error reading response from " << kwalletd_name_
               << " (close): " << response->ToString();
    return CANNOT_READ;
  }
  *success_ptr = return_code == 0;
  return SUCCESS;
}