develooper Front page | perl.perl5.porters | Postings from July 2000

[PATCH and BUGFIX] [ID 20000326.008] Shell.pm, cracked?

From:
Casey R. Tweten
Date:
July 25, 2000 07:00
Subject:
[PATCH and BUGFIX] [ID 20000326.008] Shell.pm, cracked?
Message ID:
Pine.OSF.4.21.0007250944390.23977-100000@home.kiski.net
Evidently Wolfgang Laun submitted a bug report that mentioned some
problems with the POD and/or the implementation of Shell.pm with reference
to shell interpretation.  The origional perlbug report follows and a patch
after that.  

I believe that removing shell interepretation is limiting Shell.pm's scope
of usefulness so, I chose to match the implementation with the POD that
already exists, rather than changing the pod to reflect the limitation.

The patch below includes the bug fix, OO Interface and use()ing strict.

[ORIGIONAL perlbug REPORT]
This is a bug report for perl from laune@attk34.aut.alcatel.at,
generated with the help of perlbug 1.27 running under perl v5.6.0.


-----------------------------------------------------------------
[Please enter your report here]

Shell.pm fails to run the examples given in the pod, and exhibits
behaviour that contradicts the claim to "run shell commands
transparently" (whatever that means ;-)

As this module is undocumented, it is hard to tell what the original
author's intention was, and what was added, or changed, and why, by
whomsoever.

(Furthermore, the current implementation uses an unescaped here
document to avoid eval - happy maintenance. :-/)

   use Shell;
   $passwd = cat("</etc/passwd");  # from the DESCRIPTION
   print "cat=\n$passwd";

results in:

   cat: </etc/passwd: No such file or directory
   cat=

And consider:

   use Shell;
   $ls = ls();
   print "ls=\n$ls";
   $ls1 = ls( "*.pl" );
   print "ls1=\n$ls1";    

resulting in:

   ls=
   sort.pl
   try.pl
   try.pod
   unini.pl
   xxx.pl
   ls: *.pl: No such file or directory
   ls1=

++++++++++++++++

Note that this behaviour is due to the addition of apostrophe-quoting
of all arguments before they are joined and passed to open(..., "...|"),
inhibiting *all* shell interpretation.

Now I would send a patch, but things are not as simple as that.

Consider the example: $foo = echo("howdy", "<funny>", "world");
If arguments are not quoted any more, this results in an error,
   sh: funny: No such file or directory
With quoting, cat("</etc/passwd") doesn't work. (An earlier version
of Shell.pm, from 5.00503 and before, used exec, which differentiates
its behavior depending on the number of arguments, but this would not
have made a difference, either.)

The gist of all of this is, that the user must be aware of the shell
seeing the arguments, and use quoting accordingly.

WHAT IS THE INTENDED BEHAVIOUR OF THIS MODULE?


[Please do not change anything below this line]
-----------------------------------------------------------------

---
Site configuration information for perl v5.6.0:

Configured by laune at Thu Mar 16 16:55:29 CET 2000.

Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration:
  Platform:
    osname=linux, osvers=2.2.12-20, archname=i686-linux
    uname='linux attk34 2.2.12-20 #1 mon sep 27 10:40:35 edt 1999 i686
unknown '
    config_args='-de'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef
usemultiplicity=undef
    useperlio=undef d_sfio=undef uselargefiles=define 
    use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=undef
  Compiler:
    cc='cc', optimize='-O2', gccversion=egcs-2.91.66 19990314/Linux
(egcs-1.1.2 release)
    cppflags='-fno-strict-aliasing'
    ccflags ='-fno-strict-aliasing -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64'
    stdchar='char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=8
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lndbm -lgdbm -ldb -ldl -lm -lc -lposix -lcrypt
    libc=/lib/libc-2.1.2.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    v5.6.0-RC2

---
@INC for perl v5.6.0:
    /usr/local/lib/perl5/5.6.0/i686-linux
    /usr/local/lib/perl5/5.6.0
    /usr/local/lib/perl5/site_perl/5.6.0/i686-linux
    /usr/local/lib/perl5/site_perl/5.6.0
    /usr/local/lib/perl5/site_perl
    .

---
Environment for perl v5.6.0:
    HOME=/home/laune
    LANG=en
    LANGUAGE (unset)
    LC_ALL=en_US
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)

PATH=/usr/bin:/bin:/usr/X11R6/bin:/usr/local/bin:/opt/bin:/usr/X11R6/bin:/home/laune/bin:/usr/X11R6/bin:/home/laune/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash

[END perlbug REPORT]

-- 
print(join(' ', qw(Casey R. Tweten)));my $sig={mail=>'crt@kiski.net',site=>
'http://home.kiski.net/~crt'};print "\n",'.'x(length($sig->{site})+6),"\n";
print map{$_.': '.$sig->{$_}."\n"}sort{$sig->{$a}cmp$sig->{$b}}keys %{$sig}
my $VERSION = '0.01'; #'patched' by Jerrad Pierce <belg4mit at MIT dot EDU>

[PATCH]
3c3,4
< our($capture_stderr, $VERSION);
---
> use strict;
> our($capture_stderr, $VERSION, $AUTOLOAD);
5c6,9
< $VERSION = '0.2';
---
> $VERSION = '0.3';
> 
> sub new { bless \$VERSION, shift } # Nothing better to bless
> sub DESTROY { }
13,14c17
<     }
<     else {
---
>     } else {
17c20,21
<     foreach $sym (@EXPORT) {
---
>     foreach my $sym (@EXPORT) {
>         no strict 'refs';
20c24
< };
---
> }
22c26,27
< AUTOLOAD {
---
> sub AUTOLOAD {
>     shift if $_[0]->isa( 'Shell' );
29,30c34
<           }
<           elsif ('$^O' eq 'os2') {
---
>           } elsif ('$^O' eq 'os2') {
49,50c53
<               }
<               else {
---
>               } else {
57,58c60
<           }
<           else {
---
>           } else {
77,78c79
<               }
<               else {
---
>               } else {
81c82
<                       \$_ = "'\$_'";
---
>                       \$_ = \$_;
91,92c92
<               }
<               else {
---
>               } else {
106a107
> 
157a159,190
> There seemed to be a problem where all arguments to a shell command
where
> quoted before being executed.  As in the following example:
> 
>  cat('</etc/passwd');
>  ls('*.pl');
> 
> really turned into:
> 
>  cat '</etc/passwd'
>  ls '*.pl'
> 
> instead of:
> 
>   cat </etc/passwd
>   ls *.pl
> 
> and of course, this is wrong.
> 
> I have fixed this bug, it was brough up by Wolfgang Laun [ID
20000326.008]
> 
> Casey
> 
> =head2 OBJECT ORIENTED SYNTAX
> 
> Shell now has an OO interface.  Good for namespace conservation and
shell representation.
> 
>  use Shell;
>  my $sh = Shell->new;
>  print $sh->ls;
> 
> Casey
> 
162a196,197
> 
> Changes and bug fixes by Casey Tweten <crt@kiski.net>




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About