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