home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / acld-1.11.tar.gz / acld-1.11.tar / acld-1.11 / acl.exp.in < prev    next >
Text File  |  2011-11-03  |  13KB  |  508 lines

  1. #!@EXPECT@ -f
  2. # @(#) $Id: acl.exp.in 802 2011-11-04 00:51:21Z leres $ (LBL)
  3. #
  4. # acl.exp - talk to acld
  5. #
  6. #  Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011
  7. #    The Regents of the University of California.  All rights reserved.
  8. #
  9. #  Redistribution and use in source and binary forms, with or without
  10. #  modification, are permitted provided that: (1) source code distributions
  11. #  retain the above copyright notice and this paragraph in its entirety, (2)
  12. #  distributions including binary code include the above copyright notice and
  13. #  this paragraph in its entirety in the documentation or other materials
  14. #  provided with the distribution, and (3) all advertising materials mentioning
  15. #  features or use of this software display the following acknowledgement:
  16. #  ``This product includes software developed by the University of California,
  17. #  Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  18. #  the University nor the names of its contributors may be used to endorse
  19. #  or promote products derived from this software without specific prior
  20. #  written permission.
  21. #  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  22. #  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  23. #  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24. #
  25.  
  26. set prog $argv0
  27. set len [string length $prog]
  28. for { set i $len } { $i > 0 } { set i [expr $i - 1] } {
  29.     if { [string index $prog $i ] == "/" } {
  30.         incr i
  31.         set prog [string range $prog $i $len]
  32.         break
  33.     }
  34. }
  35.  
  36. match_max 2048
  37.  
  38. set f "[string trimright $prog ".exp"].log"
  39. if [catch {log_file -a /var/log/acld/$f} err] {
  40.     send_error "$prog: warning: $err\n"
  41.     if [catch {log_file -a $f} err] {
  42.         send_error "$prog: warning: $err\n"
  43.     }
  44. }
  45. log_user 0
  46.  
  47. if [catch {set ident $env(USER)} err] {
  48.     set ident "[exec id -un]"
  49. }
  50. if [catch {set host $env(HOST)} err] {
  51.     if [catch {set host $env(HOSTNAME)} err] {
  52.         set host "[exec hostname]"
  53.     }
  54. }
  55.  
  56. # Adjust path to pick up socket
  57. set env(PATH) "/usr/local/bin:$env(PATH)"
  58.  
  59. if { $host != "" } {
  60.     append ident "@$host"
  61. }
  62.  
  63. #
  64. # Print usage and exit
  65. #
  66. proc usage {} {
  67.     global prog
  68.  
  69.     set version "@VERSION@"
  70.     send_error "Version: $version\n"
  71.     set c "\[-c comment ...\]"
  72.     send_error "usage: $prog \[drop|restore|query] addr $c\n"
  73.     send_error "       $prog \[drop|restore|query] network/mask $c\n"
  74.     send_error "       $prog \[drop|restore|query] -f file $c\n"
  75.     send_error "       $prog \[blockhosthost|restorehosthost] addr1 addr2 $c\n"
  76.     send_error "       $prog \[blockhosthost|restorehosthost] -f file $c\n"
  77.     send_error "       $prog \[dropudpport|restoreudpport] port ACL $c\n"
  78.     send_error "       $prog \[droptcpport|restoretcpport] port ACL $c\n"
  79.     send_error "       $prog \[permitudpdsthostport|unpermitudpdsthostport] addr port \\\n\t\t$c\n"
  80.     send_error "       $prog \[permittcpdsthostport|unpermittcpdsthostport] addr port \\\n\t\t$c\n"
  81.     send_error "       $prog \[droptcpdsthostport|restoretcpdsthostport] addr port \\\n\t\t$c\n"
  82.     send_error "       $prog \[nullzero|nonullzero|querynullzero] addr $c\n"
  83.     send_error "       $prog \[nullzero|nonullzero|querynullzero] network/mask $c\n"
  84.     send_error "       $prog \[nullzero|nonullzero|querynullzero] -f file $c\n"
  85.     send_error "       $prog \[addwhitelist|remwhitelist|querywhitelist] \\\n\t\t\[addr|net/width|net mask] $c\n"
  86.     send_error "       $prog \[addwhitelist|remwhitelist|querywhitelist] -f file \\\n\t\t$c\n"
  87.     send_error "       $prog listacl ACL $c\n"
  88.     send_error "       $prog compact ACL $c\n"
  89.     send_error "       $prog listroute $c\n"
  90.     send_error "       $prog whitelist $c\n"
  91.     send_error "       $prog state $c\n"
  92.     exit 1
  93. }
  94.  
  95. #
  96. # Reap response
  97. #
  98. proc getresponse { what cookie } {
  99.     global prog
  100.     global comment
  101.     global lasttime
  102.     global dst
  103.  
  104.     set response "^(\[0-9]+\.\[0-9]+) $cookie $what"
  105.     set doingcomment 0
  106.     set comment ""
  107.     set status 0
  108.     set lasttime 0
  109.     if { $what == "compact" || $what == "listacl" } {
  110.         set timeout 3600
  111.     } else {
  112.         set timeout 90
  113.     }
  114.     expect {
  115.         -re "$response\r\n" {
  116.             set lasttime $expect_out(1,string)
  117.             # done
  118.         }
  119.         -re "$response -\r\n" {
  120.             set lasttime $expect_out(1,string)
  121.             set doingcomment 1
  122.             # done
  123.         }
  124.         -re "$response-failed\r\n" {
  125.             set lasttime $expect_out(1,string)
  126.             set status 1
  127.             # done
  128.         }
  129.         -re "$response-failed -\r\n" {
  130.             set lasttime $expect_out(1,string)
  131.             set doingcomment 1
  132.             set status 1
  133.             # done
  134.         }
  135.         -re "^(.*)\[\r\n]*\r\n" {
  136.             send_error "$prog: $expect_out(1,string)\n"
  137.             exp_continue
  138.         }
  139.         timeout {
  140.             send_error "$prog: $dst: timeout $what ($timeout secs)\n"
  141.             exit 1
  142.         }
  143.         eof {
  144.             send_error "$prog: $dst: child exited\n"
  145.             exit 1
  146.         }
  147.     }
  148.     if { $doingcomment != 0 } {
  149.         expect {
  150.             "^.\r\n" {
  151.                 if { $lasttime == 0 } {
  152.                     send_error \
  153.                         "$prog: missing lasttime at EOM\n"
  154.                     exit 1
  155.                 }
  156.                 # done
  157.             }
  158.             -re "^(\[^\r\n]*)\r\n" {
  159.                 append comment "$expect_out(1,string)\n"
  160.                 exp_continue
  161.             }
  162.             timeout {
  163.                 send_error \
  164.                     "$prog: timeout2 $what ($timeout secs)\n"
  165.                 exit 1
  166.             }
  167.             eof {
  168.                 send_error "$prog: child exited2\n"
  169.                 exit 1
  170.             }
  171.         }
  172.     }
  173.     if { $comment != "" } {
  174.         if { $status != 0 } {
  175.             send_error "$prog: acld error: $comment"
  176.         } elseif { $what != "acld" } {
  177.             send_user "$comment"
  178.         }
  179.     }
  180.     return $status
  181. }
  182.  
  183. # Send acld commands with identification
  184. proc sendcmd {line} {
  185.     global ident
  186.     global usercomment
  187.  
  188.     send -- "$line -\r"
  189.     send -- "{$ident}\r"
  190.     if { [string length $usercomment] > 0 } {
  191.         send -- "$usercomment\r"
  192.     }
  193.     send -- ".\r"
  194. }
  195.  
  196. ##############################################################################
  197.  
  198. #
  199. # Main program
  200. #
  201. if { $argc < 1 } {
  202.     usage
  203. }
  204.  
  205. set what [lindex $argv 0]
  206. set file ""
  207. set addr1 ""
  208. set addr2 ""
  209. set port ""
  210. set acl ""
  211. set acl
  212. if [catch {set aclipaddr $env(ACLIPADDR)} err] {
  213.     set aclipaddr "127.0.0.1"
  214. }
  215. if [catch {set aclport $env(ACLPORT)} err] {
  216.     set aclport "1965"
  217. }
  218.  
  219. # Optional user comment (must trail due to the way expect processes arguments)
  220. set usercomment ""
  221. for { set i 0 } { $i < $argc } { set i [expr $i + 1] } {
  222.     if { [lindex $argv $i] == "-c" } {
  223.         set usercomment "[lrange $argv [expr $i + 1] [expr $argc - 1]]"
  224.         if { $usercomment == "." } {
  225.             set $usercomment ".$usercomment"
  226.         }
  227.         set argc $i
  228.         break
  229.     }
  230. }
  231.  
  232. # Validate flags
  233. for { set i 1 } { $i < $argc } { set i [expr $i + 1] } {
  234.     set a [lindex $argv $i]
  235.     if { [string index $a 0] == "-" } {
  236.         if { $a == "-f" &&
  237.             ( $what == "drop" || \
  238.             $what == "restore" || \
  239.             $what == "query" || \
  240.             $what == "blockhosthost" || \
  241.             $what == "restorehosthost" || \
  242.             $what == "nullzero" || \
  243.             $what == "nonullzero" || \
  244.             $what == "querynullzero" || \
  245.             $what == "addwhitelist" || \
  246.             $what == "remwhitelist" || \
  247.             $what == "querywhitelist" ) } {
  248.             continue
  249.         }
  250.         usage
  251.     }
  252. }
  253.  
  254. if { $what == "drop" || $what == "restore" || $what == "query" } {
  255.     if { [lindex $argv 1] == "-f" } {
  256.         if { $argc != 3 } {
  257.             usage
  258.         }
  259.         set file [lindex $argv 2]
  260.         if [catch {open $file} fd] {
  261.             send_error "$prog: $fd\n"
  262.             exit 1
  263.         }
  264.         set fd [open $file]
  265.     } elseif { $argc == 2 } {
  266.         set addr1 [lindex $argv 1]
  267.     } else {
  268.         usage
  269.     }
  270. } elseif { $what == "blockhosthost" || $what == "restorehosthost" } {
  271.     if { [lindex $argv 1] == "-f" } {
  272.         if { $argc != 3 } {
  273.             usage
  274.         }
  275.         set file [lindex $argv 2]
  276.         if [catch {open $file} fd] {
  277.             send_error "$prog: $fd\n"
  278.             exit 1
  279.         }
  280.         set fd [open $file]
  281.     } elseif { $argc == 3 } {
  282.         set addr1 [lindex $argv 1]
  283.         set addr2 [lindex $argv 2]
  284.     } else {
  285.         usage
  286.     }
  287. } elseif { $what == "dropudpport" || $what == "droptcpport" ||
  288.     $what == "restoreudpport" || $what == "restoretcpport" } {
  289.     if { $argc != 3 } {
  290.         usage
  291.     }
  292.     set port [lindex $argv 1]
  293.     set acl [lindex $argv 2]
  294. } elseif { $what == "permitudpdsthostport" ||
  295.     $what == "unpermitudpdsthostport" ||
  296.     $what == "permittcpdsthostport" ||
  297.     $what == "unpermittcpdsthostport" } {
  298.     if { $argc != 3 } {
  299.         usage
  300.     }
  301.     set addr1 [lindex $argv 1]
  302.     set port [lindex $argv 2]
  303. } elseif { $what == "droptcpdsthostport" ||
  304.     $what == "restoretcpdsthostport" } {
  305.     if { $argc != 3 } {
  306.         usage
  307.     }
  308.     set addr1 [lindex $argv 1]
  309.     set port [lindex $argv 2]
  310. } elseif { $what == "nullzero" || $what == "nonullzero" ||
  311.     $what == "querynullzero" } {
  312.     if { [lindex $argv 1] == "-f" } {
  313.         if { $argc != 3 } {
  314.             usage
  315.         }
  316.         set file [lindex $argv 2]
  317.         if [catch {open $file} fd] {
  318.             send_error "$prog: $fd\n"
  319.             exit 1
  320.         }
  321.         set fd [open $file]
  322.     } elseif { $argc == 2 } {
  323.         set addr1 [lindex $argv 1]
  324.     } else {
  325.         usage
  326.     }
  327. } elseif { $what == "addwhitelist" || $what == "remwhitelist" ||
  328.     $what == "querywhitelist" } {
  329.     if { [lindex $argv 1] == "-f" } {
  330.         if { $argc != 3 } {
  331.             usage
  332.         }
  333.         set file [lindex $argv 2]
  334.         if [catch {open $file} fd] {
  335.             send_error "$prog: $fd\n"
  336.             exit 1
  337.         }
  338.         set fd [open $file]
  339.     } elseif { $argc == 2 } {
  340.         set addr1 [lindex $argv 1]
  341.     } elseif { $argc == 3 } {
  342.         set addr1 [lrange $argv 1 2]
  343.     } else {
  344.         usage
  345.     }
  346. } elseif { $argc == 2 && ( $what == "compact" || $what == "listacl" ) } {
  347.     if { $argc != 2 } {
  348.         usage
  349.     }
  350.     set acl [lindex $argv 1]
  351. } elseif { $what == "state" || $what == "listroute" || $what == "whitelist" } {
  352.     if { $argc != 1 } {
  353.         usage
  354.     }
  355. } else {
  356.     usage
  357. }
  358.  
  359. send_error "$prog: [clock format [clock seconds] -format "%b %d %T"] $argv\n"
  360.  
  361. #
  362. # Connect to the acl daemon
  363. #
  364. set stty_init "-echo"
  365. set dst "$aclipaddr.$aclport"
  366. set cmd "socket -v -c $aclipaddr $aclport"
  367. eval spawn $cmd
  368. set timeout 60
  369. expect {
  370.     # Support socket 1.4
  371.     -re "^connected to (\[^ ]*) \[^ ]* port (\[0-9]*)\r\n" {
  372.         set dst "$expect_out(1,string).$expect_out(2,string)"
  373.     }
  374.     # Unfortunately there are different messages depending on USE_INET6
  375.     -re "^connected to (\[^ ]*) with address \[^ ]* at port (\[0-9]*)\r\n" {
  376.         set dst "$expect_out(1,string).$expect_out(2,string)"
  377.     }
  378.     # Unfortunately socket tries to convert the port to a string
  379.  
  380.     -re "^connected to (\[^ ]*) with address \[^ ]* at port (\[^\r\n]*)\r\n" {
  381.         set dst "$expect_out(1,string).$expect_out(2,string)"
  382.     }
  383.     # "connected to 127.0.0.1 port 1965 (tivoli-npm)"
  384.     -re "^connected to (\[^ ]*) port (\[0-9]*) \(\[^\r\n]*\)\r\n" {
  385.         set dst "$expect_out(1,string).$expect_out(2,string)"
  386.     }
  387.     # "connected to 127.0.0.1 (hosed.lbl.gov) port 1965 (tivoli-npm)"
  388.     -re "^connected to (\[^ ]*) \(\[^\r\n]*\) port (\[0-9]*) \(\[^\r\n]*\)\r\n" {
  389.         set dst "$expect_out(1,string).$expect_out(2,string)"
  390.     }
  391.     # "connected to 127.0.0.1 port 1965"
  392.     -re "^connected to (\[^ ]*) port (\[0-9]*)\r\n" {
  393.         set dst "$expect_out(1,string).$expect_out(2,string)"
  394.     }
  395.     -re "^(.*)\[\r\n]*\r\n" {
  396.         send_error "$prog: $expect_out(1,string)\n"
  397.         exp_continue
  398.     }
  399.     timeout {
  400.         send_error "$prog: $dst: timeout socket ($timeout secs)\n"
  401.         exit 1
  402.     }
  403.     eof {
  404.         send_error "$prog: $dst: child exited\n"
  405.         exit 1
  406.     }
  407. }
  408. if { [getresponse "acld" 0] != 0 } {
  409.     send_error "$prog: \"$cmd\" failed\n"
  410.     exit 1
  411. }
  412. set start $lasttime
  413.  
  414. set lastcookie 0
  415. set firstcookie 1
  416. set outstanding 0
  417. set estatus 0
  418. if { $what == "compact" || $what == "listacl" } {
  419.     #
  420.     # list/compact an ACL
  421.     #
  422.     incr lastcookie
  423.     sendcmd "$what $lastcookie $acl"
  424.     incr outstanding
  425. } elseif { $what == "state" || $what == "listroute" || $what == "whitelist" } {
  426.     #
  427.     # state inquiry, route list or whitelist
  428.     #
  429.     incr lastcookie
  430.     sendcmd "$what $lastcookie"
  431.     incr outstanding
  432. } elseif { $addr1 != "" && $addr2 != "" } {
  433.     #
  434.     # Handle hosthost (two addresses)
  435.     #
  436.     incr lastcookie
  437.     sendcmd "$what $lastcookie $addr1 $addr2"
  438.     incr outstanding
  439. } elseif { $addr1 != "" && $port != "" } {
  440.     #
  441.     # Handle an address and port
  442.     #
  443.     incr lastcookie
  444.     sendcmd "$what $lastcookie $addr1 $port $acl"
  445.     incr outstanding
  446. } elseif { $addr1 != "" } {
  447.     #
  448.     # Handle a single address
  449.     #
  450.     incr lastcookie
  451.     sendcmd "$what $lastcookie $addr1"
  452.     incr outstanding
  453. } elseif { $port != "" } {
  454.     #
  455.     # Handle a port
  456.     #
  457.     incr lastcookie
  458.     sendcmd "$what $lastcookie $port $acl"
  459.     incr outstanding
  460. } else {
  461.     #
  462.     # Loop on addrs from the file
  463.     #
  464.     set maxoutstanding 5
  465.     while {[gets $fd addr1] != -1} {
  466.         if { [regexp "^\[ \t]*$" $addr1] } {
  467.             # eat blank lines
  468.             continue
  469.         }
  470.         incr lastcookie
  471.         sendcmd "$what $lastcookie $addr1"
  472.         incr outstanding
  473.         if { $outstanding >= $maxoutstanding } {
  474.             if { [getresponse $what $firstcookie] != 0 } {
  475.                 set estatus 1
  476.             }
  477.             incr firstcookie
  478.             set outstanding [expr $outstanding - 1]
  479.         }
  480.     }
  481. }
  482.  
  483. # Finish up
  484. while { $outstanding > 0 } {
  485.     if { [getresponse $what $firstcookie] != 0 } {
  486.         set estatus 1
  487.     }
  488.     incr firstcookie
  489.     set outstanding [expr $outstanding - 1]
  490. }
  491.  
  492. flush stdout
  493.  
  494. #send_error "(start $start finish $lasttime)\n"
  495. set delta [format "%.3f" [expr $lasttime - $start]]
  496.  
  497. send_error "$prog: (took $delta seconds)\n"
  498.  
  499. incr lastcookie
  500. send -- "exit $lastcookie\r"
  501. expect "^(\[0-9]+\.\[0-9]+) $lastcookie exit"
  502.  
  503. if { $estatus != 0 } {
  504.     send_error "$prog: $what FAILED\n"
  505. }
  506.  
  507. exit $estatus
  508.