21 #include "RConfigure.h"
49 #include <sys/types.h>
51 #if !defined(R__WIN32) && !defined(R__MACOSX) && !defined(R__FBSD) && \
58 #if defined(R__LINUX) || defined(R__FBSD) || defined(R__OBSD)
63 # include <sys/time.h>
66 #if defined(R__MACOSX)
67 extern "C" char *crypt(
const char *,
const char *);
72 # include <openssl/bio.h>
73 # include <openssl/err.h>
74 # include <openssl/pem.h>
75 # include <openssl/rand.h>
76 # include <openssl/rsa.h>
77 # include <openssl/ssl.h>
78 # include <openssl/blowfish.h>
81 struct R__rsa_KEY: rsa_KEY { R__rsa_KEY(): rsa_KEY() {} };
82 struct R__rsa_KEY_export: rsa_KEY_export {};
83 struct R__rsa_NUMBER: rsa_NUMBER {};
86 static BF_KEY fgBFKey;
90 TList *TAuthenticate::fgAuthInfo = 0;
91 TString TAuthenticate::fgAuthMeth[] = {
"UsrPwd",
"Unsupported",
"Unsupported",
92 "Unsupported",
"Unsupported",
"Unsupported" };
93 Bool_t TAuthenticate::fgAuthReUse;
94 TString TAuthenticate::fgDefaultUser;
95 TDatime TAuthenticate::fgExpDate;
96 TDatime TAuthenticate::fgLastAuthrc;
97 TString TAuthenticate::fgPasswd;
98 TPluginHandler *TAuthenticate::fgPasswdDialog = (TPluginHandler *)(-1);
99 Bool_t TAuthenticate::fgPromptUser;
100 TList *TAuthenticate::fgProofAuthInfo = 0;
101 Bool_t TAuthenticate::fgPwHash;
102 Bool_t TAuthenticate::fgReadHomeAuthrc = kTRUE;
103 TString TAuthenticate::fgRootAuthrc;
104 Int_t TAuthenticate::fgRSAKey = -1;
105 Int_t TAuthenticate::fgRSAInit = 0;
106 R__rsa_KEY TAuthenticate::fgRSAPriKey;
107 R__rsa_KEY_export R__fgRSAPubExport[2] = {{}, {}};
108 R__rsa_KEY_export* TAuthenticate::fgRSAPubExport = R__fgRSAPubExport;
109 R__rsa_KEY TAuthenticate::fgRSAPubKey;
110 SecureAuth_t TAuthenticate::fgSecAuthHook;
111 TString TAuthenticate::fgUser;
112 Bool_t TAuthenticate::fgUsrPwdCrypt;
113 Int_t TAuthenticate::fgLastError = -1;
114 Int_t TAuthenticate::fgAuthTO = -2;
117 Int_t TAuthenticate::fgProcessID = -1;
119 TVirtualMutex *gAuthenticateMutex = 0;
122 Int_t StdCheckSecCtx(
const char *, TRootSecContext *);
125 ClassImp(TAuthenticate);
130 static int auth_rand()
133 int frnd = open(
"/dev/urandom", O_RDONLY);
134 if (frnd < 0) frnd = open(
"/dev/random", O_RDONLY);
137 ssize_t rs = read(frnd, (
void *) &r,
sizeof(
int));
140 if (rs ==
sizeof(
int))
return r;
142 Printf(
"+++ERROR+++ : auth_rand: neither /dev/urandom nor /dev/random are available or readable!");
144 if (gettimeofday(&tv,0) == 0) {
146 memcpy((
void *)&t1, (
void *)&tv.tv_sec,
sizeof(
int));
147 memcpy((
void *)&t2, (
void *)&tv.tv_usec,
sizeof(
int));
162 TAuthenticate::TAuthenticate(TSocket *sock,
const char *remote,
163 const char *proto,
const char *user)
165 if (gDebug > 2 && gAuthenticateMutex)
166 Info(
"Authenticate",
"locking mutex (pid: %d)",gSystem->GetPid());
167 R__LOCKGUARD2(gAuthenticateMutex);
170 if (gROOT->IsProofServ())
175 fgProcessID = gSystem->GetPid();
178 fgAuthTO = gEnv->GetValue(
"Auth.Timeout",-1);
187 Info(
"TAuthenticate",
"Enter: local host: %s, user is: %s (proto: %s)",
188 gSystem->HostName(), user, proto);
193 Int_t servtype = TSocket::kSOCKD;
194 if (proto && strlen(proto) > 0) {
195 char *sproto = StrDup(proto);
196 if ((pdd = strstr(sproto,
":")) != 0) {
197 int rproto = atoi(pdd + 1);
199 if (strstr(sproto,
"root") != 0) {
214 servtype = TSocket::kROOTD;
216 if (strstr(sproto,
"proof") != 0) {
228 servtype = TSocket::kPROOFD;
231 Info(
"TAuthenticate",
232 "service: %s (remote protocol: %d): fVersion: %d", sproto,
242 if (user && strlen(user) > 0) {
246 UserGroup_t *u = gSystem->GetUserInfo();
248 checkUser = u->fUser;
259 if (gEnv->GetValue(
"RSA.KeyType",0) == 1)
267 Info(
"TAuthenticate",
"RSA key: default type %d", fgRSAKey);
277 TInetAddress addr = gSystem->GetHostByName(fRemote);
279 fqdn = addr.GetHostName();
281 fqdnsrv.Form(
"%s:%d",fqdn.Data(),servtype);
284 TAuthenticate::ReadRootAuthrc();
287 Info(
"TAuthenticate",
288 "number of HostAuth Instantiations in memory: %d",
289 GetAuthInfo()->GetSize());
290 TAuthenticate::Show(
"H");
291 TAuthenticate::Show(
"P");
295 fHostAuth = GetHostAuth(fqdnsrv, checkUser);
300 if (strchr(fHostAuth->GetHost(),
'*') || strchr(fHostAuth->GetHost(),
'*') ||
301 fHostAuth->GetServer() == -1 ) {
302 fHostAuth =
new THostAuth(*fHostAuth);
303 fHostAuth->SetHost(fqdn);
304 fHostAuth->SetUser(checkUser);
305 fHostAuth->SetServer(servtype);
311 TString tmp = fProtocol;
312 tmp.ReplaceAll(
"root",4,
"",0);
313 tmp.ReplaceAll(
"proof",5,
"",0);
314 tmp.ReplaceAll(
"sock",4,
"",0);
315 if (!strncmp(tmp.Data(),
"up",2))
317 else if (!strncmp(tmp.Data(),
"s",1))
319 else if (!strncmp(tmp.Data(),
"k",1))
321 else if (!strncmp(tmp.Data(),
"g",1))
323 else if (!strncmp(tmp.Data(),
"h",1))
325 else if (!strncmp(tmp.Data(),
"ug",2))
327 if (sec > -1 && sec < kMAXSEC) {
328 if (fHostAuth->HasMethod(sec)) {
329 fHostAuth->SetFirst(sec);
331 char *dtmp = GetDefaultDetails(sec, 1, checkUser);
333 fHostAuth->AddFirst(sec, det);
341 TIter next(fHostAuth->Established());
342 TRootSecContext *ctx;
343 while ((ctx = (TRootSecContext *) next()))
351 void TAuthenticate::CatchTimeOut()
353 Info(
"CatchTimeOut",
"%d sec timeout expired (protocol: %s)",
354 fgAuthTO, fgAuthMeth[fSecurity].Data());
358 fSocket->Close(
"force");
367 Bool_t TAuthenticate::Authenticate()
369 if (gDebug > 2 && gAuthenticateMutex)
370 Info(
"Authenticate",
"locking mutex (pid: %d)",gSystem->GetPid());
371 R__LOCKGUARD2(gAuthenticateMutex);
375 Int_t remMeth = 0, rMth[kMAXSEC], tMth[kMAXSEC] = {0};
377 char noSupport[80] = { 0 };
378 char triedMeth[80] = { 0 };
381 TString user, passwd;
385 Info(
"Authenticate",
"enter: fUser: %s", fUser.Data());
391 alarm =
new TTimer(0, kFALSE);
392 alarm->SetInterruptSyscalls();
394 alarm->Connect(
"Timeout()",
"TAuthenticate",
this,
"CatchTimeOut()");
402 Info(
"Authenticate",
"try #: %d", ntry);
409 fSecurity = (ESecurity) fHostAuth->GetMethod(meth);
410 fDetails = fHostAuth->GetDetails((Int_t) fSecurity);
413 "trying authentication: method:%d, default details:%s",
414 fSecurity, fDetails.Data());
417 if (triedMeth[0] !=
'\0')
418 (void) strlcat(triedMeth,
" ",
sizeof(triedMeth) - 1);
420 (void) strlcat(triedMeth, fgAuthMeth[fSecurity].Data(),
sizeof(triedMeth) - 1);
430 if (fgAuthTO > 0 && alarm) {
431 alarm->Start(fgAuthTO*1000, kTRUE);
435 if (fSecurity == kClear) {
440 user = fgDefaultUser;
442 CheckNetrc(user, passwd, pwhash, kFALSE);
445 char *u = PromptUser(fRemote);
449 rc = GetUserPasswd(user, passwd, pwhash, kFALSE);
457 st = ClearAuth(user, passwd, pwhash);
459 Error(
"Authenticate",
460 "unable to get user name for UsrPwd authentication");
466 if (alarm) alarm->Stop();
469 st = (fTimeOut > 0) ? -3 : st;
477 Int_t nmet = fHostAuth->NumMethods();
478 Int_t remloc = nmet - ntry;
480 Info(
"Authenticate",
"remloc: %d, ntry: %d, meth: %d, fSecurity: %d",
481 remloc, ntry, meth, fSecurity);
488 fHostAuth->CountSuccess((Int_t)fSecurity);
490 fSecContext->Print();
491 if (fSecContext->IsActive())
492 fSecContext->AddForCleanup(fSocket->GetPort(),
493 fSocket->GetRemoteProtocol(),fSocket->GetServType());
500 fHostAuth->CountFailure((Int_t)fSecurity);
506 "negotiation not supported remotely: try next method, if any");
507 if (meth < nmet - 1) {
518 if (fSocket->Recv(stat, kind) < 0) {
524 "after failed attempt: kind= %d, stat= %d", kind, stat);
525 if (kind == kROOTD_ERR) {
528 }
else if (kind == kROOTD_NEGOTIA) {
531 char *answer =
new char[len];
532 int nrec = fSocket->Recv(answer, len, kind);
538 if (kind != kMESS_STRING)
539 Warning(
"Authenticate",
540 "strings with accepted methods not received (%d:%d)",
543 sscanf(answer,
"%d %d %d %d %d %d", &rMth[0], &rMth[1],
544 &rMth[2], &rMth[3], &rMth[4], &rMth[5]);
545 if (gDebug > 0 && remloc > 0)
547 "remotely allowed methods not yet tried: %s",
550 }
else if (stat == 0) {
552 "no more methods accepted remotely to be tried");
565 std::string available{};
566 Bool_t methfound = kFALSE;
567 for (i = 0; i < remMeth; i++) {
568 for (j = 0; j < nmet; j++) {
569 if (fHostAuth->GetMethod(j) == rMth[i] && tMth[j] == 0) {
576 available +=
" " + std::to_string(fHostAuth->GetMethod(j));
578 if (methfound)
break;
580 if (methfound)
break;
584 Warning(
"Authenticate",
"no match with those locally available: %s", available.c_str());
598 fHostAuth->CountFailure((Int_t)fSecurity);
601 "method not even started: insufficient or wrong info: %s",
602 "try with next method, if any");
603 fHostAuth->RemoveMethod(fSecurity);
615 fHostAuth->CountFailure((Int_t)fSecurity);
618 Warning(
"Authenticate",
619 "status code -2 not expected from old daemons");
629 fHostAuth->CountFailure((Int_t)fSecurity);
631 Info(
"Authenticate",
"got a timeout");
632 fHostAuth->SetLast(fSecurity);
633 if (meth < nmet - 1) {
641 fHostAuth->CountFailure((Int_t)fSecurity);
643 Info(
"Authenticate",
"unknown status code: %d - assume failure",st);
654 fSocket->Send(
"0", kROOTD_BYE);
657 if (strlen(noSupport) > 0)
658 Info(
"Authenticate",
"attempted methods %s are not supported"
659 " by remote server version", noSupport);
661 "failure: list of attempted methods: %s", triedMeth);
662 AuthError(
"Authenticate",-1);
680 void TAuthenticate::SetEnvironment()
682 R__LOCKGUARD2(gAuthenticateMutex);
685 Info(
"SetEnvironment",
686 "setting environment: fSecurity:%d, fDetails:%s", fSecurity,
690 fgDefaultUser = fgUser;
692 fgPromptUser = kFALSE;
695 if (fDetails !=
"") {
696 char usdef[kMAXPATHLEN] = { 0 };
697 char pt[5] = { 0 }, ru[5] = { 0 };
698 Int_t hh = 0, mm = 0;
699 char us[kMAXPATHLEN] = {0}, cp[kMAXPATHLEN] = {0};
702 TString usrPromptDef = TString(GetAuthMethod(fSecurity)) +
".LoginPrompt";
703 if ((ptr = strstr(fDetails,
"pt:")) != 0) {
704 sscanf(ptr + 3,
"%4s %8191s", pt, usdef);
706 if (!strncasecmp(gEnv->GetValue(usrPromptDef,
""),
"no",2) ||
707 !strncmp(gEnv->GetValue(usrPromptDef,
""),
"0",1))
712 TString usrReUseDef = TString(GetAuthMethod(fSecurity)) +
".ReUse";
713 if ((ptr = strstr(fDetails,
"ru:")) != 0) {
714 sscanf(ptr + 3,
"%4s %8191s", ru, usdef);
716 if (!strncasecmp(gEnv->GetValue(usrReUseDef,
""),
"no",2) ||
717 !strncmp(gEnv->GetValue(usrReUseDef,
""),
"0",1))
722 TString usrValidDef = TString(GetAuthMethod(fSecurity)) +
".Valid";
723 TString hours(gEnv->GetValue(usrValidDef,
"24:00"));
725 if ((pd = hours.Index(
":")) > -1) {
726 TString minutes = hours;
728 minutes.Replace(0,pd+1,
"");
729 hh = atoi(hours.Data());
730 mm = atoi(minutes.Data());
732 hh = atoi(hours.Data());
737 if (fSecurity == kClear) {
738 if ((ptr = strstr(fDetails,
"us:")) != 0)
739 sscanf(ptr + 3,
"%8191s %8191s", us, usdef);
740 if ((ptr = strstr(fDetails,
"cp:")) != 0)
741 sscanf(ptr + 3,
"%8191s %8191s", cp, usdef);
743 Info(
"SetEnvironment",
"details:%s, pt:%s, ru:%s, us:%s cp:%s",
744 fDetails.Data(), pt, ru, us, cp);
746 if ((ptr = strstr(fDetails,
"us:")) != 0)
747 sscanf(ptr + 3,
"%8191s %8191s", us, usdef);
749 Info(
"SetEnvironment",
"details:%s, pt:%s, ru:%s, us:%s",
750 fDetails.Data(), pt, ru, us);
754 if (!strncasecmp(pt,
"yes",3) || !strncmp(pt,
"1", 1))
755 fgPromptUser = kTRUE;
758 if (!gROOT->IsProofServ()) {
760 if (!strncasecmp(ru,
"no",2) || !strncmp(ru,
"0",1))
761 fgAuthReUse = kFALSE;
765 fgExpDate = TDatime();
766 fgExpDate.Set(fgExpDate.Convert() + hh*3600 + mm*60);
769 if (fSecurity == kClear) {
770 fgUsrPwdCrypt = kTRUE;
771 if (!strncmp(cp,
"no", 2) || !strncmp(cp,
"0", 1))
772 fgUsrPwdCrypt = kFALSE;
778 if (strlen(us) > 0) snprintf(usdef, kMAXPATHLEN,
"%s", us);
780 snprintf(usdef, kMAXPATHLEN,
"%s", fUser.Data());
783 if (strlen(usdef) > 0) {
784 fgDefaultUser = usdef;
787 fgDefaultUser = fgUser;
789 UserGroup_t *u = gSystem->GetUserInfo();
791 fgDefaultUser = u->fUser;
795 if (fgDefaultUser ==
"anonymous" || fgDefaultUser ==
"rootd" ||
796 fgUser !=
"" || fUser !=
"") {
798 fgPromptUser = kFALSE;
802 Info(
"SetEnvironment",
"usdef:%s", fgDefaultUser.Data());
809 Bool_t TAuthenticate::GetUserPasswd(TString &user, TString &passwd,
810 Bool_t &pwhash, Bool_t srppwd)
813 Error(
"GetUserPasswd",
"SRP no longer supported by ROOT");
818 Info(
"GetUserPasswd",
"Enter: User: '%s' Hash:%d SRP:%d",
819 user.Data(),(Int_t)pwhash,(Int_t)
false);
822 if (user ==
"" && fgUser !=
"")
825 if (fgUser !=
"" && user == fgUser) {
826 if (passwd ==
"" && fgPasswd !=
"") {
833 Info(
"GetUserPasswd",
"In memory: User: '%s' Hash:%d",
834 user.Data(),(Int_t)pwhash);
838 UserGroup_t *u = gSystem->GetUserInfo();
843 Info(
"GetUserPasswd",
"In memory: User: '%s' Hash:%d",
844 user.Data(),(Int_t)pwhash);
849 if (user ==
"" || passwd ==
"") {
851 Info(
"GetUserPasswd",
"Checking .netrc family ...");
852 CheckNetrc(user, passwd, pwhash,
false);
855 Info(
"GetUserPasswd",
"From .netrc family: User: '%s' Hash:%d",
856 user.Data(),(Int_t)pwhash);
860 char *p = PromptUser(fRemote);
864 Error(
"GetUserPasswd",
"user name not set");
877 Bool_t TAuthenticate::CheckNetrc(TString &user, TString &passwd)
880 return CheckNetrc(user, passwd, hash,
false);
907 Bool_t TAuthenticate::CheckNetrc(TString &user, TString &passwd,
908 Bool_t &pwhash, Bool_t srppwd)
911 Error(
"CheckNetrc",
"SRP no longer supported by ROOT");
915 Bool_t result = kFALSE;
916 Bool_t first = kTRUE;
917 TString remote = fRemote;
923 gSystem->ConcatFileName(gSystem->HomeDirectory(),
".rootnetrc");
926 TInetAddress addr = gSystem->GetHostByName(fRemote);
928 remote = addr.GetHostName();
933 if (gSystem->GetPathInfo(net, buf) == 0) {
936 bool mode0600 =
true;
938 bool mode0600 = (buf.fMode & 0777) == (kS_IRUSR | kS_IWUSR);
940 if (R_ISREG(buf.fMode) && !R_ISDIR(buf.fMode) && mode0600) {
941 FILE *fd = fopen(net,
"r");
943 while (fgets(line,
sizeof(line), fd) != 0) {
947 int nword = sscanf(line,
"%63s %63s %63s %63s %63s %63s",
948 word[0], word[1], word[2], word[3], word[4], word[5]);
951 if (strcmp(word[0],
"machine"))
953 if (strcmp(word[2],
"login"))
955 if (strcmp(word[4],
"password") && strcmp(word[4],
"password-hash"))
960 TString href(word[1]);
961 href.ReplaceAll(
"*",
".*");
963 if (remote.Index(rg) != kNPOS) {
967 if (!strcmp(word[4],
"password-hash"))
972 if (!strcmp(word[3], user.Data())) {
974 if (!strcmp(word[4],
"password-hash"))
984 Warning(
"CheckNetrc",
985 "file %s exists but has not 0600 permission", net);
989 if (first && !result) {
990 net = gSystem->ConcatFileName(gSystem->HomeDirectory(),
".netrc");
1001 const char *TAuthenticate::GetGlobalUser()
1009 Bool_t TAuthenticate::GetGlobalPwHash()
1017 Bool_t TAuthenticate::GetGlobalSRPPwd()
1025 TDatime TAuthenticate::GetGlobalExpDate()
1033 const char *TAuthenticate::GetDefaultUser()
1035 return fgDefaultUser;
1041 const char *TAuthenticate::GetKrb5Principal()
1043 ::Error(
"Krb5Auth",
"Kerberos5 is no longer supported by ROOT");
1050 Bool_t TAuthenticate::GetAuthReUse()
1058 Bool_t TAuthenticate::GetPromptUser()
1060 return fgPromptUser;
1066 const char *TAuthenticate::GetAuthMethod(Int_t idx)
1068 R__LOCKGUARD2(gAuthenticateMutex);
1070 if (idx < 0 || idx > kMAXSEC-1) {
1071 ::Error(
"Authenticate::GetAuthMethod",
"idx out of bounds (%d)", idx);
1074 return fgAuthMeth[idx];
1081 Int_t TAuthenticate::GetAuthMethodIdx(
const char *meth)
1083 R__LOCKGUARD2(gAuthenticateMutex);
1085 if (meth && meth[0]) {
1086 for (Int_t i = 0; i < kMAXSEC; i++) {
1087 if (!fgAuthMeth[i].CompareTo(meth, TString::kIgnoreCase))
1101 char *TAuthenticate::PromptUser(
const char *remote)
1103 R__LOCKGUARD2(gAuthenticateMutex);
1106 if (fgDefaultUser !=
"")
1107 user = fgDefaultUser;
1109 user = gSystem->Getenv(
"USER");
1112 user = gSystem->Getenv(
"USERNAME");
1114 if (isatty(0) == 0 || isatty(1) == 0) {
1115 ::Warning(
"TAuthenticate::PromptUser",
1116 "not tty: cannot prompt for user, returning default");
1118 return StrDup(user);
1120 return StrDup(
"None");
1123 const char *usrIn = Getline(Form(
"Name (%s:%s): ", remote, user));
1126 usr.Remove(usr.Length() - 1);
1130 return StrDup(user);
1141 char *TAuthenticate::PromptPasswd(
const char *prompt)
1143 if (isatty(0) == 0 || isatty(1) == 0) {
1144 ::Warning(
"TAuthenticate::PromptPasswd",
1145 "not tty: cannot prompt for passwd, returning -1");
1146 static char noint[4] = {
"-1"};
1147 return StrDup(noint);
1151 const char *pw = buf;
1153 if (!gROOT->IsBatch() && (fgPasswdDialog == (TPluginHandler *)(-1)) &&
1154 gEnv->GetValue(
"Auth.UsePasswdDialogBox", 1) == 1) {
1155 if ((fgPasswdDialog =
1156 gROOT->GetPluginManager()->FindHandler(
"TGPasswdDialog"))) {
1157 if (fgPasswdDialog->LoadPlugin() == -1) {
1159 ::Warning(
"TAuthenticate",
1160 "could not load plugin for the password dialog box");
1164 if (fgPasswdDialog && (fgPasswdDialog != (TPluginHandler *)(-1))) {
1167 fgPasswdDialog->ExecPlugin(3, prompt, buf, 128);
1170 while (gROOT->IsInterrupted())
1171 gSystem->DispatchOneEvent(kFALSE);
1174 Gl_config(
"noecho", 1);
1175 pw = Getline(prompt);
1176 Gl_config(
"noecho", 0);
1182 if (spw.EndsWith(
"\n"))
1183 spw.Remove(spw.Length() - 1);
1184 char *rpw = StrDup(spw);
1193 GlobusAuth_t TAuthenticate::GetGlobusAuthHook()
1201 const char *TAuthenticate::GetRSAPubExport(Int_t key)
1203 key = (key >= 0 && key <= 1) ? key : 0;
1204 return fgRSAPubExport[key].keys;
1210 Int_t TAuthenticate::GetRSAInit()
1218 void TAuthenticate::SetDefaultRSAKeyType(Int_t key)
1220 if (key >= 0 && key <= 1)
1227 void TAuthenticate::SetRSAInit(Int_t init)
1235 TList *TAuthenticate::GetAuthInfo()
1237 R__LOCKGUARD2(gAuthenticateMutex);
1240 fgAuthInfo =
new TList;
1248 TList *TAuthenticate::GetProofAuthInfo()
1250 R__LOCKGUARD2(gAuthenticateMutex);
1252 if (!fgProofAuthInfo)
1253 fgProofAuthInfo =
new TList;
1254 return fgProofAuthInfo;
1260 void TAuthenticate::AuthError(
const char *where, Int_t err)
1262 R__LOCKGUARD2(gAuthenticateMutex);
1265 err = (err < kErrError) ? ((err > -1) ? err : -1) : kErrError;
1268 Bool_t forceprint = kFALSE;
1269 TString lasterr =
"";
1273 lasterr =
"(last error only; re-run with gDebug > 0 for more details)";
1277 if (gDebug > 0 || forceprint) {
1278 if (gRootdErrStr[erc])
1279 ::Error(Form(
"TAuthenticate::%s", where),
"%s %s",
1280 gRootdErrStr[erc], lasterr.Data());
1282 ::Error(Form(
"TAuthenticate::%s", where),
1283 "unknown error code: server must be running a newer ROOT version %s",
1294 void TAuthenticate::SetGlobalUser(
const char *user)
1296 R__LOCKGUARD2(gAuthenticateMutex);
1301 if (user && user[0])
1308 void TAuthenticate::SetGlobalPasswd(
const char *passwd)
1310 R__LOCKGUARD2(gAuthenticateMutex);
1315 if (passwd && passwd[0])
1322 void TAuthenticate::SetGlobalPwHash(Bool_t pwhash)
1330 void TAuthenticate::SetGlobalSRPPwd(Bool_t)
1332 ::Error(
"SetGlobalSRPPwd",
"SRP no longer supported by ROOT");
1340 void TAuthenticate::SetReadHomeAuthrc(Bool_t readhomeauthrc)
1342 fgReadHomeAuthrc = readhomeauthrc;
1348 void TAuthenticate::SetGlobalExpDate(TDatime expdate)
1350 fgExpDate = expdate;
1356 void TAuthenticate::SetDefaultUser(
const char *defaultuser)
1358 if (fgDefaultUser !=
"")
1361 if (defaultuser && defaultuser[0])
1362 fgDefaultUser = defaultuser;
1368 void TAuthenticate::SetTimeOut(Int_t to)
1370 fgAuthTO = (to <= 0) ? -1 : to;
1376 void TAuthenticate::SetAuthReUse(Bool_t authreuse)
1378 fgAuthReUse = authreuse;
1384 void TAuthenticate::SetPromptUser(Bool_t promptuser)
1386 fgPromptUser = promptuser;
1392 void TAuthenticate::SetSecureAuthHook(SecureAuth_t func)
1394 fgSecAuthHook = func;
1401 void TAuthenticate::SetKrb5AuthHook(Krb5Auth_t)
1403 ::Error(
"Krb5Auth",
"Kerberos5 is no longer supported by ROOT");
1410 void TAuthenticate::SetGlobusAuthHook(GlobusAuth_t)
1412 ::Error(
"GlobusAuth",
"Globus is no longer supported by ROOT");
1418 Int_t TAuthenticate::SshAuth(TString & )
1420 ::Error(
"SshAuth",
"SSH is no longer supported by ROOT");
1427 const char *TAuthenticate::GetSshUser(TString )
const
1429 ::Error(
"GetSshUser",
"SSH is no longer supported by ROOT");
1439 Bool_t TAuthenticate::CheckHost(
const char *host,
const char *href)
1441 R__LOCKGUARD2(gAuthenticateMutex);
1443 Bool_t retval = kTRUE;
1450 if (!strcmp(href,
"*"))
1456 Bool_t name = kFALSE;
1457 TRegexp rename(
"[+a-zA-Z]");
1459 if (rename.Index(href,&len) != -1 || strstr(href,
"-"))
1463 Bool_t wild = kFALSE;
1464 if (strstr(href,
"*"))
1468 TRegexp rehost(href,wild);
1471 TString theHost(host);
1473 TInetAddress addr = gSystem->GetHostByName(host);
1474 theHost = addr.GetHostAddress();
1476 ::Info(
"TAuthenticate::CheckHost",
"checking host IP: %s", theHost.Data());
1480 Ssiz_t pos = rehost.Index(theHost,&len);
1487 if (pos > 0 && pos != (Ssiz_t)(theHost.Length()-strlen(href)))
1497 Int_t TAuthenticate::RfioAuth(TString &)
1499 ::Error(
"RfioAuth",
"RfioAuth is no longer supported by ROOT");
1508 Int_t TAuthenticate::ClearAuth(TString &user, TString &passwd, Bool_t &pwdhash)
1510 R__LOCKGUARD2(gAuthenticateMutex);
1513 Info(
"ClearAuth",
"enter: user: %s (passwd hashed?: %d)",
1514 user.Data(),(Int_t)pwdhash);
1516 Int_t reuse = fgAuthReUse;
1517 Int_t prompt = fgPromptUser;
1518 Int_t cryptopt = fgUsrPwdCrypt;
1522 fDetails = TString::Format(
"pt:%d ru:%d cp:%d us:",
1523 fgPromptUser, fgAuthReUse, fgUsrPwdCrypt) + user;
1525 Info(
"ClearAuth",
"ru:%d pt:%d cp:%d ns:%d rk:%d",
1526 fgAuthReUse,fgPromptUser,fgUsrPwdCrypt,needsalt,fgRSAKey);
1539 TString pashash =
"";
1542 UserGroup_t *pw = gSystem->GetUserInfo(gSystem->GetEffectiveUid());
1545 effUser = TString(pw->fUser);
1551 int opt = (reuse * kAUTH_REUSE_MSK) + (cryptopt * kAUTH_CRYPT_MSK) +
1552 (needsalt * kAUTH_SSALT_MSK) + (fRSAKey * kAUTH_RSATY_MSK);
1554 options.Form(
"%d %ld %s %ld %s", opt,
1555 (Long_t)user.Length(), user.Data(),
1556 (Long_t)effUser.Length(), effUser.Data());
1562 if ((rc = AuthExists(user, (Int_t) TAuthenticate::kClear, options,
1563 &kind, &stat, &StdCheckSecCtx)) == 1) {
1570 if (stat == kErrNotAllowed && kind == kROOTD_ERR) {
1574 if (kind == kROOTD_AUTH && stat == -1) {
1576 Info(
"ClearAuth",
"anonymous user");
1585 char ctag[11] = {0};
1586 if (anon == 0 && cryptopt == 1) {
1589 if (kind != kROOTD_RSAKEY || stat < 1 || stat > 2 ) {
1591 if (kind != kROOTD_ERR) {
1592 Warning(
"ClearAuth",
1593 "problems recvn RSA key flag: got message %d, flag: %d",
1599 Info(
"ClearAuth",
"get key request ...");
1605 if (SendRSAPublicKey(fSocket,fRSAKey) < 0)
1612 if ((slen = SecureRecv(fSocket, 1, fRSAKey, &tmpsalt)) == -1) {
1613 Warning(
"ClearAuth",
"problems secure-receiving salt -"
1614 " may result in corrupted salt");
1615 Warning(
"ClearAuth",
"switch off reuse for this session");
1623 while (ltmp && tmpsalt[ltmp-1] !=
'#') ltmp--;
1625 if (tmpsalt[ltmp-1] ==
'#' &&
1626 tmpsalt[ltmp-10] ==
'#') {
1627 strlcpy(ctag,&tmpsalt[ltmp-10],11);
1642 salt = TString(tmpsalt);
1646 Info(
"ClearAuth",
"got salt: '%s' (len: %d)", salt.Data(), slen);
1649 Info(
"ClearAuth",
"Salt not required");
1651 if (SecureRecv(fSocket, 1, fRSAKey, &tmptag) == -1) {
1652 Warning(
"ClearAuth",
"problems secure-receiving rndmtag -"
1653 " may result in corrupted rndmtag");
1656 strlcpy(ctag, tmptag, 11);
1668 if (fgPasswd.Contains(
"@")) {
1674 pw = gSystem->GetUserInfo();
1676 char *u = StrDup(pw->fUser);
1681 static TString localFQDN;
1682 if (localFQDN ==
"") {
1683 TInetAddress addr = gSystem->GetHostByName(gSystem->HostName());
1685 localFQDN = addr.GetHostName();
1687 passwd.Form(
"%s@%s", localuser.Data(), localFQDN.Data());
1690 "automatically generated anonymous passwd: %s",
1696 if (prompt == 1 || pashash.Length() == 0) {
1700 xp.Form(
"%s@%s password: ", user.Data(),fRemote.Data());
1701 char *pwd = PromptPasswd(xp);
1702 passwd = TString(pwd);
1705 Error(
"ClearAuth",
"password not set");
1706 fSocket->Send(
"-1", kROOTD_PASS);
1710 if (needsalt && !pwdhash) {
1712 pashash = TString(crypt(passwd, salt));
1713 if (!pashash.BeginsWith(salt)) {
1736 if (anon == 0 && cryptopt == 1) {
1739 if (fSocket->Send(
"\0", kROOTD_PASS) < 0)
1747 if (SecureSend(fSocket, 1, fRSAKey, pashash.Data()) == -1) {
1748 Warning(
"ClearAuth",
"problems secure-sending pass hash"
1749 " - may result in authentication failure");
1756 for (
int i = 0; i < passwd.Length(); i++) {
1757 char inv = ~passwd(i);
1758 passwd.Replace(i, 1, inv);
1761 if (fSocket->Send(passwd.Data(), kROOTD_PASS) < 0)
1767 if ((nrec = fSocket->Recv(stat, kind)) < 0 )
1770 Info(
"ClearAuth",
"after kROOTD_PASS: kind= %d, stat= %d", kind,
1774 if (kind == kROOTD_ERR) {
1775 AuthError(
"ClearAuth", stat);
1780 if (kind != kROOTD_PASS || stat < 1)
1781 Warning(
"ClearAuth",
1782 "problems recvn (user,offset) length (%d:%d bytes:%d)",
1787 int reclen = (stat+1 > 256) ? 256 : stat+1;
1788 if ((nrec = fSocket->Recv(answer, reclen, kind)) < 0)
1790 if (kind != kMESS_STRING)
1791 Warning(
"ClearAuth",
1792 "username and offset not received (%d:%d)", kind,
1798 sscanf(answer,
"%127s %d", lUser, &offset);
1801 "received from server: user: %s, offset: %d (%s)", lUser,
1808 if (reuse == 1 && offset > -1) {
1810 if (cryptopt == 1) {
1811 if (SecureRecv(fSocket, 1, fRSAKey, &token) == -1) {
1812 Warning(
"ClearAuth",
1813 "problems secure-receiving token -"
1814 " may result in corrupted token");
1819 token =
new char[tlen];
1820 if (fSocket->Recv(token, tlen, kind) < 0) {
1824 if (kind != kMESS_STRING)
1825 Warning(
"ClearAuth",
"token not received (%d:%d)", kind,
1828 for (
int i = 0; i < (int) strlen(token); i++) {
1829 token[i] = ~token[i];
1834 Info(
"ClearAuth",
"received from server: token: '%s' ",
1837 TPwdCtx *pwdctx =
new TPwdCtx(fPasswd,fPwHash);
1839 fSecContext = fHostAuth->CreateSecContext((
const char *)lUser, fRemote,
1840 kClear, offset, fDetails, (
const char *)token,
1841 fgExpDate, (
void *)pwdctx, fRSAKey);
1848 if (fSocket->Recv(stat, kind) < 0)
1852 if (kind == kROOTD_AUTH && stat >= 1) {
1853 if (stat == 5 && fSocket->GetServType() == TSocket::kPROOFD)
1858 fSecContext->SetID(
"AFS authentication");
1862 if (kind == kROOTD_ERR)
1863 AuthError(
"ClearAuth", stat);
1872 if (fSocket->Send(user.Data(), kROOTD_USER) < 0)
1876 if (fSocket->Recv(stat, kind) < 0)
1881 if (kind == kROOTD_AUTH && stat == 1) {
1883 fHostAuth->CreateSecContext(user,fRemote,kClear,-1,fDetails,0);
1887 if (kind == kROOTD_ERR) {
1888 TString server =
"sockd";
1889 if (fProtocol.Contains(
"root"))
1891 if (fProtocol.Contains(
"proof"))
1893 if (stat == kErrConnectionRefused) {
1896 "%s@%s does not accept connections from %s@%s",
1897 server.Data(),fRemote.Data(),
1898 fUser.Data(),gSystem->HostName());
1900 }
else if (stat == kErrNotAllowed) {
1903 "%s@%s does not accept %s authentication from %s@%s",
1904 server.Data(),fRemote.Data(),
1905 TAuthenticate::fgAuthMeth[0].Data(),
1906 fUser.Data(),gSystem->HostName());
1908 AuthError(
"ClearAuth", stat);
1915 xp.Form(
"%s@%s password: ", user.Data(),fRemote.Data());
1916 char *p = PromptPasswd(xp);
1920 Error(
"ClearAuth",
"password not set");
1922 if (fUser ==
"anonymous" || fUser ==
"rootd") {
1923 if (!passwd.Contains(
"@")) {
1924 Warning(
"ClearAuth",
1925 "please use passwd of form: user@host.do.main");
1936 for (
int i = 0; i < passwd.Length(); i++) {
1937 char inv = ~passwd(i);
1938 passwd.Replace(i, 1, inv);
1942 if (fSocket->Send(passwd, kROOTD_PASS) < 0)
1946 if (fSocket->Recv(stat, kind) < 0)
1949 Info(
"ClearAuth",
"after kROOTD_PASS: kind= %d, stat= %d", kind,
1952 if (kind == kROOTD_AUTH && stat == 1) {
1954 fHostAuth->CreateSecContext(user,fRemote,kClear,-1,fDetails,0);
1957 if (kind == kROOTD_ERR)
1958 AuthError(
"ClearAuth", stat);
1971 THostAuth *TAuthenticate::GetHostAuth(
const char *host,
const char *user,
1972 Option_t *opt, Int_t *exact)
1978 ::Info(
"TAuthenticate::GetHostAuth",
"enter ... %s ... %s", host, user);
1982 TString hostname = host;
1983 if (hostname.Contains(
":")) {
1984 char *ps = (
char *)strstr(host,
":");
1986 srvtyp = atoi(ps+1);
1987 hostname.Remove(hostname.Index(
":"));
1989 TString hostFQDN = hostname;
1990 if (strncmp(host,
"default",7) && !hostFQDN.Contains(
"*")) {
1991 TInetAddress addr = gSystem->GetHostByName(hostFQDN);
1993 hostFQDN = addr.GetHostName();
2001 TIter *next =
new TIter(GetAuthInfo());
2002 if (!strncasecmp(opt,
"P",1)) {
2004 next =
new TIter(GetProofAuthInfo());
2008 Bool_t notFound = kTRUE;
2009 Bool_t serverOK = kTRUE;
2010 while ((ai = (THostAuth *) (*next)())) {
2012 ai->Print(
"Authenticate::GetHostAuth");
2015 if (!(serverOK = (ai->GetServer() == -1) ||
2016 (ai->GetServer() == srvtyp)))
2020 if (!strcmp(ai->GetHost(),
"default") && serverOK && notFound)
2024 if (CheckHost(hostFQDN,ai->GetHost()) &&
2025 CheckHost(usr,ai->GetUser()) && serverOK) {
2030 if (hostFQDN == ai->GetHost() &&
2031 usr == ai->GetUser() && srvtyp == ai->GetServer() ) {
2048 THostAuth *TAuthenticate::HasHostAuth(
const char *host,
const char *user,
2052 ::Info(
"TAuthenticate::HasHostAuth",
"enter ... %s ... %s", host, user);
2056 TString hostFQDN = host;
2057 if (hostFQDN.Contains(
":")) {
2058 char *ps = (
char *)strstr(host,
":");
2060 srvtyp = atoi(ps+1);
2061 hostFQDN.Remove(hostFQDN.Index(
":"));
2063 if (strncmp(host,
"default",7) && !hostFQDN.Contains(
"*")) {
2064 TInetAddress addr = gSystem->GetHostByName(hostFQDN);
2066 hostFQDN = addr.GetHostName();
2069 TIter *next =
new TIter(GetAuthInfo());
2070 if (!strncasecmp(opt,
"P",1)) {
2072 next =
new TIter(GetProofAuthInfo());
2075 while ((ai = (THostAuth *) (*next)())) {
2077 if (hostFQDN == ai->GetHost() &&
2078 !strcmp(user, ai->GetUser()) && srvtyp == ai->GetServer()) {
2092 void TAuthenticate::FileExpand(
const char *fexp, FILE *ftmp)
2095 char line[kMAXPATHLEN];
2096 char cinc[20], fileinc[kMAXPATHLEN];
2099 ::Info(
"TAuthenticate::FileExpand",
"enter ... '%s' ... 0x%lx", fexp, (Long_t)ftmp);
2101 fin = fopen(fexp,
"r");
2105 while (fgets(line,
sizeof(line), fin) != 0) {
2109 if (line[strlen(line) - 1] ==
'\n')
2110 line[strlen(line) - 1] =
'\0';
2112 ::Info(
"TAuthenticate::FileExpand",
"read line ... '%s'", line);
2113 int nw = sscanf(line,
"%19s %8191s", cinc, fileinc);
2116 if (strcmp(cinc,
"include") != 0) {
2118 fprintf(ftmp,
"%s\n", line);
2123 ln.ReplaceAll(
"\"",1,
"",0);
2124 ln.ReplaceAll(
"'",1,
"",0);
2125 sscanf(ln.Data(),
"%19s %8191s", cinc, fileinc);
2128 if (fileinc[0] ==
'$') {
2129 TString finc(fileinc);
2130 TString edir(fileinc);
2131 if (edir.Contains(
"/")) {
2132 edir.Remove(edir.Index(
"/"));
2134 if (gSystem->Getenv(edir.Data())) {
2136 finc.ReplaceAll(edir.Data(),gSystem->Getenv(edir.Data()));
2138 strncpy(fileinc,finc.Data(),kMAXPATHLEN);
2139 fileinc[kMAXPATHLEN-1] =
'\0';
2145 if (fileinc[0] ==
'~') {
2148 strlen(fileinc) + strlen(gSystem->HomeDirectory()) + 10;
2149 char *ffull =
new char[flen];
2150 snprintf(ffull, flen,
"%s/%s", gSystem->HomeDirectory(), fileinc + 1);
2151 if (strlen(ffull) < kMAXPATHLEN - 1) strlcpy(fileinc, ffull,kMAXPATHLEN);
2155 if (!gSystem->AccessPathName(fileinc, kReadPermission)) {
2156 FileExpand(fileinc, ftmp);
2158 ::Warning(
"TAuthenticate::FileExpand",
2159 "file specified by 'include' cannot be open or read (%s)",
2171 char *TAuthenticate::GetDefaultDetails(
int sec,
int opt,
const char *usr)
2173 char temp[kMAXPATHLEN] = { 0 };
2174 const char copt[2][5] = {
"no",
"yes" };
2177 ::Info(
"TAuthenticate::GetDefaultDetails",
2178 "enter ... %d ...pt:%d ... '%s'", sec, opt, usr);
2180 if (opt < 0 || opt > 1)
2184 if (sec == TAuthenticate::kClear) {
2185 if (!usr[0] || !strncmp(usr,
"*",1))
2186 usr = gEnv->GetValue(
"UsrPwd.Login",
"");
2187 snprintf(temp, kMAXPATHLEN,
"pt:%s ru:%s cp:%s us:%s",
2188 gEnv->GetValue(
"UsrPwd.LoginPrompt", copt[opt]),
2189 gEnv->GetValue(
"UsrPwd.ReUse",
"1"),
2190 gEnv->GetValue(
"UsrPwd.Crypt",
"1"), usr);
2194 ::Info(
"TAuthenticate::GetDefaultDetails",
"returning ... %s", temp);
2196 return StrDup(temp);
2202 void TAuthenticate::RemoveHostAuth(THostAuth * ha, Option_t *opt)
2204 if (!strncasecmp(opt,
"P",1))
2205 GetProofAuthInfo()->Remove(ha);
2207 GetAuthInfo()->Remove(ha);
2218 void TAuthenticate::Show(Option_t *opt)
2222 if (sopt.Contains(
"s",TString::kIgnoreCase)) {
2225 TIter next(gROOT->GetListOfSecContexts());
2226 TSecContext *sc = 0;
2227 while ((sc = (TSecContext *)next()))
2233 " +--------------------------- BEGIN --------------------------------+");
2236 if (sopt.Contains(
"p",TString::kIgnoreCase)) {
2238 " + List fgProofAuthInfo has %4d members +",
2239 GetProofAuthInfo()->GetSize());
2243 " +------------------------------------------------------------------+");
2244 TIter next(GetProofAuthInfo());
2246 while ((ai = (THostAuth *) next())) {
2251 " + List fgAuthInfo has %4d members +",
2252 GetAuthInfo()->GetSize());
2256 " +------------------------------------------------------------------+");
2257 TIter next(GetAuthInfo());
2259 while ((ai = (THostAuth *) next())) {
2261 ai->PrintEstablished();
2265 " +---------------------------- END ---------------------------------+");
2275 Int_t TAuthenticate::AuthExists(TString username, Int_t method,
const char *options,
2276 Int_t *message, Int_t *rflag,
2277 CheckSecCtx_t checksecctx)
2281 Info(
"AuthExists",
"%d: enter: msg: %d options: '%s'",
2282 method,*message, options);
2285 Bool_t notHA = kFALSE;
2288 TIter next(fHostAuth->Established());
2289 TRootSecContext *secctx;
2290 while ((secctx = (TRootSecContext *)next())) {
2291 if (secctx->GetMethod() == method) {
2292 if (fRemote == secctx->GetHost()) {
2294 (*checksecctx)(username,secctx) == 1)
2302 next = TIter(gROOT->GetListOfSecContexts());
2303 while ((secctx = (TRootSecContext *)next())) {
2304 if (secctx->GetMethod() == method) {
2305 if (fRemote == secctx->GetHost()) {
2307 (*checksecctx)(username,secctx) == 1) {
2320 offset = secctx->GetOffSet();
2321 token = secctx->GetToken();
2324 "found valid TSecContext: offset: %d token: '%s'",
2325 offset, token.Data());
2330 sstr.Form(
"%d %d %s", fgProcessID, offset, options);
2333 if (fSocket->Send(sstr, *message) < 0)
2336 Int_t reuse = *rflag;
2337 if (reuse == 1 && offset > -1) {
2343 Int_t rproto = fSocket->GetRemoteProtocol();
2344 Bool_t oldsrv = ((fProtocol.BeginsWith(
"root") && rproto == 9) ||
2345 (fProtocol.BeginsWith(
"proof") && rproto == 8));
2346 Int_t stat = 1, kind;
2348 if (fSocket->Recv(stat, kind) < 0)
2350 if (kind != kROOTD_AUTH)
2351 Warning(
"AuthExists",
"protocol error: expecting %d got %d"
2352 " (value: %d)",kROOTD_AUTH,kind,stat);
2357 Info(
"AuthExists",
"offset OK");
2359 Int_t rsaKey = secctx->GetRSAKey();
2361 Info(
"AuthExists",
"key type: %d", rsaKey);
2370 snprintf(tag, 9,
"%08x",stat);
2376 if (SecureSend(fSocket, 1, rsaKey, token) == -1) {
2377 Warning(
"AuthExists",
"problems secure-sending token %s",
2378 "- may trigger problems in proofing Id ");
2383 for (
int i = 0; i < token.Length(); i++) {
2384 char inv = ~token(i);
2385 token.Replace(i, 1, inv);
2387 if (fSocket->Send(token, kMESS_STRING) < 0)
2392 Info(
"AuthExists",
"offset not OK - rerun authentication");
2395 secctx->DeActivate(
"");
2400 if (fSocket->Recv(stat, kind) < 0)
2403 Info(
"AuthExists",
"%d: after msg %d: kind= %d, stat= %d",
2404 method,*message, kind, stat);
2410 if (kind == kROOTD_ERR) {
2411 TString server =
"sockd";
2412 if (fSocket->GetServType() == TSocket::kROOTD)
2414 if (fSocket->GetServType() == TSocket::kPROOFD)
2416 if (stat == kErrConnectionRefused) {
2417 Error(
"AuthExists",
"%s@%s does not accept connections from %s@%s",
2418 server.Data(),fRemote.Data(),fUser.Data(),gSystem->HostName());
2420 }
else if (stat == kErrNotAllowed) {
2423 "%s@%s does not accept %s authentication from %s@%s",
2424 server.Data(),fRemote.Data(), fgAuthMeth[method].Data(),
2425 fUser.Data(),gSystem->HostName());
2427 AuthError(
"AuthExists", stat);
2431 secctx->DeActivate(
"");
2435 if (kind == kROOTD_AUTH && stat >= 1) {
2438 fHostAuth->CreateSecContext(fUser,fRemote,method,-stat,fDetails,0);
2441 Info(
"AuthExists",
"valid authentication exists");
2443 Info(
"AuthExists",
"valid authentication exists: offset changed");
2445 Info(
"AuthExists",
"remote access authorized by /etc/hosts.equiv");
2447 Info(
"AuthExists",
"no authentication required remotely");
2453 if (fSocket->Recv(newOffSet, kind) < 0)
2456 secctx->SetOffSet(newOffSet);
2459 fSecContext = secctx;
2462 fHostAuth->Established()->Add(secctx);
2472 void TAuthenticate::InitRandom()
2474 static Bool_t notinit = kTRUE;
2477 const char *randdev =
"/dev/urandom";
2480 if ((fd = open(randdev, O_RDONLY)) != -1) {
2482 ::Info(
"InitRandom",
"taking seed from %s", randdev);
2483 if (read(fd, &seed,
sizeof(seed)) !=
sizeof(seed))
2484 ::Warning(
"InitRandom",
"could not read seed from %s", randdev);
2488 ::Info(
"InitRandom",
"%s not available: using time()", randdev);
2500 Int_t TAuthenticate::GenRSAKeys()
2503 Info(
"GenRSAKeys",
"enter");
2505 if (fgRSAInit == 1) {
2507 Info(
"GenRSAKeys",
"Keys prviously generated - return");
2511 TString lib =
"libRsa";
2514 if (!TRSA_fun::RSA_genprim()) {
2516 if ((p = gSystem->DynamicPathName(lib, kTRUE))) {
2523 TAuthenticate::InitRandom();
2526 if (fgRSAKey == 1) {
2529 Info(
"GenRSAKeys",
"SSL: Generate Blowfish key");
2535 SSL_load_error_strings();
2538 OpenSSL_add_all_ciphers();
2541 Int_t nbits = gEnv->GetValue(
"SSL.BFBits",256);
2544 nbits = (nbits >= 128) ? nbits : 128;
2547 nbits = (nbits <= 15912) ? nbits : 15912;
2550 Int_t klen = nbits / 8 ;
2553 char *rbuf = GetRandString(0,klen);
2554 RAND_seed(rbuf,strlen(rbuf));
2557 fgRSAPubExport[1].len = klen;
2558 fgRSAPubExport[1].keys = rbuf;
2560 Info(
"GenRSAKeys",
"SSL: BF key length: %d", fgRSAPubExport[1].len);
2563 BF_set_key(&fgBFKey, klen, (
const unsigned char *)rbuf);
2571 rsa_NUMBER p1, p2, rsa_n, rsa_e, rsa_d;
2572 Int_t l_n = 0, l_d = 0;
2573 char buf_n[rsa_STRLEN], buf_e[rsa_STRLEN], buf_d[rsa_STRLEN];
2576 char buf[rsa_STRLEN];
2579 Int_t nAttempts = 0;
2580 Int_t thePrimeLen = kPRIMELENGTH;
2581 Int_t thePrimeExp = kPRIMEEXP;
2582 while (notOk && nAttempts < kMAXRSATRIES) {
2585 if (gDebug > 2 && nAttempts > 1) {
2586 Info(
"GenRSAKeys",
"retry no. %d",nAttempts);
2591 p1 = TRSA_fun::RSA_genprim()(thePrimeLen, thePrimeExp);
2592 p2 = TRSA_fun::RSA_genprim()(thePrimeLen+1, thePrimeExp);
2596 while (TRSA_fun::RSA_cmp()(&p1, &p2) == 0 && nPrimes < kMAXRSATRIES) {
2599 Info(
"GenRSAKeys",
"equal primes: regenerate (%d times)",nPrimes);
2601 p1 = TRSA_fun::RSA_genprim()(thePrimeLen, thePrimeExp);
2602 p2 = TRSA_fun::RSA_genprim()(thePrimeLen+1, thePrimeExp);
2606 TRSA_fun::RSA_num_sput()(&p1, buf, rsa_STRLEN);
2607 Info(
"GenRSAKeys",
"local: p1: '%s' ", buf);
2608 TRSA_fun::RSA_num_sput()(&p2, buf, rsa_STRLEN);
2609 Info(
"GenRSAKeys",
"local: p2: '%s' ", buf);
2613 if (TRSA_fun::RSA_genrsa()(p1, p2, &rsa_n, &rsa_e, &rsa_d)) {
2614 if (gDebug > 2 && nAttempts > 1)
2615 Info(
"GenRSAKeys",
" genrsa: unable to generate keys (%d)",
2621 TRSA_fun::RSA_num_sput()(&rsa_n, buf_n, rsa_STRLEN);
2622 l_n = strlen(buf_n);
2623 TRSA_fun::RSA_num_sput()(&rsa_e, buf_e, rsa_STRLEN);
2625 l_e = strlen(buf_e);
2627 TRSA_fun::RSA_num_sput()(&rsa_d, buf_d, rsa_STRLEN);
2628 l_d = strlen(buf_d);
2632 Info(
"GenRSAKeys",
"local: n: '%s' length: %d", buf_n, l_n);
2633 Info(
"GenRSAKeys",
"local: e: '%s' length: %d", buf_e, l_e);
2634 Info(
"GenRSAKeys",
"local: d: '%s' length: %d", buf_d, l_d);
2637 if (TRSA_fun::RSA_cmp()(&rsa_n, &rsa_e) <= 0)
2639 if (TRSA_fun::RSA_cmp()(&rsa_n, &rsa_d) <= 0)
2643 char test[2 * rsa_STRLEN] =
"ThisIsTheStringTest01203456-+/";
2645 char *tdum = GetRandString(0, lTes - 1);
2646 strlcpy(test, tdum, lTes+1);
2648 char buf[2 * rsa_STRLEN];
2650 Info(
"GenRSAKeys",
"local: test string: '%s' ", test);
2653 strlcpy(buf, test, lTes+1);
2656 int lout = TRSA_fun::RSA_encode()(buf, lTes, rsa_n, rsa_e);
2659 "local: length of crypted string: %d bytes", lout);
2662 TRSA_fun::RSA_decode()(buf, lout, rsa_n, rsa_d);
2665 Info(
"GenRSAKeys",
"local: after private/public : '%s' ", buf);
2667 if (strncmp(test, buf, lTes))
2671 strlcpy(buf, test, lTes+1);
2674 lout = TRSA_fun::RSA_encode()(buf, lTes, rsa_n, rsa_d);
2676 Info(
"GenRSAKeys",
"local: length of crypted string: %d bytes ",
2680 TRSA_fun::RSA_decode()(buf, lout, rsa_n, rsa_e);
2683 Info(
"GenRSAKeys",
"local: after public/private : '%s' ", buf);
2685 if (strncmp(test, buf, lTes))
2692 TRSA_fun::RSA_assign()(&fgRSAPriKey.n, &rsa_n);
2693 TRSA_fun::RSA_assign()(&fgRSAPriKey.e, &rsa_e);
2696 TRSA_fun::RSA_assign()(&fgRSAPubKey.n, &rsa_n);
2697 TRSA_fun::RSA_assign()(&fgRSAPubKey.e, &rsa_d);
2702 Info(
"GenRSAKeys",
"local: generated keys are:");
2703 Info(
"GenRSAKeys",
"local: n: '%s' length: %d", buf_n, l_n);
2704 Info(
"GenRSAKeys",
"local: e: '%s' length: %d", buf_e, l_e);
2705 Info(
"GenRSAKeys",
"local: d: '%s' length: %d", buf_d, l_d);
2709 if (fgRSAPubExport[0].keys) {
2710 delete [] fgRSAPubExport[0].keys;
2711 fgRSAPubExport[0].len = 0;
2713 fgRSAPubExport[0].len = l_n + l_d + 4;
2714 fgRSAPubExport[0].keys =
new char[fgRSAPubExport[0].len];
2716 fgRSAPubExport[0].keys[0] =
'#';
2717 memcpy(fgRSAPubExport[0].keys + 1, buf_n, l_n);
2718 fgRSAPubExport[0].keys[l_n + 1] =
'#';
2719 memcpy(fgRSAPubExport[0].keys + l_n + 2, buf_d, l_d);
2720 fgRSAPubExport[0].keys[l_n + l_d + 2] =
'#';
2721 fgRSAPubExport[0].keys[l_n + l_d + 3] = 0;
2724 Info(
"GenRSAKeys",
"local: export pub: '%s'", fgRSAPubExport[0].keys);
2727 Info(
"GenRSAKeys",
"local: export pub length: %d bytes", fgRSAPubExport[0].len);
2744 char *TAuthenticate::GetRandString(Int_t opt, Int_t len)
2746 unsigned int iimx[4][4] = {
2747 {0x0, 0xffffff08, 0xafffffff, 0x2ffffffe},
2748 {0x0, 0x3ff0000, 0x7fffffe, 0x7fffffe},
2749 {0x0, 0x3ff0000, 0x7e, 0x7e},
2750 {0x0, 0x3ffc000, 0x7fffffe, 0x7fffffe}
2753 const char *cOpt[4] = {
"Any",
"LetNum",
"Hex",
"Crypt" };
2756 if (opt < 0 || opt > 2) {
2759 Info(
"GetRandString",
"unknown option: %d : assume 0", opt);
2762 Info(
"GetRandString",
"enter ... len: %d %s", len, cOpt[opt]);
2765 char *buf =
new char[len + 1];
2768 TAuthenticate::InitRandom();
2772 Int_t i, j, l, m, frnd;
2775 for (m = 7; m < 32; m += 7) {
2776 i = 0x7F & (frnd >> m);
2779 if ((iimx[opt][j] & (1 << l))) {
2791 Info(
"GetRandString",
"got '%s' ", buf);
2802 Int_t TAuthenticate::SecureSend(TSocket *sock, Int_t enc,
2803 Int_t key,
const char *str)
2805 char buftmp[kMAXSECBUF];
2809 ::Info(
"TAuthenticate::SecureSend",
"local: enter ... (enc: %d)", enc);
2811 Int_t slen = strlen(str) + 1;
2816 strlcpy(buftmp, str, slen+1);
2819 ttmp = TRSA_fun::RSA_encode()(buftmp, slen, fgRSAPriKey.n,
2822 ttmp = TRSA_fun::RSA_encode()(buftmp, slen, fgRSAPubKey.n,
2826 }
else if (key == 1) {
2831 ttmp = ((ttmp + 8)/8) * 8;
2832 unsigned char iv[8];
2833 memset((
void *)&iv[0],0,8);
2834 BF_cbc_encrypt((
const unsigned char *)str, (
unsigned char *)buftmp,
2835 strlen(str), &fgBFKey, iv, BF_ENCRYPT);
2838 ::Info(
"TAuthenticate::SecureSend",
"not compiled with SSL support:"
2839 " you should not have got here!");
2843 ::Info(
"TAuthenticate::SecureSend",
"unknown key type (%d)",key);
2847 snprintf(buflen,20,
"%d",ttmp);
2848 if (sock->Send(buflen, kROOTD_ENCRYPT) < 0)
2850 nsen = sock->SendRaw(buftmp, ttmp);
2852 ::Info(
"TAuthenticate::SecureSend",
2853 "local: sent %d bytes (expected: %d)", nsen,ttmp);
2863 Int_t TAuthenticate::SecureRecv(TSocket *sock, Int_t dec, Int_t key,
char **str)
2866 char buftmp[kMAXSECBUF];
2875 if (sock->Recv(buflen, 20, kind) < 0)
2877 Int_t len = atoi(buflen);
2879 ::Info(
"TAuthenticate::SecureRecv",
"got len '%s' %d (msg kind: %d)",
2884 if (!strncmp(buflen,
"-1", 2))
2888 if ((nrec = sock->RecvRaw(buftmp, len)) < 0)
2892 TRSA_fun::RSA_decode()(buftmp, len, fgRSAPriKey.n, fgRSAPriKey.e);
2894 TRSA_fun::RSA_decode()(buftmp, len, fgRSAPubKey.n, fgRSAPubKey.e);
2899 const size_t strSize = strlen(buftmp) + 1;
2900 *str =
new char[strSize];
2901 strlcpy(*str, buftmp, strSize);
2903 }
else if (key == 1) {
2905 unsigned char iv[8];
2906 memset((
void *)&iv[0],0,8);
2907 *str =
new char[nrec + 1];
2908 BF_cbc_encrypt((
const unsigned char *)buftmp, (
unsigned char *)(*str),
2909 nrec, &fgBFKey, iv, BF_DECRYPT);
2910 (*str)[nrec] =
'\0';
2913 ::Info(
"TAuthenticate::SecureRecv",
"not compiled with SSL support:"
2914 " you should not have got here!");
2918 ::Info(
"TAuthenticate::SecureRecv",
"unknown key type (%d)",key);
2930 Int_t TAuthenticate::DecodeRSAPublic(
const char *rsaPubExport, R__rsa_NUMBER &rsa_n,
2931 R__rsa_NUMBER &rsa_d,
char **rsassl)
2937 ::Info(
"TAuthenticate::DecodeRSAPublic",
2938 "enter: string length: %ld bytes", (Long_t)strlen(rsaPubExport));
2940 char str[kMAXPATHLEN] = { 0 };
2941 Int_t klen = strlen(rsaPubExport);
2942 if (klen > kMAXPATHLEN - 1) {
2943 ::Info(
"TAuthenticate::DecodeRSAPublic",
2944 "key too long (%d): truncate to %d",klen,kMAXPATHLEN);
2945 klen = kMAXPATHLEN - 1;
2947 memcpy(str, rsaPubExport, klen);
2956 while (str[k] == 32) k++;
2958 if (str[k] ==
'#') {
2963 char *pd1 = strstr(str,
"#");
2964 char *pd2 = pd1 ? strstr(pd1 + 1,
"#") : (char *)0;
2965 char *pd3 = pd2 ? strstr(pd2 + 1,
"#") : (char *)0;
2966 if (pd1 && pd2 && pd3) {
2968 int l1 = (int) (pd2 - pd1 - 1);
2969 char *rsa_n_exp =
new char[l1 + 1];
2970 strlcpy(rsa_n_exp, pd1 + 1, l1+1);
2972 ::Info(
"TAuthenticate::DecodeRSAPublic",
2973 "got %ld bytes for rsa_n_exp", (Long_t)strlen(rsa_n_exp));
2975 int l2 = (int) (pd3 - pd2 - 1);
2976 char *rsa_d_exp =
new char[l2 + 1];
2977 strlcpy(rsa_d_exp, pd2 + 1, 13);
2979 ::Info(
"TAuthenticate::DecodeRSAPublic",
2980 "got %ld bytes for rsa_d_exp", (Long_t)strlen(rsa_d_exp));
2982 TRSA_fun::RSA_num_sget()(&rsa_n, rsa_n_exp);
2983 TRSA_fun::RSA_num_sget()(&rsa_d, rsa_d_exp);
2989 ::Info(
"TAuthenticate::DecodeRSAPublic",
"bad format for input string");
2998 BIO *bpub = BIO_new(BIO_s_mem());
3001 BIO_write(bpub,(
void *)str,strlen(str));
3004 if (!(rsatmp = PEM_read_bio_RSAPublicKey(bpub, 0, 0, 0))) {
3006 ::Info(
"TAuthenticate::DecodeRSAPublic",
3007 "unable to read pub key from bio");
3010 *rsassl = (
char *)rsatmp;
3012 ::Info(
"TAuthenticate::DecodeRSAPublic",
3013 "no space allocated for output variable");
3020 ::Info(
"TAuthenticate::DecodeRSAPublic",
"not compiled with SSL support:"
3021 " you should not have got here!");
3033 Int_t TAuthenticate::SetRSAPublic(
const char *rsaPubExport, Int_t klen)
3036 ::Info(
"TAuthenticate::SetRSAPublic",
3037 "enter: string length %ld bytes", (Long_t)strlen(rsaPubExport));
3047 while (rsaPubExport[k0] == 32) k0++;
3056 if (rsaPubExport[k0] ==
'#' && rsaPubExport[k2] ==
'#') {
3057 char *p0 = (
char *)&rsaPubExport[k0];
3058 char *p2 = (
char *)&rsaPubExport[k2];
3059 char *p1 = strchr(p0+1,
'#');
3060 if (p1 > p0 && p1 < p2) {
3061 Int_t l01 = (Int_t)(p1-p0)-1;
3062 Int_t l12 = (Int_t)(p2-p1)-1;
3063 if (l01 >= kPRIMELENGTH*2 && l12 >= kPRIMELENGTH*2) {
3066 while (c < p1 && ((*c < 58 && *c > 47) || (*c < 91 && *c > 64)))
3070 while (c < p2 && ((*c < 58 && *c > 47) || (*c < 91 && *c > 64)))
3079 ::Info(
"TAuthenticate::SetRSAPublic",
" Key type: %d",rsakey);
3083 R__rsa_NUMBER rsa_n, rsa_d;
3084 rsakey = TAuthenticate::DecodeRSAPublic(rsaPubExport,rsa_n,rsa_d);
3087 TRSA_fun::RSA_assign()(&fgRSAPubKey.n, &rsa_n);
3088 TRSA_fun::RSA_assign()(&fgRSAPubKey.e, &rsa_d);
3094 BF_set_key(&fgBFKey, klen, (
const unsigned char *)rsaPubExport);
3097 ::Info(
"TAuthenticate::SetRSAPublic",
3098 "not compiled with SSL support:"
3099 " you should not have got here!");
3111 Int_t TAuthenticate::SendRSAPublicKey(TSocket *socket, Int_t key)
3114 char serverPubKey[kMAXSECBUF];
3116 if ((nr = socket->Recv(serverPubKey, kMAXSECBUF, kind)) < 0)
3119 ::Info(
"TAuthenticate::SendRSAPublicKey",
3120 "received key from server %ld bytes", (Long_t)strlen(serverPubKey));
3123 R__rsa_NUMBER rsa_n, rsa_d;
3126 if (TAuthenticate::DecodeRSAPublic(serverPubKey,rsa_n,rsa_d,
3129 RSA_free((RSA *)tmprsa);
3132 RSA *RSASSLServer = (RSA *)tmprsa;
3134 if (TAuthenticate::DecodeRSAPublic(serverPubKey,rsa_n,rsa_d) != key)
3139 char buftmp[kMAXSECBUF] = {0};
3140 char buflen[20] = {0};
3141 Int_t slen = fgRSAPubExport[key].len;
3144 strlcpy(buftmp,fgRSAPubExport[key].keys,slen+1);
3145 ttmp = TRSA_fun::RSA_encode()(buftmp, slen, rsa_n, rsa_d);
3146 snprintf(buflen, 20,
"%d", ttmp);
3147 }
else if (key == 1) {
3149 Int_t lcmax = RSA_size(RSASSLServer) - 11;
3154 Int_t lc = (ns > lcmax) ? lcmax : ns ;
3155 if ((ttmp = RSA_public_encrypt(lc,
3156 (
unsigned char *)&fgRSAPubExport[key].keys[kk],
3157 (
unsigned char *)&buftmp[ke],
3158 RSASSLServer,RSA_PKCS1_PADDING)) < 0) {
3160 ERR_error_string(ERR_get_error(), errstr);
3161 ::Info(
"TAuthenticate::SendRSAPublicKey",
"SSL: error: '%s' ",errstr);
3168 snprintf(buflen, 20,
"%d", ttmp);
3171 ::Info(
"TAuthenticate::SendRSAPublicKey",
"not compiled with SSL support:"
3172 " you should not have got here!");
3177 ::Info(
"TAuthenticate::SendRSAPublicKey",
"unknown key type (%d)",key);
3180 RSA_free(RSASSLServer);
3186 if ((nr = socket->Send(buflen, kROOTD_ENCRYPT)) < 0)
3189 Int_t nsen = socket->SendRaw(buftmp, ttmp);
3191 ::Info(
"TAuthenticate::SendRSAPublicKey",
3192 "local: sent %d bytes (expected: %d)", nsen,ttmp);
3195 RSA_free(RSASSLServer);
3206 Int_t TAuthenticate::ReadRootAuthrc()
3210 if (gSystem->Getenv(
"ROOTAUTHRC") != 0) {
3211 authrc = StrDup(gSystem->Getenv(
"ROOTAUTHRC"));
3213 if (fgReadHomeAuthrc)
3214 authrc = gSystem->ConcatFileName(gSystem->HomeDirectory(),
".rootauthrc");
3216 if (authrc && gDebug > 2)
3217 ::Info(
"TAuthenticate::ReadRootAuthrc",
"Checking file: %s", authrc);
3218 if (!authrc || gSystem->AccessPathName(authrc, kReadPermission)) {
3219 if (authrc && gDebug > 1)
3220 ::Info(
"TAuthenticate::ReadRootAuthrc",
3221 "file %s cannot be read (errno: %d)", authrc, errno);
3223 authrc = gSystem->ConcatFileName(TROOT::GetEtcDir(),
"system.rootauthrc");
3225 ::Info(
"TAuthenticate::ReadRootAuthrc",
"Checking system file: %s", authrc);
3226 if (gSystem->AccessPathName(authrc, kReadPermission)) {
3228 ::Info(
"TAuthenticate::ReadRootAuthrc",
3229 "file %s cannot be read (errno: %d)", authrc, errno);
3236 TString tRootAuthrc = authrc;
3237 if (tRootAuthrc == fgRootAuthrc) {
3239 stat(tRootAuthrc, &si);
3240 if ((UInt_t)si.st_mtime < fgLastAuthrc.Convert()) {
3242 ::Info(
"TAuthenticate::ReadRootAuthrc",
3243 "file %s already read", authrc);
3250 fgRootAuthrc = tRootAuthrc;
3251 fgLastAuthrc = TDatime();
3254 TList *authinfo = TAuthenticate::GetAuthInfo();
3255 TList *proofauthinfo = TAuthenticate::GetProofAuthInfo();
3259 TString filetmp =
"rootauthrc";
3260 FILE *ftmp = gSystem->TempFileName(filetmp);
3262 ::Info(
"TAuthenticate::ReadRootAuthrc",
"got tmp file: %s open at 0x%lx",
3263 filetmp.Data(), (Long_t)ftmp);
3270 TAuthenticate::FileExpand(authrc, ftmp);
3275 fd = fopen(authrc,
"r");
3278 ::Info(
"TAuthenticate::ReadRootAuthrc",
3279 "file %s cannot be open (errno: %d)", authrc, errno);
3287 char line[kMAXPATHLEN];
3288 Bool_t cont = kFALSE;
3290 while (fgets(line,
sizeof(line), fd) != 0) {
3297 if (line[strlen(line) - 1] ==
'\n')
3298 line[strlen(line) - 1] =
'\0';
3305 const size_t tmpSize = strlen(line) + 1;
3306 char *tmp =
new char[tmpSize];
3308 ::Error(
"TAuthenticate::ReadRootAuthrc",
3309 "could not allocate temporary buffer");
3313 strlcpy(tmp, line, tmpSize);
3314 char *nxt = strtok(tmp,
" ");
3316 if (!strcmp(nxt,
"proofserv") || cont) {
3326 proofserv += TString((
const char *)ph);
3327 proofserv += TString(
" ");
3337 TString hostsrv = nxt;
3338 TString host = hostsrv;
3339 TString server =
"";
3340 if (hostsrv.Contains(
":")) {
3342 host.Remove(host.Index(
":"));
3343 server.Remove(0,server.Index(
":")+1);
3346 if (server.Length()) {
3347 if (server ==
"0" || server.BeginsWith(
"sock"))
3348 srvtyp = TSocket::kSOCKD;
3349 else if (server ==
"1" || server.BeginsWith(
"root"))
3350 srvtyp = TSocket::kROOTD;
3351 else if (server ==
"2" || server.BeginsWith(
"proof"))
3352 srvtyp = TSocket::kPROOFD;
3358 nxt = strtok(0,
" ");
3359 if (!strncmp(nxt,
"user",4)) {
3360 nxt = strtok(0,
" ");
3361 if (strncmp(nxt,
"list",4) && strncmp(nxt,
"method",6)) {
3362 user = TString(nxt);
3363 nxt = strtok(0,
" ");
3368 TIter next(&tmpAuthInfo);
3370 while ((ha = (THostAuth *)next())) {
3371 if (host == ha->GetHost() && user == ha->GetUser() &&
3372 srvtyp == ha->GetServer())
3377 ha =
new THostAuth(host,srvtyp,user);
3378 tmpAuthInfo.Add(ha);
3381 if (!strncmp(nxt,
"list",4)) {
3383 Int_t nm = 0, me[kMAXSEC] = {0};
3384 char *mth = strtok(0,
" ");
3387 if (strlen(mth) > 1) {
3389 met = GetAuthMethodIdx(mth);
3390 if (met == -1 && gDebug > 2)
3391 ::Info(
"TAuthenticate::ReadRootAuthrc",
3392 "unrecognized method (%s): ", mth);
3396 if (met > -1 && met < kMAXSEC)
3398 mth = strtok(0,
" ");
3403 }
else if (!strncmp(nxt,
"method",6)) {
3406 char *mth = strtok(0,
" ");
3408 if (strlen(mth) > 1) {
3410 met = GetAuthMethodIdx(mth);
3411 if (met == -1 && gDebug > 2)
3412 ::Info(
"TAuthenticate::ReadRootAuthrc",
3413 "unrecognized method (%s): ", mth);
3417 if (met > -1 && met < kMAXSEC) {
3418 const char *det = 0;
3419 nxt = strtok(0,
" ");
3421 det = (
const char *)strstr(line,nxt);
3423 if (ha->HasMethod(met))
3424 ha->SetDetails(met,det);
3426 ha->AddMethod(met,det);
3430 if (tmp)
delete [] tmp;
3435 gSystem->Unlink(filetmp);
3440 TAuthenticate::MergeHostAuthList(authinfo,&tmpAuthInfo);
3444 TAuthenticate::Show();
3448 TList tmpproofauthinfo;
3449 if (proofserv.Length() > 0) {
3450 char *tmps =
new char[proofserv.Length()+1];
3451 strlcpy(tmps,proofserv.Data(),proofserv.Length()+1);
3452 char *nxt = strtok(tmps,
" ");
3454 TString tmp((
const char *)nxt);
3458 if ((pdd = tmp.Index(
":")) == -1) {
3465 tmp.Remove(0,pdd+1);
3469 if ((pdd = tmp.Index(
":")) == -1) {
3476 tmp.Remove(0,pdd+1);
3480 Int_t nm = 0, me[kMAXSEC] = {0}, met = -1;
3481 while (tmp.Length() > 0) {
3483 if ((pdd = tmp.Index(
":")) > -1)
3485 if (meth.Length() > 1) {
3487 met = GetAuthMethodIdx(meth.Data());
3488 if (met == -1 && gDebug > 2)
3489 ::Info(
"TAuthenticate::ReadRootAuthrc",
3490 "unrecognized method (%s): ",meth.Data());
3491 }
else if (meth.Length() == 1) {
3492 met = atoi(meth.Data());
3493 if (met > -1 && met < kMAXSEC)
3497 tmp.Remove(0,pdd+1);
3504 THostAuth *hatmp = TAuthenticate::GetHostAuth(host,user);
3506 ha =
new THostAuth(host,user,nm,me,0);
3509 ha =
new THostAuth(host,user);
3516 tmpproofauthinfo.Add(ha);
3518 nxt = strtok(0,
" ");
3524 TAuthenticate::MergeHostAuthList(proofauthinfo,&tmpproofauthinfo,
"P");
3527 TAuthenticate::Show(
"P");
3529 return authinfo->GetSize();
3535 Bool_t TAuthenticate::CheckProofAuth(Int_t cSec, TString &out)
3538 const char netrc[2][20] = {
"/.netrc",
"/.rootnetrc" };
3542 UserGroup_t *pw = gSystem->GetUserInfo();
3544 user = TString(pw->fUser);
3547 ::Info(
"CheckProofAuth",
3548 "not properly logged on (getpwuid unable to find relevant info)!");
3554 if (cSec == (Int_t) TAuthenticate::kClear) {
3556 for (; i < 2; i++) {
3557 TString infofile = TString(gSystem->HomeDirectory())+TString(netrc[i]);
3558 if (!gSystem->AccessPathName(infofile, kReadPermission))
3562 out.Form(
"pt:0 ru:1 us:%s",user.Data());
3566 if (strlen(out) > 0)
3567 ::Info(
"CheckProofAuth",
3568 "meth: %d ... is available: details: %s", cSec, out.Data());
3570 ::Info(
"CheckProofAuth",
3571 "meth: %d ... is NOT available", cSec);
3584 Int_t StdCheckSecCtx(
const char *user, TRootSecContext *ctx)
3588 if (ctx->IsActive()) {
3589 if (!strcmp(user,ctx->GetUser()) &&
3590 strncmp(
"AFS", ctx->GetID(), 3))
3605 void TAuthenticate::MergeHostAuthList(TList *std, TList *nin, Option_t *opt)
3610 while ((ha = (THostAuth *) nxstd())) {
3611 if (!ha->IsActive()) {
3620 while ((hanew = (THostAuth *)nxnew())) {
3621 if (hanew->NumMethods()) {
3623 hostsrv.Form(
"%s:%d",hanew->GetHost(),hanew->GetServer());
3625 TAuthenticate::HasHostAuth(hostsrv,hanew->GetUser(),opt);
3628 hastd->Update(hanew);
3630 hanew->DeActivate();
3637 hanew->DeActivate();
3642 while ((hanew = (THostAuth *)nxnew())) {
3643 if (!hanew->IsActive()) {
3655 void TAuthenticate::RemoveSecContext(TRootSecContext *ctx)
3660 TIter nxai(GetAuthInfo());
3661 while ((ha = (THostAuth *)nxai())) {
3662 TIter next(ha->Established());
3663 TRootSecContext *lctx = 0;
3664 while ((lctx = (TRootSecContext *) next())) {
3666 ha->Established()->Remove(ctx);
3673 TIter nxpa(GetProofAuthInfo());
3674 while ((ha = (THostAuth *)nxpa())) {
3675 TIter next(ha->Established());
3676 TRootSecContext *lctx = 0;
3677 while ((lctx = (TRootSecContext *) next())) {
3679 ha->Established()->Remove(ctx);
3693 Int_t TAuthenticate::ProofAuthSetup()
3695 static Bool_t done = kFALSE;
3703 const char *p = gSystem->Getenv(
"ROOTPROOFAUTHSETUP");
3706 Info(
"ProofAuthSetup",
"Buffer not found: nothing to do");
3709 TString mbuf = TBase64::Decode(p);
3712 TMessage *mess =
new TMessage((
void*)mbuf.Data(), mbuf.Length()+
sizeof(UInt_t));
3716 TString passwd =
"";
3717 Bool_t pwhash = kFALSE;
3718 Bool_t srppwd = kFALSE;
3720 *mess >> user >> passwd >> pwhash >> srppwd >> rsakey;
3723 TAuthenticate::SetGlobalUser(user);
3724 TAuthenticate::SetGlobalPasswd(passwd);
3725 TAuthenticate::SetGlobalPwHash(pwhash);
3726 TAuthenticate::SetDefaultRSAKeyType(rsakey);
3727 const char *h = gSystem->Getenv(
"ROOTHOMEAUTHRC");
3729 Bool_t rha = (Bool_t)(strtol(h, (
char **)0, 10));
3730 TAuthenticate::SetReadHomeAuthrc(rha);
3734 TList *pha = (TList *)mess->ReadObject(TList::Class());
3737 Info(
"ProofAuthSetup",
"List of THostAuth not found");
3741 Bool_t master = gROOT->IsProofServ();
3744 while ((ha = (THostAuth *)next())) {
3748 THostAuth *haex = 0;
3749 Bool_t fromProofAI = kFALSE;
3752 haex = TAuthenticate::GetHostAuth(ha->GetHost(),ha->GetUser(),
"P",&kExact);
3756 TAuthenticate::GetHostAuth(ha->GetHost(),ha->GetUser(),
"R",&kExact);
3758 fromProofAI = kTRUE;
3761 haex = TAuthenticate::GetHostAuth(ha->GetHost(),ha->GetUser(),
"R",&kExact);
3769 if (!master || fromProofAI) {
3778 TAuthenticate::GetProofAuthInfo()->Add(ha);
3783 for (; i < haex->NumMethods(); i++) {
3784 Int_t met = haex->GetMethod(i);
3785 if (!ha->HasMethod(met))
3786 ha->AddMethod(met,haex->GetDetails(met));
3790 TAuthenticate::GetProofAuthInfo()->Add(ha);
3793 TAuthenticate::GetAuthInfo()->Add(ha);
3798 TAuthenticate::GetProofAuthInfo()->Add(ha);
3801 TAuthenticate::GetAuthInfo()->Add(ha);
3814 Int_t TAuthenticate::ProofAuthSetup(TSocket *sock, Bool_t )
3817 TSecContext *sc = sock->GetSecContext();
3818 TString user = sc->GetUser();
3819 Int_t remoteOffSet = sc->GetOffSet();
3824 TString passwd =
"";
3825 Bool_t pwhash = kFALSE;
3826 Bool_t srppwd = kFALSE;
3828 Bool_t upwd = sc->IsA(
"UsrPwd");
3830 TPwdCtx *pwdctx = 0;
3831 if (remoteOffSet > -1 && upwd)
3832 pwdctx = (TPwdCtx *)(sc->GetContext());
3834 if (upwd && pwdctx) {
3835 passwd = pwdctx->GetPasswd();
3836 pwhash = pwdctx->IsPwHash();
3839 Int_t keytyp = ((TRootSecContext *)sc)->GetRSAKey();
3843 mess << user << passwd << pwhash << srppwd << keytyp;
3846 mess.WriteObject(TAuthenticate::GetProofAuthInfo());
3849 char *mbuf = mess.Buffer();
3850 Int_t mlen = mess.Length();
3851 TString messb64 = TBase64::Encode(mbuf, mlen);
3854 ::Info(
"ProofAuthSetup",
"sending %d bytes", messb64.Length());
3857 if (remoteOffSet > -1) {
3858 if (TAuthenticate::SecureSend(sock, 1, keytyp, messb64.Data()) == -1) {
3859 ::Error(
"ProofAuthSetup",
"problems secure-sending message buffer");
3865 snprintf(buflen,20,
"%d", messb64.Length());
3866 if (sock->Send(buflen, kMESS_ANY) < 0) {
3867 ::Error(
"ProofAuthSetup",
"plain: problems sending message length");
3870 if (sock->SendRaw(messb64.Data(), messb64.Length()) < 0) {
3871 ::Error(
"ProofAuthSetup",
"problems sending message buffer");
3883 Int_t TAuthenticate::GetClientProtocol()
3885 return TSocket::GetClientProtocol();
3902 static Int_t SendHostAuth(TSocket *s)
3904 Int_t retval = 0, ns = 0;
3907 Error(
"SendHostAuth",
"invalid input: socket undefined");
3912 TIter next(TAuthenticate::GetProofAuthInfo());
3914 while ((ha = (THostAuth *)next())) {
3917 if((ns = s->Send(buf, kPROOF_HOSTAUTH)) < 1) {
3922 Info(
"SendHostAuth",
"sent %d bytes (%s)",ns,buf.Data());
3926 if ((ns = s->Send(
"END", kPROOF_HOSTAUTH)) < 1)
3929 Info(
"SendHostAuth",
"sent %d bytes for closing",ns);
3940 static Int_t RecvHostAuth(TSocket *s, Option_t *opt)
3943 Error(
"RecvHostAuth",
"invalid input: socket undefined");
3948 Bool_t master = !strncasecmp(opt,
"M",1) ? kTRUE : kFALSE;
3951 TAuthenticate::ReadRootAuthrc();
3955 char buf[kMAXSECBUF];
3956 Int_t nr = s->Recv(buf, kMAXSECBUF, kind);
3957 if (nr < 0 || kind != kPROOF_HOSTAUTH) {
3958 Error(
"RecvHostAuth",
"received: kind: %d (%d bytes)", kind, nr);
3962 Info(
"RecvHostAuth",
"received %d bytes (%s)",nr,buf);
3964 while (strcmp(buf,
"END")) {
3966 Int_t nc = (nr >= kMAXSECBUF) ? kMAXSECBUF - 1 : nr ;
3970 THostAuth *ha =
new THostAuth((
const char *)&buf);
3974 THostAuth *haex = 0;
3975 Bool_t fromProofAI = kFALSE;
3978 haex = TAuthenticate::GetHostAuth(ha->GetHost(),ha->GetUser(),
"P",&kExact);
3982 TAuthenticate::GetHostAuth(ha->GetHost(),ha->GetUser(),
"R",&kExact);
3984 fromProofAI = kTRUE;
3987 haex = TAuthenticate::GetHostAuth(ha->GetHost(),ha->GetUser(),
"R",&kExact);
3995 if (!master || fromProofAI) {
4004 TAuthenticate::GetProofAuthInfo()->Add(ha);
4009 for (; i < haex->NumMethods(); i++) {
4010 Int_t met = haex->GetMethod(i);
4011 if (!ha->HasMethod(met))
4012 ha->AddMethod(met,haex->GetDetails(met));
4016 TAuthenticate::GetProofAuthInfo()->Add(ha);
4019 TAuthenticate::GetAuthInfo()->Add(ha);
4024 TAuthenticate::GetProofAuthInfo()->Add(ha);
4027 TAuthenticate::GetAuthInfo()->Add(ha);
4032 nr = s->Recv(buf, kMAXSECBUF, kind);
4033 if (nr < 0 || kind != kPROOF_HOSTAUTH) {
4034 Info(
"RecvHostAuth",
"Error: received: kind: %d (%d bytes)", kind, nr);
4038 Info(
"RecvHostAuth",
"received %d bytes (%s)",nr,buf);
4051 Int_t OldSlaveAuthSetup(TSocket *sock,
4052 Bool_t , TString ord, TString conf)
4056 TSecContext *sc = sock->GetSecContext();
4057 TString user = sc->GetUser();
4058 Int_t proofdProto = sock->GetRemoteProtocol();
4059 Int_t remoteOffSet = sc->GetOffSet();
4064 TString passwd =
"";
4065 Bool_t pwhash = kFALSE;
4066 Bool_t srppwd = kFALSE;
4068 Bool_t upwd = sc->IsA(
"UsrPwd");
4070 TPwdCtx *pwdctx = 0;
4071 if (remoteOffSet > -1 && upwd)
4072 pwdctx = (TPwdCtx *)(sc->GetContext());
4074 if (upwd && pwdctx) {
4077 if (sock->Send(remoteOffSet, kROOTD_RSAKEY) != 2*
sizeof(Int_t)) {
4078 Error(
"OldAuthSetup",
"failed to send offset in RSA key");
4083 passwd = pwdctx->GetPasswd();
4084 pwhash = pwdctx->IsPwHash();
4087 Int_t keytyp = ((TRootSecContext *)sc)->GetRSAKey();
4088 if (TAuthenticate::SecureSend(sock, 1, keytyp, passwd.Data()) == -1) {
4089 if (remoteOffSet > -1)
4090 Warning(
"OldAuthSetup",
"problems secure-sending pass hash %s",
4091 "- may result in failures");
4094 for (
int i = 0; i < passwd.Length(); i++) {
4095 char inv = ~passwd(i);
4096 passwd.Replace(i, 1, inv);
4100 if (sock->Send(mess) < 0) {
4101 Error(
"OldAuthSetup",
"failed to send inverted password");
4110 if (sock->Send(-2, kROOTD_RSAKEY) != 2*
sizeof(Int_t)) {
4111 Error(
"OldAuthSetup",
"failed to send no offset notification in RSA key");
4118 mess << user << pwhash << srppwd << ord << conf;
4120 if (sock->Send(mess) < 0) {
4121 Error(
"OldAuthSetup",
"failed to send ordinal and config info");
4125 if (proofdProto > 6) {
4130 if (SendHostAuth(sock) < 0) {
4131 Error(
"OldAuthSetup",
"failed to send HostAuth info");
4145 Int_t OldProofServAuthSetup(TSocket *sock, Bool_t master, Int_t protocol,
4146 TString &user, TString &ord, TString &conf)
4150 if (sock->Recv(retval, kind) != 2*
sizeof(Int_t)) {
4152 Info(
"OldProofServAuthSetup",
4153 "socket has been closed due to protocol mismatch - Exiting");
4159 if (kind == kROOTD_RSAKEY) {
4162 if (gSystem->Getenv(
"ROOTKEYFILE")) {
4164 TString keyfile = gSystem->Getenv(
"ROOTKEYFILE");
4168 char pubkey[kMAXPATHLEN] = { 0 };
4169 if (!gSystem->AccessPathName(keyfile.Data(), kReadPermission)) {
4170 if ((fKey = fopen(keyfile.Data(),
"r"))) {
4171 Int_t klen = fread((
void *)pubkey,1,
sizeof(pubkey),fKey);
4173 Error(
"OldProofServAuthSetup",
4174 "failed to read public key from '%s'", keyfile.Data());
4180 rsakey = TAuthenticate::SetRSAPublic(pubkey,klen);
4183 Error(
"OldProofServAuthSetup",
"failed to open '%s'", keyfile.Data());
4191 if (TAuthenticate::SecureRecv(sock, 2, rsakey, &pwd) < 0) {
4192 Error(
"OldProofServAuthSetup",
"failed to receive password");
4198 }
else if (retval == -1) {
4202 if ((sock->Recv(mess) <= 0) || !mess) {
4203 Error(
"OldProofServAuthSetup",
"failed to receive inverted password");
4209 for (Int_t i = 0; i < passwd.Length(); i++) {
4210 char inv = ~passwd(i);
4211 passwd.Replace(i, 1, inv);
4219 if ((sock->Recv(mess) <= 0) || !mess) {
4220 Error(
"OldProofServAuthSetup",
"failed to receive ordinal and config info");
4225 Bool_t pwhash, srppwd;
4228 (*mess) >> user >> pwhash >> srppwd >> conf;
4231 (*mess) >> user >> pwhash >> srppwd >> ord >> conf;
4236 (*mess) >> user >> pwhash >> srppwd >> iord;
4240 (*mess) >> user >> pwhash >> srppwd >> ord >> conf;
4246 TAuthenticate::SetGlobalUser(user);
4247 TAuthenticate::SetGlobalPasswd(passwd);
4248 TAuthenticate::SetGlobalPwHash(pwhash);
4249 TAuthenticate::SetDefaultRSAKeyType(rsakey);
4250 const char *h = gSystem->Getenv(
"ROOTHOMEAUTHRC");
4252 Bool_t rha = (Bool_t)(strtol(h, (
char **)0, 10));
4253 TAuthenticate::SetReadHomeAuthrc(rha);
4258 Int_t harc = master ? RecvHostAuth(sock,
"M") : RecvHostAuth(sock,
"S");
4261 Error(
"OldProofServAuthSetup",
"failed to receive HostAuth info");