Create ask_password() and re-use code

Commit add function ask_password() which leads to code re-usability and shedding
couple lines of otherwise redundant code.
This commit is contained in:
Zdenek Styblik 2014-12-20 13:43:38 +01:00
parent 1d1ce49bf4
commit befb21497f

View File

@ -533,6 +533,24 @@ ipmi_user_build_password_prompt(uint8_t user_id)
return prompt; return prompt;
} }
/* ask_password - ask user for password
*
* @user_id: User ID which will be built-in into text
*
* @returns pointer to char with password
*/
char *
ask_password(uint8_t user_id)
{
const char *password_prompt =
ipmi_user_build_password_prompt(user_id);
# ifdef HAVE_GETPASSPHRASE
return getpassphrase(password_prompt);
# else
return (char*)getpass(password_prompt);
# endif
}
int int
ipmi_user_summary(struct ipmi_intf *intf, int argc, char **argv) ipmi_user_summary(struct ipmi_intf *intf, int argc, char **argv)
{ {
@ -575,7 +593,6 @@ int
ipmi_user_test(struct ipmi_intf *intf, int argc, char **argv) ipmi_user_test(struct ipmi_intf *intf, int argc, char **argv)
{ {
/* Test */ /* Test */
int retval = 0;
char *password = NULL; char *password = NULL;
int password_length = 0; int password_length = 0;
uint8_t user_id = 0; uint8_t user_id = 0;
@ -597,29 +614,18 @@ ipmi_user_test(struct ipmi_intf *intf, int argc, char **argv)
} }
if (argc == 3) { if (argc == 3) {
/* We need to prompt for a password */ /* We need to prompt for a password */
const char *password_prompt = password = ask_password(user_id);
ipmi_user_build_password_prompt(user_id);
# ifdef HAVE_GETPASSPHRASE
password = getpassphrase(password_prompt);
# else
password = (char*)getpass(password_prompt);
# endif
if (password == NULL) { if (password == NULL) {
lprintf(LOG_ERR, "ipmitool: malloc failure"); lprintf(LOG_ERR, "ipmitool: malloc failure");
return (-1); return (-1);
} }
} else { } else {
password = strdup(argv[3]); password = argv[3];
} }
retval = ipmi_user_test_password(intf, return ipmi_user_test_password(intf,
user_id, user_id,
password, password,
password_length == 20); password_length == 20);
if (password != NULL) {
free(password);
password = NULL;
}
return retval;
} }
int int
@ -678,7 +684,6 @@ int
ipmi_user_password(struct ipmi_intf *intf, int argc, char **argv) ipmi_user_password(struct ipmi_intf *intf, int argc, char **argv)
{ {
char *password = NULL; char *password = NULL;
int retval = 0;
uint8_t user_id = 0; uint8_t user_id = 0;
if (is_ipmi_user_id(argv[2], &user_id)) { if (is_ipmi_user_id(argv[2], &user_id)) {
return (-1); return (-1);
@ -687,26 +692,12 @@ ipmi_user_password(struct ipmi_intf *intf, int argc, char **argv)
if (argc == 3) { if (argc == 3) {
/* We need to prompt for a password */ /* We need to prompt for a password */
char *tmp; char *tmp;
const char *password_prompt = password = ask_password(user_id);
ipmi_user_build_password_prompt(user_id);
# ifdef HAVE_GETPASSPHRASE
tmp = getpassphrase(password_prompt);
# else
tmp = (char*)getpass(password_prompt);
# endif
if (tmp != NULL) {
password = strdup(tmp);
tmp = NULL;
}
if (password == NULL) { if (password == NULL) {
lprintf(LOG_ERR, "ipmitool: malloc failure"); lprintf(LOG_ERR, "ipmitool: malloc failure");
return (-1); return (-1);
} }
# ifdef HAVE_GETPASSPHRASE tmp = ask_password(user_id);
tmp = getpassphrase(password_prompt);
# else
tmp = (char*)getpass(password_prompt);
# endif
if (tmp == NULL) { if (tmp == NULL) {
lprintf(LOG_ERR, "ipmitool: malloc failure"); lprintf(LOG_ERR, "ipmitool: malloc failure");
return (-1); return (-1);
@ -714,13 +705,10 @@ ipmi_user_password(struct ipmi_intf *intf, int argc, char **argv)
if (strlen(password) != strlen(tmp) if (strlen(password) != strlen(tmp)
|| strncmp(password, tmp, strlen(tmp))) { || strncmp(password, tmp, strlen(tmp))) {
lprintf(LOG_ERR, "Passwords do not match."); lprintf(LOG_ERR, "Passwords do not match.");
free(password);
password = NULL;
return (-1); return (-1);
} }
tmp = NULL;
} else { } else {
password = strdup(argv[3]); password = argv[3];
} }
if (password == NULL) { if (password == NULL) {
@ -731,16 +719,11 @@ ipmi_user_password(struct ipmi_intf *intf, int argc, char **argv)
return (-1); return (-1);
} }
retval = ipmi_user_set_password(intf, return ipmi_user_set_password(intf,
user_id, user_id,
IPMI_PASSWORD_SET_PASSWORD, IPMI_PASSWORD_SET_PASSWORD,
password, password,
strlen(password) > 16); strlen(password) > 16);
if (password != NULL) {
free(password);
password = NULL;
}
return retval;
} }
int int