TAFS.cxx

Go to the documentation of this file.
00001 // @(#)root/auth:$Id: TAFS.cxx 30851 2009-10-23 14:50:29Z ganis $
00002 // Author: G. Ganis, Nov 2006
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, 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 #ifndef WIN32
00013 #   include <unistd.h>
00014 #else
00015 #   define ssize_t int
00016 #   include <io.h>
00017 #   include <sys/types.h>
00018 #endif
00019 
00020 //////////////////////////////////////////////////////////////////////////
00021 //                                                                      //
00022 // TAFS                                                                 //
00023 //                                                                      //
00024 // Utility class to acquire and handle an AFS tokens.                   //
00025 // Interface to libTAFS.so.                                             //
00026 //                                                                      //
00027 //////////////////////////////////////////////////////////////////////////
00028 
00029 #include "AFSAuth.h"
00030 #include "TAFS.h"
00031 #include "TError.h"
00032 #include "TPluginManager.h"
00033 #include "TROOT.h"
00034 #include "TString.h"
00035 #include "TSystem.h"
00036 #include "Varargs.h"
00037 #include "Getline.h"
00038 
00039 Bool_t          TAFS::fgUsePwdDialog = kTRUE;
00040 TPluginHandler *TAFS::fgPasswdDialog = (TPluginHandler *)(-1);
00041 
00042 ClassImp(TAFS)
00043 
00044 // Hook to the constructor. This is needed to avoid using the plugin manager
00045 // which may create problems in multi-threaded environments.
00046 extern "C" {
00047    TAFS *GetTAFS(const char *f, const char *u, Int_t lf) {
00048       // Create and instance and return it only if valid
00049       TAFS *afs = new TAFS(f, u, lf);
00050       if (afs->Verify())
00051          return afs;
00052       delete afs;
00053       return 0;
00054    }
00055 }
00056 
00057 //________________________________________________________________________
00058 TAFS::TAFS(const char *fpw, const char *user, int life)
00059 {
00060    // Constructor: get AFS token for usr using credentials from file 'fpw'.
00061    // If 'usr' is undefined the current user is used.
00062    // If 'fpw' is undefined the caller is prompt for a password.
00063 
00064    // Used to test validity
00065    fToken = 0;
00066 
00067    // Determine the user
00068    TString usr = (user && strlen(user) > 0) ? user : "";
00069    if (usr.IsNull()) {
00070       UserGroup_t *u = gSystem->GetUserInfo();
00071       if (u) {
00072          usr = (const char *) u->fUser;
00073          delete u;
00074       } else {
00075          Info("TAFS","user undefined");
00076          return;
00077       }
00078    }
00079 
00080    // Find credentials
00081    char *pw = 0;
00082    Int_t pwlen = 0;
00083    if (fpw) {
00084       // Reading credentials from file
00085       struct stat st;
00086       if (!stat(fpw, &st)) {
00087          pwlen = st.st_size;
00088          // Open the file for reading
00089          Int_t fd = open(fpw, O_RDONLY);
00090          if (fd > 0) {
00091             pw = new char[pwlen];
00092             if (read(fd, pw, pwlen) != pwlen) {
00093                delete [] pw;
00094                pw = 0;
00095                pwlen = 0;
00096             }
00097          }
00098       }
00099       // Notify failure
00100       if (!pw) {
00101          Info("TAFS","could not read credentials from %s", fpw);
00102       }
00103    }
00104 
00105    // Prompt for credentials if not yet found
00106    if (!pw) {
00107 
00108       TString prompt = Form("AFS password for %s@%s", usr.Data(), AFSLocalCell());
00109 
00110       // Init the dialog box, if needed
00111       if (fgUsePwdDialog) {
00112          if (fgPasswdDialog == (TPluginHandler *)(-1)) {
00113             if (!gROOT->IsBatch()) {
00114                if ((fgPasswdDialog =
00115                     gROOT->GetPluginManager()->FindHandler("TGPasswdDialog")))
00116                   if (fgPasswdDialog->LoadPlugin() == -1) {
00117                      fgPasswdDialog = 0;
00118                      Warning("TAFS",
00119                              "could not load plugin for the password dialog box");
00120                   }
00121             } else
00122                fgPasswdDialog = 0;
00123          }
00124       } else {
00125          fgPasswdDialog = 0;
00126       }
00127 
00128       // Get the password now
00129       char buf[128];
00130       pw = buf;
00131       if (fgPasswdDialog) {
00132          // Use graphic dialog
00133          fgPasswdDialog->ExecPlugin(3, prompt.Data(), buf, 128);
00134          // Wait until the user is done
00135          while (gROOT->IsInterrupted())
00136             gSystem->DispatchOneEvent(kFALSE);
00137       } else {
00138          if (isatty(0) != 0 && isatty(1) != 0) {
00139             Gl_config("noecho", 1);
00140             pw = Getline((char *) prompt.Data());
00141             Gl_config("noecho", 0);
00142          } else {
00143             Warning("TAFS", "not tty: cannot prompt for passwd: failure");
00144             pw[0] = 0;
00145          }
00146       }
00147 
00148       // Final checks
00149       if (pw[0]) {
00150          if (pw[strlen(pw)-1] == '\n')
00151             pw[strlen(pw) - 1] = 0;   // get rid of \n
00152       }
00153    }
00154 
00155    // Now get the token
00156    char *emsg;
00157    if (!(fToken = GetAFSToken(usr, pw, pwlen, life, &emsg))) {
00158       Info("TAFS", "token acquisition failed: %s", emsg);
00159       return;
00160    }
00161 
00162    // Success
00163    return;
00164 }
00165 
00166 //________________________________________________________________________
00167 TAFS::~TAFS()
00168 {
00169    // Destructor
00170 
00171    if (fToken)
00172       DeleteAFSToken(fToken);
00173 }
00174 
00175 //________________________________________________________________________
00176 Int_t TAFS::Verify()
00177 {
00178    // Return seconds to expiration (negative means expired)
00179 
00180    return (fToken ? VerifyAFSToken(fToken) : -1);
00181 }
00182 
00183 //________________________________________________________________________
00184 void TAFS::SetUsePwdDialog(Bool_t on)
00185 {
00186    // Switch on/off usage of password dialog box
00187 
00188    fgUsePwdDialog = on;
00189 }

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