| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/network/policy_util.h" | 5 #include "chromeos/network/policy_util.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 // This fake credential contains a random postfix which is extremely unlikely to | 28 // This fake credential contains a random postfix which is extremely unlikely to |
| 29 // be used by any user. | 29 // be used by any user. |
| 30 const char kFakeCredential[] = "FAKE_CREDENTIAL_VPaJDV9x"; | 30 const char kFakeCredential[] = "FAKE_CREDENTIAL_VPaJDV9x"; |
| 31 | 31 |
| 32 | 32 |
| 33 // Removes all kFakeCredential values from sensitive fields (determined by | 33 // Removes all kFakeCredential values from sensitive fields (determined by |
| 34 // onc::FieldIsCredential) of |onc_object|. | 34 // onc::FieldIsCredential) of |onc_object|. |
| 35 void RemoveFakeCredentials( | 35 void RemoveFakeCredentials( |
| 36 const onc::OncValueSignature& signature, | 36 const onc::OncValueSignature& signature, |
| 37 base::DictionaryValue* onc_object) { | 37 base::DictionaryValue* onc_object) { |
| 38 base::DictionaryValue::Iterator it(*onc_object); | 38 std::vector<std::string> entries_to_remove; |
| 39 while (!it.IsAtEnd()) { | 39 for (base::DictionaryValue::Iterator it(*onc_object); !it.IsAtEnd(); |
| 40 base::Value* value = NULL; | 40 it.Advance()) { |
| 41 base::Value* value = nullptr; |
| 41 std::string field_name = it.key(); | 42 std::string field_name = it.key(); |
| 42 // We need the non-const entry to remove nested values but DictionaryValue | 43 // We need the non-const entry to remove nested values but DictionaryValue |
| 43 // has no non-const iterator. | 44 // has no non-const iterator. |
| 44 onc_object->GetWithoutPathExpansion(field_name, &value); | 45 onc_object->GetWithoutPathExpansion(field_name, &value); |
| 45 // Advance before delete. | |
| 46 it.Advance(); | |
| 47 | 46 |
| 48 // If |value| is a dictionary, recurse. | 47 // If |value| is a dictionary, recurse. |
| 49 base::DictionaryValue* nested_object = NULL; | 48 base::DictionaryValue* nested_object = nullptr; |
| 50 if (value->GetAsDictionary(&nested_object)) { | 49 if (value->GetAsDictionary(&nested_object)) { |
| 51 const onc::OncFieldSignature* field_signature = | 50 const onc::OncFieldSignature* field_signature = |
| 52 onc::GetFieldSignature(signature, field_name); | 51 onc::GetFieldSignature(signature, field_name); |
| 53 if (field_signature) | 52 if (field_signature) |
| 54 RemoveFakeCredentials(*field_signature->value_signature, nested_object); | 53 RemoveFakeCredentials(*field_signature->value_signature, nested_object); |
| 55 else | 54 else |
| 56 LOG(ERROR) << "ONC has unrecognized field: " << field_name; | 55 LOG(ERROR) << "ONC has unrecognized field: " << field_name; |
| 57 continue; | 56 continue; |
| 58 } | 57 } |
| 59 | 58 |
| 60 // If |value| is a string, check if it is a fake credential. | 59 // If |value| is a string, check if it is a fake credential. |
| 61 std::string string_value; | 60 std::string string_value; |
| 62 if (value->GetAsString(&string_value) && | 61 if (value->GetAsString(&string_value) && |
| 63 onc::FieldIsCredential(signature, field_name)) { | 62 onc::FieldIsCredential(signature, field_name)) { |
| 64 if (string_value == kFakeCredential) { | 63 if (string_value == kFakeCredential) { |
| 65 // The value wasn't modified by the UI, thus we remove the field to keep | 64 // The value wasn't modified by the UI, thus we remove the field to keep |
| 66 // the existing value that is stored in Shill. | 65 // the existing value that is stored in Shill. |
| 67 onc_object->RemoveWithoutPathExpansion(field_name, NULL); | 66 entries_to_remove.push_back(field_name); |
| 68 } | 67 } |
| 69 // Otherwise, the value is set and modified by the UI, thus we keep that | 68 // Otherwise, the value is set and modified by the UI, thus we keep that |
| 70 // value to overwrite whatever is stored in Shill. | 69 // value to overwrite whatever is stored in Shill. |
| 71 } | 70 } |
| 72 } | 71 } |
| 72 for (auto field_name : entries_to_remove) |
| 73 onc_object->RemoveWithoutPathExpansion(field_name, nullptr); |
| 73 } | 74 } |
| 74 | 75 |
| 75 // Returns true if |policy| matches |actual_network|, which must be part of a | 76 // Returns true if |policy| matches |actual_network|, which must be part of a |
| 76 // ONC NetworkConfiguration. This should be the only such matching function | 77 // ONC NetworkConfiguration. This should be the only such matching function |
| 77 // within Chrome. Shill does such matching in several functions for network | 78 // within Chrome. Shill does such matching in several functions for network |
| 78 // identification. For compatibility, we currently should stick to Shill's | 79 // identification. For compatibility, we currently should stick to Shill's |
| 79 // matching behavior. | 80 // matching behavior. |
| 80 bool IsPolicyMatching(const base::DictionaryValue& policy, | 81 bool IsPolicyMatching(const base::DictionaryValue& policy, |
| 81 const base::DictionaryValue& actual_network) { | 82 const base::DictionaryValue& actual_network) { |
| 82 std::string policy_type; | 83 std::string policy_type; |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 for (auto it = policies.begin(); it != policies.end(); ++it) { | 402 for (auto it = policies.begin(); it != policies.end(); ++it) { |
| 402 if (IsPolicyMatching(*it->second, actual_network)) | 403 if (IsPolicyMatching(*it->second, actual_network)) |
| 403 return it->second.get(); | 404 return it->second.get(); |
| 404 } | 405 } |
| 405 return NULL; | 406 return NULL; |
| 406 } | 407 } |
| 407 | 408 |
| 408 } // namespace policy_util | 409 } // namespace policy_util |
| 409 | 410 |
| 410 } // namespace chromeos | 411 } // namespace chromeos |
| OLD | NEW |