5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER START
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * The contents of this file are subject to the terms of the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Common Development and Distribution License (the "License").
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You may not use this file except in compliance with the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * or http://www.opensolaris.org/os/licensing.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * See the License for the specific language governing permissions
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and limitations under the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If applicable, add the following below this CDDL HEADER, with the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER END
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Use is subject to license terms.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* All Rights Reserved */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#include <fcntl.h> /* creat() declaration */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int do_exec(int update, char *script, char *output,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int respfile_ro = 0; /* read only resp file */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This informs the calling routine if a read-only response file has been
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * provided on the command line.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This function creates a working copy of the checkinstall script.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This is needed in situations where the packages parent directories modes
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * are set too restrictively, i.e. 700.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Returns: A pointer to the location of the copied checkinstall
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * script or NULL
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic char *tmpname = "checkinstallXXXXXX";
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* determine length for destination script path */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland dstpathLen = strlen(tmpdir) + strlen(tmpname) + 3;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* allocate storage to hold destination script path */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return ((char *)NULL);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* create destination script path */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) snprintf(dstpath, dstpathLen, "%s/%s", tmpdir, tmpname);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* make copy of script */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (r != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Make the copy of the script readable by all */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This function creates a temporary working copy of a read-only response
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * file. It changes the resppath pointer to point to the working copy.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(tpath, path, sizeof (tpath));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) snprintf(path, sizeof (path), "%s/respXXXXXX", tmpdir);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Copy the contents of the user's response file to the working copy. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (r != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Make it writable by the non-privileged installation user-id,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * but readable by the world.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This function establishes the response file passed on the command line if
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * it's called with a valid string. If called with NULL, it checks to see if
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * there's a response file already. If there isn't, it creates a temporary.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandset_respfile(char *respfile, char *pkginst, int resp_stat)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (respfile == NULL && !respfile_defined) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* A temporary response file needs to be constructed. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) snprintf(path, sizeof (path), "%s/respXXXXXX", tmpdir);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* OK, we're being passed a response file or directory. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(path, respfile, sizeof (path));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* This exposes the working response file. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Execute the request script if present assuming the response file
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * isn't read only.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandreqexec(int update, char *script, int non_abi_scripts,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * determine which alternative user to execute the request script as
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if the default user "install" is not defined.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* use the root user */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } else if (non_abi_scripts != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* non-compliant package user */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* standard non-privileged user */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If we can't get to the the script or the response file, skip this.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (access(script, F_OK) != 0 || respfile_ro)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* No interact means no interact. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* If there's no response file, create one. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Clear out the old response file (if there is one). */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((access(resppath, F_OK) == 0) && unlink(resppath)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Create a zero length response file which is only writable
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * by the non-privileged installation user-id, but is readable
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * by the world
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((fd = open(resppath, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644)) < 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (do_exec(update, script, resppath, REQ_STDIN, req_user));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If we're up against a read-only response file from the command
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * line. Create a working copy.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Make sure we can get to it. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* If there's no response file, create a fresh one. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * create a zero length response file which is only writable
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * by the non-priveledged installation user-id, but is readable
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * by the world
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland fd = open(resppath, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return (do_exec(update, script, resppath, CHK_STDIN, CHK_USER_ALT));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlanddo_exec(int update, char *script, char *output, char *inport, char *alt_user)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Determine which user to run the request script as:
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - if CHK_USER is a valid user, run the script as CHK_USER
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - otherwise, if alt_user is a valid user, run the script
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * -- as alt_user
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - otherwise, output an error message and return failure
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((pwp = getpwnam(CHK_USER)) != (struct passwd *)NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } else if ((pwp = getpwnam(alt_user)) != (struct passwd *)NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ptext(stderr, ERR_BADUSER, CHK_USER, CHK_USER_ALT);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Determine which group to run the request script as:
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - If CHK_GRP is a valid group, run the script as CHK_GRP
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - otherwise, assume group "1" user "other"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((grp = getgrnam(CHK_GRP)) != (struct group *)NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland echoDebug(DBG_DO_EXEC_REQUEST_USER, script, output, uname, instuid,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Copy the checkinstall script to tmpdir in case parent directories
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * are restrictive, i.e. 700. Only do this for non updates, i.e.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * package installs and not patch package installs.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } else if ((tmp_script = dup_chkinstall(script)) == NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Use the original checkinstall script */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland retcode = pkgexecl(inport, CHK_STDOUT, uname, CHK_GRP, SHELL,