TRootSecContext.cxx

Go to the documentation of this file.
00001 // @(#)root/auth:$Id: TRootSecContext.cxx 36467 2010-11-01 17:58:35Z ganis $
00002 // Author: G. Ganis   08/07/2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, 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 // TRootSecContext                                                      //
00015 //                                                                      //
00016 // Special implementation of TSecContext                                //
00017 //                                                                      //
00018 //////////////////////////////////////////////////////////////////////////
00019 
00020 #include "RConfigure.h"
00021 
00022 #include <stdlib.h>
00023 
00024 #include "TError.h"
00025 #include "TRootSecContext.h"
00026 #include "TROOT.h"
00027 #include "TSocket.h"
00028 #include "TUrl.h"
00029 #include "TVirtualMutex.h"
00030 
00031 ClassImp(TRootSecContext)
00032 
00033 //______________________________________________________________________________
00034    TRootSecContext::TRootSecContext(const char *user, const char *host, Int_t meth,
00035                                     Int_t offset, const char *id,
00036                                     const char *token, TDatime expdate,
00037                                     void *ctx, Int_t key)
00038       : TSecContext(user, host, meth, offset, id, token, expdate, ctx)
00039 {
00040    // Ctor for SecContext object.
00041    R__ASSERT(gROOT);
00042 
00043    fRSAKey  = key;
00044    fMethodName = TAuthenticate::GetAuthMethod(fMethod);
00045 }
00046 
00047 //______________________________________________________________________________
00048 TRootSecContext::TRootSecContext(const char *url, Int_t meth, Int_t offset,
00049                                  const char *id, const char *token,
00050                                  TDatime expdate, void *ctx, Int_t key)
00051    : TSecContext(url, meth, offset, id, token, expdate, ctx)
00052 {
00053    // Ctor for SecContext object.
00054    // User and host from url = user@host .
00055    R__ASSERT(gROOT);
00056 
00057    fRSAKey  = key;
00058    fMethodName = TAuthenticate::GetAuthMethod(fMethod);
00059 }
00060 
00061 //______________________________________________________________________________
00062 TRootSecContext::~TRootSecContext()
00063 {
00064    // Dtor: delete (deActivate, local/remote cleanup, list removal)
00065    // all what is still active
00066 
00067    TSecContext::Cleanup();
00068 }
00069 
00070 //______________________________________________________________________________
00071 void TRootSecContext::DeActivate(Option_t *Opt)
00072 {
00073    // Set OffSet to -1 and expiring Date to default
00074    // Remove from the list
00075    // If globus, cleanup local stuff
00076    // If Opt contains "C" or "c", ask for remote cleanup
00077    // If Opt contains "R" or "r", remove from the list
00078    // Default Opt="CR"
00079 
00080    // Ask remote cleanup of this context
00081    Bool_t clean = (strstr(Opt,"C") || strstr(Opt,"c"));
00082    if (clean && fOffSet > -1)
00083       CleanupSecContext(kFALSE);
00084 
00085    // Cleanup TPwdCtx object fro UsrPwd and SRP
00086    if (fMethod == TAuthenticate::kClear ||
00087        fMethod == TAuthenticate::kSRP)
00088       if (fContext) {
00089          delete (TPwdCtx *)fContext;
00090          fContext = 0;
00091       }
00092 
00093    // Cleanup globus security context if needed
00094    if (fMethod == TAuthenticate::kGlobus && fContext) {
00095       GlobusAuth_t globusAuthHook = TAuthenticate::GetGlobusAuthHook();
00096       if (globusAuthHook != 0) {
00097          TString det("context");
00098          TString us("-1");
00099          (*globusAuthHook)((TAuthenticate *)fContext,us,det);
00100          fContext = 0;
00101       }
00102    }
00103 
00104    Bool_t remove = (strstr(Opt,"R") || strstr(Opt,"r"));
00105    if (remove && fOffSet > -1){
00106       R__LOCKGUARD2(gROOTMutex);
00107       // Remove from the global list
00108       gROOT->GetListOfSecContexts()->Remove(this);
00109       // Remove also from local lists in THostAuth objects
00110       TAuthenticate::RemoveSecContext(this);
00111    }
00112 
00113    // Set inactive
00114    fOffSet  = -1;
00115    fExpDate = kROOTTZERO;
00116 
00117 }
00118 
00119 //______________________________________________________________________________
00120 Bool_t TRootSecContext::CleanupSecContext(Bool_t all)
00121 {
00122    // Ask remote client to cleanup security context 'ctx'
00123    // If 'all', all sec context with the same host as ctx
00124    // are cleaned.
00125    Bool_t cleaned = kFALSE;
00126 
00127    // Nothing to do if inactive ...
00128    if (!IsActive())
00129       return kTRUE;
00130 
00131    // Contact remote services that used this context,
00132    // starting from the last ...
00133    TIter last(fCleanup,kIterBackward);
00134    TSecContextCleanup *nscc = 0;
00135    while ((nscc = (TSecContextCleanup *)last()) && !cleaned) {
00136 
00137       // First check if remote daemon supports cleaning
00138       Int_t srvtyp = nscc->GetType();
00139       Int_t rproto = nscc->GetProtocol();
00140       Int_t level = 2;
00141       if ((srvtyp == TSocket::kROOTD && rproto < 10) ||
00142           (srvtyp == TSocket::kPROOFD && rproto < 9))
00143          level = 1;
00144       if ((srvtyp == TSocket::kROOTD && rproto < 8) ||
00145           (srvtyp == TSocket::kPROOFD && rproto < 7))
00146          level = 0;
00147       if (level) {
00148          Int_t port = nscc->GetPort();
00149 
00150          TSocket *news = new TSocket(fHost.Data(),port,-1);
00151 
00152          if (news && news->IsValid()) {
00153             if (srvtyp == TSocket::kPROOFD) {
00154                news->SetOption(kNoDelay, 1);
00155                news->Send("cleaning request");
00156             } else
00157                news->SetOption(kNoDelay, 0);
00158 
00159             // Backward compatibility: send socket size
00160             if (srvtyp == TSocket::kROOTD && level == 1)
00161                news->Send((Int_t)0, (Int_t)0);
00162 
00163             if (all || level == 1) {
00164                news->Send(Form("%d",TAuthenticate::fgProcessID), kROOTD_CLEANUP);
00165                cleaned = kTRUE;
00166             } else {
00167                news->Send(Form("%d %d %d %s", TAuthenticate::fgProcessID, fMethod,
00168                                fOffSet, fUser.Data()), kROOTD_CLEANUP);
00169                if (TAuthenticate::SecureSend(news, 1, fRSAKey,
00170                                              (char *)(fToken.Data())) == -1) {
00171                   Info("CleanupSecContext", "problems secure-sending token");
00172                } else {
00173                   cleaned = kTRUE;
00174                }
00175             }
00176             if (cleaned && gDebug > 2) {
00177                char srvname[3][10] = {"sockd", "rootd", "proofd"};
00178                Info("CleanupSecContext",
00179                     "remote %s notified for cleanup (%s,%d)",
00180                     srvname[srvtyp],fHost.Data(),port);
00181             }
00182          }
00183          SafeDelete(news);
00184       }
00185    }
00186 
00187    if (!cleaned)
00188       if (gDebug > 2)
00189          Info("CleanupSecContext",
00190               "unable to open valid socket for cleanup for %s", fHost.Data());
00191 
00192    return cleaned;
00193 }
00194 
00195 //______________________________________________________________________________
00196 void TRootSecContext::Print(Option_t *opt) const
00197 {
00198    // If opt is "F" (default) print object content.
00199    // If opt is "<number>" print in special form for calls within THostAuth
00200    // with cardinality <number>
00201    // If opt is "S" prints short in-line form for calls within TFTP,
00202    // TSlave, TProof ...
00203 
00204    // Check if option is numeric
00205    Int_t ord = -1, i = 0;
00206    for (; i < (Int_t)strlen(opt); i++) {
00207       if (opt[i] < 48 || opt[i] > 57) {
00208          ord = -2;
00209          break;
00210       }
00211    }
00212    // If numeric get the cardinality and prepare the strings
00213    if (ord == -1)
00214       ord = atoi(opt);
00215 
00216    if (!strncasecmp(opt,"F",1)) {
00217       Info("Print",
00218            "+------------------------------------------------------+");
00219       Info("Print",
00220            "+ Host:%s Method:%d (%s) User:'%s'",
00221            GetHost(), fMethod, GetMethodName(),
00222            fUser.Data());
00223       Info("Print",
00224            "+         OffSet:%d Id: '%s'", fOffSet, fID.Data());
00225       if (fOffSet > -1)
00226          Info("Print",
00227               "+         Expiration time: %s",fExpDate.AsString());
00228       Info("Print",
00229            "+------------------------------------------------------+");
00230    } else if (!strncasecmp(opt,"S",1)) {
00231       if (fOffSet > -1) {
00232          if (fID.BeginsWith("AFS"))
00233             Printf("Security context:     Method: AFS, not reusable");
00234          else
00235             Printf("Security context:     Method: %d (%s) expiring on %s",
00236                    fMethod, GetMethodName(),
00237                    fExpDate.AsString());
00238       } else {
00239          Printf("Security context:     Method: %d (%s) not reusable",
00240                 fMethod, GetMethodName());
00241       }
00242    } else {
00243       // special printing form for THostAuth
00244       Info("PrintEstblshed","+ %d \t h:%s met:%d (%s) us:'%s'",
00245                                ord, GetHost(), fMethod, GetMethodName(),
00246                                fUser.Data());
00247       Info("PrintEstblshed","+ \t offset:%d id: '%s'", fOffSet, fID.Data());
00248       if (fOffSet > -1)
00249          Info("PrintEstblshed","+ \t expiring: %s",fExpDate.AsString());
00250    }
00251 }
00252 
00253 //______________________________________________________________________________
00254 const char *TRootSecContext::AsString(TString &out)
00255 {
00256    // Returns short string with relevant information about this
00257    // security context
00258 
00259    if (fOffSet > -1) {
00260       if (fID.BeginsWith("AFS"))
00261          out = Form("Method: AFS, not reusable");
00262       else {
00263          char expdate[32];
00264          out = Form("Method: %d (%s) expiring on %s",
00265                     fMethod, GetMethodName(), fExpDate.AsString(expdate));
00266       }
00267    } else {
00268       if (fOffSet == -1)
00269          out = Form("Method: %d (%s) not reusable", fMethod, GetMethodName());
00270       else if (fOffSet == -3)
00271          out = Form("Method: %d (%s) authorized by /etc/hosts.equiv or $HOME/.rhosts",
00272                     fMethod, GetMethodName());
00273       else if (fOffSet == -4)
00274          out = Form("No authentication required remotely");
00275    }
00276    return out.Data();
00277 }

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