Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS 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 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 5 | #ifndef KERBEROS_CONFIG_PARSER_H_ |
| 6 | #define KERBEROS_CONFIG_PARSER_H_ |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 7 | |
| 8 | #include <string> |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 9 | |
Felipe Andrade | f097d25 | 2020-04-08 15:10:50 +0200 | [diff] [blame] | 10 | #include <base/containers/flat_set.h> |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 11 | #include <base/macros.h> |
| 12 | |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 13 | #include "kerberos/kerberos_metrics.h" |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 14 | #include "kerberos/proto_bindings/kerberos_service.pb.h" |
| 15 | |
| 16 | namespace kerberos { |
| 17 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 18 | // Parses the Kerberos configuration for either validation or encryption types |
| 19 | // retrieval. During Validation, verifies that only whitelisted configuration |
| 20 | // options are used. The Kerberos daemon does not allow all options for security |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 21 | // reasons. Also performs basic syntax checks and returns more useful error |
| 22 | // information than "You screwed up your config, screw you!" |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 23 | class ConfigParser { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 24 | public: |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 25 | ConfigParser(); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 26 | ConfigParser(const ConfigParser&) = delete; |
| 27 | ConfigParser& operator=(const ConfigParser&) = delete; |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 28 | |
| 29 | // Checks the Kerberos configuration |krb5conf|. If the config cannot be |
| 30 | // parsed or a non-whitelisted option is used, returns a message with proper |
| 31 | // error code and the 0-based line index where the error occurred. If the |
| 32 | // config was validated successfully, returns a message with code set to |
| 33 | // |CONFIG_ERROR_NONE|. |
| 34 | ConfigErrorInfo Validate(const std::string& krb5conf) const; |
| 35 | |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 36 | // Retrieves the encryption types allowed in |krb5conf| and returns whether |
| 37 | // the operation was successful or not. It should fail only if the config is |
| 38 | // invalid. Encryption types can be specified in three different fields. If |
| 39 | // any of these fields is not specified, the default value for the |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 40 | // corresponding field in krb5.conf ('all') will be used. The union of the |
| 41 | // three provided lists will be taken into consideration and mapped into one |
| 42 | // of the following comprehensive disjoint groups: |
| 43 | // * 'All': contains at least one AES type and at least one type from another |
| 44 | // encryption family |
| 45 | // * 'Strong': contains only AES encryption types (at least one of them) |
| 46 | // * 'Legacy': contains no AES encryption types |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 47 | bool GetEncryptionTypes(const std::string& krb5conf, |
| 48 | KerberosEncryptionTypes* encryption_types) const; |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 49 | |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 50 | private: |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 51 | // Internal method with common parsing features, used by |Validate(krb5conf)| |
| 52 | // and |GetEncryptionTypes(krb5conf)|. Returns both the ConfigErrorInfo and |
| 53 | // KerberosEncryptionTypes for the given config. The last value is meaningful |
| 54 | // only if the config is valid. |
| 55 | ConfigErrorInfo ParseConfig(const std::string& krb5conf, |
| 56 | KerberosEncryptionTypes* encryption_types) const; |
| 57 | |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 58 | bool IsKeySupported(const std::string& key, |
| 59 | const std::string& section, |
| 60 | int group_level) const; |
| 61 | |
Felipe Andrade | f097d25 | 2020-04-08 15:10:50 +0200 | [diff] [blame] | 62 | using StringSet = base::flat_set<std::string>; |
| 63 | const StringSet libdefaults_whitelist_; |
| 64 | const StringSet realms_whitelist_; |
| 65 | const StringSet section_whitelist_; |
| 66 | const StringSet enctypes_fields_; |
| 67 | const StringSet weak_enctypes_; |
| 68 | const StringSet strong_enctypes_; |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace kerberos |
| 72 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 73 | #endif // KERBEROS_CONFIG_PARSER_H_ |