-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
345 lines (271 loc) · 9.77 KB
/
setup.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python2
'''
@Author : Debapriya Das
@Email : [email protected]
'''
import os
import sys
import platform
import shutil
SQUID_CONFIGURATION_PATH = "/etc/squid3/squid.conf"
PASSWD_FILE = "/etc/squid3/passwd"
def apt_update():
'''
function to update the Packages
'''
from subprocess import STDOUT, check_call, PIPE, Popen
# a = check_call(['apt-get', 'install', '-y', packagetoinstall], stdout=STDOUT, stderr=STDOUT)
p = Popen(['apt-get', 'update'], stdout=PIPE)
print p.communicate()
if p.returncode is 0 :
print "Success"
return p.returncode
def apt_upgrade():
'''
function to upgrade the Packages
'''
from subprocess import STDOUT, check_call, PIPE, Popen
# a = check_call(['apt-get', 'install', '-y', packagetoinstall], stdout=STDOUT, stderr=STDOUT)
p = Popen(['apt-get', 'upgrade'], stdout=PIPE)
print p.communicate()
if p.returncode is 0 :
print "Success"
return p.returncode
def apt_install(packagetoinstall):
'''
function to install packages
'''
packagetoinstall = str(packagetoinstall)
from subprocess import STDOUT, check_call, PIPE, Popen
# a = check_call(['apt-get', 'install', '-y', packagetoinstall], stdout=STDOUT, stderr=STDOUT)
p = Popen(['apt-get', 'install', '-y', packagetoinstall], stdout=PIPE)
print p.communicate()
if p.returncode is 0 :
print "Success"
return p.returncode
def mycopy(pathfrom, topath):
'''
Simple Copy handler
'''
status = -1
if os.path.exists(pathfrom) is False:
print "Path Does not exist"
return status
try :
shutil.copy(pathfrom, topath)
status = 0
except IOError, PermissionDeniedError:
status = -9
print IOError
print PermissionDeniedError
return status
def deleteContent(fName):
'''
Empty a file
'''
status = -1
try:
with open(fName, "w"):
pass
status = 0
except IOError, PermissionDeniedError:
print IOError
print PermissionDeniedError
print "File as not able to modify"
return status
def iptables_install():
servicefilepath = "/etc/init.d"
file = "iptables-persistent"
pathto = servicefilepath + os.sep + file
print pathto
print mycopy(file, servicefilepath)
print os.chmod(servicefilepath, 0755)
command = "update-rc.d iptables defaults"
print os.system(command)
def configuration_copy_handler(newfile):
'''
configuration files properly copy handling
'''
basepath, filename = os.path.split(SQUID_CONFIGURATION_PATH)
backupfile = filename + ".backup"
print backupfile
backuppath = basepath + os.sep + backupfile
print backuppath
print "creating backup ... "
filepath = basepath + os.sep + filename
p = mycopy(filepath, backuppath)
if p is not 0 :
print "backup didnt happen, some issues with Copying...let's contact Debapriya"
exit(-13)
print "Backup Done, time to rewrite the configuration file now"
deleteContent(filepath)
print "Deleted file contents..."
p = mycopy(newfile,filepath)
if p is not 0 :
print "new file copy didnt happen, some issues with Copying...let's contact Debapriya"
exit(-13)
print "new file replaced"
def adduser(username, password):
'''
a function that takes an Username and Password and creates the user
'''
from subprocess import STDOUT, check_call, PIPE, Popen
# a = check_call(['apt-get', 'install', '-y', packagetoinstall], stdout=STDOUT, stderr=STDOUT)
p = Popen(['htpasswd', '-b', PASSWD_FILE, username, password], stdout=PIPE)
print p.communicate()
if p.returncode is 0 :
print "Success in adding user %s", username
return p.returncode
def handlemultipleuseradd(userdict):
'''
This function area is for multiple user addition
'''
status = 0
from subprocess import STDOUT, check_call, PIPE, Popen
if os.path.exists(PASSWD_FILE) is False:
print "passwd path does not exist"
print "creating one ...."
p = Popen(['touch', PASSWD_FILE], stdout=PIPE)
print p.communicate()
if p.returncode is not 0 :
print PASSWD_FILE + "File creation failed"
print "Exiting.."
status = -13
exit(-13)
else :
print "File got created..."
status = 1
for user in userdict.keys():
password = userdict[user]
status = adduser(user, password)
print password, status
if status is not 0 :
print "Some Error while adding users..."
break
else:
status = 0
print "Added all users from list ..."
return status
def getportlist(conffile):
'''
read configuration file and extract port numbers
'''
portlist = [] # will have SSH by default
file = open(conffile,'r')
lines = file.readlines()
for line in lines:
if line[0] is not '#' and "http_port" in line and line[0] is 'h':
http_port, port = line.split()
portlist.append(port)
file.close()
return portlist
def handleservice(package, command):
if command not in ['save', 'restart', 'stop']:
print "Invalid command"
return -13
from subprocess import STDOUT, check_call, PIPE, Popen
p = Popen(['service', package, command], stdout=PIPE)
print p.communicate()
if p.returncode is 0 :
print "Success"
return p.returncode
def iptable_exec(exec_string):
'''
takes an IPTable command string and executes it
'''
iptable = ["iptables"]
commandlist = exec_string.split()
for command in commandlist:
iptable.append(command)
from subprocess import STDOUT, check_call, PIPE, Popen
p = Popen(iptable, stdout=PIPE)
print p.communicate()
if p.returncode is 0 :
print "Success"
return p.returncode
def firewall_configuration(conffilepath):
space = " "
ports = getportlist(conffilepath)
print "list of ports : "
print ports
if 'PORT' in ports:
ports.remove('PORT')
for port in ports:
rulestring = "-I INPUT -p tcp --dport" + space + port + space + "-j ACCEPT"
status = iptable_exec(rulestring)
if status is not 0:
print "Something is wrong...call Debapriya"
exit(-5)
rulestring = "-I INPUT -p tcp --dport" + space + "22" + space + "-j ACCEPT"
status = iptable_exec(rulestring)
iptable_exec("-A INPUT -i lo -j ACCEPT")
iptable_exec("-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT")
iptable_exec("-A INPUT -j DROP")
handleservice("iptables-persistent", "save")
handleservice("iptables-persistent", "restart")
handleservice("squid3", "restart")
iptable_exec("-D INPUT -j DROP")
iptable_exec("-A INPUT -p icmp -j ACCEPT")
iptable_exec("-A INPUT -j DROP")
handleservice("iptables-persistent", "save")
handleservice("iptables-persistent", "restart")
handleservice("squid3", "restart")
print "Done!"
def main():
'''
Main() : main functionality of the code
'''
if os.name is 'Posix' or 'posix':
# checking a valid POSIX Shell
print "Hi " + os.getenv('USER') + ", this looks like a POSIX Shell"
if os.geteuid() is not 0 :
print "This program is supposed to be run as root"
print "N00B$ are not supposed to run this"
print "May the force be with you, Motherfucker!"
exit(-13)
if 'linux' in sys.platform :
print "Okay, you have a Linux OS, this script is compatible"
print "Now checking if you have Ubuntu"
if 'ubuntu' or 'Ubuntu' in platform.linux_distribution():
print platform.linux_distribution()
print "You have an Ubuntu OS : " \
+ platform.linux_distribution()[0] + " " \
+ platform.linux_distribution()[1] + " " \
+ platform.linux_distribution()[2]
if "trusty" not in platform.linux_distribution()[2] :
print "only for 14.04, run on 14.04 only"
exit(-14)
print "Yippee!"
print "Now Time for work!!"
if apt_update() is 0 :
print "Update Success"
else :
print "Something failed, try again later or communicate to Debapriya"
if apt_upgrade() is 0 :
print "Upgrade Success"
else :
print "Something failed, try again later or communicate to Debapriya"
packagestoinstall = ["apache2-utils", "squid3", "iptables-persistent"]
for packagetoinstall in packagestoinstall:
if apt_install(packagetoinstall) is 0 :
print "Install Success"
else :
print "Something failed, try again later or communicate to Debapriya"
exit(-9)
print "Installation of all packages complete, time to configure ....."
userdict = {'user1':"", 'user2':""}
squidconfig = "squid.conf.file"
handlemultipleuseradd(userdict)
print "Added users"
configuration_copy_handler(squidconfig)
print "Copied my config file"
firewall_configuration(SQUID_CONFIGURATION_PATH)
print "Configuration Done...."
print "Sucesssfully Completed!"
print "Say thanks to Deb"
elif sys.platform is 'darwin':
print "Okay, you have a Mac, work in progress!"
else:
print "Sorry, Not a Posix Shell"
if __name__=="__main__":
main()