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