22 #include "RConfigure.h"
37 THostAuth::THostAuth() : TObject()
49 THostAuth::THostAuth(
const char *host,
const char *user, Int_t nmeth,
50 Int_t *authmeth,
char **details) : TObject()
52 Create(host, user, nmeth, authmeth, details);
62 THostAuth::THostAuth(
const char *host, Int_t server,
const char *user,
63 Int_t nmeth, Int_t *authmeth,
char **details) : TObject()
65 Create(host, user, nmeth, authmeth, details);
75 THostAuth::THostAuth(
const char *host,
const char *user, Int_t authmeth,
76 const char *details) : TObject()
78 Create(host, user, 1, &authmeth, (
char **)&details);
86 THostAuth::THostAuth(
const char *host, Int_t server,
const char *user,
87 Int_t authmeth,
const char *details) : TObject()
89 Create(host, user, 1, &authmeth, (
char **)&details);
100 void THostAuth::Create(
const char *host,
const char *user, Int_t nmeth,
101 Int_t *authmeth,
char **details)
111 if (fHost.Contains(
":")) {
114 fHost.Remove(fHost.Index(
":"));
115 srv.Remove(0,srv.Index(
":")+1);
116 }
else if (fHost.Contains(
"://")) {
118 srv = TUrl(fHost).GetProtocol();
119 fHost.Remove(0,fHost.Index(
"://")+3);
122 if (srv ==
"0" || srv.BeginsWith(
"sock"))
123 fServer = TSocket::kSOCKD;
124 else if (srv ==
"1" || srv.BeginsWith(
"root"))
125 fServer = TSocket::kROOTD;
126 else if (srv ==
"2" || srv.BeginsWith(
"proof"))
127 fServer = TSocket::kPROOFD;
131 if (fHost !=
"default" && !fHost.Contains(
"*")) {
132 TInetAddress addr = gSystem->GetHostByName(fHost);
134 fHost = addr.GetHostName();
140 fUser = gSystem->Getenv(
"USER");
142 UserGroup_t *u = gSystem->GetUserInfo();
150 if (fNumMethods > 0) {
153 for (i = 0; i < kMAXSEC; i++) {
154 if (i < fNumMethods) {
155 fMethods[i] = authmeth[i];
167 if (fNumMethods > 0) {
168 for (i = 0; i < fNumMethods; i++) {
169 if (details && details[i] && strlen(details[i]) > 0) {
170 fDetails[i] = details[i];
173 char *tmp = TAuthenticate::GetDefaultDetails(fMethods[i],0,fUser);
174 fDetails[i] = (
const char *)tmp;
181 fSecContexts =
new TList;
194 THostAuth::THostAuth(
const char *asstring) : TObject()
198 TString strtmp(asstring);
199 char *tmp = strdup(asstring);
201 fHost = TString((
const char *)strtok(tmp,
" "));
202 strtmp.ReplaceAll(fHost,
"");
203 fHost.Remove(0,fHost.Index(
":")+1);
205 fUser = TString((
const char *)strtok(0,
" "));
206 strtmp.ReplaceAll(fUser,
"");
207 fUser.Remove(0,fUser.Index(
":")+1);
210 fNmet = TString((
const char *)strtok(0,
" "));
211 strtmp.ReplaceAll(fNmet,
"");
212 fNmet.Remove(0,fNmet.Index(
":")+1);
216 fNumMethods = atoi(fNmet.Data());
218 for (; i < fNumMethods; i++) {
219 TString det = strtmp;
220 det.Remove(0,det.Index(
"'")+1);
221 det.Resize(det.Index(
"'"));
224 sscanf(det.Data(),
"%10s",cmet);
225 Int_t met = atoi(cmet);
226 if (met > -1 && met < kMAXSEC) {
227 det.ReplaceAll(cmet,
"");
228 while (det.First(
' ') == 0)
230 while (det.Last(
' ') == (det.Length() - 1))
231 det.Resize(det.Length() - 1);
237 strtmp.Remove(0,strtmp.Index(
"'",strtmp.Index(
"'")+1)+1);
239 for (i = fNumMethods; i < kMAXSEC ; i++) {
246 fSecContexts =
new TList;
256 THostAuth::THostAuth(THostAuth &ha) : TObject()
259 fServer = ha.fServer;
261 fNumMethods = ha.fNumMethods;
263 for (; i < kMAXSEC; i++) {
264 fMethods[i] = ha.fMethods[i];
265 fSuccess[i] = ha.fSuccess[i];
266 fFailure[i] = ha.fFailure[i];
267 fDetails[i] = ha.fDetails[i];
269 fSecContexts = ha.Established();
270 fActive = ha.fActive;
277 void THostAuth::AddMethod(Int_t meth,
const char *details)
280 if (meth < 0 || meth >= kMAXSEC)
return;
283 if (HasMethod(meth)) {
284 SetDetails(meth,details);
289 fMethods[fNumMethods] = meth;
290 fSuccess[fNumMethods] = 0;
291 fFailure[fNumMethods] = 0;
292 if (details && strlen(details) > 0) {
293 fDetails[fNumMethods] = details;
296 char *tmp = TAuthenticate::GetDefaultDetails(meth,0,fUser);
297 fDetails[fNumMethods] = (
const char *)tmp;
304 if (gDebug > 3) Print();
310 void THostAuth::RemoveMethod(Int_t meth)
314 if (!HasMethod(meth,&pos))
return;
318 for (; i < fNumMethods; i++) {
320 fMethods[k] = fMethods[i];
321 fSuccess[k] = fSuccess[i];
322 fFailure[k] = fFailure[i];
323 fDetails[k] = fDetails[i];
332 fMethods[fNumMethods] = -1;
333 fSuccess[fNumMethods] = -1;
334 fFailure[fNumMethods] = -1;
335 fDetails[fNumMethods].Resize(0);
337 if (gDebug > 3) Print();
344 void THostAuth::Reset()
348 for (; i < fNumMethods; i++) {
352 fDetails[i].Resize(0);
362 THostAuth::~THostAuth()
371 const char *THostAuth::GetDetails(Int_t level)
374 if (HasMethod(level,&i)) {
376 Info(
"GetDetails",
" %d: returning fDetails[%d]: %s",
377 level,i,fDetails[i].Data());
380 static const char *empty =
" ";
387 Bool_t THostAuth::HasMethod(Int_t level, Int_t *pos)
390 for (i = 0; i < fNumMethods; i++) {
391 if (fMethods[i] == level) {
403 void THostAuth::SetDetails(Int_t level,
const char *details)
406 if (HasMethod(level,&i)) {
407 if (details && strlen(details) > 0) {
408 fDetails[i] = details;
411 char *tmp = TAuthenticate::GetDefaultDetails(level,0,fUser);
412 fDetails[i] = (
const char *)tmp;
417 AddMethod(level, details);
424 void THostAuth::Print(Option_t *proc)
const
426 char srvnam[5][8] = {
"any",
"sockd",
"rootd",
"proofd",
"???" };
428 Int_t isrv = (fServer >= -1 && fServer <= TSocket::kPROOFD) ?
429 fServer+1 : TSocket::kPROOFD+2;
432 "%s +------------------------------------------------------------------+",proc);
433 Info(
"Print",
"%s + Host:%s - srv:%s - User:%s - # of available methods:%d",
434 proc, fHost.Data(), srvnam[isrv], fUser.Data(), fNumMethods);
436 for (i = 0; i < fNumMethods; i++){
437 Info(
"Print",
"%s + Method: %d (%s) Ok:%d Ko:%d Dets:%s", proc,
438 fMethods[i],TAuthenticate::GetAuthMethod(fMethods[i]),
439 fSuccess[i], fFailure[i], fDetails[i].Data());
442 "%s +------------------------------------------------------------------+",proc);
448 void THostAuth::PrintEstablished()
const
450 Info(
"PrintEstablished",
451 "+------------------------------------------------------------------------------+");
452 Info(
"PrintEstablished",
"+ Host:%s - Number of active sec contexts: %d",
453 fHost.Data(), fSecContexts->GetSize());
456 if (fSecContexts->GetSize()>0) {
457 TIter next(fSecContexts);
458 TSecContext *ctx = 0;
460 while ((ctx = (TSecContext *) next())) {
466 Info(
"PrintEstablished",
467 "+------------------------------------------------------------------------------+");
473 void THostAuth::ReOrder(Int_t nmet, Int_t *fmet)
476 Int_t tMethods[kMAXSEC] = {0};
477 Int_t tSuccess[kMAXSEC] = {0};
478 Int_t tFailure[kMAXSEC] = {0};
479 TString tDetails[kMAXSEC];
480 Int_t flag[kMAXSEC] = {0};
484 for ( ; j < nmet; j++) {
486 if (HasMethod(fmet[j],&i)) {
487 tMethods[j] = fMethods[i];
488 tSuccess[j] = fSuccess[i];
489 tFailure[j] = fFailure[i];
490 tDetails[j] = fDetails[i];
492 }
else if (fmet[j] >= 0 && fmet[j] < kMAXSEC) {
493 tMethods[j] = fmet[j];
496 char *tmp = TAuthenticate::GetDefaultDetails(fmet[j],0,fUser);
497 tDetails[j] = (
const char *)tmp;
500 Warning(
"ReOrder",
"Method id out of range (%d) - skipping",fmet[j]);
505 Int_t k = nmet, i = 0;
506 for(; i < fNumMethods; i++){
508 tMethods[k] = fMethods[i];
509 tSuccess[k] = fSuccess[i];
510 tFailure[k] = fFailure[i];
511 tDetails[k] = fDetails[i];
519 for (i = 0; i < fNumMethods; i++) {
520 fMethods[i] = tMethods[i];
521 fSuccess[i] = tSuccess[i];
522 fFailure[i] = tFailure[i];
523 fDetails[i] = tDetails[i];
526 if (gDebug > 3) Print();
533 void THostAuth::Update(THostAuth *ha)
536 Int_t tNumMethods = fNumMethods;
537 Int_t tMethods[kMAXSEC];
538 Int_t tSuccess[kMAXSEC];
539 Int_t tFailure[kMAXSEC];
540 TString tDetails[kMAXSEC];
544 for ( ; i < fNumMethods; i++) {
545 tMethods[i] = fMethods[i];
546 tSuccess[i] = fSuccess[i];
547 tFailure[i] = fFailure[i];
548 tDetails[i] = fDetails[i];
555 for(i = 0; i < ha->NumMethods(); i++){
556 fMethods[i] = ha->GetMethod(i);
557 fSuccess[i] = ha->GetSuccess(i);
558 fFailure[i] = ha->GetFailure(i);
559 fDetails[i] = ha->GetDetailsByIdx(i);
563 fNumMethods = ha->NumMethods();
566 if (fNumMethods < kMAXSEC) {
567 for (i = 0; i < tNumMethods; i++) {
568 if (!HasMethod(tMethods[i]) && fNumMethods < kMAXSEC) {
569 fMethods[fNumMethods] = tMethods[i];
570 fSuccess[fNumMethods] = tSuccess[i];
571 fFailure[fNumMethods] = tFailure[i];
572 fDetails[fNumMethods] = tDetails[i];
577 if (gDebug > 3) Print();
583 void THostAuth::SetFirst(Int_t method)
586 if (HasMethod(method,&i)) {
588 Int_t tMe = fMethods[i];
589 Int_t tSu = fSuccess[i];
590 Int_t tFa = fFailure[i];
591 TString tDe = fDetails[i];
596 fMethods[j] = fMethods[j-1];
597 fSuccess[j] = fSuccess[j-1];
598 fFailure[j] = fFailure[j-1];
599 fDetails[j] = fDetails[j-1];
609 if (gDebug > 3) Print();
615 void THostAuth::SetLast(Int_t method)
618 if (HasMethod(method,&i)) {
620 Int_t tMe = fMethods[i];
621 Int_t tSu = fSuccess[i];
622 Int_t tFa = fFailure[i];
623 TString tDe = fDetails[i];
627 for (; j < (fNumMethods - 1); j++) {
628 fMethods[j] = fMethods[j+1];
629 fSuccess[j] = fSuccess[j+1];
630 fFailure[j] = fFailure[j+1];
631 fDetails[j] = fDetails[j+1];
635 Int_t lp = fNumMethods - 1;
642 if (gDebug > 3) Print();
651 void THostAuth::AddFirst(Int_t level,
const char *details)
654 if (HasMethod(level,&i)) {
656 SetDetails(level, details);
659 if (gDebug > 3) Print();
664 for (i = fNumMethods; i > 0; i--) {
665 fMethods[i] = fMethods[i-1];
666 fSuccess[i] = fSuccess[i-1];
667 fFailure[i] = fFailure[i-1];
668 fDetails[i] = fDetails[i-1];
675 if (details && strlen(details) > 0) {
676 fDetails[0] = details;
679 char *tmp = TAuthenticate::GetDefaultDetails(level,0,fUser);
680 fDetails[0] = (
const char *)tmp;
687 if (gDebug > 3) Print();
694 void THostAuth::CountSuccess(Int_t method)
697 for (i = 0; i < fNumMethods; i++) {
698 if (fMethods[i] == method) {
708 void THostAuth::CountFailure(Int_t method)
711 for (i = 0; i < fNumMethods; i++) {
712 if (fMethods[i] == method) {
723 TRootSecContext *THostAuth::CreateSecContext(
const char *user,
const char *host,
724 Int_t meth, Int_t offset,
725 const char *details,
const char *token,
726 TDatime expdate,
void *sctx, Int_t key)
728 TRootSecContext *ctx =
new TRootSecContext(user, host, meth, offset, details,
729 token, expdate, sctx, key);
732 fSecContexts->Add(ctx);
741 void THostAuth::AsString(TString &Out)
const
743 Out = Form(
"h:%s u:%s n:%d",GetHost(),GetUser(),fNumMethods);
746 for (; i < fNumMethods; i++) {
747 Out += TString(Form(
" '%d %s'",fMethods[i],fDetails[i].Data()));