Logo ROOT   6.14/05
Reference Guide
TAFS.cxx
Go to the documentation of this file.
1 // @(#)root/auth:$Id$
2 // Author: G. Ganis, Nov 2006
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, 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 #ifndef WIN32
13 # include <unistd.h>
14 #else
15 # define ssize_t int
16 # include <io.h>
17 # include <sys/types.h>
18 #endif
19 
20 //////////////////////////////////////////////////////////////////////////
21 // //
22 // TAFS //
23 // //
24 // Utility class to acquire and handle an AFS tokens. //
25 // Interface to libTAFS.so. //
26 // //
27 //////////////////////////////////////////////////////////////////////////
28 
29 #include "AFSAuth.h"
30 #include "TAFS.h"
31 #include "TError.h"
32 #include "TPluginManager.h"
33 #include "TROOT.h"
34 #include "TString.h"
35 #include "TSystem.h"
36 #include "Varargs.h"
37 #include "Getline.h"
38 
41 
42 ClassImp(TAFS);
43 
44 // Hook to the constructor. This is needed to avoid using the plugin manager
45 // which may create problems in multi-threaded environments.
46 extern "C" {
47  TAFS *GetTAFS(const char *f, const char *u, Int_t lf) {
48  // Create and instance and return it only if valid
49  TAFS *afs = new TAFS(f, u, lf);
50  if (afs->Verify())
51  return afs;
52  delete afs;
53  return 0;
54  }
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Constructor: get AFS token for usr using credentials from file 'fpw'.
59 /// If 'usr' is undefined the current user is used.
60 /// If 'fpw' is undefined the caller is prompt for a password.
61 
62 TAFS::TAFS(const char *fpw, const char *user, int life)
63 {
64  // Used to test validity
65  fToken = 0;
66 
67  // Determine the user
68  TString usr = (user && strlen(user) > 0) ? user : "";
69  if (usr.IsNull()) {
71  if (u) {
72  usr = (const char *) u->fUser;
73  delete u;
74  } else {
75  Info("TAFS","user undefined");
76  return;
77  }
78  }
79 
80  // Find credentials
81  char *pw = 0;
82  Int_t pwlen = 0;
83  if (fpw) {
84  // Reading credentials from file
85  struct stat st;
86  if (!stat(fpw, &st)) {
87  pwlen = st.st_size;
88  // Open the file for reading
89  Int_t fd = open(fpw, O_RDONLY);
90  if (fd > 0) {
91  pw = new char[pwlen];
92  if (read(fd, pw, pwlen) != pwlen) {
93  delete [] pw;
94  pw = 0;
95  pwlen = 0;
96  }
97  }
98  }
99  // Notify failure
100  if (!pw) {
101  Info("TAFS","could not read credentials from %s", fpw);
102  }
103  }
104 
105  // Prompt for credentials if not yet found
106  if (!pw) {
107 
108  TString prompt = TString::Format("AFS password for %s@%s", usr.Data(), AFSLocalCell());
109 
110  // Init the dialog box, if needed
111  if (fgUsePwdDialog) {
112  if (fgPasswdDialog == (TPluginHandler *)(-1)) {
113  if (!gROOT->IsBatch()) {
114  if ((fgPasswdDialog =
115  gROOT->GetPluginManager()->FindHandler("TGPasswdDialog")))
116  if (fgPasswdDialog->LoadPlugin() == -1) {
117  fgPasswdDialog = 0;
118  Warning("TAFS",
119  "could not load plugin for the password dialog box");
120  }
121  } else
122  fgPasswdDialog = 0;
123  }
124  } else {
125  fgPasswdDialog = 0;
126  }
127 
128  // Get the password now
129  char buf[128];
130  pw = buf;
131  if (fgPasswdDialog) {
132  // Use graphic dialog
133  fgPasswdDialog->ExecPlugin(3, prompt.Data(), buf, 128);
134  // Wait until the user is done
135  while (gROOT->IsInterrupted())
137  } else {
138  if (isatty(0) != 0 && isatty(1) != 0) {
139  Gl_config("noecho", 1);
140  pw = Getline((char *) prompt.Data());
141  Gl_config("noecho", 0);
142  } else {
143  Warning("TAFS", "not tty: cannot prompt for passwd: failure");
144  pw[0] = 0;
145  }
146  }
147 
148  // Final checks
149  if (pw[0]) {
150  if (pw[strlen(pw)-1] == '\n')
151  pw[strlen(pw) - 1] = 0; // get rid of \n
152  }
153  }
154 
155  // Now get the token
156  char *emsg;
157  if (!(fToken = GetAFSToken(usr, pw, pwlen, life, &emsg))) {
158  Info("TAFS", "token acquisition failed: %s", emsg);
159  return;
160  }
161 
162  // Success
163  return;
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Destructor
168 
170 {
171  if (fToken)
173 }
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 /// Return seconds to expiration (negative means expired)
177 
179 {
180  return (fToken ? VerifyAFSToken(fToken) : -1);
181 }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// Switch on/off usage of password dialog box
185 
187 {
188  fgUsePwdDialog = on;
189 }
virtual ~TAFS()
Destructor.
Definition: TAFS.cxx:169
void * fToken
Definition: TAFS.h:31
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:854
TAFS(const char *fn=0, const char *usr=0, int life=-1)
Constructor: get AFS token for usr using credentials from file &#39;fpw&#39;.
Definition: TAFS.cxx:62
static Bool_t fgUsePwdDialog
Definition: TAFS.h:33
Int_t Verify()
Return seconds to expiration (negative means expired)
Definition: TAFS.cxx:178
static void SetUsePwdDialog(Bool_t on=kTRUE)
Switch on/off usage of password dialog box.
Definition: TAFS.cxx:186
#define gROOT
Definition: TROOT.h:410
Int_t LoadPlugin()
Load the plugin library for this handler.
Basic string class.
Definition: TString.h:131
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
char * AFSLocalCell()
Returns a pointer to a string with the local cell.
Definition: AFSAuth.cxx:239
static TPluginHandler * fgPasswdDialog
Definition: TAFS.h:34
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2286
virtual UserGroup_t * GetUserInfo(Int_t uid)
Returns all user info in the UserGroup_t structure.
Definition: TSystem.cxx:1574
TAFS * GetTAFS(const char *f, const char *u, Int_t lf)
Definition: TAFS.cxx:47
void * GetAFSToken(const char *usr, const char *pwd, int pwlen=-1, int life=DFLTTOKENLIFETIME, char **emsg=0)
Get AFS token for the local cell for &#39;usr&#39;.
Definition: AFSAuth.cxx:93
int VerifyAFSToken(void *token)
Verify validity an AFS token.
Definition: AFSAuth.cxx:212
void DeleteAFSToken(void *token)
Delete an AFS token returned by a successful call to GetAFSToken.
Definition: AFSAuth.cxx:229
TString fUser
Definition: TSystem.h:142
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
Long_t ExecPlugin(int nargs, const T &... params)
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:359
virtual void DispatchOneEvent(Bool_t pendingOnly=kFALSE)
Dispatch a single event.
Definition: TSystem.cxx:437
Bool_t IsNull() const
Definition: TString.h:402
const Bool_t kTRUE
Definition: RtypesCore.h:87
Definition: TAFS.h:29
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:866
const char * Data() const
Definition: TString.h:364