/* ipmi_password_file_read - Open file and read password from it * * @filename: file name to read from * * returns pointer to allocated buffer containing password * (caller is expected to free when finished) * returns NULL on error */ static char * ipmi_password_file_read(char * filename) { FILE * fp; char * pass = NULL; int l; pass = malloc(21); if (pass == NULL) { lprintf(LOG_ERR, "ipmitool: malloc failure"); return NULL; } memset(pass, 0, 21); fp = ipmi_open_file_read((const char *)filename); if (fp == NULL) { lprintf(LOG_ERR, "Unable to open password file %s", filename); free(pass); return NULL; } /* read in id */ if (fgets(pass, 21, fp) == NULL) { lprintf(LOG_ERR, "Unable to read password from file %s", filename); free(pass); fclose(fp); return NULL; } /* remove trailing whitespace */ l = strcspn(pass, " \r\n\t"); if (l > 0) { pass[l] = '\0'; } fclose(fp); return pass; }
int ipmi_exec_main(struct ipmi_intf * intf, int argc, char ** argv) { FILE * fp; char buf[EXEC_BUF_SIZE]; char * ptr, * tok, * ret, * tmp; int __argc, i, r; char * __argv[EXEC_ARG_SIZE]; int rc=0; if (argc < 1) { lprintf(LOG_ERR, "Usage: exec <filename>"); return -1; } fp = ipmi_open_file_read(argv[0]); if (fp == NULL) return -1; while (feof(fp) == 0) { ret = fgets(buf, EXEC_BUF_SIZE, fp); if (ret == NULL) continue; /* clip off optional comment tail indicated by # */ ptr = strchr(buf, '#'); if (ptr) *ptr = '\0'; else ptr = buf + strlen(buf); /* change "" and '' with spaces in the middle to ~ */ ptr = buf; while (*ptr != '\0') { if (*ptr == '"') { ptr++; while (*ptr != '"') { if (isspace((int)*ptr)) *ptr = '~'; ptr++; } } if (*ptr == '\'') { ptr++; while (*ptr != '\'') { if (isspace((int)*ptr)) *ptr = '~'; ptr++; } } ptr++; } /* clip off trailing and leading whitespace */ ptr--; while (isspace((int)*ptr) && ptr >= buf) *ptr-- = '\0'; ptr = buf; while (isspace((int)*ptr)) ptr++; if (strlen(ptr) == 0) continue; /* parse it and make argument list */ __argc = 0; for (tok = strtok(ptr, " "); tok != NULL; tok = strtok(NULL, " ")) { if (__argc < EXEC_ARG_SIZE) { __argv[__argc++] = strdup(tok); if (__argv[__argc-1] == NULL) { lprintf(LOG_ERR, "ipmitool: malloc failure"); 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'; } } } /* now run the command, save the result if not successful */ r = ipmi_cmd_run(intf, __argv[0], __argc-1, &(__argv[1])); if (r != 0) rc = r; /* free argument list */ for (i=0; i<__argc; i++) { if (__argv[i] != NULL) { free(__argv[i]); __argv[i] = NULL; } } } fclose(fp); return rc; }