llpermissions.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /**
  2. * @file llpermissions.cpp
  3. * @author Phoenix
  4. * @brief Permissions for objects and inventory.
  5. *
  6. * $LicenseInfo:firstyear=2002&license=viewergpl$
  7. *
  8. * Copyright (c) 2002-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "linden_common.h"
  34. #include "llpermissions.h"
  35. #include "llsd.h"
  36. #include "llmessage.h"
  37. ///----------------------------------------------------------------------------
  38. /// Class LLPermissions
  39. ///----------------------------------------------------------------------------
  40. const LLPermissions LLPermissions::DEFAULT;
  41. // No creator = created by system
  42. LLPermissions::LLPermissions()
  43. {
  44. init(LLUUID::null, LLUUID::null, LLUUID::null, LLUUID::null);
  45. }
  46. // Default to created by system
  47. void LLPermissions::init(const LLUUID& creator, const LLUUID& owner,
  48. const LLUUID& last_owner, const LLUUID& group)
  49. {
  50. mCreator = creator;
  51. mOwner = owner;
  52. mLastOwner = last_owner;
  53. mGroup = group;
  54. mMaskBase = PERM_ALL;
  55. mMaskOwner = PERM_ALL;
  56. mMaskEveryone = PERM_ALL;
  57. mMaskGroup = PERM_ALL;
  58. mMaskNextOwner = PERM_ALL;
  59. fixOwnership();
  60. }
  61. void LLPermissions::initMasks(PermissionMask base, PermissionMask owner,
  62. PermissionMask everyone, PermissionMask group,
  63. PermissionMask next)
  64. {
  65. mMaskBase = base;
  66. mMaskOwner = owner;
  67. mMaskEveryone = everyone;
  68. mMaskGroup = group;
  69. mMaskNextOwner = next;
  70. fixFairUse();
  71. fix();
  72. }
  73. // ! BACKWARDS COMPATIBILITY ! Override masks for inventory types that
  74. // no longer can have restricted permissions. This takes care of previous
  75. // version landmarks that could have had no copy/mod/transfer bits set.
  76. void LLPermissions::initMasks(LLInventoryType::EType type)
  77. {
  78. if (LLInventoryType::cannotRestrictPermissions(type))
  79. {
  80. initMasks(PERM_ALL, PERM_ALL, PERM_ALL, PERM_ALL, PERM_ALL);
  81. }
  82. }
  83. bool LLPermissions::getOwnership(LLUUID& owner_id, bool& is_group_owned) const
  84. {
  85. if (mOwner.notNull())
  86. {
  87. owner_id = mOwner;
  88. is_group_owned = false;
  89. return true;
  90. }
  91. else if (mIsGroupOwned)
  92. {
  93. owner_id = mGroup;
  94. is_group_owned = true;
  95. return true;
  96. }
  97. return false;
  98. }
  99. LLUUID LLPermissions::getSafeOwner() const
  100. {
  101. if (mOwner.notNull())
  102. {
  103. return mOwner;
  104. }
  105. else if (mIsGroupOwned)
  106. {
  107. return mGroup;
  108. }
  109. else
  110. {
  111. llwarns << "No valid owner !" << llendl;
  112. LLUUID unused_uuid;
  113. unused_uuid.generate();
  114. return unused_uuid;
  115. }
  116. }
  117. U32 LLPermissions::getCRC32(bool skip_last_owner) const
  118. {
  119. U32 rv = mCreator.getCRC32();
  120. rv += mOwner.getCRC32();
  121. if (!skip_last_owner)
  122. {
  123. rv += mLastOwner.getCRC32();
  124. }
  125. rv += mGroup.getCRC32();
  126. rv += mMaskBase + mMaskOwner + mMaskEveryone + mMaskGroup;
  127. return rv;
  128. }
  129. void LLPermissions::set(const LLPermissions& from)
  130. {
  131. mCreator = from.mCreator;
  132. mOwner = from.mOwner;
  133. mLastOwner = from.mLastOwner;
  134. mGroup = from.mGroup;
  135. mMaskBase = from.mMaskBase;
  136. mMaskOwner = from.mMaskOwner;
  137. mMaskEveryone = from.mMaskEveryone;
  138. mMaskGroup = from.mMaskGroup;
  139. mMaskNextOwner = from.mMaskNextOwner;
  140. mIsGroupOwned = from.mIsGroupOwned;
  141. }
  142. // Fix hierarchy of permissions.
  143. void LLPermissions::fix()
  144. {
  145. mMaskOwner &= mMaskBase;
  146. mMaskGroup &= mMaskOwner;
  147. // next owner uses base, since you may want to sell locked objects.
  148. mMaskNextOwner &= mMaskBase;
  149. mMaskEveryone &= mMaskOwner;
  150. mMaskEveryone &= ~PERM_MODIFY;
  151. if (!(mMaskBase & PERM_TRANSFER) && !mIsGroupOwned)
  152. {
  153. mMaskGroup &= ~PERM_COPY;
  154. mMaskEveryone &= ~PERM_COPY;
  155. // Do not set mask next owner to too restrictive because if we
  156. // rez an object, it may require an ownership transfer during
  157. // rez, which will note the overly restrictive perms, and then
  158. // fix them to allow fair use, which may be different than the
  159. // original intention.
  160. }
  161. }
  162. // Correct for fair use - you can never take away the right to move stuff you
  163. // own, and you can never take away the right to transfer something you cannot
  164. // otherwise copy.
  165. void LLPermissions::fixFairUse()
  166. {
  167. mMaskBase |= PERM_MOVE;
  168. if (!(mMaskBase & PERM_COPY))
  169. {
  170. mMaskBase |= PERM_TRANSFER;
  171. }
  172. // (mask next owner == PERM_NONE) iff mask base is no transfer
  173. if (mMaskNextOwner != PERM_NONE)
  174. {
  175. mMaskNextOwner |= PERM_MOVE;
  176. }
  177. }
  178. void LLPermissions::fixOwnership()
  179. {
  180. if (mOwner.isNull() && mGroup.notNull())
  181. {
  182. mIsGroupOwned = true;
  183. }
  184. else
  185. {
  186. mIsGroupOwned = false;
  187. }
  188. }
  189. // Allow accumulation of permissions. Results in the tightest
  190. // permissions possible. In the case of clashing UUIDs, it sets the ID
  191. // to LLUUID::null.
  192. void LLPermissions::accumulate(const LLPermissions& perm)
  193. {
  194. if (perm.mCreator != mCreator)
  195. {
  196. mCreator.setNull();
  197. }
  198. if (perm.mOwner != mOwner)
  199. {
  200. mOwner.setNull();
  201. }
  202. if (perm.mLastOwner != mLastOwner)
  203. {
  204. mLastOwner.setNull();
  205. }
  206. if (perm.mGroup != mGroup)
  207. {
  208. mGroup.setNull();
  209. }
  210. mMaskBase &= perm.mMaskBase;
  211. mMaskOwner &= perm.mMaskOwner;
  212. mMaskGroup &= perm.mMaskGroup;
  213. mMaskEveryone &= perm.mMaskEveryone;
  214. mMaskNextOwner &= perm.mMaskNextOwner;
  215. fix();
  216. }
  217. // saves last owner, sets current owner, and sets the group. Note that this
  218. // function has to more cleverly apply the fair use permissions.
  219. bool LLPermissions::setOwnerAndGroup(const LLUUID& agent, const LLUUID& owner,
  220. const LLUUID& group, bool is_atomic)
  221. {
  222. bool allowed = false;
  223. if (agent.isNull() || mOwner.isNull() ||
  224. (agent == mOwner && (owner == mOwner || (mMaskOwner & PERM_TRANSFER))))
  225. {
  226. // ...system can alway set owner
  227. // ...public objects can be claimed by anyone
  228. // ...otherwise, agent must own it and have transfer ability
  229. allowed = true;
  230. }
  231. if (allowed)
  232. {
  233. if (mLastOwner.isNull() || (!mOwner.isNull() && owner != mLastOwner))
  234. {
  235. mLastOwner = mOwner;
  236. }
  237. if (mOwner != owner ||
  238. (mOwner.isNull() && owner.isNull() && mGroup != group))
  239. {
  240. mMaskBase = mMaskNextOwner;
  241. mOwner = owner;
  242. // this is a selective use of fair use for atomic permissions.
  243. if (is_atomic && !(mMaskBase & PERM_COPY))
  244. {
  245. mMaskBase |= PERM_TRANSFER;
  246. }
  247. }
  248. mGroup = group;
  249. fixOwnership();
  250. #if 0 // If it's not atomic and we fix fair use, it blows away objects as
  251. // inventory items which have different permissions than it's
  252. // contents. :(
  253. fixFairUse();
  254. #endif
  255. mMaskBase |= PERM_MOVE;
  256. if (mMaskNextOwner != PERM_NONE)
  257. {
  258. mMaskNextOwner |= PERM_MOVE;
  259. }
  260. fix();
  261. }
  262. return allowed;
  263. }
  264. // Fix for DEV-33917, last owner isn't used much and has little impact on
  265. // permissions so it's reasonably safe to do this, however, for now, limiting
  266. // the functionality of this routine to objects which are group owned.
  267. void LLPermissions::setLastOwner(const LLUUID& last_owner)
  268. {
  269. if (mIsGroupOwned)
  270. {
  271. mLastOwner = last_owner;
  272. }
  273. }
  274. bool LLPermissions::deedToGroup(const LLUUID& agent, const LLUUID& group)
  275. {
  276. if (group.notNull() &&
  277. (agent.isNull() ||
  278. (group == mGroup && (mMaskOwner & PERM_TRANSFER) &&
  279. (mMaskGroup & PERM_MOVE))))
  280. {
  281. if (mOwner.notNull())
  282. {
  283. mLastOwner = mOwner;
  284. mOwner.setNull();
  285. }
  286. mMaskBase = mMaskNextOwner;
  287. mMaskGroup = PERM_NONE;
  288. mGroup = group;
  289. mIsGroupOwned = true;
  290. fixFairUse();
  291. fix();
  292. return true;
  293. }
  294. return false;
  295. }
  296. bool LLPermissions::setBaseBits(const LLUUID& agent, bool set,
  297. PermissionMask bits)
  298. {
  299. bool ownership = false;
  300. if (agent.isNull())
  301. {
  302. // only the system is always allowed to change base bits
  303. ownership = true;
  304. }
  305. if (ownership)
  306. {
  307. if (set)
  308. {
  309. mMaskBase |= bits; // turn on bits
  310. }
  311. else
  312. {
  313. mMaskBase &= ~bits; // turn off bits
  314. }
  315. fix();
  316. }
  317. return ownership;
  318. }
  319. // Note: If you attempt to set bits that the base bits doesn't allow, the
  320. // function will succeed, but those bits will not be set.
  321. bool LLPermissions::setOwnerBits(const LLUUID& agent, bool set,
  322. PermissionMask bits)
  323. {
  324. bool ownership = false;
  325. if (agent.isNull() || agent == mOwner)
  326. {
  327. // ...system always allowed to change things
  328. // ...owner bits can only be set by owner
  329. ownership = true;
  330. }
  331. // If we have correct ownership and
  332. if (ownership)
  333. {
  334. if (set)
  335. {
  336. mMaskOwner |= bits; // turn on bits
  337. }
  338. else
  339. {
  340. mMaskOwner &= ~bits; // turn off bits
  341. }
  342. fix();
  343. }
  344. return ownership;
  345. }
  346. bool LLPermissions::setGroupBits(const LLUUID& agent, const LLUUID& group,
  347. bool set, PermissionMask bits)
  348. {
  349. bool ownership = false;
  350. if (agent.isNull() || agent == mOwner ||
  351. (group == mGroup && !mGroup.isNull()))
  352. {
  353. // The group bits can be set by the system, the owner, or a group
  354. // member.
  355. ownership = true;
  356. }
  357. if (ownership)
  358. {
  359. if (set)
  360. {
  361. mMaskGroup |= bits;
  362. }
  363. else
  364. {
  365. mMaskGroup &= ~bits;
  366. }
  367. fix();
  368. }
  369. return ownership;
  370. }
  371. // Note: If you attempt to set bits that the creator or owner doesn't allow,
  372. // the function will succeed, but those bits will not be set.
  373. bool LLPermissions::setEveryoneBits(const LLUUID& agent, const LLUUID& group,
  374. bool set, PermissionMask bits)
  375. {
  376. bool ownership = false;
  377. if (agent.isNull() || agent == mOwner ||
  378. (group == mGroup && !mGroup.isNull()))
  379. {
  380. // The everyone bits can be set by the system, the owner, or a group
  381. // member.
  382. ownership = true;
  383. }
  384. if (ownership)
  385. {
  386. if (set)
  387. {
  388. mMaskEveryone |= bits;
  389. }
  390. else
  391. {
  392. mMaskEveryone &= ~bits;
  393. }
  394. // Fix hierarchy of permissions
  395. fix();
  396. }
  397. return ownership;
  398. }
  399. // Note: If you attempt to set bits that the creator or owner doesn't allow,
  400. // the function will succeed, but those bits will not be set.
  401. bool LLPermissions::setNextOwnerBits(const LLUUID& agent, const LLUUID& group,
  402. bool set, PermissionMask bits)
  403. {
  404. bool ownership = false;
  405. if (agent.isNull() || agent == mOwner ||
  406. (group == mGroup && !mGroup.isNull()))
  407. {
  408. // The next owner bits can be set by the system, the owner, or a group
  409. // member.
  410. ownership = true;
  411. }
  412. if (ownership)
  413. {
  414. if (set)
  415. {
  416. mMaskNextOwner |= bits;
  417. }
  418. else
  419. {
  420. mMaskNextOwner &= ~bits;
  421. }
  422. // Fix-up permissions
  423. if (!(mMaskNextOwner & PERM_COPY))
  424. {
  425. mMaskNextOwner |= PERM_TRANSFER;
  426. }
  427. fix();
  428. }
  429. return ownership;
  430. }
  431. bool LLPermissions::allowOperationBy(PermissionBit op,
  432. const LLUUID& requester,
  433. const LLUUID& group) const
  434. {
  435. if (requester.isNull())
  436. {
  437. // ...system making request
  438. // ...not owned
  439. return true;
  440. }
  441. else if (mIsGroupOwned && (mGroup == requester))
  442. {
  443. // group checking ownership permissions
  444. return (mMaskOwner & op) != 0;
  445. }
  446. else if (!mIsGroupOwned && (mOwner == requester))
  447. {
  448. // ...owner making request
  449. return (mMaskOwner & op) != 0;
  450. }
  451. else if (mGroup.notNull() && (mGroup == group))
  452. {
  453. // group member making request
  454. return (mMaskGroup & op) != 0 || (mMaskEveryone & op) != 0;
  455. }
  456. return (mMaskEveryone & op) != 0;
  457. }
  458. bool LLPermissions::fromLibrary() const
  459. {
  460. return mOwner == ALEXANDRIA_LINDEN_ID;
  461. }
  462. //
  463. // LLSD support for HTTP messages.
  464. //
  465. LLSD LLPermissions::packMessage() const
  466. {
  467. LLSD result;
  468. result["creator-id"] = mCreator;
  469. result["owner-id"] = mOwner;
  470. result["group-id"] = mGroup;
  471. result["base-mask"] = (S32)mMaskBase;
  472. result["owner-mask"] = (S32)mMaskOwner;
  473. result["group-mask"] = (S32)mMaskGroup;
  474. result["everyone-mask"] = (S32)mMaskEveryone;
  475. result["next-owner-mask"] = (S32)mMaskNextOwner;
  476. result["group-owned"] = (LLSD::Boolean)mIsGroupOwned;
  477. return result;
  478. }
  479. //
  480. // Messaging support
  481. //
  482. void LLPermissions::packMessage(LLMessageSystem* msg) const
  483. {
  484. msg->addUUIDFast(_PREHASH_CreatorID, mCreator);
  485. msg->addUUIDFast(_PREHASH_OwnerID, mOwner);
  486. msg->addUUIDFast(_PREHASH_GroupID, mGroup);
  487. msg->addU32Fast(_PREHASH_BaseMask, mMaskBase);
  488. msg->addU32Fast(_PREHASH_OwnerMask, mMaskOwner);
  489. msg->addU32Fast(_PREHASH_GroupMask, mMaskGroup);
  490. msg->addU32Fast(_PREHASH_EveryoneMask, mMaskEveryone);
  491. msg->addU32Fast(_PREHASH_NextOwnerMask, mMaskNextOwner);
  492. msg->addBoolFast(_PREHASH_GroupOwned, mIsGroupOwned);
  493. }
  494. void LLPermissions::unpackMessage(LLSD perms)
  495. {
  496. mCreator = perms["creator-id"];
  497. mOwner = perms["owner-id"];
  498. mGroup = perms["group-id"];
  499. mMaskBase = (U32)perms["base-mask"].asInteger();
  500. mMaskOwner = (U32)perms["owner-mask"].asInteger();
  501. mMaskGroup = (U32)perms["group-mask"].asInteger();
  502. mMaskEveryone = (U32)perms["everyone-mask"].asInteger();
  503. mMaskNextOwner = (U32)perms["next-owner-mask"].asInteger();
  504. mIsGroupOwned = perms["group-owned"].asBoolean();
  505. }
  506. void LLPermissions::unpackMessage(LLMessageSystem* msg, const char* block,
  507. S32 block_num)
  508. {
  509. msg->getUUIDFast(block, _PREHASH_CreatorID, mCreator, block_num);
  510. msg->getUUIDFast(block, _PREHASH_OwnerID, mOwner, block_num);
  511. msg->getUUIDFast(block, _PREHASH_GroupID, mGroup, block_num);
  512. msg->getU32Fast(block, _PREHASH_BaseMask, mMaskBase, block_num);
  513. msg->getU32Fast(block, _PREHASH_OwnerMask, mMaskOwner, block_num);
  514. msg->getU32Fast(block, _PREHASH_GroupMask, mMaskGroup, block_num);
  515. msg->getU32Fast(block, _PREHASH_EveryoneMask, mMaskEveryone, block_num);
  516. msg->getU32Fast(block, _PREHASH_NextOwnerMask, mMaskNextOwner, block_num);
  517. msg->getBoolFast(block, _PREHASH_GroupOwned, mIsGroupOwned, block_num);
  518. }
  519. bool LLPermissions::importLegacyStream(std::istream& input_stream)
  520. {
  521. init(LLUUID::null, LLUUID::null, LLUUID::null, LLUUID::null);
  522. const S32 BUFSIZE = 16384;
  523. // *NOTE: Changing the buffer size will require changing the scanf
  524. // calls below.
  525. char buffer[BUFSIZE];
  526. char keyword[256];
  527. char valuestr[256];
  528. char uuid_str[256];
  529. U32 mask;
  530. keyword[0] = '\0';
  531. valuestr[0] = '\0';
  532. while (input_stream.good())
  533. {
  534. input_stream.getline(buffer, BUFSIZE);
  535. sscanf(buffer, " %255s %255s", keyword, valuestr);
  536. if (!strcmp("{", keyword))
  537. {
  538. continue;
  539. }
  540. if (!strcmp("}", keyword))
  541. {
  542. break;
  543. }
  544. else if (!strcmp("creator_mask", keyword))
  545. {
  546. // legacy support for "creator" masks
  547. sscanf(valuestr, "%x", &mask);
  548. mMaskBase = mask;
  549. fixFairUse();
  550. }
  551. else if (!strcmp("base_mask", keyword))
  552. {
  553. sscanf(valuestr, "%x", &mask);
  554. mMaskBase = mask;
  555. //fixFairUse();
  556. }
  557. else if (!strcmp("owner_mask", keyword))
  558. {
  559. sscanf(valuestr, "%x", &mask);
  560. mMaskOwner = mask;
  561. }
  562. else if (!strcmp("group_mask", keyword))
  563. {
  564. sscanf(valuestr, "%x", &mask);
  565. mMaskGroup = mask;
  566. }
  567. else if (!strcmp("everyone_mask", keyword))
  568. {
  569. sscanf(valuestr, "%x", &mask);
  570. mMaskEveryone = mask;
  571. }
  572. else if (!strcmp("next_owner_mask", keyword))
  573. {
  574. sscanf(valuestr, "%x", &mask);
  575. mMaskNextOwner = mask;
  576. }
  577. else if (!strcmp("creator_id", keyword))
  578. {
  579. sscanf(valuestr, "%255s", uuid_str);
  580. mCreator.set(uuid_str);
  581. }
  582. else if (!strcmp("owner_id", keyword))
  583. {
  584. sscanf(valuestr, "%255s", uuid_str);
  585. mOwner.set(uuid_str);
  586. }
  587. else if (!strcmp("last_owner_id", keyword))
  588. {
  589. sscanf(valuestr, "%255s", uuid_str);
  590. mLastOwner.set(uuid_str);
  591. }
  592. else if (!strcmp("group_id", keyword))
  593. {
  594. sscanf(valuestr, "%255s", uuid_str);
  595. mGroup.set(uuid_str);
  596. }
  597. else if (!strcmp("group_owned", keyword))
  598. {
  599. sscanf(valuestr, "%d", &mask);
  600. if (mask) mIsGroupOwned = true;
  601. else mIsGroupOwned = false;
  602. }
  603. else
  604. {
  605. llinfos << "unknown keyword " << keyword << " in permissions import" << llendl;
  606. }
  607. }
  608. fix();
  609. return true;
  610. }
  611. bool LLPermissions::exportLegacyStream(std::ostream& output_stream) const
  612. {
  613. std::string uuid_str;
  614. output_stream << "\tpermissions 0\n";
  615. output_stream << "\t{\n";
  616. std::string buffer;
  617. buffer = llformat("\t\tbase_mask\t%08x\n", mMaskBase);
  618. output_stream << buffer;
  619. buffer = llformat("\t\towner_mask\t%08x\n", mMaskOwner);
  620. output_stream << buffer;
  621. buffer = llformat("\t\tgroup_mask\t%08x\n", mMaskGroup);
  622. output_stream << buffer;
  623. buffer = llformat("\t\teveryone_mask\t%08x\n", mMaskEveryone);
  624. output_stream << buffer;
  625. buffer = llformat("\t\tnext_owner_mask\t%08x\n", mMaskNextOwner);
  626. output_stream << buffer;
  627. mCreator.toString(uuid_str);
  628. output_stream << "\t\tcreator_id\t" << uuid_str << "\n";
  629. mOwner.toString(uuid_str);
  630. output_stream << "\t\towner_id\t" << uuid_str << "\n";
  631. mLastOwner.toString(uuid_str);
  632. output_stream << "\t\tlast_owner_id\t" << uuid_str << "\n";
  633. mGroup.toString(uuid_str);
  634. output_stream << "\t\tgroup_id\t" << uuid_str << "\n";
  635. if (mIsGroupOwned)
  636. {
  637. output_stream << "\t\tgroup_owned\t1\n";
  638. }
  639. output_stream << "\t}\n";
  640. return true;
  641. }
  642. // Deleted LLPermissions::exportFileXML() and LLPermissions::importXML()
  643. // because I can't find any non-test code references to it. 2009-05-04 JC
  644. bool LLPermissions::operator==(const LLPermissions& rhs) const
  645. {
  646. return mCreator == rhs.mCreator && mOwner == rhs.mOwner &&
  647. mLastOwner == rhs.mLastOwner && mGroup == rhs.mGroup &&
  648. mMaskBase == rhs.mMaskBase && mMaskOwner == rhs.mMaskOwner &&
  649. mMaskGroup == rhs.mMaskGroup &&
  650. mMaskEveryone == rhs.mMaskEveryone &&
  651. mMaskNextOwner == rhs.mMaskNextOwner &&
  652. mIsGroupOwned == rhs.mIsGroupOwned;
  653. }
  654. bool LLPermissions::operator!=(const LLPermissions& rhs) const
  655. {
  656. return mCreator != rhs.mCreator || mOwner != rhs.mOwner ||
  657. mLastOwner != rhs.mLastOwner || mGroup != rhs.mGroup ||
  658. mMaskBase != rhs.mMaskBase || mMaskOwner != rhs.mMaskOwner ||
  659. mMaskGroup != rhs.mMaskGroup ||
  660. mMaskEveryone != rhs.mMaskEveryone ||
  661. mMaskNextOwner != rhs.mMaskNextOwner ||
  662. mIsGroupOwned != rhs.mIsGroupOwned;
  663. }
  664. std::ostream& operator<<(std::ostream& s, const LLPermissions& perm)
  665. {
  666. s << "{Creator=" << perm.getCreator()
  667. << ", Owner=" << perm.getOwner()
  668. << ", Group=" << perm.getGroup()
  669. << std::hex
  670. << ", BaseMask=0x" << perm.getMaskBase()
  671. << ", OwnerMask=0x" << perm.getMaskOwner()
  672. << ", EveryoneMask=0x" << perm.getMaskEveryone()
  673. << ", GroupMask=0x" << perm.getMaskGroup()
  674. << ", NextOwnerMask=0x" << perm.getMaskNextOwner()
  675. << std::dec << "}";
  676. return s;
  677. }
  678. ///----------------------------------------------------------------------------
  679. /// Class LLAggregatePermissions
  680. ///----------------------------------------------------------------------------
  681. const LLAggregatePermissions LLAggregatePermissions::empty;
  682. LLAggregatePermissions::LLAggregatePermissions()
  683. {
  684. for (S32 i = 0; i < PI_COUNT; ++i)
  685. {
  686. mBits[i] = AP_EMPTY;
  687. }
  688. }
  689. LLAggregatePermissions::EValue LLAggregatePermissions::getValue(PermissionBit bit) const
  690. {
  691. EPermIndex idx = perm2PermIndex(bit);
  692. EValue rv = AP_EMPTY;
  693. if (idx != PI_END)
  694. {
  695. rv = (LLAggregatePermissions::EValue)(mBits[idx]);
  696. }
  697. return rv;
  698. }
  699. // returns the bits compressed into a single byte: 00TTMMCC
  700. // where TT = transfer, MM = modify, and CC = copy
  701. // LSB is to the right
  702. U8 LLAggregatePermissions::getU8() const
  703. {
  704. U8 byte = mBits[PI_TRANSFER];
  705. byte <<= 2;
  706. byte |= mBits[PI_MODIFY];
  707. byte <<= 2;
  708. byte |= mBits[PI_COPY];
  709. return byte;
  710. }
  711. bool LLAggregatePermissions::isEmpty() const
  712. {
  713. for (S32 i = 0; i < PI_END; ++i)
  714. {
  715. if (mBits[i] != AP_EMPTY)
  716. {
  717. return false;
  718. }
  719. }
  720. return true;
  721. }
  722. void LLAggregatePermissions::aggregate(PermissionMask mask)
  723. {
  724. bool is_allowed = (mask & PERM_COPY) != 0;
  725. aggregateBit(PI_COPY, is_allowed);
  726. is_allowed = (mask & PERM_MODIFY) != 0;
  727. aggregateBit(PI_MODIFY, is_allowed);
  728. is_allowed = (mask & PERM_TRANSFER) != 0;
  729. aggregateBit(PI_TRANSFER, is_allowed);
  730. }
  731. void LLAggregatePermissions::aggregate(const LLAggregatePermissions& ag)
  732. {
  733. for (S32 idx = PI_COPY; idx != PI_END; ++idx)
  734. {
  735. aggregateIndex((EPermIndex)idx, ag.mBits[idx]);
  736. }
  737. }
  738. void LLAggregatePermissions::aggregateBit(EPermIndex idx, bool allowed)
  739. {
  740. switch (mBits[idx])
  741. {
  742. case AP_SOME:
  743. // no-op
  744. break;
  745. case AP_EMPTY:
  746. mBits[idx] = allowed ? AP_ALL : AP_NONE;
  747. break;
  748. case AP_NONE:
  749. mBits[idx] = allowed ? AP_SOME: AP_NONE;
  750. break;
  751. case AP_ALL:
  752. mBits[idx] = allowed ? AP_ALL : AP_SOME;
  753. break;
  754. default:
  755. llwarns << "Bad aggregateBit " << (S32)idx
  756. << (allowed ? " true" : " false") << llendl;
  757. }
  758. }
  759. void LLAggregatePermissions::aggregateIndex(EPermIndex idx, U8 bits)
  760. {
  761. switch (mBits[idx])
  762. {
  763. case AP_EMPTY:
  764. mBits[idx] = bits;
  765. break;
  766. case AP_NONE:
  767. switch (bits)
  768. {
  769. case AP_SOME:
  770. case AP_ALL:
  771. mBits[idx] = AP_SOME;
  772. break;
  773. case AP_EMPTY:
  774. case AP_NONE:
  775. default:
  776. // no-op
  777. break;
  778. }
  779. break;
  780. case AP_SOME:
  781. // no-op
  782. break;
  783. case AP_ALL:
  784. switch (bits)
  785. {
  786. case AP_NONE:
  787. case AP_SOME:
  788. mBits[idx] = AP_SOME;
  789. break;
  790. case AP_EMPTY:
  791. case AP_ALL:
  792. default:
  793. // no-op
  794. break;
  795. }
  796. break;
  797. default:
  798. llwarns << "Bad aggregate index " << (S32)idx << " " << (S32)bits
  799. << llendl;
  800. }
  801. }
  802. // static
  803. LLAggregatePermissions::EPermIndex LLAggregatePermissions::perm2PermIndex(PermissionBit bit)
  804. {
  805. EPermIndex idx = PI_END; // past any good value.
  806. switch (bit)
  807. {
  808. case PERM_COPY:
  809. idx = PI_COPY;
  810. break;
  811. case PERM_MODIFY:
  812. idx = PI_MODIFY;
  813. break;
  814. case PERM_TRANSFER:
  815. idx = PI_TRANSFER;
  816. break;
  817. default:
  818. break;
  819. }
  820. return idx;
  821. }
  822. void LLAggregatePermissions::packMessage(LLMessageSystem* msg,
  823. const char* field) const
  824. {
  825. msg->addU8Fast(field, getU8());
  826. }
  827. void LLAggregatePermissions::unpackMessage(LLMessageSystem* msg,
  828. const char* block,
  829. const char* field,
  830. S32 block_num)
  831. {
  832. const U8 TWO_BITS = 0x3; // binary 00000011
  833. U8 bits = 0;
  834. msg->getU8Fast(block, field, bits, block_num);
  835. mBits[PI_COPY] = bits & TWO_BITS;
  836. bits >>= 2;
  837. mBits[PI_MODIFY] = bits & TWO_BITS;
  838. bits >>= 2;
  839. mBits[PI_TRANSFER] = bits & TWO_BITS;
  840. }
  841. const std::string AGGREGATE_VALUES[4] = {
  842. std::string("Empty"),
  843. std::string("None"),
  844. std::string("Some"),
  845. std::string("All")
  846. };
  847. std::ostream& operator<<(std::ostream& s, const LLAggregatePermissions& perm)
  848. {
  849. s << "{PI_COPY="
  850. << AGGREGATE_VALUES[perm.mBits[LLAggregatePermissions::PI_COPY]]
  851. << ", PI_MODIFY="
  852. << AGGREGATE_VALUES[perm.mBits[LLAggregatePermissions::PI_MODIFY]]
  853. << ", PI_TRANSFER="
  854. << AGGREGATE_VALUES[perm.mBits[LLAggregatePermissions::PI_TRANSFER]]
  855. << "}";
  856. return s;
  857. }
  858. // This converts a permissions mask into a string for debugging use.
  859. void mask_to_string(U32 mask, char* str, bool export_support)
  860. {
  861. if (mask & PERM_MOVE)
  862. {
  863. *str++ = 'V';
  864. }
  865. else
  866. {
  867. *str++ = ' ';
  868. }
  869. if (mask & PERM_MODIFY)
  870. {
  871. *str++ = 'M';
  872. }
  873. else
  874. {
  875. *str++ = ' ';
  876. }
  877. if (mask & PERM_COPY)
  878. {
  879. *str++ = 'C';
  880. }
  881. else
  882. {
  883. *str++ = ' ';
  884. }
  885. if (mask & PERM_TRANSFER)
  886. {
  887. *str++ = 'T';
  888. }
  889. else
  890. {
  891. *str++ = ' ';
  892. }
  893. if (export_support)
  894. {
  895. if (mask & PERM_EXPORT)
  896. {
  897. *str++ = 'E';
  898. }
  899. else
  900. {
  901. *str++ = ' ';
  902. }
  903. }
  904. *str = '\0';
  905. }
  906. std::string mask_to_string(U32 mask, bool export_support)
  907. {
  908. char str[16];
  909. mask_to_string(mask, str, export_support);
  910. return std::string(str);
  911. }
  912. ///----------------------------------------------------------------------------
  913. /// exported functions
  914. ///----------------------------------------------------------------------------
  915. static const std::string PERM_CREATOR_ID_LABEL("creator_id");
  916. static const std::string PERM_OWNER_ID_LABEL("owner_id");
  917. static const std::string PERM_LAST_OWNER_ID_LABEL("last_owner_id");
  918. static const std::string PERM_GROUP_ID_LABEL("group_id");
  919. static const std::string PERM_IS_OWNER_GROUP_LABEL("is_owner_group");
  920. static const std::string PERM_BASE_MASK_LABEL("base_mask");
  921. static const std::string PERM_OWNER_MASK_LABEL("owner_mask");
  922. static const std::string PERM_GROUP_MASK_LABEL("group_mask");
  923. static const std::string PERM_EVERYONE_MASK_LABEL("everyone_mask");
  924. static const std::string PERM_NEXT_OWNER_MASK_LABEL("next_owner_mask");
  925. LLSD ll_create_sd_from_permissions(const LLPermissions& perm)
  926. {
  927. LLSD rv;
  928. rv[PERM_CREATOR_ID_LABEL] = perm.getCreator();
  929. rv[PERM_OWNER_ID_LABEL] = perm.getOwner();
  930. rv[PERM_LAST_OWNER_ID_LABEL] = perm.getLastOwner();
  931. rv[PERM_GROUP_ID_LABEL] = perm.getGroup();
  932. rv[PERM_IS_OWNER_GROUP_LABEL] = perm.isGroupOwned();
  933. rv[PERM_BASE_MASK_LABEL] = (S32)perm.getMaskBase();
  934. rv[PERM_OWNER_MASK_LABEL] = (S32)perm.getMaskOwner();
  935. rv[PERM_GROUP_MASK_LABEL] = (S32)perm.getMaskGroup();
  936. rv[PERM_EVERYONE_MASK_LABEL] = (S32)perm.getMaskEveryone();
  937. rv[PERM_NEXT_OWNER_MASK_LABEL] = (S32)perm.getMaskNextOwner();
  938. return rv;
  939. }
  940. LLPermissions ll_permissions_from_sd(const LLSD& sd_perm)
  941. {
  942. LLPermissions rv;
  943. rv.init(sd_perm[PERM_CREATOR_ID_LABEL].asUUID(),
  944. sd_perm[PERM_OWNER_ID_LABEL].asUUID(),
  945. sd_perm[PERM_LAST_OWNER_ID_LABEL].asUUID(),
  946. sd_perm[PERM_GROUP_ID_LABEL].asUUID());
  947. // We do a cast to U32 here since LLSD does not attempt to
  948. // represent unsigned ints.
  949. PermissionMask mask;
  950. mask = (U32)(sd_perm[PERM_BASE_MASK_LABEL].asInteger());
  951. rv.setMaskBase(mask);
  952. mask = (U32)(sd_perm[PERM_OWNER_MASK_LABEL].asInteger());
  953. rv.setMaskOwner(mask);
  954. mask = (U32)(sd_perm[PERM_EVERYONE_MASK_LABEL].asInteger());
  955. rv.setMaskEveryone(mask);
  956. mask = (U32)(sd_perm[PERM_GROUP_MASK_LABEL].asInteger());
  957. rv.setMaskGroup(mask);
  958. mask = (U32)(sd_perm[PERM_NEXT_OWNER_MASK_LABEL].asInteger());
  959. rv.setMaskNext(mask);
  960. rv.fix();
  961. return rv;
  962. }
  963. bool LLPermissions::allowExportBy(const LLUUID& requester,
  964. ExportPolicy policy) const
  965. {
  966. return !mIsGroupOwned && requester == mOwner && // Must be owner.
  967. // Export is allowed for all export policies when creator.
  968. (requester == mCreator ||
  969. // Allow export for non-creator when PERM_EXPORT bit is set.
  970. (policy == ep_export_bit && (mMaskEveryone & PERM_EXPORT) != 0) ||
  971. // Allow export for non-creator when item is full perm.
  972. (policy == ep_full_perm &&
  973. (mMaskOwner & PERM_ITEM_UNRESTRICTED) == PERM_ITEM_UNRESTRICTED));
  974. }
  975. bool can_set_export(const U32& base, const U32& own, const U32& next)
  976. {
  977. // base and own must have EXPORT, next owner must be UNRESTRICTED
  978. return (base & PERM_EXPORT) != 0 && (own & PERM_EXPORT) != 0 &&
  979. (next & PERM_ITEM_UNRESTRICTED) == PERM_ITEM_UNRESTRICTED;
  980. }
  981. bool perms_allow_export(const LLPermissions& perms)
  982. {
  983. return (perms.getMaskBase() & PERM_EXPORT) != 0 &&
  984. (perms.getMaskEveryone() & PERM_EXPORT) != 0;
  985. }