xref: /illumos-gate/usr/src/cmd/tsol/setlabel/setlabel.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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  *	setlabel - sets a file label.
29  */
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <locale.h>
34 #include <pwd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <utmp.h>
40 
41 #include <tsol/label.h>
42 
43 static int	set_label(char *, char *);
44 static int	setlabel(char *, bslabel_t *);
45 static void	usage(void);
46 static void	m_label_err(const char *, const int);
47 
48 static char *prog = NULL;
49 
50 int
main(int argc,char ** argv)51 main(int argc, char **argv)
52 {
53 	int	rc = 0;
54 	char	*label;
55 
56 	(void) setlocale(LC_ALL, "");
57 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
58 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
59 #endif
60 	(void) textdomain(TEXT_DOMAIN);
61 
62 	if ((prog = strrchr(argv[0], '/')) == NULL)
63 		prog = argv[0];
64 	else
65 		prog++;
66 
67 	if (argc < 3) {
68 		usage();
69 		return (2);
70 	}
71 
72 	argv++;
73 	argc--;
74 
75 	label = *argv;
76 	argv++;
77 	argc--;
78 	while (argc-- > 0) {
79 		if (set_label(*argv++, label) != 0)
80 			rc = 1;
81 	}
82 
83 	return (rc);
84 }
85 
86 static int
set_label(char * filename,char * label)87 set_label(char *filename, char *label)
88 {
89 	int rval = 0;
90 	int err;
91 	m_label_t *blabel;
92 
93 	if ((blabel = m_label_alloc(MAC_LABEL)) == NULL) {
94 		(void) fprintf(stderr, "setlabel: ");
95 		perror(filename);
96 		return (2);
97 	}
98 	rval = getlabel(filename, blabel);
99 	if (rval) {
100 		(void) fprintf(stderr, "setlabel: ");
101 		perror(filename);
102 		return (rval);
103 	}
104 	if (!bslvalid(blabel)) {
105 		(void) fprintf(stderr,
106 		    gettext("%s: Current label is invalid\n"),
107 		    filename);
108 		blabel = NULL;
109 	}
110 	if (str_to_label(label, &blabel, MAC_LABEL, L_DEFAULT, &err) == -1) {
111 		m_label_err(label, err);
112 	}
113 
114 	rval = setlabel(filename, blabel);
115 	if (rval == 0)
116 		m_label_free(blabel);
117 	return (rval);
118 }
119 
120 static int
setlabel(char * filename,bslabel_t * label)121 setlabel(char *filename, bslabel_t *label)
122 {
123 	int	rval;
124 
125 	rval = setflabel(filename, label);
126 
127 	if (rval) {
128 		(void) fprintf(stderr, "setlabel: ");
129 		perror(filename);
130 	}
131 	return (rval);
132 }
133 
134 static void
m_label_err(const char * ascii,const int err)135 m_label_err(const char *ascii, const int err)
136 {
137 	if (errno == EINVAL) {
138 		switch (err) {
139 		case M_BAD_STRING:
140 			(void) fprintf(stderr,
141 			    gettext("setlabel: bad string %s\n"), ascii);
142 		break;
143 		case M_BAD_LABEL:
144 			(void) fprintf(stderr,
145 			    gettext("setlabel: bad previous label\n"));
146 		break;
147 		default:
148 			(void) fprintf(stderr,
149 			    gettext("setlabel: parsing error found in "
150 			    "\"%s\" at position %d\n"), ascii, err);
151 		break;
152 		}
153 	} else {
154 		perror("setlabel");
155 	}
156 	exit(1);
157 }
158 /*
159  * usage()
160  *
161  * This routine is called whenever there is a usage type of error has
162  * occured.  For example, when a invalid option has has been specified.
163  *
164  */
165 static void
usage(void)166 usage(void)
167 {
168 
169 	(void) fprintf(stderr, gettext("Usage: \n"));
170 	(void) fprintf(stderr, gettext(
171 	    "	%s newlabel filename [...] \n"), prog);
172 
173 }
174