| 1 |
// $OpenLDAP$ |
| 2 |
/* |
| 3 |
* Copyright 2000-2024 The OpenLDAP Foundation, All Rights Reserved. |
| 4 |
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "debug.h" |
| 8 |
|
| 9 |
#include "LDAPResult.h" |
| 10 |
#include "LDAPException.h" |
| 11 |
#include "LDAPUrlList.h" |
| 12 |
|
| 13 |
#include "LDAPConnection.h" |
| 14 |
const int LDAPConnection::SEARCH_BASE = LDAPAsynConnection::SEARCH_BASE; |
| 15 |
const int LDAPConnection::SEARCH_ONE = LDAPAsynConnection::SEARCH_ONE; |
| 16 |
const int LDAPConnection::SEARCH_SUB = LDAPAsynConnection::SEARCH_SUB; |
| 17 |
|
| 18 |
using namespace std; |
| 19 |
|
| 20 |
LDAPConnection::LDAPConnection(const string& hostname, int port, |
| 21 |
LDAPConstraints* cons) : |
| 22 |
LDAPAsynConnection(hostname, port, cons){ |
| 23 |
} |
| 24 |
|
| 25 |
LDAPConnection::~LDAPConnection(){ |
| 26 |
} |
| 27 |
|
| 28 |
void LDAPConnection::start_tls(){ |
| 29 |
LDAPAsynConnection::start_tls(); |
| 30 |
} |
| 31 |
|
| 32 |
void LDAPConnection::bind(const string& dn, const string& passwd, |
| 33 |
LDAPConstraints* cons){ |
| 34 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::bind" << endl); |
| 35 |
LDAPMessageQueue* msg=0; |
| 36 |
LDAPResult* res=0; |
| 37 |
try{ |
| 38 |
msg = LDAPAsynConnection::bind(dn,passwd,cons); |
| 39 |
res = (LDAPResult*)msg->getNext(); |
| 40 |
}catch(LDAPException e){ |
| 41 |
delete msg; |
| 42 |
delete res; |
| 43 |
throw; |
| 44 |
} |
| 45 |
int resCode=res->getResultCode(); |
| 46 |
if(resCode != LDAPResult::SUCCESS) { |
| 47 |
if(resCode == LDAPResult::REFERRAL){ |
| 48 |
LDAPUrlList urls = res->getReferralUrls(); |
| 49 |
delete res; |
| 50 |
delete msg; |
| 51 |
throw LDAPReferralException(urls); |
| 52 |
}else{ |
| 53 |
string srvMsg = res->getErrMsg(); |
| 54 |
delete res; |
| 55 |
delete msg; |
| 56 |
throw LDAPException(resCode, srvMsg); |
| 57 |
} |
| 58 |
} |
| 59 |
delete res; |
| 60 |
delete msg; // memcheck |
| 61 |
} |
| 62 |
|
| 63 |
void LDAPConnection::saslInteractiveBind( const std::string &mech, |
| 64 |
int flags, |
| 65 |
SaslInteractionHandler *sih, |
| 66 |
const LDAPConstraints *cons) |
| 67 |
{ |
| 68 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::bind" << endl); |
| 69 |
LDAPMessageQueue* msg=0; |
| 70 |
LDAPResult* res=0; |
| 71 |
try{ |
| 72 |
msg = LDAPAsynConnection::saslInteractiveBind(mech, flags, sih, cons); |
| 73 |
res = (LDAPResult*)msg->getNext(); |
| 74 |
}catch(LDAPException e){ |
| 75 |
delete msg; |
| 76 |
delete res; |
| 77 |
throw; |
| 78 |
} |
| 79 |
int resCode=res->getResultCode(); |
| 80 |
if(resCode != LDAPResult::SUCCESS) { |
| 81 |
if(resCode == LDAPResult::REFERRAL){ |
| 82 |
LDAPUrlList urls = res->getReferralUrls(); |
| 83 |
delete res; |
| 84 |
delete msg; |
| 85 |
throw LDAPReferralException(urls); |
| 86 |
}else{ |
| 87 |
string srvMsg = res->getErrMsg(); |
| 88 |
delete res; |
| 89 |
delete msg; |
| 90 |
throw LDAPException(resCode, srvMsg); |
| 91 |
} |
| 92 |
} |
| 93 |
delete res; |
| 94 |
delete msg; |
| 95 |
} |
| 96 |
|
| 97 |
void LDAPConnection::unbind(){ |
| 98 |
LDAPAsynConnection::unbind(); |
| 99 |
} |
| 100 |
|
| 101 |
bool LDAPConnection::compare(const string& dn, const LDAPAttribute& attr, |
| 102 |
LDAPConstraints* cons){ |
| 103 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::compare" << endl); |
| 104 |
LDAPMessageQueue* msg=0; |
| 105 |
LDAPResult* res=0; |
| 106 |
try{ |
| 107 |
msg = LDAPAsynConnection::compare(dn,attr,cons); |
| 108 |
res = (LDAPResult*)msg->getNext(); |
| 109 |
}catch(LDAPException e){ |
| 110 |
delete msg; |
| 111 |
delete res; |
| 112 |
throw; |
| 113 |
} |
| 114 |
int resCode=res->getResultCode(); |
| 115 |
switch (resCode){ |
| 116 |
case LDAPResult::COMPARE_TRUE : |
| 117 |
delete res; |
| 118 |
delete msg; |
| 119 |
return true; |
| 120 |
break; |
| 121 |
case LDAPResult::COMPARE_FALSE : |
| 122 |
delete res; |
| 123 |
delete msg; |
| 124 |
return false; |
| 125 |
break; |
| 126 |
case LDAPResult::REFERRAL : |
| 127 |
{ |
| 128 |
LDAPUrlList urls = res->getReferralUrls(); |
| 129 |
delete res; |
| 130 |
delete msg; |
| 131 |
throw LDAPReferralException(urls); |
| 132 |
} |
| 133 |
break; |
| 134 |
default : |
| 135 |
string srvMsg = res->getErrMsg(); |
| 136 |
delete res; |
| 137 |
delete msg; |
| 138 |
throw LDAPException(resCode, srvMsg); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void LDAPConnection::del(const string& dn, const LDAPConstraints* cons){ |
| 143 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::del" << endl); |
| 144 |
LDAPMessageQueue* msg=0; |
| 145 |
LDAPResult* res=0; |
| 146 |
try{ |
| 147 |
msg = LDAPAsynConnection::del(dn,cons); |
| 148 |
res = (LDAPResult*)msg->getNext(); |
| 149 |
}catch(LDAPException e){ |
| 150 |
delete msg; |
| 151 |
delete res; |
| 152 |
throw; |
| 153 |
} |
| 154 |
int resCode=res->getResultCode(); |
| 155 |
switch (resCode){ |
| 156 |
case LDAPResult::SUCCESS : |
| 157 |
delete res; |
| 158 |
delete msg; |
| 159 |
break; |
| 160 |
case LDAPResult::REFERRAL : |
| 161 |
{ |
| 162 |
LDAPUrlList urls = res->getReferralUrls(); |
| 163 |
delete res; |
| 164 |
delete msg; |
| 165 |
throw LDAPReferralException(urls); |
| 166 |
} |
| 167 |
break; |
| 168 |
default : |
| 169 |
string srvMsg = res->getErrMsg(); |
| 170 |
delete res; |
| 171 |
delete msg; |
| 172 |
throw LDAPException(resCode, srvMsg); |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
void LDAPConnection::add(const LDAPEntry* le, const LDAPConstraints* cons){ |
| 178 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::add" << endl); |
| 179 |
LDAPMessageQueue* msg=0; |
| 180 |
LDAPResult* res=0; |
| 181 |
try{ |
| 182 |
msg = LDAPAsynConnection::add(le,cons); |
| 183 |
res = (LDAPResult*)msg->getNext(); |
| 184 |
}catch(LDAPException e){ |
| 185 |
delete msg; |
| 186 |
delete res; |
| 187 |
throw; |
| 188 |
} |
| 189 |
int resCode=res->getResultCode(); |
| 190 |
switch (resCode){ |
| 191 |
case LDAPResult::SUCCESS : |
| 192 |
delete res; |
| 193 |
delete msg; |
| 194 |
break; |
| 195 |
case LDAPResult::REFERRAL : |
| 196 |
{ |
| 197 |
LDAPUrlList urls = res->getReferralUrls(); |
| 198 |
delete res; |
| 199 |
delete msg; |
| 200 |
throw LDAPReferralException(urls); |
| 201 |
} |
| 202 |
break; |
| 203 |
default : |
| 204 |
string srvMsg = res->getErrMsg(); |
| 205 |
delete res; |
| 206 |
delete msg; |
| 207 |
throw LDAPException(resCode, srvMsg); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void LDAPConnection::modify(const string& dn, const LDAPModList* mods, |
| 212 |
const LDAPConstraints* cons){ |
| 213 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::modify" << endl); |
| 214 |
LDAPMessageQueue* msg=0; |
| 215 |
LDAPResult* res=0; |
| 216 |
try{ |
| 217 |
msg = LDAPAsynConnection::modify(dn,mods,cons); |
| 218 |
res = (LDAPResult*)msg->getNext(); |
| 219 |
}catch(LDAPException e){ |
| 220 |
delete msg; |
| 221 |
delete res; |
| 222 |
throw; |
| 223 |
} |
| 224 |
int resCode=res->getResultCode(); |
| 225 |
switch (resCode){ |
| 226 |
case LDAPResult::SUCCESS : |
| 227 |
delete res; |
| 228 |
delete msg; |
| 229 |
break; |
| 230 |
case LDAPResult::REFERRAL : |
| 231 |
{ |
| 232 |
LDAPUrlList urls = res->getReferralUrls(); |
| 233 |
delete res; |
| 234 |
delete msg; |
| 235 |
throw LDAPReferralException(urls); |
| 236 |
} |
| 237 |
break; |
| 238 |
default : |
| 239 |
string srvMsg = res->getErrMsg(); |
| 240 |
delete res; |
| 241 |
delete msg; |
| 242 |
throw LDAPException(resCode, srvMsg); |
| 243 |
} |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
void LDAPConnection::rename(const string& dn, const string& newRDN, |
| 248 |
bool delOldRDN, const string& newParentDN, |
| 249 |
const LDAPConstraints* cons){ |
| 250 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::rename" << endl); |
| 251 |
LDAPMessageQueue* msg=0; |
| 252 |
LDAPResult* res=0; |
| 253 |
try{ |
| 254 |
msg = LDAPAsynConnection::rename(dn,newRDN,delOldRDN, newParentDN, |
| 255 |
cons); |
| 256 |
res = (LDAPResult*)msg->getNext(); |
| 257 |
}catch(LDAPException e){ |
| 258 |
delete msg; |
| 259 |
delete res; |
| 260 |
throw; |
| 261 |
} |
| 262 |
int resCode=res->getResultCode(); |
| 263 |
switch (resCode){ |
| 264 |
case LDAPResult::SUCCESS : |
| 265 |
delete res; |
| 266 |
delete msg; |
| 267 |
break; |
| 268 |
case LDAPResult::REFERRAL : |
| 269 |
{ |
| 270 |
LDAPUrlList urls = res->getReferralUrls(); |
| 271 |
delete res; |
| 272 |
delete msg; |
| 273 |
throw LDAPReferralException(urls); |
| 274 |
} |
| 275 |
break; |
| 276 |
default : |
| 277 |
string srvMsg = res->getErrMsg(); |
| 278 |
delete res; |
| 279 |
delete msg; |
| 280 |
throw LDAPException(resCode, srvMsg); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
LDAPSearchResults* LDAPConnection::search(const string& base, int scope, |
| 285 |
const string& filter, const StringList& attrs, bool attrsOnly, |
| 286 |
const LDAPConstraints* cons){ |
| 287 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::search" << endl); |
| 288 |
LDAPMessageQueue* msgq=0; |
| 289 |
LDAPResult* res=0; |
| 290 |
LDAPSearchResults* results= 0; |
| 291 |
|
| 292 |
try{ |
| 293 |
results = new LDAPSearchResults(); |
| 294 |
msgq = LDAPAsynConnection::search(base,scope, filter, attrs, attrsOnly, |
| 295 |
cons); |
| 296 |
res = results->readMessageQueue(msgq); |
| 297 |
}catch(LDAPException e){ |
| 298 |
delete results; // memcheck |
| 299 |
delete msgq; |
| 300 |
throw; |
| 301 |
} |
| 302 |
if(res != 0){ |
| 303 |
int resCode=res->getResultCode(); |
| 304 |
switch (resCode){ |
| 305 |
case LDAPResult::SUCCESS : |
| 306 |
delete res; |
| 307 |
delete msgq; |
| 308 |
return results; |
| 309 |
break; |
| 310 |
case LDAPResult::REFERRAL : |
| 311 |
{ |
| 312 |
LDAPUrlList urls = res->getReferralUrls(); |
| 313 |
delete results; // memcheck |
| 314 |
delete res; |
| 315 |
delete msgq; |
| 316 |
throw LDAPReferralException(urls); |
| 317 |
} |
| 318 |
break; |
| 319 |
default : |
| 320 |
string srvMsg = res->getErrMsg(); |
| 321 |
delete results; // memcheck |
| 322 |
delete res; |
| 323 |
delete msgq; |
| 324 |
throw LDAPException(resCode, srvMsg); |
| 325 |
} |
| 326 |
} |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
LDAPExtResult* LDAPConnection::extOperation(const string& oid, |
| 331 |
const string& value, const LDAPConstraints *cons){ |
| 332 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::extOperation" << endl); |
| 333 |
LDAPMessageQueue* msg=0; |
| 334 |
LDAPExtResult* res=0; |
| 335 |
try{ |
| 336 |
msg = LDAPAsynConnection::extOperation(oid,value,cons); |
| 337 |
res = (LDAPExtResult*)msg->getNext(); |
| 338 |
}catch(LDAPException e){ |
| 339 |
delete msg; |
| 340 |
delete res; |
| 341 |
throw; |
| 342 |
} |
| 343 |
int resCode=res->getResultCode(); |
| 344 |
switch (resCode){ |
| 345 |
case LDAPResult::SUCCESS : |
| 346 |
delete msg; |
| 347 |
return res; |
| 348 |
case LDAPResult::REFERRAL : |
| 349 |
{ |
| 350 |
LDAPUrlList urls = res->getReferralUrls(); |
| 351 |
delete res; |
| 352 |
delete msg; |
| 353 |
throw LDAPReferralException(urls); |
| 354 |
} |
| 355 |
break; |
| 356 |
default : |
| 357 |
string srvMsg = res->getErrMsg(); |
| 358 |
delete res; |
| 359 |
delete msg; |
| 360 |
throw LDAPException(resCode, srvMsg); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
const string& LDAPConnection::getHost() const{ |
| 365 |
return LDAPAsynConnection::getHost(); |
| 366 |
} |
| 367 |
|
| 368 |
int LDAPConnection::getPort() const{ |
| 369 |
return LDAPAsynConnection::getPort(); |
| 370 |
} |
| 371 |
|
| 372 |
void LDAPConnection::setConstraints(LDAPConstraints* cons){ |
| 373 |
LDAPAsynConnection::setConstraints(cons); |
| 374 |
} |
| 375 |
|
| 376 |
const LDAPConstraints* LDAPConnection::getConstraints() const{ |
| 377 |
return LDAPAsynConnection::getConstraints(); |
| 378 |
} |
| 379 |
|
| 380 |
TlsOptions LDAPConnection::getTlsOptions() const { |
| 381 |
return LDAPAsynConnection::getTlsOptions(); |
| 382 |
} |