llpermissions.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /**
  2. * @file llpermissions.h
  3. * @brief Permissions structures for objects.
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLPERMISSIONS_H
  33. #define LL_LLPERMISSIONS_H
  34. #include "llinventorytype.h"
  35. #include "llpermissionsflags.h"
  36. #include "llpreprocessor.h"
  37. #include "llsd.h"
  38. #include "lluuid.h"
  39. #include "llxmlnode.h"
  40. class LLMessageSystem;
  41. class LLPermissions;
  42. extern void mask_to_string(U32 mask, char* str, bool export_support = false);
  43. extern std::string mask_to_string(U32 mask, bool export_support = false);
  44. bool can_set_export(const U32& base, const U32& own, const U32& next);
  45. bool perms_allow_export(const LLPermissions& perms);
  46. enum ExportPolicy
  47. {
  48. ep_creator_only, // Used for SecondLife: only allow export when being
  49. // creator.
  50. ep_full_perm, // Used on OpenSIM grids not supporting the PERM_EXPORT
  51. // bit: allow exporting of full perm objects.
  52. ep_export_bit // Used on OpenSIM grids that support the PERM_EXPORT bit.
  53. };
  54. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55. // Class LLPermissions
  56. //
  57. // Class which encapsulates object and inventory permissions/ownership/etc.
  58. //
  59. // Permissions where originally a static state creator/owner and set
  60. // of cap bits. Since then, it has grown to include group information,
  61. // last owner, masks for different people. The implementation has been
  62. // chosen such that a uuid is stored for each current/past owner, and
  63. // a bitmask is stored for the base permissions, owner permissions,
  64. // group permissions, and everyone else permissions.
  65. //
  66. // The base permissions represent the most permissive state that the
  67. // permissions can possibly be in. Thus, if the base permissions do
  68. // not allow copying, no one can ever copy the object. The permissions
  69. // also maintain a tree-like hierarchy of permissions, thus, if we
  70. // (for sake of discussions) denote more permissive as '>', then this
  71. // is invariant:
  72. //
  73. // base mask >= owner mask >= group mask
  74. // >= everyone mask
  75. // >= next owner mask
  76. // NOTE: the group mask does not effect everyone or next, everyone
  77. // does not effect group or next, etc.
  78. //
  79. // It is considered a fair use right to move or delete any object you
  80. // own. Another fair use right is the ability to give away anything
  81. // which you cannot copy. One way to look at that is that if you have
  82. // a unique item, you can always give that one copy you have to
  83. // someone else.
  84. //
  85. // Most of the bitmask is easy to understand, PERM_COPY means you can
  86. // copy !PERM_TRANSFER means you cannot transfer, etc. Given that we
  87. // now track the concept of 'next owner' inside of the permissions
  88. // object, we can describe some new meta-meaning to the PERM_MODIFY
  89. // flag. PERM_MODIFY is usually meant to note if you can change an
  90. // item, but since we record next owner permissions, we can interpret
  91. // a no-modify object as 'you cannot modify this object and you cannot
  92. // make derivative works.' When evaluating functionality, and
  93. // comparisons against permissions, keep this concept in mind for
  94. // logical consistency.
  95. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. class LLPermissions
  97. {
  98. protected:
  99. LOG_CLASS(LLPermissions);
  100. public:
  101. static const LLPermissions DEFAULT;
  102. LLPermissions(); // defaults to created by system
  103. #if 0
  104. ~LLPermissions();
  105. #endif
  106. // base initialization code
  107. void init(const LLUUID& creator, const LLUUID& owner,
  108. const LLUUID& last_owner, const LLUUID& group);
  109. void initMasks(PermissionMask base, PermissionMask owner,
  110. PermissionMask everyone, PermissionMask group,
  111. PermissionMask next);
  112. // adjust permissions based on inventory type.
  113. void initMasks(LLInventoryType::EType type);
  114. //
  115. // ACCESSORS
  116. //
  117. // Returns the agent_id of the agent that created the item
  118. LL_INLINE const LLUUID& getCreator() const { return mCreator; }
  119. // Returns the agent_id of the owner. returns LLUUID::null if group
  120. // owned or public (a really big group).
  121. LL_INLINE const LLUUID& getOwner() const { return mOwner; }
  122. // Returns the group_id of the group associated with the
  123. // object.
  124. LL_INLINE const LLUUID& getGroup() const { return mGroup; }
  125. // Returns the agent_id of the last agent owner. Only returns LLUUID::null
  126. // if there has never been a previous owner (note: this is apparently not
  127. // true, say for textures in inventory, it may return LLUUID::null even if
  128. // there was a previous owner).
  129. LL_INLINE const LLUUID& getLastOwner() const { return mLastOwner; }
  130. LL_INLINE U32 getMaskBase() const { return mMaskBase; }
  131. LL_INLINE U32 getMaskOwner() const { return mMaskOwner; }
  132. LL_INLINE U32 getMaskGroup() const { return mMaskGroup; }
  133. LL_INLINE U32 getMaskEveryone() const { return mMaskEveryone; }
  134. LL_INLINE U32 getMaskNextOwner() const { return mMaskNextOwner; }
  135. LL_INLINE bool unrestricted() const
  136. {
  137. return (mMaskBase & PERM_ITEM_UNRESTRICTED) == PERM_ITEM_UNRESTRICTED;
  138. }
  139. // Returns true when owned by Alexandria Linden. HB
  140. bool fromLibrary() const;
  141. // Returns true if the object has any owner
  142. LL_INLINE bool isOwned() const { return mOwner.notNull() || mIsGroupOwned; }
  143. // Returns true if group_id is owner.
  144. LL_INLINE bool isGroupOwned() const { return mIsGroupOwned; }
  145. // Returns true if the object is owned at all, and false otherwise. If it
  146. // is owned at all, owner_id is filled with either the owner Id or the
  147. // group Id, and the is_group_owned parameter is appropriately filled. The
  148. // values of owner_id and is_group_owned are not changed if the object is
  149. // not owned.
  150. bool getOwnership(LLUUID& owner_id, bool& is_group_owned) const;
  151. // Gets the 'safe' owner. This should never return LLUUID::null.
  152. // If no group owned, return the agent owner id normally.
  153. // If group owned, return the group id.
  154. // If not owned, return a random uuid which should have no power.
  155. LLUUID getSafeOwner() const;
  156. // Returns a cheap CRC.
  157. // When 'skip_last_owner' is true, do not account for the last owner UUID
  158. // (which currently gets lost/reset by the server during inventory items
  159. // copy actions). HB
  160. U32 getCRC32(bool skip_last_owner = false) const;
  161. //
  162. // MANIPULATORS
  163. //
  164. // Fixes hierarchy of permissions, applies appropriate permissions at each
  165. // level to ensure that base permissions are respected, and also ensures
  166. // that if base cannot transfer, then group and other cannot copy.
  167. void fix();
  168. // All of these methods just do exactly what they say. There is no
  169. // permissions checking to see if the operation is allowed, and do not fix
  170. // the permissions hierarchy. So please only use these methods when you
  171. // know what you are doing and coding on behalf of the system, i.e. acting
  172. // as god.
  173. void set(const LLPermissions& permissions);
  174. LL_INLINE void setMaskBase(U32 mask) { mMaskBase = mask; }
  175. LL_INLINE void setMaskOwner(U32 mask) { mMaskOwner = mask; }
  176. LL_INLINE void setMaskEveryone(U32 mask) { mMaskEveryone = mask;}
  177. LL_INLINE void setMaskGroup(U32 mask) { mMaskGroup = mask;}
  178. LL_INLINE void setMaskNext(U32 mask) { mMaskNextOwner = mask; }
  179. // Allows accumulation of permissions. Results in the tightest permissions
  180. // possible. In the case of clashing UUIDs, it sets the ID to LLUUID::null.
  181. void accumulate(const LLPermissions& perm);
  182. //
  183. // CHECKED MANIPULATORS
  184. //
  185. // These methods return true on success. They return false if the given
  186. // agent is not allowed to make the change. You can pass LLUUID::null as
  187. // the agent id if the change is being made by the simulator itself, not on
  188. // behalf of any agent - this will always succeed. Passing in group id of
  189. // LLUUID:null means no group, and does not offer special permission to do
  190. // anything.
  191. // Saves last owner, sets current owner, and sets the group. When is_atomic
  192. // is true, it means that this permission represents an atomic permission
  193. // and not a collection of permissions. Currently, the only way to have a
  194. // collection is when an object has inventory and is then itself rolled up
  195. // into an inventory item.
  196. bool setOwnerAndGroup(const LLUUID& agent, const LLUUID& owner,
  197. const LLUUID& group, bool is_atomic);
  198. // Last owner does not have much in the way of permissions so it is not too
  199. // dangerous to do this.
  200. void setLastOwner(const LLUUID& last_owner);
  201. // Saves last owner, sets owner to uuid null, sets group owned. group_id
  202. // must be the group of the object (that's who it is being deeded to) and
  203. // the object must be group modify. Technically, the agent id and group id
  204. // are not necessary, but I wanted this function to look like the other
  205. // checked manipulators (since that is how it is used.) If the agent is the
  206. // system or (group == mGroup and group modify and owner transfer) then
  207. // this function will deed the permissions, set the next owner mask, and
  208. // Return true. Otherwise, no change is effected, and the function returns
  209. // false.
  210. bool deedToGroup(const LLUUID& agent, const LLUUID& group);
  211. // Attempts to set or clear the given bitmask. Returns true if you are
  212. // allowed to modify the permissions. If you attempt to turn on bits not
  213. // allowed by the base bits, the function will return true, but those bits
  214. // will not be set.
  215. bool setBaseBits(const LLUUID& agent, bool set, PermissionMask bits);
  216. bool setOwnerBits(const LLUUID& agent, bool set, PermissionMask bits);
  217. bool setGroupBits(const LLUUID& agent, const LLUUID& group, bool set,
  218. PermissionMask bits);
  219. bool setEveryoneBits(const LLUUID& agent, const LLUUID& group, bool set,
  220. PermissionMask bits);
  221. bool setNextOwnerBits(const LLUUID& agent, const LLUUID& group, bool set,
  222. PermissionMask bits);
  223. // This is currently only used in the Viewer to handle calling cards
  224. // where the creator is actually used to store the target. Use with care.
  225. LL_INLINE void setCreator(const LLUUID& creator) { mCreator = creator; }
  226. //
  227. // METHODS
  228. //
  229. // All the allow*() methods return true if the given agent or group can
  230. // perform the function. Prefer using this set of operations to check
  231. // permissions on an object. These return true if the given agent or group
  232. // can perform the function. They also return true if the object is not
  233. // owned, or the requesting agent is a system agent.
  234. // See llpermissionsflags.h for bits.
  235. bool allowOperationBy(PermissionBit op, const LLUUID& agent,
  236. const LLUUID& group = LLUUID::null) const;
  237. LL_INLINE bool allowModifyBy(const LLUUID& agent_id) const
  238. {
  239. return allowOperationBy(PERM_MODIFY, agent_id);
  240. }
  241. LL_INLINE bool allowCopyBy(const LLUUID& agent_id) const
  242. {
  243. return allowOperationBy(PERM_COPY, agent_id);
  244. }
  245. LL_INLINE bool allowTransferBy(const LLUUID& agent_id) const
  246. {
  247. return allowOperationBy(PERM_TRANSFER, agent_id);
  248. }
  249. LL_INLINE bool allowMoveBy(const LLUUID& agent_id) const
  250. {
  251. return allowOperationBy(PERM_MOVE, agent_id);
  252. }
  253. LL_INLINE bool allowModifyBy(const LLUUID& agent_id,
  254. const LLUUID& group_id) const
  255. {
  256. return allowOperationBy(PERM_MODIFY, agent_id, group_id);
  257. }
  258. LL_INLINE bool allowCopyBy(const LLUUID& agent_id,
  259. const LLUUID& group_id) const
  260. {
  261. return allowOperationBy(PERM_COPY, agent_id, group_id);
  262. }
  263. LL_INLINE bool allowMoveBy(const LLUUID& agent_id,
  264. const LLUUID& group_id) const
  265. {
  266. return allowOperationBy(PERM_MOVE, agent_id, group_id);
  267. }
  268. // Returns true if export is allowed.
  269. bool allowExportBy(const LLUUID& requester, ExportPolicy policy) const;
  270. // This somewhat specialized function is meant for testing if the current
  271. // owner is allowed to transfer to the specified agent id.
  272. LL_INLINE bool allowTransferTo(const LLUUID& agent_id) const
  273. {
  274. if (mIsGroupOwned)
  275. {
  276. return allowOperationBy(PERM_TRANSFER, mGroup, mGroup);
  277. }
  278. return mOwner == agent_id || allowOperationBy(PERM_TRANSFER, mOwner);
  279. }
  280. //
  281. // MISC METHODS and OPERATORS
  282. //
  283. LLSD packMessage() const;
  284. void unpackMessage(LLSD perms);
  285. // For messaging system support
  286. void packMessage(LLMessageSystem* msg) const;
  287. void unpackMessage(LLMessageSystem* msg, const char* block,
  288. S32 block_num = 0);
  289. bool importLegacyStream(std::istream& input_stream);
  290. bool exportLegacyStream(std::ostream& output_stream) const;
  291. bool operator==(const LLPermissions& rhs) const;
  292. bool operator!=(const LLPermissions& rhs) const;
  293. friend std::ostream& operator<<(std::ostream& s,
  294. const LLPermissions& perm);
  295. private:
  296. // Correct for fair use - you can never take away the right to move stuff
  297. // you own, and you can never take away the right to transfer something you
  298. // cannot otherwise copy.
  299. void fixFairUse();
  300. // Fix internal consistency for group/agent ownership
  301. void fixOwnership();
  302. private:
  303. LLUUID mCreator; // null if object created by system
  304. LLUUID mOwner; // null if object "unowned" (owned by system)
  305. LLUUID mLastOwner; // object's last owner
  306. LLUUID mGroup; // The group association
  307. // Initially permissive, progressively AND restricted by each owner
  308. PermissionMask mMaskBase;
  309. // Set by owner, applies to owner only, restricts lower permissions
  310. PermissionMask mMaskOwner;
  311. // Set by owner, applies to everyone else
  312. PermissionMask mMaskEveryone;
  313. // Set by owner, applies to group that is associated with permissions
  314. PermissionMask mMaskGroup;
  315. // Set by owner, applied to base on transfer.
  316. PermissionMask mMaskNextOwner;
  317. // Usually set in the fixOwnership() method based on current uuid values.
  318. bool mIsGroupOwned;
  319. };
  320. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  321. // Class LLAggregatePermissions
  322. //
  323. // Class which encapsulates object and inventory permissions, ownership, etc.
  324. // Currently, it only aggregates PERM_COPY, PERM_MODIFY, and PERM_TRANSFER.
  325. //
  326. // Usually you will construct an instance and hand the object several
  327. // permissions masks to aggregate the copy, modify, and transferability into a
  328. // nice trinary value.
  329. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  330. class LLAggregatePermissions
  331. {
  332. protected:
  333. LOG_CLASS(LLAggregatePermissions);
  334. public:
  335. enum EValue
  336. {
  337. AP_EMPTY = 0x00,
  338. AP_NONE = 0x01,
  339. AP_SOME = 0x02,
  340. AP_ALL = 0x03
  341. };
  342. // Constructs an empty aggregate permissions
  343. LLAggregatePermissions();
  344. // Pass in a PERM_COPY, PERM_TRANSFER, etc and get out a EValue enumeration
  345. // describing the current aggregate permissions.
  346. EValue getValue(PermissionBit bit) const;
  347. // Returns the permissions packed into the 6 LSB of a U8:
  348. // 00TTMMCC
  349. // where TT = transfer, MM = modify, and CC = copy
  350. // LSB is to the right
  351. U8 getU8() const;
  352. // Returns true if the aggregate permissions are empty, otherwise false.
  353. bool isEmpty() const;
  354. // Given a mask, aggregates the useful permissions.
  355. void aggregate(PermissionMask mask);
  356. // Aggregates aggregates
  357. void aggregate(const LLAggregatePermissions& ag);
  358. // Message handling
  359. void packMessage(LLMessageSystem* msg, const char* field) const;
  360. void unpackMessage(LLMessageSystem* msg, const char* block,
  361. const char* field, S32 block_num = 0);
  362. static const LLAggregatePermissions empty;
  363. friend std::ostream& operator<<(std::ostream& s,
  364. const LLAggregatePermissions& perm);
  365. protected:
  366. enum EPermIndex
  367. {
  368. PI_COPY = 0,
  369. PI_MODIFY = 1,
  370. PI_TRANSFER = 2,
  371. PI_END = 3,
  372. PI_COUNT = 3
  373. };
  374. void aggregateBit(EPermIndex idx, bool allowed);
  375. void aggregateIndex(EPermIndex idx, U8 bits);
  376. static EPermIndex perm2PermIndex(PermissionBit bit);
  377. protected:
  378. // Structure used to store the aggregate so far.
  379. U8 mBits[PI_COUNT];
  380. };
  381. // These functions convert between structured data and permissions as
  382. // appropriate for serialization. The permissions are a map of things like
  383. // 'creator_id', 'owner_id', etc, with the value copied from the permission
  384. // object.
  385. LLSD ll_create_sd_from_permissions(const LLPermissions& perm);
  386. LLPermissions ll_permissions_from_sd(const LLSD& sd_perm);
  387. #endif