00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "RConfigure.h"
00023 #include "TSystem.h"
00024 #include "THostAuth.h"
00025 #include "TRootSecContext.h"
00026 #include "TAuthenticate.h"
00027 #include "TSocket.h"
00028 #include "TUrl.h"
00029 #include <stdlib.h>
00030
00031
00032 ClassImp(THostAuth)
00033
00034
00035 THostAuth::THostAuth() : TObject()
00036 {
00037
00038
00039 Create(0, 0);
00040 }
00041
00042
00043 THostAuth::THostAuth(const char *host, const char *user, Int_t nmeth,
00044 Int_t *authmeth, char **details) : TObject()
00045 {
00046
00047
00048
00049
00050
00051
00052 Create(host, user, nmeth, authmeth, details);
00053 }
00054
00055
00056 THostAuth::THostAuth(const char *host, Int_t server, const char *user,
00057 Int_t nmeth, Int_t *authmeth, char **details) : TObject()
00058 {
00059
00060
00061
00062
00063
00064
00065 Create(host, user, nmeth, authmeth, details);
00066
00067 fServer = server;
00068 }
00069
00070
00071 THostAuth::THostAuth(const char *host, const char *user, Int_t authmeth,
00072 const char *details) : TObject()
00073 {
00074
00075
00076
00077
00078 Create(host, user, 1, &authmeth, (char **)&details);
00079 }
00080
00081
00082 THostAuth::THostAuth(const char *host, Int_t server, const char *user,
00083 Int_t authmeth, const char *details) : TObject()
00084 {
00085
00086
00087
00088
00089 Create(host, user, 1, &authmeth, (char **)&details);
00090 fServer = server;
00091 }
00092
00093
00094 void THostAuth::Create(const char *host, const char *user, Int_t nmeth,
00095 Int_t *authmeth, char **details)
00096 {
00097
00098
00099
00100
00101
00102
00103 int i;
00104
00105
00106 fHost = host;
00107
00108 fServer = -1;
00109
00110 TString srv("");
00111 if (fHost.Contains(":")) {
00112
00113 srv = fHost;
00114 fHost.Remove(fHost.Index(":"));
00115 srv.Remove(0,srv.Index(":")+1);
00116 } else if (fHost.Contains("://")) {
00117
00118 srv = TUrl(fHost).GetProtocol();
00119 fHost.Remove(0,fHost.Index("://")+3);
00120 }
00121 if (srv.Length()) {
00122 if (srv == "0" || srv.BeginsWith("sock"))
00123 fServer = TSocket::kSOCKD;
00124 else if (srv == "1" || srv.BeginsWith("root"))
00125 fServer = TSocket::kROOTD;
00126 else if (srv == "2" || srv.BeginsWith("proof"))
00127 fServer = TSocket::kPROOFD;
00128 }
00129
00130
00131 if (fHost != "default" && !fHost.Contains("*")) {
00132 TInetAddress addr = gSystem->GetHostByName(fHost);
00133 if (addr.IsValid())
00134 fHost = addr.GetHostName();
00135 }
00136
00137
00138 fUser = user;
00139 if (fUser == "")
00140 fUser = gSystem->Getenv("USER");
00141 if (fUser == "") {
00142 UserGroup_t *u = gSystem->GetUserInfo();
00143 if (u)
00144 fUser = u->fUser;
00145 delete u;
00146 }
00147
00148
00149 fNumMethods = nmeth;
00150 if (fNumMethods > 0) {
00151 if (!authmeth)
00152 fNumMethods = 0;
00153 for (i = 0; i < kMAXSEC; i++) {
00154 if (i < fNumMethods) {
00155 fMethods[i] = authmeth[i];
00156 fSuccess[i] = 0;
00157 fFailure[i] = 0;
00158 } else {
00159 fMethods[i] = -1;
00160 fSuccess[i] = -1;
00161 fFailure[i] = -1;
00162 }
00163 }
00164 }
00165
00166
00167 if (fNumMethods > 0) {
00168 for (i = 0; i < fNumMethods; i++) {
00169 if (details && details[i] && strlen(details[i]) > 0) {
00170 fDetails[i] = details[i];
00171 } else {
00172
00173 char *tmp = TAuthenticate::GetDefaultDetails(fMethods[i],0,fUser);
00174 fDetails[i] = (const char *)tmp;
00175 delete[] tmp;
00176 }
00177 }
00178 }
00179
00180
00181 fSecContexts = new TList;
00182
00183
00184 fActive = kTRUE;
00185 }
00186
00187
00188
00189 THostAuth::THostAuth(const char *asstring) : TObject()
00190 {
00191
00192
00193
00194
00195
00196 fServer = -1;
00197
00198 TString strtmp(asstring);
00199 char *tmp = new char[strlen(asstring)+1];
00200 strncpy(tmp,asstring,strlen(asstring));
00201 tmp[strlen(asstring)] = 0;
00202
00203 fHost = TString((const char *)strtok(tmp," "));
00204 strtmp.ReplaceAll(fHost,"");
00205 fHost.Remove(0,fHost.Index(":")+1);
00206
00207 fUser = TString((const char *)strtok(0," "));
00208 strtmp.ReplaceAll(fUser,"");
00209 fUser.Remove(0,fUser.Index(":")+1);
00210
00211 TString fNmet;
00212 fNmet = TString((const char *)strtok(0," "));
00213 strtmp.ReplaceAll(fNmet,"");
00214 fNmet.Remove(0,fNmet.Index(":")+1);
00215
00216 delete[] tmp;
00217
00218 fNumMethods = atoi(fNmet.Data());
00219 Int_t i = 0;
00220 for (; i < fNumMethods; i++) {
00221 TString det = strtmp;
00222 det.Remove(0,det.Index("'")+1);
00223 det.Resize(det.Index("'"));
00224
00225 char cmet[20];
00226 sscanf(det.Data(),"%10s",cmet);
00227 Int_t met = atoi(cmet);
00228 if (met > -1 && met < kMAXSEC) {
00229 det.ReplaceAll(cmet,"");
00230 while (det.First(' ') == 0)
00231 det.Remove(0,1);
00232 while (det.Last(' ') == (det.Length() - 1))
00233 det.Resize(det.Length() - 1);
00234 fMethods[i] = met;
00235 fSuccess[i] = 0;
00236 fFailure[i] = 0;
00237 fDetails[i] = det;
00238 }
00239 strtmp.Remove(0,strtmp.Index("'",strtmp.Index("'")+1)+1);
00240 }
00241 for (i = fNumMethods; i < kMAXSEC ; i++) {
00242 fMethods[i] = -1;
00243 fSuccess[i] = -1;
00244 fFailure[i] = -1;
00245 }
00246
00247
00248 fSecContexts = new TList;
00249
00250
00251 fActive = kTRUE;
00252 }
00253
00254
00255
00256 THostAuth::THostAuth(THostAuth &ha) : TObject()
00257 {
00258
00259
00260 fHost = ha.fHost;
00261 fServer = ha.fServer;
00262 fUser = ha.fUser;
00263 fNumMethods = ha.fNumMethods;
00264 Int_t i = 0;
00265 for (; i < kMAXSEC; i++) {
00266 fMethods[i] = ha.fMethods[i];
00267 fSuccess[i] = ha.fSuccess[i];
00268 fFailure[i] = ha.fFailure[i];
00269 fDetails[i] = ha.fDetails[i];
00270 }
00271 fSecContexts = ha.Established();
00272 fActive = ha.fActive;
00273 }
00274
00275
00276 void THostAuth::AddMethod(Int_t meth, const char *details)
00277 {
00278
00279
00280
00281
00282 if (meth < 0 || meth >= kMAXSEC) return;
00283
00284
00285 if (HasMethod(meth)) {
00286 SetDetails(meth,details);
00287 return;
00288 }
00289
00290
00291 fMethods[fNumMethods] = meth;
00292 fSuccess[fNumMethods] = 0;
00293 fFailure[fNumMethods] = 0;
00294 if (details && strlen(details) > 0) {
00295 fDetails[fNumMethods] = details;
00296 } else {
00297
00298 char *tmp = TAuthenticate::GetDefaultDetails(meth,0,fUser);
00299 fDetails[fNumMethods] = (const char *)tmp;
00300 delete[] tmp;
00301 }
00302
00303
00304 fNumMethods++;
00305
00306 if (gDebug > 3) Print();
00307 }
00308
00309
00310 void THostAuth::RemoveMethod(Int_t meth)
00311 {
00312
00313
00314
00315 Int_t pos = -1;
00316 if (!HasMethod(meth,&pos)) return;
00317
00318
00319 Int_t i = 0, k = 0;
00320 for (; i < fNumMethods; i++) {
00321 if (i != pos) {
00322 fMethods[k] = fMethods[i];
00323 fSuccess[k] = fSuccess[i];
00324 fFailure[k] = fFailure[i];
00325 fDetails[k] = fDetails[i];
00326 k++;
00327 }
00328 }
00329
00330
00331 fNumMethods--;
00332
00333
00334 fMethods[fNumMethods] = -1;
00335 fSuccess[fNumMethods] = -1;
00336 fFailure[fNumMethods] = -1;
00337 fDetails[fNumMethods].Resize(0);
00338
00339 if (gDebug > 3) Print();
00340 }
00341
00342
00343 void THostAuth::Reset()
00344 {
00345
00346
00347
00348
00349 Int_t i = 0;
00350 for (; i < fNumMethods; i++) {
00351 fMethods[i] = -1;
00352 fSuccess[i] = -1;
00353 fFailure[i] = -1;
00354 fDetails[i].Resize(0);
00355 }
00356
00357
00358 fNumMethods = 0;
00359 }
00360
00361
00362 THostAuth::~THostAuth()
00363 {
00364
00365
00366 delete fSecContexts;
00367 }
00368
00369
00370 const char *THostAuth::GetDetails(Int_t level)
00371 {
00372
00373
00374
00375 Int_t i = -1;
00376 if (HasMethod(level,&i)) {
00377 if (gDebug > 3)
00378 Info("GetDetails"," %d: returning fDetails[%d]: %s",
00379 level,i,fDetails[i].Data());
00380 return fDetails[i];
00381 }
00382 static const char *empty = " ";
00383 return empty;
00384 }
00385
00386
00387 Bool_t THostAuth::HasMethod(Int_t level, Int_t *pos)
00388 {
00389
00390
00391 int i;
00392 for (i = 0; i < fNumMethods; i++) {
00393 if (fMethods[i] == level) {
00394 if (pos) *pos = i;
00395 return kTRUE;
00396 }
00397 }
00398 if (pos) *pos = -1;
00399 return kFALSE;
00400 }
00401
00402
00403 void THostAuth::SetDetails(Int_t level, const char *details)
00404 {
00405
00406
00407 Int_t i = -1;
00408 if (HasMethod(level,&i)) {
00409 if (details && strlen(details) > 0) {
00410 fDetails[i] = details;
00411 } else {
00412
00413 char *tmp = TAuthenticate::GetDefaultDetails(level,0,fUser);
00414 fDetails[i] = (const char *)tmp;
00415 delete[] tmp;
00416 }
00417 } else {
00418
00419 AddMethod(level, details);
00420 }
00421 }
00422
00423
00424 void THostAuth::Print(Option_t *proc) const
00425 {
00426
00427
00428 char srvnam[5][8] = { "any", "sockd", "rootd", "proofd", "???" };
00429
00430 Int_t isrv = (fServer >= -1 && fServer <= TSocket::kPROOFD) ?
00431 fServer+1 : TSocket::kPROOFD+2;
00432
00433 Info("Print",
00434 "%s +------------------------------------------------------------------+",proc);
00435 Info("Print","%s + Host:%s - srv:%s - User:%s - # of available methods:%d",
00436 proc, fHost.Data(), srvnam[isrv], fUser.Data(), fNumMethods);
00437 int i = 0;
00438 for (i = 0; i < fNumMethods; i++){
00439 Info("Print","%s + Method: %d (%s) Ok:%d Ko:%d Dets:%s", proc,
00440 fMethods[i],TAuthenticate::GetAuthMethod(fMethods[i]),
00441 fSuccess[i], fFailure[i], fDetails[i].Data());
00442 }
00443 Info("Print",
00444 "%s +------------------------------------------------------------------+",proc);
00445 }
00446
00447
00448 void THostAuth::PrintEstablished() const
00449 {
00450
00451
00452 Info("PrintEstablished",
00453 "+------------------------------------------------------------------------------+");
00454 Info("PrintEstablished","+ Host:%s - Number of active sec contexts: %d",
00455 fHost.Data(), fSecContexts->GetSize());
00456
00457
00458 if (fSecContexts->GetSize()>0) {
00459 TIter next(fSecContexts);
00460 TSecContext *ctx = 0;
00461 Int_t k = 1;
00462 while ((ctx = (TSecContext *) next())) {
00463 TString opt;
00464 opt += k++;
00465 ctx->Print(opt);
00466 }
00467 }
00468 Info("PrintEstablished",
00469 "+------------------------------------------------------------------------------+");
00470 }
00471
00472
00473 void THostAuth::ReOrder(Int_t nmet, Int_t *fmet)
00474 {
00475
00476
00477
00478 Int_t tMethods[kMAXSEC] = {0};
00479 Int_t tSuccess[kMAXSEC] = {0};
00480 Int_t tFailure[kMAXSEC] = {0};
00481 TString tDetails[kMAXSEC];
00482 Int_t flag[kMAXSEC] = {0};
00483
00484
00485 Int_t j = 0;
00486 for ( ; j < nmet; j++) {
00487 Int_t i = -1;
00488 if (HasMethod(fmet[j],&i)) {
00489 tMethods[j] = fMethods[i];
00490 tSuccess[j] = fSuccess[i];
00491 tFailure[j] = fFailure[i];
00492 tDetails[j] = fDetails[i];
00493 flag[i]++;
00494 } else if (fmet[j] >= 0 && fmet[j] < kMAXSEC) {
00495 tMethods[j] = fmet[j];
00496 tSuccess[j] = 0;
00497 tFailure[j] = 0;
00498 char *tmp = TAuthenticate::GetDefaultDetails(fmet[j],0,fUser);
00499 tDetails[j] = (const char *)tmp;
00500 delete[] tmp;
00501 } else {
00502 Warning("ReOrder","Method id out of range (%d) - skipping",fmet[j]);
00503 }
00504 }
00505
00506
00507 Int_t k = nmet, i = 0;
00508 for(; i < fNumMethods; i++){
00509 if (flag[i] == 0) {
00510 tMethods[k] = fMethods[i];
00511 tSuccess[k] = fSuccess[i];
00512 tFailure[k] = fFailure[i];
00513 tDetails[k] = fDetails[i];
00514 k++;
00515 flag[i] = 1;
00516 }
00517 }
00518
00519
00520 fNumMethods = k;
00521 for (i = 0; i < fNumMethods; i++) {
00522 fMethods[i] = tMethods[i];
00523 fSuccess[i] = tSuccess[i];
00524 fFailure[i] = tFailure[i];
00525 fDetails[i] = tDetails[i];
00526 }
00527
00528 if (gDebug > 3) Print();
00529 }
00530
00531
00532 void THostAuth::Update(THostAuth *ha)
00533 {
00534
00535
00536
00537
00538 Int_t tNumMethods = fNumMethods;
00539 Int_t tMethods[kMAXSEC];
00540 Int_t tSuccess[kMAXSEC];
00541 Int_t tFailure[kMAXSEC];
00542 TString tDetails[kMAXSEC];
00543
00544
00545 Int_t i = 0;
00546 for ( ; i < fNumMethods; i++) {
00547 tMethods[i] = fMethods[i];
00548 tSuccess[i] = fSuccess[i];
00549 tFailure[i] = fFailure[i];
00550 tDetails[i] = fDetails[i];
00551 }
00552
00553
00554 Reset();
00555
00556
00557 for(i = 0; i < ha->NumMethods(); i++){
00558 fMethods[i] = ha->GetMethod(i);
00559 fSuccess[i] = ha->GetSuccess(i);
00560 fFailure[i] = ha->GetFailure(i);
00561 fDetails[i] = ha->GetDetailsByIdx(i);
00562 }
00563
00564
00565 fNumMethods = ha->NumMethods();
00566
00567
00568 if (fNumMethods < kMAXSEC) {
00569 for (i = 0; i < tNumMethods; i++) {
00570 if (!HasMethod(tMethods[i]) && fNumMethods < kMAXSEC) {
00571 fMethods[fNumMethods] = tMethods[i];
00572 fSuccess[fNumMethods] = tSuccess[i];
00573 fFailure[fNumMethods] = tFailure[i];
00574 fDetails[fNumMethods] = tDetails[i];
00575 fNumMethods++;
00576 }
00577 }
00578 }
00579 if (gDebug > 3) Print();
00580 }
00581
00582
00583 void THostAuth::SetFirst(Int_t method)
00584 {
00585
00586
00587 Int_t i = -1;
00588 if (HasMethod(method,&i)) {
00589
00590 Int_t tMe = fMethods[i];
00591 Int_t tSu = fSuccess[i];
00592 Int_t tFa = fFailure[i];
00593 TString tDe = fDetails[i];
00594
00595
00596 Int_t j = i;
00597 for (; j > 0; j--) {
00598 fMethods[j] = fMethods[j-1];
00599 fSuccess[j] = fSuccess[j-1];
00600 fFailure[j] = fFailure[j-1];
00601 fDetails[j] = fDetails[j-1];
00602 }
00603
00604
00605 fMethods[0] = tMe;
00606 fSuccess[0] = tSu;
00607 fFailure[0] = tFa;
00608 fDetails[0] = tDe;
00609 }
00610
00611 if (gDebug > 3) Print();
00612 }
00613
00614
00615 void THostAuth::SetLast(Int_t method)
00616 {
00617
00618
00619 Int_t i = -1;
00620 if (HasMethod(method,&i)) {
00621
00622 Int_t tMe = fMethods[i];
00623 Int_t tSu = fSuccess[i];
00624 Int_t tFa = fFailure[i];
00625 TString tDe = fDetails[i];
00626
00627
00628 Int_t j = i;
00629 for (; j < (fNumMethods - 1); j++) {
00630 fMethods[j] = fMethods[j+1];
00631 fSuccess[j] = fSuccess[j+1];
00632 fFailure[j] = fFailure[j+1];
00633 fDetails[j] = fDetails[j+1];
00634 }
00635
00636
00637 Int_t lp = fNumMethods - 1;
00638 fMethods[lp] = tMe;
00639 fSuccess[lp] = tSu;
00640 fFailure[lp] = tFa;
00641 fDetails[lp] = tDe;
00642 }
00643
00644 if (gDebug > 3) Print();
00645 }
00646
00647
00648 void THostAuth::AddFirst(Int_t level, const char *details)
00649 {
00650
00651
00652
00653
00654
00655 Int_t i = -1;
00656 if (HasMethod(level,&i)) {
00657 if (i > 0) {
00658 SetDetails(level, details);
00659 SetFirst(level);
00660 }
00661 if (gDebug > 3) Print();
00662 return;
00663 }
00664
00665
00666 for (i = fNumMethods; i > 0; i--) {
00667 fMethods[i] = fMethods[i-1];
00668 fSuccess[i] = fSuccess[i-1];
00669 fFailure[i] = fFailure[i-1];
00670 fDetails[i] = fDetails[i-1];
00671 }
00672
00673
00674 fMethods[0] = level;
00675 fSuccess[0] = 0;
00676 fFailure[0] = 0;
00677 if (details && strlen(details) > 0) {
00678 fDetails[0] = details;
00679 } else {
00680
00681 char *tmp = TAuthenticate::GetDefaultDetails(level,0,fUser);
00682 fDetails[0] = (const char *)tmp;
00683 delete[] tmp;
00684 }
00685
00686
00687 fNumMethods++;
00688
00689 if (gDebug > 3) Print();
00690 }
00691
00692
00693
00694 void THostAuth::CountSuccess(Int_t method)
00695 {
00696
00697
00698 int i;
00699 for (i = 0; i < fNumMethods; i++) {
00700 if (fMethods[i] == method) {
00701 fSuccess[i]++;
00702 break;
00703 }
00704 }
00705 }
00706
00707
00708 void THostAuth::CountFailure(Int_t method)
00709 {
00710
00711
00712 int i;
00713 for (i = 0; i < fNumMethods; i++) {
00714 if (fMethods[i] == method) {
00715 fFailure[i]++;
00716 break;
00717 }
00718 }
00719 }
00720
00721
00722 TRootSecContext *THostAuth::CreateSecContext(const char *user, const char *host,
00723 Int_t meth, Int_t offset,
00724 const char *details, const char *token,
00725 TDatime expdate, void *sctx, Int_t key)
00726 {
00727
00728
00729
00730 TRootSecContext *ctx = new TRootSecContext(user, host, meth, offset, details,
00731 token, expdate, sctx, key);
00732
00733 if (ctx->IsActive())
00734 fSecContexts->Add(ctx);
00735
00736 return ctx;
00737
00738 }
00739
00740
00741 void THostAuth::AsString(TString &Out) const
00742 {
00743
00744
00745 Out = Form("h:%s u:%s n:%d",GetHost(),GetUser(),fNumMethods);
00746
00747 Int_t i = 0;
00748 for (; i < fNumMethods; i++) {
00749 Out += TString(Form(" '%d %s'",fMethods[i],fDetails[i].Data()));
00750 }
00751
00752 }