TSecContext.cxx

Go to the documentation of this file.
00001 // @(#)root/net:$Id: TSecContext.cxx 36255 2010-10-10 10:46:37Z brun $
00002 // Author: G. Ganis   19/03/2003
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TSecContext                                                          //
00015 //                                                                      //
00016 // Contains details about an established security context               //
00017 // Used by THostAuth                                                    //
00018 //                                                                      //
00019 //////////////////////////////////////////////////////////////////////////
00020 
00021 #include "RConfigure.h"
00022 
00023 #include <stdlib.h>
00024 
00025 #include "TSecContext.h"
00026 #include "TSocket.h"
00027 #include "TUrl.h"
00028 #include "TROOT.h"
00029 #include "TError.h"
00030 #include "TVirtualMutex.h"
00031 
00032 ClassImp(TSecContext)
00033 ClassImp(TSecContextCleanup)
00034 
00035 //______________________________________________________________________________
00036 TSecContext::TSecContext(const char *user, const char *host, Int_t meth,
00037                          Int_t offset, const char *id,
00038                          const char *token, TDatime expdate, void *ctx)
00039             : TObject()
00040 {
00041    // Ctor for SecContext object.
00042    R__ASSERT(gROOT);
00043 
00044    fContext = ctx;
00045    fCleanup = new TList;
00046    fExpDate = expdate;
00047    if (offset > -1) {
00048       if (fExpDate < TDatime()) {
00049          // This means expdate was not initialized
00050          // We set it to default, ie 1 day from now
00051          fExpDate.Set(TDatime().GetDate() + 1, TDatime().GetTime());
00052       }
00053    }
00054    fHost    = host;
00055    fID      = id;
00056    fMethod  = meth;
00057    fMethodName = "";
00058    fOffSet  = offset;
00059    fToken   = token;
00060    fUser    = user;
00061 
00062    // Keep official list updated with active TSecContexts
00063    if (fOffSet > -1) {
00064       R__LOCKGUARD2(gROOTMutex);
00065       gROOT->GetListOfSecContexts()->Add(this);
00066    }
00067 }
00068 
00069 //______________________________________________________________________________
00070 TSecContext::TSecContext(const char *url, Int_t meth, Int_t offset,
00071                          const char *token, const char *id,
00072                          TDatime expdate, void *ctx)
00073             : TObject()
00074 {
00075    // Ctor for SecContext object.
00076    // User and host from url = user@host .
00077    R__ASSERT(gROOT);
00078 
00079    fContext = ctx;
00080    fCleanup = new TList;
00081    fExpDate = expdate;
00082    if (offset > -1) {
00083       if (fExpDate < TDatime()) {
00084          // This means expdate was not initialized
00085          // We set it to default, ie 1 day from now
00086          fExpDate.Set(TDatime().GetDate() + 1, TDatime().GetTime());
00087       }
00088    }
00089    fHost    = TUrl(url).GetHost();
00090    fID      = id;
00091    fMethod  = meth;
00092    fMethodName = "";
00093    fOffSet  = offset;
00094    fToken   = token;
00095    fUser    = TUrl(url).GetUser();
00096 
00097    // Keep official list updated with active TSecContexts
00098    if (fOffSet > -1) {
00099       R__LOCKGUARD2(gROOTMutex);
00100       gROOT->GetListOfSecContexts()->Add(this);
00101    }
00102 }
00103 
00104 //______________________________________________________________________________
00105 TSecContext::TSecContext(const TSecContext& sc) :
00106   TObject(sc),
00107   fContext(sc.fContext),
00108   fCleanup(sc.fCleanup),
00109   fExpDate(sc.fExpDate),
00110   fHost(sc.fHost),
00111   fID(sc.fID),
00112   fMethod(sc.fMethod),
00113   fMethodName(sc.fMethodName),
00114   fOffSet(sc.fOffSet),
00115   fToken(sc.fToken),
00116   fUser(sc.fUser)
00117 {
00118    //copy constructor
00119 }
00120 
00121 //______________________________________________________________________________
00122 TSecContext& TSecContext::operator=(const TSecContext& sc)
00123 {
00124    //assignement operator
00125    if(this!=&sc) {
00126       TObject::operator=(sc);
00127       fContext=sc.fContext;
00128       fCleanup=sc.fCleanup;
00129       fExpDate=sc.fExpDate;
00130       fHost=sc.fHost;
00131       fID=sc.fID;
00132       fMethod=sc.fMethod;
00133       fMethodName=sc.fMethodName;
00134       fOffSet=sc.fOffSet;
00135       fToken=sc.fToken;
00136       fUser=sc.fUser;
00137    }
00138    return *this;
00139 }
00140 
00141 //______________________________________________________________________________
00142 TSecContext::~TSecContext()
00143 {
00144    // Dtor: delete (deActivate, local/remote cleanup, list removal)
00145    // all what is still active
00146 
00147    Cleanup();
00148 }
00149 //______________________________________________________________________________
00150 void TSecContext::Cleanup()
00151 {
00152    // Cleanup what is still active
00153 
00154    if (IsActive()) {
00155       CleanupSecContext(kTRUE);
00156       DeActivate("R");
00157       // All have been remotely Deactivated
00158       TIter nxtl(gROOT->GetListOfSecContexts());
00159       TSecContext *nscl;
00160       while ((nscl = (TSecContext *)nxtl())) {
00161          if (nscl != this && !strcmp(nscl->GetHost(), fHost.Data())) {
00162             // Need to set ofs=-1 to avoid sending another
00163             // cleanup request
00164             nscl->DeActivate("");
00165          }
00166       }
00167    }
00168 
00169    // Delete the cleanup list
00170    if (fCleanup) {
00171       fCleanup->Delete();
00172       delete fCleanup;
00173       fCleanup = 0;
00174    }
00175 }
00176 
00177 //______________________________________________________________________________
00178 void TSecContext::DeActivate(Option_t *Opt)
00179 {
00180    // Set OffSet to -1 and expiring Date to default
00181    // Remove from the list
00182    // If Opt contains "C" or "c", ask for remote cleanup
00183    // If Opt contains "R" or "r", remove from the list
00184    // Default Opt="CR"
00185 
00186    // Ask remote cleanup of this context
00187    Bool_t clean = (strstr(Opt,"C") || strstr(Opt,"c"));
00188    if (clean && fOffSet > -1)
00189       CleanupSecContext(kFALSE);
00190 
00191    Bool_t remove = (strstr(Opt,"R") || strstr(Opt,"r"));
00192    if (remove && fOffSet > -1){
00193       R__LOCKGUARD2(gROOTMutex);
00194       // Remove from the global list
00195       gROOT->GetListOfSecContexts()->Remove(this);
00196    }
00197 
00198    // Set inactive
00199    fOffSet  = -1;
00200    fExpDate = kROOTTZERO;
00201 }
00202 
00203 //______________________________________________________________________________
00204 void TSecContext::AddForCleanup(Int_t port, Int_t proto, Int_t type)
00205 {
00206    // Create a new TSecContextCleanup
00207    // Internally is added to the list
00208 
00209    TSecContextCleanup *tscc = new TSecContextCleanup(port, proto, type);
00210    fCleanup->Add(tscc);
00211 
00212 }
00213 
00214 //______________________________________________________________________________
00215 Bool_t TSecContext::IsA(const char *methname)
00216 {
00217    // Checks if this security context is for method named 'methname'
00218    // Case sensitive.
00219 
00220    return Bool_t(!strcmp(methname, GetMethodName()));
00221 }
00222 
00223 //______________________________________________________________________________
00224 Bool_t TSecContext::IsActive() const
00225 {
00226    // Check remote OffSet and expiring Date
00227 
00228    if (fOffSet > -1 && fExpDate > TDatime())
00229       return kTRUE;
00230    // Invalid
00231    return kFALSE;
00232 }
00233 
00234 //______________________________________________________________________________
00235 void TSecContext::Print(Option_t *opt) const
00236 {
00237    // If opt is "F" (default) print object content.
00238    // If opt is "<number>" print in special form for calls within THostAuth
00239    // with cardinality <number>
00240    // If opt is "S" prints short in-line form for calls within TFTP,
00241    // TSlave, TProof ...
00242 
00243    char aOrd[10] = {0};
00244    char aSpc[10] = {0};
00245 
00246    // Check if option is numeric
00247    Int_t ord = -1, i = 0;
00248    for (; i < (Int_t)strlen(opt); i++) {
00249       if (opt[i] < 48 || opt[i] > 57) {
00250          ord = -2;
00251          break;
00252       }
00253    }
00254    // If numeric get the cardinality and prepare the strings
00255    if (ord == -1)
00256       ord = atoi(opt);
00257 
00258    // If asked to print ordinal number, preapre the string
00259    if (ord > -1) {
00260       snprintf(aOrd,10,"%d)",ord);
00261       // and take care of alignment
00262       Int_t len=strlen(aOrd);
00263       while (len--)
00264          strlcat(aSpc," ",10);
00265    }
00266 
00267    if (!strncasecmp(opt,"F",1)) {
00268       Info("Print",
00269            "+------------------------------------------------------+");
00270       Info("Print",
00271            "+ Host:%s Method:%d (%s) User:'%s'",
00272             GetHost(), fMethod, GetMethodName(),
00273             fUser.Data());
00274       Info("Print",
00275            "+         OffSet:%d, id:%s", fOffSet, fID.Data());
00276       if (fOffSet > -1)
00277          Info("Print",
00278            "+         Expiration time: %s",fExpDate.AsString());
00279       Info("Print",
00280            "+------------------------------------------------------+");
00281    } else if (!strncasecmp(opt,"S",1)) {
00282       if (fOffSet > -1) {
00283          Printf("Security context:     Method: %d (%s) expiring on %s",
00284                 fMethod, GetMethodName(),
00285                 fExpDate.AsString());
00286       } else {
00287          Printf("Security context:     Method: %d (%s) not reusable",
00288                 fMethod, GetMethodName());
00289       }
00290    } else {
00291       // special printing form for THostAuth
00292       Info("PrintEstblshed","+ %s h:%s met:%d (%s) us:'%s'",
00293             aOrd, GetHost(), fMethod, GetMethodName(),
00294             fUser.Data());
00295       Info("PrintEstblshed","+ %s offset:%d id:%s", aSpc, fOffSet, fID.Data());
00296       if (fOffSet > -1)
00297          Info("PrintEstblshed","+ %s expiring: %s",aSpc,fExpDate.AsString());
00298    }
00299 }
00300 
00301 //______________________________________________________________________________
00302 const char *TSecContext::AsString(TString &out)
00303 {
00304    // Returns short string with relevant information about this
00305    // security context
00306 
00307    if (fOffSet > -1) {
00308       char expdate[32];
00309       out = Form("Method: %d (%s) expiring on %s",
00310                  fMethod, GetMethodName(), fExpDate.AsString(expdate));
00311    } else {
00312       if (fOffSet == -1)
00313          out = Form("Method: %d (%s) not reusable", fMethod, GetMethodName());
00314       else if (fOffSet == -3)
00315          out = Form("Method: %d (%s) authorized by /etc/hosts.equiv or $HOME/.rhosts",
00316                     fMethod, GetMethodName());
00317       else if (fOffSet == -4)
00318          out = Form("No authentication required remotely");
00319    }
00320    return out.Data();
00321 }
00322 
00323 //______________________________________________________________________________
00324 Bool_t TSecContext::CleanupSecContext(Bool_t)
00325 {
00326    // Ask remote client to cleanup security context 'ctx'
00327    // If 'all', all sec context with the same host as ctx
00328    // are cleaned.
00329 
00330    AbstractMethod("CleanupSecContext");
00331    return kFALSE;
00332 }

Generated on Tue Jul 5 14:46:11 2011 for ROOT_528-00b_version by  doxygen 1.5.1