21 #include <kldap/ldapserver.h>
24 #include <KStandardDirs>
26 #include <KConfigGroup>
27 #include <KMessageBox>
32 using namespace KLDAP;
34 class LdapClientSearchConfig::Private
46 wallet->deleteLater();
51 KWallet::Wallet* wallet;
56 KConfig* LdapClientSearchConfig::config()
61 LdapClientSearchConfig::LdapClientSearchConfig(
QObject *parent)
62 :
QObject(parent), d(new LdapClientSearchConfig::Private())
66 LdapClientSearchConfig::~LdapClientSearchConfig()
71 void LdapClientSearchConfig::readConfig( KLDAP::LdapServer &server, KConfigGroup &config,
int j,
bool active )
75 prefix = QLatin1String(
"Selected");
78 const QString host = config.readEntry( prefix + QString::fromLatin1(
"Host%1" ).arg( j ),
79 QString() ).trimmed();
80 if ( !host.isEmpty() ) {
81 server.setHost( host );
84 const int port = config.readEntry( prefix + QString::fromLatin1(
"Port%1" ).arg( j ), 389 );
85 server.setPort( port );
87 const QString base = config.readEntry( prefix + QString::fromLatin1(
"Base%1" ).arg( j ),
88 QString() ).trimmed();
89 if ( !base.isEmpty() ) {
90 server.setBaseDn( KLDAP::LdapDN( base ) );
93 const QString user = config.readEntry( prefix + QString::fromLatin1(
"User%1" ).arg( j ),
94 QString() ).trimmed();
95 if ( !user.isEmpty() ) {
96 server.setUser( user );
99 const QString bindDN = config.readEntry( prefix + QString::fromLatin1(
"Bind%1" ).arg( j ), QString() ).trimmed();
100 if ( !bindDN.isEmpty() ) {
101 server.setBindDn( bindDN );
104 const QString pwdBindBNEntry = prefix + QString::fromLatin1(
"PwdBind%1" ).arg( j );
105 QString pwdBindDN = config.readEntry( pwdBindBNEntry, QString() );
106 if ( !pwdBindDN.isEmpty() ) {
107 if ( KMessageBox::Yes == KMessageBox::questionYesNo(0, i18n(
"LDAP password is stored as clear text, do you want to store it in kwallet?"),
108 i18n(
"Store clear text password in KWallet"),
109 KStandardGuiItem::yes(),
110 KStandardGuiItem::no(),
111 QLatin1String(
"DoAskToStoreToKwallet"))) {
112 d->wallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
114 connect(d->wallet, SIGNAL(walletClosed()), SLOT(slotWalletClosed()));
116 if ( !d->wallet->hasFolder( QLatin1String(
"ldapclient") ) ) {
117 d->wallet->createFolder( QLatin1String(
"ldapclient") );
119 d->wallet->setFolder( QLatin1String(
"ldapclient") );
120 d->wallet->writePassword(pwdBindBNEntry, pwdBindDN );
121 config.deleteEntry(pwdBindBNEntry);
125 server.setPassword( pwdBindDN );
127 d->wallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
130 if ( !d->wallet->setFolder( QLatin1String(
"ldapclient") ) ) {
131 d->wallet->createFolder( QLatin1String(
"ldapclient") );
132 d->wallet->setFolder( QLatin1String(
"ldapclient") );
134 d->wallet->readPassword( pwdBindBNEntry, pwdBindDN );
135 if (!pwdBindDN.isEmpty())
136 server.setPassword( pwdBindDN );
138 d->useWallet =
false;
142 server.setTimeLimit( config.readEntry( prefix + QString::fromLatin1(
"TimeLimit%1" ).arg( j ), 0 ) );
143 server.setSizeLimit( config.readEntry( prefix + QString::fromLatin1(
"SizeLimit%1" ).arg( j ), 0 ) );
144 server.setPageSize( config.readEntry( prefix + QString::fromLatin1(
"PageSize%1" ).arg( j ), 0 ) );
145 server.setVersion( config.readEntry( prefix + QString::fromLatin1(
"Version%1" ).arg( j ), 3 ) );
148 tmp = config.readEntry( prefix + QString::fromLatin1(
"Security%1" ).arg( j ),
149 QString::fromLatin1(
"None" ) );
150 server.setSecurity( KLDAP::LdapServer::None );
151 if ( tmp == QLatin1String(
"SSL") ) {
152 server.setSecurity( KLDAP::LdapServer::SSL );
153 }
else if ( tmp == QLatin1String(
"TLS") ) {
154 server.setSecurity( KLDAP::LdapServer::TLS );
157 tmp = config.readEntry( prefix + QString::fromLatin1(
"Auth%1" ).arg( j ),
158 QString::fromLatin1(
"Anonymous" ) );
159 server.setAuth( KLDAP::LdapServer::Anonymous );
160 if ( tmp == QLatin1String(
"Simple") ) {
161 server.setAuth( KLDAP::LdapServer::Simple );
162 }
else if ( tmp == QLatin1String(
"SASL") ) {
163 server.setAuth( KLDAP::LdapServer::SASL );
166 server.setMech( config.readEntry( prefix + QString::fromLatin1(
"Mech%1" ).arg( j ), QString() ) );
169 void LdapClientSearchConfig::writeConfig(
const KLDAP::LdapServer &server, KConfigGroup &config,
int j,
bool active )
173 prefix = QLatin1String(
"Selected");
176 config.writeEntry( prefix + QString::fromLatin1(
"Host%1" ).arg( j ), server.host() );
177 config.writeEntry( prefix + QString::fromLatin1(
"Port%1" ).arg( j ), server.port() );
178 config.writeEntry( prefix + QString::fromLatin1(
"Base%1" ).arg( j ), server.baseDn().toString() );
179 config.writeEntry( prefix + QString::fromLatin1(
"User%1" ).arg( j ), server.user() );
180 config.writeEntry( prefix + QString::fromLatin1(
"Bind%1" ).arg( j ), server.bindDn() );
182 const QString passwordEntry = prefix + QString::fromLatin1(
"PwdBind%1" ).arg( j );
183 const QString password = server.password();
184 if (!password.isEmpty()) {
185 if (d->useWallet && !d->wallet) {
186 d->wallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
189 d->wallet->writePassword(passwordEntry, password );
191 config.writeEntry( passwordEntry, password );
192 d->useWallet =
false;
196 config.writeEntry( prefix + QString::fromLatin1(
"TimeLimit%1" ).arg( j ), server.timeLimit() );
197 config.writeEntry( prefix + QString::fromLatin1(
"SizeLimit%1" ).arg( j ), server.sizeLimit() );
198 config.writeEntry( prefix + QString::fromLatin1(
"PageSize%1" ).arg( j ), server.pageSize() );
199 config.writeEntry( prefix + QString::fromLatin1(
"Version%1" ).arg( j ), server.version() );
201 switch ( server.security() ) {
202 case KLDAP::LdapServer::TLS:
203 tmp = QLatin1String(
"TLS");
205 case KLDAP::LdapServer::SSL:
206 tmp = QLatin1String(
"SSL");
209 tmp = QLatin1String(
"None");
211 config.writeEntry( prefix + QString::fromLatin1(
"Security%1" ).arg( j ), tmp );
212 switch ( server.auth() ) {
213 case KLDAP::LdapServer::Simple:
214 tmp = QLatin1String(
"Simple");
216 case KLDAP::LdapServer::SSL:
217 tmp = QLatin1String(
"SASL");
220 tmp = QLatin1String(
"Anonymous");
222 config.writeEntry( prefix + QString::fromLatin1(
"Auth%1" ).arg( j ), tmp );
223 config.writeEntry( prefix + QString::fromLatin1(
"Mech%1" ).arg( j ), server.mech() );
226 void LdapClientSearchConfig::slotWalletClosed()
233 #include "ldapclientsearchconfig.moc"
K_GLOBAL_STATIC_WITH_ARGS(KConfig, s_config,(QLatin1String("kabldaprc"), KConfig::NoGlobals)) KConfig *LdapClientSearchConfig