1 | /* |
2 | Copyright (c) 2010 Laurent Montel <montel@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "ldap.h" |
21 | |
22 | #include <KConfig> |
23 | #include <KConfigGroup> |
24 | #include <KLocalizedString> |
25 | |
26 | |
27 | Ldap::Ldap( QObject *parent ) |
28 | : SetupObject( parent ) |
29 | { |
30 | } |
31 | |
32 | Ldap::~Ldap() |
33 | { |
34 | } |
35 | |
36 | void Ldap::create() |
37 | { |
38 | emit info( i18n( "Setting up LDAP server..." ) ); |
39 | |
40 | if ( m_server.isEmpty() || m_user.isEmpty() ) |
41 | return; |
42 | |
43 | const QString host = m_server; |
44 | |
45 | // Figure out the basedn |
46 | QString basedn = host; |
47 | // If the user gave a full email address, the domain name |
48 | // of that overrides the server name for the ldap dn |
49 | const QString user = m_user; |
50 | int pos = user.indexOf( QLatin1String("@" ) ); |
51 | if ( pos > 0 ) { |
52 | const QString h = user.mid( pos+1 ); |
53 | if ( !h.isEmpty() ) |
54 | // The user did type in a domain on the email address. Use that |
55 | basedn = h; |
56 | } |
57 | { // while we're here, write default domain |
58 | KConfig c( QLatin1String("kmail2rc" ) ); |
59 | KConfigGroup group = c.group( "General" ); |
60 | group.writeEntry( "Default domain" , basedn ); |
61 | } |
62 | |
63 | basedn.replace( QLatin1Char('.'), QLatin1String(",dc=" ) ); |
64 | basedn.prepend( QLatin1String("dc=" ) ); |
65 | |
66 | // Set the changes |
67 | KConfig c( QLatin1String("kabldaprc" ) ); |
68 | KConfigGroup group = c.group( "LDAP" ); |
69 | bool hasMyServer = false; |
70 | uint selHosts = group.readEntry( "NumSelectedHosts" , 0 ); |
71 | for ( uint i = 0 ; i < selHosts && !hasMyServer; ++i ) |
72 | if ( group.readEntry( QString::fromLatin1( "SelectedHost%1" ).arg( i ), QString() ) == host ) |
73 | hasMyServer = true; |
74 | if ( !hasMyServer ) { |
75 | group.writeEntry( "NumSelectedHosts" , selHosts + 1 ); |
76 | group.writeEntry( QString::fromLatin1( "SelectedHost%1" ).arg( selHosts ), host ); |
77 | group.writeEntry( QString::fromLatin1( "SelectedBase%1" ).arg( selHosts ), basedn ); |
78 | group.writeEntry( QString::fromLatin1( "SelectedPort%1" ).arg( selHosts ), "389" ); |
79 | if ( !m_authMethod.isEmpty() ) { |
80 | group.writeEntry( QString::fromLatin1( "SelectedAuth%1" ).arg( selHosts ), m_authMethod ); |
81 | group.writeEntry( QString::fromLatin1( "SelectedBind%1" ).arg( selHosts ), m_bindDn ); |
82 | group.writeEntry( QString::fromLatin1( "SelectedPwdBind%1" ).arg( selHosts ), m_password ); |
83 | } |
84 | } |
85 | emit finished( i18n( "LDAP set up." ) ); |
86 | } |
87 | |
88 | void Ldap::destroy() |
89 | { |
90 | emit info( i18n( "LDAP not configuring." ) ); |
91 | } |
92 | |
93 | void Ldap::setUser( const QString &user ) |
94 | { |
95 | m_user = user; |
96 | } |
97 | |
98 | void Ldap::setServer( const QString &server ) |
99 | { |
100 | m_server = server; |
101 | } |
102 | |
103 | void Ldap::setAuthenticationMethod(const QString& meth) |
104 | { |
105 | m_authMethod = meth; |
106 | } |
107 | |
108 | void Ldap::setBindDn(const QString& bindDn) |
109 | { |
110 | m_bindDn = bindDn; |
111 | } |
112 | |
113 | void Ldap::setPassword(const QString& password) |
114 | { |
115 | m_password = password; |
116 | } |
117 | |
118 | |