Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

patricide / matricide

by dshahin (Pilgrim)
on Mar 10, 2001 at 04:21 UTC ( [id://63410]=perlquestion: print w/replies, xml ) Need Help??

dshahin has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to be able to kill the shell that invoked my perl program
BASH offers a $PPID and sh offers $$, but $$ gets translated to to the pid of perl, not the underlying shell.
basically, my program is invoked at login, and when it exits, I want to make sure the user logs out instantly,
regardless of their chosen shell

any tips as to determing the pid of the shell that invoked your perl interpreter?

(extra credit: is there a term that means killing one's parents that is not gender specific?)

Replies are listed 'Best First'.
Re (tilly) 1: patricide / matricide
by tilly (Archbishop) on Mar 10, 2001 at 04:52 UTC
    Why not solve this problem in the invoking shell script by execing Perl rather than launching it? That is both much less work and more secure.

    Which reminds me. My favorite silly way to exit a shell is

    exec echo Hasta la Vista!
      we have a winner!

      this is the simplest and best solution I've come across yet!

      thanks!!

Re: patricide / matricide
by Hot Pastrami (Monk) on Mar 10, 2001 at 04:26 UTC
    Not sure on the PID thing (one of the geniuses will have to pick that one up), but I've got the extra credit:

    par·ri·cide
    1. The murdering of one's father, mother, or other near relative. 2. One who commits such a murder.

    Hot Pastrami
Re: patricide / matricide
by athomason (Curate) on Mar 10, 2001 at 04:34 UTC
    Try the builtin getppid.

    Update:
    I just looked at what you're doing more closely, and I'm a bit curious about how you intend to guarantee that your kill call actually gets executed. A crafty user (if there are any trying to subvert your desires), could potentially interrupt or kill your script before it gets to the end, leaving them with an open shell. If your script is such that they'll never get the opportunity to do something like that, then you'll be fine. But just in case, you may want to toss in some signal handlers (e.g. SIGINT, SIGKILL, etc) that kills the shell as well.

      I am supposed to trust my users to some degree.
      I know, I know....
      I just want to make sure they don't accidentally escape to a shell with a ctrl-d or exit
        SECURITY RULE NUMBER ONE : Trust nobody !

        You should never trust a user nor any data and barely a program if you want to have a minimal security...


        "Trying to be a SMART lamer" (thanx to Merlyn ;-)
Re: patricide / matricide
by Merlin42 (Friar) on Mar 10, 2001 at 07:08 UTC
    I assume that you are thinking about having the perl script exec'ed from a login script. Which is inherently insecure. A quick fingered user could hit ctrl-c just b4 the perl script was executed and get to the command prompt. I have actually done this personally. The best way to do this is to NOT have a real shell. ie set the users shell (in /etc/passwd) to the perl script (and add the script to /etc/shells on most systems). That way there NEVER is a functional shell for the possibly malicious user to get into.
Re: patricide / matricide
by the_slycer (Chaplain) on Mar 10, 2001 at 12:12 UTC
    Just for fun, this works as long as the script is only running under a parent shell :-)
    my $psres = `ps -xf`; my @psres=split /\n/,$psres; for (my $i = 0; $i < @psres; ++$i){ if ($psres[$i] =~ /bash/ && $psres[++$i] =~ /\s+\\_\sperl\sscriptn +ame\.pl/){ print "$1\n" if $psres[--$i] =~ /\s(\d+[^\s])/; } }
    Obviously though, there are much better solutions to what you actually wanted above..
Re: patricide / matricide
by Anonymous Monk on Mar 10, 2001 at 22:05 UTC
    you can set the program as your shell in /etc/passwd if this is the only thing you'll be executing on a givin account. Also, if the program is interrupted, you can place the getpid'ed shell's ID kill statement in the END {...} subroutine. It's not a totally sure fire way i imagine, but it appears that even if the user ^C terminates the program, then anything in END {...} (err. handling, etc) will be executed. Of course, this doesn't apply to ^Z suspention. You could use signal handlers (as already mentioned) which would probably garantee proper termination much more than END will.
Re: patricide / matricide
by Anonymous Monk on Mar 11, 2001 at 06:00 UTC
    Why not just make the script the shell for the user?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://63410]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found