-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutm.rb
More file actions
535 lines (455 loc) · 15.1 KB
/
utm.rb
File metadata and controls
535 lines (455 loc) · 15.1 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/usr/bin/ruby
module IFCONFIG
class Ifconfig
def initialize
end
def tab_liste_interface_reseau
tab_interface_reseau = Array.new
compteur = 0
affichage_interface_reseau.each_line{ |line|
if compteur > 0
tab_interface_reseau_split = line.to_s.strip.split(" ")
tab_interface_reseau.push(tab_interface_reseau_split[0].to_s.strip)
end
compteur = compteur + 1
}
tab_interface_reseau
end
def tab_liste_interface_reseau_bsd
liste_interface_reseau_bsd = affichage_interface_reseau_bsd
tab_liste_interface_reseau_bsd = liste_interface_reseau_bsd.split(" ")
tab_liste_interface_reseau_bsd
end
protected
def affichage_interface_reseau
%x[ifconfig -s]
end
def affichage_interface_reseau_bsd
%x[ifconfig -l]
end
end
end
module FIREWALL
class Firewall
def initialize
end
def lect_file_iptables
%x[cat /Users/nicolas/network/interfaces]
end
def register_file_iptables(contenu_interfaces_iptables)
File.open("/Users/nicolas/network/interfaces", "w+") do |file|
file.write(contenu_interfaces_iptables)
end
end
def tab_liste_regles_iptables
hash1 = Hash.new
tab_regles_iptables = Array.new
tab_liste_regle_hash = Array.new
lect_file_iptables.each_line{ |line|
tab_regles_iptables.push(line.to_s.strip)
}
0.upto(tab_regles_iptables.length-1){ |i|
tab_split_iptables = tab_regles_iptables[i].split(" ")
1.upto(tab_split_iptables.length-1){ |e|
hash1[tab_split_iptables[e].to_s.strip] = tab_split_iptables[e+1].to_s.strip
e = e + 1
}
tab_liste_regle_hash.push(hash1)
}
tab_liste_regle_hash
end
end
end
module FILESYSTEM
class Filesystem
def initialize(list_file_system)
@list_file_system = list_file_system
end
def tab_liste_file_system
tab_liste_file_system = Array.new
parcours_file_system.each_line{ |liste_file_system|
if liste_file_system.to_s.strip.scan(/^.{1,}\..{1,}$/) == Array.new
tab_liste_file_system.push(liste_file_system.to_s.strip)
end
}
tab_liste_file_system
end
protected
def parcours_file_system
%x[ls #{@list_file_system}]
end
end
end
module USER_GROUPE
class Groupe
def initialize(groupe)
@groupe = groupe
end
def groupadd
system("groupadd #{@groupe}")
end
def groupmod(new_name_groupe)
system("groupmod #{@groupe} -n #{new_name_groupe}")
end
def groupdel
system("groupdel #{@groupe}")
end
end
class User
def initialize
end
def add_user(user,groupe,repertoire_perso,password_crypt,partage_public)
liste_groupe_add = ""
0.upto(groupe.length-1){ |i|
if i < groupe.length-1
liste_groupe_add = "#{groupe[i]},"
else
liste_groupe_add = "#{groupe[i]}"
end
}
creation_repertoire_home(repertoire_perso)
creation_repetoir_public_html(repertoire_perso,user) if partage_public.to_i == 1
system("useradd #{user} #{liste_groupe_add.empty? ? "" : "-G #{liste_groupe_add}"} #{repertoire_perso.empty? ? "" : "-d #{repertoire_perso}"} -p #{password_crypt}")
end
def tab_gener_password_aleatoire
hash_password_aleatoire = Hash.new
tab_password_aleatoire = gener_password_aleatoire.split(" ")
hash_password_aleatoire["password"] = tab_password_aleatoire[0]
hash_password_aleatoire["password_crypt"] = tab_password_aleatoire[1]
hash_password_aleatoire
end
def userdel(user)
system("userdel #{user}")
end
private
def gener_password_aleatoire
%x["makepasswd"]
end
def creation_repetoir_public_html(repertoire_perso,user)
system("mkdir #{repertoire_perso}/#{user}/public_html/")
end
def creation_repertoire_home(repertoire_perso)
system("mkdir #{repertoire_perso}")
end
end
end
module DNS
class Dns_config
def initialize
end
def lect_file_config_dns
%x[cat /Users/nicolas/bind/named.conf.local]
end
def register_file_DNS_conf_expert(contenu_file_dns_conf)
File.open("/Users/nicolas/bind/named.conf.local", "w+") do |file|
file.write(contenu_file_dns_conf)
end
end
def recup_info_config_dns
tab_recup_info_config_dns_file = Array.new
tab_final_hachage = Array.new
lect_file_config_dns.each_line{ |line|
tab_recup_info_config_dns_file.push(line)
}
0.upto(tab_recup_info_config_dns_file.length-1){ |i|
if tab_recup_info_config_dns_file[i].to_s.strip.scan(/^zone(.{1,})\{$/) != Array.new
hash1 = Hash.new
hash1["nom_zone"] = tab_recup_info_config_dns_file[i].to_s.strip.scan(/^zone(.{1,})\{$/)
while tab_recup_info_config_dns_file[i].to_s.strip != "};"
i = i + 1
tab_recup_info_config_dns_file_split = tab_recup_info_config_dns_file[i].to_s.strip.split(" ")
hash1[tab_recup_info_config_dns_file_split[0].to_s.strip] = tab_recup_info_config_dns_file_split[1].to_s.strip
end
tab_final_hachage.push(hash1)
end
}
tab_final_hachage
end
def update_info_config_dns(nom_zone,type_zone_local,notify_zone_local,file_zone_local,nom_zone_inverse,type_zone_inverse,notify_zone_inverse,file_zone_inverse)
contenu_fichier_conf_dns = "zone '#{nom_zone}' {
type #{type_zone_local};
notify #{notify_zone_local};
file '#{file_zone_local}';
allow-update { key rndc-key; };
};\n\n
zone '#{nom_zone_inverse}' {
type #{type_zone_inverse};
notify #{notify_zone_inverse};
allow-update { key rndc-key; };
};"
File.open("/Users/nicolas/bind/named.conf.local", "w+") do |file|
file.write(contenu_fichier_conf_dns)
end
end
end
class DNS_database
def initialize
end
def lect_file_DB_dns
%x[cat /Users/nicolas/bind/db.nicolas.fr]
end
def recup_database_DB_dns
hash = Hash.new
compteur = 0
lect_file_DB_dns.each_line{ |line|
if compteur > 10 && line.to_s.strip.scan(/^\$TTL(.{1,})$/) == Array.new && line.to_s.strip.scan(/^TXT(.{1,})$/) == Array.new
tab_file_DB_dns = line.to_s.strip.split(" ")
hash[tab_file_DB_dns[0]] = tab_file_DB_dns[2]
end
compteur = compteur + 1
}
hash
end
def sup_database_DB_dns(nom_machine)
tab_contenu_DB_dns = Array.new
compteur = 0
lect_file_DB_dns.each_line{ |line|
tab_contenu_DB_dns.push(line.to_s)
}
0.upto(tab_contenu_DB_dns.length-1){ |i|
tab_contenu_DB_dns_split = tab_contenu_DB_dns[i].to_s.strip.split(" ")
if tab_contenu_DB_dns_split[0].to_s.strip == nom_machine.to_s.strip
tab_contenu_DB_dns.delete_at(i)
if tab_contenu_DB_dns[i+1].to_s.strip.scan(/^TXT(.{1,})$/) != Array.new
tab_contenu_DB_dns[i+1].delete_at(i+1)
end
end
}
File.open("/Users/nicolas/bind/db.nicolas.fr", "w+") do |file|
0.upto(tab_contenu_DB_dns.length-1){ |i|
file.write(tab_contenu_DB_dns[i])
}
end
end
def recup_info_db_dns_pour_modif(nom_machine)
tab_file_DB_dns = Array.new
hash1 = Hash.new
lect_file_DB_dns.each_line{ |line|
tab_file_DB_dns.push(line)
}
0.upto(tab_file_DB_dns.length-1){ |i|
tab_file_DB_dns_split = tab_file_DB_dns[i].to_s.strip.split(" ")
if tab_file_DB_dns_split[0].to_s.strip == nom_machine.to_s.strip
hash1["nom_machine"] = tab_file_DB_dns_split[0]
hash1["ip_machine"] = tab_file_DB_dns_split[2]
end
}
hash1
end
def update_db_dns_file(nom_machine_hide,nom_machine,ip_machine)
tab_file_DB_dns = Array.new
lect_file_DB_dns.each_line{ |line|
tab_file_DB_dns.push(line)
}
0.upto(tab_file_DB_dns.length-1){ |i|
tab_file_DB_dns_split = tab_file_DB_dns[i].to_s.strip.split(" ")
if tab_file_DB_dns_split[0].to_s.strip == nom_machine_hide.to_s.strip
tab_file_DB_dns[i] = "#{nom_machine} A #{ip_machine}"
end
}
File.open("/Users/nicolas/bind/db.nicolas.fr", "w+") do |file|
0.upto(tab_file_DB_dns.length-1){ |i|
file.write("#{tab_file_DB_dns[i].to_s.strip}\n")
}
end
end
end
end
module DHCP
class Dhcp
def initialize
end
def lect_file_dhcp
%x[cat /Users/nicolas/dhcpd.conf]
end
def register_file_DHCP_expert(contenu_file_dhcp)
File.open("/Users/nicolas/bind/dhcpd.conf.local", "w+") do |file|
file.write(contenu_file_dhcp)
end
end
def recup_donnes_dhcp(nom_colonne_recup)
hash1 = Hash.new
tab_donnes_dhcp = Array.new
lect_file_dhcp.each_line{ |line|
tab_donnes_dhcp.push(line)
}
0.upto(tab_donnes_dhcp.length-1){ |i|
tab_detail_donnes_dhcp_recup = tab_donnes_dhcp.to_s.strip.split(" ")
0.upto(tab_detail_donnes_dhcp_recup.length-1){ |e|
if nom_colonne_recup.to_s == tab_detail_donnes_dhcp_recup[e].to_s.strip
hash1[nom_colonne_recup.to_s] = tab_detail_donnes_dhcp_recup[e+1].to_s.strip
#hash1["domain-name-servers"] = tab_detail_donnes_dhcp_recup[e+1].strip
end
}
}
hash1
end
def enregistrement_update_dhcp(ddns_update_style,domain_name_servers,subnet,netmask,domain_name,routers,subnet_mask,broadcast_address,premier_range,deuxieme_range,default_lease_time,max_lease_time,ddns_updates)
contenu_fichier_configuration_dhcp = "ddns-update-style #{ddns_update_style};
authoritative;
deny duplicates;
ignore declines;
option domain-name-servers #{domain_name_servers};
subnet #{subnet} netmask #{netmask} {
option domain-name '#{domain_name}';
option routers #{routers};
option subnet-mask #{subnet_mask};
option broadcast-address #{broadcast_address};
range #{premier_range} #{deuxieme_range};
default-lease-time #{default_lease_time};
max-lease-time #{max_lease_time};
}
ddns-updates #{ddns_updates};
ddns-domainname '#{domain_name}';
ddns-rev-domainname 'in-addr.arpa';
include '/etc/bind/rndc.key';
zone nicolas.fr {
primary 192.168.2.1;
key rndc-key;
}
zone 2.169.192.in-addr.arpa {
primary 192.168.2.1;
key rndc-key;
}"
File.open("/Users/nicolas/dhcpd.conf", "w+") do |file|
file.write(contenu_fichier_configuration_dhcp)
end
end
end
end
module SAMBA
class Samba
def initialize
end
def lect_file_samba
%x[cat /Users/nicolas/smb.conf]
end
def admin_samba_general
lect_file_samba
end
def update_modification_samba_general(fichier_samba)
File.open("/Users/nicolas/smb.conf", "w+") do |file|
file.write(fichier_samba)
end
end
def liste_partage_samba
tab_liste_partage_samba = Array.new
tab_lecture_partage_samba_valid = Array.new
lect_file_samba.each{ |line|
tab_liste_partage_samba.push(line.strip)
}
0.upto(tab_liste_partage_samba.length-1){ |i|
if tab_liste_partage_samba[i].to_s.strip.scan(/^\[(.{1,})\]$/) != Array.new && tab_liste_partage_samba[i].to_s.strip != "[global]" && tab_liste_partage_samba[i].to_s.strip != "[homes]" && tab_liste_partage_samba[i].to_s.strip != "[netlogon]" && tab_liste_partage_samba[i].to_s.strip != "[profiles]" && tab_liste_partage_samba[i].to_s.strip != "[Tous]" && tab_liste_partage_samba[i].to_s.strip != "[printers]" && tab_liste_partage_samba[i] != "[print$]"
tab_lecture_partage_samba_valid.push(tab_liste_partage_samba[i].to_s.strip)
end
}
tab_lecture_partage_samba_valid
end
def ajout_partage_samba(nom_partage,comment,path,printable,writable,browseable,choix_public,share_modes,guest_ok,droit_create_mask,droit_directory_mask,valid_users)
contenu_partage_ajoute = "[#{nom_partage}]
comment = #{comment}
path = #{path}
printable = #{printable.to_i == 1 ? 'yes' : 'no'}
writable = #{writable.to_i == 1 ? 'yes' : 'no'}
browseable = #{browseable.to_i == 1 ? 'yes' : 'no'}
public = #{choix_public.to_i == 1 ? 'yes' : 'no'}
share modes = #{share_modes.to_i == 1 ? 'yes' : 'no'}
guest ok = #{guest_ok.to_i == 1 ? 'yes' : 'no'}
create mask = 00#{droit_create_mask}
directory mask = 00#{droit_directory_mask}
#{valid_users.empty? ? "" : 'valid users = '+valid_users}\n\n"
File.open("/Users/nicolas/smb.conf", "a+") do |file|
file.write(contenu_partage_ajoute)
end
end
def recup_info_partage_samba_update(nom_partage)
hash1 = Hash.new
tab_liste_info_partage_samba = Array.new
lect_file_samba.each_line{ |liste_contenu_samba|
#if liste_contenu_samba.to_s.strip = nom_partage.to_s.strip
#hash1["nom_partage"] =
#end
tab_liste_info_partage_samba.push(liste_contenu_samba.to_s.strip)
}
0.upto(tab_liste_info_partage_samba.length-1){ |i|
if tab_liste_info_partage_samba[i].to_s.strip == nom_partage.to_s.strip
hash1["nom_partage"] = nom_partage.to_s.strip
while !tab_liste_info_partage_samba[i].empty?
i = i + 1
tab_contenu_partage_detail = tab_liste_info_partage_samba[i].to_s.strip.split("=")
hash1[tab_contenu_partage_detail[0].to_s.strip] = tab_contenu_partage_detail[1]
end
end
}
hash1
end
def modification_partage_samba(nom_evenement_hide,nom_evenement,comment,path,printable,writable,browseable,choix_public,share_modes,guest_ok,valid_users,droit_create_mask,droit_directory_mask)
tab_contenu_samba = Array.new
lect_file_samba.each_line{ |line|
tab_contenu_samba.push(line.to_s.strip)
}
0.upto(tab_contenu_samba.length-1){ |i|
if nom_evenement_hide.to_s.strip == tab_contenu_samba[i].to_s.strip
tab_contenu_samba[i] = "[#{nom_evenement}]"
i = i + 1
tab_contenu_samba[i] = "comment = #{comment}"
i = i + 1
tab_contenu_samba[i] = "path = #{path}"
i = i + 1
tab_contenu_samba[i] = "printable = #{printable.to_i == 1 ? 'yes' : 'no'}"
i = i + 1
tab_contenu_samba[i] = "writable = #{writable.to_i == 1 ? 'yes' : 'no'}"
i = i + 1
tab_contenu_samba[i] = "browseable = #{browseable.to_i == 1 ? 'yes' : 'no'}"
i = i + 1
tab_contenu_samba[i] = "public = #{choix_public.to_i == 1 ? 'yes' : 'no'}"
i = i + 1
tab_contenu_samba[i] = "guest ok = #{guest_ok.to_i == 1 ? 'yes' : 'no'}"
i = i + 1
tab_contenu_samba[i] = "share modes = #{share_modes.to_i == 1 ? 'yes' : 'no'}"
i = i + 1
tab_contenu_samba[i] = "create mask = 00#{droit_create_mask}"
i = i + 1
if valid_users.empty?
tab_contenu_samba[i] = "directory mask = 00#{droit_directory_mask}\n\n"
else
tab_contenu_samba[i] = "directory mask = 00#{droit_directory_mask}"
i = i + 1
tab_contenu_samba[i] = "valid users = #{valid_users}\n\n"
end
end
}
File.open("/Users/nicolas/smb.conf", "w") do |file|
0.upto(tab_contenu_samba.length-1){ |i|
file.write("#{tab_contenu_samba[i]}\n")
}
end
end
def sup_partage_samba(nom_partage)
tab_contenu_samba = Array.new
lect_file_samba.each_line{ |line|
tab_contenu_samba.push(line.to_s.strip)
}
0.upto(tab_contenu_samba.length-1){ |i|
if tab_contenu_samba[i].to_s.strip == nom_partage.to_s.strip
while !tab_contenu_samba[i].empty?
tab_contenu_samba.delete_at(i)
i = i + 1
end
end
}
File.open("/Users/nicolas/smb.conf", "w") do |file|
0.upto(tab_contenu_samba.length-1){ |i|
file.write("#{tab_contenu_samba[i]}\n")
}
end
end
def reste_form_add_iptables
render(:update){ |page|
if params[:id].to_s == 'forward'
else
end
}
end
end
end