PermissionManager.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using libsecondlife;
  28. using OpenSim.Region.Environment.Interfaces;
  29. using OpenSim.Region.Environment.Scenes;
  30. namespace OpenSim.Region.Environment
  31. {
  32. public class PermissionManager
  33. {
  34. protected Scene m_scene;
  35. // These are here for testing. They will be taken out
  36. //private uint PERM_ALL = (uint)2147483647;
  37. private uint PERM_COPY = (uint)32768;
  38. //private uint PERM_MODIFY = (uint)16384;
  39. private uint PERM_MOVE = (uint)524288;
  40. //private uint PERM_TRANS = (uint)8192;
  41. private uint PERM_LOCKED = (uint)540672;
  42. // Bypasses the permissions engine (always returns OK)
  43. // disable in any production environment
  44. // TODO: Change this to false when permissions are a desired default
  45. // TODO: Move to configuration option.
  46. private bool m_bypassPermissions = true;
  47. public bool BypassPermissions
  48. {
  49. get { return m_bypassPermissions; }
  50. set { m_bypassPermissions = value; }
  51. }
  52. public PermissionManager()
  53. {
  54. }
  55. public PermissionManager(Scene scene)
  56. {
  57. m_scene = scene;
  58. }
  59. public void Initialise(Scene scene)
  60. {
  61. m_scene = scene;
  62. }
  63. protected virtual void SendPermissionError(LLUUID user, string reason)
  64. {
  65. m_scene.EventManager.TriggerPermissionError(user, reason);
  66. }
  67. protected virtual bool IsAdministrator(LLUUID user)
  68. {
  69. if (m_bypassPermissions)
  70. {
  71. return true;
  72. }
  73. // If there is no master avatar, return false
  74. if (m_scene.RegionInfo.MasterAvatarAssignedUUID != LLUUID.Zero)
  75. {
  76. return m_scene.RegionInfo.MasterAvatarAssignedUUID == user;
  77. }
  78. return false;
  79. }
  80. public virtual bool IsEstateManager(LLUUID user)
  81. {
  82. if (m_bypassPermissions)
  83. {
  84. return true;
  85. }
  86. if (user != LLUUID.Zero)
  87. {
  88. LLUUID[] estatemanagers = m_scene.RegionInfo.EstateSettings.estateManagers;
  89. for (int i = 0; i < estatemanagers.Length; i++)
  90. {
  91. if (estatemanagers[i] == user)
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. protected virtual bool IsGridUser(LLUUID user)
  98. {
  99. return true;
  100. }
  101. protected virtual bool IsGuest(LLUUID user)
  102. {
  103. return false;
  104. }
  105. public virtual bool CanRezObject(LLUUID user, LLVector3 position)
  106. {
  107. bool permission = false;
  108. string reason = "Insufficient permission";
  109. ILandObject land = m_scene.LandChannel.getLandObject(position.X, position.Y);
  110. if (land == null) return false;
  111. if ((land.landData.landFlags & ((int)Parcel.ParcelFlags.CreateObjects)) ==
  112. (int)Parcel.ParcelFlags.CreateObjects)
  113. permission = true;
  114. //TODO: check for group rights
  115. if (IsAdministrator(user))
  116. {
  117. permission = true;
  118. }
  119. else
  120. {
  121. reason = "Not an administrator";
  122. }
  123. if (GenericParcelPermission(user, position))
  124. {
  125. permission = true;
  126. }
  127. else
  128. {
  129. reason = "Not the parcel owner";
  130. }
  131. if (!permission)
  132. SendPermissionError(user, reason);
  133. return permission;
  134. }
  135. /// <summary>
  136. /// Permissions check - can user enter an object?
  137. /// </summary>
  138. /// <param name="user">User attempting move an object</param>
  139. /// <param name="oldPos">Source object-position</param>
  140. /// <param name="newPos">Target object-position</param>
  141. /// <returns>Has permission?</returns>
  142. public virtual bool CanObjectEntry(LLUUID user, LLVector3 oldPos, LLVector3 newPos)
  143. {
  144. if ((newPos.X > 257f || newPos.X < -1f || newPos.Y > 257f || newPos.Y < -1f))
  145. {
  146. return true;
  147. }
  148. ILandObject land1 = m_scene.LandChannel.getLandObject(oldPos.X, oldPos.Y);
  149. ILandObject land2 = m_scene.LandChannel.getLandObject(newPos.X, newPos.Y);
  150. if (land1 == null || land2 == null)
  151. {
  152. return false;
  153. }
  154. if (land2 == null)
  155. {
  156. // need this for crossing borders
  157. return true;
  158. }
  159. if (land1.landData.globalID == land2.landData.globalID)
  160. {
  161. return true;
  162. }
  163. if ((land2.landData.landFlags & ((int)Parcel.ParcelFlags.AllowAllObjectEntry)) != 0)
  164. {
  165. return true;
  166. }
  167. //TODO: check for group rights
  168. if (GenericParcelPermission(user, newPos))
  169. {
  170. return true;
  171. }
  172. SendPermissionError(user, "Not allowed to move objects in this parcel!");
  173. return false;
  174. }
  175. #region Object Permissions
  176. public virtual uint GenerateClientFlags(LLUUID user, LLUUID objID)
  177. {
  178. // Here's the way this works,
  179. // ObjectFlags and Permission flags are two different enumerations
  180. // ObjectFlags, however, tells the client to change what it will allow the user to do.
  181. // So, that means that all of the permissions type ObjectFlags are /temporary/ and only
  182. // supposed to be set when customizing the objectflags for the client.
  183. // These temporary objectflags get computed and added in this function based on the
  184. // Permission mask that's appropriate!
  185. // Outside of this method, they should never be added to objectflags!
  186. // -teravus
  187. SceneObjectPart task=m_scene.GetSceneObjectPart(objID);
  188. // this shouldn't ever happen.. return no permissions/objectflags.
  189. if (task == null)
  190. return (uint)0;
  191. uint objflags = task.GetEffectiveObjectFlags();
  192. LLUUID objectOwner = task.OwnerID;
  193. // Remove any of the objectFlags that are temporary. These will get added back if appropriate
  194. // in the next bit of code
  195. objflags &= (uint)
  196. ~(LLObject.ObjectFlags.ObjectCopy | // Tells client you can copy the object
  197. LLObject.ObjectFlags.ObjectModify | // tells client you can modify the object
  198. LLObject.ObjectFlags.ObjectMove | // tells client that you can move the object (only, no mod)
  199. LLObject.ObjectFlags.ObjectTransfer | // tells the client that you can /take/ the object if you don't own it
  200. LLObject.ObjectFlags.ObjectYouOwner | // Tells client that you're the owner of the object
  201. LLObject.ObjectFlags.ObjectYouOfficer // Tells client that you've got group object editing permission. Used when ObjectGroupOwned is set
  202. );
  203. // Creating the three ObjectFlags options for this method to choose from.
  204. // Customize the OwnerMask
  205. uint objectOwnerMask = ApplyObjectModifyMasks(task.OwnerMask, objflags);
  206. objectOwnerMask |= (uint)LLObject.ObjectFlags.ObjectYouOwner;
  207. // Customize the GroupMask
  208. uint objectGroupMask = ApplyObjectModifyMasks(task.GroupMask, objflags);
  209. // Customize the EveryoneMask
  210. uint objectEveryoneMask = ApplyObjectModifyMasks(task.EveryoneMask, objflags);
  211. // Hack to allow collaboration until Groups and Group Permissions are implemented
  212. if ((objectEveryoneMask & (uint)LLObject.ObjectFlags.ObjectMove) != 0)
  213. objectEveryoneMask |= (uint)LLObject.ObjectFlags.ObjectModify;
  214. if (m_bypassPermissions)
  215. return objectOwnerMask;
  216. // Object owners should be able to edit their own content
  217. if (user == objectOwner)
  218. {
  219. return objectOwnerMask;
  220. }
  221. // Users should be able to edit what is over their land.
  222. ILandObject parcel = m_scene.LandChannel.getLandObject(task.AbsolutePosition.X, task.AbsolutePosition.Y);
  223. if (parcel != null && parcel.landData.ownerID == user)
  224. return objectOwnerMask;
  225. // Admin objects should not be editable by the above
  226. if (IsAdministrator(objectOwner))
  227. return objectEveryoneMask;
  228. // Estate users should be able to edit anything in the sim
  229. if (IsEstateManager(user))
  230. return objectOwnerMask;
  231. // Admin should be able to edit anything in the sim (including admin objects)
  232. if (IsAdministrator(user))
  233. return objectOwnerMask;
  234. return objectEveryoneMask;
  235. }
  236. private uint ApplyObjectModifyMasks(uint setPermissionMask, uint objectFlagsMask)
  237. {
  238. // We are adding the temporary objectflags to the object's objectflags based on the
  239. // permission flag given. These change the F flags on the client.
  240. if ((setPermissionMask & (uint)PermissionMask.Copy) != 0)
  241. {
  242. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectCopy;
  243. }
  244. if ((setPermissionMask & (uint)PermissionMask.Move) != 0)
  245. {
  246. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectMove;
  247. }
  248. if ((setPermissionMask & (uint)PermissionMask.Modify) != 0)
  249. {
  250. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectModify;
  251. }
  252. if ((setPermissionMask & (uint)PermissionMask.Transfer) != 0)
  253. {
  254. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectTransfer;
  255. }
  256. return objectFlagsMask;
  257. }
  258. protected virtual bool GenericObjectPermission(LLUUID currentUser, LLUUID objId)
  259. {
  260. // Default: deny
  261. bool permission = false;
  262. bool locked = false;
  263. if (!m_scene.Entities.ContainsKey(objId))
  264. {
  265. return false;
  266. }
  267. // If it's not an object, we cant edit it.
  268. if ((!(m_scene.Entities[objId] is SceneObjectGroup)))
  269. {
  270. return false;
  271. }
  272. SceneObjectGroup group = (SceneObjectGroup)m_scene.Entities[objId];
  273. LLUUID objectOwner = group.OwnerID;
  274. locked = ((group.RootPart.OwnerMask & PERM_LOCKED) == 0);
  275. // People shouldn't be able to do anything with locked objects, except the Administrator
  276. // The 'set permissions' runs through a different permission check, so when an object owner
  277. // sets an object locked, the only thing that they can do is unlock it.
  278. //
  279. // Nobody but the object owner can set permissions on an object
  280. //
  281. if (locked && (!IsAdministrator(currentUser)))
  282. {
  283. return false;
  284. }
  285. // Object owners should be able to edit their own content
  286. if (currentUser == objectOwner)
  287. {
  288. permission = true;
  289. }
  290. // Users should be able to edit what is over their land.
  291. ILandObject parcel = m_scene.LandChannel.getLandObject(group.AbsolutePosition.X, group.AbsolutePosition.Y);
  292. if ((parcel != null) && (parcel.landData.ownerID == currentUser))
  293. {
  294. permission = true;
  295. }
  296. // Estate users should be able to edit anything in the sim
  297. if (IsEstateManager(currentUser))
  298. {
  299. permission = true;
  300. }
  301. // Admin objects should not be editable by the above
  302. if (IsAdministrator(objectOwner))
  303. {
  304. permission = false;
  305. }
  306. // Admin should be able to edit anything in the sim (including admin objects)
  307. if (IsAdministrator(currentUser))
  308. {
  309. permission = true;
  310. }
  311. return permission;
  312. }
  313. /// <summary>
  314. /// Permissions check - can user delete an object?
  315. /// </summary>
  316. /// <param name="user">User attempting the delete</param>
  317. /// <param name="obj">Target object</param>
  318. /// <returns>Has permission?</returns>
  319. public virtual bool CanDeRezObject(LLUUID user, LLUUID obj)
  320. {
  321. return GenericObjectPermission(user, obj);
  322. }
  323. public virtual bool CanEditObject(LLUUID user, LLUUID obj)
  324. {
  325. return GenericObjectPermission(user, obj);
  326. }
  327. public virtual bool CanEditObjectPosition(LLUUID user, LLUUID obj)
  328. {
  329. bool permission = GenericObjectPermission(user, obj);
  330. if (!permission)
  331. {
  332. if (!m_scene.Entities.ContainsKey(obj))
  333. {
  334. return false;
  335. }
  336. // The client
  337. // may request to edit linked parts, and therefore, it needs
  338. // to also check for SceneObjectPart
  339. // If it's not an object, we cant edit it.
  340. if ((!(m_scene.Entities[obj] is SceneObjectGroup)))
  341. {
  342. return false;
  343. }
  344. SceneObjectGroup task = (SceneObjectGroup)m_scene.Entities[obj];
  345. LLUUID taskOwner = null;
  346. // Added this because at this point in time it wouldn't be wise for
  347. // the administrator object permissions to take effect.
  348. LLUUID objectOwner = task.OwnerID;
  349. // Anyone can move
  350. if ((task.RootPart.EveryoneMask & PERM_MOVE) != 0)
  351. permission = true;
  352. // Locked
  353. if ((task.RootPart.OwnerMask & PERM_LOCKED) == 0)
  354. permission = false;
  355. }
  356. else
  357. {
  358. bool locked = false;
  359. if (!m_scene.Entities.ContainsKey(obj))
  360. {
  361. return false;
  362. }
  363. // If it's not an object, we cant edit it.
  364. if ((!(m_scene.Entities[obj] is SceneObjectGroup)))
  365. {
  366. return false;
  367. }
  368. SceneObjectGroup group = (SceneObjectGroup)m_scene.Entities[obj];
  369. LLUUID objectOwner = group.OwnerID;
  370. locked = ((group.RootPart.OwnerMask & PERM_LOCKED) == 0);
  371. // This is an exception to the generic object permission.
  372. // Administrators who lock their objects should not be able to move them,
  373. // however generic object permission should return true.
  374. // This keeps locked objects from being affected by random click + drag actions by accident
  375. // and allows the administrator to grab or delete a locked object.
  376. // Administrators and estate managers are still able to click+grab locked objects not
  377. // owned by them in the scene
  378. // This is by design.
  379. if (locked && (user == objectOwner))
  380. return false;
  381. }
  382. return permission;
  383. }
  384. public virtual bool CanCopyObject(LLUUID user, LLUUID obj)
  385. {
  386. bool permission = GenericObjectPermission(user, obj);
  387. if (!permission)
  388. {
  389. if (!m_scene.Entities.ContainsKey(obj))
  390. {
  391. return false;
  392. }
  393. // If it's not an object, we cant edit it.
  394. if (!(m_scene.Entities[obj] is SceneObjectGroup))
  395. {
  396. return false;
  397. }
  398. SceneObjectGroup task = (SceneObjectGroup)m_scene.Entities[obj];
  399. LLUUID taskOwner = null;
  400. // Added this because at this point in time it wouldn't be wise for
  401. // the administrator object permissions to take effect.
  402. LLUUID objectOwner = task.OwnerID;
  403. if ((task.RootPart.EveryoneMask & PERM_COPY) != 0)
  404. permission = true;
  405. }
  406. return permission;
  407. }
  408. public virtual bool CanReturnObject(LLUUID user, LLUUID obj)
  409. {
  410. return GenericObjectPermission(user, obj);
  411. }
  412. #endregion
  413. #region Communication Permissions
  414. public virtual bool GenericCommunicationPermission(LLUUID user, LLUUID target)
  415. {
  416. bool permission = false;
  417. string reason = "Only registered users may communicate with another account.";
  418. if (IsGridUser(user))
  419. permission = true;
  420. if (!IsGridUser(user))
  421. {
  422. permission = false;
  423. reason = "The person that you are messaging is not a registered user.";
  424. }
  425. if (IsAdministrator(user))
  426. permission = true;
  427. if (IsEstateManager(user))
  428. permission = true;
  429. if (!permission)
  430. SendPermissionError(user, reason);
  431. return permission;
  432. }
  433. public virtual bool CanInstantMessage(LLUUID user, LLUUID target)
  434. {
  435. return GenericCommunicationPermission(user, target);
  436. }
  437. public virtual bool CanInventoryTransfer(LLUUID user, LLUUID target)
  438. {
  439. return GenericCommunicationPermission(user, target);
  440. }
  441. #endregion
  442. public virtual bool CanEditScript(LLUUID user, LLUUID script)
  443. {
  444. return IsAdministrator(user);
  445. }
  446. public virtual bool CanRunScript(LLUUID user, LLUUID script)
  447. {
  448. return IsAdministrator(user);
  449. }
  450. public virtual bool CanRunConsoleCommand(LLUUID user)
  451. {
  452. return IsAdministrator(user);
  453. }
  454. public virtual bool CanTerraform(LLUUID user, LLVector3 position)
  455. {
  456. bool permission = false;
  457. // Estate override
  458. if (GenericEstatePermission(user))
  459. permission = true;
  460. float X = position.X;
  461. float Y = position.Y;
  462. if (X > 255)
  463. X = 255;
  464. if (Y > 255)
  465. Y = 255;
  466. if (X < 0)
  467. X = 0;
  468. if (Y < 0)
  469. Y = 0;
  470. // Land owner can terraform too
  471. ILandObject parcel = m_scene.LandChannel.getLandObject(X, Y);
  472. if (parcel != null && GenericParcelPermission(user, parcel))
  473. permission = true;
  474. if (!permission)
  475. SendPermissionError(user, "Not authorized to terraform at this location.");
  476. return permission;
  477. }
  478. #region Estate Permissions
  479. public virtual bool GenericEstatePermission(LLUUID user)
  480. {
  481. // Default: deny
  482. bool permission = false;
  483. // Estate admins should be able to use estate tools
  484. if (IsEstateManager(user))
  485. permission = true;
  486. // Administrators always have permission
  487. if (IsAdministrator(user))
  488. permission = true;
  489. return permission;
  490. }
  491. public virtual bool CanEditEstateTerrain(LLUUID user)
  492. {
  493. return GenericEstatePermission(user);
  494. }
  495. public virtual bool CanRestartSim(LLUUID user)
  496. {
  497. // Since this is potentially going on a grid...
  498. return GenericEstatePermission(user);
  499. //return m_scene.RegionInfo.MasterAvatarAssignedUUID == user;
  500. }
  501. #endregion
  502. #region Parcel Permissions
  503. protected virtual bool GenericParcelPermission(LLUUID user, ILandObject parcel)
  504. {
  505. bool permission = false;
  506. if (parcel.landData.ownerID == user)
  507. {
  508. permission = true;
  509. }
  510. if (parcel.landData.isGroupOwned)
  511. {
  512. // TODO: Need to do some extra checks here. Requires group code.
  513. }
  514. if (IsEstateManager(user))
  515. {
  516. permission = true;
  517. }
  518. if (IsAdministrator(user))
  519. {
  520. permission = true;
  521. }
  522. return permission;
  523. }
  524. protected virtual bool GenericParcelPermission(LLUUID user, LLVector3 pos)
  525. {
  526. ILandObject parcel = m_scene.LandChannel.getLandObject(pos.X, pos.Y);
  527. if (parcel == null) return false;
  528. return GenericParcelPermission(user, parcel);
  529. }
  530. public virtual bool CanEditParcel(LLUUID user, ILandObject parcel)
  531. {
  532. return GenericParcelPermission(user, parcel);
  533. }
  534. public virtual bool CanSellParcel(LLUUID user, ILandObject parcel)
  535. {
  536. return GenericParcelPermission(user, parcel);
  537. }
  538. public virtual bool CanAbandonParcel(LLUUID user, ILandObject parcel)
  539. {
  540. return GenericParcelPermission(user, parcel);
  541. }
  542. #endregion
  543. }
  544. }