libutil++  1.9.3
 All Classes Functions Variables
Privilege.h
1 /*
2 ** libutil++
3 ** $Id: Privilege.h 1659 2016-03-15 22:20:58Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__util__Privilege_H__
10 #define __libutilxx__sella__util__Privilege_H__
11 
12 #include "../../common.h"
13 #include "PrivilegeException.h"
14 
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/capability.h>
18 
19 #include <string>
20 #include <memory>
21 
22 namespace sella {
23  namespace util {
24  class Privilege;
25  }
26 }
27 
29  public:
30  typedef std::shared_ptr<Privilege> shared;
31  typedef Privilege *ptr;
32 
33  public:
34  Privilege() throw ();
35  Privilege(const Privilege &other);
36  virtual ~Privilege();
37 
38  Privilege& operator=(const Privilege &other) throw ();
39  bool operator==(const Privilege &other) const throw () = delete;
40  bool operator!=(const Privilege &other) const throw () = delete;
41 
42  /* Portable User/Group API. */
43  void applyIdentity(void) const throw (PrivilegeException);
44  void applyEffectiveIdentity(void) const throw (PrivilegeException);
45  void restoreIdentity(void) const throw (PrivilegeException);
46  void restoreEffectiveIdentity(void) const throw (PrivilegeException);
47 
48  void setUID(uid_t &uid, bool setGID = true) throw (PrivilegeException);
49  void setUID(const std::string &user, bool setGID = true) throw (PrivilegeException);
50  void setGID(gid_t &gid) throw ();
51  void setGID(const std::string &group) throw (PrivilegeException);
52 
53  const uid_t& getUID(void) const throw ();
54  const uid_t getCurrentUID(void) const throw ();
55  const uid_t getEffectiveUID(void) const throw ();
56  const uid_t& getOriginalUID(void) const throw ();
57  const gid_t& getGID(void) const throw ();
58  const gid_t getCurrentGID(void) const throw ();
59  const gid_t getEffectiveGID(void) const throw ();
60  const gid_t& getOriginalGID(void) const throw ();
61 
62  bool isRoot(void) const throw (); /* Is Currently Root */
63  bool isRootGroup(void) const throw (); /* Is Currently Root Group */
64  bool wasRoot(void) const throw (); /* Was Originally Root */
65  bool wasRootGroup(void) const throw (); /* Was Originally Root Group */
66 
67  void clear(void) throw ();
68 
69  /* Linux Only Capabilities API - See man 7 capabilities */
70  void applyCapabilities(bool retain = true) const throw (PrivilegeException);
71  void clearRetain(void) const throw (PrivilegeException);
72 
73  bool setAll(void) throw ();
74  bool setCapability(cap_value_t cap) throw ();
75  bool setChown(void) throw ();
76  bool setSetGid(void) throw ();
77  bool setSetUid(void) throw ();
78  bool setProcessCapability(void) throw ();
79  bool setLinuxImmutable(void) throw ();
80  bool setNetBroadcast(void) throw ();
81  bool setNetAdmin(void) throw ();
82  bool setIPCOwner(void) throw ();
83  bool setSysModule(void) throw ();
84  bool setSysProcessTrace(void) throw ();
85  bool setSysProcessAccount(void) throw ();
86  bool setSysAdmin(void) throw ();
87  bool setSysBoot(void) throw ();
88  bool setSysTime(void) throw ();
89  bool setSysTTYConfig(void) throw ();
90  bool setMakeNode(void) throw ();
91  bool setLease(void) throw ();
92  bool setAuditWrite(void) throw ();
93  bool setAuditControl(void) throw ();
94  bool setSetFileCapability(void) throw ();
95  bool setMACOverride(void) throw ();
96  bool setMACAdmin(void) throw ();
97  bool setSyslog(void) throw ();
98  bool setWakeAlarm(void) throw ();
99  bool setBlockSuspend(void) throw ();
100  bool setDACOverride(void) throw ();
101  bool setDACReadSearch(void) throw ();
102  bool setFileOwner(void) throw ();
103  bool setFileSetId(void) throw ();
104  bool setIPCLock(void) throw ();
105  bool setKill(void) throw ();
106  bool setNetBindService(void) throw ();
107  bool setNetRaw(void) throw ();
108  bool setSetUID(void) throw ();
109  bool setSysChroot(void) throw ();
110  bool setSysNice(void) throw ();
111  bool setSysRawIO(void) throw ();
112  bool setSysResource(void) throw ();
113 
114  bool clearAll(void) throw ();
115  bool clearCapability(int capability) throw ();
116  bool clearChown(void) throw ();
117  bool clearIPCLock(void) throw ();
118  bool clearKill(void) throw ();
119  bool clearNetBindService(void) throw ();
120  bool clearNetRaw(void) throw ();
121  bool clearSetUID(void) throw ();
122  bool clearSysChroot(void) throw ();
123  bool clearSysNice(void) throw ();
124  bool clearSysRawIO(void) throw ();
125  bool clearSysResource(void) throw ();
126 
127  bool hasNone(void) const throw ();
128  bool hasCapability(int capability) const throw ();
129  bool hasChown(void) const throw ();
130  bool hasIPCLock(void) const throw ();
131  bool hasKill(void) const throw ();
132  bool hasNetBindService(void) const throw ();
133  bool hasNetRaw(void) const throw ();
134  bool hasSetUID(void) const throw ();
135  bool hasSysChroot(void) const throw ();
136  bool hasSysNice(void) const throw ();
137  bool hasSysRawIO(void) const throw ();
138  bool hasSysResource(void) const throw ();
139 
140  static const Privilege::shared shared_ptr(void) throw ();
141 
142  private:
143  uid_t ouid, uid;
144  gid_t ogid, gid;
145 
146  cap_t capabilities;
147 
148  bool setFlags(cap_value_t capability[], int ncap, cap_flag_value_t value) throw ();
149  bool getFlag(cap_value_t capability, cap_flag_value_t &value) const throw ();
150 };
151 
152 #endif
153 
154 /*
155 ** vim: noet ts=3 sw=3
156 */