[sysadmin-bin] Improve the script removing some useless functions



commit 79c1e661df1247d3cbc75f76c86c885094cb0e40
Author: Andrea Veri <av gnome org>
Date:   Fri Oct 3 18:41:56 2014 +0200

    Improve the script removing some useless functions

 reset-my-password.py |   58 +++++++------------------------------------------
 1 files changed, 9 insertions(+), 49 deletions(-)
---
diff --git a/reset-my-password.py b/reset-my-password.py
index afd4438..c29179b 100755
--- a/reset-my-password.py
+++ b/reset-my-password.py
@@ -13,19 +13,7 @@ LDAP_GROUP_BASE='cn=groups,cn=accounts,dc=gnome,dc=org'
 LDAP_USER_BASE='cn=users,cn=accounts,dc=gnome,dc=org'
 
 
-file = open('/home/admin/secret/freeipa','r')
-lines = file.readlines()
-
-for line in lines:
-        if line.find("ldap_password") > -1:
-                dirty_password = line.split()
-                ldap_password = str(dirty_password)
-
-                sanitize_file=["ldap_password", "\"", "[", "]", "=", "'"]
-                for i in range(len(sanitize_file)):
-                        ldap_password = ldap_password.replace(sanitize_file[i],"")
-file.close()
-
+execfile('/home/admin/secret/freeipa')
 
 try:
     l = ldap.open('localhost')
@@ -41,10 +29,8 @@ def _parse_members_from_group(group):
     results = l.search_s(LDAP_GROUP_BASE, ldap.SCOPE_SUBTREE, filter, ('memberUid', ))
 
     members = set()
-    for entry in results:
-        id = entry[0]
-        attr = entry[1]
-
+ 
+    for _, attr in results:
         members.update(attr['memberUid'])
 
 
@@ -100,43 +86,17 @@ def check_existing_password(userid):
        print 'You are not allowed to reset your password, please contact the GNOME Sysadmin Team to know why'
        sys.exit(1)
 
-    try:
-        userpassword = _get_attributes_from_ldap(userid, 'userPassword')
-    except KeyError:
-        add_new_password(userid)
-    else:
-        update_existing_password(userid)
-
-
-def update_existing_password(userid):
-    dn = 'uid=%s,%s' % (userid, LDAP_USER_BASE)
-
-    getattr_userpassword = _get_attributes_from_ldap(userid, 'userPassword')
-    getattr_name = _get_attributes_from_ldap(userid, 'cn')
-    getattr_mail = _get_attributes_from_ldap(userid, 'mail')
-    old_password = getattr_userpassword[userid]
-
-    password = {'userPassword': '%s' % (old_password)}
-    newpassword = {'userPassword': gen_passwd(length=20)}
-
-    ldif = modlist.modifyModlist(password, newpassword)
-    l.modify_s(dn, ldif)
-
-
-    send_password_to_user(getattr_name[userid], getattr_mail[userid], newpassword['userPassword'])
-
+    update_password(userid)
 
-def add_new_password(userid):
-    dn = 'uid=%s,%s' % (userid, LDAP_USER_BASE)
 
+def update_password(userid):
     getattr_name = _get_attributes_from_ldap(userid, 'cn')
     getattr_mail = _get_attributes_from_ldap(userid, 'mail')
 
-    nopassword = {'userPassword': ''}
-    newpassword = {'userPassword': gen_passwd(length=20)}
+    newpassword = {'userPassword': gen_passwd()}
 
-    ldif = modlist.modifyModlist(nopassword, newpassword)
-    l.modify_s(dn, ldif)
+    add_members = [(ldap.MOD_REPLACE, 'userPassword', newpassword['userPassword'])]
+    l.modify_s('uid=%s,cn=users,cn=accounts,dc=gnome,dc=org' % userid, add_members)
 
 
     send_password_to_user(getattr_name[userid], getattr_mail[userid], newpassword['userPassword'])
@@ -144,7 +104,7 @@ def add_new_password(userid):
 
 def send_password_to_user(name, email, password):
     form_letter = """
-Hello %s, your password has been reset successfully and is available here:
+Hello %s, your password has been reset successfully. Your temporary password is
 
 %s
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]