xref: /titanic_51/usr/src/cmd/srptadm/srptadm.c (revision c3a558e7c77127215b010652905be7916ec5a080)
1*c3a558e7SSue Gleeson /*
2*c3a558e7SSue Gleeson  * CDDL HEADER START
3*c3a558e7SSue Gleeson  *
4*c3a558e7SSue Gleeson  * The contents of this file are subject to the terms of the
5*c3a558e7SSue Gleeson  * Common Development and Distribution License (the "License").
6*c3a558e7SSue Gleeson  * You may not use this file except in compliance with the License.
7*c3a558e7SSue Gleeson  *
8*c3a558e7SSue Gleeson  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*c3a558e7SSue Gleeson  * or http://www.opensolaris.org/os/licensing.
10*c3a558e7SSue Gleeson  * See the License for the specific language governing permissions
11*c3a558e7SSue Gleeson  * and limitations under the License.
12*c3a558e7SSue Gleeson  *
13*c3a558e7SSue Gleeson  * When distributing Covered Code, include this CDDL HEADER in each
14*c3a558e7SSue Gleeson  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*c3a558e7SSue Gleeson  * If applicable, add the following below this CDDL HEADER, with the
16*c3a558e7SSue Gleeson  * fields enclosed by brackets "[]" replaced with your own identifying
17*c3a558e7SSue Gleeson  * information: Portions Copyright [yyyy] [name of copyright owner]
18*c3a558e7SSue Gleeson  *
19*c3a558e7SSue Gleeson  * CDDL HEADER END
20*c3a558e7SSue Gleeson  */
21*c3a558e7SSue Gleeson /*
22*c3a558e7SSue Gleeson  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23*c3a558e7SSue Gleeson  */
24*c3a558e7SSue Gleeson #include <stdlib.h>
25*c3a558e7SSue Gleeson #include <stdio.h>
26*c3a558e7SSue Gleeson #include <sys/types.h>
27*c3a558e7SSue Gleeson #include <sys/stat.h>
28*c3a558e7SSue Gleeson #include <fcntl.h>
29*c3a558e7SSue Gleeson #include <unistd.h>
30*c3a558e7SSue Gleeson #include <errno.h>
31*c3a558e7SSue Gleeson #include <string.h>
32*c3a558e7SSue Gleeson #include <getopt.h>
33*c3a558e7SSue Gleeson #include <strings.h>
34*c3a558e7SSue Gleeson #include <ctype.h>
35*c3a558e7SSue Gleeson #include <libnvpair.h>
36*c3a558e7SSue Gleeson #include <libintl.h>
37*c3a558e7SSue Gleeson #include <libgen.h>
38*c3a558e7SSue Gleeson #include <pwd.h>
39*c3a558e7SSue Gleeson #include <auth_attr.h>
40*c3a558e7SSue Gleeson #include <secdb.h>
41*c3a558e7SSue Gleeson #include <libscf.h>
42*c3a558e7SSue Gleeson #include <limits.h>
43*c3a558e7SSue Gleeson #include <locale.h>
44*c3a558e7SSue Gleeson #include <dirent.h>
45*c3a558e7SSue Gleeson 
46*c3a558e7SSue Gleeson #include <libstmf.h>
47*c3a558e7SSue Gleeson #include <libsrpt.h>
48*c3a558e7SSue Gleeson 
49*c3a558e7SSue Gleeson /* SMF service info */
50*c3a558e7SSue Gleeson #define	STMF_SVC	"svc:/system/stmf:default"
51*c3a558e7SSue Gleeson 
52*c3a558e7SSue Gleeson #define	STMF_STALE(ret) {\
53*c3a558e7SSue Gleeson 	if (ret == STMF_ERROR_PROV_DATA_STALE) {\
54*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "%s\n",\
55*c3a558e7SSue Gleeson 		    gettext("Configuration changed during processing.  "\
56*c3a558e7SSue Gleeson 		    "Check the configuration, then retry this command "\
57*c3a558e7SSue Gleeson 		    "if appropriate."));\
58*c3a558e7SSue Gleeson 	}\
59*c3a558e7SSue Gleeson }
60*c3a558e7SSue Gleeson 
61*c3a558e7SSue Gleeson #define	SRPTADM_CHKAUTH(sec) {\
62*c3a558e7SSue Gleeson 	if (!chkauthattr(sec, srptadm_uname)) {\
63*c3a558e7SSue Gleeson 		(void) fprintf(stderr,\
64*c3a558e7SSue Gleeson 		    gettext("Error, operation requires authorization %s"),\
65*c3a558e7SSue Gleeson 		    sec);\
66*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "\n");\
67*c3a558e7SSue Gleeson 		return (1);\
68*c3a558e7SSue Gleeson 	}\
69*c3a558e7SSue Gleeson }
70*c3a558e7SSue Gleeson 
71*c3a558e7SSue Gleeson #define	PROPS_FORMAT	"    %-20s: "
72*c3a558e7SSue Gleeson 
73*c3a558e7SSue Gleeson static struct option srptadm_long[] = {
74*c3a558e7SSue Gleeson 	{"enable",		no_argument,		NULL, 'e'},
75*c3a558e7SSue Gleeson 	{"disable",		no_argument,		NULL, 'd'},
76*c3a558e7SSue Gleeson 	{"reset",		no_argument,		NULL, 'r'},
77*c3a558e7SSue Gleeson 	{"help",		no_argument,		NULL, '?'},
78*c3a558e7SSue Gleeson 	{"help",		no_argument,		NULL, 'h'},
79*c3a558e7SSue Gleeson 	{NULL, 0, NULL, 0}
80*c3a558e7SSue Gleeson };
81*c3a558e7SSue Gleeson 
82*c3a558e7SSue Gleeson static char m_def[] = "srptadm modify-defaults [-e] [-d]";
83*c3a558e7SSue Gleeson static char l_def[] = "srptadm list-defaults";
84*c3a558e7SSue Gleeson static char s_tgt[] = "srptadm modify-target [-e] [-d] [-r] <hca>";
85*c3a558e7SSue Gleeson static char l_tgt[] = "srptadm list-target [<hca>]";
86*c3a558e7SSue Gleeson 
87*c3a558e7SSue Gleeson /* keep the order of this enum in the same order as the 'subcmds' struct */
88*c3a558e7SSue Gleeson typedef enum {
89*c3a558e7SSue Gleeson 	MODIFY_DEFAULT,
90*c3a558e7SSue Gleeson 	LIST_DEFAULT,
91*c3a558e7SSue Gleeson 	MODIFY_TARGET,
92*c3a558e7SSue Gleeson 	LIST_TARGET,
93*c3a558e7SSue Gleeson 	NULL_SUBCMD	/* must always be last! */
94*c3a558e7SSue Gleeson } srptadm_sub_t;
95*c3a558e7SSue Gleeson 
96*c3a558e7SSue Gleeson typedef struct {
97*c3a558e7SSue Gleeson 	char		*name;
98*c3a558e7SSue Gleeson 	char		*shortopts;
99*c3a558e7SSue Gleeson 	char		*usemsg;
100*c3a558e7SSue Gleeson } srptadm_subcmds_t;
101*c3a558e7SSue Gleeson 
102*c3a558e7SSue Gleeson static srptadm_subcmds_t	subcmds[] = {
103*c3a558e7SSue Gleeson 	{"modify-defaults", "edh?", m_def},
104*c3a558e7SSue Gleeson 	{"list-defaults", "h?", l_def},
105*c3a558e7SSue Gleeson 	{"modify-target", "edrh?", s_tgt},
106*c3a558e7SSue Gleeson 	{"list-target", "h?", l_tgt},
107*c3a558e7SSue Gleeson 	{NULL, ":h?", NULL},
108*c3a558e7SSue Gleeson };
109*c3a558e7SSue Gleeson 
110*c3a558e7SSue Gleeson /* used for checking if user is authorized */
111*c3a558e7SSue Gleeson static char *srptadm_uname = NULL;
112*c3a558e7SSue Gleeson 
113*c3a558e7SSue Gleeson /* prototypes */
114*c3a558e7SSue Gleeson static int get_local_hcas(char **hcaArray, int count);
115*c3a558e7SSue Gleeson static int print_target_props(char *hca);
116*c3a558e7SSue Gleeson static int list_target(char *hca);
117*c3a558e7SSue Gleeson static int disable_target(char *hca);
118*c3a558e7SSue Gleeson static int reset_target(char *hca);
119*c3a558e7SSue Gleeson static int list_defaults(void);
120*c3a558e7SSue Gleeson static int enable_target(char *hca);
121*c3a558e7SSue Gleeson static int set_default_state(boolean_t enabled);
122*c3a558e7SSue Gleeson 
123*c3a558e7SSue Gleeson int
124*c3a558e7SSue Gleeson main(int argc, char *argv[])
125*c3a558e7SSue Gleeson {
126*c3a558e7SSue Gleeson 	int		ret = 0;
127*c3a558e7SSue Gleeson 	int		idx = NULL_SUBCMD;
128*c3a558e7SSue Gleeson 	char		c;
129*c3a558e7SSue Gleeson 	int		newargc = argc;
130*c3a558e7SSue Gleeson 	char		**newargv = NULL;
131*c3a558e7SSue Gleeson 	char		*objp;
132*c3a558e7SSue Gleeson 	int		srptind = 0;
133*c3a558e7SSue Gleeson 	struct passwd	*pwd = NULL;
134*c3a558e7SSue Gleeson 	char		*smfstate = NULL;
135*c3a558e7SSue Gleeson 	boolean_t	reset = B_FALSE;
136*c3a558e7SSue Gleeson 	int		dflag = 0;
137*c3a558e7SSue Gleeson 	int		eflag = 0;
138*c3a558e7SSue Gleeson 
139*c3a558e7SSue Gleeson 	(void) setlocale(LC_ALL, "");
140*c3a558e7SSue Gleeson 	(void) textdomain(TEXT_DOMAIN);
141*c3a558e7SSue Gleeson 
142*c3a558e7SSue Gleeson 	if (argc < 2) {
143*c3a558e7SSue Gleeson 		ret = 1;
144*c3a558e7SSue Gleeson 		goto usage_error;
145*c3a558e7SSue Gleeson 	}
146*c3a558e7SSue Gleeson 
147*c3a558e7SSue Gleeson 	for (idx = 0; subcmds[idx].name != NULL; idx++) {
148*c3a558e7SSue Gleeson 		if (strcmp(argv[1], subcmds[idx].name) == 0) {
149*c3a558e7SSue Gleeson 			break;
150*c3a558e7SSue Gleeson 		}
151*c3a558e7SSue Gleeson 	}
152*c3a558e7SSue Gleeson 
153*c3a558e7SSue Gleeson 	/* get the caller's user name for subsequent chkauthattr() calls */
154*c3a558e7SSue Gleeson 	pwd = getpwuid(getuid());
155*c3a558e7SSue Gleeson 	if (pwd == NULL) {
156*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "%s\n",
157*c3a558e7SSue Gleeson 		    gettext("Could not determine callers user name."));
158*c3a558e7SSue Gleeson 		return (1);
159*c3a558e7SSue Gleeson 	}
160*c3a558e7SSue Gleeson 
161*c3a558e7SSue Gleeson 	srptadm_uname = strdup(pwd->pw_name);
162*c3a558e7SSue Gleeson 
163*c3a558e7SSue Gleeson 	/* increment past command & subcommand */
164*c3a558e7SSue Gleeson 	newargc--;
165*c3a558e7SSue Gleeson 	newargv = &(argv[1]);
166*c3a558e7SSue Gleeson 
167*c3a558e7SSue Gleeson 	while ((ret == 0) && (newargv)) {
168*c3a558e7SSue Gleeson 		c = getopt_long(newargc, newargv, subcmds[idx].shortopts,
169*c3a558e7SSue Gleeson 		    srptadm_long, &srptind);
170*c3a558e7SSue Gleeson 		if (c == -1) {
171*c3a558e7SSue Gleeson 			break;
172*c3a558e7SSue Gleeson 		}
173*c3a558e7SSue Gleeson 
174*c3a558e7SSue Gleeson 		switch (c) {
175*c3a558e7SSue Gleeson 			case 0:
176*c3a558e7SSue Gleeson 				/* flag set by getopt */
177*c3a558e7SSue Gleeson 				break;
178*c3a558e7SSue Gleeson 			case 'd':
179*c3a558e7SSue Gleeson 				dflag++;
180*c3a558e7SSue Gleeson 				break;
181*c3a558e7SSue Gleeson 			case 'e':
182*c3a558e7SSue Gleeson 				eflag++;
183*c3a558e7SSue Gleeson 				break;
184*c3a558e7SSue Gleeson 			case 'r':
185*c3a558e7SSue Gleeson 				reset = B_TRUE;
186*c3a558e7SSue Gleeson 				break;
187*c3a558e7SSue Gleeson 			case '?':
188*c3a558e7SSue Gleeson 				/*
189*c3a558e7SSue Gleeson 				 * '?' is returned for both unrecognized
190*c3a558e7SSue Gleeson 				 * options and if explicitly provided on
191*c3a558e7SSue Gleeson 				 * the command line.  The latter should
192*c3a558e7SSue Gleeson 				 * be handled the same as -h.
193*c3a558e7SSue Gleeson 				 */
194*c3a558e7SSue Gleeson 				if (strcmp(newargv[optind-1], "-?") != 0) {
195*c3a558e7SSue Gleeson 					(void) fprintf(stderr,
196*c3a558e7SSue Gleeson 					    gettext("Unrecognized option %s"),
197*c3a558e7SSue Gleeson 					    newargv[optind-1]);
198*c3a558e7SSue Gleeson 					(void) fprintf(stderr, "\n");
199*c3a558e7SSue Gleeson 					ret = 1;
200*c3a558e7SSue Gleeson 				}
201*c3a558e7SSue Gleeson 				goto usage_error;
202*c3a558e7SSue Gleeson 			case 'h':
203*c3a558e7SSue Gleeson 				goto usage_error;
204*c3a558e7SSue Gleeson 			case ':':
205*c3a558e7SSue Gleeson 				(void) fprintf(stderr,
206*c3a558e7SSue Gleeson 				    gettext("Option %s requires an operand."),
207*c3a558e7SSue Gleeson 				    newargv[optind-1]);
208*c3a558e7SSue Gleeson 				(void) fprintf(stderr, "\n");
209*c3a558e7SSue Gleeson 
210*c3a558e7SSue Gleeson 				/* fall through to default */
211*c3a558e7SSue Gleeson 			default:
212*c3a558e7SSue Gleeson 				ret = 1;
213*c3a558e7SSue Gleeson 				break;
214*c3a558e7SSue Gleeson 		}
215*c3a558e7SSue Gleeson 	}
216*c3a558e7SSue Gleeson 
217*c3a558e7SSue Gleeson 	if (ret != 0) {
218*c3a558e7SSue Gleeson 		goto usage_error;
219*c3a558e7SSue Gleeson 	}
220*c3a558e7SSue Gleeson 
221*c3a558e7SSue Gleeson 	/* after getopt() to allow handling of -h option */
222*c3a558e7SSue Gleeson 	if ((srptadm_sub_t)idx == NULL_SUBCMD) {
223*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "%s\n",
224*c3a558e7SSue Gleeson 		    gettext("Error, no subcommand specified"));
225*c3a558e7SSue Gleeson 		ret = 1;
226*c3a558e7SSue Gleeson 		goto usage_error;
227*c3a558e7SSue Gleeson 	}
228*c3a558e7SSue Gleeson 
229*c3a558e7SSue Gleeson 	newargc -= optind;
230*c3a558e7SSue Gleeson 	if (newargc == 0) {
231*c3a558e7SSue Gleeson 		newargv = NULL;
232*c3a558e7SSue Gleeson 		objp = NULL;
233*c3a558e7SSue Gleeson 	} else {
234*c3a558e7SSue Gleeson 		newargv = &(newargv[optind]);
235*c3a558e7SSue Gleeson 		objp = newargv[0];
236*c3a558e7SSue Gleeson 	}
237*c3a558e7SSue Gleeson 
238*c3a558e7SSue Gleeson 	if (objp == NULL) {
239*c3a558e7SSue Gleeson 		switch ((srptadm_sub_t)idx) {
240*c3a558e7SSue Gleeson 		case MODIFY_TARGET:
241*c3a558e7SSue Gleeson 			/* These subcommands need operands */
242*c3a558e7SSue Gleeson 			ret = 1;
243*c3a558e7SSue Gleeson 			goto usage_error;
244*c3a558e7SSue Gleeson 		default:
245*c3a558e7SSue Gleeson 			break;
246*c3a558e7SSue Gleeson 		}
247*c3a558e7SSue Gleeson 	}
248*c3a558e7SSue Gleeson 
249*c3a558e7SSue Gleeson 	if (newargc > 1) {
250*c3a558e7SSue Gleeson 		switch ((srptadm_sub_t)idx) {
251*c3a558e7SSue Gleeson 		case MODIFY_TARGET:
252*c3a558e7SSue Gleeson 		case LIST_TARGET:
253*c3a558e7SSue Gleeson 			/* These subcommands should have at most one operand */
254*c3a558e7SSue Gleeson 			ret = 1;
255*c3a558e7SSue Gleeson 			goto usage_error;
256*c3a558e7SSue Gleeson 
257*c3a558e7SSue Gleeson 		default:
258*c3a558e7SSue Gleeson 			break;
259*c3a558e7SSue Gleeson 		}
260*c3a558e7SSue Gleeson 	}
261*c3a558e7SSue Gleeson 
262*c3a558e7SSue Gleeson 
263*c3a558e7SSue Gleeson 	/*
264*c3a558e7SSue Gleeson 	 * Make sure STMF service is enabled before proceeding.
265*c3a558e7SSue Gleeson 	 */
266*c3a558e7SSue Gleeson 	smfstate = smf_get_state(STMF_SVC);
267*c3a558e7SSue Gleeson 	if (!smfstate ||
268*c3a558e7SSue Gleeson 	    (strcmp(smfstate, SCF_STATE_STRING_ONLINE) != 0)) {
269*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "%s\n",
270*c3a558e7SSue Gleeson 		    gettext("The STMF service must be online "
271*c3a558e7SSue Gleeson 		    "before running this command."));
272*c3a558e7SSue Gleeson 		(void) fprintf(stderr,
273*c3a558e7SSue Gleeson 		    gettext("Use 'svcadm enable -r %s'"), STMF_SVC);
274*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "\n");
275*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "%s\n",
276*c3a558e7SSue Gleeson 		    gettext("to enable the service and its prerequisite "
277*c3a558e7SSue Gleeson 		    "services and/or"));
278*c3a558e7SSue Gleeson 		(void) fprintf(stderr,
279*c3a558e7SSue Gleeson 		    gettext("'svcs -x %s' to determine why it is not online."),
280*c3a558e7SSue Gleeson 		    STMF_SVC);
281*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "\n");
282*c3a558e7SSue Gleeson 
283*c3a558e7SSue Gleeson 		return (1);
284*c3a558e7SSue Gleeson 	}
285*c3a558e7SSue Gleeson 
286*c3a558e7SSue Gleeson 	switch ((srptadm_sub_t)idx) {
287*c3a558e7SSue Gleeson 		case MODIFY_DEFAULT:
288*c3a558e7SSue Gleeson 			if (eflag) {
289*c3a558e7SSue Gleeson 				ret = set_default_state(B_TRUE);
290*c3a558e7SSue Gleeson 			} else if (dflag) {
291*c3a558e7SSue Gleeson 				ret = set_default_state(B_FALSE);
292*c3a558e7SSue Gleeson 			} else {
293*c3a558e7SSue Gleeson 				ret = 1;
294*c3a558e7SSue Gleeson 				goto usage_error;
295*c3a558e7SSue Gleeson 			}
296*c3a558e7SSue Gleeson 			break;
297*c3a558e7SSue Gleeson 		case LIST_DEFAULT:
298*c3a558e7SSue Gleeson 			ret = list_defaults();
299*c3a558e7SSue Gleeson 			break;
300*c3a558e7SSue Gleeson 		case MODIFY_TARGET:
301*c3a558e7SSue Gleeson 			if (reset) {
302*c3a558e7SSue Gleeson 				ret = reset_target(objp);
303*c3a558e7SSue Gleeson 			} else if (eflag) {
304*c3a558e7SSue Gleeson 				ret = enable_target(objp);
305*c3a558e7SSue Gleeson 			} else if (dflag) {
306*c3a558e7SSue Gleeson 				ret = disable_target(objp);
307*c3a558e7SSue Gleeson 			} else {
308*c3a558e7SSue Gleeson 				ret = 1;
309*c3a558e7SSue Gleeson 				goto usage_error;
310*c3a558e7SSue Gleeson 			}
311*c3a558e7SSue Gleeson 			break;
312*c3a558e7SSue Gleeson 		case LIST_TARGET:
313*c3a558e7SSue Gleeson 			ret = list_target(objp);
314*c3a558e7SSue Gleeson 			break;
315*c3a558e7SSue Gleeson 		default:
316*c3a558e7SSue Gleeson 			ret = 1;
317*c3a558e7SSue Gleeson 			goto usage_error;
318*c3a558e7SSue Gleeson 	}
319*c3a558e7SSue Gleeson 
320*c3a558e7SSue Gleeson 	if (ret != 0) {
321*c3a558e7SSue Gleeson 		(void) fprintf(stderr,
322*c3a558e7SSue Gleeson 		    gettext("srptadm %s failed with error %d"),
323*c3a558e7SSue Gleeson 		    subcmds[idx].name, ret);
324*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "\n");
325*c3a558e7SSue Gleeson 	}
326*c3a558e7SSue Gleeson 	return (ret);
327*c3a558e7SSue Gleeson 
328*c3a558e7SSue Gleeson usage_error:
329*c3a558e7SSue Gleeson 	if (subcmds[idx].name) {
330*c3a558e7SSue Gleeson 		(void) printf("%s\n", gettext(subcmds[idx].usemsg));
331*c3a558e7SSue Gleeson 	} else {
332*c3a558e7SSue Gleeson 		/* overall usage */
333*c3a558e7SSue Gleeson 		(void) printf("%s\n\n", gettext("srptadm usage:"));
334*c3a558e7SSue Gleeson 		for (idx = 0; subcmds[idx].name != NULL; idx++) {
335*c3a558e7SSue Gleeson 			if (!subcmds[idx].usemsg) {
336*c3a558e7SSue Gleeson 				continue;
337*c3a558e7SSue Gleeson 			}
338*c3a558e7SSue Gleeson 			(void) printf("\t%s\n", gettext(subcmds[idx].usemsg));
339*c3a558e7SSue Gleeson 		}
340*c3a558e7SSue Gleeson 	}
341*c3a558e7SSue Gleeson 
342*c3a558e7SSue Gleeson 	return (ret);
343*c3a558e7SSue Gleeson }
344*c3a558e7SSue Gleeson 
345*c3a558e7SSue Gleeson static int
346*c3a558e7SSue Gleeson set_default_state(boolean_t enabled)
347*c3a558e7SSue Gleeson {
348*c3a558e7SSue Gleeson 	int		ret;
349*c3a558e7SSue Gleeson 	char		*sec = "solaris.smf.modify.stmf";
350*c3a558e7SSue Gleeson 
351*c3a558e7SSue Gleeson 	SRPTADM_CHKAUTH(sec);
352*c3a558e7SSue Gleeson 
353*c3a558e7SSue Gleeson 	ret = srpt_SetDefaultState(enabled);
354*c3a558e7SSue Gleeson 
355*c3a558e7SSue Gleeson 	return (ret);
356*c3a558e7SSue Gleeson }
357*c3a558e7SSue Gleeson 
358*c3a558e7SSue Gleeson static int
359*c3a558e7SSue Gleeson enable_target(char *hca)
360*c3a558e7SSue Gleeson {
361*c3a558e7SSue Gleeson 	int		ret;
362*c3a558e7SSue Gleeson 	char		*sec = "solaris.smf.modify.stmf";
363*c3a558e7SSue Gleeson 
364*c3a558e7SSue Gleeson 	SRPTADM_CHKAUTH(sec);
365*c3a558e7SSue Gleeson 
366*c3a558e7SSue Gleeson 	ret = srpt_SetTargetState(hca, B_TRUE);
367*c3a558e7SSue Gleeson 
368*c3a558e7SSue Gleeson 	return (ret);
369*c3a558e7SSue Gleeson }
370*c3a558e7SSue Gleeson 
371*c3a558e7SSue Gleeson static int
372*c3a558e7SSue Gleeson disable_target(char *hca)
373*c3a558e7SSue Gleeson {
374*c3a558e7SSue Gleeson 	int		ret;
375*c3a558e7SSue Gleeson 	char		*sec = "solaris.smf.modify.stmf";
376*c3a558e7SSue Gleeson 
377*c3a558e7SSue Gleeson 	SRPTADM_CHKAUTH(sec);
378*c3a558e7SSue Gleeson 
379*c3a558e7SSue Gleeson 	ret = srpt_SetTargetState(hca, B_FALSE);
380*c3a558e7SSue Gleeson 
381*c3a558e7SSue Gleeson 	return (ret);
382*c3a558e7SSue Gleeson }
383*c3a558e7SSue Gleeson 
384*c3a558e7SSue Gleeson static int
385*c3a558e7SSue Gleeson reset_target(char *hca)
386*c3a558e7SSue Gleeson {
387*c3a558e7SSue Gleeson 	int		ret;
388*c3a558e7SSue Gleeson 	char		*sec = "solaris.smf.modify.stmf";
389*c3a558e7SSue Gleeson 
390*c3a558e7SSue Gleeson 	SRPTADM_CHKAUTH(sec);
391*c3a558e7SSue Gleeson 
392*c3a558e7SSue Gleeson 	ret = srpt_ResetTarget(hca);
393*c3a558e7SSue Gleeson 
394*c3a558e7SSue Gleeson 	return (ret);
395*c3a558e7SSue Gleeson }
396*c3a558e7SSue Gleeson 
397*c3a558e7SSue Gleeson static int
398*c3a558e7SSue Gleeson list_defaults(void)
399*c3a558e7SSue Gleeson {
400*c3a558e7SSue Gleeson 	int		ret;
401*c3a558e7SSue Gleeson 	char		*sec = "solaris.smf.read.stmf";
402*c3a558e7SSue Gleeson 	boolean_t	enabled;
403*c3a558e7SSue Gleeson 
404*c3a558e7SSue Gleeson 	SRPTADM_CHKAUTH(sec);
405*c3a558e7SSue Gleeson 
406*c3a558e7SSue Gleeson 	/* only state set as default for now */
407*c3a558e7SSue Gleeson 	ret = srpt_GetDefaultState(&enabled);
408*c3a558e7SSue Gleeson 
409*c3a558e7SSue Gleeson 	if (ret == 0) {
410*c3a558e7SSue Gleeson 		(void) printf("%s:\n\n",
411*c3a558e7SSue Gleeson 		    gettext("SRP Target Service Default Properties"));
412*c3a558e7SSue Gleeson 
413*c3a558e7SSue Gleeson 		(void) printf("    %s:\t",
414*c3a558e7SSue Gleeson 		    gettext("Target creation enabled by default"));
415*c3a558e7SSue Gleeson 
416*c3a558e7SSue Gleeson 		if (enabled) {
417*c3a558e7SSue Gleeson 			(void) printf("%s\n", gettext("true"));
418*c3a558e7SSue Gleeson 		} else {
419*c3a558e7SSue Gleeson 			(void) printf("%s\n", gettext("false"));
420*c3a558e7SSue Gleeson 		}
421*c3a558e7SSue Gleeson 	}
422*c3a558e7SSue Gleeson 
423*c3a558e7SSue Gleeson 	return (ret);
424*c3a558e7SSue Gleeson }
425*c3a558e7SSue Gleeson 
426*c3a558e7SSue Gleeson static int
427*c3a558e7SSue Gleeson list_target(char *hca)
428*c3a558e7SSue Gleeson {
429*c3a558e7SSue Gleeson 	int		ret;
430*c3a558e7SSue Gleeson 	char		*sec = "solaris.smf.read.stmf";
431*c3a558e7SSue Gleeson 	char		*hcaArr[1024];	/* way bigger than we'll ever see */
432*c3a558e7SSue Gleeson 	int		i;
433*c3a558e7SSue Gleeson 
434*c3a558e7SSue Gleeson 	SRPTADM_CHKAUTH(sec);
435*c3a558e7SSue Gleeson 
436*c3a558e7SSue Gleeson 	if (hca != NULL) {
437*c3a558e7SSue Gleeson 		ret = print_target_props(hca);
438*c3a558e7SSue Gleeson 		return (ret);
439*c3a558e7SSue Gleeson 	}
440*c3a558e7SSue Gleeson 
441*c3a558e7SSue Gleeson 	/* get list of HCAs configured on this system, from /dev/cfg */
442*c3a558e7SSue Gleeson 	(void) memset(&hcaArr, 0, 1024 * sizeof (char *));
443*c3a558e7SSue Gleeson 
444*c3a558e7SSue Gleeson 	ret = get_local_hcas(hcaArr, sizeof (hcaArr));
445*c3a558e7SSue Gleeson 	if (ret == ETOOMANYREFS) {
446*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "Internal error:  too many HCAs\n");
447*c3a558e7SSue Gleeson 		goto done;
448*c3a558e7SSue Gleeson 	} else if (ret != 0) {
449*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "Error getting list of HCAs: %d\n", ret);
450*c3a558e7SSue Gleeson 		goto done;
451*c3a558e7SSue Gleeson 	}
452*c3a558e7SSue Gleeson 
453*c3a558e7SSue Gleeson 	for (i = 0; i < 1024; i++) {
454*c3a558e7SSue Gleeson 		if (hcaArr[i] == NULL) {
455*c3a558e7SSue Gleeson 			break;
456*c3a558e7SSue Gleeson 		}
457*c3a558e7SSue Gleeson 		ret = print_target_props(hcaArr[i]);
458*c3a558e7SSue Gleeson 	}
459*c3a558e7SSue Gleeson 
460*c3a558e7SSue Gleeson done:
461*c3a558e7SSue Gleeson 	for (i = 0; i < 1024; i++) {
462*c3a558e7SSue Gleeson 		if (hcaArr[i] == NULL) {
463*c3a558e7SSue Gleeson 			break;
464*c3a558e7SSue Gleeson 		}
465*c3a558e7SSue Gleeson 		free(hcaArr[i]);
466*c3a558e7SSue Gleeson 	}
467*c3a558e7SSue Gleeson 
468*c3a558e7SSue Gleeson 	return (ret);
469*c3a558e7SSue Gleeson }
470*c3a558e7SSue Gleeson 
471*c3a558e7SSue Gleeson static int
472*c3a558e7SSue Gleeson print_target_props(char *hca)
473*c3a558e7SSue Gleeson {
474*c3a558e7SSue Gleeson 	int		ret;
475*c3a558e7SSue Gleeson 	boolean_t	enabled;
476*c3a558e7SSue Gleeson 	char		buf[32];
477*c3a558e7SSue Gleeson 	char		euibuf[64];
478*c3a558e7SSue Gleeson 	uint64_t	hcaguid;
479*c3a558e7SSue Gleeson 	stmfDevid	devid;
480*c3a558e7SSue Gleeson 	stmfTargetProperties	props;
481*c3a558e7SSue Gleeson 	char		*state;
482*c3a558e7SSue Gleeson 
483*c3a558e7SSue Gleeson 	ret = srpt_NormalizeGuid(hca, buf, sizeof (buf), &hcaguid);
484*c3a558e7SSue Gleeson 	if (ret != 0) {
485*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "Invalid target HCA: %s\n",
486*c3a558e7SSue Gleeson 		    hca);
487*c3a558e7SSue Gleeson 		return (ret);
488*c3a558e7SSue Gleeson 	}
489*c3a558e7SSue Gleeson 
490*c3a558e7SSue Gleeson 	/* only property set is enabled */
491*c3a558e7SSue Gleeson 	ret = srpt_GetTargetState(buf, &enabled);
492*c3a558e7SSue Gleeson 	if (ret != 0) {
493*c3a558e7SSue Gleeson 		(void) fprintf(stderr,
494*c3a558e7SSue Gleeson 		    "Could not get enabled state for %s: %d\n",
495*c3a558e7SSue Gleeson 		    buf, ret);
496*c3a558e7SSue Gleeson 		return (ret);
497*c3a558e7SSue Gleeson 	}
498*c3a558e7SSue Gleeson 
499*c3a558e7SSue Gleeson 	(void) printf("Target HCA %s:\n", buf);
500*c3a558e7SSue Gleeson 
501*c3a558e7SSue Gleeson 	(void) printf(PROPS_FORMAT, gettext("Enabled"));
502*c3a558e7SSue Gleeson 
503*c3a558e7SSue Gleeson 	if (enabled) {
504*c3a558e7SSue Gleeson 		(void) printf("%s\n", gettext("true"));
505*c3a558e7SSue Gleeson 	} else {
506*c3a558e7SSue Gleeson 		(void) printf("%s\n", gettext("false"));
507*c3a558e7SSue Gleeson 	}
508*c3a558e7SSue Gleeson 
509*c3a558e7SSue Gleeson 	state = "-";
510*c3a558e7SSue Gleeson 
511*c3a558e7SSue Gleeson 	(void) snprintf(euibuf, sizeof (euibuf), "eui.%016llX", hcaguid);
512*c3a558e7SSue Gleeson 
513*c3a558e7SSue Gleeson 	ret = stmfDevidFromIscsiName(euibuf, &devid);
514*c3a558e7SSue Gleeson 	if (ret == STMF_STATUS_SUCCESS) {
515*c3a558e7SSue Gleeson 		ret = stmfGetTargetProperties(&devid, &props);
516*c3a558e7SSue Gleeson 		if (ret == STMF_STATUS_SUCCESS) {
517*c3a558e7SSue Gleeson 			if (props.status == STMF_TARGET_PORT_ONLINE) {
518*c3a558e7SSue Gleeson 				state = "online";
519*c3a558e7SSue Gleeson 			} else {
520*c3a558e7SSue Gleeson 				state = "offline";
521*c3a558e7SSue Gleeson 			}
522*c3a558e7SSue Gleeson 		}
523*c3a558e7SSue Gleeson 	}
524*c3a558e7SSue Gleeson 
525*c3a558e7SSue Gleeson 	(void) printf(PROPS_FORMAT, gettext("SRP Target Name"));
526*c3a558e7SSue Gleeson 	(void) printf("%s\n", euibuf);
527*c3a558e7SSue Gleeson 	(void) printf(PROPS_FORMAT, gettext("Operational Status"));
528*c3a558e7SSue Gleeson 	(void) printf("%s\n", state);
529*c3a558e7SSue Gleeson 
530*c3a558e7SSue Gleeson 	(void) printf("\n");
531*c3a558e7SSue Gleeson 
532*c3a558e7SSue Gleeson 	return (0);
533*c3a558e7SSue Gleeson }
534*c3a558e7SSue Gleeson 
535*c3a558e7SSue Gleeson 
536*c3a558e7SSue Gleeson static int
537*c3a558e7SSue Gleeson get_local_hcas(char **hcaArray, int count)
538*c3a558e7SSue Gleeson {
539*c3a558e7SSue Gleeson 	int		ret = 0;
540*c3a558e7SSue Gleeson 	char		*cfgdir = "/dev/cfg";
541*c3a558e7SSue Gleeson 	DIR		*dirp = NULL;
542*c3a558e7SSue Gleeson 	struct dirent	*entry;
543*c3a558e7SSue Gleeson 	int		idx = 0;
544*c3a558e7SSue Gleeson 	char		*bufp;
545*c3a558e7SSue Gleeson 
546*c3a558e7SSue Gleeson 	if ((hcaArray == NULL) || (count == 0)) {
547*c3a558e7SSue Gleeson 		return (EINVAL);
548*c3a558e7SSue Gleeson 	}
549*c3a558e7SSue Gleeson 
550*c3a558e7SSue Gleeson 	dirp = opendir(cfgdir);
551*c3a558e7SSue Gleeson 
552*c3a558e7SSue Gleeson 	if (dirp == NULL) {
553*c3a558e7SSue Gleeson 		ret = errno;
554*c3a558e7SSue Gleeson 		(void) fprintf(stderr, "Could not open %s: errno %d\n",
555*c3a558e7SSue Gleeson 		    cfgdir, ret);
556*c3a558e7SSue Gleeson 		return (ret);
557*c3a558e7SSue Gleeson 	}
558*c3a558e7SSue Gleeson 
559*c3a558e7SSue Gleeson 	while ((entry = readdir(dirp)) != NULL) {
560*c3a558e7SSue Gleeson 		bufp = &entry->d_name[0];
561*c3a558e7SSue Gleeson 
562*c3a558e7SSue Gleeson 		if (strncmp(bufp, "hca:", 4) != 0) {
563*c3a558e7SSue Gleeson 			continue;
564*c3a558e7SSue Gleeson 		}
565*c3a558e7SSue Gleeson 
566*c3a558e7SSue Gleeson 		bufp += 4;
567*c3a558e7SSue Gleeson 
568*c3a558e7SSue Gleeson 		hcaArray[idx] = strdup(bufp);
569*c3a558e7SSue Gleeson 		if (hcaArray[idx] == NULL) {
570*c3a558e7SSue Gleeson 			ret = ENOMEM;
571*c3a558e7SSue Gleeson 			break;
572*c3a558e7SSue Gleeson 		}
573*c3a558e7SSue Gleeson 		idx++;
574*c3a558e7SSue Gleeson 
575*c3a558e7SSue Gleeson 		if (idx >= count) {
576*c3a558e7SSue Gleeson 			ret = ETOOMANYREFS;
577*c3a558e7SSue Gleeson 			break;
578*c3a558e7SSue Gleeson 		}
579*c3a558e7SSue Gleeson 	}
580*c3a558e7SSue Gleeson 
581*c3a558e7SSue Gleeson 	return (ret);
582*c3a558e7SSue Gleeson }
583