Repository navigation
Expand file tree
/
Copy pathfe
More file actions
executable file
·187 lines (159 loc) · 5.73 KB
/
Copy pathfe
File metadata and controls
executable file
·187 lines (159 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
"""
fe - the File Editor. Uses RCS to maintain ACL policy versioning.
"""
import os
import re
from simpleparse.error import ParserSyntaxError
import sys
import termios
from trigger import acl
from trigger.acl.parser import TIP
from trigger.utils.cli import yesno
psa_checks = (
(lambda t: 'source-port' in t.match, 'source port'),
(lambda t: 'tos' in t.match, 'ToS'),
(lambda t: 'precedence' in t.match, 'precedence'),
(lambda t: 'protocol' in t.match and 'igmp' in t.match['protocol'], 'IGMP')
)
def gsr_checks(a):
'''PSA and SALSA checks, if the ACL is tagged appropriately.'''
ok = True
max_lines, psa = None, False
if [c for c in a.comments if 'SALSA' in c]:
max_lines = 128
elif [c for c in a.comments if 'PSA-128' in c]:
max_lines, psa = 128, True
elif [c for c in a.comments if 'PSA-448' in c]:
max_lines, psa = 448, True
if max_lines and len(a.terms) > max_lines:
print('ACL has %d lines, max %d (GSR limit)' \
% (len(a.terms), max_lines))
ok = False
if psa:
for t in a.terms:
for check, reason in psa_checks:
if check(t):
print('Match on %s not permitted in PSA mode' % reason)
for line in t.output_ios():
print(' ' + line)
ok = False
return ok
def normalize(a):
'''Fix up the ACL, and return False if there are problems.'''
if isinstance(a, acl.PolicerGroup):
return True
ok = True
# JunOS counter policy and duplicate term names.
if a.format == 'junos':
names = set()
for t in a.terms:
if t.name in names:
print('Duplicate term name', t.name)
ok = False
else:
names.add(t.name)
# GSR checks.
if a.format == 'ios': # not ios_named
if not gsr_checks(a):
ok = False
# Check for 10/8.
for t in a.terms:
for addr in ('address', 'source-address', 'destination-address'):
for block in t.match.get(addr, []):
if block == TIP('10.0.0.0/8'):
print('Matching on 10.0.0.0/8 is never correct')
for line in t.output(a.format):
print(' ' + line)
ok = False
# Here is the place to put other policy checks; for example, we could
# check blue to green HTTP and make sure all those terms have a comment
# in a standardized format saying it was approved.
return ok
def edit(editfile):
"""
Edits the file and calls normalize(). Loops until it passes or the user
confirms they want to bypass failed normalization. Returns a file object of
the diff output.
"""
editor = os.environ.get('EDITOR', 'vim')
if editor.startswith('vi'):
os.spawnlp(os.P_WAIT, editor, editor, '+set sw=4', '+set softtabstop=4',
editfile)
else:
os.spawnlp(os.P_WAIT, editor, editor, editfile)
if os.path.basename(editfile): #.startswith('acl.'):
print('Normalizing ACL...')
a = None
try:
a = acl.parse(open(editfile))
except ParserSyntaxError as e:
print(e)
except TypeError as e:
print(e)
except Exception as e:
print('ACL parse failed: ', sys.exc_info()[0], ':', e)
if a and normalize(a):
output = '\n'.join(a.output(replace=True)) + '\n'
open(editfile, 'w').write(output)
else:
if yesno('Re-edit?'):
return edit(editfile)
# should use a list version of popen
return os.popen('rcsdiff -u -b -q ' + editfile).read()
if len(sys.argv) < 2:
print('usage: fe files...', file=sys.stderr)
sys.exit(2)
for editfile in sys.argv[1:]:
try:
stat = os.stat(editfile)
except OSError:
stat = None
if not stat:
if yesno('%s does not exist; create?' % editfile, False):
edit(editfile)
os.spawnlp(os.P_WAIT, 'ci', 'ci', '-u', editfile)
else:
rv = os.spawnlp(os.P_WAIT, 'co', 'co', '-f', '-l', editfile)
if rv:
print("couldn't check out " + editfile, file=sys.stderr)
continue
diff = edit(editfile)
if not diff:
print('No changes made.')
os.spawnlp(os.P_WAIT, 'rcs', 'rcs', '-u', editfile)
# When no changes are made, ci leaves the file writable.
os.chmod(editfile, stat.st_mode & 0o555)
continue
print('')
print('"%s"' % os.path.basename(editfile))
print('BEGINNING OF CHANGES========================')
print(diff, end='')
print('END OF CHANGES==============================')
print('')
print('')
if not yesno('Do you want to save changes?'):
print('Restoring original file')
os.spawnlp(os.P_WAIT, 'co', 'co', '-f', '-u', editfile)
continue
# Try to auto-detect log message from comments.
log = ''
pats = (re.compile(r'^\+.*!+(.*)'), re.compile(r'^\+.*/\*(.*)\*/'))
for line in diff.split('\n'):
for pat in pats:
m = pat.match(line)
if m:
msg = m.group(1).strip()
if msg:
log += m.group(1).strip() + '\n'
break
if log:
print('Autodetected log message:')
print(log)
print('')
if not yesno('Use this?'):
log = ''
if log:
os.spawnlp(os.P_WAIT, 'ci', 'ci', '-u', '-m' + log, editfile)
else:
os.spawnlp(os.P_WAIT, 'ci', 'ci', '-u', editfile)