xref: /titanic_51/usr/src/cmd/pfexec/pfexec.c (revision 134a1f4e3289b54e0f980e9cf05352e419a60bee)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f48205beScasper  * Common Development and Distribution License (the "License").
6f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*134a1f4eSCasper H.S. Dik  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
25*134a1f4eSCasper H.S. Dik /*
26*134a1f4eSCasper H.S. Dik  * New implementation of pfexec(1) and all of the profile shells.
27*134a1f4eSCasper H.S. Dik  *
28*134a1f4eSCasper H.S. Dik  * The algorithm is as follows:
29*134a1f4eSCasper H.S. Dik  * 	first try to derive the shell's path from getexecname();
30*134a1f4eSCasper H.S. Dik  *	note that this requires a *hard* link to the program, so
31*134a1f4eSCasper H.S. Dik  *	if we find that we are actually executing pfexec, we start
32*134a1f4eSCasper H.S. Dik  *	looking at argv[0].
33*134a1f4eSCasper H.S. Dik  *	argv[0] is also our fallback in case getexecname doesn't find it.
34*134a1f4eSCasper H.S. Dik  */
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*134a1f4eSCasper H.S. Dik #include <alloca.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
38*134a1f4eSCasper H.S. Dik #include <locale.h>
397c478bd9Sstevel@tonic-gate #include <priv.h>
40*134a1f4eSCasper H.S. Dik #include <stdio.h>
41*134a1f4eSCasper H.S. Dik #include <stdlib.h>
42*134a1f4eSCasper H.S. Dik #include <string.h>
43*134a1f4eSCasper H.S. Dik #include <unistd.h>
447c478bd9Sstevel@tonic-gate 
45*134a1f4eSCasper H.S. Dik #define	PFEXEC	"pfexec"
46*134a1f4eSCasper H.S. Dik #ifndef TEXT_DOMAIN
477c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate 
50*134a1f4eSCasper H.S. Dik #define	RES_PFEXEC	1
51*134a1f4eSCasper H.S. Dik #define	RES_OK		0
52*134a1f4eSCasper H.S. Dik #define	RES_FAILURE	-1
537c478bd9Sstevel@tonic-gate 
54*134a1f4eSCasper H.S. Dik /*
55*134a1f4eSCasper H.S. Dik  * Return the shellname
56*134a1f4eSCasper H.S. Dik  */
57*134a1f4eSCasper H.S. Dik int
58*134a1f4eSCasper H.S. Dik shellname(const char *name, char buf[MAXPATHLEN])
59*134a1f4eSCasper H.S. Dik {
60*134a1f4eSCasper H.S. Dik 	const char *cmd = strrchr(name, '/');
617c478bd9Sstevel@tonic-gate 
62*134a1f4eSCasper H.S. Dik 	if (cmd == NULL)
63*134a1f4eSCasper H.S. Dik 		cmd = name;
64*134a1f4eSCasper H.S. Dik 	else
65*134a1f4eSCasper H.S. Dik 		cmd++;
667c478bd9Sstevel@tonic-gate 
67*134a1f4eSCasper H.S. Dik 	if (strncmp(cmd, "pf", 2) != 0)
68*134a1f4eSCasper H.S. Dik 		return (RES_FAILURE);
697c478bd9Sstevel@tonic-gate 
70*134a1f4eSCasper H.S. Dik 	if (strcmp(cmd, PFEXEC) == 0)
71*134a1f4eSCasper H.S. Dik 		return (RES_PFEXEC);
72*134a1f4eSCasper H.S. Dik 
73*134a1f4eSCasper H.S. Dik 	if (strlen(name) >= MAXPATHLEN)
74*134a1f4eSCasper H.S. Dik 		return (RES_FAILURE);
75*134a1f4eSCasper H.S. Dik 
76*134a1f4eSCasper H.S. Dik 	if (cmd == name) {
77*134a1f4eSCasper H.S. Dik 		(void) strlcpy(buf, cmd + 2, MAXPATHLEN);
78*134a1f4eSCasper H.S. Dik 	} else {
79*134a1f4eSCasper H.S. Dik 		(void) strncpy(buf, name, cmd - name);
80*134a1f4eSCasper H.S. Dik 		(void) strcpy(buf + (cmd - name), cmd + 2);
81*134a1f4eSCasper H.S. Dik 	}
82*134a1f4eSCasper H.S. Dik 	return (RES_OK);
83*134a1f4eSCasper H.S. Dik 
84*134a1f4eSCasper H.S. Dik }
85*134a1f4eSCasper H.S. Dik 
86*134a1f4eSCasper H.S. Dik static void
87*134a1f4eSCasper H.S. Dik usage(void)
88*134a1f4eSCasper H.S. Dik {
89*134a1f4eSCasper H.S. Dik 	(void) fprintf(stderr, gettext("pfexec [-P privset] cmd [arg ..]\n"));
90*134a1f4eSCasper H.S. Dik 	exit(EXIT_FAILURE);
91*134a1f4eSCasper H.S. Dik }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate int
94*134a1f4eSCasper H.S. Dik main(int argc, char **argv)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	char *cmd;
977c478bd9Sstevel@tonic-gate 	char *pset = NULL;
98*134a1f4eSCasper H.S. Dik 	char pathbuf[MAXPATHLEN];
99*134a1f4eSCasper H.S. Dik 	int c;
100*134a1f4eSCasper H.S. Dik 	priv_set_t *wanted;
101*134a1f4eSCasper H.S. Dik 	int oflag;
1027c478bd9Sstevel@tonic-gate 
103*134a1f4eSCasper H.S. Dik 	oflag = getpflags(PRIV_PFEXEC);
104*134a1f4eSCasper H.S. Dik 	if (setpflags(PRIV_PFEXEC, 1) != 0) {
105*134a1f4eSCasper H.S. Dik 		perror("setpflags(PRIV_PFEXEC)");
106*134a1f4eSCasper H.S. Dik 		exit(1);
107*134a1f4eSCasper H.S. Dik 	}
1087c478bd9Sstevel@tonic-gate 
109*134a1f4eSCasper H.S. Dik 	if (*argv[0] == '-')
110*134a1f4eSCasper H.S. Dik 		cmd = argv[0] + 1;
111*134a1f4eSCasper H.S. Dik 	else
112*134a1f4eSCasper H.S. Dik 		cmd = argv[0];
113*134a1f4eSCasper H.S. Dik 
114*134a1f4eSCasper H.S. Dik 	/* Strip "pf" from argv[0], it confuses some shells. */
115*134a1f4eSCasper H.S. Dik 	if (strncmp(cmd, "pf", 2) == 0) {
116*134a1f4eSCasper H.S. Dik 		argv[0] += 2;
117*134a1f4eSCasper H.S. Dik 		/* argv[0] will need to start with '-' again. */
118*134a1f4eSCasper H.S. Dik 		if (argv[0][-2] == '-')
119*134a1f4eSCasper H.S. Dik 			*argv[0] = '-';
120*134a1f4eSCasper H.S. Dik 	}
121*134a1f4eSCasper H.S. Dik 
122*134a1f4eSCasper H.S. Dik 	/* If this fails, we just continue with plan B */
123*134a1f4eSCasper H.S. Dik 	if (shellname(getexecname(), pathbuf) == RES_OK)
124*134a1f4eSCasper H.S. Dik 		(void) execv(pathbuf, argv);
125*134a1f4eSCasper H.S. Dik 
126*134a1f4eSCasper H.S. Dik 	switch (shellname(cmd, pathbuf)) {
127*134a1f4eSCasper H.S. Dik 	case RES_OK:
128*134a1f4eSCasper H.S. Dik 		(void) execv(pathbuf, argv);
129*134a1f4eSCasper H.S. Dik 		perror(pathbuf);
130*134a1f4eSCasper H.S. Dik 		return (1);
131*134a1f4eSCasper H.S. Dik 	case RES_PFEXEC:
132*134a1f4eSCasper H.S. Dik 	case RES_FAILURE:
1337c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "P:")) != EOF) {
1347c478bd9Sstevel@tonic-gate 			switch (c) {
1357c478bd9Sstevel@tonic-gate 			case 'P':
1367c478bd9Sstevel@tonic-gate 				if (pset == NULL) {
1377c478bd9Sstevel@tonic-gate 					pset = optarg;
1387c478bd9Sstevel@tonic-gate 					break;
1397c478bd9Sstevel@tonic-gate 				}
1407c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
1417c478bd9Sstevel@tonic-gate 			default:
1427c478bd9Sstevel@tonic-gate 				usage();
1437c478bd9Sstevel@tonic-gate 			}
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		argc -= optind;
1467c478bd9Sstevel@tonic-gate 		argv += optind;
1477c478bd9Sstevel@tonic-gate 		if (argc < 1)
1487c478bd9Sstevel@tonic-gate 			usage();
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		if (pset != NULL) {
151*134a1f4eSCasper H.S. Dik 			wanted = priv_str_to_set(pset, ",", NULL);
1527c478bd9Sstevel@tonic-gate 			if (setppriv(PRIV_ON, PRIV_INHERITABLE, wanted) != 0) {
1537c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1547c478bd9Sstevel@tonic-gate 				    gettext("setppriv(): %s\n"),
1557c478bd9Sstevel@tonic-gate 				    strerror(errno));
1567c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
1577c478bd9Sstevel@tonic-gate 			}
158*134a1f4eSCasper H.S. Dik 			(void) setpflags(PRIV_PFEXEC, oflag);
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 
161*134a1f4eSCasper H.S. Dik 		(void) execvp(argv[0], argv);
162*134a1f4eSCasper H.S. Dik 		perror(argv[0]);
1637c478bd9Sstevel@tonic-gate 		return (1);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	return (1);
1667c478bd9Sstevel@tonic-gate }
167