Merge branch 'less_closed'
[unleashed.git] / usr / src / lib / libtsol / common / setflabel.c
blob1d645c8d2340b48399f3b80a87bd0a0985b3dd0b
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Change the label of a file
32 #include <ctype.h>
33 #include <locale.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <strings.h>
37 #include <errno.h>
39 #include <tsol/label.h>
41 #include "labeld.h"
42 #include <sys/tsol/label_macro.h>
44 #include <sys/types.h>
46 #include <zone.h>
47 #include <sys/zone.h>
48 #include <sys/param.h>
49 #include <string.h>
51 static int abspath(char *, const char *, char *);
54 * setflabel(3TSOL) - set file label
56 * This is the library interface to the door call.
59 #define clcall callp->param.acall.cargs.setfbcl_arg
60 #define clret callp->param.aret.rvals.setfbcl_ret
63 * Exit error = If error reported, the error indicator,
64 * -1, Unable to access label encodings file;
65 * 0, Invalid binary label passed;
66 * >0, Position after the first character in
67 * string of error, 1 indicates entire string.
68 * Otherwise, unchanged.
70 * Returns 0, If error.
71 * 1, If successful.
73 * Calls __call_labeld(SETFLABEL)
77 int
78 setflabel(const char *path, m_label_t *label)
80 labeld_data_t call;
81 labeld_data_t *callp = &call;
82 size_t bufsize = sizeof (labeld_data_t);
83 size_t datasize;
84 size_t path_len;
85 static char cwd[MAXPATHLEN];
86 char canon[MAXPATHLEN];
90 * If path is relative and we haven't already determined the current
91 * working directory, do so now. Calculating the working directory
92 * here lets us do the work once, instead of (potentially) repeatedly
93 * in realpath().
95 if (*path != '/' && cwd[0] == '\0') {
96 if (getcwd(cwd, MAXPATHLEN) == NULL) {
97 cwd[0] = '\0';
98 return (-1);
102 * Find an absolute pathname in the native file system name space that
103 * corresponds to path, stuffing it into canon.
105 if (abspath(cwd, path, canon) < 0)
106 return (-1);
108 path_len = strlen(canon) + 1;
110 datasize = CALL_SIZE(setfbcl_call_t, path_len - BUFSIZE);
111 datasize += 2; /* PAD */
113 if (datasize > bufsize) {
114 if ((callp = (labeld_data_t *)malloc(datasize)) == NULL) {
115 return (-1);
117 bufsize = datasize;
120 callp->callop = SETFLABEL;
122 clcall.sl = *label;
123 (void) strcpy(clcall.pathname, canon);
125 if (__call_labeld(&callp, &bufsize, &datasize) == SUCCESS) {
126 int err = callp->reterr;
128 if (callp != &call) {
129 /* free allocated buffer */
130 free(callp);
133 * reterr == 0, OK,
134 * reterr < 0, invalid binary label,
136 if (err == 0) {
137 if (clret.status > 0) {
138 errno = clret.status;
139 return (-1);
140 } else {
141 return (0);
143 } else if (err < 0) {
144 err = 0;
146 errno = ECONNREFUSED;
147 return (-1);
148 } else {
149 if (callp != &call) {
150 /* free allocated buffer */
151 free(callp);
153 /* server not present */
154 errno = ECONNREFUSED;
155 return (-1);
157 } /* setflabel */
159 #undef clcall
160 #undef clret
162 #define clcall callp->param.acall.cargs.zcopy_arg
163 #define clret callp->param.aret.rvals.zcopy_ret
166 * Exit status = result of zone copy request
167 * -1, Copy not confirmed
168 * Otherwise, unchanged.
170 * Returns 0, If error.
171 * 1, If successful.
173 * Calls __call_labeld(ZCOPY)
177 zonecopy(m_label_t *src_win_sl, char *remote_dir, char *filename,
178 char *local_dir, int transfer_mode)
180 labeld_data_t call;
181 labeld_data_t *callp = &call;
182 size_t bufsize = sizeof (labeld_data_t);
183 size_t datasize;
184 size_t strings;
185 size_t remote_dir_len;
186 size_t filename_len;
187 size_t local_dir_len;
188 size_t display_len;
189 char *display;
191 remote_dir_len = strlen(remote_dir) + 1;
192 filename_len = strlen(filename) + 1;
193 local_dir_len = strlen(local_dir) + 1;
195 if ((display = getenv("DISPLAY")) == NULL)
196 display = "";
197 display_len = strlen(display) + 1;
199 strings = remote_dir_len + filename_len + local_dir_len + display_len;
201 datasize = CALL_SIZE(zcopy_call_t, strings - BUFSIZE);
203 datasize += 4; /* PAD */
205 if (datasize > bufsize) {
206 if ((callp = (labeld_data_t *)malloc(datasize)) == NULL) {
207 return (NULL);
209 bufsize = datasize;
212 strings = 0;
213 callp->callop = ZCOPY;
215 clcall.src_win_sl = *src_win_sl;
216 clcall.transfer_mode = transfer_mode;
217 clcall.remote_dir = strings;
218 strings += remote_dir_len;
219 clcall.filename = strings;
220 strings += filename_len;
221 clcall.local_dir = strings;
222 strings += local_dir_len;
223 clcall.display = strings;
225 (void) strcpy(&clcall.buf[clcall.remote_dir], remote_dir);
226 (void) strcpy(&clcall.buf[clcall.filename], filename);
227 (void) strcpy(&clcall.buf[clcall.local_dir], local_dir);
228 (void) strcpy(&clcall.buf[clcall.display], display);
230 if (__call_labeld(&callp, &bufsize, &datasize) == SUCCESS) {
231 int err = callp->reterr;
233 if (callp != &call) {
234 /* free allocated buffer */
235 free(callp);
238 * reterr == 0, OK,
239 * reterr < 0, transer not confirmed
241 if (err == 0) {
242 return (clret.status);
243 } else if (err < 0) {
244 err = 0;
246 return (PIPEMSG_CANCEL);
247 } else {
248 if (callp != &call) {
249 /* free allocated buffer */
250 free(callp);
252 /* server not present */
253 return (PIPEMSG_CANCEL);
258 * Convert the path given in raw to canonical, absolute, symlink-free
259 * form, storing the result in the buffer named by canon, which must be
260 * at least MAXPATHLEN bytes long. If wd is non-NULL, assume that it
261 * points to a path for the current working directory and use it instead
262 * of invoking getcwd; accepting this value as an argument lets our caller
263 * cache the value, so that realpath (called from this routine) doesn't have
264 * to recalculate it each time it's given a relative pathname.
266 * Return 0 on success, -1 on failure.
269 abspath(char *wd, const char *raw, char *canon)
271 char absbuf[MAXPATHLEN];
274 * Preliminary sanity check.
276 if (raw == NULL || canon == NULL)
277 return (-1);
280 * If the path is relative, convert it to absolute form,
281 * using wd if it's been supplied.
283 if (raw[0] != '/') {
284 char *limit = absbuf + sizeof (absbuf);
285 char *d;
287 /* Fill in working directory. */
288 if (wd != NULL)
289 (void) strncpy(absbuf, wd, sizeof (absbuf));
290 else if (getcwd(absbuf, strlen(absbuf)) == NULL)
291 return (-1);
293 /* Add separating slash. */
294 d = absbuf + strlen(absbuf);
295 if (d < limit)
296 *d++ = '/';
298 /* Glue on the relative part of the path. */
299 while (d < limit && (*d++ = *raw++))
300 continue;
302 raw = absbuf;
306 * Call realpath to canonicalize and resolve symlinks.
308 return (realpath(raw, canon) == NULL ? -1 : 0);