Skip to content

Commit c63bdde

Browse files
committed
Afegit:
wbpass: Lector d'arxius winbox gbuser: Script per autogestió d'usuaris Guifibages (LDAP)
1 parent a762615 commit c63bdde

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed

gbuser

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Script per gestió de contrasenyes LDAP
5+
#
6+
# Copyright (c) 2012, Ignacio Torres Masdeu <[email protected]>
7+
#
8+
# Permission to use, copy, modify, and/or distribute this software for any
9+
# purpose with or without fee is hereby granted, provided that the above
10+
# copyright notice and this permission notice appear in all copies.
11+
#
12+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13+
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14+
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15+
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16+
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17+
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18+
# PERFORMANCE OF THIS SOFTWARE.
19+
#
20+
21+
import ldap
22+
import ldap.modlist as modlist
23+
import getpass
24+
import sys
25+
import os
26+
27+
def connect():
28+
try:
29+
global password
30+
global user_dn
31+
global uid
32+
uid = getpass.getuser()
33+
user_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net"%uid
34+
password = getpass.getpass("Contrasenya: ")
35+
l = ldap.initialize("ldaps://aaa.guifibages.net:636")
36+
l.simple_bind_s(user_dn,password)
37+
return l
38+
except ldap.LDAPError, error_message:
39+
print "Couldn't Connect. %s " % error_message
40+
exit(1)
41+
42+
def password_name(password_type):
43+
password_names = {
44+
"userPassword": "contrasenya d'usuari",
45+
"guifibagesPlaintextPassword": "contrasenya de xarxa",
46+
"guifibagesApplicationPassword": "contrasenya d'aplicacions"
47+
}
48+
return password_names[password_type]
49+
50+
def compare_password(new_password, current_passwords):
51+
error_message = "Error: La nova contrasenya és igual a l'actual %s.\nContrasenya no canviada"
52+
if (new_password in password or password in new_password):
53+
print error_message % password_name("userPassword")
54+
sys.exit(1)
55+
56+
for password_type in current_passwords:
57+
if password_type == 'userPassword':
58+
continue
59+
for cpassword in current_passwords[password_type]:
60+
if (new_password in cpassword or cpassword in new_password):
61+
print error_message % password_name(password_type)
62+
sys.exit(1)
63+
64+
def modify_ldap_property(old, new):
65+
try:
66+
ldif = modlist.modifyModlist(old, new)
67+
l.modify_s(user_dn, ldif)
68+
return True
69+
except:
70+
return False
71+
72+
def set_password(password_type):
73+
new_password = getpass.getpass("Nova %s: " % password_name(password_type))
74+
if (len(new_password)<8):
75+
print "Error: La nova contrasenya ha de tenir un mínim de 8 caracters"
76+
sys.exit(1)
77+
if password_type == "userPassword":
78+
try:
79+
l.passwd(user_dn, password, new_password)
80+
print "%s canviada correctament" % (password_name(password_type).capitalize())
81+
except:
82+
print "Error intentant modificar %s " % (password_name(password_type))
83+
print "Unexpected error:", sys.exc_info()[0]
84+
raise
85+
sys.exit(0)
86+
87+
88+
new = { password_type: new_password }
89+
current_passwords = get_password()
90+
91+
compare_password(new_password, current_passwords)
92+
93+
if (password_type in current_passwords):
94+
old = { password_type: current_passwords[password_type][0] }
95+
else:
96+
old = {}
97+
98+
99+
if (modify_ldap_property(old,new)):
100+
print "%s canviada correctament" % (password_name(password_type).capitalize())
101+
else:
102+
print "Error intentant modificar %s " % (password_name(password_type))
103+
104+
def set_plaintext_password():
105+
pass
106+
107+
def set_network_password():
108+
pass
109+
110+
def set_application_password():
111+
pass
112+
113+
def print_passwords():
114+
current_passwords = get_password()
115+
for password_type in current_passwords:
116+
if password_type == 'userPassword':
117+
continue
118+
for cpassword in current_passwords[password_type]:
119+
print "%s: %s" % (password_name(password_type).capitalize(), cpassword)
120+
121+
def get_password(password_type=None):
122+
try:
123+
res = l.search_s(user_dn, ldap.SCOPE_BASE, 'objectClass=*', ['guifibagesPlaintextPassword', 'userPassword', 'guifibagesApplicationPassword'])
124+
passwords = res[0][1]
125+
if (password_type == None):
126+
return res[0][1]
127+
else:
128+
return res[0][1][password_type]
129+
130+
except AttributeError, error_message:
131+
print "No such user"
132+
133+
def print_help():
134+
print """Veure o modificar contrasenyes Guifibages
135+
Ús: %s [-h | -u | -x | -a]
136+
137+
Sense opcions: mostrar contrasenyes de xarxa i aplicacions
138+
139+
Opcions:
140+
-h Mostrar aquesta ajuda
141+
-u Modificar contrasenya d'usuari
142+
-x Modificar contrasenya de xarxa
143+
-a Modificar contrasenya d'aplicacions""" % (sys.argv[0])
144+
sys.exit(0)
145+
146+
def parse_args():
147+
global l
148+
try:
149+
if (sys.argv[1] == '-h'):
150+
print_help()
151+
sys.exit(0)
152+
switch = {
153+
'-u': "userPassword",
154+
'-x': "guifibagesPlaintextPassword",
155+
'-a': "guifibagesApplicationPassword"
156+
}
157+
password_type = switch.get(sys.argv[1])
158+
if password_type == None:
159+
print_help()
160+
sys.exit(1)
161+
l = connect()
162+
set_password(password_type)
163+
164+
except IndexError:
165+
l = connect()
166+
print_passwords()
167+
168+
if __name__=='__main__':
169+
parse_args()

wbpass

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Lector d'arxius winbox
5+
#
6+
# Copyright (c) 2012, Ignacio Torres Masdeu <[email protected]>
7+
#
8+
# Permission to use, copy, modify, and/or distribute this software for any
9+
# purpose with or without fee is hereby granted, provided that the above
10+
# copyright notice and this permission notice appear in all copies.
11+
#
12+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13+
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14+
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15+
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16+
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17+
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18+
# PERFORMANCE OF THIS SOFTWARE.
19+
#
20+
21+
import sys
22+
import re
23+
from os.path import basename
24+
progname = basename(sys.argv[0])
25+
26+
def print_help():
27+
print """Forma d’ús: %s <FITXER>
28+
Lector d'arxius winbox.cfg i wbx
29+
""" % progname
30+
31+
32+
def clean(item):
33+
bound = '\7f'
34+
keep = ''
35+
for i in range(0,31):
36+
bound += chr(i)
37+
for i in range(32,127):
38+
keep += chr(i)
39+
for i in range(128,255):
40+
keep += chr(i)
41+
retval={}
42+
43+
# La miga del tema és aqui
44+
# Bàsicament el que fem es cercar cadenes de text (ascii 32-127 i ascii
45+
# extés 128-255) que estiguin envoltades per caràcters de control
46+
47+
values = re.findall('[%s][%s]+[%s]' % (bound, keep, bound), item)
48+
for v in values:
49+
for key in 'host login note pwd'.split():
50+
if v[1:len(key)+1] == key:
51+
# Com que la regexp inclou un caràcter de control a
52+
# cada banda hem de tenir-ho en compte per extraure
53+
# l'informació
54+
retval[key] = v[len(key)+1:-1]
55+
return retval
56+
57+
58+
if __name__=='__main__':
59+
try:
60+
f = open(sys.argv[1])
61+
except IndexError:
62+
print "%s: cal especificar un fitxer" % progname
63+
sys.exit(1)
64+
except:
65+
print_help()
66+
sys.exit(1)
67+
68+
a = f.read()
69+
items = re.split('\x09\x00\x04', a)
70+
keep = items[0]
71+
for (i, val) in enumerate(items):
72+
items[i] = clean(val)
73+
for key in items[i]:
74+
print "%s: %s" % (key, items[i][key])
75+
print

0 commit comments

Comments
 (0)