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