xref: /titanic_52/usr/src/lib/libiscsit/common/libiscsit.c (revision fcc214c383d20beb968b623b83d851672e174702)
1a6d42e7dSPeter Dunlap /*
2a6d42e7dSPeter Dunlap  * CDDL HEADER START
3a6d42e7dSPeter Dunlap  *
4a6d42e7dSPeter Dunlap  * The contents of this file are subject to the terms of the
5a6d42e7dSPeter Dunlap  * Common Development and Distribution License (the "License").
6a6d42e7dSPeter Dunlap  * You may not use this file except in compliance with the License.
7a6d42e7dSPeter Dunlap  *
8a6d42e7dSPeter Dunlap  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9a6d42e7dSPeter Dunlap  * or http://www.opensolaris.org/os/licensing.
10a6d42e7dSPeter Dunlap  * See the License for the specific language governing permissions
11a6d42e7dSPeter Dunlap  * and limitations under the License.
12a6d42e7dSPeter Dunlap  *
13a6d42e7dSPeter Dunlap  * When distributing Covered Code, include this CDDL HEADER in each
14a6d42e7dSPeter Dunlap  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15a6d42e7dSPeter Dunlap  * If applicable, add the following below this CDDL HEADER, with the
16a6d42e7dSPeter Dunlap  * fields enclosed by brackets "[]" replaced with your own identifying
17a6d42e7dSPeter Dunlap  * information: Portions Copyright [yyyy] [name of copyright owner]
18a6d42e7dSPeter Dunlap  *
19a6d42e7dSPeter Dunlap  * CDDL HEADER END
20a6d42e7dSPeter Dunlap  */
21a6d42e7dSPeter Dunlap /*
22836fc322SSam Cramer  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23a6d42e7dSPeter Dunlap  * Use is subject to license terms.
24a6d42e7dSPeter Dunlap  */
25a6d42e7dSPeter Dunlap 
26a6d42e7dSPeter Dunlap #include <sys/types.h>
27a6d42e7dSPeter Dunlap #include <sys/stat.h>
28a6d42e7dSPeter Dunlap #include <ctype.h>
29a6d42e7dSPeter Dunlap #include <fcntl.h>
30a6d42e7dSPeter Dunlap #include <uuid/uuid.h>
31a6d42e7dSPeter Dunlap #include <errno.h>
32a6d42e7dSPeter Dunlap #include <unistd.h>
33a6d42e7dSPeter Dunlap #include <strings.h>
34a6d42e7dSPeter Dunlap #include <libintl.h>
355de03f84SSue Gleeson #include <libscf.h>
36a6d42e7dSPeter Dunlap 
37a6d42e7dSPeter Dunlap #include <libstmf.h>
38a6d42e7dSPeter Dunlap #include <libiscsit.h>
39a6d42e7dSPeter Dunlap #include <sys/iscsi_protocol.h>
40a6d42e7dSPeter Dunlap #include <sys/iscsit/isns_protocol.h>
41a6d42e7dSPeter Dunlap 
42a6d42e7dSPeter Dunlap /* From iscsitgtd */
43a6d42e7dSPeter Dunlap #define	TARGET_NAME_VERS	2
44a6d42e7dSPeter Dunlap 
45a6d42e7dSPeter Dunlap /* this should be defined someplace central... */
46a6d42e7dSPeter Dunlap #define	ISCSI_NAME_LEN_MAX	223
47a6d42e7dSPeter Dunlap 
48a6d42e7dSPeter Dunlap /* max length of a base64 encoded secret */
49a6d42e7dSPeter Dunlap #define	MAX_BASE64_LEN		341
50a6d42e7dSPeter Dunlap 
51a6d42e7dSPeter Dunlap /* Default RADIUS server port */
52a6d42e7dSPeter Dunlap #define	DEFAULT_RADIUS_PORT	1812
53a6d42e7dSPeter Dunlap 
545de03f84SSue Gleeson /* The iscsit SMF service FMRI */
555de03f84SSue Gleeson #define	ISCSIT_FMRI		"svc:/network/iscsi/target:default"
56a6d42e7dSPeter Dunlap /*
57a6d42e7dSPeter Dunlap  * The kernel reserves target portal group tag value 1 as the default.
58a6d42e7dSPeter Dunlap  */
59a6d42e7dSPeter Dunlap #define	ISCSIT_DEFAULT_TPGT	1
60a6d42e7dSPeter Dunlap #define	MAXTAG			0xffff
61a6d42e7dSPeter Dunlap 
62a6d42e7dSPeter Dunlap /* helper for property list validation */
63a6d42e7dSPeter Dunlap #define	PROPERR(lst, key, value) { \
64a6d42e7dSPeter Dunlap 	if (lst) { \
65a6d42e7dSPeter Dunlap 		(void) nvlist_add_string(lst, key, value); \
66a6d42e7dSPeter Dunlap 	} \
67a6d42e7dSPeter Dunlap }
68a6d42e7dSPeter Dunlap 
69a6d42e7dSPeter Dunlap /* helper function declarations */
70a6d42e7dSPeter Dunlap static int
71a6d42e7dSPeter Dunlap it_iqn_generate(char *iqn_buf, int iqn_buf_len, char *opt_iqn_suffix);
72a6d42e7dSPeter Dunlap 
73a6d42e7dSPeter Dunlap static int
74a6d42e7dSPeter Dunlap it_val_pass(char *name, char *val, nvlist_t *e);
75a6d42e7dSPeter Dunlap 
76a6d42e7dSPeter Dunlap /* consider making validate funcs public */
77a6d42e7dSPeter Dunlap static int
78a6d42e7dSPeter Dunlap it_validate_configprops(nvlist_t *nvl, nvlist_t *errs);
79a6d42e7dSPeter Dunlap 
80a6d42e7dSPeter Dunlap static int
81a6d42e7dSPeter Dunlap it_validate_tgtprops(nvlist_t *nvl, nvlist_t *errs);
82a6d42e7dSPeter Dunlap 
83a6d42e7dSPeter Dunlap static int
84a6d42e7dSPeter Dunlap it_validate_iniprops(nvlist_t *nvl, nvlist_t *errs);
85a6d42e7dSPeter Dunlap 
865de03f84SSue Gleeson static boolean_t
875de03f84SSue Gleeson is_iscsit_enabled(void);
885de03f84SSue Gleeson 
89*fcc214c3SCharles Ting static void
90*fcc214c3SCharles Ting iqnstr(char *s);
91*fcc214c3SCharles Ting 
92*fcc214c3SCharles Ting static void
93*fcc214c3SCharles Ting euistr(char *s);
94*fcc214c3SCharles Ting 
95a6d42e7dSPeter Dunlap /*
96a6d42e7dSPeter Dunlap  * Function:  it_config_load()
97a6d42e7dSPeter Dunlap  *
98a6d42e7dSPeter Dunlap  * Allocate and create an it_config_t structure representing the
99a6d42e7dSPeter Dunlap  * current iSCSI configuration.  This structure is compiled using
100a6d42e7dSPeter Dunlap  * the 'provider' data returned by stmfGetProviderData().  If there
101a6d42e7dSPeter Dunlap  * is no provider data associated with iscsit, the it_config_t
102a6d42e7dSPeter Dunlap  * structure will be set to a default configuration.
103a6d42e7dSPeter Dunlap  *
104a6d42e7dSPeter Dunlap  * Parameters:
105a6d42e7dSPeter Dunlap  *    cfg	A C representation of the current iSCSI configuration
106a6d42e7dSPeter Dunlap  *
107a6d42e7dSPeter Dunlap  * Return Values:
108a6d42e7dSPeter Dunlap  *    0		Success
109a6d42e7dSPeter Dunlap  *    ENOMEM	Could not allocate resources
110a6d42e7dSPeter Dunlap  *    EINVAL	Invalid parameter
111a6d42e7dSPeter Dunlap  */
112a6d42e7dSPeter Dunlap int
113a6d42e7dSPeter Dunlap it_config_load(it_config_t **cfg)
114a6d42e7dSPeter Dunlap {
115a6d42e7dSPeter Dunlap 	int		ret = 0;
116a6d42e7dSPeter Dunlap 	nvlist_t	*cfg_nv = NULL;
117a6d42e7dSPeter Dunlap 	it_config_t	*newcfg = NULL;
118a6d42e7dSPeter Dunlap 	uint64_t	stmf_token = 0;
119a6d42e7dSPeter Dunlap 
120a6d42e7dSPeter Dunlap 	if (!cfg) {
121a6d42e7dSPeter Dunlap 		return (EINVAL);
122a6d42e7dSPeter Dunlap 	}
123a6d42e7dSPeter Dunlap 
124a6d42e7dSPeter Dunlap 	*cfg = NULL;
125a6d42e7dSPeter Dunlap 
126a6d42e7dSPeter Dunlap 	ret = stmfGetProviderDataProt(ISCSIT_MODNAME, &cfg_nv,
127a6d42e7dSPeter Dunlap 	    STMF_PORT_PROVIDER_TYPE, &stmf_token);
128a6d42e7dSPeter Dunlap 
129a6d42e7dSPeter Dunlap 	if ((ret == STMF_STATUS_SUCCESS) ||
130a6d42e7dSPeter Dunlap 	    (ret == STMF_ERROR_NOT_FOUND)) {
131a6d42e7dSPeter Dunlap 		/*
132a6d42e7dSPeter Dunlap 		 * If not initialized yet, return empty it_config_t
133a6d42e7dSPeter Dunlap 		 * Else, convert nvlist to struct
134a6d42e7dSPeter Dunlap 		 */
135a6d42e7dSPeter Dunlap 		ret = it_nv_to_config(cfg_nv, &newcfg);
136a6d42e7dSPeter Dunlap 	}
137a6d42e7dSPeter Dunlap 
138a6d42e7dSPeter Dunlap 	if (ret == 0) {
139a6d42e7dSPeter Dunlap 		newcfg->stmf_token = stmf_token;
140a6d42e7dSPeter Dunlap 		*cfg = newcfg;
141a6d42e7dSPeter Dunlap 	}
142a6d42e7dSPeter Dunlap 
1433c006dd5SCharles Ting 	if (cfg_nv) {
1443c006dd5SCharles Ting 		nvlist_free(cfg_nv);
1453c006dd5SCharles Ting 	}
1463c006dd5SCharles Ting 
147a6d42e7dSPeter Dunlap 	return (ret);
148a6d42e7dSPeter Dunlap }
149a6d42e7dSPeter Dunlap 
150a6d42e7dSPeter Dunlap /*
151a6d42e7dSPeter Dunlap  * Function:  it_config_commit()
152a6d42e7dSPeter Dunlap  *
153a6d42e7dSPeter Dunlap  * Informs the iscsit service that the configuration has changed and
154a6d42e7dSPeter Dunlap  * commits the new configuration to persistent store by calling
155a6d42e7dSPeter Dunlap  * stmfSetProviderData.  This function can be called multiple times
156a6d42e7dSPeter Dunlap  * during a configuration sequence if necessary.
157a6d42e7dSPeter Dunlap  *
158a6d42e7dSPeter Dunlap  * Parameters:
159a6d42e7dSPeter Dunlap  *    cfg	A C representation of the current iSCSI configuration
160a6d42e7dSPeter Dunlap  *
161a6d42e7dSPeter Dunlap  * Return Values:
162a6d42e7dSPeter Dunlap  *    0		Success
163a6d42e7dSPeter Dunlap  *    ENOMEM	Could not allocate resources
164a6d42e7dSPeter Dunlap  *    EINVAL	Invalid it_config_t structure
165a6d42e7dSPeter Dunlap  *    TBD	ioctl() failed
166a6d42e7dSPeter Dunlap  *    TBD	could not save config to STMF
167a6d42e7dSPeter Dunlap  */
168a6d42e7dSPeter Dunlap int
169a6d42e7dSPeter Dunlap it_config_commit(it_config_t *cfg)
170a6d42e7dSPeter Dunlap {
171a6d42e7dSPeter Dunlap 	int			ret;
172a6d42e7dSPeter Dunlap 	nvlist_t		*cfgnv = NULL;
173a6d42e7dSPeter Dunlap 	char			*packednv = NULL;
174a6d42e7dSPeter Dunlap 	int			iscsit_fd = -1;
175a6d42e7dSPeter Dunlap 	size_t			pnv_size;
176a6d42e7dSPeter Dunlap 	iscsit_ioc_set_config_t	iop;
177a6d42e7dSPeter Dunlap 	it_tgt_t		*tgtp;
178a6d42e7dSPeter Dunlap 
179a6d42e7dSPeter Dunlap 	if (!cfg) {
180a6d42e7dSPeter Dunlap 		return (EINVAL);
181a6d42e7dSPeter Dunlap 	}
182a6d42e7dSPeter Dunlap 
183a6d42e7dSPeter Dunlap 	ret = it_config_to_nv(cfg, &cfgnv);
184a6d42e7dSPeter Dunlap 	if (ret == 0) {
185a6d42e7dSPeter Dunlap 		ret = nvlist_size(cfgnv, &pnv_size, NV_ENCODE_NATIVE);
186a6d42e7dSPeter Dunlap 	}
187a6d42e7dSPeter Dunlap 
1885de03f84SSue Gleeson 	/*
1895de03f84SSue Gleeson 	 * If the iscsit service is enabled, send the changes to the
1905de03f84SSue Gleeson 	 * kernel first.  Kernel will be the final sanity check before
1915de03f84SSue Gleeson 	 * the config is saved persistently.
1925de03f84SSue Gleeson 	 *
1935de03f84SSue Gleeson 	 * This somewhat leaves open the simultaneous-change hole
1945de03f84SSue Gleeson 	 * that STMF was trying to solve, but is a better sanity
1955de03f84SSue Gleeson 	 * check and allows for graceful handling of target renames.
1965de03f84SSue Gleeson 	 */
1975de03f84SSue Gleeson 	if ((ret == 0) && is_iscsit_enabled()) {
198a6d42e7dSPeter Dunlap 		packednv = malloc(pnv_size);
199a6d42e7dSPeter Dunlap 		if (!packednv) {
200a6d42e7dSPeter Dunlap 			ret = ENOMEM;
201a6d42e7dSPeter Dunlap 		} else {
202a6d42e7dSPeter Dunlap 			ret = nvlist_pack(cfgnv, &packednv, &pnv_size,
203a6d42e7dSPeter Dunlap 			    NV_ENCODE_NATIVE, 0);
204a6d42e7dSPeter Dunlap 		}
205a6d42e7dSPeter Dunlap 
206a6d42e7dSPeter Dunlap 		if (ret == 0) {
2075de03f84SSue Gleeson 			iscsit_fd = open(ISCSIT_NODE, O_RDWR|O_EXCL);
2085de03f84SSue Gleeson 			if (iscsit_fd != -1) {
209a6d42e7dSPeter Dunlap 				iop.set_cfg_vers = ISCSIT_API_VERS0;
210a6d42e7dSPeter Dunlap 				iop.set_cfg_pnvlist = packednv;
211a6d42e7dSPeter Dunlap 				iop.set_cfg_pnvlist_len = pnv_size;
2125de03f84SSue Gleeson 				if ((ioctl(iscsit_fd, ISCSIT_IOC_SET_CONFIG,
2135de03f84SSue Gleeson 				    &iop)) != 0) {
214a6d42e7dSPeter Dunlap 					ret = errno;
215a6d42e7dSPeter Dunlap 				}
2165de03f84SSue Gleeson 
2175de03f84SSue Gleeson 				(void) close(iscsit_fd);
2185de03f84SSue Gleeson 			} else {
2195de03f84SSue Gleeson 				ret = errno;
2205de03f84SSue Gleeson 			}
2215de03f84SSue Gleeson 		}
2225de03f84SSue Gleeson 
2235de03f84SSue Gleeson 		if (packednv != NULL) {
2245de03f84SSue Gleeson 			free(packednv);
2255de03f84SSue Gleeson 		}
226a6d42e7dSPeter Dunlap 	}
227a6d42e7dSPeter Dunlap 
228a6d42e7dSPeter Dunlap 	/*
229a6d42e7dSPeter Dunlap 	 * Before saving the config persistently, remove any
230a6d42e7dSPeter Dunlap 	 * PROP_OLD_TARGET_NAME entries.  This is only interesting to
231a6d42e7dSPeter Dunlap 	 * the active service.
232a6d42e7dSPeter Dunlap 	 */
233a6d42e7dSPeter Dunlap 	if (ret == 0) {
2345de03f84SSue Gleeson 		boolean_t	changed = B_FALSE;
2355de03f84SSue Gleeson 
236a6d42e7dSPeter Dunlap 		tgtp = cfg->config_tgt_list;
237a6d42e7dSPeter Dunlap 		for (; tgtp != NULL; tgtp = tgtp->tgt_next) {
238a6d42e7dSPeter Dunlap 			if (!tgtp->tgt_properties) {
239a6d42e7dSPeter Dunlap 				continue;
240a6d42e7dSPeter Dunlap 			}
241a6d42e7dSPeter Dunlap 			if (nvlist_exists(tgtp->tgt_properties,
242a6d42e7dSPeter Dunlap 			    PROP_OLD_TARGET_NAME)) {
243a6d42e7dSPeter Dunlap 				(void) nvlist_remove_all(tgtp->tgt_properties,
244a6d42e7dSPeter Dunlap 				    PROP_OLD_TARGET_NAME);
2455de03f84SSue Gleeson 				changed = B_TRUE;
246a6d42e7dSPeter Dunlap 			}
247a6d42e7dSPeter Dunlap 		}
2485de03f84SSue Gleeson 
2495de03f84SSue Gleeson 		if (changed) {
2505de03f84SSue Gleeson 			/* rebuild the config nvlist */
2515de03f84SSue Gleeson 			nvlist_free(cfgnv);
2525de03f84SSue Gleeson 			cfgnv = NULL;
2535de03f84SSue Gleeson 			ret = it_config_to_nv(cfg, &cfgnv);
2545de03f84SSue Gleeson 		}
255a6d42e7dSPeter Dunlap 	}
256a6d42e7dSPeter Dunlap 
257a6d42e7dSPeter Dunlap 	/*
258a6d42e7dSPeter Dunlap 	 * stmfGetProviderDataProt() checks to ensure
259a6d42e7dSPeter Dunlap 	 * that the config data hasn't changed since we fetched it.
260a6d42e7dSPeter Dunlap 	 *
261a6d42e7dSPeter Dunlap 	 * The kernel now has a version we need to save persistently.
262a6d42e7dSPeter Dunlap 	 * CLI will 'do the right thing' and warn the user if it
263a6d42e7dSPeter Dunlap 	 * gets STMF_ERROR_PROV_DATA_STALE.  We'll try once to revert
264a6d42e7dSPeter Dunlap 	 * the kernel to the persistently saved data, but ultimately,
265a6d42e7dSPeter Dunlap 	 * it's up to the administrator to validate things are as they
266a6d42e7dSPeter Dunlap 	 * want them to be.
267a6d42e7dSPeter Dunlap 	 */
268a6d42e7dSPeter Dunlap 	if (ret == 0) {
269a6d42e7dSPeter Dunlap 		ret = stmfSetProviderDataProt(ISCSIT_MODNAME, cfgnv,
270a6d42e7dSPeter Dunlap 		    STMF_PORT_PROVIDER_TYPE, &(cfg->stmf_token));
271a6d42e7dSPeter Dunlap 
272a6d42e7dSPeter Dunlap 		if (ret == STMF_STATUS_SUCCESS) {
273a6d42e7dSPeter Dunlap 			ret = 0;
274a6d42e7dSPeter Dunlap 		} else if (ret == STMF_ERROR_NOMEM) {
275a6d42e7dSPeter Dunlap 			ret = ENOMEM;
276a6d42e7dSPeter Dunlap 		} else if (ret == STMF_ERROR_PROV_DATA_STALE) {
277a6d42e7dSPeter Dunlap 			int		st;
278a6d42e7dSPeter Dunlap 			it_config_t	*rcfg = NULL;
279a6d42e7dSPeter Dunlap 
280a6d42e7dSPeter Dunlap 			st = it_config_load(&rcfg);
281a6d42e7dSPeter Dunlap 			if (st == 0) {
282a6d42e7dSPeter Dunlap 				(void) it_config_commit(rcfg);
283a6d42e7dSPeter Dunlap 				it_config_free(rcfg);
284a6d42e7dSPeter Dunlap 			}
285a6d42e7dSPeter Dunlap 		}
286a6d42e7dSPeter Dunlap 	}
287a6d42e7dSPeter Dunlap 
288a6d42e7dSPeter Dunlap 	if (cfgnv) {
289a6d42e7dSPeter Dunlap 		nvlist_free(cfgnv);
290a6d42e7dSPeter Dunlap 	}
291a6d42e7dSPeter Dunlap 
292a6d42e7dSPeter Dunlap 	return (ret);
293a6d42e7dSPeter Dunlap }
294a6d42e7dSPeter Dunlap 
295a6d42e7dSPeter Dunlap /*
296a6d42e7dSPeter Dunlap  * Function:  it_config_setprop()
297a6d42e7dSPeter Dunlap  *
298a6d42e7dSPeter Dunlap  * Validate the provided property list and set the global properties
299a6d42e7dSPeter Dunlap  * for iSCSI Target.  If errlist is not NULL, returns detailed
300a6d42e7dSPeter Dunlap  * errors for each property that failed.  The format for errorlist
301a6d42e7dSPeter Dunlap  * is key = property, value = error string.
302a6d42e7dSPeter Dunlap  *
303a6d42e7dSPeter Dunlap  * Parameters:
304a6d42e7dSPeter Dunlap  *
305a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configuration obtained from
306a6d42e7dSPeter Dunlap  *			it_config_load()
307a6d42e7dSPeter Dunlap  *    proplist		nvlist_t containing properties for this target.
308a6d42e7dSPeter Dunlap  *    errlist		(optional)  nvlist_t of errors encountered when
309a6d42e7dSPeter Dunlap  *                      validating the properties.
310a6d42e7dSPeter Dunlap  *
311a6d42e7dSPeter Dunlap  * Return Values:
312a6d42e7dSPeter Dunlap  *    0			Success
313a6d42e7dSPeter Dunlap  *    EINVAL		Invalid property
314a6d42e7dSPeter Dunlap  *
315a6d42e7dSPeter Dunlap  */
316a6d42e7dSPeter Dunlap int
317a6d42e7dSPeter Dunlap it_config_setprop(it_config_t *cfg, nvlist_t *proplist, nvlist_t **errlist)
318a6d42e7dSPeter Dunlap {
319a6d42e7dSPeter Dunlap 	int		ret;
320a6d42e7dSPeter Dunlap 	it_portal_t	*isns = NULL;
321a6d42e7dSPeter Dunlap 	it_portal_t	*pnext = NULL;
322a6d42e7dSPeter Dunlap 	it_portal_t	*newisnslist = NULL;
323a6d42e7dSPeter Dunlap 	char		**arr;
324a6d42e7dSPeter Dunlap 	uint32_t	count;
325a6d42e7dSPeter Dunlap 	uint32_t	newcount;
326a6d42e7dSPeter Dunlap 	nvlist_t	*cprops = NULL;
327a6d42e7dSPeter Dunlap 	char		*val = NULL;
328a6d42e7dSPeter Dunlap 
329a6d42e7dSPeter Dunlap 	if (!cfg || !proplist) {
330a6d42e7dSPeter Dunlap 		return (EINVAL);
331a6d42e7dSPeter Dunlap 	}
332a6d42e7dSPeter Dunlap 
333a6d42e7dSPeter Dunlap 	if (errlist) {
334a6d42e7dSPeter Dunlap 		(void) nvlist_alloc(errlist, 0, 0);
335a6d42e7dSPeter Dunlap 	}
336a6d42e7dSPeter Dunlap 
337a6d42e7dSPeter Dunlap 	/*
338a6d42e7dSPeter Dunlap 	 * copy the existing properties, merge, then validate
339a6d42e7dSPeter Dunlap 	 * the merged properties before committing them.
340a6d42e7dSPeter Dunlap 	 */
341a6d42e7dSPeter Dunlap 	if (cfg->config_global_properties) {
342a6d42e7dSPeter Dunlap 		ret = nvlist_dup(cfg->config_global_properties, &cprops, 0);
343a6d42e7dSPeter Dunlap 	} else {
344a6d42e7dSPeter Dunlap 		ret = nvlist_alloc(&cprops, NV_UNIQUE_NAME, 0);
345a6d42e7dSPeter Dunlap 	}
346a6d42e7dSPeter Dunlap 
347a6d42e7dSPeter Dunlap 	/* base64 encode the radius secret, if it's changed */
348a6d42e7dSPeter Dunlap 	val = NULL;
349a6d42e7dSPeter Dunlap 	(void) nvlist_lookup_string(proplist, PROP_RADIUS_SECRET, &val);
350a6d42e7dSPeter Dunlap 	if (val) {
351a6d42e7dSPeter Dunlap 		char		bsecret[MAX_BASE64_LEN];
352a6d42e7dSPeter Dunlap 
353a6d42e7dSPeter Dunlap 		ret = it_val_pass(PROP_RADIUS_SECRET, val, *errlist);
354a6d42e7dSPeter Dunlap 
355a6d42e7dSPeter Dunlap 		if (ret == 0) {
356a6d42e7dSPeter Dunlap 			(void) memset(bsecret, 0, MAX_BASE64_LEN);
357a6d42e7dSPeter Dunlap 
358a6d42e7dSPeter Dunlap 			ret = iscsi_binary_to_base64_str((uint8_t *)val,
359a6d42e7dSPeter Dunlap 			    strlen(val), bsecret, MAX_BASE64_LEN);
360a6d42e7dSPeter Dunlap 
361a6d42e7dSPeter Dunlap 			if (ret == 0) {
362a6d42e7dSPeter Dunlap 				/* replace the value in the nvlist */
363a6d42e7dSPeter Dunlap 				ret = nvlist_add_string(proplist,
364a6d42e7dSPeter Dunlap 				    PROP_RADIUS_SECRET, bsecret);
365a6d42e7dSPeter Dunlap 			}
366a6d42e7dSPeter Dunlap 		}
367a6d42e7dSPeter Dunlap 	}
368a6d42e7dSPeter Dunlap 
369a6d42e7dSPeter Dunlap 	if (ret == 0) {
370a6d42e7dSPeter Dunlap 		ret = nvlist_merge(cprops, proplist, 0);
371a6d42e7dSPeter Dunlap 	}
372a6d42e7dSPeter Dunlap 
373a6d42e7dSPeter Dunlap 	/* see if we need to remove the radius server setting */
374a6d42e7dSPeter Dunlap 	val = NULL;
375a6d42e7dSPeter Dunlap 	(void) nvlist_lookup_string(cprops, PROP_RADIUS_SERVER, &val);
376a6d42e7dSPeter Dunlap 	if (val && (strcasecmp(val, "none") == 0)) {
377a6d42e7dSPeter Dunlap 		(void) nvlist_remove_all(cprops, PROP_RADIUS_SERVER);
378a6d42e7dSPeter Dunlap 	}
379a6d42e7dSPeter Dunlap 
380a6d42e7dSPeter Dunlap 	/* and/or remove the alias */
381a6d42e7dSPeter Dunlap 	val = NULL;
382a6d42e7dSPeter Dunlap 	(void) nvlist_lookup_string(cprops, PROP_ALIAS, &val);
383a6d42e7dSPeter Dunlap 	if (val && (strcasecmp(val, "none") == 0)) {
384a6d42e7dSPeter Dunlap 		(void) nvlist_remove_all(cprops, PROP_ALIAS);
385a6d42e7dSPeter Dunlap 	}
386a6d42e7dSPeter Dunlap 
387a6d42e7dSPeter Dunlap 	if (ret == 0) {
388a6d42e7dSPeter Dunlap 		ret = it_validate_configprops(cprops, *errlist);
389a6d42e7dSPeter Dunlap 	}
390a6d42e7dSPeter Dunlap 
391a6d42e7dSPeter Dunlap 	if (ret != 0) {
392a6d42e7dSPeter Dunlap 		if (cprops) {
393a6d42e7dSPeter Dunlap 			nvlist_free(cprops);
394a6d42e7dSPeter Dunlap 		}
395a6d42e7dSPeter Dunlap 		return (ret);
396a6d42e7dSPeter Dunlap 	}
397a6d42e7dSPeter Dunlap 
398a6d42e7dSPeter Dunlap 	/*
399a6d42e7dSPeter Dunlap 	 * Update iSNS server list, if exists in provided property list.
400a6d42e7dSPeter Dunlap 	 */
401a6d42e7dSPeter Dunlap 	ret = nvlist_lookup_string_array(proplist, PROP_ISNS_SERVER,
402a6d42e7dSPeter Dunlap 	    &arr, &count);
403a6d42e7dSPeter Dunlap 
404a6d42e7dSPeter Dunlap 	if (ret == 0) {
405a6d42e7dSPeter Dunlap 		/* special case:  if "none", remove all defined */
406a6d42e7dSPeter Dunlap 		if (strcasecmp(arr[0], "none") != 0) {
407a6d42e7dSPeter Dunlap 			ret = it_array_to_portallist(arr, count,
408a6d42e7dSPeter Dunlap 			    ISNS_DEFAULT_SERVER_PORT, &newisnslist, &newcount);
409a6d42e7dSPeter Dunlap 		} else {
410a6d42e7dSPeter Dunlap 			newisnslist = NULL;
411a6d42e7dSPeter Dunlap 			newcount = 0;
412a6d42e7dSPeter Dunlap 			(void) nvlist_remove_all(cprops, PROP_ISNS_SERVER);
413a6d42e7dSPeter Dunlap 		}
414a6d42e7dSPeter Dunlap 
415a6d42e7dSPeter Dunlap 		if (ret == 0) {
416a6d42e7dSPeter Dunlap 			isns = cfg->config_isns_svr_list;
417a6d42e7dSPeter Dunlap 			while (isns) {
418a6d42e7dSPeter Dunlap 				pnext = isns->next;
419a6d42e7dSPeter Dunlap 				free(isns);
420a6d42e7dSPeter Dunlap 				isns = pnext;
421a6d42e7dSPeter Dunlap 			}
422a6d42e7dSPeter Dunlap 
423a6d42e7dSPeter Dunlap 			cfg->config_isns_svr_list = newisnslist;
424a6d42e7dSPeter Dunlap 			cfg->config_isns_svr_count = newcount;
425a6d42e7dSPeter Dunlap 
426a6d42e7dSPeter Dunlap 			/*
427a6d42e7dSPeter Dunlap 			 * Replace the array in the nvlist to ensure
428a6d42e7dSPeter Dunlap 			 * duplicates are properly removed & port numbers
429a6d42e7dSPeter Dunlap 			 * are added.
430a6d42e7dSPeter Dunlap 			 */
431a6d42e7dSPeter Dunlap 			if (newcount > 0) {
432a6d42e7dSPeter Dunlap 				int	i = 0;
433a6d42e7dSPeter Dunlap 				char	**newarray;
434a6d42e7dSPeter Dunlap 
435a6d42e7dSPeter Dunlap 				newarray = malloc(sizeof (char *) * newcount);
436a6d42e7dSPeter Dunlap 				if (newarray == NULL) {
437a6d42e7dSPeter Dunlap 					ret = ENOMEM;
438a6d42e7dSPeter Dunlap 				} else {
439a6d42e7dSPeter Dunlap 					for (isns = newisnslist; isns != NULL;
440a6d42e7dSPeter Dunlap 					    isns = isns->next) {
441a6d42e7dSPeter Dunlap 						(void) sockaddr_to_str(
442a6d42e7dSPeter Dunlap 						    &(isns->portal_addr),
443a6d42e7dSPeter Dunlap 						    &(newarray[i++]));
444a6d42e7dSPeter Dunlap 					}
445a6d42e7dSPeter Dunlap 					(void) nvlist_add_string_array(cprops,
446a6d42e7dSPeter Dunlap 					    PROP_ISNS_SERVER, newarray,
447a6d42e7dSPeter Dunlap 					    newcount);
448a6d42e7dSPeter Dunlap 
449a6d42e7dSPeter Dunlap 					for (i = 0; i < newcount; i++) {
450a6d42e7dSPeter Dunlap 						if (newarray[i]) {
451a6d42e7dSPeter Dunlap 							free(newarray[i]);
452a6d42e7dSPeter Dunlap 						}
453a6d42e7dSPeter Dunlap 					}
454a6d42e7dSPeter Dunlap 					free(newarray);
455a6d42e7dSPeter Dunlap 				}
456a6d42e7dSPeter Dunlap 			}
457a6d42e7dSPeter Dunlap 		}
458a6d42e7dSPeter Dunlap 	} else if (ret == ENOENT) {
459a6d42e7dSPeter Dunlap 		/* not an error */
460a6d42e7dSPeter Dunlap 		ret = 0;
461a6d42e7dSPeter Dunlap 	}
462a6d42e7dSPeter Dunlap 
463a6d42e7dSPeter Dunlap 	if (ret == 0) {
464a6d42e7dSPeter Dunlap 		/* replace the global properties list */
465a6d42e7dSPeter Dunlap 		nvlist_free(cfg->config_global_properties);
466a6d42e7dSPeter Dunlap 		cfg->config_global_properties = cprops;
467a6d42e7dSPeter Dunlap 	} else {
468a6d42e7dSPeter Dunlap 		if (cprops) {
469a6d42e7dSPeter Dunlap 			nvlist_free(cprops);
470a6d42e7dSPeter Dunlap 		}
471a6d42e7dSPeter Dunlap 	}
472a6d42e7dSPeter Dunlap 
473a6d42e7dSPeter Dunlap 	return (ret);
474a6d42e7dSPeter Dunlap }
475a6d42e7dSPeter Dunlap 
476a6d42e7dSPeter Dunlap /*
477a6d42e7dSPeter Dunlap  * Function:  it_config_free()
478a6d42e7dSPeter Dunlap  *
479a6d42e7dSPeter Dunlap  * Free any resources associated with the it_config_t structure.
480a6d42e7dSPeter Dunlap  *
481a6d42e7dSPeter Dunlap  * Parameters:
482a6d42e7dSPeter Dunlap  *    cfg	A C representation of the current iSCSI configuration
483a6d42e7dSPeter Dunlap  */
484a6d42e7dSPeter Dunlap void
485a6d42e7dSPeter Dunlap it_config_free(it_config_t *cfg)
486a6d42e7dSPeter Dunlap {
487a6d42e7dSPeter Dunlap 	it_config_free_cmn(cfg);
488a6d42e7dSPeter Dunlap }
489a6d42e7dSPeter Dunlap 
490a6d42e7dSPeter Dunlap /*
491a6d42e7dSPeter Dunlap  * Function:  it_tgt_create()
492a6d42e7dSPeter Dunlap  *
493a6d42e7dSPeter Dunlap  * Allocate and create an it_tgt_t structure representing a new iSCSI
494a6d42e7dSPeter Dunlap  * target node.  If tgt_name is NULL, then a unique target node name will
495a6d42e7dSPeter Dunlap  * be generated automatically.  Otherwise, the value of tgt_name will be
496a6d42e7dSPeter Dunlap  * used as the target node name.  The new it_tgt_t structure is added to
497a6d42e7dSPeter Dunlap  * the target list (cfg_tgt_list) in the configuration structure, and the
498a6d42e7dSPeter Dunlap  * new target will not be instantiated until the modified configuration
499a6d42e7dSPeter Dunlap  * is committed by calling it_config_commit().
500a6d42e7dSPeter Dunlap  *
501a6d42e7dSPeter Dunlap  * Parameters:
502a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configuration obtained from
503a6d42e7dSPeter Dunlap  *			it_config_load()
504a6d42e7dSPeter Dunlap  *    tgt		Pointer to an iSCSI target structure
505a6d42e7dSPeter Dunlap  *    tgt_name		The target node name for the target to be created.
506a6d42e7dSPeter Dunlap  *			The name must be in either IQN or EUI format.  If
507a6d42e7dSPeter Dunlap  *			this value is NULL, a node name will be generated
508a6d42e7dSPeter Dunlap  *			automatically in IQN format.
509a6d42e7dSPeter Dunlap  *
510a6d42e7dSPeter Dunlap  * Return Values:
511a6d42e7dSPeter Dunlap  *    0			Success
512a6d42e7dSPeter Dunlap  *    ENOMEM		Could not allocated resources
513a6d42e7dSPeter Dunlap  *    EINVAL		Invalid parameter
514a6d42e7dSPeter Dunlap  *    EFAULT		Invalid iSCSI name specified
515836fc322SSam Cramer  *    E2BIG		Too many already exist
516a6d42e7dSPeter Dunlap  */
517a6d42e7dSPeter Dunlap int
518a6d42e7dSPeter Dunlap it_tgt_create(it_config_t *cfg, it_tgt_t **tgt, char *tgt_name)
519a6d42e7dSPeter Dunlap {
520a6d42e7dSPeter Dunlap 	int		ret = 0;
521a6d42e7dSPeter Dunlap 	it_tgt_t	*ptr;
522a6d42e7dSPeter Dunlap 	it_tgt_t	*cfgtgt;
523*fcc214c3SCharles Ting 	char		*namep;
524a6d42e7dSPeter Dunlap 	char		buf[ISCSI_NAME_LEN_MAX + 1];
525a6d42e7dSPeter Dunlap 
526a6d42e7dSPeter Dunlap 	if (!cfg || !tgt) {
527a6d42e7dSPeter Dunlap 		return (EINVAL);
528a6d42e7dSPeter Dunlap 	}
529a6d42e7dSPeter Dunlap 
530*fcc214c3SCharles Ting 	if (!tgt_name) {
531a6d42e7dSPeter Dunlap 		/* generate a name */
532a6d42e7dSPeter Dunlap 		ret = it_iqn_generate(buf, sizeof (buf), NULL);
533a6d42e7dSPeter Dunlap 		if (ret != 0) {
534a6d42e7dSPeter Dunlap 			return (ret);
535a6d42e7dSPeter Dunlap 		}
536a6d42e7dSPeter Dunlap 	} else {
537a6d42e7dSPeter Dunlap 		/* validate the passed-in name */
538*fcc214c3SCharles Ting 		if (!validate_iscsi_name(tgt_name)) {
539a6d42e7dSPeter Dunlap 			return (EFAULT);
540a6d42e7dSPeter Dunlap 		}
541*fcc214c3SCharles Ting 		(void) strlcpy(buf, tgt_name, sizeof (buf));
542*fcc214c3SCharles Ting 		canonical_iscsi_name(buf);
543a6d42e7dSPeter Dunlap 	}
544*fcc214c3SCharles Ting 	namep = buf;
545a6d42e7dSPeter Dunlap 
546a6d42e7dSPeter Dunlap 	/* make sure this name isn't already on the list */
547a6d42e7dSPeter Dunlap 	cfgtgt = cfg->config_tgt_list;
548a6d42e7dSPeter Dunlap 	while (cfgtgt != NULL) {
549*fcc214c3SCharles Ting 		if (strcasecmp(namep, cfgtgt->tgt_name) == 0) {
550a6d42e7dSPeter Dunlap 			return (EEXIST);
551a6d42e7dSPeter Dunlap 		}
552a6d42e7dSPeter Dunlap 		cfgtgt = cfgtgt->tgt_next;
553a6d42e7dSPeter Dunlap 	}
554a6d42e7dSPeter Dunlap 
555ade94d8bSCharles Ting 	/* Too many targets? */
556ade94d8bSCharles Ting 	if (cfg->config_tgt_count >= MAX_TARGETS) {
557ade94d8bSCharles Ting 		return (E2BIG);
558ade94d8bSCharles Ting 	}
559ade94d8bSCharles Ting 
560a6d42e7dSPeter Dunlap 	ptr = calloc(1, sizeof (it_tgt_t));
561a6d42e7dSPeter Dunlap 	if (ptr == NULL) {
562a6d42e7dSPeter Dunlap 		return (ENOMEM);
563a6d42e7dSPeter Dunlap 	}
564a6d42e7dSPeter Dunlap 
565a6d42e7dSPeter Dunlap 	(void) strlcpy(ptr->tgt_name, namep, sizeof (ptr->tgt_name));
566a6d42e7dSPeter Dunlap 	ptr->tgt_generation = 1;
567a6d42e7dSPeter Dunlap 	ptr->tgt_next = cfg->config_tgt_list;
568a6d42e7dSPeter Dunlap 	cfg->config_tgt_list = ptr;
569a6d42e7dSPeter Dunlap 	cfg->config_tgt_count++;
570a6d42e7dSPeter Dunlap 
571a6d42e7dSPeter Dunlap 	*tgt = ptr;
572a6d42e7dSPeter Dunlap 
573a6d42e7dSPeter Dunlap 	return (0);
574a6d42e7dSPeter Dunlap }
575a6d42e7dSPeter Dunlap 
576a6d42e7dSPeter Dunlap /*
577a6d42e7dSPeter Dunlap  * Function:  it_tgt_setprop()
578a6d42e7dSPeter Dunlap  *
579a6d42e7dSPeter Dunlap  * Validate the provided property list and set the properties for
580a6d42e7dSPeter Dunlap  * the specified target.  If errlist is not NULL, returns detailed
581a6d42e7dSPeter Dunlap  * errors for each property that failed.  The format for errorlist
582a6d42e7dSPeter Dunlap  * is key = property, value = error string.
583a6d42e7dSPeter Dunlap  *
584a6d42e7dSPeter Dunlap  * Parameters:
585a6d42e7dSPeter Dunlap  *
586a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configuration obtained from
587a6d42e7dSPeter Dunlap  *			it_config_load()
588a6d42e7dSPeter Dunlap  *    tgt		Pointer to an iSCSI target structure
589a6d42e7dSPeter Dunlap  *    proplist		nvlist_t containing properties for this target.
590a6d42e7dSPeter Dunlap  *    errlist		(optional)  nvlist_t of errors encountered when
591a6d42e7dSPeter Dunlap  *			validating the properties.
592a6d42e7dSPeter Dunlap  *
593a6d42e7dSPeter Dunlap  * Return Values:
594a6d42e7dSPeter Dunlap  *    0			Success
595a6d42e7dSPeter Dunlap  *    EINVAL		Invalid property
596a6d42e7dSPeter Dunlap  *
597a6d42e7dSPeter Dunlap  */
598a6d42e7dSPeter Dunlap int
599a6d42e7dSPeter Dunlap it_tgt_setprop(it_config_t *cfg, it_tgt_t *tgt, nvlist_t *proplist,
600a6d42e7dSPeter Dunlap     nvlist_t **errlist)
601a6d42e7dSPeter Dunlap {
602a6d42e7dSPeter Dunlap 	int		ret;
603a6d42e7dSPeter Dunlap 	nvlist_t	*tprops = NULL;
604a6d42e7dSPeter Dunlap 	char		*val = NULL;
605a6d42e7dSPeter Dunlap 
606a6d42e7dSPeter Dunlap 	if (!cfg || !tgt || !proplist) {
607a6d42e7dSPeter Dunlap 		return (EINVAL);
608a6d42e7dSPeter Dunlap 	}
609a6d42e7dSPeter Dunlap 
610*fcc214c3SCharles Ting 	/* verify the target name in case the target node is renamed */
611*fcc214c3SCharles Ting 	if (!validate_iscsi_name(tgt->tgt_name)) {
612*fcc214c3SCharles Ting 		return (EINVAL);
613*fcc214c3SCharles Ting 	}
614*fcc214c3SCharles Ting 	canonical_iscsi_name(tgt->tgt_name);
615*fcc214c3SCharles Ting 
616a6d42e7dSPeter Dunlap 	if (errlist) {
617a6d42e7dSPeter Dunlap 		(void) nvlist_alloc(errlist, 0, 0);
618a6d42e7dSPeter Dunlap 	}
619a6d42e7dSPeter Dunlap 
620a6d42e7dSPeter Dunlap 	/*
621a6d42e7dSPeter Dunlap 	 * copy the existing properties, merge, then validate
622a6d42e7dSPeter Dunlap 	 * the merged properties before committing them.
623a6d42e7dSPeter Dunlap 	 */
624a6d42e7dSPeter Dunlap 	if (tgt->tgt_properties) {
625a6d42e7dSPeter Dunlap 		ret = nvlist_dup(tgt->tgt_properties, &tprops, 0);
626a6d42e7dSPeter Dunlap 	} else {
627a6d42e7dSPeter Dunlap 		ret = nvlist_alloc(&tprops, NV_UNIQUE_NAME, 0);
628a6d42e7dSPeter Dunlap 	}
629a6d42e7dSPeter Dunlap 
630a6d42e7dSPeter Dunlap 	if (ret == 0) {
631a6d42e7dSPeter Dunlap 		ret = nvlist_merge(tprops, proplist, 0);
632a6d42e7dSPeter Dunlap 	}
633a6d42e7dSPeter Dunlap 
634a6d42e7dSPeter Dunlap 	/* unset chap username or alias if requested */
635a6d42e7dSPeter Dunlap 	val = NULL;
636a6d42e7dSPeter Dunlap 	(void) nvlist_lookup_string(proplist, PROP_TARGET_CHAP_USER, &val);
637a6d42e7dSPeter Dunlap 	if (val && (strcasecmp(val, "none") == 0)) {
638a6d42e7dSPeter Dunlap 		(void) nvlist_remove_all(tprops, PROP_TARGET_CHAP_USER);
639a6d42e7dSPeter Dunlap 	}
640a6d42e7dSPeter Dunlap 
641a6d42e7dSPeter Dunlap 	val = NULL;
642a6d42e7dSPeter Dunlap 	(void) nvlist_lookup_string(proplist, PROP_ALIAS, &val);
643a6d42e7dSPeter Dunlap 	if (val && (strcasecmp(val, "none") == 0)) {
644a6d42e7dSPeter Dunlap 		(void) nvlist_remove_all(tprops, PROP_ALIAS);
645a6d42e7dSPeter Dunlap 	}
646a6d42e7dSPeter Dunlap 
647a6d42e7dSPeter Dunlap 	/* base64 encode the CHAP secret, if it's changed */
648a6d42e7dSPeter Dunlap 	val = NULL;
649a6d42e7dSPeter Dunlap 	(void) nvlist_lookup_string(proplist, PROP_TARGET_CHAP_SECRET, &val);
650a6d42e7dSPeter Dunlap 	if (val) {
651a6d42e7dSPeter Dunlap 		char		bsecret[MAX_BASE64_LEN];
652a6d42e7dSPeter Dunlap 
653a6d42e7dSPeter Dunlap 		ret = it_val_pass(PROP_TARGET_CHAP_SECRET, val, *errlist);
654a6d42e7dSPeter Dunlap 
655a6d42e7dSPeter Dunlap 		if (ret == 0) {
656a6d42e7dSPeter Dunlap 			(void) memset(bsecret, 0, MAX_BASE64_LEN);
657a6d42e7dSPeter Dunlap 
658a6d42e7dSPeter Dunlap 			ret = iscsi_binary_to_base64_str((uint8_t *)val,
659a6d42e7dSPeter Dunlap 			    strlen(val), bsecret, MAX_BASE64_LEN);
660a6d42e7dSPeter Dunlap 
661a6d42e7dSPeter Dunlap 			if (ret == 0) {
662a6d42e7dSPeter Dunlap 				/* replace the value in the nvlist */
663a6d42e7dSPeter Dunlap 				ret = nvlist_add_string(tprops,
664a6d42e7dSPeter Dunlap 				    PROP_TARGET_CHAP_SECRET, bsecret);
665a6d42e7dSPeter Dunlap 			}
666a6d42e7dSPeter Dunlap 		}
667a6d42e7dSPeter Dunlap 	}
668a6d42e7dSPeter Dunlap 
669a6d42e7dSPeter Dunlap 	if (ret == 0) {
670a6d42e7dSPeter Dunlap 		ret = it_validate_tgtprops(tprops, *errlist);
671a6d42e7dSPeter Dunlap 	}
672a6d42e7dSPeter Dunlap 
673a6d42e7dSPeter Dunlap 	if (ret != 0) {
674a6d42e7dSPeter Dunlap 		if (tprops) {
675a6d42e7dSPeter Dunlap 			nvlist_free(tprops);
676a6d42e7dSPeter Dunlap 		}
677a6d42e7dSPeter Dunlap 		return (ret);
678a6d42e7dSPeter Dunlap 	}
679a6d42e7dSPeter Dunlap 
680a6d42e7dSPeter Dunlap 	if (tgt->tgt_properties) {
681a6d42e7dSPeter Dunlap 		nvlist_free(tgt->tgt_properties);
682a6d42e7dSPeter Dunlap 	}
683a6d42e7dSPeter Dunlap 	tgt->tgt_properties = tprops;
684a6d42e7dSPeter Dunlap 
685a6d42e7dSPeter Dunlap 	return (0);
686a6d42e7dSPeter Dunlap }
687a6d42e7dSPeter Dunlap 
688a6d42e7dSPeter Dunlap 
689a6d42e7dSPeter Dunlap /*
690a6d42e7dSPeter Dunlap  * Function:  it_tgt_delete()
691a6d42e7dSPeter Dunlap  *
692a6d42e7dSPeter Dunlap  * Delete target represented by 'tgt', where 'tgt' is an existing
693a6d42e7dSPeter Dunlap  * it_tgt_structure within the configuration 'cfg'.  The target removal
694a6d42e7dSPeter Dunlap  * will not take effect until the modified configuration is committed
695a6d42e7dSPeter Dunlap  * by calling it_config_commit().
696a6d42e7dSPeter Dunlap  *
697a6d42e7dSPeter Dunlap  * Parameters:
698a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configuration obtained from
699a6d42e7dSPeter Dunlap  *			it_config_load()
700a6d42e7dSPeter Dunlap  *    tgt		Pointer to an iSCSI target structure
701a6d42e7dSPeter Dunlap  *
702a6d42e7dSPeter Dunlap  *    force		Set the target to offline before removing it from
703a6d42e7dSPeter Dunlap  *			the config.  If not specified, the operation will
704a6d42e7dSPeter Dunlap  *			fail if the target is determined to be online.
705a6d42e7dSPeter Dunlap  * Return Values:
706a6d42e7dSPeter Dunlap  *    0			Success
707a6d42e7dSPeter Dunlap  *    EBUSY		Target is online
708a6d42e7dSPeter Dunlap  */
709a6d42e7dSPeter Dunlap int
710a6d42e7dSPeter Dunlap it_tgt_delete(it_config_t *cfg, it_tgt_t *tgt, boolean_t force)
711a6d42e7dSPeter Dunlap {
712a6d42e7dSPeter Dunlap 	int			ret;
713a6d42e7dSPeter Dunlap 	it_tgt_t		*ptgt;
714a6d42e7dSPeter Dunlap 	it_tgt_t		*prev = NULL;
715a6d42e7dSPeter Dunlap 	stmfDevid		devid;
716a6d42e7dSPeter Dunlap 	stmfTargetProperties	props;
717a6d42e7dSPeter Dunlap 
718a6d42e7dSPeter Dunlap 	if (!cfg || !tgt) {
719a6d42e7dSPeter Dunlap 		return (0);
720a6d42e7dSPeter Dunlap 	}
721a6d42e7dSPeter Dunlap 
722a6d42e7dSPeter Dunlap 	ptgt = cfg->config_tgt_list;
723a6d42e7dSPeter Dunlap 	while (ptgt != NULL) {
724*fcc214c3SCharles Ting 		if (strcasecmp(tgt->tgt_name, ptgt->tgt_name) == 0) {
725a6d42e7dSPeter Dunlap 			break;
726a6d42e7dSPeter Dunlap 		}
727a6d42e7dSPeter Dunlap 		prev = ptgt;
728a6d42e7dSPeter Dunlap 		ptgt = ptgt->tgt_next;
729a6d42e7dSPeter Dunlap 	}
730a6d42e7dSPeter Dunlap 
731a6d42e7dSPeter Dunlap 	if (!ptgt) {
732a6d42e7dSPeter Dunlap 		return (0);
733a6d42e7dSPeter Dunlap 	}
734a6d42e7dSPeter Dunlap 
735a6d42e7dSPeter Dunlap 	/*
736a6d42e7dSPeter Dunlap 	 * check to see if this target is offline.  If it is not,
737a6d42e7dSPeter Dunlap 	 * and the 'force' flag is TRUE, tell STMF to offline it
738a6d42e7dSPeter Dunlap 	 * before removing from the configuration.
739a6d42e7dSPeter Dunlap 	 */
740a6d42e7dSPeter Dunlap 	ret = stmfDevidFromIscsiName(ptgt->tgt_name, &devid);
741a6d42e7dSPeter Dunlap 	if (ret != STMF_STATUS_SUCCESS) {
742a6d42e7dSPeter Dunlap 		/* can't happen? */
743a6d42e7dSPeter Dunlap 		return (EINVAL);
744a6d42e7dSPeter Dunlap 	}
745a6d42e7dSPeter Dunlap 
746a6d42e7dSPeter Dunlap 	ret = stmfGetTargetProperties(&devid, &props);
747a6d42e7dSPeter Dunlap 	if (ret == STMF_STATUS_SUCCESS) {
748a6d42e7dSPeter Dunlap 		/*
749a6d42e7dSPeter Dunlap 		 * only other return is STMF_ERROR_NOT_FOUND, which
750a6d42e7dSPeter Dunlap 		 * means we don't have to offline it.
751a6d42e7dSPeter Dunlap 		 */
752a6d42e7dSPeter Dunlap 		if (props.status == STMF_TARGET_PORT_ONLINE) {
753a6d42e7dSPeter Dunlap 			if (!force) {
754a6d42e7dSPeter Dunlap 				return (EBUSY);
755a6d42e7dSPeter Dunlap 			}
756a6d42e7dSPeter Dunlap 			ret = stmfOfflineTarget(&devid);
757a6d42e7dSPeter Dunlap 			if (ret != 0) {
758a6d42e7dSPeter Dunlap 				return (EBUSY);
759a6d42e7dSPeter Dunlap 			}
760a6d42e7dSPeter Dunlap 		}
761a6d42e7dSPeter Dunlap 	}
762a6d42e7dSPeter Dunlap 
763a6d42e7dSPeter Dunlap 	if (prev) {
764a6d42e7dSPeter Dunlap 		prev->tgt_next = ptgt->tgt_next;
765a6d42e7dSPeter Dunlap 	} else {
766a6d42e7dSPeter Dunlap 		/* first one on the list */
767a6d42e7dSPeter Dunlap 		cfg->config_tgt_list = ptgt->tgt_next;
768a6d42e7dSPeter Dunlap 	}
769a6d42e7dSPeter Dunlap 
770a6d42e7dSPeter Dunlap 	ptgt->tgt_next = NULL; /* Only free this target */
771a6d42e7dSPeter Dunlap 
772a6d42e7dSPeter Dunlap 	cfg->config_tgt_count--;
773a6d42e7dSPeter Dunlap 	it_tgt_free(ptgt);
774a6d42e7dSPeter Dunlap 
775a6d42e7dSPeter Dunlap 	return (0);
776a6d42e7dSPeter Dunlap }
777a6d42e7dSPeter Dunlap 
778a6d42e7dSPeter Dunlap /*
779a6d42e7dSPeter Dunlap  * Function:  it_tgt_free()
780a6d42e7dSPeter Dunlap  *
781a6d42e7dSPeter Dunlap  * Frees an it_tgt_t structure.  If tgt_next is not NULL, frees
782a6d42e7dSPeter Dunlap  * all structures in the list.
783a6d42e7dSPeter Dunlap  */
784a6d42e7dSPeter Dunlap void
785a6d42e7dSPeter Dunlap it_tgt_free(it_tgt_t *tgt)
786a6d42e7dSPeter Dunlap {
787a6d42e7dSPeter Dunlap 	it_tgt_free_cmn(tgt);
788a6d42e7dSPeter Dunlap }
789a6d42e7dSPeter Dunlap 
790a6d42e7dSPeter Dunlap /*
791a6d42e7dSPeter Dunlap  * Function:  it_tpgt_create()
792a6d42e7dSPeter Dunlap  *
793a6d42e7dSPeter Dunlap  * Allocate and create an it_tpgt_t structure representing a new iSCSI
794a6d42e7dSPeter Dunlap  * target portal group tag.  The new it_tpgt_t structure is added to the
795a6d42e7dSPeter Dunlap  * target tpgt list (tgt_tpgt_list) in the it_tgt_t structure.  The new
796a6d42e7dSPeter Dunlap  * target portal group tag will not be instantiated until the modified
797a6d42e7dSPeter Dunlap  * configuration is committed by calling it_config_commit().
798a6d42e7dSPeter Dunlap  *
799a6d42e7dSPeter Dunlap  * Parameters:
800a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configuration obtained from
801a6d42e7dSPeter Dunlap  *			it_config_load()
802a6d42e7dSPeter Dunlap  *    tgt		Pointer to the iSCSI target structure associated
803a6d42e7dSPeter Dunlap  *			with the target portal group tag
804a6d42e7dSPeter Dunlap  *    tpgt		Pointer to a target portal group tag structure
805a6d42e7dSPeter Dunlap  *    tpg_name		The name of the TPG to be associated with this TPGT
806a6d42e7dSPeter Dunlap  *    tpgt_tag		16-bit numerical identifier for this TPGT.  If
807a6d42e7dSPeter Dunlap  *			tpgt_tag is '0', this function will choose the
808a6d42e7dSPeter Dunlap  *			tag number.  If tpgt_tag is >0, and the requested
809a6d42e7dSPeter Dunlap  *			tag is determined to be in use, another value
810a6d42e7dSPeter Dunlap  *			will be chosen.
811a6d42e7dSPeter Dunlap  *
812a6d42e7dSPeter Dunlap  * Return Values:
813a6d42e7dSPeter Dunlap  *    0			Success
814a6d42e7dSPeter Dunlap  *    ENOMEM		Could not allocate resources
815a6d42e7dSPeter Dunlap  *    EINVAL		Invalid parameter
816a6d42e7dSPeter Dunlap  *    EEXIST		Specified tag name is already used.
817a6d42e7dSPeter Dunlap  *    E2BIG		No available tag numbers
818a6d42e7dSPeter Dunlap  */
819a6d42e7dSPeter Dunlap int
820a6d42e7dSPeter Dunlap it_tpgt_create(it_config_t *cfg, it_tgt_t *tgt, it_tpgt_t **tpgt,
821a6d42e7dSPeter Dunlap     char *tpg_name, uint16_t tpgt_tag)
822a6d42e7dSPeter Dunlap {
823a6d42e7dSPeter Dunlap 	it_tpgt_t	*ptr = NULL;
824a6d42e7dSPeter Dunlap 	it_tpgt_t	*cfgt;
825a6d42e7dSPeter Dunlap 	char		tagid_used[MAXTAG + 1];
826a6d42e7dSPeter Dunlap 	uint16_t	tagid = ISCSIT_DEFAULT_TPGT;
827a6d42e7dSPeter Dunlap 
828a6d42e7dSPeter Dunlap 	if (!cfg || !tgt || !tpgt || !tpg_name) {
829a6d42e7dSPeter Dunlap 		return (EINVAL);
830a6d42e7dSPeter Dunlap 	}
831a6d42e7dSPeter Dunlap 
832a6d42e7dSPeter Dunlap 	(void) memset(&(tagid_used[0]), 0, sizeof (tagid_used));
833a6d42e7dSPeter Dunlap 
834a6d42e7dSPeter Dunlap 	/*
835a6d42e7dSPeter Dunlap 	 * Make sure this name and/or tag isn't already on the list
836a6d42e7dSPeter Dunlap 	 * At the same time, capture all tag ids in use for this target
837a6d42e7dSPeter Dunlap 	 *
838a6d42e7dSPeter Dunlap 	 * About tag numbering -- since tag numbers are used by
839a6d42e7dSPeter Dunlap 	 * the iSCSI protocol, we should be careful about reusing
840a6d42e7dSPeter Dunlap 	 * them too quickly.  Start with a value greater than the
841a6d42e7dSPeter Dunlap 	 * highest one currently defined.  If current == MAXTAG,
842a6d42e7dSPeter Dunlap 	 * just find an unused tag.
843a6d42e7dSPeter Dunlap 	 */
844a6d42e7dSPeter Dunlap 	cfgt = tgt->tgt_tpgt_list;
845a6d42e7dSPeter Dunlap 	while (cfgt != NULL) {
846a6d42e7dSPeter Dunlap 		tagid_used[cfgt->tpgt_tag] = 1;
847a6d42e7dSPeter Dunlap 
848a6d42e7dSPeter Dunlap 		if (strcmp(tpg_name, cfgt->tpgt_tpg_name) == 0) {
849a6d42e7dSPeter Dunlap 			return (EEXIST);
850a6d42e7dSPeter Dunlap 		}
851a6d42e7dSPeter Dunlap 
852a6d42e7dSPeter Dunlap 		if (cfgt->tpgt_tag > tagid) {
853a6d42e7dSPeter Dunlap 			tagid = cfgt->tpgt_tag;
854a6d42e7dSPeter Dunlap 		}
855a6d42e7dSPeter Dunlap 
856a6d42e7dSPeter Dunlap 		cfgt = cfgt->tpgt_next;
857a6d42e7dSPeter Dunlap 	}
858a6d42e7dSPeter Dunlap 
859a6d42e7dSPeter Dunlap 	if ((tpgt_tag > ISCSIT_DEFAULT_TPGT) && (tpgt_tag < MAXTAG) &&
860a6d42e7dSPeter Dunlap 	    (tagid_used[tpgt_tag] == 0)) {
861a6d42e7dSPeter Dunlap 		/* ok to use requested */
862a6d42e7dSPeter Dunlap 		tagid = tpgt_tag;
863a6d42e7dSPeter Dunlap 	} else if (tagid == MAXTAG) {
864a6d42e7dSPeter Dunlap 		/*
865a6d42e7dSPeter Dunlap 		 * The highest value is used, find an available id.
866a6d42e7dSPeter Dunlap 		 */
867a6d42e7dSPeter Dunlap 		tagid = ISCSIT_DEFAULT_TPGT + 1;
868a6d42e7dSPeter Dunlap 		for (; tagid < MAXTAG; tagid++) {
869a6d42e7dSPeter Dunlap 			if (tagid_used[tagid] == 0) {
870a6d42e7dSPeter Dunlap 				break;
871a6d42e7dSPeter Dunlap 			}
872a6d42e7dSPeter Dunlap 		}
873a6d42e7dSPeter Dunlap 		if (tagid >= MAXTAG) {
874a6d42e7dSPeter Dunlap 			return (E2BIG);
875a6d42e7dSPeter Dunlap 		}
876a6d42e7dSPeter Dunlap 	} else {
877a6d42e7dSPeter Dunlap 		/* next available ID */
878a6d42e7dSPeter Dunlap 		tagid++;
879a6d42e7dSPeter Dunlap 	}
880a6d42e7dSPeter Dunlap 
881a6d42e7dSPeter Dunlap 	ptr = calloc(1, sizeof (it_tpgt_t));
882a6d42e7dSPeter Dunlap 	if (!ptr) {
883a6d42e7dSPeter Dunlap 		return (ENOMEM);
884a6d42e7dSPeter Dunlap 	}
885a6d42e7dSPeter Dunlap 
886a6d42e7dSPeter Dunlap 	(void) strlcpy(ptr->tpgt_tpg_name, tpg_name,
887a6d42e7dSPeter Dunlap 	    sizeof (ptr->tpgt_tpg_name));
888a6d42e7dSPeter Dunlap 	ptr->tpgt_generation = 1;
889a6d42e7dSPeter Dunlap 	ptr->tpgt_tag = tagid;
890a6d42e7dSPeter Dunlap 
891a6d42e7dSPeter Dunlap 	ptr->tpgt_next = tgt->tgt_tpgt_list;
892a6d42e7dSPeter Dunlap 	tgt->tgt_tpgt_list = ptr;
893a6d42e7dSPeter Dunlap 	tgt->tgt_tpgt_count++;
894a6d42e7dSPeter Dunlap 	tgt->tgt_generation++;
895a6d42e7dSPeter Dunlap 
896a6d42e7dSPeter Dunlap 	*tpgt = ptr;
897a6d42e7dSPeter Dunlap 
898a6d42e7dSPeter Dunlap 	return (0);
899a6d42e7dSPeter Dunlap }
900a6d42e7dSPeter Dunlap 
901a6d42e7dSPeter Dunlap /*
902a6d42e7dSPeter Dunlap  * Function:  it_tpgt_delete()
903a6d42e7dSPeter Dunlap  *
904a6d42e7dSPeter Dunlap  * Delete the target portal group tag represented by 'tpgt', where
905a6d42e7dSPeter Dunlap  * 'tpgt' is an existing is_tpgt_t structure within the target 'tgt'.
906a6d42e7dSPeter Dunlap  * The target portal group tag removal will not take effect until the
907a6d42e7dSPeter Dunlap  * modified configuration is committed by calling it_config_commit().
908a6d42e7dSPeter Dunlap  *
909a6d42e7dSPeter Dunlap  * Parameters:
910a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configuration obtained from
911a6d42e7dSPeter Dunlap  *			it_config_load()
912a6d42e7dSPeter Dunlap  *    tgt		Pointer to the iSCSI target structure associated
913a6d42e7dSPeter Dunlap  *			with the target portal group tag
914a6d42e7dSPeter Dunlap  *    tpgt		Pointer to a target portal group tag structure
915a6d42e7dSPeter Dunlap  */
916a6d42e7dSPeter Dunlap void
917a6d42e7dSPeter Dunlap it_tpgt_delete(it_config_t *cfg, it_tgt_t *tgt, it_tpgt_t *tpgt)
918a6d42e7dSPeter Dunlap {
919a6d42e7dSPeter Dunlap 	it_tpgt_t	*ptr;
920a6d42e7dSPeter Dunlap 	it_tpgt_t	*prev = NULL;
921a6d42e7dSPeter Dunlap 
922a6d42e7dSPeter Dunlap 	if (!cfg || !tgt || !tpgt) {
923a6d42e7dSPeter Dunlap 		return;
924a6d42e7dSPeter Dunlap 	}
925a6d42e7dSPeter Dunlap 
926a6d42e7dSPeter Dunlap 	ptr = tgt->tgt_tpgt_list;
927a6d42e7dSPeter Dunlap 	while (ptr) {
928a6d42e7dSPeter Dunlap 		if (ptr->tpgt_tag == tpgt->tpgt_tag) {
929a6d42e7dSPeter Dunlap 			break;
930a6d42e7dSPeter Dunlap 		}
931a6d42e7dSPeter Dunlap 		prev = ptr;
932a6d42e7dSPeter Dunlap 		ptr = ptr->tpgt_next;
933a6d42e7dSPeter Dunlap 	}
934a6d42e7dSPeter Dunlap 
935a6d42e7dSPeter Dunlap 	if (!ptr) {
936a6d42e7dSPeter Dunlap 		return;
937a6d42e7dSPeter Dunlap 	}
938a6d42e7dSPeter Dunlap 
939a6d42e7dSPeter Dunlap 	if (prev) {
940a6d42e7dSPeter Dunlap 		prev->tpgt_next = ptr->tpgt_next;
941a6d42e7dSPeter Dunlap 	} else {
942a6d42e7dSPeter Dunlap 		tgt->tgt_tpgt_list = ptr->tpgt_next;
943a6d42e7dSPeter Dunlap 	}
944a6d42e7dSPeter Dunlap 	ptr->tpgt_next = NULL;
945a6d42e7dSPeter Dunlap 
946a6d42e7dSPeter Dunlap 	tgt->tgt_tpgt_count--;
947a6d42e7dSPeter Dunlap 	tgt->tgt_generation++;
948a6d42e7dSPeter Dunlap 
949a6d42e7dSPeter Dunlap 	it_tpgt_free(ptr);
950a6d42e7dSPeter Dunlap }
951a6d42e7dSPeter Dunlap 
952a6d42e7dSPeter Dunlap /*
953a6d42e7dSPeter Dunlap  * Function:  it_tpgt_free()
954a6d42e7dSPeter Dunlap  *
955a6d42e7dSPeter Dunlap  * Deallocates resources of an it_tpgt_t structure.  If tpgt->next
956a6d42e7dSPeter Dunlap  * is not NULL, frees all members of the list.
957a6d42e7dSPeter Dunlap  */
958a6d42e7dSPeter Dunlap void
959a6d42e7dSPeter Dunlap it_tpgt_free(it_tpgt_t *tpgt)
960a6d42e7dSPeter Dunlap {
961a6d42e7dSPeter Dunlap 	it_tpgt_free_cmn(tpgt);
962a6d42e7dSPeter Dunlap }
963a6d42e7dSPeter Dunlap 
964a6d42e7dSPeter Dunlap /*
965a6d42e7dSPeter Dunlap  * Function:  it_tpg_create()
966a6d42e7dSPeter Dunlap  *
967a6d42e7dSPeter Dunlap  * Allocate and create an it_tpg_t structure representing a new iSCSI
968a6d42e7dSPeter Dunlap  * target portal group.  The new it_tpg_t structure is added to the global
969a6d42e7dSPeter Dunlap  * tpg list (cfg_tgt_list) in the it_config_t structure.  The new target
970a6d42e7dSPeter Dunlap  * portal group will not be instantiated until the modified configuration
971a6d42e7dSPeter Dunlap  * is committed by calling it_config_commit().
972a6d42e7dSPeter Dunlap  *
973a6d42e7dSPeter Dunlap  * Parameters:
974a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configuration obtained from
975a6d42e7dSPeter Dunlap  *			it_config_load()
976a6d42e7dSPeter Dunlap  *    tpg		Pointer to the it_tpg_t structure representing
977a6d42e7dSPeter Dunlap  *			the target portal group
978a6d42e7dSPeter Dunlap  *    tpg_name		Identifier for the target portal group
979a6d42e7dSPeter Dunlap  *    portal_ip_port	A string containing an appropriatedly formatted
980a6d42e7dSPeter Dunlap  *			IP address:port.  Both IPv4 and IPv6 addresses are
981a6d42e7dSPeter Dunlap  *			permitted.  This value becomes the first portal in
982a6d42e7dSPeter Dunlap  *			the TPG -- applications can add additional values
983a6d42e7dSPeter Dunlap  *			using it_portal_create() before committing the TPG.
984a6d42e7dSPeter Dunlap  * Return Values:
985a6d42e7dSPeter Dunlap  *    0			Success
986a6d42e7dSPeter Dunlap  *    ENOMEM		Cannot allocate resources
987a6d42e7dSPeter Dunlap  *    EINVAL		Invalid parameter
988a6d42e7dSPeter Dunlap  *    EEXIST		Requested portal in use by another target portal
989a6d42e7dSPeter Dunlap  *			group
990a6d42e7dSPeter Dunlap  */
991a6d42e7dSPeter Dunlap int
992a6d42e7dSPeter Dunlap it_tpg_create(it_config_t *cfg, it_tpg_t **tpg, char *tpg_name,
993a6d42e7dSPeter Dunlap     char *portal_ip_port)
994a6d42e7dSPeter Dunlap {
995a6d42e7dSPeter Dunlap 	int		ret;
996a6d42e7dSPeter Dunlap 	it_tpg_t	*ptr;
997a6d42e7dSPeter Dunlap 	it_portal_t	*portal = NULL;
998a6d42e7dSPeter Dunlap 
999a6d42e7dSPeter Dunlap 	if (!cfg || !tpg || !tpg_name || !portal_ip_port) {
1000a6d42e7dSPeter Dunlap 		return (EINVAL);
1001a6d42e7dSPeter Dunlap 	}
1002a6d42e7dSPeter Dunlap 
1003a6d42e7dSPeter Dunlap 	*tpg = NULL;
1004a6d42e7dSPeter Dunlap 
1005a6d42e7dSPeter Dunlap 	ptr = cfg->config_tpg_list;
1006a6d42e7dSPeter Dunlap 	while (ptr) {
1007a6d42e7dSPeter Dunlap 		if (strcmp(tpg_name, ptr->tpg_name) == 0) {
1008a6d42e7dSPeter Dunlap 			break;
1009a6d42e7dSPeter Dunlap 		}
1010a6d42e7dSPeter Dunlap 		ptr = ptr->tpg_next;
1011a6d42e7dSPeter Dunlap 	}
1012a6d42e7dSPeter Dunlap 
1013a6d42e7dSPeter Dunlap 	if (ptr) {
1014a6d42e7dSPeter Dunlap 		return (EEXIST);
1015a6d42e7dSPeter Dunlap 	}
1016a6d42e7dSPeter Dunlap 
1017a6d42e7dSPeter Dunlap 	ptr = calloc(1, sizeof (it_tpg_t));
1018a6d42e7dSPeter Dunlap 	if (!ptr) {
1019a6d42e7dSPeter Dunlap 		return (ENOMEM);
1020a6d42e7dSPeter Dunlap 	}
1021a6d42e7dSPeter Dunlap 
1022a6d42e7dSPeter Dunlap 	ptr->tpg_generation = 1;
1023a6d42e7dSPeter Dunlap 	(void) strlcpy(ptr->tpg_name, tpg_name, sizeof (ptr->tpg_name));
1024a6d42e7dSPeter Dunlap 
1025a6d42e7dSPeter Dunlap 	/* create the portal */
1026a6d42e7dSPeter Dunlap 	ret = it_portal_create(cfg, ptr, &portal, portal_ip_port);
1027a6d42e7dSPeter Dunlap 	if (ret != 0) {
1028a6d42e7dSPeter Dunlap 		free(ptr);
1029a6d42e7dSPeter Dunlap 		return (ret);
1030a6d42e7dSPeter Dunlap 	}
1031a6d42e7dSPeter Dunlap 
1032a6d42e7dSPeter Dunlap 	ptr->tpg_next = cfg->config_tpg_list;
1033a6d42e7dSPeter Dunlap 	cfg->config_tpg_list = ptr;
1034a6d42e7dSPeter Dunlap 	cfg->config_tpg_count++;
1035a6d42e7dSPeter Dunlap 
1036a6d42e7dSPeter Dunlap 	*tpg = ptr;
1037a6d42e7dSPeter Dunlap 
1038a6d42e7dSPeter Dunlap 	return (0);
1039a6d42e7dSPeter Dunlap }
1040a6d42e7dSPeter Dunlap 
1041a6d42e7dSPeter Dunlap /*
1042a6d42e7dSPeter Dunlap  * Function:  it_tpg_delete()
1043a6d42e7dSPeter Dunlap  *
1044a6d42e7dSPeter Dunlap  * Delete target portal group represented by 'tpg', where 'tpg' is an
1045a6d42e7dSPeter Dunlap  * existing it_tpg_t structure within the global configuration 'cfg'.
1046a6d42e7dSPeter Dunlap  * The target portal group removal will not take effect until the
1047a6d42e7dSPeter Dunlap  * modified configuration is committed by calling it_config_commit().
1048a6d42e7dSPeter Dunlap  *
1049a6d42e7dSPeter Dunlap  * Parameters:
1050a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configuration obtained from
1051a6d42e7dSPeter Dunlap  *			it_config_load()
1052a6d42e7dSPeter Dunlap  *    tpg		Pointer to the it_tpg_t structure representing
1053a6d42e7dSPeter Dunlap  *			the target portal group
1054a6d42e7dSPeter Dunlap  *    force		Remove this target portal group even if it's
1055a6d42e7dSPeter Dunlap  *			associated with one or more targets.
1056a6d42e7dSPeter Dunlap  *
1057a6d42e7dSPeter Dunlap  * Return Values:
1058a6d42e7dSPeter Dunlap  *    0			Success
1059a6d42e7dSPeter Dunlap  *    EINVAL		Invalid parameter
1060a6d42e7dSPeter Dunlap  *    EBUSY		Portal group associated with one or more targets.
1061a6d42e7dSPeter Dunlap  */
1062a6d42e7dSPeter Dunlap int
1063a6d42e7dSPeter Dunlap it_tpg_delete(it_config_t *cfg, it_tpg_t *tpg, boolean_t force)
1064a6d42e7dSPeter Dunlap {
1065a6d42e7dSPeter Dunlap 	it_tpg_t	*ptr;
1066a6d42e7dSPeter Dunlap 	it_tpg_t	*prev = NULL;
1067a6d42e7dSPeter Dunlap 	it_tgt_t	*tgt;
1068a6d42e7dSPeter Dunlap 	it_tpgt_t	*tpgt;
1069a6d42e7dSPeter Dunlap 	it_tpgt_t	*ntpgt;
1070a6d42e7dSPeter Dunlap 
1071a6d42e7dSPeter Dunlap 	if (!cfg || !tpg) {
1072a6d42e7dSPeter Dunlap 		return (EINVAL);
1073a6d42e7dSPeter Dunlap 	}
1074a6d42e7dSPeter Dunlap 
1075a6d42e7dSPeter Dunlap 	ptr = cfg->config_tpg_list;
1076a6d42e7dSPeter Dunlap 	while (ptr) {
1077a6d42e7dSPeter Dunlap 		if (strcmp(ptr->tpg_name, tpg->tpg_name) == 0) {
1078a6d42e7dSPeter Dunlap 			break;
1079a6d42e7dSPeter Dunlap 		}
1080a6d42e7dSPeter Dunlap 		prev = ptr;
1081a6d42e7dSPeter Dunlap 		ptr = ptr->tpg_next;
1082a6d42e7dSPeter Dunlap 	}
1083a6d42e7dSPeter Dunlap 
1084a6d42e7dSPeter Dunlap 	if (!ptr) {
1085a6d42e7dSPeter Dunlap 		return (0);
1086a6d42e7dSPeter Dunlap 	}
1087a6d42e7dSPeter Dunlap 
1088a6d42e7dSPeter Dunlap 	/*
1089a6d42e7dSPeter Dunlap 	 * See if any targets are using this portal group.
1090a6d42e7dSPeter Dunlap 	 * If there are, and the force flag is not set, fail.
1091a6d42e7dSPeter Dunlap 	 */
1092a6d42e7dSPeter Dunlap 	tgt = cfg->config_tgt_list;
1093a6d42e7dSPeter Dunlap 	while (tgt) {
1094a6d42e7dSPeter Dunlap 		tpgt = tgt->tgt_tpgt_list;
1095a6d42e7dSPeter Dunlap 		while (tpgt) {
1096a6d42e7dSPeter Dunlap 			ntpgt = tpgt->tpgt_next;
1097a6d42e7dSPeter Dunlap 
1098a6d42e7dSPeter Dunlap 			if (strcmp(tpgt->tpgt_tpg_name, tpg->tpg_name)
1099a6d42e7dSPeter Dunlap 			    == 0) {
1100a6d42e7dSPeter Dunlap 				if (!force) {
1101a6d42e7dSPeter Dunlap 					return (EBUSY);
1102a6d42e7dSPeter Dunlap 				}
1103a6d42e7dSPeter Dunlap 				it_tpgt_delete(cfg, tgt, tpgt);
1104a6d42e7dSPeter Dunlap 			}
1105a6d42e7dSPeter Dunlap 
1106a6d42e7dSPeter Dunlap 			tpgt = ntpgt;
1107a6d42e7dSPeter Dunlap 		}
1108a6d42e7dSPeter Dunlap 		tgt = tgt->tgt_next;
1109a6d42e7dSPeter Dunlap 	}
1110a6d42e7dSPeter Dunlap 
1111a6d42e7dSPeter Dunlap 	/* Now that it's not in use anywhere, remove the TPG */
1112a6d42e7dSPeter Dunlap 	if (prev) {
1113a6d42e7dSPeter Dunlap 		prev->tpg_next = ptr->tpg_next;
1114a6d42e7dSPeter Dunlap 	} else {
1115a6d42e7dSPeter Dunlap 		cfg->config_tpg_list = ptr->tpg_next;
1116a6d42e7dSPeter Dunlap 	}
1117a6d42e7dSPeter Dunlap 	ptr->tpg_next = NULL;
1118a6d42e7dSPeter Dunlap 
1119a6d42e7dSPeter Dunlap 	cfg->config_tpg_count--;
1120a6d42e7dSPeter Dunlap 
1121a6d42e7dSPeter Dunlap 	it_tpg_free(ptr);
1122a6d42e7dSPeter Dunlap 
1123a6d42e7dSPeter Dunlap 	return (0);
1124a6d42e7dSPeter Dunlap }
1125a6d42e7dSPeter Dunlap 
1126a6d42e7dSPeter Dunlap /*
1127a6d42e7dSPeter Dunlap  * Function:  it_tpg_free()
1128a6d42e7dSPeter Dunlap  *
1129a6d42e7dSPeter Dunlap  * Deallocates resources associated with an it_tpg_t structure.
1130a6d42e7dSPeter Dunlap  * If tpg->next is not NULL, frees all members of the list.
1131a6d42e7dSPeter Dunlap  */
1132a6d42e7dSPeter Dunlap void
1133a6d42e7dSPeter Dunlap it_tpg_free(it_tpg_t *tpg)
1134a6d42e7dSPeter Dunlap {
1135a6d42e7dSPeter Dunlap 	it_tpg_free_cmn(tpg);
1136a6d42e7dSPeter Dunlap }
1137a6d42e7dSPeter Dunlap 
1138a6d42e7dSPeter Dunlap /*
1139a6d42e7dSPeter Dunlap  * Function:  it_portal_create()
1140a6d42e7dSPeter Dunlap  *
1141a6d42e7dSPeter Dunlap  * Add an it_portal_t structure presenting a new portal to the specified
1142a6d42e7dSPeter Dunlap  * target portal group.  The change to the target portal group will not take
1143a6d42e7dSPeter Dunlap  * effect until the modified configuration is committed by calling
1144a6d42e7dSPeter Dunlap  * it_config_commit().
1145a6d42e7dSPeter Dunlap  *
1146a6d42e7dSPeter Dunlap  * Parameters:
1147a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configration obtained from
1148a6d42e7dSPeter Dunlap  *			it_config_load()
1149a6d42e7dSPeter Dunlap  *    tpg		Pointer to the it_tpg_t structure representing the
1150a6d42e7dSPeter Dunlap  *			target portal group
1151a6d42e7dSPeter Dunlap  *    portal		Pointer to the it_portal_t structure representing
1152a6d42e7dSPeter Dunlap  *			the portal
1153a6d42e7dSPeter Dunlap  *    portal_ip_port	A string containing an appropriately formatted
1154a6d42e7dSPeter Dunlap  *			IP address or IP address:port in either IPv4 or
1155a6d42e7dSPeter Dunlap  *			IPv6 format.
1156a6d42e7dSPeter Dunlap  * Return Values:
1157a6d42e7dSPeter Dunlap  *    0			Success
1158a6d42e7dSPeter Dunlap  *    ENOMEM		Could not allocate resources
1159a6d42e7dSPeter Dunlap  *    EINVAL		Invalid parameter
1160a6d42e7dSPeter Dunlap  *    EEXIST		Portal already configured for another portal group
1161a6d42e7dSPeter Dunlap  */
1162a6d42e7dSPeter Dunlap int
1163a6d42e7dSPeter Dunlap it_portal_create(it_config_t *cfg, it_tpg_t *tpg, it_portal_t **portal,
1164a6d42e7dSPeter Dunlap     char *portal_ip_port)
1165a6d42e7dSPeter Dunlap {
1166a6d42e7dSPeter Dunlap 	struct sockaddr_storage		sa;
1167a6d42e7dSPeter Dunlap 	it_portal_t			*ptr;
1168a6d42e7dSPeter Dunlap 	it_tpg_t			*ctpg = NULL;
1169a6d42e7dSPeter Dunlap 
1170a6d42e7dSPeter Dunlap 	if (!cfg || !tpg || !portal || !portal_ip_port) {
1171a6d42e7dSPeter Dunlap 		return (EINVAL);
1172a6d42e7dSPeter Dunlap 	}
1173a6d42e7dSPeter Dunlap 
1174a6d42e7dSPeter Dunlap 	if ((it_common_convert_sa(portal_ip_port, &sa, ISCSI_LISTEN_PORT))
1175a6d42e7dSPeter Dunlap 	    == NULL) {
1176a6d42e7dSPeter Dunlap 		return (EINVAL);
1177a6d42e7dSPeter Dunlap 	}
1178a6d42e7dSPeter Dunlap 
1179a6d42e7dSPeter Dunlap 	/* Check that this portal doesn't appear in any other tag */
1180a6d42e7dSPeter Dunlap 	ctpg = cfg->config_tpg_list;
1181a6d42e7dSPeter Dunlap 	while (ctpg) {
1182a6d42e7dSPeter Dunlap 		ptr = ctpg->tpg_portal_list;
1183a6d42e7dSPeter Dunlap 		for (; ptr != NULL; ptr = ptr->next) {
1184a6d42e7dSPeter Dunlap 			if (it_sa_compare(&(ptr->portal_addr), &sa) != 0) {
1185a6d42e7dSPeter Dunlap 				continue;
1186a6d42e7dSPeter Dunlap 			}
1187a6d42e7dSPeter Dunlap 
1188a6d42e7dSPeter Dunlap 			/*
1189a6d42e7dSPeter Dunlap 			 * Existing in the same group is not an error,
1190a6d42e7dSPeter Dunlap 			 * but don't add it again.
1191a6d42e7dSPeter Dunlap 			 */
1192a6d42e7dSPeter Dunlap 			if (strcmp(ctpg->tpg_name, tpg->tpg_name) == 0) {
1193a6d42e7dSPeter Dunlap 				return (0);
1194a6d42e7dSPeter Dunlap 			} else {
1195a6d42e7dSPeter Dunlap 				/* Not allowed */
1196a6d42e7dSPeter Dunlap 				return (EEXIST);
1197a6d42e7dSPeter Dunlap 			}
1198a6d42e7dSPeter Dunlap 		}
1199a6d42e7dSPeter Dunlap 		ctpg = ctpg->tpg_next;
1200a6d42e7dSPeter Dunlap 	}
1201a6d42e7dSPeter Dunlap 
1202a6d42e7dSPeter Dunlap 	ptr = calloc(1, sizeof (it_portal_t));
1203a6d42e7dSPeter Dunlap 	if (!ptr) {
1204a6d42e7dSPeter Dunlap 		return (ENOMEM);
1205a6d42e7dSPeter Dunlap 	}
1206a6d42e7dSPeter Dunlap 
1207a6d42e7dSPeter Dunlap 	(void) memcpy(&(ptr->portal_addr), &sa,
1208a6d42e7dSPeter Dunlap 	    sizeof (struct sockaddr_storage));
1209a6d42e7dSPeter Dunlap 	ptr->next = tpg->tpg_portal_list;
1210a6d42e7dSPeter Dunlap 	tpg->tpg_portal_list = ptr;
1211a6d42e7dSPeter Dunlap 	tpg->tpg_portal_count++;
1212a6d42e7dSPeter Dunlap 	tpg->tpg_generation++;
1213a6d42e7dSPeter Dunlap 
1214a6d42e7dSPeter Dunlap 	return (0);
1215a6d42e7dSPeter Dunlap }
1216a6d42e7dSPeter Dunlap 
1217a6d42e7dSPeter Dunlap /*
1218a6d42e7dSPeter Dunlap  * Function:  it_portal_delete()
1219a6d42e7dSPeter Dunlap  *
1220a6d42e7dSPeter Dunlap  * Remove the specified portal from the specified target portal group.
1221a6d42e7dSPeter Dunlap  * The portal removal will not take effect until the modified configuration
1222a6d42e7dSPeter Dunlap  * is committed by calling it_config_commit().
1223a6d42e7dSPeter Dunlap  *
1224a6d42e7dSPeter Dunlap  * Parameters:
1225a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configration obtained from
1226a6d42e7dSPeter Dunlap  *			it_config_load()
1227a6d42e7dSPeter Dunlap  *    tpg		Pointer to the it_tpg_t structure representing the
1228a6d42e7dSPeter Dunlap  *			target portal group
1229a6d42e7dSPeter Dunlap  *    portal		Pointer to the it_portal_t structure representing
1230a6d42e7dSPeter Dunlap  *			the portal
1231a6d42e7dSPeter Dunlap  */
1232a6d42e7dSPeter Dunlap void
1233a6d42e7dSPeter Dunlap it_portal_delete(it_config_t *cfg, it_tpg_t *tpg, it_portal_t *portal)
1234a6d42e7dSPeter Dunlap {
1235a6d42e7dSPeter Dunlap 	it_portal_t	*ptr;
1236a6d42e7dSPeter Dunlap 	it_portal_t	*prev;
1237a6d42e7dSPeter Dunlap 
1238a6d42e7dSPeter Dunlap 	if (!cfg || !tpg || !portal) {
1239a6d42e7dSPeter Dunlap 		return;
1240a6d42e7dSPeter Dunlap 	}
1241a6d42e7dSPeter Dunlap 
1242a6d42e7dSPeter Dunlap 	ptr = tpg->tpg_portal_list;
1243a6d42e7dSPeter Dunlap 	while (ptr) {
1244a6d42e7dSPeter Dunlap 		if (memcmp(&(ptr->portal_addr), &(portal->portal_addr),
1245a6d42e7dSPeter Dunlap 		    sizeof (ptr->portal_addr)) == 0) {
1246a6d42e7dSPeter Dunlap 			break;
1247a6d42e7dSPeter Dunlap 		}
1248a6d42e7dSPeter Dunlap 		prev = ptr;
1249a6d42e7dSPeter Dunlap 		ptr = ptr->next;
1250a6d42e7dSPeter Dunlap 	}
1251a6d42e7dSPeter Dunlap 
1252a6d42e7dSPeter Dunlap 	if (!ptr) {
1253a6d42e7dSPeter Dunlap 		return;
1254a6d42e7dSPeter Dunlap 	}
1255a6d42e7dSPeter Dunlap 
1256a6d42e7dSPeter Dunlap 	if (prev) {
1257a6d42e7dSPeter Dunlap 		prev->next = ptr->next;
1258a6d42e7dSPeter Dunlap 	} else {
1259a6d42e7dSPeter Dunlap 		tpg->tpg_portal_list = ptr->next;
1260a6d42e7dSPeter Dunlap 	}
1261a6d42e7dSPeter Dunlap 	tpg->tpg_portal_count--;
1262a6d42e7dSPeter Dunlap 	tpg->tpg_generation++;
1263a6d42e7dSPeter Dunlap 
1264a6d42e7dSPeter Dunlap 	free(ptr);
1265a6d42e7dSPeter Dunlap }
1266a6d42e7dSPeter Dunlap 
1267a6d42e7dSPeter Dunlap /*
1268a6d42e7dSPeter Dunlap  * Function:  it_ini_create()
1269a6d42e7dSPeter Dunlap  *
1270a6d42e7dSPeter Dunlap  * Add an initiator context to the global configuration. The new
1271a6d42e7dSPeter Dunlap  * initiator context will not be instantiated until the modified
1272a6d42e7dSPeter Dunlap  * configuration is committed by calling it_config_commit().
1273a6d42e7dSPeter Dunlap  *
1274a6d42e7dSPeter Dunlap  * Parameters:
1275a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configration obtained from
1276a6d42e7dSPeter Dunlap  *			it_config_load()
1277a6d42e7dSPeter Dunlap  *    ini		Pointer to the it_ini_t structure representing
1278a6d42e7dSPeter Dunlap  *			the initiator context.
1279a6d42e7dSPeter Dunlap  *    ini_node_name	The iSCSI node name of the remote initiator.
1280a6d42e7dSPeter Dunlap  *
1281a6d42e7dSPeter Dunlap  * Return Values:
1282a6d42e7dSPeter Dunlap  *    0			Success
1283a6d42e7dSPeter Dunlap  *    ENOMEM		Could not allocate resources
1284a6d42e7dSPeter Dunlap  *    EINVAL		Invalid parameter.
1285a6d42e7dSPeter Dunlap  *    EFAULT		Invalid initiator name
1286a6d42e7dSPeter Dunlap  */
1287a6d42e7dSPeter Dunlap int
1288a6d42e7dSPeter Dunlap it_ini_create(it_config_t *cfg, it_ini_t **ini, char *ini_node_name)
1289a6d42e7dSPeter Dunlap {
1290a6d42e7dSPeter Dunlap 	it_ini_t	*ptr;
1291a6d42e7dSPeter Dunlap 
1292a6d42e7dSPeter Dunlap 	if (!cfg || !ini || !ini_node_name) {
1293a6d42e7dSPeter Dunlap 		return (EINVAL);
1294a6d42e7dSPeter Dunlap 	}
1295a6d42e7dSPeter Dunlap 
1296a6d42e7dSPeter Dunlap 	/*
1297a6d42e7dSPeter Dunlap 	 * Ensure this is a valid ini name
1298a6d42e7dSPeter Dunlap 	 */
1299a6d42e7dSPeter Dunlap 	if (!validate_iscsi_name(ini_node_name)) {
1300a6d42e7dSPeter Dunlap 		return (EFAULT);
1301a6d42e7dSPeter Dunlap 	}
1302a6d42e7dSPeter Dunlap 
1303a6d42e7dSPeter Dunlap 	ptr = cfg->config_ini_list;
1304a6d42e7dSPeter Dunlap 	while (ptr) {
1305a6d42e7dSPeter Dunlap 		if (strcmp(ptr->ini_name, ini_node_name) == 0) {
1306a6d42e7dSPeter Dunlap 			break;
1307a6d42e7dSPeter Dunlap 		}
1308a6d42e7dSPeter Dunlap 		ptr = ptr->ini_next;
1309a6d42e7dSPeter Dunlap 	}
1310a6d42e7dSPeter Dunlap 
1311a6d42e7dSPeter Dunlap 	if (ptr) {
1312a6d42e7dSPeter Dunlap 		return (EEXIST);
1313a6d42e7dSPeter Dunlap 	}
1314a6d42e7dSPeter Dunlap 
1315a6d42e7dSPeter Dunlap 	ptr = calloc(1, sizeof (it_ini_t));
1316a6d42e7dSPeter Dunlap 	if (!ptr) {
1317a6d42e7dSPeter Dunlap 		return (ENOMEM);
1318a6d42e7dSPeter Dunlap 	}
1319a6d42e7dSPeter Dunlap 
1320a6d42e7dSPeter Dunlap 	(void) strlcpy(ptr->ini_name, ini_node_name, sizeof (ptr->ini_name));
1321a6d42e7dSPeter Dunlap 	ptr->ini_generation = 1;
1322a6d42e7dSPeter Dunlap 	/* nvlist for props? */
1323a6d42e7dSPeter Dunlap 
1324a6d42e7dSPeter Dunlap 	ptr->ini_next = cfg->config_ini_list;
1325a6d42e7dSPeter Dunlap 	cfg->config_ini_list = ptr;
1326a6d42e7dSPeter Dunlap 	cfg->config_ini_count++;
1327a6d42e7dSPeter Dunlap 
1328a6d42e7dSPeter Dunlap 	*ini = ptr;
1329a6d42e7dSPeter Dunlap 
1330a6d42e7dSPeter Dunlap 	return (0);
1331a6d42e7dSPeter Dunlap }
1332a6d42e7dSPeter Dunlap 
1333a6d42e7dSPeter Dunlap /*
1334a6d42e7dSPeter Dunlap  * Function:  it_ini_setprop()
1335a6d42e7dSPeter Dunlap  *
1336a6d42e7dSPeter Dunlap  * Validate the provided property list and set the initiator properties.
1337a6d42e7dSPeter Dunlap  * If errlist is not NULL, returns detailed errors for each property
1338a6d42e7dSPeter Dunlap  * that failed.  The format for errorlist is key = property,
1339a6d42e7dSPeter Dunlap  * value = error string.
1340a6d42e7dSPeter Dunlap  *
1341a6d42e7dSPeter Dunlap  * Parameters:
1342a6d42e7dSPeter Dunlap  *
1343a6d42e7dSPeter Dunlap  *    ini		The initiator being updated.
1344a6d42e7dSPeter Dunlap  *    proplist		nvlist_t containing properties for this target.
1345a6d42e7dSPeter Dunlap  *    errlist		(optional)  nvlist_t of errors encountered when
1346a6d42e7dSPeter Dunlap  *			validating the properties.
1347a6d42e7dSPeter Dunlap  *
1348a6d42e7dSPeter Dunlap  * Return Values:
1349a6d42e7dSPeter Dunlap  *    0			Success
1350a6d42e7dSPeter Dunlap  *    EINVAL		Invalid property
1351a6d42e7dSPeter Dunlap  *
1352a6d42e7dSPeter Dunlap  */
1353a6d42e7dSPeter Dunlap int
1354a6d42e7dSPeter Dunlap it_ini_setprop(it_ini_t *ini, nvlist_t *proplist, nvlist_t **errlist)
1355a6d42e7dSPeter Dunlap {
1356a6d42e7dSPeter Dunlap 	int		ret;
1357a6d42e7dSPeter Dunlap 	nvlist_t	*iprops = NULL;
1358a6d42e7dSPeter Dunlap 	char		*val = NULL;
1359a6d42e7dSPeter Dunlap 
1360a6d42e7dSPeter Dunlap 	if (!ini || !proplist) {
1361a6d42e7dSPeter Dunlap 		return (EINVAL);
1362a6d42e7dSPeter Dunlap 	}
1363a6d42e7dSPeter Dunlap 
1364a6d42e7dSPeter Dunlap 	if (errlist) {
1365a6d42e7dSPeter Dunlap 		(void) nvlist_alloc(errlist, 0, 0);
1366a6d42e7dSPeter Dunlap 	}
1367a6d42e7dSPeter Dunlap 
1368a6d42e7dSPeter Dunlap 	/*
1369a6d42e7dSPeter Dunlap 	 * copy the existing properties, merge, then validate
1370a6d42e7dSPeter Dunlap 	 * the merged properties before committing them.
1371a6d42e7dSPeter Dunlap 	 */
1372a6d42e7dSPeter Dunlap 	if (ini->ini_properties) {
1373a6d42e7dSPeter Dunlap 		ret = nvlist_dup(ini->ini_properties, &iprops, 0);
1374a6d42e7dSPeter Dunlap 	} else {
1375a6d42e7dSPeter Dunlap 		ret = nvlist_alloc(&iprops, NV_UNIQUE_NAME, 0);
1376a6d42e7dSPeter Dunlap 	}
1377a6d42e7dSPeter Dunlap 
1378a6d42e7dSPeter Dunlap 	if (ret == 0) {
1379a6d42e7dSPeter Dunlap 		ret = nvlist_merge(iprops, proplist, 0);
1380a6d42e7dSPeter Dunlap 	}
1381a6d42e7dSPeter Dunlap 
1382a6d42e7dSPeter Dunlap 	/* unset chap username if requested */
1383a6d42e7dSPeter Dunlap 	if ((nvlist_lookup_string(proplist, PROP_CHAP_USER, &val)) == 0) {
1384a6d42e7dSPeter Dunlap 		if (strcasecmp(val, "none") == 0) {
1385a6d42e7dSPeter Dunlap 			(void) nvlist_remove_all(iprops, PROP_CHAP_USER);
1386a6d42e7dSPeter Dunlap 		}
1387a6d42e7dSPeter Dunlap 	}
1388a6d42e7dSPeter Dunlap 
1389a6d42e7dSPeter Dunlap 	/* base64 encode the CHAP secret, if it's changed */
1390a6d42e7dSPeter Dunlap 	if ((nvlist_lookup_string(proplist, PROP_CHAP_SECRET, &val)) == 0) {
1391a6d42e7dSPeter Dunlap 		char		bsecret[MAX_BASE64_LEN];
1392a6d42e7dSPeter Dunlap 
1393a6d42e7dSPeter Dunlap 		ret = it_val_pass(PROP_CHAP_SECRET, val, *errlist);
1394a6d42e7dSPeter Dunlap 		if (ret == 0) {
1395a6d42e7dSPeter Dunlap 			(void) memset(bsecret, 0, MAX_BASE64_LEN);
1396a6d42e7dSPeter Dunlap 
1397a6d42e7dSPeter Dunlap 			ret = iscsi_binary_to_base64_str((uint8_t *)val,
1398a6d42e7dSPeter Dunlap 			    strlen(val), bsecret, MAX_BASE64_LEN);
1399a6d42e7dSPeter Dunlap 
1400a6d42e7dSPeter Dunlap 			if (ret == 0) {
1401a6d42e7dSPeter Dunlap 				/* replace the value in the nvlist */
1402a6d42e7dSPeter Dunlap 				ret = nvlist_add_string(iprops,
1403a6d42e7dSPeter Dunlap 				    PROP_CHAP_SECRET, bsecret);
1404a6d42e7dSPeter Dunlap 			}
1405a6d42e7dSPeter Dunlap 		}
1406a6d42e7dSPeter Dunlap 	}
1407a6d42e7dSPeter Dunlap 
1408a6d42e7dSPeter Dunlap 	if (ret == 0) {
1409a6d42e7dSPeter Dunlap 		ret = it_validate_iniprops(iprops, *errlist);
1410a6d42e7dSPeter Dunlap 	}
1411a6d42e7dSPeter Dunlap 
1412a6d42e7dSPeter Dunlap 	if (ret != 0) {
1413a6d42e7dSPeter Dunlap 		if (iprops) {
1414a6d42e7dSPeter Dunlap 			nvlist_free(iprops);
1415a6d42e7dSPeter Dunlap 		}
1416a6d42e7dSPeter Dunlap 		return (ret);
1417a6d42e7dSPeter Dunlap 	}
1418a6d42e7dSPeter Dunlap 
1419a6d42e7dSPeter Dunlap 	if (ini->ini_properties) {
1420a6d42e7dSPeter Dunlap 		nvlist_free(ini->ini_properties);
1421a6d42e7dSPeter Dunlap 	}
1422a6d42e7dSPeter Dunlap 	ini->ini_properties = iprops;
1423a6d42e7dSPeter Dunlap 
1424a6d42e7dSPeter Dunlap 	return (0);
1425a6d42e7dSPeter Dunlap }
1426a6d42e7dSPeter Dunlap 
1427a6d42e7dSPeter Dunlap /*
1428a6d42e7dSPeter Dunlap  * Function:  it_ini_delete()
1429a6d42e7dSPeter Dunlap  *
1430a6d42e7dSPeter Dunlap  * Remove the specified initiator context from the global configuration.
1431a6d42e7dSPeter Dunlap  * The removal will not take effect until the modified configuration is
1432a6d42e7dSPeter Dunlap  * committed by calling it_config_commit().
1433a6d42e7dSPeter Dunlap  *
1434a6d42e7dSPeter Dunlap  * Parameters:
1435a6d42e7dSPeter Dunlap  *    cfg		The current iSCSI configration obtained from
1436a6d42e7dSPeter Dunlap  *			it_config_load()
1437a6d42e7dSPeter Dunlap  *    ini		Pointer to the it_ini_t structure representing
1438a6d42e7dSPeter Dunlap  *			the initiator context.
1439a6d42e7dSPeter Dunlap  */
1440a6d42e7dSPeter Dunlap void
1441a6d42e7dSPeter Dunlap it_ini_delete(it_config_t *cfg, it_ini_t *ini)
1442a6d42e7dSPeter Dunlap {
1443a6d42e7dSPeter Dunlap 	it_ini_t	*ptr;
1444a6d42e7dSPeter Dunlap 	it_ini_t	*prev = NULL;
1445a6d42e7dSPeter Dunlap 
1446a6d42e7dSPeter Dunlap 	if (!cfg || !ini) {
1447a6d42e7dSPeter Dunlap 		return;
1448a6d42e7dSPeter Dunlap 	}
1449a6d42e7dSPeter Dunlap 
1450a6d42e7dSPeter Dunlap 	ptr = cfg->config_ini_list;
1451a6d42e7dSPeter Dunlap 	while (ptr) {
1452a6d42e7dSPeter Dunlap 		if (strcmp(ptr->ini_name, ini->ini_name) == 0) {
1453a6d42e7dSPeter Dunlap 			break;
1454a6d42e7dSPeter Dunlap 		}
1455a6d42e7dSPeter Dunlap 		prev = ptr;
1456a6d42e7dSPeter Dunlap 		ptr = ptr->ini_next;
1457a6d42e7dSPeter Dunlap 	}
1458a6d42e7dSPeter Dunlap 
1459a6d42e7dSPeter Dunlap 	if (!ptr) {
1460a6d42e7dSPeter Dunlap 		return;
1461a6d42e7dSPeter Dunlap 	}
1462a6d42e7dSPeter Dunlap 
1463a6d42e7dSPeter Dunlap 	if (prev) {
1464a6d42e7dSPeter Dunlap 		prev->ini_next = ptr->ini_next;
1465a6d42e7dSPeter Dunlap 	} else {
1466a6d42e7dSPeter Dunlap 		cfg->config_ini_list = ptr->ini_next;
1467a6d42e7dSPeter Dunlap 	}
1468a6d42e7dSPeter Dunlap 
1469a6d42e7dSPeter Dunlap 	ptr->ini_next = NULL; /* Only free this initiator */
1470a6d42e7dSPeter Dunlap 
1471a6d42e7dSPeter Dunlap 	cfg->config_ini_count--;
1472a6d42e7dSPeter Dunlap 
1473a6d42e7dSPeter Dunlap 	it_ini_free(ptr);
1474a6d42e7dSPeter Dunlap }
1475a6d42e7dSPeter Dunlap 
1476a6d42e7dSPeter Dunlap /*
1477a6d42e7dSPeter Dunlap  * Function:  it_ini_free()
1478a6d42e7dSPeter Dunlap  *
1479a6d42e7dSPeter Dunlap  * Deallocates resources of an it_ini_t structure. If ini->next is
1480a6d42e7dSPeter Dunlap  * not NULL, frees all members of the list.
1481a6d42e7dSPeter Dunlap  */
1482a6d42e7dSPeter Dunlap void
1483a6d42e7dSPeter Dunlap it_ini_free(it_ini_t *ini)
1484a6d42e7dSPeter Dunlap {
1485a6d42e7dSPeter Dunlap 	it_ini_free_cmn(ini);
1486a6d42e7dSPeter Dunlap }
1487a6d42e7dSPeter Dunlap 
1488a6d42e7dSPeter Dunlap /*
1489a6d42e7dSPeter Dunlap  * Goes through the target property list and validates
1490a6d42e7dSPeter Dunlap  * each entry.  If errs is non-NULL, will return explicit errors
1491a6d42e7dSPeter Dunlap  * for each property that fails validation.
1492a6d42e7dSPeter Dunlap  */
1493a6d42e7dSPeter Dunlap static int
1494a6d42e7dSPeter Dunlap it_validate_tgtprops(nvlist_t *nvl, nvlist_t *errs)
1495a6d42e7dSPeter Dunlap {
1496a6d42e7dSPeter Dunlap 	int		errcnt = 0;
1497a6d42e7dSPeter Dunlap 	nvpair_t	*nvp = NULL;
1498a6d42e7dSPeter Dunlap 	data_type_t	nvtype;
1499a6d42e7dSPeter Dunlap 	char		*name;
1500a6d42e7dSPeter Dunlap 	char		*val;
1501a6d42e7dSPeter Dunlap 	char		*auth = NULL;
1502a6d42e7dSPeter Dunlap 
1503a6d42e7dSPeter Dunlap 	if (!nvl) {
1504a6d42e7dSPeter Dunlap 		return (0);
1505a6d42e7dSPeter Dunlap 	}
1506a6d42e7dSPeter Dunlap 
1507a6d42e7dSPeter Dunlap 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
1508a6d42e7dSPeter Dunlap 		name = nvpair_name(nvp);
1509a6d42e7dSPeter Dunlap 		nvtype = nvpair_type(nvp);
1510a6d42e7dSPeter Dunlap 
1511a6d42e7dSPeter Dunlap 		if (!name) {
1512a6d42e7dSPeter Dunlap 			continue;
1513a6d42e7dSPeter Dunlap 		}
1514a6d42e7dSPeter Dunlap 
1515a6d42e7dSPeter Dunlap 		val = NULL;
1516a6d42e7dSPeter Dunlap 		if (strcmp(name, PROP_TARGET_CHAP_USER) == 0) {
1517a6d42e7dSPeter Dunlap 			if (nvtype != DATA_TYPE_STRING) {
1518a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1519a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1520a6d42e7dSPeter Dunlap 				errcnt++;
1521a6d42e7dSPeter Dunlap 				continue;
1522a6d42e7dSPeter Dunlap 			}
1523a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_TARGET_CHAP_SECRET) == 0) {
1524a6d42e7dSPeter Dunlap 			/*
1525a6d42e7dSPeter Dunlap 			 * must be between 12 and 255 chars in cleartext.
1526a6d42e7dSPeter Dunlap 			 * will be base64 encoded when it's set.
1527a6d42e7dSPeter Dunlap 			 */
1528a6d42e7dSPeter Dunlap 			if (nvtype == DATA_TYPE_STRING) {
1529a6d42e7dSPeter Dunlap 				(void) nvpair_value_string(nvp, &val);
1530a6d42e7dSPeter Dunlap 			}
1531a6d42e7dSPeter Dunlap 
1532a6d42e7dSPeter Dunlap 			if (!val) {
1533a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1534a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1535a6d42e7dSPeter Dunlap 				errcnt++;
1536a6d42e7dSPeter Dunlap 				continue;
1537a6d42e7dSPeter Dunlap 			}
1538a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_ALIAS) == 0) {
1539a6d42e7dSPeter Dunlap 			if (nvtype != DATA_TYPE_STRING) {
1540a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1541a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1542a6d42e7dSPeter Dunlap 				errcnt++;
1543a6d42e7dSPeter Dunlap 				continue;
1544a6d42e7dSPeter Dunlap 			}
1545a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_AUTH) == 0) {
1546a6d42e7dSPeter Dunlap 			if (nvtype == DATA_TYPE_STRING) {
1547a6d42e7dSPeter Dunlap 				val = NULL;
1548a6d42e7dSPeter Dunlap 				(void) nvpair_value_string(nvp, &val);
1549a6d42e7dSPeter Dunlap 			}
1550a6d42e7dSPeter Dunlap 
1551a6d42e7dSPeter Dunlap 			if (!val) {
1552a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1553a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1554a6d42e7dSPeter Dunlap 				errcnt++;
1555a6d42e7dSPeter Dunlap 				continue;
1556a6d42e7dSPeter Dunlap 			}
1557a6d42e7dSPeter Dunlap 			if ((strcmp(val, PA_AUTH_NONE) != 0) &&
1558a6d42e7dSPeter Dunlap 			    (strcmp(val, PA_AUTH_CHAP) != 0) &&
1559a6d42e7dSPeter Dunlap 			    (strcmp(val, PA_AUTH_RADIUS) != 0) &&
1560a6d42e7dSPeter Dunlap 			    (strcmp(val, "default") != 0)) {
1561a6d42e7dSPeter Dunlap 				PROPERR(errs, val, gettext(
1562a6d42e7dSPeter Dunlap 				    "must be none, chap, radius or default"));
1563a6d42e7dSPeter Dunlap 				errcnt++;
1564a6d42e7dSPeter Dunlap 			}
1565a6d42e7dSPeter Dunlap 			auth = val;
1566a6d42e7dSPeter Dunlap 			continue;
1567a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_OLD_TARGET_NAME) == 0) {
1568a6d42e7dSPeter Dunlap 			continue;
1569a6d42e7dSPeter Dunlap 		} else {
1570a6d42e7dSPeter Dunlap 			/* unrecognized property */
1571a6d42e7dSPeter Dunlap 			PROPERR(errs, name, gettext("unrecognized property"));
1572a6d42e7dSPeter Dunlap 			errcnt++;
1573a6d42e7dSPeter Dunlap 		}
1574a6d42e7dSPeter Dunlap 	}
1575a6d42e7dSPeter Dunlap 
1576a6d42e7dSPeter Dunlap 	if (errcnt) {
1577a6d42e7dSPeter Dunlap 		return (EINVAL);
1578a6d42e7dSPeter Dunlap 	}
1579a6d42e7dSPeter Dunlap 
1580a6d42e7dSPeter Dunlap 	/* if auth is being set to default, remove from this nvlist */
1581a6d42e7dSPeter Dunlap 	if (auth && (strcmp(auth, "default") == 0)) {
1582a6d42e7dSPeter Dunlap 		(void) nvlist_remove_all(nvl, PROP_AUTH);
1583a6d42e7dSPeter Dunlap 	}
1584a6d42e7dSPeter Dunlap 
1585a6d42e7dSPeter Dunlap 	return (0);
1586a6d42e7dSPeter Dunlap }
1587a6d42e7dSPeter Dunlap 
1588a6d42e7dSPeter Dunlap /*
1589a6d42e7dSPeter Dunlap  * Goes through the config property list and validates
1590a6d42e7dSPeter Dunlap  * each entry.  If errs is non-NULL, will return explicit errors
1591a6d42e7dSPeter Dunlap  * for each property that fails validation.
1592a6d42e7dSPeter Dunlap  */
1593a6d42e7dSPeter Dunlap static int
1594a6d42e7dSPeter Dunlap it_validate_configprops(nvlist_t *nvl, nvlist_t *errs)
1595a6d42e7dSPeter Dunlap {
1596a6d42e7dSPeter Dunlap 	int				errcnt = 0;
1597a6d42e7dSPeter Dunlap 	nvpair_t			*nvp = NULL;
1598a6d42e7dSPeter Dunlap 	data_type_t			nvtype;
1599a6d42e7dSPeter Dunlap 	char				*name;
1600a6d42e7dSPeter Dunlap 	char				*val;
1601a6d42e7dSPeter Dunlap 	struct sockaddr_storage		sa;
1602bf604c64SPeter Dunlap 	boolean_t			update_rad_server = B_FALSE;
1603bf604c64SPeter Dunlap 	char				*rad_server;
1604a6d42e7dSPeter Dunlap 	char				*auth = NULL;
1605a6d42e7dSPeter Dunlap 
1606a6d42e7dSPeter Dunlap 	if (!nvl) {
1607a6d42e7dSPeter Dunlap 		return (0);
1608a6d42e7dSPeter Dunlap 	}
1609a6d42e7dSPeter Dunlap 
1610a6d42e7dSPeter Dunlap 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
1611a6d42e7dSPeter Dunlap 		name = nvpair_name(nvp);
1612a6d42e7dSPeter Dunlap 		nvtype = nvpair_type(nvp);
1613a6d42e7dSPeter Dunlap 
1614a6d42e7dSPeter Dunlap 		if (!name) {
1615a6d42e7dSPeter Dunlap 			continue;
1616a6d42e7dSPeter Dunlap 		}
1617a6d42e7dSPeter Dunlap 
1618a6d42e7dSPeter Dunlap 		val = NULL;
1619a6d42e7dSPeter Dunlap 
1620a6d42e7dSPeter Dunlap 		/* prefetch string value as we mostly need it */
1621a6d42e7dSPeter Dunlap 		if (nvtype == DATA_TYPE_STRING) {
1622a6d42e7dSPeter Dunlap 			(void) nvpair_value_string(nvp, &val);
1623a6d42e7dSPeter Dunlap 		}
1624a6d42e7dSPeter Dunlap 
1625a6d42e7dSPeter Dunlap 		if (strcmp(name, PROP_ALIAS) == 0) {
1626a6d42e7dSPeter Dunlap 			if (!val) {
1627a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1628a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1629a6d42e7dSPeter Dunlap 				errcnt++;
1630a6d42e7dSPeter Dunlap 			}
1631a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_AUTH) == 0) {
1632a6d42e7dSPeter Dunlap 			if (!val) {
1633a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1634a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1635a6d42e7dSPeter Dunlap 				errcnt++;
1636a6d42e7dSPeter Dunlap 				continue;
1637a6d42e7dSPeter Dunlap 			}
1638a6d42e7dSPeter Dunlap 
1639a6d42e7dSPeter Dunlap 			if ((strcmp(val, PA_AUTH_NONE) != 0) &&
1640a6d42e7dSPeter Dunlap 			    (strcmp(val, PA_AUTH_CHAP) != 0) &&
1641a6d42e7dSPeter Dunlap 			    (strcmp(val, PA_AUTH_RADIUS) != 0)) {
1642a6d42e7dSPeter Dunlap 				PROPERR(errs, PROP_AUTH,
1643a6d42e7dSPeter Dunlap 				    gettext("must be none, chap or radius"));
1644a6d42e7dSPeter Dunlap 				errcnt++;
1645a6d42e7dSPeter Dunlap 			}
1646a6d42e7dSPeter Dunlap 
1647a6d42e7dSPeter Dunlap 			auth = val;
1648a6d42e7dSPeter Dunlap 
1649a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_ISNS_ENABLED) == 0) {
1650a6d42e7dSPeter Dunlap 			if (nvtype != DATA_TYPE_BOOLEAN_VALUE) {
1651a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1652a6d42e7dSPeter Dunlap 				    gettext("must be a boolean value"));
1653a6d42e7dSPeter Dunlap 				errcnt++;
1654a6d42e7dSPeter Dunlap 			}
1655a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_ISNS_SERVER) == 0) {
1656a6d42e7dSPeter Dunlap 			char		**arr = NULL;
1657a6d42e7dSPeter Dunlap 			uint32_t	acount = 0;
1658a6d42e7dSPeter Dunlap 
1659a6d42e7dSPeter Dunlap 			(void) nvlist_lookup_string_array(nvl, name,
1660a6d42e7dSPeter Dunlap 			    &arr, &acount);
1661a6d42e7dSPeter Dunlap 
1662a6d42e7dSPeter Dunlap 			while (acount > 0) {
1663a6d42e7dSPeter Dunlap 				if (strcasecmp(arr[acount - 1], "none") == 0) {
1664a6d42e7dSPeter Dunlap 					break;
1665a6d42e7dSPeter Dunlap 				}
1666a6d42e7dSPeter Dunlap 				if ((it_common_convert_sa(arr[acount - 1],
1667a6d42e7dSPeter Dunlap 				    &sa, 0)) == NULL) {
1668a6d42e7dSPeter Dunlap 					PROPERR(errs, arr[acount - 1],
1669a6d42e7dSPeter Dunlap 					    gettext("invalid address"));
1670a6d42e7dSPeter Dunlap 					errcnt++;
1671a6d42e7dSPeter Dunlap 				}
1672a6d42e7dSPeter Dunlap 				acount--;
1673a6d42e7dSPeter Dunlap 			}
1674a6d42e7dSPeter Dunlap 
1675a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_RADIUS_SECRET) == 0) {
1676a6d42e7dSPeter Dunlap 			if (!val) {
1677a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1678a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1679a6d42e7dSPeter Dunlap 				errcnt++;
1680a6d42e7dSPeter Dunlap 				continue;
1681a6d42e7dSPeter Dunlap 			}
1682a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_RADIUS_SERVER) == 0) {
1683a6d42e7dSPeter Dunlap 			struct sockaddr_storage		sa;
1684a6d42e7dSPeter Dunlap 			if (!val) {
1685a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1686a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1687a6d42e7dSPeter Dunlap 				errcnt++;
1688a6d42e7dSPeter Dunlap 				continue;
1689a6d42e7dSPeter Dunlap 			}
1690a6d42e7dSPeter Dunlap 
1691a6d42e7dSPeter Dunlap 			if ((it_common_convert_sa(val, &sa,
1692a6d42e7dSPeter Dunlap 			    DEFAULT_RADIUS_PORT)) == NULL) {
1693a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1694a6d42e7dSPeter Dunlap 				    gettext("invalid address"));
1695a6d42e7dSPeter Dunlap 				errcnt++;
1696a6d42e7dSPeter Dunlap 			} else {
1697a6d42e7dSPeter Dunlap 				/*
1698a6d42e7dSPeter Dunlap 				 * rewrite this property to ensure port
1699a6d42e7dSPeter Dunlap 				 * number is added.
1700a6d42e7dSPeter Dunlap 				 */
1701a6d42e7dSPeter Dunlap 
1702bf604c64SPeter Dunlap 				if (sockaddr_to_str(&sa, &rad_server) == 0) {
1703bf604c64SPeter Dunlap 					update_rad_server = B_TRUE;
1704a6d42e7dSPeter Dunlap 				}
1705a6d42e7dSPeter Dunlap 			}
1706a6d42e7dSPeter Dunlap 		} else {
1707a6d42e7dSPeter Dunlap 			/* unrecognized property */
1708a6d42e7dSPeter Dunlap 			PROPERR(errs, name, gettext("unrecognized property"));
1709a6d42e7dSPeter Dunlap 			errcnt++;
1710a6d42e7dSPeter Dunlap 		}
1711a6d42e7dSPeter Dunlap 	}
1712a6d42e7dSPeter Dunlap 
1713a6d42e7dSPeter Dunlap 	/*
1714bf604c64SPeter Dunlap 	 * If we successfully reformatted the radius server to add the port
1715bf604c64SPeter Dunlap 	 * number then update the nvlist
1716bf604c64SPeter Dunlap 	 */
1717bf604c64SPeter Dunlap 	if (update_rad_server) {
17186ced70a9SPriya Krishnan 		(void) nvlist_add_string(nvl, PROP_RADIUS_SERVER, rad_server);
1719bf604c64SPeter Dunlap 	}
1720bf604c64SPeter Dunlap 
1721bf604c64SPeter Dunlap 	/*
1722a6d42e7dSPeter Dunlap 	 * if auth = radius, ensure radius server & secret are set.
1723a6d42e7dSPeter Dunlap 	 */
1724a6d42e7dSPeter Dunlap 	if (auth) {
1725a6d42e7dSPeter Dunlap 		if (strcmp(auth, PA_AUTH_RADIUS) == 0) {
1726a6d42e7dSPeter Dunlap 			/* need server & secret for radius */
1727a6d42e7dSPeter Dunlap 			if (!nvlist_exists(nvl, PROP_RADIUS_SERVER)) {
1728a6d42e7dSPeter Dunlap 				PROPERR(errs, PROP_RADIUS_SERVER,
1729a6d42e7dSPeter Dunlap 				    gettext("missing required property"));
1730a6d42e7dSPeter Dunlap 				errcnt++;
1731a6d42e7dSPeter Dunlap 			}
1732a6d42e7dSPeter Dunlap 			if (!nvlist_exists(nvl, PROP_RADIUS_SECRET)) {
1733a6d42e7dSPeter Dunlap 				PROPERR(errs, PROP_RADIUS_SECRET,
1734a6d42e7dSPeter Dunlap 				    gettext("missing required property"));
1735a6d42e7dSPeter Dunlap 				errcnt++;
1736a6d42e7dSPeter Dunlap 			}
1737a6d42e7dSPeter Dunlap 		}
1738a6d42e7dSPeter Dunlap 	}
1739a6d42e7dSPeter Dunlap 
1740a6d42e7dSPeter Dunlap 	if (errcnt) {
1741a6d42e7dSPeter Dunlap 		return (EINVAL);
1742a6d42e7dSPeter Dunlap 	}
1743a6d42e7dSPeter Dunlap 
1744a6d42e7dSPeter Dunlap 	return (0);
1745a6d42e7dSPeter Dunlap }
1746a6d42e7dSPeter Dunlap 
1747a6d42e7dSPeter Dunlap /*
1748a6d42e7dSPeter Dunlap  * Goes through the ini property list and validates
1749a6d42e7dSPeter Dunlap  * each entry.  If errs is non-NULL, will return explicit errors
1750a6d42e7dSPeter Dunlap  * for each property that fails validation.
1751a6d42e7dSPeter Dunlap  */
1752a6d42e7dSPeter Dunlap static int
1753a6d42e7dSPeter Dunlap it_validate_iniprops(nvlist_t *nvl, nvlist_t *errs)
1754a6d42e7dSPeter Dunlap {
1755a6d42e7dSPeter Dunlap 	int				errcnt = 0;
1756a6d42e7dSPeter Dunlap 	nvpair_t			*nvp = NULL;
1757a6d42e7dSPeter Dunlap 	data_type_t			nvtype;
1758a6d42e7dSPeter Dunlap 	char				*name;
1759a6d42e7dSPeter Dunlap 	char				*val;
1760a6d42e7dSPeter Dunlap 
1761a6d42e7dSPeter Dunlap 	if (!nvl) {
1762a6d42e7dSPeter Dunlap 		return (0);
1763a6d42e7dSPeter Dunlap 	}
1764a6d42e7dSPeter Dunlap 
1765a6d42e7dSPeter Dunlap 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
1766a6d42e7dSPeter Dunlap 		name = nvpair_name(nvp);
1767a6d42e7dSPeter Dunlap 		nvtype = nvpair_type(nvp);
1768a6d42e7dSPeter Dunlap 
1769a6d42e7dSPeter Dunlap 		if (!name) {
1770a6d42e7dSPeter Dunlap 			continue;
1771a6d42e7dSPeter Dunlap 		}
1772a6d42e7dSPeter Dunlap 
1773a6d42e7dSPeter Dunlap 		if (strcmp(name, PROP_CHAP_USER) == 0) {
1774a6d42e7dSPeter Dunlap 			if (nvtype != DATA_TYPE_STRING) {
1775a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1776a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1777a6d42e7dSPeter Dunlap 				errcnt++;
1778a6d42e7dSPeter Dunlap 				continue;
1779a6d42e7dSPeter Dunlap 			}
1780a6d42e7dSPeter Dunlap 		} else if (strcmp(name, PROP_CHAP_SECRET) == 0) {
1781a6d42e7dSPeter Dunlap 			/*
1782a6d42e7dSPeter Dunlap 			 * must be between 12 and 255 chars in cleartext.
1783a6d42e7dSPeter Dunlap 			 * will be base64 encoded when it's set.
1784a6d42e7dSPeter Dunlap 			 */
1785a6d42e7dSPeter Dunlap 			if (nvtype == DATA_TYPE_STRING) {
1786a6d42e7dSPeter Dunlap 				val = NULL;
1787a6d42e7dSPeter Dunlap 				(void) nvpair_value_string(nvp, &val);
1788a6d42e7dSPeter Dunlap 			}
1789a6d42e7dSPeter Dunlap 
1790a6d42e7dSPeter Dunlap 			if (!val) {
1791a6d42e7dSPeter Dunlap 				PROPERR(errs, name,
1792a6d42e7dSPeter Dunlap 				    gettext("must be a string value"));
1793a6d42e7dSPeter Dunlap 				errcnt++;
1794a6d42e7dSPeter Dunlap 				continue;
1795a6d42e7dSPeter Dunlap 			}
1796a6d42e7dSPeter Dunlap 		} else {
1797a6d42e7dSPeter Dunlap 			/* unrecognized property */
1798a6d42e7dSPeter Dunlap 			PROPERR(errs, name, gettext("unrecognized property"));
1799a6d42e7dSPeter Dunlap 			errcnt++;
1800a6d42e7dSPeter Dunlap 		}
1801a6d42e7dSPeter Dunlap 	}
1802a6d42e7dSPeter Dunlap 
1803a6d42e7dSPeter Dunlap 	if (errcnt) {
1804a6d42e7dSPeter Dunlap 		return (EINVAL);
1805a6d42e7dSPeter Dunlap 	}
1806a6d42e7dSPeter Dunlap 
1807a6d42e7dSPeter Dunlap 	return (0);
1808a6d42e7dSPeter Dunlap }
1809a6d42e7dSPeter Dunlap 
1810a6d42e7dSPeter Dunlap static int
1811a6d42e7dSPeter Dunlap it_iqn_generate(char *iqn_buf, int iqn_buf_len, char *opt_iqn_suffix)
1812a6d42e7dSPeter Dunlap {
1813a6d42e7dSPeter Dunlap 	int		ret;
1814a6d42e7dSPeter Dunlap 	uuid_t		id;
1815a6d42e7dSPeter Dunlap 	char		id_str[UUID_PRINTABLE_STRING_LENGTH];
1816a6d42e7dSPeter Dunlap 
1817a6d42e7dSPeter Dunlap 	uuid_generate_random(id);
1818a6d42e7dSPeter Dunlap 	uuid_unparse(id, id_str);
1819a6d42e7dSPeter Dunlap 
1820a6d42e7dSPeter Dunlap 	if (opt_iqn_suffix) {
1821a6d42e7dSPeter Dunlap 		ret = snprintf(iqn_buf, iqn_buf_len, "iqn.1986-03.com.sun:"
1822a6d42e7dSPeter Dunlap 		    "%02d:%s.%s", TARGET_NAME_VERS, id_str, opt_iqn_suffix);
1823a6d42e7dSPeter Dunlap 	} else {
1824a6d42e7dSPeter Dunlap 		ret = snprintf(iqn_buf, iqn_buf_len, "iqn.1986-03.com.sun:"
1825a6d42e7dSPeter Dunlap 		    "%02d:%s", TARGET_NAME_VERS, id_str);
1826a6d42e7dSPeter Dunlap 	}
1827a6d42e7dSPeter Dunlap 
1828a6d42e7dSPeter Dunlap 	if (ret > iqn_buf_len) {
1829a6d42e7dSPeter Dunlap 		return (1);
1830a6d42e7dSPeter Dunlap 	}
1831a6d42e7dSPeter Dunlap 
1832a6d42e7dSPeter Dunlap 	return (0);
1833a6d42e7dSPeter Dunlap }
1834a6d42e7dSPeter Dunlap 
1835a6d42e7dSPeter Dunlap static int
1836a6d42e7dSPeter Dunlap it_val_pass(char *name, char *val, nvlist_t *e)
1837a6d42e7dSPeter Dunlap {
1838a6d42e7dSPeter Dunlap 	size_t		sz;
1839a6d42e7dSPeter Dunlap 
1840a6d42e7dSPeter Dunlap 	if (!name || !val) {
1841a6d42e7dSPeter Dunlap 		return (EINVAL);
1842a6d42e7dSPeter Dunlap 	}
1843a6d42e7dSPeter Dunlap 
1844a6d42e7dSPeter Dunlap 	/*
1845a6d42e7dSPeter Dunlap 	 * must be at least 12 chars and less than 256 chars cleartext.
1846a6d42e7dSPeter Dunlap 	 */
1847a6d42e7dSPeter Dunlap 	sz = strlen(val);
1848a6d42e7dSPeter Dunlap 
1849a6d42e7dSPeter Dunlap 	/*
1850a6d42e7dSPeter Dunlap 	 * Since we will be automatically encoding secrets we don't really
1851a6d42e7dSPeter Dunlap 	 * need the prefix anymore.
1852a6d42e7dSPeter Dunlap 	 */
1853a6d42e7dSPeter Dunlap 	if (sz < 12) {
1854a6d42e7dSPeter Dunlap 		PROPERR(e, name, gettext("secret too short"));
1855a6d42e7dSPeter Dunlap 	} else if (sz > 255) {
1856a6d42e7dSPeter Dunlap 		PROPERR(e, name, gettext("secret too long"));
1857a6d42e7dSPeter Dunlap 	} else {
1858a6d42e7dSPeter Dunlap 		/* all is well */
1859a6d42e7dSPeter Dunlap 		return (0);
1860a6d42e7dSPeter Dunlap 	}
1861a6d42e7dSPeter Dunlap 
1862a6d42e7dSPeter Dunlap 	return (1);
1863a6d42e7dSPeter Dunlap }
1864a6d42e7dSPeter Dunlap 
1865a6d42e7dSPeter Dunlap /*
1866a6d42e7dSPeter Dunlap  * Function:  validate_iscsi_name()
1867a6d42e7dSPeter Dunlap  *
1868a6d42e7dSPeter Dunlap  * Ensures the passed-in string is a valid IQN or EUI iSCSI name
1869a6d42e7dSPeter Dunlap  *
1870a6d42e7dSPeter Dunlap  */
1871a6d42e7dSPeter Dunlap boolean_t
1872a6d42e7dSPeter Dunlap validate_iscsi_name(char *in_name)
1873a6d42e7dSPeter Dunlap {
1874a6d42e7dSPeter Dunlap 	size_t		in_len;
1875a6d42e7dSPeter Dunlap 	int		i;
1876a6d42e7dSPeter Dunlap 	char		month[3];
1877a6d42e7dSPeter Dunlap 
1878a6d42e7dSPeter Dunlap 	if (in_name == NULL) {
1879a6d42e7dSPeter Dunlap 		return (B_FALSE);
1880a6d42e7dSPeter Dunlap 	}
1881a6d42e7dSPeter Dunlap 
1882a6d42e7dSPeter Dunlap 	in_len = strlen(in_name);
1883a6d42e7dSPeter Dunlap 	if (in_len < 12) {
1884a6d42e7dSPeter Dunlap 		return (B_FALSE);
1885a6d42e7dSPeter Dunlap 	}
1886a6d42e7dSPeter Dunlap 
1887*fcc214c3SCharles Ting 	if (IS_IQN_NAME(in_name)) {
1888a6d42e7dSPeter Dunlap 		/*
1889a6d42e7dSPeter Dunlap 		 * IQN names are iqn.yyyy-mm.<xxx>
1890a6d42e7dSPeter Dunlap 		 */
1891a6d42e7dSPeter Dunlap 		if ((!isdigit(in_name[4])) ||
1892a6d42e7dSPeter Dunlap 		    (!isdigit(in_name[5])) ||
1893a6d42e7dSPeter Dunlap 		    (!isdigit(in_name[6])) ||
1894a6d42e7dSPeter Dunlap 		    (!isdigit(in_name[7])) ||
1895a6d42e7dSPeter Dunlap 		    (in_name[8] != '-') ||
1896a6d42e7dSPeter Dunlap 		    (!isdigit(in_name[9])) ||
1897a6d42e7dSPeter Dunlap 		    (!isdigit(in_name[10])) ||
1898a6d42e7dSPeter Dunlap 		    (in_name[11] != '.')) {
1899a6d42e7dSPeter Dunlap 			return (B_FALSE);
1900a6d42e7dSPeter Dunlap 		}
1901a6d42e7dSPeter Dunlap 
1902a6d42e7dSPeter Dunlap 		(void) strncpy(month, &(in_name[9]), 2);
1903a6d42e7dSPeter Dunlap 		month[2] = '\0';
1904a6d42e7dSPeter Dunlap 
1905a6d42e7dSPeter Dunlap 		i = atoi(month);
1906a6d42e7dSPeter Dunlap 		if ((i < 0) || (i > 12)) {
1907a6d42e7dSPeter Dunlap 			return (B_FALSE);
1908a6d42e7dSPeter Dunlap 		}
1909a6d42e7dSPeter Dunlap 
1910836fc322SSam Cramer 		/*
1911836fc322SSam Cramer 		 * RFC 3722: if using only ASCII chars, only the following
1912836fc322SSam Cramer 		 * chars are allowed: dash, dot, colon, lower case a-z, 0-9.
1913836fc322SSam Cramer 		 * We allow upper case names, which should be folded
1914836fc322SSam Cramer 		 * to lower case names later.
1915836fc322SSam Cramer 		 */
1916836fc322SSam Cramer 		for (i = 12; i < in_len; i++) {
1917836fc322SSam Cramer 			char c = in_name[i];
1918836fc322SSam Cramer 
1919836fc322SSam Cramer 			if ((c != '-') && (c != '.') && (c != ':') &&
1920836fc322SSam Cramer 			    !isalpha(c) && !isdigit(c)) {
1921836fc322SSam Cramer 				return (B_FALSE);
1922836fc322SSam Cramer 			}
1923836fc322SSam Cramer 		}
1924836fc322SSam Cramer 
1925a6d42e7dSPeter Dunlap 		/* Finally, validate the overall length, in wide chars */
1926a6d42e7dSPeter Dunlap 		in_len = mbstowcs(NULL, in_name, 0);
1927a6d42e7dSPeter Dunlap 		if (in_len > ISCSI_NAME_LEN_MAX) {
1928a6d42e7dSPeter Dunlap 			return (B_FALSE);
1929a6d42e7dSPeter Dunlap 		}
1930*fcc214c3SCharles Ting 	} else if (IS_EUI_NAME(in_name)) {
1931a6d42e7dSPeter Dunlap 		/*
1932a6d42e7dSPeter Dunlap 		 * EUI names are "eui." + 16 hex chars
1933a6d42e7dSPeter Dunlap 		 */
1934a6d42e7dSPeter Dunlap 		if (in_len != 20) {
1935a6d42e7dSPeter Dunlap 			return (B_FALSE);
1936a6d42e7dSPeter Dunlap 		}
1937a6d42e7dSPeter Dunlap 
1938a6d42e7dSPeter Dunlap 		for (i = 4; i < in_len; i++) {
1939a6d42e7dSPeter Dunlap 			if (!isxdigit(in_name[i])) {
1940a6d42e7dSPeter Dunlap 				return (B_FALSE);
1941a6d42e7dSPeter Dunlap 			}
1942a6d42e7dSPeter Dunlap 		}
1943a6d42e7dSPeter Dunlap 	} else {
1944a6d42e7dSPeter Dunlap 		return (B_FALSE);
1945a6d42e7dSPeter Dunlap 	}
1946a6d42e7dSPeter Dunlap 
1947a6d42e7dSPeter Dunlap 	return (B_TRUE);
1948a6d42e7dSPeter Dunlap }
19495de03f84SSue Gleeson 
19505de03f84SSue Gleeson static boolean_t
19515de03f84SSue Gleeson is_iscsit_enabled(void)
19525de03f84SSue Gleeson {
19535de03f84SSue Gleeson 	char		*state;
19545de03f84SSue Gleeson 
19555de03f84SSue Gleeson 	state = smf_get_state(ISCSIT_FMRI);
19565de03f84SSue Gleeson 	if (state != NULL) {
19575de03f84SSue Gleeson 		if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
19585de03f84SSue Gleeson 			return (B_TRUE);
19595de03f84SSue Gleeson 		}
19605de03f84SSue Gleeson 	}
19615de03f84SSue Gleeson 
19625de03f84SSue Gleeson 	return (B_FALSE);
19635de03f84SSue Gleeson }
1964*fcc214c3SCharles Ting 
1965*fcc214c3SCharles Ting /*
1966*fcc214c3SCharles Ting  * Function:  canonical_iscsi_name()
1967*fcc214c3SCharles Ting  *
1968*fcc214c3SCharles Ting  * Fold the iqn iscsi name to lower-case and the EUI-64 identifier of
1969*fcc214c3SCharles Ting  * the eui iscsi name to upper-case.
1970*fcc214c3SCharles Ting  * Ensures the passed-in string is a valid IQN or EUI iSCSI name
1971*fcc214c3SCharles Ting  */
1972*fcc214c3SCharles Ting void
1973*fcc214c3SCharles Ting canonical_iscsi_name(char *tgt)
1974*fcc214c3SCharles Ting {
1975*fcc214c3SCharles Ting 	if (IS_IQN_NAME(tgt)) {
1976*fcc214c3SCharles Ting 		/* lowercase iqn names */
1977*fcc214c3SCharles Ting 		iqnstr(tgt);
1978*fcc214c3SCharles Ting 	} else {
1979*fcc214c3SCharles Ting 		/* uppercase EUI-64 identifier */
1980*fcc214c3SCharles Ting 		euistr(tgt);
1981*fcc214c3SCharles Ting 	}
1982*fcc214c3SCharles Ting }
1983*fcc214c3SCharles Ting 
1984*fcc214c3SCharles Ting /*
1985*fcc214c3SCharles Ting  * Fold an iqn name to lower-case.
1986*fcc214c3SCharles Ting  */
1987*fcc214c3SCharles Ting static void
1988*fcc214c3SCharles Ting iqnstr(char *s)
1989*fcc214c3SCharles Ting {
1990*fcc214c3SCharles Ting 	if (s != NULL) {
1991*fcc214c3SCharles Ting 		while (*s) {
1992*fcc214c3SCharles Ting 			*s = tolower(*s);
1993*fcc214c3SCharles Ting 			s++;
1994*fcc214c3SCharles Ting 		}
1995*fcc214c3SCharles Ting 	}
1996*fcc214c3SCharles Ting }
1997*fcc214c3SCharles Ting 
1998*fcc214c3SCharles Ting /*
1999*fcc214c3SCharles Ting  * Fold the EUI-64 identifier of a eui name to upper-case.
2000*fcc214c3SCharles Ting  */
2001*fcc214c3SCharles Ting static void
2002*fcc214c3SCharles Ting euistr(char *s)
2003*fcc214c3SCharles Ting {
2004*fcc214c3SCharles Ting 	if (s != NULL) {
2005*fcc214c3SCharles Ting 		char *l = s + 4;
2006*fcc214c3SCharles Ting 		while (*l) {
2007*fcc214c3SCharles Ting 			*l = toupper(*l);
2008*fcc214c3SCharles Ting 			l++;
2009*fcc214c3SCharles Ting 		}
2010*fcc214c3SCharles Ting 	}
2011*fcc214c3SCharles Ting }
2012