2 * Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 * Library General Public License for more details.
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
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include <kldap/ldapdefs.h>
26 #include "ku_groupldap.h"
29 KU_GroupLDAP::KU_GroupLDAP( KU_PrefsBase
*cfg
) : KU_Groups( cfg
)
31 if ( mCfg
->ldapssl() )
32 mUrl
.setProtocol("ldaps");
34 mUrl
.setProtocol("ldap");
36 mUrl
.setHost( mCfg
->ldaphost() );
37 mUrl
.setPort( mCfg
->ldapport() );
38 mUrl
.setDn( KLDAP::LdapDN( mCfg
->ldapgroupbase() + ',' + mCfg
->ldapdn() ) );
39 if ( !mCfg
->ldapanon() ) {
40 mUrl
.setUser( mCfg
->ldapuser() );
41 mUrl
.setPass( mCfg
->ldappassword() );
42 QString binddn
= mCfg
->ldapbinddn();
43 if ( !binddn
.isEmpty() )
44 mUrl
.setExtension( "bindname",binddn
);
46 mUrl
.setFilter( mCfg
->ldapgroupfilter() );
48 if ( mCfg
->ldaptls() ) mUrl
.setExtension("x-tls","");
49 if ( mCfg
->ldapsasl() ) {
50 mUrl
.setExtension( "x-sasl", "" );
51 mUrl
.setExtension( "x-mech", mCfg
->ldapsaslmech() );
54 mUrl
.setScope(KLDAP::LdapUrl::One
);
55 mUrl
.setExtension("x-dir","base");
57 if ( mCfg
->ldaptimelimit() )
58 mUrl
.setExtension("x-timelimit",QString::number(mCfg
->ldaptimelimit()));
59 if ( mCfg
->ldapsizelimit() )
60 mUrl
.setExtension("x-sizelimit",QString::number(mCfg
->ldapsizelimit()));
61 if ( mCfg
->ldappagesize() )
62 mUrl
.setExtension("x-pagesize",QString::number(mCfg
->ldappagesize()));
65 if ( mCfg
->ldapsam() ) {
67 domsid
= mCfg
->samdomsid();
71 KU_GroupLDAP::~KU_GroupLDAP()
75 QString
KU_GroupLDAP::getRDN( const KU_Group
&group
) const
77 switch ( mCfg
->ldapgrouprdn() ) {
78 case KU_PrefsBase::EnumLdapgrouprdn::cn
:
79 return "cn=" + group
.getName();
80 case KU_PrefsBase::EnumLdapgrouprdn::gidNumber
:
81 return "gidNumber=" + QString::number( group
.getGID() );
87 void KU_GroupLDAP::result( KLDAP::LdapSearch
*search
)
89 kDebug() << "LDAP result: " << search
->error();
92 if ( search
->error() ) {
93 mErrorString
= KLDAP::LdapConnection::errorString(search
->error());
100 void KU_GroupLDAP::data( KLDAP::LdapSearch
*, const KLDAP::LdapObject
& data
)
104 KLDAP::LdapAttrMap attrs
= data
.attributes();
105 for ( KLDAP::LdapAttrMap::ConstIterator it
= attrs
.constBegin(); it
!= attrs
.constEnd(); ++it
) {
106 QString name
= it
.key().toLower();
108 if ( name
== "objectclass" ) {
109 for ( KLDAP::LdapAttrValue::ConstIterator it2
= (*it
).constBegin(); it2
!= (*it
).constEnd(); ++it2
) {
110 if ( (*it2
).toLower() == "sambagroupmapping" )
111 group
.setCaps( KU_Group::Cap_Samba
);
116 if ( name
== "memberuid" ) {
117 for ( KLDAP::LdapAttrValue::ConstIterator it2
= (*it
).constBegin(); it2
!= (*it
).constEnd(); ++it2
) {
118 group
.addUser( (*it2
) );
123 KLDAP::LdapAttrValue values
= (*it
);
124 if ( values
.isEmpty() ) continue;
125 QString val
= QString::fromUtf8( values
.first(), values
.first().size() );
126 if ( name
== "gidnumber" )
127 group
.setGID( val
.toLong() );
128 else if ( name
== "cn" )
129 group
.setName( val
);
130 else if ( name
== "userpassword" )
132 else if ( name
== "sambasid" )
134 else if ( name
== "sambagrouptype" )
135 group
.setType( val
.toInt() );
136 else if ( name
== "displayname" )
137 group
.setDisplayName( val
);
138 else if ( name
== "description" )
139 group
.setDesc( val
);
144 if ( ( count() & 7 ) == 7 ) {
145 mProg
->setValue( mProg
->value() + mAdv
);
146 if ( mProg
->value() == 0 ) mAdv
= 1;
147 if ( mProg
->value() == mProg
->maximum()-1 ) mAdv
= -1;
151 bool KU_GroupLDAP::reload()
153 kDebug() << "KU_GroupLDAP::reload()";
154 mErrorString
= mErrorDetails
= QString();
155 mProg
= new QProgressDialog( 0 );
156 mProg
->setLabel( new QLabel (i18n("Loading Groups From LDAP")) );
157 mProg
->setAutoClose( false );
158 mProg
->setMaximum( 100 );
162 qApp
->processEvents();
164 KLDAP::LdapSearch search
;
166 SIGNAL( data( KLDAP::LdapSearch
*, const KLDAP::LdapObject
& ) ),
167 this, SLOT ( data ( KLDAP::LdapSearch
*, const KLDAP::LdapObject
&) ) );
169 SIGNAL( result( KLDAP::LdapSearch
* ) ),
170 this, SLOT ( result ( KLDAP::LdapSearch
* ) ) );
172 if (search
.search( mUrl
)) {
174 if ( mProg
->wasCanceled() ) search
.abandon();
176 kDebug() << "search failed";
178 mErrorString
= KLDAP::LdapConnection::errorString(search
.error());
179 mErrorDetails
= search
.errorString();
185 bool KU_GroupLDAP::dbcommit()
190 mErrorString
= mErrorDetails
= QString();
191 KLDAP::LdapConnection
conn( mUrl
);
193 if ( conn
.connect() != KLDAP_SUCCESS
) {
194 mErrorString
= conn
.connectionError();
198 KLDAP::LdapOperation
op( conn
);
200 if ( op
.bind_s() != KLDAP_SUCCESS
) {
201 mErrorString
= KLDAP::LdapConnection::errorString(conn
.ldapErrorCode());
202 mErrorDetails
= conn
.ldapErrorString();
205 KLDAP::LdapOperation::ModOps ops
;
207 mProg
= new QProgressDialog( 0 );
208 mProg
->setLabel( new QLabel(i18n("LDAP Operation")) );
209 mProg
->setAutoClose( false );
210 mProg
->setAutoReset( false );
211 mProg
->setMaximum( mAdd
.count() + mMod
.count() + mDel
.count() );
214 for ( KU_Groups::ModList::Iterator it
= mMod
.begin(); it
!= mMod
.end(); ++it
) {
215 QString oldrdn
= getRDN( at( it
.key() ) );
216 QString newrdn
= getRDN( it
.value() );
218 if ( oldrdn
!= newrdn
) {
219 int ret
= op
.rename_s( KLDAP::LdapDN( oldrdn
+ ',' + mUrl
.dn().toString() ),
221 mUrl
.dn().toString().toUtf8(),
223 if ( ret
!= KLDAP_SUCCESS
) {
224 mErrorString
= KLDAP::LdapConnection::errorString(conn
.ldapErrorCode());
225 mErrorDetails
= conn
.ldapErrorString();
232 createModStruct( it
.value(), it
.key(), ops
);
233 int ret
= op
.modify_s( KLDAP::LdapDN( getRDN( it
.value() ) + ',' + mUrl
.dn().toString() ), ops
);
234 if ( ret
!= KLDAP_SUCCESS
) {
235 mErrorString
= KLDAP::LdapConnection::errorString(conn
.ldapErrorCode());
236 mErrorDetails
= conn
.ldapErrorString();
240 mModSucc
.insert( it
.key(), it
.value() );
245 for ( KU_Groups::AddList::Iterator it
= mAdd
.begin(); it
!= mAdd
.end(); ++it
) {
247 createModStruct( (*it
), -1, ops
);
248 kDebug() << "add name: " << (*it
).getName();
249 int ret
= op
.add_s( KLDAP::LdapDN( getRDN( (*it
) ) + ',' + mUrl
.dn().toString() ), ops
);
250 if ( ret
!= KLDAP_SUCCESS
) {
251 mErrorString
= KLDAP::LdapConnection::errorString(conn
.ldapErrorCode());
252 mErrorDetails
= conn
.ldapErrorString();
256 mAddSucc
.append( (*it
) );
261 for ( KU_Groups::DelList::Iterator it
= mDel
.begin(); it
!= mDel
.end(); ++it
) {
262 kDebug() << "delete name: " << at((*it
)).getName();
263 int ret
= op
.del_s( KLDAP::LdapDN( getRDN( at((*it
)) ) + ',' + mUrl
.dn().toString() ) );
264 if ( ret
!= KLDAP_SUCCESS
) {
265 mErrorString
= KLDAP::LdapConnection::errorString(conn
.ldapErrorCode());
266 mErrorDetails
= conn
.ldapErrorString();
270 mDelSucc
.append( (*it
) );
278 void KU_GroupLDAP::createModStruct( const KU_Group
&group
, int oldindex
, KLDAP::LdapOperation::ModOps
&ops
)
280 QList
<QByteArray
> vals
;
281 bool mod
= ( oldindex
!= -1 );
283 vals
.append("posixgroup");
284 if ( ( getCaps() & Cap_Samba
) && ( group
.getCaps() & KU_Group::Cap_Samba
) ) {
285 vals
.append("sambagroupmapping");
287 ku_add2ops( ops
, "objectClass", vals
);
289 ku_add2ops( ops
, "cn", group
.getName().toUtf8() );
290 ku_add2ops( ops
, "gidnumber", QString::number(group
.getGID()).toUtf8() );
291 ku_add2ops( ops
, "userpassword", group
.getPwd().toUtf8() );
292 for ( uint i
=0; i
< group
.count(); i
++ ) {
293 vals
.append( group
.user(i
).toUtf8() );
295 ku_add2ops( ops
, "memberuid", vals
);
297 if ( getCaps() & Cap_Samba
) {
298 if ( group
.getCaps() & KU_Group::Cap_Samba
) {
299 ku_add2ops( ops
, "sambasid", group
.getSID().getSID().toUtf8() );
300 ku_add2ops( ops
, "displayname", group
.getDisplayName().toUtf8() );
301 ku_add2ops( ops
, "description", group
.getDesc().toUtf8() );
302 ku_add2ops( ops
, "sambagrouptype", QString::number( group
.getType() ).toUtf8() );
304 ku_add2ops( ops
, "sambasid" );
305 ku_add2ops( ops
, "displayname" );
306 ku_add2ops( ops
, "description" );
307 ku_add2ops( ops
, "sambagrouptype" );
312 #include "ku_groupldap.moc"