Пример #1
0
int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
{
    if (argc == 0 || strncmp(argv[0], "help", 4) == 0) {
        ipmi_set_usage();
        return -1;
    }

    /* these options can have no arguments */
    if (strncmp(argv[0], "verbose", 7) == 0) {
        verbose = (argc > 1) ? atoi(argv[1]) : verbose+1;
        return 0;
    }
    if (strncmp(argv[0], "csv", 3) == 0) {
        csv_output = (argc > 1) ? atoi(argv[1]) : 1;
        return 0;
    }

    /* the rest need an argument */
    if (argc == 1) {
        ipmi_set_usage();
        return -1;
    }

    if (strncmp(argv[0], "host", 4) == 0 ||
            strncmp(argv[0], "hostname", 8) == 0) {
        ipmi_intf_session_set_hostname(intf, argv[1]);
        printf("Set session hostname to %s\n", intf->session->hostname);
    }
    else if (strncmp(argv[0], "user", 4) == 0 ||
             strncmp(argv[0], "username", 8) == 0) {
        ipmi_intf_session_set_username(intf, argv[1]);
        printf("Set session username to %s\n", intf->session->username);
    }
    else if (strncmp(argv[0], "pass", 4) == 0 ||
             strncmp(argv[0], "password", 8) == 0) {
        ipmi_intf_session_set_password(intf, argv[1]);
        printf("Set session password\n");
    }
    else if (strncmp(argv[0], "authtype", 8) == 0) {
        int authtype;
        authtype = str2val(argv[1], ipmi_authtype_session_vals);
        if (authtype == 0xFF) {
            lprintf(LOG_ERR, "Invalid authtype: %s", argv[1]);
        } else {
            ipmi_intf_session_set_authtype(intf, authtype);
            printf("Set session authtype to %s\n",
                   val2str(intf->session->authtype_set, ipmi_authtype_session_vals));
        }
    }
    else if (strncmp(argv[0], "privlvl", 7) == 0) {
        int privlvl;
        privlvl = str2val(argv[1], ipmi_privlvl_vals);
        if (privlvl == 0xFF) {
            lprintf(LOG_ERR, "Invalid privilege level: %s", argv[1]);
        } else {
            ipmi_intf_session_set_privlvl(intf, privlvl);
            printf("Set session privilege level to %s\n",
                   val2str(intf->session->privlvl, ipmi_privlvl_vals));
        }
    }
    else if (strncmp(argv[0], "port", 4) == 0) {
        int port = atoi(argv[1]);
        ipmi_intf_session_set_port(intf, port);
        printf("Set session port to %d\n", intf->session->port);
    }
    else if (strncmp(argv[0], "localaddr", 9) == 0) {
        intf->my_addr = (uint8_t)strtol(argv[1], NULL, 0);
        printf("Set local IPMB address to 0x%02x\n", intf->my_addr);
    }
    else if (strncmp(argv[0], "targetaddr", 10) == 0) {
        intf->target_addr = (uint8_t)strtol(argv[1], NULL, 0);
        printf("Set remote IPMB address to 0x%02x\n", intf->target_addr);
    }
    else {
        ipmi_set_usage();
        return -1;
    }
    return 0;
}
Пример #2
0
/* ipmi_parse_options  -  helper function to handle parsing command line options
 *
 * @argc:	count of options
 * @argv:	list of options
 * @cmdlist:	list of supported commands
 * @intflist:	list of supported interfaces
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_main(int argc, char ** argv,
		struct ipmi_cmd * cmdlist,
		struct ipmi_intf_support * intflist)
{
	struct ipmi_intf_support * sup;
	int privlvl = 0;
	uint8_t target_addr = 0;
	uint8_t target_channel = 0;

	uint8_t transit_addr = 0;
	uint8_t transit_channel = 0;
	uint8_t target_lun     = 0;
	uint8_t arg_addr = 0, addr;
	uint16_t my_long_packet_size=0;
	uint8_t my_long_packet_set=0;
	uint8_t lookupbit = 0x10;	/* use name-only lookup by default */
	int retry = 0;
	uint32_t timeout = 0;
	int authtype = -1;
	char * tmp_pass = NULL;
	char * tmp_env = NULL;
	char * hostname = NULL;
	char * username = NULL;
	char * password = NULL;
	char * intfname = NULL;
	char * progname = NULL;
	char * oemtype  = NULL;
	char * sdrcache = NULL;
	unsigned char * kgkey = NULL;
	char * seloem   = NULL;
	int port = 0;
	int devnum = 0;
	int cipher_suite_id = 3; /* See table 22-19 of the IPMIv2 spec */
	int argflag, i, found;
	int rc = -1;
	char sol_escape_char = SOL_ESCAPE_CHARACTER_DEFAULT;
	char * devfile  = NULL;

	/* save program name */
	progname = strrchr(argv[0], '/');
	progname = ((progname == NULL) ? argv[0] : progname+1);
	signal(SIGINT, ipmi_catch_sigint);

	while ((argflag = getopt(argc, (char **)argv, OPTION_STRING)) != -1)
	{
		switch (argflag) {
		case 'I':
			if (intfname) {
				free(intfname);
				intfname = NULL;
			}
			intfname = strdup(optarg);
			if (intfname == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			if (intflist != NULL) {
				found = 0;
				for (sup=intflist; sup->name != NULL; sup++) {
					if (strncmp(sup->name, intfname, strlen(intfname)) == 0 &&
							strncmp(sup->name, intfname, strlen(sup->name)) == 0 &&
							sup->supported == 1)
						found = 1;
				}
				if (!found) {
					lprintf(LOG_ERR, "Interface %s not supported", intfname);
					goto out_free;
				}
			}
			break;
		case 'h':
			ipmi_option_usage(progname, cmdlist, intflist);
			rc = 0;
			goto out_free;
			break;
		case 'V':
			printf("%s version %s\n", progname, VERSION);
			rc = 0;
			goto out_free;
			break;
		case 'd':
			if (str2int(optarg, &devnum) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-d'.");
				rc = -1;
				goto out_free;
			}
			/* Check if device number is -gt 0; I couldn't find limit for
			 * kernels > 2.6, thus right side is unlimited.
			 */
			if (devnum < 0) {
				lprintf(LOG_ERR, "Device number %i is out of range.", devnum);
				rc = -1;
				goto out_free;
			}
			break;
		case 'p':
			if (str2int(optarg, &port) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-p'.");
				rc = -1;
				goto out_free;
			}
			/* Check if port is -gt 0 && port is -lt 65535 */
			if (port < 0 || port > 65535) {
				lprintf(LOG_ERR, "Port number %i is out of range.", port);
				rc = -1;
				goto out_free;
			}
			break;
		case 'C':
			if (str2int(optarg, &cipher_suite_id) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-C'.");
				rc = -1;
				goto out_free;
			}
			/* add check Cipher is -gt 0 */
			if (cipher_suite_id < 0) {
				lprintf(LOG_ERR, "Cipher suite ID %i is invalid.", cipher_suite_id);
				rc = -1;
				goto out_free;
			}
			break;
		case 'v':
			verbose++;
			break;
		case 'c':
			csv_output = 1;
			break;
		case 'H':
			if (hostname) {
				free(hostname);
				hostname = NULL;
			}
			hostname = strdup(optarg);
			if (hostname == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'f':
			if (password) {
				free(password);
				password = NULL;
			}
			password = ipmi_password_file_read(optarg);
			if (password == NULL)
				lprintf(LOG_ERR, "Unable to read password "
						"from file %s", optarg);
			break;
		case 'a':
#ifdef HAVE_GETPASSPHRASE
			tmp_pass = getpassphrase("Password: "******"Password: "******"%s: malloc failure", progname);
					goto out_free;
				}
			}
			break;
		case 'k':
			if (kgkey) {
				free(kgkey);
				kgkey = NULL;
			}
			kgkey = strdup(optarg);
			if (kgkey == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'K':
			if ((tmp_env = getenv("IPMI_KGKEY"))) {
				if (kgkey) {
					free(kgkey);
					kgkey = NULL;
				}
				kgkey = strdup(tmp_env);
				if (kgkey == NULL) {
					lprintf(LOG_ERR, "%s: malloc failure", progname);
					goto out_free;
				}
			} else {
				lprintf(LOG_WARN, "Unable to read kgkey from environment");
			}
			break;
		case 'y':
			if (kgkey) {
				free(kgkey);
				kgkey = NULL;
			}
			kgkey = ipmi_parse_hex(optarg);
			if (kgkey == NULL) {
				goto out_free;
			}
			break;
		case 'Y':
#ifdef HAVE_GETPASSPHRASE
			tmp_pass = getpassphrase("Key: ");
#else
			tmp_pass = getpass("Key: ");
#endif
			if (tmp_pass != NULL) {
				if (kgkey) {
					free(kgkey);
					kgkey = NULL;
				}
				kgkey = strdup(tmp_pass);
				tmp_pass = NULL;
				if (kgkey == NULL) {
					lprintf(LOG_ERR, "%s: malloc failure", progname);
					goto out_free;
				}
			}
			break;
		case 'U':
			if (username) {
				free(username);
				username = NULL;
			}
			if (strlen(optarg) > 16) {
				lprintf(LOG_ERR, "Username is too long (> 16 bytes)");
				goto out_free;
			}
			username = strdup(optarg);
			if (username == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'S':
			if (sdrcache) {
				free(sdrcache);
				sdrcache = NULL;
			}
			sdrcache = strdup(optarg);
			if (sdrcache == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'D':
			/* check for subsequent instance of -D */
			if (devfile) {
				/* free memory for previous string */
				free(devfile);
			}
			devfile = strdup(optarg);
			if (devfile == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
#ifdef ENABLE_ALL_OPTIONS
		case 'o':
			if (oemtype) {
				free(oemtype);
				oemtype = NULL;
			}
			oemtype = strdup(optarg);
			if (oemtype == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			if (strncmp(oemtype, "list", 4) == 0 ||
					strncmp(oemtype, "help", 4) == 0) {
				ipmi_oem_print();
				rc = 0;
				goto out_free;
			}
			break;
		case 'g':
			/* backwards compatible oem hack */
			if (oemtype) {
				free(oemtype);
				oemtype = NULL;
			}
			oemtype = strdup("intelwv2");
			break;
		case 's':
			/* backwards compatible oem hack */
			if (oemtype) {
				free(oemtype);
				oemtype = NULL;
			}
			oemtype = strdup("supermicro");
			break;
		case 'P':
			if (password) {
				free(password);
				password = NULL;
			}
			password = strdup(optarg);
			if (password == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}

			/* Prevent password snooping with ps */
			i = strlen(optarg);
			memset(optarg, 'X', i);
			break;
		case 'E':
			if ((tmp_env = getenv("IPMITOOL_PASSWORD"))) {
				if (password) {
					free(password);
					password = NULL;
				}
				password = strdup(tmp_env);
				if (password == NULL) {
					lprintf(LOG_ERR, "%s: malloc failure", progname);
					goto out_free;
				}
			}
			else if ((tmp_env = getenv("IPMI_PASSWORD"))) {
				if (password) {
					free(password);
					password = NULL;
				}
				password = strdup(tmp_env);
				if (password == NULL) {
					lprintf(LOG_ERR, "%s: malloc failure", progname);
					goto out_free;
				}
			}
			else {
				lprintf(LOG_WARN, "Unable to read password from environment");
			}
			break;
		case 'L':
			i = strlen(optarg);
			if ((i > 0) && (optarg[i-1] == '+')) {
				lookupbit = 0;
				optarg[i-1] = 0;
			}
			privlvl = str2val(optarg, ipmi_privlvl_vals);
			if (privlvl == 0xFF) {
				lprintf(LOG_WARN, "Invalid privilege level %s", optarg);
			}
			break;
		case 'A':
			authtype = str2val(optarg, ipmi_authtype_session_vals);
			break;
		case 't':
			if (str2uchar(optarg, &target_addr) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-t'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'b':
			if (str2uchar(optarg, &target_channel) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-b'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'T':
			if (str2uchar(optarg, &transit_addr) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-T'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'B':
			if (str2uchar(optarg, &transit_channel) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-B'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'l':
			if (str2uchar(optarg, &target_lun) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-l'.");
				rc = 1;
				goto out_free;
			}
			break;
		case 'm':
			if (str2uchar(optarg, &arg_addr) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-m'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'e':
			sol_escape_char = optarg[0];
			break;
		case 'O':
			if (seloem) {
				free(seloem);
				seloem = NULL;
			}
			seloem = strdup(optarg);
			if (seloem == NULL) {
				lprintf(LOG_ERR, "%s: malloc failure", progname);
				goto out_free;
			}
			break;
		case 'z':
			if (str2ushort(optarg, &my_long_packet_size) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-z'.");
				rc = -1;
				goto out_free;
			}
			break;
		/* Retry and Timeout */
		case 'R':
			if (str2int(optarg, &retry) != 0 || retry < 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-R'.");
				rc = -1;
				goto out_free;
			}
			break;
		case 'N':
			if (str2uint(optarg, &timeout) != 0) {
				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-N'.");
				rc = -1;
				goto out_free;
			}
			break;
#endif
		default:
			ipmi_option_usage(progname, cmdlist, intflist);
			goto out_free;
		}
	}

	/* check for command before doing anything */
	if (argc-optind > 0 &&
			strncmp(argv[optind], "help", 4) == 0) {
		ipmi_cmd_print(cmdlist);
		rc = 0;
		goto out_free;
	}

	/*
	 * If the user has specified a hostname (-H option)
	 * then this is a remote access session.
	 *
	 * If no password was specified by any other method
	 * and the authtype was not explicitly set to NONE
	 * then prompt the user.
	 */
	if (hostname != NULL && password == NULL &&
			(authtype != IPMI_SESSION_AUTHTYPE_NONE || authtype < 0)) {
#ifdef HAVE_GETPASSPHRASE
		tmp_pass = getpassphrase("Password: "******"Password: "******"%s: malloc failure", progname);
				goto out_free;
			}
		}
	}

	/* if no interface was specified but a
	 * hostname was then use LAN by default
	 * otherwise the default is hardcoded
	 * to use the first entry in the list
	 */
	if (intfname == NULL && hostname != NULL) {
		intfname = strdup("lan");
		if (intfname == NULL) {
			lprintf(LOG_ERR, "%s: malloc failure", progname);
			goto out_free;
		}
	}

	if (password != NULL && intfname != NULL) {
		if (strcmp(intfname, "lan") == 0 && strlen(password) > 16) {
			lprintf(LOG_ERR, "%s: password is longer than 16 bytes.", intfname);
			rc = -1;
			goto out_free;
		} else if (strcmp(intfname, "lanplus") == 0 && strlen(password) > 20) {
			lprintf(LOG_ERR, "%s: password is longer than 20 bytes.", intfname);
			rc = -1;
			goto out_free;
		}
	} /* if (password != NULL && intfname != NULL) */

	/* load interface */
	ipmi_main_intf = ipmi_intf_load(intfname);
	if (ipmi_main_intf == NULL) {
		lprintf(LOG_ERR, "Error loading interface %s", intfname);
		goto out_free;
	}

	/* setup log */
	log_init(progname, 0, verbose);

	/* run OEM setup if found */
	if (oemtype != NULL &&
	    ipmi_oem_setup(ipmi_main_intf, oemtype) < 0) {
		lprintf(LOG_ERR, "OEM setup for \"%s\" failed", oemtype);
		goto out_free;
	}

	/* set session variables */
	if (hostname != NULL)
		ipmi_intf_session_set_hostname(ipmi_main_intf, hostname);
	if (username != NULL)
		ipmi_intf_session_set_username(ipmi_main_intf, username);
	if (password != NULL)
		ipmi_intf_session_set_password(ipmi_main_intf, password);
	if (kgkey != NULL)
		ipmi_intf_session_set_kgkey(ipmi_main_intf, kgkey);
	if (port > 0)
		ipmi_intf_session_set_port(ipmi_main_intf, port);
	if (authtype >= 0)
		ipmi_intf_session_set_authtype(ipmi_main_intf, (uint8_t)authtype);
	if (privlvl > 0)
		ipmi_intf_session_set_privlvl(ipmi_main_intf, (uint8_t)privlvl);
	else
		ipmi_intf_session_set_privlvl(ipmi_main_intf,
				IPMI_SESSION_PRIV_ADMIN);	/* default */
	/* Adding retry and timeout for interface that support it */
	if (retry > 0)
		ipmi_intf_session_set_retry(ipmi_main_intf, retry);
	if (timeout > 0)
		ipmi_intf_session_set_timeout(ipmi_main_intf, timeout);

	ipmi_intf_session_set_lookupbit(ipmi_main_intf, lookupbit);
	ipmi_intf_session_set_sol_escape_char(ipmi_main_intf, sol_escape_char);
	ipmi_intf_session_set_cipher_suite_id(ipmi_main_intf, cipher_suite_id);

	ipmi_main_intf->devnum = devnum;

	/* setup device file if given */
	ipmi_main_intf->devfile = devfile;

	/* Open the interface with the specified or default IPMB address */
	ipmi_main_intf->my_addr = arg_addr ? arg_addr : IPMI_BMC_SLAVE_ADDR;
	if (ipmi_main_intf->open != NULL)
		ipmi_main_intf->open(ipmi_main_intf);

	/*
	 * Attempt picmg discovery of the actual interface address unless
	 * the users specified an address.
	 *	Address specification always overrides discovery
	 */
	if (picmg_discover(ipmi_main_intf) && !arg_addr) {
		lprintf(LOG_DEBUG, "Running PICMG Get Address Info");
		addr = ipmi_picmg_ipmb_address(ipmi_main_intf);
		lprintf(LOG_INFO,  "Discovered IPMB-0 address 0x%x", addr);
	}

	/*
	 * If we discovered the ipmb address and it is not the same as what we
	 * used for open, Set the discovered IPMB address as my address if the
	 * interface supports it.
	 */
	if (addr != 0 && addr != ipmi_main_intf->my_addr &&
						ipmi_main_intf->set_my_addr) {
		/*
		 * Only set the interface address on interfaces which support
		 * it
		 */
		(void) ipmi_main_intf->set_my_addr(ipmi_main_intf, addr);
	}

	/* If bridging addresses are specified, handle them */
	if (transit_addr > 0 || target_addr > 0) {
		/* sanity check, transit makes no sense without a target */
		if ((transit_addr != 0 || transit_channel != 0) &&
			target_addr == 0) {
			lprintf(LOG_ERR,
				"Transit address/channel %#x/%#x ignored. "
				"Target address must be specified!",
				transit_addr, transit_channel);
			goto out_free;
		}
		ipmi_main_intf->target_addr = target_addr;
		ipmi_main_intf->target_lun = target_lun ;
		ipmi_main_intf->target_channel = target_channel ;

		ipmi_main_intf->transit_addr    = transit_addr;
		ipmi_main_intf->transit_channel = transit_channel;


		/* must be admin level to do this over lan */
		ipmi_intf_session_set_privlvl(ipmi_main_intf, IPMI_SESSION_PRIV_ADMIN);
		/* Get the ipmb address of the targeted entity */
		ipmi_main_intf->target_ipmb_addr =
					ipmi_picmg_ipmb_address(ipmi_main_intf);
		lprintf(LOG_DEBUG, "Specified addressing     Target  %#x:%#x Transit %#x:%#x",
					   ipmi_main_intf->target_addr,
					   ipmi_main_intf->target_channel,
					   ipmi_main_intf->transit_addr,
					   ipmi_main_intf->transit_channel);
		if (ipmi_main_intf->target_ipmb_addr) {
			lprintf(LOG_INFO, "Discovered Target IPMB-0 address %#x",
					   ipmi_main_intf->target_ipmb_addr);
		}
	}

	lprintf(LOG_DEBUG, "Interface address: my_addr %#x "
			   "transit %#x:%#x target %#x:%#x "
			   "ipmb_target %#x\n",
			ipmi_main_intf->my_addr,
			ipmi_main_intf->transit_addr,
			ipmi_main_intf->transit_channel,
			ipmi_main_intf->target_addr,
			ipmi_main_intf->target_channel,
			ipmi_main_intf->target_ipmb_addr);

	/* parse local SDR cache if given */
	if (sdrcache != NULL) {
		ipmi_sdr_list_cache_fromfile(ipmi_main_intf, sdrcache);
	}
	/* Parse SEL OEM file if given */
	if (seloem != NULL) {
		ipmi_sel_oem_init(seloem);
	}

	/* Enable Big Buffer when requested */
	ipmi_main_intf->channel_buf_size = 0;
	if ( my_long_packet_size != 0 ) {
		printf("Setting large buffer to %i\n", my_long_packet_size);
		if (ipmi_kontronoem_set_large_buffer( ipmi_main_intf, my_long_packet_size ) == 0)
		{
			my_long_packet_set = 1;
			ipmi_main_intf->channel_buf_size = my_long_packet_size;
		}
	}

	ipmi_main_intf->cmdlist = cmdlist;

	/* now we finally run the command */
	if (argc-optind > 0)
		rc = ipmi_cmd_run(ipmi_main_intf, argv[optind], argc-optind-1,
				&(argv[optind+1]));
	else
		rc = ipmi_cmd_run(ipmi_main_intf, NULL, 0, NULL);

	if (my_long_packet_set == 1) {
		/* Restore defaults */
		ipmi_kontronoem_set_large_buffer( ipmi_main_intf, 0 );
	}

	/* clean repository caches */
	ipmi_cleanup(ipmi_main_intf);

	/* call interface close function if available */
	if (ipmi_main_intf->opened > 0 && ipmi_main_intf->close != NULL)
		ipmi_main_intf->close(ipmi_main_intf);

	out_free:
	log_halt();

	if (intfname != NULL) {
		free(intfname);
		intfname = NULL;
	}
	if (hostname != NULL) {
		free(hostname);
		hostname = NULL;
	}
	if (username != NULL) {
		free(username);
		username = NULL;
	}
	if (password != NULL) {
		free(password);
		password = NULL;
	}
	if (oemtype != NULL) {
		free(oemtype);
		oemtype = NULL;
	}
	if (seloem != NULL) {
		free(seloem);
		seloem = NULL;
	}
	if (kgkey != NULL) {
		free(kgkey);
		kgkey = NULL;
	}
	if (sdrcache != NULL) {
		free(sdrcache);
		sdrcache = NULL;
	}
	if (devfile) {
		free(devfile);
		devfile = NULL;
	}

	return rc;
}