From 4bf8a667310d74561a0535655ece8745d19d1864 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 26 May 2020 08:39:34 +0200 Subject: [PATCH] libcli:smb: Add smb_encryption_setting_translate() Add encryption enum and function to avoid confusion when reading the code. Signed-off-by: Andreas Schneider Reviewed-by: Stefan Metzmacher --- libcli/smb/smb_util.h | 1 + libcli/smb/test_util_translate.c | 19 +++++++++++++++++++ libcli/smb/util.c | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/libcli/smb/smb_util.h b/libcli/smb/smb_util.h index 15bdbe856d1..2a727db8b6f 100644 --- a/libcli/smb/smb_util.h +++ b/libcli/smb/smb_util.h @@ -51,5 +51,6 @@ NTSTATUS smb_bytes_pull_str(TALLOC_CTX *mem_ctx, char **_str, bool ucs2, size_t *_consumed); enum smb_signing_setting smb_signing_setting_translate(const char *str); +enum smb_encryption_setting smb_encryption_setting_translate(const char *str); #endif /* _SMB_UTIL_H */ diff --git a/libcli/smb/test_util_translate.c b/libcli/smb/test_util_translate.c index 4b81984affa..b300af52c09 100644 --- a/libcli/smb/test_util_translate.c +++ b/libcli/smb/test_util_translate.c @@ -46,11 +46,30 @@ static void test_smb_signing_setting_translate(void **state) } +static void test_smb_encryption_setting_translate(void **state) +{ + enum smb_encryption_setting encryption_state; + + encryption_state = smb_encryption_setting_translate("wurst"); + assert_int_equal(encryption_state, SMB_ENCRYPTION_REQUIRED); + + encryption_state = smb_encryption_setting_translate("off"); + assert_int_equal(encryption_state, SMB_ENCRYPTION_OFF); + + encryption_state = smb_encryption_setting_translate("if_required"); + assert_int_equal(encryption_state, SMB_ENCRYPTION_IF_REQUIRED); + + encryption_state = smb_encryption_setting_translate("mandatory"); + assert_int_equal(encryption_state, SMB_ENCRYPTION_REQUIRED); + +} + int main(int argc, char *argv[]) { int rc; const struct CMUnitTest tests[] = { cmocka_unit_test(test_smb_signing_setting_translate), + cmocka_unit_test(test_smb_encryption_setting_translate), }; if (argc == 2) { diff --git a/libcli/smb/util.c b/libcli/smb/util.c index da0e4db2bf3..ac2887ee5c4 100644 --- a/libcli/smb/util.c +++ b/libcli/smb/util.c @@ -448,3 +448,23 @@ enum smb_signing_setting smb_signing_setting_translate(const char *str) return signing_state; } + +/** + * @brief Translate SMB encryption settings as string to an enum. + * + * @param[in] str The string to translate. + * + * @return A corresponding enum @smb_encryption_setting tranlated from the + * string. + */ +enum smb_encryption_setting smb_encryption_setting_translate(const char *str) +{ + enum smb_encryption_setting encryption_state = SMB_ENCRYPTION_REQUIRED; + int32_t val = lpcfg_parse_enum_vals("client smb encrypt", str); + + if (val != INT32_MIN) { + encryption_state = val; + } + + return encryption_state; +}