00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "TLDAPServer.h"
00010 #include "TLDAPResult.h"
00011 #include "TLDAPEntry.h"
00012 #include "TLDAPAttribute.h"
00013 #include "TObjString.h"
00014 #include "TList.h"
00015 #include "TError.h"
00016
00017
00018 ClassImp(TLDAPServer)
00019
00020
00021 TLDAPServer::TLDAPServer(const char *host, Int_t port, const char *binddn,
00022 const char *password, Int_t version)
00023 {
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 fLd = 0;
00040 fIsConnected = kFALSE;
00041 fBinddn = binddn;
00042 fPassword = password;
00043
00044 fLd = ldap_init(host, port);
00045 if (!fLd) {
00046 Error("TLDAPServer", "error in ldap_init function");
00047 } else {
00048 if (ldap_set_option(fLd, LDAP_OPT_PROTOCOL_VERSION, &version) != LDAP_OPT_SUCCESS ) {
00049 Error("Bind", "Could not set protocol version!");
00050 return;
00051 }
00052
00053 Bind( );
00054 }
00055 }
00056
00057
00058 TLDAPServer::TLDAPServer(const TLDAPServer& lds) :
00059 TObject(lds),
00060 fLd(lds.fLd),
00061 fBinddn(lds.fBinddn),
00062 fPassword(lds.fPassword),
00063 fIsConnected(lds.fIsConnected)
00064 {
00065
00066 }
00067
00068
00069 TLDAPServer& TLDAPServer::operator=(const TLDAPServer& lds)
00070 {
00071
00072 if(this!=&lds) {
00073 TObject::operator=(lds);
00074 fLd=lds.fLd;
00075 fBinddn=lds.fBinddn;
00076 fPassword=lds.fPassword;
00077 fIsConnected=lds.fIsConnected;
00078 } return *this;
00079 }
00080
00081
00082 TLDAPServer::~TLDAPServer()
00083 {
00084
00085
00086 Unbind();
00087 }
00088
00089
00090 Int_t TLDAPServer::Bind()
00091 {
00092
00093
00094
00095 if (!IsConnected()) {
00096 Int_t result = ldap_simple_bind_s(fLd, fBinddn.Data(), fPassword.Data());
00097 if (result != LDAP_SUCCESS) {
00098 ldap_unbind(fLd);
00099 fIsConnected = kFALSE;
00100 switch (result) {
00101 case LDAP_INVALID_CREDENTIALS:
00102 Error("Bind", "invalid password");
00103 break;
00104 case LDAP_INAPPROPRIATE_AUTH:
00105 Error("Bind", "entry has no password to check");
00106 break;
00107 default :
00108 Error("Bind", "%s", ldap_err2string(result));
00109 break;
00110 }
00111 } else {
00112 fIsConnected = kTRUE;
00113 }
00114 return result;
00115 }
00116 return 0;
00117 }
00118
00119
00120 void TLDAPServer::Unbind()
00121 {
00122
00123
00124 if (IsConnected()) {
00125 ldap_unbind(fLd);
00126 fIsConnected = kFALSE;
00127 }
00128 }
00129
00130
00131 const char *TLDAPServer::GetNamingContexts()
00132 {
00133
00134
00135
00136
00137 TList *attrs = new TList;
00138 attrs->SetOwner();
00139 attrs->AddLast(new TObjString("namingContexts"));
00140
00141 TLDAPResult *result = Search("", LDAP_SCOPE_BASE, 0, attrs, 0);
00142
00143 TLDAPEntry *entry = result->GetNext();
00144
00145 TLDAPAttribute *attribute = entry->GetAttribute();
00146
00147 const char *namingcontexts = attribute->GetValue();
00148
00149 delete entry;
00150 delete result;
00151 delete attrs;
00152
00153 return namingcontexts;
00154 }
00155
00156
00157 const char *TLDAPServer::GetSubschemaSubentry()
00158 {
00159
00160
00161
00162
00163 TList *attrs = new TList;
00164 attrs->SetOwner();
00165 attrs->AddLast(new TObjString("subschemaSubentry"));
00166
00167 TLDAPResult *result = Search("", LDAP_SCOPE_BASE, 0, attrs, 0);
00168
00169 TLDAPEntry *entry = result->GetNext();
00170
00171 TLDAPAttribute *attribute = entry->GetAttribute();
00172
00173 const char *subschema = attribute->GetValue();
00174
00175 delete entry;
00176 delete result;
00177 delete attrs;
00178
00179 return subschema;
00180 }
00181
00182
00183 TLDAPResult *TLDAPServer::GetObjectClasses()
00184 {
00185
00186
00187
00188
00189 const char *subschema = GetSubschemaSubentry();
00190
00191 TList *attrs = new TList;
00192 attrs->SetOwner();
00193 attrs->AddLast(new TObjString("objectClasses"));
00194
00195 TLDAPResult *result = Search(subschema, LDAP_SCOPE_BASE, 0, attrs, 0);
00196
00197 delete attrs;
00198
00199 return result;
00200 }
00201
00202
00203 TLDAPResult *TLDAPServer::GetAttributeTypes()
00204 {
00205
00206
00207
00208
00209 const char *subschema = GetSubschemaSubentry();
00210
00211 TList *attrs = new TList;
00212 attrs->SetOwner();
00213 attrs->AddLast(new TObjString("attributeTypes"));
00214
00215 TLDAPResult *result = Search(subschema, LDAP_SCOPE_BASE, 0, attrs, 0);
00216
00217 delete attrs;
00218
00219 return result;
00220 }
00221
00222
00223 TLDAPResult *TLDAPServer::Search(const char *base, Int_t scope,
00224 const char *filter, TList *attrs,
00225 Bool_t attrsonly)
00226 {
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 Bind();
00247
00248 Int_t errcode;
00249 TLDAPResult *result = 0;
00250
00251 if (IsConnected()) {
00252
00253 LDAPMessage *searchresult;
00254 char **attrslist = 0;
00255 if (attrs) {
00256 Int_t n = attrs->GetSize();
00257 attrslist = new char* [n + 1];
00258 for (Int_t i = 0; i < n; i++)
00259 attrslist[i] = (char*) ((TObjString*)attrs->At(i))->GetName();
00260 attrslist[n] = 0;
00261 }
00262 if (filter == 0)
00263 filter = "(objectClass=*)";
00264
00265 errcode = ldap_search_s(fLd, base, scope, filter, attrslist,
00266 attrsonly, &searchresult);
00267
00268 delete [] attrslist;
00269
00270 if (errcode == LDAP_SUCCESS) {
00271 result = new TLDAPResult(fLd, searchresult);
00272 } else {
00273 ldap_msgfree(searchresult);
00274 Error("Search", "%s", ldap_err2string(errcode));
00275 }
00276
00277 } else {
00278 errcode = LDAP_SERVER_DOWN;
00279 Error("Search", "%s", "server is not connected");
00280 }
00281
00282 return result;
00283 }
00284
00285
00286 Int_t TLDAPServer::AddEntry(TLDAPEntry &entry)
00287 {
00288
00289
00290
00291
00292 Bind();
00293
00294 Int_t errcode;
00295 if (IsConnected()) {
00296 LDAPMod **ms = entry.GetMods(0);
00297 errcode = ldap_add_s(fLd, entry.GetDn(), ms);
00298 TLDAPServer::DeleteMods(ms);
00299 if (errcode != LDAP_SUCCESS)
00300 Error("AddEntry", "%s", ldap_err2string(errcode));
00301 } else {
00302 errcode = LDAP_SERVER_DOWN;
00303 Error("AddEntry", "server is not connected");
00304 }
00305 return errcode;
00306 }
00307
00308
00309 Int_t TLDAPServer::ModifyEntry(TLDAPEntry &entry, Int_t mode)
00310 {
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 Bind();
00324
00325 Int_t errcode;
00326 if (IsConnected()) {
00327 LDAPMod **ms = entry.GetMods(mode);
00328 errcode = ldap_modify_s(fLd, entry.GetDn(), ms);
00329 TLDAPServer::DeleteMods(ms);
00330 if (errcode != LDAP_SUCCESS)
00331 Error("ModifyEntry", "%s", ldap_err2string(errcode));
00332 } else {
00333 errcode = LDAP_SERVER_DOWN;
00334 Error("ModifyEntry", "server is not connected");
00335 }
00336 return errcode;
00337 }
00338
00339
00340 Int_t TLDAPServer::DeleteEntry(const char *dn)
00341 {
00342
00343
00344
00345
00346 Bind();
00347
00348 Int_t errcode;
00349 if (IsConnected()) {
00350 errcode = ldap_delete_s(fLd, dn);
00351 if (errcode != LDAP_SUCCESS)
00352 Error("DeleteEntry", "%s", ldap_err2string(errcode));
00353 } else {
00354 errcode = LDAP_SERVER_DOWN;
00355 Error("DeleteEntry", "server is not connected");
00356 }
00357 return errcode;
00358 }
00359
00360
00361 Int_t TLDAPServer::RenameEntry(const char *dn, const char *newrdn, Bool_t removeattr)
00362 {
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 Int_t errcode;
00376 if (IsConnected()) {
00377 errcode = ldap_modrdn2_s(fLd, dn, newrdn, removeattr);
00378 if (errcode != LDAP_SUCCESS)
00379 Error( "RenameEntry", "%s", ldap_err2string(errcode));
00380 } else {
00381 errcode = LDAP_SERVER_DOWN;
00382 Error("RenameEntry", "server is not connected");
00383 }
00384 return errcode;
00385 }
00386
00387
00388 void TLDAPServer::DeleteMods(LDAPMod **mods)
00389 {
00390
00391
00392
00393
00394 #if 1
00395 ldap_mods_free(mods, 1);
00396 #else
00397 Int_t i = 0;
00398 LDAPMod *mod;
00399 while ((mod = mods[i++]) != 0) {
00400 if (mod->mod_op & LDAP_MOD_BVALUES) {
00401 ber_bvecfree(mod->mod_bvalues);
00402 } else {
00403 Int_t j = 0;
00404 char *c;
00405 while ((c = mod->mod_values[j++]) != 0)
00406 delete c;
00407 }
00408 delete mod->mod_type;
00409 delete mod;
00410 }
00411 delete mods;
00412 #endif
00413 }