TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

478 lines
17 KiB

  1. /*
  2. chiasmusbackend.cpp
  3. This file is part of libkleopatra, the KDE keymanagement library
  4. Copyright (c) 2005 Klarälvdalens Datakonsult AB
  5. Libkleopatra is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. Libkleopatra is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. In addition, as a special exception, the copyright holders give
  17. permission to link the code of this program with any edition of
  18. the TQt library by Trolltech AS, Norway (or with modified versions
  19. of TQt that use the same license as TQt), and distribute linked
  20. combinations including the two. You must obey the GNU General
  21. Public License in all respects for all of the code used other than
  22. TQt. If you modify this file, you may extend this exception to
  23. your version of the file, but you are not obligated to do so. If
  24. you do not wish to do so, delete this exception statement from
  25. your version.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30. #include "chiasmusbackend.h"
  31. #include "config_data.h"
  32. #include "obtainkeysjob.h"
  33. #include "chiasmusjob.h"
  34. #include "kleo/cryptoconfig.h"
  35. #include <tdelocale.h>
  36. #include <tdeconfig.h>
  37. #include <kshell.h>
  38. #include <kdebug.h>
  39. #include <tqstringlist.h>
  40. #include <tqvariant.h>
  41. #include <tqfileinfo.h>
  42. #include <map>
  43. #include <memory>
  44. #include <cassert>
  45. namespace {
  46. //
  47. // The usual TQVariant template helpers:
  48. //
  49. // to<> is a demarshaller. It's a class b/c you can't partially
  50. // specialise function templates yet. However, to<> can be used as if
  51. // it was a function: TQString s = to<TQString>( myVariant );
  52. template <typename T> class to {};
  53. #define MAKE_TO( type, func ) \
  54. template <> \
  55. class to< type > { \
  56. type m; \
  57. public: \
  58. to( const TQVariant & v ) : m( v.func() ) {} \
  59. operator type() const { return m; } \
  60. }
  61. MAKE_TO( int, toInt );
  62. MAKE_TO( unsigned int, toUInt );
  63. template <>
  64. class to<KURL> {
  65. KURL m;
  66. public:
  67. to( const TQVariant & v ) {
  68. m.setPath( v.toString() );
  69. }
  70. operator KURL() const { return m; }
  71. };
  72. template <typename T>
  73. class to< TQValueList<T> > {
  74. TQValueList<T> m;
  75. public:
  76. to( const TQVariant & v ) {
  77. const TQValueList<TQVariant> vl = v.toList();
  78. for ( TQValueList<TQVariant>::const_iterator it = vl.begin(), end = vl.end() ; it != end ; ++it )
  79. m.push_back( to<T>( *it ) );
  80. }
  81. operator TQValueList<T> () const { return m; }
  82. };
  83. template <>
  84. class to<KURL::List> {
  85. KURL::List m;
  86. public:
  87. to( const TQVariant & v ) {
  88. // wow, KURL::List is broken... it lacks conversion from and to TQVL<KURL>...
  89. m += to< TQValueList<KURL> >( v );
  90. }
  91. operator KURL::List() const { return m; }
  92. };
  93. // from<> is the demarshaller. See to<> for why this is a class...
  94. template <typename T>
  95. struct from_helper : public TQVariant {
  96. from_helper( const T & t ) : TQVariant( t ) {}
  97. };
  98. template <typename T>
  99. TQVariant from( const T & t ) {
  100. return from_helper<T>( t );
  101. }
  102. // some special types:
  103. template <> struct from_helper<KURL> : public TQVariant {
  104. from_helper( const KURL & url ) : TQVariant( url.path() ) {}
  105. };
  106. template <typename T> struct from_helper< TQValueList<T> > : public TQVariant {
  107. from_helper( const TQValueList<T> & l ) {
  108. TQValueList<TQVariant> result;
  109. for ( typename TQValueList<T>::const_iterator it = l.begin(), end = l.end() ; it != end ; ++it )
  110. result.push_back( from( *it ) );
  111. TQVariant::operator=( result );
  112. }
  113. };
  114. template <> struct from_helper<KURL::List> : public from_helper< TQValueList<KURL> > {
  115. from_helper( const KURL::List & l ) : from_helper< TQValueList<KURL> >( l ) {}
  116. };
  117. class ChiasmusConfigEntry : public Kleo::CryptoConfigEntry {
  118. unsigned int mIdx;
  119. TQVariant mValue;
  120. bool mDirty;
  121. public:
  122. ChiasmusConfigEntry( unsigned int i )
  123. : Kleo::CryptoConfigEntry(),
  124. mIdx( i ), mValue( defaultValue() ), mDirty( false )
  125. {
  126. assert( i < kleo_chiasmus_config_entries_dim );
  127. }
  128. TQString name() const { return kleo_chiasmus_config_entries[mIdx].name; }
  129. TQString description() const { return i18n( kleo_chiasmus_config_entries[mIdx].description ); }
  130. bool isOptional() const { return kleo_chiasmus_config_entries[mIdx].is_optional; }
  131. bool isReadOnly() const { return false; }
  132. bool isList() const { return kleo_chiasmus_config_entries[mIdx].is_list; }
  133. bool isRuntime() const { return kleo_chiasmus_config_entries[mIdx].is_runtime; }
  134. Level level() const { return static_cast<Level>( kleo_chiasmus_config_entries[mIdx].level ); }
  135. ArgType argType() const { return static_cast<ArgType>( kleo_chiasmus_config_entries[mIdx].type ); }
  136. bool isSet() const { return mValue != defaultValue(); }
  137. bool boolValue() const { return mValue.toBool(); }
  138. TQString stringValue() const { return mValue.toString(); }
  139. int intValue() const { return mValue.toInt(); }
  140. unsigned int uintValue() const { return mValue.toUInt(); }
  141. KURL urlValue() const {
  142. if ( argType() != ArgType_Path && argType() != ArgType_DirPath ) return KURL( mValue.toString() );
  143. KURL u; u.setPath( mValue.toString() ); return u;
  144. }
  145. unsigned int numberOfTimesSet() const { return 0; }
  146. TQStringList stringValueList() const { return mValue.toStringList(); }
  147. TQValueList<int> intValueList() const { return to< TQValueList<int> >( mValue ); }
  148. TQValueList<unsigned int> uintValueList() const { return to< TQValueList<unsigned int> >( mValue ); }
  149. KURL::List urlValueList() const {
  150. if ( argType() != ArgType_Path && argType()!= ArgType_DirPath ) return mValue.toStringList();
  151. else return to<KURL::List>( mValue ); }
  152. void resetToDefault() { mValue = defaultValue(); mDirty = false; }
  153. void setBoolValue( bool value ) { setValue( TQVariant( value ) ); }
  154. void setStringValue( const TQString & value ) { setValue( value ); }
  155. void setIntValue( int value ) { setValue( value ); }
  156. void setUIntValue( unsigned int value ) { setValue( value ); }
  157. void setURLValue( const KURL & value ) {
  158. if ( argType() != ArgType_Path && argType()!= ArgType_DirPath ) setValue( value.url() );
  159. else setValue( value.path() );
  160. }
  161. void setNumberOfTimesSet( unsigned int ) {}
  162. void setStringValueList( const TQStringList & value ) { setValue( value ); }
  163. void setIntValueList( const TQValueList<int> & l ) { setValue( from( l ) ); }
  164. void setUIntValueList( const TQValueList<unsigned int> & l ) { setValue( from( l ) ); }
  165. void setURLValueList( const KURL::List & l ) { setValue( from( l ) ); }
  166. bool isDirty() const { return mDirty; }
  167. TQVariant value() const { return mValue; }
  168. void sync( TDEConfigBase * config ) {
  169. if ( !mDirty )
  170. return;
  171. mDirty = false;
  172. config->writeEntry( kleo_chiasmus_config_entries[mIdx].name, mValue );
  173. }
  174. void read( const TDEConfigBase * config ) {
  175. mDirty = false;
  176. mValue = config->readPropertyEntry( kleo_chiasmus_config_entries[mIdx].name, defaultValue() );
  177. }
  178. private:
  179. TQVariant defaultValue() const;
  180. void setValue( const TQVariant & value ) { mValue = value; mDirty = true; }
  181. };
  182. TQVariant ChiasmusConfigEntry::defaultValue() const {
  183. const kleo_chiasmus_config_data & data = kleo_chiasmus_config_entries[mIdx];
  184. switch ( data.type ) {
  185. default:
  186. return TQVariant();
  187. case ArgType_None:
  188. if ( isList() )
  189. return TQValueList<TQVariant>() << TQVariant( data.defaults.boolean.value );
  190. else
  191. return TQVariant( data.defaults.boolean.value );
  192. case ArgType_String:
  193. if ( isList() )
  194. return TQStringList( TQString::fromLatin1( data.defaults.string ) );
  195. else
  196. return TQString::fromLatin1( data.defaults.string );
  197. case ArgType_Int:
  198. if ( isList() )
  199. return TQValueList<TQVariant>() << data.defaults.integer;
  200. else
  201. return data.defaults.integer;
  202. case ArgType_UInt:
  203. if ( isList() )
  204. return TQValueList<TQVariant>() << data.defaults.unsigned_integer;
  205. else
  206. return data.defaults.unsigned_integer;
  207. case ArgType_Path:
  208. case ArgType_DirPath:
  209. if ( isList() )
  210. return TQValueList<TQVariant>() << TQString::fromLatin1( data.defaults.path );
  211. else
  212. return TQString::fromLatin1( data.defaults.path );
  213. case ArgType_URL:
  214. case ArgType_LDAPURL:
  215. if ( isList() )
  216. return TQValueList<TQVariant>() << TQString::fromLatin1( data.defaults.url );
  217. else
  218. return TQString::fromLatin1( data.defaults.url );
  219. }
  220. }
  221. class ChiasmusGeneralGroup : public Kleo::CryptoConfigGroup {
  222. mutable std::map<TQString,ChiasmusConfigEntry*> mCache;
  223. mutable TDEConfig * mConfigObject;
  224. public:
  225. ChiasmusGeneralGroup() : Kleo::CryptoConfigGroup(), mConfigObject( 0 ) {}
  226. ~ChiasmusGeneralGroup() { clear(); delete mConfigObject; }
  227. TQString name() const { return "General"; }
  228. TQString iconName() const { return "chiasmus_chi"; }
  229. TQString description() const { return i18n( "General" ); }
  230. Kleo::CryptoConfigEntry::Level level() const { return Kleo::CryptoConfigEntry::Level_Basic; }
  231. TQStringList entryList() const {
  232. TQStringList result;
  233. for ( unsigned int i = 0 ; i < kleo_chiasmus_config_entries_dim ; ++i )
  234. result.push_back( kleo_chiasmus_config_entries[i].name );
  235. return result;
  236. }
  237. Kleo::CryptoConfigEntry * entry( const TQString & name ) const {
  238. if ( ChiasmusConfigEntry * entry = mCache[name] )
  239. return entry;
  240. const TDEConfigGroup group( configObject(), "Chiasmus" );
  241. for ( unsigned int i = 0 ; i < kleo_chiasmus_config_entries_dim ; ++i )
  242. if ( name == kleo_chiasmus_config_entries[i].name ) {
  243. ChiasmusConfigEntry * entry = new ChiasmusConfigEntry( i );
  244. entry->read( &group );
  245. return mCache[name] = entry;
  246. }
  247. return 0;
  248. }
  249. void sync() {
  250. TDEConfigGroup group( configObject(), "Chiasmus" );
  251. for ( std::map<TQString,ChiasmusConfigEntry*>::const_iterator it = mCache.begin(), end = mCache.end() ; it != end ; ++it )
  252. it->second->sync( &group );
  253. group.sync();
  254. clear();
  255. }
  256. private:
  257. TDEConfig * configObject() const {
  258. if ( !mConfigObject )
  259. // this is unsafe. We're a lib, used by concurrent apps.
  260. mConfigObject = new TDEConfig( "chiasmusbackendrc" );
  261. return mConfigObject;
  262. }
  263. void clear() {
  264. for ( std::map<TQString,ChiasmusConfigEntry*>::const_iterator it = mCache.begin(), end = mCache.end() ; it != end ; ++it )
  265. delete it->second;
  266. mCache.clear();
  267. }
  268. };
  269. class ChiasmusComponent : public Kleo::CryptoConfigComponent {
  270. mutable ChiasmusGeneralGroup * mGeneralGroup;
  271. public:
  272. ChiasmusComponent() : Kleo::CryptoConfigComponent(), mGeneralGroup( 0 ) {}
  273. ~ChiasmusComponent() { delete mGeneralGroup; }
  274. void sync() {
  275. if ( mGeneralGroup )
  276. mGeneralGroup->sync();
  277. }
  278. TQString name() const { return "Chiasmus"; }
  279. TQString iconName() const { return "chiasmus_chi"; }
  280. TQString description() const { return i18n( "Chiasmus" ); }
  281. TQStringList groupList() const { return TQStringList() << "General"; }
  282. Kleo::CryptoConfigGroup * group( const TQString & name ) const {
  283. if ( name != "General" )
  284. return 0;
  285. if ( !mGeneralGroup )
  286. mGeneralGroup = new ChiasmusGeneralGroup();
  287. return mGeneralGroup;
  288. }
  289. };
  290. }
  291. class Kleo::ChiasmusBackend::CryptoConfig : public Kleo::CryptoConfig {
  292. mutable ChiasmusComponent * mComponent;
  293. public:
  294. CryptoConfig() : Kleo::CryptoConfig(), mComponent( 0 ) {}
  295. ~CryptoConfig() { delete mComponent; }
  296. TQStringList componentList() const { return TQStringList() << "Chiasmus" ; }
  297. ChiasmusComponent * component( const TQString & name ) const {
  298. if ( name != "Chiasmus" )
  299. return 0;
  300. if ( !mComponent )
  301. mComponent = new ChiasmusComponent();
  302. return mComponent;
  303. }
  304. void sync( bool ) {
  305. if ( mComponent )
  306. mComponent->sync();
  307. }
  308. void clear() { delete mComponent; mComponent = 0; }
  309. };
  310. class Kleo::ChiasmusBackend::Protocol : public Kleo::CryptoBackend::Protocol {
  311. Kleo::CryptoConfig * mCryptoConfig;
  312. public:
  313. Protocol( Kleo::CryptoConfig * config )
  314. : Kleo::CryptoBackend::Protocol(), mCryptoConfig( config )
  315. {
  316. assert( config );
  317. }
  318. ~Protocol() {}
  319. TQString name() const { return "Chiasmus"; }
  320. TQString displayName() const { return i18n( "Chiasmus command line tool" ); }
  321. KeyListJob * keyListJob( bool, bool, bool ) const { return 0; }
  322. EncryptJob * encryptJob( bool, bool ) const { return 0; }
  323. DecryptJob * decryptJob() const { return 0; }
  324. SignJob * signJob( bool, bool ) const { return 0; }
  325. VerifyDetachedJob * verifyDetachedJob( bool ) const { return 0; }
  326. VerifyOpaqueJob * verifyOpaqueJob( bool ) const { return 0; }
  327. KeyGenerationJob * keyGenerationJob() const { return 0; }
  328. ImportJob * importJob() const { return 0; }
  329. ExportJob * publicKeyExportJob( bool ) const { return 0; }
  330. ExportJob * secretKeyExportJob( bool, const TQString& ) const { return 0; }
  331. DownloadJob * downloadJob( bool ) const { return 0; }
  332. DeleteJob * deleteJob() const { return 0; }
  333. SignEncryptJob * signEncryptJob( bool, bool ) const { return 0; }
  334. DecryptVerifyJob * decryptVerifyJob( bool ) const { return 0; }
  335. RefreshKeysJob * refreshKeysJob() const { return 0; }
  336. SpecialJob * specialJob( const char * type, const TQStringVariantMap & args ) const {
  337. if ( tqstricmp( type, "x-obtain-keys" ) == 0 && args.size() == 0 )
  338. return new ObtainKeysJob();
  339. if ( tqstricmp( type, "x-encrypt" ) == 0 && args.size() == 0 )
  340. return new ChiasmusJob( ChiasmusJob::Encrypt );
  341. if ( tqstricmp( type, "x-decrypt" ) == 0 && args.size() == 0 )
  342. return new ChiasmusJob( ChiasmusJob::Decrypt );
  343. kdDebug(5150) << "ChiasmusBackend::Protocol: tried to instantiate unknown job type \""
  344. << type << "\"" << endl;
  345. return 0;
  346. }
  347. };
  348. Kleo::ChiasmusBackend * Kleo::ChiasmusBackend::self = 0;
  349. Kleo::ChiasmusBackend::ChiasmusBackend()
  350. : Kleo::CryptoBackend(),
  351. mCryptoConfig( 0 ),
  352. mProtocol( 0 )
  353. {
  354. self = this;
  355. }
  356. Kleo::ChiasmusBackend::~ChiasmusBackend() {
  357. self = 0;
  358. delete mCryptoConfig;
  359. delete mProtocol;
  360. }
  361. TQString Kleo::ChiasmusBackend::name() const {
  362. return "Chiasmus";
  363. }
  364. TQString Kleo::ChiasmusBackend::displayName() const {
  365. return i18n( "Chiasmus" );
  366. }
  367. Kleo::CryptoConfig * Kleo::ChiasmusBackend::config() const {
  368. if ( !mCryptoConfig )
  369. mCryptoConfig = new CryptoConfig();
  370. return mCryptoConfig;
  371. }
  372. Kleo::CryptoBackend::Protocol * Kleo::ChiasmusBackend::protocol( const char * name ) const {
  373. if ( tqstricmp( name, "Chiasmus" ) != 0 )
  374. return 0;
  375. if ( !mProtocol )
  376. if ( checkForChiasmus() )
  377. mProtocol = new Protocol( config() );
  378. return mProtocol;
  379. }
  380. bool Kleo::ChiasmusBackend::checkForOpenPGP( TQString * reason ) const {
  381. if ( reason )
  382. *reason = i18n( "Unsupported protocol \"%1\"" ).arg( "OpenPGP" );
  383. return false;
  384. }
  385. bool Kleo::ChiasmusBackend::checkForSMIME( TQString * reason ) const {
  386. if ( reason )
  387. *reason = i18n( "Unsupported protocol \"%1\"" ).arg( "SMIME" );
  388. return false;
  389. }
  390. bool Kleo::ChiasmusBackend::checkForChiasmus( TQString * reason ) const {
  391. // kills the protocol instance when we return false:
  392. std::auto_ptr<Protocol> tmp( mProtocol );
  393. mProtocol = 0;
  394. const CryptoConfigEntry * path = config()->entry( "Chiasmus", "General", "path" );
  395. assert( path ); assert( path->argType() == CryptoConfigEntry::ArgType_Path );
  396. const TQString chiasmus = path->urlValue().path();
  397. const TQFileInfo fi( KShell::tildeExpand( chiasmus ) );
  398. if ( !fi.isExecutable() ) {
  399. if ( reason )
  400. *reason = i18n( "File \"%1\" does not exist or is not executable." ).arg( chiasmus );
  401. return false;
  402. }
  403. // FIXME: more checks?
  404. mProtocol = tmp.release();
  405. return true;
  406. }
  407. bool Kleo::ChiasmusBackend::checkForProtocol( const char * name, TQString * reason ) const {
  408. if ( tqstricmp( name, "Chiasmus" ) == 0 )
  409. return checkForChiasmus( reason );
  410. if ( reason )
  411. *reason = i18n( "Unsupported protocol \"%1\"" ).arg( name );
  412. return 0;
  413. }
  414. bool Kleo::ChiasmusBackend::supportsProtocol( const char * name ) const {
  415. return tqstricmp( name, "Chiasmus" ) == 0;
  416. }
  417. const char * Kleo::ChiasmusBackend::enumerateProtocols( int i ) const {
  418. return i == 0 ? "Chiasmus" : 0 ;
  419. }