#!/usr/bin/perl # Copyright Andrew Gavin 2009, 2010 # # This file is part of OpenDLP. # # OpenDLP is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # OpenDLP is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with OpenDLP. If not, see . use CGI qw/:standard/; use DBI; my $user = remote_user(); my $MAX_LENGTH_PROFILE = 64; my $MAX_LENGTH_USERNAME = 128; my $MAX_LENGTH_PASSWORD = 255; my $MAX_LENGTH_PATH = 255; my $MAX_LENGTH_DOMAIN = 255; my $MAX_LENGTH_EXTS = 65535; my $MAX_LENGTH_DIRS = 65535; my $MAX_LENGTH_REGEXES = 65535; my $MAX_LENGTH_URL = 255; my $MAX_LENGTH_URL_USER = 32; my $MAX_LENGTH_URL_PASS = 32; my $MAX_DELAYTIME = 65535; my $MAX_LENGTH_DESCRIPTION = 255; my $MAX_CONCURRENT = 100; my $db_admin_file = "../etc/db_admin"; my $is_valid = 1; my( $profile, $username, $password, $domain, $dir_choice, $directories ) = ""; my( $ext_choice, $extensions, $path ) = ""; my( $db_username, $db_password ) = ""; my( $url, $urluser, $urlpass, $delaytime, $description, $debug, $concurrent ) = ""; my @regexes = (); open( DB, $db_admin_file ); my $db_line = ; close( DB ); chomp $db_line; ($db_username, $db_password) = split( ":", $db_line ); header(); if( request_method() ne "POST" ) { $is_valid = 0; print "Only POST requests are allowed. Try starting here.
\n"; } foreach $p (param()) { $form{$p} = param($p); if( $p eq "profilename" ) { $profile = $form{$p}; $profile =~ s/^\ +//g; $profile =~ s/\ +$//g; if( length( $profile ) > $MAX_LENGTH_PROFILE ) { $is_valid = 0; print "Profile is too long. Limit it to $MAX_LENGTH_SCAN characters.

\n"; } if( $profile !~ /^[a-z0-9\ \,\.\-\_]+$/i ) { $is_valid = 0; print "Profile cannot be blank and can only contain the following characters, including whitespace: A-Z0-9,.-_

\n"; } } if( $p eq "username" ) { $username = $form{$p}; $username =~ s/^\ +//g; $username =~ s/\ +$//g; if( length( $username ) > $MAX_LENGTH_USERNAME ) { $is_valid = 0; print "Username is too long. Limit it to $MAX_LENGTH_USERNAME characters.

\n"; } if( $username eq "" ) { $is_valid = 0; print "Username cannot be blank.

\n"; } } if( $p eq "password" ) { $password = $form{$p}; if( length( $password ) > $MAX_LENGTH_PASSWORD ) { $is_valid = 0; print "Password is too long. Limit it to $MAX_LENGTH_PASSWORD characters.

\n"; } } if( $p eq "domain" ) { $domain = $form{$p}; if( length( $domain ) > $MAX_LENGTH_DOMAIN ) { $is_valid = 0; print "Domain is too long. Limit it to $MAX_LENGTH_DOMAIN characters.

\n"; } if( $domain eq "" ) { $is_valid = 0; print "Domain/Workgroup cannot be blank.

\n"; } } if( $p eq "path" ) { $path = $form{$p}; $path =~ s/^\ +//g; $path =~ s/\ +$//g; if( length( $path ) > $MAX_LENGTH_PATH ) { $is_valid = 0; print "Path is too long. Limit it to $MAX_LENGTH_PATH characters.

\n"; } if( $path eq "" ) { $is_valid = 0; print "Path cannot be blank.

\n"; } } if( $p eq "dir_choice" ) { $dir_choice = $form{$p}; if( $dir_choice !~ /^(everything|ignore|allow)$/ ) { $is_valid = 0; print "Select a radio option for the directories.

\n"; } } if( $p eq "directories" ) { $directories = $form{$p}; if( length( $directories ) > $MAX_LENGTH_DIRS ) { $is_valid = 0; print "Total length of all directories must be less than " . $MAX_LENGTH_DIRS + 1 . " characters. Yours was a length of " . length($directories) . " characters.

\n"; } } if( $p eq "ext_choice" ) { $ext_choice = $form{$p}; if( $ext_choice !~ /^(everything|ignore|allow)$/ ) { $is_valid = 0; print "Select a radio option for the file extensions.

\n"; } } if( $p eq "extensions" ) { $extensions = $form{$p}; if( length( $extensions ) > $MAX_LENGTH_EXTS ) { $is_valid = 0; print "Total length of all file extensions must be less than " . $MAX_LENGTH_EXTS + 1 . " characters. Yours was a length of " . length($extensions) . " characters.

\n"; } my @temp_exts = split( "\r\n", $extensions ); my $line_exts = 1; foreach my $temp_ext( @temp_exts ) { chomp $temp_ext; if( $temp_ext eq "" || $temp_ext =~ /^\ +$/ ) { print "Blank or empty file extension on line $line_exts

\n"; $is_valid = 0; } $line_exts++; } } if( $p =~ /^regex_[0-9]+$/ ) { my $temp_regex = $p; if( $form{$temp_regex} == 1 ) { $temp_regex =~ s/^.*_//g; push @regexes, $temp_regex; } else { print "Invalid option for $temp_regex

\n"; $is_valid = 0; } } if( $p eq "url" ) { $url = $form{$p}; if( length( $url ) > $MAX_LENGTH_URL ) { $is_valid = 0; print "Length of phone home URL must be less than " . $MAX_LENGTH_URL + 1 . " characters. Yours was a length of " . length($url) . " characters.

\n"; } if( $url !~ /^http(s|):\/\// ) { $is_valid = 0; print "Phone home URL must start with \"http://\" or \"https://\".

\n"; } } if( $p eq "urluser" ) { $urluser = $form{$p}; if( length( $urluser ) > $MAX_LENGTH_URL_USER ) { $is_valid = 0; print "Length of phone home URL\'s username must be less than " . $MAX_LENGTH_URL_USER + 1 . " characters.

\n"; } if( $urluser eq "" ) { $is_valid = 0; print "Phone home URL\'s username cannot be blank.

\n"; } } if( $p eq "urlpass" ) { $urlpass = $form{$p}; if( length( $urlpass ) > $MAX_LENGTH_URL_PASS ) { $is_valid = 0; print "Length of phone home URL\'s password must be less than " . $MAX_LENGTH_URL_PASS + 1 . " characters.

\n"; } if( $urlpass eq "" ) { $is_valid = 0; print "Phone home URL\'s password cannot be blank.

\n"; } } if( $p eq "wait" ) { $delaytime = $form{$p}; if( $delaytime > $MAX_DELAYTIME ) { $is_valid = 0; print "Maximum delay time is $MAX_DELAYTIME.

\n"; } if( $delaytime !~ /^[0-9]+$/ ) { $is_valid = 0; print "Delay time must be a digit.

\n"; } } if( $p eq "description" ) { $description = $form{$p}; if( $description > $MAX_LENGTH_DESCRIPTION ) { $is_valid = 0; print "Length of description cannot exceed $MAX_LENGTH_DESCRIPTION.

\n"; } } if( $p eq "debug" ) { $debug = $form{$p}; if( $debug < 0 || $debug > 2 ) { $is_valid = 0; print "Log verbosity must either be 0, 1, or 2.

\n"; } } if( $p eq "concurrent" ) { $concurrent = $form{$p}; if( $concurrent < 1 || $concurrent > $MAX_CONCURRENT ) { $is_valid = 0; print "Concurrent must be between 1 and $MAX_CONCURRENT.

\n"; } } } if( $is_valid ) { my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password); my $string = "SELECT profile from profiles where profile=?;"; my $sth = $dbh->prepare( $string ); $sth->execute( $profile ); my $results = $sth->fetchrow_arrayref(); if( $$results[0] eq "" ) { my $regex_db = ""; my $length_regex = @regexes; my $x_regex = 0; while( $x_regex < $length_regex ) { $regex_db .= "$regexes[$x_regex]"; if( $x_regex < ($length_regex - 1)) { $regex_db .= ","; } $x_regex++; } $string = "INSERT INTO profiles SET profile=?,username=?,password=?,domain=?,exts=?,ignore_exts=?,dirs=?,ignore_dirs=?,regex=?,path=?,phonehomeurl=?,phonehomeuser=?,phonehomepass=?,delaytime=?,description=?,debug=?,concurrent=?;"; $sth = $dbh->prepare($string); $sth->execute($profile, $username, $password, $domain, $extensions, $ext_choice, $directories, $dir_choice, $regex_db, $path, $url, $urluser, $urlpass, $delaytime, $description, $debug, $concurrent ); print "New Profile Submission:

\n"; print "\n"; print "\n"; my $username_copy = filter( $username ); print "\n"; print "\n"; my $domain_copy = filter( $domain ); print "\n"; my $path_copy = filter( $path ); print "\n"; print "\n"; my $directories_copy = filter( $directories ); print "\n"; print "\n"; my $extensions_copy = filter( $extensions ); print "\n"; print "\n"; my $url_copy = filter( $url ); print "\n"; my $urluser_copy = filter( $urluser ); print "\n"; print "\n"; print "\n"; print "\n"; my $description_copy = filter( $description ); print "\n"; print "\n"; print "
Profile Name$profile
Username$username_copy
Password****************
Domain Name$domain_copy
Installation Path$path_copy
Scan directories$dir_choice
Directories$directories_copy
Scan file extensions$ext_choice
Extensions$extensions_copy
Regular Expressions"; my @regex_lookups = split(",", $regex_db ); my $length_regex_lookups = @regex_lookups; my $regex_lookups_x = 0; foreach( @regex_lookups ) { $string = "SELECT name from regexes where number=?;"; $sth = $dbh->prepare($string); $sth->execute($_); my $results = $sth->fetchrow_arrayref(); if( $$results[0] ne "" ) { print "$$results[0]"; } else { print "Cannot find regex #" . $_ . ""; } if( $regex_lookups_x < ($length_regex_lookups - 1 )) { print ", "; } $regex_lookups_x++; } print "
Phone home URL$url_copy
Phone home URL username$urluser_copy
Phone home URL password****************
Phone home delay$delaytime
Concurrent deployments$concurrent
Windows Service description$description_copy
Log Verbosity$debug
\n"; } else { print "Duplicate profile name found. Try again with a different profile name.\n\n"; } $sth->finish; $dbh->disconnect; } footer(); sub header { print "Content-type: text/html\n\n"; print "\n\n"; print "OpenDLP 0.1\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print '
' . "\n"; } sub footer { print "
\n"; } sub filter { my $thing = shift; $thing =~ s/\&/\&/g; $thing =~ s/\#/#/g; $thing =~ s/"/"/g; $thing =~ s/\%/%/g; $thing =~ s/\'/'/g; $thing =~ s/\////g; $thing =~ s//>/g; $thing =~ s/\[/[/g; $thing =~ s/\\/\/g; $thing =~ s/\]/]/g; $thing =~ s/`/`/g; $thing =~ s/{/{/g; $thing =~ s/\|/|/g; $thing =~ s/}/}/g; return $thing; }