summaryrefslogtreecommitdiff
path: root/src/backends/tde/TDEPlatform.cpp
blob: d262f57ed5a7de9696208c827a25047acb39f7be (plain)
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
/*
 * Copyright (C) 2016 Emanoil Kotsev emanoil.kotsev@fincom.at
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 *
 *
 * $Id: TDEPlatform.cpp,v 1.5 2016/09/12 19:57:42 emanoil Exp $
 *
 */

/*
*
* WARNING This code is untested! It is based on theory. Feedback is welcome!
*
*/

#include <config.h>

#ifdef ENABLE_TDEWALLET

#include "TDEPlatform.h"

#include <syncevo/Exception.h>
#include <syncevo/UserInterface.h>
#include <syncevo/SyncConfig.h>

#include <tdeapplication.h>
#include <tdeaboutdata.h>
#include <tdecmdlineargs.h>

#include <tdewallet.h>

#include <syncevo/declarations.h>
SE_BEGIN_CXX

// TODO: this check should be global
// static bool HaveDBus;

void TDEInitMainSlot(const char *appname)
{
	
	int argc = 1;
	static char *argv[] = { const_cast<char *>(appname), NULL };

	TDEAboutData aboutData( "syncevotdewlt", // internal program name
		"SyncEvolution-TDEPIM-plugin",              // displayable program name.
		"0.1",                               // version string
		"SyncEvolution TDEPIM plugin",            // short porgram description
		TDEAboutData::License_GPL,             // license type
		"(c) 2016, emanoil.kotsev@fincom.at" // copyright statement
	);

	TDECmdLineArgs::init(argc, argv, &aboutData);
	
	TDEApplication syncevotdewallet( "syncevolution-tdewallet" );
	syncevotdewlt.dcopClient()->registerAs(syncevotdewallet.name());
	
}

static bool UseTDEWallet(const InitStateTri &keyring,
                       int slotCount)
{
	// Disabled by user?
	if (keyring.getValue() == InitStateTri::VALUE_FALSE) {
		return false;
	}
	
	// When both (presumably) GNOME keyring and TDE Wallet are available,
	// check if the user really wanted TDE Wallet before using TDE Wallet
	// instead of GNOME keyring. This default favors GNOME keyring
	// over TDE Wallet because SyncEvolution traditionally used that.
	if (keyring.getValue() == InitStateTri::VALUE_TRUE &&
		slotCount > 1) {
		return false;
	}
	
	// If explicitly selected, it must be us.
	if (keyring.getValue() == InitStateTri::VALUE_STRING &&
		!boost::iequals(keyring.get(), "TDE")) {
		return false;
	}
	
	// Use KWallet.
	return true;
}

/**
 * Here we use server sync url without protocol prefix and
 * user account name as the key in the keyring.
 *
 * Also since the KWallet's API supports only storing (key,password)
 * or Map<TQString,TQString> , the former is used.
 */
bool TDEWalletLoadPasswordSlot(const InitStateTri &keyring,
			const std::string &passwordName,
			const std::string &descr,
			const ConfigPasswordKey &key,
			InitStateString &password)
{
	if (!UseTDEWallet(keyring,
			GetLoadPasswordSignal().num_slots() - INTERNAL_LOAD_PASSWORD_SLOTS)) {
		SE_LOG_DEBUG(NULL, "not using TDE Wallet");
		return false;
	}
	
	TQString walletPassword;
	TQString walletKey = TQString(key.user.c_str()) + ',' +
		TQString(key.domain.c_str())+ ','+
		TQString(key.server.c_str())+','+
		TQString(key.object.c_str())+','+
		TQString(key.protocol.c_str())+','+
		TQString(key.authtype.c_str())+','+
		TQString::number(key.port);
	
	TQString wallet_name = TDEWallet::Wallet::NetworkWallet();
	
	const TQString folder("Syncevolution");
	
	bool found = false;
	if (!TDEWallet::Wallet::keyDoesNotExist(wallet_name, folder, walletKey)) {
		TDEWallet::Wallet *wallet = TDEWallet::Wallet::openWallet(wallet_name, -1, TDEWallet::Wallet::Synchronous);
		if ( wallet &&
			wallet->setFolder(folder) &&
			wallet->readPassword(walletKey, walletPassword) == 0 ) {
			std::string text1(walletPassword.utf8(),walletPassword.utf8().length());
			password = text1;
			found = true;
		}
	}
	SE_LOG_DEBUG(NULL, "%s password in KWallet using %s",
			found ? "found" : "no",
			key.toString().c_str());
	
	return true;
}
	
	
bool TDEWalletSavePasswordSlot(const InitStateTri &keyring,
			const std::string &passwordName,
			const std::string &password,
			const ConfigPasswordKey &key)
{
	if (!UseTDEWallet(keyring,
			GetSavePasswordSignal().num_slots() - INTERNAL_SAVE_PASSWORD_SLOTS)) {
		SE_LOG_DEBUG(NULL, "not using TDE Wallet");
		return false;
	}
	
	/*
	* It is possible to let CmdlineSyncClient decide which 
	* fields in ConfigPasswordKey it would use
	* but currently only use passed key instead 
	*/
	
	// write password to keyring
	std::string s = key.user + ',' + key.domain + ',' + key.server + ',' + key.object + ',' + key.protocol + ',' + key.authtype + ',';
	const TQString walletKey =TQString::fromUtf8(s.data(),s.size()) + TQString::number(key.port);
	const TQString walletPassword = TQString::fromUtf8(password.data(),password.size());
	
	bool write_success = false;
	const TQString wallet_name = TDEWallet::Wallet::NetworkWallet();
	const TQString folder ("Syncevolution");
	TDEWallet::Wallet *wallet = TDEWallet::Wallet::openWallet(wallet_name, -1,
								TDEWallet::Wallet::Synchronous);
	if (wallet) {
		if (!wallet->hasFolder(folder)) {
			wallet->createFolder(folder);
		}
	
		if (wallet->setFolder(folder) &&
		    wallet->writePassword(walletKey, walletPassword) == 0) {
			write_success = true;
		}
	}
	
	if (!write_success) {
		Exception::throwError(SE_HERE, "Saving " + passwordName + " in TDE Wallet failed.");
	}
	SE_LOG_DEBUG(NULL, "stored password in KWallet using %s", key.toString().c_str());
	return write_success;
}

SE_END_CXX

#endif // ENABLE_TDEWALLET