PermissionManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. */
  28. using libsecondlife;
  29. using OpenSim.Region.Environment.LandManagement;
  30. using OpenSim.Region.Environment.Scenes;
  31. namespace OpenSim.Region.Environment
  32. {
  33. public class PermissionManager
  34. {
  35. protected Scene m_scene;
  36. // These are here for testing. They will be taken out
  37. private uint PERM_ALL = (uint)2147483647;
  38. private uint PERM_COPY = (uint)32768;
  39. private uint PERM_MODIFY = (uint)16384;
  40. private uint PERM_MOVE = (uint)524288;
  41. private uint PERM_TRANS = (uint)8192;
  42. private uint PERM_LOCKED = (uint)540672;
  43. // Bypasses the permissions engine (always returns OK)
  44. // disable in any production environment
  45. // TODO: Change this to false when permissions are a desired default
  46. // TODO: Move to configuration option.
  47. private bool m_bypassPermissions = true;
  48. public bool BypassPermissions
  49. {
  50. get { return m_bypassPermissions; }
  51. set { m_bypassPermissions = value; }
  52. }
  53. public PermissionManager()
  54. {
  55. }
  56. public PermissionManager(Scene scene)
  57. {
  58. m_scene = scene;
  59. }
  60. public void Initialise(Scene scene)
  61. {
  62. m_scene = scene;
  63. }
  64. protected virtual void SendPermissionError(LLUUID user, string reason)
  65. {
  66. m_scene.EventManager.TriggerPermissionError(user, reason);
  67. }
  68. protected virtual bool IsAdministrator(LLUUID user)
  69. {
  70. if (m_bypassPermissions)
  71. {
  72. return true;
  73. }
  74. // If there is no master avatar, return false
  75. if (m_scene.RegionInfo.MasterAvatarAssignedUUID != null)
  76. {
  77. return m_scene.RegionInfo.MasterAvatarAssignedUUID == user;
  78. }
  79. return false;
  80. }
  81. public virtual bool IsEstateManager(LLUUID user)
  82. {
  83. if (m_bypassPermissions)
  84. {
  85. return true;
  86. }
  87. if (user != null)
  88. {
  89. LLUUID[] estatemanagers = m_scene.RegionInfo.EstateSettings.estateManagers;
  90. for (int i = 0; i < estatemanagers.Length; i++)
  91. {
  92. if (estatemanagers[i] == user)
  93. return true;
  94. }
  95. }
  96. // The below is commented out because logically it happens anyway. It's left in for readability
  97. //else
  98. //{
  99. //return false;
  100. //}
  101. return false;
  102. }
  103. protected virtual bool IsGridUser(LLUUID user)
  104. {
  105. return true;
  106. }
  107. protected virtual bool IsGuest(LLUUID user)
  108. {
  109. return false;
  110. }
  111. public virtual bool CanRezObject(LLUUID user, LLVector3 position)
  112. {
  113. bool permission = false;
  114. string reason = "Insufficient permission";
  115. Land land = m_scene.LandManager.getLandObject(position.X, position.Y);
  116. if (land == null) return false;
  117. if ((land.landData.landFlags & ((int)Parcel.ParcelFlags.CreateObjects)) ==
  118. (int)Parcel.ParcelFlags.CreateObjects)
  119. permission = true;
  120. //TODO: check for group rights
  121. if (IsAdministrator(user))
  122. {
  123. permission = true;
  124. }
  125. else
  126. {
  127. reason = "Not an administrator";
  128. }
  129. if (GenericParcelPermission(user, position))
  130. {
  131. permission = true;
  132. }
  133. else
  134. {
  135. reason = "Not the parcel owner";
  136. }
  137. if (!permission)
  138. SendPermissionError(user, reason);
  139. return permission;
  140. }
  141. #region Object Permissions
  142. public virtual uint GenerateClientFlags(LLUUID user, LLUUID objID)
  143. {
  144. // Here's the way this works,
  145. // ObjectFlags and Permission flags are two different enumerations
  146. // ObjectFlags, however, tells the client to change what it will allow the user to do.
  147. // So, that means that all of the permissions type ObjectFlags are /temporary/ and only
  148. // supposed to be set when customizing the objectflags for the client.
  149. // These temporary objectflags get computed and added in this function based on the
  150. // Permission mask that's appropriate!
  151. // Outside of this method, they should never be added to objectflags!
  152. // -teravus
  153. if (!m_scene.Entities.ContainsKey(objID))
  154. {
  155. return 0;
  156. }
  157. // If it's not an object, we cant edit it.
  158. if (!(m_scene.Entities[objID] is SceneObjectGroup))
  159. {
  160. return 0;
  161. }
  162. SceneObjectGroup task = (SceneObjectGroup)m_scene.Entities[objID];
  163. LLUUID objectOwner = task.OwnerID;
  164. uint objflags = task.RootPart.ObjectFlags;
  165. // Remove any of the objectFlags that are temporary. These will get added back if appropriate
  166. // in the next bit of code
  167. objflags &= (uint)
  168. ~(LLObject.ObjectFlags.ObjectCopy | // Tells client you can copy the object
  169. LLObject.ObjectFlags.ObjectModify | // tells client you can modify the object
  170. LLObject.ObjectFlags.ObjectMove | // tells client that you can move the object (only, no mod)
  171. LLObject.ObjectFlags.ObjectTransfer | // tells the client that you can /take/ the object if you don't own it
  172. LLObject.ObjectFlags.ObjectYouOwner | // Tells client that you're the owner of the object
  173. LLObject.ObjectFlags.ObjectYouOfficer // Tells client that you've got group object editing permission. Used when ObjectGroupOwned is set
  174. );
  175. // Creating the three ObjectFlags options for this method to choose from.
  176. // Customize the OwnerMask
  177. uint objectOwnerMask = ApplyObjectModifyMasks(task.RootPart.OwnerMask, objflags);
  178. objectOwnerMask |= (uint)LLObject.ObjectFlags.ObjectYouOwner;
  179. // Customize the GroupMask
  180. uint objectGroupMask = ApplyObjectModifyMasks(task.RootPart.GroupMask, objflags);
  181. // Customize the EveryoneMask
  182. uint objectEveryoneMask = ApplyObjectModifyMasks(task.RootPart.EveryoneMask, objflags);
  183. // Hack to allow collaboration until Groups and Group Permissions are implemented
  184. if ((objectEveryoneMask & (uint)LLObject.ObjectFlags.ObjectMove) != 0)
  185. objectEveryoneMask |= (uint)LLObject.ObjectFlags.ObjectModify;
  186. if (m_bypassPermissions)
  187. return objectOwnerMask;
  188. // Object owners should be able to edit their own content
  189. if (user == objectOwner)
  190. {
  191. return objectOwnerMask;
  192. }
  193. // Users should be able to edit what is over their land.
  194. Land parcel = m_scene.LandManager.getLandObject(task.AbsolutePosition.X, task.AbsolutePosition.Y);
  195. if (parcel != null && parcel.landData.ownerID == user)
  196. return objectOwnerMask;
  197. // Admin objects should not be editable by the above
  198. if (IsAdministrator(objectOwner))
  199. return objectEveryoneMask;
  200. // Estate users should be able to edit anything in the sim
  201. if (IsEstateManager(user))
  202. return objectOwnerMask;
  203. // Admin should be able to edit anything in the sim (including admin objects)
  204. if (IsAdministrator(user))
  205. return objectOwnerMask;
  206. return objectEveryoneMask;
  207. }
  208. private uint ApplyObjectModifyMasks(uint setPermissionMask, uint objectFlagsMask)
  209. {
  210. // We are adding the temporary objectflags to the object's objectflags based on the
  211. // permission flag given. These change the F flags on the client.
  212. if ((setPermissionMask & (uint)PermissionMask.Copy) != 0)
  213. {
  214. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectCopy;
  215. }
  216. if ((setPermissionMask & (uint)PermissionMask.Move) != 0)
  217. {
  218. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectMove;
  219. }
  220. if ((setPermissionMask & (uint)PermissionMask.Modify) != 0)
  221. {
  222. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectModify;
  223. }
  224. if ((setPermissionMask & (uint)PermissionMask.Transfer) != 0)
  225. {
  226. objectFlagsMask |= (uint)LLObject.ObjectFlags.ObjectTransfer;
  227. }
  228. return objectFlagsMask;
  229. }
  230. protected virtual bool GenericObjectPermission(LLUUID currentUser, LLUUID objId)
  231. {
  232. // Default: deny
  233. bool permission = false;
  234. bool locked = false;
  235. if (!m_scene.Entities.ContainsKey(objId))
  236. {
  237. return false;
  238. }
  239. // If it's not an object, we cant edit it.
  240. if ((!(m_scene.Entities[objId] is SceneObjectGroup)))
  241. {
  242. return false;
  243. }
  244. SceneObjectGroup group = (SceneObjectGroup)m_scene.Entities[objId];
  245. LLUUID objectOwner = group.OwnerID;
  246. locked = ((group.RootPart.OwnerMask & PERM_LOCKED) == 0);
  247. // People shouldn't be able to do anything with locked objects, except the Administrator
  248. // The 'set permissions' runs through a different permission check, so when an object owner
  249. // sets an object locked, the only thing that they can do is unlock it.
  250. //
  251. // Nobody but the object owner can set permissions on an object
  252. //
  253. if (locked && (!IsAdministrator(currentUser)))
  254. {
  255. return false;
  256. }
  257. // Object owners should be able to edit their own content
  258. if (currentUser == objectOwner)
  259. {
  260. permission = true;
  261. }
  262. // Users should be able to edit what is over their land.
  263. Land parcel = m_scene.LandManager.getLandObject(group.AbsolutePosition.X, group.AbsolutePosition.Y);
  264. if ((parcel != null) && (parcel.landData.ownerID == currentUser))
  265. {
  266. permission = true;
  267. }
  268. // Estate users should be able to edit anything in the sim
  269. if (IsEstateManager(currentUser))
  270. {
  271. permission = true;
  272. }
  273. // Admin objects should not be editable by the above
  274. if (IsAdministrator(objectOwner))
  275. {
  276. permission = false;
  277. }
  278. // Admin should be able to edit anything in the sim (including admin objects)
  279. if (IsAdministrator(currentUser))
  280. {
  281. permission = true;
  282. }
  283. return permission;
  284. }
  285. /// <summary>
  286. /// Permissions check - can user delete an object?
  287. /// </summary>
  288. /// <param name="user">User attempting the delete</param>
  289. /// <param name="obj">Target object</param>
  290. /// <returns>Has permission?</returns>
  291. public virtual bool CanDeRezObject(LLUUID user, LLUUID obj)
  292. {
  293. return GenericObjectPermission(user, obj);
  294. }
  295. public virtual bool CanEditObject(LLUUID user, LLUUID obj)
  296. {
  297. return GenericObjectPermission(user, obj);
  298. }
  299. public virtual bool CanEditObjectPosition(LLUUID user, LLUUID obj)
  300. {
  301. bool permission = GenericObjectPermission(user, obj);
  302. if (!permission)
  303. {
  304. if (!m_scene.Entities.ContainsKey(obj))
  305. {
  306. return false;
  307. }
  308. // The client
  309. // may request to edit linked parts, and therefore, it needs
  310. // to also check for SceneObjectPart
  311. // If it's not an object, we cant edit it.
  312. if ((!(m_scene.Entities[obj] is SceneObjectGroup)))
  313. {
  314. return false;
  315. }
  316. SceneObjectGroup task = (SceneObjectGroup)m_scene.Entities[obj];
  317. LLUUID taskOwner = null;
  318. // Added this because at this point in time it wouldn't be wise for
  319. // the administrator object permissions to take effect.
  320. LLUUID objectOwner = task.OwnerID;
  321. // Anyone can move
  322. if ((task.RootPart.EveryoneMask & PERM_MOVE) != 0)
  323. permission = true;
  324. // Locked
  325. if ((task.RootPart.OwnerMask & PERM_LOCKED) != 0)
  326. permission = false;
  327. }
  328. return permission;
  329. }
  330. public virtual bool CanCopyObject(LLUUID user, LLUUID obj)
  331. {
  332. bool permission = GenericObjectPermission(user, obj);
  333. if (!permission)
  334. {
  335. if (!m_scene.Entities.ContainsKey(obj))
  336. {
  337. return false;
  338. }
  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. if ((task.RootPart.EveryoneMask & PERM_COPY) != 0)
  350. permission = true;
  351. }
  352. return permission;
  353. }
  354. public virtual bool CanReturnObject(LLUUID user, LLUUID obj)
  355. {
  356. return GenericObjectPermission(user, obj);
  357. }
  358. #endregion
  359. #region Communication Permissions
  360. public virtual bool GenericCommunicationPermission(LLUUID user, LLUUID target)
  361. {
  362. bool permission = false;
  363. string reason = "Only registered users may communicate with another account.";
  364. if (IsGridUser(user))
  365. permission = true;
  366. if (!IsGridUser(user))
  367. {
  368. permission = false;
  369. reason = "The person that you are messaging is not a registered user.";
  370. }
  371. if (IsAdministrator(user))
  372. permission = true;
  373. if (IsEstateManager(user))
  374. permission = true;
  375. if (!permission)
  376. SendPermissionError(user, reason);
  377. return permission;
  378. }
  379. public virtual bool CanInstantMessage(LLUUID user, LLUUID target)
  380. {
  381. return GenericCommunicationPermission(user, target);
  382. }
  383. public virtual bool CanInventoryTransfer(LLUUID user, LLUUID target)
  384. {
  385. return GenericCommunicationPermission(user, target);
  386. }
  387. #endregion
  388. public virtual bool CanEditScript(LLUUID user, LLUUID script)
  389. {
  390. return IsAdministrator(user);
  391. }
  392. public virtual bool CanRunScript(LLUUID user, LLUUID script)
  393. {
  394. return IsAdministrator(user);
  395. }
  396. public virtual bool CanRunConsoleCommand(LLUUID user)
  397. {
  398. return IsAdministrator(user);
  399. }
  400. public virtual bool CanTerraform(LLUUID user, LLVector3 position)
  401. {
  402. bool permission = false;
  403. // Estate override
  404. if (GenericEstatePermission(user))
  405. permission = true;
  406. float X = position.X;
  407. float Y = position.Y;
  408. if (X > 255)
  409. X = 255;
  410. if (Y > 255)
  411. Y = 255;
  412. if (X < 0)
  413. X = 0;
  414. if (Y < 0)
  415. Y = 0;
  416. // Land owner can terraform too
  417. Land parcel = m_scene.LandManager.getLandObject(X, Y);
  418. if (parcel != null && GenericParcelPermission(user, parcel))
  419. permission = true;
  420. if (!permission)
  421. SendPermissionError(user, "Not authorized to terraform at this location.");
  422. return permission;
  423. }
  424. #region Estate Permissions
  425. public virtual bool GenericEstatePermission(LLUUID user)
  426. {
  427. // Default: deny
  428. bool permission = false;
  429. // Estate admins should be able to use estate tools
  430. if (IsEstateManager(user))
  431. permission = true;
  432. // Administrators always have permission
  433. if (IsAdministrator(user))
  434. permission = true;
  435. return permission;
  436. }
  437. public virtual bool CanEditEstateTerrain(LLUUID user)
  438. {
  439. return GenericEstatePermission(user);
  440. }
  441. public virtual bool CanRestartSim(LLUUID user)
  442. {
  443. // Since this is potentially going on a grid...
  444. return GenericEstatePermission(user);
  445. //return m_scene.RegionInfo.MasterAvatarAssignedUUID == user;
  446. }
  447. #endregion
  448. #region Parcel Permissions
  449. protected virtual bool GenericParcelPermission(LLUUID user, Land parcel)
  450. {
  451. bool permission = false;
  452. if (parcel.landData.ownerID == user)
  453. {
  454. permission = true;
  455. }
  456. if (parcel.landData.isGroupOwned)
  457. {
  458. // TODO: Need to do some extra checks here. Requires group code.
  459. }
  460. if (IsEstateManager(user))
  461. {
  462. permission = true;
  463. }
  464. if (IsAdministrator(user))
  465. {
  466. permission = true;
  467. }
  468. return permission;
  469. }
  470. protected virtual bool GenericParcelPermission(LLUUID user, LLVector3 pos)
  471. {
  472. Land parcel = m_scene.LandManager.getLandObject(pos.X, pos.Y);
  473. if (parcel == null) return false;
  474. return GenericParcelPermission(user, parcel);
  475. }
  476. public virtual bool CanEditParcel(LLUUID user, Land parcel)
  477. {
  478. return GenericParcelPermission(user, parcel);
  479. }
  480. public virtual bool CanSellParcel(LLUUID user, Land parcel)
  481. {
  482. return GenericParcelPermission(user, parcel);
  483. }
  484. public virtual bool CanAbandonParcel(LLUUID user, Land parcel)
  485. {
  486. return GenericParcelPermission(user, parcel);
  487. }
  488. #endregion
  489. }
  490. }