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]
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
39 #include <tsol/label.h>
42 #include <sys/tsol/label_macro.h>
44 #include <sys/types.h>
48 #include <sys/param.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.
73 * Calls __call_labeld(SETFLABEL)
78 setflabel(const char *path
, m_label_t
*label
)
81 labeld_data_t
*callp
= &call
;
82 size_t bufsize
= sizeof (labeld_data_t
);
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
95 if (*path
!= '/' && cwd
[0] == '\0') {
96 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
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)
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
) {
120 callp
->callop
= SETFLABEL
;
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 */
134 * reterr < 0, invalid binary label,
137 if (clret
.status
> 0) {
138 errno
= clret
.status
;
143 } else if (err
< 0) {
146 errno
= ECONNREFUSED
;
149 if (callp
!= &call
) {
150 /* free allocated buffer */
153 /* server not present */
154 errno
= ECONNREFUSED
;
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.
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
)
181 labeld_data_t
*callp
= &call
;
182 size_t bufsize
= sizeof (labeld_data_t
);
185 size_t remote_dir_len
;
187 size_t local_dir_len
;
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
)
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
) {
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 */
239 * reterr < 0, transer not confirmed
242 return (clret
.status
);
243 } else if (err
< 0) {
246 return (PIPEMSG_CANCEL
);
248 if (callp
!= &call
) {
249 /* free allocated buffer */
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
)
280 * If the path is relative, convert it to absolute form,
281 * using wd if it's been supplied.
284 char *limit
= absbuf
+ sizeof (absbuf
);
287 /* Fill in working directory. */
289 (void) strncpy(absbuf
, wd
, sizeof (absbuf
));
290 else if (getcwd(absbuf
, strlen(absbuf
)) == NULL
)
293 /* Add separating slash. */
294 d
= absbuf
+ strlen(absbuf
);
298 /* Glue on the relative part of the path. */
299 while (d
< limit
&& (*d
++ = *raw
++))
306 * Call realpath to canonicalize and resolve symlinks.
308 return (realpath(raw
, canon
) == NULL
? -1 : 0);