Skip to content

Commit 4d0e15f

Browse files
committed
adduser functionality
1 parent 382105f commit 4d0e15f

File tree

2 files changed

+90
-16
lines changed

2 files changed

+90
-16
lines changed

api.py

+82-15
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,26 @@
3535
app = Flask(__name__)
3636

3737
def validate_login(form):
38-
print "vlcero"
39-
print form
38+
39+
print "validate_login: %s" % form
40+
4041
try:
41-
print "vluno"
42-
print form
4342
username = form['username']
44-
print "vldos"
4543
password = form['password']
4644
if len(username)==0:
4745
return response({'error': 'Invalid credentials'} ,status=401)
4846
except KeyError, error:
4947
print "KeyError %s" % form
5048
return Response('Wrong request' ,status=400)
5149
except:
52-
print "vlcuatro"
53-
print form
50+
5451
raise
55-
print "vltres"
52+
5653

5754
user_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net" % username
5855
l = ldap_bind(user_dn, password)
5956
return (user_dn, l, username, password)
57+
6058
def add_record(l,dn, new):
6159
try:
6260
ldif = modlist.addModlist(new)
@@ -69,6 +67,20 @@ def add_record(l,dn, new):
6967
raise
7068
return False
7169

70+
def increase_current_uidnumber(l):
71+
update_dn = "uid=template.user,ou=Users,ou=auth,dc=guifibages,dc=net"
72+
original_record = l.search_s(update_dn, ldap.SCOPE_BASE)[0][1]
73+
modified_record = copy.deepcopy(original_record)
74+
modified_record['uidNumber'][0] = str(int(modified_record['uidNumber'][0])+1)
75+
modified, error = modify_ldap_property(l, update_dn, original_record, modified_record)
76+
if modified:
77+
print "increase_current_uidnumber correct"
78+
return True
79+
else:
80+
print "couldn't increase_current_uidnumber"
81+
return False
82+
83+
7284
def modify_ldap_property(l, modified_dn, old, new):
7385
try:
7486
ldif = modlist.modifyModlist(old, new)
@@ -121,8 +133,7 @@ def read_pac(view="internet"):
121133

122134
def user_access(l, user_dn):
123135
result = l.search_s(user_dn, ldap.SCOPE_SUBTREE, 'objectClass=*', ['memberOf'])[0][1]
124-
print user_dn
125-
print result
136+
print "user_access:\n\t%s\n\t%s" % (user_dn, result)
126137
if 'memberOf' in result and "cn=ldapAdmin,ou=Groups,ou=auth,dc=guifibages,dc=net" in result['memberOf']:
127138
return "admin"
128139
else:
@@ -184,7 +195,7 @@ def list_users():
184195
@app.route('/api/user/<target_user>/get', methods = ['POST'])
185196
@crossdomain('*')
186197
def get_user(target_user):
187-
print "Uno"
198+
188199
try:
189200
user_dn, l, username, password = validate_login(request.form)
190201
except ldap.INVALID_CREDENTIALS:
@@ -194,22 +205,75 @@ def get_user(target_user):
194205
except:
195206
return response({'error': "shit happened"}, 500)
196207

197-
print "Dos"
208+
198209
target_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net" % target_user
199210

200211
try:
201212
result = l.search_s(target_dn, ldap.SCOPE_SUBTREE, 'objectClass=*')[0][1]
202213
except ldap.NO_SUCH_OBJECT:
203214
return response({'error': 'Not found'} ,status=404)
204-
print "tres"
215+
205216

206217
result['access_level'] = user_access(l, user_dn)
207-
print "cuatro"
218+
208219
return response(result)
209220

221+
@app.route('/api/user/new', methods = ['POST'])
222+
@crossdomain('*')
223+
def add_user():
224+
result = {}
225+
try:
226+
user_dn, l, username, password = validate_login(request.form)
227+
except ldap.INVALID_CREDENTIALS:
228+
return response({'error': 'Invalid credentials'} ,status=401)
229+
except ldap.SERVER_DOWN:
230+
return response({'error': "Can't connect to server"} ,status=500)
231+
except:
232+
return response({'error': "shit happened"}, 500)
233+
update_dn = "uid=template.user,ou=Users,ou=auth,dc=guifibages,dc=net"
234+
original_record = l.search_s(update_dn, ldap.SCOPE_BASE)[0][1]
235+
modified_record = copy.deepcopy(original_record)
236+
print request.form
237+
check_search = '(uid=%s)' % request.form['newuser_uid']
238+
print "add_user -1"
239+
check = l.search_s("ou=Users,ou=auth,dc=guifibages,dc=net", ldap.SCOPE_SUBTREE, check_search, ['uid', 'cn', 'uidNumber'])
240+
print "add_user"
241+
print "add_user: %s (%d)" % (check, len(check))
242+
if len(check) > 0:
243+
244+
return response("User already exists %s" % request.form['newuser_uid'], 409)
245+
246+
247+
for field in request.form:
248+
if field in ['username', 'password']:
249+
continue
250+
new_value = request.form[field]
251+
new_value = new_value.encode('utf-8')
252+
if field == 'userPassword':
253+
new_value = ssha(request.form[field])
254+
if field[0:8] == 'newuser_':
255+
modified_record[field[8:]] = new_value
256+
continue
257+
if field not in original_record or original_record[field] != new_value and new_value not in original_record[field]:
258+
modified_record[field] = new_value
259+
modified_record['homeDirectory'] = "/guifibages/users/%s" % modified_record['uid']
260+
261+
print "Minga longa %s" % modified_record
262+
263+
new_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net" % modified_record['uid']
264+
modified, error = add_record(l, new_dn, modified_record)
265+
print "Result:\n modified: %s\n error: %s" % (modified, error)
266+
if modified:
267+
increase_current_uidnumber(l)
268+
return response(modified_record)
269+
else:
270+
return response(error, error['code'])
271+
272+
210273
@app.route('/api/user/<target_user>/update', methods = ['POST'])
211274
@crossdomain('*')
212275
def update_user(target_user):
276+
print "update_user:\n\t%s" % request.form
213277
result = {}
214278
try:
215279
user_dn, l, username, password = validate_login(request.form)
@@ -219,6 +283,7 @@ def update_user(target_user):
219283
return response({'error': "Can't connect to server"} ,status=500)
220284
except:
221285
return response({'error': "shit happened"}, 500)
286+
222287
update_dn = "uid=%s,ou=Users,ou=auth,dc=guifibages,dc=net" % target_user
223288
original_record = l.search_s(update_dn, ldap.SCOPE_BASE)[0][1]
224289
modified_record = copy.deepcopy(original_record)
@@ -228,6 +293,8 @@ def update_user(target_user):
228293
continue
229294
new_value = request.form[field]
230295
new_value = new_value.encode('utf-8')
296+
if field == 'userPassword':
297+
new_value = ssha(request.form[field])
231298
if field not in original_record or original_record[field] != new_value and new_value not in original_record[field]:
232299
modified_record[field] = new_value
233300

@@ -272,4 +339,4 @@ def login():
272339
if __name__ == "__main__":
273340
global sessions
274341
sessions = dict()
275-
app.run(debug=True,port=8050)
342+
app.run(debug=True,port=8050)

ssha.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import base64, getpass, hashlib, os
22
def ssha(string):
3+
string = string.encode('utf-8')
34
salt = os.urandom(8) # edit the length as you see fit
4-
return '{SSHA}' + base64.b64encode(hashlib.sha1(string + salt).digest() + salt)
5+
print "salt: " + salt
6+
hash = hashlib.sha1(string + salt)
7+
dgst = hash.digest()
8+
print "dgst: " + dgst
9+
b64 = base64.b64encode(dgst + salt)
10+
return '{SSHA}' + b64
11+
#return '{SSHA}' + base64.b64encode(hashlib.sha1(string + salt).digest() + salt)

0 commit comments

Comments
 (0)