channel: Refactor set_user_access option processing

Reduce code duplication by extracting option names, types,
and value ranges into a separate structure, and rewriting
the option parsing code without mixing it with the data.

Signed-off-by: Alexander Amelkin <alexander@amelkin.msk.ru>
This commit is contained in:
Alexander Amelkin 2020-06-10 00:54:15 +03:00 committed by Alexander Amelkin
parent 6e037d6bfb
commit e3fc775d26
3 changed files with 62 additions and 29 deletions

View File

@ -74,6 +74,7 @@
#define IPMI_SESSION_PRIV_OPERATOR 0x3 #define IPMI_SESSION_PRIV_OPERATOR 0x3
#define IPMI_SESSION_PRIV_ADMIN 0x4 #define IPMI_SESSION_PRIV_ADMIN 0x4
#define IPMI_SESSION_PRIV_OEM 0x5 #define IPMI_SESSION_PRIV_OEM 0x5
#define IPMI_SESSION_PRIV_NOACCESS 0xF
#define IPMI_SET_IN_PROGRESS_SET_COMPLETE 0x00 #define IPMI_SET_IN_PROGRESS_SET_COMPLETE 0x00
#define IPMI_SET_IN_PROGRESS_IN_PROGRESS 0x01 #define IPMI_SET_IN_PROGRESS_IN_PROGRESS 0x01

View File

@ -803,8 +803,26 @@ ipmi_set_user_access(struct ipmi_intf *intf, int argc, char **argv)
int ccode = 0; int ccode = 0;
int i = 0; int i = 0;
uint8_t channel = 0; uint8_t channel = 0;
uint8_t priv = 0;
uint8_t user_id = 0; uint8_t user_id = 0;
struct {
const char *option;
enum {
UA_INTEGER, /* direct integer value */
UA_BOOLEAN, /* off/disable = false, on/enable = true */
UA_BOOLEAN_INVERSE /* off/disable = true, on/enable = false */
} type;
uint8_t *val;
uint8_t min; /* minimum value for UA_INTEGER options */
uint8_t max; /* maximum value for UA_INTEGER options */
} options[] = {
{ "callin=", UA_BOOLEAN_INVERSE, &user_access.callin_callback, 0, 0},
{ "link=", UA_BOOLEAN, &user_access.link_auth, 0, 0},
{ "ipmi=", UA_BOOLEAN, &user_access.ipmi_messaging, 0, 0},
{ "privilege=", UA_INTEGER, &user_access.privilege_limit
, IPMI_SESSION_PRIV_CALLBACK
, IPMI_SESSION_PRIV_NOACCESS },
};
if (argc > 0 && strcmp(argv[0], "help") == 0) { if (argc > 0 && strcmp(argv[0], "help") == 0) {
printf_channel_usage(); printf_channel_usage();
return 0; return 0;
@ -827,33 +845,47 @@ ipmi_set_user_access(struct ipmi_intf *intf, int argc, char **argv)
return (-1); return (-1);
} }
for (i = 2; i < argc; i ++) { for (i = 2; i < argc; i ++) {
if (strcmp(argv[i], "callin=") == 0) { size_t j;
if (strcmp(argv[i] + strlen("callin="), "off") == 0) { for (j = 0; j < ARRAY_SIZE(options); ++j) {
user_access.callin_callback = 1; const char *opt = argv[i];
} else { const int optlen = strlen(options[j].option);
user_access.callin_callback = 0; if (!strncmp(opt, options[j].option, optlen)) {
const char *optval = opt + optlen;
uint16_t val;
if (UA_INTEGER != options[j].type) {
bool boolval = (UA_BOOLEAN_INVERSE == options[j].type)
? false
: true;
*options[j].val = boolval;
if (!strcmp(optval, "off")
|| !strcmp(optval, "disable")
|| !strcmp(optval, "no")
)
{
boolval = !boolval;
} }
} else if (strcmp(argv[i], "link=") == 0) { } else if (UINT8_MAX
if (strcmp(argv[i] + strlen("link="), "off") == 0) { != (val = str2val(optval, ipmi_privlvl_vals)))
user_access.link_auth = 0; {
} else { *options[j].val = (uint8_t)val;
user_access.link_auth = 1; } else if (str2uchar(optval, options[j].val)) {
} lprintf(LOG_ERR
} else if (strcmp(argv[i], "ipmi=") == 0) { , "Numeric [%hhu-%hhu] value expected, "
if (strcmp(argv[i] + strlen("ipmi="), "off") == 0) { "but '%s' given."
user_access.ipmi_messaging = 0; , options[j].min
} else { , options[j].max
user_access.ipmi_messaging = 1; , optval);
}
} else if (strcmp(argv[i], "privilege=") == 0) {
if (str2uchar(argv[i] + 10, &priv) != 0) {
lprintf(LOG_ERR,
"Numeric value expected, but '%s' given.",
argv[i] + 10);
return (-1); return (-1);
} }
user_access.privilege_limit = priv; lprintf(LOG_DEBUG
} else { , "Option %s=%hhu"
, options[j].option
, *options[j].val);
break;
}
}
if (ARRAY_SIZE(options) == j) {
lprintf(LOG_ERR, "Invalid option: %s\n", argv[i]); lprintf(LOG_ERR, "Invalid option: %s\n", argv[i]);
return (-1); return (-1);
} }

View File

@ -1031,8 +1031,8 @@ const struct valstr ipmi_privlvl_vals[] = {
{ IPMI_SESSION_PRIV_OPERATOR, "OPERATOR" }, { IPMI_SESSION_PRIV_OPERATOR, "OPERATOR" },
{ IPMI_SESSION_PRIV_ADMIN, "ADMINISTRATOR" }, { IPMI_SESSION_PRIV_ADMIN, "ADMINISTRATOR" },
{ IPMI_SESSION_PRIV_OEM, "OEM" }, { IPMI_SESSION_PRIV_OEM, "OEM" },
{ 0xF, "NO ACCESS" }, { IPMI_SESSION_PRIV_NOACCESS, "NO ACCESS" },
{ 0xFF, NULL }, { UINT8_MAX, NULL },
}; };