in an interactive session..
amt do you do your interactive session as the web server user (or the whoever runs your CGI) - it sounds as if you are anyway, but, judging by the error message - the remote home dir is var/www (web server user?) so maybe you really do have a permissions problem there..
I wonder if your config file parses correctly - if the remote $addr was wrong - you'd have this problem too I bet.
update
-- the perl code looks nice but I was wondering how you get the user to write files in /usr/local/etc/ on the remote host, or maybe you've got some special permissions set up on each remote box?
-harold