00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
00054
00055 R__ASSERT(gROOT);
00056
00057 fRSAKey = key;
00058 fMethodName = TAuthenticate::GetAuthMethod(fMethod);
00059 }
00060
00061
00062 TRootSecContext::~TRootSecContext()
00063 {
00064
00065
00066
00067 TSecContext::Cleanup();
00068 }
00069
00070
00071 void TRootSecContext::DeActivate(Option_t *Opt)
00072 {
00073
00074
00075
00076
00077
00078
00079
00080
00081 Bool_t clean = (strstr(Opt,"C") || strstr(Opt,"c"));
00082 if (clean && fOffSet > -1)
00083 CleanupSecContext(kFALSE);
00084
00085
00086 if (fMethod == TAuthenticate::kClear ||
00087 fMethod == TAuthenticate::kSRP)
00088 if (fContext) {
00089 delete (TPwdCtx *)fContext;
00090 fContext = 0;
00091 }
00092
00093
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
00108 gROOT->GetListOfSecContexts()->Remove(this);
00109
00110 TAuthenticate::RemoveSecContext(this);
00111 }
00112
00113
00114 fOffSet = -1;
00115 fExpDate = kROOTTZERO;
00116
00117 }
00118
00119
00120 Bool_t TRootSecContext::CleanupSecContext(Bool_t all)
00121 {
00122
00123
00124
00125 Bool_t cleaned = kFALSE;
00126
00127
00128 if (!IsActive())
00129 return kTRUE;
00130
00131
00132
00133 TIter last(fCleanup,kIterBackward);
00134 TSecContextCleanup *nscc = 0;
00135 while ((nscc = (TSecContextCleanup *)last()) && !cleaned) {
00136
00137
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
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
00199
00200
00201
00202
00203
00204
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
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
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
00257
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 }