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

I realize how dangerous this is, but would it be possible to have a Perl script access and modify the current .htaccess file?

I'd like to be able to add/remove Redirects, Errors, etc. on the fly.

Thanks

Replies are listed 'Best First'.
Re: Can a Perl script modify .htaccess
by hippo (Archbishop) on Jul 22, 2019 at 21:43 UTC

    Yes, it is both dangerous and possible. Apache::Admin::Config and taint mode will help to limit the danger, but only a bit.

Re: Can a Perl script modify .htaccess
by soonix (Chancellor) on Jul 22, 2019 at 21:50 UTC

      Sorry, I see my error now

      The documentation says my $obj = Apache::Htaccess->new($path_to_htaccess); and I took that to literally mean just the path.

      My apologies I meant to say I tried that module. And it has so many warnings and I am having trouble figuring it out.

      I'll call  my $obj = Apache::Htaccess->new('/var/www/examplecom/public_html/');

      And I get

      Use of uninitialized value in substitution (s///) at /usr/local/share/ +perl/5.26.1/Apache/Htaccess.pm line 63. Use of uninitialized value in pattern match (m//) at /usr/local/share/ +perl/5.26.1/Apache/Htaccess.pm line 67. Use of uninitialized value in substitution (s///) at /usr/local/share/ +perl/5.26.1/Apache/Htaccess.pm line 68. Use of uninitialized value in substitution (s///) at /usr/local/share/ +perl/5.26.1/Apache/Htaccess.pm line 86. Use of uninitialized value in pattern match (m//) at /usr/local/share/ +perl/5.26.1/Apache/Htaccess.pm line 92.

      If there were some sample documentation maybe I'd have a chance. My knowledge base is extremely limited in Perl and Linux, and I've Googled this module a lot to try and find samples. So I was just thinking if I could run a Cron job to do what I needed to do that might be a way to get done what I was trying to do.

      So after a lot of testing, and playing I finally got the correct code. However in the end the changes I make only get saved if someone other than root owns .htaccess

      Not sure whether this follow up question belongs here in the replies or in a new question, but what would be the safest ownership of .htaccess so that I can change it from Perl scripts run via CGI?

        Assuming that you have exhausted all the other, safer means of achieving the same goal then in your place I might:

        • Ensure AllowOverride is set to the smallest list feasible
        • Create a user and group specifically for this task
        • Ensure that only the files to be modified are owned by this user with tight perms (0644 should be easily lax enough)
        • Set up suEXEC for just the one script to be run by this user/group
        • Have an already-written contingency plan for when this system is broken into.