PermissionManager.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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.Scenes;
  29. using OpenSim.Region.Environment.Interfaces;
  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. #region Object Permissions
  136. public virtual uint GenerateClientFlags(LLUUID user, LLUUID objID)
  137. {
  138. // Here's the way this works,
  139. // ObjectFlags and Permission flags are two different enumerations
  140. // ObjectFlags, however, tells the client to change what it will allow the user to do.
  141. // So, that means that all of the permissions type ObjectFlags are /temporary/ and only
  142. // supposed to be set when customizing the objectflags for the client.
  143. // These temporary objectflags get computed and added in this function based on the
  144. // Permission mask that's appropriate!
  145. // Outside of this method, they should never be added to objectflags!
  146. // -teravus
  147. if (!m_scene.Entities.ContainsKey(objID))
  148. {
  149. return 0;
  150. }
  151. // If it's not an object, we cant edit it.
  152. if (!(m_scene.Entities[objID] is SceneObjectGroup))
  153. {
  154. return 0;
  155. }
  156. SceneObjectGroup task = (SceneObjectGroup)m_scene.Entities[objID];
  157. LLUUID objectOwner = task.OwnerID;
  158. uint objflags = task.RootPart.ObjectFlags;
  159. // Remove any of the objectFlags that are temporary. These will get added back if appropriate
  160. // in the next bit of code
  161. objflags &= (uint)
  162. ~(LLObject.ObjectFlags.ObjectCopy | // Tells client you can copy the object
  163. LLObject.ObjectFlags.ObjectModify | // tells client you can modify the object
  164. LLObject.ObjectFlags.ObjectMove | // tells client that you can move the object (only, no mod)
  165. LLObject.ObjectFlags.ObjectTransfer | // tells the client that you can /take/ the object if you don't own it
  166. LLObject.ObjectFlags.ObjectYouOwner | // Tells client that you're the owner of the object
  167. LLObject.ObjectFlags.ObjectYouOfficer // Tells client that you've got group object editing permission. Used when ObjectGroupOwned is set
  168. );
  169. // Creating the three ObjectFlags options for this method to choose from.
  170. // Customize the OwnerMask
  171. uint objectOwnerMask = ApplyObjectModifyMasks(task.RootPart.OwnerMask, objflags);
  172. objectOwnerMask |= (uint)LLObject.ObjectFlags.ObjectYouOwner;
  173. // Customize the GroupMask
  174. uint objectGroupMask = ApplyObjectModifyMasks(task.RootPart.GroupMask, objflags);
  175. // Customize the EveryoneMask
  176. uint objectEveryoneMask = ApplyObjectModifyMasks(task.RootPart.EveryoneMask, objflags);
  177. // Hack to allow collaboration until Groups and Group Permissions are implemented
  178. if ((objectEveryoneMask & (uint)LLObject.ObjectFlags.ObjectMove) != 0)
  179. objectEveryoneMask |= (uint)LLObject.ObjectFlags.ObjectModify;
  180. if (m_bypassPermissions)
  181. return objectOwnerMask;
  182. // Object owners should be able to edit their own content
  183. if (user == objectOwner)
  184. {
  185. return objectOwnerMask;
  186. }
  187. // Users should be able to edit what is over their land.
  188. ILandObject parcel = m_scene.LandChannel.getLandObject(task.AbsolutePosition.X, task.AbsolutePosition.Y);
  189. if (parcel != null && parcel.landData.ownerID == user)
  190. return objectOwnerMask;
  191. // Admin objects should not be editable by the above
  192. if (IsAdministrator(objectOwner))
  193. return objectEveryoneMask;
  194. // Estate users should be able to edit anything in the sim
  195. if (IsEstateManager(user))
  196. return objectOwnerMask;
  197. // Admin should be able to edit anything in the sim (including admin objects)
  198. if (IsAdministrator(user))
  199. return objectOwnerMask;
  200. return objectEveryoneMask;
  201. }
  202. private uint ApplyObjectModifyMasks(uint setPermissionMask, uint objectFlagsMask)
  203. {
  204. // We are adding the temporary objectflags to the object's objectflags based on the
  205. // permission flag given. These change the F flags on the client.
  206. if ((setPermissionMask & (uint)PermissionMask.Copy) != 0)
  207. {
  208. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectCopy;
  209. }
  210. if ((setPermissionMask & (uint)PermissionMask.Move) != 0)
  211. {
  212. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectMove;
  213. }
  214. if ((setPermissionMask & (uint)PermissionMask.Modify) != 0)
  215. {
  216. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectModify;
  217. }
  218. if ((setPermissionMask & (uint)PermissionMask.Transfer) != 0)
  219. {
  220. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectTransfer;
  221. }
  222. return objectFlagsMask;
  223. }
  224. protected virtual bool GenericObjectPermission(LLUUID currentUser, LLUUID objId)
  225. {
  226. // Default: deny
  227. bool permission = false;
  228. bool locked = false;
  229. if (!m_scene.Entities.ContainsKey(objId))
  230. {
  231. return false;
  232. }
  233. // If it's not an object, we cant edit it.
  234. if ((!(m_scene.Entities[objId] is SceneObjectGroup)))
  235. {
  236. return false;
  237. }
  238. SceneObjectGroup group = (SceneObjectGroup)m_scene.Entities[objId];
  239. LLUUID objectOwner = group.OwnerID;
  240. locked = ((group.RootPart.OwnerMask & PERM_LOCKED) == 0);
  241. // People shouldn't be able to do anything with locked objects, except the Administrator
  242. // The 'set permissions' runs through a different permission check, so when an object owner
  243. // sets an object locked, the only thing that they can do is unlock it.
  244. //
  245. // Nobody but the object owner can set permissions on an object
  246. //
  247. if (locked && (!IsAdministrator(currentUser)))
  248. {
  249. return false;
  250. }
  251. // Object owners should be able to edit their own content
  252. if (currentUser == objectOwner)
  253. {
  254. permission = true;
  255. }
  256. // Users should be able to edit what is over their land.
  257. ILandObject parcel = m_scene.LandChannel.getLandObject(group.AbsolutePosition.X, group.AbsolutePosition.Y);
  258. if ((parcel != null) && (parcel.landData.ownerID == currentUser))
  259. {
  260. permission = true;
  261. }
  262. // Estate users should be able to edit anything in the sim
  263. if (IsEstateManager(currentUser))
  264. {
  265. permission = true;
  266. }
  267. // Admin objects should not be editable by the above
  268. if (IsAdministrator(objectOwner))
  269. {
  270. permission = false;
  271. }
  272. // Admin should be able to edit anything in the sim (including admin objects)
  273. if (IsAdministrator(currentUser))
  274. {
  275. permission = true;
  276. }
  277. return permission;
  278. }
  279. /// <summary>
  280. /// Permissions check - can user delete an object?
  281. /// </summary>
  282. /// <param name="user">User attempting the delete</param>
  283. /// <param name="obj">Target object</param>
  284. /// <returns>Has permission?</returns>
  285. public virtual bool CanDeRezObject(LLUUID user, LLUUID obj)
  286. {
  287. return GenericObjectPermission(user, obj);
  288. }
  289. public virtual bool CanEditObject(LLUUID user, LLUUID obj)
  290. {
  291. return GenericObjectPermission(user, obj);
  292. }
  293. public virtual bool CanEditObjectPosition(LLUUID user, LLUUID obj)
  294. {
  295. bool permission = GenericObjectPermission(user, obj);
  296. if (!permission)
  297. {
  298. if (!m_scene.Entities.ContainsKey(obj))
  299. {
  300. return false;
  301. }
  302. // The client
  303. // may request to edit linked parts, and therefore, it needs
  304. // to also check for SceneObjectPart
  305. // If it's not an object, we cant edit it.
  306. if ((!(m_scene.Entities[obj] is SceneObjectGroup)))
  307. {
  308. return false;
  309. }
  310. SceneObjectGroup task = (SceneObjectGroup)m_scene.Entities[obj];
  311. LLUUID taskOwner = null;
  312. // Added this because at this point in time it wouldn't be wise for
  313. // the administrator object permissions to take effect.
  314. LLUUID objectOwner = task.OwnerID;
  315. // Anyone can move
  316. if ((task.RootPart.EveryoneMask & PERM_MOVE) != 0)
  317. permission = true;
  318. // Locked
  319. if ((task.RootPart.OwnerMask & PERM_LOCKED) == 0)
  320. permission = false;
  321. }
  322. else
  323. {
  324. bool locked = false;
  325. if (!m_scene.Entities.ContainsKey(obj))
  326. {
  327. return false;
  328. }
  329. // If it's not an object, we cant edit it.
  330. if ((!(m_scene.Entities[obj] is SceneObjectGroup)))
  331. {
  332. return false;
  333. }
  334. SceneObjectGroup group = (SceneObjectGroup)m_scene.Entities[obj];
  335. LLUUID objectOwner = group.OwnerID;
  336. locked = ((group.RootPart.OwnerMask & PERM_LOCKED) == 0);
  337. // This is an exception to the generic object permission.
  338. // Administrators who lock their objects should not be able to move them,
  339. // however generic object permission should return true.
  340. // This keeps locked objects from being affected by random click + drag actions by accident
  341. // and allows the administrator to grab or delete a locked object.
  342. // Administrators and estate managers are still able to click+grab locked objects not
  343. // owned by them in the scene
  344. // This is by design.
  345. if (locked && (user == objectOwner))
  346. return false;
  347. }
  348. return permission;
  349. }
  350. public virtual bool CanCopyObject(LLUUID user, LLUUID obj)
  351. {
  352. bool permission = GenericObjectPermission(user, obj);
  353. if (!permission)
  354. {
  355. if (!m_scene.Entities.ContainsKey(obj))
  356. {
  357. return false;
  358. }
  359. // If it's not an object, we cant edit it.
  360. if (!(m_scene.Entities[obj] is SceneObjectGroup))
  361. {
  362. return false;
  363. }
  364. SceneObjectGroup task = (SceneObjectGroup)m_scene.Entities[obj];
  365. LLUUID taskOwner = null;
  366. // Added this because at this point in time it wouldn't be wise for
  367. // the administrator object permissions to take effect.
  368. LLUUID objectOwner = task.OwnerID;
  369. if ((task.RootPart.EveryoneMask & PERM_COPY) != 0)
  370. permission = true;
  371. }
  372. return permission;
  373. }
  374. public virtual bool CanReturnObject(LLUUID user, LLUUID obj)
  375. {
  376. return GenericObjectPermission(user, obj);
  377. }
  378. #endregion
  379. #region Communication Permissions
  380. public virtual bool GenericCommunicationPermission(LLUUID user, LLUUID target)
  381. {
  382. bool permission = false;
  383. string reason = "Only registered users may communicate with another account.";
  384. if (IsGridUser(user))
  385. permission = true;
  386. if (!IsGridUser(user))
  387. {
  388. permission = false;
  389. reason = "The person that you are messaging is not a registered user.";
  390. }
  391. if (IsAdministrator(user))
  392. permission = true;
  393. if (IsEstateManager(user))
  394. permission = true;
  395. if (!permission)
  396. SendPermissionError(user, reason);
  397. return permission;
  398. }
  399. public virtual bool CanInstantMessage(LLUUID user, LLUUID target)
  400. {
  401. return GenericCommunicationPermission(user, target);
  402. }
  403. public virtual bool CanInventoryTransfer(LLUUID user, LLUUID target)
  404. {
  405. return GenericCommunicationPermission(user, target);
  406. }
  407. #endregion
  408. public virtual bool CanEditScript(LLUUID user, LLUUID script)
  409. {
  410. return IsAdministrator(user);
  411. }
  412. public virtual bool CanRunScript(LLUUID user, LLUUID script)
  413. {
  414. return IsAdministrator(user);
  415. }
  416. public virtual bool CanRunConsoleCommand(LLUUID user)
  417. {
  418. return IsAdministrator(user);
  419. }
  420. public virtual bool CanTerraform(LLUUID user, LLVector3 position)
  421. {
  422. bool permission = false;
  423. // Estate override
  424. if (GenericEstatePermission(user))
  425. permission = true;
  426. float X = position.X;
  427. float Y = position.Y;
  428. if (X > 255)
  429. X = 255;
  430. if (Y > 255)
  431. Y = 255;
  432. if (X < 0)
  433. X = 0;
  434. if (Y < 0)
  435. Y = 0;
  436. // Land owner can terraform too
  437. ILandObject parcel = m_scene.LandChannel.getLandObject(X, Y);
  438. if (parcel != null && GenericParcelPermission(user, parcel))
  439. permission = true;
  440. if (!permission)
  441. SendPermissionError(user, "Not authorized to terraform at this location.");
  442. return permission;
  443. }
  444. #region Estate Permissions
  445. public virtual bool GenericEstatePermission(LLUUID user)
  446. {
  447. // Default: deny
  448. bool permission = false;
  449. // Estate admins should be able to use estate tools
  450. if (IsEstateManager(user))
  451. permission = true;
  452. // Administrators always have permission
  453. if (IsAdministrator(user))
  454. permission = true;
  455. return permission;
  456. }
  457. public virtual bool CanEditEstateTerrain(LLUUID user)
  458. {
  459. return GenericEstatePermission(user);
  460. }
  461. public virtual bool CanRestartSim(LLUUID user)
  462. {
  463. // Since this is potentially going on a grid...
  464. return GenericEstatePermission(user);
  465. //return m_scene.RegionInfo.MasterAvatarAssignedUUID == user;
  466. }
  467. #endregion
  468. #region Parcel Permissions
  469. protected virtual bool GenericParcelPermission(LLUUID user, ILandObject parcel)
  470. {
  471. bool permission = false;
  472. if (parcel.landData.ownerID == user)
  473. {
  474. permission = true;
  475. }
  476. if (parcel.landData.isGroupOwned)
  477. {
  478. // TODO: Need to do some extra checks here. Requires group code.
  479. }
  480. if (IsEstateManager(user))
  481. {
  482. permission = true;
  483. }
  484. if (IsAdministrator(user))
  485. {
  486. permission = true;
  487. }
  488. return permission;
  489. }
  490. protected virtual bool GenericParcelPermission(LLUUID user, LLVector3 pos)
  491. {
  492. ILandObject parcel = m_scene.LandChannel.getLandObject(pos.X, pos.Y);
  493. if (parcel == null) return false;
  494. return GenericParcelPermission(user, parcel);
  495. }
  496. public virtual bool CanEditParcel(LLUUID user, ILandObject parcel)
  497. {
  498. return GenericParcelPermission(user, parcel);
  499. }
  500. public virtual bool CanSellParcel(LLUUID user, ILandObject parcel)
  501. {
  502. return GenericParcelPermission(user, parcel);
  503. }
  504. public virtual bool CanAbandonParcel(LLUUID user, ILandObject parcel)
  505. {
  506. return GenericParcelPermission(user, parcel);
  507. }
  508. #endregion
  509. }
  510. }