xref: /titanic_41/usr/src/cmd/lp/lib/lp/tx.c (revision 3eb7f67186d33bd2e65b4d12d5c6134da44658df)
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 /*
23*3eb7f671SThuy Fettig  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2445916cd2Sjpk  */
2545916cd2Sjpk 
2645916cd2Sjpk #include <sys/types.h>
2745916cd2Sjpk #include <sys/zone.h>
2845916cd2Sjpk #include <syslog.h>
2945916cd2Sjpk #include <strings.h>
3045916cd2Sjpk 
3145916cd2Sjpk #include <ucred.h>
3245916cd2Sjpk #include "tsol/label.h"
3345916cd2Sjpk /* lpsched include files */
3445916cd2Sjpk #if defined PS_FAULTED
3545916cd2Sjpk #undef  PS_FAULTED
3645916cd2Sjpk #endif /* PS_FAULTED */
3745916cd2Sjpk #include "lp.h"
38*3eb7f671SThuy Fettig #include <sys/tsol/label_macro.h>
3945916cd2Sjpk 
4045916cd2Sjpk /*
4145916cd2Sjpk  * get_labeled_zonename - gets the the zonename with the same label.
4245916cd2Sjpk  *
4345916cd2Sjpk  *	Input:
4445916cd2Sjpk  *		slabel - USER_CLEAR label to match
4545916cd2Sjpk  *
4645916cd2Sjpk  *	Output:
4745916cd2Sjpk  *		-1 - zonename with that label could not be found
4845916cd2Sjpk  *			or no memory for zonename
4945916cd2Sjpk  *		 0 - label was GLOBAL_ZONENAME
5045916cd2Sjpk  *		 addr - zonename of zone matching USER_CLEAR label
5145916cd2Sjpk  *			must be retuened by calling Free(addr)
5245916cd2Sjpk  *
5345916cd2Sjpk  */
5445916cd2Sjpk 
5545916cd2Sjpk char *
get_labeled_zonename(char * slabel)5645916cd2Sjpk get_labeled_zonename(char *slabel)
5745916cd2Sjpk {
5845916cd2Sjpk 	m_label_t	*bsl = NULL;
5945916cd2Sjpk 	int	err = 0;
6045916cd2Sjpk 	ssize_t	zonename_size = -1;
6145916cd2Sjpk 	zoneid_t	zid = -1;
6245916cd2Sjpk 	char *zname = NULL;
6345916cd2Sjpk 
6445916cd2Sjpk 	syslog(LOG_DEBUG, "lpsched: get_labeled_zonename %s", slabel);
6545916cd2Sjpk 	/*
6645916cd2Sjpk 	 * convert the label to binary.
6745916cd2Sjpk 	 */
6845916cd2Sjpk 	if (str_to_label(slabel, &bsl, USER_CLEAR,
6945916cd2Sjpk 	    L_NO_CORRECTION, &err) == -1) {
7045916cd2Sjpk 		/* label could not be converted, error */
7145916cd2Sjpk 		syslog(LOG_WARNING,
7245916cd2Sjpk 		    "lpsched: %s: label not recognized (error==%d)",
7345916cd2Sjpk 		    slabel, err);
7445916cd2Sjpk 		return ((char *)-1);
7545916cd2Sjpk 	}
7645916cd2Sjpk 	if ((zid = getzoneidbylabel(bsl)) < 0) {
7745916cd2Sjpk 		/* no zone with that label, cannot send mail */
7845916cd2Sjpk 		syslog(LOG_WARNING,
7945916cd2Sjpk 		    "lpsched: cannot send mail, no zone with %s label",
8045916cd2Sjpk 		    slabel);
8145916cd2Sjpk 		m_label_free(bsl);
8245916cd2Sjpk 		return ((char *)-1);
8345916cd2Sjpk 	}
8445916cd2Sjpk 	zname = Malloc(ZONENAME_MAX + 1);
8545916cd2Sjpk 	if ((zonename_size = getzonenamebyid(zid, zname, ZONENAME_MAX + 1))
8645916cd2Sjpk 	    == -1) {
8745916cd2Sjpk 		/* cannot get zone name, cannot send mail */
8845916cd2Sjpk 		syslog(LOG_WARNING,
8945916cd2Sjpk 		    "lpsched: cannot send mail, no zone name for %s",
9045916cd2Sjpk 		    slabel);
9145916cd2Sjpk 		m_label_free(bsl);
9245916cd2Sjpk 		Free(zname);
9345916cd2Sjpk 		return ((char *)-1);
9445916cd2Sjpk 	} else {
9545916cd2Sjpk 		m_label_free(bsl);
9645916cd2Sjpk 		if (strcmp(zname, GLOBAL_ZONENAME) == 0) {
9745916cd2Sjpk 			Free(zname);
9845916cd2Sjpk 			zname = NULL;
9945916cd2Sjpk 		}
10045916cd2Sjpk 	}
10145916cd2Sjpk 	return (zname);
10245916cd2Sjpk }
10345916cd2Sjpk 
10445916cd2Sjpk int
get_peer_label(int fd,char ** slabel)10545916cd2Sjpk get_peer_label(int fd, char **slabel)
10645916cd2Sjpk {
10745916cd2Sjpk 	if (is_system_labeled()) {
10845916cd2Sjpk 		ucred_t *uc = NULL;
10945916cd2Sjpk 		m_label_t *sl;
110*3eb7f671SThuy Fettig 		m_label_t admin_low;
111*3eb7f671SThuy Fettig 		m_label_t admin_high;
11245916cd2Sjpk 		char *pslabel = NULL; /* peer's slabel */
11345916cd2Sjpk 
11445916cd2Sjpk 		if ((fd < 0) || (slabel == NULL)) {
11545916cd2Sjpk 			errno = EINVAL;
11645916cd2Sjpk 			return (-1);
11745916cd2Sjpk 		}
118*3eb7f671SThuy Fettig 		bsllow(&admin_low);
119*3eb7f671SThuy Fettig 		bslhigh(&admin_high);
12045916cd2Sjpk 
12145916cd2Sjpk 		if (getpeerucred(fd, &uc) == -1)
12245916cd2Sjpk 			return (-1);
12345916cd2Sjpk 
12445916cd2Sjpk 		sl = ucred_getlabel(uc);
125*3eb7f671SThuy Fettig 
126*3eb7f671SThuy Fettig 		/*
127*3eb7f671SThuy Fettig 		 * Remote print requests from the global zone
128*3eb7f671SThuy Fettig 		 * arrive at admin_low, make them admin_high to
129*3eb7f671SThuy Fettig 		 * avoid downgrade.
130*3eb7f671SThuy Fettig 		 */
131*3eb7f671SThuy Fettig 		if (blequal(sl, &admin_low)) {
132*3eb7f671SThuy Fettig 			sl = &admin_high;
133*3eb7f671SThuy Fettig 			syslog(LOG_DEBUG, "get_peer_label(): upgrade"
134*3eb7f671SThuy Fettig 			    " admin_low label to admin_high");
135*3eb7f671SThuy Fettig 		}
136*3eb7f671SThuy Fettig 
13745916cd2Sjpk 		if (label_to_str(sl, &pslabel, M_INTERNAL, DEF_NAMES) != 0)
13845916cd2Sjpk 			syslog(LOG_WARNING, "label_to_str(): %m");
13945916cd2Sjpk 		ucred_free(uc);
14045916cd2Sjpk 
14145916cd2Sjpk 		if (pslabel != NULL) {
14245916cd2Sjpk 			syslog(LOG_DEBUG, "get_peer_label(%d, %s): becomes %s",
14345916cd2Sjpk 			    fd, (*slabel ? *slabel : "NULL"), pslabel);
14445916cd2Sjpk 			if (*slabel != NULL)
14545916cd2Sjpk 				free(*slabel);
14645916cd2Sjpk 			*slabel = strdup(pslabel);
14745916cd2Sjpk 		}
14845916cd2Sjpk 	}
14945916cd2Sjpk 
15045916cd2Sjpk 	return (0);
15145916cd2Sjpk }
152