fix handling of names with spaces in exec/shell

This commit is contained in:
Duncan Laurie 2005-03-16 23:13:57 +00:00
parent 6d1f8521e6
commit 04561e2433

View File

@ -230,19 +230,27 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
printf("Set session password\n"); printf("Set session password\n");
} }
else if (strncmp(argv[0], "authtype", 8) == 0) { else if (strncmp(argv[0], "authtype", 8) == 0) {
uint8_t authtype; int authtype;
authtype = (uint8_t)str2val(argv[1], ipmi_authtype_session_vals); authtype = str2val(argv[1], ipmi_authtype_session_vals);
if (authtype < 0) {
lprintf(LOG_ERR, "Invalid authtype: %s", argv[1]);
} else {
ipmi_intf_session_set_authtype(intf, authtype); ipmi_intf_session_set_authtype(intf, authtype);
printf("Set session authtype to %s\n", printf("Set session authtype to %s\n",
val2str(intf->session->authtype_set, ipmi_authtype_session_vals)); val2str(intf->session->authtype_set, ipmi_authtype_session_vals));
} }
}
else if (strncmp(argv[0], "privlvl", 7) == 0) { else if (strncmp(argv[0], "privlvl", 7) == 0) {
uint8_t privlvl; int privlvl;
privlvl = (uint8_t)str2val(argv[1], ipmi_privlvl_vals); privlvl = str2val(argv[1], ipmi_privlvl_vals);
if (privlvl < 0) {
lprintf(LOG_ERR, "Invalid privilege level: %s", argv[1]);
} else {
ipmi_intf_session_set_privlvl(intf, privlvl); ipmi_intf_session_set_privlvl(intf, privlvl);
printf("Set session privilege level to %s\n", printf("Set session privilege level to %s\n",
val2str(intf->session->privlvl, ipmi_privlvl_vals)); val2str(intf->session->privlvl, ipmi_privlvl_vals));
} }
}
else if (strncmp(argv[0], "port", 4) == 0) { else if (strncmp(argv[0], "port", 4) == 0) {
int port = atoi(argv[1]); int port = atoi(argv[1]);
ipmi_intf_session_set_port(intf, port); ipmi_intf_session_set_port(intf, port);
@ -267,7 +275,7 @@ int ipmi_exec_main(struct ipmi_intf * intf, int argc, char ** argv)
{ {
FILE * fp; FILE * fp;
char buf[EXEC_BUF_SIZE]; char buf[EXEC_BUF_SIZE];
char * ptr, * tok, * ret; char * ptr, * tok, * ret, * tmp;
int __argc, i, r; int __argc, i, r;
char * __argv[EXEC_ARG_SIZE]; char * __argv[EXEC_ARG_SIZE];
int rc=0; int rc=0;
@ -293,6 +301,29 @@ int ipmi_exec_main(struct ipmi_intf * intf, int argc, char ** argv)
else else
ptr = buf + strlen(buf); ptr = buf + strlen(buf);
/* change "" and '' with spaces in the middle to ~
* this is really ugly but I'm in a hurry */
ptr = buf;
while (*ptr != '\0') {
if (*ptr == '"') {
ptr++;
while (*ptr != '"') {
if (isspace(*ptr))
*ptr = '~';
ptr++;
}
}
if (*ptr == '\'') {
ptr++;
while (*ptr != '\'') {
if (isspace(*ptr))
*ptr = '~';
ptr++;
}
}
ptr++;
}
/* clip off trailing and leading whitespace */ /* clip off trailing and leading whitespace */
ptr--; ptr--;
while (isspace(*ptr) && ptr >= buf) while (isspace(*ptr) && ptr >= buf)
@ -305,16 +336,33 @@ int ipmi_exec_main(struct ipmi_intf * intf, int argc, char ** argv)
/* parse it and make argument list */ /* parse it and make argument list */
__argc = 0; __argc = 0;
tok = strtok(ptr, " "); for (tok = strtok(ptr, " "); tok != NULL; tok = strtok(NULL, " ")) {
while (tok) {
if (__argc < EXEC_ARG_SIZE) { if (__argc < EXEC_ARG_SIZE) {
__argv[__argc++] = strdup(tok); __argv[__argc++] = strdup(tok);
if (__argv[__argc-1] == NULL) { if (__argv[__argc-1] == NULL) {
lprintf(LOG_ERR, "ipmitool: malloc failure"); lprintf(LOG_ERR, "ipmitool: malloc failure");
return -1; return -1;
} }
tmp = __argv[__argc-1];
if (*tmp == '\'') {
memmove(tmp, tmp+1, strlen(tmp));
while (*tmp != '\'') {
if (*tmp == '~')
*tmp = ' ';
tmp++;
}
*tmp = '\0';
}
if (*tmp == '"') {
memmove(tmp, tmp+1, strlen(tmp));
while (*tmp != '"') {
if (*tmp == '~')
*tmp = ' ';
tmp++;
}
*tmp = '\0';
}
} }
tok = strtok(NULL, " ");
} }
/* now run the command, save the result if not successful */ /* now run the command, save the result if not successful */