PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ovaldi-5.9.1-src/src/probes/unix/XinetdProbe.h

#
C Header | 277 lines | 81 code | 36 blank | 160 comment | 0 complexity | d090dc68482d864e7099066bbbaa7503 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. //
  2. //
  3. //****************************************************************************************//
  4. // Copyright (c) 2002-2011, The MITRE Corporation
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without modification, are
  8. // permitted provided that the following conditions are met:
  9. //
  10. // * Redistributions of source code must retain the above copyright notice, this list
  11. // of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above copyright notice, this
  13. // list of conditions and the following disclaimer in the documentation and/or other
  14. // materials provided with the distribution.
  15. // * Neither the name of The MITRE Corporation nor the names of its contributors may be
  16. // used to endorse or promote products derived from this software without specific
  17. // prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  20. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  22. // SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  24. // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  26. // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. //****************************************************************************************//
  30. #ifndef XINETDPROBE_H
  31. #define XINETDPROBE_H
  32. #include <AbsProbe.h>
  33. #include <Common.h>
  34. #include <Item.h>
  35. #include <map>
  36. #include <Object.h>
  37. #include <ObjectEntity.h>
  38. #include <REGEX.h>
  39. #include <set>
  40. #include <string>
  41. class XinetdParam;
  42. // You will probably notice that there is no 'using namespace std' at the
  43. // top of this file, and I spelled out the namespace for all the stl types
  44. // I used below. It seems to me that people wouldn't want headers
  45. // importing types from other namespaces and possibly clashing with their
  46. // own types. So to me, 'using namespace ...' seems more appropriate for
  47. // an impl file, not a header.
  48. /**
  49. * The probe for analyzing xinetd config files.
  50. * <p>
  51. * It is written in a quick and dirty style using regexes. This means that
  52. * malformed config files will not be handled well. Either the regexes
  53. * will just not match, or will match in funny ways, giving funny results.
  54. * It makes sense perhaps, that this tool should not be used for validation,
  55. * so it should assume valid config files. But be aware of its limitations.
  56. * <p>
  57. * Also, to make this probe more tractable for the time I had to implement it,
  58. * the 'xinetd' app is not mimicked perfectly. E.g. there are rules about which
  59. * params support which operators, which are cumulative and which aren't, etc.
  60. * There is special code for handling all the special network address formats.
  61. * This probe discards all that complexity, handling all parameters uniformly.
  62. * In particular, no parameter is cumulative, all params support all operators,
  63. * all params may have multiple values, and all param values are treated as
  64. * simple strings.
  65. */
  66. class XinetdProbe : public AbsProbe {
  67. public:
  68. virtual ~XinetdProbe();
  69. static XinetdProbe* Instance();
  70. protected:
  71. virtual ItemVector* CollectItems(Object* object);
  72. virtual Item* CreateItem();
  73. private:
  74. /** Type representing a service configuration entry. */
  75. typedef std::map<std::string, std::vector<XinetdParam> > ServiceEntryMap;
  76. XinetdProbe();
  77. /**
  78. * Processes the given xinetd config file. Information about
  79. * services contained therein are stored in the 'services' param.
  80. * The defaults section is stored separately, in the given
  81. * 'defaults' param.
  82. */
  83. void ProcessConfigFile(const std::string& confFileName,
  84. StringVector& includeStack,
  85. std::vector<ServiceEntryMap>& services,
  86. ServiceEntryMap& defaults,
  87. ObjectEntity* nameEntity,
  88. ObjectEntity* protocolEntity);
  89. /**
  90. * Processes the given directory of config files. Information
  91. * from all config files is collected into the services and
  92. * defaults parameters. Only regular files, those without a
  93. * '.' in their name, and those which don't end with '~' are
  94. * processed. If there is more than one default section found,
  95. * all defaults are collected together into one big group.
  96. */
  97. void ProcessIncludeDir(const std::string& includeDir,
  98. StringVector& includeStack,
  99. std::vector<ServiceEntryMap>& services,
  100. ServiceEntryMap& defaults,
  101. ObjectEntity* nameEntity,
  102. ObjectEntity* protocolEntity);
  103. /**
  104. * Given a chunk of text containing the innards of a service description
  105. * (the stuff between '{' and '}'), finds all the params and values and
  106. * puts them into the given map. The values of duplicate parameters are
  107. * merged together into a single parameter.
  108. */
  109. void ProcessEntryParams(const std::string& entryParams, ServiceEntryMap& entryMap);
  110. /**
  111. * Combines duplicate default parameter definitions into a single parameter
  112. * definition. This is doable in the defaults entry, without looking at any
  113. * other entries. The resulting parameters will be suitable for merging into
  114. * all the actual service entries.
  115. */
  116. void NormalizeDefaults(ServiceEntryMap& defaults);
  117. /**
  118. * Merges values from the given defaults into the given service.
  119. */
  120. void MergeDefaultsIntoService(ServiceEntryMap& service, const ServiceEntryMap& defaults);
  121. /**
  122. * Given a parameter definition, and some pre-existing values for it, merge all sets
  123. * of values together such that the given param will only have one set of values,
  124. * with an operator of XinetdParam::ASSIGN. All the entries in the given vector
  125. * are removed and replaced with a single entry with the combined values. All
  126. * elements of \c param should be for the same service parameter.
  127. */
  128. void MergeParamValues(std::vector<XinetdParam>& param, const StringVector& preexistingVals = StringVector());
  129. /**
  130. * xinetd itself fills in some service params according to other param values
  131. * and info queried from the OS. This method mimics that behavior, filling
  132. * out some other params (where possible) that are missing in the service.
  133. * <p>
  134. * This method assumes the service has already been normalized (so one XinetdParam
  135. * object per param).
  136. */
  137. void FillInOtherParams(ServiceEntryMap& service);
  138. /**
  139. * Constructs an oval item from the given service. If an item could not
  140. * be created because either required service params were missing or did not
  141. * match the given object entities, NULL is returned.
  142. */
  143. Item* Service2Item(const ServiceEntryMap& service,
  144. ObjectEntity* nameEntity,
  145. ObjectEntity* protocolEntity);
  146. /**
  147. * Determines whether the given service is enabled. The rules regarding whether
  148. * or not a service is enabled are complicated. The xinetd.conf man page does
  149. * not describe this well.
  150. * <p>
  151. * An individual service entry may have a "disable" parameter. The defaults
  152. * section may have either or both of "disabled" and "enabled". Additionally,
  153. * the man page says there is a "DISABLE" flag, but from inspecting the source,
  154. * no such flag seems to exist. In the defaults section, "enabled" takes
  155. * precendence over "disabled". The man page says that the "disable" param
  156. * for a service takes precendence over an "enabled" param in the defaults.
  157. * It does not say whether "disable" in a service takes precedence over
  158. * "disabled" in the defaults. I will assume generally that disabling takes
  159. * precedence over enabling. So even though "disable = yes" in a service entry
  160. * overrides "enabled" in the defaults, I will not let "disable = no" in a
  161. * service entry override "disabled" in the defaults.
  162. */
  163. bool ResolveEnabled(const ServiceEntryMap& service);
  164. /**
  165. * Adds an entity to the given item with the given name, from the service param
  166. * of the same name from the given service.
  167. */
  168. void AddItemEntity(Item *item,
  169. const ServiceEntryMap& service,
  170. const std::string& paramName,
  171. OvalEnum::Datatype dataType=OvalEnum::DATATYPE_STRING);
  172. /**
  173. * Adds an entity to the given item with the given name, from the service param
  174. * of the given name from the given service. In this one, the entity is named
  175. * differently from the service param name.
  176. */
  177. void AddItemEntity(Item *item,
  178. const ServiceEntryMap& service,
  179. const std::string& paramName,
  180. const std::string& entityName,
  181. OvalEnum::Datatype dataType=OvalEnum::DATATYPE_STRING);
  182. /**
  183. * Reads the contents of the given file into the given string.
  184. */
  185. void ReadFileToString(const std::string& fileName, std::string &contents);
  186. /**
  187. * \e Modifies the given filename by transforming it into canonical
  188. * form (resolving symlinks, making it absolute, normalizing . and .., etc.)
  189. * This allows us to detect circular includes even if the same file
  190. * is referred to via different paths.
  191. */
  192. void CanonicalizeFileName(std::string& fileName);
  193. REGEX re;
  194. std::set<std::string> allowedFlags;
  195. std::set<std::string> allowedTypes;
  196. std::set<std::string> allowedSocketTypes;
  197. static XinetdProbe* instance;
  198. };
  199. /**
  200. * Represents a single parameter definition in a service entry. The
  201. * entry has a name, an operator, and one or more values. The name is
  202. * not stored in this class because it is used as a map key. Since
  203. * name is stored as a map key, I didn't think it necessary to store it
  204. * redundantly here. Multiple XinetdParam objects may be stored for a
  205. * given parameter. This is because it is legal for some parameters to
  206. * be defined multiple times in an xinetd config file. In this case,
  207. * the values are combined according to the operators for each definition.
  208. */
  209. class XinetdParam {
  210. public:
  211. enum Op {
  212. ASSIGN,
  213. APPEND,
  214. REMOVE
  215. };
  216. XinetdParam(); //this ctor required when creating new map entries, e.g. map["foo"] = someParam;
  217. XinetdParam(const XinetdParam& param);
  218. XinetdParam(Op op, const std::string& paramValue);
  219. XinetdParam(const std::string& opStr, const StringVector& paramValues);
  220. XinetdParam(Op op, const StringVector& paramValues);
  221. /** Coverts the given string to an Op constant. */
  222. Op string2Op(const std::string& opStr) const;
  223. /**
  224. * Makes a string representation of the param. This will include all
  225. * values, delimited by spaces.
  226. */
  227. std::string str() const;
  228. /**
  229. * This is used for sorting the params by operator. When applying operators
  230. * to obtain a final list of values, it's important to apply them in the proper
  231. * order.
  232. */
  233. bool operator<(const XinetdParam& other) const;
  234. /** cause when you create a copy ctor, you're sposed to override =... right? */
  235. XinetdParam& operator=(const XinetdParam& other);
  236. /** Looks for the given value for this param, returns true if found, false if not. */
  237. bool IncludesValue(const std::string& value) const;
  238. StringVector paramValues;
  239. Op op;
  240. };
  241. #endif