ipmi_user.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * Redistribution of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * Redistribution in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * Neither the name of Sun Microsystems, Inc. or the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * This software is provided "AS IS," without a warranty of any kind.
  20. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  21. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  22. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  23. * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
  24. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  25. * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  26. * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  27. * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  28. * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  29. * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  30. * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  31. */
  32. #define _BSD_SOURCE || \
  33. (_XOPEN_SOURCE >= 500 || \
  34. _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && \
  35. !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39. #include <sys/types.h>
  40. #include <sys/select.h>
  41. #include <sys/time.h>
  42. #include <signal.h>
  43. #include <unistd.h>
  44. #include <ipmitool/helper.h>
  45. #include <ipmitool/log.h>
  46. #include <ipmitool/ipmi.h>
  47. #include <ipmitool/ipmi_intf.h>
  48. #include <ipmitool/ipmi_user.h>
  49. #include <ipmitool/ipmi_constants.h>
  50. #include <ipmitool/ipmi_strings.h>
  51. #include <ipmitool/bswap.h>
  52. extern int verbose;
  53. extern int csv_output;
  54. /* _ipmi_get_user_access - Get User Access for given channel. Results are stored
  55. * into passed struct.
  56. *
  57. * @intf - IPMI interface
  58. * @user_access_rsp - ptr to user_access_t with UID and Channel set
  59. *
  60. * returns - negative number means error, positive is a ccode
  61. */
  62. int
  63. _ipmi_get_user_access(struct ipmi_intf *intf,
  64. struct user_access_t *user_access_rsp)
  65. {
  66. struct ipmi_rq req = {0};
  67. struct ipmi_rs *rsp;
  68. uint8_t data[2];
  69. if (user_access_rsp == NULL) {
  70. return (-3);
  71. }
  72. data[0] = user_access_rsp->channel & 0x0F;
  73. data[1] = user_access_rsp->user_id & 0x3F;
  74. req.msg.netfn = IPMI_NETFN_APP;
  75. req.msg.cmd = IPMI_GET_USER_ACCESS;
  76. req.msg.data = data;
  77. req.msg.data_len = 2;
  78. rsp = intf->sendrecv(intf, &req);
  79. if (rsp == NULL) {
  80. return (-1);
  81. } else if (rsp->ccode != 0) {
  82. return rsp->ccode;
  83. } else if (rsp->data_len != 4) {
  84. return (-2);
  85. }
  86. user_access_rsp->max_user_ids = rsp->data[0] & 0x3F;
  87. user_access_rsp->enable_status = rsp->data[1] & 0xC0;
  88. user_access_rsp->enabled_user_ids = rsp->data[1] & 0x3F;
  89. user_access_rsp->fixed_user_ids = rsp->data[2] & 0x3F;
  90. user_access_rsp->callin_callback = rsp->data[3] & 0x40;
  91. user_access_rsp->link_auth = rsp->data[3] & 0x20;
  92. user_access_rsp->ipmi_messaging = rsp->data[3] & 0x10;
  93. user_access_rsp->privilege_limit = rsp->data[3] & 0x0F;
  94. return rsp->ccode;
  95. }
  96. /* _ipmi_get_user_name - Fetch User Name for given User ID. User Name is stored
  97. * into passed structure.
  98. *
  99. * @intf - ipmi interface
  100. * @user_name - user_name_t struct with UID set
  101. *
  102. * returns - negative number means error, positive is a ccode
  103. */
  104. int
  105. _ipmi_get_user_name(struct ipmi_intf *intf, struct user_name_t *user_name_ptr)
  106. {
  107. struct ipmi_rq req = {0};
  108. struct ipmi_rs *rsp;
  109. uint8_t data[1];
  110. if (user_name_ptr == NULL) {
  111. return (-3);
  112. }
  113. data[0] = user_name_ptr->user_id & 0x3F;
  114. req.msg.netfn = IPMI_NETFN_APP;
  115. req.msg.cmd = IPMI_GET_USER_NAME;
  116. req.msg.data = data;
  117. req.msg.data_len = 1;
  118. rsp = intf->sendrecv(intf, &req);
  119. if (rsp == NULL) {
  120. return (-1);
  121. } else if (rsp->ccode > 0) {
  122. return rsp->ccode;
  123. } else if (rsp->data_len != 16) {
  124. return (-2);
  125. }
  126. memset(user_name_ptr->user_name, '\0', 17);
  127. memcpy(user_name_ptr->user_name, rsp->data, 16);
  128. return rsp->ccode;
  129. }
  130. /* _ipmi_set_user_access - Set User Access for given channel.
  131. *
  132. * @intf - IPMI interface
  133. * @user_access_req - ptr to user_access_t with desired User Access.
  134. * @change_priv_limit_only - change User's privilege limit only
  135. *
  136. * returns - negative number means error, positive is a ccode
  137. */
  138. int
  139. _ipmi_set_user_access(struct ipmi_intf *intf,
  140. struct user_access_t *user_access_req,
  141. uint8_t change_priv_limit_only)
  142. {
  143. uint8_t data[4];
  144. struct ipmi_rq req = {0};
  145. struct ipmi_rs *rsp;
  146. if (user_access_req == NULL) {
  147. return (-3);
  148. }
  149. data[0] = change_priv_limit_only ? 0x00 : 0x80;
  150. if (user_access_req->callin_callback) {
  151. data[0] |= 0x40;
  152. }
  153. if (user_access_req->link_auth) {
  154. data[0] |= 0x20;
  155. }
  156. if (user_access_req->ipmi_messaging) {
  157. data[0] |= 0x10;
  158. }
  159. data[0] |= (user_access_req->channel & 0x0F);
  160. data[1] = user_access_req->user_id & 0x3F;
  161. data[2] = user_access_req->privilege_limit & 0x0F;
  162. data[3] = user_access_req->session_limit & 0x0F;
  163. req.msg.netfn = IPMI_NETFN_APP;
  164. req.msg.cmd = IPMI_SET_USER_ACCESS;
  165. req.msg.data = data;
  166. req.msg.data_len = 4;
  167. rsp = intf->sendrecv(intf, &req);
  168. if (rsp == NULL) {
  169. return (-1);
  170. } else {
  171. return rsp->ccode;
  172. }
  173. }
  174. /* _ipmi_set_user_password - Set User Password command.
  175. *
  176. * @intf - IPMI interface
  177. * @user_id - IPMI User ID
  178. * @operation - which operation to perform(en/disable user, set/test password)
  179. * @password - User Password
  180. * @is_twenty_byte - 0 = store as 16byte, otherwise store as 20byte password
  181. *
  182. * returns - negative number means error, positive is a ccode
  183. */
  184. int
  185. _ipmi_set_user_password(struct ipmi_intf *intf, uint8_t user_id,
  186. uint8_t operation, const char *password,
  187. uint8_t is_twenty_byte)
  188. {
  189. struct ipmi_rq req = {0};
  190. struct ipmi_rs *rsp;
  191. uint8_t *data;
  192. uint8_t data_len = (is_twenty_byte) ? 22 : 18;
  193. data = malloc(sizeof(uint8_t) * data_len);
  194. if (data == NULL) {
  195. return (-4);
  196. }
  197. memset(data, 0, data_len);
  198. data[0] = (is_twenty_byte) ? 0x80 : 0x00;
  199. data[0] |= (0x0F & user_id);
  200. data[1] = 0x03 & operation;
  201. if (password != NULL) {
  202. size_t copy_len = strlen(password);
  203. if (copy_len > (data_len - 2)) {
  204. copy_len = data_len - 2;
  205. } else if (copy_len < 1) {
  206. copy_len = 0;
  207. }
  208. strncpy((char *)(data + 2), password, copy_len);
  209. }
  210. req.msg.netfn = IPMI_NETFN_APP;
  211. req.msg.cmd = IPMI_SET_USER_PASSWORD;
  212. req.msg.data = data;
  213. req.msg.data_len = data_len;
  214. rsp = intf->sendrecv(intf, &req);
  215. free(data);
  216. data = NULL;
  217. if (rsp == NULL) {
  218. return (-1);
  219. }
  220. return rsp->ccode;
  221. }
  222. static void
  223. dump_user_access(const char *user_name,
  224. struct user_access_t *user_access)
  225. {
  226. static int printed_header = 0;
  227. if (!printed_header) {
  228. printf("ID Name Callin Link Auth IPMI Msg "
  229. "Channel Priv Limit\n");
  230. printed_header = 1;
  231. }
  232. printf("%-4d%-17s%-8s%-11s%-11s%-s\n",
  233. user_access->user_id,
  234. user_name,
  235. user_access->callin_callback? "false": "true ",
  236. user_access->link_auth? "true ": "false",
  237. user_access->ipmi_messaging? "true ": "false",
  238. val2str(user_access->privilege_limit,
  239. ipmi_privlvl_vals));
  240. }
  241. static void
  242. dump_user_access_csv(const char *user_name,
  243. struct user_access_t *user_access)
  244. {
  245. printf("%d,%s,%s,%s,%s,%s\n",
  246. user_access->user_id,
  247. user_name,
  248. user_access->callin_callback? "false": "true",
  249. user_access->link_auth? "true": "false",
  250. user_access->ipmi_messaging? "true": "false",
  251. val2str(user_access->privilege_limit,
  252. ipmi_privlvl_vals));
  253. }
  254. /* ipmi_print_user_list - List IPMI Users and their ACLs for given channel.
  255. *
  256. * @intf - IPMI interface
  257. * @channel_number - IPMI channel
  258. *
  259. * returns - 0 on success, (-1) on error
  260. */
  261. static int
  262. ipmi_print_user_list(struct ipmi_intf *intf, uint8_t channel_number)
  263. {
  264. struct user_access_t user_access = {0};
  265. struct user_name_t user_name = {0};
  266. int ccode = 0;
  267. uint8_t current_user_id = 1;
  268. do {
  269. memset(&user_access, 0, sizeof(user_access));
  270. user_access.user_id = current_user_id;
  271. user_access.channel = channel_number;
  272. ccode = _ipmi_get_user_access(intf, &user_access);
  273. if (eval_ccode(ccode) != 0) {
  274. return (-1);
  275. }
  276. memset(&user_name, 0, sizeof(user_name));
  277. user_name.user_id = current_user_id;
  278. ccode = _ipmi_get_user_name(intf, &user_name);
  279. if (ccode == 0xCC) {
  280. user_name.user_id = current_user_id;
  281. memset(&user_name.user_name, '\0', 17);
  282. } else if (eval_ccode(ccode) != 0) {
  283. return (-1);
  284. }
  285. if (csv_output) {
  286. dump_user_access_csv((char *)user_name.user_name,
  287. &user_access);
  288. } else {
  289. dump_user_access((char *)user_name.user_name,
  290. &user_access);
  291. }
  292. ++current_user_id;
  293. } while ((current_user_id <= user_access.max_user_ids)
  294. && (current_user_id <= IPMI_UID_MAX));
  295. return 0;
  296. }
  297. /* ipmi_print_user_summary - print User statistics for given channel
  298. *
  299. * @intf - IPMI interface
  300. * @channel_number - channel number
  301. *
  302. * returns - 0 on success, (-1) on error
  303. */
  304. static int
  305. ipmi_print_user_summary(struct ipmi_intf *intf, uint8_t channel_number)
  306. {
  307. struct user_access_t user_access = {0};
  308. int ccode = 0;
  309. user_access.channel = channel_number;
  310. user_access.user_id = 1;
  311. ccode = _ipmi_get_user_access(intf, &user_access);
  312. if (eval_ccode(ccode) != 0) {
  313. return (-1);
  314. }
  315. if (csv_output) {
  316. printf("%" PRIu8 ",%" PRIu8 ",%" PRIu8 "\n",
  317. user_access.max_user_ids,
  318. user_access.enabled_user_ids,
  319. user_access.fixed_user_ids);
  320. } else {
  321. printf("Maximum IDs : %" PRIu8 "\n",
  322. user_access.max_user_ids);
  323. printf("Enabled User Count : %" PRIu8 "\n",
  324. user_access.enabled_user_ids);
  325. printf("Fixed Name Count : %" PRIu8 "\n",
  326. user_access.fixed_user_ids);
  327. }
  328. return 0;
  329. }
  330. /*
  331. * ipmi_user_set_username
  332. */
  333. static int
  334. ipmi_user_set_username(
  335. struct ipmi_intf *intf,
  336. uint8_t user_id,
  337. const char *name)
  338. {
  339. struct ipmi_rs * rsp;
  340. struct ipmi_rq req;
  341. uint8_t msg_data[17];
  342. /*
  343. * Ensure there is space for the name in the request message buffer
  344. */
  345. if (strlen(name) >= sizeof(msg_data)) {
  346. return -1;
  347. }
  348. memset(&req, 0, sizeof(req));
  349. req.msg.netfn = IPMI_NETFN_APP; /* 0x06 */
  350. req.msg.cmd = IPMI_SET_USER_NAME; /* 0x45 */
  351. req.msg.data = msg_data;
  352. req.msg.data_len = sizeof(msg_data);
  353. memset(msg_data, 0, sizeof(msg_data));
  354. /* The channel number will remain constant throughout this function */
  355. msg_data[0] = user_id;
  356. strncpy((char *)(msg_data + 1), name, strlen(name));
  357. rsp = intf->sendrecv(intf, &req);
  358. if (rsp == NULL) {
  359. lprintf(LOG_ERR, "Set User Name command failed (user %d, name %s)",
  360. user_id, name);
  361. return -1;
  362. }
  363. if (rsp->ccode > 0) {
  364. lprintf(LOG_ERR, "Set User Name command failed (user %d, name %s): %s",
  365. user_id, name, val2str(rsp->ccode, completion_code_vals));
  366. return -1;
  367. }
  368. return 0;
  369. }
  370. /* ipmi_user_test_password - Call _ipmi_set_user_password() with operation bit
  371. * set to test password and interpret result.
  372. */
  373. static int
  374. ipmi_user_test_password(struct ipmi_intf *intf, uint8_t user_id,
  375. const char *password, uint8_t is_twenty_byte_password)
  376. {
  377. int ret = 0;
  378. ret = _ipmi_set_user_password(intf, user_id,
  379. IPMI_PASSWORD_TEST_PASSWORD, password,
  380. is_twenty_byte_password);
  381. switch (ret) {
  382. case 0:
  383. printf("Success\n");
  384. break;
  385. case 0x80:
  386. printf("Failure: password incorrect\n");
  387. break;
  388. case 0x81:
  389. printf("Failure: wrong password size\n");
  390. break;
  391. default:
  392. printf("Unknown error\n");
  393. }
  394. return ((ret == 0) ? 0 : -1);
  395. }
  396. /*
  397. * print_user_usage
  398. */
  399. static void
  400. print_user_usage(void)
  401. {
  402. lprintf(LOG_NOTICE,
  403. "User Commands:");
  404. lprintf(LOG_NOTICE,
  405. " summary [<channel number>]");
  406. lprintf(LOG_NOTICE,
  407. " list [<channel number>]");
  408. lprintf(LOG_NOTICE,
  409. " set name <user id> <username>");
  410. lprintf(LOG_NOTICE,
  411. " set password <user id> [<password> <16|20>]");
  412. lprintf(LOG_NOTICE,
  413. " disable <user id>");
  414. lprintf(LOG_NOTICE,
  415. " enable <user id>");
  416. lprintf(LOG_NOTICE,
  417. " priv <user id> <privilege level> [<channel number>]");
  418. lprintf(LOG_NOTICE,
  419. " Privilege levels:");
  420. lprintf(LOG_NOTICE,
  421. " * 0x1 - Callback");
  422. lprintf(LOG_NOTICE,
  423. " * 0x2 - User");
  424. lprintf(LOG_NOTICE,
  425. " * 0x3 - Operator");
  426. lprintf(LOG_NOTICE,
  427. " * 0x4 - Administrator");
  428. lprintf(LOG_NOTICE,
  429. " * 0x5 - OEM Proprietary");
  430. lprintf(LOG_NOTICE,
  431. " * 0xF - No Access");
  432. lprintf(LOG_NOTICE, "");
  433. lprintf(LOG_NOTICE,
  434. " test <user id> <16|20> [<password]>");
  435. lprintf(LOG_NOTICE, "");
  436. }
  437. const char *
  438. ipmi_user_build_password_prompt(uint8_t user_id)
  439. {
  440. static char prompt[128];
  441. memset(prompt, 0, 128);
  442. snprintf(prompt, 128, "Password for user %d: ", user_id);
  443. return prompt;
  444. }
  445. /* ask_password - ask user for password
  446. *
  447. * @user_id: User ID which will be built-in into text
  448. *
  449. * @returns pointer to char with password
  450. */
  451. char *
  452. ask_password(uint8_t user_id)
  453. {
  454. const char *password_prompt =
  455. ipmi_user_build_password_prompt(user_id);
  456. # ifdef HAVE_GETPASSPHRASE
  457. return getpassphrase(password_prompt);
  458. # else
  459. return (char*)getpass(password_prompt);
  460. # endif
  461. }
  462. int
  463. ipmi_user_summary(struct ipmi_intf *intf, int argc, char **argv)
  464. {
  465. /* Summary*/
  466. uint8_t channel;
  467. if (argc == 1) {
  468. channel = 0x0E; /* Ask about the current channel */
  469. } else if (argc == 2) {
  470. if (is_ipmi_channel_num(argv[1], &channel) != 0) {
  471. return (-1);
  472. }
  473. } else {
  474. print_user_usage();
  475. return (-1);
  476. }
  477. return ipmi_print_user_summary(intf, channel);
  478. }
  479. int
  480. ipmi_user_list(struct ipmi_intf *intf, int argc, char **argv)
  481. {
  482. /* List */
  483. uint8_t channel;
  484. if (argc == 1) {
  485. channel = 0x0E; /* Ask about the current channel */
  486. } else if (argc == 2) {
  487. if (is_ipmi_channel_num(argv[1], &channel) != 0) {
  488. return (-1);
  489. }
  490. } else {
  491. print_user_usage();
  492. return (-1);
  493. }
  494. return ipmi_print_user_list(intf, channel);
  495. }
  496. int
  497. ipmi_user_test(struct ipmi_intf *intf, int argc, char **argv)
  498. {
  499. /* Test */
  500. char *password = NULL;
  501. int password_length = 0;
  502. uint8_t user_id = 0;
  503. /* a little irritating, isn't it */
  504. if (argc != 3 && argc != 4) {
  505. print_user_usage();
  506. return (-1);
  507. }
  508. if (is_ipmi_user_id(argv[1], &user_id)) {
  509. return (-1);
  510. }
  511. if (str2int(argv[2], &password_length) != 0
  512. || (password_length != 16 && password_length != 20)) {
  513. lprintf(LOG_ERR,
  514. "Given password length '%s' is invalid.",
  515. argv[2]);
  516. lprintf(LOG_ERR, "Expected value is either 16 or 20.");
  517. return (-1);
  518. }
  519. if (argc == 3) {
  520. /* We need to prompt for a password */
  521. password = ask_password(user_id);
  522. if (password == NULL) {
  523. lprintf(LOG_ERR, "ipmitool: malloc failure");
  524. return (-1);
  525. }
  526. } else {
  527. password = argv[3];
  528. }
  529. return ipmi_user_test_password(intf,
  530. user_id,
  531. password,
  532. password_length == 20);
  533. }
  534. int
  535. ipmi_user_priv(struct ipmi_intf *intf, int argc, char **argv)
  536. {
  537. struct user_access_t user_access = {0};
  538. int ccode = 0;
  539. if (argc != 3 && argc != 4) {
  540. print_user_usage();
  541. return (-1);
  542. }
  543. if (argc == 4) {
  544. if (is_ipmi_channel_num(argv[3], &user_access.channel) != 0) {
  545. return (-1);
  546. }
  547. } else {
  548. /* Use channel running on */
  549. user_access.channel = 0x0E;
  550. }
  551. if (is_ipmi_user_priv_limit(argv[2], &user_access.privilege_limit) != 0
  552. || is_ipmi_user_id(argv[1], &user_access.user_id) != 0) {
  553. return (-1);
  554. }
  555. ccode = _ipmi_set_user_access(intf, &user_access, 1);
  556. if (eval_ccode(ccode) != 0) {
  557. lprintf(LOG_ERR, "Set Privilege Level command failed (user %d)",
  558. user_access.user_id);
  559. return (-1);
  560. } else {
  561. printf("Set Privilege Level command successful (user %d)\n",
  562. user_access.user_id);
  563. return 0;
  564. }
  565. }
  566. int
  567. ipmi_user_mod(struct ipmi_intf *intf, int argc, char **argv)
  568. {
  569. /* Disable / Enable */
  570. uint8_t user_id;
  571. uint8_t operation;
  572. uint8_t ccode;
  573. if (argc != 2) {
  574. print_user_usage();
  575. return (-1);
  576. }
  577. if (is_ipmi_user_id(argv[1], &user_id)) {
  578. return (-1);
  579. }
  580. operation = (strncmp(argv[0], "disable", 7) == 0) ?
  581. IPMI_PASSWORD_DISABLE_USER : IPMI_PASSWORD_ENABLE_USER;
  582. ccode = _ipmi_set_user_password(intf, user_id, operation,
  583. (char *)NULL, 0);
  584. if (eval_ccode(ccode) != 0) {
  585. lprintf(LOG_ERR, "Set User Password command failed (user %d)",
  586. user_id);
  587. return (-1);
  588. }
  589. return 0;
  590. }
  591. int
  592. ipmi_user_password(struct ipmi_intf *intf, int argc, char **argv)
  593. {
  594. char *password = NULL;
  595. int ccode = 0;
  596. uint8_t password_type = 16;
  597. uint8_t user_id = 0;
  598. if (is_ipmi_user_id(argv[2], &user_id)) {
  599. return (-1);
  600. }
  601. if (argc == 3) {
  602. /* We need to prompt for a password */
  603. char *tmp;
  604. password = ask_password(user_id);
  605. if (password == NULL) {
  606. lprintf(LOG_ERR, "ipmitool: malloc failure");
  607. return (-1);
  608. }
  609. tmp = ask_password(user_id);
  610. if (tmp == NULL) {
  611. lprintf(LOG_ERR, "ipmitool: malloc failure");
  612. return (-1);
  613. }
  614. if (strlen(password) != strlen(tmp)
  615. || strncmp(password, tmp, strlen(tmp))) {
  616. lprintf(LOG_ERR, "Passwords do not match.");
  617. return (-1);
  618. }
  619. } else {
  620. password = argv[3];
  621. if (argc > 4) {
  622. if ((str2uchar(argv[4], &password_type) != 0)
  623. || (password_type != 16 && password_type != 20)) {
  624. lprintf(LOG_ERR, "Invalid password length '%s'", argv[4]);
  625. return (-1);
  626. }
  627. } else {
  628. password_type = 16;
  629. }
  630. }
  631. if (password == NULL) {
  632. lprintf(LOG_ERR, "Unable to parse password argument.");
  633. return (-1);
  634. } else if (strlen(password) > 20) {
  635. lprintf(LOG_ERR, "Password is too long (> 20 bytes)");
  636. return (-1);
  637. }
  638. ccode = _ipmi_set_user_password(intf, user_id,
  639. IPMI_PASSWORD_SET_PASSWORD, password,
  640. password_type > 16);
  641. if (eval_ccode(ccode) != 0) {
  642. lprintf(LOG_ERR, "Set User Password command failed (user %d)",
  643. user_id);
  644. return (-1);
  645. } else {
  646. printf("Set User Password command successful (user %d)\n",
  647. user_id);
  648. return 0;
  649. }
  650. }
  651. int
  652. ipmi_user_name(struct ipmi_intf *intf, int argc, char **argv)
  653. {
  654. /* Set Name */
  655. uint8_t user_id = 0;
  656. if (argc != 4) {
  657. print_user_usage();
  658. return (-1);
  659. }
  660. if (is_ipmi_user_id(argv[2], &user_id)) {
  661. return (-1);
  662. }
  663. if (strlen(argv[3]) > 16) {
  664. lprintf(LOG_ERR, "Username is too long (> 16 bytes)");
  665. return (-1);
  666. }
  667. return ipmi_user_set_username(intf, user_id, argv[3]);
  668. }
  669. /*
  670. * ipmi_user_main
  671. *
  672. * Upon entry to this function argv should contain our arguments
  673. * specific to this subcommand
  674. */
  675. int
  676. ipmi_user_main(struct ipmi_intf *intf, int argc, char **argv)
  677. {
  678. if (argc == 0) {
  679. lprintf(LOG_ERR, "Not enough parameters given.");
  680. print_user_usage();
  681. return (-1);
  682. }
  683. if (strncmp(argv[0], "help", 4) == 0) {
  684. /* Help */
  685. print_user_usage();
  686. return 0;
  687. } else if (strncmp(argv[0], "summary", 7) == 0) {
  688. return ipmi_user_summary(intf, argc, argv);
  689. } else if (strncmp(argv[0], "list", 4) == 0) {
  690. return ipmi_user_list(intf, argc, argv);
  691. } else if (strncmp(argv[0], "test", 4) == 0) {
  692. return ipmi_user_test(intf, argc, argv);
  693. } else if (strncmp(argv[0], "set", 3) == 0) {
  694. /* Set */
  695. if ((argc >= 3)
  696. && (strncmp("password", argv[1], 8) == 0)) {
  697. return ipmi_user_password(intf, argc, argv);
  698. } else if ((argc >= 2)
  699. && (strncmp("name", argv[1], 4) == 0)) {
  700. return ipmi_user_name(intf, argc, argv);
  701. } else {
  702. print_user_usage();
  703. return (-1);
  704. }
  705. } else if (strncmp(argv[0], "priv", 4) == 0) {
  706. return ipmi_user_priv(intf, argc, argv);
  707. } else if ((strncmp(argv[0], "disable", 7) == 0)
  708. || (strncmp(argv[0], "enable", 6) == 0)) {
  709. return ipmi_user_mod(intf, argc, argv);
  710. } else {
  711. lprintf(LOG_ERR, "Invalid user command: '%s'\n", argv[0]);
  712. print_user_usage();
  713. return (-1);
  714. }
  715. }