00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__util__Privilege_H__
00010 #define __libutilxx__sella__util__Privilege_H__
00011
00012 #include "../../common.h"
00013 #include "PrivilegeException.h"
00014
00015 #include <unistd.h>
00016 #include <sys/types.h>
00017 #include <cap-ng.h>
00018
00019 #include <string>
00020 #include <memory>
00021
00022 namespace sella {
00023 namespace util {
00024 class Privilege;
00025 }
00026 }
00027
00028 class sella::util::Privilege {
00029 public:
00030 typedef std::shared_ptr<Privilege> shared;
00031 typedef Privilege *ptr;
00032
00033 public:
00034 Privilege() throw ();
00035 Privilege(const Privilege &other);
00036 virtual ~Privilege();
00037
00038 Privilege& operator=(const Privilege &other) throw ();
00039 bool operator==(const Privilege &other) const throw () = delete;
00040 bool operator!=(const Privilege &other) const throw () = delete;
00041
00042
00043 void applyIdentity(void) const throw (PrivilegeException);
00044 void applyEffectiveIdentity(void) const throw (PrivilegeException);
00045 void restoreIdentity(void) const throw (PrivilegeException);
00046 void restoreEffectiveIdentity(void) const throw (PrivilegeException);
00047
00048 void setUID(uid_t &uid, bool setGID = true) throw (PrivilegeException);
00049 void setUID(const std::string &user, bool setGID = true) throw (PrivilegeException);
00050 void setGID(gid_t &gid) throw ();
00051 void setGID(const std::string &group) throw (PrivilegeException);
00052
00053 const uid_t& getUID(void) const throw ();
00054 const uid_t getCurrentUID(void) const throw ();
00055 const uid_t getEffectiveUID(void) const throw ();
00056 const uid_t& getOriginalUID(void) const throw ();
00057 const gid_t& getGID(void) const throw ();
00058 const gid_t getCurrentGID(void) const throw ();
00059 const gid_t getEffectiveGID(void) const throw ();
00060 const gid_t& getOriginalGID(void) const throw ();
00061
00062 bool isRoot(void) const throw ();
00063 bool isRootGroup(void) const throw ();
00064 bool wasRoot(void) const throw ();
00065 bool wasRootGroup(void) const throw ();
00066
00067 void clear(void) throw ();
00068
00069 #ifdef HAVE_LINUX_CAPABILITY_H
00070
00071 void applyCapabilities(void) throw (PrivilegeException);
00072
00073 void setAll(void) throw ();
00074 bool setCapability(int capability) throw ();
00075 bool setChown(void) throw ();
00076 bool setKill(void) throw ();
00077 bool setNetBindService(void) throw ();
00078 bool setNetRaw(void) throw ();
00079 bool setSetUID(void) throw ();
00080 bool setSysChroot(void) throw ();
00081 bool setSysNice(void) throw ();
00082 bool setSysRawIO(void) throw ();
00083 bool setSysResource(void) throw ();
00084
00085 void clearAll(void) throw ();
00086 bool clearCapability(int capability) throw ();
00087 bool clearChown(void) throw ();
00088 bool clearKill(void) throw ();
00089 bool clearNetBindService(void) throw ();
00090 bool clearNetRaw(void) throw ();
00091 bool clearSetUID(void) throw ();
00092 bool clearSysChroot(void) throw ();
00093 bool clearSysNice(void) throw ();
00094 bool clearSysRawIO(void) throw ();
00095 bool clearSysResource(void) throw ();
00096
00097 bool hasNone(void) const throw ();
00098 bool hasCapability(int capability) const throw ();
00099 bool hasChown(void) const throw ();
00100 bool hasKill(void) const throw ();
00101 bool hasNetBindService(void) const throw ();
00102 bool hasNetRaw(void) const throw ();
00103 bool hasSetUID(void) const throw ();
00104 bool hasSysChroot(void) const throw ();
00105 bool hasSysNice(void) const throw ();
00106 bool hasSysRawIO(void) const throw ();
00107 bool hasSysResource(void) const throw ();
00108 #endif
00109
00110 static const Privilege::shared shared_ptr(void) throw ();
00111
00112 private:
00113 uid_t ouid, uid;
00114 gid_t ogid, gid;
00115 };
00116
00117 #endif
00118
00119
00120
00121