1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1982-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * regression test intercept control
1N/A * enable with SHOPT_REGRESS==1 in Makefile
1N/A * not for production use
1N/A * see --man for details
1N/A * all string constants inline here instead of in data/...
1N/A *
1N/A * David Korn
1N/A * at&t research
1N/A */
1N/A
1N/A#include "defs.h"
1N/A
1N/A#if SHOPT_REGRESS
1N/A
1N/A#include <error.h>
1N/A#include <ls.h>
1N/A#include "io.h"
1N/A#include "builtins.h"
1N/A#include <tmx.h>
1N/A
1N/A#define REGRESS_HEADER "ksh:REGRESS:"
1N/A
1N/A#define TRACE(r,i,f) sh_regress(REGRESS_##r, i, sfprints f, __LINE__, __FILE__)
1N/A
1N/Astatic const char usage[] =
1N/A"[-1p0?\n@(#)$Id: __regress__ (AT&T Research) 2009-03-29 $\n]"
1N/AUSAGE_LICENSE
1N/A"[+NAME?__regress__ - shell regression test intercept control]"
1N/A"[+DESCRIPTION?\b__regress__\b controls the regression test intercepts "
1N/A "for shells compiled with SHOPT_REGRESS==1. Shells compiled this way are "
1N/A "for testing only. In addition to \b__regress__\b and the \b--regress\b "
1N/A "command line option, these shells may contain system library function "
1N/A "intercepts that behave different from the native counterparts.]"
1N/A"[+?Each option controls a different test and possibly a different set "
1N/A "of intercepts. The options are interpreted \bdd\b(1) style -- '-' or "
1N/A "'--' prefix not required. This simplifies the specification of the "
1N/A "command line \b--regress\b=\avalue\a option, where \avalue\a is passed "
1N/A "as an option to the \b__regress__\b builtin. Typically regression test "
1N/A "intercepts are enabled with one or more command line \b--regress\b "
1N/A "options, with optional specific calls to \b__regress__\b in test "
1N/A "scripts to enable/disable intercepts as the test progresses.]"
1N/A"[+?Each enabled intercept may result in trace lines of the form \b" REGRESS_HEADER
1N/A "\aoption\a:\aintercept\a:\ainfo\a on the standard error, where "
1N/A "\aoption\a is one of the options below, \aintercept\a is the name of "
1N/A "the specific intercept for \aoption\a, and \ainfo\a is \aoption\a "
1N/A "specific information. Unless noted otherwise, one regression test trace "
1N/A "line is produced each time an enabled intercept is called.]"
1N/A"[101:egid?The intercept effective gid is set to \aoriginal-egid\a. The "
1N/A "effective gid of the underlying system process is not affected. The "
1N/A "trace line info is either \begid==rgid\b or \begid!=rgid\b. The "
1N/A "intercepts are:]#?[original-egid:=1]"
1N/A "{"
1N/A "[+getegid()?The intercept effecive gid is returned. The "
1N/A "\bsetgid\b() intercept may change this between the real gid and "
1N/A "\aoriginal-egid\a.]"
1N/A "[+setgid(gid)?Sets the intercept effective gid to \agid\a. "
1N/A "Fails if \agid\a is neither the real gid nor "
1N/A "\aoriginal-egid\a.]"
1N/A "}"
1N/A"[102:euid?The intercept effective uid is set to \aoriginal-euid\a. The "
1N/A "effective uid of the underlying system process is not affected. The "
1N/A "trace line info is either \beuid==ruid\b or \beuid!=ruid\b. The "
1N/A "intercepts are:]#?[original-euid:=1]"
1N/A "{"
1N/A "[+geteuid()?The intercept effecive uid is returned. The "
1N/A "\bsetuid\b() intercept may change this between the real uid and "
1N/A "\aoriginal-euid\a.]"
1N/A "[+setuid(uid)?Sets the intercept effective uid to \auid\a. "
1N/A "Fails if \auid\a is neither the real uid nor "
1N/A "\aoriginal-euid\a.]"
1N/A "}"
1N/A"[103:p_suid?Specifies a value for SHOPT_P_SUID. Effective uids greater "
1N/A "than the non-privileged-uid disable the priveleged mode. The intercepts "
1N/A "are:]#?[non-privileged-uid:=1]"
1N/A "{"
1N/A "[+SHOPT_P_SUID?The SHOPT_P_SUID macro value is overridden by "
1N/A "\bp_suid\b. A trace line is output for each SHOPT_P_SUID "
1N/A "access.]"
1N/A "}"
1N/A"[104:source?The intercepts are:]"
1N/A "{"
1N/A "[+sh_source()?The trace line info is the path of the script "
1N/A "being sourced. Used to trace shell startup scripts.]"
1N/A "}"
1N/A"[105:etc?Map file paths matching \b/etc/\b* to \aetc-dir\a/*. The "
1N/A "intercepts are:]:[etc-dir:=/etc]"
1N/A "{"
1N/A "[+sh_open()?Paths matching \b/etc/\b* are changed to "
1N/A "\aetc-dir\a/*.]"
1N/A "}"
1N/A"[+SEE ALSO?\bksh\b(1), \bregress\b(1), \brt\b(1)]"
1N/A;
1N/A
1N/Astatic const char* regress_options[] =
1N/A{
1N/A "ERROR",
1N/A "egid",
1N/A "euid",
1N/A "p_suid",
1N/A "source",
1N/A "etc",
1N/A};
1N/A
1N/Avoid sh_regress_init(Shell_t* shp)
1N/A{
1N/A static Regress_t state;
1N/A
1N/A shp->regress = &state;
1N/A}
1N/A
1N/A/*
1N/A * regress info trace output
1N/A */
1N/A
1N/Avoid sh_regress(unsigned int index, const char* intercept, const char* info, unsigned int line, const char* file)
1N/A{
1N/A char* name;
1N/A char buf[16];
1N/A
1N/A if (index >= 1 && index <= elementsof(regress_options))
1N/A name = (char*)regress_options[index];
1N/A else
1N/A sfsprintf(name = buf, sizeof(buf), "%u", index);
1N/A sfprintf(sfstderr, REGRESS_HEADER "%s:%s:%s\n", name, intercept, fmtesc(info));
1N/A}
1N/A
1N/A/*
1N/A * egid intercepts
1N/A */
1N/A
1N/Astatic gid_t intercept_sgid = 0;
1N/Astatic gid_t intercept_egid = -1;
1N/Astatic gid_t intercept_rgid = -1;
1N/A
1N/Agid_t getegid(void)
1N/A{
1N/A if (intercept_rgid == -1)
1N/A intercept_rgid = getgid();
1N/A if (sh_isregress(REGRESS_egid))
1N/A {
1N/A TRACE(egid, "getegid", ("%s", intercept_egid == intercept_rgid ? "egid==rgid" : "egid!=rgid"));
1N/A return intercept_egid;
1N/A }
1N/A return intercept_rgid;
1N/A}
1N/A
1N/Aint setgid(gid_t gid)
1N/A{
1N/A if (intercept_rgid == -1)
1N/A intercept_rgid = getgid();
1N/A if (sh_isregress(REGRESS_egid))
1N/A {
1N/A if (gid != intercept_rgid && gid != intercept_sgid)
1N/A {
1N/A TRACE(egid, "setgid", ("%s", "EPERM"));
1N/A errno = EPERM;
1N/A return -1;
1N/A }
1N/A intercept_egid = gid;
1N/A TRACE(egid, "setgid", ("%s", intercept_egid == intercept_rgid ? "egid==rgid" : "egid!=rgid"));
1N/A }
1N/A else if (gid != intercept_rgid)
1N/A {
1N/A errno = EPERM;
1N/A return -1;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * euid intercepts
1N/A */
1N/A
1N/Astatic uid_t intercept_suid = 0;
1N/Astatic uid_t intercept_euid = -1;
1N/Astatic uid_t intercept_ruid = -1;
1N/A
1N/Auid_t geteuid(void)
1N/A{
1N/A if (intercept_ruid == -1)
1N/A intercept_ruid = getuid();
1N/A if (sh_isregress(REGRESS_euid))
1N/A {
1N/A TRACE(euid, "geteuid", ("%s", intercept_euid == intercept_ruid ? "euid==ruid" : "euid!=ruid"));
1N/A return intercept_euid;
1N/A }
1N/A return intercept_ruid;
1N/A}
1N/A
1N/Aint setuid(uid_t uid)
1N/A{
1N/A if (intercept_ruid == -1)
1N/A intercept_ruid = getuid();
1N/A if (sh_isregress(REGRESS_euid))
1N/A {
1N/A if (uid != intercept_ruid && uid != intercept_suid)
1N/A {
1N/A TRACE(euid, "setuid", ("%s", "EPERM"));
1N/A errno = EPERM;
1N/A return -1;
1N/A }
1N/A intercept_euid = uid;
1N/A TRACE(euid, "setuid", ("%s", intercept_euid == intercept_ruid ? "euid==ruid" : "euid!=ruid"));
1N/A }
1N/A else if (uid != intercept_ruid)
1N/A {
1N/A errno = EPERM;
1N/A return -1;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * p_suid intercept
1N/A */
1N/A
1N/Astatic uid_t intercept_p_suid = 0x7fffffff;
1N/A
1N/Auid_t sh_regress_p_suid(unsigned int line, const char* file)
1N/A{
1N/A REGRESS(p_suid, "SHOPT_P_SUID", ("%d", intercept_p_suid));
1N/A return intercept_p_suid;
1N/A}
1N/A
1N/A/*
1N/A * p_suid intercept
1N/A */
1N/A
1N/Astatic char* intercept_etc = 0;
1N/A
1N/Achar* sh_regress_etc(const char* path, unsigned int line, const char* file)
1N/A{
1N/A REGRESS(etc, "sh_open", ("%s => %s%s", path, intercept_etc, path+4));
1N/A return intercept_etc;
1N/A}
1N/A
1N/A/*
1N/A * __regress__ builtin
1N/A */
1N/A
1N/Aint b___regress__(int argc, char** argv, void *extra)
1N/A{
1N/A register Shell_t* shp = ((Shbltin_t*)extra)->shp;
1N/A int n;
1N/A
1N/A for (;;)
1N/A {
1N/A switch (n = optget(argv, usage))
1N/A {
1N/A case '?':
1N/A errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
1N/A break;
1N/A case ':':
1N/A errormsg(SH_DICT, 2, "%s", opt_info.arg);
1N/A break;
1N/A case 0:
1N/A break;
1N/A default:
1N/A if (n < -100)
1N/A {
1N/A n = -(n + 100);
1N/A if (opt_info.arg || opt_info.number)
1N/A sh_onregress(n);
1N/A else
1N/A sh_offregress(n);
1N/A switch (n)
1N/A {
1N/A case REGRESS_egid:
1N/A if (sh_isregress(n))
1N/A {
1N/A intercept_egid = intercept_sgid = (gid_t)opt_info.number;
1N/A TRACE(egid, argv[0], ("%d", intercept_egid));
1N/A }
1N/A else
1N/A TRACE(egid, argv[0], ("%s", "off"));
1N/A break;
1N/A case REGRESS_euid:
1N/A if (sh_isregress(n))
1N/A {
1N/A intercept_euid = intercept_suid = (uid_t)opt_info.number;
1N/A TRACE(euid, argv[0], ("%d", intercept_euid));
1N/A }
1N/A else
1N/A TRACE(euid, argv[0], ("%s", "off"));
1N/A break;
1N/A case REGRESS_p_suid:
1N/A if (sh_isregress(n))
1N/A {
1N/A intercept_p_suid = (uid_t)opt_info.number;
1N/A TRACE(p_suid, argv[0], ("%d", intercept_p_suid));
1N/A }
1N/A else
1N/A TRACE(p_suid, argv[0], ("%s", "off"));
1N/A break;
1N/A case REGRESS_source:
1N/A TRACE(source, argv[0], ("%s", sh_isregress(n) ? "on" : "off"));
1N/A break;
1N/A case REGRESS_etc:
1N/A if (sh_isregress(n))
1N/A {
1N/A intercept_etc = opt_info.arg;
1N/A TRACE(etc, argv[0], ("%s", intercept_etc));
1N/A }
1N/A else
1N/A TRACE(etc, argv[0], ("%s", "off"));
1N/A break;
1N/A }
1N/A }
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A if (error_info.errors || *(argv + opt_info.index))
1N/A errormsg(SH_DICT, ERROR_usage(2), "%s", optusage(NiL));
1N/A return 0;
1N/A}
1N/A
1N/A#else
1N/A
1N/ANoN(regress)
1N/A
1N/A#endif