ROOT  6.06/09
Reference Guide
TSecContext.cxx
Go to the documentation of this file.
1 // @(#)root/net:$Id$
2 // Author: G. Ganis 19/03/2003
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TSecContext //
15 // //
16 // Contains details about an established security context //
17 // Used by THostAuth //
18 // //
19 //////////////////////////////////////////////////////////////////////////
20 
21 #include "RConfigure.h"
22 
23 #include <stdlib.h>
24 
25 #include "TSecContext.h"
26 #include "TSocket.h"
27 #include "TUrl.h"
28 #include "TROOT.h"
29 #include "TError.h"
30 #include "TVirtualMutex.h"
31 
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// Ctor for SecContext object.
37 
38 TSecContext::TSecContext(const char *user, const char *host, Int_t meth,
39  Int_t offset, const char *id,
40  const char *token, TDatime expdate, void *ctx)
41  : TObject()
42 {
44 
45  fContext = ctx;
46  fCleanup = new TList;
47  fExpDate = expdate;
48  if (offset > -1) {
49  if (fExpDate < TDatime()) {
50  // This means expdate was not initialized
51  // We set it to default, ie 1 day from now
52  fExpDate.Set(TDatime().GetDate() + 1, TDatime().GetTime());
53  }
54  }
55  fHost = host;
56  fID = id;
57  fMethod = meth;
58  fMethodName = "";
59  fOffSet = offset;
60  fToken = token;
61  fUser = user;
62 
63  // Keep official list updated with active TSecContexts
64  if (fOffSet > -1) {
66  gROOT->GetListOfSecContexts()->Add(this);
67  }
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Ctor for SecContext object.
72 /// User and host from url = user@host .
73 
74 TSecContext::TSecContext(const char *url, Int_t meth, Int_t offset,
75  const char *token, const char *id,
76  TDatime expdate, void *ctx)
77  : TObject()
78 {
80 
81  fContext = ctx;
82  fCleanup = new TList;
83  fExpDate = expdate;
84  if (offset > -1) {
85  if (fExpDate < TDatime()) {
86  // This means expdate was not initialized
87  // We set it to default, ie 1 day from now
88  fExpDate.Set(TDatime().GetDate() + 1, TDatime().GetTime());
89  }
90  }
91  fHost = TUrl(url).GetHost();
92  fID = id;
93  fMethod = meth;
94  fMethodName = "";
95  fOffSet = offset;
96  fToken = token;
97  fUser = TUrl(url).GetUser();
98 
99  // Keep official list updated with active TSecContexts
100  if (fOffSet > -1) {
102  gROOT->GetListOfSecContexts()->Add(this);
103  }
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 ///copy constructor
108 
110  TObject(sc),
111  fContext(sc.fContext),
112  fCleanup(sc.fCleanup),
113  fExpDate(sc.fExpDate),
114  fHost(sc.fHost),
115  fID(sc.fID),
116  fMethod(sc.fMethod),
117  fMethodName(sc.fMethodName),
118  fOffSet(sc.fOffSet),
119  fToken(sc.fToken),
120  fUser(sc.fUser)
121 {
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 ///assignement operator
126 
128 {
129  if(this!=&sc) {
130  TObject::operator=(sc);
131  fContext=sc.fContext;
132  fCleanup=sc.fCleanup;
133  fExpDate=sc.fExpDate;
134  fHost=sc.fHost;
135  fID=sc.fID;
136  fMethod=sc.fMethod;
138  fOffSet=sc.fOffSet;
139  fToken=sc.fToken;
140  fUser=sc.fUser;
141  }
142  return *this;
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// Dtor: delete (deActivate, local/remote cleanup, list removal)
147 /// all what is still active
148 
150 {
151  Cleanup();
152 }
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Cleanup what is still active
155 
157 {
158  if (IsActive()) {
160  DeActivate("R");
161  // All have been remotely Deactivated
162  TIter nxtl(gROOT->GetListOfSecContexts());
163  TSecContext *nscl;
164  while ((nscl = (TSecContext *)nxtl())) {
165  if (nscl != this && !strcmp(nscl->GetHost(), fHost.Data())) {
166  // Need to set ofs=-1 to avoid sending another
167  // cleanup request
168  nscl->DeActivate("");
169  }
170  }
171  }
172 
173  // Delete the cleanup list
174  if (fCleanup) {
175  fCleanup->Delete();
176  delete fCleanup;
177  fCleanup = 0;
178  }
179 }
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 /// Set OffSet to -1 and expiring Date to default
183 /// Remove from the list
184 /// If Opt contains "C" or "c", ask for remote cleanup
185 /// If Opt contains "R" or "r", remove from the list
186 /// Default Opt="CR"
187 
189 {
190  // Ask remote cleanup of this context
191  Bool_t clean = (strstr(Opt,"C") || strstr(Opt,"c"));
192  if (clean && fOffSet > -1)
194 
195  Bool_t remove = (strstr(Opt,"R") || strstr(Opt,"r"));
196  if (remove && fOffSet > -1){
198  // Remove from the global list
199  gROOT->GetListOfSecContexts()->Remove(this);
200  }
201 
202  // Set inactive
203  fOffSet = -1;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Create a new TSecContextCleanup
209 /// Internally is added to the list
210 
212 {
213  TSecContextCleanup *tscc = new TSecContextCleanup(port, proto, type);
214  fCleanup->Add(tscc);
215 
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// Checks if this security context is for method named 'methname'
220 /// Case sensitive.
221 
222 Bool_t TSecContext::IsA(const char *methname)
223 {
224  return Bool_t(!strcmp(methname, GetMethodName()));
225 }
226 
227 ////////////////////////////////////////////////////////////////////////////////
228 /// Check remote OffSet and expiring Date
229 
231 {
232  if (fOffSet > -1 && fExpDate > TDatime())
233  return kTRUE;
234  // Invalid
235  return kFALSE;
236 }
237 
238 ////////////////////////////////////////////////////////////////////////////////
239 /// If opt is "F" (default) print object content.
240 /// If opt is "<number>" print in special form for calls within THostAuth
241 /// with cardinality <number>
242 /// If opt is "S" prints short in-line form for calls within TFTP,
243 /// TSlave, TProof ...
244 
245 void TSecContext::Print(Option_t *opt) const
246 {
247  char aOrd[10] = {0};
248  char aSpc[10] = {0};
249 
250  // Check if option is numeric
251  Int_t ord = -1, i = 0;
252  for (; i < (Int_t)strlen(opt); i++) {
253  if (opt[i] < 48 || opt[i] > 57) {
254  ord = -2;
255  break;
256  }
257  }
258  // If numeric get the cardinality and prepare the strings
259  if (ord == -1)
260  ord = atoi(opt);
261 
262  // If asked to print ordinal number, preapre the string
263  if (ord > -1) {
264  snprintf(aOrd,10,"%d)",ord);
265  // and take care of alignment
266  Int_t len=strlen(aOrd);
267  while (len--)
268  strlcat(aSpc," ",10);
269  }
270 
271  if (!strncasecmp(opt,"F",1)) {
272  Info("Print",
273  "+------------------------------------------------------+");
274  Info("Print",
275  "+ Host:%s Method:%d (%s) User:'%s'",
277  fUser.Data());
278  Info("Print",
279  "+ OffSet:%d, id:%s", fOffSet, fID.Data());
280  if (fOffSet > -1)
281  Info("Print",
282  "+ Expiration time: %s",fExpDate.AsString());
283  Info("Print",
284  "+------------------------------------------------------+");
285  } else if (!strncasecmp(opt,"S",1)) {
286  if (fOffSet > -1) {
287  Printf("Security context: Method: %d (%s) expiring on %s",
289  fExpDate.AsString());
290  } else {
291  Printf("Security context: Method: %d (%s) not reusable",
293  }
294  } else {
295  // special printing form for THostAuth
296  Info("PrintEstblshed","+ %s h:%s met:%d (%s) us:'%s'",
297  aOrd, GetHost(), fMethod, GetMethodName(),
298  fUser.Data());
299  Info("PrintEstblshed","+ %s offset:%d id:%s", aSpc, fOffSet, fID.Data());
300  if (fOffSet > -1)
301  Info("PrintEstblshed","+ %s expiring: %s",aSpc,fExpDate.AsString());
302  }
303 }
304 
305 ////////////////////////////////////////////////////////////////////////////////
306 /// Returns short string with relevant information about this
307 /// security context
308 
310 {
311  if (fOffSet > -1) {
312  char expdate[32];
313  out = Form("Method: %d (%s) expiring on %s",
314  fMethod, GetMethodName(), fExpDate.AsString(expdate));
315  } else {
316  if (fOffSet == -1)
317  out = Form("Method: %d (%s) not reusable", fMethod, GetMethodName());
318  else if (fOffSet == -3)
319  out = Form("Method: %d (%s) authorized by /etc/hosts.equiv or $HOME/.rhosts",
321  else if (fOffSet == -4)
322  out = Form("No authentication required remotely");
323  }
324  return out.Data();
325 }
326 
327 ////////////////////////////////////////////////////////////////////////////////
328 /// Ask remote client to cleanup security context 'ctx'
329 /// If 'all', all sec context with the same host as ctx
330 /// are cleaned.
331 
333 {
334  AbstractMethod("CleanupSecContext");
335  return kFALSE;
336 }
const char * GetHost() const
Definition: TUrl.h:76
TSecContext(const TSecContext &)
copy constructor
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:404
void Set()
Set Date/Time to current time as reported by the system.
Definition: TDatime.cxx:286
void AddForCleanup(Int_t port, Int_t proto, Int_t type)
Create a new TSecContextCleanup Internally is added to the list.
const char Option_t
Definition: RtypesCore.h:62
This class represents a WWW compatible URL.
Definition: TUrl.h:41
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:892
virtual ~TSecContext()
Dtor: delete (deActivate, local/remote cleanup, list removal) all what is still active.
const char * GetHost() const
Definition: TSecContext.h:81
const char * GetMethodName() const
Definition: TSecContext.h:84
#define R__ASSERT(e)
Definition: TError.h:98
#define gROOT
Definition: TROOT.h:340
Basic string class.
Definition: TString.h:137
Bool_t IsActive() const
Check remote OffSet and expiring Date.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TString fUser
Definition: TSecContext.h:56
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:63
const Bool_t kFALSE
Definition: Rtypes.h:92
TString fMethodName
Definition: TSecContext.h:53
TString fToken
Definition: TSecContext.h:55
const char * Data() const
Definition: TString.h:349
const char * ord
Definition: TXSlave.cxx:46
UChar_t mod R__LOCKGUARD2(gSrvAuthenticateMutex)
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.cxx:102
XFontStruct * id
Definition: TGX11.cxx:108
void Cleanup()
Cleanup what is still active.
char * out
Definition: TBase64.cxx:29
Int_t fMethod
Definition: TSecContext.h:52
A doubly linked list.
Definition: TList.h:47
TString fID
Definition: TSecContext.h:51
virtual Bool_t CleanupSecContext(Bool_t all)
Ask remote client to cleanup security context 'ctx' If 'all', all sec context with the same host as c...
TList * fCleanup
Definition: TSecContext.h:48
TString fHost
Definition: TSecContext.h:50
Bool_t IsA(const char *methodname)
Checks if this security context is for method named 'methname' Case sensitive.
TSecContext & operator=(const TSecContext &)
assignement operator
char * Form(const char *fmt,...)
virtual void DeActivate(Option_t *opt="CR")
Set OffSet to -1 and expiring Date to default Remove from the list If Opt contains "C" or "c"...
void AbstractMethod(const char *method) const
Use this method to implement an "abstract" method that you don't want to leave purely abstract...
Definition: TObject.cxx:960
virtual const char * AsString(TString &out)
Returns short string with relevant information about this security context.
#define Printf
Definition: TGeoToOCC.h:18
int type
Definition: TGX11.cxx:120
virtual void Print(Option_t *option="F") const
If opt is "F" (default) print object content.
const TDatime kROOTTZERO
Definition: TSecContext.h:36
ClassImp(TSecContext) ClassImp(TSecContextCleanup) TSecContext
Ctor for SecContext object.
Definition: TSecContext.cxx:32
Mother of all ROOT objects.
Definition: TObject.h:58
virtual void Add(TObject *obj)
Definition: TList.h:81
const char * AsString() const
Return the date & time as a string (ctime() format).
Definition: TDatime.cxx:99
const Bool_t kTRUE
Definition: Rtypes.h:91
Int_t fOffSet
Definition: TSecContext.h:54
const char * GetUser() const
Definition: TUrl.h:74
void * fContext
Definition: TSecContext.h:47
TDatime fExpDate
Definition: TSecContext.h:49
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:39