none.ck 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*++
  2. Copyright (c) 2017 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. none.ck
  9. Abstract:
  10. This module implements no-op containment support (ie no containment).
  11. Author:
  12. Evan Green 1-Jun-2017
  13. Environment:
  14. Chalk
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. from santa.config import config;
  20. from santa.containment import Containment, ContainmentError;
  21. from santa.file import chdir, mkdir, path, rmtree;
  22. //
  23. // --------------------------------------------------------------------- Macros
  24. //
  25. //
  26. // ---------------------------------------------------------------- Definitions
  27. //
  28. //
  29. // ------------------------------------------------------ Data Type Definitions
  30. //
  31. //
  32. // ----------------------------------------------- Internal Function Prototypes
  33. //
  34. //
  35. // -------------------------------------------------------------------- Globals
  36. //
  37. //
  38. // ------------------------------------------------------------------ Functions
  39. //
  40. class NoneContainment is Containment {
  41. var _parameters;
  42. function
  43. create (
  44. parameters
  45. )
  46. /*++
  47. Routine Description:
  48. This routine creates a new container and initializes this instance's
  49. internal variables to represent it.
  50. Arguments:
  51. parameters - Supplies a dictionary of creation parameters.
  52. Return Value:
  53. None.
  54. --*/
  55. {
  56. var rootpath;
  57. _parameters = parameters.copy();
  58. _parameters.type = "none";
  59. try {
  60. rootpath = parameters.path;
  61. } except KeyError {
  62. Core.raise(ContainmentError("Required parameter is missing"));
  63. }
  64. if (rootpath && (rootpath != "/")) {
  65. mkdir(rootpath);
  66. }
  67. if (config.getKey("core.verbose")) {
  68. Core.print("Created container at %s" % rootpath);
  69. }
  70. return;
  71. }
  72. function
  73. destroy (
  74. )
  75. /*++
  76. Routine Description:
  77. This routine destroys the container represented by this instance.
  78. Arguments:
  79. None.
  80. Return Value:
  81. None.
  82. --*/
  83. {
  84. var rootpath = _parameters.path;
  85. var verbose = config.getKey("core.verbose");
  86. if (verbose) {
  87. Core.print("Destroying container at %s" % rootpath);
  88. }
  89. rmtree(rootpath);
  90. _parameters = null;
  91. if (verbose) {
  92. Core.print("Destroyed container at %s" % rootpath);
  93. }
  94. return;
  95. }
  96. function
  97. load (
  98. parameters
  99. )
  100. /*++
  101. Routine Description:
  102. This routine initializes this instance to reflect the container
  103. identified by the given parameters.
  104. Arguments:
  105. parameters - Supplies a dictionary of parameters.
  106. Return Value:
  107. None.
  108. --*/
  109. {
  110. _parameters = parameters;
  111. try {
  112. parameters.path;
  113. } except KeyError {
  114. Core.raise(ContainmentError("Required parameter is missing"));
  115. }
  116. return;
  117. }
  118. function
  119. save (
  120. )
  121. /*++
  122. Routine Description:
  123. This routine returns the dictionary of state and identification needed
  124. to restore information about this container by other instances of this
  125. class.
  126. Arguments:
  127. None.
  128. Return Value:
  129. Returns a dictionary of parameters to save describing this instance.
  130. --*/
  131. {
  132. return _parameters;
  133. }
  134. function
  135. enter (
  136. parameters
  137. )
  138. /*++
  139. Routine Description:
  140. This routine enters the given container environment.
  141. Arguments:
  142. parameters - Supplies an optional set of additional parameters
  143. specific to this entry into the environment.
  144. Return Value:
  145. None. Upon return, the current execution environment will be confined
  146. to the container.
  147. --*/
  148. {
  149. var path = _parameters.path;
  150. chdir(path);
  151. if (config.getKey("core.verbose")) {
  152. Core.print("Changed working directory to container at %s" % path);
  153. }
  154. }
  155. function
  156. outerPath (
  157. filepath
  158. )
  159. /*++
  160. Routine Description:
  161. This routine translates from a path within the container to a path
  162. outside of the container.
  163. Arguments:
  164. filepath - Supplies the path rooted from within the container.
  165. Return Value:
  166. Returns the path to the file from the perspective of an application
  167. not executing within the container.
  168. --*/
  169. {
  170. var rootpath = path(_parameters.path);
  171. if (filepath == "/") {
  172. return rootpath;
  173. }
  174. if (!rootpath || (rootpath == "/")) {
  175. return filepath;
  176. }
  177. //
  178. // Slice off the drive letter if this is an absolute Windows path.
  179. //
  180. if (filepath[1..2] == ":") {
  181. filepath = filepath[2...-1];
  182. }
  183. return "/".join([rootpath, filepath]);
  184. }
  185. function
  186. innerPath (
  187. filepath
  188. )
  189. /*++
  190. Routine Description:
  191. This routine translates from a path outside the container to a path
  192. within of the container.
  193. Arguments:
  194. filepath - Supplies the path rooted from outside the container.
  195. Return Value:
  196. Returns the path to the file from the perspective of an application
  197. executing within the container.
  198. --*/
  199. {
  200. var rootpath = _parameters.path;
  201. if (!rootpath || (rootpath == "/")) {
  202. return filepath;
  203. }
  204. if (!filepath.startsWith(rootpath)) {
  205. rootpath = path(rootpath);
  206. if (!filepath.startsWith(rootpath)) {
  207. Core.raise(ValueError("Path '%s' does not start in container "
  208. "rooted at '%s'" % [filepath, rootpath]));
  209. }
  210. }
  211. //
  212. // The "none" containment doesn't change the root, so the path doesn't
  213. // change.
  214. //
  215. return filepath;
  216. }
  217. }
  218. //
  219. // Set the variables needed so that the module loader can enumerate this class.
  220. //
  221. var description = "No containment.";
  222. var containment = NoneContainment;
  223. //
  224. // --------------------------------------------------------- Internal Functions
  225. //