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]
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/types.h>
35 #include <libnvpair.h>
39 #include <auth_attr.h>
49 /* SMF service info */
50 #define STMF_SVC "svc:/system/stmf:default"
52 #define STMF_STALE(ret) {\
53 if (ret == STMF_ERROR_PROV_DATA_STALE) {\
54 (void) fprintf(stderr, "%s\n",\
55 gettext("Configuration changed during processing. "\
56 "Check the configuration, then retry this command "\
61 #define SRPTADM_CHKAUTH(sec) {\
62 if (!chkauthattr(sec, srptadm_uname)) {\
63 (void) fprintf(stderr,\
64 gettext("Error, operation requires authorization %s"),\
66 (void) fprintf(stderr, "\n");\
71 #define PROPS_FORMAT " %-20s: "
73 static struct option srptadm_long
[] = {
74 {"enable", no_argument
, NULL
, 'e'},
75 {"disable", no_argument
, NULL
, 'd'},
76 {"reset", no_argument
, NULL
, 'r'},
77 {"help", no_argument
, NULL
, '?'},
78 {"help", no_argument
, NULL
, 'h'},
82 static char m_def
[] = "srptadm modify-defaults [-e] [-d]";
83 static char l_def
[] = "srptadm list-defaults";
84 static char s_tgt
[] = "srptadm modify-target [-e] [-d] [-r] <hca>";
85 static char l_tgt
[] = "srptadm list-target [<hca>]";
87 /* keep the order of this enum in the same order as the 'subcmds' struct */
93 NULL_SUBCMD
/* must always be last! */
102 static srptadm_subcmds_t subcmds
[] = {
103 {"modify-defaults", "edh?", m_def
},
104 {"list-defaults", "h?", l_def
},
105 {"modify-target", "edrh?", s_tgt
},
106 {"list-target", "h?", l_tgt
},
110 /* used for checking if user is authorized */
111 static char *srptadm_uname
= NULL
;
114 static int get_local_hcas(char **hcaArray
, int count
);
115 static int print_target_props(char *hca
);
116 static int list_target(char *hca
);
117 static int disable_target(char *hca
);
118 static int reset_target(char *hca
);
119 static int list_defaults(void);
120 static int enable_target(char *hca
);
121 static int set_default_state(boolean_t enabled
);
124 main(int argc
, char *argv
[])
127 int idx
= NULL_SUBCMD
;
130 char **newargv
= NULL
;
133 struct passwd
*pwd
= NULL
;
134 char *smfstate
= NULL
;
135 boolean_t reset
= B_FALSE
;
139 (void) setlocale(LC_ALL
, "");
140 (void) textdomain(TEXT_DOMAIN
);
147 for (idx
= 0; subcmds
[idx
].name
!= NULL
; idx
++) {
148 if (strcmp(argv
[1], subcmds
[idx
].name
) == 0) {
153 /* get the caller's user name for subsequent chkauthattr() calls */
154 pwd
= getpwuid(getuid());
156 (void) fprintf(stderr
, "%s\n",
157 gettext("Could not determine callers user name."));
161 srptadm_uname
= strdup(pwd
->pw_name
);
163 /* increment past command & subcommand */
165 newargv
= &(argv
[1]);
167 while ((ret
== 0) && (newargv
)) {
168 c
= getopt_long(newargc
, newargv
, subcmds
[idx
].shortopts
,
169 srptadm_long
, &srptind
);
176 /* flag set by getopt */
189 * '?' is returned for both unrecognized
190 * options and if explicitly provided on
191 * the command line. The latter should
192 * be handled the same as -h.
194 if (strcmp(newargv
[optind
-1], "-?") != 0) {
195 (void) fprintf(stderr
,
196 gettext("Unrecognized option %s"),
198 (void) fprintf(stderr
, "\n");
205 (void) fprintf(stderr
,
206 gettext("Option %s requires an operand."),
208 (void) fprintf(stderr
, "\n");
210 /* fall through to default */
221 /* after getopt() to allow handling of -h option */
222 if ((srptadm_sub_t
)idx
== NULL_SUBCMD
) {
223 (void) fprintf(stderr
, "%s\n",
224 gettext("Error, no subcommand specified"));
234 newargv
= &(newargv
[optind
]);
239 switch ((srptadm_sub_t
)idx
) {
241 /* These subcommands need operands */
250 switch ((srptadm_sub_t
)idx
) {
253 /* These subcommands should have at most one operand */
264 * Make sure STMF service is enabled before proceeding.
266 smfstate
= smf_get_state(STMF_SVC
);
268 (strcmp(smfstate
, SCF_STATE_STRING_ONLINE
) != 0)) {
269 (void) fprintf(stderr
, "%s\n",
270 gettext("The STMF service must be online "
271 "before running this command."));
272 (void) fprintf(stderr
,
273 gettext("Use 'svcadm enable -r %s'"), STMF_SVC
);
274 (void) fprintf(stderr
, "\n");
275 (void) fprintf(stderr
, "%s\n",
276 gettext("to enable the service and its prerequisite "
278 (void) fprintf(stderr
,
279 gettext("'svcs -x %s' to determine why it is not online."),
281 (void) fprintf(stderr
, "\n");
286 switch ((srptadm_sub_t
)idx
) {
289 ret
= set_default_state(B_TRUE
);
291 ret
= set_default_state(B_FALSE
);
298 ret
= list_defaults();
302 ret
= reset_target(objp
);
304 ret
= enable_target(objp
);
306 ret
= disable_target(objp
);
313 ret
= list_target(objp
);
321 (void) fprintf(stderr
,
322 gettext("srptadm %s failed with error %d"),
323 subcmds
[idx
].name
, ret
);
324 (void) fprintf(stderr
, "\n");
329 if (subcmds
[idx
].name
) {
330 (void) printf("%s\n", gettext(subcmds
[idx
].usemsg
));
333 (void) printf("%s\n\n", gettext("srptadm usage:"));
334 for (idx
= 0; subcmds
[idx
].name
!= NULL
; idx
++) {
335 if (!subcmds
[idx
].usemsg
) {
338 (void) printf("\t%s\n", gettext(subcmds
[idx
].usemsg
));
346 set_default_state(boolean_t enabled
)
349 char *sec
= "solaris.smf.modify.stmf";
351 SRPTADM_CHKAUTH(sec
);
353 ret
= srpt_SetDefaultState(enabled
);
359 enable_target(char *hca
)
362 char *sec
= "solaris.smf.modify.stmf";
364 SRPTADM_CHKAUTH(sec
);
366 ret
= srpt_SetTargetState(hca
, B_TRUE
);
372 disable_target(char *hca
)
375 char *sec
= "solaris.smf.modify.stmf";
377 SRPTADM_CHKAUTH(sec
);
379 ret
= srpt_SetTargetState(hca
, B_FALSE
);
385 reset_target(char *hca
)
388 char *sec
= "solaris.smf.modify.stmf";
390 SRPTADM_CHKAUTH(sec
);
392 ret
= srpt_ResetTarget(hca
);
401 char *sec
= "solaris.smf.read.stmf";
404 SRPTADM_CHKAUTH(sec
);
406 /* only state set as default for now */
407 ret
= srpt_GetDefaultState(&enabled
);
410 (void) printf("%s:\n\n",
411 gettext("SRP Target Service Default Properties"));
413 (void) printf(" %s:\t",
414 gettext("Target creation enabled by default"));
417 (void) printf("%s\n", gettext("true"));
419 (void) printf("%s\n", gettext("false"));
427 list_target(char *hca
)
430 char *sec
= "solaris.smf.read.stmf";
431 char *hcaArr
[1024]; /* way bigger than we'll ever see */
434 SRPTADM_CHKAUTH(sec
);
437 ret
= print_target_props(hca
);
441 /* get list of HCAs configured on this system, from /dev/cfg */
442 (void) memset(&hcaArr
, 0, 1024 * sizeof (char *));
444 ret
= get_local_hcas(hcaArr
, sizeof (hcaArr
));
445 if (ret
== ETOOMANYREFS
) {
446 (void) fprintf(stderr
, "Internal error: too many HCAs\n");
448 } else if (ret
!= 0) {
449 (void) fprintf(stderr
, "Error getting list of HCAs: %d\n", ret
);
453 for (i
= 0; i
< 1024; i
++) {
454 if (hcaArr
[i
] == NULL
) {
457 ret
= print_target_props(hcaArr
[i
]);
461 for (i
= 0; i
< 1024; i
++) {
462 if (hcaArr
[i
] == NULL
) {
472 print_target_props(char *hca
)
480 stmfTargetProperties props
;
483 ret
= srpt_NormalizeGuid(hca
, buf
, sizeof (buf
), &hcaguid
);
485 (void) fprintf(stderr
, "Invalid target HCA: %s\n",
490 /* only property set is enabled */
491 ret
= srpt_GetTargetState(buf
, &enabled
);
493 (void) fprintf(stderr
,
494 "Could not get enabled state for %s: %d\n",
499 (void) printf("Target HCA %s:\n", buf
);
501 (void) printf(PROPS_FORMAT
, gettext("Enabled"));
504 (void) printf("%s\n", gettext("true"));
506 (void) printf("%s\n", gettext("false"));
511 (void) snprintf(euibuf
, sizeof (euibuf
), "eui.%016llX", hcaguid
);
513 ret
= stmfDevidFromIscsiName(euibuf
, &devid
);
514 if (ret
== STMF_STATUS_SUCCESS
) {
515 ret
= stmfGetTargetProperties(&devid
, &props
);
516 if (ret
== STMF_STATUS_SUCCESS
) {
517 if (props
.status
== STMF_TARGET_PORT_ONLINE
) {
525 (void) printf(PROPS_FORMAT
, gettext("SRP Target Name"));
526 (void) printf("%s\n", euibuf
);
527 (void) printf(PROPS_FORMAT
, gettext("Operational Status"));
528 (void) printf("%s\n", state
);
537 get_local_hcas(char **hcaArray
, int count
)
540 char *cfgdir
= "/dev/cfg";
542 struct dirent
*entry
;
546 if ((hcaArray
== NULL
) || (count
== 0)) {
550 dirp
= opendir(cfgdir
);
554 (void) fprintf(stderr
, "Could not open %s: errno %d\n",
559 while ((entry
= readdir(dirp
)) != NULL
) {
560 bufp
= &entry
->d_name
[0];
562 if (strncmp(bufp
, "hca:", 4) != 0) {
568 hcaArray
[idx
] = strdup(bufp
);
569 if (hcaArray
[idx
] == NULL
) {