00001
00002
00003
00004
00005
00006
00007
00008
00009
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
00023
00024
00025
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
00045
00046 extern "C" {
00047 TAFS *GetTAFS(const char *f, const char *u, Int_t lf) {
00048
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
00061
00062
00063
00064
00065 fToken = 0;
00066
00067
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
00081 char *pw = 0;
00082 Int_t pwlen = 0;
00083 if (fpw) {
00084
00085 struct stat st;
00086 if (!stat(fpw, &st)) {
00087 pwlen = st.st_size;
00088
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
00100 if (!pw) {
00101 Info("TAFS","could not read credentials from %s", fpw);
00102 }
00103 }
00104
00105
00106 if (!pw) {
00107
00108 TString prompt = Form("AFS password for %s@%s", usr.Data(), AFSLocalCell());
00109
00110
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
00129 char buf[128];
00130 pw = buf;
00131 if (fgPasswdDialog) {
00132
00133 fgPasswdDialog->ExecPlugin(3, prompt.Data(), buf, 128);
00134
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
00149 if (pw[0]) {
00150 if (pw[strlen(pw)-1] == '\n')
00151 pw[strlen(pw) - 1] = 0;
00152 }
00153 }
00154
00155
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
00163 return;
00164 }
00165
00166
00167 TAFS::~TAFS()
00168 {
00169
00170
00171 if (fToken)
00172 DeleteAFSToken(fToken);
00173 }
00174
00175
00176 Int_t TAFS::Verify()
00177 {
00178
00179
00180 return (fToken ? VerifyAFSToken(fToken) : -1);
00181 }
00182
00183
00184 void TAFS::SetUsePwdDialog(Bool_t on)
00185 {
00186
00187
00188 fgUsePwdDialog = on;
00189 }