xref: /titanic_41/usr/src/cmd/lp/lib/lp/tx.c (revision 10db1377dafab8ba3feedef26db9c5d8539a5cd1)
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 2006 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 #include <sys/types.h>
30 #include <sys/zone.h>
31 #include <syslog.h>
32 #include <strings.h>
33 
34 #include <ucred.h>
35 #include "tsol/label.h"
36 /* lpsched include files */
37 #if defined PS_FAULTED
38 #undef  PS_FAULTED
39 #endif /* PS_FAULTED */
40 #include "lp.h"
41 
42 /*
43  * get_labeled_zonename - gets the the zonename with the same label.
44  *
45  *	Input:
46  *		slabel - USER_CLEAR label to match
47  *
48  *	Output:
49  *		-1 - zonename with that label could not be found
50  *			or no memory for zonename
51  *		 0 - label was GLOBAL_ZONENAME
52  *		 addr - zonename of zone matching USER_CLEAR label
53  *			must be retuened by calling Free(addr)
54  *
55  */
56 
57 char *
58 get_labeled_zonename(char *slabel)
59 {
60 	m_label_t	*bsl = NULL;
61 	int	err = 0;
62 	ssize_t	zonename_size = -1;
63 	zoneid_t	zid = -1;
64 	char *zname = NULL;
65 
66 	syslog(LOG_DEBUG, "lpsched: get_labeled_zonename %s", slabel);
67 	/*
68 	 * convert the label to binary.
69 	 */
70 	if (str_to_label(slabel, &bsl, USER_CLEAR,
71 	    L_NO_CORRECTION, &err) == -1) {
72 		/* label could not be converted, error */
73 		syslog(LOG_WARNING,
74 		    "lpsched: %s: label not recognized (error==%d)",
75 		    slabel, err);
76 		return ((char *)-1);
77 	}
78 	if ((zid = getzoneidbylabel(bsl)) < 0) {
79 		/* no zone with that label, cannot send mail */
80 		syslog(LOG_WARNING,
81 		    "lpsched: cannot send mail, no zone with %s label",
82 		    slabel);
83 		m_label_free(bsl);
84 		return ((char *)-1);
85 	}
86 	zname = Malloc(ZONENAME_MAX + 1);
87 	if ((zonename_size = getzonenamebyid(zid, zname, ZONENAME_MAX + 1))
88 	    == -1) {
89 		/* cannot get zone name, cannot send mail */
90 		syslog(LOG_WARNING,
91 		    "lpsched: cannot send mail, no zone name for %s",
92 		    slabel);
93 		m_label_free(bsl);
94 		Free(zname);
95 		return ((char *)-1);
96 	} else {
97 		m_label_free(bsl);
98 		if (strcmp(zname, GLOBAL_ZONENAME) == 0) {
99 			Free(zname);
100 			zname = NULL;
101 		}
102 	}
103 	return (zname);
104 }
105 
106 int
107 get_peer_label(int fd, char **slabel)
108 {
109 	if (is_system_labeled()) {
110 		ucred_t *uc = NULL;
111 		m_label_t *sl;
112 		char *pslabel = NULL; /* peer's slabel */
113 
114 		if ((fd < 0) || (slabel == NULL)) {
115 			errno = EINVAL;
116 			return (-1);
117 		}
118 
119 		if (getpeerucred(fd, &uc) == -1)
120 			return (-1);
121 
122 		sl = ucred_getlabel(uc);
123 		if (label_to_str(sl, &pslabel, M_INTERNAL, DEF_NAMES) != 0)
124 			syslog(LOG_WARNING, "label_to_str(): %m");
125 		ucred_free(uc);
126 
127 		if (pslabel != NULL) {
128 			syslog(LOG_DEBUG, "get_peer_label(%d, %s): becomes %s",
129 				fd, (*slabel ? *slabel : "NULL"), pslabel);
130 			if (*slabel != NULL)
131 				free(*slabel);
132 			*slabel = strdup(pslabel);
133 		}
134 	}
135 
136 	return (0);
137 }
138