home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / cops_104.zip / cops_104 / extra_src / trust.pl < prev    next >
Perl Script  |  1992-03-10  |  4KB  |  166 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # Checks a host.equiv file to see what it trusted
  4. #
  5. #  trust.he [-h host.equiv] [-d]
  6. #
  7. #  options specify alternate data files, except the -d, which is the debug flag.
  8. #
  9. #  Originally written by me, but then tim tessin grabbed it and optimized
  10. # it.  This is one of the building blocks for SATAN, BTW.  You'll see
  11. # more of this later...
  12. #
  13. #  - d
  14. #
  15. #  routine tjt hack job - 10/28/91
  16. #  - deleted netgroup file specifcation.  netgroups is always gotten 
  17. #    from yp maps, never from the file.
  18. #  - simplified and optimized second pass, store intermediate values
  19. #    avoiding second open.  Code runs in 1/3 the time. 
  20. #  - make the %trusted array hold individual hostnames, so we can subtract
  21. #    more easily.
  22. #  - implemented subtract option.
  23. #  - fixed a bug or two adding single hosts.
  24. #  - output one per line so I can sort and diff it to test it.
  25. #  - Wow! Total rethink of algorithm.  Need to tag untrusted as well
  26. #    as trusted cuz 1st match either way causes access search to 
  27. #    terminate. 
  28. #  - single entries can have - as per spec
  29. #  - lots of error checking bypassed.  Do we need it?  If there are
  30. #    syntax errors will system allow access? In what cases?
  31. #
  32.  
  33. # Process the command args...
  34. require 'getopts.pl';
  35. $usage = "Usage: $0  [-h host.equiv] [-n netgroup] [-d]\n";
  36. die $usage unless &Getopts('h:n:d');
  37.  
  38. if (!defined($opt_h)) { $hosts_equiv = "/etc/hosts.equiv"; }
  39. else { $hosts_equiv = $opt_h; }
  40.  
  41. &init_net_group;
  42.  
  43. open (HE, $hosts_equiv) || die "Can't open $hosts_equiv\n";
  44. while (<HE>) {
  45.     chop;
  46.     /^\s*#/ && next;
  47.     /^\s*$/ && next;         # trash garbage
  48.     /^\+\s*$/ && do {        # + in hosts.equiv is trouble
  49.         print "Trust WORLD\n";
  50.         exit 0;
  51.         };
  52.  
  53.     ($sign,$at,$name) = /^([+-])?(@)?(.*)/;
  54.     print "sign: $sign at: $at name: $name\n" if $opt_d;
  55.     unless ($at) {
  56.         if ($sign eq "-") {
  57.             $untrusted{$name} = $name unless ($trusted{$name});
  58.             }
  59.         else {
  60.             $trusted{$name} = $name unless ($untrusted{$name});
  61.             }
  62.         }
  63.     # handle netgroups now...
  64.     else {
  65.         # add
  66.         if ($sign ne "-") {
  67.             for (split(/\s+/,$members{$name})) {
  68.                 $trusted{$_} = $_ unless $untrusted{$_};
  69.                 }
  70.             print "Add $name:\n", $members{$name},"\n" if $opt_d;
  71.             }
  72.         # delete
  73.         else {
  74.             for (split(/\s+/,$members{$name})) {
  75.                 $untrusted{$_} = $_ unless $trusted{$_};
  76.                 }
  77.             print "Subtract $name:\n", $members{$name},"\n" if $opt_d;
  78.             }
  79.         }    # end of netgroup stuff
  80.     }
  81. close(HE);
  82.  
  83. for $trust (values %trusted) {
  84.     print "$trust\n";
  85.     }
  86.  
  87. #############################
  88. sub init_net_group {
  89.  
  90. #   Make two passes through netgroup file -- first pass grabs all the
  91. # groupnames for expansion when parsing the triples
  92.  
  93. #  1st Pass:
  94. #
  95. # get the net groups for the 2nd pass:
  96. if (defined($opt_n)) { open (NG, $opt_n) || die "Can't open $opt_n\n"; }
  97. else { open (NG, "ypcat -k netgroup |") || die "Can't open netgroups\n";}
  98. while (<NG>) {
  99.     chop;
  100.     push(@lines,$_);
  101.     ($group, @members) = split(/\s+/, $_);
  102.     $member = pop(@members);
  103.     for (@members) { $member .= " $_"; }
  104.  
  105.     if ($second_pass{$group} eq "") {
  106.         $second_pass{$group} = $member;
  107.         }
  108.     else { warn "Duplicate net-group found: $group\n"; }
  109.  
  110.     $member = "";
  111.     }
  112. close NG;
  113.  
  114. #  2nd Pass:
  115. #
  116. foreach $line (@lines) {
  117.     ($group, @members) = split(/\s+/, $line);
  118.  
  119.     print "\n===>: $group\nMembers: @members\n\n" if ($opt_d);
  120.  
  121.     $wild1 = $wild2 = $wild3 = 0;
  122.     for (@members) {
  123.         unless ( ($machine,$name,$domain) = /\((.*),(.*),(.*)\)/ ) {
  124.             $netgrp = $_;
  125.             }
  126.  
  127.         if ($machine || $name || $domain) {
  128.             print "line: ($machine,$name,$domain)\n" if ($opt_d);
  129.             $wild1 = 1 unless $machine;
  130.             $wild2 = 1 unless $name;
  131.             $wild3 = 1 unless $domain;
  132.  
  133.             $wild1 = -1 if ( $machine && $machine =~ /^\W/ );
  134.             $wild2 = -1 if ( $name && $name =~ /^\W/ );
  135.             $wild3 = -1 if ( $domain && $domain =~ /^\W/ );
  136.  
  137.             # wildcards or not; no action if $wild1 == -1:
  138.             if ($wild1 > 0) {
  139.                 $members{$group} = "WILDCARD";
  140.                 }
  141.             elsif (!$wild1) {
  142.                 if ($members{$group} eq "") {
  143.                     $members{$group} = $machine; }
  144.                 elsif ($members{$group} ne "WILDCARD") {
  145.                     $members{$group} .= " $machine"; }
  146.                 }
  147.             }
  148.         else {
  149.             print "line: $netgrp\n" if ($opt_d);
  150.             print "PUSHING $netgrp: $second_pass{$netgrp}\n" if $opt_d;
  151.             # what if groups instead of (,,) stuff?
  152.             @stuff = split(/\s+/, $second_pass{$netgrp});
  153.             for $i (@stuff) {
  154.                 next if ($i eq "");
  155.                 print "PUSH $i\n" if $opt_d;
  156.                 push(@members, $i);
  157.                 }
  158.             }
  159.         }
  160.  
  161.     print "\nSAVED:",  $group, " ", $members{$group}, "\n" if $opt_d;
  162.     $line = "";
  163.     }
  164.  
  165. }    # end of netgroup stuff
  166.