Proof-reading - fixed one usage of the i18n plural form (it wasn't doing before,...
[kdeadmin.git] / kuser / ku_groupldap.cpp
blob47132efd76f989dbab51e2b9eb80b99e33f21316
1 /*
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.
18 **/
20 #include <QLabel>
22 #include <kdebug.h>
23 #include <klocale.h>
24 #include <kldap/ldapdefs.h>
26 #include "ku_groupldap.h"
27 #include "ku_misc.h"
29 KU_GroupLDAP::KU_GroupLDAP( KU_PrefsBase *cfg ) : KU_Groups( cfg )
31 if ( mCfg->ldapssl() )
32 mUrl.setProtocol("ldaps");
33 else
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()));
64 caps = Cap_Passwd;
65 if ( mCfg->ldapsam() ) {
66 caps |= Cap_Samba;
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() );
82 default:
83 return "";
87 void KU_GroupLDAP::result( KLDAP::LdapSearch *search )
89 kDebug() << "LDAP result: " << search->error();
90 mProg->hide();
92 if ( search->error() ) {
93 mErrorString = KLDAP::LdapConnection::errorString(search->error());
94 mOk = false;
95 } else {
96 mOk = true;
100 void KU_GroupLDAP::data( KLDAP::LdapSearch *, const KLDAP::LdapObject& data )
102 KU_Group group;
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 );
113 continue;
116 if ( name == "memberuid" ) {
117 for ( KLDAP::LdapAttrValue::ConstIterator it2 = (*it).constBegin(); it2 != (*it).constEnd(); ++it2 ) {
118 group.addUser( (*it2) );
120 continue;
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" )
131 group.setPwd( val );
132 else if ( name == "sambasid" )
133 group.setSID( val );
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 );
142 append( group );
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 );
159 mAdv = 1;
160 mOk = true;
161 mProg->show();
162 qApp->processEvents();
164 KLDAP::LdapSearch search;
165 connect( &search,
166 SIGNAL( data( KLDAP::LdapSearch*, const KLDAP::LdapObject& ) ),
167 this, SLOT ( data ( KLDAP::LdapSearch*, const KLDAP::LdapObject&) ) );
168 connect( &search,
169 SIGNAL( result( KLDAP::LdapSearch* ) ),
170 this, SLOT ( result ( KLDAP::LdapSearch* ) ) );
172 if (search.search( mUrl )) {
173 mProg->exec();
174 if ( mProg->wasCanceled() ) search.abandon();
175 } else {
176 kDebug() << "search failed";
177 mOk = false;
178 mErrorString = KLDAP::LdapConnection::errorString(search.error());
179 mErrorDetails = search.errorString();
181 delete mProg;
182 return( mOk );
185 bool KU_GroupLDAP::dbcommit()
187 mAddSucc.clear();
188 mDelSucc.clear();
189 mModSucc.clear();
190 mErrorString = mErrorDetails = QString();
191 KLDAP::LdapConnection conn( mUrl );
193 if ( conn.connect() != KLDAP_SUCCESS ) {
194 mErrorString = conn.connectionError();
195 return false;
198 KLDAP::LdapOperation op( conn );
200 if ( op.bind_s() != KLDAP_SUCCESS ) {
201 mErrorString = KLDAP::LdapConnection::errorString(conn.ldapErrorCode());
202 mErrorDetails = conn.ldapErrorString();
203 return false;
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() );
213 //modify
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() ),
220 newrdn,
221 mUrl.dn().toString().toUtf8(),
222 true );
223 if ( ret != KLDAP_SUCCESS ) {
224 mErrorString = KLDAP::LdapConnection::errorString(conn.ldapErrorCode());
225 mErrorDetails = conn.ldapErrorString();
226 delete mProg;
227 return false;
231 ops.clear();
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();
237 delete mProg;
238 return false;
239 } else {
240 mModSucc.insert( it.key(), it.value() );
244 //add
245 for ( KU_Groups::AddList::Iterator it = mAdd.begin(); it != mAdd.end(); ++it ) {
246 ops.clear();
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();
253 delete mProg;
254 return false;
255 } else {
256 mAddSucc.append( (*it) );
260 //del
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();
267 delete mProg;
268 return false;
269 } else {
270 mDelSucc.append( (*it) );
274 delete mProg;
275 return true;
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 );
288 vals.clear();
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 );
296 vals.clear();
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() );
303 } else if (mod) {
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"