summaryrefslogtreecommitdiff
path: root/utils/open-isns/tags.c
blob: 7413ceea1116a524547e133d361bb7989df46376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
 * Define all iSNS tags with their types, etc.
 *
 * Copyright (C) 2007 Olaf Kirch <olaf.kirch@oracle.com>
 */

#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "isns-proto.h"
#include "vendor.h"
#include "attrs.h"
#include "security.h"
#include "objects.h"
#include "util.h"

#define ISNS_MAX_BUILTIN_TAG	4096


static void	print_bitfield(unsigned long, char **, char *, size_t);
static int	parse_bitfield( char **, const char *, uint32_t *);
static const char *help_bitfield(char **);

#define DECLARE_VALIDATOR(name) \
static int		isns_##name##_validate(const isns_value_t *, const isns_policy_t *);
#define DECLARE_ACCESSORS(name) \
static int		isns_##name##_parse(isns_value_t *, const char *buf); \
static void		isns_##name##_print(const isns_value_t *, char *buf, size_t size); \
static const char *	isns_##name##_help(void)
#define USE_VALIDATOR(name) \
	.it_validate = isns_##name##_validate
#define USE_ACCESSORS(name) \
	.it_parse = isns_##name##_parse, \
	.it_print = isns_##name##_print, \
	.it_help = isns_##name##_help

DECLARE_VALIDATOR(entity_protocol);
DECLARE_ACCESSORS(entity_protocol);
DECLARE_ACCESSORS(tcpudp_port);
DECLARE_VALIDATOR(iscsi_node_type);
DECLARE_ACCESSORS(iscsi_node_type);
DECLARE_ACCESSORS(timestamp);
DECLARE_ACCESSORS(portal_secbitmap);
DECLARE_ACCESSORS(scn_bitmap);
DECLARE_ACCESSORS(dd_features);
DECLARE_ACCESSORS(policy_object_type);
DECLARE_ACCESSORS(policy_function);

static const char *isns_authmethod_help(void);

#define TAG(ID, name, type, args...) \
[ISNS_TAG_##ID] = { \
	.it_id		= ISNS_TAG_##ID,	\
	.it_name	= name, \
	.it_type	= &isns_attr_type_##type, \
	args \
}

static isns_tag_type_t	isns_tags[ISNS_MAX_BUILTIN_TAG] = {
TAG(DELIMITER,			"Delimiter",		nil),
TAG(ENTITY_IDENTIFIER,		"Entity identifier",	string),
TAG(ENTITY_PROTOCOL,		"Entity protocol",	uint32,
				USE_VALIDATOR(entity_protocol),
				USE_ACCESSORS(entity_protocol)),
TAG(MGMT_IP_ADDRESS,		"Mgmt IP address",	ipaddr),
TAG(TIMESTAMP,			"Timestamp",		uint64,
				USE_ACCESSORS(timestamp),
				.it_readonly = 1),
TAG(PROTOCOL_VERSION_RANGE,	"Protocol version range", range16),
TAG(REGISTRATION_PERIOD,	"Registration Period",	uint32),
TAG(ENTITY_INDEX,		"Entity index",		uint32,
				.it_readonly = 1),
TAG(ENTITY_NEXT_INDEX,		"Entity next index",	uint32,
				.it_readonly = 1),
TAG(PORTAL_IP_ADDRESS,		"Portal IP address",	ipaddr),
TAG(PORTAL_TCP_UDP_PORT,	"Portal TCP/UDP port",	uint32,
				USE_ACCESSORS(tcpudp_port)),
TAG(ESI_INTERVAL,		"ESI interval",		uint32),
TAG(ESI_PORT,			"ESI port",		uint32,
				USE_ACCESSORS(tcpudp_port)),
TAG(PORTAL_SYMBOLIC_NAME,	"Portal name",		string),
TAG(PORTAL_INDEX,		"Portal index",		uint32),
TAG(SCN_PORT,			"SCN port",		uint32,
				USE_ACCESSORS(tcpudp_port)),
TAG(PORTAL_SECURITY_BITMAP,	"Portal security bitmap", uint32,
				USE_ACCESSORS(portal_secbitmap)),
TAG(PORTAL_NEXT_INDEX,		"Portal next index",	uint32,
				.it_readonly = 1),

TAG(ISCSI_NAME,			"iSCSI name",		string),
TAG(ISCSI_NODE_TYPE,		"iSCSI node type",	uint32,
				USE_VALIDATOR(iscsi_node_type),
				USE_ACCESSORS(iscsi_node_type)),
TAG(ISCSI_ALIAS,		"iSCSI alias",		string),
TAG(ISCSI_SCN_BITMAP,		"iSCSI SCN bitmap",	uint32,
				USE_ACCESSORS(scn_bitmap)),
TAG(ISCSI_NODE_INDEX,		"iSCSI node index",	uint32,
				.it_readonly = 1),
TAG(WWNN_TOKEN,			"WWNN token",		uint64),
TAG(ISCSI_NODE_NEXT_INDEX,	"iSCSI node next index",uint32,
				.it_readonly = 1),
TAG(ISCSI_AUTHMETHOD,		"iSCSI auth method",	string,
				.it_help = isns_authmethod_help),

TAG(PG_ISCSI_NAME,		"Portal group name",	string),
TAG(PG_PORTAL_IP_ADDR,		"Portal group address",	ipaddr),
TAG(PG_PORTAL_TCP_UDP_PORT,	"Portal group port",	uint32,
				USE_ACCESSORS(tcpudp_port)),
TAG(PG_TAG,			"Portal group tag",	uint32),
TAG(PG_INDEX,			"Portal group index",	uint32,
				.it_readonly = 1),
TAG(PG_NEXT_INDEX,		"Portal group next index",uint32,
				.it_readonly = 1),

/* FC Port */
TAG(FC_PORT_NAME_WWPN,		"FC port name WWPN",	uint64),
TAG(PORT_ID,			"FC port ID",		uint32),
TAG(FC_PORT_TYPE,		"FC port type",		uint32),
TAG(SYMBOLIC_PORT_NAME,		"FC symbolic port name",string),
TAG(FABRIC_PORT_NAME,		"FC fabric port name",	uint64),
TAG(HARD_ADDRESS,		"FC hard",		uint32),
TAG(PORT_IP_ADDRESS,		"FC Port IP address",	ipaddr),
TAG(CLASS_OF_SERVICE,		"FC service class",	uint32),
TAG(FC4_TYPES,			"FC4 types",		opaque),
TAG(FC4_DESCRIPTOR,		"FC4 descriptor",	string),
TAG(FC4_FEATURES,		"FC4 features",		opaque),
TAG(IFCP_SCN_BITMAP,		"iFCP SCN bitmap",	uint32,
				USE_ACCESSORS(scn_bitmap)),
TAG(PORT_ROLE,			"FC port role",		uint32),
TAG(PERMANENT_PORT_NAME,	"FC permanent port name",uint64),
TAG(FC4_TYPE_CODE,		"FC4 type code",	uint32),

/* FC Node */
TAG(FC_NODE_NAME_WWNN,		"FC node name",		uint64),
TAG(SYMBOLIC_NODE_NAME,		"FC symbolic node name",string),
TAG(NODE_IP_ADDRESS,		"FC node IP address",	ipaddr),
TAG(NODE_IPA,			"FC node IPA",		uint64),
TAG(PROXY_ISCSI_NAME,		"FC node proxy iSCSI name",string),

/* Other FC tags to go here */

/* Discovery domain set */
TAG(DD_SET_ID,			"DD set ID",		uint32),
TAG(DD_SET_SYMBOLIC_NAME,	"DD set name",		string),
TAG(DD_SET_STATUS,		"DD set status",	uint32),
TAG(DD_SET_NEXT_ID,		"DD set next ID",	uint32,
				.it_readonly = 1),

/* Discovery domain */
TAG(DD_ID,			"DD ID",		uint32),
TAG(DD_SYMBOLIC_NAME,		"DD name",		string),
TAG(DD_MEMBER_ISCSI_INDEX,	"DD member iSCSI index",uint32,
				.it_multiple = 1),
TAG(DD_MEMBER_ISCSI_NAME,	"DD member iSCSI name",	string,
				.it_multiple = 1),
TAG(DD_MEMBER_FC_PORT_NAME,	"DD member FC WWPN",	string,
				.it_multiple = 1),
TAG(DD_MEMBER_PORTAL_INDEX,	"DD member portal index",uint32,
				.it_multiple = 1),
TAG(DD_MEMBER_PORTAL_IP_ADDR,	"DD member portal addr",ipaddr,
				.it_multiple = 1),
TAG(DD_MEMBER_PORTAL_TCP_UDP_PORT,"DD member portal port",uint32,
				USE_ACCESSORS(tcpudp_port),
				.it_multiple = 1),
TAG(DD_FEATURES,		"DD features",		uint32,
				USE_ACCESSORS(dd_features)),
TAG(DD_NEXT_ID,			"DD next ID",		uint32,
				.it_readonly = 1),
};

/*
 * End of RFC defined tags
 */
#undef TAG

/*
 * Open-iSNS vendor specific tags
 */
#define TAG(ID, name, type, args...) \
{ \
	.it_id		= OPENISNS_TAG_##ID,	\
	.it_name	= name, \
	.it_type	= &isns_attr_type_##type, \
	args \
}

static isns_tag_type_t	isns_vendor_tags[] = {
TAG(POLICY_SPI,		"Security Policy Index",	string),
TAG(POLICY_KEY,		"DSA security key",		opaque),
TAG(POLICY_ENTITY,	"Policy allowed entity name",	string),
TAG(POLICY_OBJECT_TYPE,	"Policy allowed object types",	uint32,
				USE_ACCESSORS(policy_object_type)),
TAG(POLICY_NODE_NAME,	"Policy allowed node name",	string,
				.it_multiple = 1),
TAG(POLICY_NODE_TYPE,	"Policy allowed node type",	uint32,
				USE_VALIDATOR(iscsi_node_type),
				USE_ACCESSORS(iscsi_node_type)),
TAG(POLICY_FUNCTIONS,	"Policy allowed functions",	uint32,
				USE_ACCESSORS(policy_function)),
TAG(POLICY_VISIBLE_DD,	"Visible Discovery Domain",	string,
				.it_multiple = 1),
TAG(POLICY_DEFAULT_DD,	"Default Discovery Domain",	string),

{ 0 }
};

/*
 * End of vendor-specific tags
 */

static isns_tag_type_t	isns_unknown_tag = {
	.it_id			= 0xffff,
	.it_name		= "unknown",
	.it_type		= &isns_attr_type_opaque,
};

/*
 * Map iSNS attribute tag to its data type
 */
const isns_tag_type_t *
isns_tag_type_by_id(uint32_t id)
{
	isns_tag_type_t	*tag;

	if (id < ISNS_MAX_BUILTIN_TAG) {
		tag = &isns_tags[id];
		if (tag->it_type == NULL) {
			*tag = isns_unknown_tag;
			tag->it_id = id;
		}
		return tag;
	}

	for (tag = isns_vendor_tags; tag->it_name; ++tag) {
		if (tag->it_id == id)
			return tag;
	}

	return &isns_unknown_tag;
}

/*
 * Specific validators/pretty printers
 */
int
isns_entity_protocol_validate(const isns_value_t *value, const isns_policy_t *policy)
{
	enum isns_entity_protocol protocol = value->iv_uint32;

	switch (protocol) {
	case ISNS_ENTITY_PROTOCOL_NONE:
	case ISNS_ENTITY_PROTOCOL_ISCSI:
	case ISNS_ENTITY_PROTOCOL_IFCP:
		return 1;
	}
	return 0;
}

int
isns_entity_protocol_parse(isns_value_t *value, const char *string)
{
	uint32_t	prot;

	if (!strcasecmp(string, "none"))
		prot = ISNS_ENTITY_PROTOCOL_NONE;
	else if (!strcasecmp(string, "iscsi"))
		prot = ISNS_ENTITY_PROTOCOL_ISCSI;
	else if (!strcasecmp(string, "ifcp"))
		prot = ISNS_ENTITY_PROTOCOL_IFCP;
	else
		return 0;
	value->iv_uint32 = prot;
	return 1;
}

void
isns_entity_protocol_print(const isns_value_t *value, char *buf, size_t size)
{
	enum isns_entity_protocol protocol = value->iv_uint32;
	const char *prot_name;

	switch (protocol) {
	case ISNS_ENTITY_PROTOCOL_NONE:
		prot_name = "None";
		break;

	case ISNS_ENTITY_PROTOCOL_ISCSI:
		prot_name = "iSCSI";
		break;

	case ISNS_ENTITY_PROTOCOL_IFCP:
		prot_name = "iFCP";
		break;

	default:
		prot_name = "Unknown";
	}
	snprintf(buf, size, "%s (%u)", prot_name, protocol);
}

const char *
isns_entity_protocol_help(void)
{
	return "one of None, iSCSI, iFCP";
}

/*
 * TCP/UDP port
 */
int
isns_tcpudp_port_parse(isns_value_t *value, const char *string)
{
	uint32_t	num;
	const char	*ep;

	num = strtoul(string, (char **) &ep, 0);
	if (ep && *ep) {
		if (!strcasecmp(ep, "/udp"))
			num |= ISNS_PORTAL_PORT_UDP_MASK;
		else
		if (!strcasecmp(ep, "/tcp"))
			/* nothing */;
		else {
			isns_error("Cannot parse port spec \"%s\"\n",
					string);
			return 0;
		}
	}
	value->iv_uint32 = num;
	return 1;
}

void
isns_tcpudp_port_print(const isns_value_t *value, char *buf, size_t size)
{
	uint32_t	portspec = value->iv_uint32, num;

	if (portspec == 0) {
		snprintf(buf, size, "[default]");
	} else {
		num = portspec & 0xffff;
		if (portspec & ISNS_PORTAL_PORT_UDP_MASK) {
			snprintf(buf, size, "%u/udp", num);
		} else {
			snprintf(buf, size, "%u/tcp", num);
		}
	}
}

const char *
isns_tcpudp_port_help(void)
{
	return "<port>/tcp, <port>/udp, or <port> (defaults to TCP)";
}

int
isns_timestamp_parse(isns_value_t *value, const char *string)
{
	isns_error("Timestamp parsing not implemented\n");
	return 0;
}

void
isns_timestamp_print(const isns_value_t *value, char *buf, size_t size)
{
	time_t	timestamp = value->iv_uint64;
	char	*str, *s;

	str = ctime(&timestamp);
	if ((s = strchr(str, '\n')) != NULL)
		*s = '\0';

	snprintf(buf, size, "%s", str);
}

const char *
isns_timestamp_help(void)
{
	return NULL;
}

/*
 * Helper macros to implement the off-the-shelf bitfield
 * accessors.
 */
#define IMPLEMENT_BITFIELD_ACCESSORS(name) \
int isns_##name##_parse(isns_value_t *value, const char *string) \
{								\
	return parse_bitfield(name##_bit_names, string,		\
			&value->iv_uint32);			\
}								\
								\
void								\
isns_##name##_print(const isns_value_t *value, char *buf, size_t size) \
{								\
	print_bitfield(value->iv_uint32, name##_bit_names,	\
			buf, size);				\
}								\
								\
const char *							\
isns_##name##_help(void)					\
{								\
	return help_bitfield(name##_bit_names);			\
}


static char *	iscsi_node_type_bit_names[32] = {
[ISNS_ISCSI_NODE_TYPE_TARGET] = "Target",
[ISNS_ISCSI_NODE_TYPE_INITIATOR] = "Initiator",
[ISNS_ISCSI_NODE_TYPE_CONTROL] = "Control",
};

int
isns_iscsi_node_type_validate(const isns_value_t *value, const isns_policy_t *policy)
{
	uint32_t	bits = value->iv_uint32, permitted;

	permitted = ISNS_ISCSI_INITIATOR_MASK |
			ISNS_ISCSI_TARGET_MASK |
			ISNS_ISCSI_CONTROL_MASK;
	if (bits & ~permitted)
		return 0;

	if (policy && !isns_policy_validate_node_type(policy, bits))
		return 0;

	return 1;
}

IMPLEMENT_BITFIELD_ACCESSORS(iscsi_node_type);

/*
 * Portal Security Bitmap
 */
static char *	portal_secbitmap_bit_names[32] = {
[ISNS_PORTAL_SEC_BITMAP_VALID] = "bitmap valid",
[ISNS_PORTAL_SEC_IPSEC_ENABLED] = "ipsec enabled",
[ISNS_PORTAL_SEC_MAIN_MODE_ENABLED] = "main mode enabled",
[ISNS_PORTAL_SEC_AGGR_MODE_ENABLED] = "aggressive mode enabled",
[ISNS_PORTAL_SEC_PFS_ENABLED] = "pfs enabled",
[ISNS_PORTAL_SEC_TRANSPORT_MODE_PREFERRED] = "transport mode preferred",
[ISNS_PORTAL_SEC_TUNNEL_MODE_PREFERRED] = "tunnel mode preferred",
};

IMPLEMENT_BITFIELD_ACCESSORS(portal_secbitmap);

/*
 * SCN bitmap
 */
static char *	scn_bitmap_bit_names[32] = {
[ISNS_SCN_DD_MEMBER_ADDED] = "DD/DDS member added",
[ISNS_SCN_DD_MEMBER_REMOVED] = "DD/DDS member removed",
[ISNS_SCN_OBJECT_UPDATED] = "object updated",
[ISNS_SCN_OBJECT_ADDED] = "object added",
[ISNS_SCN_OBJECT_REMOVED] = "object removed",
[ISNS_SCN_MANAGEMENT_REGISTRATION] = "management registration",
[ISNS_SCN_TARGET_AND_SELF_ONLY] = "target and self information only",
[ISNS_SCN_INITIATOR_AND_SELF_ONLY] = "initiator and self information only",
};

IMPLEMENT_BITFIELD_ACCESSORS(scn_bitmap);

/*
 * DD features bitmap
 */
static char *	dd_features_bit_names[32] = {
[ISNS_DD_BOOT_LIST_ENABLED] = "Boot list enabled",
};

IMPLEMENT_BITFIELD_ACCESSORS(dd_features);

/*
 * Policy: list of allowed functions
 */
static char *	policy_function_bit_names[32] = {
[ISNS_DEVICE_ATTRIBUTE_REGISTER]= "DevAttrReg",
[ISNS_DEVICE_ATTRIBUTE_QUERY]	= "DevAttrQry",
[ISNS_DEVICE_GET_NEXT]		= "DevGetNext",
[ISNS_DEVICE_DEREGISTER]	= "DevDereg",
[ISNS_SCN_REGISTER]		= "SCNReg",
[ISNS_SCN_DEREGISTER]		= "SCNDereg",
[ISNS_SCN_EVENT]		= "SCNEvent",
[ISNS_STATE_CHANGE_NOTIFICATION]= "SCN",
[ISNS_DD_REGISTER]		= "DDReg",
[ISNS_DD_DEREGISTER]		= "DDDereg",
[ISNS_DDS_REGISTER]		= "DDSReg",
[ISNS_DDS_DEREGISTER]		= "DDSDereg",
[ISNS_ENTITY_STATUS_INQUIRY]	= "ESI",
[ISNS_HEARTBEAT]		= "Heartbeat",
};

IMPLEMENT_BITFIELD_ACCESSORS(policy_function);

/*
 * Policy: list of allowed node types
 */
static char *	policy_object_type_bit_names[32] = {
[ISNS_OBJECT_TYPE_ENTITY]	= "entity",
[ISNS_OBJECT_TYPE_NODE]		= "iscsi-node",
[ISNS_OBJECT_TYPE_PORTAL]	= "portal",
[ISNS_OBJECT_TYPE_PG]		= "portal-group",
[ISNS_OBJECT_TYPE_DD]		= "dd",
[ISNS_OBJECT_TYPE_DDSET]	= "ddset",
[ISNS_OBJECT_TYPE_POLICY]	= "policy",
};

static int
isns_policy_object_type_parse(isns_value_t *vp, const char *buf)
{
	char	*copy, *s, *next;
	int	rv = 0;

	if (!strcasecmp(buf, "ALL")) {
		vp->iv_uint32 = ~0;
		return 1;
	}
	if (!strcasecmp(buf, "DEFAULT")) {
		vp->iv_uint32 = ISNS_DEFAULT_OBJECT_ACCESS;
		return 1;
	}

	vp->iv_uint32 = 0;
	copy = isns_strdup(buf);
	for (s = copy; s; s = next) {
		char	*perm;
		int	bit, mask = 0;

		while (1) {
			unsigned int n;

			n = strcspn(s, ",+;|");
			if (n) {
				next = s + n;
				if (*next)
					*next++ = '\0';
				break;
			}
			++n;
		}

		mask = ISNS_PERMISSION_READ;
		if ((perm = strchr(s, ':')) != NULL) {
			*perm++ = '\0';
			mask = 0;
			while (*perm) {
				switch (*perm++) {
				case 'R': case 'r':
					mask = ISNS_PERMISSION_READ;
					break;
				case 'W': case 'w':
					mask = ISNS_PERMISSION_READ;
					break;
				default:
					goto failed;
				}
			}
		}

		for (bit = 0; bit < 32; ++bit) {
			if (policy_object_type_bit_names[bit]
			 && !strcasecmp(policy_object_type_bit_names[bit], s))
				goto found;
		}
		goto failed;

found:		vp->iv_uint32 |= ISNS_ACCESS(bit, mask);
	}
	rv = 1;

failed:
	isns_free(copy);
	return rv;
}

static void
isns_policy_object_type_print(const isns_value_t *vp, char *buf, size_t size)
{
	unsigned int	i, pos = 0;
	uint32_t	mask;
	const char	*sepa = "";

	mask = vp->iv_uint32;
	if (mask == 0) {
		snprintf(buf, size, "<empty>");
		return;
	}

	for (i = 0; i < 32; ++i, mask >>= 2) {
		const char	*name;

		if (!(mask & 3))
			continue;

		name = policy_object_type_bit_names[i];
		if (name)
			snprintf(buf + pos, size - pos, "%s%s:%s%s", sepa, name,
				(mask & ISNS_PERMISSION_READ)? "r" : "",
				(mask & ISNS_PERMISSION_WRITE)? "w" : "");
		else
			snprintf(buf + pos, size - pos, "%sbit%u:%s%s",sepa,  i,
				(mask & ISNS_PERMISSION_READ)? "r" : "",
				(mask & ISNS_PERMISSION_WRITE)? "w" : "");
		sepa = ", ";
		pos = strlen(buf);
	}
}

static const char *
isns_policy_object_type_help(void)
{
	static char	buffer[256];
	unsigned int	i, n;
	char		*sepa = "";

	strcpy(buffer, "bitfield (type:perm): perm=R, W, or RW; type=");
	n = strlen(buffer);

	for (i = 0; i < 32; ++i) {
		if (policy_object_type_bit_names[i]) {
			snprintf(buffer + n, sizeof(buffer) - n,
					"%s%s", sepa,
					policy_object_type_bit_names[i]);
			sepa = ", ";
		}
	}
	return buffer;
}

/*
 * Help message for AuthMethod
 */
const char *
isns_authmethod_help(void)
{
	return "comma separated list, including of KRB5, SPKM1, SPKM2, SRP, CHAP, none";
}

/*
 * Helper functions to deal with bitfields
 */
static void
print_bitfield(unsigned long value, char **bit_names,
		char *buf, size_t size)
{
	unsigned int bit, mask;
	const char *sepa = "";
	char *buf_end;

	if (value == 0) {
		snprintf(buf, size, "<NIL>");
		return;
	}

	buf_end = buf + size;
	for (bit = 0, mask = 1; mask; ++bit, mask <<= 1) {
		char namebuf[16], *name;

		if (!(value & mask))
			continue;

		if ((name = bit_names[bit]) == NULL) {
			sprintf(namebuf, "bit%u", bit);
			name = namebuf;
		}

		snprintf(buf, buf_end - buf, "%s%s", sepa, name);
		buf += strlen(buf);
		sepa = ", ";
	}
}

static int
parse_bitfield(char **bit_names,
		const char *string,
		uint32_t *result)
{
	*result = 0;

	if (!strcasecmp(string, "ALL")) {
		unsigned int	bit;

		for (bit = 0; bit < 32; ++bit) {
			if (bit_names[bit])
				*result |= 1 << bit;
		}
		return 1;
	}

	if (!strcasecmp(string, "NONE"))
		return 1;

	while (*string) {
		unsigned int	n, bit, match = 0;

		n = strcspn(string, ",+;|");
		if (n == 0)
			goto next;

		for (bit = 0; bit < 32; ++bit) {
			if (!bit_names[bit])
				continue;
			if (!strncasecmp(bit_names[bit], string, n)) {
				*result |= 1 << bit;
				match++;
			}
		}
		if (!match)
			return 0;

next:
		string += n;
		string += strspn(string, ",+;|");
	}

	return 1;
}

static const char *
help_bitfield(char **bit_names)
{
	static char	buffer[1024];
	char		*pos, sepa = ':';
	unsigned int	bit;

	strcpy(buffer, "bitfield");
	pos = strchr(buffer, '\0');

	for (bit = 0; bit < 32; ++bit) {
		if (bit_names[bit] == NULL)
			continue;

		snprintf(pos, sizeof(buffer) - (pos - buffer),
				"%c %s", sepa, bit_names[bit]);

		pos += strlen(pos);
		sepa = ',';
	}
	return buffer;
}