update error handling for "session" and "user" commands

check channel medium type before attempting to do lan operations on it
This commit is contained in:
Duncan Laurie 2005-01-06 21:17:52 +00:00
parent 32cba5f174
commit dbbeee1b6f
8 changed files with 324 additions and 223 deletions

View File

@ -222,8 +222,9 @@ struct set_user_access_data {
#endif
} __attribute__ ((packed));
int ipmi_channel_main(struct ipmi_intf *, int, char **);
unsigned char ipmi_get_channel_medium(struct ipmi_intf * intf, unsigned char channel);
unsigned char ipmi_current_channel_medium(struct ipmi_intf * intf);
int ipmi_channel_main(struct ipmi_intf * intf, int argc, char ** argv);
int ipmi_get_channel_auth_cap(struct ipmi_intf * intf, unsigned char channel, unsigned char priv);
int ipmi_get_channel_info(struct ipmi_intf * intf, unsigned char channel);

View File

@ -80,4 +80,18 @@
#define IPMI_SET_IN_PROGRESS_IN_PROGRESS 0x01
#define IPMI_SET_IN_PROGRESS_COMMIT_WRITE 0x02
#define IPMI_CHANNEL_MEDIUM_RESERVED 0x0
#define IPMI_CHANNEL_MEDIUM_IPMB 0x1
#define IPMI_CHANNEL_MEDIUM_ICMB_1 0x2
#define IPMI_CHANNEL_MEDIUM_ICMB_09 0x3
#define IPMI_CHANNEL_MEDIUM_LAN 0x4
#define IPMI_CHANNEL_MEDIUM_SERIAL 0x5
#define IPMI_CHANNEL_MEDIUM_LAN_OTHER 0x6
#define IPMI_CHANNEL_MEDIUM_SMBUS_PCI 0x7
#define IPMI_CHANNEL_MEDIUM_SMBUS_1 0x8
#define IPMI_CHANNEL_MEDIUM_SMBUS_2 0x9
#define IPMI_CHANNEL_MEDIUM_USB_1 0xa
#define IPMI_CHANNEL_MEDIUM_USB_2 0xb
#define IPMI_CHANNEL_MEDIUM_SYSTEM 0xc
#endif /*IPMI_CONSTANTS_H*/

View File

@ -526,21 +526,59 @@ ipmi_set_user_access(struct ipmi_intf * intf, int argc, char ** argv)
return 0;
}
unsigned char
ipmi_get_channel_medium(struct ipmi_intf * intf, unsigned char channel)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
struct get_channel_info_rsp info;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_APP;
req.msg.cmd = IPMI_GET_CHANNEL_INFO;
req.msg.data = &channel;
req.msg.data_len = 1;
rsp = intf->sendrecv(intf, &req);
if (rsp == NULL) {
lprintf(LOG_ERR, "Get Channel Info command failed");
return -1;
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get Channel Info command failed: %s",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
memcpy(&info, rsp->data, sizeof(struct get_channel_info_rsp));
lprintf(LOG_DEBUG, "Channel type: %s",
val2str(info.channel_medium, ipmi_channel_medium_vals));
return info.channel_medium;
}
unsigned char
ipmi_current_channel_medium(struct ipmi_intf * intf)
{
return ipmi_get_channel_medium(intf, 0xE);
}
void
printf_channel_usage()
{
printf("Channel Commands: authcap <channel number> <max privilege>\n");
printf(" getaccess <channel number> [user id]\n");
printf(" setaccess <channel number> <user id> [callin=on|off] [ipmi=on|off] [link=on|off] [privilege=level]\n");
printf(" info [channel number]\n");
printf("\n");
printf("Possible privilege levels are:\n");
printf(" 1 Callback level\n");
printf(" 2 User level\n");
printf(" 3 Operator level\n");
printf(" 4 Administrator level\n");
printf(" 5 OEM Proprietary level\n");
printf(" 15 No access\n");
lprintf(LOG_NOTICE, "Channel Commands: authcap <channel number> <max privilege>");
lprintf(LOG_NOTICE, " getaccess <channel number> [user id]");
lprintf(LOG_NOTICE, " setaccess <channel number> "
"<user id> [callin=on|off] [ipmi=on|off] [link=on|off] [privilege=level]");
lprintf(LOG_NOTICE, " info [channel number]\n");
lprintf(LOG_NOTICE, "Possible privilege levels are:");
lprintf(LOG_NOTICE, " 1 Callback level");
lprintf(LOG_NOTICE, " 2 User level");
lprintf(LOG_NOTICE, " 3 Operator level");
lprintf(LOG_NOTICE, " 4 Administrator level");
lprintf(LOG_NOTICE, " 5 OEM Proprietary level");
lprintf(LOG_NOTICE, " 15 No access");
}

View File

@ -53,47 +53,13 @@
#include <ipmitool/ipmi_strings.h>
#include <ipmitool/ipmi_channel.h>
static int
ipmi_current_channel_medium(struct ipmi_intf * intf)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
unsigned char channel = 0xE;
struct get_channel_info_rsp info;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_APP;
req.msg.cmd = IPMI_GET_CHANNEL_INFO;
req.msg.data = &channel;
req.msg.data_len = 1;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
printf("Error in Get Channel Info command\n");
return -1;
}
else if (rsp->ccode) {
printf("Error in Get Channel Info command: %s\n",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
memcpy(&info, rsp->data, sizeof(struct get_channel_info_rsp));
if (verbose)
printf("Channel type: %s\n",
val2str(info.channel_medium, ipmi_channel_medium_vals));
return info.channel_medium;
}
static int
ipmi_send_platform_event(struct ipmi_intf * intf, int num)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
unsigned char rqdata[8];
int chmed;
unsigned char chmed;
int p = 0;
ipmi_intf_session_set_privlvl(intf, IPMI_SESSION_PRIV_ADMIN);
@ -107,17 +73,17 @@ ipmi_send_platform_event(struct ipmi_intf * intf, int num)
req.msg.data_len = 7;
chmed = ipmi_current_channel_medium(intf);
if (chmed == 0xc) {
if (chmed == IPMI_CHANNEL_MEDIUM_SYSTEM) {
/* system interface, need extra generator ID */
req.msg.data_len = 8;
rqdata[p++] = 0x20;
}
printf("Sending ");
/* IPMB/LAN/etc */
switch (num) {
case 1: /* temperature */
printf("Temperature - Upper Critical - Going High");
printf("Sending SAMPLE event: Temperature - "
"Upper Critical - Going High\n");
rqdata[p++] = 0x04; /* EvMRev */
rqdata[p++] = 0x01; /* Sensor Type */
rqdata[p++] = 0x30; /* Sensor # */
@ -127,7 +93,8 @@ ipmi_send_platform_event(struct ipmi_intf * intf, int num)
rqdata[p++] = 0x00; /* Event Data 3 */
break;
case 2: /* voltage error */
printf("Voltage Threshold - Lower Critical - Going Low");
printf("Sending SAMPLE event: Voltage Threshold - "
"Lower Critical - Going Low\n");
rqdata[p++] = 0x04; /* EvMRev */
rqdata[p++] = 0x02; /* Sensor Type */
rqdata[p++] = 0x60; /* Sensor # */
@ -137,7 +104,7 @@ ipmi_send_platform_event(struct ipmi_intf * intf, int num)
rqdata[p++] = 0x00; /* Event Data 3 */
break;
case 3: /* correctable ECC */
printf("Memory - Correctable ECC");
printf("Sending SAMPLE event: Memory - Correctable ECC\n");
rqdata[p++] = 0x04; /* EvMRev */
rqdata[p++] = 0x0c; /* Sensor Type */
rqdata[p++] = 0x53; /* Sensor # */
@ -147,22 +114,25 @@ ipmi_send_platform_event(struct ipmi_intf * intf, int num)
rqdata[p++] = 0x00; /* Event Data 3 */
break;
default:
printf("Invalid event number: %d\n", num);
lprintf(LOG_ERR, "Invalid event number: %d", num);
return -1;
}
printf(" event to BMC\n");
rsp = intf->sendrecv(intf, &req);
if (!rsp || rsp->ccode) {
printf("Error:%x Platform Event Message Command\n", rsp?rsp->ccode:0);
if (rsp == NULL) {
lprintf(LOG_ERR, "Platform Event Message command failed");
return -1;
}
else if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Platform Event Message command failed: %s",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
return 0;
}
static void
static int
ipmi_event_fromfile(struct ipmi_intf * intf, char * file)
{
FILE * fp;
@ -172,10 +142,12 @@ ipmi_event_fromfile(struct ipmi_intf * intf, char * file)
unsigned char rqdata[8];
char buf[1024];
char * ptr, * tok;
int i, j, chmed;
int i, j;
unsigned char chmed;
int rc = 0;
if (!file)
return;
if (file == NULL)
return -1;
/* must be admin privilege to do this */
ipmi_intf_session_set_privlvl(intf, IPMI_SESSION_PRIV_ADMIN);
@ -190,18 +162,18 @@ ipmi_event_fromfile(struct ipmi_intf * intf, char * file)
req.msg.data_len = 7;
chmed = ipmi_current_channel_medium(intf);
if (chmed == 0xc) {
if (chmed == IPMI_CHANNEL_MEDIUM_SYSTEM) {
/* system interface, need extra generator ID */
rqdata[0] = 0x20;
req.msg.data_len = 8;
}
fp = ipmi_open_file_read(file);
if (!fp)
return;
if (fp == NULL)
return -1;
while (!feof(fp)) {
if (!fgets(buf, 1024, fp))
while (feof(fp) != 0) {
if (fgets(buf, 1024, fp) == NULL)
continue;
/* clip off optional comment tail indicated by # */
@ -218,7 +190,7 @@ ipmi_event_fromfile(struct ipmi_intf * intf, char * file)
ptr = buf;
while (isspace(*ptr))
ptr++;
if (!strlen(ptr))
if (strlen(ptr) == 0)
continue;
/* parse the event, 7 bytes with optional comment */
@ -229,7 +201,7 @@ ipmi_event_fromfile(struct ipmi_intf * intf, char * file)
if (i == 7)
break;
j = i++;
if (chmed == 0xc)
if (chmed == IPMI_CHANNEL_MEDIUM_SYSTEM)
j++;
rqdata[j] = (unsigned char)strtol(tok, NULL, 0);
tok = strtok(NULL, " ");
@ -244,7 +216,7 @@ ipmi_event_fromfile(struct ipmi_intf * intf, char * file)
sel_event.record_id = 0;
sel_event.gen_id = 2;
j = (chmed == 0xc) ? 1 : 0;
j = (chmed == IPMI_CHANNEL_MEDIUM_SYSTEM) ? 1 : 0;
sel_event.evm_rev = rqdata[j++];
sel_event.sensor_type = rqdata[j++];
sel_event.sensor_num = rqdata[j++];
@ -257,39 +229,48 @@ ipmi_event_fromfile(struct ipmi_intf * intf, char * file)
ipmi_sel_print_std_entry(&sel_event);
rsp = intf->sendrecv(intf, &req);
if (rsp == NULL)
if (rsp == NULL) {
lprintf(LOG_ERR, "Platform Event Message command failed");
else if (rsp->ccode > 0)
rc = -1;
}
else if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Platform Event Message command failed: %s",
val2str(rsp->ccode, completion_code_vals));
rc = -1;
}
}
fclose(fp);
return rc;
}
int ipmi_event_main(struct ipmi_intf * intf, int argc, char ** argv)
int
ipmi_event_main(struct ipmi_intf * intf, int argc, char ** argv)
{
unsigned char c;
int rc = 0;
if (!argc || !strncmp(argv[0], "help", 4)) {
printf("usage: event <num>\n");
printf(" 1 : Temperature - Upper Critical - Going High\n");
printf(" 2 : Voltage Threshold - Lower Critical - Going Low\n");
printf(" 3 : Memory - Correctable ECC\n");
printf("usage: event file <filename>\n");
printf(" Will read list of events from file\n");
if (argc == 0 || strncmp(argv[0], "help", 4) == 0) {
lprintf(LOG_NOTICE, "usage: event <num>");
lprintf(LOG_NOTICE, " 1 : Temperature - Upper Critical - Going High");
lprintf(LOG_NOTICE, " 2 : Voltage Threshold - Lower Critical - Going Low");
lprintf(LOG_NOTICE, " 3 : Memory - Correctable ECC");
lprintf(LOG_NOTICE, "usage: event file <filename>");
lprintf(LOG_NOTICE, " Will read list of events from file");
return 0;
}
if (!strncmp(argv[0], "file", 4)) {
if (argc < 2)
printf("usage: event file <filename>\n");
else
ipmi_event_fromfile(intf, argv[1]);
if (strncmp(argv[0], "file", 4) == 0) {
if (argc < 2) {
lprintf(LOG_NOTICE, "usage: event file <filename>\n");
rc = -1;
} else {
rc = ipmi_event_fromfile(intf, argv[1]);
}
} else {
c = (unsigned char)strtol(argv[0], NULL, 0);
ipmi_send_platform_event(intf, c);
rc = ipmi_send_platform_event(intf, c);
}
return 0;
return rc;
}

View File

@ -51,8 +51,10 @@
#include <ipmitool/log.h>
#include <ipmitool/ipmi_intf.h>
#include <ipmitool/helper.h>
#include <ipmitool/ipmi_constants.h>
#include <ipmitool/ipmi_strings.h>
#include <ipmitool/ipmi_lanp.h>
#include <ipmitool/ipmi_channel.h>
extern int verbose;
@ -82,11 +84,11 @@ get_lan_param(struct ipmi_intf * intf, unsigned char chan, int param)
rsp = intf->sendrecv(intf, &req);
if (rsp == NULL) {
lprintf(LOG_INFO, "Unable to get LAN Parameter");
lprintf(LOG_INFO, "Get LAN Parameter command failed");
return NULL;
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get Lan Parameter failed: %s",
lprintf(LOG_ERR, "Get LAN Parameter command failed: %s",
val2str(rsp->ccode, completion_code_vals));
return NULL;
}
@ -323,8 +325,23 @@ static int
ipmi_lan_print(struct ipmi_intf * intf, unsigned char chan)
{
struct lan_param * p;
unsigned char medium;
int rc = 0;
if (chan < 1 || chan > IPMI_CHANNEL_NUMBER_MAX) {
lprintf(LOG_ERR, "Invalid Channel %d", chan);
return -1;
}
/* find type of channel and only accept 802.3 LAN */
medium = ipmi_get_channel_medium(intf, chan);
if (medium != IPMI_CHANNEL_MEDIUM_LAN &&
medium != IPMI_CHANNEL_MEDIUM_LAN_OTHER) {
lprintf(LOG_ERR, "Channel %d (%s) is not a LAN channel",
chan, val2str(medium, ipmi_channel_medium_vals), medium);
return -1;
}
p = get_lan_param(intf, chan, IPMI_LANP_SET_IN_PROGRESS);
if (p)
printf("%-24s: 0x%02x\n", p->desc, p->data[0]);
@ -681,7 +698,7 @@ static int
ipmi_lan_set(struct ipmi_intf * intf, int argc, char ** argv)
{
unsigned char data[32];
unsigned char chan;
unsigned char chan, medium;
int rc = 0;
if (argc < 2) {
@ -689,15 +706,28 @@ ipmi_lan_set(struct ipmi_intf * intf, int argc, char ** argv)
return 0;
}
chan = (unsigned char)strtol(argv[0], NULL, 0);
if ((chan < 1) || (chan > IPMI_CHANNEL_NUMBER_MAX) ||
(strncmp(argv[0], "help", 4) == 0) ||
(strncmp(argv[1], "help", 4) == 0)) {
if (strncmp(argv[0], "help", 4) == 0 ||
strncmp(argv[1], "help", 4) == 0) {
ipmi_lan_set_usage();
return 0;
}
chan = (unsigned char)strtol(argv[0], NULL, 0);
if (chan < 1 || chan > IPMI_CHANNEL_NUMBER_MAX) {
lprintf(LOG_ERR, "Invalid Channel %d", chan);
ipmi_lan_set_usage();
return -1;
}
/* find type of channel and only accept 802.3 LAN */
medium = ipmi_get_channel_medium(intf, chan);
if (medium != IPMI_CHANNEL_MEDIUM_LAN ||
medium != IPMI_CHANNEL_MEDIUM_LAN_OTHER) {
lprintf(LOG_ERR, "Channel %d is not a LAN channel!", chan);
return -1;
}
memset(&data, 0, sizeof(data));
/* set user access */

View File

@ -48,6 +48,7 @@
#include <ipmitool/ipmi.h>
#include <ipmitool/ipmi_intf.h>
#include <ipmitool/helper.h>
#include <ipmitool/log.h>
#include <ipmitool/ipmi_lanp.h>
#include <ipmitool/ipmi_session.h>
#include <ipmitool/ipmi_strings.h>
@ -300,17 +301,26 @@ ipmi_get_session_info(struct ipmi_intf * intf,
break;
}
rsp = intf->sendrecv(intf, &req);
if (!rsp || rsp->ccode) {
printf("Error:%x Get Session Info Command\n", rsp ? rsp->ccode : 0);
if ((session_request_type == IPMI_SESSION_REQUEST_CURRENT) &&
(strncmp(intf->name, "intf_lan", 8)))
printf("It is likely that the channel in use does not support sessions\n");
if (rsp == NULL)
{
lprintf(LOG_ERR, "Get Session Info command failed");
retval = -1;
}
else if (rsp->ccode > 0)
{
lprintf(LOG_ERR, "Get Session Info command failed: %s",
val2str(rsp->ccode, completion_code_vals));
retval = -1;
}
if (retval < 0)
{
if ((session_request_type == IPMI_SESSION_REQUEST_CURRENT) &&
(strncmp(intf->name, "intf_lan", 8) != 0))
lprintf(LOG_ERR, "It is likely that the channel in use "
"does not support sessions");
}
else
{
memcpy(&session_info, rsp->data, rsp->data_len);
@ -326,20 +336,27 @@ ipmi_get_session_info(struct ipmi_intf * intf,
rqdata[0] = i++;
rsp = intf->sendrecv(intf, &req);
if (!rsp || rsp->ccode || (rsp->data_len < 3))
if (rsp == NULL)
{
if (!rsp || (rsp->ccode != 0xCC))
{
printf("Error:%x Get Session Info Command\n", rsp ? rsp->ccode : 0);
retval = -1;
}
lprintf(LOG_ERR, "Get Session Info command failed");
retval = -1;
break;
}
else
else if (rsp->ccode > 0 && rsp->ccode != 0xCC && rsp->ccode != 0xCB)
{
memcpy(&session_info, rsp->data, rsp->data_len);
print_session_info(&session_info, rsp->data_len);
lprintf(LOG_ERR, "Get Session Info command failed: %s",
val2str(rsp->ccode, completion_code_vals));
retval = -1;
break;
}
else if (rsp->data_len < 3)
{
retval = -1;
break;
}
memcpy(&session_info, rsp->data, rsp->data_len);
print_session_info(&session_info, rsp->data_len);
} while (i <= session_info.session_slot_count);
break;
@ -353,7 +370,7 @@ ipmi_get_session_info(struct ipmi_intf * intf,
void
printf_session_usage()
{
printf("Session Commands: info <active | all | id 0xnnnnnnnn | handle 0xnn>\n");
lprintf(LOG_NOTICE, "Session Commands: info <active | all | id 0xnnnnnnnn | handle 0xnn>");
}
@ -362,31 +379,27 @@ ipmi_session_main(struct ipmi_intf * intf, int argc, char ** argv)
{
int retval = 0;
if (!argc || !strncmp(argv[0], "help", 4))
if (argc == 0 || strncmp(argv[0], "help", 4) == 0)
{
printf_session_usage();
retval = 1;
}
else if (!strncmp(argv[0], "info", 4))
else if (strncmp(argv[0], "info", 4) == 0)
{
if ((argc < 2) || !strncmp(argv[1], "help", 4))
if ((argc < 2) || strncmp(argv[1], "help", 4) == 0)
{
printf_session_usage();
retval = 1;
}
else
{
Ipmi_Session_Request_Type session_request_type = 0;
uint32_t id_or_handle = 0;
if (!strncmp(argv[1], "active", 6))
if (strncmp(argv[1], "active", 6) == 0)
session_request_type = IPMI_SESSION_REQUEST_CURRENT;
else if (!strncmp(argv[1], "all", 3))
else if (strncmp(argv[1], "all", 3) == 0)
session_request_type = IPMI_SESSION_REQUEST_ALL;
else if (!strncmp(argv[1], "id", 2))
else if (strncmp(argv[1], "id", 2) == 0)
{
if (argc >= 3)
{
@ -395,12 +408,12 @@ ipmi_session_main(struct ipmi_intf * intf, int argc, char ** argv)
}
else
{
printf("Missing id argument\n");
lprintf(LOG_ERR, "Missing id argument");
printf_session_usage();
retval = 1;
retval = -1;
}
}
else if (!strncmp(argv[1], "handle", 6))
else if (strncmp(argv[1], "handle", 6) == 0)
{
if (argc >= 3)
{
@ -409,20 +422,20 @@ ipmi_session_main(struct ipmi_intf * intf, int argc, char ** argv)
}
else
{
printf("Missing handle argument\n");
lprintf(LOG_ERR, "Missing handle argument");
printf_session_usage();
retval = 1;
retval = -1;
}
}
else
{
printf("Invalid SESSION info parameter: %s\n", argv[1]);
lprintf(LOG_ERR, "Invalid SESSION info parameter: %s", argv[1]);
printf_session_usage();
retval = 1;
retval = -1;
}
if (! retval)
if (retval == 0)
retval = ipmi_get_session_info(intf,
session_request_type,
id_or_handle);
@ -430,9 +443,9 @@ ipmi_session_main(struct ipmi_intf * intf, int argc, char ** argv)
}
else
{
printf("Invalid SESSION command: %s\n", argv[0]);
lprintf(LOG_ERR, "Invalid SESSION command: %s", argv[0]);
printf_session_usage();
retval = 1;
retval = -1;
}
return retval;

View File

@ -210,19 +210,19 @@ const struct valstr ipmi_channel_protocol_vals[] = {
const struct valstr ipmi_channel_medium_vals[] = {
{ 0x00, "reserved" },
{ 0x01, "IPMB (I2C)" },
{ 0x02, "ICMB v1.0" },
{ 0x03, "ICMB v0.9" },
{ 0x04, "802.3 LAN" },
{ 0x05, "Serial/Modem" },
{ 0x06, "Other LAN" },
{ 0x07, "PCI SMBus" },
{ 0x08, "SMBus v1.0/v1.1" },
{ 0x09, "SMBus v2.0" },
{ 0x0a, "USB 1.x" },
{ 0x0b, "USB 2.x" },
{ 0x0c, "System Interface" },
{ IPMI_CHANNEL_MEDIUM_RESERVED, "reserved" },
{ IPMI_CHANNEL_MEDIUM_IPMB, "IPMB (I2C)" },
{ IPMI_CHANNEL_MEDIUM_ICMB_1, "ICMB v1.0" },
{ IPMI_CHANNEL_MEDIUM_ICMB_09, "ICMB v0.9" },
{ IPMI_CHANNEL_MEDIUM_LAN, "802.3 LAN" },
{ IPMI_CHANNEL_MEDIUM_SERIAL, "Serial/Modem" },
{ IPMI_CHANNEL_MEDIUM_LAN_OTHER,"Other LAN" },
{ IPMI_CHANNEL_MEDIUM_SMBUS_PCI,"PCI SMBus" },
{ IPMI_CHANNEL_MEDIUM_SMBUS_1, "SMBus v1.0/v1.1" },
{ IPMI_CHANNEL_MEDIUM_SMBUS_2, "SMBus v2.0" },
{ IPMI_CHANNEL_MEDIUM_USB_1, "USB 1.x" },
{ IPMI_CHANNEL_MEDIUM_USB_2, "USB 2.x" },
{ IPMI_CHANNEL_MEDIUM_SYSTEM, "System Interface" },
{ 0x00, NULL },
};

View File

@ -44,6 +44,7 @@
#include <unistd.h>
#include <ipmitool/helper.h>
#include <ipmitool/log.h>
#include <ipmitool/ipmi.h>
#include <ipmitool/ipmi_intf.h>
#include <ipmitool/ipmi_user.h>
@ -98,10 +99,15 @@ ipmi_get_user_access(
rsp = intf->sendrecv(intf, &req);
if (!rsp || rsp->ccode)
{
printf("Error:%x Get User Access Command (user 0x%x)\n",
rsp ? rsp->ccode : 0, msg_data[1]);
if (rsp == NULL) {
lprintf(LOG_ERR, "Get User Access command failed "
"(channel %d, user %d)", channel_number, user_id);
return -1;
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get User Access command failed "
"(channel %d, user %d): %s", channel_number, user_id,
val2str(rsp->ccode, completion_code_vals));
return -1;
}
@ -146,10 +152,14 @@ ipmi_get_user_name(
rsp = intf->sendrecv(intf, &req);
if (!rsp || rsp->ccode)
{
printf("Error:%x Get User Name Command (user 0x%x)\n",
rsp ? rsp->ccode : 0, msg_data[0]);
if (rsp == NULL) {
lprintf(LOG_ERR, "Get User Name command failed (user %d)",
user_id);
return -1;
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get User Name command failed (user %d): %s",
user_id, val2str(rsp->ccode, completion_code_vals));
return -1;
}
@ -320,13 +330,16 @@ ipmi_user_set_username(
rsp = intf->sendrecv(intf, &req);
if (!rsp || rsp->ccode)
{
printf("Error:%x Set User Name Command\n",
rsp ? rsp->ccode : 0);
if (rsp == NULL) {
lprintf(LOG_ERR, "Set User Name command failed (user %d, name %s)",
user_id, name);
return -1;
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Set User Name command failed (user %d, name %s): %s",
user_id, name, val2str(rsp->ccode, completion_code_vals));
return -1;
}
return 0;
}
@ -351,7 +364,6 @@ ipmi_user_set_password(
struct ipmi_rs * rsp;
struct ipmi_rq req;
char * msg_data;
int ret = 0;
int password_length = (is_twenty_byte_password? 20 : 16);
@ -375,20 +387,23 @@ ipmi_user_set_password(
memset(msg_data + 2, 0, password_length);
if (password)
if (password != NULL)
strncpy(msg_data + 2, password, password_length);
rsp = intf->sendrecv(intf, &req);
if (!rsp || rsp->ccode)
{
printf("Error:%x Set User Password Command\n",
rsp ? rsp->ccode : 0);
ret = (rsp? rsp->ccode : -1);
if (rsp == NULL) {
lprintf(LOG_ERR, "Set User Password command failed (user %d)",
user_id);
return -1;
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Set User Password command failed (user %d): %s",
user_id, val2str(rsp->ccode, completion_code_vals));
return rsp->ccode;
}
return ret;
return 0;
}
@ -413,16 +428,21 @@ ipmi_user_test_password(
password,
is_twenty_byte_password);
if (! ret)
switch (ret) {
case 0:
printf("Success\n");
else if (ret == 0x80)
break;
case 0x80:
printf("Failure: password incorrect\n");
else if (ret == 0x81)
break;
case 0x81:
printf("Failure: wrong password size\n");
else
break;
default:
printf("Unknown error\n");
}
return (ret ? -1 : 0);
return ((ret == 0) ? 0 : -1);
}
@ -433,25 +453,22 @@ ipmi_user_test_password(
void
print_user_usage()
{
printf("\n");
printf("User Commands: summary [<channel number>]\n");
printf(" list [<channel number>]\n");
printf(" set name <user id> <username>\n");
printf(" set password <user id> [<password>]\n");
printf(" disable <user id> [<channel number>]\n");
printf(" enable <user id> [<channel number>]\n");
printf(" test <user id> <16|20> [<password]>\n");
printf("\n");
lprintf(LOG_NOTICE, "User Commands: summary [<channel number>]");
lprintf(LOG_NOTICE, " list [<channel number>]");
lprintf(LOG_NOTICE, " set name <user id> <username>");
lprintf(LOG_NOTICE, " set password <user id> [<password>]");
lprintf(LOG_NOTICE, " disable <user id> [<channel number>]");
lprintf(LOG_NOTICE, " enable <user id> [<channel number>]");
lprintf(LOG_NOTICE, " test <user id> <16|20> [<password]>\n");
}
const char *
ipmi_user_build_password_prompt(unsigned char user_id)
{
static char prompt[128];
sprintf(prompt, "Password for user %d: ", user_id);
memset(prompt, 0, 128);
snprintf(prompt, 128, "Password for user %d: ", user_id);
return prompt;
}
@ -472,13 +489,16 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
/*
* Help
*/
if (!argc || !strncmp(argv[0], "help", 4))
if (argc == 0 || strncmp(argv[0], "help", 4) == 0)
{
print_user_usage();
}
/*
* Summary
*/
else if (!strncmp(argv[0], "summary", 7)) {
else if (strncmp(argv[0], "summary", 7) == 0)
{
unsigned char channel;
if (argc == 1)
@ -498,7 +518,8 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
/*
* List
*/
else if (!strncmp(argv[0], "list", 4)) {
else if (strncmp(argv[0], "list", 4) == 0)
{
unsigned char channel;
if (argc == 1)
@ -519,21 +540,21 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
/*
* Test
*/
else if (!strncmp(argv[0], "test", 4))
else if (strncmp(argv[0], "test", 4) == 0)
{
// a little fucking irritating, isn't it
if ((argc == 3 || argc == 4) &&
((!strncmp(argv[2], "16", 2)) ||
(!strncmp(argv[2], "20", 2))))
((strncmp(argv[2], "16", 2) == 0) ||
(strncmp(argv[2], "20", 2) == 0)))
{
char * password = NULL;
int password_length = atoi(argv[2]);
unsigned char user_id = (unsigned char)strtol(argv[1],
NULL,
0);
if (! user_id)
if (user_id == 0)
{
printf("Error. Invalid user ID: %d\n", user_id);
lprintf(LOG_ERR, "Invalid user ID: %d", user_id);
return -1;
}
@ -547,20 +568,17 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
ipmi_user_build_password_prompt(user_id);
#ifdef HAVE_GETPASSPHRASE
if ((tmp = getpassphrase (password_prompt)))
tmp = getpassphrase (password_prompt);
#else
if ((tmp = (char*)getpass(password_prompt)))
tmp = (char*)getpass (password_prompt);
#endif
{
if (tmp != NULL)
password = strdup(tmp);
}
}
else
password = argv[3];
retval = ipmi_user_test_password(intf,
user_id,
password,
@ -579,21 +597,21 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
/*
* Set
*/
else if (!strncmp(argv[0], "set", 3))
else if (strncmp(argv[0], "set", 3) == 0)
{
/*
* Set Password
*/
if ((argc >= 3) &&
(! strcmp("password", argv[1])))
(strncmp("password", argv[1], 8) == 0))
{
char * password = NULL;
unsigned char user_id = (unsigned char)strtol(argv[2],
NULL,
0);
if (! user_id)
if (user_id == 0)
{
printf("Error. Invalid user ID: %d\n", user_id);
lprintf(LOG_ERR, "Invalid user ID: %d", user_id);
return -1;
}
@ -607,25 +625,32 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
ipmi_user_build_password_prompt(user_id);
#ifdef HAVE_GETPASSPHRASE
if ((tmp = getpassphrase (password_prompt)))
tmp = getpassphrase (password_prompt);
#else
if ((tmp = (char*)getpass (password_prompt)))
tmp = (char*)getpass (password_prompt);
#endif
if (tmp != NULL)
{
password = strdup(tmp);
#ifdef HAVE_GETPASSPHRASE
if ((tmp = getpassphrase (password_prompt)))
tmp = getpassphrase (password_prompt);
#else
if ((tmp = (char*)getpass (password_prompt)))
tmp = (char*)getpass (password_prompt);
#endif
if (tmp != NULL)
{
if (strlen(password) != strlen(tmp))
{
if (strcmp(password, tmp))
{
printf("Error. Passwords to not match.\n");
return -1;
}
lprintf(LOG_ERR, "Passwords do not match");
return -1;
}
if (strncmp(password, tmp, strlen(tmp)))
{
lprintf(LOG_ERR, "Passwords to not match");
return -1;
}
}
}
}
else
@ -633,7 +658,7 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
if (strlen(password) > 20)
{
printf("Error. Password is too long (> 20 bytes).\n");
lprintf(LOG_ERR, "Password is too long (> 20 bytes)");
return -1;
}
@ -650,7 +675,7 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
* Set Name
*/
else if ((argc >= 2) &&
(! strcmp("name", argv[1])))
(strncmp("name", argv[1], 4) == 0))
{
if (argc != 4)
{
@ -675,8 +700,8 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
/*
* Disable / Enable
*/
else if ((!strncmp(argv[0], "disable", 7)) ||
(!strncmp(argv[0], "enable", 6)))
else if ((strncmp(argv[0], "disable", 7) == 0) ||
(strncmp(argv[0], "enable", 6) == 0))
{
unsigned char user_id;
unsigned char operation;
@ -693,15 +718,15 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
user_id = (unsigned char)strtol(argv[1],
NULL,
0);
if (! user_id)
if (user_id == 0)
{
printf("Error. Invalid user ID: %d\n", user_id);
lprintf(LOG_ERR, "Invalid user ID: %d", user_id);
return -1;
}
operation = (!strncmp(argv[0], "disable", 7))?
IPMI_PASSWORD_DISABLE_USER: IPMI_PASSWORD_ENABLE_USER;
operation = (strncmp(argv[0], "disable", 7) == 0) ?
IPMI_PASSWORD_DISABLE_USER : IPMI_PASSWORD_ENABLE_USER;
retval = ipmi_user_set_password(intf,
user_id,
@ -715,7 +740,6 @@ ipmi_user_main(struct ipmi_intf * intf, int argc, char ** argv)
else
{
print_user_usage();
retval = -1;
}
return retval;