-
Notifications
You must be signed in to change notification settings - Fork 1
/
website_del_script.py
325 lines (284 loc) · 12.1 KB
/
website_del_script.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
'''/website_del_script/website_del_script.py'''
from tempfile import TemporaryFile
from sys import exc_info
import argparse
import psuldap
import socket
import subprocess
import os
#this global should be the location of the local copy of the puppet dir
__puppet_dir__ = '/home/maxgarvey/mint_stuff'
#and this one is a local copy of named.db
__dns_dir__ = '/home/maxgarvey/bind_stuff/named.db'
#the two whitelists
__puppet_whitelist_addr__ = '/home/maxgarvey/mint_stuff/whitelist.txt'
__dns_whitelist_addr__ = '/home/maxgarvey/bind_stuff/whitelist.txt'
def get_website( website_input = '' ):
'''this method puts the output of getting the website's information into a
string format... that way, when you want python structures, you can use
get_website_py but otherwise, you can just get it as a string'''
#this part does all of the work. the rest just formats it.
website, dns, vhost, uid, google_acct = get_website_py(website_input)
#split up the dns entry, formatting for output
output_string = 'website: ' + website + '\n' + 'dns: \n'
for line in dns:
if not (line.isspace() or line == ''):
output_string += line + '\n'
#add vhost stuff to output
output_string += 'vhosts: \n'
for line in vhost:
if not (line.isspace() or line == ''):
output_string += line + '\n'
#catch the case where there's no vhosts
if len(vhost) == 0:
output_string += '\tno vhosts.\n'
output_string += 'uid: \n\t' + uid + '\n\n'
output_string += google_acct
return output_string
def strip_http(website_http):
'''this method is a helper method to strip off the http or https from the
front of a website.'''
if website_http.startswith('https://'):
website_no_http = website_http[8:]
elif website_http.startswith('http://'):
website_no_http = website_http[7:]
else:
website_no_http = website_http
return website_no_http
def strip_www(website_www):
'''this method is a helper method to strip off the www from the front of
a website.'''
if website_www.startswith( 'www.' ):
website_no_www = website_www[4:]
else:
website_no_www = website_www
return website_no_www
def dns_lookup(website):
'''this method looks up a website to get the IP address that corresponds
to it.'''
dns_string = ''
try:
dns_string = socket.gethostbyname( website )
except socket.error:
dns_string = 'no dns.'
return dns_string
def read_whitelist(location):
'''this method reads in the whitelist given the path to the whitelist'''
try:
whitelist_file = open(location, 'r' )
whitelist_contents = whitelist_file.read( )
whitelist_file.close( )
whitelist_lines = whitelist_contents.split( '\n' )
except IOError:
whitelist_lines = []
print 'error working with whitelist file: ' + str( exc_info()[1] )
return whitelist_lines
def find_lines(website, dns_dir, my_type='dns'):
'''this method actually greps the directory for items with the website
we're looking for.'''
with (TemporaryFile()) as temp_file:
try:
subprocess.call( ['grep', '-rn', website, dns_dir],
stdout=temp_file, stderr=temp_file )
except IOError, err:
print '\nerror occurred during grepping {0} directory: {1}'.format(
my_type,err)
temp_file.seek(0)
file_string = temp_file.read()
file_lines = file_string.split('\n')
return file_lines
def process_lines(file_lines, whitelist_lines, start_dir):
'''this method is a helper for figuring out which lines in our results are
worthwhile and formatting them a little bit.'''
keeper_lines = [] #the lines we will be keeping
current_file = '' #the file being grepped (since there are usually multiple
#relevant lines per file).
for line in file_lines:
#if the line isn't relevant, ignore it.
if line.endswith('No such file or directory') or ( len(line) == 0 ):
pass
else:
#otherwise, skip past the part of the path that we know.
if line.startswith(start_dir):
line = line[(len(start_dir)):]
whitelisted = False
#compare with each element from whitelist, if the path starts with
#a whitelisted prefix, then ignore it.
for prefix in whitelist_lines:
if not (prefix.isspace() or prefix == ''):
if line.startswith(prefix):
whitelisted = True
if whitelisted == True:
pass
#if not whitelisted, add the filename and the relevant file contents
#properly tabbed for easy reading.
else:
if current_file != line[:(line.index(':'))]:
current_file = line[:(line.index(':'))]
keeper_lines.append( ('\t' + current_file + ':') )
keeper_lines.append( '\t\tline ' + str(
line[(line.index(':')+1):line.index(':')+1+\
line[(line.index(':')+1):].index(':') ])
+ ': ' + str( line[(line.rindex(':')+1):] ) )
return keeper_lines
#this method gets the website's info from various locations
def get_website_py( website_input = '' ):
'''this method does all of the work of finding a single website's data; it
outputs a python list'''
if website_input == '':
#user input of the website url
website = raw_input("enter the website to delete:\n")
while website == '':
website = raw_input("enter the website to delete:\n")
else:
website = website_input
website = strip_http(website)
website = strip_www(website)
#this line finds the ip address of the website
dns = dns_lookup(website)
#this part greps the dns/bind stuff
whitelist_lines = read_whitelist(__dns_whitelist_addr__)
file_lines = find_lines(website, __dns_dir__)
dns_lines = process_lines(file_lines, whitelist_lines, __dns_dir__)
#prepend ip address to the dns information
dns_lines.insert(0,('\t' + dns))
#this part greps the puppet directory structure for vhosts
whitelist_lines = read_whitelist(__puppet_whitelist_addr__)
file_lines = find_lines(website, __puppet_dir__)
vhost_lines = process_lines(file_lines, whitelist_lines, __puppet_dir__)
uid, _ = ldap_lookup(website)
if uid == '':
uid = 'no uid.'
if uid != 'no uid.':
#got to trim the bracket, and quote off both sides
uid = uid[2:-2]
google_acct = gam_lookup(uid)
else:
google_acct = 'no ldap uid found to check for google account.'
return website, dns_lines, vhost_lines, uid, google_acct
def gam_lookup(ldap_uid):
'''this is a helper method for looking up a google account for the website
and returning it if it exists.'''
with TemporaryFile() as temp:
with TemporaryFile() as temperr:
subprocess.call(['python', 'gam.py', 'info', 'user', ldap_uid], stdout=temp, stderr=temperr)
temp.seek(0)
gam_output = temp.read()
if gam_output != None and gam_output != '':
google_out_string = 'google account: {0}'.format(
gam_output.split('\n')[0].split(': ')[1])
else:
google_out_string = 'no google account found for: user={0}'.format(
ldap_uid)
return google_out_string
#this will perform the function on a python list of URLs
def get_website_list( website_list ):
'''looks up each URL from a list of urls.'''
out_file = open( 'outfile.txt', 'w' )
websites_str = ''
for website in website_list:
if website != '':
website_str = get_website( str(website) )
websites_str += website_str
out_file.close()
return websites_str
#this will work from a file with a URL on each line
def get_websites_from_file( input_file ):
'''this method takes a file location as a string and reads it in, splitting
on newlines and putting each URL through the lookup.'''
with open( input_file, 'r' ) as in_file:
filestring = in_file.read()
my_list = filestring.split('\n')
output_list = get_website_list( my_list )
return output_list
def file_to_file( input_file, output_file ):
'''performs operations on each line of the input file, and writes the output
to the output file.'''
output = get_websites_from_file( input_file )
with open( output_file, 'w' ) as output_file:
output_file.write( output )
def ldap_connect():
'''a helper method for connecting to ldap'''
try:
ldap_obj = psuldap.psuldap()
ldap_obj.connect(ldapurl='ldap://ldap.oit.pdx.edu/')
ldap_connected = True
return ldap_obj, ldap_connected
except psuldap.ldap.LDAPError, err:
print '\nerror connecting to ldap: {0}\n'.format(err)
return None, False
def ldap_search(ldap_obj, search_filter, ldap_searched, ldap_results):
'''a helper method to handle ldap searches in the context of this specific
app.'''
search_results = []
try:
search_results = ldap_obj.search( searchfilter=search_filter )
ldap_searched = True
if len( search_results ) > 0:
ldap_results.append( search_results )
except psuldap.ldap.FILTER_ERROR, err:
print '\nerror searching ldap\n {0}\n'.format(err)
return ldap_searched, ldap_results, search_results
def ldap_lookup( website ):
'''helper method for handling the ldap lookup'''
ldap_connected = False
ldap_searched = False
search_results = []
ldap_results = []
ldap_obj, ldap_connected = ldap_connect()
if ldap_connected:
this_search_filter = '(&(eduPersonAffiliation=WEB)(labeledUri=*' + \
str( website ) + '*))'
ldap_searched, ldap_results, search_results = ldap_search(
ldap_obj, this_search_filter, ldap_searched, ldap_results)
if len( search_results ) is 0:
this_search_filter = '(&(eduPersonAffiliation=WEB)(cn=*' + \
str( website ) + '*))'
try:
search_results = ldap_obj.search(
searchfilter=this_search_filter )
ldap_searched = True
if len( search_results ) > 0:
ldap_results.append( search_results )
except psuldap.ldap.FILTER_ERROR, err:
print '\nerror searching ldap\n{0}\n'.format(err)
if len( search_results ) is 0:
this_search_filter = '(&(eduPersonAffiliation=WEB)(gecos=*' + \
str( website ) + '*))'
try:
search_results = ldap_obj.search(
searchfilter=this_search_filter )
ldap_searched = True
if len( search_results ) > 0:
ldap_results.append( search_results )
except psuldap.ldap.FILTER_ERROR, err:
print '\nerror searching ldap\n{0}\n'.format(err)
#ldap searched will always be true provided there was no error
if ldap_searched:
if len( search_results ) is 0:
uid = ''
else:
uid = str( search_results[0][1]['uid'])
else:
uid = ''
return uid, search_results
if __name__ == '__main__':
__input_entered__ = False
try:
__parser__ = argparse.ArgumentParser(description = \
('this script takes a website as input and finds the ip ' + \
'address, indicating DNS records; checks in puppet for any ' + \
'puppet records containing the file; and an ldap lookup to see ' + \
'if there\'s a uid for the website'))
__parser__.add_argument('website', action='store', nargs = '*')
__args__ = __parser__.parse_args()
for site in __args__.website:
#if there are any args, input_entered is set to true
if not __input_entered__:
__input_entered__ = True
print '\n' + str(get_website(site))
except Exception, err:
print 'error: {0}'.format(err)
print get_website('')
if not __input_entered__:
print get_website('')