OldScriptFinder: Documentation: Custom Definitions

Old Script Finder can use custom definitions - definitions that you've written yourself, to scan for your homemade scripts and other files with version information. Note that scanning with a custom definition is quite a bit slower than using a normal fingerprint. A custom definition is a short perl script. It is called whenever a potentially matching file is found, and it is expected to inspect the file to find the version number.

Custom Definition Directory

If you've never created a custom definition before, you'll need to create the directory for it. It is /var/oldscripts/customdefs , should be owned by root, and chmod 0700. You can then create your definition script inside it. For this example we'll create a definition for a "test" script, so we'll call it test, and create the definition script as /var/oldscripts/customdefs/test .

Script Information Section

The first thing you need for your custom definition is the Script Information section. This is a perl subroutine, named "loaddef_" plus the name of the script. For example, loaddef_myscript, loaddef_something or loaddef_botswana. Here's an example of this section:

#!/usr/bin/perl

sub loaddef_test {
   my %def;
   $def{latestversion} = '1.9.4';
   $def{filenames} = [ 'test.php' ];

   return \%def;
}

This tells Old Script Finder that the latest version of our script is 1.9.4 (note: which must be enclosed in quotes), and the filename we're looking for (which contains the version information) is test.php. That's everything we need to make Old Script Finder call our custom definition.

The Scan Section

This is the main section of our definition script. It should be in the same file as the Information Section above - for example, /var/oldscripts/customdefs/test . The scan section should be named "def_" plus the name of the script. Here's an example scan section:

sub def_test {
    my $file = shift;
    my $dir = shift;

    if (!open(FILE,$file)) {
       print "Couldn't open $file: $!\n";
       return -2;
    }

    while (<FILE>) {
       if (/\$version = ([\d\.]+)\;/) {
          return $1;
       }
    }
    close(FILE);
    return -1;
}
1;

The arguments to our subroutine are the full path to the file found, and the directory it's in. The file will be named test.php, as we specified previously. If we encounter an error, we return with a value of -2. If the script isn't a copy of the script we are looking for, we return -1. Otherwise if we find the version number successfully, we return the version number itself.

In the above example, you can see that we're opening $file, and returning -2 if that fails. We're then simply scanning through the entire file for a line which includes "$version = <VERSION NUMBER>;". If we find it (and the version number is a number or a period), we return that version number. Although this is slower than the fingerprint method that Old Script Finder uses itself, it's quite simple and effective.

If the version number isn't found, we return -1. In a real situation, we may want to make sure that the file is a copy of the script we're looking for, and return -1 if it isn't.

That's all you need to know to write your own custom definitions - apart, perhaps, from some more Perl Regex. Here's a slightly more complicated example of a full custom definition script, to be saved as /var/oldscripts/customdefs/elephant:

sub loaddef_elephant {
   my %def = ( latestversion => "1.5", filenames => [ "elephant.php" ] );
   return \%def;
}

sub def_elephant {
   my $file = shift;
   my $dir = shift;
    if (!open(FILE,$file)) {
       print "Couldn't open $file: $!\n";
       return -2;
    }

   my ($ver, $found);
    while (<FILE>) {
       if (/define\("VERSION","([\d\.]+)"\)/) {
          $ver = $1;
       }
      if (/define\("APPNAME","Elephant"\)/) {
         # This is definitely an elephant script
         $found = 1;
      }
      return $ver if (defined($ver) && $found);
    }
    close(FILE);
    return -1;
}
1;

That would match a file named "elephant.php" with this contents:

<?php

define("APPNAME","Elephant");
define("VERSION","1.4");

?>

Our elephant.php is then detected:

Old Script Finder v1.0
Checking for fingerprint updates...No updates available.
Scanning 1 directory...
/home/example/public_html/elephant contains an old elephant (1.4, latest version is 1.5)
All Finished. Found 0 up to date and 1 out of date scripts.

That's all!