dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / itadm / itadm.c
blobab369026ae34f9b7878882b4c1d6c7629cae8638
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <getopt.h>
35 #include <strings.h>
36 #include <ctype.h>
37 #include <libnvpair.h>
38 #include <libintl.h>
39 #include <libgen.h>
40 #include <pwd.h>
41 #include <auth_attr.h>
42 #include <secdb.h>
43 #include <libscf.h>
44 #include <limits.h>
45 #include <locale.h>
47 #include <libstmf.h>
48 #include <libiscsit.h>
50 /* what's this used for?? */
51 #define ITADM_VERSION "1.0"
53 /* SMF service info */
54 #define ISCSIT_SVC "svc:/network/iscsi/target:default"
56 #define STMF_STALE(ret) {\
57 if (ret == STMF_ERROR_PROV_DATA_STALE) {\
58 output_config_error(ret, NULL);\
59 } else if (ret != 0) {\
60 output_config_error(ret,\
61 gettext("Configuration change failed"));\
65 #define ITADM_CHKAUTH(sec) {\
66 if (!chkauthattr(sec, itadm_uname)) {\
67 (void) fprintf(stderr,\
68 gettext("Error, operation requires authorization %s"),\
69 sec);\
70 (void) fprintf(stderr, "\n");\
71 return (1);\
76 static struct option itadm_long[] = {
77 {"alias", required_argument, NULL, 'l'},
78 {"auth-method", required_argument, NULL, 'a'},
79 {"chap-secret", no_argument, NULL, 's'},
80 {"chap-secret-file", required_argument, NULL, 'S'},
81 {"chap-user", required_argument, NULL, 'u'},
82 {"force", no_argument, NULL, 'f'},
83 {"help", no_argument, NULL, 'h'},
84 {"help", no_argument, NULL, '?'},
85 {"isns", required_argument, NULL, 'i'},
86 {"isns-server", required_argument, NULL, 'I'},
87 {"node-name", required_argument, NULL, 'n'},
88 {"parsable", no_argument, NULL, 'p'},
89 {"radius-secret", no_argument, NULL, 'd'},
90 {"radius-secret-file", required_argument, NULL, 'D'},
91 {"radius-server", required_argument, NULL, 'r'},
92 {"tpg-tag", required_argument, NULL, 't'},
93 {"verbose", no_argument, NULL, 'v'},
94 {"version", no_argument, NULL, 'V'},
95 {NULL, 0, NULL, 0}
98 char c_tgt[] =
99 " create-target [-a radius|chap|none|default] [-s]\n"
100 " [-S <chap-secret-path>] [-u <chap-user-name>]\n"
101 " [-n <target-node-name>] [-l <alias>]\n"
102 " [-t <tpg-name>[,<tpg-name>]...]";
104 static char m_tgt[] =
105 " modify-target [-a radius|chap|none|default] [-s]\n"
106 " [-S <chap-secret-path>] [-u <chap-username>]\n"
107 " [-n <new-target-node-name>] [-l <alias>]\n"
108 " [-t <tpg-name>[,<tpg-name>]...] <target-node-name>";
110 static char d_tgt[] =
111 " delete-target [-f] <target-node-name>";
113 static char l_tgt[] =
114 " list-target [-pv] [<target-node-name>]";
116 static char c_tpg[] =
117 " create-tpg <tpg-name> <IP-address>[:<port>]...";
119 static char l_tpg[] =
120 " list-tpg [-pv] [<tpg-name>]";
122 static char d_tpg[] =
123 " delete-tpg [-f] <tpg-name>";
125 static char c_ini[] =
126 " create-initiator [-s] [-S <chap-secret-path>]\n"
127 " [-u <chap-username>] <initiator-node-name>";
129 static char m_ini[] =
130 " modify-initiator [-s] [-S <chap-secret-path>]\n"
131 " [-u <chap-username>] <initiator-node-name>";
133 static char l_ini[] =
134 " list-initiator [-pv] [<initiator-node-name>]";
136 static char d_ini[] =
137 " delete-initiator <initiator-node-name>";
139 static char m_def[] =
140 " modify-defaults [-a radius|chap|none] [-r <IP-address>[:<port>]] [-d]\n"
141 " [-D <radius-secret-path>] [-i enable|disable]\n"
142 " [-I <IP-address>[:<port>][,<IP-adddress>[:<port>]]...]";
144 static char l_def[] =
145 " list-defaults [-p]";
148 /* keep the order of this enum in the same order as the 'subcmds' struct */
149 typedef enum {
150 CREATE_TGT,
151 MODIFY_TGT,
152 DELETE_TGT,
153 LIST_TGT,
154 CREATE_TPG,
155 DELETE_TPG,
156 LIST_TPG,
157 CREATE_INI,
158 MODIFY_INI,
159 LIST_INI,
160 DELETE_INI,
161 MODIFY_DEF,
162 LIST_DEF,
163 NULL_SUBCMD /* must always be last! */
164 } itadm_sub_t;
166 typedef struct {
167 char *name;
168 char *shortopts;
169 char *usemsg;
170 } itadm_subcmds_t;
172 static itadm_subcmds_t subcmds[] = {
173 {"create-target", ":a:sS:u:n:l:t:h?", c_tgt},
174 {"modify-target", ":a:sS:u:n:l:t:h?", m_tgt},
175 {"delete-target", ":fh?", d_tgt},
176 {"list-target", ":hpv?", l_tgt},
177 {"create-tpg", ":h?", c_tpg},
178 {"delete-tpg", ":fh?", d_tpg},
179 {"list-tpg", ":hpv?", l_tpg},
180 {"create-initiator", ":sS:u:h?", c_ini},
181 {"modify-initiator", ":sS:u:h?", m_ini},
182 {"list-initiator", ":hpv?", l_ini},
183 {"delete-initiator", ":h?", d_ini},
184 {"modify-defaults", ":a:r:dD:i:I:h?", m_def},
185 {"list-defaults", ":hp?", l_def},
186 {NULL, ":h?", NULL},
189 /* used for checking if user is authorized */
190 static char *itadm_uname = NULL;
192 /* prototypes */
193 static int
194 itadm_get_password(nvlist_t *nvl, char *key, char *passfile,
195 char *phrase);
197 static int
198 itadm_opt_to_arr(nvlist_t *nvl, char *key, char *opt, uint32_t *num);
200 static int
201 create_target(char *tgt, nvlist_t *proplist);
203 static int
204 modify_target(char *tgt, char *new, nvlist_t *proplist);
206 static int
207 delete_target(char *tgt, boolean_t force);
209 static int
210 list_target(char *tgt, boolean_t verbose, boolean_t script);
212 static int
213 create_tpg(char *tpg, int addrc, char **addrs);
215 static int
216 list_tpg(char *tpg, boolean_t verbose, boolean_t script);
218 static int
219 delete_tpg(char *tpg, boolean_t force);
221 static int
222 modify_initiator(char *ini, nvlist_t *proplist, boolean_t create);
224 static int
225 list_initiator(char *ini, boolean_t verbose, boolean_t script);
227 static int
228 delete_initiator(char *ini);
230 static int
231 modify_defaults(nvlist_t *proplist);
233 static int
234 list_defaults(boolean_t script);
236 static void
237 tag_name_to_num(char *tagname, uint16_t *tagnum);
239 /* prototype from iscsit_common.h */
240 extern int
241 sockaddr_to_str(struct sockaddr_storage *sa, char **addr);
243 static void output_config_error(int error_code, char *msg);
246 main(int argc, char *argv[])
248 int ret = 0;
249 int idx = NULL_SUBCMD;
250 char c;
251 int newargc = argc;
252 char **newargv = NULL;
253 char *objp;
254 int itind = 0;
255 nvlist_t *proplist = NULL;
256 boolean_t verbose = B_FALSE;
257 boolean_t script = B_FALSE;
258 boolean_t tbool;
259 char *targetname = NULL;
260 char *propname;
261 boolean_t force = B_FALSE;
262 struct passwd *pwd = NULL;
263 uint32_t count = 0;
264 char *smfstate = NULL;
266 (void) setlocale(LC_ALL, "");
267 (void) textdomain(TEXT_DOMAIN);
269 if (argc < 2) {
270 ret = 1;
271 goto usage_error;
274 for (idx = 0; subcmds[idx].name != NULL; idx++) {
275 if (strcmp(argv[1], subcmds[idx].name) == 0) {
276 break;
281 /* get the caller's user name for subsequent chkauthattr() calls */
282 pwd = getpwuid(getuid());
283 if (pwd == NULL) {
284 (void) fprintf(stderr, "%s\n",
285 gettext("Could not determine callers user name"));
286 return (1);
289 itadm_uname = strdup(pwd->pw_name);
291 /* increment past command & subcommand */
292 newargc--;
293 newargv = &(argv[1]);
295 ret = nvlist_alloc(&proplist, NV_UNIQUE_NAME, 0);
296 if (ret != 0) {
297 ret = errno;
298 output_config_error(ret, gettext("Could not allocate nvlist"));
299 ret = 1;
300 goto usage_error;
303 while ((ret == 0) && (newargv)) {
304 c = getopt_long(newargc, newargv, subcmds[idx].shortopts,
305 itadm_long, &itind);
306 if (c == -1) {
307 break;
310 switch (c) {
311 case 0:
312 /* flag set by getopt */
313 break;
314 case 'a':
315 ret = nvlist_add_string(proplist,
316 "auth", optarg);
317 break;
318 case 'd':
319 ret = itadm_get_password(proplist,
320 "radiussecret", NULL,
321 gettext("Enter RADIUS secret: "));
322 break;
323 case 'D':
324 ret = itadm_get_password(proplist,
325 "radiussecret", optarg, NULL);
326 break;
327 case 'f':
328 force = B_TRUE;
329 break;
330 case '?':
332 * '?' is returned for both unrecognized
333 * options and if explicitly provided on
334 * the command line. The latter should
335 * be handled the same as -h.
337 if (strcmp(newargv[optind-1], "-?") != 0) {
338 (void) fprintf(stderr,
339 gettext("Unrecognized option %s"),
340 newargv[optind-1]);
341 (void) fprintf(stderr, "\n");
342 ret = 1;
344 goto usage_error;
345 case 'h':
346 goto usage_error;
347 case 'i':
348 if (strncmp(optarg, "enable", strlen(optarg))
349 == 0) {
350 tbool = B_TRUE;
351 } else if (strncmp(optarg, "disable",
352 strlen(optarg)) == 0) {
353 tbool = B_FALSE;
354 } else {
355 (void) fprintf(stderr, "%s\n",
356 gettext("invalid value for -i"));
357 ret = 1;
358 break;
360 ret = nvlist_add_boolean_value(proplist,
361 "isns", tbool);
362 break;
363 case 'I':
364 /* possibly multi-valued */
365 ret = itadm_opt_to_arr(proplist,
366 "isnsserver", optarg, &count);
367 if ((ret == 0) && (count > 8)) {
368 (void) fprintf(stderr, "%s\n",
369 gettext(
370 "Too many iSNS servers specified, "
371 "maximum of 8 allowed"));
372 ret = 1;
374 break;
375 case 'l':
376 ret = nvlist_add_string(proplist,
377 "alias", optarg);
378 break;
379 case 'n':
380 targetname = strdup(optarg);
381 if (targetname == NULL) {
382 ret = ENOMEM;
384 break;
385 case 'p':
386 script = B_TRUE;
387 break;
388 case 'r':
389 ret = nvlist_add_string(proplist,
390 "radiusserver", optarg);
391 break;
392 case 's':
393 if ((idx == CREATE_TGT) ||
394 (idx == MODIFY_TGT)) {
395 propname = "targetchapsecret";
396 } else {
397 propname = "chapsecret";
399 ret = itadm_get_password(proplist,
400 propname, NULL,
401 gettext("Enter CHAP secret: "));
402 break;
403 case 'S':
404 if ((idx == CREATE_TGT) ||
405 (idx == MODIFY_TGT)) {
406 propname = "targetchapsecret";
407 } else {
408 propname = "chapsecret";
410 ret = itadm_get_password(proplist,
411 propname, optarg, NULL);
412 break;
413 case 't':
414 /* possibly multi-valued */
415 ret = itadm_opt_to_arr(proplist,
416 "tpg-tag", optarg, NULL);
417 break;
418 case 'u':
419 if ((idx == CREATE_TGT) ||
420 (idx == MODIFY_TGT)) {
421 propname = "targetchapuser";
422 } else {
423 propname = "chapuser";
425 ret = nvlist_add_string(proplist,
426 propname, optarg);
427 break;
428 case 'v':
429 verbose = B_TRUE;
430 break;
431 case ':':
432 (void) fprintf(stderr,
433 gettext("Option %s requires an operand"),
434 newargv[optind-1]);
435 (void) fprintf(stderr, "\n");
437 /* fall through to default */
438 default:
439 ret = 1;
440 break;
444 if (ret != 0) {
445 goto usage_error;
448 /* after getopt() to allow handling of -h option */
449 if ((itadm_sub_t)idx == NULL_SUBCMD) {
450 (void) fprintf(stderr, "%s\n",
451 gettext("Error, no subcommand specified"));
452 ret = 1;
453 goto usage_error;
457 * some subcommands take multiple operands, so adjust now that
458 * getopt is complete
460 newargc -= optind;
461 if (newargc == 0) {
462 newargv = NULL;
463 objp = NULL;
464 } else {
465 newargv = &(newargv[optind]);
466 objp = newargv[0];
469 if (objp == NULL) {
470 switch ((itadm_sub_t)idx) {
471 case MODIFY_TGT:
472 case DELETE_TGT:
473 case CREATE_TPG:
474 case DELETE_TPG:
475 case CREATE_INI:
476 case MODIFY_INI:
477 case DELETE_INI:
478 /* These subcommands need at least one operand */
479 (void) fprintf(stderr,
480 gettext("Error, %s requires an operand"),
481 subcmds[idx].name);
482 (void) fprintf(stderr, "\n");
484 ret = 1;
485 goto usage_error;
486 default:
487 break;
491 if (newargc > 1) {
492 switch ((itadm_sub_t)idx) {
493 case MODIFY_TGT:
494 case DELETE_TGT:
495 case LIST_TGT:
496 case DELETE_TPG:
497 case LIST_TPG:
498 case CREATE_INI:
499 case MODIFY_INI:
500 case LIST_INI:
501 case DELETE_INI:
502 /* These subcommands should have at most one operand */
503 (void) fprintf(stderr,
504 gettext("Error, %s accepts only a single operand"),
505 subcmds[idx].name);
506 (void) fprintf(stderr, "\n");
508 ret = 1;
509 goto usage_error;
511 default:
512 break;
516 if (newargc > 0) {
517 switch ((itadm_sub_t)idx) {
518 case CREATE_TGT:
519 case MODIFY_DEF:
520 case LIST_DEF:
521 /* These subcommands do not support an operand */
522 (void) fprintf(stderr,
523 gettext("Error, %s does not support any operands"),
524 subcmds[idx].name);
525 (void) fprintf(stderr, "\n");
527 ret = 1;
528 goto usage_error;
530 default:
531 break;
536 * XXX - this should probably get pushed down to the library
537 * depending on the decision to allow/disallow configuratoin
538 * without the service running.
541 * Make sure iSCSI target service is enabled before
542 * proceeding.
544 smfstate = smf_get_state(ISCSIT_SVC);
545 if (!smfstate ||
546 (strcmp(smfstate, SCF_STATE_STRING_ONLINE) != 0)) {
547 (void) fprintf(stderr, "%s\n",
548 gettext("The iSCSI target service must be online "
549 "before running this command."));
550 (void) fprintf(stderr,
551 gettext("Use 'svcadm enable -r %s'"), ISCSIT_SVC);
552 (void) fprintf(stderr, "\n");
553 (void) fprintf(stderr, "%s\n",
554 gettext("to enable the service and its prerequisite "
555 "services and/or"));
556 (void) fprintf(stderr,
557 gettext("'svcs -x %s' to determine why it is not online."),
558 ISCSIT_SVC);
559 (void) fprintf(stderr, "\n");
561 return (1);
564 switch ((itadm_sub_t)idx) {
565 case CREATE_TGT:
567 * OK for targetname to be NULL here. If the
568 * user did not specify a target name,
569 * one will be generated.
571 ret = create_target(targetname, proplist);
572 break;
573 case MODIFY_TGT:
574 ret = modify_target(objp, targetname, proplist);
575 break;
576 case DELETE_TGT:
577 ret = delete_target(objp, force);
578 break;
579 case LIST_TGT:
580 ret = list_target(objp, verbose, script);
581 break;
582 case CREATE_TPG:
583 ret = create_tpg(objp, newargc - 1, &(newargv[1]));
584 break;
585 case DELETE_TPG:
586 ret = delete_tpg(objp, force);
587 break;
588 case LIST_TPG:
589 ret = list_tpg(objp, verbose, script);
590 break;
591 case CREATE_INI:
592 ret = modify_initiator(objp, proplist, B_TRUE);
593 break;
594 case MODIFY_INI:
595 ret = modify_initiator(objp, proplist, B_FALSE);
596 break;
597 case LIST_INI:
598 ret = list_initiator(objp, verbose, script);
599 break;
600 case DELETE_INI:
601 ret = delete_initiator(objp);
602 break;
603 case MODIFY_DEF:
604 ret = modify_defaults(proplist);
605 break;
606 case LIST_DEF:
607 ret = list_defaults(script);
608 break;
609 default:
610 ret = 1;
611 goto usage_error;
614 if (ret != 0) {
615 (void) fprintf(stderr,
616 gettext("itadm %s failed with error %d"),
617 subcmds[idx].name, ret);
618 (void) fprintf(stderr, "\n");
620 return (ret);
622 usage_error:
623 if (subcmds[idx].name) {
624 (void) printf("%s\n%s\n", gettext("usage:"),
625 gettext(subcmds[idx].usemsg));
626 } else {
627 /* overall usage */
628 (void) printf("%s\n",
629 gettext("usage: itadm <subcommand> <args> ..."));
630 for (idx = 0; subcmds[idx].name != NULL; idx++) {
631 if (!subcmds[idx].usemsg) {
632 continue;
634 (void) printf("%s\n", gettext(subcmds[idx].usemsg));
638 return (ret);
641 static int
642 create_target(char *tgt, nvlist_t *proplist)
644 int ret;
645 it_config_t *cfg = NULL;
646 it_tgt_t *tgtp;
647 char **tags = NULL;
648 uint32_t count = 0;
649 nvlist_t *errlist = NULL;
650 int i;
651 it_tpg_t *tpg = NULL;
652 uint16_t tagid = 0;
653 it_tpgt_t *tpgt;
654 char *sec = "solaris.smf.modify.stmf";
655 boolean_t did_it_config_load = B_FALSE;
657 ITADM_CHKAUTH(sec);
659 if (tgt) {
661 * Validate target name.
663 if (!IS_IQN_NAME(tgt) && !IS_EUI_NAME(tgt)) {
664 (void) fprintf(stderr, gettext("Invalid name %s"),
665 tgt);
666 (void) fprintf(stderr, "\n");
667 return (EINVAL);
671 ret = it_config_load(&cfg);
672 if (ret != 0) {
673 output_config_error(ret,
674 gettext("Error retrieving iSCSI target configuration"));
675 goto done;
678 did_it_config_load = B_TRUE;
680 ret = it_tgt_create(cfg, &tgtp, tgt);
681 if (ret != 0) {
682 if (ret == EFAULT) {
683 (void) fprintf(stderr,
684 gettext("Invalid iSCSI name %s"), tgt);
685 (void) fprintf(stderr, "\n");
686 } else if (ret == EEXIST) {
687 (void) fprintf(stderr,
688 gettext("iSCSI target %s already configured"),
689 tgt);
690 (void) fprintf(stderr, "\n");
691 } else if (ret == E2BIG) {
692 (void) fprintf(stderr,
693 gettext("Maximum of %d iSCSI targets"),
694 MAX_TARGETS);
695 (void) fprintf(stderr, "\n");
696 } else {
697 output_config_error(ret,
698 gettext("Error creating target"));
701 goto done;
704 /* set the target portal group tags */
705 ret = nvlist_lookup_string_array(proplist, "tpg-tag", &tags,
706 &count);
708 if (ret == ENOENT) {
709 /* none specified. is this ok? */
710 ret = 0;
711 } else if (ret != 0) {
712 output_config_error(ret, gettext("Internal error"));
713 goto done;
716 /* special case, don't set any TPGs */
717 if (tags && (count == 1) && (strcmp("default", tags[0]) == 0)) {
718 count = 0;
721 for (i = 0; i < count; i++) {
722 if (!tags[i]) {
723 continue;
726 /* see that all referenced groups are already defined */
727 tpg = cfg->config_tpg_list;
728 while (tpg != NULL) {
729 if (strcmp(tags[i], tpg->tpg_name) == 0) {
730 break;
733 tpg = tpg->tpg_next;
735 if (tpg == NULL) {
736 (void) fprintf(stderr,
737 gettext("Invalid tpg-tag %s, tag not defined"),
738 tags[i]);
739 (void) fprintf(stderr, "\n");
740 ret = 1;
741 goto done;
744 /* generate the tag number to use */
745 tag_name_to_num(tags[i], &tagid);
747 ret = it_tpgt_create(cfg, tgtp, &tpgt, tags[i], tagid);
748 if (ret != 0) {
749 (void) fprintf(stderr, gettext(
750 "Could not add target portal group tag %s: "),
751 tags[i]);
752 output_config_error(ret, NULL);
753 goto done;
755 tagid++;
758 /* remove the tags from the proplist before continuing */
759 if (tags) {
760 (void) nvlist_remove_all(proplist, "tpg-tag");
763 ret = it_tgt_setprop(cfg, tgtp, proplist, &errlist);
764 if (ret != 0) {
765 (void) fprintf(stderr,
766 gettext("Error setting target properties: %d"), ret);
767 (void) fprintf(stderr, "\n");
768 if (errlist) {
769 nvpair_t *nvp = NULL;
770 char *nn;
771 char *nv;
773 while ((nvp = nvlist_next_nvpair(errlist, nvp))
774 != NULL) {
775 nv = NULL;
777 nn = nvpair_name(nvp);
778 (void) nvpair_value_string(nvp, &nv);
780 if (nv != NULL) {
781 (void) fprintf(stderr, "\t%s: %s\n",
782 nn, nv);
786 nvlist_free(errlist);
788 goto done;
791 if (ret == 0) {
792 ret = it_config_commit(cfg);
793 STMF_STALE(ret);
796 done:
797 if (ret == 0) {
798 (void) printf(gettext("Target %s successfully created"),
799 tgtp->tgt_name);
800 (void) printf("\n");
803 if (did_it_config_load)
804 it_config_free(cfg);
806 return (ret);
810 list_target(char *tgt, boolean_t verbose, boolean_t script)
812 int ret;
813 it_config_t *cfg;
814 it_tgt_t *ptr;
815 boolean_t found = B_FALSE;
816 boolean_t first = B_TRUE;
817 boolean_t first_tag = B_TRUE;
818 char *gauth = "none";
819 char *galias = "-";
820 char *auth;
821 char *alias;
822 char *chapu;
823 char *chaps;
824 it_tpgt_t *tagp;
825 char *sec = "solaris.smf.read.stmf";
826 stmfDevid devid;
827 stmfSessionList *sess = NULL;
828 stmfTargetProperties props;
829 char *state;
830 int num_sessions;
832 ITADM_CHKAUTH(sec);
834 ret = it_config_load(&cfg);
835 if (ret != 0) {
836 output_config_error(ret,
837 gettext("Error retrieving iSCSI target configuration"));
838 return (ret);
841 ptr = cfg->config_tgt_list;
843 /* grab global defaults for auth, alias */
844 if (cfg->config_global_properties) {
845 (void) nvlist_lookup_string(cfg->config_global_properties,
846 "alias", &galias);
847 (void) nvlist_lookup_string(cfg->config_global_properties,
848 "auth", &gauth);
851 for (; ptr != NULL; ptr = ptr->tgt_next) {
852 if (found) {
853 break;
856 if (tgt) {
858 * We do a case-insensitive match in case
859 * a non-lower case value got stored.
861 if (strcasecmp(tgt, ptr->tgt_name) != 0) {
862 continue;
863 } else {
864 found = B_TRUE;
868 state = "-";
869 num_sessions = 0;
870 sess = NULL;
873 * make a best effort to retrieve target status and
874 * number of active sessions from STMF.
876 ret = stmfDevidFromIscsiName(ptr->tgt_name, &devid);
877 if (ret == STMF_STATUS_SUCCESS) {
878 ret = stmfGetTargetProperties(&devid, &props);
879 if (ret == STMF_STATUS_SUCCESS) {
880 if (props.status == STMF_TARGET_PORT_ONLINE) {
881 state = "online";
882 } else {
883 state = "offline";
887 if (ret == STMF_STATUS_SUCCESS) {
888 ret = stmfGetSessionList(&devid, &sess);
889 if (ret == STMF_STATUS_SUCCESS) {
890 num_sessions = sess->cnt;
891 free(sess);
895 /* reset ret so we don't return an error */
896 ret = 0;
898 if (!script && first) {
899 (void) printf("%-61s%-9s%-9s\n", "TARGET NAME",
900 "STATE", "SESSIONS");
901 first = B_FALSE;
904 if (!script) {
906 * try not to let columns run into each other.
907 * Stick a tab after too-long fields.
908 * Lengths chosen are for the 'common' cases.
910 (void) printf("%-61s", ptr->tgt_name);
911 if (strlen(ptr->tgt_name) > 60) {
912 (void) printf("\t");
914 (void) printf("%-9s%-9d", state, num_sessions);
915 } else {
916 (void) printf("%s\t%s\t%d", ptr->tgt_name,
917 state, num_sessions);
920 if (!verbose) {
921 (void) printf("\n");
922 continue;
925 auth = gauth;
926 alias = galias;
927 chapu = "-";
928 chaps = "unset";
930 if (ptr->tgt_properties) {
931 (void) nvlist_lookup_string(ptr->tgt_properties,
932 "auth", &auth);
933 (void) nvlist_lookup_string(ptr->tgt_properties,
934 "alias", &alias);
935 if (nvlist_exists(ptr->tgt_properties,
936 "targetchapsecret")) {
937 chaps = "set";
939 (void) nvlist_lookup_string(ptr->tgt_properties,
940 "targetchapuser", &chapu);
943 if (!script) {
944 (void) printf("\n\t%-20s\t%s\n\t%-20s\t%s %s\n"
945 "\t%-20s\t%s\n\t%-20s\t%s\n\t%-20s\t",
946 "alias:", alias, "auth:", auth,
947 ((auth == gauth) ? "(defaults)" : ""),
948 "targetchapuser:",
949 chapu, "targetchapsecret:", chaps, "tpg-tags:");
950 } else {
951 (void) printf("\t%s\t%s %s\t%s\t%s\t",
952 alias, auth,
953 ((auth == gauth) ? "(defaults)" : ""),
954 chapu, chaps);
957 first_tag = B_TRUE;
958 tagp = ptr->tgt_tpgt_list;
959 for (; tagp != NULL; tagp = tagp->tpgt_next) {
960 if (!first_tag) {
961 (void) printf(",");
962 } else {
963 first_tag = B_FALSE;
965 (void) printf("%s = %d",
966 tagp->tpgt_tpg_name, tagp->tpgt_tag);
969 if (first_tag) {
970 /* didn't find any */
971 (void) printf("default");
974 (void) printf("\n");
977 if (tgt && (!found)) {
978 (void) fprintf(stderr,
979 gettext("Target %s not found!"), tgt);
980 (void) fprintf(stderr, "\n");
981 ret = 1;
984 it_config_free(cfg);
986 return (ret);
990 delete_target(char *tgt, boolean_t force)
992 int ret;
993 it_config_t *cfg;
994 it_tgt_t *ptr;
995 char *sec = "solaris.smf.modify.stmf";
997 ITADM_CHKAUTH(sec);
999 if (!tgt) {
1000 (void) fprintf(stderr, "%s\n",
1001 gettext("Error, no target specified"));
1002 return (EINVAL);
1005 ret = it_config_load(&cfg);
1006 if (ret != 0) {
1007 output_config_error(ret,
1008 gettext("Error retrieving iSCSI target configuration"));
1009 return (ret);
1012 ptr = cfg->config_tgt_list;
1013 while (ptr) {
1015 * We do a case-insensitive match in case
1016 * a non-lower case value got stored.
1018 if (strcasecmp(ptr->tgt_name, tgt) == 0) {
1019 break;
1022 ptr = ptr->tgt_next;
1025 if (ptr) {
1026 ret = it_tgt_delete(cfg, ptr, force);
1028 if (ret != 0) {
1029 if (ret == EBUSY) {
1030 (void) fprintf(stderr,
1031 gettext("The target is online or busy. "
1032 "Use the -f (force) option, or "
1033 "'stmfadm offline-target %s'"), tgt);
1034 (void) fprintf(stderr, "\n");
1035 } else {
1036 output_config_error(ret, gettext(
1037 "Error deleting target"));
1041 if (ret == 0) {
1042 ret = it_config_commit(cfg);
1043 STMF_STALE(ret);
1045 } else {
1046 (void) fprintf(stderr,
1047 gettext("Target %s not found"), tgt);
1048 (void) fprintf(stderr, "\n");
1049 ret = 1;
1052 it_config_free(cfg);
1054 return (ret);
1057 static int
1058 modify_target(char *tgt, char *newname, nvlist_t *proplist)
1060 int ret;
1061 it_config_t *cfg = NULL;
1062 it_tgt_t *ptr = NULL;
1063 it_tgt_t *tgtp = NULL;
1064 char **tags = NULL;
1065 uint32_t count = 0;
1066 nvlist_t *errlist = NULL;
1067 int i;
1068 it_tpg_t *tpg = NULL;
1069 uint16_t tagid;
1070 it_tpgt_t *tpgt = NULL;
1071 char *sec = "solaris.smf.modify.stmf";
1072 boolean_t did_it_config_load = B_FALSE;
1074 ITADM_CHKAUTH(sec);
1076 /* XXX: Do we need to offline anything here too? */
1078 if (!tgt) {
1079 (void) fprintf(stderr, "%s\n",
1080 gettext("Error, no target specified"));
1081 ret = EINVAL;
1082 goto done;
1085 ret = it_config_load(&cfg);
1086 if (ret != 0) {
1087 output_config_error(ret,
1088 gettext("Error retrieving iSCSI target configuration"));
1089 goto done;
1092 did_it_config_load = B_TRUE;
1095 * If newname is specified, ensure it is a valid name.
1097 if (newname) {
1098 if (!validate_iscsi_name(newname)) {
1099 (void) fprintf(stderr,
1100 gettext("Invalid iSCSI name %s"), newname);
1101 (void) fprintf(stderr, "\n");
1102 ret = 1;
1103 goto done;
1108 * Loop through to verify that the target to be modified truly
1109 * exists. If this target is to be renamed, ensure the new
1110 * name is not already in use.
1112 ptr = cfg->config_tgt_list;
1113 while (ptr) {
1115 * Does a target with the new name already exist?
1117 if (newname &&
1118 (strcasecmp(newname, ptr->tgt_name) == 0)) {
1119 (void) fprintf(stderr,
1120 gettext("A target with name %s already exists"),
1121 newname);
1122 (void) fprintf(stderr, "\n");
1123 ret = 1;
1124 goto done;
1127 if (strcasecmp(ptr->tgt_name, tgt) == 0) {
1128 tgtp = ptr;
1131 ptr = ptr ->tgt_next;
1134 if (!tgtp) {
1135 (void) fprintf(stderr,
1136 gettext("Target %s not found"), tgt);
1137 (void) fprintf(stderr, "\n");
1138 ret = EINVAL;
1139 goto done;
1142 /* set the target portal group tags */
1143 ret = nvlist_lookup_string_array(proplist, "tpg-tag", &tags,
1144 &count);
1146 if (ret == ENOENT) {
1147 /* none specified. is this ok? */
1148 ret = 0;
1149 } else if (ret != 0) {
1150 output_config_error(ret, gettext("Internal error"));
1151 goto done;
1154 /* special case, remove all explicit TPGs, and don't add any */
1155 if (tags && (count == 1) && (strcmp("default", tags[0]) == 0)) {
1156 count = 0;
1159 for (i = 0; i < count; i++) {
1160 if (!tags || !tags[i]) {
1161 continue;
1164 /* see that all referenced groups are already defined */
1165 tpg = cfg->config_tpg_list;
1166 while (tpg != NULL) {
1167 if (strcmp(tags[i], tpg->tpg_name) == 0) {
1168 break;
1170 tpg = tpg->tpg_next;
1172 if (tpg == NULL) {
1173 (void) fprintf(stderr,
1174 gettext("Invalid tpg-name %s: not defined"),
1175 tags[i]);
1176 (void) fprintf(stderr, "\n");
1177 ret = 1;
1178 goto done;
1183 * don't recreate tags that are already associated,
1184 * remove tags not requested.
1186 if (tags) {
1187 tpgt = tgtp->tgt_tpgt_list;
1188 while (tpgt) {
1189 for (i = 0; i < count; i++) {
1190 if (!tags[i]) {
1191 continue;
1194 if (strcmp(tpgt->tpgt_tpg_name, tags[i])
1195 == 0) {
1196 /* non-null tags will be created */
1197 tags[i] = NULL;
1198 break;
1201 if (i == count) {
1202 /* one to remove */
1203 it_tpgt_t *ptr = tpgt;
1205 tpgt = ptr->tpgt_next;
1206 it_tpgt_delete(cfg, tgtp, ptr);
1207 } else {
1208 tpgt = tpgt->tpgt_next;
1213 /* see if there are any left to add */
1214 for (i = 0; i < count; i++) {
1215 if (!tags || !tags[i]) {
1216 continue;
1219 /* generate the tag number to use */
1220 tag_name_to_num(tags[i], &tagid);
1222 ret = it_tpgt_create(cfg, tgtp, &tpgt, tags[i], tagid);
1223 if (ret != 0) {
1224 if (ret == E2BIG) {
1225 (void) fprintf(stderr, "%s\n",
1226 gettext("Error, no portal tag available"));
1227 } else {
1228 (void) fprintf(stderr, gettext(
1229 "Could not add target portal group"
1230 " tag %s: "), tags[i]);
1231 output_config_error(ret, NULL);
1233 goto done;
1237 /* remove the tags from the proplist before continuing */
1238 (void) nvlist_remove_all(proplist, "tpg-tag");
1241 * Rename this target, if requested. Save the old name in
1242 * the property list, so the kernel knows this is a renamed
1243 * target, and not a new one.
1245 if (newname && (strlen(newname) > 0)) {
1246 ret = nvlist_add_string(proplist, "oldtargetname",
1247 tgtp->tgt_name);
1248 if (ret != 0) {
1249 output_config_error(ret,
1250 gettext("Error renaming target"));
1251 goto done;
1253 (void) strlcpy(tgtp->tgt_name, newname,
1254 sizeof (tgtp->tgt_name));
1257 ret = it_tgt_setprop(cfg, tgtp, proplist, &errlist);
1258 if (ret != 0) {
1259 (void) fprintf(stderr,
1260 gettext("Error setting target properties: %d"), ret);
1261 (void) fprintf(stderr, "\n");
1262 if (errlist) {
1263 nvpair_t *nvp = NULL;
1264 char *nn;
1265 char *nv;
1267 while ((nvp = nvlist_next_nvpair(errlist, nvp))
1268 != NULL) {
1269 nv = NULL;
1271 nn = nvpair_name(nvp);
1272 (void) nvpair_value_string(nvp, &nv);
1274 if (nv != NULL) {
1275 (void) fprintf(stderr, "\t%s: %s\n",
1276 nn, nv);
1280 nvlist_free(errlist);
1282 goto done;
1285 if (ret == 0) {
1286 ret = it_config_commit(cfg);
1287 STMF_STALE(ret);
1290 done:
1291 if (ret == 0) {
1292 (void) printf(gettext("Target %s successfully modified"),
1293 tgtp->tgt_name);
1294 (void) printf("\n");
1297 if (did_it_config_load)
1298 it_config_free(cfg);
1300 return (ret);
1304 create_tpg(char *tpg, int addrc, char **addrs)
1306 int ret;
1307 it_config_t *cfg;
1308 it_tpg_t *tpgp;
1309 int count = 0;
1310 it_portal_t *ptl;
1311 char *sec = "solaris.smf.modify.stmf";
1312 int i = 0;
1314 ITADM_CHKAUTH(sec);
1316 if (!tpg) {
1317 (void) fprintf(stderr, "%s\n",
1318 gettext("Error, no target portal group specified"));
1319 return (EINVAL);
1322 if (strlen(tpg) > (MAX_TPG_NAMELEN - 1)) {
1323 (void) fprintf(stderr,
1324 gettext("Target Portal Group name must be no longer "
1325 "than %d characters"), (MAX_TPG_NAMELEN - 1));
1326 (void) fprintf(stderr, "\n");
1327 return (EINVAL);
1330 if (!addrs || (addrc <= 0)) {
1331 (void) fprintf(stderr, "%s\n",
1332 gettext("Error, no portal addresses specified"));
1333 return (EINVAL);
1336 ret = it_config_load(&cfg);
1337 if (ret != 0) {
1338 output_config_error(ret,
1339 gettext("Error retrieving iSCSI target configuration"));
1340 return (ret);
1343 tpgp = cfg->config_tpg_list;
1344 while (tpgp != NULL) {
1345 if (strcmp(tpgp->tpg_name, tpg) == 0) {
1346 (void) fprintf(stderr,
1347 gettext("Target Portal Group %s already exists"),
1348 tpg);
1349 (void) fprintf(stderr, "\n");
1350 it_config_free(cfg);
1351 return (1);
1353 tpgp = tpgp->tpg_next;
1357 * Ensure that the addrs don't contain commas.
1359 for (i = 0; i < addrc; i++) {
1360 if (strchr(addrs[i], ',')) {
1361 (void) fprintf(stderr,
1362 gettext("Bad portal name %s"),
1363 addrs[i]);
1364 (void) fprintf(stderr, "\n");
1366 it_config_free(cfg);
1367 return (EINVAL);
1372 * Create the portal group and first portal
1374 ret = it_tpg_create(cfg, &tpgp, tpg, addrs[count]);
1375 if (ret != 0) {
1376 if (ret == EEXIST) {
1377 (void) fprintf(stderr,
1378 gettext("Portal %s already in use"),
1379 addrs[count]);
1380 (void) fprintf(stderr, "\n");
1381 } else {
1382 output_config_error(ret, gettext("Could not create the "
1383 "target portal group"));
1385 it_config_free(cfg);
1386 return (ret);
1390 * Add the remaining portals
1392 for (count = 1; count < addrc; count++) {
1393 if (!addrs[count]) {
1394 continue;
1397 ret = it_portal_create(cfg, tpgp, &ptl, addrs[count]);
1398 if (ret != 0) {
1399 if (ret == EEXIST) {
1400 (void) fprintf(stderr,
1401 gettext("Portal %s already in use"),
1402 addrs[count]);
1403 (void) fprintf(stderr, "\n");
1404 } else {
1405 (void) fprintf(stderr,
1406 gettext("Error adding portal %s: "),
1407 addrs[count]);
1408 output_config_error(ret, NULL);
1409 break;
1414 if (ret == 0) {
1415 ret = it_config_commit(cfg);
1416 STMF_STALE(ret);
1419 it_config_free(cfg);
1421 return (ret);
1424 static int
1425 list_tpg(char *tpg, boolean_t verbose, boolean_t script)
1427 int ret;
1428 it_config_t *cfg;
1429 it_tpg_t *ptr;
1430 boolean_t found = B_FALSE;
1431 it_portal_t *portal;
1432 boolean_t first = B_TRUE;
1433 boolean_t first_portal;
1434 char *pstr;
1435 char *sec = "solaris.smf.read.stmf";
1437 ITADM_CHKAUTH(sec);
1439 ret = it_config_load(&cfg);
1440 if (ret != 0) {
1441 output_config_error(ret,
1442 gettext("Error retrieving iSCSI target configuration"));
1443 return (ret);
1446 ptr = cfg->config_tpg_list;
1448 for (; ptr != NULL; ptr = ptr->tpg_next) {
1449 if (found) {
1450 break;
1453 if (tpg) {
1454 if (strcmp(tpg, ptr->tpg_name) != 0) {
1455 continue;
1456 } else {
1457 found = B_TRUE;
1461 if (!script && first) {
1462 (void) printf("%-30s%-9s\n", "TARGET PORTAL GROUP",
1463 "PORTAL COUNT");
1464 first = B_FALSE;
1467 if (!script) {
1468 (void) printf("%-30s", ptr->tpg_name);
1469 if (strlen(ptr->tpg_name) > 30) {
1470 (void) printf("\t");
1472 (void) printf("%-9d", ptr->tpg_portal_count);
1473 } else {
1474 (void) printf("%s\t%d", ptr->tpg_name,
1475 ptr->tpg_portal_count);
1478 if (!verbose) {
1479 (void) printf("\n");
1480 continue;
1483 if (!script) {
1484 (void) printf("\n portals:");
1487 first_portal = B_TRUE;
1489 portal = ptr->tpg_portal_list;
1490 for (; portal != NULL; portal = portal->portal_next) {
1491 ret = sockaddr_to_str(&(portal->portal_addr), &pstr);
1492 if (ret != 0) {
1493 /* invalid addr? */
1494 continue;
1496 if (!first_portal) {
1497 (void) printf(",");
1498 } else {
1499 (void) printf("\t");
1500 first_portal = B_FALSE;
1503 (void) printf("%s", pstr);
1504 free(pstr);
1507 if (first_portal) {
1508 /* none found */
1509 (void) printf("\t<none>");
1512 (void) printf("\n");
1515 if (tpg && (!found)) {
1516 (void) fprintf(stderr,
1517 gettext("Target Portal Group %s not found!\n"), tpg);
1518 (void) fprintf(stderr, "\n");
1519 ret = 1;
1522 it_config_free(cfg);
1524 return (ret);
1527 static int
1528 delete_tpg(char *tpg, boolean_t force)
1530 int ret;
1531 it_config_t *cfg;
1532 it_tpg_t *ptpg = NULL;
1533 char *sec = "solaris.smf.modify.stmf";
1535 ITADM_CHKAUTH(sec);
1537 if (!tpg) {
1538 (void) fprintf(stderr, "%s\n",
1539 gettext("Error, no target portal group specified"));
1540 return (EINVAL);
1543 ret = it_config_load(&cfg);
1544 if (ret != 0) {
1545 output_config_error(ret,
1546 gettext("Error retrieving iSCSI target configuration"));
1547 return (ret);
1550 ptpg = cfg->config_tpg_list;
1551 for (; ptpg != NULL; ptpg = ptpg->tpg_next) {
1552 if (strcmp(tpg, ptpg->tpg_name) == 0) {
1553 break;
1557 if (!ptpg) {
1558 (void) fprintf(stderr,
1559 gettext("Target portal group %s does not exist"),
1560 tpg);
1561 (void) fprintf(stderr, "\n");
1562 ret = 1;
1563 } else {
1564 ret = it_tpg_delete(cfg, ptpg, force);
1565 if (ret == EBUSY) {
1566 (void) fprintf(stderr, "%s\n",
1567 gettext(
1568 "Target portal group associated with one or more "
1569 "targets. Cannot delete."));
1570 } else if (ret != 0) {
1571 output_config_error(ret, gettext("Could not delete "
1572 "target portal group"));
1575 if (ret == 0) {
1576 ret = it_config_commit(cfg);
1577 STMF_STALE(ret);
1581 it_config_free(cfg);
1583 return (ret);
1586 static int
1587 modify_initiator(char *ini, nvlist_t *proplist, boolean_t create)
1589 int ret;
1590 it_config_t *cfg;
1591 it_ini_t *inip;
1592 nvlist_t *errlist = NULL;
1593 nvpair_t *nvp = NULL;
1594 char *sec = "solaris.smf.modify.stmf";
1595 boolean_t changed = B_TRUE;
1597 ITADM_CHKAUTH(sec);
1599 if (!ini) {
1600 (void) fprintf(stderr, "%s\n",
1601 gettext("Error, no initiator specified"));
1602 return (EINVAL);
1603 } else if (create) {
1605 * validate input name - what are the rules for EUI
1606 * and IQN values?
1608 if (!IS_IQN_NAME(ini) && !IS_EUI_NAME(ini)) {
1609 (void) fprintf(stderr, gettext("Invalid name %s"),
1610 ini);
1611 (void) fprintf(stderr, "\n");
1612 return (EINVAL);
1617 * See if any properties were actually specified.
1619 if (proplist) {
1620 nvp = nvlist_next_nvpair(proplist, nvp);
1623 if ((nvp == NULL) && !create) {
1624 changed = B_FALSE;
1628 * If no properties, and this is really a modify op, verify
1629 * that the requested initiator exists, but then don't do anything.
1630 * Modifying non-existent is an error; doing nothing to a defined
1631 * initiator is not.
1634 ret = it_config_load(&cfg);
1635 if (ret != 0) {
1636 output_config_error(ret,
1637 gettext("Error retrieving iSCSI target configuration"));
1638 return (ret);
1641 inip = cfg->config_ini_list;
1642 while (inip) {
1643 if (strcasecmp(inip->ini_name, ini) == 0) {
1644 break;
1647 inip = inip->ini_next;
1650 if (create) {
1651 if (inip) {
1652 (void) fprintf(stderr,
1653 gettext("Initiator %s already exists"),
1654 inip->ini_name);
1655 (void) fprintf(stderr, "\n");
1656 ret = EINVAL;
1657 } else {
1658 ret = it_ini_create(cfg, &inip, ini);
1659 if (ret != 0) {
1660 if (ret == EFAULT) {
1661 (void) fprintf(stderr,
1662 gettext("Invalid iSCSI name %s"),
1663 ini);
1664 (void) fprintf(stderr, "\n");
1665 } else {
1666 output_config_error(ret, gettext(
1667 "Error creating initiator"));
1671 } else if (!inip) {
1672 ret = ENOENT;
1673 (void) fprintf(stderr,
1674 gettext("Error, initiator %s not found"),
1675 ini);
1676 (void) fprintf(stderr, "\n");
1679 if ((ret == 0) && nvp) {
1680 ret = it_ini_setprop(inip, proplist, &errlist);
1682 if (ret != 0) {
1683 (void) fprintf(stderr,
1684 gettext("Error setting initiator properties: %d"),
1685 ret);
1686 (void) fprintf(stderr, "\n");
1687 if (errlist) {
1688 nvpair_t *nvp = NULL;
1689 char *nn;
1690 char *nv;
1692 while ((nvp = nvlist_next_nvpair(errlist, nvp))
1693 != NULL) {
1694 nv = NULL;
1696 nn = nvpair_name(nvp);
1697 (void) nvpair_value_string(nvp, &nv);
1699 if (nv != NULL) {
1700 (void) fprintf(stderr,
1701 "\t%s: %s\n", nn, nv);
1705 nvlist_free(errlist);
1710 if ((ret == 0) && changed) {
1711 ret = it_config_commit(cfg);
1712 STMF_STALE(ret);
1715 it_config_free(cfg);
1717 return (ret);
1720 static int
1721 list_initiator(char *ini, boolean_t verbose, boolean_t script) /* ARGSUSED */
1723 int ret;
1724 it_config_t *cfg;
1725 it_ini_t *ptr;
1726 boolean_t found = B_FALSE;
1727 boolean_t first = B_TRUE;
1728 char *isecret;
1729 char *iuser;
1730 char *sec = "solaris.smf.read.stmf";
1732 ITADM_CHKAUTH(sec);
1734 ret = it_config_load(&cfg);
1735 if (ret != 0) {
1736 output_config_error(ret,
1737 gettext("Error retrieving iSCSI target configuration"));
1738 return (ret);
1741 ptr = cfg->config_ini_list;
1743 for (; ptr != NULL; ptr = ptr->ini_next) {
1744 isecret = "unset";
1745 iuser = "<none>";
1747 if (found) {
1748 break;
1751 if (ini) {
1752 if (strcasecmp(ini, ptr->ini_name) != 0) {
1753 continue;
1754 } else {
1755 found = B_TRUE;
1759 if (ptr->ini_properties) {
1760 if (nvlist_exists(ptr->ini_properties, "chapsecret")) {
1761 isecret = "set";
1763 (void) nvlist_lookup_string(ptr->ini_properties,
1764 "chapuser", &iuser);
1768 /* there's nothing to print for verbose yet */
1769 if (!script && first) {
1770 (void) printf("%-61s%-10s%-7s\n", "INITIATOR NAME",
1771 "CHAPUSER", "SECRET");
1772 first = B_FALSE;
1775 if (!script) {
1777 * try not to let columns run into each other.
1778 * Stick a tab after too-long fields.
1779 * Lengths chosen are for the 'common' cases.
1781 (void) printf("%-61s", ptr->ini_name);
1783 if (strlen(ptr->ini_name) > 60) {
1784 (void) printf("\t");
1787 (void) printf("%-15s", iuser);
1788 if (strlen(iuser) >= 15) {
1789 (void) printf("\t");
1792 (void) printf("%-4s", isecret);
1793 } else {
1794 (void) printf("%s\t%s\t%s", ptr->ini_name,
1795 iuser, isecret);
1798 (void) printf("\n");
1801 if (ini && (!found)) {
1802 (void) fprintf(stderr,
1803 gettext("Initiator %s not found!"), ini);
1804 (void) fprintf(stderr, "\n");
1805 ret = 1;
1808 it_config_free(cfg);
1810 return (ret);
1814 delete_initiator(char *ini)
1816 int ret;
1817 it_config_t *cfg;
1818 it_ini_t *ptr;
1819 char *sec = "solaris.smf.modify.stmf";
1821 ITADM_CHKAUTH(sec);
1823 if (!ini) {
1824 (void) fprintf(stderr, "%s\n",
1825 gettext("Error, no initiator specified"));
1826 return (EINVAL);
1829 ret = it_config_load(&cfg);
1830 if (ret != 0) {
1831 output_config_error(ret,
1832 gettext("Error retrieving iSCSI target configuration"));
1833 return (ret);
1836 ptr = cfg->config_ini_list;
1837 while (ptr) {
1838 if (strcasecmp(ptr->ini_name, ini) == 0) {
1839 break;
1842 ptr = ptr->ini_next;
1845 if (ptr) {
1846 it_ini_delete(cfg, ptr);
1848 ret = it_config_commit(cfg);
1849 STMF_STALE(ret);
1850 } else {
1851 (void) fprintf(stderr,
1852 gettext("Initiator %s not found"), ini);
1853 (void) fprintf(stderr, "\n");
1854 ret = 1;
1857 return (ret);
1860 static int
1861 modify_defaults(nvlist_t *proplist)
1863 int ret;
1864 it_config_t *cfg;
1865 nvlist_t *errlist = NULL;
1866 nvpair_t *nvp = NULL;
1867 char *sec = "solaris.smf.modify.stmf";
1869 ITADM_CHKAUTH(sec);
1871 if (proplist) {
1872 /* make sure at least one property is specified */
1873 nvp = nvlist_next_nvpair(proplist, nvp);
1876 if (nvp == NULL) {
1877 /* empty list */
1878 (void) fprintf(stderr, "%s\n",
1879 gettext("Error, no properties specified"));
1880 return (EINVAL);
1883 ret = it_config_load(&cfg);
1884 if (ret != 0) {
1885 output_config_error(ret,
1886 gettext("Error retrieving iSCSI target configuration"));
1887 return (ret);
1890 ret = it_config_setprop(cfg, proplist, &errlist);
1891 if (ret != 0) {
1892 (void) fprintf(stderr,
1893 gettext("Error setting global properties: %d"),
1894 ret);
1895 (void) fprintf(stderr, "\n");
1896 if (errlist) {
1897 nvpair_t *nvp = NULL;
1898 char *nn;
1899 char *nv;
1901 while ((nvp = nvlist_next_nvpair(errlist, nvp))
1902 != NULL) {
1903 nv = NULL;
1905 nn = nvpair_name(nvp);
1906 (void) nvpair_value_string(nvp, &nv);
1908 if (nv != NULL) {
1909 (void) fprintf(stderr, "\t%s: %s\n",
1910 nn, nv);
1914 nvlist_free(errlist);
1918 if (ret == 0) {
1919 ret = it_config_commit(cfg);
1920 STMF_STALE(ret);
1923 it_config_free(cfg);
1925 return (ret);
1928 static int
1929 list_defaults(boolean_t script)
1931 int ret;
1932 it_config_t *cfg;
1933 nvlist_t *nvl;
1934 char *alias = "<none>";
1935 char *auth = "<none>";
1936 char *isns = "disabled";
1937 char **isvrs = NULL;
1938 uint32_t scount = 0;
1939 char *rsvr = "<none>";
1940 char *rsecret = "unset";
1941 boolean_t val = B_FALSE;
1942 int i;
1943 char *sec = "solaris.smf.read.stmf";
1945 ITADM_CHKAUTH(sec);
1947 ret = it_config_load(&cfg);
1948 if (ret != 0) {
1949 output_config_error(ret,
1950 gettext("Error retrieving iSCSI target configuration"));
1951 return (ret);
1954 nvl = cfg->config_global_properties;
1956 /* look up all possible options */
1957 (void) nvlist_lookup_string(nvl, "alias", &alias);
1958 (void) nvlist_lookup_string(nvl, "auth", &auth);
1959 (void) nvlist_lookup_boolean_value(nvl, "isns", &val);
1960 if (val == B_TRUE) {
1961 isns = "enabled";
1963 (void) nvlist_lookup_string_array(nvl, "isnsserver", &isvrs,
1964 &scount);
1965 (void) nvlist_lookup_string(nvl, "radiusserver", &rsvr);
1966 if (nvlist_exists(nvl, "radiussecret")) {
1967 rsecret = "set";
1970 if (!script) {
1971 (void) printf("%s:\n\n",
1972 gettext("iSCSI Target Default Properties"));
1975 if (script) {
1976 (void) printf("%s\t%s\t%s\t%s\t%s\t",
1977 alias, auth, rsvr, rsecret, isns);
1978 } else {
1979 (void) printf("%-15s\t%s\n%-15s\t%s\n%-15s\t%s\n%-15s\t%s\n"
1980 "%-15s\t%s\n%-15s\t",
1981 "alias:", alias, "auth:", auth, "radiusserver:",
1982 rsvr, "radiussecret:", rsecret, "isns:", isns,
1983 "isnsserver:");
1986 for (i = 0; i < scount; i++) {
1987 if (!isvrs || !isvrs[i]) {
1988 break;
1990 if (i > 0) {
1991 (void) printf(",");
1993 (void) printf("%s", isvrs[i]);
1996 if (i == 0) {
1997 (void) printf("%s", "<none>");
2000 (void) printf("\n");
2002 it_config_free(cfg);
2004 return (0);
2007 static int
2008 itadm_get_password(nvlist_t *nvl, char *key, char *passfile,
2009 char *phrase)
2011 int ret = 0;
2012 char *pass;
2013 char buf[1024];
2014 int fd;
2015 struct stat64 sbuf;
2016 size_t rd;
2018 if (!nvl || !key) {
2019 return (EINVAL);
2022 if (passfile) {
2023 ret = stat64(passfile, &sbuf);
2024 if ((ret != 0) || (!S_ISREG(sbuf.st_mode))) {
2025 (void) fprintf(stderr,
2026 gettext("Invalid secret file %s"),
2027 passfile);
2028 (void) fprintf(stderr, "\n");
2029 return (EBADF);
2032 fd = open64(passfile, O_RDONLY);
2033 if (fd == -1) {
2034 ret = errno;
2035 (void) fprintf(stderr,
2036 gettext("Could not open secret file %s: "),
2037 passfile);
2038 output_config_error(ret, NULL);
2039 return (ret);
2042 rd = read(fd, buf, sbuf.st_size);
2043 (void) close(fd);
2045 if (rd != sbuf.st_size) {
2046 ret = EIO;
2047 (void) fprintf(stderr,
2048 gettext("Could not read secret file %s: "),
2049 passfile);
2050 output_config_error(ret, NULL);
2051 return (ret);
2054 /* ensure buf is properly terminated */
2055 buf[rd] = '\0';
2057 /* if last char is a newline, strip it off */
2058 if (buf[rd - 1] == '\n') {
2059 buf[rd - 1] = '\0';
2062 /* validate length */
2063 if ((strlen(buf) > 255) || (strlen(buf) < 12)) {
2064 (void) fprintf(stderr, "%s\n",
2065 gettext(
2066 "Secret must be between 12 and 255 characters"));
2067 return (EINVAL);
2069 } else {
2070 /* prompt for secret */
2071 if (!phrase) {
2072 return (EINVAL);
2075 pass = getpassphrase(phrase);
2076 if (!pass) {
2077 ret = errno;
2078 output_config_error(ret,
2079 gettext("Could not read secret"));
2080 return (ret);
2083 /* validate length */
2084 if ((strlen(pass) > 255) || (strlen(pass) < 12)) {
2085 (void) fprintf(stderr, "%s\n",
2086 gettext(
2087 "Secret must be between 12 and 255 characters"));
2088 return (EINVAL);
2091 (void) strlcpy(buf, pass, sizeof (buf));
2093 /* confirm entered secret */
2094 pass = getpassphrase(gettext("Re-enter secret: "));
2095 if (!pass) {
2096 ret = errno;
2097 output_config_error(ret,
2098 gettext("Could not read secret"));
2099 return (ret);
2102 if (strcmp(buf, pass) != 0) {
2103 ret = EINVAL;
2104 (void) fprintf(stderr, "%s\n",
2105 gettext("Secret validation failed"));
2106 return (ret);
2111 ret = nvlist_add_string(nvl, key, buf);
2113 return (ret);
2116 static int
2117 itadm_opt_to_arr(nvlist_t *nvl, char *key, char *opt, uint32_t *num)
2119 int count;
2120 char *bufp;
2121 char **arr;
2123 if (!opt || !key || !nvl) {
2124 return (EINVAL);
2127 bufp = opt;
2128 count = 1;
2130 for (;;) {
2131 bufp = strchr(bufp, ',');
2132 if (!bufp) {
2133 break;
2135 bufp++;
2136 count++;
2139 arr = calloc(count, sizeof (char *));
2140 if (!arr) {
2141 return (ENOMEM);
2144 bufp = opt;
2145 /* set delimiter to comma */
2146 (void) bufsplit(",", 0, NULL);
2148 /* split up that buf! */
2149 (void) bufsplit(bufp, count, arr);
2151 /* if requested, return the number of array members found */
2152 if (num) {
2153 *num = count;
2156 return (nvlist_add_string_array(nvl, key, arr, count));
2159 static void
2160 tag_name_to_num(char *tagname, uint16_t *tagnum)
2162 ulong_t id;
2163 char *ptr = NULL;
2165 if (!tagname || !tagnum) {
2166 return;
2169 *tagnum = 0;
2171 id = strtoul(tagname, &ptr, 10);
2173 /* Must be entirely numeric and in-range */
2174 if (ptr && (*ptr != '\0')) {
2175 return;
2178 if ((id <= UINT16_MAX) && (id > 1)) {
2179 *tagnum = (uint16_t)id;
2184 * Print error messages to stderr for errnos and expected stmf errors.
2185 * This function should generally not be used for cases where the
2186 * calling code can generate a more detailed error message based on
2187 * the contextual knowledge of the meaning of specific errors.
2189 static void
2190 output_config_error(int error, char *msg)
2193 if (msg) {
2194 (void) fprintf(stderr, "%s: ", msg);
2197 if (error & STMF_STATUS_ERROR) {
2198 switch (error) {
2199 case STMF_ERROR_PERM:
2200 (void) fprintf(stderr, "%s",
2201 gettext("permission denied"));
2202 break;
2203 case STMF_ERROR_BUSY:
2204 (void) fprintf(stderr, "%s",
2205 gettext("resource busy"));
2206 break;
2207 case STMF_ERROR_NOMEM:
2208 (void) fprintf(stderr, "%s",
2209 gettext("out of memory"));
2210 break;
2211 case STMF_ERROR_SERVICE_NOT_FOUND:
2212 (void) fprintf(stderr, "%s",
2213 gettext("STMF service not found"));
2214 break;
2215 case STMF_ERROR_SERVICE_DATA_VERSION:
2216 (void) fprintf(stderr, "%s",
2217 gettext("STMF service version incorrect"));
2218 break;
2219 case STMF_ERROR_PROV_DATA_STALE:
2220 (void) fprintf(stderr, "%s",
2221 gettext("Configuration changed during processing. "
2222 "Check the configuration, then retry this "
2223 "command if appropriate."));
2224 break;
2225 default:
2226 (void) fprintf(stderr, "%s", gettext("unknown error"));
2227 break;
2229 } else {
2230 char buf[80] = "";
2232 (void) strerror_r(error, buf, sizeof (buf));
2233 (void) fprintf(stderr, "%s", buf);
2236 (void) fprintf(stderr, "\n");