145916cd2Sjpk /*
245916cd2Sjpk  * CDDL HEADER START
345916cd2Sjpk  *
445916cd2Sjpk  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
745916cd2Sjpk  *
845916cd2Sjpk  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945916cd2Sjpk  * or http://www.opensolaris.org/os/licensing.
1045916cd2Sjpk  * See the License for the specific language governing permissions
1145916cd2Sjpk  * and limitations under the License.
1245916cd2Sjpk  *
1345916cd2Sjpk  * When distributing Covered Code, include this CDDL HEADER in each
1445916cd2Sjpk  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545916cd2Sjpk  * If applicable, add the following below this CDDL HEADER, with the
1645916cd2Sjpk  * fields enclosed by brackets "[]" replaced with your own identifying
1745916cd2Sjpk  * information: Portions Copyright [yyyy] [name of copyright owner]
1845916cd2Sjpk  *
1945916cd2Sjpk  * CDDL HEADER END
2045916cd2Sjpk  */
2145916cd2Sjpk /*
2245916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2345916cd2Sjpk  * Use is subject to license terms.
2445916cd2Sjpk  */
2545916cd2Sjpk 
2645916cd2Sjpk /*
2745916cd2Sjpk  *	Change the label of a file
2845916cd2Sjpk  */
2945916cd2Sjpk 
3045916cd2Sjpk #include <ctype.h>
3145916cd2Sjpk #include <locale.h>
3245916cd2Sjpk #include <stdio.h>
3345916cd2Sjpk #include <stdlib.h>
3445916cd2Sjpk #include <strings.h>
3545916cd2Sjpk #include <errno.h>
3645916cd2Sjpk 
3745916cd2Sjpk #include <tsol/label.h>
3845916cd2Sjpk 
3945916cd2Sjpk #include "labeld.h"
4045916cd2Sjpk #include <sys/tsol/label_macro.h>
4145916cd2Sjpk 
4245916cd2Sjpk #include <sys/types.h>
4345916cd2Sjpk 
4445916cd2Sjpk #include <zone.h>
4545916cd2Sjpk #include <sys/zone.h>
4645916cd2Sjpk #include <sys/param.h>
4745916cd2Sjpk #include <string.h>
4845916cd2Sjpk 
4945916cd2Sjpk static int abspath(char *, const char *, char *);
5045916cd2Sjpk 
5145916cd2Sjpk /*
5245916cd2Sjpk  * setflabel(3TSOL) - set file label
5345916cd2Sjpk  *
5445916cd2Sjpk  * This is the library interface to the door call.
5545916cd2Sjpk  */
5645916cd2Sjpk 
5745916cd2Sjpk #define	clcall callp->param.acall.cargs.setfbcl_arg
5845916cd2Sjpk #define	clret callp->param.aret.rvals.setfbcl_ret
5945916cd2Sjpk /*
6045916cd2Sjpk  *
6145916cd2Sjpk  *	Exit	error = If error reported, the error indicator,
6245916cd2Sjpk  *				-1, Unable to access label encodings file;
6345916cd2Sjpk  *				 0, Invalid binary label passed;
6445916cd2Sjpk  *				>0, Position after the first character in
6545916cd2Sjpk  *				    string of error, 1 indicates entire string.
6645916cd2Sjpk  *			Otherwise, unchanged.
6745916cd2Sjpk  *
6845916cd2Sjpk  *	Returns	0, If error.
6945916cd2Sjpk  *		1, If successful.
7045916cd2Sjpk  *
7145916cd2Sjpk  *	Calls	__call_labeld(SETFLABEL)
7245916cd2Sjpk  *
7345916cd2Sjpk  */
7445916cd2Sjpk 
7545916cd2Sjpk int
setflabel(const char * path,m_label_t * label)7645916cd2Sjpk setflabel(const char *path, m_label_t *label)
7745916cd2Sjpk {
7845916cd2Sjpk 	labeld_data_t	call;
7945916cd2Sjpk 	labeld_data_t	*callp = &call;
8045916cd2Sjpk 	size_t	bufsize = sizeof (labeld_data_t);
8145916cd2Sjpk 	size_t	datasize;
8245916cd2Sjpk 	size_t	path_len;
8345916cd2Sjpk 	static char	cwd[MAXPATHLEN];
8445916cd2Sjpk 	char		canon[MAXPATHLEN];
8545916cd2Sjpk 
8645916cd2Sjpk 
8745916cd2Sjpk 	/*
8845916cd2Sjpk 	 * If path is relative and we haven't already determined the current
8945916cd2Sjpk 	 * working directory, do so now.  Calculating the working directory
9045916cd2Sjpk 	 * here lets us do the work once, instead of (potentially) repeatedly
9145916cd2Sjpk 	 * in realpath().
9245916cd2Sjpk 	 */
9345916cd2Sjpk 	if (*path != '/' && cwd[0] == '\0') {
9445916cd2Sjpk 		if (getcwd(cwd, MAXPATHLEN) == NULL) {
9545916cd2Sjpk 			cwd[0] = '\0';
9645916cd2Sjpk 			return (-1);
9745916cd2Sjpk 		}
9845916cd2Sjpk 	}
9945916cd2Sjpk 	/*
10045916cd2Sjpk 	 * Find an absolute pathname in the native file system name space that
10145916cd2Sjpk 	 * corresponds to path, stuffing it into canon.
10245916cd2Sjpk 	 */
10345916cd2Sjpk 	if (abspath(cwd, path, canon) < 0)
10445916cd2Sjpk 		return (-1);
10545916cd2Sjpk 
10645916cd2Sjpk 	path_len = strlen(canon) + 1;
10745916cd2Sjpk 
10845916cd2Sjpk 	datasize = CALL_SIZE(setfbcl_call_t, path_len - BUFSIZE);
10945916cd2Sjpk 	datasize += 2; /* PAD */
11045916cd2Sjpk 
11145916cd2Sjpk 	if (datasize > bufsize) {
11245916cd2Sjpk 		if ((callp = (labeld_data_t *)malloc(datasize)) == NULL) {
11345916cd2Sjpk 			return (-1);
11445916cd2Sjpk 		}
11545916cd2Sjpk 		bufsize = datasize;
11645916cd2Sjpk 	}
11745916cd2Sjpk 
11845916cd2Sjpk 	callp->callop = SETFLABEL;
11945916cd2Sjpk 
12045916cd2Sjpk 	clcall.sl = *label;
12145916cd2Sjpk 	(void) strcpy(clcall.pathname, canon);
12245916cd2Sjpk 
12345916cd2Sjpk 	if (__call_labeld(&callp, &bufsize, &datasize) == SUCCESS) {
12445916cd2Sjpk 		int err = callp->reterr;
12545916cd2Sjpk 
12645916cd2Sjpk 		if (callp != &call) {
12745916cd2Sjpk 			/* free allocated buffer */
12845916cd2Sjpk 			free(callp);
12945916cd2Sjpk 		}
13045916cd2Sjpk 		/*
13145916cd2Sjpk 		 * reterr == 0, OK,
13245916cd2Sjpk 		 * reterr < 0, invalid binary label,
13345916cd2Sjpk 		 */
13445916cd2Sjpk 		if (err == 0) {
13545916cd2Sjpk 			if (clret.status > 0) {
13645916cd2Sjpk 				errno = clret.status;
13745916cd2Sjpk 				return (-1);
13845916cd2Sjpk 			} else {
13945916cd2Sjpk 				return (0);
14045916cd2Sjpk 			}
14145916cd2Sjpk 		} else if (err < 0) {
14245916cd2Sjpk 			err = 0;
14345916cd2Sjpk 		}
14445916cd2Sjpk 		errno = ECONNREFUSED;
14545916cd2Sjpk 		return (-1);
14645916cd2Sjpk 	} else {
14745916cd2Sjpk 		if (callp != &call) {
14845916cd2Sjpk 			/* free allocated buffer */
14945916cd2Sjpk 			free(callp);
15045916cd2Sjpk 		}
15145916cd2Sjpk 		/* server not present */
15245916cd2Sjpk 		errno = ECONNREFUSED;
15345916cd2Sjpk 		return (-1);
15445916cd2Sjpk 	}
15545916cd2Sjpk }  /* setflabel */
15645916cd2Sjpk 
15745916cd2Sjpk #undef	clcall
15845916cd2Sjpk #undef	clret
15945916cd2Sjpk 
16045916cd2Sjpk #define	clcall callp->param.acall.cargs.zcopy_arg
16145916cd2Sjpk #define	clret callp->param.aret.rvals.zcopy_ret
16245916cd2Sjpk /*
16345916cd2Sjpk  *
16445916cd2Sjpk  *	Exit	status = result of zone copy request
16545916cd2Sjpk  *				-1, Copy not confirmed
16645916cd2Sjpk  *			Otherwise, unchanged.
16745916cd2Sjpk  *
16845916cd2Sjpk  *	Returns	0, If error.
16945916cd2Sjpk  *		1, If successful.
17045916cd2Sjpk  *
17145916cd2Sjpk  *	Calls	__call_labeld(ZCOPY)
17245916cd2Sjpk  *
17345916cd2Sjpk  */
17445916cd2Sjpk int
zonecopy(m_label_t * src_win_sl,char * remote_dir,char * filename,char * local_dir,int transfer_mode)17545916cd2Sjpk zonecopy(m_label_t *src_win_sl, char *remote_dir, char *filename,
17645916cd2Sjpk     char *local_dir, int  transfer_mode)
17745916cd2Sjpk {
17845916cd2Sjpk 	labeld_data_t	call;
17945916cd2Sjpk 	labeld_data_t	*callp = &call;
18045916cd2Sjpk 	size_t		bufsize = sizeof (labeld_data_t);
18145916cd2Sjpk 	size_t		datasize;
18245916cd2Sjpk 	size_t		strings;
18345916cd2Sjpk 	size_t		remote_dir_len;
18445916cd2Sjpk 	size_t		filename_len;
18545916cd2Sjpk 	size_t		local_dir_len;
18645916cd2Sjpk 	size_t		display_len;
18745916cd2Sjpk 	char		*display;
18845916cd2Sjpk 
18945916cd2Sjpk 	remote_dir_len = strlen(remote_dir) + 1;
19045916cd2Sjpk 	filename_len = strlen(filename) + 1;
19145916cd2Sjpk 	local_dir_len = strlen(local_dir) + 1;
19245916cd2Sjpk 
19345916cd2Sjpk 	if ((display = getenv("DISPLAY")) == NULL)
19445916cd2Sjpk 		display = "";
19545916cd2Sjpk 	display_len = strlen(display) + 1;
19645916cd2Sjpk 
19745916cd2Sjpk 	strings = remote_dir_len + filename_len + local_dir_len + display_len;
19845916cd2Sjpk 
19945916cd2Sjpk 	datasize = CALL_SIZE(zcopy_call_t, strings - BUFSIZE);
20045916cd2Sjpk 
20145916cd2Sjpk 	datasize += 4; /* PAD */
20245916cd2Sjpk 
20345916cd2Sjpk 	if (datasize > bufsize) {
20445916cd2Sjpk 		if ((callp = (labeld_data_t *)malloc(datasize)) == NULL) {
205*ce67cb24SToomas Soome 			return (0);
20645916cd2Sjpk 		}
20745916cd2Sjpk 		bufsize = datasize;
20845916cd2Sjpk 	}
20945916cd2Sjpk 
21045916cd2Sjpk 	strings = 0;
21145916cd2Sjpk 	callp->callop = ZCOPY;
21245916cd2Sjpk 
21345916cd2Sjpk 	clcall.src_win_sl = *src_win_sl;
21445916cd2Sjpk 	clcall.transfer_mode = transfer_mode;
21545916cd2Sjpk 	clcall.remote_dir = strings;
21645916cd2Sjpk 	strings += remote_dir_len;
21745916cd2Sjpk 	clcall.filename = strings;
21845916cd2Sjpk 	strings += filename_len;
21945916cd2Sjpk 	clcall.local_dir = strings;
22045916cd2Sjpk 	strings += local_dir_len;
22145916cd2Sjpk 	clcall.display = strings;
22245916cd2Sjpk 
22345916cd2Sjpk 	(void) strcpy(&clcall.buf[clcall.remote_dir], remote_dir);
22445916cd2Sjpk 	(void) strcpy(&clcall.buf[clcall.filename], filename);
22545916cd2Sjpk 	(void) strcpy(&clcall.buf[clcall.local_dir], local_dir);
22645916cd2Sjpk 	(void) strcpy(&clcall.buf[clcall.display], display);
22745916cd2Sjpk 
22845916cd2Sjpk 	if (__call_labeld(&callp, &bufsize, &datasize) == SUCCESS) {
22945916cd2Sjpk 		int err = callp->reterr;
23045916cd2Sjpk 
23145916cd2Sjpk 		if (callp != &call) {
23245916cd2Sjpk 			/* free allocated buffer */
23345916cd2Sjpk 			free(callp);
23445916cd2Sjpk 		}
23545916cd2Sjpk 		/*
23645916cd2Sjpk 		 * reterr == 0, OK,
23745916cd2Sjpk 		 * reterr < 0, transer not confirmed
23845916cd2Sjpk 		 */
23945916cd2Sjpk 		if (err == 0) {
24045916cd2Sjpk 			return (clret.status);
24145916cd2Sjpk 		} else if (err < 0) {
24245916cd2Sjpk 			err = 0;
24345916cd2Sjpk 		}
24445916cd2Sjpk 		return (PIPEMSG_CANCEL);
24545916cd2Sjpk 	} else {
24645916cd2Sjpk 		if (callp != &call) {
24745916cd2Sjpk 			/* free allocated buffer */
24845916cd2Sjpk 			free(callp);
24945916cd2Sjpk 		}
25045916cd2Sjpk 		/* server not present */
25145916cd2Sjpk 		return (PIPEMSG_CANCEL);
25245916cd2Sjpk 	}
25345916cd2Sjpk }
25445916cd2Sjpk 
25545916cd2Sjpk /*
25645916cd2Sjpk  * Convert the path given in raw to canonical, absolute, symlink-free
25745916cd2Sjpk  * form, storing the result in the buffer named by canon, which must be
25845916cd2Sjpk  * at least MAXPATHLEN bytes long.  If wd is non-NULL, assume that it
25945916cd2Sjpk  * points to a path for the current working directory and use it instead
26045916cd2Sjpk  * of invoking getcwd; accepting this value as an argument lets our caller
26145916cd2Sjpk  * cache the value, so that realpath (called from this routine) doesn't have
26245916cd2Sjpk  * to recalculate it each time it's given a relative pathname.
26345916cd2Sjpk  *
26445916cd2Sjpk  * Return 0 on success, -1 on failure.
26545916cd2Sjpk  */
26645916cd2Sjpk int
abspath(char * wd,const char * raw,char * canon)26745916cd2Sjpk abspath(char *wd, const char *raw, char *canon)
26845916cd2Sjpk {
26945916cd2Sjpk 	char		absbuf[MAXPATHLEN];
27045916cd2Sjpk 
27145916cd2Sjpk 	/*
27245916cd2Sjpk 	 * Preliminary sanity check.
27345916cd2Sjpk 	 */
27445916cd2Sjpk 	if (raw == NULL || canon == NULL)
27545916cd2Sjpk 		return (-1);
27645916cd2Sjpk 
27745916cd2Sjpk 	/*
27845916cd2Sjpk 	 * If the path is relative, convert it to absolute form,
27945916cd2Sjpk 	 * using wd if it's been supplied.
28045916cd2Sjpk 	 */
28145916cd2Sjpk 	if (raw[0] != '/') {
28245916cd2Sjpk 		char	*limit = absbuf + sizeof (absbuf);
28345916cd2Sjpk 		char	*d;
28445916cd2Sjpk 
28545916cd2Sjpk 		/* Fill in working directory. */
28645916cd2Sjpk 		if (wd != NULL)
28745916cd2Sjpk 			(void) strncpy(absbuf, wd, sizeof (absbuf));
28845916cd2Sjpk 		else if (getcwd(absbuf, strlen(absbuf)) == NULL)
28945916cd2Sjpk 			return (-1);
29045916cd2Sjpk 
29145916cd2Sjpk 		/* Add separating slash. */
29245916cd2Sjpk 		d = absbuf + strlen(absbuf);
29345916cd2Sjpk 		if (d < limit)
29445916cd2Sjpk 			*d++ = '/';
29545916cd2Sjpk 
29645916cd2Sjpk 		/* Glue on the relative part of the path. */
29745916cd2Sjpk 		while (d < limit && (*d++ = *raw++))
29845916cd2Sjpk 			continue;
29945916cd2Sjpk 
30045916cd2Sjpk 		raw = absbuf;
30145916cd2Sjpk 	}
30245916cd2Sjpk 
30345916cd2Sjpk 	/*
30445916cd2Sjpk 	 * Call realpath to canonicalize and resolve symlinks.
30545916cd2Sjpk 	 */
30645916cd2Sjpk 	return (realpath(raw, canon) == NULL ? -1 : 0);
30745916cd2Sjpk }
308