@@ -0,0 +1,3177 @@
+#!/usr/bin/env python2.7%0A# Copyright (C) 2014 Job Snijders %3Cjob@instituut.net%3E%0A#%0A# This file is part of ACLHound%0A#%0A# Redistribution and use in source and binary forms, with or without%0A# modification, are permitted provided that the following conditions are met:%0A#%0A# 1. Redistributions of source code must retain the above copyright notice,%0A# this list of conditions and the following disclaimer.%0A#%0A# 2. Redistributions in binary form must reproduce the above copyright notice,%0A# this list of conditions and the following disclaimer in the documentation%0A# and/or other materials provided with the distribution.%0A#%0A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22%0A# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE%0A# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE%0A# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE%0A# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR%0A# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF%0A# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS%0A# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN%0A# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)%0A# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE%0A# POSSIBILITY OF SUCH DAMAGE.%0A%0Aimport sys%0A%0Afrom aclhound.aclsemantics import grammarSemantics%0Afrom aclhound.parser import grammarParser%0Afrom aclhound.render import Render%0A%0A%0Adef generate_policy(filename, startrule='start', trace=True, whitespace=None,%0A                    afi=4, vendor=None):%0A    %22%22%22%0A    Open a file, run it through the parser, recurse if needed%0A    Output resembles what you would expect in running-config.%0A    %22%22%22%0A%0A    safe_letters = %22abcdefghijklmnopqrstuvwxyz0123456789-_%22%0A%0A    def check_name(name):%0A        for letter in name:%0A            if not letter in safe_letters:%0A                print(%22ERROR: invalid policy filename: %25s%22 %25 name)%0A                sys.exit(2)%0A        return name%0A%0A    policy_name = check_name(filename)%0A    seen = %5Bfilename%5D%0A%0A    def walk_file(filename, seen=%5B%5D, policy=%5B%5D):%0A        try:%0A            f = open(%22policy/%25s%22 %25 filename).read().splitlines()%0A        except IOError:%0A            print(%22filename %25s referenced in %25s does not exist%22%0A                  %25 (filename, seen%5B-1%5D))%0A            print(%22HINT: ensure you are in your ACLHound data directory%22)%0A            sys.exit()%0A        for line in f:%0A            if line.startswith('@'):%0A                filename = line.split('#')%5B0%5D%5B1:%5D%0A                if filename not in seen:%0A                    seen.append(filename)%0A                    policy = policy + walk_file(filename, seen, policy)%0A            elif line.startswith(('allow', 'deny')) and line not in policy:%0A                policy.append(line)%0A        return policy%0A%0A    parser = grammarParser(parseinfo=False, semantics=grammarSemantics())%0A    acl = Render(name=policy_name)%0A    for line in walk_file(filename, seen):%0A        ast = parser.parse(line, startrule)%0A        acl.add(ast)%0A    output = %22%5Cn%22.join(acl.output(vendor=vendor, afi=afi))%0A    return output%0A

add generate