xref: /titanic_52/usr/src/cmd/tsol/getlabel/getlabel.c (revision f875b4ebb1dd9fdbeb043557cab38ab3bf7f6e01)
1*f875b4ebSrica /*
2*f875b4ebSrica  * CDDL HEADER START
3*f875b4ebSrica  *
4*f875b4ebSrica  * The contents of this file are subject to the terms of the
5*f875b4ebSrica  * Common Development and Distribution License (the "License").
6*f875b4ebSrica  * You may not use this file except in compliance with the License.
7*f875b4ebSrica  *
8*f875b4ebSrica  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*f875b4ebSrica  * or http://www.opensolaris.org/os/licensing.
10*f875b4ebSrica  * See the License for the specific language governing permissions
11*f875b4ebSrica  * and limitations under the License.
12*f875b4ebSrica  *
13*f875b4ebSrica  * When distributing Covered Code, include this CDDL HEADER in each
14*f875b4ebSrica  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*f875b4ebSrica  * If applicable, add the following below this CDDL HEADER, with the
16*f875b4ebSrica  * fields enclosed by brackets "[]" replaced with your own identifying
17*f875b4ebSrica  * information: Portions Copyright [yyyy] [name of copyright owner]
18*f875b4ebSrica  *
19*f875b4ebSrica  * CDDL HEADER END
20*f875b4ebSrica  */
21*f875b4ebSrica 
22*f875b4ebSrica /*
23*f875b4ebSrica  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*f875b4ebSrica  * Use is subject to license terms.
25*f875b4ebSrica  */
26*f875b4ebSrica 
27*f875b4ebSrica #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*f875b4ebSrica 
29*f875b4ebSrica /*
30*f875b4ebSrica  *	getlabel - gets file label.
31*f875b4ebSrica  *
32*f875b4ebSrica  */
33*f875b4ebSrica #include <stdio.h>
34*f875b4ebSrica #include <stdlib.h>
35*f875b4ebSrica #include <unistd.h>
36*f875b4ebSrica #include <fcntl.h>
37*f875b4ebSrica #include <string.h>
38*f875b4ebSrica #include <locale.h>
39*f875b4ebSrica #include <tsol/label.h>
40*f875b4ebSrica 
41*f875b4ebSrica #define	s_flag	0x04
42*f875b4ebSrica #define	S_flag	0x08
43*f875b4ebSrica 
44*f875b4ebSrica 
45*f875b4ebSrica static int
46*f875b4ebSrica get_label(char *filename, uint_t opt_flag)
47*f875b4ebSrica {
48*f875b4ebSrica 	m_label_t *fl;
49*f875b4ebSrica 	char	*label;
50*f875b4ebSrica 
51*f875b4ebSrica 	if ((fl = m_label_alloc(MAC_LABEL)) == NULL) {
52*f875b4ebSrica 		perror("m_label_alloc");
53*f875b4ebSrica 		return (1);
54*f875b4ebSrica 	} else if (getlabel(filename, fl) != 0) {
55*f875b4ebSrica 		perror(filename);
56*f875b4ebSrica 		m_label_free(fl);
57*f875b4ebSrica 		return (1);
58*f875b4ebSrica 	}
59*f875b4ebSrica 
60*f875b4ebSrica 	(void) printf("%s:\t", filename);
61*f875b4ebSrica 	switch (opt_flag)  {
62*f875b4ebSrica 	case S_flag:
63*f875b4ebSrica 		if (label_to_str(fl, &label, M_LABEL, LONG_NAMES) != 0) {
64*f875b4ebSrica 			perror(gettext("%s:unable to translate "
65*f875b4ebSrica 			    "Sensitivity label"));
66*f875b4ebSrica 			m_label_free(fl);
67*f875b4ebSrica 			return (2);
68*f875b4ebSrica 		}
69*f875b4ebSrica 		break;
70*f875b4ebSrica 	case s_flag:
71*f875b4ebSrica 		if (label_to_str(fl, &label, M_LABEL, SHORT_NAMES) != 0) {
72*f875b4ebSrica 			perror(gettext("unable to translate "
73*f875b4ebSrica 			    "Sensitivity label"));
74*f875b4ebSrica 			m_label_free(fl);
75*f875b4ebSrica 			return (2);
76*f875b4ebSrica 		}
77*f875b4ebSrica 		break;
78*f875b4ebSrica 	default:
79*f875b4ebSrica 		if (label_to_str(fl, &label, M_LABEL, DEF_NAMES) != 0) {
80*f875b4ebSrica 			perror(gettext("unable to translate "
81*f875b4ebSrica 			    "Sensitivity label"));
82*f875b4ebSrica 			m_label_free(fl);
83*f875b4ebSrica 			return (2);
84*f875b4ebSrica 		}
85*f875b4ebSrica 		break;
86*f875b4ebSrica 	}
87*f875b4ebSrica 	(void) printf("%s\n", label);
88*f875b4ebSrica 
89*f875b4ebSrica 	free(label);
90*f875b4ebSrica 	m_label_free(fl);
91*f875b4ebSrica 	return (0);
92*f875b4ebSrica }
93*f875b4ebSrica 
94*f875b4ebSrica void
95*f875b4ebSrica usage(char *prog)
96*f875b4ebSrica {
97*f875b4ebSrica 	(void) fprintf(stderr, gettext("Usage: \n"));
98*f875b4ebSrica 	(void) fprintf(stderr, gettext("\t%s [-S | -s] filename ...\n"),
99*f875b4ebSrica 	    prog);
100*f875b4ebSrica 	exit(1);
101*f875b4ebSrica }
102*f875b4ebSrica 
103*f875b4ebSrica 
104*f875b4ebSrica int
105*f875b4ebSrica main(int argc, char **argv)
106*f875b4ebSrica {
107*f875b4ebSrica 	uint_t	opt_flag = 0;
108*f875b4ebSrica 	int	rc = 0;
109*f875b4ebSrica 	int	opt;
110*f875b4ebSrica 	char	*prog;
111*f875b4ebSrica 
112*f875b4ebSrica 	(void) setlocale(LC_ALL, "");
113*f875b4ebSrica #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
114*f875b4ebSrica #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
115*f875b4ebSrica #endif
116*f875b4ebSrica 	(void) textdomain(TEXT_DOMAIN);
117*f875b4ebSrica 
118*f875b4ebSrica 	if ((prog = strrchr(argv[0], '/')) == NULL)
119*f875b4ebSrica 		prog = argv[0];
120*f875b4ebSrica 	else
121*f875b4ebSrica 		prog++;
122*f875b4ebSrica 
123*f875b4ebSrica 	if (argc < 2) {
124*f875b4ebSrica 		usage(prog);
125*f875b4ebSrica 	}
126*f875b4ebSrica 
127*f875b4ebSrica 	while ((opt = getopt(argc, argv, ":sS")) != EOF) {
128*f875b4ebSrica 		switch (opt) {
129*f875b4ebSrica 		case 's':
130*f875b4ebSrica 			if (opt_flag != 0)
131*f875b4ebSrica 				usage(prog);
132*f875b4ebSrica 			opt_flag = s_flag;
133*f875b4ebSrica 			break;
134*f875b4ebSrica 		case 'S':
135*f875b4ebSrica 			if (opt_flag != 0)
136*f875b4ebSrica 				usage(prog);
137*f875b4ebSrica 			opt_flag = S_flag;
138*f875b4ebSrica 			break;
139*f875b4ebSrica 		default:
140*f875b4ebSrica 			usage(prog);
141*f875b4ebSrica 		}
142*f875b4ebSrica 	}
143*f875b4ebSrica 	if ((argc -= optind) < 1) {
144*f875b4ebSrica 		usage(prog);
145*f875b4ebSrica 	}
146*f875b4ebSrica 	argv += optind;
147*f875b4ebSrica 	while (argc-- > 0) {
148*f875b4ebSrica 		if (get_label(*argv++, opt_flag) != 0)
149*f875b4ebSrica 			rc = 2;
150*f875b4ebSrica 	}
151*f875b4ebSrica 	return (rc);
152*f875b4ebSrica }
153