00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00042 R__ASSERT(gROOT);
00043
00044 fContext = ctx;
00045 fCleanup = new TList;
00046 fExpDate = expdate;
00047 if (offset > -1) {
00048 if (fExpDate < TDatime()) {
00049
00050
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
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
00076
00077 R__ASSERT(gROOT);
00078
00079 fContext = ctx;
00080 fCleanup = new TList;
00081 fExpDate = expdate;
00082 if (offset > -1) {
00083 if (fExpDate < TDatime()) {
00084
00085
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
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
00119 }
00120
00121
00122 TSecContext& TSecContext::operator=(const TSecContext& sc)
00123 {
00124
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
00145
00146
00147 Cleanup();
00148 }
00149
00150 void TSecContext::Cleanup()
00151 {
00152
00153
00154 if (IsActive()) {
00155 CleanupSecContext(kTRUE);
00156 DeActivate("R");
00157
00158 TIter nxtl(gROOT->GetListOfSecContexts());
00159 TSecContext *nscl;
00160 while ((nscl = (TSecContext *)nxtl())) {
00161 if (nscl != this && !strcmp(nscl->GetHost(), fHost.Data())) {
00162
00163
00164 nscl->DeActivate("");
00165 }
00166 }
00167 }
00168
00169
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
00181
00182
00183
00184
00185
00186
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
00195 gROOT->GetListOfSecContexts()->Remove(this);
00196 }
00197
00198
00199 fOffSet = -1;
00200 fExpDate = kROOTTZERO;
00201 }
00202
00203
00204 void TSecContext::AddForCleanup(Int_t port, Int_t proto, Int_t type)
00205 {
00206
00207
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
00218
00219
00220 return Bool_t(!strcmp(methname, GetMethodName()));
00221 }
00222
00223
00224 Bool_t TSecContext::IsActive() const
00225 {
00226
00227
00228 if (fOffSet > -1 && fExpDate > TDatime())
00229 return kTRUE;
00230
00231 return kFALSE;
00232 }
00233
00234
00235 void TSecContext::Print(Option_t *opt) const
00236 {
00237
00238
00239
00240
00241
00242
00243 char aOrd[10] = {0};
00244 char aSpc[10] = {0};
00245
00246
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
00255 if (ord == -1)
00256 ord = atoi(opt);
00257
00258
00259 if (ord > -1) {
00260 snprintf(aOrd,10,"%d)",ord);
00261
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
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
00305
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
00327
00328
00329
00330 AbstractMethod("CleanupSecContext");
00331 return kFALSE;
00332 }