1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2014 Gary Mills
* Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* New implementation of pfexec(1) and all of the profile shells.
*
* The algorithm is as follows:
* first try to derive the shell's path from getexecname();
* note that this requires a *hard* link to the program, so
* if we find that we are actually executing pfexec, we start
* looking at argv[0].
* argv[0] is also our fallback in case getexecname doesn't find it.
*/
#include <sys/param.h>
#include <alloca.h>
#include <errno.h>
#include <locale.h>
#include <priv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define PFEXEC "pfexec"
#ifndef TEXT_DOMAIN
#define TEXT_DOMAIN "SYS_TEST"
#endif
#define RES_PFEXEC 1
#define RES_OK 0
#define RES_FAILURE -1
/*
* Return the shellname
*/
int
shellname(const char *name, char buf[MAXPATHLEN])
{
const char *cmd = strrchr(name, '/');
if (cmd == NULL)
cmd = name;
else
cmd++;
if (strncmp(cmd, "pf", 2) != 0)
return (RES_FAILURE);
if (strcmp(cmd, PFEXEC) == 0)
return (RES_PFEXEC);
if (strlen(name) >= MAXPATHLEN)
return (RES_FAILURE);
if (cmd == name) {
(void) strlcpy(buf, cmd + 2, MAXPATHLEN);
} else {
(void) strncpy(buf, name, cmd - name);
(void) strcpy(buf + (cmd - name), cmd + 2);
}
return (RES_OK);
}
static void
usage(void)
{
(void) fprintf(stderr, gettext("pfexec [-P privset] cmd [arg ..]\n"));
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
char *cmd;
char *pset = NULL;
char pathbuf[MAXPATHLEN];
int c;
priv_set_t *wanted;
int oflag;
oflag = getpflags(PRIV_PFEXEC);
if (setpflags(PRIV_PFEXEC, 1) != 0) {
(void) fprintf(stderr,
gettext("pfexec: unable to set PFEXEC flag: %s\n"),
strerror(errno));
exit(1);
}
if (*argv[0] == '-')
cmd = argv[0] + 1;
else
cmd = argv[0];
/* Strip "pf" from argv[0], it confuses some shells. */
if (strncmp(cmd, "pf", 2) == 0) {
argv[0] += 2;
/* argv[0] will need to start with '-' again. */
if (argv[0][-2] == '-')
*argv[0] = '-';
}
/* If this fails, we just continue with plan B */
if (shellname(getexecname(), pathbuf) == RES_OK)
(void) execv(pathbuf, argv);
switch (shellname(cmd, pathbuf)) {
case RES_OK:
(void) execv(pathbuf, argv);
(void) fprintf(stderr,
gettext("pfexec: unable to execute %s: %s\n"),
pathbuf, strerror(errno));
return (1);
case RES_PFEXEC:
case RES_FAILURE:
while ((c = getopt(argc, argv, "P:")) != EOF) {
switch (c) {
case 'P':
if (pset == NULL) {
pset = optarg;
break;
}
/* FALLTHROUGH */
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage();
if (pset != NULL) {
if ((wanted = priv_str_to_set(pset, ",", NULL)) ==
NULL) {
(void) fprintf(stderr,
gettext("pfexec: error parsing "
"privileges: %s\n"), strerror(errno));
exit(EXIT_FAILURE);
}
if (setppriv(PRIV_ON, PRIV_INHERITABLE, wanted) != 0) {
(void) fprintf(stderr,
gettext("pfexec: error setting "
"privileges: %s\n"), strerror(errno));
exit(EXIT_FAILURE);
}
(void) setpflags(PRIV_PFEXEC, oflag);
}
(void) execvp(argv[0], argv);
(void) fprintf(stderr,
gettext("pfexec: unable to execute %s: %s\n"),
argv[0], strerror(errno));
return (1);
}
return (1);
}
|