2 * kwallet.cpp: KWallet provider for SVN_AUTH_CRED_*
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
24 /* ==================================================================== */
34 #include <apr_pools.h>
35 #include <apr_strings.h>
37 #include <dbus/dbus.h>
38 #include <QtCore/QCoreApplication>
39 #include <QtCore/QString>
41 #include <kaboutdata.h>
42 #include <klocalizedstring.h>
46 #include "svn_config.h"
47 #include "svn_error.h"
50 #include "svn_pools.h"
51 #include "svn_string.h"
52 #include "svn_version.h"
54 #include "private/svn_auth_private.h"
56 #include "svn_private_config.h"
59 #include <kcmdlineargs.h>
60 #include <kcomponentdata.h>
63 /*-----------------------------------------------------------------------*/
64 /* KWallet simple provider, puts passwords in KWallet */
65 /*-----------------------------------------------------------------------*/
67 static int q_argc = 1;
68 static char q_argv0[] = "svn"; // Build non-const char * from string constant
69 static char *q_argv[] = { q_argv0 };
72 get_application_name(apr_hash_t *parameters,
75 svn_config_t *config =
76 static_cast<svn_config_t *> (apr_hash_get(parameters,
77 SVN_AUTH_PARAM_CONFIG_CATEGORY_CONFIG,
78 APR_HASH_KEY_STRING));
79 svn_boolean_t svn_application_name_with_pid;
80 svn_config_get_bool(config,
81 &svn_application_name_with_pid,
82 SVN_CONFIG_SECTION_AUTH,
83 SVN_CONFIG_OPTION_KWALLET_SVN_APPLICATION_NAME_WITH_PID,
85 const char *svn_application_name;
86 if (svn_application_name_with_pid)
88 svn_application_name = apr_psprintf(pool, "Subversion [%ld]", long(getpid()));
92 svn_application_name = "Subversion";
94 return svn_application_name;
98 get_wallet_name(apr_hash_t *parameters)
100 svn_config_t *config =
101 static_cast<svn_config_t *> (apr_hash_get(parameters,
102 SVN_AUTH_PARAM_CONFIG_CATEGORY_CONFIG,
103 APR_HASH_KEY_STRING));
104 const char *wallet_name;
105 svn_config_get(config,
107 SVN_CONFIG_SECTION_AUTH,
108 SVN_CONFIG_OPTION_KWALLET_WALLET,
110 if (strcmp(wallet_name, "") == 0)
112 return KWallet::Wallet::NetworkWallet();
116 return QString::fromUtf8(wallet_name);
124 const char *wid_env_string = getenv("WINDOWID");
131 err = svn_cstring_atoi64(&wid_env, wid_env_string);
133 svn_error_clear(err);
141 /* Forward definition */
143 kwallet_terminate(void *data);
145 static KWallet::Wallet *
146 get_wallet(QString wallet_name,
147 apr_hash_t *parameters)
149 KWallet::Wallet *wallet =
150 static_cast<KWallet::Wallet *> (svn_hash_gets(parameters,
152 if (! wallet && ! svn_hash_gets(parameters, "kwallet-opening-failed"))
154 wallet = KWallet::Wallet::openWallet(wallet_name, get_wid(),
155 KWallet::Wallet::Synchronous);
159 svn_hash_sets(parameters, "kwallet-wallet", wallet);
161 apr_pool_cleanup_register(apr_hash_pool_get(parameters),
162 parameters, kwallet_terminate,
163 apr_pool_cleanup_null);
165 svn_hash_sets(parameters, "kwallet-initialized", "");
169 svn_hash_sets(parameters, "kwallet-opening-failed", "");
176 kwallet_terminate(void *data)
178 apr_hash_t *parameters = static_cast<apr_hash_t *> (data);
179 if (svn_hash_gets(parameters, "kwallet-initialized"))
181 KWallet::Wallet *wallet = get_wallet(NULL, parameters);
183 svn_hash_sets(parameters, "kwallet-wallet", NULL);
184 svn_hash_sets(parameters, "kwallet-initialized", NULL);
189 /* Implementation of svn_auth__password_get_t that retrieves
190 the password from KWallet. */
192 kwallet_password_get(svn_boolean_t *done,
193 const char **password,
195 const char *realmstring,
196 const char *username,
197 apr_hash_t *parameters,
198 svn_boolean_t non_interactive,
201 QString wallet_name = get_wallet_name(parameters);
205 if (! dbus_bus_get(DBUS_BUS_SESSION, NULL))
212 if (!KWallet::Wallet::isOpen(wallet_name))
215 /* There is a race here: the wallet was open just now, but will
216 it still be open when we come to use it below? */
219 QCoreApplication *app;
223 app = new QCoreApplication(argc, q_argv);
227 KLocalizedString::setApplicationDomain("subversion"); /* translation domain */
229 /* componentName appears in KDE GUI prompts */
230 KAboutData aboutData(QStringLiteral("subversion"), /* componentName */
231 i18n(get_application_name(parameters,
232 pool)), /* displayName */
233 QStringLiteral(SVN_VER_NUMBER));
234 KAboutData::setApplicationData(aboutData);
236 KCmdLineArgs::init(q_argc, q_argv,
237 get_application_name(parameters, pool),
239 ki18n(get_application_name(parameters, pool)),
241 ki18n("Version control system"),
242 KCmdLineArgs::CmdLineArgKDE);
243 KComponentData component_data(KCmdLineArgs::aboutData());
246 QString folder = QString::fromUtf8("Subversion");
248 QString::fromUtf8(username) + "@" + QString::fromUtf8(realmstring);
249 if (! KWallet::Wallet::keyDoesNotExist(wallet_name, folder, key))
251 KWallet::Wallet *wallet = get_wallet(wallet_name, parameters);
254 if (wallet->setFolder(folder))
257 if (wallet->readPassword(key, q_password) == 0)
259 *password = apr_pstrmemdup(pool,
260 q_password.toUtf8().data(),
271 /* Implementation of svn_auth__password_set_t that stores
272 the password in KWallet. */
274 kwallet_password_set(svn_boolean_t *done,
276 const char *realmstring,
277 const char *username,
278 const char *password,
279 apr_hash_t *parameters,
280 svn_boolean_t non_interactive,
283 QString wallet_name = get_wallet_name(parameters);
287 if (! dbus_bus_get(DBUS_BUS_SESSION, NULL))
294 if (!KWallet::Wallet::isOpen(wallet_name))
297 /* There is a race here: the wallet was open just now, but will
298 it still be open when we come to use it below? */
301 QCoreApplication *app;
305 app = new QCoreApplication(argc, q_argv);
309 KLocalizedString::setApplicationDomain("subversion"); /* translation domain */
311 /* componentName appears in KDE GUI prompts */
312 KAboutData aboutData(QStringLiteral("subversion"), /* componentName */
313 i18n(get_application_name(parameters,
314 pool)), /* displayName */
315 QStringLiteral(SVN_VER_NUMBER));
316 KAboutData::setApplicationData(aboutData);
318 KCmdLineArgs::init(q_argc, q_argv,
319 get_application_name(parameters, pool),
321 ki18n(get_application_name(parameters, pool)),
323 ki18n("Version control system"),
324 KCmdLineArgs::CmdLineArgKDE);
325 KComponentData component_data(KCmdLineArgs::aboutData());
328 QString q_password = QString::fromUtf8(password);
329 QString folder = QString::fromUtf8("Subversion");
330 KWallet::Wallet *wallet = get_wallet(wallet_name, parameters);
333 if (! wallet->hasFolder(folder))
335 wallet->createFolder(folder);
337 if (wallet->setFolder(folder))
339 QString key = QString::fromUtf8(username) + "@"
340 + QString::fromUtf8(realmstring);
341 if (wallet->writePassword(key, q_password) == 0)
351 /* Get cached encrypted credentials from the simple provider's cache. */
353 kwallet_simple_first_creds(void **credentials,
355 void *provider_baton,
356 apr_hash_t *parameters,
357 const char *realmstring,
360 return svn_auth__simple_creds_cache_get(credentials,
365 kwallet_password_get,
366 SVN_AUTH__KWALLET_PASSWORD_TYPE,
370 /* Save encrypted credentials to the simple provider's cache. */
372 kwallet_simple_save_creds(svn_boolean_t *saved,
374 void *provider_baton,
375 apr_hash_t *parameters,
376 const char *realmstring,
379 return svn_auth__simple_creds_cache_set(saved, credentials,
383 kwallet_password_set,
384 SVN_AUTH__KWALLET_PASSWORD_TYPE,
388 static const svn_auth_provider_t kwallet_simple_provider = {
389 SVN_AUTH_CRED_SIMPLE,
390 kwallet_simple_first_creds,
392 kwallet_simple_save_creds
398 svn_auth_get_kwallet_simple_provider(svn_auth_provider_object_t **provider,
401 svn_auth_provider_object_t *po =
402 static_cast<svn_auth_provider_object_t *> (apr_pcalloc(pool, sizeof(*po)));
404 po->vtable = &kwallet_simple_provider;
410 /*-----------------------------------------------------------------------*/
411 /* KWallet SSL client certificate passphrase provider, */
412 /* puts passphrases in KWallet */
413 /*-----------------------------------------------------------------------*/
415 /* Get cached encrypted credentials from the ssl client cert password
418 kwallet_ssl_client_cert_pw_first_creds(void **credentials,
420 void *provider_baton,
421 apr_hash_t *parameters,
422 const char *realmstring,
425 return svn_auth__ssl_client_cert_pw_cache_get(credentials,
426 iter_baton, provider_baton,
427 parameters, realmstring,
428 kwallet_password_get,
429 SVN_AUTH__KWALLET_PASSWORD_TYPE,
433 /* Save encrypted credentials to the ssl client cert password provider's
436 kwallet_ssl_client_cert_pw_save_creds(svn_boolean_t *saved,
438 void *provider_baton,
439 apr_hash_t *parameters,
440 const char *realmstring,
443 return svn_auth__ssl_client_cert_pw_cache_set(saved, credentials,
444 provider_baton, parameters,
446 kwallet_password_set,
447 SVN_AUTH__KWALLET_PASSWORD_TYPE,
451 static const svn_auth_provider_t kwallet_ssl_client_cert_pw_provider = {
452 SVN_AUTH_CRED_SSL_CLIENT_CERT_PW,
453 kwallet_ssl_client_cert_pw_first_creds,
455 kwallet_ssl_client_cert_pw_save_creds
461 svn_auth_get_kwallet_ssl_client_cert_pw_provider
462 (svn_auth_provider_object_t **provider,
465 svn_auth_provider_object_t *po =
466 static_cast<svn_auth_provider_object_t *> (apr_pcalloc(pool, sizeof(*po)));
468 po->vtable = &kwallet_ssl_client_cert_pw_provider;