libnm-core: add _nm_setting_secret_flags_valid() helper

Secret-flags are flags, but most combinations don't actually make sense
and maybe should be rejected. Anyway, that is not done, and most places
just check that there are no unknown flags set.

Add _nm_setting_secret_flags_valid() to perform the check at one place
instead of having the implementation at various places.
This commit is contained in:
Thomas Haller 2018-12-29 21:23:09 +01:00
parent 038d509695
commit a5b20ba211
3 changed files with 17 additions and 6 deletions

View file

@ -121,11 +121,21 @@
*/
#define NM_SETTING_COMPARE_FLAG_NONE ((NMSettingCompareFlags) 0)
/*****************************************************************************/
#define NM_SETTING_SECRET_FLAGS_ALL \
(NM_SETTING_SECRET_FLAG_NONE | \
NM_SETTING_SECRET_FLAG_AGENT_OWNED | \
NM_SETTING_SECRET_FLAG_NOT_SAVED | \
NM_SETTING_SECRET_FLAG_NOT_REQUIRED)
((NMSettingSecretFlags) ( NM_SETTING_SECRET_FLAG_NONE \
| NM_SETTING_SECRET_FLAG_AGENT_OWNED \
| NM_SETTING_SECRET_FLAG_NOT_SAVED \
| NM_SETTING_SECRET_FLAG_NOT_REQUIRED))
static inline gboolean
_nm_setting_secret_flags_valid (NMSettingSecretFlags flags)
{
return !NM_FLAGS_ANY (flags, ~NM_SETTING_SECRET_FLAGS_ALL);
}
/*****************************************************************************/
typedef enum { /*< skip >*/
NM_SETTING_PARSE_FLAGS_NONE = 0,

View file

@ -730,7 +730,8 @@ get_secret_flags (NMSetting *setting,
}
i64 = _nm_utils_ascii_str_to_int64 (flags_val, 10, 0, NM_SETTING_SECRET_FLAGS_ALL, -1);
if (i64 == -1) {
if ( i64 == -1
|| !_nm_setting_secret_flags_valid (i64)) {
/* The flags keys is set to an unexpected value. That is a configuration
* error. Note that keys named "*-flags" are reserved for secrets. The user
* must not use this for anything but secret flags. Hence, we cannot fail

View file

@ -2159,7 +2159,7 @@ nm_setting_set_secret_flags (NMSetting *setting,
{
g_return_val_if_fail (NM_IS_SETTING (setting), FALSE);
g_return_val_if_fail (secret_name != NULL, FALSE);
g_return_val_if_fail (flags <= NM_SETTING_SECRET_FLAGS_ALL, FALSE);
g_return_val_if_fail (_nm_setting_secret_flags_valid (flags), FALSE);
return NM_SETTING_GET_CLASS (setting)->set_secret_flags (setting, secret_name, flags, error);
}