xref: /onnv-gate/usr/src/cmd/tsol/plabel/plabel.c (revision 4746:0bc0c48f4304)
1*4746Srica /*
2*4746Srica  * CDDL HEADER START
3*4746Srica  *
4*4746Srica  * The contents of this file are subject to the terms of the
5*4746Srica  * Common Development and Distribution License (the "License").
6*4746Srica  * You may not use this file except in compliance with the License.
7*4746Srica  *
8*4746Srica  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*4746Srica  * or http://www.opensolaris.org/os/licensing.
10*4746Srica  * See the License for the specific language governing permissions
11*4746Srica  * and limitations under the License.
12*4746Srica  *
13*4746Srica  * When distributing Covered Code, include this CDDL HEADER in each
14*4746Srica  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*4746Srica  * If applicable, add the following below this CDDL HEADER, with the
16*4746Srica  * fields enclosed by brackets "[]" replaced with your own identifying
17*4746Srica  * information: Portions Copyright [yyyy] [name of copyright owner]
18*4746Srica  *
19*4746Srica  * CDDL HEADER END
20*4746Srica  */
21*4746Srica 
22*4746Srica /*
23*4746Srica  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*4746Srica  * Use is subject to license terms.
25*4746Srica  */
26*4746Srica 
27*4746Srica #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*4746Srica 
29*4746Srica /*
30*4746Srica  *	plabel - gets process label.
31*4746Srica  */
32*4746Srica #include <stdio.h>
33*4746Srica #include <stdlib.h>
34*4746Srica #include <errno.h>
35*4746Srica #include <unistd.h>
36*4746Srica #include <fcntl.h>
37*4746Srica #include <string.h>
38*4746Srica #include <locale.h>
39*4746Srica #include <procfs.h>
40*4746Srica #include <sys/proc.h>
41*4746Srica #include <zone.h>
42*4746Srica 
43*4746Srica #include <sys/tsol/label_macro.h>
44*4746Srica 
45*4746Srica #include <tsol/label.h>
46*4746Srica 
47*4746Srica #define	s_flag	0x04
48*4746Srica #define	S_flag	0x08
49*4746Srica 
50*4746Srica #define	INIT_ALLOC_LEN	1024
51*4746Srica #define	MAX_ALLOC_NUM	11
52*4746Srica 
53*4746Srica static int look(char *);
54*4746Srica static int perr(char *);
55*4746Srica static void usage(void);
56*4746Srica 
57*4746Srica static char procname[64];
58*4746Srica 
59*4746Srica static unsigned int opt_flag = 0;
60*4746Srica static char *cmd = NULL;
61*4746Srica 
62*4746Srica int
main(int argc,char ** argv)63*4746Srica main(int argc, char **argv)
64*4746Srica {
65*4746Srica 	int err, rc = 0;
66*4746Srica 	int opt;
67*4746Srica 
68*4746Srica 	(void) setlocale(LC_ALL, "");
69*4746Srica #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
70*4746Srica #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
71*4746Srica #endif
72*4746Srica 	(void) textdomain(TEXT_DOMAIN);
73*4746Srica 
74*4746Srica 	if ((cmd = strrchr(argv[0], '/')) == NULL)
75*4746Srica 		cmd = argv[0];
76*4746Srica 	else
77*4746Srica 		cmd++;
78*4746Srica 
79*4746Srica 	/* Error if labeling is not active. */
80*4746Srica 	if (!is_system_labeled()) {
81*4746Srica 		(void) fprintf(stderr,
82*4746Srica 		    gettext("%s: Trusted Extensions must be enabled\n"), cmd);
83*4746Srica 		return (1);
84*4746Srica 	}
85*4746Srica 
86*4746Srica 	while ((opt = getopt(argc, argv, "sS")) != EOF) {
87*4746Srica 		switch (opt) {
88*4746Srica 		case 's':
89*4746Srica 			if (opt_flag & (s_flag | S_flag)) {
90*4746Srica 				usage();
91*4746Srica 				return (1);
92*4746Srica 			}
93*4746Srica 			opt_flag |= s_flag;
94*4746Srica 			break;
95*4746Srica 
96*4746Srica 		case 'S':
97*4746Srica 			if (opt_flag & (s_flag | S_flag)) {
98*4746Srica 				usage();
99*4746Srica 				return (1);
100*4746Srica 			}
101*4746Srica 			opt_flag |= S_flag;
102*4746Srica 			break;
103*4746Srica 		default:
104*4746Srica 			usage();
105*4746Srica 			return (1);
106*4746Srica 		}
107*4746Srica 	}
108*4746Srica 
109*4746Srica 	argc -= optind;
110*4746Srica 	argv += optind;
111*4746Srica 	if (argc == 0) {
112*4746Srica 		char pid[11]; /* 32 bit pids go to 4294967295 plus a NUL */
113*4746Srica 
114*4746Srica 		(void) sprintf(pid, "%d", (int)getpid());
115*4746Srica 		rc = look(pid);
116*4746Srica 	} else {
117*4746Srica 		while (argc-- > 0) {
118*4746Srica 			err = look(*argv++);
119*4746Srica 			if (rc == 0)
120*4746Srica 				rc = err;
121*4746Srica 		}
122*4746Srica 	}
123*4746Srica 	return (rc);
124*4746Srica }
125*4746Srica 
126*4746Srica static int
look(char * arg)127*4746Srica look(char *arg)
128*4746Srica {
129*4746Srica 	int fd;
130*4746Srica 	m_label_t *plabel;
131*4746Srica 	psinfo_t info;		/* process information from /proc */
132*4746Srica 	char *str;
133*4746Srica 	int wordlen = DEF_NAMES;
134*4746Srica 
135*4746Srica 	if (opt_flag == S_flag)
136*4746Srica 		wordlen = LONG_NAMES;
137*4746Srica 	else if (opt_flag == s_flag)
138*4746Srica 		wordlen = SHORT_NAMES;
139*4746Srica 
140*4746Srica 	if (strchr(arg, '/') != NULL)
141*4746Srica 		(void) strncpy(procname, arg, sizeof (procname));
142*4746Srica 	else {
143*4746Srica 		(void) strcpy(procname, "/proc/");
144*4746Srica 		(void) strncat(procname, arg,
145*4746Srica 		    sizeof (procname) - strlen(procname));
146*4746Srica 	}
147*4746Srica 	(void) strlcat(procname, "/psinfo", sizeof (procname)
148*4746Srica 	    - strlen(procname));
149*4746Srica 
150*4746Srica 	/*
151*4746Srica 	 * Open the process to be examined.
152*4746Srica 	 */
153*4746Srica retry:
154*4746Srica 	if ((fd = open(procname, O_RDONLY)) < 0) {
155*4746Srica 		/*
156*4746Srica 		 * Make clean message for non-existent process.
157*4746Srica 		 */
158*4746Srica 		if (errno == ENOENT) {
159*4746Srica 			errno = ESRCH;
160*4746Srica 			perror(arg);
161*4746Srica 			return (1);
162*4746Srica 		}
163*4746Srica 		return (perr(NULL));
164*4746Srica 	}
165*4746Srica 
166*4746Srica 
167*4746Srica 	/*
168*4746Srica 	 * Get the info structure for the process and close quickly.
169*4746Srica 	 */
170*4746Srica 	if (read(fd, &info, sizeof (info)) < 0) {
171*4746Srica 		int	saverr = errno;
172*4746Srica 
173*4746Srica 		(void) close(fd);
174*4746Srica 		if (saverr == EAGAIN)
175*4746Srica 			goto retry;
176*4746Srica 		if (saverr != ENOENT)
177*4746Srica 			perror(arg);
178*4746Srica 		return (1);
179*4746Srica 	}
180*4746Srica 	(void) close(fd);
181*4746Srica 
182*4746Srica 	if (info.pr_lwp.pr_state == 0)  /* can't happen? */
183*4746Srica 		return (1);
184*4746Srica 
185*4746Srica 	if ((plabel = getzonelabelbyid(info.pr_zoneid)) == NULL) {
186*4746Srica 		return (1);
187*4746Srica 	}
188*4746Srica 
189*4746Srica 	/*
190*4746Srica 	 * The process label for global zone is admin_high
191*4746Srica 	 */
192*4746Srica 	if (info.pr_zoneid == GLOBAL_ZONEID) {
193*4746Srica 		_BSLHIGH(plabel);
194*4746Srica 	}
195*4746Srica 
196*4746Srica 	if (label_to_str(plabel, &str, M_LABEL, wordlen) != 0) {
197*4746Srica 		perror(arg);
198*4746Srica 		return (2);
199*4746Srica 	}
200*4746Srica 	(void) printf("%s\n", str);
201*4746Srica 	m_label_free(plabel);
202*4746Srica 	free(str);
203*4746Srica 	return (0);
204*4746Srica }
205*4746Srica 
206*4746Srica 
207*4746Srica /*
208*4746Srica  * usage()
209*4746Srica  *
210*4746Srica  * This routine is called whenever there is a usage type of error has
211*4746Srica  * occured.  For example, when a invalid option has has been specified.
212*4746Srica  *
213*4746Srica  */
214*4746Srica static void
usage(void)215*4746Srica usage(void)
216*4746Srica {
217*4746Srica 
218*4746Srica 	(void) fprintf(stderr, "Usage: \n");
219*4746Srica 	(void) fprintf(stderr,
220*4746Srica 	    gettext("	%s [pid ...]    \n"), cmd);
221*4746Srica 	(void) fprintf(stderr,
222*4746Srica 	    gettext("	%s -s  [pid ...] \n"), cmd);
223*4746Srica 	(void) fprintf(stderr,
224*4746Srica 	    gettext("	%s -S  [pid ...] \n"), cmd);
225*4746Srica }
226*4746Srica 
227*4746Srica static int
perr(char * s)228*4746Srica perr(char *s) {
229*4746Srica 
230*4746Srica 	if (s)
231*4746Srica 		(void) fprintf(stderr, "%s: ", procname);
232*4746Srica 	else
233*4746Srica 		s = procname;
234*4746Srica 	perror(s);
235*4746Srica 	return (1);
236*4746Srica }
237