From e908fe277c620777820ea56e8fe329bd0925b485 Mon Sep 17 00:00:00 2001
From: Fons Rademakers <Fons.Rademakers@cern.ch>
Date: Mon, 5 Feb 2007 10:44:33 +0000
Subject: [PATCH] From Gerri: - fix issue giving a "permission denied" in the
 creation of a subdirectory to   store credentials for authentication - fix
 issue breaking the shutdown machinery in the case of multiple  
 connections/disconnections.

git-svn-id: http://root.cern.ch/svn/root/trunk@17690 27541ba8-7e3a-0410-8455-c3a389f83636
---
 proofd/inc/XrdProofServProxy.h   |  5 ++++-
 proofd/src/XrdProofServProxy.cxx |  4 +++-
 proofd/src/XrdProofdProtocol.cxx | 22 ++++++++++++++--------
 3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/proofd/inc/XrdProofServProxy.h b/proofd/inc/XrdProofServProxy.h
index 3419923e5f1..e2324b9d525 100644
--- a/proofd/inc/XrdProofServProxy.h
+++ b/proofd/inc/XrdProofServProxy.h
@@ -1,4 +1,4 @@
-// @(#)root/proofd:$Name:  $:$Id: XrdProofServProxy.h,v 1.7 2006/11/20 15:56:35 rdm Exp $
+// @(#)root/proofd:$Name:  $:$Id: XrdProofServProxy.h,v 1.8 2006/11/27 14:19:58 rdm Exp $
 // Author: G. Ganis  June 2005
 
 /*************************************************************************
@@ -133,6 +133,7 @@ public:
    inline void         SetSrvType(int id) { XrdOucMutexHelper mhp(fMutex); fSrvType = id; }
    inline void         SetStartMsg(XrdSrvBuffer *sm) { XrdOucMutexHelper mhp(fMutex); fStartMsg = sm; }
    inline void         SetStatus(int st) { XrdOucMutexHelper mhp(fMutex); fStatus = st; }
+   inline void         SetShutdown(bool sd = 1) { XrdOucMutexHelper mhp(fMutex); fIsShutdown = sd; }
    inline void         SetValid(bool valid = 1) { XrdOucMutexHelper mhp(fMutex); fIsValid = valid; }
    inline XrdSrvBuffer *StartMsg() const { XrdOucMutexHelper mhp(fMutex); return fStartMsg; }
    inline int          Status() const { XrdOucMutexHelper mhp(fMutex); return fStatus;}
@@ -176,6 +177,7 @@ public:
    void                SetUserEnvs(const char *t, int l = 0)
                           { XrdOucMutexHelper mhp(fMutex); XrdProofServProxy::SetCharValue(&fUserEnvs, t, l); }
 
+   bool                IsShutdown() const { XrdOucMutexHelper mhp(fMutex); return fIsShutdown; }
    bool                IsValid() const { XrdOucMutexHelper mhp(fMutex); return fIsValid; }
    const char         *StatusAsString() const;
 
@@ -204,6 +206,7 @@ public:
    char                     *fFileout;
 
    bool                      fIsValid;   // Validity flag
+   bool                      fIsShutdown; // Whether asked to shutdown
 
    char                     *fAlias;     // Session alias
    char                     *fClient;    // Client name
diff --git a/proofd/src/XrdProofServProxy.cxx b/proofd/src/XrdProofServProxy.cxx
index ee6d32b803f..c084118b457 100644
--- a/proofd/src/XrdProofServProxy.cxx
+++ b/proofd/src/XrdProofServProxy.cxx
@@ -1,4 +1,4 @@
-// @(#)root/proofd:$Name:  $:$Id: XrdProofServProxy.cxx,v 1.13 2006/11/27 14:19:58 rdm Exp $
+// @(#)root/proofd:$Name:  $:$Id: XrdProofServProxy.cxx,v 1.14 2006/12/12 14:32:06 rdm Exp $
 // Author: Gerardo Ganis  12/12/2005
 
 /*************************************************************************
@@ -48,6 +48,7 @@ XrdProofServProxy::XrdProofServProxy()
    fSrvID = -1;
    fSrvType = kXPD_AnyServer;
    fID = -1;
+   fIsShutdown = false;
    fIsValid = true;  // It is created for a valid server ...
    fProtVer = -1;
    fFileout = 0;
@@ -116,6 +117,7 @@ void XrdProofServProxy::Reset()
    fSrvID = -1;
    fSrvType = kXPD_AnyServer;
    fID = -1;
+   fIsShutdown = false;
    fIsValid = 0;
    fProtVer = -1;
    SafeDelArray(fClient);
diff --git a/proofd/src/XrdProofdProtocol.cxx b/proofd/src/XrdProofdProtocol.cxx
index 256c5b0693f..54f9338f70c 100644
--- a/proofd/src/XrdProofdProtocol.cxx
+++ b/proofd/src/XrdProofdProtocol.cxx
@@ -1,4 +1,4 @@
-// @(#)root/proofd:$Name:  $:$Id: XrdProofdProtocol.cxx,v 1.40 2007/01/23 13:11:14 rdm Exp $
+// @(#)root/proofd:$Name:  $:$Id: XrdProofdProtocol.cxx,v 1.41 2007/01/24 15:17:52 rdm Exp $
 // Author: Gerardo Ganis  12/12/2005
 
 /*************************************************************************
@@ -1998,8 +1998,6 @@ int XrdProofdProtocol::Process2()
 
    TRACEP(REQ, "Process2: enter: req id: " << fRequest.header.requestid);
 
-   XPDPRT("Process2: this: "<<this<<", auth: "<<fAuthProt);
-
    // If the user is not yet logged in, restrict what the user can do
    if (!fStatus || !(fStatus & XPD_LOGGEDIN))
       switch(fRequest.header.requestid) {
@@ -2152,8 +2150,6 @@ void XrdProofdProtocol::Recycle(XrdLink *, int, const char *)
                         XrdOucString msg("Recycle: could not send shutdown info to proofsrv");
                         TRACEP(XERR, msg.c_str());
                      }
-                     // Set in shutdown state
-                     psrv->SetStatus(kXPD_shutdown);
                   }
                }
             }
@@ -2792,6 +2788,15 @@ int XrdProofdProtocol::Login()
       // ... make sure that the directory for credentials exists in the sandbox ...
       XrdOucString credsdir = fUI.fWorkDir;
       credsdir += "/.creds";
+      // Acquire user identity
+      XrdSysPrivGuard pGuard((uid_t)fUI.fUid, (gid_t)fUI.fGid);
+      if (!pGuard.Valid()) {
+         XrdOucString emsg("Login: could not get privileges to create credential dir ");
+         emsg += credsdir;
+         TRACEP(XERR, emsg);
+         fResponse.Send(kXP_ServerError, emsg.c_str());
+         return rc;
+      }
       if (AssertDir(credsdir.c_str(), fUI) == -1) {
          XrdOucString emsg("Login: unable to create credential dir: ");
          emsg += credsdir;
@@ -2988,7 +2993,7 @@ int XrdProofdProtocol::MapClient(bool all)
             for (is = 0; is < (int) pmgr->ProofServs()->size(); is++) {
                if ((psrv = pmgr->ProofServs()->at(is)) &&
                     psrv->IsValid() && (psrv->SrvType() == kXPD_TopMaster) &&
-                    psrv->Status() == kXPD_shutdown) {
+                    psrv->IsShutdown()) {
                   if (SetShutdownTimer(psrv, 0) != 0) {
                      XrdOucString msg("MapClient: could not stop shutdown timer in proofsrv ");
                      msg += psrv->SrvID();
@@ -4417,8 +4422,7 @@ int XrdProofdProtocol::SendMsg()
       // Additional info about the message
       if (opt & kXPD_setidle) {
          TRACEP(DBG, "SendMsg: INT: setting proofserv in 'idle' state");
-         if (xps->Status() != kXPD_shutdown)
-            xps->SetStatus(kXPD_idle);
+         xps->SetStatus(kXPD_idle);
          // Clean start processing message, if any
          xps->DeleteStartMsg();
       } else if (opt & kXPD_querynum) {
@@ -5281,9 +5285,11 @@ int XrdProofdProtocol::SetShutdownTimer(XrdProofServProxy *xps, bool on)
             msg += "; action: when idle";
          else if (fgShutdownOpt == 2)
             msg += "; action: immediate";
+         xps->SetShutdown(1);
       } else {
          msg += "cancellation of shutdown action notified to process ";
          msg += xps->SrvID();
+         xps->SetShutdown(0);
       }
       TRACEP(DBG, msg.c_str());
    }
-- 
GitLab