xref: /titanic_50/usr/src/cmd/iscsiadm/iscsiadm_main.c (revision 129b3e6c5b0ac55b5021a4c38db6387b6acdaaf1)
1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte /*
221a1a84a3SPeter Dunlap  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fcf3ce44SJohn Forte  * Use is subject to license terms.
24fcf3ce44SJohn Forte  */
25fcf3ce44SJohn Forte 
26fcf3ce44SJohn Forte #include <stdlib.h>
27fcf3ce44SJohn Forte #include <stdio.h>
28fcf3ce44SJohn Forte #include <wchar.h>
29fcf3ce44SJohn Forte #include <widec.h>
30fcf3ce44SJohn Forte #include <sys/types.h>
31fcf3ce44SJohn Forte #include <sys/socket.h>
32fcf3ce44SJohn Forte #include <arpa/inet.h>
33fcf3ce44SJohn Forte #include <netdb.h>
34fcf3ce44SJohn Forte #include <unistd.h>
35fcf3ce44SJohn Forte #include <libintl.h>
36fcf3ce44SJohn Forte #include <limits.h>
37fcf3ce44SJohn Forte #include <string.h>
38fcf3ce44SJohn Forte #include <strings.h>
39fcf3ce44SJohn Forte #include <syslog.h>
40fcf3ce44SJohn Forte #include <errno.h>
41fcf3ce44SJohn Forte #include <netinet/in.h>
42fcf3ce44SJohn Forte #include <sys/socket.h>
43fcf3ce44SJohn Forte #include <arpa/inet.h>
44fcf3ce44SJohn Forte #include <wctype.h>
45fcf3ce44SJohn Forte #include <assert.h>
46fcf3ce44SJohn Forte 
47fcf3ce44SJohn Forte #include <ima.h>
481a1a84a3SPeter Dunlap #include <sys/iscsi_protocol.h>
49fcf3ce44SJohn Forte #include <sys/scsi/adapters/iscsi_if.h>
50fcf3ce44SJohn Forte 
51fcf3ce44SJohn Forte #include "cmdparse.h"
52fcf3ce44SJohn Forte #include "sun_ima.h"
53fcf3ce44SJohn Forte #include "iscsiadm.h"
54fcf3ce44SJohn Forte 
55fcf3ce44SJohn Forte #define	VERSION_STRING_MAX_LEN	10
56fcf3ce44SJohn Forte #define	MAX_LONG_CHAR_LEN 19
57fcf3ce44SJohn Forte 
58fcf3ce44SJohn Forte #define	MAX_AUTH_METHODS 5
59fcf3ce44SJohn Forte /*
60fcf3ce44SJohn Forte  * Version number:
61fcf3ce44SJohn Forte  *  MAJOR - This should only change when there is an incompatible change made
62fcf3ce44SJohn Forte  *  to the interfaces or the output.
63fcf3ce44SJohn Forte  *
64fcf3ce44SJohn Forte  *  MINOR - This should change whenever there is a new command or new feature
65fcf3ce44SJohn Forte  *  with no incompatible change.
66fcf3ce44SJohn Forte  */
67fcf3ce44SJohn Forte #define	VERSION_STRING_MAJOR	    "1"
68fcf3ce44SJohn Forte #define	VERSION_STRING_MINOR	    "0"
69fcf3ce44SJohn Forte 
70fcf3ce44SJohn Forte #define	OPTIONSTRING1	"yes|no"
71fcf3ce44SJohn Forte #define	OPTIONSTRING2	"initiator node name"
72fcf3ce44SJohn Forte #define	OPTIONSTRING3	"initiator node alias"
73fcf3ce44SJohn Forte #define	OPTIONSTRING4	"enable|disable"
74fcf3ce44SJohn Forte #define	OPTIONSTRING5	"key=value,..."
75fcf3ce44SJohn Forte #define	OPTIONSTRING6	"none|CRC32"
76fcf3ce44SJohn Forte #define	OPTIONSTRING7	"CHAP name"
77fcf3ce44SJohn Forte #define	OPTIONSTRING8	"<# sessions>|<IP Address>[,<IP Address>]*"
78fcf3ce44SJohn Forte #define	OPTIONVAL1	"0 to 3600"
79fcf3ce44SJohn Forte #define	OPTIONVAL2	"512 to 2**24 - 1"
80fcf3ce44SJohn Forte #define	OPTIONVAL3	"1 to 65535"
81fcf3ce44SJohn Forte #define	OPTIONVAL4	"<IP address>[:port]"
82fcf3ce44SJohn Forte 
83fcf3ce44SJohn Forte #define	MAX_ISCSI_NAME_LEN	    223
84fcf3ce44SJohn Forte #define	MAX_ADDRESS_LEN		    255
85fcf3ce44SJohn Forte #define	MIN_CHAP_SECRET_LEN	    12
86fcf3ce44SJohn Forte #define	MAX_CHAP_SECRET_LEN	    16
87fcf3ce44SJohn Forte #define	DEFAULT_ISCSI_PORT	    3260
88fcf3ce44SJohn Forte #define	ISNS_DEFAULT_SERVER_PORT    3205
89fcf3ce44SJohn Forte #define	DEFAULT_RADIUS_PORT	    1812
90fcf3ce44SJohn Forte #define	MAX_CHAP_NAME_LEN	    512
91fcf3ce44SJohn Forte 
92fcf3ce44SJohn Forte /* For listNode */
93fcf3ce44SJohn Forte #define	INF_ERROR		1
94fcf3ce44SJohn Forte #define	INVALID_NODE_NAME	2
95fcf3ce44SJohn Forte 
96fcf3ce44SJohn Forte #define	IMABOOLPRINT(prop, option)	 \
97fcf3ce44SJohn Forte 	if ((option) == PRINT_CONFIGURED_PARAMS) { \
98fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%s/%s\n", \
99fcf3ce44SJohn Forte 		(prop).defaultValue == IMA_TRUE ? gettext("yes") : \
100fcf3ce44SJohn Forte 			gettext("no"), \
101fcf3ce44SJohn Forte 		(prop).currentValueValid == IMA_TRUE ? \
102fcf3ce44SJohn Forte 			((prop).currentValue == IMA_TRUE ? \
103fcf3ce44SJohn Forte 			gettext("yes"): gettext("no")) : "-"); \
104fcf3ce44SJohn Forte 	} else if ((option) == PRINT_NEGOTIATED_PARAMS) { \
105fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%s\n", \
106fcf3ce44SJohn Forte 		(prop).currentValueValid == IMA_TRUE ? \
107fcf3ce44SJohn Forte 		(((prop).currentValue == IMA_TRUE) ? gettext("yes") : \
108fcf3ce44SJohn Forte 		gettext("no")) : "-"); \
109fcf3ce44SJohn Forte 	}
110fcf3ce44SJohn Forte 
111fcf3ce44SJohn Forte #define	IMAMINMAXPRINT(prop, option) \
112fcf3ce44SJohn Forte 	if ((option) == PRINT_CONFIGURED_PARAMS) { \
113fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%d/", (prop).defaultValue); \
114fcf3ce44SJohn Forte 		if ((prop).currentValueValid == IMA_TRUE) { \
115fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%d\n", (prop).currentValue); \
116fcf3ce44SJohn Forte 		} else if ((prop).currentValueValid == IMA_FALSE) { \
117fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s\n", "-"); \
118fcf3ce44SJohn Forte 		} \
119fcf3ce44SJohn Forte 	} else if ((option) == PRINT_NEGOTIATED_PARAMS) { \
120fcf3ce44SJohn Forte 		if ((prop).currentValueValid == IMA_TRUE) { \
121fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%d\n", (prop).currentValue); \
122fcf3ce44SJohn Forte 		} else if ((prop).currentValueValid == IMA_FALSE) { \
123fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s\n", "-"); \
124fcf3ce44SJohn Forte 		} \
125fcf3ce44SJohn Forte 	}
126fcf3ce44SJohn Forte 
127fcf3ce44SJohn Forte /* forward declarations */
128fcf3ce44SJohn Forte #define	PARSE_ADDR_OK				0
129fcf3ce44SJohn Forte #define	PARSE_ADDR_MISSING_CLOSING_BRACKET	1
130fcf3ce44SJohn Forte #define	PARSE_ADDR_PORT_OUT_OF_RANGE		2
131fcf3ce44SJohn Forte #define	PARSE_TARGET_OK				0
132fcf3ce44SJohn Forte #define	PARSE_TARGET_INVALID_TPGT		1
133fcf3ce44SJohn Forte #define	PARSE_TARGET_INVALID_ADDR		2
134fcf3ce44SJohn Forte 
135fcf3ce44SJohn Forte #define	PRINT_CONFIGURED_PARAMS			1
136fcf3ce44SJohn Forte #define	PRINT_NEGOTIATED_PARAMS			2
137fcf3ce44SJohn Forte 
138fcf3ce44SJohn Forte typedef enum iSCSINameCheckStatus {
139fcf3ce44SJohn Forte 	iSCSINameCheckOK,
140fcf3ce44SJohn Forte 	iSCSINameLenZero,
141fcf3ce44SJohn Forte 	iSCSINameLenExceededMax,
142fcf3ce44SJohn Forte 	iSCSINameUnknownType,
143fcf3ce44SJohn Forte 	iSCSINameInvalidCharacter,
144fcf3ce44SJohn Forte 	iSCSINameIqnFormatError,
145fcf3ce44SJohn Forte 	iSCSINameEUIFormatError,
146fcf3ce44SJohn Forte 	iSCSINameIqnDateFormatError,
147fcf3ce44SJohn Forte 	iSCSINameIqnSubdomainFormatError,
148fcf3ce44SJohn Forte 	iSCSINameIqnInvalidYearError,
149fcf3ce44SJohn Forte 	iSCSINameIqnInvalidMonthError,
150fcf3ce44SJohn Forte 	iSCSINameIqnFQDNError
151fcf3ce44SJohn Forte } iSCSINameCheckStatusType;
152fcf3ce44SJohn Forte 
153fcf3ce44SJohn Forte /* Utility functions */
154fcf3ce44SJohn Forte iSCSINameCheckStatusType iSCSINameStringProfileCheck(wchar_t *name);
155fcf3ce44SJohn Forte boolean_t isNaturalNumber(char *numberStr, uint32_t upperBound);
156fcf3ce44SJohn Forte static int parseAddress(char *address_port_str, uint16_t defaultPort,
157fcf3ce44SJohn Forte     char *address_str, size_t address_str_len,
158fcf3ce44SJohn Forte     uint16_t *port, boolean_t *isIpv6);
159fcf3ce44SJohn Forte int parseTarget(char *targetStr,
160fcf3ce44SJohn Forte     wchar_t *targetNameStr,
161fcf3ce44SJohn Forte     size_t targetNameStrLen,
162fcf3ce44SJohn Forte     boolean_t *targetAddressSpecified,
163fcf3ce44SJohn Forte     wchar_t *targetAddressStr,
164fcf3ce44SJohn Forte     size_t targetAddressStrLen,
165fcf3ce44SJohn Forte     uint16_t *port,
166fcf3ce44SJohn Forte     boolean_t *tpgtSpecified,
167fcf3ce44SJohn Forte     uint16_t *tpgt,
168fcf3ce44SJohn Forte     boolean_t *isIpv6);
169fcf3ce44SJohn Forte 
170fcf3ce44SJohn Forte /* subcommand functions */
171fcf3ce44SJohn Forte static int addFunc(int, char **, int, cmdOptions_t *, void *, int *);
172fcf3ce44SJohn Forte static int listFunc(int, char **, int, cmdOptions_t *, void *, int *);
173fcf3ce44SJohn Forte static int modifyFunc(int, char **, int, cmdOptions_t *, void *, int *);
174fcf3ce44SJohn Forte static int removeFunc(int, char **, int, cmdOptions_t *, void *, int *);
175fcf3ce44SJohn Forte 
176fcf3ce44SJohn Forte /* helper functions */
177fcf3ce44SJohn Forte static char *getExecBasename(char *);
178fcf3ce44SJohn Forte static int getNodeProps(IMA_NODE_PROPERTIES *);
179fcf3ce44SJohn Forte static int getSecret(char *, int *, int, int);
180fcf3ce44SJohn Forte static int getTargetAddress(int, char *, IMA_TARGET_ADDRESS *);
181fcf3ce44SJohn Forte static int printLoginParameters(char *, IMA_OID, int);
182fcf3ce44SJohn Forte static void printDiscoveryMethod(char *, IMA_UINT32);
183fcf3ce44SJohn Forte static void printTargetLuns(IMA_OID_LIST *);
184fcf3ce44SJohn Forte static void printSendTargets(SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES *);
185fcf3ce44SJohn Forte static void printDigestAlgorithm(SUN_IMA_DIGEST_ALGORITHM_VALUE *, int);
186fcf3ce44SJohn Forte static int setLoginParameter(IMA_OID, int, char *);
187fcf3ce44SJohn Forte static int setLoginParameters(IMA_OID, char *);
188fcf3ce44SJohn Forte static void printLibError(IMA_STATUS);
189fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
190fcf3ce44SJohn Forte static int sunPluginChk(IMA_OID, boolean_t *);
191fcf3ce44SJohn Forte static int sunInitiatorFind(IMA_OID *);
192fcf3ce44SJohn Forte static int getAuthMethodValue(char *, IMA_AUTHMETHOD *);
193fcf3ce44SJohn Forte static int getLoginParam(char *);
194fcf3ce44SJohn Forte static void iSCSINameCheckStatusDisplay(iSCSINameCheckStatusType status);
195fcf3ce44SJohn Forte static int modifyIndividualTargetParam(cmdOptions_t *optionList,
196fcf3ce44SJohn Forte     IMA_OID targetOid, int *);
197fcf3ce44SJohn Forte static void listCHAPName(IMA_OID oid);
198fcf3ce44SJohn Forte static int printConfiguredSessions(IMA_OID);
199fcf3ce44SJohn Forte 
200fcf3ce44SJohn Forte /* object functions per subcommand */
201fcf3ce44SJohn Forte static int addAddress(int, int, char *[], int *);
202fcf3ce44SJohn Forte static int addStaticConfig(int, char *[], int *);
203fcf3ce44SJohn Forte static int listDiscovery(int *);
204fcf3ce44SJohn Forte static int listDiscoveryAddress(int, char *[], cmdOptions_t *, int *);
205fcf3ce44SJohn Forte static int listISNSServerAddress(int, char *[], cmdOptions_t *, int *);
206fcf3ce44SJohn Forte static int listNode(int *);
207fcf3ce44SJohn Forte static int listStaticConfig(int, char *[], int *);
208fcf3ce44SJohn Forte static int listTarget(int, char *[], cmdOptions_t *, int *);
209fcf3ce44SJohn Forte static int listTargetParam(int, char *[], cmdOptions_t *, int *);
210fcf3ce44SJohn Forte static int modifyDiscovery(cmdOptions_t *, int *);
211fcf3ce44SJohn Forte static int modifyNodeAuthMethod(IMA_OID, char *, int *);
212fcf3ce44SJohn Forte static int modifyNodeAuthParam(IMA_OID oid, int, char *, int *);
213fcf3ce44SJohn Forte static int modifyNodeRadiusConfig(IMA_OID, char *, int *);
214fcf3ce44SJohn Forte static int modifyNodeRadiusAccess(IMA_OID, char *, int *);
215fcf3ce44SJohn Forte static int modifyNodeRadiusSharedSecret(IMA_OID, int *);
216fcf3ce44SJohn Forte static int modifyNode(cmdOptions_t *, int *);
217fcf3ce44SJohn Forte static int modifyTargetAuthMethod(IMA_OID, char *, int *);
218fcf3ce44SJohn Forte static int modifyTargetAuthParam(IMA_OID oid, int param, char *chapName, int *);
219fcf3ce44SJohn Forte static int modifyTargetParam(cmdOptions_t *, char *, int *);
220fcf3ce44SJohn Forte static int removeAddress(int, int, char *[], int *);
221fcf3ce44SJohn Forte static int removeStaticConfig(int, char *[], int *);
222fcf3ce44SJohn Forte static int removeTargetParam(int, char *[], int *);
223fcf3ce44SJohn Forte static int modifyTargetBidirAuthFlag(IMA_OID, char *, int *);
224fcf3ce44SJohn Forte static int modifyConfiguredSessions(IMA_OID targetOid, char *optarg);
225fcf3ce44SJohn Forte 
226fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
227fcf3ce44SJohn Forte static IMA_STATUS getISCSINodeParameter(int paramType,
228fcf3ce44SJohn Forte     IMA_OID *oid,
229fcf3ce44SJohn Forte     void *pProps,
230fcf3ce44SJohn Forte     uint32_t paramIndex);
231fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
232fcf3ce44SJohn Forte static IMA_STATUS setISCSINodeParameter(int paramType,
233fcf3ce44SJohn Forte     IMA_OID *oid,
234fcf3ce44SJohn Forte     void *pProps,
235fcf3ce44SJohn Forte     uint32_t paramIndex);
236fcf3ce44SJohn Forte /* LINTED E_STATIC_UNUSED */
237fcf3ce44SJohn Forte static IMA_STATUS getDigest(IMA_OID oid, int ioctlCmd,
238fcf3ce44SJohn Forte     SUN_IMA_DIGEST_ALGORITHM_VALUE *algorithm);
239fcf3ce44SJohn Forte 
240fcf3ce44SJohn Forte IMA_STATUS getNegotiatedDigest(int digestType,
241fcf3ce44SJohn Forte 	SUN_IMA_DIGEST_ALGORITHM_VALUE *algorithm,
242fcf3ce44SJohn Forte 	SUN_IMA_CONN_PROPERTIES *connProps);
243fcf3ce44SJohn Forte 
244fcf3ce44SJohn Forte /* globals */
245fcf3ce44SJohn Forte static char *cmdName;
246fcf3ce44SJohn Forte 
247fcf3ce44SJohn Forte /*
248fcf3ce44SJohn Forte  * Available option letters:
249fcf3ce44SJohn Forte  *
250fcf3ce44SJohn Forte  * bcefgijklmnoquwxyz
251fcf3ce44SJohn Forte  *
252fcf3ce44SJohn Forte  * DEFGHIJKLMOQTUVWXYZ
253fcf3ce44SJohn Forte  */
254fcf3ce44SJohn Forte 
255fcf3ce44SJohn Forte /*
256fcf3ce44SJohn Forte  * Add new options here
257fcf3ce44SJohn Forte  */
258fcf3ce44SJohn Forte optionTbl_t longOptions[] = {
259fcf3ce44SJohn Forte 	{"static", required_arg, 's', OPTIONSTRING4},
260fcf3ce44SJohn Forte 	{"sendtargets", required_arg, 't', OPTIONSTRING4},
261fcf3ce44SJohn Forte 	{"iSNS", required_arg, 'i', OPTIONSTRING4},
262fcf3ce44SJohn Forte 	{"headerdigest", required_arg, 'h', OPTIONSTRING6},
263fcf3ce44SJohn Forte 	{"datadigest", required_arg, 'd', OPTIONSTRING6},
264fcf3ce44SJohn Forte 	{"login-param", required_arg, 'p', OPTIONSTRING5},
265fcf3ce44SJohn Forte 	{"authentication", required_arg, 'a', "CHAP|none"},
266fcf3ce44SJohn Forte 	{"bi-directional-authentication", required_arg, 'B', OPTIONSTRING4},
267fcf3ce44SJohn Forte 	{"CHAP-secret", no_arg, 'C', NULL},
268fcf3ce44SJohn Forte 	{"CHAP-name", required_arg, 'H', OPTIONSTRING7},
269fcf3ce44SJohn Forte 	{"node-name", required_arg, 'N', OPTIONSTRING2},
270fcf3ce44SJohn Forte 	{"node-alias", required_arg, 'A', OPTIONSTRING3},
271fcf3ce44SJohn Forte 	{"radius-server", required_arg, 'r', OPTIONVAL4},
272fcf3ce44SJohn Forte 	{"radius-access", required_arg, 'R', OPTIONSTRING4},
273fcf3ce44SJohn Forte 	{"radius-shared-secret", no_arg, 'P', NULL},
274fcf3ce44SJohn Forte 	{"verbose", no_arg, 'v', NULL},
275fcf3ce44SJohn Forte 	{"scsi-target", no_arg, 'S', NULL},
276fcf3ce44SJohn Forte 	{"configured-sessions", required_arg, 'c', OPTIONSTRING8},
277fcf3ce44SJohn Forte 	{NULL, 0, 0, 0}
278fcf3ce44SJohn Forte };
279fcf3ce44SJohn Forte 
280fcf3ce44SJohn Forte parameterTbl_t loginParams[] = {
281fcf3ce44SJohn Forte 	{"dataseqinorder", DATA_SEQ_IN_ORDER},
282fcf3ce44SJohn Forte 	{"defaulttime2retain", DEFAULT_TIME_2_RETAIN},
283fcf3ce44SJohn Forte 	{"defaulttime2wait", DEFAULT_TIME_2_WAIT},
284fcf3ce44SJohn Forte 	{"firstburstlength", FIRST_BURST_LENGTH},
285fcf3ce44SJohn Forte 	{"immediatedata", IMMEDIATE_DATA},
286fcf3ce44SJohn Forte 	{"initialr2t", INITIAL_R2T},
287fcf3ce44SJohn Forte 	{"maxburstlength", MAX_BURST_LENGTH},
288fcf3ce44SJohn Forte 	{"datapduinorder", DATA_PDU_IN_ORDER},
289fcf3ce44SJohn Forte 	{"maxoutstandingr2t", MAX_OUTSTANDING_R2T},
290fcf3ce44SJohn Forte 	{"maxrecvdataseglen", MAX_RECV_DATA_SEG_LEN},
291fcf3ce44SJohn Forte 	{"maxconnections", MAX_CONNECTIONS},
292fcf3ce44SJohn Forte 	{"errorrecoverylevel", ERROR_RECOVERY_LEVEL},
293fcf3ce44SJohn Forte 	{NULL, 0}
294fcf3ce44SJohn Forte };
295fcf3ce44SJohn Forte 
296fcf3ce44SJohn Forte /*
297fcf3ce44SJohn Forte  * Add new subcommands here
298fcf3ce44SJohn Forte  */
299fcf3ce44SJohn Forte subcommand_t subcommands[] = {
300fcf3ce44SJohn Forte 	{"add", ADD, addFunc},
301fcf3ce44SJohn Forte 	{"list", LIST, listFunc},
302fcf3ce44SJohn Forte 	{"modify", MODIFY, modifyFunc},
303fcf3ce44SJohn Forte 	{"remove", REMOVE, removeFunc},
304fcf3ce44SJohn Forte 	{NULL, 0, NULL}
305fcf3ce44SJohn Forte };
306fcf3ce44SJohn Forte 
307fcf3ce44SJohn Forte /*
308fcf3ce44SJohn Forte  * Add objects here
309fcf3ce44SJohn Forte  */
310fcf3ce44SJohn Forte object_t objects[] = {
311fcf3ce44SJohn Forte 	{"discovery", DISCOVERY},
312fcf3ce44SJohn Forte 	{"discovery-address", DISCOVERY_ADDRESS},
313fcf3ce44SJohn Forte 	{"isns-server", ISNS_SERVER_ADDRESS},
314fcf3ce44SJohn Forte 	{"initiator-node", NODE},
315fcf3ce44SJohn Forte 	{"static-config", STATIC_CONFIG},
316fcf3ce44SJohn Forte 	{"target", TARGET},
317fcf3ce44SJohn Forte 	{"target-param", TARGET_PARAM},
318fcf3ce44SJohn Forte 	{NULL, 0}
319fcf3ce44SJohn Forte };
320fcf3ce44SJohn Forte 
321fcf3ce44SJohn Forte /*
322fcf3ce44SJohn Forte  * Rules for subcommands and objects
323fcf3ce44SJohn Forte  */
324fcf3ce44SJohn Forte objectRules_t objectRules[] = {
325fcf3ce44SJohn Forte 	{TARGET, 0, LIST, 0, ADD|REMOVE|MODIFY, LIST,
326fcf3ce44SJohn Forte 	"target-name"},
327fcf3ce44SJohn Forte 	{TARGET_PARAM, MODIFY|REMOVE, LIST, 0, ADD, MODIFY,
328fcf3ce44SJohn Forte 	"target-name"},
329fcf3ce44SJohn Forte 	{DISCOVERY, 0, 0, LIST|MODIFY, ADD|REMOVE, 0, NULL},
330fcf3ce44SJohn Forte 	{NODE, 0, 0, MODIFY|LIST, ADD|REMOVE, 0, NULL},
331fcf3ce44SJohn Forte 	{STATIC_CONFIG, ADD|REMOVE, LIST, 0, MODIFY, ADD|REMOVE|LIST,
332fcf3ce44SJohn Forte 	"target-name,target-address[:port-number][,tpgt]"},
333fcf3ce44SJohn Forte 	{DISCOVERY_ADDRESS, ADD|REMOVE, LIST, 0, MODIFY,
334fcf3ce44SJohn Forte 	ADD|REMOVE|LIST, "IP-address[:port-number]"},
335fcf3ce44SJohn Forte 	{ISNS_SERVER_ADDRESS, ADD|REMOVE, LIST, 0, MODIFY,
336fcf3ce44SJohn Forte 	ADD|REMOVE|LIST, "IP-address[:port-number]"},
337fcf3ce44SJohn Forte 	{0, 0, 0, 0, 0, NULL}
338fcf3ce44SJohn Forte };
339fcf3ce44SJohn Forte 
340fcf3ce44SJohn Forte /*
341fcf3ce44SJohn Forte  * list of objects, subcommands, valid short options, required flag and
342fcf3ce44SJohn Forte  * exclusive option string
343fcf3ce44SJohn Forte  *
344fcf3ce44SJohn Forte  * If it's not here, there are no options for that object.
345fcf3ce44SJohn Forte  */
346fcf3ce44SJohn Forte optionRules_t optionRules[] = {
347fcf3ce44SJohn Forte 	{DISCOVERY, MODIFY, "sti", B_TRUE, NULL},
348fcf3ce44SJohn Forte 	{DISCOVERY_ADDRESS, LIST, "v", B_FALSE, NULL},
349fcf3ce44SJohn Forte 	{ISNS_SERVER_ADDRESS, LIST, "v", B_FALSE, NULL},
350fcf3ce44SJohn Forte 	{TARGET, LIST, "vS", B_FALSE, NULL},
351fcf3ce44SJohn Forte 	{NODE, MODIFY, "NAhdCaRrPHc", B_TRUE, "CP"},
352fcf3ce44SJohn Forte 	{TARGET_PARAM, MODIFY, "ahdBCpcH", B_TRUE, "C"},
353fcf3ce44SJohn Forte 	{TARGET_PARAM, LIST, "v", B_FALSE, NULL},
354fcf3ce44SJohn Forte 	{0, 0, 0, 0, 0}
355fcf3ce44SJohn Forte };
356fcf3ce44SJohn Forte 
357fcf3ce44SJohn Forte 
358fcf3ce44SJohn Forte static boolean_t
359fcf3ce44SJohn Forte targetNamesEqual(wchar_t *name1, wchar_t *name2)
360fcf3ce44SJohn Forte {
361fcf3ce44SJohn Forte 	int i;
362fcf3ce44SJohn Forte 	wchar_t wchar1, wchar2;
363fcf3ce44SJohn Forte 
364fcf3ce44SJohn Forte 	if (name1 == NULL || name2 == NULL) {
365fcf3ce44SJohn Forte 		return (B_FALSE);
366fcf3ce44SJohn Forte 	}
367fcf3ce44SJohn Forte 
368fcf3ce44SJohn Forte 	if (wcslen(name1) != wcslen(name2)) {
369fcf3ce44SJohn Forte 		return (B_FALSE);
370fcf3ce44SJohn Forte 	}
371fcf3ce44SJohn Forte 
372fcf3ce44SJohn Forte 	/*
373fcf3ce44SJohn Forte 	 * Convert names to lower case and compare
374fcf3ce44SJohn Forte 	 */
375fcf3ce44SJohn Forte 	for (i = 0; i < wcslen(name1); i++) {
376fcf3ce44SJohn Forte 		wchar1 = towctrans((wint_t)name1[i], wctrans("tolower"));
377fcf3ce44SJohn Forte 		wchar2 = towctrans((wint_t)name2[i], wctrans("tolower"));
378fcf3ce44SJohn Forte 
379fcf3ce44SJohn Forte 		if (wchar1 != wchar2) {
380fcf3ce44SJohn Forte 			return (B_FALSE);
381fcf3ce44SJohn Forte 		}
382fcf3ce44SJohn Forte 	}
383fcf3ce44SJohn Forte 
384fcf3ce44SJohn Forte 	return (B_TRUE);
385fcf3ce44SJohn Forte }
386fcf3ce44SJohn Forte 
387fcf3ce44SJohn Forte static boolean_t
388fcf3ce44SJohn Forte ipAddressesEqual(IMA_TARGET_ADDRESS addr1, IMA_TARGET_ADDRESS addr2)
389fcf3ce44SJohn Forte {
390fcf3ce44SJohn Forte #define	IPV4_ADDR_BYTES 4
391fcf3ce44SJohn Forte #define	IPV6_ADDR_BYTES 16
392fcf3ce44SJohn Forte 
393fcf3ce44SJohn Forte 	int compSize;
394fcf3ce44SJohn Forte 
395fcf3ce44SJohn Forte 	if (addr1.hostnameIpAddress.id.ipAddress.ipv4Address !=
396fcf3ce44SJohn Forte 	    addr2.hostnameIpAddress.id.ipAddress.ipv4Address) {
397fcf3ce44SJohn Forte 		return (B_FALSE);
398fcf3ce44SJohn Forte 	}
399fcf3ce44SJohn Forte 
400fcf3ce44SJohn Forte 	compSize = IPV6_ADDR_BYTES;
401fcf3ce44SJohn Forte 	if (addr1.hostnameIpAddress.id.ipAddress.ipv4Address) {
402fcf3ce44SJohn Forte 		compSize = IPV4_ADDR_BYTES;
403fcf3ce44SJohn Forte 	}
404fcf3ce44SJohn Forte 
405fcf3ce44SJohn Forte 	if (bcmp(addr1.hostnameIpAddress.id.ipAddress.ipAddress,
406fcf3ce44SJohn Forte 	    addr2.hostnameIpAddress.id.ipAddress.ipAddress, compSize) == 0) {
407fcf3ce44SJohn Forte 		return (B_TRUE);
408fcf3ce44SJohn Forte 	}
409fcf3ce44SJohn Forte 
410fcf3ce44SJohn Forte 	return (B_FALSE);
411fcf3ce44SJohn Forte }
412fcf3ce44SJohn Forte 
413fcf3ce44SJohn Forte static int
414fcf3ce44SJohn Forte getLoginParam(char *arg)
415fcf3ce44SJohn Forte {
416fcf3ce44SJohn Forte 	parameterTbl_t *paramp;
417fcf3ce44SJohn Forte 	int len;
418fcf3ce44SJohn Forte 
419fcf3ce44SJohn Forte 	for (paramp = loginParams; paramp->name; paramp++) {
420fcf3ce44SJohn Forte 		len = strlen(arg);
421fcf3ce44SJohn Forte 		if (len == strlen(paramp->name) &&
422fcf3ce44SJohn Forte 		    strncasecmp(arg, paramp->name, len) == 0) {
423fcf3ce44SJohn Forte 			return (paramp->val);
424fcf3ce44SJohn Forte 		}
425fcf3ce44SJohn Forte 	}
426fcf3ce44SJohn Forte 	return (-1);
427fcf3ce44SJohn Forte }
428fcf3ce44SJohn Forte 
429fcf3ce44SJohn Forte static void
430fcf3ce44SJohn Forte printLibError(IMA_STATUS status)
431fcf3ce44SJohn Forte {
432fcf3ce44SJohn Forte 	char *errorString;
433fcf3ce44SJohn Forte 	switch (status) {
434fcf3ce44SJohn Forte 	case IMA_ERROR_NOT_SUPPORTED:
435fcf3ce44SJohn Forte 		errorString =
436fcf3ce44SJohn Forte 		gettext("Operation currently not supported");
437fcf3ce44SJohn Forte 		break;
438fcf3ce44SJohn Forte 	case IMA_ERROR_INSUFFICIENT_MEMORY:
439fcf3ce44SJohn Forte 		errorString = gettext("Insufficient memory");
440fcf3ce44SJohn Forte 		break;
441fcf3ce44SJohn Forte 	case IMA_ERROR_UNEXPECTED_OS_ERROR:
442fcf3ce44SJohn Forte 		errorString = gettext("unexpected OS error");
443fcf3ce44SJohn Forte 		break;
444fcf3ce44SJohn Forte 	case IMA_ERROR_UNKNOWN_ERROR:
445fcf3ce44SJohn Forte 		errorString = gettext("Unknown error");
446fcf3ce44SJohn Forte 		break;
447fcf3ce44SJohn Forte 	case IMA_ERROR_LU_IN_USE:
448fcf3ce44SJohn Forte 		errorString = gettext("Logical unit in use");
449fcf3ce44SJohn Forte 		break;
450fcf3ce44SJohn Forte 	case IMA_ERROR_INVALID_PARAMETER:
451fcf3ce44SJohn Forte 		errorString = gettext("Invalid parameter specified");
452fcf3ce44SJohn Forte 		break;
453fcf3ce44SJohn Forte 	case IMA_ERROR_INVALID_OBJECT_TYPE:
454fcf3ce44SJohn Forte 		errorString =
455fcf3ce44SJohn Forte 		gettext("Internal library error: Invalid oid type specified");
456fcf3ce44SJohn Forte 		break;
457fcf3ce44SJohn Forte 	case IMA_ERROR_INCORRECT_OBJECT_TYPE:
458fcf3ce44SJohn Forte 		errorString =
459fcf3ce44SJohn Forte 		gettext("Internal library error: Incorrect oid type specified");
460fcf3ce44SJohn Forte 		break;
461fcf3ce44SJohn Forte 	case IMA_ERROR_OBJECT_NOT_FOUND:
462fcf3ce44SJohn Forte 		errorString = gettext("Internal library error: Oid not found");
463fcf3ce44SJohn Forte 		break;
464fcf3ce44SJohn Forte 	case IMA_ERROR_NAME_TOO_LONG:
465fcf3ce44SJohn Forte 		errorString = gettext("Name too long");
466fcf3ce44SJohn Forte 		break;
467fcf3ce44SJohn Forte 	default:
468fcf3ce44SJohn Forte 		errorString = gettext("Unknown error");
469fcf3ce44SJohn Forte 	}
470fcf3ce44SJohn Forte 	(void) fprintf(stderr, "%s: %s\n", cmdName, errorString);
471fcf3ce44SJohn Forte }
472fcf3ce44SJohn Forte 
473fcf3ce44SJohn Forte /*
474fcf3ce44SJohn Forte  * input:
475fcf3ce44SJohn Forte  *  execFullName - exec name of program (argv[0])
476fcf3ce44SJohn Forte  *
477fcf3ce44SJohn Forte  * Returns:
478fcf3ce44SJohn Forte  *  command name portion of execFullName
479fcf3ce44SJohn Forte  */
480fcf3ce44SJohn Forte static char *
481fcf3ce44SJohn Forte getExecBasename(char *execFullname)
482fcf3ce44SJohn Forte {
483fcf3ce44SJohn Forte 	char *lastSlash, *execBasename;
484fcf3ce44SJohn Forte 
485fcf3ce44SJohn Forte 	/* guard against '/' at end of command invocation */
486fcf3ce44SJohn Forte 	for (;;) {
487fcf3ce44SJohn Forte 		lastSlash = strrchr(execFullname, '/');
488fcf3ce44SJohn Forte 		if (lastSlash == NULL) {
489fcf3ce44SJohn Forte 			execBasename = execFullname;
490fcf3ce44SJohn Forte 			break;
491fcf3ce44SJohn Forte 		} else {
492fcf3ce44SJohn Forte 			execBasename = lastSlash + 1;
493fcf3ce44SJohn Forte 			if (*execBasename == '\0') {
494fcf3ce44SJohn Forte 				*lastSlash = '\0';
495fcf3ce44SJohn Forte 				continue;
496fcf3ce44SJohn Forte 			}
497fcf3ce44SJohn Forte 			break;
498fcf3ce44SJohn Forte 		}
499fcf3ce44SJohn Forte 	}
500fcf3ce44SJohn Forte 	return (execBasename);
501fcf3ce44SJohn Forte }
502fcf3ce44SJohn Forte 
503fcf3ce44SJohn Forte 
504fcf3ce44SJohn Forte /*
505fcf3ce44SJohn Forte  * input:
506fcf3ce44SJohn Forte  *  nodeProps - pointer to caller allocated IMA_NODE_PROPERTIES
507fcf3ce44SJohn Forte  *
508fcf3ce44SJohn Forte  * returns:
509fcf3ce44SJohn Forte  *  zero on success
510fcf3ce44SJohn Forte  *  non-zero otherwise
511fcf3ce44SJohn Forte  */
512fcf3ce44SJohn Forte static int
513fcf3ce44SJohn Forte getNodeProps(IMA_NODE_PROPERTIES *nodeProps)
514fcf3ce44SJohn Forte {
515fcf3ce44SJohn Forte 	IMA_OID sharedNodeOid;
516fcf3ce44SJohn Forte 
517fcf3ce44SJohn Forte 	IMA_STATUS status = IMA_GetSharedNodeOid(&sharedNodeOid);
518fcf3ce44SJohn Forte 	if (!(IMA_SUCCESS(status))) {
519fcf3ce44SJohn Forte 		printLibError(status);
520fcf3ce44SJohn Forte 		return (INF_ERROR);
521fcf3ce44SJohn Forte 	}
522fcf3ce44SJohn Forte 
523fcf3ce44SJohn Forte 	status = IMA_GetNodeProperties(sharedNodeOid, nodeProps);
524fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
525fcf3ce44SJohn Forte 		printLibError(status);
526fcf3ce44SJohn Forte 		return (INF_ERROR);
527fcf3ce44SJohn Forte 	}
528fcf3ce44SJohn Forte 
529fcf3ce44SJohn Forte 	return (0);
530fcf3ce44SJohn Forte }
531fcf3ce44SJohn Forte 
532fcf3ce44SJohn Forte /*
533fcf3ce44SJohn Forte  * sunInitiatorFind
534fcf3ce44SJohn Forte  * Purpose:
535fcf3ce44SJohn Forte  *  Finds the Sun iSCSI initiator (LHBA). This CLI currently supports only
536fcf3ce44SJohn Forte  *  one initiator.
537fcf3ce44SJohn Forte  *
538fcf3ce44SJohn Forte  * output:
539fcf3ce44SJohn Forte  *  oid of initiator
540fcf3ce44SJohn Forte  *
541fcf3ce44SJohn Forte  * Returns:
542fcf3ce44SJohn Forte  *  zero on success with initiator found
543fcf3ce44SJohn Forte  *  > 0 on success with no initiator found
544fcf3ce44SJohn Forte  *  < 0 on failure
545fcf3ce44SJohn Forte  */
546fcf3ce44SJohn Forte static int
547fcf3ce44SJohn Forte sunInitiatorFind(IMA_OID *oid)
548fcf3ce44SJohn Forte {
549f5fc5c04SJack Meng 	IMA_OID_LIST *lhbaList = NULL;
550fcf3ce44SJohn Forte 
551fcf3ce44SJohn Forte 	IMA_STATUS status = IMA_GetLhbaOidList(&lhbaList);
552fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
553fcf3ce44SJohn Forte 		printLibError(status);
554fcf3ce44SJohn Forte 		return (-1);
555fcf3ce44SJohn Forte 	}
556fcf3ce44SJohn Forte 
557f5fc5c04SJack Meng 	if ((lhbaList == NULL) || (lhbaList->oidCount == 0)) {
558f5fc5c04SJack Meng 		printLibError(IMA_ERROR_OBJECT_NOT_FOUND);
559f5fc5c04SJack Meng 		if (lhbaList != NULL)
560f5fc5c04SJack Meng 			(void) IMA_FreeMemory(lhbaList);
561f5fc5c04SJack Meng 		return (-1);
562f5fc5c04SJack Meng 	}
563f5fc5c04SJack Meng 
564fcf3ce44SJohn Forte 	*oid = lhbaList->oids[0];
565f5fc5c04SJack Meng 	(void) IMA_FreeMemory(lhbaList);
566fcf3ce44SJohn Forte 
567fcf3ce44SJohn Forte 	return (0);
568fcf3ce44SJohn Forte }
569fcf3ce44SJohn Forte 
570fcf3ce44SJohn Forte /*
571fcf3ce44SJohn Forte  * input:
572fcf3ce44SJohn Forte  *  wcInput - wide character string containing discovery address
573fcf3ce44SJohn Forte  * output:
574fcf3ce44SJohn Forte  *  address - IMA_TARGET_ADDRESS structure containing valid
575fcf3ce44SJohn Forte  *	discovery address
576fcf3ce44SJohn Forte  * returns:
577fcf3ce44SJohn Forte  *  zero on success
578fcf3ce44SJohn Forte  *  non-zero on failure
579fcf3ce44SJohn Forte  */
580fcf3ce44SJohn Forte 
581fcf3ce44SJohn Forte static int
582fcf3ce44SJohn Forte getTargetAddress(int addrType, char *ipStr, IMA_TARGET_ADDRESS *address)
583fcf3ce44SJohn Forte {
584fcf3ce44SJohn Forte 	char cCol = ':';
585fcf3ce44SJohn Forte 	char cBracketL = '['; /* Open Bracket '[' */
586fcf3ce44SJohn Forte 	char cBracketR = ']'; /* Close Bracket ']' */
587fcf3ce44SJohn Forte 	char *colPos;
588fcf3ce44SJohn Forte 	char *startPos;
589fcf3ce44SJohn Forte 	unsigned long inputPort;
590fcf3ce44SJohn Forte 	int addressType = AF_INET;
591fcf3ce44SJohn Forte 	char *tmpStrPtr, tmpStr[SUN_IMA_IP_ADDRESS_PORT_LEN];
592fcf3ce44SJohn Forte 	int rval;
593fcf3ce44SJohn Forte 
594fcf3ce44SJohn Forte 	/* Check if this is a ipv6 address */
595fcf3ce44SJohn Forte 	if (ipStr[0] == cBracketL) {
596fcf3ce44SJohn Forte 		addressType = AF_INET6;
597fcf3ce44SJohn Forte 		startPos = strchr(ipStr, cBracketR);
598fcf3ce44SJohn Forte 		if (!startPos) {
599fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s: ']' %s\n",
600fcf3ce44SJohn Forte 			    cmdName, ipStr, gettext("missing"));
601fcf3ce44SJohn Forte 			return (1);
602fcf3ce44SJohn Forte 		}
603fcf3ce44SJohn Forte 		(void) strlcpy(tmpStr, ipStr+1, startPos-ipStr);
604fcf3ce44SJohn Forte 		address->hostnameIpAddress.id.ipAddress.ipv4Address = IMA_FALSE;
605fcf3ce44SJohn Forte 		tmpStrPtr = tmpStr;
606fcf3ce44SJohn Forte 	} else {
607fcf3ce44SJohn Forte 		/* set start position to beginning of input object */
608fcf3ce44SJohn Forte 		addressType = AF_INET;
609fcf3ce44SJohn Forte 		startPos = ipStr;
610fcf3ce44SJohn Forte 		address->hostnameIpAddress.id.ipAddress.ipv4Address = IMA_TRUE;
611fcf3ce44SJohn Forte 		tmpStrPtr = ipStr;
612fcf3ce44SJohn Forte 	}
613fcf3ce44SJohn Forte 	/* wcschr for ':'. If not there, use default port */
614fcf3ce44SJohn Forte 	colPos = strchr(startPos, cCol);
615fcf3ce44SJohn Forte 
616fcf3ce44SJohn Forte 	if (!colPos) {
617fcf3ce44SJohn Forte 		if (addrType == DISCOVERY_ADDRESS) {
618fcf3ce44SJohn Forte 			inputPort = DEFAULT_ISCSI_PORT;
619fcf3ce44SJohn Forte 		} else if (addrType == ISNS_SERVER_ADDRESS) {
620fcf3ce44SJohn Forte 			inputPort = ISNS_DEFAULT_SERVER_PORT;
621fcf3ce44SJohn Forte 		} else {
622fcf3ce44SJohn Forte 			*colPos = NULL;
623fcf3ce44SJohn Forte 		}
624fcf3ce44SJohn Forte 	} else {
625fcf3ce44SJohn Forte 		*colPos = NULL;
626fcf3ce44SJohn Forte 	}
627fcf3ce44SJohn Forte 
628fcf3ce44SJohn Forte 	rval = inet_pton(addressType, tmpStrPtr,
629fcf3ce44SJohn Forte 	    address->hostnameIpAddress.id.ipAddress.ipAddress);
630fcf3ce44SJohn Forte 	/* inet_pton returns 1 on success */
631fcf3ce44SJohn Forte 	if (rval != 1) {
632fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s: %s\n", cmdName, ipStr,
633fcf3ce44SJohn Forte 		    gettext("invalid IP address"));
634fcf3ce44SJohn Forte 		return (1);
635fcf3ce44SJohn Forte 	}
636fcf3ce44SJohn Forte 
637fcf3ce44SJohn Forte 
638fcf3ce44SJohn Forte 	if (colPos) {
639fcf3ce44SJohn Forte 		char *errchr;
640fcf3ce44SJohn Forte 
641fcf3ce44SJohn Forte 		colPos++;
642fcf3ce44SJohn Forte 		if (*colPos == NULL) {
643fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s: %s\n",
644fcf3ce44SJohn Forte 			    cmdName, ipStr,
645fcf3ce44SJohn Forte 			    gettext("port number missing"));
646fcf3ce44SJohn Forte 			return (1);
647fcf3ce44SJohn Forte 		}
648fcf3ce44SJohn Forte 
649fcf3ce44SJohn Forte 		/*
650fcf3ce44SJohn Forte 		 * convert port string to unsigned value
651fcf3ce44SJohn Forte 		 * Note:  Don't remove errno = 0 as you may get false failures.
652fcf3ce44SJohn Forte 		 */
653fcf3ce44SJohn Forte 		errno = 0;
654fcf3ce44SJohn Forte 		inputPort = strtol(colPos, &errchr, 10);
655fcf3ce44SJohn Forte 		if (errno != 0 || inputPort == 0 && errchr != NULL) {
656fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s:%s %s\n",
657fcf3ce44SJohn Forte 			    cmdName, ipStr, colPos,
658fcf3ce44SJohn Forte 			    gettext("port number invalid"));
659fcf3ce44SJohn Forte 			return (1);
660fcf3ce44SJohn Forte 		}
661fcf3ce44SJohn Forte 		/* make sure it's in the range */
662fcf3ce44SJohn Forte 		if (inputPort > USHRT_MAX) {
663fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s: %s\n",
664fcf3ce44SJohn Forte 			    cmdName, ipStr,
665fcf3ce44SJohn Forte 			    gettext("port number out of range"));
666fcf3ce44SJohn Forte 			return (1);
667fcf3ce44SJohn Forte 		}
668fcf3ce44SJohn Forte 	}
669fcf3ce44SJohn Forte 	address->portNumber  = inputPort;
670fcf3ce44SJohn Forte 
671fcf3ce44SJohn Forte 	return (0);
672fcf3ce44SJohn Forte }
673fcf3ce44SJohn Forte 
674fcf3ce44SJohn Forte /*
675fcf3ce44SJohn Forte  * Print results of send targets command
676fcf3ce44SJohn Forte  */
677fcf3ce44SJohn Forte static void
678fcf3ce44SJohn Forte printSendTargets(SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES *pList)
679fcf3ce44SJohn Forte {
680fcf3ce44SJohn Forte 	char outBuf[INET6_ADDRSTRLEN];
681fcf3ce44SJohn Forte 	int inetSize;
682fcf3ce44SJohn Forte 	int af;
683fcf3ce44SJohn Forte 	int i;
684fcf3ce44SJohn Forte 
685fcf3ce44SJohn Forte 	for (i = 0; i < pList->keyCount; i++) {
686fcf3ce44SJohn Forte 		if (pList->keys[i].address.ipAddress.ipv4Address == IMA_TRUE) {
687fcf3ce44SJohn Forte 			af = AF_INET;
688fcf3ce44SJohn Forte 			inetSize = INET_ADDRSTRLEN;
689fcf3ce44SJohn Forte 		} else {
690fcf3ce44SJohn Forte 			af = AF_INET6;
691fcf3ce44SJohn Forte 			inetSize = INET6_ADDRSTRLEN;
692fcf3ce44SJohn Forte 		}
693fcf3ce44SJohn Forte 		(void) fprintf(stdout, gettext("\tTarget name: %ws\n"),
694fcf3ce44SJohn Forte 		    pList->keys[i].name);
695fcf3ce44SJohn Forte 		(void) fprintf(stdout, "\t\t%s: %15s:%d", "Target address",
696fcf3ce44SJohn Forte 		    inet_ntop(af, &(pList->keys[i].address.ipAddress.ipAddress),
697fcf3ce44SJohn Forte 		    outBuf, inetSize), pList->keys[i].address.portNumber);
698fcf3ce44SJohn Forte 		(void) fprintf(stdout, ", %d", pList->keys[i].tpgt);
699fcf3ce44SJohn Forte 		(void) fprintf(stdout, "\n");
700fcf3ce44SJohn Forte 	}
701fcf3ce44SJohn Forte }
702fcf3ce44SJohn Forte 
703fcf3ce44SJohn Forte 
704fcf3ce44SJohn Forte /*
705fcf3ce44SJohn Forte  * Print all login parameters
706fcf3ce44SJohn Forte  */
707fcf3ce44SJohn Forte static int
708fcf3ce44SJohn Forte printLoginParameters(char *prefix, IMA_OID oid, int printOption)
709fcf3ce44SJohn Forte {
710fcf3ce44SJohn Forte 	IMA_STATUS status;
711fcf3ce44SJohn Forte 	IMA_BOOL_VALUE propBool;
712fcf3ce44SJohn Forte 	IMA_MIN_MAX_VALUE propMinMax;
713fcf3ce44SJohn Forte 	char longString[MAX_LONG_CHAR_LEN + 1];
714fcf3ce44SJohn Forte 	SUN_IMA_CONN_PROPERTIES	*connProps = NULL;
715fcf3ce44SJohn Forte 	IMA_OID_LIST *pConnList;
716fcf3ce44SJohn Forte 
717fcf3ce44SJohn Forte 	(void) memset(longString, 0, sizeof (longString));
718fcf3ce44SJohn Forte 
719fcf3ce44SJohn Forte 	switch (printOption) {
720fcf3ce44SJohn Forte 		case PRINT_CONFIGURED_PARAMS:
721fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s%s:\n",
722fcf3ce44SJohn Forte 			    prefix,
723fcf3ce44SJohn Forte 			    gettext("Login Parameters (Default/Configured)"));
724fcf3ce44SJohn Forte 			break;
725fcf3ce44SJohn Forte 		case PRINT_NEGOTIATED_PARAMS:
726fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s%s:\n",
727fcf3ce44SJohn Forte 			    prefix,
728fcf3ce44SJohn Forte 			    gettext("Login Parameters (Negotiated)"));
729fcf3ce44SJohn Forte 			status = SUN_IMA_GetConnOidList(
730fcf3ce44SJohn Forte 			    &oid,
731fcf3ce44SJohn Forte 			    &pConnList);
732fcf3ce44SJohn Forte 
733fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
734fcf3ce44SJohn Forte 				printLibError(status);
735fcf3ce44SJohn Forte 				return (1);
736fcf3ce44SJohn Forte 			}
737fcf3ce44SJohn Forte 
738fcf3ce44SJohn Forte 			status = SUN_IMA_GetConnProperties(&pConnList->oids[0],
739fcf3ce44SJohn Forte 			    &connProps);
740fcf3ce44SJohn Forte 			propBool.currentValueValid = connProps->valuesValid;
741fcf3ce44SJohn Forte 			propMinMax.currentValueValid = connProps->valuesValid;
742fcf3ce44SJohn Forte 			break;
743fcf3ce44SJohn Forte 		default:
744fcf3ce44SJohn Forte 			return (1);
745fcf3ce44SJohn Forte 	}
746fcf3ce44SJohn Forte 
747fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
748fcf3ce44SJohn Forte 		propBool.currentValue = connProps->dataSequenceInOrder;
749fcf3ce44SJohn Forte 	} else {
750fcf3ce44SJohn Forte 		status = IMA_GetDataSequenceInOrderProperties(oid, &propBool);
751fcf3ce44SJohn Forte 	}
752fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
753fcf3ce44SJohn Forte 		printLibError(status);
754fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
755fcf3ce44SJohn Forte 		return (1);
756fcf3ce44SJohn Forte 	}
757fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix,
758fcf3ce44SJohn Forte 	    gettext("Data Sequence In Order"));
759fcf3ce44SJohn Forte 	IMABOOLPRINT(propBool, printOption);
760fcf3ce44SJohn Forte 
761fcf3ce44SJohn Forte 
762fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
763fcf3ce44SJohn Forte 		propBool.currentValue = connProps->dataPduInOrder;
764fcf3ce44SJohn Forte 	} else {
765fcf3ce44SJohn Forte 		status = IMA_GetDataPduInOrderProperties(oid, &propBool);
766fcf3ce44SJohn Forte 	}
767fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
768fcf3ce44SJohn Forte 		printLibError(status);
769fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
770fcf3ce44SJohn Forte 		return (1);
771fcf3ce44SJohn Forte 	}
772fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix,
773fcf3ce44SJohn Forte 	    gettext("Data PDU In Order"));
774fcf3ce44SJohn Forte 	IMABOOLPRINT(propBool, printOption);
775fcf3ce44SJohn Forte 
776fcf3ce44SJohn Forte 
777fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
778fcf3ce44SJohn Forte 		propMinMax.currentValue = connProps->defaultTime2Retain;
779fcf3ce44SJohn Forte 	} else {
780fcf3ce44SJohn Forte 		status = IMA_GetDefaultTime2RetainProperties(oid, &propMinMax);
781fcf3ce44SJohn Forte 	}
782fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
783fcf3ce44SJohn Forte 		printLibError(status);
784fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
785fcf3ce44SJohn Forte 		return (1);
786fcf3ce44SJohn Forte 	}
787fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix,
788fcf3ce44SJohn Forte 	    gettext("Default Time To Retain"));
789fcf3ce44SJohn Forte 	IMAMINMAXPRINT(propMinMax, printOption);
790fcf3ce44SJohn Forte 
791fcf3ce44SJohn Forte 
792fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
793fcf3ce44SJohn Forte 		propMinMax.currentValue = connProps->defaultTime2Wait;
794fcf3ce44SJohn Forte 	} else {
795fcf3ce44SJohn Forte 		status = IMA_GetDefaultTime2WaitProperties(oid, &propMinMax);
796fcf3ce44SJohn Forte 	}
797fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
798fcf3ce44SJohn Forte 		printLibError(status);
799fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
800fcf3ce44SJohn Forte 		return (1);
801fcf3ce44SJohn Forte 	}
802fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix,
803fcf3ce44SJohn Forte 	    gettext("Default Time To Wait"));
804fcf3ce44SJohn Forte 	IMAMINMAXPRINT(propMinMax, printOption);
805fcf3ce44SJohn Forte 
806fcf3ce44SJohn Forte 
807fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
808fcf3ce44SJohn Forte 		propMinMax.currentValue = connProps->errorRecoveryLevel;
809fcf3ce44SJohn Forte 	} else {
810fcf3ce44SJohn Forte 		status = IMA_GetErrorRecoveryLevelProperties(oid, &propMinMax);
811fcf3ce44SJohn Forte 	}
812fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
813fcf3ce44SJohn Forte 		printLibError(status);
814fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
815fcf3ce44SJohn Forte 		return (1);
816fcf3ce44SJohn Forte 	}
817fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix,
818fcf3ce44SJohn Forte 	    gettext("Error Recovery Level"));
819fcf3ce44SJohn Forte 	IMAMINMAXPRINT(propMinMax, printOption);
820fcf3ce44SJohn Forte 
821fcf3ce44SJohn Forte 
822fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
823fcf3ce44SJohn Forte 		propMinMax.currentValue = connProps->firstBurstLength;
824fcf3ce44SJohn Forte 	} else {
825fcf3ce44SJohn Forte 		status = IMA_GetFirstBurstLengthProperties(oid,
826fcf3ce44SJohn Forte 		    &propMinMax);
827fcf3ce44SJohn Forte 	}
828fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
829fcf3ce44SJohn Forte 		printLibError(status);
830fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
831fcf3ce44SJohn Forte 		return (1);
832fcf3ce44SJohn Forte 	}
833fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ",
834fcf3ce44SJohn Forte 	    prefix, gettext("First Burst Length"));
835fcf3ce44SJohn Forte 	IMAMINMAXPRINT(propMinMax, printOption);
836fcf3ce44SJohn Forte 
837fcf3ce44SJohn Forte 
838fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
839fcf3ce44SJohn Forte 		propBool.currentValue = connProps->immediateData;
840fcf3ce44SJohn Forte 	} else {
841fcf3ce44SJohn Forte 		status = IMA_GetImmediateDataProperties(oid, &propBool);
842fcf3ce44SJohn Forte 	}
843fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
844fcf3ce44SJohn Forte 		printLibError(status);
845fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
846fcf3ce44SJohn Forte 		return (1);
847fcf3ce44SJohn Forte 	}
848fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix, gettext("Immediate Data"));
849fcf3ce44SJohn Forte 	IMABOOLPRINT(propBool, printOption);
850fcf3ce44SJohn Forte 
851fcf3ce44SJohn Forte 
852fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
853fcf3ce44SJohn Forte 		propBool.currentValue = connProps->initialR2T;
854fcf3ce44SJohn Forte 	} else {
855fcf3ce44SJohn Forte 		status = IMA_GetInitialR2TProperties(oid, &propBool);
856fcf3ce44SJohn Forte 	}
857fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
858fcf3ce44SJohn Forte 		printLibError(status);
859fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
860fcf3ce44SJohn Forte 		return (1);
861fcf3ce44SJohn Forte 	}
862fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix,
863fcf3ce44SJohn Forte 	    gettext("Initial Ready To Transfer (R2T)"));
864fcf3ce44SJohn Forte 	IMABOOLPRINT(propBool, printOption);
865fcf3ce44SJohn Forte 
866fcf3ce44SJohn Forte 
867fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
868fcf3ce44SJohn Forte 		propMinMax.currentValue = connProps->maxBurstLength;
869fcf3ce44SJohn Forte 	} else {
870fcf3ce44SJohn Forte 		status = IMA_GetMaxBurstLengthProperties(oid, &propMinMax);
871fcf3ce44SJohn Forte 	}
872fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
873fcf3ce44SJohn Forte 		printLibError(status);
874fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
875fcf3ce44SJohn Forte 		return (1);
876fcf3ce44SJohn Forte 	}
877fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix, gettext("Max Burst Length"));
878fcf3ce44SJohn Forte 	IMAMINMAXPRINT(propMinMax, printOption);
879fcf3ce44SJohn Forte 
880fcf3ce44SJohn Forte 
881fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
882fcf3ce44SJohn Forte 		propMinMax.currentValue = connProps->maxOutstandingR2T;
883fcf3ce44SJohn Forte 	} else {
884fcf3ce44SJohn Forte 		status = IMA_GetMaxOutstandingR2TProperties(oid, &propMinMax);
885fcf3ce44SJohn Forte 	}
886fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
887fcf3ce44SJohn Forte 		printLibError(status);
888fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
889fcf3ce44SJohn Forte 		return (1);
890fcf3ce44SJohn Forte 	}
891fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix,
892fcf3ce44SJohn Forte 	    gettext("Max Outstanding R2T"));
893fcf3ce44SJohn Forte 	IMAMINMAXPRINT(propMinMax, printOption);
894fcf3ce44SJohn Forte 
895fcf3ce44SJohn Forte 
896fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
897fcf3ce44SJohn Forte 		propMinMax.currentValue = connProps->maxRecvDataSegmentLength;
898fcf3ce44SJohn Forte 	} else {
899fcf3ce44SJohn Forte 		status = IMA_GetMaxRecvDataSegmentLengthProperties(oid,
900fcf3ce44SJohn Forte 		    &propMinMax);
901fcf3ce44SJohn Forte 	}
902fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
903fcf3ce44SJohn Forte 		printLibError(status);
904fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
905fcf3ce44SJohn Forte 		return (1);
906fcf3ce44SJohn Forte 	}
907fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix,
908fcf3ce44SJohn Forte 	    gettext("Max Receive Data Segment Length"));
909fcf3ce44SJohn Forte 	IMAMINMAXPRINT(propMinMax, printOption);
910fcf3ce44SJohn Forte 
911fcf3ce44SJohn Forte 
912fcf3ce44SJohn Forte 	if (printOption == PRINT_NEGOTIATED_PARAMS) {
913fcf3ce44SJohn Forte 		propMinMax.currentValue = connProps->maxConnections;
914fcf3ce44SJohn Forte 	} else {
915fcf3ce44SJohn Forte 		status = IMA_GetMaxConnectionsProperties(oid, &propMinMax);
916fcf3ce44SJohn Forte 	}
917fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
918fcf3ce44SJohn Forte 		printLibError(status);
919fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
920fcf3ce44SJohn Forte 		return (1);
921fcf3ce44SJohn Forte 	}
922fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s\t%s: ", prefix, gettext("Max Connections"));
923fcf3ce44SJohn Forte 	IMAMINMAXPRINT(propMinMax, printOption);
924fcf3ce44SJohn Forte 
925fcf3ce44SJohn Forte 	(void) IMA_FreeMemory(connProps);
926fcf3ce44SJohn Forte 	return (0);
927fcf3ce44SJohn Forte }
928fcf3ce44SJohn Forte 
929fcf3ce44SJohn Forte /*
930fcf3ce44SJohn Forte  * Print discovery information.
931fcf3ce44SJohn Forte  */
932fcf3ce44SJohn Forte static void
933fcf3ce44SJohn Forte printDiscoveryMethod(char *prefix, IMA_UINT32 discoveryMethodFlags)
934fcf3ce44SJohn Forte {
935fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s%s: ", prefix, gettext("Discovery Method"));
936fcf3ce44SJohn Forte 	if (discoveryMethodFlags == IMA_TARGET_DISCOVERY_METHOD_UNKNOWN) {
937fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%s\n", gettext("NA"));
938fcf3ce44SJohn Forte 	} else {
939fcf3ce44SJohn Forte 		if (!((discoveryMethodFlags &
940fcf3ce44SJohn Forte 		    IMA_TARGET_DISCOVERY_METHOD_STATIC) ^
941fcf3ce44SJohn Forte 		    IMA_TARGET_DISCOVERY_METHOD_STATIC)) {
942fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s ", gettext("Static"));
943fcf3ce44SJohn Forte 		}
944fcf3ce44SJohn Forte 		if (!((discoveryMethodFlags &
945fcf3ce44SJohn Forte 		    IMA_TARGET_DISCOVERY_METHOD_SENDTARGETS) ^
946fcf3ce44SJohn Forte 		    IMA_TARGET_DISCOVERY_METHOD_SENDTARGETS)) {
947fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s ", gettext("SendTargets"));
948fcf3ce44SJohn Forte 		}
949fcf3ce44SJohn Forte 		if (!((discoveryMethodFlags &
950fcf3ce44SJohn Forte 		    IMA_TARGET_DISCOVERY_METHOD_ISNS) ^
951fcf3ce44SJohn Forte 		    IMA_TARGET_DISCOVERY_METHOD_ISNS)) {
952fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s ", gettext("iSNS"));
953fcf3ce44SJohn Forte 		}
954fcf3ce44SJohn Forte 		(void) fprintf(stdout, "\n");
955fcf3ce44SJohn Forte 	}
956fcf3ce44SJohn Forte }
957fcf3ce44SJohn Forte 
958fcf3ce44SJohn Forte /*
959fcf3ce44SJohn Forte  * printConnectionList - Prints the conection list provided
960fcf3ce44SJohn Forte  */
961fcf3ce44SJohn Forte static void
962fcf3ce44SJohn Forte printConnectionList(char *prefix, IMA_OID_LIST *pConnList)
963fcf3ce44SJohn Forte {
964fcf3ce44SJohn Forte 	IMA_STATUS		imaStatus;
965fcf3ce44SJohn Forte 	int			i;
966fcf3ce44SJohn Forte 	SUN_IMA_CONN_PROPERTIES	*connProps;
967fcf3ce44SJohn Forte 	union {
968fcf3ce44SJohn Forte 		char	ipv4[INET_ADDRSTRLEN+1];
969fcf3ce44SJohn Forte 		char	ipv6[INET6_ADDRSTRLEN+1];
970fcf3ce44SJohn Forte 	} tmp;
971fcf3ce44SJohn Forte 
972fcf3ce44SJohn Forte 	for (i = 0; i < pConnList->oidCount; i++) {
973fcf3ce44SJohn Forte 		imaStatus = SUN_IMA_GetConnProperties(&pConnList->oids[i],
974fcf3ce44SJohn Forte 		    &connProps);
975fcf3ce44SJohn Forte 
976fcf3ce44SJohn Forte 		if (imaStatus != IMA_STATUS_SUCCESS) {
977fcf3ce44SJohn Forte 			continue;
978fcf3ce44SJohn Forte 		}
979fcf3ce44SJohn Forte 
980fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%sCID: %d\n", prefix,
981fcf3ce44SJohn Forte 		    connProps->connectionID);
982fcf3ce44SJohn Forte 
983fcf3ce44SJohn Forte 		(void) memset(&tmp, 0, sizeof (tmp));
984fcf3ce44SJohn Forte 		if (connProps->local.ipAddress.ipv4Address == IMA_TRUE) {
985fcf3ce44SJohn Forte 			if (inet_ntop(AF_INET,
986fcf3ce44SJohn Forte 			    &connProps->local.ipAddress.ipAddress[0],
987fcf3ce44SJohn Forte 			    &tmp.ipv4[0],
988fcf3ce44SJohn Forte 			    INET_ADDRSTRLEN)) {
989fcf3ce44SJohn Forte 				(void) fprintf(stdout,
990fcf3ce44SJohn Forte 				    "%s  %s: %s:%u\n",
991fcf3ce44SJohn Forte 				    prefix,
992fcf3ce44SJohn Forte 				    gettext("IP address (Local)"),
993fcf3ce44SJohn Forte 				    &tmp.ipv4[0],
994fcf3ce44SJohn Forte 				    ntohs(connProps->local.portNumber));
995fcf3ce44SJohn Forte 			}
996fcf3ce44SJohn Forte 		} else {
997fcf3ce44SJohn Forte 			if (inet_ntop(AF_INET6,
998fcf3ce44SJohn Forte 			    &connProps->local.ipAddress.ipAddress[0],
999fcf3ce44SJohn Forte 			    &tmp.ipv6[0],
1000fcf3ce44SJohn Forte 			    INET6_ADDRSTRLEN)) {
1001fcf3ce44SJohn Forte 				(void) fprintf(stdout,
1002fcf3ce44SJohn Forte 				    "%s  %s: [%s]:%u\n",
1003fcf3ce44SJohn Forte 				    prefix,
1004fcf3ce44SJohn Forte 				    gettext("IP address (Local)"),
1005fcf3ce44SJohn Forte 				    &tmp.ipv6[0],
1006fcf3ce44SJohn Forte 				    ntohs(connProps->local.portNumber));
1007fcf3ce44SJohn Forte 			}
1008fcf3ce44SJohn Forte 		}
1009fcf3ce44SJohn Forte 		if (connProps->peer.ipAddress.ipv4Address == IMA_TRUE) {
1010fcf3ce44SJohn Forte 			if (inet_ntop(AF_INET,
1011fcf3ce44SJohn Forte 			    &connProps->peer.ipAddress.ipAddress[0],
1012fcf3ce44SJohn Forte 			    &tmp.ipv4[0],
1013fcf3ce44SJohn Forte 			    INET_ADDRSTRLEN)) {
1014fcf3ce44SJohn Forte 				(void) fprintf(stdout,
1015fcf3ce44SJohn Forte 				    "%s  %s: %s:%u\n",
1016fcf3ce44SJohn Forte 				    prefix,
1017fcf3ce44SJohn Forte 				    gettext("IP address (Peer)"),
1018fcf3ce44SJohn Forte 				    &tmp.ipv4[0],
1019fcf3ce44SJohn Forte 				    ntohs(connProps->peer.portNumber));
1020fcf3ce44SJohn Forte 			}
1021fcf3ce44SJohn Forte 		} else {
1022fcf3ce44SJohn Forte 			if (inet_ntop(AF_INET6,
1023fcf3ce44SJohn Forte 			    &connProps->peer.ipAddress.ipAddress[0],
1024fcf3ce44SJohn Forte 			    &tmp.ipv6[0],
1025fcf3ce44SJohn Forte 			    INET6_ADDRSTRLEN)) {
1026fcf3ce44SJohn Forte 				(void) fprintf(stdout,
1027fcf3ce44SJohn Forte 				    "%s  %s: [%s]:%u\n",
1028fcf3ce44SJohn Forte 				    prefix,
1029fcf3ce44SJohn Forte 				    gettext("IP address (Peer)"),
1030fcf3ce44SJohn Forte 				    &tmp.ipv6[0],
1031fcf3ce44SJohn Forte 				    ntohs(connProps->peer.portNumber));
1032fcf3ce44SJohn Forte 			}
1033fcf3ce44SJohn Forte 		}
1034fcf3ce44SJohn Forte 
1035fcf3ce44SJohn Forte 		(void) IMA_FreeMemory(connProps);
1036fcf3ce44SJohn Forte 	}
1037fcf3ce44SJohn Forte }
1038fcf3ce44SJohn Forte 
1039fcf3ce44SJohn Forte /*
1040fcf3ce44SJohn Forte  * Set login parameters on a target or initiator
1041fcf3ce44SJohn Forte  */
1042fcf3ce44SJohn Forte static int
1043fcf3ce44SJohn Forte setLoginParameter(IMA_OID oid, int optval, char *optarg)
1044fcf3ce44SJohn Forte {
1045fcf3ce44SJohn Forte 	IMA_STATUS status = IMA_STATUS_SUCCESS;
1046fcf3ce44SJohn Forte 	IMA_UINT uintValue;
1047fcf3ce44SJohn Forte 	IMA_BOOL boolValue;
1048fcf3ce44SJohn Forte 	SUN_IMA_DIGEST_ALGORITHM digestAlgList[1];
1049fcf3ce44SJohn Forte 	IMA_MIN_MAX_VALUE propMinMax;
1050fcf3ce44SJohn Forte 	char *endptr;
1051fcf3ce44SJohn Forte 
1052fcf3ce44SJohn Forte 	/*
1053fcf3ce44SJohn Forte 	 * for clarity, there are two switch statements
1054fcf3ce44SJohn Forte 	 * The first loads the variable and the second
1055fcf3ce44SJohn Forte 	 * calls the appropriate API
1056fcf3ce44SJohn Forte 	 */
1057fcf3ce44SJohn Forte 	switch (optval) {
1058fcf3ce44SJohn Forte 		case DATA_SEQ_IN_ORDER:
1059fcf3ce44SJohn Forte 		case IMMEDIATE_DATA:
1060fcf3ce44SJohn Forte 		case INITIAL_R2T:
1061fcf3ce44SJohn Forte 		case DATA_PDU_IN_ORDER:
1062fcf3ce44SJohn Forte 			/* implement 'default'? */
1063fcf3ce44SJohn Forte 			if (strcasecmp(optarg, "yes") == 0) {
1064fcf3ce44SJohn Forte 				boolValue = IMA_TRUE;
1065fcf3ce44SJohn Forte 			} else if (strcasecmp(optarg, "no") == 0) {
1066fcf3ce44SJohn Forte 				boolValue = IMA_FALSE;
1067fcf3ce44SJohn Forte 			} else {
1068fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s - %s\n",
1069fcf3ce44SJohn Forte 				    cmdName,
1070fcf3ce44SJohn Forte 				    gettext("invalid option argument"),
1071fcf3ce44SJohn Forte 				    optarg);
1072fcf3ce44SJohn Forte 				return (1);
1073fcf3ce44SJohn Forte 			}
1074fcf3ce44SJohn Forte 			break;
1075fcf3ce44SJohn Forte 		case DEFAULT_TIME_2_RETAIN:
1076fcf3ce44SJohn Forte 		case DEFAULT_TIME_2_WAIT:
1077fcf3ce44SJohn Forte 			errno = 0;
1078fcf3ce44SJohn Forte 			uintValue = strtoul(optarg, &endptr, 0);
1079fcf3ce44SJohn Forte 			if (*endptr != '\0' || errno != 0) {
1080fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s - %s\n",
1081fcf3ce44SJohn Forte 				    cmdName,
1082fcf3ce44SJohn Forte 				    gettext("invalid option argument"),
1083fcf3ce44SJohn Forte 				    optarg);
1084fcf3ce44SJohn Forte 				return (1);
1085fcf3ce44SJohn Forte 			}
1086fcf3ce44SJohn Forte 			if (uintValue > 3600) {
1087fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n",
1088fcf3ce44SJohn Forte 				    cmdName,
1089fcf3ce44SJohn Forte gettext("value must be between 0 and 3600"));
1090fcf3ce44SJohn Forte 				return (1);
1091fcf3ce44SJohn Forte 			}
1092fcf3ce44SJohn Forte 			break;
1093fcf3ce44SJohn Forte 		case FIRST_BURST_LENGTH:
1094fcf3ce44SJohn Forte 		case MAX_BURST_LENGTH:
1095fcf3ce44SJohn Forte 		case MAX_RECV_DATA_SEG_LEN:
1096fcf3ce44SJohn Forte 			errno = 0;
1097fcf3ce44SJohn Forte 			/* implement 'default'? */
1098fcf3ce44SJohn Forte 			uintValue = strtoul(optarg, &endptr, 0);
1099fcf3ce44SJohn Forte 			if (*endptr != '\0' || errno != 0) {
1100fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s - %s\n",
1101fcf3ce44SJohn Forte 				    cmdName,
1102fcf3ce44SJohn Forte 				    gettext("invalid option argument"),
1103fcf3ce44SJohn Forte 				    optarg);
1104fcf3ce44SJohn Forte 				return (1);
1105fcf3ce44SJohn Forte 			}
1106fcf3ce44SJohn Forte 			if (uintValue < 512 || uintValue > 16777215) {
1107fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n",
1108fcf3ce44SJohn Forte 				    cmdName,
1109fcf3ce44SJohn Forte gettext("value must be between 512 and 16777215"));
1110fcf3ce44SJohn Forte 				return (1);
1111fcf3ce44SJohn Forte 			}
1112fcf3ce44SJohn Forte 			break;
1113fcf3ce44SJohn Forte 		case MAX_OUTSTANDING_R2T:
1114fcf3ce44SJohn Forte 			errno = 0;
1115fcf3ce44SJohn Forte 			uintValue = strtoul(optarg, &endptr, 0);
1116fcf3ce44SJohn Forte 			if (*endptr != '\0' || errno != 0) {
1117fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s - %s\n",
1118fcf3ce44SJohn Forte 				    cmdName,
1119fcf3ce44SJohn Forte 				    gettext("invalid option argument"),
1120fcf3ce44SJohn Forte 				    optarg);
1121fcf3ce44SJohn Forte 				return (1);
1122fcf3ce44SJohn Forte 			}
1123fcf3ce44SJohn Forte 			if (uintValue < 1 || uintValue > 65535) {
1124fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n",
1125fcf3ce44SJohn Forte 				    cmdName,
1126fcf3ce44SJohn Forte gettext("value must be between 1 and 65535"));
1127fcf3ce44SJohn Forte 				return (1);
1128fcf3ce44SJohn Forte 			}
1129fcf3ce44SJohn Forte 			break;
1130fcf3ce44SJohn Forte 		case HEADER_DIGEST:
1131fcf3ce44SJohn Forte 		case DATA_DIGEST:
1132fcf3ce44SJohn Forte 			if (strcasecmp(optarg, "none") == 0) {
1133fcf3ce44SJohn Forte 				digestAlgList[0] = SUN_IMA_DIGEST_NONE;
1134fcf3ce44SJohn Forte 			} else if (strcasecmp(optarg, "CRC32") == 0) {
1135fcf3ce44SJohn Forte 				digestAlgList[0] = SUN_IMA_DIGEST_CRC32;
1136fcf3ce44SJohn Forte 			} else {
1137fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s - %s\n",
1138fcf3ce44SJohn Forte 				    cmdName,
1139fcf3ce44SJohn Forte 				    gettext("invalid option argument"),
1140fcf3ce44SJohn Forte 				    optarg);
1141fcf3ce44SJohn Forte 				return (1);
1142fcf3ce44SJohn Forte 			}
1143fcf3ce44SJohn Forte 			break;
1144fcf3ce44SJohn Forte 		case MAX_CONNECTIONS:
1145fcf3ce44SJohn Forte 			errno = 0;
1146fcf3ce44SJohn Forte 			uintValue = strtoul(optarg, &endptr, 0);
1147fcf3ce44SJohn Forte 			if (*endptr != '\0' || errno != 0) {
1148fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s - %s\n",
1149fcf3ce44SJohn Forte 				    cmdName,
1150fcf3ce44SJohn Forte 				    gettext("invalid option argument"),
1151fcf3ce44SJohn Forte 				    optarg);
1152fcf3ce44SJohn Forte 				return (1);
1153fcf3ce44SJohn Forte 			}
1154fcf3ce44SJohn Forte 			if (uintValue < 1 || uintValue > 256) {
1155fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n",
1156fcf3ce44SJohn Forte 				    cmdName,
1157fcf3ce44SJohn Forte gettext("value must be between 1 and 256"));
1158fcf3ce44SJohn Forte 				return (1);
1159fcf3ce44SJohn Forte 			}
1160fcf3ce44SJohn Forte 			break;
1161fcf3ce44SJohn Forte 		case ERROR_RECOVERY_LEVEL:
1162fcf3ce44SJohn Forte 			errno = 0;
1163fcf3ce44SJohn Forte 			uintValue = strtoul(optarg, &endptr, 0);
1164fcf3ce44SJohn Forte 			if (*endptr != '\0' || errno != 0) {
1165fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s - %s\n",
1166fcf3ce44SJohn Forte 				    cmdName,
1167fcf3ce44SJohn Forte 				    gettext("invalid option argument"),
1168fcf3ce44SJohn Forte 				    optarg);
1169fcf3ce44SJohn Forte 				return (1);
1170fcf3ce44SJohn Forte 			}
1171fcf3ce44SJohn Forte 			if (uintValue > 2) {
1172fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n",
1173fcf3ce44SJohn Forte 				    cmdName,
1174fcf3ce44SJohn Forte gettext("value must be between 0 and 2"));
1175fcf3ce44SJohn Forte 				return (1);
1176fcf3ce44SJohn Forte 			}
1177fcf3ce44SJohn Forte 			break;
1178fcf3ce44SJohn Forte 		default:
1179fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %c: %s\n",
1180fcf3ce44SJohn Forte 			    cmdName, optval, gettext("unknown option"));
1181fcf3ce44SJohn Forte 			return (1);
1182fcf3ce44SJohn Forte 	}
1183fcf3ce44SJohn Forte 
1184fcf3ce44SJohn Forte 	switch (optval) {
1185fcf3ce44SJohn Forte 		case DATA_PDU_IN_ORDER:
1186fcf3ce44SJohn Forte 			status = IMA_SetDataPduInOrder(oid, boolValue);
1187fcf3ce44SJohn Forte 			break;
1188fcf3ce44SJohn Forte 		case DATA_SEQ_IN_ORDER:
1189fcf3ce44SJohn Forte 			status = IMA_SetDataSequenceInOrder(oid, boolValue);
1190fcf3ce44SJohn Forte 			break;
1191fcf3ce44SJohn Forte 		case DEFAULT_TIME_2_RETAIN:
1192fcf3ce44SJohn Forte 			status = IMA_SetDefaultTime2Retain(oid, uintValue);
1193fcf3ce44SJohn Forte 			break;
1194fcf3ce44SJohn Forte 		case DEFAULT_TIME_2_WAIT:
1195fcf3ce44SJohn Forte 			status = IMA_SetDefaultTime2Wait(oid, uintValue);
1196fcf3ce44SJohn Forte 			break;
1197fcf3ce44SJohn Forte 		case FIRST_BURST_LENGTH:
1198fcf3ce44SJohn Forte 			status = IMA_SetFirstBurstLength(oid, uintValue);
1199fcf3ce44SJohn Forte 
1200fcf3ce44SJohn Forte 			/*
1201fcf3ce44SJohn Forte 			 * If this call fails check to see if it's because
1202fcf3ce44SJohn Forte 			 * the requested value is > than maxBurstLength
1203fcf3ce44SJohn Forte 			 */
1204fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
1205fcf3ce44SJohn Forte 				status = IMA_GetMaxBurstLengthProperties(oid,
1206fcf3ce44SJohn Forte 				    &propMinMax);
1207fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
1208fcf3ce44SJohn Forte 					printLibError(status);
1209fcf3ce44SJohn Forte 					return (1);
1210fcf3ce44SJohn Forte 				}
1211fcf3ce44SJohn Forte 				if (uintValue > propMinMax.currentValue) {
1212fcf3ce44SJohn Forte 					(void) fprintf(stderr,
1213fcf3ce44SJohn Forte 					    "%s: %s\n", cmdName,
1214fcf3ce44SJohn Forte 					    gettext("firstBurstLength must " \
1215fcf3ce44SJohn Forte 					    "be less than or equal to than " \
1216fcf3ce44SJohn Forte 					    "maxBurstLength"));
1217fcf3ce44SJohn Forte 				}
1218fcf3ce44SJohn Forte 				return (1);
1219fcf3ce44SJohn Forte 			}
1220fcf3ce44SJohn Forte 
1221fcf3ce44SJohn Forte 			break;
1222fcf3ce44SJohn Forte 		case IMMEDIATE_DATA:
1223fcf3ce44SJohn Forte 			status = IMA_SetImmediateData(oid, boolValue);
1224fcf3ce44SJohn Forte 			break;
1225fcf3ce44SJohn Forte 		case INITIAL_R2T:
1226fcf3ce44SJohn Forte 			status = IMA_SetInitialR2T(oid, boolValue);
1227fcf3ce44SJohn Forte 			break;
1228fcf3ce44SJohn Forte 		case MAX_BURST_LENGTH:
1229fcf3ce44SJohn Forte 			status = IMA_SetMaxBurstLength(oid, uintValue);
1230fcf3ce44SJohn Forte 			/*
1231fcf3ce44SJohn Forte 			 * If this call fails check to see if it's because
1232fcf3ce44SJohn Forte 			 * the requested value is < than firstBurstLength
1233fcf3ce44SJohn Forte 			 */
1234fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
1235fcf3ce44SJohn Forte 				status = IMA_GetFirstBurstLengthProperties(oid,
1236fcf3ce44SJohn Forte 				    &propMinMax);
1237fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
1238fcf3ce44SJohn Forte 					printLibError(status);
1239fcf3ce44SJohn Forte 					return (1);
1240fcf3ce44SJohn Forte 				}
1241fcf3ce44SJohn Forte 				if (uintValue < propMinMax.currentValue) {
1242fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s: %s\n",
1243fcf3ce44SJohn Forte 					    cmdName,
1244fcf3ce44SJohn Forte 					    gettext("maxBurstLength must be " \
1245fcf3ce44SJohn Forte 					    "greater than or equal to " \
1246fcf3ce44SJohn Forte 					    "firstBurstLength"));
1247fcf3ce44SJohn Forte 				}
1248fcf3ce44SJohn Forte 				return (1);
1249fcf3ce44SJohn Forte 			}
1250fcf3ce44SJohn Forte 			break;
1251fcf3ce44SJohn Forte 
1252fcf3ce44SJohn Forte 		case MAX_OUTSTANDING_R2T:
1253fcf3ce44SJohn Forte 			status = IMA_SetMaxOutstandingR2T(oid, uintValue);
1254fcf3ce44SJohn Forte 			break;
1255fcf3ce44SJohn Forte 		case MAX_RECV_DATA_SEG_LEN:
1256fcf3ce44SJohn Forte 			status = IMA_SetMaxRecvDataSegmentLength(oid,
1257fcf3ce44SJohn Forte 			    uintValue);
1258fcf3ce44SJohn Forte 			break;
1259fcf3ce44SJohn Forte 		case HEADER_DIGEST:
1260fcf3ce44SJohn Forte 			status = SUN_IMA_SetHeaderDigest(oid, 1,
1261fcf3ce44SJohn Forte 			    &digestAlgList[0]);
1262fcf3ce44SJohn Forte 			break;
1263fcf3ce44SJohn Forte 		case DATA_DIGEST:
1264fcf3ce44SJohn Forte 			status = SUN_IMA_SetDataDigest(oid, 1,
1265fcf3ce44SJohn Forte 			    &digestAlgList[0]);
1266fcf3ce44SJohn Forte 			break;
1267fcf3ce44SJohn Forte 		case MAX_CONNECTIONS:
1268fcf3ce44SJohn Forte 			status = IMA_SetMaxConnections(oid, uintValue);
1269fcf3ce44SJohn Forte 			break;
1270fcf3ce44SJohn Forte 		case ERROR_RECOVERY_LEVEL:
1271fcf3ce44SJohn Forte 			status = IMA_SetErrorRecoveryLevel(oid, uintValue);
1272fcf3ce44SJohn Forte 			break;
1273fcf3ce44SJohn Forte 	}
1274fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
1275fcf3ce44SJohn Forte 		printLibError(status);
1276fcf3ce44SJohn Forte 		return (1);
1277fcf3ce44SJohn Forte 	}
1278fcf3ce44SJohn Forte 	return (0);
1279fcf3ce44SJohn Forte }
1280fcf3ce44SJohn Forte 
1281fcf3ce44SJohn Forte static void
1282fcf3ce44SJohn Forte printDigestAlgorithm(SUN_IMA_DIGEST_ALGORITHM_VALUE *digestAlgorithms,
1283fcf3ce44SJohn Forte     int printOption)
1284fcf3ce44SJohn Forte {
1285fcf3ce44SJohn Forte 	int i;
1286fcf3ce44SJohn Forte 
1287fcf3ce44SJohn Forte 	if (printOption == PRINT_CONFIGURED_PARAMS) {
1288fcf3ce44SJohn Forte 		for (i = 0; i < digestAlgorithms->defaultAlgorithmCount; i++) {
1289fcf3ce44SJohn Forte 			if (i > 0) {
1290fcf3ce44SJohn Forte 				(void) fprintf(stdout, "|");
1291fcf3ce44SJohn Forte 			}
1292fcf3ce44SJohn Forte 			switch (digestAlgorithms->defaultAlgorithms[i]) {
1293fcf3ce44SJohn Forte 				case SUN_IMA_DIGEST_NONE:
1294fcf3ce44SJohn Forte 					(void) fprintf(stdout,
1295fcf3ce44SJohn Forte 					    gettext("NONE"));
1296fcf3ce44SJohn Forte 					break;
1297fcf3ce44SJohn Forte 				case SUN_IMA_DIGEST_CRC32:
1298fcf3ce44SJohn Forte 					(void) fprintf(stdout,
1299fcf3ce44SJohn Forte 					    gettext("CRC32"));
1300fcf3ce44SJohn Forte 					break;
1301fcf3ce44SJohn Forte 				default:
1302fcf3ce44SJohn Forte 					(void) fprintf(stdout,
1303fcf3ce44SJohn Forte 					    gettext("Unknown"));
1304fcf3ce44SJohn Forte 					break;
1305fcf3ce44SJohn Forte 			}
1306fcf3ce44SJohn Forte 		}
1307fcf3ce44SJohn Forte 		(void) fprintf(stdout, "/");
1308fcf3ce44SJohn Forte 		if (digestAlgorithms->currentValid == IMA_TRUE) {
1309fcf3ce44SJohn Forte 			for (i = 0;
1310fcf3ce44SJohn Forte 			    i < digestAlgorithms->currentAlgorithmCount; i++) {
1311fcf3ce44SJohn Forte 				if (i > 0) {
1312fcf3ce44SJohn Forte 					(void) fprintf(stdout, "|");
1313fcf3ce44SJohn Forte 				}
1314fcf3ce44SJohn Forte 				switch (digestAlgorithms->
1315fcf3ce44SJohn Forte 				    currentAlgorithms[i]) {
1316fcf3ce44SJohn Forte 					case SUN_IMA_DIGEST_NONE:
1317fcf3ce44SJohn Forte 						(void) fprintf(stdout,
1318fcf3ce44SJohn Forte 						    gettext("NONE"));
1319fcf3ce44SJohn Forte 						break;
1320fcf3ce44SJohn Forte 					case SUN_IMA_DIGEST_CRC32:
1321fcf3ce44SJohn Forte 						(void) fprintf(stdout,
1322fcf3ce44SJohn Forte 						    gettext("CRC32"));
1323fcf3ce44SJohn Forte 						break;
1324fcf3ce44SJohn Forte 					default:
1325fcf3ce44SJohn Forte 						(void) fprintf(stdout,
1326fcf3ce44SJohn Forte 						    gettext("Unknown"));
1327fcf3ce44SJohn Forte 						break;
1328fcf3ce44SJohn Forte 				}
1329fcf3ce44SJohn Forte 			}
1330fcf3ce44SJohn Forte 		} else {
1331fcf3ce44SJohn Forte 			(void) fprintf(stdout, "-");
1332fcf3ce44SJohn Forte 		}
1333fcf3ce44SJohn Forte 		(void) fprintf(stdout, "\n");
1334fcf3ce44SJohn Forte 	} else if (printOption == PRINT_NEGOTIATED_PARAMS) {
1335fcf3ce44SJohn Forte 
1336fcf3ce44SJohn Forte 		if (digestAlgorithms->negotiatedValid == IMA_TRUE) {
1337fcf3ce44SJohn Forte 			for (i = 0;
1338fcf3ce44SJohn Forte 			    i < digestAlgorithms->negotiatedAlgorithmCount;
1339fcf3ce44SJohn Forte 			    i++) {
1340fcf3ce44SJohn Forte 				if (i > 0) {
1341fcf3ce44SJohn Forte 					(void) fprintf(stdout, "|");
1342fcf3ce44SJohn Forte 				}
1343fcf3ce44SJohn Forte 				switch (digestAlgorithms->
1344fcf3ce44SJohn Forte 				    negotiatedAlgorithms[i]) {
1345fcf3ce44SJohn Forte 					case SUN_IMA_DIGEST_NONE:
1346fcf3ce44SJohn Forte 						(void) fprintf(stdout,
1347fcf3ce44SJohn Forte 						    gettext("NONE"));
1348fcf3ce44SJohn Forte 						break;
1349fcf3ce44SJohn Forte 					case SUN_IMA_DIGEST_CRC32:
1350fcf3ce44SJohn Forte 						(void) fprintf(stdout,
1351fcf3ce44SJohn Forte 						    gettext("CRC32"));
1352fcf3ce44SJohn Forte 						break;
1353fcf3ce44SJohn Forte 					default:
1354fcf3ce44SJohn Forte 						(void) fprintf(stdout,
1355fcf3ce44SJohn Forte 						    gettext("Unknown"));
1356fcf3ce44SJohn Forte 						break;
1357fcf3ce44SJohn Forte 				}
1358fcf3ce44SJohn Forte 			}
1359fcf3ce44SJohn Forte 		} else {
1360fcf3ce44SJohn Forte 			(void) fprintf(stdout, "-");
1361fcf3ce44SJohn Forte 		}
1362fcf3ce44SJohn Forte 		(void) fprintf(stdout, "\n");
1363fcf3ce44SJohn Forte 	}
1364fcf3ce44SJohn Forte }
1365fcf3ce44SJohn Forte 
1366fcf3ce44SJohn Forte static int
1367fcf3ce44SJohn Forte setLoginParameters(IMA_OID oid, char *optarg)
1368fcf3ce44SJohn Forte {
1369fcf3ce44SJohn Forte 	char keyp[MAXOPTARGLEN];
1370fcf3ce44SJohn Forte 	char valp[MAXOPTARGLEN];
1371fcf3ce44SJohn Forte 	int key;
1372fcf3ce44SJohn Forte 	char *nameValueString, *indexp, *delim = NULL;
1373fcf3ce44SJohn Forte 
1374fcf3ce44SJohn Forte 	if ((nameValueString = strdup(optarg)) == NULL) {
1375fcf3ce44SJohn Forte 		if (errno == ENOMEM) {
1376fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
1377fcf3ce44SJohn Forte 			    cmdName, strerror(errno));
1378fcf3ce44SJohn Forte 		} else {
1379fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n", cmdName,
1380fcf3ce44SJohn Forte 			    gettext("unknown error"));
1381fcf3ce44SJohn Forte 		}
1382fcf3ce44SJohn Forte 		return (1);
1383fcf3ce44SJohn Forte 	}
1384fcf3ce44SJohn Forte 
1385fcf3ce44SJohn Forte 	indexp = nameValueString;
1386fcf3ce44SJohn Forte 
1387fcf3ce44SJohn Forte 	/*
1388fcf3ce44SJohn Forte 	 * Retrieve all login params from option argument
1389fcf3ce44SJohn Forte 	 * Syntax <key=value,...>
1390fcf3ce44SJohn Forte 	 */
1391fcf3ce44SJohn Forte 	while (indexp) {
1392fcf3ce44SJohn Forte 		if (delim = strchr(indexp, ',')) {
1393fcf3ce44SJohn Forte 			delim[0] = '\0';
1394fcf3ce44SJohn Forte 		}
1395fcf3ce44SJohn Forte 		(void) memset(keyp, 0, sizeof (keyp));
1396fcf3ce44SJohn Forte 		(void) memset(valp, 0, sizeof (valp));
1397fcf3ce44SJohn Forte 		if (sscanf(indexp, gettext("%[^=]=%s"), keyp, valp) != 2) {
1398fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s: %s\n", cmdName,
1399fcf3ce44SJohn Forte 			    gettext("Unknown param"), indexp);
1400fcf3ce44SJohn Forte 			return (1);
1401fcf3ce44SJohn Forte 		}
1402fcf3ce44SJohn Forte 		if ((key = getLoginParam(keyp)) == -1) {
1403fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s: %s\n", cmdName,
1404fcf3ce44SJohn Forte 			    gettext("Unknown key"), keyp);
1405fcf3ce44SJohn Forte 			return (1);
1406fcf3ce44SJohn Forte 		}
1407fcf3ce44SJohn Forte 		if (setLoginParameter(oid, key, valp) != 0) {
1408fcf3ce44SJohn Forte 			return (1);
1409fcf3ce44SJohn Forte 		}
1410fcf3ce44SJohn Forte 		if (delim) {
1411fcf3ce44SJohn Forte 			indexp = delim + 1;
1412fcf3ce44SJohn Forte 		} else {
1413fcf3ce44SJohn Forte 			indexp = NULL;
1414fcf3ce44SJohn Forte 		}
1415fcf3ce44SJohn Forte 	}
1416fcf3ce44SJohn Forte 
1417fcf3ce44SJohn Forte 	return (0);
1418fcf3ce44SJohn Forte }
1419fcf3ce44SJohn Forte 
1420fcf3ce44SJohn Forte /*
1421fcf3ce44SJohn Forte  * Print logical unit information for a specific target
1422fcf3ce44SJohn Forte  */
1423fcf3ce44SJohn Forte static void
1424fcf3ce44SJohn Forte printTargetLuns(IMA_OID_LIST * lunList)
1425fcf3ce44SJohn Forte {
1426fcf3ce44SJohn Forte 	int	j;
1427fcf3ce44SJohn Forte 	IMA_STATUS status;
1428fcf3ce44SJohn Forte 	SUN_IMA_LU_PROPERTIES	lunProps;
1429fcf3ce44SJohn Forte 
1430fcf3ce44SJohn Forte 	for (j = 0; j < lunList->oidCount; j++) {
1431fcf3ce44SJohn Forte 		status = SUN_IMA_GetLuProperties(lunList->oids[j],
1432fcf3ce44SJohn Forte 		    &lunProps);
1433fcf3ce44SJohn Forte 		if (!IMA_SUCCESS(status)) {
1434fcf3ce44SJohn Forte 			printLibError(status);
1435fcf3ce44SJohn Forte 			return;
1436fcf3ce44SJohn Forte 		}
1437fcf3ce44SJohn Forte 
1438fcf3ce44SJohn Forte 		if (lunProps.imaProps.osDeviceNameValid == IMA_TRUE) {
1439fcf3ce44SJohn Forte 			(void) fprintf(stdout, "\tLUN: %lld\n",
1440fcf3ce44SJohn Forte 			    lunProps.imaProps.targetLun);
1441fcf3ce44SJohn Forte 			(void) fprintf(stdout, "\t     Vendor:  %s\n",
1442fcf3ce44SJohn Forte 			    lunProps.vendorId);
1443fcf3ce44SJohn Forte 			(void) fprintf(stdout, "\t     Product: %s\n",
1444fcf3ce44SJohn Forte 			    lunProps.productId);
1445fcf3ce44SJohn Forte 			(void) fprintf(stdout,
1446fcf3ce44SJohn Forte 			    gettext("\t     OS Device Name: %ws\n"),
1447fcf3ce44SJohn Forte 			    lunProps.imaProps.osDeviceName);
1448fcf3ce44SJohn Forte 		}
1449fcf3ce44SJohn Forte 	}
1450fcf3ce44SJohn Forte }
1451fcf3ce44SJohn Forte 
1452fcf3ce44SJohn Forte /*
1453fcf3ce44SJohn Forte  * Retrieve CHAP secret from input
1454fcf3ce44SJohn Forte  */
1455fcf3ce44SJohn Forte static int
1456fcf3ce44SJohn Forte getSecret(char *secret, int *secretLen, int minSecretLen, int maxSecretLen)
1457fcf3ce44SJohn Forte {
1458fcf3ce44SJohn Forte 	char *chapSecret;
1459fcf3ce44SJohn Forte 
1460fcf3ce44SJohn Forte 	/* get password */
1461fcf3ce44SJohn Forte 	chapSecret = getpassphrase(gettext("Enter secret:"));
1462fcf3ce44SJohn Forte 
14635279807dSJack Meng 	if (chapSecret == NULL) {
14645279807dSJack Meng 		(void) fprintf(stderr, "%s: %s\n", cmdName,
14655279807dSJack Meng 		    gettext("Unable to get secret"));
14665279807dSJack Meng 		*secret = NULL;
14675279807dSJack Meng 		return (1);
14685279807dSJack Meng 	}
14695279807dSJack Meng 
1470fcf3ce44SJohn Forte 	if (strlen(chapSecret) > maxSecretLen) {
1471fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s %d\n", cmdName,
1472fcf3ce44SJohn Forte 		    gettext("secret too long, maximum length is"),
1473fcf3ce44SJohn Forte 		    maxSecretLen);
1474fcf3ce44SJohn Forte 		*secret = NULL;
1475fcf3ce44SJohn Forte 		return (1);
1476fcf3ce44SJohn Forte 	}
1477fcf3ce44SJohn Forte 
1478fcf3ce44SJohn Forte 	if (strlen(chapSecret) < minSecretLen) {
1479fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s %d\n", cmdName,
1480fcf3ce44SJohn Forte 		    gettext("secret too short, minimum length is"),
1481fcf3ce44SJohn Forte 		    minSecretLen);
1482fcf3ce44SJohn Forte 		*secret = NULL;
1483fcf3ce44SJohn Forte 		return (1);
1484fcf3ce44SJohn Forte 	}
1485fcf3ce44SJohn Forte 
1486fcf3ce44SJohn Forte 	(void) strcpy(secret, chapSecret);
1487fcf3ce44SJohn Forte 
1488fcf3ce44SJohn Forte 	chapSecret = getpassphrase(gettext("Re-enter secret:"));
14895279807dSJack Meng 
14905279807dSJack Meng 	if (chapSecret == NULL) {
14915279807dSJack Meng 		(void) fprintf(stderr, "%s: %s\n", cmdName,
14925279807dSJack Meng 		    gettext("Unable to get secret"));
14935279807dSJack Meng 		*secret = NULL;
14945279807dSJack Meng 		return (1);
14955279807dSJack Meng 	}
14965279807dSJack Meng 
1497fcf3ce44SJohn Forte 	if (strcmp(secret, chapSecret) != 0) {
1498fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n", cmdName,
1499fcf3ce44SJohn Forte 		    gettext("secrets do not match, secret not changed"));
1500fcf3ce44SJohn Forte 		*secret = NULL;
1501fcf3ce44SJohn Forte 		return (1);
1502fcf3ce44SJohn Forte 	}
1503fcf3ce44SJohn Forte 	*secretLen = strlen(chapSecret);
1504fcf3ce44SJohn Forte 	return (0);
1505fcf3ce44SJohn Forte }
1506fcf3ce44SJohn Forte 
1507fcf3ce44SJohn Forte /*
1508fcf3ce44SJohn Forte  * Lists the discovery attributes
1509fcf3ce44SJohn Forte  */
1510fcf3ce44SJohn Forte static int
1511fcf3ce44SJohn Forte listDiscovery(int *funcRet)
1512fcf3ce44SJohn Forte {
1513fcf3ce44SJohn Forte 	IMA_OID	initiatorOid;
1514fcf3ce44SJohn Forte 	IMA_DISCOVERY_PROPERTIES discProps;
1515fcf3ce44SJohn Forte 	int ret;
1516fcf3ce44SJohn Forte 	IMA_STATUS status;
1517fcf3ce44SJohn Forte 
1518fcf3ce44SJohn Forte 	assert(funcRet != NULL);
1519fcf3ce44SJohn Forte 
1520fcf3ce44SJohn Forte 
1521fcf3ce44SJohn Forte 	/* Find Sun initiator */
1522fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
1523fcf3ce44SJohn Forte 	if (ret > 0) {
1524fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
1525fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
1526fcf3ce44SJohn Forte 	}
1527fcf3ce44SJohn Forte 
1528fcf3ce44SJohn Forte 	if (ret != 0) {
1529fcf3ce44SJohn Forte 		return (ret);
1530fcf3ce44SJohn Forte 	}
1531fcf3ce44SJohn Forte 
1532fcf3ce44SJohn Forte 	/* Get discovery attributes from IMA */
1533fcf3ce44SJohn Forte 	status = IMA_GetDiscoveryProperties(initiatorOid, &discProps);
1534fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
1535fcf3ce44SJohn Forte 		printLibError(status);
1536fcf3ce44SJohn Forte 		*funcRet = 1;
1537fcf3ce44SJohn Forte 		return (ret);
1538fcf3ce44SJohn Forte 	}
1539fcf3ce44SJohn Forte 
1540fcf3ce44SJohn Forte 
1541fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s:\n", "Discovery");
1542fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\tStatic: %s\n",
1543fcf3ce44SJohn Forte 	    discProps.staticDiscoveryEnabled == IMA_TRUE ? \
1544fcf3ce44SJohn Forte 	    gettext("enabled") : gettext("disabled"));
1545fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\tSend Targets: %s\n",
1546fcf3ce44SJohn Forte 	    discProps.sendTargetsDiscoveryEnabled == IMA_TRUE ? \
1547fcf3ce44SJohn Forte 	    gettext("enabled") : gettext("disabled"));
1548fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\tiSNS: %s\n",
1549fcf3ce44SJohn Forte 	    discProps.iSnsDiscoveryEnabled == IMA_TRUE ? \
1550fcf3ce44SJohn Forte 	    gettext("enabled") : gettext("disabled"));
1551fcf3ce44SJohn Forte 
1552fcf3ce44SJohn Forte 	return (0);
1553fcf3ce44SJohn Forte }
1554fcf3ce44SJohn Forte 
1555fcf3ce44SJohn Forte /*
1556fcf3ce44SJohn Forte  * Print all initiator node attributes
1557fcf3ce44SJohn Forte  */
1558fcf3ce44SJohn Forte static int
1559fcf3ce44SJohn Forte listNode(int *funcRet)
1560fcf3ce44SJohn Forte {
1561fcf3ce44SJohn Forte 	IMA_OID	initiatorOid;
1562fcf3ce44SJohn Forte 	IMA_NODE_PROPERTIES nodeProps;
1563fcf3ce44SJohn Forte 	IMA_STATUS status;
1564fcf3ce44SJohn Forte 	int ret;
1565fcf3ce44SJohn Forte 	IMA_UINT maxEntries = MAX_AUTH_METHODS;
1566fcf3ce44SJohn Forte 	IMA_AUTHMETHOD	methodList[MAX_AUTH_METHODS];
1567fcf3ce44SJohn Forte 	SUN_IMA_RADIUS_CONFIG radiusConfig;
1568fcf3ce44SJohn Forte 	SUN_IMA_DIGEST_ALGORITHM_VALUE digestAlgorithms;
1569fcf3ce44SJohn Forte 	IMA_BOOL radiusAccess;
1570fcf3ce44SJohn Forte 
1571fcf3ce44SJohn Forte 	int i;
1572fcf3ce44SJohn Forte 
1573fcf3ce44SJohn Forte 	assert(funcRet != NULL);
1574fcf3ce44SJohn Forte 
1575fcf3ce44SJohn Forte 	ret = getNodeProps(&nodeProps);
1576fcf3ce44SJohn Forte 	if (ret != 0) {
1577fcf3ce44SJohn Forte 		return (ret);
1578fcf3ce44SJohn Forte 	}
1579fcf3ce44SJohn Forte 
1580fcf3ce44SJohn Forte 	if (nodeProps.nameValid == IMA_FALSE) {
1581fcf3ce44SJohn Forte 		return (INVALID_NODE_NAME);
1582fcf3ce44SJohn Forte 	}
1583fcf3ce44SJohn Forte 
1584fcf3ce44SJohn Forte 	/* Find Sun initiator */
1585fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
1586fcf3ce44SJohn Forte 	if (ret > 0) {
1587fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
1588fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
1589fcf3ce44SJohn Forte 	}
1590fcf3ce44SJohn Forte 
1591fcf3ce44SJohn Forte 	if (ret != 0) {
1592fcf3ce44SJohn Forte 		return (ret);
1593fcf3ce44SJohn Forte 	}
1594fcf3ce44SJohn Forte 	/* Begin output */
1595fcf3ce44SJohn Forte 	(void) fprintf(stdout, gettext("%s: %ws\n"),
1596fcf3ce44SJohn Forte 	    gettext("Initiator node name"),
1597fcf3ce44SJohn Forte 	    nodeProps.name);
1598fcf3ce44SJohn Forte 	(void) fprintf(stdout, gettext("Initiator node alias: "));
1599fcf3ce44SJohn Forte 	if (nodeProps.aliasValid == IMA_TRUE) {
1600fcf3ce44SJohn Forte 		(void) fprintf(stdout, gettext("%ws\n"), nodeProps.alias);
1601fcf3ce44SJohn Forte 	} else {
1602fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%s\n", "-");
1603fcf3ce44SJohn Forte 	}
1604fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\t%s:\n",
1605fcf3ce44SJohn Forte 	    gettext("Login Parameters (Default/Configured)"));
1606fcf3ce44SJohn Forte 
1607fcf3ce44SJohn Forte 	/* Get Digest configuration */
1608fcf3ce44SJohn Forte 	status = SUN_IMA_GetHeaderDigest(initiatorOid, &digestAlgorithms);
1609fcf3ce44SJohn Forte 	if (IMA_SUCCESS(status)) {
1610fcf3ce44SJohn Forte 		(void) fprintf(stdout, "\t\t%s: ", gettext("Header Digest"));
1611fcf3ce44SJohn Forte 		printDigestAlgorithm(&digestAlgorithms,
1612fcf3ce44SJohn Forte 		    PRINT_CONFIGURED_PARAMS);
1613fcf3ce44SJohn Forte 	} else {
1614fcf3ce44SJohn Forte 		printLibError(status);
1615fcf3ce44SJohn Forte 		*funcRet = 1;
1616fcf3ce44SJohn Forte 		return (ret);
1617fcf3ce44SJohn Forte 	}
1618fcf3ce44SJohn Forte 
1619fcf3ce44SJohn Forte 	status = SUN_IMA_GetDataDigest(initiatorOid, &digestAlgorithms);
1620fcf3ce44SJohn Forte 	if (IMA_SUCCESS(status)) {
1621fcf3ce44SJohn Forte 		(void) fprintf(stdout, "\t\t%s: ", gettext("Data Digest"));
1622fcf3ce44SJohn Forte 		printDigestAlgorithm(&digestAlgorithms,
1623fcf3ce44SJohn Forte 		    PRINT_CONFIGURED_PARAMS);
1624fcf3ce44SJohn Forte 	} else {
1625fcf3ce44SJohn Forte 		printLibError(status);
1626fcf3ce44SJohn Forte 		*funcRet = 1;
1627fcf3ce44SJohn Forte 		return (ret);
1628fcf3ce44SJohn Forte 	}
1629fcf3ce44SJohn Forte 
1630fcf3ce44SJohn Forte 	/* Get authentication type for this lhba */
1631fcf3ce44SJohn Forte 	status = IMA_GetInUseInitiatorAuthMethods(initiatorOid, &maxEntries,
1632fcf3ce44SJohn Forte 	    &methodList[0]);
1633fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\t%s: ", gettext("Authentication Type"));
1634fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
1635fcf3ce44SJohn Forte 		/* No authentication method set - default is NONE */
1636fcf3ce44SJohn Forte 		(void) fprintf(stdout, gettext("NONE"));
1637fcf3ce44SJohn Forte 	} else {
1638fcf3ce44SJohn Forte 		for (i = 0; i < maxEntries; i++) {
1639fcf3ce44SJohn Forte 			if (i > 0) {
1640fcf3ce44SJohn Forte 				(void) fprintf(stdout, "|");
1641fcf3ce44SJohn Forte 			}
1642fcf3ce44SJohn Forte 			switch (methodList[i]) {
1643fcf3ce44SJohn Forte 				case IMA_AUTHMETHOD_NONE:
1644fcf3ce44SJohn Forte 					(void) fprintf(stdout, gettext("NONE"));
1645fcf3ce44SJohn Forte 					break;
1646fcf3ce44SJohn Forte 				case IMA_AUTHMETHOD_CHAP:
1647fcf3ce44SJohn Forte 					(void) fprintf(stdout, gettext("CHAP"));
1648fcf3ce44SJohn Forte 					listCHAPName(initiatorOid);
1649fcf3ce44SJohn Forte 					break;
1650fcf3ce44SJohn Forte 				default:
1651fcf3ce44SJohn Forte 					(void) fprintf(stdout,
1652fcf3ce44SJohn Forte 					    gettext("unknown type"));
1653fcf3ce44SJohn Forte 					break;
1654fcf3ce44SJohn Forte 			}
1655fcf3ce44SJohn Forte 		}
1656fcf3ce44SJohn Forte 	}
1657fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\n");
1658fcf3ce44SJohn Forte 
1659fcf3ce44SJohn Forte 
1660fcf3ce44SJohn Forte 	/* Get RADIUS configuration */
1661fcf3ce44SJohn Forte 	status = SUN_IMA_GetInitiatorRadiusConfig(initiatorOid, &radiusConfig);
1662fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\t%s: ", gettext("RADIUS Server"));
1663fcf3ce44SJohn Forte 	if (IMA_SUCCESS(status)) {
1664fcf3ce44SJohn Forte 		if (strlen(radiusConfig.hostnameIpAddress) > 0) {
1665fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s:%d",
1666fcf3ce44SJohn Forte 			    radiusConfig.hostnameIpAddress,
1667fcf3ce44SJohn Forte 			    radiusConfig.port);
1668fcf3ce44SJohn Forte 		} else {
1669fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s", gettext("NONE"));
1670fcf3ce44SJohn Forte 		}
1671fcf3ce44SJohn Forte 	} else {
1672fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%s", gettext("NONE"));
1673fcf3ce44SJohn Forte 	}
1674fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\n");
1675fcf3ce44SJohn Forte 
1676fcf3ce44SJohn Forte 	status = SUN_IMA_GetInitiatorRadiusAccess(initiatorOid,
1677fcf3ce44SJohn Forte 	    &radiusAccess);
1678fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\t%s: ", gettext("RADIUS Access"));
1679fcf3ce44SJohn Forte 	if (IMA_SUCCESS(status)) {
1680fcf3ce44SJohn Forte 		if (radiusAccess == IMA_TRUE) {
1681fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s", gettext("enabled"));
1682fcf3ce44SJohn Forte 		} else {
1683fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s", gettext("disabled"));
1684fcf3ce44SJohn Forte 		}
1685fcf3ce44SJohn Forte 	} else if (status == IMA_ERROR_OBJECT_NOT_FOUND) {
1686fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%s", gettext("disabled"));
1687fcf3ce44SJohn Forte 	} else {
1688fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%s", gettext("unknown"));
1689fcf3ce44SJohn Forte 	}
1690fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\n");
1691fcf3ce44SJohn Forte 
1692fcf3ce44SJohn Forte 	/* print configured session information. */
1693fcf3ce44SJohn Forte 	ret = printConfiguredSessions(initiatorOid);
1694fcf3ce44SJohn Forte 
1695fcf3ce44SJohn Forte 	return (ret);
1696fcf3ce44SJohn Forte }
1697fcf3ce44SJohn Forte 
1698fcf3ce44SJohn Forte /*
1699fcf3ce44SJohn Forte  * Print discovery addresses
1700fcf3ce44SJohn Forte  */
1701fcf3ce44SJohn Forte static int
1702fcf3ce44SJohn Forte listDiscoveryAddress(int objectLen, char *objects[], cmdOptions_t *options,
1703fcf3ce44SJohn Forte     int *funcRet)
1704fcf3ce44SJohn Forte {
1705fcf3ce44SJohn Forte 	IMA_OID	initiatorOid;
1706fcf3ce44SJohn Forte 	SUN_IMA_DISC_ADDR_PROP_LIST *discoveryAddressPropertiesList;
1707fcf3ce44SJohn Forte 	IMA_DISCOVERY_ADDRESS_PROPERTIES discAddrProps;
1708fcf3ce44SJohn Forte 	IMA_TARGET_ADDRESS address;
1709fcf3ce44SJohn Forte 	SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES *pList;
1710fcf3ce44SJohn Forte 	IMA_STATUS status;
1711fcf3ce44SJohn Forte 	wchar_t wcInputObject[MAX_ADDRESS_LEN + 1];
1712fcf3ce44SJohn Forte 	int ret;
1713fcf3ce44SJohn Forte 	boolean_t object = B_FALSE;
1714fcf3ce44SJohn Forte 	int outerLoop;
1715fcf3ce44SJohn Forte 	boolean_t found;
1716fcf3ce44SJohn Forte 	boolean_t verbose = B_FALSE;
1717fcf3ce44SJohn Forte 	int i, j;
1718fcf3ce44SJohn Forte 	cmdOptions_t *optionList = options;
1719fcf3ce44SJohn Forte 	char sAddr[SUN_IMA_IP_ADDRESS_PORT_LEN];
1720fcf3ce44SJohn Forte 
1721fcf3ce44SJohn Forte 	assert(funcRet != NULL);
1722fcf3ce44SJohn Forte 
1723fcf3ce44SJohn Forte 	/* Find Sun initiator */
1724fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
1725fcf3ce44SJohn Forte 	if (ret > 0) {
1726fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
1727fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
1728fcf3ce44SJohn Forte 	}
1729fcf3ce44SJohn Forte 
1730fcf3ce44SJohn Forte 	if (ret != 0) {
1731fcf3ce44SJohn Forte 		return (ret);
1732fcf3ce44SJohn Forte 	}
1733fcf3ce44SJohn Forte 
1734fcf3ce44SJohn Forte 	for (; optionList->optval; optionList++) {
1735fcf3ce44SJohn Forte 		switch (optionList->optval) {
1736fcf3ce44SJohn Forte 			case 'v':
1737fcf3ce44SJohn Forte 				verbose = B_TRUE;
1738fcf3ce44SJohn Forte 				break;
1739fcf3ce44SJohn Forte 			default:
1740fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %c: %s\n",
1741fcf3ce44SJohn Forte 				    cmdName, optionList->optval,
1742fcf3ce44SJohn Forte 				    gettext("unknown option"));
1743fcf3ce44SJohn Forte 				return (1);
1744fcf3ce44SJohn Forte 		}
1745fcf3ce44SJohn Forte 	}
1746fcf3ce44SJohn Forte 
1747fcf3ce44SJohn Forte 	/*
1748fcf3ce44SJohn Forte 	 * If there are multiple objects, execute outer 'for' loop that
1749fcf3ce44SJohn Forte 	 * many times for each target detail, otherwise, execute it only
1750fcf3ce44SJohn Forte 	 * once with summaries only
1751fcf3ce44SJohn Forte 	 */
1752fcf3ce44SJohn Forte 	if (objectLen > 0) {
1753fcf3ce44SJohn Forte 		object = B_TRUE;
1754fcf3ce44SJohn Forte 		outerLoop = objectLen;
1755fcf3ce44SJohn Forte 	} else {
1756fcf3ce44SJohn Forte 		object = B_FALSE;
1757fcf3ce44SJohn Forte 		outerLoop = 1;
1758fcf3ce44SJohn Forte 	}
1759fcf3ce44SJohn Forte 
1760fcf3ce44SJohn Forte 	status = SUN_IMA_GetDiscoveryAddressPropertiesList(
1761fcf3ce44SJohn Forte 	    &discoveryAddressPropertiesList);
1762fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
1763fcf3ce44SJohn Forte 		printLibError(status);
1764fcf3ce44SJohn Forte 		*funcRet = 1;
1765fcf3ce44SJohn Forte 		return (ret);
1766fcf3ce44SJohn Forte 	}
1767fcf3ce44SJohn Forte 
1768fcf3ce44SJohn Forte 	for (i = 0; i < outerLoop; i++) {
1769fcf3ce44SJohn Forte 		if (object) {
1770fcf3ce44SJohn Forte 			/* initialize */
1771fcf3ce44SJohn Forte 			(void) memset(&wcInputObject[0], 0,
1772fcf3ce44SJohn Forte 			    sizeof (wcInputObject));
1773fcf3ce44SJohn Forte 			(void) memset(&address, 0, sizeof (address));
1774fcf3ce44SJohn Forte 			if (mbstowcs(wcInputObject, objects[i],
1775fcf3ce44SJohn Forte 			    (MAX_ADDRESS_LEN + 1)) == (size_t)-1) {
1776fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n",
1777fcf3ce44SJohn Forte 				    cmdName,
1778fcf3ce44SJohn Forte 				    gettext("conversion error"));
1779fcf3ce44SJohn Forte 				ret = 1;
1780fcf3ce44SJohn Forte 				continue;
1781fcf3ce44SJohn Forte 			}
1782fcf3ce44SJohn Forte 
1783fcf3ce44SJohn Forte 			/*
1784fcf3ce44SJohn Forte 			 * if one or more objects were input,
1785fcf3ce44SJohn Forte 			 * get the values
1786fcf3ce44SJohn Forte 			 */
1787fcf3ce44SJohn Forte 			if (getTargetAddress(DISCOVERY_ADDRESS,
1788fcf3ce44SJohn Forte 			    objects[i], &address) != 0) {
1789fcf3ce44SJohn Forte 				ret = 1;
1790fcf3ce44SJohn Forte 				continue;
1791fcf3ce44SJohn Forte 			}
1792fcf3ce44SJohn Forte 		}
1793fcf3ce44SJohn Forte 		for (found = B_FALSE, j = 0;
1794fcf3ce44SJohn Forte 		    j < discoveryAddressPropertiesList->discAddrCount;
1795fcf3ce44SJohn Forte 		    j++) {
1796fcf3ce44SJohn Forte 			discAddrProps =
1797fcf3ce44SJohn Forte 			    discoveryAddressPropertiesList->props[j];
1798fcf3ce44SJohn Forte 
1799fcf3ce44SJohn Forte 			/*
1800fcf3ce44SJohn Forte 			 * Compare the discovery address with the input if
1801fcf3ce44SJohn Forte 			 * one was input
1802fcf3ce44SJohn Forte 			 */
1803fcf3ce44SJohn Forte 			if (object &&
1804fcf3ce44SJohn Forte 			    ipAddressesEqual(discAddrProps.discoveryAddress,
1805fcf3ce44SJohn Forte 			    address) && (discAddrProps.discoveryAddress.
1806fcf3ce44SJohn Forte 			    portNumber == address.portNumber)) {
1807fcf3ce44SJohn Forte 				found = B_TRUE;
1808fcf3ce44SJohn Forte 			}
1809fcf3ce44SJohn Forte 
1810fcf3ce44SJohn Forte 			if (!object || found) {
1811fcf3ce44SJohn Forte 				/* Print summary - always */
1812fcf3ce44SJohn Forte 				if (discAddrProps.discoveryAddress.
1813fcf3ce44SJohn Forte 				    hostnameIpAddress.id.ipAddress.
1814fcf3ce44SJohn Forte 				    ipv4Address) {
1815fcf3ce44SJohn Forte 					(void) inet_ntop(AF_INET, discAddrProps.
1816fcf3ce44SJohn Forte 					    discoveryAddress.hostnameIpAddress.
1817fcf3ce44SJohn Forte 					    id.ipAddress.ipAddress, sAddr,
1818fcf3ce44SJohn Forte 					    sizeof (sAddr));
1819fcf3ce44SJohn Forte 					(void) fprintf(stdout,
1820fcf3ce44SJohn Forte 					    "Discovery Address: %s:%u\n",
1821fcf3ce44SJohn Forte 					    sAddr, discAddrProps.
1822fcf3ce44SJohn Forte 					    discoveryAddress.portNumber);
1823fcf3ce44SJohn Forte 				} else {
1824fcf3ce44SJohn Forte 					(void) inet_ntop(AF_INET6,
1825fcf3ce44SJohn Forte 					    discAddrProps.
1826fcf3ce44SJohn Forte 					    discoveryAddress.hostnameIpAddress.
1827fcf3ce44SJohn Forte 					    id.ipAddress.ipAddress, sAddr,
1828fcf3ce44SJohn Forte 					    sizeof (sAddr));
1829fcf3ce44SJohn Forte 					(void) fprintf(stdout,
1830fcf3ce44SJohn Forte 					    "DiscoveryAddress: [%s]:%u\n",
1831fcf3ce44SJohn Forte 					    sAddr, discAddrProps.
1832fcf3ce44SJohn Forte 					    discoveryAddress.portNumber);
1833fcf3ce44SJohn Forte 				}
1834fcf3ce44SJohn Forte 			}
1835fcf3ce44SJohn Forte 
1836fcf3ce44SJohn Forte 			if ((!object || found) && verbose) {
1837fcf3ce44SJohn Forte 				IMA_NODE_PROPERTIES nodeProps;
1838fcf3ce44SJohn Forte 
1839fcf3ce44SJohn Forte 				if (getNodeProps(&nodeProps) != 0) {
1840fcf3ce44SJohn Forte 					break;
1841fcf3ce44SJohn Forte 				}
1842fcf3ce44SJohn Forte 
1843fcf3ce44SJohn Forte 				/*
1844fcf3ce44SJohn Forte 				 * Issue sendTargets only when an addr is
1845fcf3ce44SJohn Forte 				 * specified.
1846fcf3ce44SJohn Forte 				 */
1847fcf3ce44SJohn Forte 				status = SUN_IMA_SendTargets(nodeProps.name,
1848fcf3ce44SJohn Forte 				    discAddrProps.discoveryAddress, &pList);
1849fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
1850fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s\n",
1851fcf3ce44SJohn Forte 					    gettext("\tUnable to get "\
1852fcf3ce44SJohn Forte 					    "targets."));
1853fcf3ce44SJohn Forte 					*funcRet = 1;
1854fcf3ce44SJohn Forte 					continue;
1855fcf3ce44SJohn Forte 				}
1856fcf3ce44SJohn Forte 				printSendTargets(pList);
1857fcf3ce44SJohn Forte 			}
1858fcf3ce44SJohn Forte 
1859fcf3ce44SJohn Forte 			if (found) {
1860fcf3ce44SJohn Forte 				/* we found the discovery address - break */
1861fcf3ce44SJohn Forte 				break;
1862fcf3ce44SJohn Forte 			}
1863fcf3ce44SJohn Forte 		}
1864fcf3ce44SJohn Forte 		/*
1865fcf3ce44SJohn Forte 		 * There was an object entered but we didn't
1866fcf3ce44SJohn Forte 		 * find it.
1867fcf3ce44SJohn Forte 		 */
1868fcf3ce44SJohn Forte 		if (object && !found) {
1869fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s: %s\n",
1870fcf3ce44SJohn Forte 			    objects[i], gettext("not found"));
1871fcf3ce44SJohn Forte 		}
1872fcf3ce44SJohn Forte 	}
1873fcf3ce44SJohn Forte 	return (ret);
1874fcf3ce44SJohn Forte }
1875fcf3ce44SJohn Forte 
1876fcf3ce44SJohn Forte /*
1877fcf3ce44SJohn Forte  * Print ISNS Server addresses
1878fcf3ce44SJohn Forte  */
1879fcf3ce44SJohn Forte static int
1880fcf3ce44SJohn Forte listISNSServerAddress(int objectLen, char *objects[], cmdOptions_t *options,
1881fcf3ce44SJohn Forte     int *funcRet)
1882fcf3ce44SJohn Forte {
1883fcf3ce44SJohn Forte 	IMA_OID	initiatorOid;
1884fcf3ce44SJohn Forte 	SUN_IMA_DISC_ADDR_PROP_LIST	    *discoveryAddressPropertiesList;
1885fcf3ce44SJohn Forte 	IMA_DISCOVERY_ADDRESS_PROPERTIES discAddrProps;
1886fcf3ce44SJohn Forte 	IMA_TARGET_ADDRESS address;
1887fcf3ce44SJohn Forte 	SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES *pList;
1888fcf3ce44SJohn Forte 	IMA_STATUS status;
1889fcf3ce44SJohn Forte 	wchar_t wcInputObject[MAX_ADDRESS_LEN + 1];
1890fcf3ce44SJohn Forte 	int ret;
1891fcf3ce44SJohn Forte 	boolean_t object = B_FALSE;
1892fcf3ce44SJohn Forte 	int outerLoop;
1893fcf3ce44SJohn Forte 	boolean_t found;
1894fcf3ce44SJohn Forte 	boolean_t showTarget = B_FALSE;
1895fcf3ce44SJohn Forte 	int i, j;
1896fcf3ce44SJohn Forte 	cmdOptions_t *optionList = options;
1897fcf3ce44SJohn Forte 	char sAddr[SUN_IMA_IP_ADDRESS_PORT_LEN];
1898fcf3ce44SJohn Forte 
1899fcf3ce44SJohn Forte 	assert(funcRet != NULL);
1900fcf3ce44SJohn Forte 
1901fcf3ce44SJohn Forte 	/* Find Sun initiator */
1902fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
1903fcf3ce44SJohn Forte 	if (ret > 0) {
1904fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
1905fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
1906fcf3ce44SJohn Forte 	}
1907fcf3ce44SJohn Forte 
1908fcf3ce44SJohn Forte 	if (ret != 0) {
1909fcf3ce44SJohn Forte 		return (ret);
1910fcf3ce44SJohn Forte 	}
1911fcf3ce44SJohn Forte 
1912fcf3ce44SJohn Forte 	for (; optionList->optval; optionList++) {
1913fcf3ce44SJohn Forte 		switch (optionList->optval) {
1914fcf3ce44SJohn Forte 			case 'v':
1915fcf3ce44SJohn Forte 				showTarget = B_TRUE;
1916fcf3ce44SJohn Forte 				break;
1917fcf3ce44SJohn Forte 			default:
1918fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %c: %s\n",
1919fcf3ce44SJohn Forte 				    cmdName, optionList->optval,
1920fcf3ce44SJohn Forte 				    gettext("unknown option"));
1921fcf3ce44SJohn Forte 				return (1);
1922fcf3ce44SJohn Forte 		}
1923fcf3ce44SJohn Forte 	}
1924fcf3ce44SJohn Forte 
1925fcf3ce44SJohn Forte 	/*
1926fcf3ce44SJohn Forte 	 * If there are multiple objects, execute outer 'for' loop that
1927fcf3ce44SJohn Forte 	 * many times for each target detail, otherwise, execute it only
1928fcf3ce44SJohn Forte 	 * once with summaries only
1929fcf3ce44SJohn Forte 	 */
1930fcf3ce44SJohn Forte 	if (objectLen > 0) {
1931fcf3ce44SJohn Forte 		object = B_TRUE;
1932fcf3ce44SJohn Forte 		outerLoop = objectLen;
1933fcf3ce44SJohn Forte 	} else {
1934fcf3ce44SJohn Forte 		object = B_FALSE;
1935fcf3ce44SJohn Forte 		outerLoop = 1;
1936fcf3ce44SJohn Forte 	}
1937fcf3ce44SJohn Forte 
1938fcf3ce44SJohn Forte 	status = SUN_IMA_GetISNSServerAddressPropertiesList(
1939fcf3ce44SJohn Forte 	    &discoveryAddressPropertiesList);
1940fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
1941fcf3ce44SJohn Forte 		printLibError(status);
1942fcf3ce44SJohn Forte 		*funcRet = 1;
1943fcf3ce44SJohn Forte 		return (ret);
1944fcf3ce44SJohn Forte 	}
1945fcf3ce44SJohn Forte 
1946fcf3ce44SJohn Forte 	for (i = 0; i < outerLoop; i++) {
1947fcf3ce44SJohn Forte 		if (object) {
1948fcf3ce44SJohn Forte 			/* initialize */
1949fcf3ce44SJohn Forte 			(void) memset(&wcInputObject[0], 0,
1950fcf3ce44SJohn Forte 			    sizeof (wcInputObject));
1951fcf3ce44SJohn Forte 			(void) memset(&address, 0, sizeof (address));
1952fcf3ce44SJohn Forte 			if (mbstowcs(wcInputObject, objects[i],
1953fcf3ce44SJohn Forte 			    (MAX_ADDRESS_LEN + 1)) == (size_t)-1) {
1954fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n",
1955fcf3ce44SJohn Forte 				    cmdName,
1956fcf3ce44SJohn Forte 				    gettext("conversion error"));
1957fcf3ce44SJohn Forte 				ret = 1;
1958fcf3ce44SJohn Forte 				continue;
1959fcf3ce44SJohn Forte 			}
1960fcf3ce44SJohn Forte 
1961fcf3ce44SJohn Forte 			/*
1962fcf3ce44SJohn Forte 			 * if one or more objects were input,
1963fcf3ce44SJohn Forte 			 * get the values
1964fcf3ce44SJohn Forte 			 */
1965fcf3ce44SJohn Forte 			if (getTargetAddress(ISNS_SERVER_ADDRESS,
1966fcf3ce44SJohn Forte 			    objects[i], &address) != 0) {
1967fcf3ce44SJohn Forte 				ret = 1;
1968fcf3ce44SJohn Forte 				continue;
1969fcf3ce44SJohn Forte 			}
1970fcf3ce44SJohn Forte 		}
1971fcf3ce44SJohn Forte 		for (found = B_FALSE, j = 0;
1972fcf3ce44SJohn Forte 		    j < discoveryAddressPropertiesList->discAddrCount;
1973fcf3ce44SJohn Forte 		    j++) {
1974fcf3ce44SJohn Forte 			discAddrProps =
1975fcf3ce44SJohn Forte 			    discoveryAddressPropertiesList->props[j];
1976fcf3ce44SJohn Forte 
1977fcf3ce44SJohn Forte 			/*
1978fcf3ce44SJohn Forte 			 * Compare the discovery address with the input if
1979fcf3ce44SJohn Forte 			 * one was input
1980fcf3ce44SJohn Forte 			 */
1981fcf3ce44SJohn Forte 			if (object &&
1982fcf3ce44SJohn Forte 			    ipAddressesEqual(discAddrProps.discoveryAddress,
1983fcf3ce44SJohn Forte 			    address) &&
1984fcf3ce44SJohn Forte 			    (discAddrProps.discoveryAddress.portNumber ==
1985fcf3ce44SJohn Forte 			    address.portNumber)) {
1986fcf3ce44SJohn Forte 				found = B_TRUE;
1987fcf3ce44SJohn Forte 			}
1988fcf3ce44SJohn Forte 
1989fcf3ce44SJohn Forte 			if (!object || found) {
1990fcf3ce44SJohn Forte 				/* Print summary - always */
1991fcf3ce44SJohn Forte 				if (discAddrProps.discoveryAddress.
1992fcf3ce44SJohn Forte 				    hostnameIpAddress.id.ipAddress.
1993fcf3ce44SJohn Forte 				    ipv4Address) {
1994fcf3ce44SJohn Forte 					(void) inet_ntop(AF_INET, discAddrProps.
1995fcf3ce44SJohn Forte 					    discoveryAddress.hostnameIpAddress.
1996fcf3ce44SJohn Forte 					    id.ipAddress.ipAddress, sAddr,
1997fcf3ce44SJohn Forte 					    sizeof (sAddr));
1998fcf3ce44SJohn Forte 				} else {
1999fcf3ce44SJohn Forte 					(void) inet_ntop(AF_INET6,
2000fcf3ce44SJohn Forte 					    discAddrProps.
2001fcf3ce44SJohn Forte 					    discoveryAddress.hostnameIpAddress.
2002fcf3ce44SJohn Forte 					    id.ipAddress.ipAddress, sAddr,
2003fcf3ce44SJohn Forte 					    sizeof (sAddr));
2004fcf3ce44SJohn Forte 				}
2005fcf3ce44SJohn Forte 				(void) fprintf(stdout,
2006fcf3ce44SJohn Forte 				    "iSNS Server IP Address: %s:%u\n",
2007fcf3ce44SJohn Forte 				    sAddr,
2008fcf3ce44SJohn Forte 				    discAddrProps.discoveryAddress.portNumber);
2009fcf3ce44SJohn Forte 			}
2010fcf3ce44SJohn Forte 
2011fcf3ce44SJohn Forte 			if ((!object || found) && showTarget) {
2012fcf3ce44SJohn Forte 				IMA_NODE_PROPERTIES nodeProps;
2013fcf3ce44SJohn Forte 
2014fcf3ce44SJohn Forte 				if (getNodeProps(&nodeProps) != 0) {
2015fcf3ce44SJohn Forte 					break;
2016fcf3ce44SJohn Forte 				}
2017fcf3ce44SJohn Forte 
2018fcf3ce44SJohn Forte 				/*
2019fcf3ce44SJohn Forte 				 * Issue sendTargets only when an addr is
2020fcf3ce44SJohn Forte 				 * specified.
2021fcf3ce44SJohn Forte 				 */
2022fcf3ce44SJohn Forte 				status = SUN_IMA_RetrieveISNSServerTargets(
2023fcf3ce44SJohn Forte 				    discAddrProps.discoveryAddress,
2024fcf3ce44SJohn Forte 				    &pList);
2025fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
2026fcf3ce44SJohn Forte 					/*
2027fcf3ce44SJohn Forte 					 * Check if the discovery mode is
2028fcf3ce44SJohn Forte 					 * disabled.
2029fcf3ce44SJohn Forte 					 */
2030fcf3ce44SJohn Forte 					if (status ==
2031fcf3ce44SJohn Forte 					    IMA_ERROR_OBJECT_NOT_FOUND) {
2032fcf3ce44SJohn Forte 						(void) fprintf(stderr, "%s\n",
2033fcf3ce44SJohn Forte 						    gettext("\tiSNS "\
2034fcf3ce44SJohn Forte 						    "discovery "\
2035fcf3ce44SJohn Forte 						    "mode "\
2036fcf3ce44SJohn Forte 						    "disabled. "\
2037fcf3ce44SJohn Forte 						    "No targets "\
2038fcf3ce44SJohn Forte 						    "to report."));
2039fcf3ce44SJohn Forte 
2040fcf3ce44SJohn Forte 					} else {
2041fcf3ce44SJohn Forte 						(void) fprintf(stderr, "%s\n",
2042fcf3ce44SJohn Forte 						    gettext("\tUnable "\
2043fcf3ce44SJohn Forte 						    "to get "\
2044fcf3ce44SJohn Forte 						    "targets."));
2045fcf3ce44SJohn Forte 					}
2046fcf3ce44SJohn Forte 					continue;
2047fcf3ce44SJohn Forte 				}
2048fcf3ce44SJohn Forte 				printSendTargets(pList);
2049fcf3ce44SJohn Forte 			}
2050fcf3ce44SJohn Forte 
2051fcf3ce44SJohn Forte 			if (found) {
2052fcf3ce44SJohn Forte 				/* we found the discovery address - break */
2053fcf3ce44SJohn Forte 				break;
2054fcf3ce44SJohn Forte 			}
2055fcf3ce44SJohn Forte 		}
2056fcf3ce44SJohn Forte 		/*
2057fcf3ce44SJohn Forte 		 * There was an object entered but we didn't
2058fcf3ce44SJohn Forte 		 * find it.
2059fcf3ce44SJohn Forte 		 */
2060fcf3ce44SJohn Forte 		if (object && !found) {
2061fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s: %s\n",
2062fcf3ce44SJohn Forte 			    objects[i], gettext("not found"));
2063fcf3ce44SJohn Forte 		}
2064fcf3ce44SJohn Forte 	}
2065fcf3ce44SJohn Forte 	return (ret);
2066fcf3ce44SJohn Forte }
2067fcf3ce44SJohn Forte 
2068fcf3ce44SJohn Forte /*
2069fcf3ce44SJohn Forte  * Print static configuration targets
2070fcf3ce44SJohn Forte  */
2071fcf3ce44SJohn Forte static int
2072fcf3ce44SJohn Forte listStaticConfig(int operandLen, char *operand[], int *funcRet)
2073fcf3ce44SJohn Forte {
2074fcf3ce44SJohn Forte 	IMA_STATUS status;
2075fcf3ce44SJohn Forte 	IMA_OID	initiatorOid;
2076fcf3ce44SJohn Forte 	IMA_OID_LIST *staticTargetList;
2077fcf3ce44SJohn Forte 	SUN_IMA_STATIC_TARGET_PROPERTIES staticTargetProps;
2078fcf3ce44SJohn Forte 	wchar_t staticTargetName[MAX_ISCSI_NAME_LEN + 1];
2079fcf3ce44SJohn Forte 	wchar_t staticTargetAddress[SUN_IMA_IP_ADDRESS_PORT_LEN];
2080fcf3ce44SJohn Forte 	wchar_t wcCol;
2081fcf3ce44SJohn Forte 	char sAddr[SUN_IMA_IP_ADDRESS_PORT_LEN];
2082fcf3ce44SJohn Forte 	int ret;
2083fcf3ce44SJohn Forte 	boolean_t object = B_FALSE;
2084fcf3ce44SJohn Forte 	int outerLoop;
2085fcf3ce44SJohn Forte 	boolean_t found; /* B_TRUE if a target name is found */
2086fcf3ce44SJohn Forte 	boolean_t matched; /* B_TRUE if a specific target is found */
2087fcf3ce44SJohn Forte 	boolean_t targetAddressSpecified = B_FALSE;
2088fcf3ce44SJohn Forte 	boolean_t tpgtSpecified = B_FALSE;
2089fcf3ce44SJohn Forte 	boolean_t isIpv6;
2090fcf3ce44SJohn Forte 	int i, j;
2091fcf3ce44SJohn Forte 	IMA_UINT16 port = 0;
2092fcf3ce44SJohn Forte 	IMA_UINT16 tpgt = 0;
2093fcf3ce44SJohn Forte 	char tmpStr[SUN_IMA_IP_ADDRESS_PORT_LEN];
2094fcf3ce44SJohn Forte 	wchar_t tmpTargetAddress[SUN_IMA_IP_ADDRESS_PORT_LEN];
2095fcf3ce44SJohn Forte 
2096fcf3ce44SJohn Forte 	assert(funcRet != NULL);
2097fcf3ce44SJohn Forte 
2098fcf3ce44SJohn Forte 	/* Find Sun initiator */
2099fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
2100fcf3ce44SJohn Forte 	if (ret > 0) {
2101fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
2102fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
2103fcf3ce44SJohn Forte 	}
2104fcf3ce44SJohn Forte 
2105fcf3ce44SJohn Forte 	if (ret != 0) {
2106fcf3ce44SJohn Forte 		return (ret);
2107fcf3ce44SJohn Forte 	}
2108fcf3ce44SJohn Forte 
2109fcf3ce44SJohn Forte 	/*
2110fcf3ce44SJohn Forte 	 * If there are multiple objects, execute outer 'for' loop that
2111fcf3ce44SJohn Forte 	 * many times for each static config detail, otherwise, execute it only
2112fcf3ce44SJohn Forte 	 * once with summaries only
2113fcf3ce44SJohn Forte 	 */
2114fcf3ce44SJohn Forte 	if (operandLen > 0) {
2115fcf3ce44SJohn Forte 		object = B_TRUE;
2116fcf3ce44SJohn Forte 		outerLoop = operandLen;
2117fcf3ce44SJohn Forte 	} else {
2118fcf3ce44SJohn Forte 		object = B_FALSE;
2119fcf3ce44SJohn Forte 		outerLoop = 1;
2120fcf3ce44SJohn Forte 	}
2121fcf3ce44SJohn Forte 
2122fcf3ce44SJohn Forte 	/* convert ':' to wide char for wchar string search */
2123fcf3ce44SJohn Forte 	if (mbtowc(&wcCol, ":", sizeof (wcCol)) == -1) {
2124fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
2125fcf3ce44SJohn Forte 		    cmdName, gettext("conversion error"));
2126fcf3ce44SJohn Forte 		return (1);
2127fcf3ce44SJohn Forte 	}
2128fcf3ce44SJohn Forte 
2129fcf3ce44SJohn Forte 	status = IMA_GetStaticDiscoveryTargetOidList(initiatorOid,
2130fcf3ce44SJohn Forte 	    &staticTargetList);
2131fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
2132fcf3ce44SJohn Forte 		printLibError(status);
2133fcf3ce44SJohn Forte 		*funcRet = 1;
2134fcf3ce44SJohn Forte 		return (ret);
2135fcf3ce44SJohn Forte 	}
2136fcf3ce44SJohn Forte 
2137fcf3ce44SJohn Forte 	for (i = 0; i < outerLoop; i++) {
2138fcf3ce44SJohn Forte 		if (object) {
2139fcf3ce44SJohn Forte 			if (parseTarget(operand[i],
2140fcf3ce44SJohn Forte 			    &staticTargetName[0],
2141fcf3ce44SJohn Forte 			    MAX_ISCSI_NAME_LEN + 1,
2142fcf3ce44SJohn Forte 			    &targetAddressSpecified,
2143fcf3ce44SJohn Forte 			    &staticTargetAddress[0],
2144fcf3ce44SJohn Forte 			    SUN_IMA_IP_ADDRESS_PORT_LEN,
2145fcf3ce44SJohn Forte 			    &port,
2146fcf3ce44SJohn Forte 			    &tpgtSpecified,
2147fcf3ce44SJohn Forte 			    &tpgt,
2148fcf3ce44SJohn Forte 			    &isIpv6) != PARSE_TARGET_OK) {
2149fcf3ce44SJohn Forte 				ret = 1;
2150fcf3ce44SJohn Forte 				continue;
2151fcf3ce44SJohn Forte 			}
2152fcf3ce44SJohn Forte 		}
2153fcf3ce44SJohn Forte 
2154fcf3ce44SJohn Forte 		for (found = B_FALSE, j = 0; j < staticTargetList->oidCount;
2155fcf3ce44SJohn Forte 		    j++) {
2156fcf3ce44SJohn Forte 			boolean_t isIpv6 = B_FALSE;
2157fcf3ce44SJohn Forte 			IMA_UINT16 stpgt;
2158fcf3ce44SJohn Forte 			IMA_BOOL defaultTpgt;
2159fcf3ce44SJohn Forte 
2160fcf3ce44SJohn Forte 			matched = B_FALSE;
2161fcf3ce44SJohn Forte 			(void) memset(&staticTargetProps, 0,
2162fcf3ce44SJohn Forte 			    sizeof (staticTargetProps));
2163fcf3ce44SJohn Forte 
2164fcf3ce44SJohn Forte 			status = SUN_IMA_GetStaticTargetProperties(
2165fcf3ce44SJohn Forte 			    staticTargetList->oids[j], &staticTargetProps);
2166fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
2167fcf3ce44SJohn Forte 				printLibError(status);
2168fcf3ce44SJohn Forte 				(void) IMA_FreeMemory(staticTargetList);
2169fcf3ce44SJohn Forte 				*funcRet = 1;
2170fcf3ce44SJohn Forte 				return (ret);
2171fcf3ce44SJohn Forte 			}
2172fcf3ce44SJohn Forte 
2173fcf3ce44SJohn Forte 			stpgt = staticTargetProps.staticTarget.targetAddress.
2174fcf3ce44SJohn Forte 			    tpgt;
2175fcf3ce44SJohn Forte 
2176fcf3ce44SJohn Forte 			defaultTpgt = staticTargetProps.staticTarget.
2177fcf3ce44SJohn Forte 			    targetAddress.defaultTpgt;
2178fcf3ce44SJohn Forte 
2179fcf3ce44SJohn Forte 			isIpv6 = !staticTargetProps.staticTarget.targetAddress.
2180fcf3ce44SJohn Forte 			    imaStruct.hostnameIpAddress.id.ipAddress.
2181fcf3ce44SJohn Forte 			    ipv4Address;
2182fcf3ce44SJohn Forte 
2183fcf3ce44SJohn Forte 			/*
2184fcf3ce44SJohn Forte 			 * Compare the static target name with the input if
2185fcf3ce44SJohn Forte 			 * one was input
2186fcf3ce44SJohn Forte 			 */
2187fcf3ce44SJohn Forte 
2188fcf3ce44SJohn Forte 			if (object &&
2189fcf3ce44SJohn Forte 			    (targetNamesEqual(
2190fcf3ce44SJohn Forte 			    staticTargetProps.staticTarget.targetName,
2191fcf3ce44SJohn Forte 			    staticTargetName) == B_TRUE)) {
2192fcf3ce44SJohn Forte 				/* targetName found - found = B_TRUE */
2193fcf3ce44SJohn Forte 				found = B_TRUE;
2194fcf3ce44SJohn Forte 				if (targetAddressSpecified == B_FALSE) {
2195fcf3ce44SJohn Forte 					matched = B_TRUE;
2196fcf3ce44SJohn Forte 				} else {
2197fcf3ce44SJohn Forte 
2198fcf3ce44SJohn Forte 				if (staticTargetProps.staticTarget.
2199fcf3ce44SJohn Forte 				    targetAddress.imaStruct.
2200fcf3ce44SJohn Forte 				    hostnameIpAddress.id.ipAddress.
2201fcf3ce44SJohn Forte 				    ipv4Address == IMA_TRUE) {
2202fcf3ce44SJohn Forte 					(void) inet_ntop(AF_INET,
2203fcf3ce44SJohn Forte 					    staticTargetProps.
2204fcf3ce44SJohn Forte 					    staticTarget.targetAddress.
2205fcf3ce44SJohn Forte 					    imaStruct.hostnameIpAddress.id.
2206fcf3ce44SJohn Forte 					    ipAddress.ipAddress, tmpStr,
2207fcf3ce44SJohn Forte 					    sizeof (tmpStr));
2208fcf3ce44SJohn Forte 				} else {
2209fcf3ce44SJohn Forte 					(void) inet_ntop(AF_INET6,
2210fcf3ce44SJohn Forte 					    staticTargetProps.
2211fcf3ce44SJohn Forte 					    staticTarget.targetAddress.
2212fcf3ce44SJohn Forte 					    imaStruct.hostnameIpAddress.id.
2213fcf3ce44SJohn Forte 					    ipAddress.ipAddress, tmpStr,
2214fcf3ce44SJohn Forte 					    sizeof (tmpStr));
2215fcf3ce44SJohn Forte 				}
2216fcf3ce44SJohn Forte 
2217fcf3ce44SJohn Forte 				if (mbstowcs(tmpTargetAddress, tmpStr,
2218fcf3ce44SJohn Forte 				    SUN_IMA_IP_ADDRESS_PORT_LEN) ==
2219fcf3ce44SJohn Forte 				    (size_t)-1) {
2220fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s: %s\n",
2221fcf3ce44SJohn Forte 					    cmdName,
2222fcf3ce44SJohn Forte 					gettext("conversion error"));
2223fcf3ce44SJohn Forte 					ret = 1;
2224fcf3ce44SJohn Forte 					continue;
2225fcf3ce44SJohn Forte 				}
2226fcf3ce44SJohn Forte 
2227fcf3ce44SJohn Forte 				if (wcsncmp(tmpTargetAddress,
2228fcf3ce44SJohn Forte 				    staticTargetAddress,
2229fcf3ce44SJohn Forte 				    SUN_IMA_IP_ADDRESS_PORT_LEN)
2230fcf3ce44SJohn Forte 				    == 0 &&
2231fcf3ce44SJohn Forte 				    staticTargetProps.
2232fcf3ce44SJohn Forte 				    staticTarget.targetAddress.
2233fcf3ce44SJohn Forte 				    imaStruct.portNumber == port) {
2234fcf3ce44SJohn Forte 					/*
2235fcf3ce44SJohn Forte 					 * Since an object is
2236fcf3ce44SJohn Forte 					 * specified, it should also
2237fcf3ce44SJohn Forte 					 * have a tpgt specified. If
2238fcf3ce44SJohn Forte 					 * not, that means the object
2239fcf3ce44SJohn Forte 					 * specified is associated with
2240fcf3ce44SJohn Forte 					 * the default tpgt. In
2241fcf3ce44SJohn Forte 					 * either case, a tpgt
2242fcf3ce44SJohn Forte 					 * comparison should be done
2243fcf3ce44SJohn Forte 					 * before claiming that a
2244fcf3ce44SJohn Forte 					 * match is found.
2245fcf3ce44SJohn Forte 					 */
2246fcf3ce44SJohn Forte 					if ((tpgt == stpgt &&
2247fcf3ce44SJohn Forte 					    tpgtSpecified == B_TRUE &&
2248fcf3ce44SJohn Forte 					    defaultTpgt == IMA_FALSE) ||
2249fcf3ce44SJohn Forte 					    (tpgt == stpgt &&
2250fcf3ce44SJohn Forte 					    tpgtSpecified == B_FALSE &&
2251fcf3ce44SJohn Forte 					    defaultTpgt == IMA_TRUE)) {
2252fcf3ce44SJohn Forte 						matched = B_TRUE;
2253fcf3ce44SJohn Forte 					}
2254fcf3ce44SJohn Forte 				}
2255fcf3ce44SJohn Forte 
2256fcf3ce44SJohn Forte 				}
2257fcf3ce44SJohn Forte 			}
2258fcf3ce44SJohn Forte 
2259fcf3ce44SJohn Forte 			if (!object || matched) {
2260fcf3ce44SJohn Forte 				/* print summary - always */
2261fcf3ce44SJohn Forte 				(void) fprintf(stdout, gettext("%s: %ws,"),
2262fcf3ce44SJohn Forte 				    "Static Configuration Target",
2263fcf3ce44SJohn Forte 				    staticTargetProps.staticTarget.targetName);
2264fcf3ce44SJohn Forte 
2265fcf3ce44SJohn Forte 				if (isIpv6 == B_FALSE) {
2266fcf3ce44SJohn Forte 					(void) inet_ntop(AF_INET,
2267fcf3ce44SJohn Forte 					    staticTargetProps.
2268fcf3ce44SJohn Forte 					    staticTarget.targetAddress.
2269fcf3ce44SJohn Forte 					    imaStruct.hostnameIpAddress.id.
2270fcf3ce44SJohn Forte 					    ipAddress.ipAddress, sAddr,
2271fcf3ce44SJohn Forte 					    sizeof (sAddr));
2272fcf3ce44SJohn Forte 					(void) fprintf(stdout, "%s:%d",
2273fcf3ce44SJohn Forte 					    sAddr,
2274fcf3ce44SJohn Forte 					    staticTargetProps.staticTarget.
2275fcf3ce44SJohn Forte 					    targetAddress.imaStruct.portNumber);
2276fcf3ce44SJohn Forte 				} else {
2277fcf3ce44SJohn Forte 					(void) inet_ntop(AF_INET6,
2278fcf3ce44SJohn Forte 					    staticTargetProps.
2279fcf3ce44SJohn Forte 					    staticTarget.targetAddress.
2280fcf3ce44SJohn Forte 					    imaStruct.hostnameIpAddress.id.
2281fcf3ce44SJohn Forte 					    ipAddress.ipAddress, sAddr,
2282fcf3ce44SJohn Forte 					    sizeof (sAddr));
2283fcf3ce44SJohn Forte 					(void) fprintf(stdout, "[%s]:%d",
2284fcf3ce44SJohn Forte 					    sAddr,
2285fcf3ce44SJohn Forte 					    staticTargetProps.staticTarget.
2286fcf3ce44SJohn Forte 					    targetAddress.imaStruct.portNumber);
2287fcf3ce44SJohn Forte 				}
2288fcf3ce44SJohn Forte 
2289fcf3ce44SJohn Forte 				if (staticTargetProps.staticTarget.
2290fcf3ce44SJohn Forte 				    targetAddress.
2291fcf3ce44SJohn Forte 				    defaultTpgt == IMA_FALSE) {
2292fcf3ce44SJohn Forte 					(void) fprintf(stdout, ",%d\n",
2293fcf3ce44SJohn Forte 					    staticTargetProps.
2294fcf3ce44SJohn Forte 					    staticTarget.targetAddress.tpgt);
2295fcf3ce44SJohn Forte 				} else {
2296fcf3ce44SJohn Forte 					(void) fprintf(stdout, "\n");
2297fcf3ce44SJohn Forte 				}
2298fcf3ce44SJohn Forte 			}
2299fcf3ce44SJohn Forte 
2300fcf3ce44SJohn Forte 		}
2301fcf3ce44SJohn Forte 		/*
2302fcf3ce44SJohn Forte 		 * No details to display, but if there were:
2303fcf3ce44SJohn Forte 		 *  if (object && found)...
2304fcf3ce44SJohn Forte 		 *
2305fcf3ce44SJohn Forte 		 */
2306fcf3ce44SJohn Forte 
2307fcf3ce44SJohn Forte 		/*
2308fcf3ce44SJohn Forte 		 * There was an object entered but we didn't
2309fcf3ce44SJohn Forte 		 * find it.
2310fcf3ce44SJohn Forte 		 */
2311fcf3ce44SJohn Forte 		if (object && !found) {
2312fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s: %s\n",
2313fcf3ce44SJohn Forte 			    operand[i], gettext("not found"));
2314fcf3ce44SJohn Forte 			ret = 1; /* DIY test fix */
2315fcf3ce44SJohn Forte 		}
2316fcf3ce44SJohn Forte 	}
2317fcf3ce44SJohn Forte 	return (ret);
2318fcf3ce44SJohn Forte }
2319fcf3ce44SJohn Forte 
2320fcf3ce44SJohn Forte /*
2321fcf3ce44SJohn Forte  * Print targets
2322fcf3ce44SJohn Forte  */
2323fcf3ce44SJohn Forte /*ARGSUSED*/
2324fcf3ce44SJohn Forte static int
2325fcf3ce44SJohn Forte listTarget(int objectLen, char *objects[], cmdOptions_t *options, int *funcRet)
2326fcf3ce44SJohn Forte {
2327fcf3ce44SJohn Forte 	IMA_OID	initiatorOid;
2328fcf3ce44SJohn Forte 	IMA_OID_LIST *targetList;
2329fcf3ce44SJohn Forte 	IMA_OID_LIST *lunList;
2330fcf3ce44SJohn Forte 	SUN_IMA_TARGET_PROPERTIES targetProps;
2331fcf3ce44SJohn Forte 	IMA_STATUS status;
2332fcf3ce44SJohn Forte 	IMA_OID_LIST *pConnList;
2333fcf3ce44SJohn Forte 	SUN_IMA_CONN_PROPERTIES *connProps;
2334fcf3ce44SJohn Forte 
2335fcf3ce44SJohn Forte 	int ret;
2336fcf3ce44SJohn Forte 	wchar_t targetName[MAX_ISCSI_NAME_LEN + 1];
2337fcf3ce44SJohn Forte 	wchar_t targetAddress[SUN_IMA_IP_ADDRESS_PORT_LEN];
2338fcf3ce44SJohn Forte 	int outerLoop;
2339fcf3ce44SJohn Forte 	boolean_t found;
2340fcf3ce44SJohn Forte 	boolean_t operandEntered = B_FALSE;
2341fcf3ce44SJohn Forte 	boolean_t verbose = B_FALSE;
2342fcf3ce44SJohn Forte 	boolean_t scsi_target = B_FALSE;
2343fcf3ce44SJohn Forte 	boolean_t targetAddressSpecified = B_FALSE;
2344fcf3ce44SJohn Forte 	boolean_t isIpv6 = B_FALSE;
2345fcf3ce44SJohn Forte 	int i, j;
2346fcf3ce44SJohn Forte 	cmdOptions_t *optionList = options;
2347fcf3ce44SJohn Forte 	boolean_t tpgtSpecified = B_FALSE;
2348fcf3ce44SJohn Forte 	IMA_UINT16 port = 0;
2349fcf3ce44SJohn Forte 	uint16_t tpgt;
2350fcf3ce44SJohn Forte 
2351fcf3ce44SJohn Forte 	assert(funcRet != NULL);
2352fcf3ce44SJohn Forte 
2353fcf3ce44SJohn Forte 	/* Find Sun initiator */
2354fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
2355fcf3ce44SJohn Forte 	if (ret > 0) {
2356fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
2357fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
2358fcf3ce44SJohn Forte 	}
2359fcf3ce44SJohn Forte 
2360fcf3ce44SJohn Forte 	if (ret != 0) {
2361fcf3ce44SJohn Forte 		return (ret);
2362fcf3ce44SJohn Forte 	}
2363fcf3ce44SJohn Forte 
2364fcf3ce44SJohn Forte 	for (; optionList->optval; optionList++) {
2365fcf3ce44SJohn Forte 		switch (optionList->optval) {
2366fcf3ce44SJohn Forte 			case 'S':
2367fcf3ce44SJohn Forte 				scsi_target = B_TRUE;
2368fcf3ce44SJohn Forte 				break;
2369fcf3ce44SJohn Forte 			case 'v':
2370fcf3ce44SJohn Forte 				verbose = B_TRUE;
2371fcf3ce44SJohn Forte 				break;
2372fcf3ce44SJohn Forte 			default:
2373fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %c: %s\n",
2374fcf3ce44SJohn Forte 				    cmdName, optionList->optval,
2375fcf3ce44SJohn Forte 				    gettext("unknown option"));
2376fcf3ce44SJohn Forte 				return (1);
2377fcf3ce44SJohn Forte 		}
2378fcf3ce44SJohn Forte 	}
2379fcf3ce44SJohn Forte 
2380fcf3ce44SJohn Forte 	/*
2381fcf3ce44SJohn Forte 	 * If there are multiple objects, execute outer 'for' loop that
2382fcf3ce44SJohn Forte 	 * many times for each target detail, otherwise, execute it only
2383fcf3ce44SJohn Forte 	 * once with summaries only
2384fcf3ce44SJohn Forte 	 */
2385fcf3ce44SJohn Forte 	if (objectLen > 0) {
2386fcf3ce44SJohn Forte 		operandEntered = B_TRUE;
2387fcf3ce44SJohn Forte 		outerLoop = objectLen;
2388fcf3ce44SJohn Forte 	} else {
2389fcf3ce44SJohn Forte 		operandEntered = B_FALSE;
2390fcf3ce44SJohn Forte 		outerLoop = 1;
2391fcf3ce44SJohn Forte 	}
2392fcf3ce44SJohn Forte 
2393fcf3ce44SJohn Forte 	status = SUN_IMA_GetSessionOidList(initiatorOid, &targetList);
2394fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
2395fcf3ce44SJohn Forte 		printLibError(status);
2396fcf3ce44SJohn Forte 		*funcRet = 1;
2397fcf3ce44SJohn Forte 		return (ret);
2398fcf3ce44SJohn Forte 	}
2399fcf3ce44SJohn Forte 
2400fcf3ce44SJohn Forte 	for (i = 0; i < outerLoop; i++) {
2401fcf3ce44SJohn Forte 
2402fcf3ce44SJohn Forte 		tpgtSpecified = B_FALSE;
2403fcf3ce44SJohn Forte 		if (operandEntered) {
2404fcf3ce44SJohn Forte 			if (parseTarget(objects[i],
2405fcf3ce44SJohn Forte 			    &targetName[0],
2406fcf3ce44SJohn Forte 			    MAX_ISCSI_NAME_LEN + 1,
2407fcf3ce44SJohn Forte 			    &targetAddressSpecified,
2408fcf3ce44SJohn Forte 			    &targetAddress[0],
2409fcf3ce44SJohn Forte 			    SUN_IMA_IP_ADDRESS_PORT_LEN,
2410fcf3ce44SJohn Forte 			    &port,
2411fcf3ce44SJohn Forte 			    &tpgtSpecified,
2412fcf3ce44SJohn Forte 			    &tpgt,
2413fcf3ce44SJohn Forte 			    &isIpv6) != PARSE_TARGET_OK) {
2414fcf3ce44SJohn Forte 				ret = 1;
2415fcf3ce44SJohn Forte 				continue;
2416fcf3ce44SJohn Forte 			}
2417fcf3ce44SJohn Forte 		}
2418fcf3ce44SJohn Forte 		for (found = B_FALSE, j = 0; j < targetList->oidCount; j++) {
2419fcf3ce44SJohn Forte 			status = SUN_IMA_GetTargetProperties(
2420fcf3ce44SJohn Forte 			    targetList->oids[j],
2421fcf3ce44SJohn Forte 			    &targetProps);
2422fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
2423fcf3ce44SJohn Forte 				printLibError(status);
2424fcf3ce44SJohn Forte 				(void) IMA_FreeMemory(targetList);
2425fcf3ce44SJohn Forte 				*funcRet = 1;
2426fcf3ce44SJohn Forte 				return (ret);
2427fcf3ce44SJohn Forte 			}
2428fcf3ce44SJohn Forte 
2429fcf3ce44SJohn Forte 			/*
2430fcf3ce44SJohn Forte 			 * Compare the target name with the input if
2431fcf3ce44SJohn Forte 			 * one was input, if they match, print the target's info
2432fcf3ce44SJohn Forte 			 *
2433fcf3ce44SJohn Forte 			 * if no target name was input, continue printing this
2434fcf3ce44SJohn Forte 			 * target
2435fcf3ce44SJohn Forte 			 */
2436fcf3ce44SJohn Forte 			if (operandEntered) {
2437fcf3ce44SJohn Forte 				if (targetNamesEqual(targetProps.imaProps.name,
2438fcf3ce44SJohn Forte 				    targetName) == B_TRUE) {
2439fcf3ce44SJohn Forte 					if (tpgtSpecified == B_TRUE) {
2440fcf3ce44SJohn Forte 						if (targetProps.
2441fcf3ce44SJohn Forte 						    defaultTpgtConf ==
2442fcf3ce44SJohn Forte 						    IMA_FALSE &&
2443fcf3ce44SJohn Forte 						    targetProps.
2444fcf3ce44SJohn Forte 						    tpgtConf == tpgt) {
2445fcf3ce44SJohn Forte 							found = B_TRUE;
2446fcf3ce44SJohn Forte 						} else {
2447fcf3ce44SJohn Forte 							/*
2448fcf3ce44SJohn Forte 							 * tpgt does not match,
2449fcf3ce44SJohn Forte 							 * move on to next
2450fcf3ce44SJohn Forte 							 * target
2451fcf3ce44SJohn Forte 							 */
2452fcf3ce44SJohn Forte 							continue;
2453fcf3ce44SJohn Forte 						}
2454fcf3ce44SJohn Forte 					} else {
2455fcf3ce44SJohn Forte 						found = B_TRUE;
2456fcf3ce44SJohn Forte 					}
2457fcf3ce44SJohn Forte 				} else {
2458fcf3ce44SJohn Forte 					/*
2459fcf3ce44SJohn Forte 					 * target name does not match, move on
2460fcf3ce44SJohn Forte 					 * to next target
2461fcf3ce44SJohn Forte 					 */
2462fcf3ce44SJohn Forte 					continue;
2463fcf3ce44SJohn Forte 				}
2464fcf3ce44SJohn Forte 			}
2465fcf3ce44SJohn Forte 
2466fcf3ce44SJohn Forte 			/* print summary - always */
2467fcf3ce44SJohn Forte 			(void) fprintf(stdout, gettext("%s: %ws\n"),
2468fcf3ce44SJohn Forte 			    gettext("Target"), targetProps.imaProps.name);
2469fcf3ce44SJohn Forte 
2470fcf3ce44SJohn Forte 			/* Alias */
2471fcf3ce44SJohn Forte 			(void) fprintf(stdout, "\t%s: ", gettext("Alias"));
2472fcf3ce44SJohn Forte 			if (wslen(targetProps.imaProps.alias) > (size_t)0) {
2473fcf3ce44SJohn Forte 				(void) fprintf(stdout, gettext("%ws\n"),
2474fcf3ce44SJohn Forte 				    targetProps.imaProps.alias);
2475fcf3ce44SJohn Forte 			} else {
2476fcf3ce44SJohn Forte 				(void) fprintf(stdout, "%s\n", "-");
2477fcf3ce44SJohn Forte 			}
2478fcf3ce44SJohn Forte 
2479fcf3ce44SJohn Forte 			if (targetProps.defaultTpgtNego != IMA_TRUE) {
2480fcf3ce44SJohn Forte 				(void) fprintf(stdout, "%s%s: %d\n",
2481fcf3ce44SJohn Forte 				    "\t", gettext("TPGT"),
2482fcf3ce44SJohn Forte 				    targetProps.tpgtNego);
2483fcf3ce44SJohn Forte 			} else if (targetProps.defaultTpgtConf != IMA_TRUE) {
2484fcf3ce44SJohn Forte 				(void) fprintf(stdout, "%s%s: %d\n",
2485fcf3ce44SJohn Forte 				    "\t", gettext("TPGT"),
2486fcf3ce44SJohn Forte 				    targetProps.tpgtConf);
2487fcf3ce44SJohn Forte 			}
2488fcf3ce44SJohn Forte 
2489fcf3ce44SJohn Forte 			(void) fprintf(stdout,
2490fcf3ce44SJohn Forte 			    "%s%s: %02x%02x%02x%02x%02x%02x\n",
2491fcf3ce44SJohn Forte 			    "\t", gettext("ISID"),
2492fcf3ce44SJohn Forte 			    targetProps.isid[0], targetProps.isid[1],
2493fcf3ce44SJohn Forte 			    targetProps.isid[2], targetProps.isid[3],
2494fcf3ce44SJohn Forte 			    targetProps.isid[4], targetProps.isid[5]);
2495fcf3ce44SJohn Forte 
2496fcf3ce44SJohn Forte 			pConnList = NULL;
2497fcf3ce44SJohn Forte 			status = SUN_IMA_GetConnOidList(
2498fcf3ce44SJohn Forte 			    &targetList->oids[j],
2499fcf3ce44SJohn Forte 			    &pConnList);
2500fcf3ce44SJohn Forte 
2501fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
2502fcf3ce44SJohn Forte 				printLibError(status);
2503fcf3ce44SJohn Forte 				(void) IMA_FreeMemory(targetList);
2504fcf3ce44SJohn Forte 				*funcRet = 1;
2505fcf3ce44SJohn Forte 				return (ret);
2506fcf3ce44SJohn Forte 			}
2507fcf3ce44SJohn Forte 
2508fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s%s: %lu\n",
2509fcf3ce44SJohn Forte 			    "\t",
2510fcf3ce44SJohn Forte 			    gettext("Connections"),
2511fcf3ce44SJohn Forte 			    pConnList->oidCount);
2512fcf3ce44SJohn Forte 
2513fcf3ce44SJohn Forte 			if (verbose) {
2514fcf3ce44SJohn Forte 				SUN_IMA_DIGEST_ALGORITHM_VALUE digestAlgorithms;
2515fcf3ce44SJohn Forte 
2516fcf3ce44SJohn Forte 				printConnectionList("\t\t", pConnList);
2517fcf3ce44SJohn Forte 				printDiscoveryMethod(
2518fcf3ce44SJohn Forte 				    "\t\t  ",
2519fcf3ce44SJohn Forte 				    targetProps.imaProps.discoveryMethodFlags);
2520fcf3ce44SJohn Forte 				(void) printLoginParameters(
2521fcf3ce44SJohn Forte 				    "\t\t  ",
2522fcf3ce44SJohn Forte 				    targetList->oids[j],
2523fcf3ce44SJohn Forte 				    PRINT_NEGOTIATED_PARAMS);
2524fcf3ce44SJohn Forte 
2525fcf3ce44SJohn Forte 				/* Get Digest configuration */
2526fcf3ce44SJohn Forte 				status = SUN_IMA_GetConnProperties(
2527fcf3ce44SJohn Forte 				    &pConnList->oids[0], &connProps);
2528fcf3ce44SJohn Forte 
2529fcf3ce44SJohn Forte 				(void) getNegotiatedDigest(
2530fcf3ce44SJohn Forte 				    ISCSI_LOGIN_PARAM_HEADER_DIGEST,
2531fcf3ce44SJohn Forte 				    &digestAlgorithms, connProps);
2532fcf3ce44SJohn Forte 
2533fcf3ce44SJohn Forte 				if (IMA_SUCCESS(status)) {
2534fcf3ce44SJohn Forte 					(void) fprintf(stdout, "\t\t  \t%s: ",
2535fcf3ce44SJohn Forte 					    gettext("Header Digest"));
2536fcf3ce44SJohn Forte 					printDigestAlgorithm(
2537fcf3ce44SJohn Forte 					    &digestAlgorithms,
2538fcf3ce44SJohn Forte 					    PRINT_NEGOTIATED_PARAMS);
2539fcf3ce44SJohn Forte 				} else {
2540fcf3ce44SJohn Forte 					(void) IMA_FreeMemory(pConnList);
2541fcf3ce44SJohn Forte 					(void) IMA_FreeMemory(targetList);
2542fcf3ce44SJohn Forte 					printLibError(status);
2543fcf3ce44SJohn Forte 					*funcRet = 1;
2544fcf3ce44SJohn Forte 					return (ret);
2545fcf3ce44SJohn Forte 				}
2546fcf3ce44SJohn Forte 
2547fcf3ce44SJohn Forte 				(void) getNegotiatedDigest(
2548fcf3ce44SJohn Forte 				    ISCSI_LOGIN_PARAM_DATA_DIGEST,
2549fcf3ce44SJohn Forte 				    &digestAlgorithms, connProps);
2550fcf3ce44SJohn Forte 
2551fcf3ce44SJohn Forte 				if (IMA_SUCCESS(status)) {
2552fcf3ce44SJohn Forte 					(void) fprintf(stdout, "\t\t  \t%s: ",
2553fcf3ce44SJohn Forte 					    gettext("Data Digest"));
2554fcf3ce44SJohn Forte 					printDigestAlgorithm(
2555fcf3ce44SJohn Forte 					    &digestAlgorithms,
2556fcf3ce44SJohn Forte 					    PRINT_NEGOTIATED_PARAMS);
2557fcf3ce44SJohn Forte 				} else {
2558fcf3ce44SJohn Forte 					(void) IMA_FreeMemory(pConnList);
2559fcf3ce44SJohn Forte 					(void) IMA_FreeMemory(targetList);
2560fcf3ce44SJohn Forte 					printLibError(status);
2561fcf3ce44SJohn Forte 					*funcRet = 1;
2562fcf3ce44SJohn Forte 					return (ret);
2563fcf3ce44SJohn Forte 				}
2564fcf3ce44SJohn Forte 
2565fcf3ce44SJohn Forte 				(void) fprintf(stdout, "\n");
2566fcf3ce44SJohn Forte 			}
2567fcf3ce44SJohn Forte 
2568fcf3ce44SJohn Forte 			if (scsi_target) {
2569fcf3ce44SJohn Forte 				status = IMA_GetLuOidList(
2570fcf3ce44SJohn Forte 				    targetList->oids[j],
2571fcf3ce44SJohn Forte 				    &lunList);
2572fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
2573fcf3ce44SJohn Forte 					printLibError(status);
2574fcf3ce44SJohn Forte 					(void) IMA_FreeMemory(targetList);
2575fcf3ce44SJohn Forte 					*funcRet = 1;
2576fcf3ce44SJohn Forte 					return (ret);
2577fcf3ce44SJohn Forte 				}
2578fcf3ce44SJohn Forte 				if (lunList->oidCount != 0) {
2579fcf3ce44SJohn Forte 					printTargetLuns(lunList);
2580fcf3ce44SJohn Forte 				}
2581fcf3ce44SJohn Forte 				(void) fprintf(stdout, "\n");
2582fcf3ce44SJohn Forte 				(void) IMA_FreeMemory(lunList);
2583fcf3ce44SJohn Forte 			}
2584fcf3ce44SJohn Forte 		}
2585fcf3ce44SJohn Forte 		/*
2586fcf3ce44SJohn Forte 		 * did we find the object
2587fcf3ce44SJohn Forte 		 */
2588fcf3ce44SJohn Forte 
2589fcf3ce44SJohn Forte 		if (operandEntered && !found) {
2590fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s: %s\n",
2591fcf3ce44SJohn Forte 			    objects[i], gettext("not found"));
2592fcf3ce44SJohn Forte 		}
2593fcf3ce44SJohn Forte 	}
2594fcf3ce44SJohn Forte 
2595fcf3ce44SJohn Forte 	(void) IMA_FreeMemory(targetList);
2596fcf3ce44SJohn Forte 	return (ret);
2597fcf3ce44SJohn Forte }
2598fcf3ce44SJohn Forte 
2599fcf3ce44SJohn Forte 
2600fcf3ce44SJohn Forte /*
2601fcf3ce44SJohn Forte  * Print configured session information
2602fcf3ce44SJohn Forte  */
2603fcf3ce44SJohn Forte static int
2604fcf3ce44SJohn Forte printConfiguredSessions(IMA_OID oid)
2605fcf3ce44SJohn Forte {
2606fcf3ce44SJohn Forte 	IMA_STATUS		status;
2607fcf3ce44SJohn Forte 	const char		*rtn;
2608fcf3ce44SJohn Forte 	SUN_IMA_CONFIG_SESSIONS	*pConfigSessions;
2609fcf3ce44SJohn Forte 	char			address[MAX_ADDRESS_LEN];
2610fcf3ce44SJohn Forte 	int			out;
2611fcf3ce44SJohn Forte 
2612fcf3ce44SJohn Forte 	/* Get configured session information */
2613fcf3ce44SJohn Forte 	status = SUN_IMA_GetConfigSessions(oid, &pConfigSessions);
2614fcf3ce44SJohn Forte 
2615fcf3ce44SJohn Forte 	if (IMA_SUCCESS(status)) {
2616fcf3ce44SJohn Forte 		(void) fprintf(stdout, "\t%s: ",
2617fcf3ce44SJohn Forte 		    gettext("Configured Sessions"));
2618fcf3ce44SJohn Forte 		if (pConfigSessions->bound == IMA_FALSE) {
2619fcf3ce44SJohn Forte 			/* default binding */
2620fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%lu\n", pConfigSessions->out);
2621fcf3ce44SJohn Forte 		} else {
2622fcf3ce44SJohn Forte 			/* hardcoded binding */
2623fcf3ce44SJohn Forte 			for (out = 0;
2624fcf3ce44SJohn Forte 			    out < pConfigSessions->out; out++) {
2625fcf3ce44SJohn Forte 				if (pConfigSessions->bindings[out].
2626fcf3ce44SJohn Forte 				    ipAddress.ipv4Address == IMA_TRUE) {
2627fcf3ce44SJohn Forte 					rtn = inet_ntop(AF_INET,
2628fcf3ce44SJohn Forte 					    pConfigSessions->bindings[out].
2629fcf3ce44SJohn Forte 					    ipAddress.ipAddress, address,
2630fcf3ce44SJohn Forte 					    MAX_ADDRESS_LEN);
2631fcf3ce44SJohn Forte 				} else {
2632fcf3ce44SJohn Forte 					rtn = inet_ntop(AF_INET6,
2633fcf3ce44SJohn Forte 					    pConfigSessions->bindings[out].
2634fcf3ce44SJohn Forte 					    ipAddress.ipAddress, address,
2635fcf3ce44SJohn Forte 					    MAX_ADDRESS_LEN);
2636fcf3ce44SJohn Forte 				}
2637fcf3ce44SJohn Forte 				if (rtn != NULL) {
2638fcf3ce44SJohn Forte 					(void) printf("%s ", address);
2639fcf3ce44SJohn Forte 				}
2640fcf3ce44SJohn Forte 			}
2641fcf3ce44SJohn Forte 			(void) fprintf(stdout, "\n");
2642fcf3ce44SJohn Forte 		}
2643fcf3ce44SJohn Forte 	} else {
2644fcf3ce44SJohn Forte 		free(pConfigSessions);
2645fcf3ce44SJohn Forte 		printLibError(status);
2646fcf3ce44SJohn Forte 		return (1);
2647fcf3ce44SJohn Forte 	}
2648fcf3ce44SJohn Forte 
2649fcf3ce44SJohn Forte 	free(pConfigSessions);
2650fcf3ce44SJohn Forte 	return (0);
2651fcf3ce44SJohn Forte }
2652fcf3ce44SJohn Forte 
2653fcf3ce44SJohn Forte /*
2654fcf3ce44SJohn Forte  * Print target parameters
2655fcf3ce44SJohn Forte  */
2656fcf3ce44SJohn Forte static int
2657fcf3ce44SJohn Forte listTargetParam(int operandLen, char *operand[], cmdOptions_t *options,
2658fcf3ce44SJohn Forte     int *funcRet)
2659fcf3ce44SJohn Forte {
2660fcf3ce44SJohn Forte 	IMA_STATUS status;
2661fcf3ce44SJohn Forte 	IMA_OID	initiatorOid;
2662fcf3ce44SJohn Forte 	IMA_OID_LIST *targetList;
2663fcf3ce44SJohn Forte 	IMA_AUTHMETHOD	methodList[MAX_AUTH_METHODS];
2664fcf3ce44SJohn Forte 	SUN_IMA_TARGET_PROPERTIES targetProps;
2665fcf3ce44SJohn Forte 	IMA_UINT maxEntries = MAX_AUTH_METHODS;
2666fcf3ce44SJohn Forte 	IMA_BOOL bidirAuth;
2667fcf3ce44SJohn Forte 	int ret;
2668fcf3ce44SJohn Forte 	wchar_t targetName[MAX_ISCSI_NAME_LEN + 1];
2669fcf3ce44SJohn Forte 	wchar_t targetAddress[SUN_IMA_IP_ADDRESS_PORT_LEN];
2670fcf3ce44SJohn Forte 	boolean_t operandEntered = B_FALSE;
2671fcf3ce44SJohn Forte 	boolean_t targetAddressSpecified = B_FALSE;
2672fcf3ce44SJohn Forte 	boolean_t printObject = B_FALSE;
2673fcf3ce44SJohn Forte 	boolean_t tpgtSpecified = B_FALSE;
2674fcf3ce44SJohn Forte 	boolean_t isIpv6 = B_FALSE;
2675fcf3ce44SJohn Forte 	int outerLoop;
2676fcf3ce44SJohn Forte 	boolean_t found;
2677fcf3ce44SJohn Forte 	int i, j;
2678fcf3ce44SJohn Forte 	SUN_IMA_DIGEST_ALGORITHM_VALUE digestAlgorithms;
2679fcf3ce44SJohn Forte 	boolean_t verbose = B_FALSE;
2680fcf3ce44SJohn Forte 	cmdOptions_t *optionList = options;
2681fcf3ce44SJohn Forte 	IMA_UINT16 port = 0;
2682fcf3ce44SJohn Forte 	IMA_UINT16 tpgt = 0;
2683fcf3ce44SJohn Forte 
2684fcf3ce44SJohn Forte 	assert(funcRet != NULL);
2685fcf3ce44SJohn Forte 
2686fcf3ce44SJohn Forte 	/* Find Sun initiator */
2687fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
2688fcf3ce44SJohn Forte 	if (ret > 0) {
2689fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
2690fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
2691fcf3ce44SJohn Forte 	}
2692fcf3ce44SJohn Forte 
2693fcf3ce44SJohn Forte 	if (ret != 0) {
2694fcf3ce44SJohn Forte 		return (ret);
2695fcf3ce44SJohn Forte 	}
2696fcf3ce44SJohn Forte 
2697fcf3ce44SJohn Forte 	for (; optionList->optval; optionList++) {
2698fcf3ce44SJohn Forte 		switch (optionList->optval) {
2699fcf3ce44SJohn Forte 			case 'v':
2700fcf3ce44SJohn Forte 				verbose = B_TRUE;
2701fcf3ce44SJohn Forte 				break;
2702fcf3ce44SJohn Forte 			default:
2703fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %c: %s\n",
2704fcf3ce44SJohn Forte 				    cmdName, optionList->optval,
2705fcf3ce44SJohn Forte 				    gettext("unknown option"));
2706fcf3ce44SJohn Forte 				return (1);
2707fcf3ce44SJohn Forte 		}
2708fcf3ce44SJohn Forte 	}
2709fcf3ce44SJohn Forte 
2710fcf3ce44SJohn Forte 	/*
2711fcf3ce44SJohn Forte 	 * If there are multiple operands, execute outer 'for' loop that
2712fcf3ce44SJohn Forte 	 * many times to find each target parameter operand entered, otherwise,
2713fcf3ce44SJohn Forte 	 * execute it only once for all target parameters returned.
2714fcf3ce44SJohn Forte 	 */
2715fcf3ce44SJohn Forte 	if (operandLen > 0) {
2716fcf3ce44SJohn Forte 		operandEntered = B_TRUE;
2717fcf3ce44SJohn Forte 		outerLoop = operandLen;
2718fcf3ce44SJohn Forte 	} else {
2719fcf3ce44SJohn Forte 		operandEntered = B_FALSE;
2720fcf3ce44SJohn Forte 		outerLoop = 1;
2721fcf3ce44SJohn Forte 	}
2722fcf3ce44SJohn Forte 
2723fcf3ce44SJohn Forte 	/*
2724fcf3ce44SJohn Forte 	 * Ideally there should be an interface available for obtaining
2725fcf3ce44SJohn Forte 	 * the list of target-param objects. Since the driver currently
2726fcf3ce44SJohn Forte 	 * creates a target OID and the associated session structure when
2727fcf3ce44SJohn Forte 	 * a target-param object is created, we can leverage the target
2728fcf3ce44SJohn Forte 	 * OID list and use it to manage the target-param objects. When
2729fcf3ce44SJohn Forte 	 * we stop creating a session for target-param object in the
2730fcf3ce44SJohn Forte 	 * driver, we will switch to using a different interface to
2731fcf3ce44SJohn Forte 	 * obtain target-param objects.
2732fcf3ce44SJohn Forte 	 */
2733fcf3ce44SJohn Forte 	status = IMA_GetTargetOidList(initiatorOid, &targetList);
2734fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
2735fcf3ce44SJohn Forte 		printLibError(status);
2736fcf3ce44SJohn Forte 		*funcRet = 1;
2737fcf3ce44SJohn Forte 		return (ret);
2738fcf3ce44SJohn Forte 	}
2739fcf3ce44SJohn Forte 
2740fcf3ce44SJohn Forte 	for (i = 0; i < outerLoop; i++) {
2741fcf3ce44SJohn Forte 		if (operandEntered) {
2742fcf3ce44SJohn Forte 			if (parseTarget(operand[i],
2743fcf3ce44SJohn Forte 			    &targetName[0],
2744fcf3ce44SJohn Forte 			    MAX_ISCSI_NAME_LEN + 1,
2745fcf3ce44SJohn Forte 			    &targetAddressSpecified,
2746fcf3ce44SJohn Forte 			    &targetAddress[0],
2747fcf3ce44SJohn Forte 			    SUN_IMA_IP_ADDRESS_PORT_LEN,
2748fcf3ce44SJohn Forte 			    &port,
2749fcf3ce44SJohn Forte 			    &tpgtSpecified,
2750fcf3ce44SJohn Forte 			    &tpgt,
2751fcf3ce44SJohn Forte 			    &isIpv6) != PARSE_TARGET_OK) {
2752fcf3ce44SJohn Forte 				ret = 1;
2753fcf3ce44SJohn Forte 				continue;
2754fcf3ce44SJohn Forte 			}
2755fcf3ce44SJohn Forte 		}
2756fcf3ce44SJohn Forte 		for (j = 0; j < targetList->oidCount; j++) {
2757fcf3ce44SJohn Forte 			found = B_FALSE;
2758fcf3ce44SJohn Forte 			printObject = B_FALSE;
2759fcf3ce44SJohn Forte 			status = SUN_IMA_GetTargetProperties(
2760fcf3ce44SJohn Forte 			    targetList->oids[j],
2761fcf3ce44SJohn Forte 			    &targetProps);
2762fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
2763fcf3ce44SJohn Forte 				printLibError(status);
2764fcf3ce44SJohn Forte 				(void) IMA_FreeMemory(targetList);
2765fcf3ce44SJohn Forte 				*funcRet = 1;
2766fcf3ce44SJohn Forte 				return (ret);
2767fcf3ce44SJohn Forte 			}
2768fcf3ce44SJohn Forte 
2769fcf3ce44SJohn Forte 			/*
2770fcf3ce44SJohn Forte 			 * Compare the target name with the input if
2771fcf3ce44SJohn Forte 			 * one was input
2772fcf3ce44SJohn Forte 			 */
2773fcf3ce44SJohn Forte 			if (operandEntered &&
2774fcf3ce44SJohn Forte 			    (targetNamesEqual(targetProps.imaProps.name,
2775fcf3ce44SJohn Forte 			    targetName) == B_TRUE)) {
2776fcf3ce44SJohn Forte 				/*
2777fcf3ce44SJohn Forte 				 * For now, regardless of whether a target
2778fcf3ce44SJohn Forte 				 * address is specified, we return B_TRUE
2779fcf3ce44SJohn Forte 				 * because IMA_TARGET_PROPERTIES does not
2780fcf3ce44SJohn Forte 				 * have a field for specifying address.
2781fcf3ce44SJohn Forte 				 */
2782fcf3ce44SJohn Forte 				found = B_TRUE;
2783fcf3ce44SJohn Forte 			}
2784fcf3ce44SJohn Forte 
2785fcf3ce44SJohn Forte 			/*
2786fcf3ce44SJohn Forte 			 * if no operand was entered OR
2787fcf3ce44SJohn Forte 			 * an operand was entered and it was
2788fcf3ce44SJohn Forte 			 * found, we want to print
2789fcf3ce44SJohn Forte 			 */
2790fcf3ce44SJohn Forte 			if (!operandEntered || found) {
2791fcf3ce44SJohn Forte 				printObject = B_TRUE;
2792fcf3ce44SJohn Forte 			}
2793fcf3ce44SJohn Forte 
2794fcf3ce44SJohn Forte 			if (printObject) {
2795fcf3ce44SJohn Forte 				(void) fprintf(stdout, gettext("%s: %ws\n"),
2796fcf3ce44SJohn Forte 				    gettext("Target"),
2797fcf3ce44SJohn Forte 				    targetProps.imaProps.name);
2798fcf3ce44SJohn Forte 
2799fcf3ce44SJohn Forte 				(void) fprintf(stdout,
2800fcf3ce44SJohn Forte 				    "\t%s: ", gettext("Alias"));
2801fcf3ce44SJohn Forte 				if (wslen(targetProps.imaProps.alias) >
2802fcf3ce44SJohn Forte 				    (size_t)0) {
2803fcf3ce44SJohn Forte 					(void) fprintf(stdout,
2804fcf3ce44SJohn Forte 					    gettext("%ws\n"),
2805fcf3ce44SJohn Forte 					    targetProps.imaProps.alias);
2806fcf3ce44SJohn Forte 				} else {
2807fcf3ce44SJohn Forte 					(void) fprintf(stdout, "%s\n", "-");
2808fcf3ce44SJohn Forte 				}
2809fcf3ce44SJohn Forte 			}
2810fcf3ce44SJohn Forte 
2811fcf3ce44SJohn Forte 			if (printObject && verbose) {
2812fcf3ce44SJohn Forte 				/* Get bidirectional authentication flag */
2813fcf3ce44SJohn Forte 				(void) fprintf(stdout, "\t%s: ",
2814fcf3ce44SJohn Forte 				    gettext("Bi-directional Authentication"));
2815fcf3ce44SJohn Forte 				status = SUN_IMA_GetTargetBidirAuthFlag(
2816fcf3ce44SJohn Forte 				    targetList->oids[j],
2817fcf3ce44SJohn Forte 				    &bidirAuth);
2818fcf3ce44SJohn Forte 				if (IMA_SUCCESS(status)) {
2819fcf3ce44SJohn Forte 					if (bidirAuth == IMA_TRUE) {
2820fcf3ce44SJohn Forte 						(void) fprintf(stdout,
2821fcf3ce44SJohn Forte 						    gettext("enabled"));
2822fcf3ce44SJohn Forte 					} else {
2823fcf3ce44SJohn Forte 						(void) fprintf(stdout,
2824fcf3ce44SJohn Forte 						    gettext("disabled"));
2825fcf3ce44SJohn Forte 					}
2826fcf3ce44SJohn Forte 				} else {
2827fcf3ce44SJohn Forte 					(void) fprintf(stdout,
2828fcf3ce44SJohn Forte 					    gettext("disabled"));
2829fcf3ce44SJohn Forte 				}
2830fcf3ce44SJohn Forte 				(void) fprintf(stdout, "\n");
2831fcf3ce44SJohn Forte 
2832fcf3ce44SJohn Forte 				/* Get authentication type for this target */
2833fcf3ce44SJohn Forte 				status = SUN_IMA_GetTargetAuthMethods(
2834fcf3ce44SJohn Forte 				    initiatorOid,
2835fcf3ce44SJohn Forte 				    targetList->oids[j],
2836fcf3ce44SJohn Forte 				    &maxEntries,
2837fcf3ce44SJohn Forte 				    &methodList[0]);
2838fcf3ce44SJohn Forte 				(void) fprintf(stdout, "\t%s: ",
2839fcf3ce44SJohn Forte 				    gettext("Authentication Type"));
2840fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
2841fcf3ce44SJohn Forte 					/*
2842fcf3ce44SJohn Forte 					 * No authentication method define
2843fcf3ce44SJohn Forte 					 * NONE by default.
2844fcf3ce44SJohn Forte 					 */
2845fcf3ce44SJohn Forte 					(void) fprintf(stdout, gettext("NONE"));
2846fcf3ce44SJohn Forte 				} else {
2847fcf3ce44SJohn Forte 					for (i = 0; i < maxEntries; i++) {
2848fcf3ce44SJohn Forte 						if (i > 0) {
2849fcf3ce44SJohn Forte 							(void) fprintf(stdout,
2850fcf3ce44SJohn Forte 							    "|");
2851fcf3ce44SJohn Forte 						}
2852fcf3ce44SJohn Forte 						switch (methodList[i]) {
2853fcf3ce44SJohn Forte 						case IMA_AUTHMETHOD_NONE:
2854fcf3ce44SJohn Forte 							(void) fprintf(stdout,
2855fcf3ce44SJohn Forte 							    gettext("NONE"));
2856fcf3ce44SJohn Forte 							break;
2857fcf3ce44SJohn Forte 
2858fcf3ce44SJohn Forte 						case IMA_AUTHMETHOD_CHAP:
2859fcf3ce44SJohn Forte 							(void) fprintf(stdout,
2860fcf3ce44SJohn Forte 							    gettext("CHAP"));
2861fcf3ce44SJohn Forte 							listCHAPName(
2862fcf3ce44SJohn Forte 							    targetList->
2863fcf3ce44SJohn Forte 							    oids[j]);
2864fcf3ce44SJohn Forte 							break;
2865fcf3ce44SJohn Forte 
2866fcf3ce44SJohn Forte 						default:
2867fcf3ce44SJohn Forte 							(void) fprintf(stdout,
2868fcf3ce44SJohn Forte 							    gettext(
2869fcf3ce44SJohn Forte 							    "unknown "
2870fcf3ce44SJohn Forte 							    "type"));
2871fcf3ce44SJohn Forte 							break;
2872fcf3ce44SJohn Forte 						}
2873fcf3ce44SJohn Forte 					}
2874fcf3ce44SJohn Forte 				}
2875fcf3ce44SJohn Forte 				(void) fprintf(stdout, "\n");
2876fcf3ce44SJohn Forte 				if (printLoginParameters("\t",
2877fcf3ce44SJohn Forte 				    targetList->oids[j],
2878fcf3ce44SJohn Forte 				    PRINT_CONFIGURED_PARAMS)
2879fcf3ce44SJohn Forte 				    != 0) {
2880fcf3ce44SJohn Forte 					(void) IMA_FreeMemory(targetList);
2881fcf3ce44SJohn Forte 					*funcRet = 1;
2882fcf3ce44SJohn Forte 					return (ret);
2883fcf3ce44SJohn Forte 				}
2884fcf3ce44SJohn Forte 
2885fcf3ce44SJohn Forte 				/* Get Digest configuration */
2886fcf3ce44SJohn Forte 				status = SUN_IMA_GetHeaderDigest(
2887fcf3ce44SJohn Forte 				    targetList->oids[j],
2888fcf3ce44SJohn Forte 				    &digestAlgorithms);
2889fcf3ce44SJohn Forte 				if (IMA_SUCCESS(status)) {
2890fcf3ce44SJohn Forte 					(void) fprintf(stdout, "\t\t%s: ",
2891fcf3ce44SJohn Forte 					    gettext("Header Digest"));
2892fcf3ce44SJohn Forte 					printDigestAlgorithm(&digestAlgorithms,
2893fcf3ce44SJohn Forte 					    PRINT_CONFIGURED_PARAMS);
2894fcf3ce44SJohn Forte 				} else {
2895fcf3ce44SJohn Forte 					printLibError(status);
2896fcf3ce44SJohn Forte 					*funcRet = 1;
2897fcf3ce44SJohn Forte 					return (ret);
2898fcf3ce44SJohn Forte 				}
2899fcf3ce44SJohn Forte 
2900fcf3ce44SJohn Forte 				status = SUN_IMA_GetDataDigest(
2901fcf3ce44SJohn Forte 				    targetList->oids[j],
2902fcf3ce44SJohn Forte 				    &digestAlgorithms);
2903fcf3ce44SJohn Forte 				if (IMA_SUCCESS(status)) {
2904fcf3ce44SJohn Forte 					(void) fprintf(stdout, "\t\t%s: ",
2905fcf3ce44SJohn Forte 					    gettext("Data Digest"));
2906fcf3ce44SJohn Forte 					printDigestAlgorithm(&digestAlgorithms,
2907fcf3ce44SJohn Forte 					    PRINT_CONFIGURED_PARAMS);
2908fcf3ce44SJohn Forte 				} else {
2909fcf3ce44SJohn Forte 					printLibError(status);
2910fcf3ce44SJohn Forte 					*funcRet = 1;
2911fcf3ce44SJohn Forte 					return (ret);
2912fcf3ce44SJohn Forte 				}
2913fcf3ce44SJohn Forte 
2914fcf3ce44SJohn Forte 				/* print configured session information */
2915fcf3ce44SJohn Forte 				if (printConfiguredSessions(
2916fcf3ce44SJohn Forte 				    targetList->oids[j]) != 0) {
2917fcf3ce44SJohn Forte 					*funcRet = 1;
2918fcf3ce44SJohn Forte 					return (ret);
2919fcf3ce44SJohn Forte 				}
2920fcf3ce44SJohn Forte 
2921fcf3ce44SJohn Forte 				(void) fprintf(stdout, "\n");
2922fcf3ce44SJohn Forte 			}
2923fcf3ce44SJohn Forte 
2924fcf3ce44SJohn Forte 			if (found) {
2925fcf3ce44SJohn Forte 				break;
2926fcf3ce44SJohn Forte 			}
2927fcf3ce44SJohn Forte 		}
2928fcf3ce44SJohn Forte 		if (operandEntered && !found) {
2929fcf3ce44SJohn Forte 			*funcRet = 1; /* DIY message fix */
2930fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s: %s\n",
2931fcf3ce44SJohn Forte 			    operand[i], gettext("not found"));
2932fcf3ce44SJohn Forte 		}
2933fcf3ce44SJohn Forte 	}
2934fcf3ce44SJohn Forte 
2935fcf3ce44SJohn Forte 	(void) IMA_FreeMemory(targetList);
2936fcf3ce44SJohn Forte 	return (ret);
2937fcf3ce44SJohn Forte }
2938fcf3ce44SJohn Forte 
2939fcf3ce44SJohn Forte /*
2940fcf3ce44SJohn Forte  * Modify discovery attributes
2941fcf3ce44SJohn Forte  */
2942fcf3ce44SJohn Forte static int
2943fcf3ce44SJohn Forte modifyDiscovery(cmdOptions_t *options, int *funcRet)
2944fcf3ce44SJohn Forte {
2945fcf3ce44SJohn Forte 	IMA_OID oid;
2946fcf3ce44SJohn Forte 	IMA_STATUS status;
2947fcf3ce44SJohn Forte 	IMA_BOOL setDiscovery;
2948fcf3ce44SJohn Forte 	IMA_HOST_ID hostId;
2949fcf3ce44SJohn Forte 
2950fcf3ce44SJohn Forte 	int ret;
2951fcf3ce44SJohn Forte 	cmdOptions_t *optionList = options;
2952fcf3ce44SJohn Forte 
2953fcf3ce44SJohn Forte 	assert(funcRet != NULL);
2954fcf3ce44SJohn Forte 
2955fcf3ce44SJohn Forte 	/* Find Sun initiator */
2956fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&oid);
2957fcf3ce44SJohn Forte 	if (ret > 0) {
2958fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
2959fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
2960fcf3ce44SJohn Forte 	}
2961fcf3ce44SJohn Forte 
2962fcf3ce44SJohn Forte 	if (ret != 0) {
2963fcf3ce44SJohn Forte 		return (ret);
2964fcf3ce44SJohn Forte 	}
2965fcf3ce44SJohn Forte 
2966fcf3ce44SJohn Forte 	for (; optionList->optval; optionList++) {
2967fcf3ce44SJohn Forte 		/* check optarg and set bool accordingly */
2968fcf3ce44SJohn Forte 		if (strcasecmp(optionList->optarg, ISCSIADM_ARG_ENABLE) == 0) {
2969fcf3ce44SJohn Forte 			setDiscovery = IMA_TRUE;
2970fcf3ce44SJohn Forte 		} else if (strcasecmp(optionList->optarg, ISCSIADM_ARG_DISABLE)
2971fcf3ce44SJohn Forte 		    == 0) {
2972fcf3ce44SJohn Forte 			setDiscovery = IMA_FALSE;
2973fcf3ce44SJohn Forte 		} else {
2974fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
2975fcf3ce44SJohn Forte 			    cmdName, gettext("invalid option argument"));
2976fcf3ce44SJohn Forte 			return (1);
2977fcf3ce44SJohn Forte 		}
2978fcf3ce44SJohn Forte 
2979fcf3ce44SJohn Forte 		switch (optionList->optval) {
2980fcf3ce44SJohn Forte 			case 's':
2981fcf3ce44SJohn Forte 				/* Set static discovery */
2982fcf3ce44SJohn Forte 				status = IMA_SetStaticDiscovery(oid,
2983fcf3ce44SJohn Forte 				    setDiscovery);
2984fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
2985fcf3ce44SJohn Forte 					printLibError(status);
2986fcf3ce44SJohn Forte 					*funcRet = 1;
2987fcf3ce44SJohn Forte 					return (ret);
2988fcf3ce44SJohn Forte 				}
2989fcf3ce44SJohn Forte 				break;
2990fcf3ce44SJohn Forte 			case 't':
2991fcf3ce44SJohn Forte 				/* Set send targets discovery */
2992fcf3ce44SJohn Forte 				status = IMA_SetSendTargetsDiscovery(oid,
2993fcf3ce44SJohn Forte 				    setDiscovery);
2994fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
2995fcf3ce44SJohn Forte 					printLibError(status);
2996fcf3ce44SJohn Forte 					*funcRet = 1;
2997fcf3ce44SJohn Forte 					return (ret);
2998fcf3ce44SJohn Forte 				}
2999fcf3ce44SJohn Forte 				break;
3000fcf3ce44SJohn Forte 			case 'i':
3001fcf3ce44SJohn Forte 				/* Set iSNS discovery */
3002fcf3ce44SJohn Forte 				(void) memset(&hostId, 0, sizeof (hostId));
3003fcf3ce44SJohn Forte 				status = IMA_SetIsnsDiscovery(oid, setDiscovery,
3004fcf3ce44SJohn Forte 				    IMA_ISNS_DISCOVERY_METHOD_STATIC, &hostId);
3005fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
3006fcf3ce44SJohn Forte 					printLibError(status);
3007fcf3ce44SJohn Forte 					*funcRet = 1;
3008fcf3ce44SJohn Forte 					return (ret);
3009fcf3ce44SJohn Forte 				}
3010fcf3ce44SJohn Forte 				break;
3011fcf3ce44SJohn Forte 			default:
3012fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %c: %s\n",
3013fcf3ce44SJohn Forte 				    cmdName, optionList->optval,
3014fcf3ce44SJohn Forte 				    gettext("unknown option"));
3015fcf3ce44SJohn Forte 				return (1);
3016fcf3ce44SJohn Forte 		}
3017fcf3ce44SJohn Forte 	}
3018fcf3ce44SJohn Forte 
3019fcf3ce44SJohn Forte 	return (ret);
3020fcf3ce44SJohn Forte }
3021fcf3ce44SJohn Forte 
3022fcf3ce44SJohn Forte /*
3023fcf3ce44SJohn Forte  * Set the initiator node's authentication method
3024fcf3ce44SJohn Forte  */
3025fcf3ce44SJohn Forte static int
3026fcf3ce44SJohn Forte modifyNodeAuthParam(IMA_OID oid, int param, char *chapName, int *funcRet)
3027fcf3ce44SJohn Forte {
3028fcf3ce44SJohn Forte 	IMA_INITIATOR_AUTHPARMS authParams;
3029fcf3ce44SJohn Forte 	IMA_STATUS status;
3030fcf3ce44SJohn Forte 	int ret;
3031fcf3ce44SJohn Forte 	int secretLen = MAX_CHAP_SECRET_LEN;
3032fcf3ce44SJohn Forte 
3033fcf3ce44SJohn Forte 	IMA_BYTE chapSecret[MAX_CHAP_SECRET_LEN + 1];
3034fcf3ce44SJohn Forte 
3035fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3036fcf3ce44SJohn Forte 
3037fcf3ce44SJohn Forte 	/*
3038fcf3ce44SJohn Forte 	 * Start with existing parameters and modify with the desired change
3039fcf3ce44SJohn Forte 	 * before passing along.  We ignore any failures as they probably
3040fcf3ce44SJohn Forte 	 * are caused by non-existence of auth params for the given node.
3041fcf3ce44SJohn Forte 	 */
3042fcf3ce44SJohn Forte 	status = IMA_GetInitiatorAuthParms(oid, IMA_AUTHMETHOD_CHAP,
3043fcf3ce44SJohn Forte 	    &authParams);
3044fcf3ce44SJohn Forte 
3045fcf3ce44SJohn Forte 	switch (param) {
3046fcf3ce44SJohn Forte 	case AUTH_NAME:
3047fcf3ce44SJohn Forte 		if (chapName == NULL) {
3048fcf3ce44SJohn Forte 			(void) fprintf(stderr, "CHAP name cannot be NULL.\n");
3049fcf3ce44SJohn Forte 			return (1);
3050fcf3ce44SJohn Forte 		}
3051*129b3e6cSJack Meng 		if (strlen(chapName) == 0) {
3052*129b3e6cSJack Meng 			(void) fprintf(stderr, "CHAP name cannot be empty.\n");
3053*129b3e6cSJack Meng 			return (1);
3054*129b3e6cSJack Meng 		}
3055fcf3ce44SJohn Forte 		(void) memset(&authParams.chapParms.name, 0,
3056fcf3ce44SJohn Forte 		    sizeof (authParams.chapParms.name));
3057fcf3ce44SJohn Forte 		(void) memcpy(&authParams.chapParms.name,
3058fcf3ce44SJohn Forte 		    &chapName[0], strlen(chapName));
3059fcf3ce44SJohn Forte 		authParams.chapParms.nameLength = strlen(chapName);
3060fcf3ce44SJohn Forte 		break;
3061fcf3ce44SJohn Forte 
3062fcf3ce44SJohn Forte 	case AUTH_PASSWORD :
3063fcf3ce44SJohn Forte 		ret = getSecret((char *)&chapSecret[0], &secretLen,
3064fcf3ce44SJohn Forte 		    MIN_CHAP_SECRET_LEN, MAX_CHAP_SECRET_LEN);
3065fcf3ce44SJohn Forte 
3066fcf3ce44SJohn Forte 		if (ret != 0) {
3067fcf3ce44SJohn Forte 			return (ret);
3068fcf3ce44SJohn Forte 		}
3069fcf3ce44SJohn Forte 
3070fcf3ce44SJohn Forte 		(void) memset(&authParams.chapParms.challengeSecret, 0,
3071fcf3ce44SJohn Forte 		    sizeof (authParams.chapParms.challengeSecret));
3072fcf3ce44SJohn Forte 		(void) memcpy(&authParams.chapParms.challengeSecret,
3073fcf3ce44SJohn Forte 		    &chapSecret[0], secretLen);
3074fcf3ce44SJohn Forte 		authParams.chapParms.challengeSecretLength = secretLen;
3075fcf3ce44SJohn Forte 		break;
3076fcf3ce44SJohn Forte 
3077fcf3ce44SJohn Forte 	default:
3078fcf3ce44SJohn Forte 		(void) fprintf(stderr, "Invalid auth parameter %d\n", param);
3079fcf3ce44SJohn Forte 		return (1);
3080fcf3ce44SJohn Forte 	}
3081fcf3ce44SJohn Forte 
3082fcf3ce44SJohn Forte 	status = IMA_SetInitiatorAuthParms(oid, IMA_AUTHMETHOD_CHAP,
3083fcf3ce44SJohn Forte 	    &authParams);
3084fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3085fcf3ce44SJohn Forte 		printLibError(status);
3086fcf3ce44SJohn Forte 		*funcRet = 1;
3087fcf3ce44SJohn Forte 	}
3088fcf3ce44SJohn Forte 	return (ret);
3089fcf3ce44SJohn Forte }
3090fcf3ce44SJohn Forte 
3091fcf3ce44SJohn Forte /*
3092fcf3ce44SJohn Forte  * Set the target's authentication method
3093fcf3ce44SJohn Forte  */
3094fcf3ce44SJohn Forte static int
3095fcf3ce44SJohn Forte modifyTargetAuthParam(IMA_OID oid, int param, char *chapName, int *funcRet)
3096fcf3ce44SJohn Forte {
3097fcf3ce44SJohn Forte 	IMA_INITIATOR_AUTHPARMS authParams;
3098fcf3ce44SJohn Forte 	IMA_STATUS status;
3099fcf3ce44SJohn Forte 	int ret;
3100fcf3ce44SJohn Forte 	int secretLen = MAX_CHAP_SECRET_LEN;
3101fcf3ce44SJohn Forte 
3102fcf3ce44SJohn Forte 	IMA_BYTE chapSecret[MAX_CHAP_SECRET_LEN + 1];
3103fcf3ce44SJohn Forte 
3104fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3105fcf3ce44SJohn Forte 
3106fcf3ce44SJohn Forte 	/*
3107fcf3ce44SJohn Forte 	 * Start with existing parameters and modify with the desired change
3108fcf3ce44SJohn Forte 	 * before passing along.  We ignore any get failures as they probably
3109fcf3ce44SJohn Forte 	 * are caused by non-existence of auth params for the given target.
3110fcf3ce44SJohn Forte 	 */
3111fcf3ce44SJohn Forte 	status = SUN_IMA_GetTargetAuthParms(oid, IMA_AUTHMETHOD_CHAP,
3112fcf3ce44SJohn Forte 	    &authParams);
3113fcf3ce44SJohn Forte 
3114fcf3ce44SJohn Forte 	switch (param) {
3115fcf3ce44SJohn Forte 	case AUTH_NAME:
3116fcf3ce44SJohn Forte 		if (chapName == NULL) {
3117fcf3ce44SJohn Forte 			(void) fprintf(stderr, "CHAP name cannot be NULL.\n");
3118fcf3ce44SJohn Forte 			return (1);
3119fcf3ce44SJohn Forte 		}
3120*129b3e6cSJack Meng 		if (strlen(chapName) == 0) {
3121*129b3e6cSJack Meng 			(void) fprintf(stderr, "CHAP name cannot be empty.\n");
3122*129b3e6cSJack Meng 			return (1);
3123*129b3e6cSJack Meng 		}
3124fcf3ce44SJohn Forte 		(void) memset(&authParams.chapParms.name, 0,
3125fcf3ce44SJohn Forte 		    sizeof (authParams.chapParms.name));
3126fcf3ce44SJohn Forte 		(void) memcpy(&authParams.chapParms.name,
3127fcf3ce44SJohn Forte 		    &chapName[0], strlen(chapName));
3128fcf3ce44SJohn Forte 		authParams.chapParms.nameLength = strlen(chapName);
3129fcf3ce44SJohn Forte 		break;
3130fcf3ce44SJohn Forte 
3131fcf3ce44SJohn Forte 	case AUTH_PASSWORD :
3132fcf3ce44SJohn Forte 		ret = getSecret((char *)&chapSecret[0], &secretLen,
3133fcf3ce44SJohn Forte 		    1, MAX_CHAP_SECRET_LEN);
3134fcf3ce44SJohn Forte 
3135fcf3ce44SJohn Forte 		if (ret != 0) {
3136fcf3ce44SJohn Forte 			return (ret);
3137fcf3ce44SJohn Forte 		}
3138fcf3ce44SJohn Forte 
3139fcf3ce44SJohn Forte 		(void) memset(&authParams.chapParms.challengeSecret, 0,
3140fcf3ce44SJohn Forte 		    sizeof (authParams.chapParms.challengeSecret));
3141fcf3ce44SJohn Forte 		(void) memcpy(&authParams.chapParms.challengeSecret,
3142fcf3ce44SJohn Forte 		    &chapSecret[0], secretLen);
3143fcf3ce44SJohn Forte 		authParams.chapParms.challengeSecretLength = secretLen;
3144fcf3ce44SJohn Forte 		break;
3145fcf3ce44SJohn Forte 
3146fcf3ce44SJohn Forte 	default:
3147fcf3ce44SJohn Forte 		(void) fprintf(stderr, "Invalid auth parameter %d\n", param);
3148fcf3ce44SJohn Forte 		return (1);
3149fcf3ce44SJohn Forte 	}
3150fcf3ce44SJohn Forte 
3151fcf3ce44SJohn Forte 	status = SUN_IMA_SetTargetAuthParams(oid, IMA_AUTHMETHOD_CHAP,
3152fcf3ce44SJohn Forte 	    &authParams);
3153fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3154fcf3ce44SJohn Forte 		printLibError(status);
3155fcf3ce44SJohn Forte 		*funcRet = 1;
3156fcf3ce44SJohn Forte 	}
3157fcf3ce44SJohn Forte 	return (0);
3158fcf3ce44SJohn Forte }
3159fcf3ce44SJohn Forte 
3160fcf3ce44SJohn Forte static int
3161fcf3ce44SJohn Forte modifyTargetBidirAuthFlag(IMA_OID targetOid, char *optarg, int *funcRet)
3162fcf3ce44SJohn Forte {
3163fcf3ce44SJohn Forte 	IMA_BOOL boolValue;
3164fcf3ce44SJohn Forte 	IMA_STATUS status;
3165fcf3ce44SJohn Forte 
3166fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3167fcf3ce44SJohn Forte 
3168fcf3ce44SJohn Forte 	if (strcasecmp(optarg, ISCSIADM_ARG_ENABLE) == 0) {
3169fcf3ce44SJohn Forte 		boolValue = IMA_TRUE;
3170fcf3ce44SJohn Forte 	} else if (strcasecmp(optarg, ISCSIADM_ARG_DISABLE) == 0) {
3171fcf3ce44SJohn Forte 		boolValue = IMA_FALSE;
3172fcf3ce44SJohn Forte 	} else {
3173fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s %s\n",
3174fcf3ce44SJohn Forte 		    cmdName, gettext("invalid option argument"), optarg);
3175fcf3ce44SJohn Forte 		return (1);
3176fcf3ce44SJohn Forte 	}
3177fcf3ce44SJohn Forte 
3178fcf3ce44SJohn Forte 	status = SUN_IMA_SetTargetBidirAuthFlag(targetOid, &boolValue);
3179fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3180fcf3ce44SJohn Forte 		printLibError(status);
3181fcf3ce44SJohn Forte 		*funcRet = 1;
3182fcf3ce44SJohn Forte 	}
3183fcf3ce44SJohn Forte 	return (0);
3184fcf3ce44SJohn Forte }
3185fcf3ce44SJohn Forte 
3186fcf3ce44SJohn Forte static int
3187fcf3ce44SJohn Forte modifyConfiguredSessions(IMA_OID targetOid, char *optarg)
3188fcf3ce44SJohn Forte {
3189fcf3ce44SJohn Forte 	SUN_IMA_CONFIG_SESSIONS *pConfigSessions;
3190fcf3ce44SJohn Forte 	IMA_STATUS		status;
3191fcf3ce44SJohn Forte 	int			sessions;
3192fcf3ce44SJohn Forte 	int			size;
3193fcf3ce44SJohn Forte 	char			tmp[1024];
3194fcf3ce44SJohn Forte 	boolean_t		isIpv6 = B_FALSE;
3195fcf3ce44SJohn Forte 	uint16_t		port;
3196fcf3ce44SJohn Forte 	char			address[MAX_ADDRESS_LEN];
3197fcf3ce44SJohn Forte 	char			*commaPos;
3198fcf3ce44SJohn Forte 	char			*addressPos;
3199fcf3ce44SJohn Forte 	int			rtn;
3200fcf3ce44SJohn Forte 
3201fcf3ce44SJohn Forte 	/*
3202fcf3ce44SJohn Forte 	 * Strip the first int value from the string.  If we sprintf
3203fcf3ce44SJohn Forte 	 * this back to a string and it matches the original string
3204fcf3ce44SJohn Forte 	 * then this command is using default binding.  If not a
3205fcf3ce44SJohn Forte 	 * match we have hard coded binding or a usage error.
3206fcf3ce44SJohn Forte 	 */
3207fcf3ce44SJohn Forte 	sessions = atoi(optarg);
3208fcf3ce44SJohn Forte 	(void) sprintf(tmp, "%d", sessions);
3209fcf3ce44SJohn Forte 	if (strcmp(optarg, tmp) == 0) {
3210fcf3ce44SJohn Forte 		/* default binding */
3211fcf3ce44SJohn Forte 
3212fcf3ce44SJohn Forte 		/* allocate the required pConfigSessions */
3213fcf3ce44SJohn Forte 		size = sizeof (SUN_IMA_CONFIG_SESSIONS);
3214fcf3ce44SJohn Forte 		pConfigSessions = (SUN_IMA_CONFIG_SESSIONS *)calloc(1, size);
3215fcf3ce44SJohn Forte 		if (pConfigSessions == NULL) {
3216fcf3ce44SJohn Forte 			return (1);
3217fcf3ce44SJohn Forte 		}
3218fcf3ce44SJohn Forte 
3219fcf3ce44SJohn Forte 		/* setup pConfigSessions */
3220fcf3ce44SJohn Forte 		pConfigSessions->bound	= IMA_FALSE;
3221fcf3ce44SJohn Forte 		pConfigSessions->in	= sessions;
3222fcf3ce44SJohn Forte 		pConfigSessions->out	= 0;
3223fcf3ce44SJohn Forte 	} else {
3224fcf3ce44SJohn Forte 		/* hardcoded binding */
3225fcf3ce44SJohn Forte 
3226fcf3ce44SJohn Forte 		/*
3227fcf3ce44SJohn Forte 		 * First we need to determine how many bindings
3228fcf3ce44SJohn Forte 		 * are available.  This can be done by scanning
3229fcf3ce44SJohn Forte 		 * for the number of ',' + 1.
3230fcf3ce44SJohn Forte 		 */
3231fcf3ce44SJohn Forte 		sessions = 1;
3232fcf3ce44SJohn Forte 		commaPos = strchr(optarg, ',');
3233fcf3ce44SJohn Forte 		while (commaPos != NULL) {
3234fcf3ce44SJohn Forte 			sessions++;
3235fcf3ce44SJohn Forte 			commaPos = strchr(++commaPos, ',');
3236fcf3ce44SJohn Forte 		}
3237fcf3ce44SJohn Forte 
3238fcf3ce44SJohn Forte 		/* allocate the required pConfigSessions */
3239fcf3ce44SJohn Forte 		size = sizeof (SUN_IMA_CONFIG_SESSIONS) + ((sessions - 1) *
3240fcf3ce44SJohn Forte 		    sizeof (IMA_ADDRESS_KEY));
3241fcf3ce44SJohn Forte 		pConfigSessions = (SUN_IMA_CONFIG_SESSIONS *)calloc(1, size);
3242fcf3ce44SJohn Forte 		if (pConfigSessions == NULL) {
3243fcf3ce44SJohn Forte 			return (1);
3244fcf3ce44SJohn Forte 		}
3245fcf3ce44SJohn Forte 
3246fcf3ce44SJohn Forte 		/* setup pConfigSessions */
3247fcf3ce44SJohn Forte 		pConfigSessions->bound	= IMA_TRUE;
3248fcf3ce44SJohn Forte 		pConfigSessions->in	= sessions;
3249fcf3ce44SJohn Forte 		pConfigSessions->out	= 0;
3250fcf3ce44SJohn Forte 
3251fcf3ce44SJohn Forte 		/* Now fill in the binding information.  */
3252fcf3ce44SJohn Forte 		sessions = 0;
3253fcf3ce44SJohn Forte 		addressPos = optarg;
3254fcf3ce44SJohn Forte 		/*
3255fcf3ce44SJohn Forte 		 * Walk thru possible address strings
3256fcf3ce44SJohn Forte 		 * stop once all strings are processed.
3257fcf3ce44SJohn Forte 		 */
3258fcf3ce44SJohn Forte 		while (addressPos != NULL) {
3259fcf3ce44SJohn Forte 			/*
3260fcf3ce44SJohn Forte 			 * Check if there is another address after this
3261fcf3ce44SJohn Forte 			 * one. If so terminate the current address and
3262fcf3ce44SJohn Forte 			 * keep a pointer to the next one.
3263fcf3ce44SJohn Forte 			 */
3264fcf3ce44SJohn Forte 			commaPos = strchr(addressPos, ',');
3265fcf3ce44SJohn Forte 			if (commaPos != NULL) {
3266fcf3ce44SJohn Forte 				*commaPos++ = 0x00;
3267fcf3ce44SJohn Forte 			}
3268fcf3ce44SJohn Forte 
3269fcf3ce44SJohn Forte 			/*
3270fcf3ce44SJohn Forte 			 * Parse current address.  If invalid abort
3271fcf3ce44SJohn Forte 			 * processing of addresses and free memory.
3272fcf3ce44SJohn Forte 			 */
3273fcf3ce44SJohn Forte 			if (parseAddress(addressPos, 0, address,
3274fcf3ce44SJohn Forte 			    MAX_ADDRESS_LEN, &port, &isIpv6) != PARSE_ADDR_OK) {
3275fcf3ce44SJohn Forte 				free(pConfigSessions);
3276fcf3ce44SJohn Forte 				printLibError(IMA_ERROR_INVALID_PARAMETER);
3277fcf3ce44SJohn Forte 				return (1);
3278fcf3ce44SJohn Forte 			}
3279fcf3ce44SJohn Forte 
3280fcf3ce44SJohn Forte 			/* Convert address into binary form */
3281fcf3ce44SJohn Forte 			if (isIpv6 == B_FALSE) {
3282fcf3ce44SJohn Forte 				pConfigSessions->bindings[sessions].
3283fcf3ce44SJohn Forte 				    ipAddress.ipv4Address = IMA_TRUE;
3284fcf3ce44SJohn Forte 				rtn = inet_pton(AF_INET, address,
3285fcf3ce44SJohn Forte 				    pConfigSessions->bindings[sessions].
3286fcf3ce44SJohn Forte 				    ipAddress.ipAddress);
3287fcf3ce44SJohn Forte 			} else {
3288fcf3ce44SJohn Forte 				pConfigSessions->bindings[sessions].ipAddress.
3289fcf3ce44SJohn Forte 				    ipv4Address =
3290fcf3ce44SJohn Forte 				    IMA_FALSE;
3291fcf3ce44SJohn Forte 				rtn = inet_pton(AF_INET6, address,
3292fcf3ce44SJohn Forte 				    pConfigSessions->bindings[sessions].
3293fcf3ce44SJohn Forte 				    ipAddress.ipAddress);
3294fcf3ce44SJohn Forte 			}
3295fcf3ce44SJohn Forte 			if (rtn == 0) {
3296fcf3ce44SJohn Forte 				/* inet_pton found address invalid */
3297fcf3ce44SJohn Forte 				free(pConfigSessions);
3298fcf3ce44SJohn Forte 				printLibError(IMA_ERROR_INVALID_PARAMETER);
3299fcf3ce44SJohn Forte 				return (1);
3300fcf3ce44SJohn Forte 			}
3301fcf3ce44SJohn Forte 
3302fcf3ce44SJohn Forte 			/* update addressPos to next address */
3303fcf3ce44SJohn Forte 			sessions++;
3304fcf3ce44SJohn Forte 			addressPos = commaPos;
3305fcf3ce44SJohn Forte 		}
3306fcf3ce44SJohn Forte 	}
3307fcf3ce44SJohn Forte 
3308fcf3ce44SJohn Forte 	/* issue SUN_IMA request */
3309fcf3ce44SJohn Forte 	status = SUN_IMA_SetConfigSessions(targetOid,
3310fcf3ce44SJohn Forte 	    pConfigSessions);
3311fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3312fcf3ce44SJohn Forte 		printLibError(status);
3313fcf3ce44SJohn Forte 		free(pConfigSessions);
3314fcf3ce44SJohn Forte 		return (1);
3315fcf3ce44SJohn Forte 	}
3316fcf3ce44SJohn Forte 
3317fcf3ce44SJohn Forte 	free(pConfigSessions);
3318fcf3ce44SJohn Forte 	return (0);
3319fcf3ce44SJohn Forte }
3320fcf3ce44SJohn Forte 
3321fcf3ce44SJohn Forte static int
3322fcf3ce44SJohn Forte getAuthMethodValue(char *method, IMA_AUTHMETHOD *value)
3323fcf3ce44SJohn Forte {
3324fcf3ce44SJohn Forte 	if (strcasecmp(method, "chap") == 0) {
3325fcf3ce44SJohn Forte 		*value = IMA_AUTHMETHOD_CHAP;
3326fcf3ce44SJohn Forte 		return (0);
3327fcf3ce44SJohn Forte 	}
3328fcf3ce44SJohn Forte 
3329fcf3ce44SJohn Forte 	if (strcasecmp(method, "none") == 0) {
3330fcf3ce44SJohn Forte 		*value =  IMA_AUTHMETHOD_NONE;
3331fcf3ce44SJohn Forte 		return (0);
3332fcf3ce44SJohn Forte 	}
3333fcf3ce44SJohn Forte 
3334fcf3ce44SJohn Forte 	return (1);
3335fcf3ce44SJohn Forte }
3336fcf3ce44SJohn Forte 
3337fcf3ce44SJohn Forte 
3338fcf3ce44SJohn Forte /*
3339fcf3ce44SJohn Forte  * Set the authentication method
3340fcf3ce44SJohn Forte  * Currently only supports CHAP and NONE
3341fcf3ce44SJohn Forte  */
3342fcf3ce44SJohn Forte static int
3343fcf3ce44SJohn Forte modifyNodeAuthMethod(IMA_OID oid, char *optarg, int *funcRet)
3344fcf3ce44SJohn Forte {
3345fcf3ce44SJohn Forte 	IMA_AUTHMETHOD methodList[MAX_AUTH_METHODS];
3346fcf3ce44SJohn Forte 	IMA_UINT methodCount = 0;
3347fcf3ce44SJohn Forte 	IMA_STATUS status;
3348fcf3ce44SJohn Forte 	IMA_AUTHMETHOD value;
3349fcf3ce44SJohn Forte 	char *method;
3350fcf3ce44SJohn Forte 	char *commaPos;
3351fcf3ce44SJohn Forte 
3352fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3353fcf3ce44SJohn Forte 
3354fcf3ce44SJohn Forte 	/*
3355fcf3ce44SJohn Forte 	 * optarg will be a , delimited set of auth methods, in order
3356fcf3ce44SJohn Forte 	 * of preference
3357fcf3ce44SJohn Forte 	 * if any values here are incorrect, return without setting
3358fcf3ce44SJohn Forte 	 * anything.
3359fcf3ce44SJohn Forte 	 */
3360fcf3ce44SJohn Forte 	method = optarg;
3361fcf3ce44SJohn Forte 
3362fcf3ce44SJohn Forte 	commaPos = strchr(optarg, ',');
3363fcf3ce44SJohn Forte 
3364fcf3ce44SJohn Forte 	while (commaPos && methodCount < MAX_AUTH_METHODS) {
3365fcf3ce44SJohn Forte 		*commaPos = NULL;
3366fcf3ce44SJohn Forte 		if (getAuthMethodValue(method, &value) != 0) {
3367fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: a: %s\n",
3368fcf3ce44SJohn Forte 			    cmdName, gettext("invalid option argument"));
3369fcf3ce44SJohn Forte 			return (1);
3370fcf3ce44SJohn Forte 		}
3371fcf3ce44SJohn Forte 		methodList[methodCount++] = value;
3372fcf3ce44SJohn Forte 		commaPos++;
3373fcf3ce44SJohn Forte 		method = commaPos;
3374fcf3ce44SJohn Forte 		commaPos = strchr(method, ',');
3375fcf3ce44SJohn Forte 	}
3376fcf3ce44SJohn Forte 	/* Should not find more method specified - if found, error */
3377fcf3ce44SJohn Forte 	if (commaPos) {
3378fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: -a: %s\n",
3379fcf3ce44SJohn Forte 		    cmdName, gettext("invalid option argument"));
3380fcf3ce44SJohn Forte 		return (1);
3381fcf3ce44SJohn Forte 	}
3382fcf3ce44SJohn Forte 	if (getAuthMethodValue(method, &value) != 0) {
3383fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: -a: %s\n",
3384fcf3ce44SJohn Forte 		    cmdName, gettext("invalid option argument"));
3385fcf3ce44SJohn Forte 		return (1);
3386fcf3ce44SJohn Forte 	}
3387fcf3ce44SJohn Forte 	methodList[methodCount++] = value;
3388fcf3ce44SJohn Forte 
3389fcf3ce44SJohn Forte 	status = IMA_SetInitiatorAuthMethods(oid, methodCount, &methodList[0]);
3390fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3391fcf3ce44SJohn Forte 		printLibError(status);
3392fcf3ce44SJohn Forte 		*funcRet = 1;
3393fcf3ce44SJohn Forte 	}
3394fcf3ce44SJohn Forte 	return (0);
3395fcf3ce44SJohn Forte }
3396fcf3ce44SJohn Forte 
3397fcf3ce44SJohn Forte static int
3398fcf3ce44SJohn Forte modifyTargetAuthMethod(IMA_OID oid, char *optarg, int *funcRet)
3399fcf3ce44SJohn Forte {
3400fcf3ce44SJohn Forte 	IMA_AUTHMETHOD methodList[MAX_AUTH_METHODS];
3401fcf3ce44SJohn Forte 	IMA_UINT methodCount = 0;
3402fcf3ce44SJohn Forte 	IMA_STATUS status;
3403fcf3ce44SJohn Forte 	IMA_AUTHMETHOD value;
3404fcf3ce44SJohn Forte 	char *method;
3405fcf3ce44SJohn Forte 	char *commaPos;
3406fcf3ce44SJohn Forte 
3407fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3408fcf3ce44SJohn Forte 
3409fcf3ce44SJohn Forte 	/*
3410fcf3ce44SJohn Forte 	 * optarg will be a , delimited set of auth methods, in order
3411fcf3ce44SJohn Forte 	 * of preference
3412fcf3ce44SJohn Forte 	 * if any values here are incorrect, return without setting
3413fcf3ce44SJohn Forte 	 * anything.
3414fcf3ce44SJohn Forte 	 */
3415fcf3ce44SJohn Forte 	method = optarg;
3416fcf3ce44SJohn Forte 
3417fcf3ce44SJohn Forte 	commaPos = strchr(optarg, ',');
3418fcf3ce44SJohn Forte 
3419fcf3ce44SJohn Forte 	while (commaPos && methodCount < MAX_AUTH_METHODS) {
3420fcf3ce44SJohn Forte 		*commaPos = NULL;
3421fcf3ce44SJohn Forte 		if (getAuthMethodValue(method, &value) != 0) {
3422fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: a: %s\n",
3423fcf3ce44SJohn Forte 			    cmdName, gettext("invalid option argument"));
3424fcf3ce44SJohn Forte 			return (1);
3425fcf3ce44SJohn Forte 		}
3426fcf3ce44SJohn Forte 		methodList[methodCount++] = value;
3427fcf3ce44SJohn Forte 		commaPos++;
3428fcf3ce44SJohn Forte 		method = commaPos;
3429fcf3ce44SJohn Forte 		commaPos = strchr(method, ',');
3430fcf3ce44SJohn Forte 	}
3431fcf3ce44SJohn Forte 	/* Should not find more method specified - if found, error */
3432fcf3ce44SJohn Forte 	if (commaPos) {
3433fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: -a: %s\n",
3434fcf3ce44SJohn Forte 		    cmdName, gettext("invalid option argument"));
3435fcf3ce44SJohn Forte 		return (1);
3436fcf3ce44SJohn Forte 	}
3437fcf3ce44SJohn Forte 	if (getAuthMethodValue(method, &value) != 0) {
3438fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: -a: %s\n",
3439fcf3ce44SJohn Forte 		    cmdName, gettext("invalid option argument"));
3440fcf3ce44SJohn Forte 		return (1);
3441fcf3ce44SJohn Forte 	}
3442fcf3ce44SJohn Forte 	methodList[methodCount++] = value;
3443fcf3ce44SJohn Forte 
3444fcf3ce44SJohn Forte 	status = SUN_IMA_SetTargetAuthMethods(oid, &methodCount,
3445fcf3ce44SJohn Forte 	    &methodList[0]);
3446fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3447fcf3ce44SJohn Forte 		printLibError(status);
3448fcf3ce44SJohn Forte 		*funcRet = 1;
3449fcf3ce44SJohn Forte 	}
3450fcf3ce44SJohn Forte 	return (0);
3451fcf3ce44SJohn Forte }
3452fcf3ce44SJohn Forte 
3453fcf3ce44SJohn Forte /*
3454fcf3ce44SJohn Forte  * Modify the RADIUS configuration of the initiator node.
3455fcf3ce44SJohn Forte  *
3456fcf3ce44SJohn Forte  * Return 0 on success.
3457fcf3ce44SJohn Forte  */
3458fcf3ce44SJohn Forte static int
3459fcf3ce44SJohn Forte modifyNodeRadiusConfig(IMA_OID oid, char *optarg, int *funcRet)
3460fcf3ce44SJohn Forte {
3461fcf3ce44SJohn Forte 	SUN_IMA_RADIUS_CONFIG config;
3462fcf3ce44SJohn Forte 	IMA_STATUS status;
3463fcf3ce44SJohn Forte 	boolean_t isIpv6 = B_FALSE;
3464fcf3ce44SJohn Forte 	uint16_t port;
3465fcf3ce44SJohn Forte 
3466fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3467fcf3ce44SJohn Forte 
3468fcf3ce44SJohn Forte 	(void) memset(&config, 0, sizeof (SUN_IMA_RADIUS_CONFIG));
3469fcf3ce44SJohn Forte 	if (parseAddress(optarg, DEFAULT_RADIUS_PORT,
3470fcf3ce44SJohn Forte 	    &config.hostnameIpAddress[0], SUN_IMA_IP_ADDRESS_PORT_LEN,
3471fcf3ce44SJohn Forte 	    &port, &isIpv6) !=
3472fcf3ce44SJohn Forte 	    PARSE_ADDR_OK) {
3473fcf3ce44SJohn Forte 		return (1);
3474fcf3ce44SJohn Forte 	}
3475fcf3ce44SJohn Forte 	config.port = (IMA_UINT16)port;
3476fcf3ce44SJohn Forte 	config.isIpv6 = (isIpv6 == B_TRUE) ? IMA_TRUE : IMA_FALSE;
3477fcf3ce44SJohn Forte 	/* Not setting shared secret here. */
3478fcf3ce44SJohn Forte 	config.sharedSecretValid = IMA_FALSE;
3479fcf3ce44SJohn Forte 
3480fcf3ce44SJohn Forte 	status = SUN_IMA_SetInitiatorRadiusConfig(oid, &config);
3481fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3482fcf3ce44SJohn Forte 		printLibError(status);
3483fcf3ce44SJohn Forte 		*funcRet = 1;
3484fcf3ce44SJohn Forte 	}
3485fcf3ce44SJohn Forte 
3486fcf3ce44SJohn Forte 	return (0);
3487fcf3ce44SJohn Forte }
3488fcf3ce44SJohn Forte 
3489fcf3ce44SJohn Forte /*
3490fcf3ce44SJohn Forte  * Modify the RADIUS access flag of the initiator node.
3491fcf3ce44SJohn Forte  *
3492fcf3ce44SJohn Forte  * Return 0 on success.
3493fcf3ce44SJohn Forte  */
3494fcf3ce44SJohn Forte static int
3495fcf3ce44SJohn Forte modifyNodeRadiusAccess(IMA_OID oid, char *optarg, int *funcRet)
3496fcf3ce44SJohn Forte {
3497fcf3ce44SJohn Forte 	IMA_BOOL radiusAccess;
3498fcf3ce44SJohn Forte 	IMA_OID initiatorOid;
3499fcf3ce44SJohn Forte 	IMA_STATUS status;
3500fcf3ce44SJohn Forte 	SUN_IMA_RADIUS_CONFIG radiusConfig;
3501fcf3ce44SJohn Forte 	int ret;
3502fcf3ce44SJohn Forte 
3503fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3504fcf3ce44SJohn Forte 
3505fcf3ce44SJohn Forte 	/* Check if Radius Config is there */
3506fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
3507fcf3ce44SJohn Forte 	if (ret != 0) {
3508fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
3509fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
3510fcf3ce44SJohn Forte 		return (1);
3511fcf3ce44SJohn Forte 	}
3512fcf3ce44SJohn Forte 	(void) memset(&radiusConfig, 0, sizeof (SUN_IMA_RADIUS_CONFIG));
3513fcf3ce44SJohn Forte 	status = SUN_IMA_GetInitiatorRadiusConfig(initiatorOid, &radiusConfig);
3514fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3515fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
3516fcf3ce44SJohn Forte 		    cmdName, gettext("RADIUS server not configured yet"));
3517fcf3ce44SJohn Forte 		*funcRet = 1;
3518fcf3ce44SJohn Forte 		return (ret);
3519fcf3ce44SJohn Forte 	}
3520fcf3ce44SJohn Forte 
3521fcf3ce44SJohn Forte 	/* Check if Radius Shared is set */
3522fcf3ce44SJohn Forte 	if (radiusConfig.sharedSecretValid == IMA_FALSE) {
3523fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n", cmdName,
3524fcf3ce44SJohn Forte 		    gettext("RADIUS server secret not configured yet"));
3525fcf3ce44SJohn Forte 		return (1);
3526fcf3ce44SJohn Forte 	}
3527fcf3ce44SJohn Forte 
3528fcf3ce44SJohn Forte 	if (strcasecmp(optarg, ISCSIADM_ARG_ENABLE) == 0) {
3529fcf3ce44SJohn Forte 		radiusAccess = IMA_TRUE;
3530fcf3ce44SJohn Forte 	} else if (strcasecmp(optarg, ISCSIADM_ARG_DISABLE) == 0) {
3531fcf3ce44SJohn Forte 		radiusAccess = IMA_FALSE;
3532fcf3ce44SJohn Forte 	} else {
3533fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s %s\n",
3534fcf3ce44SJohn Forte 		    cmdName,
3535fcf3ce44SJohn Forte 		    gettext("invalid option argument"),
3536fcf3ce44SJohn Forte 		    optarg);
3537fcf3ce44SJohn Forte 		return (1);
3538fcf3ce44SJohn Forte 	}
3539fcf3ce44SJohn Forte 	status = SUN_IMA_SetInitiatorRadiusAccess(oid, radiusAccess);
3540fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3541fcf3ce44SJohn Forte 		printLibError(status);
3542fcf3ce44SJohn Forte 		*funcRet = 1;
3543fcf3ce44SJohn Forte 	}
3544fcf3ce44SJohn Forte 
3545fcf3ce44SJohn Forte 	return (ret);
3546fcf3ce44SJohn Forte }
3547fcf3ce44SJohn Forte 
3548fcf3ce44SJohn Forte /*
3549fcf3ce44SJohn Forte  * Modify the RADIUS shared secret.
3550fcf3ce44SJohn Forte  *
3551fcf3ce44SJohn Forte  * Returns:
3552fcf3ce44SJohn Forte  *  zero on success.
3553fcf3ce44SJohn Forte  *  > 0 on failure.
3554fcf3ce44SJohn Forte  */
3555fcf3ce44SJohn Forte static int
3556fcf3ce44SJohn Forte modifyNodeRadiusSharedSecret(IMA_OID oid, int *funcRet)
3557fcf3ce44SJohn Forte {
3558fcf3ce44SJohn Forte 	IMA_BYTE radiusSharedSecret[SUN_IMA_MAX_RADIUS_SECRET_LEN + 1];
3559fcf3ce44SJohn Forte 	IMA_OID initiatorOid;
3560fcf3ce44SJohn Forte 	IMA_STATUS status;
3561fcf3ce44SJohn Forte 	SUN_IMA_RADIUS_CONFIG radiusConfig;
3562fcf3ce44SJohn Forte 	int ret;
3563fcf3ce44SJohn Forte 	int secretLen = SUN_IMA_MAX_RADIUS_SECRET_LEN;
3564fcf3ce44SJohn Forte 
3565fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3566fcf3ce44SJohn Forte 
3567fcf3ce44SJohn Forte 	ret = getSecret((char *)&radiusSharedSecret[0], &secretLen,
3568fcf3ce44SJohn Forte 	    0, SUN_IMA_MAX_RADIUS_SECRET_LEN);
3569fcf3ce44SJohn Forte 	if (ret != 0) {
3570fcf3ce44SJohn Forte 		return (1);
3571fcf3ce44SJohn Forte 	}
3572fcf3ce44SJohn Forte 
3573fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
3574fcf3ce44SJohn Forte 	if (ret > 0) {
3575fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
3576fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
3577fcf3ce44SJohn Forte 	}
3578fcf3ce44SJohn Forte 	if (ret != 0) {
3579fcf3ce44SJohn Forte 		return (1);
3580fcf3ce44SJohn Forte 	}
3581fcf3ce44SJohn Forte 	/* First obtain existing RADIUS configuration (if any) */
3582fcf3ce44SJohn Forte 	(void) memset(&radiusConfig, 0, sizeof (SUN_IMA_RADIUS_CONFIG));
3583fcf3ce44SJohn Forte 	status = SUN_IMA_GetInitiatorRadiusConfig(initiatorOid, &radiusConfig);
3584fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3585fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
3586fcf3ce44SJohn Forte 		    cmdName, gettext("RADIUS server not configured yet"));
3587fcf3ce44SJohn Forte 		return (1);
3588fcf3ce44SJohn Forte 	}
3589fcf3ce44SJohn Forte 
3590fcf3ce44SJohn Forte 	/* Modify the shared secret only */
3591fcf3ce44SJohn Forte 	radiusConfig.sharedSecretLength = secretLen;
3592fcf3ce44SJohn Forte 	(void) memcpy(&radiusConfig.sharedSecret,
3593fcf3ce44SJohn Forte 	    &radiusSharedSecret[0], secretLen);
3594fcf3ce44SJohn Forte 	radiusConfig.sharedSecretValid = IMA_TRUE;
3595fcf3ce44SJohn Forte 	status = SUN_IMA_SetInitiatorRadiusConfig(oid, &radiusConfig);
3596fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3597fcf3ce44SJohn Forte 		printLibError(status);
3598fcf3ce44SJohn Forte 		*funcRet = 1;
3599fcf3ce44SJohn Forte 	}
3600fcf3ce44SJohn Forte 
3601fcf3ce44SJohn Forte 	return (0);
3602fcf3ce44SJohn Forte }
3603fcf3ce44SJohn Forte 
3604fcf3ce44SJohn Forte /*
3605fcf3ce44SJohn Forte  * Set initiator node attributes.
3606fcf3ce44SJohn Forte  */
3607fcf3ce44SJohn Forte static int
3608fcf3ce44SJohn Forte modifyNode(cmdOptions_t *options, int *funcRet)
3609fcf3ce44SJohn Forte {
3610fcf3ce44SJohn Forte 	IMA_NODE_NAME	nodeName;
3611fcf3ce44SJohn Forte 	IMA_NODE_ALIAS	nodeAlias;
3612fcf3ce44SJohn Forte 	IMA_OID		oid;
3613fcf3ce44SJohn Forte 	IMA_STATUS	status;
3614fcf3ce44SJohn Forte 	cmdOptions_t	*optionList = options;
3615fcf3ce44SJohn Forte 	int		ret;
3616fcf3ce44SJohn Forte 	iSCSINameCheckStatusType nameCheckStatus;
3617fcf3ce44SJohn Forte 	IMA_OID sharedNodeOid;
3618fcf3ce44SJohn Forte 	int 		i;
3619fcf3ce44SJohn Forte 	int 		lowerCase;
36206cefaae1SJack Meng 	IMA_BOOL	iscsiBoot = IMA_FALSE;
36216cefaae1SJack Meng 	IMA_BOOL	mpxioEnabled = IMA_FALSE;
36227f848965Sbing zhao - Sun Microsystems - Beijing China 	char		*mb_name = NULL;
36237f848965Sbing zhao - Sun Microsystems - Beijing China 	int		prefixlen = 0;
3624fcf3ce44SJohn Forte 
3625fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3626fcf3ce44SJohn Forte 
36276cefaae1SJack Meng 	/* Get boot session's info */
36286cefaae1SJack Meng 	(void) SUN_IMA_GetBootIscsi(&iscsiBoot);
36296cefaae1SJack Meng 	if (iscsiBoot == IMA_TRUE) {
36306cefaae1SJack Meng 		status = SUN_IMA_GetBootMpxio(&mpxioEnabled);
36316cefaae1SJack Meng 		if (!IMA_SUCCESS(status)) {
36326cefaae1SJack Meng 			(void) fprintf(stderr, "%s: %s\n",
36336cefaae1SJack Meng 			    cmdName, gettext("unable to get MPxIO info"
36346cefaae1SJack Meng 			    " of root disk"));
36356cefaae1SJack Meng 			*funcRet = 1;
36366cefaae1SJack Meng 			return (1);
36376cefaae1SJack Meng 		}
36386cefaae1SJack Meng 	}
36396cefaae1SJack Meng 
3640fcf3ce44SJohn Forte 	/* Find Sun initiator */
3641fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&oid);
3642f5fc5c04SJack Meng 	if (ret != 0) {
3643fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
3644fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
3645fcf3ce44SJohn Forte 		return (ret);
3646fcf3ce44SJohn Forte 	}
3647fcf3ce44SJohn Forte 
3648fcf3ce44SJohn Forte 	for (; optionList->optval; optionList++) {
3649fcf3ce44SJohn Forte 		switch (optionList->optval) {
3650fcf3ce44SJohn Forte 			case 'N':
3651fcf3ce44SJohn Forte 				if (strlen(optionList->optarg) >=
3652fcf3ce44SJohn Forte 				    MAX_ISCSI_NAME_LEN) {
3653fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s: %s %d\n",
3654fcf3ce44SJohn Forte 					    cmdName,
3655fcf3ce44SJohn Forte 					    gettext("name too long, \
3656fcf3ce44SJohn Forte 					    maximum length is:"),
3657fcf3ce44SJohn Forte 					    MAX_ISCSI_NAME_LEN);
3658fcf3ce44SJohn Forte 				}
3659fcf3ce44SJohn Forte 
3660fcf3ce44SJohn Forte 				/* Take the first operand as node name. */
3661fcf3ce44SJohn Forte 				(void) memset(&nodeName, 0,
3662fcf3ce44SJohn Forte 				    sizeof (IMA_NODE_NAME));
3663fcf3ce44SJohn Forte 				if (mbstowcs(nodeName, optionList->optarg,
3664fcf3ce44SJohn Forte 				    IMA_NODE_NAME_LEN) == (size_t)-1) {
3665fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s: %s\n",
3666fcf3ce44SJohn Forte 					    cmdName,
3667fcf3ce44SJohn Forte 					    gettext("conversion error"));
3668fcf3ce44SJohn Forte 					return (1);
3669fcf3ce44SJohn Forte 				}
3670fcf3ce44SJohn Forte 
36717f848965Sbing zhao - Sun Microsystems - Beijing China 				prefixlen = strlen(ISCSI_IQN_NAME_PREFIX);
36727f848965Sbing zhao - Sun Microsystems - Beijing China 				mb_name = (char *)calloc(1, prefixlen + 1);
36737f848965Sbing zhao - Sun Microsystems - Beijing China 				if (mb_name == NULL) {
36747f848965Sbing zhao - Sun Microsystems - Beijing China 					return (1);
36757f848965Sbing zhao - Sun Microsystems - Beijing China 				}
36767f848965Sbing zhao - Sun Microsystems - Beijing China 
36777f848965Sbing zhao - Sun Microsystems - Beijing China 				if (wcstombs(mb_name, nodeName,
36787f848965Sbing zhao - Sun Microsystems - Beijing China 				    prefixlen) == (size_t)-1) {
36797f848965Sbing zhao - Sun Microsystems - Beijing China 					(void) fprintf(stderr, "%s: %s\n",
36807f848965Sbing zhao - Sun Microsystems - Beijing China 					    cmdName,
36817f848965Sbing zhao - Sun Microsystems - Beijing China 					    gettext("conversion error"));
36827f848965Sbing zhao - Sun Microsystems - Beijing China 					(void) IMA_FreeMemory(mb_name);
36837f848965Sbing zhao - Sun Microsystems - Beijing China 					return (1);
36847f848965Sbing zhao - Sun Microsystems - Beijing China 				}
36857f848965Sbing zhao - Sun Microsystems - Beijing China 				if (strncmp(mb_name, ISCSI_IQN_NAME_PREFIX,
36867f848965Sbing zhao - Sun Microsystems - Beijing China 				    prefixlen) == 0) {
36877f848965Sbing zhao - Sun Microsystems - Beijing China 					/*
36887f848965Sbing zhao - Sun Microsystems - Beijing China 					 * For iqn format, we should map
36897f848965Sbing zhao - Sun Microsystems - Beijing China 					 * the upper-case characters to
36907f848965Sbing zhao - Sun Microsystems - Beijing China 					 * their lower-case equivalents.
36917f848965Sbing zhao - Sun Microsystems - Beijing China 					 */
3692fcf3ce44SJohn Forte 					for (i = 0; nodeName[i] != 0; i++) {
36937f848965Sbing zhao - Sun Microsystems - Beijing China 						lowerCase =
36947f848965Sbing zhao - Sun Microsystems - Beijing China 						    tolower(nodeName[i]);
3695fcf3ce44SJohn Forte 						nodeName[i] = lowerCase;
3696fcf3ce44SJohn Forte 					}
36977f848965Sbing zhao - Sun Microsystems - Beijing China 				}
36987f848965Sbing zhao - Sun Microsystems - Beijing China 				(void) IMA_FreeMemory(mb_name);
36997f848965Sbing zhao - Sun Microsystems - Beijing China 
3700fcf3ce44SJohn Forte 				/* Perform string profile checks */
3701fcf3ce44SJohn Forte 				nameCheckStatus =
3702fcf3ce44SJohn Forte 				    iSCSINameStringProfileCheck(nodeName);
3703fcf3ce44SJohn Forte 				iSCSINameCheckStatusDisplay(nameCheckStatus);
3704fcf3ce44SJohn Forte 				if (nameCheckStatus != iSCSINameCheckOK) {
3705fcf3ce44SJohn Forte 					*funcRet = 1; /* DIY message fix */
3706fcf3ce44SJohn Forte 					return (1);
3707fcf3ce44SJohn Forte 				}
3708fcf3ce44SJohn Forte 
3709fcf3ce44SJohn Forte 				/*
3710fcf3ce44SJohn Forte 				 * IMA_GetSharedNodeOid(&sharedNodeOid);
3711fcf3ce44SJohn Forte 				 * if (!IMA_SUCCESS(status)) {
3712fcf3ce44SJohn Forte 				 *   printLibError(status);
3713fcf3ce44SJohn Forte 				 *   return (INF_ERROR);
3714fcf3ce44SJohn Forte 				 * }
3715fcf3ce44SJohn Forte 				 */
37166cefaae1SJack Meng 				if (iscsiBoot == IMA_TRUE) {
37176cefaae1SJack Meng 					(void) fprintf(stderr, "%s: %s\n",
37186cefaae1SJack Meng 					    cmdName, gettext("iscsi boot, not"
37196cefaae1SJack Meng 					    " allowed to change"
37206cefaae1SJack Meng 					    " initiator's name"));
37216cefaae1SJack Meng 					return (1);
37226cefaae1SJack Meng 				}
3723fcf3ce44SJohn Forte 				oid.objectType = IMA_OBJECT_TYPE_NODE;
3724fcf3ce44SJohn Forte 				status = IMA_SetNodeName(oid, nodeName);
3725fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
3726fcf3ce44SJohn Forte 					printLibError(status);
3727fcf3ce44SJohn Forte 					*funcRet = 1;
3728fcf3ce44SJohn Forte 					return (ret);
3729fcf3ce44SJohn Forte 				}
3730fcf3ce44SJohn Forte 				break;
3731fcf3ce44SJohn Forte 
3732fcf3ce44SJohn Forte 			case 'A':
37336cefaae1SJack Meng 				if (iscsiBoot == IMA_TRUE) {
37346cefaae1SJack Meng 					(void) fprintf(stderr, "%s: %s\n",
37356cefaae1SJack Meng 					    cmdName, gettext("iscsi boot, not"
37366cefaae1SJack Meng 					    " allowed to change"
37376cefaae1SJack Meng 					    " initiator's alias"));
37386cefaae1SJack Meng 					return (1);
37396cefaae1SJack Meng 				}
3740fcf3ce44SJohn Forte 				/* Take the first operand as node alias. */
3741fcf3ce44SJohn Forte 				if (strlen(optionList->optarg) >=
3742fcf3ce44SJohn Forte 				    MAX_ISCSI_NAME_LEN) {
3743fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s: %s %d\n",
3744fcf3ce44SJohn Forte 					    cmdName,
3745fcf3ce44SJohn Forte 					    gettext("alias too long, maximum  \
3746fcf3ce44SJohn Forte 					    length is:"),
3747fcf3ce44SJohn Forte 					    MAX_ISCSI_NAME_LEN);
3748fcf3ce44SJohn Forte 				}
3749fcf3ce44SJohn Forte 
3750fcf3ce44SJohn Forte 				(void) memset(&nodeAlias, 0,
3751fcf3ce44SJohn Forte 				    sizeof (IMA_NODE_ALIAS));
3752fcf3ce44SJohn Forte 				if (mbstowcs(nodeAlias, optionList->optarg,
3753fcf3ce44SJohn Forte 				    IMA_NODE_ALIAS_LEN) == (size_t)-1) {
3754fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s: %s\n",
3755fcf3ce44SJohn Forte 					    cmdName,
3756fcf3ce44SJohn Forte 					    gettext("conversion error"));
3757fcf3ce44SJohn Forte 					return (1);
3758fcf3ce44SJohn Forte 				}
3759fcf3ce44SJohn Forte 
3760fcf3ce44SJohn Forte 				status = IMA_GetSharedNodeOid(&sharedNodeOid);
3761fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
3762fcf3ce44SJohn Forte 					printLibError(status);
3763fcf3ce44SJohn Forte 					*funcRet = 1;
3764fcf3ce44SJohn Forte 					return (ret);
3765fcf3ce44SJohn Forte 				}
3766fcf3ce44SJohn Forte 
3767fcf3ce44SJohn Forte 				status = IMA_SetNodeAlias(sharedNodeOid,
3768fcf3ce44SJohn Forte 				    nodeAlias);
3769fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
3770fcf3ce44SJohn Forte 					printLibError(status);
3771fcf3ce44SJohn Forte 					*funcRet = 1;
3772fcf3ce44SJohn Forte 					return (ret);
3773fcf3ce44SJohn Forte 				}
3774fcf3ce44SJohn Forte 				break;
3775fcf3ce44SJohn Forte 
3776fcf3ce44SJohn Forte 			case 'a':
37776cefaae1SJack Meng 				if (iscsiBoot == IMA_TRUE) {
37786cefaae1SJack Meng 					(void) fprintf(stderr, "%s: %s\n",
37796cefaae1SJack Meng 					    cmdName, gettext("iscsi boot, not"
37806cefaae1SJack Meng 					    " allowed to change authentication"
37816cefaae1SJack Meng 					    " method"));
37826cefaae1SJack Meng 					return (1);
37836cefaae1SJack Meng 				}
3784fcf3ce44SJohn Forte 				if (modifyNodeAuthMethod(oid, options->optarg,
3785fcf3ce44SJohn Forte 				    funcRet) != 0) {
3786fcf3ce44SJohn Forte 					return (1);
3787fcf3ce44SJohn Forte 				}
3788fcf3ce44SJohn Forte 				break;
3789fcf3ce44SJohn Forte 
3790fcf3ce44SJohn Forte 			case 'R':
3791fcf3ce44SJohn Forte 				if (modifyNodeRadiusAccess(oid, options->optarg,
3792fcf3ce44SJohn Forte 				    funcRet) != 0) {
3793fcf3ce44SJohn Forte 					return (1);
3794fcf3ce44SJohn Forte 				}
3795fcf3ce44SJohn Forte 				break;
3796fcf3ce44SJohn Forte 
3797fcf3ce44SJohn Forte 			case 'r':
3798fcf3ce44SJohn Forte 				if (modifyNodeRadiusConfig(oid, options->optarg,
3799fcf3ce44SJohn Forte 				    funcRet) != 0) {
3800fcf3ce44SJohn Forte 					return (1);
3801fcf3ce44SJohn Forte 				}
3802fcf3ce44SJohn Forte 				break;
3803fcf3ce44SJohn Forte 
3804fcf3ce44SJohn Forte 			case 'P':
3805fcf3ce44SJohn Forte 				if (modifyNodeRadiusSharedSecret(oid, funcRet)
3806fcf3ce44SJohn Forte 				    != 0) {
3807fcf3ce44SJohn Forte 					return (1);
3808fcf3ce44SJohn Forte 				}
3809fcf3ce44SJohn Forte 				break;
3810fcf3ce44SJohn Forte 
3811fcf3ce44SJohn Forte 			case 'C':
38126cefaae1SJack Meng 				if (iscsiBoot == IMA_TRUE) {
38136cefaae1SJack Meng 					(void) fprintf(stderr, "%s: %s\n",
38146cefaae1SJack Meng 					    cmdName, gettext("iscsi boot, not"
38156cefaae1SJack Meng 					    " allowed to change CHAP secret"));
38166cefaae1SJack Meng 					return (1);
38176cefaae1SJack Meng 				}
3818fcf3ce44SJohn Forte 				if (modifyNodeAuthParam(oid, AUTH_PASSWORD,
3819fcf3ce44SJohn Forte 				    NULL, funcRet) != 0) {
3820fcf3ce44SJohn Forte 					return (1);
3821fcf3ce44SJohn Forte 				}
3822fcf3ce44SJohn Forte 				break;
3823fcf3ce44SJohn Forte 
3824fcf3ce44SJohn Forte 			case 'c':
38256cefaae1SJack Meng 				if (iscsiBoot == IMA_TRUE) {
38266cefaae1SJack Meng 					if (mpxioEnabled == IMA_FALSE) {
38276cefaae1SJack Meng 						(void) fprintf(stderr,
38286cefaae1SJack Meng 						    "%s: %s\n", cmdName,
38296cefaae1SJack Meng 						    gettext("iscsi"
38306cefaae1SJack Meng 						    " boot and MPxIO"
38316cefaae1SJack Meng 						    " is disabled, not allowed"
38326cefaae1SJack Meng 						    " to change number of"
38336cefaae1SJack Meng 						    " sessions to be"
38346cefaae1SJack Meng 						    " configured"));
38356cefaae1SJack Meng 						return (1);
38366cefaae1SJack Meng 					}
38376cefaae1SJack Meng 				}
3838fcf3ce44SJohn Forte 				if (modifyConfiguredSessions(oid,
3839fcf3ce44SJohn Forte 				    optionList->optarg) != 0) {
38406cefaae1SJack Meng 					if (iscsiBoot == IMA_TRUE) {
38416cefaae1SJack Meng 						(void) fprintf(stderr,
38426cefaae1SJack Meng 						    "%s: %s\n", cmdName,
38436cefaae1SJack Meng 						    gettext("iscsi boot,"
38446cefaae1SJack Meng 						    " fail to set configured"
38456cefaae1SJack Meng 						    " session"));
38466cefaae1SJack Meng 					}
3847fcf3ce44SJohn Forte 					return (1);
3848fcf3ce44SJohn Forte 				}
3849fcf3ce44SJohn Forte 				break;
3850fcf3ce44SJohn Forte 
38516cefaae1SJack Meng 
3852fcf3ce44SJohn Forte 			case 'H':
38536cefaae1SJack Meng 				if (iscsiBoot == IMA_TRUE) {
38546cefaae1SJack Meng 					(void) fprintf(stderr, "%s: %s\n",
38556cefaae1SJack Meng 					    cmdName, gettext("iscsi boot, not"
38566cefaae1SJack Meng 					    " allowed to change CHAP name"));
38576cefaae1SJack Meng 					return (1);
38586cefaae1SJack Meng 				}
3859fcf3ce44SJohn Forte 				if (modifyNodeAuthParam(oid, AUTH_NAME,
3860fcf3ce44SJohn Forte 				    optionList->optarg, funcRet) != 0) {
3861fcf3ce44SJohn Forte 					return (1);
3862fcf3ce44SJohn Forte 				}
3863fcf3ce44SJohn Forte 				break;
3864fcf3ce44SJohn Forte 
38656cefaae1SJack Meng 
3866fcf3ce44SJohn Forte 			case 'd':
38676cefaae1SJack Meng 				if (iscsiBoot == IMA_TRUE) {
38686cefaae1SJack Meng 					if (mpxioEnabled == IMA_FALSE) {
38696cefaae1SJack Meng 						(void) fprintf(stderr,
38706cefaae1SJack Meng 						    "%s: %s\n", cmdName,
38716cefaae1SJack Meng 						    gettext("iscsi"
38726cefaae1SJack Meng 						    " boot and MPxIO"
38736cefaae1SJack Meng 						    " is disabled, not"
38746cefaae1SJack Meng 						    " allowed to"
38756cefaae1SJack Meng 						    " change initiator's"
38766cefaae1SJack Meng 						    " login params"));
38776cefaae1SJack Meng 						return (1);
38786cefaae1SJack Meng 					}
38796cefaae1SJack Meng 				}
3880fcf3ce44SJohn Forte 				if (setLoginParameter(oid, DATA_DIGEST,
3881fcf3ce44SJohn Forte 				    optionList->optarg) != 0) {
3882fcf3ce44SJohn Forte 					return (1);
3883fcf3ce44SJohn Forte 				}
3884fcf3ce44SJohn Forte 				break;
3885fcf3ce44SJohn Forte 
3886fcf3ce44SJohn Forte 			case 'h':
38876cefaae1SJack Meng 				if (iscsiBoot == IMA_TRUE) {
38886cefaae1SJack Meng 					if (mpxioEnabled == IMA_FALSE) {
38896cefaae1SJack Meng 						(void) fprintf(stderr,
38906cefaae1SJack Meng 						    "%s: %s\n", cmdName,
38916cefaae1SJack Meng 						    gettext("iscsi"
38926cefaae1SJack Meng 						    " boot and MPxIO"
38936cefaae1SJack Meng 						    " is disabled, not"
38946cefaae1SJack Meng 						    " allowed to"
38956cefaae1SJack Meng 						    " change initiator's"
38966cefaae1SJack Meng 						    " login params"));
38976cefaae1SJack Meng 						return (1);
38986cefaae1SJack Meng 					}
38996cefaae1SJack Meng 				}
3900fcf3ce44SJohn Forte 				if (setLoginParameter(oid, HEADER_DIGEST,
3901fcf3ce44SJohn Forte 				    optionList->optarg) != 0) {
3902fcf3ce44SJohn Forte 					return (1);
3903fcf3ce44SJohn Forte 				}
3904fcf3ce44SJohn Forte 				break;
3905fcf3ce44SJohn Forte 
3906fcf3ce44SJohn Forte 			default:
3907fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %c: %s\n",
3908fcf3ce44SJohn Forte 				    cmdName, optionList->optval,
3909fcf3ce44SJohn Forte 				    gettext("unknown option"));
3910fcf3ce44SJohn Forte 				break;
3911fcf3ce44SJohn Forte 		}
3912fcf3ce44SJohn Forte 	}
3913fcf3ce44SJohn Forte 
3914fcf3ce44SJohn Forte 	return (ret);
3915fcf3ce44SJohn Forte }
3916fcf3ce44SJohn Forte 
3917fcf3ce44SJohn Forte /*
3918fcf3ce44SJohn Forte  * Modify target parameters
3919fcf3ce44SJohn Forte  */
3920fcf3ce44SJohn Forte static int
3921fcf3ce44SJohn Forte modifyTargetParam(cmdOptions_t *options, char *targetName, int *funcRet)
3922fcf3ce44SJohn Forte {
3923fcf3ce44SJohn Forte 	IMA_OID oid;
3924fcf3ce44SJohn Forte 	IMA_OID targetOid;
3925fcf3ce44SJohn Forte 	IMA_STATUS status;
3926fcf3ce44SJohn Forte 	IMA_OID_LIST *targetList;
3927fcf3ce44SJohn Forte 	SUN_IMA_TARGET_PROPERTIES targetProps;
3928fcf3ce44SJohn Forte 	wchar_t wcInputObject[MAX_ISCSI_NAME_LEN + 1];
3929fcf3ce44SJohn Forte 	wchar_t targetAddress[SUN_IMA_IP_ADDRESS_PORT_LEN];
3930fcf3ce44SJohn Forte 	int ret;
3931fcf3ce44SJohn Forte 	boolean_t found;
3932fcf3ce44SJohn Forte 	boolean_t targetAddressSpecified = B_TRUE;
3933fcf3ce44SJohn Forte 	boolean_t tpgtSpecified = B_FALSE;
3934fcf3ce44SJohn Forte 	boolean_t isIpv6 = B_FALSE;
3935fcf3ce44SJohn Forte 	int i;
3936fcf3ce44SJohn Forte 	iSCSINameCheckStatusType nameCheckStatus;
3937fcf3ce44SJohn Forte 	IMA_UINT16 port = 0;
3938fcf3ce44SJohn Forte 	IMA_UINT16 tpgt = 0;
3939fcf3ce44SJohn Forte 
39406cefaae1SJack Meng 	IMA_NODE_NAME bootTargetName;
39416cefaae1SJack Meng 	IMA_INITIATOR_AUTHPARMS bootTargetCHAP;
39426cefaae1SJack Meng 	IMA_BOOL  iscsiBoot;
39436cefaae1SJack Meng 	IMA_BOOL  mpxioEnabled;
39446cefaae1SJack Meng 
3945fcf3ce44SJohn Forte 	cmdOptions_t *optionList = options;
3946fcf3ce44SJohn Forte 
3947fcf3ce44SJohn Forte 	assert(funcRet != NULL);
3948fcf3ce44SJohn Forte 
3949fcf3ce44SJohn Forte 	/* Find Sun initiator */
3950fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&oid);
3951fcf3ce44SJohn Forte 	if (ret > 0) {
3952fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
3953fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
3954fcf3ce44SJohn Forte 	}
3955fcf3ce44SJohn Forte 
3956fcf3ce44SJohn Forte 	if (ret != 0) {
3957fcf3ce44SJohn Forte 		return (ret);
3958fcf3ce44SJohn Forte 	}
3959fcf3ce44SJohn Forte 
3960fcf3ce44SJohn Forte 	if (parseTarget(targetName,
3961fcf3ce44SJohn Forte 	    &wcInputObject[0],
3962fcf3ce44SJohn Forte 	    MAX_ISCSI_NAME_LEN + 1,
3963fcf3ce44SJohn Forte 	    &targetAddressSpecified,
3964fcf3ce44SJohn Forte 	    &targetAddress[0],
3965fcf3ce44SJohn Forte 	    SUN_IMA_IP_ADDRESS_PORT_LEN,
3966fcf3ce44SJohn Forte 	    &port,
3967fcf3ce44SJohn Forte 	    &tpgtSpecified,
3968fcf3ce44SJohn Forte 	    &tpgt,
3969fcf3ce44SJohn Forte 	    &isIpv6) != PARSE_TARGET_OK) {
3970fcf3ce44SJohn Forte 		return (1);
3971fcf3ce44SJohn Forte 	}
3972fcf3ce44SJohn Forte 
3973fcf3ce44SJohn Forte 	/* Perform string profile checks */
3974fcf3ce44SJohn Forte 	nameCheckStatus = iSCSINameStringProfileCheck(wcInputObject);
3975fcf3ce44SJohn Forte 	iSCSINameCheckStatusDisplay(nameCheckStatus);
3976fcf3ce44SJohn Forte 	if (nameCheckStatus != iSCSINameCheckOK) {
3977fcf3ce44SJohn Forte 		return (1);
3978fcf3ce44SJohn Forte 	}
3979fcf3ce44SJohn Forte 
3980fcf3ce44SJohn Forte 	status = IMA_GetTargetOidList(oid, &targetList);
3981fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
3982fcf3ce44SJohn Forte 		printLibError(status);
3983fcf3ce44SJohn Forte 		*funcRet = 1;
3984fcf3ce44SJohn Forte 		return (0);
3985fcf3ce44SJohn Forte 	}
3986fcf3ce44SJohn Forte 
39876cefaae1SJack Meng 	(void) SUN_IMA_GetBootIscsi(&iscsiBoot);
39886cefaae1SJack Meng 	if (iscsiBoot == IMA_TRUE) {
39896cefaae1SJack Meng 		status = SUN_IMA_GetBootMpxio(&mpxioEnabled);
39906cefaae1SJack Meng 		if (!IMA_SUCCESS(status)) {
39916cefaae1SJack Meng 			(void) fprintf(stderr, "%s: %s\n",
39926cefaae1SJack Meng 			    cmdName, gettext("unable to get MPxIO info"
39936cefaae1SJack Meng 			    " of root disk"));
39946cefaae1SJack Meng 			*funcRet = 1;
39956cefaae1SJack Meng 			return (ret);
39966cefaae1SJack Meng 		}
39976cefaae1SJack Meng 		status = SUN_IMA_GetBootTargetName(bootTargetName);
39986cefaae1SJack Meng 		if (!IMA_SUCCESS(status)) {
39996cefaae1SJack Meng 			(void) fprintf(stderr, "%s: %s\n",
40006cefaae1SJack Meng 			    cmdName, gettext("unable to get boot target's"
40016cefaae1SJack Meng 			    " name"));
40026cefaae1SJack Meng 			*funcRet = 1;
40036cefaae1SJack Meng 			return (ret);
40046cefaae1SJack Meng 		}
40056cefaae1SJack Meng 		status = SUN_IMA_GetBootTargetAuthParams(&bootTargetCHAP);
40066cefaae1SJack Meng 		if (!IMA_SUCCESS(status)) {
40076cefaae1SJack Meng 			(void) fprintf(stderr, "%s: %s\n",
40086cefaae1SJack Meng 			    cmdName, gettext("unable to get boot target's"
40096cefaae1SJack Meng 			    " auth param"));
40106cefaae1SJack Meng 			*funcRet = 1;
40116cefaae1SJack Meng 			return (ret);
40126cefaae1SJack Meng 		}
40136cefaae1SJack Meng 	}
40146cefaae1SJack Meng 
4015fcf3ce44SJohn Forte 	/* find target oid */
4016fcf3ce44SJohn Forte 	for (found = B_FALSE, i = 0; i < targetList->oidCount; i++) {
4017fcf3ce44SJohn Forte 		status = SUN_IMA_GetTargetProperties(targetList->oids[i],
4018fcf3ce44SJohn Forte 		    &targetProps);
4019fcf3ce44SJohn Forte 		if (!IMA_SUCCESS(status)) {
4020fcf3ce44SJohn Forte 			printLibError(status);
4021fcf3ce44SJohn Forte 			(void) IMA_FreeMemory(targetList);
4022fcf3ce44SJohn Forte 			*funcRet = 1;
4023fcf3ce44SJohn Forte 			return (ret);
4024fcf3ce44SJohn Forte 		}
4025fcf3ce44SJohn Forte 
4026fcf3ce44SJohn Forte 		/*
4027fcf3ce44SJohn Forte 		 * Compare the target name with the input name
4028fcf3ce44SJohn Forte 		 */
4029fcf3ce44SJohn Forte 		if ((targetNamesEqual(wcInputObject, targetProps.imaProps.name)
4030fcf3ce44SJohn Forte 		    == B_TRUE)) {
4031fcf3ce44SJohn Forte 			/*
4032fcf3ce44SJohn Forte 			 * For now, regardless of whether a target address
4033fcf3ce44SJohn Forte 			 * is specified, we return B_TRUE because
4034fcf3ce44SJohn Forte 			 * IMA_TARGET_PROPERTIES does not have a field for
4035fcf3ce44SJohn Forte 			 * specifying address.
4036fcf3ce44SJohn Forte 			 */
4037fcf3ce44SJohn Forte 			found = B_TRUE;
4038fcf3ce44SJohn Forte 			targetOid = targetList->oids[i];
40396cefaae1SJack Meng 
40406cefaae1SJack Meng 			if ((targetNamesEqual(bootTargetName, wcInputObject)
40416cefaae1SJack Meng 			    == B_TRUE) && (iscsiBoot == IMA_TRUE)) {
40426cefaae1SJack Meng 				/*
40436cefaae1SJack Meng 				 * iscsi booting, need changed target param is
40446cefaae1SJack Meng 				 * booting target, for auth param, not allow
40456cefaae1SJack Meng 				 * to change, for others dependent on mpxio
40466cefaae1SJack Meng 				 */
40476cefaae1SJack Meng 
40486cefaae1SJack Meng 				if ((optionList->optval == 'C') ||
40496cefaae1SJack Meng 				    (optionList->optval == 'H') ||
40506cefaae1SJack Meng 				    (optionList->optval == 'B') ||
40516cefaae1SJack Meng 				    (optionList->optval == 'a')) {
40526cefaae1SJack Meng 					/*
40536cefaae1SJack Meng 					 * -C CHAP secret set
40546cefaae1SJack Meng 					 * -H CHAP name set
40556cefaae1SJack Meng 					 * -a authentication
40566cefaae1SJack Meng 					 * -B bi-directional-authentication
40576cefaae1SJack Meng 					 */
40586cefaae1SJack Meng 					(void) fprintf(stderr, "%s: %s\n",
40596cefaae1SJack Meng 					    cmdName, gettext("iscsi boot,"
40606cefaae1SJack Meng 					    " not allowed to modify"
40616cefaae1SJack Meng 					    " authentication parameters"
40626cefaae1SJack Meng 					    "  of boot target"));
40636cefaae1SJack Meng 					return (1);
40646cefaae1SJack Meng 				}
40656cefaae1SJack Meng 				if (mpxioEnabled == IMA_FALSE) {
40666cefaae1SJack Meng 					(void) fprintf(stderr, "%s: %s\n",
40676cefaae1SJack Meng 					    cmdName, gettext("iscsi boot and"
40686cefaae1SJack Meng 					    " MPxIO is disabled, not allowed"
40696cefaae1SJack Meng 					    " to modify boot target's"
40706cefaae1SJack Meng 					    " parameters"));
40716cefaae1SJack Meng 					return (1);
40726cefaae1SJack Meng 				}
40736cefaae1SJack Meng 
40746cefaae1SJack Meng 			}
40756cefaae1SJack Meng 
4076fcf3ce44SJohn Forte 			if (modifyIndividualTargetParam(optionList, targetOid,
4077fcf3ce44SJohn Forte 			    funcRet) != 0) {
4078fcf3ce44SJohn Forte 				return (ret);
4079fcf3ce44SJohn Forte 			}
4080fcf3ce44SJohn Forte 
4081fcf3ce44SJohn Forte 			/*
4082fcf3ce44SJohn Forte 			 * Even after finding a matched target, keep going
4083fcf3ce44SJohn Forte 			 * since there could be multiple target objects
4084fcf3ce44SJohn Forte 			 * associated with one target name in the system
4085fcf3ce44SJohn Forte 			 * because of different TPGTs.
4086fcf3ce44SJohn Forte 			 */
4087fcf3ce44SJohn Forte 		}
4088fcf3ce44SJohn Forte 	}
4089fcf3ce44SJohn Forte 
4090fcf3ce44SJohn Forte 	/* If the target OID cannot be found create one */
4091fcf3ce44SJohn Forte 	if (!found) {
4092fcf3ce44SJohn Forte 		status = SUN_IMA_CreateTargetOid(wcInputObject, &targetOid);
4093fcf3ce44SJohn Forte 		if (!IMA_SUCCESS(status)) {
4094fcf3ce44SJohn Forte 			printLibError(status);
4095fcf3ce44SJohn Forte 			(void) IMA_FreeMemory(targetList);
4096fcf3ce44SJohn Forte 			*funcRet = 1;
4097fcf3ce44SJohn Forte 			return (ret);
4098fcf3ce44SJohn Forte 		}
4099fcf3ce44SJohn Forte 		if (modifyIndividualTargetParam(optionList, targetOid,
4100fcf3ce44SJohn Forte 		    funcRet) != 0) {
4101fcf3ce44SJohn Forte 				return (ret);
4102fcf3ce44SJohn Forte 		}
4103fcf3ce44SJohn Forte 	}
4104fcf3ce44SJohn Forte 
4105fcf3ce44SJohn Forte 	(void) IMA_FreeMemory(targetList);
4106fcf3ce44SJohn Forte 	return (ret);
4107fcf3ce44SJohn Forte }
4108fcf3ce44SJohn Forte 
4109fcf3ce44SJohn Forte /*
4110fcf3ce44SJohn Forte  * Add one or more addresses
4111fcf3ce44SJohn Forte  */
4112fcf3ce44SJohn Forte static int
4113fcf3ce44SJohn Forte addAddress(int addrType, int operandLen, char *operand[], int *funcRet)
4114fcf3ce44SJohn Forte {
4115fcf3ce44SJohn Forte 	IMA_STATUS status;
4116fcf3ce44SJohn Forte 	IMA_OID oid, addressOid;
4117fcf3ce44SJohn Forte 	SUN_IMA_TARGET_ADDRESS address;
4118fcf3ce44SJohn Forte 	wchar_t wcInputObject[MAX_ADDRESS_LEN + 1];
4119fcf3ce44SJohn Forte 	int ret;
4120fcf3ce44SJohn Forte 	int i;
4121fcf3ce44SJohn Forte 
4122fcf3ce44SJohn Forte 	assert(funcRet != NULL);
4123fcf3ce44SJohn Forte 
4124fcf3ce44SJohn Forte 	/* Find Sun initiator */
4125fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&oid);
4126fcf3ce44SJohn Forte 	if (ret > 0) {
4127fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
4128fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
4129fcf3ce44SJohn Forte 	}
4130fcf3ce44SJohn Forte 
4131fcf3ce44SJohn Forte 	if (ret != 0) {
4132fcf3ce44SJohn Forte 		return (ret);
4133fcf3ce44SJohn Forte 	}
4134fcf3ce44SJohn Forte 
4135fcf3ce44SJohn Forte 	/*
4136fcf3ce44SJohn Forte 	 * Format of discovery address operand:
4137fcf3ce44SJohn Forte 	 *
4138fcf3ce44SJohn Forte 	 * <IP address|hostname>:<port>
4139fcf3ce44SJohn Forte 	 */
4140fcf3ce44SJohn Forte 	for (i = 0; i < operandLen; i++) {
4141fcf3ce44SJohn Forte 		/* initialize */
4142fcf3ce44SJohn Forte 		(void) memset(&wcInputObject[0], 0, sizeof (wcInputObject));
4143fcf3ce44SJohn Forte 		(void) memset(&address, 0, sizeof (address));
4144fcf3ce44SJohn Forte 
4145fcf3ce44SJohn Forte 		if (mbstowcs(wcInputObject, operand[i],
4146fcf3ce44SJohn Forte 		    (MAX_ADDRESS_LEN + 1)) == (size_t)-1) {
4147fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4148fcf3ce44SJohn Forte 			    cmdName, gettext("conversion error"));
4149fcf3ce44SJohn Forte 			ret = 1;
4150fcf3ce44SJohn Forte 			continue;
4151fcf3ce44SJohn Forte 		}
4152fcf3ce44SJohn Forte 		if (getTargetAddress(addrType, operand[i], &address.imaStruct)
4153fcf3ce44SJohn Forte 		    != 0) {
4154fcf3ce44SJohn Forte 			ret = 1;
4155fcf3ce44SJohn Forte 			continue;
4156fcf3ce44SJohn Forte 		}
4157fcf3ce44SJohn Forte 		if (addrType == DISCOVERY_ADDRESS) {
4158fcf3ce44SJohn Forte 			status = IMA_AddDiscoveryAddress(oid,
4159fcf3ce44SJohn Forte 			    address.imaStruct, &addressOid);
4160fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
4161fcf3ce44SJohn Forte 				printLibError(status);
4162fcf3ce44SJohn Forte 				*funcRet = 1;
4163fcf3ce44SJohn Forte 				return (ret);
4164fcf3ce44SJohn Forte 			}
4165fcf3ce44SJohn Forte 		} else if (addrType == ISNS_SERVER_ADDRESS) {
4166fcf3ce44SJohn Forte 			status = SUN_IMA_AddISNSServerAddress(address);
4167fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
4168fcf3ce44SJohn Forte 				printLibError(status);
4169fcf3ce44SJohn Forte 				*funcRet = 1;
4170fcf3ce44SJohn Forte 				return (ret);
4171fcf3ce44SJohn Forte 			}
4172fcf3ce44SJohn Forte 		}
4173fcf3ce44SJohn Forte 	}
4174fcf3ce44SJohn Forte 	return (ret);
4175fcf3ce44SJohn Forte }
4176fcf3ce44SJohn Forte 
4177fcf3ce44SJohn Forte /*
4178fcf3ce44SJohn Forte  * Add one or more static configuration targets
4179fcf3ce44SJohn Forte  */
4180fcf3ce44SJohn Forte static int
4181fcf3ce44SJohn Forte addStaticConfig(int operandLen, char *operand[], int *funcRet)
4182fcf3ce44SJohn Forte {
4183fcf3ce44SJohn Forte 	int i;
4184fcf3ce44SJohn Forte 	boolean_t targetAddressSpecified = B_FALSE;
4185fcf3ce44SJohn Forte 	boolean_t tpgtSpecified = B_FALSE;
4186fcf3ce44SJohn Forte 	boolean_t isIpv6 = B_FALSE;
4187fcf3ce44SJohn Forte 	int ret;
4188fcf3ce44SJohn Forte 	int addrType;
4189fcf3ce44SJohn Forte 	IMA_STATUS status;
4190fcf3ce44SJohn Forte 	IMA_OID oid;
4191fcf3ce44SJohn Forte 	SUN_IMA_STATIC_DISCOVERY_TARGET staticConfig;
4192fcf3ce44SJohn Forte 	IMA_UINT16 port = 0;
4193fcf3ce44SJohn Forte 	IMA_UINT16 tpgt = 0;
4194fcf3ce44SJohn Forte 	wchar_t staticTargetName[MAX_ISCSI_NAME_LEN + 1];
4195fcf3ce44SJohn Forte 	wchar_t staticTargetAddress[SUN_IMA_IP_ADDRESS_PORT_LEN];
4196fcf3ce44SJohn Forte 	iSCSINameCheckStatusType nameCheckStatus;
4197fcf3ce44SJohn Forte 	char sAddr[SUN_IMA_IP_ADDRESS_PORT_LEN];
4198fcf3ce44SJohn Forte 
4199fcf3ce44SJohn Forte 	assert(funcRet != NULL);
4200fcf3ce44SJohn Forte 
4201fcf3ce44SJohn Forte 	/* Find Sun initiator */
4202fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&oid);
4203fcf3ce44SJohn Forte 	if (ret > 0) {
4204fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
4205fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
4206fcf3ce44SJohn Forte 	}
4207fcf3ce44SJohn Forte 
4208fcf3ce44SJohn Forte 	if (ret != 0) {
4209fcf3ce44SJohn Forte 		return (ret);
4210fcf3ce44SJohn Forte 	}
4211fcf3ce44SJohn Forte 
4212fcf3ce44SJohn Forte 	/*
4213fcf3ce44SJohn Forte 	 * Format of static config operand:
4214fcf3ce44SJohn Forte 	 *  <target-name>,<IP address|hostname>[:port][,tpgt]
4215fcf3ce44SJohn Forte 	 */
4216fcf3ce44SJohn Forte 	for (i = 0; i < operandLen; i++) {
4217fcf3ce44SJohn Forte 		if (parseTarget(operand[i],
4218fcf3ce44SJohn Forte 		    &staticTargetName[0],
4219fcf3ce44SJohn Forte 		    MAX_ISCSI_NAME_LEN + 1,
4220fcf3ce44SJohn Forte 		    &targetAddressSpecified,
4221fcf3ce44SJohn Forte 		    &staticTargetAddress[0],
4222fcf3ce44SJohn Forte 		    SUN_IMA_IP_ADDRESS_PORT_LEN,
4223fcf3ce44SJohn Forte 		    &port,
4224fcf3ce44SJohn Forte 		    &tpgtSpecified,
4225fcf3ce44SJohn Forte 		    &tpgt,
4226fcf3ce44SJohn Forte 		    &isIpv6) != PARSE_TARGET_OK) {
4227fcf3ce44SJohn Forte 			ret = 1;
4228fcf3ce44SJohn Forte 			continue;
4229fcf3ce44SJohn Forte 		}
4230fcf3ce44SJohn Forte 
4231fcf3ce44SJohn Forte 		if (targetAddressSpecified != B_TRUE) {
4232fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4233fcf3ce44SJohn Forte 			    cmdName, gettext("missing target address"));
4234fcf3ce44SJohn Forte 			*funcRet = 1; /* DIY message fix */
4235fcf3ce44SJohn Forte 			return (1);
4236fcf3ce44SJohn Forte 		}
4237fcf3ce44SJohn Forte 		/* Perform string profile checks */
4238fcf3ce44SJohn Forte 		nameCheckStatus = iSCSINameStringProfileCheck(staticTargetName);
4239fcf3ce44SJohn Forte 		iSCSINameCheckStatusDisplay(nameCheckStatus);
4240fcf3ce44SJohn Forte 		if (nameCheckStatus != iSCSINameCheckOK) {
4241fcf3ce44SJohn Forte 			*funcRet = 1; /* DIY message fix */
4242fcf3ce44SJohn Forte 			return (1);
4243fcf3ce44SJohn Forte 		}
4244fcf3ce44SJohn Forte 		(void) wcsncpy(staticConfig.targetName, staticTargetName,
4245fcf3ce44SJohn Forte 		    MAX_ISCSI_NAME_LEN + 1);
4246fcf3ce44SJohn Forte 
4247fcf3ce44SJohn Forte 		(void) wcstombs(sAddr, staticTargetAddress, sizeof (sAddr));
4248fcf3ce44SJohn Forte 
4249fcf3ce44SJohn Forte 		if (isIpv6 == B_TRUE) {
4250fcf3ce44SJohn Forte 			staticConfig.targetAddress.imaStruct.hostnameIpAddress.
4251fcf3ce44SJohn Forte 			    id.ipAddress.ipv4Address = B_FALSE;
4252fcf3ce44SJohn Forte 			addrType = AF_INET6;
4253fcf3ce44SJohn Forte 		} else {
4254fcf3ce44SJohn Forte 			staticConfig.targetAddress.imaStruct.hostnameIpAddress.
4255fcf3ce44SJohn Forte 			    id.ipAddress.ipv4Address = B_TRUE;
4256fcf3ce44SJohn Forte 			addrType = AF_INET;
4257fcf3ce44SJohn Forte 		}
4258fcf3ce44SJohn Forte 
4259fcf3ce44SJohn Forte 		if (inet_pton(addrType, sAddr, staticConfig.targetAddress.
4260fcf3ce44SJohn Forte 		    imaStruct.hostnameIpAddress.id.ipAddress.ipAddress) != 1) {
4261fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4262fcf3ce44SJohn Forte 			    cmdName, gettext("static config conversion error"));
4263fcf3ce44SJohn Forte 			ret = 1;
4264fcf3ce44SJohn Forte 			continue;
4265fcf3ce44SJohn Forte 		}
4266fcf3ce44SJohn Forte 
4267fcf3ce44SJohn Forte 		staticConfig.targetAddress.imaStruct.portNumber = port;
4268fcf3ce44SJohn Forte 		if (tpgtSpecified == B_TRUE) {
4269fcf3ce44SJohn Forte 			staticConfig.targetAddress.defaultTpgt = B_FALSE;
4270fcf3ce44SJohn Forte 			staticConfig.targetAddress.tpgt = tpgt;
4271fcf3ce44SJohn Forte 		} else {
4272fcf3ce44SJohn Forte 			staticConfig.targetAddress.defaultTpgt = B_TRUE;
4273fcf3ce44SJohn Forte 			staticConfig.targetAddress.tpgt = 0;
4274fcf3ce44SJohn Forte 		}
4275fcf3ce44SJohn Forte 
4276fcf3ce44SJohn Forte 		status = SUN_IMA_AddStaticTarget(oid, staticConfig, &oid);
4277fcf3ce44SJohn Forte 		if (!IMA_SUCCESS(status)) {
4278fcf3ce44SJohn Forte 			printLibError(status);
4279fcf3ce44SJohn Forte 			*funcRet = 1;
4280fcf3ce44SJohn Forte 			return (1);
4281fcf3ce44SJohn Forte 		}
4282fcf3ce44SJohn Forte 	}
42835df5713fSbing zhao - Sun Microsystems - Beijing China 
42845df5713fSbing zhao - Sun Microsystems - Beijing China 	if (ret != 0) {
42855df5713fSbing zhao - Sun Microsystems - Beijing China 		*funcRet = 1;
42865df5713fSbing zhao - Sun Microsystems - Beijing China 	}
42875df5713fSbing zhao - Sun Microsystems - Beijing China 
4288fcf3ce44SJohn Forte 	return (ret);
4289fcf3ce44SJohn Forte }
4290fcf3ce44SJohn Forte 
4291fcf3ce44SJohn Forte /*
4292fcf3ce44SJohn Forte  * Remove one or more addresses
4293fcf3ce44SJohn Forte  */
4294fcf3ce44SJohn Forte static int
4295fcf3ce44SJohn Forte removeAddress(int addrType, int operandLen, char *operand[], int *funcRet)
4296fcf3ce44SJohn Forte {
4297fcf3ce44SJohn Forte 	IMA_STATUS status;
4298fcf3ce44SJohn Forte 	IMA_OID initiatorOid;
4299fcf3ce44SJohn Forte 	SUN_IMA_TARGET_ADDRESS address;
4300fcf3ce44SJohn Forte 	wchar_t wcInputObject[MAX_ADDRESS_LEN + 1];
4301fcf3ce44SJohn Forte 	int ret;
4302fcf3ce44SJohn Forte 	int i;
4303fcf3ce44SJohn Forte 
4304fcf3ce44SJohn Forte 	assert(funcRet != NULL);
4305fcf3ce44SJohn Forte 
4306fcf3ce44SJohn Forte 	/* Find Sun initiator */
4307fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
4308fcf3ce44SJohn Forte 	if (ret > 0) {
4309fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
4310fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
4311fcf3ce44SJohn Forte 	}
4312fcf3ce44SJohn Forte 
4313fcf3ce44SJohn Forte 	if (ret != 0) {
4314fcf3ce44SJohn Forte 		return (ret);
4315fcf3ce44SJohn Forte 	}
4316fcf3ce44SJohn Forte 
4317fcf3ce44SJohn Forte 	for (i = 0; i < operandLen; i++) {
4318fcf3ce44SJohn Forte 		/* initialize */
4319fcf3ce44SJohn Forte 		(void) memset(&wcInputObject[0], 0, sizeof (wcInputObject));
4320fcf3ce44SJohn Forte 		(void) memset(&address, 0, sizeof (address));
4321fcf3ce44SJohn Forte 
4322fcf3ce44SJohn Forte 		if (mbstowcs(wcInputObject, operand[i],
4323fcf3ce44SJohn Forte 		    MAX_ADDRESS_LEN + 1) == (size_t)-1) {
4324fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4325fcf3ce44SJohn Forte 			    cmdName, gettext("conversion error"));
4326fcf3ce44SJohn Forte 			ret = 1;
4327fcf3ce44SJohn Forte 			continue;
4328fcf3ce44SJohn Forte 		}
4329fcf3ce44SJohn Forte 
4330fcf3ce44SJohn Forte 		if (getTargetAddress(addrType, operand[i], &address.imaStruct)
4331fcf3ce44SJohn Forte 		    != 0) {
4332fcf3ce44SJohn Forte 			ret = 1;
4333fcf3ce44SJohn Forte 			continue;
4334fcf3ce44SJohn Forte 		}
4335fcf3ce44SJohn Forte 
4336fcf3ce44SJohn Forte 		if (addrType == DISCOVERY_ADDRESS) {
4337fcf3ce44SJohn Forte 			status = SUN_IMA_RemoveDiscoveryAddress(address);
4338fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
4339fcf3ce44SJohn Forte 				if (status == IMA_ERROR_OBJECT_NOT_FOUND) {
4340fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s: %s\n",
4341fcf3ce44SJohn Forte 					    operand[i], gettext("not found"));
4342fcf3ce44SJohn Forte 				} else {
4343fcf3ce44SJohn Forte 					printLibError(status);
4344fcf3ce44SJohn Forte 				}
4345fcf3ce44SJohn Forte 				*funcRet = 1;
4346fcf3ce44SJohn Forte 			}
4347fcf3ce44SJohn Forte 		} else {
4348fcf3ce44SJohn Forte 			status = SUN_IMA_RemoveISNSServerAddress(address);
4349fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
4350fcf3ce44SJohn Forte 				printLibError(status);
4351fcf3ce44SJohn Forte 				*funcRet = 1;
4352fcf3ce44SJohn Forte 			}
4353fcf3ce44SJohn Forte 		}
4354fcf3ce44SJohn Forte 	}
4355fcf3ce44SJohn Forte 	return (ret);
4356fcf3ce44SJohn Forte }
4357fcf3ce44SJohn Forte 
4358fcf3ce44SJohn Forte /*
4359fcf3ce44SJohn Forte  * Remove one or more static configuration targets
4360fcf3ce44SJohn Forte  */
4361fcf3ce44SJohn Forte static int
4362fcf3ce44SJohn Forte removeStaticConfig(int operandLen, char *operand[], int *funcRet)
4363fcf3ce44SJohn Forte {
4364fcf3ce44SJohn Forte 	IMA_STATUS status;
4365fcf3ce44SJohn Forte 	IMA_OID initiatorOid;
4366fcf3ce44SJohn Forte 	IMA_OID_LIST *staticTargetList;
4367fcf3ce44SJohn Forte 	SUN_IMA_STATIC_TARGET_PROPERTIES staticTargetProps;
4368fcf3ce44SJohn Forte 	wchar_t staticTargetName[MAX_ISCSI_NAME_LEN + 1];
4369fcf3ce44SJohn Forte 	wchar_t staticTargetAddress[SUN_IMA_IP_ADDRESS_PORT_LEN];
4370fcf3ce44SJohn Forte 	int ret;
4371fcf3ce44SJohn Forte 	boolean_t atLeastFoundOne;
4372fcf3ce44SJohn Forte 	boolean_t matched;
4373fcf3ce44SJohn Forte 	boolean_t targetAddressSpecified = B_TRUE;
4374fcf3ce44SJohn Forte 	boolean_t tpgtSpecified = B_FALSE;
4375fcf3ce44SJohn Forte 	boolean_t isIpv6 = B_FALSE;
4376fcf3ce44SJohn Forte 	int i, j;
4377fcf3ce44SJohn Forte 	IMA_UINT16 port = 0;
4378fcf3ce44SJohn Forte 	IMA_UINT16 tpgt = 0;
4379fcf3ce44SJohn Forte 	iSCSINameCheckStatusType nameCheckStatus;
4380fcf3ce44SJohn Forte 	char tmpStr[SUN_IMA_IP_ADDRESS_PORT_LEN];
4381fcf3ce44SJohn Forte 	wchar_t tmpTargetAddress[SUN_IMA_IP_ADDRESS_PORT_LEN];
4382fcf3ce44SJohn Forte 
4383fcf3ce44SJohn Forte 	assert(funcRet != NULL);
4384fcf3ce44SJohn Forte 
4385fcf3ce44SJohn Forte 	/* Find Sun initiator */
4386fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
4387fcf3ce44SJohn Forte 	if (ret > 0) {
4388fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
4389fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
4390fcf3ce44SJohn Forte 	}
4391fcf3ce44SJohn Forte 
4392fcf3ce44SJohn Forte 	if (ret != 0) {
4393fcf3ce44SJohn Forte 		return (ret);
4394fcf3ce44SJohn Forte 	}
4395fcf3ce44SJohn Forte 
4396fcf3ce44SJohn Forte 	status = IMA_GetStaticDiscoveryTargetOidList(initiatorOid,
4397fcf3ce44SJohn Forte 	    &staticTargetList);
4398fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
4399fcf3ce44SJohn Forte 		printLibError(status);
4400fcf3ce44SJohn Forte 		*funcRet = 1;
4401fcf3ce44SJohn Forte 		return (ret);
4402fcf3ce44SJohn Forte 	}
4403fcf3ce44SJohn Forte 
4404fcf3ce44SJohn Forte 	for (i = 0; i < operandLen; i++) {
4405fcf3ce44SJohn Forte 		if (parseTarget(operand[i],
4406fcf3ce44SJohn Forte 		    &staticTargetName[0],
4407fcf3ce44SJohn Forte 		    MAX_ISCSI_NAME_LEN + 1,
4408fcf3ce44SJohn Forte 		    &targetAddressSpecified,
4409fcf3ce44SJohn Forte 		    &staticTargetAddress[0],
4410fcf3ce44SJohn Forte 		    SUN_IMA_IP_ADDRESS_PORT_LEN,
4411fcf3ce44SJohn Forte 		    &port,
4412fcf3ce44SJohn Forte 		    &tpgtSpecified,
4413fcf3ce44SJohn Forte 		    &tpgt,
4414fcf3ce44SJohn Forte 		    &isIpv6) != PARSE_TARGET_OK) {
4415fcf3ce44SJohn Forte 			ret = 1;
4416fcf3ce44SJohn Forte 			continue;
4417fcf3ce44SJohn Forte 		}
4418fcf3ce44SJohn Forte 
4419fcf3ce44SJohn Forte 		/* Perform string profile checks */
4420fcf3ce44SJohn Forte 		nameCheckStatus = iSCSINameStringProfileCheck(staticTargetName);
4421fcf3ce44SJohn Forte 		iSCSINameCheckStatusDisplay(nameCheckStatus);
4422fcf3ce44SJohn Forte 		if (nameCheckStatus != iSCSINameCheckOK) {
4423fcf3ce44SJohn Forte 			return (1);
4424fcf3ce44SJohn Forte 		}
4425fcf3ce44SJohn Forte 
4426fcf3ce44SJohn Forte 		for (atLeastFoundOne = B_FALSE, j = 0;
4427fcf3ce44SJohn Forte 		    j < staticTargetList->oidCount;
4428fcf3ce44SJohn Forte 		    j++) {
4429fcf3ce44SJohn Forte 			IMA_UINT16 stpgt;
4430fcf3ce44SJohn Forte 
4431fcf3ce44SJohn Forte 			matched = B_FALSE;
4432fcf3ce44SJohn Forte 			status = SUN_IMA_GetStaticTargetProperties(
4433fcf3ce44SJohn Forte 			    staticTargetList->oids[j], &staticTargetProps);
4434fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
4435fcf3ce44SJohn Forte 				if (status == IMA_ERROR_OBJECT_NOT_FOUND) {
4436fcf3ce44SJohn Forte 					/*
4437fcf3ce44SJohn Forte 					 * When removing multiple static-config
4438fcf3ce44SJohn Forte 					 * entries we need to expect get
4439fcf3ce44SJohn Forte 					 * failures. These failures occur when
4440fcf3ce44SJohn Forte 					 * we are trying to get entry
4441fcf3ce44SJohn Forte 					 * information we have just removed.
4442fcf3ce44SJohn Forte 					 * Ignore the failure and continue.
4443fcf3ce44SJohn Forte 					 */
4444fcf3ce44SJohn Forte 					ret = 1;
4445fcf3ce44SJohn Forte 					continue;
4446fcf3ce44SJohn Forte 				} else {
4447fcf3ce44SJohn Forte 					printLibError(status);
4448fcf3ce44SJohn Forte 					(void) IMA_FreeMemory(staticTargetList);
4449fcf3ce44SJohn Forte 					*funcRet = 1;
4450fcf3ce44SJohn Forte 					return (ret);
4451fcf3ce44SJohn Forte 				}
4452fcf3ce44SJohn Forte 			}
4453fcf3ce44SJohn Forte 
4454fcf3ce44SJohn Forte 			stpgt =
4455fcf3ce44SJohn Forte 			    staticTargetProps.staticTarget.targetAddress.tpgt;
4456fcf3ce44SJohn Forte 
4457fcf3ce44SJohn Forte 			/*
4458fcf3ce44SJohn Forte 			 * Compare the static target name with the input if
4459fcf3ce44SJohn Forte 			 * one was input
4460fcf3ce44SJohn Forte 			 */
4461fcf3ce44SJohn Forte 			if ((targetNamesEqual(
4462fcf3ce44SJohn Forte 			    staticTargetProps.staticTarget.targetName,
4463fcf3ce44SJohn Forte 			    staticTargetName) == B_TRUE)) {
4464fcf3ce44SJohn Forte 				if (targetAddressSpecified == B_FALSE) {
4465fcf3ce44SJohn Forte 					matched = B_TRUE;
4466fcf3ce44SJohn Forte 				} else {
4467fcf3ce44SJohn Forte 
4468fcf3ce44SJohn Forte 					if (staticTargetProps.staticTarget.
4469fcf3ce44SJohn Forte 					    targetAddress.imaStruct.
4470fcf3ce44SJohn Forte 					    hostnameIpAddress.
4471fcf3ce44SJohn Forte 					    id.ipAddress.ipv4Address ==
4472fcf3ce44SJohn Forte 					    IMA_TRUE) {
4473fcf3ce44SJohn Forte 						(void) inet_ntop(AF_INET,
4474fcf3ce44SJohn Forte 						    staticTargetProps.
4475fcf3ce44SJohn Forte 						    staticTarget.targetAddress.
4476fcf3ce44SJohn Forte 						    imaStruct.hostnameIpAddress.
4477fcf3ce44SJohn Forte 						    id.ipAddress.ipAddress,
4478fcf3ce44SJohn Forte 						    tmpStr,
4479fcf3ce44SJohn Forte 						    sizeof (tmpStr));
4480fcf3ce44SJohn Forte 					} else {
4481fcf3ce44SJohn Forte 						(void) inet_ntop(AF_INET6,
4482fcf3ce44SJohn Forte 						    staticTargetProps.
4483fcf3ce44SJohn Forte 						    staticTarget.targetAddress.
4484fcf3ce44SJohn Forte 						    imaStruct.hostnameIpAddress.
4485fcf3ce44SJohn Forte 						    id.ipAddress.ipAddress,
4486fcf3ce44SJohn Forte 						    tmpStr,
4487fcf3ce44SJohn Forte 						    sizeof (tmpStr));
4488fcf3ce44SJohn Forte 					}
4489fcf3ce44SJohn Forte 
4490fcf3ce44SJohn Forte 					if (mbstowcs(tmpTargetAddress, tmpStr,
4491fcf3ce44SJohn Forte 					    SUN_IMA_IP_ADDRESS_PORT_LEN) ==
4492fcf3ce44SJohn Forte 					    (size_t)-1) {
4493fcf3ce44SJohn Forte 						(void) fprintf(stderr,
4494fcf3ce44SJohn Forte 						    "%s: %s\n",
4495fcf3ce44SJohn Forte 						    cmdName, gettext(
4496fcf3ce44SJohn Forte 						    "conversion error"));
4497fcf3ce44SJohn Forte 						ret = 1;
4498fcf3ce44SJohn Forte 						continue;
4499fcf3ce44SJohn Forte 					}
4500fcf3ce44SJohn Forte 
4501fcf3ce44SJohn Forte 					if ((wcsncmp(tmpTargetAddress,
4502fcf3ce44SJohn Forte 					    staticTargetAddress,
4503fcf3ce44SJohn Forte 					    SUN_IMA_IP_ADDRESS_PORT_LEN) ==
4504fcf3ce44SJohn Forte 					    0) && (staticTargetProps.
4505fcf3ce44SJohn Forte 					    staticTarget.targetAddress.
4506fcf3ce44SJohn Forte 					    imaStruct.portNumber == port)) {
4507fcf3ce44SJohn Forte 						if (tpgtSpecified == B_FALSE) {
4508fcf3ce44SJohn Forte 							matched = B_TRUE;
4509fcf3ce44SJohn Forte 						} else {
4510fcf3ce44SJohn Forte 							if (tpgt == stpgt) {
4511fcf3ce44SJohn Forte 								matched =
4512fcf3ce44SJohn Forte 								    B_TRUE;
4513fcf3ce44SJohn Forte 							}
4514fcf3ce44SJohn Forte 						}
4515fcf3ce44SJohn Forte 					}
4516fcf3ce44SJohn Forte 				}
4517fcf3ce44SJohn Forte 
4518fcf3ce44SJohn Forte 				if (matched) {
4519fcf3ce44SJohn Forte 					status =
4520fcf3ce44SJohn Forte 					    IMA_RemoveStaticDiscoveryTarget(
4521fcf3ce44SJohn Forte 					    staticTargetList->oids[j]);
4522fcf3ce44SJohn Forte 					if (!IMA_SUCCESS(status)) {
4523fcf3ce44SJohn Forte 						printLibError(status);
4524fcf3ce44SJohn Forte 						*funcRet = 1;
4525fcf3ce44SJohn Forte 						return (ret);
4526fcf3ce44SJohn Forte 					}
4527fcf3ce44SJohn Forte 					atLeastFoundOne = B_TRUE;
4528fcf3ce44SJohn Forte 				}
4529fcf3ce44SJohn Forte 			}
4530fcf3ce44SJohn Forte 		}
4531fcf3ce44SJohn Forte 		if (!atLeastFoundOne) {
4532fcf3ce44SJohn Forte 			(void) fprintf(stderr, gettext("%ws,%ws: %s\n"),
4533fcf3ce44SJohn Forte 			    staticTargetName, staticTargetAddress,
4534fcf3ce44SJohn Forte 			    gettext("not found"));
4535fcf3ce44SJohn Forte 		}
4536fcf3ce44SJohn Forte 	}
4537fcf3ce44SJohn Forte 	return (ret);
4538fcf3ce44SJohn Forte }
4539fcf3ce44SJohn Forte 
4540fcf3ce44SJohn Forte /*
4541fcf3ce44SJohn Forte  * Remove one or more target params.
4542fcf3ce44SJohn Forte  */
4543fcf3ce44SJohn Forte static int
4544fcf3ce44SJohn Forte removeTargetParam(int operandLen, char *operand[], int *funcRet)
4545fcf3ce44SJohn Forte {
4546fcf3ce44SJohn Forte 	char *commaPos;
4547fcf3ce44SJohn Forte 	IMA_STATUS status;
4548fcf3ce44SJohn Forte 	IMA_OID initiatorOid;
4549fcf3ce44SJohn Forte 	IMA_OID_LIST *targetList;
4550fcf3ce44SJohn Forte 	SUN_IMA_TARGET_PROPERTIES targetProps;
4551fcf3ce44SJohn Forte 	wchar_t wcInputObject[MAX_ISCSI_NAME_LEN + 1];
4552fcf3ce44SJohn Forte 	int ret;
4553fcf3ce44SJohn Forte 	boolean_t found;
4554fcf3ce44SJohn Forte 	int i, j;
45556cefaae1SJack Meng 	IMA_NODE_NAME bootTargetName;
45566cefaae1SJack Meng 	IMA_BOOL	iscsiBoot = IMA_FALSE;
45576cefaae1SJack Meng 	IMA_BOOL	mpxioEnabled = IMA_FALSE;
45586cefaae1SJack Meng 
45596cefaae1SJack Meng 	/* Get boot session's info */
45606cefaae1SJack Meng 	(void) SUN_IMA_GetBootIscsi(&iscsiBoot);
45616cefaae1SJack Meng 	if (iscsiBoot == IMA_TRUE) {
45626cefaae1SJack Meng 		status = SUN_IMA_GetBootMpxio(&mpxioEnabled);
45636cefaae1SJack Meng 		if (!IMA_SUCCESS(status)) {
45646cefaae1SJack Meng 			(void) fprintf(stderr, "%s: %s\n",
45656cefaae1SJack Meng 			    cmdName, gettext("unable to get MPxIO info of"
45666cefaae1SJack Meng 			    " root disk"));
45676cefaae1SJack Meng 			*funcRet = 1;
45686cefaae1SJack Meng 			return (1);
45696cefaae1SJack Meng 		}
45706cefaae1SJack Meng 		status = SUN_IMA_GetBootTargetName(bootTargetName);
45716cefaae1SJack Meng 		if (!IMA_SUCCESS(status)) {
45726cefaae1SJack Meng 			(void) fprintf(stderr, "%s: %s\n",
45736cefaae1SJack Meng 			    cmdName, gettext("unable to get boot"
45746cefaae1SJack Meng 			    " target's name"));
45756cefaae1SJack Meng 			*funcRet = 1;
45766cefaae1SJack Meng 			return (1);
45776cefaae1SJack Meng 		}
45786cefaae1SJack Meng 	}
4579fcf3ce44SJohn Forte 
4580fcf3ce44SJohn Forte 	assert(funcRet != NULL);
4581fcf3ce44SJohn Forte 
4582fcf3ce44SJohn Forte 	/* Find Sun initiator */
4583fcf3ce44SJohn Forte 	ret = sunInitiatorFind(&initiatorOid);
4584fcf3ce44SJohn Forte 	if (ret > 0) {
4585fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
4586fcf3ce44SJohn Forte 		    cmdName, gettext("no initiator found"));
4587fcf3ce44SJohn Forte 	}
4588fcf3ce44SJohn Forte 
4589fcf3ce44SJohn Forte 	if (ret != 0) {
4590fcf3ce44SJohn Forte 		return (ret);
4591fcf3ce44SJohn Forte 	}
4592fcf3ce44SJohn Forte 
4593fcf3ce44SJohn Forte 	status = IMA_GetTargetOidList(initiatorOid, &targetList);
4594fcf3ce44SJohn Forte 	if (!IMA_SUCCESS(status)) {
4595fcf3ce44SJohn Forte 		printLibError(status);
4596fcf3ce44SJohn Forte 		*funcRet = 1;
4597fcf3ce44SJohn Forte 		return (ret);
4598fcf3ce44SJohn Forte 	}
4599fcf3ce44SJohn Forte 
4600fcf3ce44SJohn Forte 	for (i = 0; i < operandLen; i++) {
4601fcf3ce44SJohn Forte 		/* initialize */
4602fcf3ce44SJohn Forte 		commaPos = strchr(operand[i], ',');
4603fcf3ce44SJohn Forte 		if (commaPos) {
4604fcf3ce44SJohn Forte 			/* Ignore IP address. */
4605fcf3ce44SJohn Forte 			*commaPos = NULL;
4606fcf3ce44SJohn Forte 		}
4607fcf3ce44SJohn Forte 		(void) memset(&wcInputObject[0], 0, sizeof (wcInputObject));
4608fcf3ce44SJohn Forte 		if (mbstowcs(wcInputObject, operand[i],
4609fcf3ce44SJohn Forte 		    MAX_ISCSI_NAME_LEN + 1) == (size_t)-1) {
4610fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n", cmdName,
4611fcf3ce44SJohn Forte 			    gettext("conversion error"));
4612fcf3ce44SJohn Forte 			ret = 1;
4613fcf3ce44SJohn Forte 			continue;
4614fcf3ce44SJohn Forte 		}
4615fcf3ce44SJohn Forte 
4616fcf3ce44SJohn Forte 		for (found = B_FALSE, j = 0; j < targetList->oidCount;
4617fcf3ce44SJohn Forte 		    j++) {
4618fcf3ce44SJohn Forte 			status = SUN_IMA_GetTargetProperties(
4619fcf3ce44SJohn Forte 			    targetList->oids[j], &targetProps);
4620fcf3ce44SJohn Forte 			if (!IMA_SUCCESS(status)) {
4621fcf3ce44SJohn Forte 				printLibError(status);
4622fcf3ce44SJohn Forte 				(void) IMA_FreeMemory(targetList);
4623fcf3ce44SJohn Forte 				*funcRet = 1;
4624fcf3ce44SJohn Forte 				return (ret);
4625fcf3ce44SJohn Forte 			}
4626fcf3ce44SJohn Forte 
4627fcf3ce44SJohn Forte 			/*
4628fcf3ce44SJohn Forte 			 * Compare the target name with the input if
4629fcf3ce44SJohn Forte 			 * one was input
4630fcf3ce44SJohn Forte 			 */
4631fcf3ce44SJohn Forte 			if (targetNamesEqual(targetProps.imaProps.name,
4632fcf3ce44SJohn Forte 			    wcInputObject) == B_TRUE) {
4633fcf3ce44SJohn Forte 				found = B_TRUE;
46346cefaae1SJack Meng 				if ((targetNamesEqual(bootTargetName,
46356cefaae1SJack Meng 				    wcInputObject) == B_TRUE) &&
46366cefaae1SJack Meng 				    (iscsiBoot == IMA_TRUE)) {
46376cefaae1SJack Meng 					/*
46386cefaae1SJack Meng 					 * iscsi booting, need changed target
46396cefaae1SJack Meng 					 * param is booting target, booting
46406cefaae1SJack Meng 					 * session mpxio disabled, not
46416cefaae1SJack Meng 					 * allow to update
46426cefaae1SJack Meng 					 */
46436cefaae1SJack Meng 					if (mpxioEnabled == IMA_FALSE) {
46446cefaae1SJack Meng 						(void) fprintf(stderr,
46456cefaae1SJack Meng 						    "%s: %s\n", cmdName,
46466cefaae1SJack Meng 						    gettext("iscsi boot"
46476cefaae1SJack Meng 						    " with MPxIO disabled,"
46486cefaae1SJack Meng 						    " not allowed to remove"
46496cefaae1SJack Meng 						    " boot sess param"));
4650f03fb72cSyi zhang - Sun Microsystems - Beijing China 						ret = 1;
46516cefaae1SJack Meng 						continue;
46526cefaae1SJack Meng 					}
46536cefaae1SJack Meng 
46546cefaae1SJack Meng 				}
46556cefaae1SJack Meng 
4656fcf3ce44SJohn Forte 				status = SUN_IMA_RemoveTargetParam(
4657fcf3ce44SJohn Forte 				    targetList->oids[j]);
4658fcf3ce44SJohn Forte 				if (!IMA_SUCCESS(status)) {
4659fcf3ce44SJohn Forte 					printLibError(status);
4660fcf3ce44SJohn Forte 					(void) IMA_FreeMemory(targetList);
4661fcf3ce44SJohn Forte 					*funcRet = 1;
4662fcf3ce44SJohn Forte 					return (ret);
4663fcf3ce44SJohn Forte 				}
4664fcf3ce44SJohn Forte 			}
4665fcf3ce44SJohn Forte 		}
4666fcf3ce44SJohn Forte 		if (!found) {
4667fcf3ce44SJohn Forte 			/* Silently ignoring it? */
4668fcf3ce44SJohn Forte 			(void) fprintf(stderr, gettext("%ws: %s\n"),
4669fcf3ce44SJohn Forte 			    wcInputObject, gettext("not found"));
4670fcf3ce44SJohn Forte 		}
4671fcf3ce44SJohn Forte 	}
4672fcf3ce44SJohn Forte 
4673fcf3ce44SJohn Forte 	(void) IMA_FreeMemory(targetList);
4674fcf3ce44SJohn Forte 	return (ret);
4675fcf3ce44SJohn Forte }
4676fcf3ce44SJohn Forte 
4677fcf3ce44SJohn Forte /*ARGSUSED*/
4678fcf3ce44SJohn Forte static int
4679fcf3ce44SJohn Forte addFunc(int operandLen, char *operand[], int object, cmdOptions_t *options,
4680fcf3ce44SJohn Forte     void *addArgs, int *funcRet)
4681fcf3ce44SJohn Forte {
4682fcf3ce44SJohn Forte 	int ret;
4683fcf3ce44SJohn Forte 
4684fcf3ce44SJohn Forte 	assert(funcRet != NULL);
4685fcf3ce44SJohn Forte 
4686fcf3ce44SJohn Forte 	switch (object) {
4687fcf3ce44SJohn Forte 		case DISCOVERY_ADDRESS:
4688fcf3ce44SJohn Forte 		case ISNS_SERVER_ADDRESS:
4689fcf3ce44SJohn Forte 			ret = addAddress(object, operandLen, operand, funcRet);
4690fcf3ce44SJohn Forte 			break;
4691fcf3ce44SJohn Forte 		case STATIC_CONFIG:
4692fcf3ce44SJohn Forte 			ret = addStaticConfig(operandLen, operand, funcRet);
4693fcf3ce44SJohn Forte 			break;
4694fcf3ce44SJohn Forte 		default:
4695fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4696fcf3ce44SJohn Forte 			    cmdName, gettext("unknown object"));
4697fcf3ce44SJohn Forte 			ret = 1;
4698fcf3ce44SJohn Forte 			break;
4699fcf3ce44SJohn Forte 	}
4700fcf3ce44SJohn Forte 	return (ret);
4701fcf3ce44SJohn Forte }
4702fcf3ce44SJohn Forte 
4703fcf3ce44SJohn Forte /*ARGSUSED*/
4704fcf3ce44SJohn Forte static int
4705fcf3ce44SJohn Forte listFunc(int operandLen, char *operand[], int object, cmdOptions_t *options,
4706fcf3ce44SJohn Forte     void *addArgs, int *funcRet)
4707fcf3ce44SJohn Forte {
4708fcf3ce44SJohn Forte 	int ret;
4709fcf3ce44SJohn Forte 
4710fcf3ce44SJohn Forte 	assert(funcRet != NULL);
4711fcf3ce44SJohn Forte 
4712fcf3ce44SJohn Forte 	switch (object) {
4713fcf3ce44SJohn Forte 	case DISCOVERY:
4714fcf3ce44SJohn Forte 		ret = listDiscovery(funcRet);
4715fcf3ce44SJohn Forte 		break;
4716fcf3ce44SJohn Forte 	case DISCOVERY_ADDRESS:
4717fcf3ce44SJohn Forte 		ret = listDiscoveryAddress(operandLen, operand, options,
4718fcf3ce44SJohn Forte 		    funcRet);
4719fcf3ce44SJohn Forte 		break;
4720fcf3ce44SJohn Forte 	case ISNS_SERVER_ADDRESS:
4721fcf3ce44SJohn Forte 		ret = listISNSServerAddress(operandLen, operand, options,
4722fcf3ce44SJohn Forte 		    funcRet);
4723fcf3ce44SJohn Forte 		break;
4724fcf3ce44SJohn Forte 	case NODE:
4725fcf3ce44SJohn Forte 		ret = listNode(funcRet);
4726fcf3ce44SJohn Forte 		break;
4727fcf3ce44SJohn Forte 	case STATIC_CONFIG:
4728fcf3ce44SJohn Forte 		ret = listStaticConfig(operandLen, operand, funcRet);
4729fcf3ce44SJohn Forte 		break;
4730fcf3ce44SJohn Forte 	case TARGET:
4731fcf3ce44SJohn Forte 		ret = listTarget(operandLen, operand, options, funcRet);
4732fcf3ce44SJohn Forte 		break;
4733fcf3ce44SJohn Forte 	case TARGET_PARAM:
4734fcf3ce44SJohn Forte 		ret = listTargetParam(operandLen, operand, options, funcRet);
4735fcf3ce44SJohn Forte 		break;
4736fcf3ce44SJohn Forte 	default:
4737fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
4738fcf3ce44SJohn Forte 		    cmdName, gettext("unknown object"));
4739fcf3ce44SJohn Forte 		ret = 1;
4740fcf3ce44SJohn Forte 		break;
4741fcf3ce44SJohn Forte 	}
4742fcf3ce44SJohn Forte 	return (ret);
4743fcf3ce44SJohn Forte }
4744fcf3ce44SJohn Forte 
4745fcf3ce44SJohn Forte /*ARGSUSED*/
4746fcf3ce44SJohn Forte static int
4747fcf3ce44SJohn Forte modifyFunc(int operandLen, char *operand[], int object, cmdOptions_t *options,
4748fcf3ce44SJohn Forte     void *addArgs, int *funcRet)
4749fcf3ce44SJohn Forte {
4750fcf3ce44SJohn Forte 	int ret, i;
4751fcf3ce44SJohn Forte 
4752fcf3ce44SJohn Forte 	assert(funcRet != NULL);
4753fcf3ce44SJohn Forte 
4754fcf3ce44SJohn Forte 	switch (object) {
4755fcf3ce44SJohn Forte 	case DISCOVERY:
4756fcf3ce44SJohn Forte 		ret = modifyDiscovery(options, funcRet);
4757fcf3ce44SJohn Forte 		break;
4758fcf3ce44SJohn Forte 	case NODE:
4759fcf3ce44SJohn Forte 		ret = modifyNode(options, funcRet);
4760fcf3ce44SJohn Forte 		break;
4761fcf3ce44SJohn Forte 	case TARGET_PARAM:
4762fcf3ce44SJohn Forte 		i = 0;
4763fcf3ce44SJohn Forte 		while (operand[i]) {
4764fcf3ce44SJohn Forte 			ret = modifyTargetParam(options, operand[i], funcRet);
4765fcf3ce44SJohn Forte 
4766fcf3ce44SJohn Forte 			if (ret) {
4767fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s: %s\n",
4768fcf3ce44SJohn Forte 				    cmdName, gettext("modify failed"),
4769fcf3ce44SJohn Forte 				    operand[i]);
4770fcf3ce44SJohn Forte 				return (ret);
4771fcf3ce44SJohn Forte 			}
4772fcf3ce44SJohn Forte 			i++;
4773fcf3ce44SJohn Forte 		}
4774fcf3ce44SJohn Forte 
4775fcf3ce44SJohn Forte 		break;
4776fcf3ce44SJohn Forte 	default:
4777fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
4778fcf3ce44SJohn Forte 		    cmdName, gettext("unknown object"));
4779fcf3ce44SJohn Forte 		ret = 1;
4780fcf3ce44SJohn Forte 		break;
4781fcf3ce44SJohn Forte 	}
4782fcf3ce44SJohn Forte 	return (ret);
4783fcf3ce44SJohn Forte }
4784fcf3ce44SJohn Forte 
4785fcf3ce44SJohn Forte /*ARGSUSED*/
4786fcf3ce44SJohn Forte static int
4787fcf3ce44SJohn Forte removeFunc(int operandLen, char *operand[], int object, cmdOptions_t *options,
4788fcf3ce44SJohn Forte     void *addArgs, int *funcRet)
4789fcf3ce44SJohn Forte {
4790fcf3ce44SJohn Forte 	int ret;
4791fcf3ce44SJohn Forte 
4792fcf3ce44SJohn Forte 	switch (object) {
4793fcf3ce44SJohn Forte 		case DISCOVERY_ADDRESS:
4794fcf3ce44SJohn Forte 		case ISNS_SERVER_ADDRESS:
4795fcf3ce44SJohn Forte 			ret = removeAddress(object, operandLen, operand,
4796fcf3ce44SJohn Forte 			    funcRet);
4797fcf3ce44SJohn Forte 			break;
4798fcf3ce44SJohn Forte 		case STATIC_CONFIG:
4799fcf3ce44SJohn Forte 			ret = removeStaticConfig(operandLen, operand, funcRet);
4800fcf3ce44SJohn Forte 			break;
4801fcf3ce44SJohn Forte 		case TARGET_PARAM:
4802fcf3ce44SJohn Forte 			ret = removeTargetParam(operandLen, operand, funcRet);
4803fcf3ce44SJohn Forte 			break;
4804fcf3ce44SJohn Forte 		default:
4805fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4806fcf3ce44SJohn Forte 			    cmdName, gettext("unknown object"));
4807fcf3ce44SJohn Forte 			ret = 1;
4808fcf3ce44SJohn Forte 			break;
4809fcf3ce44SJohn Forte 	}
4810fcf3ce44SJohn Forte 	return (ret);
4811fcf3ce44SJohn Forte }
4812fcf3ce44SJohn Forte 
4813fcf3ce44SJohn Forte static void
4814fcf3ce44SJohn Forte iSCSINameCheckStatusDisplay(iSCSINameCheckStatusType status)
4815fcf3ce44SJohn Forte {
4816fcf3ce44SJohn Forte 	switch (status) {
4817fcf3ce44SJohn Forte 		case iSCSINameLenZero:
4818fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4819fcf3ce44SJohn Forte 			    cmdName, gettext("empty iSCSI name."));
4820fcf3ce44SJohn Forte 			break;
4821fcf3ce44SJohn Forte 		case iSCSINameLenExceededMax:
4822fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n", cmdName,
4823fcf3ce44SJohn Forte 			    gettext("iSCSI name exceeded maximum length."));
4824fcf3ce44SJohn Forte 			break;
4825fcf3ce44SJohn Forte 		case iSCSINameUnknownType:
4826fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n", cmdName,
4827fcf3ce44SJohn Forte 			    gettext("unknown iSCSI name type."));
4828fcf3ce44SJohn Forte 			break;
4829fcf3ce44SJohn Forte 		case iSCSINameInvalidCharacter:
4830fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4831fcf3ce44SJohn Forte 			    cmdName,
4832fcf3ce44SJohn Forte 			    gettext("iSCSI name invalid character used"));
4833fcf3ce44SJohn Forte 			break;
4834fcf3ce44SJohn Forte 		case iSCSINameIqnFormatError:
4835fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n", cmdName,
4836fcf3ce44SJohn Forte 			    gettext("iqn formatting error."));
4837fcf3ce44SJohn Forte 			break;
4838fcf3ce44SJohn Forte 		case iSCSINameIqnDateFormatError:
4839fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4840fcf3ce44SJohn Forte 			    cmdName, gettext("invalid iqn date." \
4841fcf3ce44SJohn Forte 			    "  format is: YYYY-MM"));
4842fcf3ce44SJohn Forte 			break;
4843fcf3ce44SJohn Forte 		case iSCSINameIqnSubdomainFormatError:
4844fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4845fcf3ce44SJohn Forte 			    cmdName, gettext("missing subdomain after \":\""));
4846fcf3ce44SJohn Forte 			break;
4847fcf3ce44SJohn Forte 		case iSCSINameIqnInvalidYearError:
4848fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4849fcf3ce44SJohn Forte 			    cmdName, gettext("invalid year"));
4850fcf3ce44SJohn Forte 			break;
4851fcf3ce44SJohn Forte 		case iSCSINameIqnInvalidMonthError:
4852fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4853fcf3ce44SJohn Forte 			    cmdName, gettext("invalid month"));
4854fcf3ce44SJohn Forte 			break;
4855fcf3ce44SJohn Forte 		case iSCSINameIqnFQDNError:
4856fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
4857fcf3ce44SJohn Forte 			    cmdName, gettext("missing reversed fully qualified"\
4858fcf3ce44SJohn Forte 			    " domain name"));
4859fcf3ce44SJohn Forte 			break;
4860fcf3ce44SJohn Forte 		case iSCSINameEUIFormatError:
4861fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n", cmdName,
4862fcf3ce44SJohn Forte 			    gettext("eui formatting error."));
4863fcf3ce44SJohn Forte 			break;
4864fcf3ce44SJohn Forte 	}
4865fcf3ce44SJohn Forte }
4866fcf3ce44SJohn Forte 
4867fcf3ce44SJohn Forte /*
4868fcf3ce44SJohn Forte  * A convenient function to modify the target parameters of an individual
4869fcf3ce44SJohn Forte  * target.
4870fcf3ce44SJohn Forte  *
4871fcf3ce44SJohn Forte  * Return 0 if successful
4872fcf3ce44SJohn Forte  * Return 1 if failed
4873fcf3ce44SJohn Forte  */
4874fcf3ce44SJohn Forte static int
4875fcf3ce44SJohn Forte modifyIndividualTargetParam(cmdOptions_t *optionList, IMA_OID targetOid,
4876fcf3ce44SJohn Forte     int *funcRet)
4877fcf3ce44SJohn Forte {
4878fcf3ce44SJohn Forte 	assert(funcRet != NULL);
4879fcf3ce44SJohn Forte 
4880fcf3ce44SJohn Forte 	for (; optionList->optval; optionList++) {
4881fcf3ce44SJohn Forte 		switch (optionList->optval) {
4882fcf3ce44SJohn Forte 			case 'a':
4883fcf3ce44SJohn Forte 				if (modifyTargetAuthMethod(targetOid,
4884fcf3ce44SJohn Forte 				    optionList->optarg, funcRet) != 0) {
4885fcf3ce44SJohn Forte 					return (1);
4886fcf3ce44SJohn Forte 				}
4887fcf3ce44SJohn Forte 				break;
4888fcf3ce44SJohn Forte 			case 'B':
4889fcf3ce44SJohn Forte 				if (modifyTargetBidirAuthFlag(targetOid,
4890fcf3ce44SJohn Forte 				    optionList->optarg, funcRet) != 0) {
4891fcf3ce44SJohn Forte 					return (1);
4892fcf3ce44SJohn Forte 				}
4893fcf3ce44SJohn Forte 				break;
4894fcf3ce44SJohn Forte 			case 'C':
4895fcf3ce44SJohn Forte 				if (modifyTargetAuthParam(targetOid,
4896fcf3ce44SJohn Forte 				    AUTH_PASSWORD, NULL, funcRet) != 0) {
4897fcf3ce44SJohn Forte 					return (1);
4898fcf3ce44SJohn Forte 				}
4899fcf3ce44SJohn Forte 				break;
4900fcf3ce44SJohn Forte 			case 'd':
4901fcf3ce44SJohn Forte 				if (setLoginParameter(targetOid, DATA_DIGEST,
4902fcf3ce44SJohn Forte 				    optionList->optarg) != 0) {
4903fcf3ce44SJohn Forte 					return (1);
4904fcf3ce44SJohn Forte 				}
4905fcf3ce44SJohn Forte 				break;
4906fcf3ce44SJohn Forte 			case 'h':
4907fcf3ce44SJohn Forte 				if (setLoginParameter(targetOid, HEADER_DIGEST,
4908fcf3ce44SJohn Forte 				    optionList->optarg) != 0) {
4909fcf3ce44SJohn Forte 					return (1);
4910fcf3ce44SJohn Forte 				}
4911fcf3ce44SJohn Forte 				break;
4912fcf3ce44SJohn Forte 			case 'p':
4913fcf3ce44SJohn Forte 				/* Login parameter */
4914fcf3ce44SJohn Forte 				if (setLoginParameters(targetOid,
4915fcf3ce44SJohn Forte 				    optionList->optarg) != 0) {
4916fcf3ce44SJohn Forte 					return (1);
4917fcf3ce44SJohn Forte 				}
4918fcf3ce44SJohn Forte 				break;
4919fcf3ce44SJohn Forte 			case 'c':
4920fcf3ce44SJohn Forte 				/* Modify configure sessions */
4921fcf3ce44SJohn Forte 				if (modifyConfiguredSessions(targetOid,
4922fcf3ce44SJohn Forte 				    optionList->optarg) != 0) {
4923fcf3ce44SJohn Forte 					return (1);
4924fcf3ce44SJohn Forte 				}
4925fcf3ce44SJohn Forte 				break;
4926fcf3ce44SJohn Forte 			case 'H':
4927fcf3ce44SJohn Forte 				if (modifyTargetAuthParam(targetOid, AUTH_NAME,
4928fcf3ce44SJohn Forte 				    optionList->optarg, funcRet) != 0) {
4929fcf3ce44SJohn Forte 					return (1);
4930fcf3ce44SJohn Forte 				}
4931fcf3ce44SJohn Forte 				break;
4932fcf3ce44SJohn Forte 		}
4933fcf3ce44SJohn Forte 	}
4934fcf3ce44SJohn Forte 
4935fcf3ce44SJohn Forte 	return (0);
4936fcf3ce44SJohn Forte }
4937fcf3ce44SJohn Forte 
4938fcf3ce44SJohn Forte /*
4939fcf3ce44SJohn Forte  * This helper function could go into a utility module for general use.
4940fcf3ce44SJohn Forte  */
4941fcf3ce44SJohn Forte static int
4942fcf3ce44SJohn Forte parseAddress(char *address_port_str,
4943fcf3ce44SJohn Forte     uint16_t defaultPort,
4944fcf3ce44SJohn Forte     char *address_str,
4945fcf3ce44SJohn Forte     size_t address_str_len,
4946fcf3ce44SJohn Forte     uint16_t *port,
4947fcf3ce44SJohn Forte     boolean_t *isIpv6)
4948fcf3ce44SJohn Forte {
4949fcf3ce44SJohn Forte 	char port_str[64];
4950fcf3ce44SJohn Forte 	int tmp_port;
4951fcf3ce44SJohn Forte 	char *errchr;
4952fcf3ce44SJohn Forte 
4953fcf3ce44SJohn Forte 	if (address_port_str[0] == '[') {
4954fcf3ce44SJohn Forte 		/* IPv6 address */
4955fcf3ce44SJohn Forte 		char *close_bracket_pos;
4956fcf3ce44SJohn Forte 		close_bracket_pos = strchr(address_port_str, ']');
4957fcf3ce44SJohn Forte 		if (!close_bracket_pos) {
4958fcf3ce44SJohn Forte 			syslog(LOG_USER|LOG_DEBUG,
4959fcf3ce44SJohn Forte 			    "IP address format error: %s\n", address_str);
4960fcf3ce44SJohn Forte 			return (PARSE_ADDR_MISSING_CLOSING_BRACKET);
4961fcf3ce44SJohn Forte 		}
4962fcf3ce44SJohn Forte 
4963fcf3ce44SJohn Forte 		*close_bracket_pos = NULL;
4964fcf3ce44SJohn Forte 		(void) strlcpy(address_str, &address_port_str[1],
4965fcf3ce44SJohn Forte 		    address_str_len);
4966fcf3ce44SJohn Forte 
4967fcf3ce44SJohn Forte 		/* Extract the port number */
4968fcf3ce44SJohn Forte 		close_bracket_pos++;
4969fcf3ce44SJohn Forte 		if (*close_bracket_pos == ':') {
4970fcf3ce44SJohn Forte 			close_bracket_pos++;
4971fcf3ce44SJohn Forte 			if (*close_bracket_pos != NULL) {
4972fcf3ce44SJohn Forte 				(void) strlcpy(port_str, close_bracket_pos, 64);
4973fcf3ce44SJohn Forte 				tmp_port = strtol(port_str, &errchr, 10);
4974fcf3ce44SJohn Forte 				if (tmp_port == 0 && errchr != NULL) {
4975fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s: %s:%s %s\n",
4976fcf3ce44SJohn Forte 					    cmdName, address_str,
4977fcf3ce44SJohn Forte 					    close_bracket_pos,
4978fcf3ce44SJohn Forte 					    gettext("port number invalid"));
4979fcf3ce44SJohn Forte 					return (PARSE_ADDR_PORT_OUT_OF_RANGE);
4980fcf3ce44SJohn Forte 				}
4981fcf3ce44SJohn Forte 				if ((tmp_port > 0) && (tmp_port > USHRT_MAX) ||
4982fcf3ce44SJohn Forte 				    (tmp_port < 0)) {
4983fcf3ce44SJohn Forte 					/* Port number out of range */
4984fcf3ce44SJohn Forte 					syslog(LOG_USER|LOG_DEBUG,
4985fcf3ce44SJohn Forte 					    "Specified port out of range: %d",
4986fcf3ce44SJohn Forte 					    tmp_port);
4987fcf3ce44SJohn Forte 					return (PARSE_ADDR_PORT_OUT_OF_RANGE);
4988fcf3ce44SJohn Forte 				} else {
4989fcf3ce44SJohn Forte 					*port = (uint16_t)tmp_port;
4990fcf3ce44SJohn Forte 				}
4991fcf3ce44SJohn Forte 			} else {
4992fcf3ce44SJohn Forte 				*port = defaultPort;
4993fcf3ce44SJohn Forte 			}
4994fcf3ce44SJohn Forte 		} else {
4995fcf3ce44SJohn Forte 			*port = defaultPort;
4996fcf3ce44SJohn Forte 		}
4997fcf3ce44SJohn Forte 
4998fcf3ce44SJohn Forte 		*isIpv6 = B_TRUE;
4999fcf3ce44SJohn Forte 	} else {
5000fcf3ce44SJohn Forte 		/* IPv4 address */
5001fcf3ce44SJohn Forte 		char *colon_pos;
5002fcf3ce44SJohn Forte 		colon_pos = strchr(address_port_str, ':');
5003fcf3ce44SJohn Forte 		if (!colon_pos) {
5004fcf3ce44SJohn Forte 			/* No port number specified. */
5005fcf3ce44SJohn Forte 			*port = defaultPort;
5006fcf3ce44SJohn Forte 			(void) strlcpy(address_str, address_port_str,
5007fcf3ce44SJohn Forte 			    address_str_len);
5008fcf3ce44SJohn Forte 		} else {
5009fcf3ce44SJohn Forte 			*colon_pos = (char)NULL;
5010fcf3ce44SJohn Forte 			(void) strlcpy(address_str, address_port_str,
5011fcf3ce44SJohn Forte 			    address_str_len);
5012fcf3ce44SJohn Forte 
5013fcf3ce44SJohn Forte 			/* Extract the port number */
5014fcf3ce44SJohn Forte 			colon_pos++;
5015fcf3ce44SJohn Forte 			if (*colon_pos != NULL) {
5016fcf3ce44SJohn Forte 
5017fcf3ce44SJohn Forte 				(void) strlcpy(port_str, colon_pos, 64);
5018fcf3ce44SJohn Forte 				tmp_port = strtol(port_str, &errchr, 10);
5019fcf3ce44SJohn Forte 				if (tmp_port == 0 && errchr != NULL) {
5020fcf3ce44SJohn Forte 					(void) fprintf(stderr, "%s: %s:%s %s\n",
5021fcf3ce44SJohn Forte 					    cmdName, address_str, colon_pos,
5022fcf3ce44SJohn Forte 					    gettext("port number invalid"));
5023fcf3ce44SJohn Forte 					return (PARSE_ADDR_PORT_OUT_OF_RANGE);
5024fcf3ce44SJohn Forte 				}
5025fcf3ce44SJohn Forte 				if ((tmp_port > 0) && (tmp_port > USHRT_MAX) ||
5026fcf3ce44SJohn Forte 				    (tmp_port < 0)) {
5027fcf3ce44SJohn Forte 					/* Port number out of range */
5028fcf3ce44SJohn Forte 					syslog(LOG_USER|LOG_DEBUG,
5029fcf3ce44SJohn Forte 					    "Specified port out of range: %d",
5030fcf3ce44SJohn Forte 					    tmp_port);
5031fcf3ce44SJohn Forte 					return (PARSE_ADDR_PORT_OUT_OF_RANGE);
5032fcf3ce44SJohn Forte 				} else {
5033fcf3ce44SJohn Forte 					*port = (uint16_t)tmp_port;
5034fcf3ce44SJohn Forte 				}
5035fcf3ce44SJohn Forte 			} else {
5036fcf3ce44SJohn Forte 				*port = defaultPort;
5037fcf3ce44SJohn Forte 			}
5038fcf3ce44SJohn Forte 		}
5039fcf3ce44SJohn Forte 
5040fcf3ce44SJohn Forte 		*isIpv6 = B_FALSE;
5041fcf3ce44SJohn Forte 	}
5042fcf3ce44SJohn Forte 
5043fcf3ce44SJohn Forte 	return (PARSE_ADDR_OK);
5044fcf3ce44SJohn Forte }
5045fcf3ce44SJohn Forte 
5046fcf3ce44SJohn Forte /*
5047fcf3ce44SJohn Forte  * This helper function could go into a utility module for general use.
5048fcf3ce44SJohn Forte  */
5049fcf3ce44SJohn Forte iSCSINameCheckStatusType
5050fcf3ce44SJohn Forte iSCSINameStringProfileCheck(wchar_t *name)
5051fcf3ce44SJohn Forte {
5052fcf3ce44SJohn Forte 	char mb_name[MAX_ISCSI_NAME_LEN + 1];
5053fcf3ce44SJohn Forte 	size_t name_len;
5054fcf3ce44SJohn Forte 	char *tmp;
5055fcf3ce44SJohn Forte 
5056fcf3ce44SJohn Forte 	(void) wcstombs(mb_name, name, MAX_ISCSI_NAME_LEN + 1);
5057fcf3ce44SJohn Forte 
5058fcf3ce44SJohn Forte 	if ((name_len = strlen(mb_name)) == 0) {
5059fcf3ce44SJohn Forte 		return (iSCSINameLenZero);
5060fcf3ce44SJohn Forte 	} else if (name_len > MAX_ISCSI_NAME_LEN) {
5061fcf3ce44SJohn Forte 		return (iSCSINameLenExceededMax);
5062fcf3ce44SJohn Forte 	}
5063fcf3ce44SJohn Forte 
5064fcf3ce44SJohn Forte 	/*
5065fcf3ce44SJohn Forte 	 * check for invalid characters
5066fcf3ce44SJohn Forte 	 * According to RFC 3722 iSCSI name must be either a letter,
5067fcf3ce44SJohn Forte 	 * a digit or one of the following '-' '.' ':'
5068fcf3ce44SJohn Forte 	 */
5069fcf3ce44SJohn Forte 	for (tmp = mb_name; *tmp != NULL; tmp++) {
5070fcf3ce44SJohn Forte 		if ((isalnum(*tmp) == 0) &&
5071fcf3ce44SJohn Forte 		    (*tmp != '-') &&
5072fcf3ce44SJohn Forte 		    (*tmp != '.') &&
5073fcf3ce44SJohn Forte 		    (*tmp != ':')) {
5074fcf3ce44SJohn Forte 			return (iSCSINameInvalidCharacter);
5075fcf3ce44SJohn Forte 		}
5076fcf3ce44SJohn Forte 	}
5077fcf3ce44SJohn Forte 
5078fcf3ce44SJohn Forte 	if (strncmp(mb_name, ISCSI_IQN_NAME_PREFIX,
5079fcf3ce44SJohn Forte 	    strlen(ISCSI_IQN_NAME_PREFIX)) == 0) {
5080fcf3ce44SJohn Forte 		/*
5081fcf3ce44SJohn Forte 		 * If name is of type iqn, check date string and naming
5082fcf3ce44SJohn Forte 		 * authority.
5083fcf3ce44SJohn Forte 		 */
5084fcf3ce44SJohn Forte 		char *strp = NULL;
5085fcf3ce44SJohn Forte 
5086fcf3ce44SJohn Forte 		/*
5087fcf3ce44SJohn Forte 		 * Don't allow the string to end with a colon.  If there is a
5088fcf3ce44SJohn Forte 		 * colon then there must be a subdomain provided.
5089fcf3ce44SJohn Forte 		 */
5090fcf3ce44SJohn Forte 		if (mb_name[strlen(mb_name) - 1] == ':') {
5091fcf3ce44SJohn Forte 			return (iSCSINameIqnSubdomainFormatError);
5092fcf3ce44SJohn Forte 		}
5093fcf3ce44SJohn Forte 
5094fcf3ce44SJohn Forte 		/* Date string */
5095fcf3ce44SJohn Forte 		strp = strtok(&mb_name[3], ".");
5096fcf3ce44SJohn Forte 		if (strp) {
5097fcf3ce44SJohn Forte 			char tmpYear[5], tmpMonth[3], *endPtr = NULL;
5098fcf3ce44SJohn Forte 			int year, month;
5099fcf3ce44SJohn Forte 
5100fcf3ce44SJohn Forte 			/* Date string should be in YYYY-MM format */
5101fcf3ce44SJohn Forte 			if (strlen(strp) != strlen("YYYY-MM") ||
5102fcf3ce44SJohn Forte 			    strp[4] != '-') {
5103fcf3ce44SJohn Forte 				return (iSCSINameIqnDateFormatError);
5104fcf3ce44SJohn Forte 			}
5105fcf3ce44SJohn Forte 
5106fcf3ce44SJohn Forte 			/*
5107fcf3ce44SJohn Forte 			 * Validate year.  Only validating that the
5108fcf3ce44SJohn Forte 			 * year can be converted to a number.  No
5109fcf3ce44SJohn Forte 			 * validation will be done on year's actual
5110fcf3ce44SJohn Forte 			 * value.
5111fcf3ce44SJohn Forte 			 */
5112fcf3ce44SJohn Forte 			(void) strncpy(tmpYear, strp, 4);
5113fcf3ce44SJohn Forte 			tmpYear[4] = '\0';
5114fcf3ce44SJohn Forte 
5115fcf3ce44SJohn Forte 			errno = 0;
5116fcf3ce44SJohn Forte 			year = strtol(tmpYear, &endPtr, 10);
5117fcf3ce44SJohn Forte 			if (errno != 0 || *endPtr != '\0' ||
5118fcf3ce44SJohn Forte 			    year < 0 || year > 9999) {
5119fcf3ce44SJohn Forte 				return (iSCSINameIqnInvalidYearError);
5120fcf3ce44SJohn Forte 			}
5121fcf3ce44SJohn Forte 
5122fcf3ce44SJohn Forte 			/*
5123fcf3ce44SJohn Forte 			 * Validate month is valid.
5124fcf3ce44SJohn Forte 			 */
5125fcf3ce44SJohn Forte 			(void) strncpy(tmpMonth, &strp[5], 2);
5126fcf3ce44SJohn Forte 			tmpMonth[2] = '\0';
5127fcf3ce44SJohn Forte 			errno = 0;
5128fcf3ce44SJohn Forte 			month = strtol(tmpMonth, &endPtr, 10);
5129fcf3ce44SJohn Forte 
5130fcf3ce44SJohn Forte 			if (errno != 0 || *endPtr != '\0' ||
5131fcf3ce44SJohn Forte 			    month < 1 || month > 12) {
5132fcf3ce44SJohn Forte 				return (iSCSINameIqnInvalidMonthError);
5133fcf3ce44SJohn Forte 			}
5134fcf3ce44SJohn Forte 
5135fcf3ce44SJohn Forte 			/*
5136fcf3ce44SJohn Forte 			 * A reversed FQDN needs to be provided.  We
5137fcf3ce44SJohn Forte 			 * will only check for a "." followed by more
5138fcf3ce44SJohn Forte 			 * than two or more characters.  The list of domains is
5139fcf3ce44SJohn Forte 			 * too large and changes too frequently to
5140fcf3ce44SJohn Forte 			 * add validation for.
5141fcf3ce44SJohn Forte 			 */
5142fcf3ce44SJohn Forte 			strp = strtok(NULL, ".");
5143fcf3ce44SJohn Forte 			if (!strp || strlen(strp) < 2) {
5144fcf3ce44SJohn Forte 				return (iSCSINameIqnFQDNError);
5145fcf3ce44SJohn Forte 			}
5146fcf3ce44SJohn Forte 
5147fcf3ce44SJohn Forte 			/* Name authority string */
5148fcf3ce44SJohn Forte 			strp = strtok(NULL, ":");
5149fcf3ce44SJohn Forte 			if (strp) {
5150fcf3ce44SJohn Forte 				return (iSCSINameCheckOK);
5151fcf3ce44SJohn Forte 			} else {
5152fcf3ce44SJohn Forte 				return (iSCSINameIqnFQDNError);
5153fcf3ce44SJohn Forte 			}
5154fcf3ce44SJohn Forte 		} else {
5155fcf3ce44SJohn Forte 			return (iSCSINameIqnFormatError);
5156fcf3ce44SJohn Forte 		}
5157fcf3ce44SJohn Forte 	} else if (strncmp(mb_name, ISCSI_EUI_NAME_PREFIX,
5158fcf3ce44SJohn Forte 	    strlen(ISCSI_EUI_NAME_PREFIX)) == 0) {
5159fcf3ce44SJohn Forte 		/* If name is of type EUI, change its length */
5160fcf3ce44SJohn Forte 
5161fcf3ce44SJohn Forte 		if (strlen(mb_name) != ISCSI_EUI_NAME_LEN) {
5162fcf3ce44SJohn Forte 			return (iSCSINameEUIFormatError);
5163fcf3ce44SJohn Forte 		}
5164fcf3ce44SJohn Forte 
5165fcf3ce44SJohn Forte 		for (tmp = mb_name + strlen(ISCSI_EUI_NAME_PREFIX) + 1;
5166fcf3ce44SJohn Forte 		    *tmp != '\0'; tmp++) {
5167fcf3ce44SJohn Forte 			if (isxdigit(*tmp)) {
5168fcf3ce44SJohn Forte 				continue;
5169fcf3ce44SJohn Forte 			}
5170fcf3ce44SJohn Forte 			return (iSCSINameEUIFormatError);
5171fcf3ce44SJohn Forte 		}
5172fcf3ce44SJohn Forte 
5173fcf3ce44SJohn Forte 		return (iSCSINameCheckOK);
5174fcf3ce44SJohn Forte 	} else {
5175fcf3ce44SJohn Forte 		return (iSCSINameUnknownType);
5176fcf3ce44SJohn Forte 	}
5177fcf3ce44SJohn Forte }
5178fcf3ce44SJohn Forte 
5179fcf3ce44SJohn Forte /*
5180fcf3ce44SJohn Forte  * This helper function could go into a utility module for general use.
5181fcf3ce44SJohn Forte  *
5182fcf3ce44SJohn Forte  * Returns:
5183fcf3ce44SJohn Forte  * B_TRUE is the numberStr is an unsigned natural number and within the
5184fcf3ce44SJohn Forte  * specified bound.
5185fcf3ce44SJohn Forte  * B_FALSE otherwise.
5186fcf3ce44SJohn Forte  */
5187fcf3ce44SJohn Forte boolean_t
5188fcf3ce44SJohn Forte isNaturalNumber(char *numberStr, uint32_t upperBound)
5189fcf3ce44SJohn Forte {
5190fcf3ce44SJohn Forte 	int i;
5191fcf3ce44SJohn Forte 	int number_str_len;
5192fcf3ce44SJohn Forte 
5193fcf3ce44SJohn Forte 	if ((number_str_len = strlen(numberStr)) == 0) {
5194fcf3ce44SJohn Forte 		return (B_FALSE);
5195fcf3ce44SJohn Forte 	}
5196fcf3ce44SJohn Forte 
5197fcf3ce44SJohn Forte 	for (i = 0; i < number_str_len; i++) {
5198fcf3ce44SJohn Forte 		if (numberStr[i] < 060 || numberStr[i] > 071) {
5199fcf3ce44SJohn Forte 			return (B_FALSE);
5200fcf3ce44SJohn Forte 		}
5201fcf3ce44SJohn Forte 	}
5202fcf3ce44SJohn Forte 
5203fcf3ce44SJohn Forte 	if (atoi(numberStr) > upperBound) {
5204fcf3ce44SJohn Forte 		return (B_FALSE);
5205fcf3ce44SJohn Forte 	}
5206fcf3ce44SJohn Forte 
5207fcf3ce44SJohn Forte 	return (B_TRUE);
5208fcf3ce44SJohn Forte }
5209fcf3ce44SJohn Forte 
5210fcf3ce44SJohn Forte /*
5211fcf3ce44SJohn Forte  * This helper function could go into a utility module for general use.
5212fcf3ce44SJohn Forte  * It parses a target string in the format of:
5213fcf3ce44SJohn Forte  *
5214fcf3ce44SJohn Forte  * 	<target_name>,[<ip_address>[:port][,tpgt]]
5215fcf3ce44SJohn Forte  *
5216fcf3ce44SJohn Forte  * and creates wchar strings for target name and target address. It
5217fcf3ce44SJohn Forte  * also populates port and tpgt if found.
5218fcf3ce44SJohn Forte  *
5219fcf3ce44SJohn Forte  * Returns:
5220fcf3ce44SJohn Forte  * 	PARSE_TARGET_OK if parsing is successful.
5221fcf3ce44SJohn Forte  *	PARSE_TARGET_INVALID_TPGT if the specified tpgt is
5222fcf3ce44SJohn Forte  *	invalid.
5223fcf3ce44SJohn Forte  * 	PARSE_TARGET_INVALID_ADDR if the address specified is
5224fcf3ce44SJohn Forte  *	invalid.
5225fcf3ce44SJohn Forte  */
5226fcf3ce44SJohn Forte int
5227fcf3ce44SJohn Forte parseTarget(char *targetStr,
5228fcf3ce44SJohn Forte 		wchar_t *targetNameStr,
5229fcf3ce44SJohn Forte 		size_t targetNameStrLen,
5230fcf3ce44SJohn Forte 		boolean_t *targetAddressSpecified,
5231fcf3ce44SJohn Forte 		wchar_t *targetAddressStr,
5232fcf3ce44SJohn Forte 		size_t targetAddressStrLen,
5233fcf3ce44SJohn Forte 		uint16_t *port,
5234fcf3ce44SJohn Forte 		boolean_t *tpgtSpecified,
5235fcf3ce44SJohn Forte 		uint16_t *tpgt,
5236fcf3ce44SJohn Forte 		boolean_t *isIpv6)
5237fcf3ce44SJohn Forte {
5238fcf3ce44SJohn Forte 	char *commaPos;
5239fcf3ce44SJohn Forte 	char *commaPos2;
5240fcf3ce44SJohn Forte 	char targetAddress[SUN_IMA_IP_ADDRESS_PORT_LEN];
5241fcf3ce44SJohn Forte 	int i;
5242fcf3ce44SJohn Forte 	int lowerCase;
5243fcf3ce44SJohn Forte 
5244fcf3ce44SJohn Forte 	(void) memset(targetNameStr, 0,
5245fcf3ce44SJohn Forte 	    targetNameStrLen * sizeof (wchar_t));
5246fcf3ce44SJohn Forte 	(void) memset(targetAddressStr, 0,
5247fcf3ce44SJohn Forte 	    targetAddressStrLen * sizeof (wchar_t));
5248fcf3ce44SJohn Forte 
5249fcf3ce44SJohn Forte 	commaPos = strchr(targetStr, ',');
5250fcf3ce44SJohn Forte 	if (commaPos != NULL) {
5251fcf3ce44SJohn Forte 		*commaPos = NULL;
5252fcf3ce44SJohn Forte 		commaPos++;
5253fcf3ce44SJohn Forte 		*targetAddressSpecified = B_TRUE;
5254fcf3ce44SJohn Forte 
5255fcf3ce44SJohn Forte 		/*
5256fcf3ce44SJohn Forte 		 * Checking of tpgt makes sense only when
5257fcf3ce44SJohn Forte 		 * the target address/port are specified.
5258fcf3ce44SJohn Forte 		 */
5259fcf3ce44SJohn Forte 		commaPos2 = strchr(commaPos, ',');
5260fcf3ce44SJohn Forte 		if (commaPos2 != NULL) {
5261fcf3ce44SJohn Forte 			*commaPos2 = NULL;
5262fcf3ce44SJohn Forte 			commaPos2++;
5263fcf3ce44SJohn Forte 			if (isNaturalNumber(commaPos2, ISCSI_MAX_TPGT_VALUE) ==
5264fcf3ce44SJohn Forte 			    B_TRUE) {
5265fcf3ce44SJohn Forte 				*tpgt = atoi(commaPos2);
5266fcf3ce44SJohn Forte 				*tpgtSpecified = B_TRUE;
5267fcf3ce44SJohn Forte 			} else {
52685df5713fSbing zhao - Sun Microsystems - Beijing China 				(void) fprintf(stderr, "%s: %s\n", cmdName,
52695df5713fSbing zhao - Sun Microsystems - Beijing China 				    gettext("parse target invalid TPGT"));
5270fcf3ce44SJohn Forte 				return (PARSE_TARGET_INVALID_TPGT);
5271fcf3ce44SJohn Forte 			}
5272fcf3ce44SJohn Forte 		}
5273fcf3ce44SJohn Forte 
5274fcf3ce44SJohn Forte 		switch (parseAddress(commaPos, ISCSI_LISTEN_PORT,
5275fcf3ce44SJohn Forte 		    &targetAddress[0], MAX_ADDRESS_LEN + 1, port, isIpv6)) {
5276fcf3ce44SJohn Forte 		case PARSE_ADDR_PORT_OUT_OF_RANGE:
5277fcf3ce44SJohn Forte 			return (PARSE_TARGET_INVALID_ADDR);
5278fcf3ce44SJohn Forte 		case PARSE_ADDR_OK:
5279fcf3ce44SJohn Forte 			break;
5280fcf3ce44SJohn Forte 		default:
5281fcf3ce44SJohn Forte 			(void) fprintf(stderr, "%s: %s\n",
5282fcf3ce44SJohn Forte 			    cmdName, gettext("cannot parse target name"));
5283fcf3ce44SJohn Forte 			return (PARSE_TARGET_INVALID_ADDR);
5284fcf3ce44SJohn Forte 		}
5285fcf3ce44SJohn Forte 		(void) mbstowcs(targetAddressStr, targetAddress,
5286fcf3ce44SJohn Forte 		    targetAddressStrLen);
5287fcf3ce44SJohn Forte 		for (i = 0; targetAddressStr[i] != 0; i++) {
5288fcf3ce44SJohn Forte 			lowerCase = tolower(targetAddressStr[i]);
5289fcf3ce44SJohn Forte 			targetAddressStr[i] = lowerCase;
5290fcf3ce44SJohn Forte 		}
5291fcf3ce44SJohn Forte 	} else {
5292fcf3ce44SJohn Forte 		*targetAddressSpecified = B_FALSE;
5293fcf3ce44SJohn Forte 		*tpgtSpecified = B_FALSE;
5294fcf3ce44SJohn Forte 	}
5295fcf3ce44SJohn Forte 
5296fcf3ce44SJohn Forte 	(void) mbstowcs(targetNameStr, targetStr, targetNameStrLen);
5297fcf3ce44SJohn Forte 	for (i = 0; targetNameStr[i] != 0; i++) {
5298fcf3ce44SJohn Forte 		lowerCase = tolower(targetNameStr[i]);
5299fcf3ce44SJohn Forte 		targetNameStr[i] = lowerCase;
5300fcf3ce44SJohn Forte 	}
5301fcf3ce44SJohn Forte 
5302fcf3ce44SJohn Forte 	return (PARSE_TARGET_OK);
5303fcf3ce44SJohn Forte }
5304fcf3ce44SJohn Forte 
5305fcf3ce44SJohn Forte /*ARGSUSED*/
5306fcf3ce44SJohn Forte static void
5307fcf3ce44SJohn Forte listCHAPName(IMA_OID oid)
5308fcf3ce44SJohn Forte {
5309fcf3ce44SJohn Forte 	IMA_INITIATOR_AUTHPARMS authParams;
5310fcf3ce44SJohn Forte 	IMA_STATUS status;
5311fcf3ce44SJohn Forte 	IMA_BYTE chapName [MAX_CHAP_NAME_LEN + 1];
5312fcf3ce44SJohn Forte 
5313fcf3ce44SJohn Forte 	/* Get Chap Name depending upon oid object type */
5314fcf3ce44SJohn Forte 	if (oid.objectType == IMA_OBJECT_TYPE_LHBA) {
5315fcf3ce44SJohn Forte 		status = IMA_GetInitiatorAuthParms(oid,
5316fcf3ce44SJohn Forte 		    IMA_AUTHMETHOD_CHAP, &authParams);
5317fcf3ce44SJohn Forte 	} else {
5318fcf3ce44SJohn Forte 		status = SUN_IMA_GetTargetAuthParms(oid,
5319fcf3ce44SJohn Forte 		    IMA_AUTHMETHOD_CHAP, &authParams);
5320fcf3ce44SJohn Forte 	}
5321fcf3ce44SJohn Forte 
5322fcf3ce44SJohn Forte 	(void) fprintf(stdout, "\n\t\t%s: ", gettext("CHAP Name"));
5323fcf3ce44SJohn Forte 
5324fcf3ce44SJohn Forte 	if (IMA_SUCCESS(status)) {
5325fcf3ce44SJohn Forte 		/*
5326fcf3ce44SJohn Forte 		 * Default chap name will be the node name.  The default will
5327fcf3ce44SJohn Forte 		 * be set by the driver.
5328fcf3ce44SJohn Forte 		 */
5329fcf3ce44SJohn Forte 		if (authParams.chapParms.nameLength != 0) {
5330fcf3ce44SJohn Forte 			(void) memset(chapName, 0, sizeof (chapName));
5331fcf3ce44SJohn Forte 			(void) memcpy(chapName, authParams.chapParms.name,
5332fcf3ce44SJohn Forte 			    authParams.chapParms.nameLength);
5333fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s", chapName);
5334fcf3ce44SJohn Forte 
5335fcf3ce44SJohn Forte 		} else {
5336fcf3ce44SJohn Forte 			(void) fprintf(stdout, "%s", "-");
5337fcf3ce44SJohn Forte 		}
5338fcf3ce44SJohn Forte 	} else {
5339fcf3ce44SJohn Forte 		(void) fprintf(stdout, "%s", "-");
5340fcf3ce44SJohn Forte 	}
5341fcf3ce44SJohn Forte }
5342fcf3ce44SJohn Forte 
53434246c8e9SJack Meng static boolean_t
53444246c8e9SJack Meng checkServiceStatus(void)
53454246c8e9SJack Meng {
53464246c8e9SJack Meng 	IMA_STATUS	status	=	IMA_ERROR_UNKNOWN_ERROR;
53474246c8e9SJack Meng 	IMA_BOOL	enabled =	0;
53484246c8e9SJack Meng 
53494246c8e9SJack Meng 	status = SUN_IMA_GetSvcStatus(&enabled);
53504246c8e9SJack Meng 
53514246c8e9SJack Meng 	if (status != IMA_STATUS_SUCCESS) {
53524246c8e9SJack Meng 		(void) fprintf(stdout, "%s\n%s\n",
53534246c8e9SJack Meng 		    gettext("Unable to query the service status of"
53544246c8e9SJack Meng 		    " iSCSI initiator."),
53554246c8e9SJack Meng 		    gettext("For more information, please refer to"
53564246c8e9SJack Meng 		    " iscsi(7D)."));
53574246c8e9SJack Meng 		return (B_FALSE);
53584246c8e9SJack Meng 	}
53594246c8e9SJack Meng 
53604246c8e9SJack Meng 	if (enabled == 0) {
53614246c8e9SJack Meng 		(void) fprintf(stdout, "%s\n%s\n",
53624246c8e9SJack Meng 		    gettext("iSCSI Initiator Service is disabled,"
53634246c8e9SJack Meng 		    " try 'svcadm enable network/iscsi/initiator' to"
53644246c8e9SJack Meng 		    " enable the service."),
53654246c8e9SJack Meng 		    gettext("For more information, please refer to"
53664246c8e9SJack Meng 		    " iscsi(7D)."));
53674246c8e9SJack Meng 		return (B_FALSE);
53684246c8e9SJack Meng 	}
53694246c8e9SJack Meng 
53704246c8e9SJack Meng 	return (B_TRUE);
53714246c8e9SJack Meng }
53724246c8e9SJack Meng 
5373fcf3ce44SJohn Forte /*
5374fcf3ce44SJohn Forte  * Prints out see manual page.
5375fcf3ce44SJohn Forte  * Called out through atexit(3C) so is always last thing displayed.
5376fcf3ce44SJohn Forte  */
5377fcf3ce44SJohn Forte void
5378fcf3ce44SJohn Forte seeMan(void)
5379fcf3ce44SJohn Forte {
5380fcf3ce44SJohn Forte 	static int sent = 0;
5381fcf3ce44SJohn Forte 
5382fcf3ce44SJohn Forte 	if (sent)
5383fcf3ce44SJohn Forte 		return;
5384fcf3ce44SJohn Forte 
5385fcf3ce44SJohn Forte 	(void) fprintf(stdout, "%s %s(1M)\n",
5386fcf3ce44SJohn Forte 	    gettext("For more information, please see"), cmdName);
5387fcf3ce44SJohn Forte 
5388fcf3ce44SJohn Forte 	sent = 1;
5389fcf3ce44SJohn Forte }
5390fcf3ce44SJohn Forte 
5391fcf3ce44SJohn Forte 
5392fcf3ce44SJohn Forte /*
5393fcf3ce44SJohn Forte  * main calls a parser that checks syntax of the input command against
5394fcf3ce44SJohn Forte  * various rules tables.
5395fcf3ce44SJohn Forte  *
5396fcf3ce44SJohn Forte  * The parser provides usage feedback based upon same tables by calling
5397fcf3ce44SJohn Forte  * two usage functions, usage and subUsage, handling command and subcommand
5398fcf3ce44SJohn Forte  * usage respectively.
5399fcf3ce44SJohn Forte  *
5400fcf3ce44SJohn Forte  * The parser handles all printing of usage syntactical errors
5401fcf3ce44SJohn Forte  *
5402fcf3ce44SJohn Forte  * When syntax is successfully validated, the parser calls the associated
5403fcf3ce44SJohn Forte  * function using the subcommands table functions.
5404fcf3ce44SJohn Forte  *
5405fcf3ce44SJohn Forte  * Syntax is as follows:
5406fcf3ce44SJohn Forte  *	command subcommand [options] resource-type [<object>]
5407fcf3ce44SJohn Forte  *
5408fcf3ce44SJohn Forte  * The return value from the function is placed in funcRet
5409fcf3ce44SJohn Forte  */
5410fcf3ce44SJohn Forte int
5411fcf3ce44SJohn Forte main(int argc, char *argv[])
5412fcf3ce44SJohn Forte {
5413fcf3ce44SJohn Forte 	synTables_t synTables;
5414fcf3ce44SJohn Forte 	char versionString[VERSION_STRING_MAX_LEN];
5415fcf3ce44SJohn Forte 	int ret;
5416fcf3ce44SJohn Forte 	int funcRet = 0;
5417fcf3ce44SJohn Forte 	void *subcommandArgs = NULL;
5418fcf3ce44SJohn Forte 
5419fcf3ce44SJohn Forte 	if (geteuid() != 0) {
5420fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s\n", gettext("permission denied"));
5421fcf3ce44SJohn Forte 		return (1);
5422fcf3ce44SJohn Forte 	}
5423fcf3ce44SJohn Forte 
54244246c8e9SJack Meng 	if (checkServiceStatus() == B_FALSE) {
54254246c8e9SJack Meng 		return (1);
54264246c8e9SJack Meng 	}
54274246c8e9SJack Meng 
5428fcf3ce44SJohn Forte 	/* set global command name */
5429fcf3ce44SJohn Forte 	cmdName = getExecBasename(argv[0]);
5430fcf3ce44SJohn Forte 
5431fcf3ce44SJohn Forte 	(void) snprintf(versionString, sizeof (versionString), "%s.%s",
5432fcf3ce44SJohn Forte 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
5433fcf3ce44SJohn Forte 	synTables.versionString = versionString;
5434fcf3ce44SJohn Forte 	synTables.longOptionTbl = &longOptions[0];
5435fcf3ce44SJohn Forte 	synTables.subcommandTbl = &subcommands[0];
5436fcf3ce44SJohn Forte 	synTables.objectTbl = &objects[0];
5437fcf3ce44SJohn Forte 	synTables.objectRulesTbl = &objectRules[0];
5438fcf3ce44SJohn Forte 	synTables.optionRulesTbl = &optionRules[0];
5439fcf3ce44SJohn Forte 
5440fcf3ce44SJohn Forte 	/* call the CLI parser */
5441fcf3ce44SJohn Forte 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
5442fcf3ce44SJohn Forte 	if (ret == -1) {
5443fcf3ce44SJohn Forte 		perror(cmdName);
5444fcf3ce44SJohn Forte 		ret = 1;
5445fcf3ce44SJohn Forte 	}
5446fcf3ce44SJohn Forte 
5447fcf3ce44SJohn Forte 	if (funcRet != 0) {
5448fcf3ce44SJohn Forte 		(void) fprintf(stderr, "%s: %s\n",
5449fcf3ce44SJohn Forte 		    cmdName, gettext("Unable to complete operation"));
5450fcf3ce44SJohn Forte 		ret = 1;
5451fcf3ce44SJohn Forte 	}
5452fcf3ce44SJohn Forte 	return (ret);
5453fcf3ce44SJohn Forte }
5454