Wise Monks,

Below is a script i have wrote which i realy need someone to check over to make sure it is secure.

#!/usr/bin/perl &GetFormInput ; $username = $field{'username'} ; $domain = $field{'domain'} ; $email = $field{'email'} ; $service = $field{'service'} ; $Signup = $field{'Signup'} ; $Cancel = $field{'Cancel'} ; $ip = $ENV{'REMOTE_ADDR'} ; $message = "" ; $found_err = "" ; $errmsg = "<p>Please Enter A Valid Username. [Between 3-30 Characters. + No Symbols Allowed.]</p>\n" ; if ($username eq "") { $message = $message.$errmsg ; $found_err = 1 ; } elsif (length($username) < 3) { $message = $message.$errmsg ; $found_err = 1 ; } elsif (length($username) > 30) { $message = $message.$errmsg ; $found_err = 1 ; } $errmsg = "<p>Error With Domain. Contact Support.</p>\n" ; if ($domain eq "") { $message = $message.$errmsg ; $found_err = 1 ; } elsif (length($domain) < 1) { $message = $message.$errmsg ; $found_err = 1 ; } elsif (length($domain) > 100) { $message = $message.$errmsg ; $found_err = 1 ; } $errmsg = "<p>Please Enter A Valid E-Mail Address. [Between 5-50 Chara +cters.]</p>\n" ; if ($email !~ /.+\@.+\..+/) { $message = $message.$errmsg ; $found_err = 1 ; } elsif (length($email) < 5) { $message = $message.$errmsg ; $found_err = 1 ; } elsif (length($email) > 50) { $message = $message.$errmsg ; $found_err = 1 ; } $errmsg = "<p>Error With Service. Contact Support.</p>\n" ; if ($service eq "") { $message = $message.$errmsg ; $found_err = 1 ; } elsif (length($service) < 1) { $message = $message.$errmsg ; $found_err = 1 ; } elsif (length($service) > 100) { $message = $message.$errmsg ; $found_err = 1 ; } if ($found_err) { &PrintError; } open (LOGFILE, ">> account.log") ; print LOGFILE "/etc/AddVirtUser.pl --username=$username --domain=$doma +in --email=$email --ip=$ip --service=$service\n" ; close (LOGFILE) ; print "Content-type: text/html\n\n"; print "<html>\n" ; print "<head>\n" ; print "blah blah blah\n" ; print "</body>\n" ; print "</html>\n" ; sub PrintError { print "Content-type: text/html\n\n"; print $message ; exit 0 ; return 1 ; } sub GetFormInput { (*fval) = @_ if @_ ; local ($buf); if ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN,$buf,$ENV{'CONTENT_LENGTH'}); } else { $buf=$ENV{'QUERY_STRING'}; } if ($buf eq "") { return 0 ; } else { @fval=split(/&/,$buf); foreach $i (0 .. $#fval){ ($name,$val)=split (/=/,$fval[$i],2); $val=~tr/+/ /; $val=~ s/%(..)/pack("c",hex($1))/ge; $name=~tr/+/ /; $name=~ s/%(..)/pack("c",hex($1))/ge; if (!defined($field{$name})) { $field{$name}=$val; } else { $field{$name} .= ",$val"; #if you want multi-selects to goto into an array chang +e to: #$field{$name} .= "\0$val"; } } } return 1; }


I also need to have the variables $username, $domain, $email, $ip and $service to be checked for nasty characters.

$Username can only accept A-Z, a-z, 1-9 and the following characters .-_
$Domain can only be in the form of domain.com
I believe i have $email sorted already.
$Service can only equal Yes or No

If one of you geniuses could modify my code to perform the above, it would be most appreciated

In reply to Need help with security by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.