xrootd
XrdSysPriv.hh
Go to the documentation of this file.
00001 #ifndef __SYS_PRIV_H__
00002 #define __SYS_PRIV_H__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                     X r d S y s P r i v . h h                              */
00006 /*                                                                            */
00007 /* (c) 2006 G. Ganis (CERN)                                                   */
00008 /*                                                                            */
00009 /* This file is part of the XRootD software suite.                            */
00010 /*                                                                            */
00011 /* XRootD is free software: you can redistribute it and/or modify it under    */
00012 /* the terms of the GNU Lesser General Public License as published by the     */
00013 /* Free Software Foundation, either version 3 of the License, or (at your     */
00014 /* option) any later version.                                                 */
00015 /*                                                                            */
00016 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00017 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00018 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00019 /* License for more details.                                                  */
00020 /*                                                                            */
00021 /* You should have received a copy of the GNU Lesser General Public License   */
00022 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00023 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00024 /*                                                                            */
00025 /* The copyright holder's institutional names and contributor's names may not */
00026 /* be used to endorse or promote products derived from this software without  */
00027 /* specific prior written permission of the institution or contributor.       */
00028 /*     All Rights Reserved. See XrdInfo.cc for complete License Terms         */
00029 /******************************************************************************/
00030 
00032 //                                                                      //
00033 // XrdSysPriv                                                           //
00034 //                                                                      //
00035 // Author: G. Ganis, CERN, 2006                                         //
00036 //                                                                      //
00037 // Implementation of a privileges handling API following the paper      //
00038 //   "Setuid Demystified" by H.Chen, D.Wagner, D.Dean                   //
00039 // also quoted in "Secure programming Cookbook" by J.Viega & M.Messier. //
00040 //                                                                      //
00041 // NB: this class can only used via XrdSysPrivGuard (see below)         //
00042 //                                                                      //
00044 
00045 #if !defined(WINDOWS)
00046 #  include <sys/types.h>
00047 #else
00048 #  define uid_t unsigned int
00049 #  define gid_t unsigned int
00050 #endif
00051 
00052 #include "XrdSys/XrdSysPthread.hh"
00053 
00054 class XrdSysPriv
00055 {
00056  friend class XrdSysPrivGuard;
00057  private:
00058    // Ownership cannot be changed by thread, so there must be an overall
00059    // locking
00060    static XrdSysRecMutex fgMutex;
00061 
00062    XrdSysPriv();
00063 
00064    static bool fDebug;
00065 
00066    static int ChangeTo(uid_t uid, gid_t gid);
00067    static void DumpUGID(const char *msg = 0);
00068    static int Restore(bool saved = 1);
00069 
00070  public:
00071    virtual ~XrdSysPriv() { }
00072    static int ChangePerm(uid_t uid, gid_t gid);
00073 };
00074 
00075 //
00076 // Guard class;
00077 // Usage:
00078 //
00079 //    {  XrdSysPrivGuard priv(tempuid);
00080 //
00081 //       // Work as tempuid (maybe superuser)
00082 //       ...
00083 //
00084 //    }
00085 //
00086 class XrdSysPrivGuard
00087 {
00088  public:
00089    XrdSysPrivGuard(uid_t uid, gid_t gid);
00090    XrdSysPrivGuard(const char *user);
00091    virtual ~XrdSysPrivGuard();
00092    bool Valid() const { return valid; }
00093  private:
00094    bool dum;
00095    bool valid;
00096    void Init(uid_t uid, gid_t gid);
00097 };
00098 
00099 #endif