Home | History | Annotate | Download | only in onc
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROMEOS_NETWORK_ONC_ONC_VALIDATOR_H_
      6 #define CHROMEOS_NETWORK_ONC_ONC_VALIDATOR_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "chromeos/chromeos_export.h"
     13 #include "chromeos/network/onc/onc_mapper.h"
     14 #include "components/onc/onc_constants.h"
     15 
     16 namespace base {
     17 class DictionaryValue;
     18 class Value;
     19 }
     20 
     21 namespace chromeos {
     22 namespace onc {
     23 
     24 struct OncValueSignature;
     25 
     26 // The ONC Validator searches for the following invalid cases:
     27 // - a value is found that has the wrong type or is not expected according to
     28 //   the ONC spec (always an error)
     29 //
     30 // - a field name is found that is not part of the signature
     31 //   (controlled by flag |error_on_unknown_field|)
     32 //
     33 // - a kRecommended array contains a field name that is not part of the
     34 //   enclosing object's signature or if that field is dictionary typed
     35 //   (controlled by flag |error_on_wrong_recommended|)
     36 //
     37 // - |managed_onc| is false and a field with name kRecommended is found
     38 //   (always ignored)
     39 //
     40 // - a required field is missing (controlled by flag |error_on_missing_field|)
     41 //
     42 // If one of these invalid cases occurs and, in case of a controlling flag, that
     43 // flag is true, then it is an error. The function ValidateAndRepairObject sets
     44 // |result| to INVALID and returns NULL.
     45 //
     46 // Otherwise, a DeepCopy of the validated object is created, which contains
     47 // all but the invalid fields and values.
     48 //
     49 // If one of the invalid cases occurs and the controlling flag is false, then
     50 // it is a warning. The function ValidateAndRepairObject sets |result| to
     51 // VALID_WITH_WARNINGS and returns the repaired copy.
     52 //
     53 // If no error occurred, |result| is set to VALID and an exact DeepCopy is
     54 // returned.
     55 class CHROMEOS_EXPORT Validator : public Mapper {
     56  public:
     57   enum Result {
     58     VALID,
     59     VALID_WITH_WARNINGS,
     60     INVALID
     61   };
     62 
     63   // See the class comment.
     64   Validator(bool error_on_unknown_field,
     65             bool error_on_wrong_recommended,
     66             bool error_on_missing_field,
     67             bool managed_onc);
     68 
     69   virtual ~Validator();
     70 
     71   // Sets the ONC source to |source|. If not set, defaults to ONC_SOURCE_NONE.
     72   // If the source is set to ONC_SOURCE_DEVICE_POLICY, validation additionally
     73   // checks:
     74   // - only the network types Wifi and Ethernet are allowed
     75   // - client certificate patterns are disallowed
     76   void SetOncSource(::onc::ONCSource source) {
     77     onc_source_ = source;
     78   }
     79 
     80   // Validate the given |onc_object| according to |object_signature|. The
     81   // |object_signature| has to be a pointer to one of the signatures in
     82   // |onc_signature.h|. If an error is found, the function returns NULL and sets
     83   // |result| to INVALID. If possible (no error encountered) a DeepCopy is
     84   // created that contains all but the invalid fields and values and returns
     85   // this "repaired" object. That means, if not handled as an error, then the
     86   // following are dropped from the copy:
     87   // - unknown fields
     88   // - invalid field names in kRecommended arrays
     89   // - kRecommended fields in an unmanaged ONC
     90   // If any of these cases occurred, sets |result| to VALID_WITH_WARNINGS and
     91   // otherwise to VALID.
     92   // For details, see the class comment.
     93   scoped_ptr<base::DictionaryValue> ValidateAndRepairObject(
     94       const OncValueSignature* object_signature,
     95       const base::DictionaryValue& onc_object,
     96       Result* result);
     97 
     98  private:
     99   // Overridden from Mapper:
    100   // Compare |onc_value|s type with |onc_type| and validate/repair according to
    101   // |signature|. On error returns NULL.
    102   virtual scoped_ptr<base::Value> MapValue(const OncValueSignature& signature,
    103                                            const base::Value& onc_value,
    104                                            bool* error) OVERRIDE;
    105 
    106   // Dispatch to the right validation function according to
    107   // |signature|. Iterates over all fields and recursively validates/repairs
    108   // these. All valid fields are added to the result dictionary. Returns the
    109   // repaired dictionary. Only on error returns NULL.
    110   virtual scoped_ptr<base::DictionaryValue> MapObject(
    111       const OncValueSignature& signature,
    112       const base::DictionaryValue& onc_object,
    113       bool* error) OVERRIDE;
    114 
    115   // Pushes/pops the |field_name| to |path_|, otherwise like |Mapper::MapField|.
    116   virtual scoped_ptr<base::Value> MapField(
    117       const std::string& field_name,
    118       const OncValueSignature& object_signature,
    119       const base::Value& onc_value,
    120       bool* found_unknown_field,
    121       bool* error) OVERRIDE;
    122 
    123   // Ignores nested errors in NetworkConfigurations and Certificates, otherwise
    124   // like |Mapper::MapArray|.
    125   virtual scoped_ptr<base::ListValue> MapArray(
    126       const OncValueSignature& array_signature,
    127       const base::ListValue& onc_array,
    128       bool* nested_error) OVERRIDE;
    129 
    130   // Pushes/pops the index to |path_|, otherwise like |Mapper::MapEntry|.
    131   virtual scoped_ptr<base::Value> MapEntry(int index,
    132                                            const OncValueSignature& signature,
    133                                            const base::Value& onc_value,
    134                                            bool* error) OVERRIDE;
    135 
    136   // This is the default validation of objects/dictionaries. Validates
    137   // |onc_object| according to |object_signature|. |result| must point to a
    138   // dictionary into which the repaired fields are written.
    139   bool ValidateObjectDefault(const OncValueSignature& object_signature,
    140                              const base::DictionaryValue& onc_object,
    141                              base::DictionaryValue* result);
    142 
    143   // Validates/repairs the kRecommended array in |result| according to
    144   // |object_signature| of the enclosing object.
    145   bool ValidateRecommendedField(const OncValueSignature& object_signature,
    146                                 base::DictionaryValue* result);
    147 
    148   bool ValidateToplevelConfiguration(base::DictionaryValue* result);
    149   bool ValidateNetworkConfiguration(base::DictionaryValue* result);
    150   bool ValidateEthernet(base::DictionaryValue* result);
    151   bool ValidateIPConfig(base::DictionaryValue* result);
    152   bool ValidateWiFi(base::DictionaryValue* result);
    153   bool ValidateVPN(base::DictionaryValue* result);
    154   bool ValidateIPsec(base::DictionaryValue* result);
    155   bool ValidateOpenVPN(base::DictionaryValue* result);
    156   bool ValidateVerifyX509(base::DictionaryValue* result);
    157   bool ValidateCertificatePattern(base::DictionaryValue* result);
    158   bool ValidateProxySettings(base::DictionaryValue* result);
    159   bool ValidateProxyLocation(base::DictionaryValue* result);
    160   bool ValidateEAP(base::DictionaryValue* result);
    161   bool ValidateCertificate(base::DictionaryValue* result);
    162 
    163   bool FieldExistsAndHasNoValidValue(const base::DictionaryValue& object,
    164                                      const std::string &field_name,
    165                                      const char** valid_values);
    166 
    167   bool FieldExistsAndIsNotInRange(const base::DictionaryValue& object,
    168                                   const std::string &field_name,
    169                                   int lower_bound,
    170                                   int upper_bound);
    171 
    172   bool FieldExistsAndIsEmpty(const base::DictionaryValue& object,
    173                              const std::string& field_name);
    174 
    175   bool RequireField(const base::DictionaryValue& dict, const std::string& key);
    176 
    177   // Prohibit certificate patterns for device policy ONC so that an unmanaged
    178   // user won't have a certificate presented for them involuntarily.
    179   bool IsCertPatternInDevicePolicy(const std::string& cert_type);
    180 
    181   // Prohibit global network configuration in user ONC imports.
    182   bool IsGlobalNetworkConfigInUserImport(
    183       const base::DictionaryValue& onc_object);
    184 
    185   std::string MessageHeader();
    186 
    187   const bool error_on_unknown_field_;
    188   const bool error_on_wrong_recommended_;
    189   const bool error_on_missing_field_;
    190   const bool managed_onc_;
    191 
    192   ::onc::ONCSource onc_source_;
    193 
    194   // The path of field names and indices to the current value. Indices
    195   // are stored as strings in decimal notation.
    196   std::vector<std::string> path_;
    197 
    198   // Tracks if an error or warning occurred within validation initiated by
    199   // function ValidateAndRepairObject.
    200   bool error_or_warning_found_;
    201 
    202   DISALLOW_COPY_AND_ASSIGN(Validator);
    203 };
    204 
    205 }  // namespace onc
    206 }  // namespace chromeos
    207 
    208 #endif  // CHROMEOS_NETWORK_ONC_ONC_VALIDATOR_H_
    209