#!/usr/bin/perl
#
# $Id: ngen,v 1.10 2001/07/02 21:33:06 levine Exp $
#
#
# Copyright (C) 2000, 2001  James D. Levine (jdl@vinecorp.com)
#
#
#   This program 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 2
#   of the License, or (at your option) any later version.
# 
#   This program 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 this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
#   02111-1307, USA.
#
####################################################################

use strict;
use Getopt::Long;
use PortScan::ScannedHost;
use PortScan::ScanContext;
use PortScan::DataStore;
use PortScan::ScanSet;
use PortScan::IPAddress;

sub usage
{
    print <<DONE;

 ngen [-o|-output <filename-or-:tag>] [-p|-ports <port list>] 
      [-h|-host <specification> ... -h|-host <specification N>]

      <specification> = 
      [!]<host spec>[:<port spec>][:ignored state] 

DONE
    ;
    exit 1;
}

my $pspecs;
my @hspecs = ();
my $output_tag = "%F-baseline";
my $help = 0;

GetOptions(
	   "p|ports=s"    => \$pspecs,
	   "h|host=s@"    => \@hspecs,
	   "o|output=s"   => \$output_tag,
	   "help"         => \$help,
	   );

usage() if $help || ! scalar ( @hspecs );

$output_tag = PortScan::DataStore::prepare_tag($output_tag);

my ($processed_output_tag, $output) = PortScan::DataStore::data_store_for($output_tag);

my $results_set = &PortScan::IPAddress::make_scanset( \@hspecs, $pspecs, "closed" );

$results_set->tag($processed_output_tag);

exit $output->put_scanset($results_set);


sub tests
{
    my $plist = sub { my $f = shift; foreach my $i (@$f) { print $i->nmap_format, "\n"; } };

    my $ports = &expand_port("22222fx");

    &$plist($ports);
    
    my $iplist = sub { my $f = shift; foreach my $i (@$f) { print $i, "\n"; } };
#    my $ips = &expand_ip("1-20");
#    &$iplist($ips);


    my $ips = &expand_host_range("248.2.3.4/20");
    &$iplist($ips);

}




=head1 NAME

ngen - create a baseline set of nmap results from command-line
specifications and/or an nmap results file.

=head1 SYNOPSIS

 ngen [-o|-output <filename-or-:tag>] [-p|-ports <port list>] 
      [-h|-host <specification> ... -h|-host <specification N>]

      <specification> = 
      [!]<host spec>[:<port spec>][:ignored state] 


=head1 DESCRIPTION

The ngen utility creates an nmap-style machine-readable results
file for use as a baseline for ndiff comparision.  

=head1 OPTIONS

=over 4

=item -o <filename-or-:tag>

=item -output <filename-or-:tag>

Specifies the output filename, or optionally a data store
tag, if begins with a colon (:).  See L<"DATA STORES"> below
for more information.


=item -p <port list>

=item -ports <port list>


Specifies the set of all ports scanned, comma-separated.  Ranges may be
specified.  For example:

    -p 80,443,6000-6010,22
    -ports 53

By default, tcp is assumed; udp may be specified by appending the "u" flag.
("t" may be specified but is redundant)

    -p 323u,80,6000-6010t

If the B<-p> option is omitted, ngen will grab the set of ports from
/usr/local/lib/nmap/nmap-services and /etc/services.

The default, "Ignored" specified for a given host is applied to all
ports in this list, except for "interesting" ports, which are
specified below with the B<-h> option.

See the description of port specifications with the B<-h> option
below for more information.


=item -h [!]<host ranges>[:<port ranges>][:<ignored state>]

=item -host  [!]<host ranges>[:<port ranges>][:<ignored state>]


Adds a host or range of hosts.  For example

    -h 192.168.2.2                   # one host
    -h 10.0.2.0-64                   # 65 hosts
    -host 192.168.1.0/26             # 64 hosts
    -host 192.168.*.*                # 65536 hosts

  
The above examples add hosts with all ports closed.  To add
ports to the hosts, append a colon and a port spec.  For example
to add localhost with tcp ports 80 and 53 open, ignored state "closed",
the specification would be

    -host 127.0.0.1:80,53

Flags may be appended to change the protocol or state for a given port, for example:

    -host 127.0.0.1:7uf

which adds echo service, udp port 7 filtered state.  


The full list of port flags are as follows:


    t - tcp port  (default)
    u - udp port

    o - port is in open state (default)
    c - port is in closed state 
    f - port is in filtered state
    x - port is in unfiltered state


The ignored state for the host may be specified by appending a colon and 
the state to the host spec.   Acceptable state labels are "open", "closed",
"filtered", and "unfiltered".

  -h 10.0.2.0:80f:closed

which says all scanned ports default to closed state, except for 80/tcp which
is filtered.

A host spec is treated as a negation if it starts with "!".  If ports are specified
as part of the host spec, those ports are deleted from any hosts previously added
which fall in the host range.  

Host specs are applied in order as they appear on the command line, and their
effects are cumulative.

=back

=head1 DATA STORES

Nrun and its related tools can manipulate results in regular nmap-format
files, in any user-specified location, or they can handle storing and organizing
the data on behalf of the user, through a user-configurable "data store".  

Whenever you precede a results tag with a colon (:), the tag will be treated
as a unique key into a data store, identifying the results set.  

Currently the only supported data store is  nmap format files placed
in a preconfigured directory.  Other types may be added at a later date.  

A legal tag may contain any alphanumeric string, plus dash, underscore, and dot.
%-style substitutions in the ilk of the "date" command are also supported,
allowing a tag to contain date, time, or the local hostname.  See L<"SUBSTITUTIONS">
below for more information.

=head1 SUBSTITUTIONS


%-style substitutions supported in tags as follows:

=over 4

=item %H = hour

=item %M = minute

=item %S = second 

=item %D = day of month

=item %m = month of year (01-12)

=item %Y = year, four digits

=item %j = day of year, three digits

=item %w = day of week (0-6) one digit

=back

Except where noted, the above items are two digits, and local time.  All are zero-padded
as appropriate.

In addtion-

=over 4

=item %F = output of "hostname" on the local machine

=back


=head1 BUGS

It is possible to create invalid nmap result files with ngen if you really
try to.
No support for human-readable hostnames and portnames.

=head1 AUTHOR

James Levine <jdl@vinecorp.com>

=cut













