<commit_before>// @(#)root/proof:$Name:  $:$Id: TSlave.cxx,v 1.20 2003/11/07 03:29:42 rdm Exp $
// Author: <NAME>   14/02/97

/*************************************************************************
 * Copyright (C) 1995-2000, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TSlave                                                               //
//                                                                      //
// This class describes a PROOF slave server.                           //
// It contains information like the slaves host name, ordinal number,   //
// performance index, socket, etc. Objects of this class can only be    //
// created via TProof member functions.                                 //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TSlave.h"
#include "TProof.h"
#include "TSocket.h"
#include "TSystem.h"
#include "TEnv.h"
#include "TROOT.h"
#include "TAuthenticate.h"
#include "TMessage.h"
#include "THostAuth.h"

ClassImp(TSlave)

//______________________________________________________________________________
TSlave::TSlave(const char *host, Int_t port, Int_t ord, Int_t perf,
               const char *image, Int_t security, TProof *proof)
{
   // Create a PROOF slave object. Called via the TProof ctor.

   fName     = host;
   fPort     = port;
   fImage    = image;
   fWorkDir  = kPROOF_WorkDir;
   fOrdinal  = ord;
   fPerfIdx  = perf;
   fSecurity = security;
   fProof    = proof;
   fSocket   = 0;
   fInput    = 0;

   // Open connection to remote PROOF slave server.
   fSocket = new TSocket(host, port, 65536);  // make tcpwindosize configurable
   if (fSocket->IsValid()) {

      // Remove socket from global TROOT socket list. Only the TProof object,
      // representing all slave sockets, will be added to this list. This will
      // ensure the correct termination of all proof servers in case the
      // root session terminates.
      gROOT->GetListOfSockets()->Remove(fSocket);

      // Tell remote server to act as master or slave server
      if (proof->IsMaster())
         fSocket->Send("slave");
      else
         fSocket->Send("master");

      // Inquire remote protocol
      fSocket->Send(kROOTD_PROTOCOL);
      Int_t    ProofdProto, what;
      fSocket->Recv(ProofdProto, what);
      if (ProofdProto <= 0) {
         Warning("TSlave",
                 "got negative protocol from proofd (%d) ... may indicate problems",
                  ProofdProto);
         ProofdProto = 7;
         Warning("TSlave", "continuing with ProofdProto: %d)",ProofdProto);
      }

      TAuthenticate *auth;
      auth = new TAuthenticate(fSocket, host,
                               Form("%s:%d", proof->GetUrlProt(),ProofdProto), "");

      // If trasmission of the SRP password is requested make sure
      // that ReUse is switched on to get and send also the Public Key
      // For masters this is already checked in TProofServ
      if (!fProof->IsMaster()) {
         if (gEnv->GetValue("Proofd.SendSRPPwd",0)) {
            TString SRPDets(auth->GetHostAuth()->GetDetails(1));
            Int_t pos = SRPDets.Index("ru:0");
            if (pos > -1) {
               SRPDets.ReplaceAll("ru:0",4,"ru:1",4);
               auth->GetHostAuth()->SetDetails(1,SRPDets);
            } else {
               TSubString ss = SRPDets.SubString("ru:no",TString::kIgnoreCase);
               if (!ss.IsNull()) {
                  SRPDets.ReplaceAll(ss.Data(),5,"ru:1",4);
                  auth->GetHostAuth()->SetDetails(1,SRPDets);
               }
            }
         }
      }

      // Authenticate to proofd...
      if (!proof->IsMaster()) {

         // we are client... need full authentication procedure, either:
         // - via TAuthenticate::SetGlobalUser()/SetGlobalPasswd())
         // - ~/.rootnetrc or ~/.netrc
         // - interactive
         if (!auth->Authenticate()) {
            Error("TSlave", "authentication failed for %s@%s",
                            auth->GetUser(), host);
            delete auth;
            // This is to terminate properly remote proofd in case of failure
            fSocket->Send(Form("%d %s", getpid(), host), kROOTD_CLEANUP);
            SafeDelete(fSocket);
            return;
         }
         proof->fUser     = auth->GetRemoteLogin(auth->GetHostAuth(),
                                                 auth->GetSecurity(),auth->GetDetails());
         proof->fPasswd   = TAuthenticate::GetGlobalPasswd();
         proof->fPwHash   = TAuthenticate::GetGlobalPwHash();
         proof->fSRPPwd   = TAuthenticate::GetGlobalSRPPwd();
         proof->fSecurity = auth->GetSecurity();
         fUser            = proof->fUser;
         fSecurity        = proof->fSecurity;

         PDB(kGlobal,3) {
            auth->GetHostAuth()->PrintEstablished();
            Info("TSlave", "Remote Client: fUser is .... %s", proof->fUser.Data());
         }

      } else {

         // we are a master server... authenticate either:
         // - stored user/passwd (coming from client)
         // - ~/.rootnetrc or ~/.netrc
         // - but NOT interactive (obviously)

         // check for .rootnetrc
         // if not in .rootnetrc and SRP -> fail
         // if not in .rootnetrc and normal -> set global user/passwd
         // Checks for (user,passwd) are done inside TAuthenticate now (4/2003)

         if (!auth->Authenticate()) {
            Error("TSlave", "authentication failed for %s@%s",
                            auth->GetUser(), host);
            delete auth;
            // This is to terminate properly remote proofd in case of failure
            fSocket->Send(Form("%d %s", getpid(), host), kROOTD_CLEANUP);
            SafeDelete(fSocket);
            return;
         }

         proof->fUser     = auth->GetRemoteLogin(auth->GetHostAuth(),
                                                 auth->GetSecurity(),auth->GetDetails());
         proof->fPasswd   = auth->GetPasswd();
         proof->fPwHash   = auth->GetPwHash();
         proof->fSRPPwd   = auth->GetSRPPwd();
         proof->fSecurity = auth->GetSecurity();
         fUser            = proof->fUser;
         fSecurity        = proof->fSecurity;

         PDB(kGlobal,3) {
            auth->GetHostAuth()->PrintEstablished();
            Info("TSlave", "Master Server: fUser is .... %s", fUser.Data());
         }
      }

      // fSecurity is the method successfully tried ...<commit_msg>use gSystem->GetPid() instead of directly getpid().<commit_after>// @(#)root/proof:$Name:  $:$Id: TSlave.cxx,v 1.21 2003/11/26 10:33:08 rdm Exp $
// Author: <NAME>   14/02/97

/*************************************************************************
 * Copyright (C) 1995-2000, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TSlave                                                               //
//                                                                      //
// This class describes a PROOF slave server.                           //
// It contains information like the slaves host name, ordinal number,   //
// performance index, socket, etc. Objects of this class can only be    //
// created via TProof member functions.                                 //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TSlave.h"
#include "TProof.h"
#include "TSocket.h"
#include "TSystem.h"
#include "TEnv.h"
#include "TROOT.h"
#include "TAuthenticate.h"
#include "TMessage.h"
#include "THostAuth.h"

ClassImp(TSlave)

//______________________________________________________________________________
TSlave::TSlave(const char *host, Int_t port, Int_t ord, Int_t perf,
               const char *image, Int_t security, TProof *proof)
{
   // Create a PROOF slave object. Called via the TProof ctor.

   fName     = host;
   fPort     = port;
   fImage    = image;
   fWorkDir  = kPROOF_WorkDir;
   fOrdinal  = ord;
   fPerfIdx  = perf;
   fSecurity = security;
   fProof    = proof;
   fSocket   = 0;
   fInput    = 0;

   // Open connection to remote PROOF slave server.
   fSocket = new TSocket(host, port, 65536);  // make tcpwindosize configurable
   if (fSocket->IsValid()) {

      // Remove socket from global TROOT socket list. Only the TProof object,
      // representing all slave sockets, will be added to this list. This will
      // ensure the correct termination of all proof servers in case the
      // root session terminates.
      gROOT->GetListOfSockets()->Remove(fSocket);

      // Tell remote server to act as master or slave server
      if (proof->IsMaster())
         fSocket->Send("slave");
      else
         fSocket->Send("master");

      // Inquire remote protocol
      fSocket->Send(kROOTD_PROTOCOL);
      Int_t    ProofdProto, what;
      fSocket->Recv(ProofdProto, what);
      if (ProofdProto <= 0) {
         Warning("TSlave",
                 "got negative protocol from proofd (%d) ... may indicate problems",
                  ProofdProto);
         ProofdProto = 7;
         Warning("TSlave", "continuing with ProofdProto: %d)",ProofdProto);
      }

      TAuthenticate *auth;
      auth = new TAuthenticate(fSocket, host,
                               Form("%s:%d", proof->GetUrlProt(),ProofdProto), "");

      // If trasmission of the SRP password is requested make sure
      // that ReUse is switched on to get and send also the Public Key
      // For masters this is already checked in TProofServ
      if (!fProof->IsMaster()) {
         if (gEnv->GetValue("Proofd.SendSRPPwd",0)) {
            TString SRPDets(auth->GetHostAuth()->GetDetails(1));
            Int_t pos = SRPDets.Index("ru:0");
            if (pos > -1) {
               SRPDets.ReplaceAll("ru:0",4,"ru:1",4);
               auth->GetHostAuth()->SetDetails(1,SRPDets);
            } else {
               TSubString ss = SRPDets.SubString("ru:no",TString::kIgnoreCase);
               if (!ss.IsNull()) {
                  SRPDets.ReplaceAll(ss.Data(),5,"ru:1",4);
                  auth->GetHostAuth()->SetDetails(1,SRPDets);
               }
            }
         }
      }

      // Authenticate to proofd...
      if (!proof->IsMaster()) {

         // we are client... need full authentication procedure, either:
         // - via TAuthenticate::SetGlobalUser()/SetGlobalPasswd())
         // - ~/.rootnetrc or ~/.netrc
         // - interactive
         if (!auth->Authenticate()) {
            Error("TSlave", "authentication failed for %s@%s",
                            auth->GetUser(), host);
            delete auth;
            // This is to terminate properly remote proofd in case of failure
            fSocket->Send(Form("%d %s", gSystem->GetPid(), host), kROOTD_CLEANUP);
            SafeDelete(fSocket);
            return;
         }
         proof->fUser     = auth->GetRemoteLogin(auth->GetHostAuth(),
                                                 auth->GetSecurity(),auth->GetDetails());
         proof->fPasswd   = TAuthenticate::GetGlobalPasswd();
         proof->fPwHash   = TAuthenticate::GetGlobalPwHash();
         proof->fSRPPwd   = TAuthenticate::GetGlobalSRPPwd();
         proof->fSecurity = auth->GetSecurity();
         fUser            = proof->fUser;
         fSecurity        = proof->fSecurity;

         PDB(kGlobal,3) {
            auth->GetHostAuth()->PrintEstablished();
            Info("TSlave", "Remote Client: fUser is .... %s", proof->fUser.Data());
         }

      } else {

         // we are a master server... authenticate either:
         // - stored user/passwd (coming from client)
         // - ~/.rootnetrc or ~/.netrc
         // - but NOT interactive (obviously)

         // check for .rootnetrc
         // if not in .rootnetrc and SRP -> fail
         // if not in .rootnetrc and normal -> set global user/passwd
         // Checks for (user,passwd) are done inside TAuthenticate now (4/2003)

         if (!auth->Authenticate()) {
            Error("TSlave", "authentication failed for %s@%s",
                            auth->GetUser(), host);
            delete auth;
            // This is to terminate properly remote proofd in case of failure
            fSocket->Send(Form("%d %s", gSystem->GetPid(), host), kROOTD_CLEANUP);
            SafeDelete(fSocket);
            return;
         }

         proof->fUser     = auth->GetRemoteLogin(auth->GetHostAuth(),
                                                 auth->GetSecurity(),auth->GetDetails());
         proof->fPasswd   = auth->GetPasswd();
         proof->fPwHash   = auth->GetPwHash();
         proof->fSRPPwd   = auth->GetSRPPwd();
         proof->fSecurity = auth->GetSecurity();
         fUser            = proof->fUser;
         fSecurity        = proof->fSecurity;

         PDB(kGlobal,3) {
            auth->GetHostAuth()->PrintEstablished();
            Info("TSlave", "Master Server: fUser is .... %s", fUser.Data());
         }
      }

      // fSecurity is the method successfully tried ...
{'fraction_non_alphanumeric': 0.15852449730954404, 'fraction_numerical': 0.008496176720475786, 'mean_word_length': 1.544128242074928, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 130, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1835456', 'n_tokens_mistral': 3802, 'n_tokens_neox': 3653, 'n_words': 951}