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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/*
* 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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h> /* creat() declaration */
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <locale.h>
#include <libintl.h>
#include <pkglib.h>
#include "install.h"
#include "libadm.h"
#include "libinst.h"
#include "pkginstall.h"
#include "messages.h"
extern char tmpdir[], instdir[];
extern int pkgverbose;
static int do_exec(int update, char *script, char *output,
char *inport, char *alt_user);
static char path[PATH_MAX];
static char *resppath = NULL;
static int fd;
static int respfile_defined = 0;
static int respfile_ro = 0; /* read only resp file */
/*
* This informs the calling routine if a read-only response file has been
* provided on the command line.
*/
int
rdonly_respfile(void)
{
return (respfile_ro);
}
int
is_a_respfile(void)
{
return (respfile_defined);
}
/*
* This function creates a working copy of the checkinstall script.
* This is needed in situations where the packages parent directories modes
* are set too restrictively, i.e. 700.
*
* Returns: A pointer to the location of the copied checkinstall
* script or NULL
*/
char *
dup_chkinstall(char *script)
{
char *dstpath;
size_t dstpathLen;
int r;
static char *tmpname = "checkinstallXXXXXX";
/* determine length for destination script path */
dstpathLen = strlen(tmpdir) + strlen(tmpname) + 3;
/* allocate storage to hold destination script path */
dstpath = (char *)malloc(dstpathLen);
if (dstpath == (char *)NULL) {
return ((char *)NULL);
}
/* create destination script path */
(void) snprintf(dstpath, dstpathLen, "%s/%s", tmpdir, tmpname);
if (mktemp(dstpath) == NULL) {
progerr(ERR_TMPFILE_CHK);
(void) free(dstpath);
return (NULL);
}
/* make copy of script */
r = copyf(script, dstpath, (time_t)0);
if (r != 0) {
progerr(ERR_CANNOT_COPY, script, dstpath);
return (NULL);
}
/* Make the copy of the script readable by all */
if (chmod(dstpath, 0444) != 0) {
progerr(ERR_CHMOD_CHK);
(void) free(dstpath);
return (NULL);
}
return (dstpath);
}
/*
* This function creates a temporary working copy of a read-only response
* file. It changes the resppath pointer to point to the working copy.
*/
static int
dup_respfile(void)
{
char tpath[PATH_MAX];
int r;
(void) strlcpy(tpath, path, sizeof (tpath));
(void) snprintf(path, sizeof (path), "%s/respXXXXXX", tmpdir);
resppath = mktemp(path);
if (resppath == NULL) {
progerr(ERR_TMPRESP);
return (99);
}
/* Copy the contents of the user's response file to the working copy. */
r = copyf(tpath, resppath, (time_t)0);
if (r != 0) {
progerr(ERR_NORESPCOPY, tpath, resppath);
return (99);
}
/*
* Make it writable by the non-privileged installation user-id,
* but readable by the world.
*/
if (chmod(resppath, 0644) != 0) {
progerr(ERR_CHMOD, resppath);
return (99);
}
respfile_ro = 0;
return (0);
}
/*
* This function establishes the response file passed on the command line if
* it's called with a valid string. If called with NULL, it checks to see if
* there's a response file already. If there isn't, it creates a temporary.
*/
int
set_respfile(char *respfile, char *pkginst, int resp_stat)
{
if (respfile == NULL && !respfile_defined) {
/* A temporary response file needs to be constructed. */
(void) snprintf(path, sizeof (path), "%s/respXXXXXX", tmpdir);
resppath = mktemp(path);
if (resppath == NULL) {
progerr(ERR_TMPRESP);
return (99);
}
} else {
/* OK, we're being passed a response file or directory. */
if (isdir(respfile) == 0) {
(void) snprintf(path, sizeof (path),
"%s/%s", respfile, pkginst);
} else {
(void) strlcpy(path, respfile, sizeof (path));
}
resppath = path;
respfile_ro = resp_stat;
}
respfile_defined++;
return (0);
}
/* This exposes the working response file. */
char *
get_respfile(void)
{
return (resppath);
}
/*
* Execute the request script if present assuming the response file
* isn't read only.
*/
int
reqexec(int update, char *script, int non_abi_scripts,
boolean_t enable_root_user)
{
char *req_user;
/*
* determine which alternative user to execute the request script as
* if the default user "install" is not defined.
*/
if (enable_root_user == B_TRUE) {
/* use the root user */
req_user = CHK_USER_ROOT;
} else if (non_abi_scripts != 0) {
/* non-compliant package user */
req_user = CHK_USER_NON;
} else {
/* standard non-privileged user */
req_user = CHK_USER_ALT;
}
/*
* If we can't get to the the script or the response file, skip this.
*/
if (access(script, F_OK) != 0 || respfile_ro)
return (0);
/* No interact means no interact. */
if (echoGetFlag() == B_FALSE) {
ptext(stderr, ERR_INTR);
return (5);
}
/* If there's no response file, create one. */
if (!respfile_defined)
if (set_respfile(NULL, NULL, 0))
return (99);
/* Clear out the old response file (if there is one). */
if ((access(resppath, F_OK) == 0) && unlink(resppath)) {
progerr(ERR_RMRESP, resppath);
return (99);
}
/*
* Create a zero length response file which is only writable
* by the non-privileged installation user-id, but is readable
* by the world
*/
if ((fd = open(resppath, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644)) < 0) {
progerr(ERR_CRERESP, resppath);
return (99);
}
(void) close(fd);
return (do_exec(update, script, resppath, REQ_STDIN, req_user));
}
int
chkexec(int update, char *script)
{
/*
* If we're up against a read-only response file from the command
* line. Create a working copy.
*/
if (respfile_ro) {
if (dup_respfile())
return (99);
/* Make sure we can get to it. */
if ((access(resppath, F_OK) != 0)) {
progerr(ERR_ACCRESP, resppath);
return (7);
}
}
/* If there's no response file, create a fresh one. */
else if (!respfile_defined) {
if (set_respfile(NULL, NULL, 0))
return (99);
/*
* create a zero length response file which is only writable
* by the non-priveledged installation user-id, but is readable
* by the world
*/
fd = open(resppath, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
if (fd < 0) {
progerr(ERR_CRERESP, resppath);
return (99);
}
(void) close(fd);
}
return (do_exec(update, script, resppath, CHK_STDIN, CHK_USER_ALT));
}
static int
do_exec(int update, char *script, char *output, char *inport, char *alt_user)
{
char *gname;
char *tmp_script;
char *uname;
gid_t instgid;
int retcode = 0;
struct group *grp;
struct passwd *pwp;
uid_t instuid;
/*
* Determine which user to run the request script as:
* - if CHK_USER is a valid user, run the script as CHK_USER
* - otherwise, if alt_user is a valid user, run the script
* -- as alt_user
* - otherwise, output an error message and return failure
*/
if ((pwp = getpwnam(CHK_USER)) != (struct passwd *)NULL) {
instuid = pwp->pw_uid;
uname = CHK_USER;
} else if ((pwp = getpwnam(alt_user)) != (struct passwd *)NULL) {
instuid = pwp->pw_uid;
uname = alt_user;
} else {
ptext(stderr, ERR_BADUSER, CHK_USER, CHK_USER_ALT);
return (1);
}
/*
* Determine which group to run the request script as:
* - If CHK_GRP is a valid group, run the script as CHK_GRP
* - otherwise, assume group "1" user "other"
*/
if ((grp = getgrnam(CHK_GRP)) != (struct group *)NULL) {
instgid = grp->gr_gid;
gname = CHK_GRP;
} else {
instgid = (gid_t)1; /* "other" group id */
gname = "other"; /* "other" group name */
}
echoDebug(DBG_DO_EXEC_REQUEST_USER, script, output, uname, instuid,
gname, instgid);
(void) chown(output, instuid, instgid);
/*
* Copy the checkinstall script to tmpdir in case parent directories
* are restrictive, i.e. 700. Only do this for non updates, i.e.
* package installs and not patch package installs.
*/
if (update) {
tmp_script = strdup(script);
} else if ((tmp_script = dup_chkinstall(script)) == NULL) {
/* Use the original checkinstall script */
tmp_script = strdup(script);
}
if (pkgverbose)
retcode = pkgexecl(inport, CHK_STDOUT, uname, CHK_GRP, SHELL,
"-x", tmp_script, output, NULL);
else
retcode = pkgexecl(inport, CHK_STDOUT, uname, CHK_GRP, SHELL,
tmp_script, output, NULL);
free(tmp_script);
return (retcode);
}
|