4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 * lslabels - Display all labels dominating the specified label.
41 #include <sys/param.h>
43 #include <tsol/label.h>
44 #include <sys/tsol/label_macro.h>
45 #include <iso/limits_iso.h>
47 #if !defined(TEXT_DOMAIN)
48 #define TEXT_DOMAIN "SYS_TEST"
49 #endif /* !defined(TEXT_DOMAIN) */
51 int hflg
= 0; /* true if hex output */
54 * Compartment mask macros.
57 typedef uint32_t comp_chunk_t
;
59 #define __NBWRD (CHAR_BIT * sizeof (comp_chunk_t))
60 #define COMP_BITS (CHAR_BIT * sizeof (Compartments_t))
61 #define compmask(n) (1 << ((__NBWRD - 1) - ((n) % __NBWRD)))
62 #define compword(n) ((n)/__NBWRD)
64 #define COMP_ADDSET(a, p) ((comp_chunk_t *)(a))[compword(p)] |= \
66 #define COMP_DELSET(a, p) ((comp_chunk_t *)(a))[compword(p)] &= \
68 #define COMP_ISMEMBER(a, p) ((((comp_chunk_t *)(a))[compword(p)] & \
71 /* Need functions to test if bit is on */
75 bitfinder(m_label_t label
, int next_bit
) {
76 char *labelstr
= NULL
;
78 Compartments_t
*comps
= &label
.compartments
;
80 while (next_bit
< COMP_BITS
) {
81 if (COMP_ISMEMBER(comps
, next_bit
)) {
82 bitfinder(label
, next_bit
+ 1);
83 COMP_DELSET(comps
, next_bit
);
85 if (label_to_str(&label
, &labelstr
, M_LABEL
,
87 m_label_t
*label2
= NULL
;
90 if (str_to_label(labelstr
, &label2
, MAC_LABEL
,
91 L_NO_CORRECTION
, &err
) == 0) {
93 (void) printf("%s\n", labelstr
);
96 (void) label_to_str(&label
,
97 &labelstr
, M_INTERNAL
, 0);
98 (void) printf("%s\n", labelstr
);
100 m_label_free(label2
);
104 bitfinder(label
, next_bit
+ 1);
112 label_error(const char *ascii
, const int err
)
114 if (errno
== EINVAL
) {
117 (void) fprintf(stderr
,
118 gettext("lslabels: bad string %s\n"), ascii
);
121 (void) fprintf(stderr
,
122 gettext("lslabels: bad previous label\n"));
125 (void) fprintf(stderr
,
126 gettext("lslabels: parsing error found in "
127 "\"%s\" at position %d\n"), ascii
, err
);
138 main(int argc
, char **argv
)
140 int errflg
= 0; /* true if arg error */
141 m_label_t
*label
= NULL
; /* binary labels */
142 char ascii
[PIPE_BUF
]; /* human readable label */
143 char *labelstr
= NULL
; /* external label to start from */
144 int err
= 0; /* label error */
147 _Classification
*level
;
149 (void) setlocale(LC_ALL
, "");
150 (void) textdomain(TEXT_DOMAIN
);
153 while ((c
= getopt(argc
, argv
, "h")) != EOF
) {
168 if (errflg
|| argc
> 2) {
170 (void) fprintf(stderr
,
171 gettext("usage: %s [-h] [label]\n"),
178 /* use label on command line */
180 (void) strlcpy(ascii
, argv
[optind
], sizeof (ascii
));
182 /* read label from standard input */
183 if ((c
= read(STDIN_FILENO
, ascii
, sizeof (ascii
))) <= 0) {
184 perror(gettext("reading ASCII coded label"));
190 * replace '\n' or (end of buffer) with end of string.
195 * flush any remaining input past the size of the buffer.
197 (void) ioctl(STDIN_FILENO
, I_FLUSH
, FLUSHR
);
200 if (str_to_label(ascii
, &label
, MAC_LABEL
, L_NO_CORRECTION
,
202 label_error(ascii
, err
);
204 if (label_to_str(label
, &labelstr
, mode
,
206 (void) printf("%s\n", labelstr
);
209 level
= &label
->classification
.class_u
.class_chunk
;
211 bitfinder(*label
, 0);
216 return (0); /* really exit(0); */