Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

sourcing a shell script/.profile and importing into %ENV

by gnu@perl (Pilgrim)
on Mar 21, 2003 at 17:01 UTC ( [id://244933] : perlquestion . print w/replies, xml ) Need Help??

gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

I have a program that is run as root and for certain tasks it changes $> to become another user then goes back to root for more work. This all works fine, except for one thing. The user that I change to needs to have it's environment updated with new $PATH info as well as some new environmental variables.

It's not hard to code them into the program, but that causes a problem. The environmental variables that need to be set get changed from time to time. The .profile of the user I become (in the new assignment to $>) is of course sourced for that user when they log in. In my script the simple assignment of their UID to $> does not do this, so many of the routines fail.

Does anyone know of a way to source a .profile while in a perl script? This .profile runs some commands and programs to set things, so it's not as easy as just picking out the relavent parts and adding them to %ENV.

Please don't take offense, but I do not want this to turn into a security debate. I do realize that doing this is a security risk. I just need to know how/if it can be done.

TIA, Chad.

  • Comment on sourcing a shell script/.profile and importing into %ENV

Replies are listed 'Best First'.
Re: sourcing a shell script/.profile and importing into %ENV
by ChemBoy (Priest) on Mar 21, 2003 at 19:51 UTC

    It isn't pretty, something along these lines should do the trick:

    #!/usr/bin/perl $sourcefile = "/etc/"; # or whatever chomp(@newenv = qx ( . $sourcefile; env) ); foreach (@newenv) { ($k,$v) = split "=",$_,2; $ENV{$k}=$v; }

    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

      I must apologize to ChemBoy. In my haste with another issue I misread his post. This will work quite well. Thank you.

      yay++ ChemBoy

      this is also way useful when your *$@*$ sysadmin people only create setup.csh scripts for applications and not the equivalent scripts...

      Thanks for your input, but that is sort of the problem I am having. The .profile for the user that I need to source is not just NAME=VALUE pairs, but it has logic in it to set vars under certain circumstances. It even has calls to other programs that it sources, so it has to be run as '. .profile' would do it.

      Yeah, a dumb design, but that's what I have to work with. I didn't do it, but I'm now stuck with it.

Re: sourcing a shell script/.profile and importing into %ENV
by Thelonius (Priest) on Mar 21, 2003 at 19:56 UTC
    You should be able to do:
    my $shell = $ENV{SHELL} || '/bin/sh'; # or maybe get user's shell fro +m /etc/passwd my $home = whatever ... my $envstr = qx($shell -c '. $home/.profile; env'); # some error checking should go on here my %NEWENV = map { split /=/, $_, 2 } split /\n/, $envstr;
    or something like that.
A reply falls below the community's threshold of quality. You may see it by logging in.