blob: d6df4729160e9263bad8bd4f0be2d357c1d3c6a8 [file] [log] [blame]
Lutz Justen51631092019-07-05 09:19:58 +02001// 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 Andradea0cbde72020-04-01 15:40:10 +02005#ifndef KERBEROS_CONFIG_PARSER_H_
6#define KERBEROS_CONFIG_PARSER_H_
Lutz Justen51631092019-07-05 09:19:58 +02007
8#include <string>
Lutz Justen51631092019-07-05 09:19:58 +02009
Felipe Andradef097d252020-04-08 15:10:50 +020010#include <base/containers/flat_set.h>
Lutz Justen51631092019-07-05 09:19:58 +020011#include <base/macros.h>
12
Felipe Andrade66aaf6b2020-03-24 13:05:57 +010013#include "kerberos/kerberos_metrics.h"
Lutz Justen51631092019-07-05 09:19:58 +020014#include "kerberos/proto_bindings/kerberos_service.pb.h"
15
16namespace kerberos {
17
Felipe Andradea0cbde72020-04-01 15:40:10 +020018// 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 Justen51631092019-07-05 09:19:58 +020021// reasons. Also performs basic syntax checks and returns more useful error
22// information than "You screwed up your config, screw you!"
Felipe Andradea0cbde72020-04-01 15:40:10 +020023class ConfigParser {
Lutz Justen51631092019-07-05 09:19:58 +020024 public:
Felipe Andradea0cbde72020-04-01 15:40:10 +020025 ConfigParser();
Qijiang Fan6bc59e12020-11-11 02:51:06 +090026 ConfigParser(const ConfigParser&) = delete;
27 ConfigParser& operator=(const ConfigParser&) = delete;
Lutz Justen51631092019-07-05 09:19:58 +020028
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 Andrade90cb84e2020-04-07 20:28:33 +020036 // 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 Andrade66aaf6b2020-03-24 13:05:57 +010040 // 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 Andrade90cb84e2020-04-07 20:28:33 +020047 bool GetEncryptionTypes(const std::string& krb5conf,
48 KerberosEncryptionTypes* encryption_types) const;
Felipe Andrade66aaf6b2020-03-24 13:05:57 +010049
Lutz Justen51631092019-07-05 09:19:58 +020050 private:
Felipe Andrade66aaf6b2020-03-24 13:05:57 +010051 // 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 Justen51631092019-07-05 09:19:58 +020058 bool IsKeySupported(const std::string& key,
59 const std::string& section,
60 int group_level) const;
61
Felipe Andradef097d252020-04-08 15:10:50 +020062 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 Justen51631092019-07-05 09:19:58 +020069};
70
71} // namespace kerberos
72
Felipe Andradea0cbde72020-04-01 15:40:10 +020073#endif // KERBEROS_CONFIG_PARSER_H_