xref: /titanic_52/usr/src/cmd/tsol/getlabel/getlabel.c (revision 3c112a2b34403220c06c3e2fcac403358cfba168)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *	getlabel - gets file label.
31  *
32  */
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <locale.h>
39 #include <tsol/label.h>
40 
41 #define	s_flag	0x04
42 #define	S_flag	0x08
43 
44 
45 static int
46 get_label(char *filename, uint_t opt_flag)
47 {
48 	m_label_t *fl;
49 	char	*label;
50 
51 	if ((fl = m_label_alloc(MAC_LABEL)) == NULL) {
52 		perror("m_label_alloc");
53 		return (1);
54 	} else if (getlabel(filename, fl) != 0) {
55 		perror(filename);
56 		m_label_free(fl);
57 		return (1);
58 	}
59 
60 	(void) printf("%s:\t", filename);
61 	switch (opt_flag)  {
62 	case S_flag:
63 		if (label_to_str(fl, &label, M_LABEL, LONG_NAMES) != 0) {
64 			perror(gettext("%s:unable to translate "
65 			    "Sensitivity label"));
66 			m_label_free(fl);
67 			return (2);
68 		}
69 		break;
70 	case s_flag:
71 		if (label_to_str(fl, &label, M_LABEL, SHORT_NAMES) != 0) {
72 			perror(gettext("unable to translate "
73 			    "Sensitivity label"));
74 			m_label_free(fl);
75 			return (2);
76 		}
77 		break;
78 	default:
79 		if (label_to_str(fl, &label, M_LABEL, DEF_NAMES) != 0) {
80 			perror(gettext("unable to translate "
81 			    "Sensitivity label"));
82 			m_label_free(fl);
83 			return (2);
84 		}
85 		break;
86 	}
87 	(void) printf("%s\n", label);
88 
89 	free(label);
90 	m_label_free(fl);
91 	return (0);
92 }
93 
94 void
95 usage(char *prog)
96 {
97 	(void) fprintf(stderr, gettext("Usage: \n"));
98 	(void) fprintf(stderr, gettext("\t%s [-S | -s] filename ...\n"),
99 	    prog);
100 	exit(1);
101 }
102 
103 
104 int
105 main(int argc, char **argv)
106 {
107 	uint_t	opt_flag = 0;
108 	int	rc = 0;
109 	int	opt;
110 	char	*prog;
111 
112 	(void) setlocale(LC_ALL, "");
113 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
114 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
115 #endif
116 	(void) textdomain(TEXT_DOMAIN);
117 
118 	if ((prog = strrchr(argv[0], '/')) == NULL)
119 		prog = argv[0];
120 	else
121 		prog++;
122 
123 	if (argc < 2) {
124 		usage(prog);
125 	}
126 
127 	while ((opt = getopt(argc, argv, ":sS")) != EOF) {
128 		switch (opt) {
129 		case 's':
130 			if (opt_flag != 0)
131 				usage(prog);
132 			opt_flag = s_flag;
133 			break;
134 		case 'S':
135 			if (opt_flag != 0)
136 				usage(prog);
137 			opt_flag = S_flag;
138 			break;
139 		default:
140 			usage(prog);
141 		}
142 	}
143 	if ((argc -= optind) < 1) {
144 		usage(prog);
145 	}
146 	argv += optind;
147 	while (argc-- > 0) {
148 		if (get_label(*argv++, opt_flag) != 0)
149 			rc = 2;
150 	}
151 	return (rc);
152 }
153