/* perl.c
*
* Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
* 2000, 2001, 2002, 2003, 2004, by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
*/
/* PSz 12 Nov 03
*
* Be proud that perl(1) may proclaim:
* Setuid Perl scripts are safer than C programs ...
* Do not abandon (deprecate) suidperl. Do not advocate C wrappers.
*
* The flow was: perl starts, notices script is suid, execs suidperl with same
* arguments; suidperl opens script, checks many things, sets itself with
* right UID, execs perl with similar arguments but with script pre-opened on
* insecure: see perlsec(1) for many problems with this approach.
*
* The "correct" flow should be: perl starts, opens script and notices it is
* suid, checks many things, execs suidperl with similar arguments but with
* same, checks arguments match #! line, sets itself with right UID, execs
* perl with same arguments; perl checks many things and does work.
*
* (Opening the script in perl instead of suidperl, we "lose" scripts that
* are readable to the target UID but not to the invoker. Where did
* unreadable scripts work anyway?)
*
* For now, suidperl and perl are pretty much the same large and cumbersome
* program, so suidperl can check its argument list (see comments elsewhere).
*
* References:
* Original bug report:
* Comments and discussion with Debian:
* Debian Security Advisory DSA 431-1 (does not fully fix problem):
* CVE candidate:
* Previous versions of this patch sent to perl5-porters:
*
Paul Szabo - psz@maths.usyd.edu.au http://www.maths.usyd.edu.au:8000/u/psz/
School of Mathematics and Statistics University of Sydney 2006 Australia
*
*/
/* PSz 13 Nov 03
* Use truthful, neat, specific error messages.
* Cannot always hide the truth; security must not depend on doing so.
*/
/* PSz 18 Feb 04
* Use global(?), thread-local fdscript for easier checks.
* (I do not understand how we could possibly get a thread race:
* do not all threads go through the same initialization? Or in
* fact, are not threads started only after we get the script and
* so know what to do? Oh well, make things super-safe...)
*/
#include "EXTERN.h"
#define PERL_IN_PERL_C
#include "perl.h"
#include "patchlevel.h" /* for local_patches */
#ifdef NETWARE
#include "nwutil.h"
char *nw_get_sitelib(const char *pl);
#endif
/* XXX If this causes problems, set i_unistd=undef in the hint file. */
#ifdef I_UNISTD
#include <unistd.h>
#endif
#ifdef __BEOS__
#endif
#ifndef HZ
# ifdef CLK_TCK
# else
# endif
#endif
#endif
#ifdef IAMSUID
#ifndef DOSUID
#define DOSUID
#endif
#endif /* IAMSUID */
#ifdef DOSUID
#endif
#endif
#if defined(USE_5005THREADS)
# define INIT_TLS_AND_INTERP \
STMT_START { \
if (!PL_curinterp) { \
INIT_THREADS; \
} \
} STMT_END
#else
# if defined(USE_ITHREADS)
# define INIT_TLS_AND_INTERP \
STMT_START { \
if (!PL_curinterp) { \
INIT_THREADS; \
PERL_SET_THX(my_perl); \
} \
else { \
PERL_SET_THX(my_perl); \
} \
} STMT_END
# else
# define INIT_TLS_AND_INTERP \
STMT_START { \
if (!PL_curinterp) { \
} \
PERL_SET_THX(my_perl); \
} STMT_END
# endif
#endif
#ifdef PERL_IMPLICIT_SYS
{
/* New() needs interpreter, so call malloc() instead */
PL_MemShared = ipMS;
PL_MemParse = ipMP;
return my_perl;
}
#else
/*
=head1 Embedding Functions
=for apidoc perl_alloc
Allocates a new Perl interpreter. See L<perlembed>.
=cut
*/
perl_alloc(void)
{
#ifdef USE_5005THREADS
dTHX;
#endif
/* New() needs interpreter, so call malloc() instead */
return my_perl;
}
#endif /* PERL_IMPLICIT_SYS */
/*
=for apidoc perl_construct
Initializes a new Perl interpreter. See L<perlembed>.
=cut
*/
void
{
#ifdef USE_5005THREADS
#ifndef FAKE_THREADS
#endif /* FAKE_THREADS */
#endif /* USE_5005THREADS */
#ifdef MULTIPLICITY
init_interp();
#else
if (PL_perl_destruct_level > 0)
init_interp();
#endif
/* Init the real globals (and main thread)? */
if (!PL_linestr) {
#ifdef USE_5005THREADS
/*
* Safe to use basic SV functions from now on (though
* not things like mortals or tainting yet).
*/
# ifdef EMULATE_ATOMIC_REFCOUNTS
# endif /* EMULATE_ATOMIC_REFCOUNTS */
thr = init_main_thread();
#endif /* USE_5005THREADS */
#ifdef PERL_FLEXIBLE_EXCEPTIONS
#endif
if (!SvREADONLY(&PL_sv_undef)) {
/* set read-only and try to insure than we wont see REFCNT==0
very often */
}
PL_pidstatus = newHV();
}
init_stacks();
init_ids();
init_i18nl10n(1);
{
U8 *s;
/* Build version strings using "native" characters */
*s = '\0';
}
#if defined(LOCAL_PATCH_COUNT)
#endif
#ifdef HAVE_INTERP_INTERN
#endif
#ifdef USE_ITHREADS
PL_regex_padav = newAV();
#endif
#ifdef USE_REENTRANT_API
#endif
/* Note that strtab is a rather special HV. Assumptions are made
about not iterating on it, and not adding tie magic to it.
It is properly deallocated in perl_destruct() */
#ifdef USE_5005THREADS
#endif
#endif /* environ */
#ifndef PERL_MICRO
# ifdef USE_ENVIRON_ARRAY
# endif
#endif
/* Use sysconf(_SC_CLK_TCK) if available, if not
* available or if the sysconf() fails, use the HZ. */
#if defined(HAS_SYSCONF) && defined(_SC_CLK_TCK)
if (PL_clocktick <= 0)
#endif
PL_clocktick = HZ;
PL_stashcache = newHV();
}
/*
=for apidoc nothreadhook
Stub that provides thread hook for perl_destruct when there are
no threads.
=cut
*/
int
{
return 0;
}
/*
=for apidoc perl_destruct
Shuts down a Perl interpreter. See L<perlembed>.
=cut
*/
int
{
#ifdef USE_5005THREADS
Thread t;
dTHX;
#endif /* USE_5005THREADS */
/* wait for all pseudo-forked children to finish */
#ifdef USE_5005THREADS
#ifndef FAKE_THREADS
/* Pass 1 on any remaining threads: detach joinables, join zombies */
"perl_destruct: waiting for %d threads...\n",
PL_nthreads - 1));
MUTEX_LOCK(&t->mutex);
switch (ThrSTATE(t)) {
case THRf_ZOMBIE:
"perl_destruct: joining zombie %p\n", t));
ThrSETSTATE(t, THRf_DEAD);
MUTEX_UNLOCK(&t->mutex);
PL_nthreads--;
/*
* The SvREFCNT_dec below may take a long time (e.g. av
* may contain an object scalar whose destructor gets
* called) so we have to unlock threads_mutex and start
* all over again.
*/
"perl_destruct: joined zombie %p OK\n", t));
goto retry_cleanup;
case THRf_R_JOINABLE:
"perl_destruct: detaching thread %p\n", t));
/*
* We unlock threads_mutex and t->mutex in the opposite order
* from which we locked them just so that DETACH won't
* deadlock if it panics. It's only a breach of good style
* not a bug since they are unlocks not locks.
*/
DETACH(t);
MUTEX_UNLOCK(&t->mutex);
goto retry_cleanup;
default:
"perl_destruct: ignoring %p (state %u)\n",
t, ThrSTATE(t)));
MUTEX_UNLOCK(&t->mutex);
/* fall through and out */
}
}
/* We leave the above "Pass 1" loop with threads_mutex still locked */
/* Pass 2 on remaining threads: wait for the thread count to drop to one */
while (PL_nthreads > 1)
{
"perl_destruct: final wait for %d threads\n",
PL_nthreads - 1));
}
/* At this point, we're the last thread */
PL_nthreads--;
#endif /* !defined(FAKE_THREADS) */
#endif /* USE_5005THREADS */
#ifdef DEBUGGING
{
char *s;
if ((s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL"))) {
int i = atoi(s);
if (destruct_level < i)
destruct_level = i;
}
}
#endif
if(PL_exit_flags & PERL_EXIT_DESTRUCT_END) {
int x = 0;
JMPENV_PUSH(x);
if (PL_endav && !PL_minus_c)
}
/* Need to flush since END blocks can produce output */
/* Threads hook has vetoed further cleanup */
return STATUS_NATIVE_EXPORT;
}
/* We must account for everything. */
/* Destroy the main CV and syntax tree */
if (PL_main_root) {
if (CvPADLIST(PL_main_cv)) {
}
}
PL_main_cv = Nullcv;
/* Tell PerlIO we are about to tear things apart in case
we have layers which are using resources that should
be cleaned up now.
*/
if (PL_sv_objcount) {
/*
* Try to destruct global references. We do this first so that the
* destructors and destructees still exist. Some sv's might remain.
* Non-referenced objects are on their own.
*/
PL_sv_objcount = 0;
}
/* unhook hooks which will soon be, or use, destroyed data */
PL_diehook = Nullsv;
/* call exit list functions */
while (PL_exitlistlen-- > 0)
PL_exitlist = NULL;
PL_exitlistlen = 0;
if (destruct_level == 0){
DEBUG_P(debprofdump());
#if defined(PERLIO_LAYERS)
/* No more IO - including error messages ! */
#endif
/* The exit() function will do everything that needs doing. */
return STATUS_NATIVE_EXPORT;
}
/* jettison our possibly duplicated environment */
/* if PERL_USE_SAFE_PUTENV is defined environ will not have been copied
* so we certainly shouldn't free it here
*/
#ifndef PERL_MICRO
#if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV)
if (environ != PL_origenviron
#ifdef USE_ITHREADS
/* only main thread can free environ[0] contents */
&& PL_curinterp == aTHX
#endif
)
{
I32 i;
for (i = 0; environ[i]; i++)
safesysfree(environ[i]);
/* Must use safesysfree() when working with environ. */
}
#endif
#endif /* !PERL_MICRO */
#ifdef USE_ITHREADS
/* the syntax tree is shared between clones
* so op_free(PL_main_root) only ReREFCNT_dec's
* REGEXPs in the parent interpreter
* we need to manually ReREFCNT_dec for the clones
*/
{
while (i) {
/* this is PL_reg_curpm, already freed
* flag is set in regexec.c:S_regtry
*/
}
else if(SvREPADTMP(resv)) {
}
else {
}
}
}
PL_regex_pad = NULL;
#endif
/* loosen bonds of global variables */
if(PL_rsfp) {
(void)PerlIO_close(PL_rsfp);
}
/* Filters for program text */
/* switches */
PL_minus_n = FALSE;
PL_minus_p = FALSE;
PL_minus_l = FALSE;
PL_minus_a = FALSE;
PL_minus_F = FALSE;
PL_inplace = Nullch;
if (PL_e_script) {
}
PL_perldb = 0;
/* magical thingies */
PL_multiline = 0; /* $* */
/* defgv, aka *_ should be taken care of elsewhere */
/* clean up after study() */
PL_screamfirst = 0;
PL_screamnext = 0;
/* float buffer */
PL_efloatsize = 0;
/* startup and shutdown function lists */
PL_beginav = Nullav;
PL_checkav = Nullav;
/* shortcuts just get cleared */
PL_stdingv = Nullgv;
PL_DBtrace = Nullsv;
/* reset so print() ends up where we expect */
PL_subname = Nullsv;
PL_linestr = Nullsv;
/* free locale stuff */
#ifdef USE_LOCALE_COLLATE
#endif
#ifdef USE_LOCALE_NUMERIC
#endif
/* clear utf8 character classes */
/* Prepare to destruct main symbol table. */
hv = PL_defstash;
PL_defstash = 0;
/* clear queued errors */
if (PL_scopestack_ix != 0)
"Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
(long)PL_scopestack_ix);
if (PL_savestack_ix != 0)
"Unbalanced saves: %ld more saves than restores\n",
(long)PL_savestack_ix);
if (PL_tmps_floor != -1)
(long)PL_tmps_floor + 1);
if (cxstack_ix != -1)
(long)cxstack_ix + 1);
}
/* Now absolutely destruct everything, somehow or other, loops or no. */
/* the 2 is for PL_fdpid and PL_strtab */
;
#ifdef HAVE_INTERP_INTERN
#endif
/* Destruct the global string table. */
{
/* Yell and reset the HeVAL() slots that are still holding refcounts,
* so that sv_free() won't fail on them.
*/
riter = 0;
for (;;) {
"Unbalanced string table refcount: (%d) for \"%s\"",
}
if (!hent) {
break;
}
}
}
#ifdef USE_ITHREADS
/* free the pointer table used for cloning */
#endif
/* free special SVs */
{
int i;
for (i=0; i<=2; i++) {
SvREFCNT(PERL_DEBUG_PAD(i)) = 0;
sv_clear(PERL_DEBUG_PAD(i));
SvFLAGS(PERL_DEBUG_PAD(i)) = 0;
}
}
#ifdef DEBUG_LEAKING_SCALARS
if (PL_sv_count != 0) {
}
}
}
}
#endif
PL_sv_count = 0;
#if defined(PERLIO_LAYERS)
/* No more IO - including error messages ! */
#endif
/* sv_undef needs to stay immortal until after PerlIO_cleanup
as currently layers use it rather than Nullsv as a marker
for no arg - and will try and SvREFCNT_dec it.
*/
SvREFCNT(&PL_sv_undef) = 0;
PL_reg_start_tmp = (char**)NULL;
PL_reg_start_tmpl = 0;
if (PL_reg_curpm)
PL_psig_pend = (int*)NULL;
nuke_stacks();
PL_tainting = FALSE;
PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
PL_debug = 0;
DEBUG_P(debprofdump());
#ifdef USE_5005THREADS
#ifdef EMULATE_ATOMIC_REFCOUNTS
#endif /* EMULATE_ATOMIC_REFCOUNTS */
/* As the penultimate thing, free the non-arena SV for thrsv */
#endif /* USE_5005THREADS */
#ifdef USE_REENTRANT_API
#endif
/* As the absolutely last thing, free the non-arena SV for mess() */
if (PL_mess_sv) {
/* it could have accumulated taint magic */
}
}
/* we know that type >= SVt_PV */
(void)SvOOK_off(PL_mess_sv);
PL_mess_sv = Nullsv;
}
return STATUS_NATIVE_EXPORT;
}
/*
=for apidoc perl_free
Releases a Perl interpreter. See L<perlembed>.
=cut
*/
void
{
# if defined(PERL_IMPLICIT_SYS)
# ifdef NETWARE
# else
void *host = w32_internal_host;
# endif
# ifdef NETWARE
# else
# endif
# else
# endif
#else
#endif
}
void
{
}
/*
=for apidoc perl_parse
Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
=cut
*/
int
{
int ret;
#ifdef USE_5005THREADS
dTHX;
#endif
#ifdef IAMSUID
setuid perl scripts securely.\n");
#endif /* IAMSUID */
#endif
#if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
/* [perl #22371] Algorimic Complexity Attack on Perl 5.6.1, 5.8.0
* This MUST be done before any hash stores or fetches take place.
* If you set PL_rehash_seed (and assumedly also PL_rehash_seed_set)
* yourself, it is your responsibility to provide a good random seed!
* You can also define PERL_HASH_SEED in compile time, see hv.h. */
if (!PL_rehash_seed_set)
{
char *s = PerlEnv_getenv("PERL_HASH_SEED_DEBUG");
if (s) {
int i = atoi(s);
if (i == 1)
}
}
#endif /* #if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) */
PL_origargc = argc;
PL_origargv = argv;
{
/* Set PL_origalen be the sum of the contiguous argv[]
* elements plus the size of the env in case that it is
* contiguous with the argv[]. This is used in mg.c:Perl_magic_set()
* as the maximum modifiable length of $0. In the worst case
* the area we are able to modify is limited to the size of
* the original argv[0]. (See below for 'contiguous', though.)
* --jhi */
char *s = NULL;
int i;
/* Do the mask check only if the args seem like aligned. */
/* See if all the arguments are contiguous in memory. Note
* that 'contiguous' is a loose term because some platforms
* align the argv[] and the envp[]. If the arguments look
* like non-aligned, assume that they are 'strictly' or
* 'traditionally' contiguous. If the arguments look like
* aligned, we just check that they are within aligned
* PTRSIZE bytes. As long as no system has something bizarre
* like the argv[] interleaved with some other data, we are
* fine. (Did I just evoke Murphy's Law?) --jhi */
while (*s) s++;
for (i = 1; i < PL_origargc; i++) {
if ((PL_origargv[i] == s + 1
#ifdef OS2
|| PL_origargv[i] == s + 2
#endif
)
||
(aligned &&
(PL_origargv[i] > s &&
PL_origargv[i] <=
)
{
s = PL_origargv[i];
while (*s) s++;
}
else
break;
}
}
/* Can we grab env area too to be used as the area for $0? */
if (PL_origenviron) {
if ((PL_origenviron[0] == s + 1
#ifdef OS2
#endif
)
||
(aligned &&
(PL_origenviron[0] > s &&
PL_origenviron[0] <=
)
{
#ifndef OS2
s = PL_origenviron[0];
while (*s) s++;
#endif
/* Force copy of environment. */
for (i = 1; PL_origenviron[i]; i++) {
if (PL_origenviron[i] == s + 1
||
(aligned &&
(PL_origenviron[i] > s &&
PL_origenviron[i] <=
)
{
s = PL_origenviron[i];
while (*s) s++;
}
else
break;
}
}
}
PL_origalen = s - PL_origargv[0];
}
if (PL_do_undump) {
/* Come here if running an undumped a.out. */
init_ids();
return 0;
}
if (PL_main_root) {
}
PL_main_cv = Nullcv;
time(&PL_basetime);
#ifdef PERL_FLEXIBLE_EXCEPTIONS
#else
#endif
switch (ret) {
case 0:
#ifndef PERL_FLEXIBLE_EXCEPTIONS
#endif
if (PL_checkav)
ret = 0;
break;
case 1:
/* FALL THROUGH */
case 2:
/* my_exit() was called */
while (PL_scopestack_ix > oldscope)
if (PL_checkav)
break;
case 3:
ret = 1;
break;
}
return ret;
}
#ifdef PERL_FLEXIBLE_EXCEPTIONS
STATIC void *
{
}
#endif
STATIC void *
{
register char *s;
PL_fdscript = -1;
PL_suidscript = -1;
SAVEFREESV(sv);
break;
#ifdef DOSUID
if (*validarg)
validarg = " PHOOEY ";
else
/*
* Can we rely on the kernel to start scripts with argv[1] set to
* contain all #! line switches (the whole line)? (argv[0] is set to
* the interpreter name, argv[2] to the script name; argv[3] and
* above may contain other arguments.)
*/
#endif
s = argv[0]+1;
switch (*s) {
case 'C':
#ifndef PERL_STRICT_CR
case '\r':
#endif
case ' ':
case '0':
case 'F':
case 'a':
case 'c':
case 'd':
case 'D':
case 'h':
case 'i':
case 'l':
case 'M':
case 'm':
case 'n':
case 'p':
case 's':
case 'u':
case 'U':
case 'v':
case 'W':
case 'X':
case 'w':
if ((s = moreswitches(s)))
goto reswitch;
break;
case 't':
if( !PL_tainting ) {
PL_tainting = TRUE;
}
s++;
goto reswitch;
case 'T':
PL_tainting = TRUE;
s++;
goto reswitch;
case 'e':
#ifdef MACOS_TRADITIONAL
/* ignore -e for Dev:Pseudo argument */
break;
#endif
forbid_setid("-e");
if (!PL_e_script) {
}
if (*++s)
sv_catpv(PL_e_script, s);
else if (argv[1]) {
}
else
break;
case 'I': /* -I handled both here and in moreswitches() */
forbid_setid("-I");
}
if (s && *s) {
char *p;
Safefree(p);
}
else
break;
case 'P':
forbid_setid("-P");
s++;
goto reswitch;
case 'S':
forbid_setid("-S");
s++;
goto reswitch;
case 'V':
if (!PL_preambleav)
PL_preambleav = newAV();
if (*++s != ':') {
#ifdef VMS
#else
#endif
# ifdef DEBUGGING
# endif
# ifdef MULTIPLICITY
# endif
# ifdef USE_5005THREADS
# endif
# ifdef USE_ITHREADS
# endif
# ifdef USE_64_BIT_INT
# endif
# ifdef USE_64_BIT_ALL
# endif
# ifdef USE_LONG_DOUBLE
# endif
# ifdef USE_LARGE_FILES
# endif
# ifdef USE_SOCKS
# endif
# ifdef PERL_IMPLICIT_CONTEXT
# endif
# ifdef PERL_IMPLICIT_SYS
# endif
#if defined(LOCAL_PATCH_COUNT)
if (LOCAL_PATCH_COUNT > 0) {
int i;
for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
if (PL_localpatches[i])
0, PL_localpatches[i], 0);
}
}
#endif
#ifdef __DATE__
# ifdef __TIME__
# else
# endif
#endif
$\"=\"\\n \"; \
@env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; ");
#ifdef __CYGWIN__
push @env, \"CYGWIN=\\\"$ENV{CYGWIN}\\\"\";");
#endif
print \" \\%ENV:\\n @env\\n\" if @env; \
print \" \\@INC:\\n @INC\\n\";");
}
else {
s += strlen(s);
}
goto reswitch;
case 'x':
PL_doextract = TRUE;
s++;
if (*s)
cddir = s;
break;
case 0:
break;
case '-':
if (!*++s || isSPACE(*s)) {
goto switch_end;
}
/* catch use of gnu style long options */
if (strEQ(s, "version")) {
s = "v";
goto reswitch;
}
if (strEQ(s, "help")) {
s = "h";
goto reswitch;
}
s--;
/* FALL THROUGH */
default:
}
}
if (
#ifndef SECURE_INTERNAL_GETENV
!PL_tainting &&
#endif
(s = PerlEnv_getenv("PERL5OPT")))
{
char *popt = s;
while (isSPACE(*s))
s++;
if (*s == '-' && *(s+1) == 'T') {
PL_tainting = TRUE;
}
else {
while (s && *s) {
char *d;
while (isSPACE(*s))
s++;
if (*s == '-') {
s++;
if (isSPACE(*s))
continue;
}
d = s;
if (!*s)
break;
if (!strchr("DIMUdmtw", *s))
while (++s && *s) {
if (isSPACE(*s)) {
if (!popt_copy) {
}
*s++ = '\0';
break;
}
}
if (*d == 't') {
if( !PL_tainting ) {
PL_tainting = TRUE;
}
} else {
moreswitches(d);
}
}
}
}
}
if (!scriptname)
scriptname = argv[0];
if (PL_e_script) {
}
else if (scriptname == Nullch) {
#ifdef MSDOS
moreswitches("h");
#endif
scriptname = "-";
}
init_perllib();
#ifndef PERL_MICRO
{
#ifndef SIGCHLD
#endif
if (ckWARN(WARN_SIGNAL))
"Can't ignore signal CHLD, forcing to default");
}
}
#endif
#endif
#ifdef MACOS_TRADITIONAL
if (PL_doextract || gMacPerl_AlwaysExtract) {
#else
if (PL_doextract) {
#endif
}
#ifdef USE_5005THREADS
#endif /* USE_5005THREADS */
if (xsinit)
#ifndef PERL_MICRO
#endif
#endif
#ifdef USE_SOCKS
# ifdef HAS_SOCKS5_INIT
socks5_init(argv[0]);
# else
# endif
#endif
/* init_postdump_symbols not currently designed to be called */
/* more than once (ENV isn't cleared first, for example) */
/* But running with -u leaves %ENV & @ARGV undefined! XXX */
if (!PL_do_undump)
/* PL_unicode is turned on by -C or by $ENV{PERL_UNICODE}.
* PL_utf8locale is conditionally turned on by
* locale.c:Perl_init_i18nl10n() if the environment
* look like the user wants to use UTF-8. */
if (PL_unicode) {
/* Requires init_predump_symbols(). */
/* Turn on UTF-8-ness on STDIN, STDOUT, STDERR
* and the default open disciplines. */
if ((PL_unicode & PERL_UNICODE_STDIN_FLAG) &&
if ((PL_unicode & PERL_UNICODE_STDOUT_FLAG) &&
if ((PL_unicode & PERL_UNICODE_STDERR_FLAG) &&
if ((PL_unicode & PERL_UNICODE_INOUT_FLAG) &&
if (in) {
if (out)
else
}
else if (out)
SvSETMAGIC(sv);
}
}
}
if ((s = PerlEnv_getenv("PERL_SIGNALS"))) {
if (strEQ(s, "unsafe"))
else if (strEQ(s, "safe"))
else
}
init_lexer();
/* now parse the script */
PL_error_count = 0;
#ifdef MACOS_TRADITIONAL
if (PL_minus_c)
else {
}
}
#else
if (yyparse() || PL_error_count) {
if (PL_minus_c)
else {
}
}
#endif
CopLINE_set(PL_curcop, 0);
if (PL_e_script) {
}
if (PL_do_undump)
my_unexec();
if (isWARN_ONCE) {
}
#ifdef MYMALLOC
dump_mstats("after compilation:");
#endif
PL_restartop = 0;
return NULL;
}
/*
=for apidoc perl_run
Tells a Perl interpreter to run. See L<perlembed>.
=cut
*/
int
{
int ret = 0;
#ifdef USE_5005THREADS
dTHX;
#endif
#ifdef VMS
VMSISH_HUSHED = 0;
#endif
#ifdef PERL_FLEXIBLE_EXCEPTIONS
#else
#endif
switch (ret) {
case 1:
goto redo_body;
case 0: /* normal completion */
#ifndef PERL_FLEXIBLE_EXCEPTIONS
#endif
/* FALL THROUGH */
case 2: /* my_exit() */
while (PL_scopestack_ix > oldscope)
if (!(PL_exit_flags & PERL_EXIT_DESTRUCT_END) &&
PL_endav && !PL_minus_c)
#ifdef MYMALLOC
if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
dump_mstats("after execution: ");
#endif
break;
case 3:
if (PL_restartop) {
goto redo_body;
}
ret = 1;
break;
}
return ret;
}
#ifdef PERL_FLEXIBLE_EXCEPTIONS
STATIC void *
{
}
#endif
STATIC void *
{
if (!PL_restartop) {
if (PL_minus_c) {
#ifdef MACOS_TRADITIONAL
#else
#endif
my_exit(0);
}
if (PERLDB_SINGLE && PL_DBsingle)
if (PL_initav)
}
/* do it */
if (PL_restartop) {
PL_restartop = 0;
}
else if (PL_main_start) {
}
my_exit(0);
/* NOTREACHED */
return NULL;
}
/*
=head1 SV Manipulation Functions
=for apidoc p||get_sv
Returns the SV of the specified Perl scalar. If C<create> is set and the
Perl variable does not exist then it will be created. If C<create> is not
set and the variable does not exist then NULL is returned.
=cut
*/
SV*
{
#ifdef USE_5005THREADS
if (tmp != NOT_IN_PAD)
}
#endif /* USE_5005THREADS */
if (gv)
return Nullsv;
}
/*
=head1 Array Manipulation Functions
=for apidoc p||get_av
Returns the AV of the specified Perl array. If C<create> is set and the
Perl variable does not exist then it will be created. If C<create> is not
set and the variable does not exist then NULL is returned.
=cut
*/
AV*
{
if (create)
if (gv)
return Nullav;
}
/*
=head1 Hash Manipulation Functions
=for apidoc p||get_hv
Returns the HV of the specified Perl hash. If C<create> is set and the
Perl variable does not exist then it will be created. If C<create> is not
set and the variable does not exist then NULL is returned.
=cut
*/
HV*
{
if (create)
if (gv)
return Nullhv;
}
/*
=head1 CV Manipulation Functions
=for apidoc p||get_cv
Returns the CV of the specified Perl subroutine. If C<create> is set and
the Perl subroutine does not exist then it will be declared (which has the
same effect as saying C<sub name;>). If C<create> is not set and the
subroutine does not exist then NULL is returned.
=cut
*/
CV*
{
/* XXX unsafe for threads if eval_owner isn't held */
/* XXX this is probably not what they think they're getting.
* It has the same effect as "sub name;", i.e. just a forward
* declaration! */
Nullop);
if (gv)
return Nullcv;
}
/* Be sure to refetch the stack pointer after calling these routines. */
/*
=head1 Callback Functions
=for apidoc p||call_argv
Performs a callback to the specified Perl sub. See L<perlcall>.
=cut
*/
/* See G_* flags in cop.h */
/* null terminated arg list */
{
dSP;
if (argv) {
while (*argv) {
argv++;
}
}
}
/*
=for apidoc p||call_pv
Performs a callback to the specified Perl sub. See L<perlcall>.
=cut
*/
/* name of the subroutine */
/* See G_* flags in cop.h */
{
}
/*
=for apidoc p||call_method
Performs a callback to the specified Perl method. The blessed object must
be on the stack. See L<perlcall>.
=cut
*/
/* name of the subroutine */
/* See G_* flags in cop.h */
{
}
/* May be called with any of a CV, a GV, or an SV containing the name. */
/*
=for apidoc p||call_sv
Performs a callback to the Perl sub whose name is in the SV. See
L<perlcall>.
=cut
*/
/* See G_* flags in cop.h */
{
dSP;
int ret;
}
SAVEOP();
*++PL_stack_sp = sv;
/* Handle first BEGIN of -d. */
/* Try harder, since this may have been a sighandler, thus
* curstash may be meaningless. */
}
}
else {
/* we're trying to emulate pp_entertry() here */
{
else
}
#ifdef PERL_FLEXIBLE_EXCEPTIONS
#else
#endif
switch (ret) {
case 0:
#ifndef PERL_FLEXIBLE_EXCEPTIONS
#endif
break;
case 1:
/* FALL THROUGH */
case 2:
/* my_exit() was called */
my_exit_jump();
/* NOTREACHED */
case 3:
if (PL_restartop) {
PL_restartop = 0;
goto redo_body;
}
retval = 0;
else {
retval = 1;
*++PL_stack_sp = &PL_sv_undef;
}
break;
}
if (PL_scopestack_ix > oldscope) {
pop_return();
}
}
retval = 0;
}
return retval;
}
#ifdef PERL_FLEXIBLE_EXCEPTIONS
STATIC void *
{
return NULL;
}
#endif
STATIC void
{
if (is_eval)
else
}
if (PL_op)
}
/* Eval a string. The G_EVAL flag is always assumed. */
/*
=for apidoc p||eval_sv
Tells Perl to C<eval> the string in the SV.
=cut
*/
/* See G_* flags in cop.h */
{
dSP;
int ret;
}
SAVEOP();
*++PL_stack_sp = sv;
#ifdef PERL_FLEXIBLE_EXCEPTIONS
#else
#endif
switch (ret) {
case 0:
#ifndef PERL_FLEXIBLE_EXCEPTIONS
#endif
break;
case 1:
/* FALL THROUGH */
case 2:
/* my_exit() was called */
my_exit_jump();
/* NOTREACHED */
case 3:
if (PL_restartop) {
PL_restartop = 0;
goto redo_body;
}
retval = 0;
else {
retval = 1;
*++PL_stack_sp = &PL_sv_undef;
}
break;
}
retval = 0;
}
return retval;
}
/*
=for apidoc p||eval_pv
Tells Perl to C<eval> the given string and return an SV* result.
=cut
*/
SV*
{
dSP;
}
return sv;
}
/* Require a module. */
/*
=head1 Embedding Functions
=for apidoc p||require_pv
Tells Perl to C<require> the file named by the string argument. It is
analogous to the Perl code C<eval "require '$file'">. It's even
implemented that way; consider using load_module instead.
=cut */
void
{
dSP;
sv = sv_newmortal();
}
void
{
}
STATIC void
{
/* This message really ought to be max 23 lines.
* Removed -h because the user already knows that option. Others? */
static char *usage_msg[] = {
"-0[octal] specify record separator (\\0, if no argument)",
"-a autosplit mode with -n or -p (splits $_ into @F)",
"-c check syntax only (runs BEGIN and CHECK blocks)",
"-d[:debugger] run program under debugger",
"-e program one line of program (several -e's allowed, omit programfile)",
"-i[extension] edit <> files in place (makes backup if extension supplied)",
"-Idirectory specify @INC/#include directory (several -I's allowed)",
"-l[octal] enable line ending processing, specifies line terminator",
"-n assume 'while (<>) { ... }' loop around program",
"-p assume loop like -n but print line also, like sed",
"-P run program through C preprocessor before compilation",
"-s enable rudimentary parsing for switches after programfile",
"-S look for programfile using PATH environment variable",
"-t enable tainting warnings",
"-T enable tainting checks",
"-u dump core after parsing program",
"-U allow unsafe operations",
"-v print version, subversion (includes VERY IMPORTANT perl info)",
"-V[:variable] print configuration summary (or a single Config.pm variable)",
"-w enable many useful warnings (RECOMMENDED)",
"-W enable all warnings",
"-x[directory] strip off text before #!perl line and perhaps cd to directory",
"-X disable all warnings",
"\n",
};
char **p = usage_msg;
"\nUsage: %s [switches] [--] [programfile] [arguments]",
name);
while (*p)
}
/* convert a string of -D options (or digits) into an int.
* sets *s to point to the char after the options */
#ifdef DEBUGGING
int
{
int i = 0;
if (isALPHA(**s)) {
/* if adding extra options, remember to update DEBUG_MASK */
for (; isALNUM(**s); (*s)++) {
if (d)
i |= 1 << (d - debopts);
else if (ckWARN_d(WARN_DEBUGGING))
"invalid option -D%c\n", **s);
}
}
else {
i = atoi(*s);
for (; isALNUM(**s); (*s)++) ;
}
# ifdef EBCDIC
"-Dp not implemented on this platform\n");
# endif
return i;
}
#endif
/* This routine handles any switches that can be given during run */
char *
{
switch (*s) {
case '0':
{
if (s[1] == 'x' && s[2]) {
char *e;
for (s += 2, e = s; *e; e++);
numlen = e - s;
if (s + numlen < e) {
rschar = 0; /* Grandfather -0xFOO as -0 -xFOO. */
numlen = 0;
s--;
}
}
else {
numlen = 4;
PL_rs = &PL_sv_undef;
else {
}
}
return s + numlen;
}
case 'C':
s++;
PL_unicode = parse_unicode_opts(&s);
return s;
case 'F':
PL_minus_F = TRUE;
PL_splitstr = ++s;
while (*s && !isSPACE(*s)) ++s;
*s = '\0';
return s;
case 'a':
PL_minus_a = TRUE;
s++;
return s;
case 'c':
PL_minus_c = TRUE;
s++;
return s;
case 'd':
forbid_setid("-d");
s++;
/* The following permits -d:Mod to accepts arguments following an =
in the fashion that -MSome::Mod does. */
if (*s == ':' || *s == '=') {
char *start;
start = ++s;
/* We now allow -d:Module=Foo,Bar */
while(isALNUM(*s) || *s==':') ++s;
if (*s != '=')
else {
}
s += strlen(s);
}
if (!PL_perldb) {
}
return s;
case 'D':
{
#ifdef DEBUGGING
forbid_setid("-D");
s++;
#else /* !DEBUGGING */
if (ckWARN_d(WARN_DEBUGGING))
"Recompile perl with -DDEBUGGING to use -D switch\n");
for (s++; isALNUM(*s); s++) ;
#endif
/*SUPPRESS 530*/
return s;
}
case 'h':
usage(PL_origargv[0]);
my_exit(0);
case 'i':
if (PL_inplace)
#if defined(__CYGWIN__) /* do backup extension automagically */
if (*(s+1) == '\0') {
return s+1;
}
#endif /* __CYGWIN__ */
/*SUPPRESS 530*/
for (s = PL_inplace; *s && !isSPACE(*s); s++) ;
if (*s) {
*s++ = '\0';
if (*s == '-') /* Additional switches on #! line. */
s++;
}
return s;
case 'I': /* -I handled both here and in parse_body() */
forbid_setid("-I");
++s;
while (*s && isSPACE(*s))
++s;
if (*s) {
char *e, *p;
p = s;
/* ignore trailing spaces (possibly followed by other switches) */
do {
for (e = p; *e && !isSPACE(*e); e++) ;
p = e;
while (isSPACE(*p))
p++;
} while (*p && *p != '-');
e = savepvn(s, e-s);
Safefree(e);
s = p;
if (*s == '-')
s++;
}
else
return s;
case 'l':
PL_minus_l = TRUE;
s++;
if (PL_ors_sv) {
}
if (isDIGIT(*s)) {
s += numlen;
}
else {
}
else {
}
}
return s;
case 'M':
/* FALL THROUGH */
case 'm':
if (*++s) {
char *start;
/* -M-foo == 'no foo' */
start = s;
/* We allow -M'Module qw(Foo Bar)' */
while(isALNUM(*s) || *s==':') ++s;
if (*s != '=') {
if (*s != '\0')
}
} else {
if (s == start)
s[-1]);
}
s += strlen(s);
if (!PL_preambleav)
PL_preambleav = newAV();
}
else
return s;
case 'n':
PL_minus_n = TRUE;
s++;
return s;
case 'p':
PL_minus_p = TRUE;
s++;
return s;
case 's':
forbid_setid("-s");
s++;
return s;
case 't':
if (!PL_tainting)
TOO_LATE_FOR('t');
s++;
return s;
case 'T':
if (!PL_tainting)
TOO_LATE_FOR('T');
s++;
return s;
case 'u':
#ifdef MACOS_TRADITIONAL
#endif
PL_do_undump = TRUE;
s++;
return s;
case 'U':
s++;
return s;
case 'v':
#if !defined(DGUX)
#else /* DGUX */
OSVERS));
#endif /* !DGUX */
#if defined(LOCAL_PATCH_COUNT)
if (LOCAL_PATCH_COUNT > 0)
"\n(with %d registered patch%s, "
"see perl -V for more detail)",
(int)LOCAL_PATCH_COUNT,
#endif
"\n\nCopyright 1987-2004, Larry Wall\n");
#ifdef MACOS_TRADITIONAL
"\nMac OS port Copyright 1991-2002, Matthias Neeracher;\n"
"maintained by Chris Nandor\n");
#endif
#ifdef MSDOS
"\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
#endif
#ifdef DJGPP
"djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n"
"djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n");
#endif
#ifdef OS2
"\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
"Version 5 port Copyright (c) 1994-2002, Andreas Kaiser, Ilya Zakharevich\n");
#endif
#ifdef atarist
"atariST series port, ++jrb bammi@cadence.com\n");
#endif
#ifdef __BEOS__
"BeOS port Copyright Tom Spindler, 1997-1999\n");
#endif
#ifdef MPE
#endif
#ifdef OEMVS
"MVS (OS390) port by Mortice Kern Systems, 1997-1999\n");
#endif
#ifdef __VOS__
"Stratus VOS port by Paul.Green@stratus.com, 1997-2002\n");
#endif
#ifdef __OPEN_VM
#endif
#ifdef POSIX_BC
"BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n");
#endif
#ifdef __MINT__
"MiNT port by Guido Flohr, 1997-1999\n");
#endif
#ifdef EPOC
"EPOC port by Olaf Flebbe, 1999-2002\n");
#endif
#ifdef UNDER_CE
#endif
#ifdef BINARY_BUILD_NOTICE
#endif
"\n\
Perl may be copied only under the terms of either the Artistic License or the\n\
GNU General Public License, which may be found in the Perl 5 source kit.\n\n\
Complete documentation for Perl, including FAQ lists, should be found on\n\
this system using `man perl' or `perldoc perl'. If you have access to the\n\
Internet, point your browser at http://www.perl.com/, the Perl Home Page.\n\n");
my_exit(0);
case 'w':
if (! (PL_dowarn & G_WARN_ALL_MASK))
s++;
return s;
case 'W':
s++;
return s;
case 'X':
s++;
return s;
case '*':
case ' ':
if (s[1] == '-') /* Additional switches on #! line. */
return s+2;
break;
case '-':
case 0:
#if defined(WIN32) || !defined(PERL_STRICT_CR)
case '\r':
#endif
case '\n':
case '\t':
break;
#ifdef ALTERNATE_SHEBANG
case 'S': /* OS/2 needs -S on "extproc" line. */
break;
#endif
case 'P':
if (PL_preprocess)
return s+1;
/* FALL THROUGH */
default:
}
return Nullch;
}
/* compliments of Tom Christiansen */
/* unexec() can be found in the Gnu emacs distribution */
/* Known to work with -DUNEXEC and using unexelf.c from GNU emacs-20.2 */
void
{
#ifdef UNEXEC
extern int etext;
/* unexec prints msg to stderr in case of failure */
#else
# ifdef VMS
# else
ABORT(); /* for use with undump */
# endif
#endif
}
/* initialize curinterp */
STATIC void
{
#ifdef MULTIPLICITY
# if defined(PERL_IMPLICIT_CONTEXT)
# if defined(USE_5005THREADS)
# else /* !USE_5005THREADS */
# endif /* USE_5005THREADS */
# else
# endif
# include "intrpvar.h"
# ifndef USE_5005THREADS
# include "thrdvar.h"
# endif
#else
# include "intrpvar.h"
# ifndef USE_5005THREADS
# include "thrdvar.h"
# endif
#endif
}
STATIC void
{
/* We must init $/ before switches are processed. */
}
/* PSz 18 Nov 03 fdscript now global but do not change prototype */
STATIC void
{
#ifndef IAMSUID
char *quote;
char *code;
char *cpp_discard_flag;
char *perl;
#endif
PL_fdscript = -1;
PL_suidscript = -1;
if (PL_e_script) {
}
else {
/* if find_script() returns, it returns a malloc()-ed value */
char *s = scriptname + 8;
PL_fdscript = atoi(s);
while (isDIGIT(*s))
s++;
if (*s) {
/* PSz 18 Feb 04
* Tell apart "normal" usage of fdscript, e.g.
* with bash on FreeBSD:
* perl <( echo '#!perl -DA'; echo 'print "$0\n"')
* from usage in suidperl.
* Does any "normal" usage leave garbage after the number???
* suidperl?
*/
PL_suidscript = 1;
/* PSz 20 Feb 04
* Be supersafe and do some sanity-checks.
* Still, can we be sure we got the right thing?
*/
if (*s != '/') {
}
if (! *(s+1)) {
}
}
}
}
scriptname = "";
if (PL_fdscript >= 0) {
if (PL_rsfp)
/* ensure close-on-exec */
# endif
}
#ifdef IAMSUID
else {
"You should not call sperl directly; do you need to "
"change a #! line\nfrom sperl to perl?\n");
/* PSz 11 Nov 03
* Do not open (or do other fancy stuff) while setuid.
* Perl does the open, and hands script to suidperl on a fd;
* suidperl only does some checks, sets up UIDs and re-execs
* perl with that fd as it has always done.
*/
}
if (PL_suidscript != 1) {
}
#else /* IAMSUID */
else if (PL_preprocess) {
if (cpp_cfg[0] == 0) /* PERL_MICRO? */
# ifndef VMS
# endif
"PL_preprocess: scriptname=\"%s\", cpp=\"%s\", sv=\"%s\", CPPMINUS=\"%s\"\n",
quote = "\"";
# else
quote = "'";
# endif
# ifdef VMS
cpp_discard_flag = "";
# else
cpp_discard_flag = "-C";
# endif
# ifdef OS2
# else
perl = PL_origargv[0];
# endif
/* This strips off Perl comments which might interfere with
the C pre-processor, including #!. #line directives are
deliberately stripped to avoid confusion with Perl's version
of #line. FWP played some golf with it so it will fit
into VMS's 255 character buffer.
*/
if( PL_doextract )
code = "(1../^#!.*perl/i)|/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
else
code = "/^\\s*#(?!\\s*((ifn?|un)def|(el|end)?if|define|include|else|error|pragma)\\b)/||!($|=1)||print";
"PL_preprocess: cmd=\"%s\"\n",
}
else if (!*scriptname) {
forbid_setid("program input from stdin");
PL_rsfp = PerlIO_stdin();
}
else {
if (PL_rsfp)
/* ensure close-on-exec */
# endif
}
#endif /* IAMSUID */
if (!PL_rsfp) {
/* PSz 16 Sep 03 Keep neat error message */
}
}
/* Mention
* I_SYSSTATVFS HAS_FSTATVFS
* I_SYSMOUNT
* I_STATFS HAS_FSTATFS HAS_GETFSSTAT
* I_MNTENT HAS_GETMNTENT HAS_HASMNTOPT
* here so that metaconfig picks them up. */
#ifdef IAMSUID
STATIC int
{
/* PSz 27 Feb 04
* We used to do this as "plain" user (after swapping UIDs with setreuid);
* but is needed also on machines without setreuid.
* Seems safe enough to run as root.
*/
/* PSz 12 Nov 03
* Need to check noexec also: nosuid might not be set, the average
* sysadmin would say that nosuid is irrelevant once he sets noexec.
*/
/*
* Preferred order: fstatvfs(), fstatfs(), ustat()+getmnt(), getmntent().
* fstatvfs() is UNIX98.
* fstatfs() is 4.3 BSD.
* ustat()+getmnt() is pre-4.3 BSD.
* getmntent() is O(number-of-mounted-filesystems) and can hang on
* an irrelevant filesystem while trying to reach the right one.
*/
# if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
defined(HAS_FSTATVFS)
# define FD_ON_NOSUID_CHECK_OKAY
#ifdef ST_NOEXEC
/* ST_NOEXEC certainly absent on AIX 5.1, and doesn't seem to be documented
on platforms where it is present. */
#endif
# endif /* fstatvfs */
# if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
defined(PERL_MOUNT_NOSUID) && \
defined(PERL_MOUNT_NOEXEC) && \
defined(HAS_FSTATFS) && \
defined(HAS_STRUCT_STATFS) && \
defined(HAS_STRUCT_STATFS_F_FLAGS)
# define FD_ON_NOSUID_CHECK_OKAY
# endif /* fstatfs */
# if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
defined(PERL_MOUNT_NOSUID) && \
defined(PERL_MOUNT_NOEXEC) && \
defined(HAS_FSTAT) && \
defined(HAS_USTAT) && \
defined(HAS_GETMNT) && \
defined(HAS_STRUCT_FS_DATA) && \
defined(NOSTAT_ONE)
# define FD_ON_NOSUID_CHECK_OKAY
/* NOSTAT_ONE here because we're not examining fields which
* vary between that case and STAT_ONE. */
check_okay = 1;
}
}
}
}
}
# endif /* fstat+ustat+getmnt */
# if !defined(FD_ON_NOSUID_CHECK_OKAY) && \
defined(HAS_GETMNTENT) && \
defined(HAS_HASMNTOPT) && \
defined(MNTOPT_NOSUID) && \
defined(MNTOPT_NOEXEC)
# define FD_ON_NOSUID_CHECK_OKAY
{
/* found the filesystem */
check_okay = 1;
on_nosuid = 1;
on_noexec = 1;
break;
} /* A single fs may well fail its stat(). */
}
}
if (mtab)
# endif /* getmntent+hasmntopt */
if (!check_okay)
if (on_nosuid)
if (on_noexec)
}
#endif /* IAMSUID */
STATIC void
{
#ifdef IAMSUID
/* int which; */
#endif /* IAMSUID */
/* do we need to emulate setuid on scripts? */
/* This code is for those BSD systems that have setuid #! scripts disabled
* in the kernel because of a security problem. Merely defining DOSUID
* in perl will not fix that problem, but if you have disabled setuid
* scripts in the kernel, this will attempt to emulate setuid and setgid
* on scripts that have those now-otherwise-useless bits set. The setuid
* root version must be called suidperl or sperlN.NNN. If regular perl
* discovers that it has opened a setuid script, it calls suidperl with
* the same argv that it had. If suidperl finds that the script it has
* just opened is NOT setuid root, it sets the effective uid back to the
* uid. We don't just make perl setuid root because that loses the
* effective uid we had before invoking perl, if it was different from the
* uid.
* PSz 27 Feb 04
* Description/comments above do not match current workings:
* suidperl must be hardlinked to sperlN.NNN (that is what we exec);
* suidperl croaks if script is not setuid;
* making perl setuid would be a huge security risk (and yes, that
* would lose any euid we might have had).
*
* DOSUID must be defined in both perl and suidperl, and IAMSUID must
* be defined in suidperl only. suidperl must be setuid root. The
* Configure script will set this up for you if you want it.
*/
#ifdef DOSUID
char *s, *s2;
#ifdef IAMSUID
/* PSz 11 Nov 03
* Since the script is opened by perl, not suidperl, some of these
* checks are superfluous. Leaving them in probably does not lower
* security(?!).
*/
/* PSz 27 Feb 04
* Do checks even for systems with no HAS_SETREUID.
* We used to swap, then re-swap UIDs with
#ifdef HAS_SETREUID
if (setreuid(PL_euid,PL_uid) < 0
|| PerlProc_getuid() != PL_euid || PerlProc_geteuid() != PL_uid)
Perl_croak(aTHX_ "Can't swap uid and euid");
#endif
#ifdef HAS_SETREUID
if (setreuid(PL_uid,PL_euid) < 0
|| PerlProc_getuid() != PL_uid || PerlProc_geteuid() != PL_euid)
Perl_croak(aTHX_ "Can't reswap uid and euid");
#endif
*/
/* On this access check to make sure the directories are readable,
* there is actually a small window that the user could use to make
* filename point to an accessible directory. So there is a faint
* chance that someone could execute a setuid script down in a
* non-accessible directory. I don't know what to do about that.
* But I don't think it's too important. The manual lies when
* it says access() is useful in setuid programs.
*
* So, access() is pretty useless... but not harmful... do anyway.
*/
}
/* If we can swap euid and uid, then we can determine access rights
* with a simple stat of the file, and then compare device and
* inode to make sure we did stat() on the same file we opened.
* Then we just have to make sure he or she can execute it.
*
* PSz 24 Feb 04
* As the script is opened by perl, not suidperl, we do not need to
* care much about access rights.
*
* The 'script changed' check is needed, or we can get lied to
* about $0 with e.g.
* Without HAS_SETREUID, is it safe to stat() as root?
*
* pass the script name as we do, so the "script changed" test would
* fail for them... but we never get here with
* SETUID_SCRIPTS_ARE_SECURE_NOW defined.
*
* This is one place where we must "lie" about return status: not
* say if the stat() failed. We are doing this as root, and could
* be tricked into reporting existence or not of files that the
* "plain" user cannot even see.
*/
{
}
}
/* PSz 27 Feb 04
* We used to do this check as the "plain" user (after swapping
* UIDs). But the check for nosuid and noexec filesystem is needed,
* and should be done even without HAS_SETREUID. (Maybe those
* operating systems do not have such mount options anyway...)
* Seems safe enough to do as root.
*/
#if !defined(NO_NOSUID_CHECK)
}
#endif
#endif /* IAMSUID */
}
/* PSz 13 Nov 03 But -s was caught elsewhere ... so unsetting it here is useless(?!) */
/* PSz 27 Feb 04 */
/* Sanity check on line length */
/* Allow more than a single space after #! */
while (isSPACE(*s)) s++;
/* Sanity check on buffer end */
while ((*s) && !isSPACE(*s)) s++;
/* Sanity check on buffer start */
while (*s == ' ' || *s == '\t') s++;
/*
* #! arg must be what we saw above. They can invoke it by
* mentioning suidperl explicitly, but they may not add any strange
* arguments beyond what #! says if they do invoke suidperl that way.
*/
/*
* The way validarg was set up, we rely on the kernel to start
* scripts with argv[1] set to contain all #! line switches (the
* whole line).
*/
/*
* Check that we got all the arguments listed in the #! line (not
* just that there are no extraneous arguments). Might not matter
* much, as switches from #! line seem to be acted upon (also), and
* so may be checked and trapped in perl. But, security checks must
* be done in suidperl and not deferred to perl. Note that suidperl
* does not get around to parsing (and checking) the switches on
* the #! line (but execs perl sooner).
* Allow (require) a trailing newline (which may be of two
* characters on some architectures?) (but no other trailing
* whitespace).
*/
#ifndef IAMSUID
if (PL_fdscript < 0 &&
if (!PL_do_undump)
FIX YOUR KERNEL, OR PUT A C WRAPPER AROUND THIS SCRIPT!\n");
#endif /* IAMSUID */
if (PL_fdscript < 0 &&
PL_euid) { /* oops, we're not the setuid root perl */
/* PSz 18 Feb 04
* When root runs a setuid script, we do not go through the same
* steps of execing sperl and then perl with fd scripts, but
* simply set up UIDs within the same perl invocation; so do
* not have the same checks (on options, whatever) that we have
* for plain users. No problem really: would have to be a script
* that does not actually work for plain users; and if root is
* foolish and can be persuaded to run such an unsafe script, he
* might run also non-setuid ones, and deserves what he gets.
*
* Or, we might drop the PL_euid check above (and rely just on
* PL_fdscript to avoid loops), and do the execs
* even for root.
*/
#ifndef IAMSUID
int which;
/* PSz 11 Nov 03
* Pass fd script to suidperl.
* Exec suidperl, substituting fd script for scriptname.
* Pass script name as "subdir" of fd, which perl will grok;
* in fact will use that to distinguish this from "normal"
* usage, see comments above.
*/
/* PSz 27 Feb 04 Sanity checks on scriptname */
if ((!scriptname) || (!*scriptname) ) {
}
if (*scriptname == '-') {
/* Or we might confuse it with an option when replacing
* name in argument list, below (though we do pointer, not
* string, comparisons).
*/
}
if (!PL_origargv[which]) {
}
#endif
(int)PERL_REVISION, (int)PERL_VERSION,
(int)PERL_SUBVERSION), PL_origargv);
#endif /* IAMSUID */
}
/* PSz 26 Feb 04
* This seems back to front: we try HAS_SETEGID first; if not available
* then try HAS_SETREGID; as a last chance we try HAS_SETRESGID. May be OK
* in the sense that we only want to set EGID; but are there any machines
* with either of the latter, but not the former? Same with UID, later.
*/
#ifdef HAS_SETEGID
#else
#ifdef HAS_SETREGID
#else
#ifdef HAS_SETRESGID
#else
#endif
#endif
#endif
}
#ifdef HAS_SETEUID
#else
#ifdef HAS_SETREUID
#else
#ifdef HAS_SETRESUID
#else
#endif
#endif
#endif
}
else if (PL_uid) { /* oops, mustn't run as root */
#ifdef HAS_SETEUID
#else
#ifdef HAS_SETREUID
#else
#ifdef HAS_SETRESUID
#else
#endif
#endif
#endif
if (PerlProc_geteuid() != PL_uid)
}
init_ids();
}
#ifdef IAMSUID
else if (PL_preprocess) /* PSz 13 Nov 03 Caught elsewhere, useless(?!) here */
/* PSz 13 Nov 03 Caught elsewhere, useless(?!) here */
else {
/* PSz 16 Sep 03 Keep neat error message */
}
/* We absolutely must clear out any saved ids here, so we */
/* exec the real perl, substituting fd script for scriptname. */
/* (We pass script name as "subdir" of fd, which perl will grok.) */
/*
* set the saved IDs) above might obviate the need to exec, and we could
* go on to "do the perl thing".
*
* Is there such a thing as "saved GID", and is that set for setuid (but
* not setgid) execution like suidperl? Without exec, it would not be
* cleared for setuid (but not setgid) scripts (or might need a dummy
* setresgid).
*
* We need suidperl to do the exact same argument checking that perl
* does. Thus it cannot be very small; while it could be significantly
* smaller, it is safer (simpler?) to make it essentially the same
* binary as perl (but they are not identical). - Maybe could defer that
* check to the invoked perl, and suidperl be a tiny wrapper instead;
* but prefer to do thorough checks in suidperl itself. Such deferral
* would make suidperl security rely on perl, a design no-no.
*
* Setuid things should be short and simple, thus easy to understand and
* verify. They should do their "own thing", without influence by
* attackers. It may help if their internal execution flow is fixed,
* regardless of platform: it may be best to exec anyway.
*
* Suidperl should at least be conceptually simple: a wrapper only,
* never to do any real perl. Maybe we should put
* #ifdef IAMSUID
* Perl_croak(aTHX_ "Suidperl should never do real perl\n");
* #endif
* into the perly bits.
*/
/* PSz 11 Nov 03
* Keep original arguments: suidperl already has fd script.
*/
/* for (which = 1; PL_origargv[which] && PL_origargv[which] != scriptname; which++) ; */
/* if (!PL_origargv[which]) { */
/* errno = EPERM; */
/* Perl_croak(aTHX_ "Permission denied\n"); */
/* } */
/* PerlIO_fileno(PL_rsfp), PL_origargv[which])); */
#endif
(int)PERL_REVISION, (int)PERL_VERSION,
#endif /* IAMSUID */
#else /* !DOSUID */
#ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
||
)
if (!PL_do_undump)
FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
#endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
/* not set-id, must be wrapped */
}
#endif /* DOSUID */
}
STATIC void
{
register char *s, *s2;
#ifdef MACOS_TRADITIONAL
int maclines = 0;
#endif
/* skip forward in input to the real script? */
forbid_setid("-x");
#ifdef MACOS_TRADITIONAL
/* Since the Mac OS does not honor #! arguments for us, we do it ourselves */
while (PL_doextract || gMacPerl_AlwaysExtract) {
if (!gMacPerl_AlwaysExtract)
if (PL_doextract) /* require explicit override ? */
if (!OverrideExtract(PL_origfilename))
else
/* Pater peccavi, file does not have #! */
break;
}
#else
while (PL_doextract) {
#endif
s2 = s;
while (*s && !(isSPACE (*s) || *s == '#')) s++;
s2 = s;
while (*s == ' ' || *s == '\t') s++;
if (*s++ == '-') {
/*SUPPRESS 530*/
while ((s = moreswitches(s)))
;
}
#ifdef MACOS_TRADITIONAL
/* We are always searching for the #!perl line in MacPerl,
* so if we find it, still keep the line count correct
* by counting lines we already skipped over
*/
break;
/* gMacPerl_AlwaysExtract is false in MPW tool */
} else if (gMacPerl_AlwaysExtract) {
++maclines;
#endif
}
}
}
STATIC void
{
PL_uid = PerlProc_getuid();
PL_euid = PerlProc_geteuid();
PL_gid = PerlProc_getgid();
PL_egid = PerlProc_getegid();
#ifdef VMS
#endif
/* Should not happen: */
/* BUG */
/* PSz 27 Feb 04
* Should go by suidscript, not uid!=euid: why disallow
* system("ls") in scripts run from setuid things?
* Or, is this run before we check arguments and set suidscript?
* What about SETUID_SCRIPTS_ARE_SECURE_NOW: could we use fdscript then?
* (We never have suidscript, can we be sure to have fdscript?)
* Or must then go by UID checks? See comments in forbid_setid also.
*/
}
/* This is used very early in the lifetime of the program,
* before even the options are parsed, so PL_tainting has
* not been initialized properly. */
bool
{
#ifndef PERL_IMPLICIT_SYS
/* If we have PERL_IMPLICIT_SYS we can't call getuid() et alia
* before we have an interpreter-- and the whole point of this
* function is to be called at such an early stage. If you are on
* a system with PERL_IMPLICIT_SYS but you do have a concept of
* "tainted because running with altered effective ids', you'll
* have to add your own checks somewhere in here. The two most
* known samples of 'implicitness' are Win32 and NetWare, neither
* of which has much of concept of 'uids'. */
#ifdef VMS
#endif
return 1;
#endif /* !PERL_IMPLICIT_SYS */
/* This is a really primitive check; environment gets ignored only
* if -T are the first chars together; otherwise one gets
* "Too late" message. */
return 1;
return 0;
}
STATIC void
{
#endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
/* PSz 29 Feb 04
* perl -e 'print "Hello\n"'
* from within setuid things?? Simply drop them: replaced by
* fdscript/suidscript and #ifdef IAMSUID checks below.
*
* This may be too late for command-line switches. Will catch those on
* the #! line, after finding the script name and setting up
* fdscript/suidscript. Note that suidperl does not get around to
* parsing (and checking) the switches on the #! line, but checks that
* the two sets are identical.
*
* With SETUID_SCRIPTS_ARE_SECURE_NOW, could we use fdscript, also or
* instead, or would that be "too late"? (We never have suidscript, can
* we be sure to have fdscript?)
*
* Catch things with suidscript (in descendant of suidperl), even with
* below; but I am paranoid.
*
* Also see comments about root running a setuid script, elsewhere.
*/
if (PL_suidscript >= 0)
#ifdef IAMSUID
/* PSz 11 Nov 03 Catch it in suidperl, always! */
#endif /* IAMSUID */
}
void
{
sv_setiv(PL_DBsingle, 0);
sv_setiv(PL_DBtrace, 0);
sv_setiv(PL_DBsignal, 0);
}
#ifndef STRESS_REALLOC
#else
#endif
void
{
/* start with 128-item stack and 8K cxstack */
PL_tmps_floor = -1;
PL_tmps_ix = -1;
PL_scopestack_ix = 0;
PL_savestack_ix = 0;
PL_retstack_ix = 0;
}
STATIC void
{
while (PL_curstackinfo->si_next)
while (PL_curstackinfo) {
/* curstackinfo->si_stack got nuked by sv_free_arenas() */
PL_curstackinfo = p;
}
}
STATIC void
{
}
STATIC void
{
if (PL_osname)
}
void
{
char *s;
if (PL_doswitches) {
if (!argv[0][1])
break;
break;
}
*s++ = '\0';
}
else
}
}
if (PL_unicode & PERL_UNICODE_ARGV_FLAG)
}
(void)sv_utf8_decode(sv);
}
}
}
#ifdef HAS_PROCSELFEXE
/* This is a function so that we don't hold on to MAXPATHLEN
bytes of stack longer than necessary
*/
STATIC void
{
includes a spurious NUL which will cause $^X to fail in system
or backticks (this will prevent extensions from being built and
many tests from working). readlink is not meant to add a NUL.
Normal readlink works fine.
*/
len--;
}
/* FreeBSD's implementation is acknowledged to be imperfect, sometimes
returning the text "unknown" from the readlink rather than the path
to the executable (or returning an error from the readlink). Any valid
path has a '/' in it somewhere, so use that to validate the result.
*/
}
else {
}
}
#endif /* HAS_PROCSELFEXE */
STATIC void
{
char *s;
PL_toptarget = NEWSV(0,0);
PL_bodytarget = NEWSV(0,0);
#ifdef MACOS_TRADITIONAL
/* $0 is not majick on a Mac */
#else
#endif
}
#ifdef HAS_PROCSELFEXE
#else
#ifdef OS2
#else
#endif
#endif
}
#ifndef PERL_MICRO
#ifdef USE_ENVIRON_ARRAY
/* Note that if the supplied env parameter is actually a copy
of the global environ then it may now point to free'd memory
if the environment has been modified since. To avoid this
problem we treat env==NULL as meaning 'use the default'
*/
if (!env)
# ifdef USE_ITHREADS
&& PL_curinterp == aTHX
# endif
)
{
}
if (env)
continue;
*s = '\0';
*s = '=';
#endif
}
#endif /* USE_ENVIRON_ARRAY */
#endif /* !PERL_MICRO */
}
}
#ifdef THREADS_HAVE_PIDS
#endif
/* touch @F array to prevent spurious warnings 20020415 MJD */
if (PL_minus_a) {
}
/* touch @- and @+ arrays to prevent spurious warnings 20020415 MJD */
}
STATIC void
{
char *s;
if (!PL_tainting) {
#ifndef VMS
s = PerlEnv_getenv("PERL5LIB");
if (s)
else
#else /* VMS */
/* Treat PERL5?LIB as a possible search list logical name -- the
* "natural" VMS idiom for a Unix path string. We allow each
* element to be a set of |-separated directories for compatibility.
*/
char buf[256];
int idx = 0;
else
#endif /* VMS */
}
/* Use the ~-expanded versions of APPLLIB (undocumented),
ARCHLIB PRIVLIB SITEARCH SITELIB VENDORARCH and VENDORLIB
*/
#ifdef APPLLIB_EXP
#endif
#ifdef ARCHLIB_EXP
#endif
#ifdef MACOS_TRADITIONAL
{
if (!macperl)
macperl = "";
}
if (!PL_tainting)
#else
#ifndef PRIVLIB_EXP
#endif
#if defined(WIN32)
#else
#endif
#ifdef SITEARCH_EXP
/* sitearch is always relative to sitelib on Windows for
* DLL-based path intuition to work correctly */
# if !defined(WIN32)
# endif
#endif
#ifdef SITELIB_EXP
# if defined(WIN32)
/* this picks up sitearch as well */
# else
# endif
#endif
#ifdef SITELIB_STEM /* Search for version-specific dirs below here */
#endif
#ifdef PERL_VENDORARCH_EXP
/* vendorarch is always relative to vendorlib on Windows for
* DLL-based path intuition to work correctly */
# if !defined(WIN32)
# endif
#endif
#ifdef PERL_VENDORLIB_EXP
# if defined(WIN32)
# else
# endif
#endif
#ifdef PERL_VENDORLIB_STEM /* Search for version-specific dirs below here */
#endif
#ifdef PERL_OTHERLIBDIRS
#endif
if (!PL_tainting)
#endif /* MACOS_TRADITIONAL */
}
#else
# if defined(VMS)
# else
# if defined(MACOS_TRADITIONAL)
# else
# endif
# endif
#endif
#ifndef PERLLIB_MANGLE
# define PERLLIB_MANGLE(s,n) (s)
#endif
STATIC void
{
if (!p || !*p)
return;
if (addsubdirs || addoldvers) {
subdir = sv_newmortal();
}
/* Break at all separators */
while (p && *p) {
char *s;
/* skip any consecutive separators */
if (usesep) {
while ( *p == PERLLIB_SEP ) {
/* Uncomment the next line for PATH semantics */
/* av_push(GvAVn(PL_incgv), newSVpvn(".", 1)); */
p++;
}
}
(STRLEN)(s - p));
p = s + 1;
}
else {
p = Nullch; /* break out */
}
#ifdef MACOS_TRADITIONAL
}
#endif
/*
* BEFORE pushing libdir onto @INC we may first push version- and
* archname-specific sub-directories.
*/
if (addsubdirs || addoldvers) {
#ifdef PERL_INC_VERSION_LIST
/* Configure terminates PERL_INC_VERSION_LIST with a NULL */
const char **incver;
#endif
#ifdef VMS
char *unix;
}
else
"Failed to unixify @INC element \"%s\"\n",
#endif
if (addsubdirs) {
#ifdef MACOS_TRADITIONAL
#else
#define PERL_AV_SUFFIX_FMT "/"
#define PERL_ARCH_FMT "/%s"
#endif
(int)PERL_REVISION, (int)PERL_VERSION,
(int)PERL_SUBVERSION, ARCHNAME);
/* .../version if -d .../version */
(int)PERL_REVISION, (int)PERL_VERSION,
(int)PERL_SUBVERSION);
/* .../archname if -d .../archname */
}
#ifdef PERL_INC_VERSION_LIST
if (addoldvers) {
/* .../xxx if -d .../xxx */
}
}
#endif
}
/* finally push this lib directory on the end of @INC */
}
}
#ifdef USE_5005THREADS
STATIC struct perl_thread *
{
#if !defined(PERL_IMPLICIT_CONTEXT)
#endif
/* thr->threadsvp is set when find_threadsv is called */
/* Handcraft thrsv similarly to mess_sv */
PL_chopset = " \n-";
PL_dumpindent = 4;
PL_nthreads++;
#ifdef HAVE_THREAD_INTERN
#endif
#ifdef SET_THREAD_SELF
#else
#endif /* SET_THREAD_SELF */
/*
* These must come after the thread self setting
* because sv_setpvn does SvTAINT and the taint
* fields thread selfness being set.
*/
PL_toptarget = NEWSV(0,0);
PL_bodytarget = NEWSV(0,0);
PL_maxscream = -1;
PL_regindent = 0;
PL_reginterp_cnt = 0;
return thr;
}
#endif /* USE_5005THREADS */
void
{
int ret;
if (PL_savebegin) {
if (paramList == PL_beginav) {
/* save PL_beginav for compiler */
if (! PL_beginav_save)
PL_beginav_save = newAV();
}
else if (paramList == PL_checkav) {
/* save PL_checkav for compiler */
if (! PL_checkav_save)
PL_checkav_save = newAV();
}
} else {
SAVEFREESV(cv);
}
#ifdef PERL_FLEXIBLE_EXCEPTIONS
#else
#endif
switch (ret) {
case 0:
#ifndef PERL_FLEXIBLE_EXCEPTIONS
#endif
if (len) {
if (paramList == PL_beginav)
else
"%s failed--call queue aborted",
: "END");
while (PL_scopestack_ix > oldscope)
}
break;
case 1:
/* FALL THROUGH */
case 2:
/* my_exit() was called */
while (PL_scopestack_ix > oldscope)
if (paramList == PL_beginav)
else
: "END");
}
my_exit_jump();
/* NOTREACHED */
case 3:
if (PL_restartop) {
JMPENV_JUMP(3);
}
break;
}
}
}
#ifdef PERL_FLEXIBLE_EXCEPTIONS
STATIC void *
{
return call_list_body(cv);
}
#endif
STATIC void *
{
return NULL;
}
void
{
switch (status) {
case 0:
break;
case 1:
break;
default:
break;
}
my_exit_jump();
}
void
{
#ifdef VMS
STATUS_NATIVE_SET(44);
}
else {
STATUS_NATIVE_SET(44);
else
}
#else
int exitstatus;
if (errno & 255)
else {
if (exitstatus & 255)
else
STATUS_POSIX_SET(255);
}
#endif
my_exit_jump();
}
STATIC void
{
if (PL_e_script) {
}
if (cxstack_ix >= 0) {
if (cxstack_ix > 0)
dounwind(0);
}
JMPENV_JUMP(2);
}
static I32
{
char *p, *nl;
p = SvPVX(PL_e_script);
if (nl-p == 0) {
return 0;
}
return 1;
}