|
#!/usr/bin/python3 |
|
|
|
import ldapHelper |
|
import sys, csv |
|
|
|
EXTRA_USER_ATTRIBUTES_STORE="/etc/linuxmuster/sophomorix/default-school/students.extra.csv" |
|
|
|
print("Start") |
|
|
|
# Functions |
|
|
|
# Sophomorix logfile stuff |
|
|
|
def getRecentlyAddedUsers(): |
|
""" |
|
Get all recently added users from sophomorix logfile |
|
""" |
|
|
|
rc, extraUserAttributes = getExtraUserAttributes() |
|
if not rc: |
|
return False, None |
|
|
|
with open("/var/log/sophomorix/userlog/user-add.log", encoding="utf-8") as sophomorixAddLogfile: |
|
csvReader = csv.reader(sophomorixAddLogfile, delimiter=':') |
|
|
|
recentlyAddedUsers = [] |
|
for line in csvReader: |
|
if line[0] != "ADD": |
|
continue |
|
|
|
user = { |
|
"importTimestamp": line[2], |
|
"school": line[6], |
|
"username": line[8], |
|
"lastName": line[10], |
|
"firstName": line[12], |
|
"class": line[14], |
|
"role": line[16], |
|
"id": line[18] |
|
} |
|
|
|
if user["id"] != "---" and user["id"] in extraUserAttributes: |
|
user = {**user, **extraUserAttributes[user["id"]]} |
|
|
|
recentlyAddedUsers.append(user) |
|
|
|
|
|
return True, recentlyAddedUsers |
|
|
|
def getUsersAddedInLastImport(): |
|
""" |
|
Get all users that were added during the last import |
|
""" |
|
rc, recentlyAddedUsers = getRecentlyAddedUsers() |
|
if not rc: |
|
return False, None |
|
|
|
usersAddedInLastImport = [] |
|
timestampOfLastImport = -1 |
|
|
|
for recentlyAddedUser in reversed(recentlyAddedUsers): |
|
if timestampOfLastImport == -1: |
|
timestampOfLastImport = recentlyAddedUser["importTimestamp"] |
|
|
|
if timestampOfLastImport != recentlyAddedUser["importTimestamp"]: |
|
break |
|
|
|
usersAddedInLastImport.append(recentlyAddedUser) |
|
|
|
return True, usersAddedInLastImport |
|
|
|
def getExtraUserAttributes(): |
|
""" |
|
Get all extra user attributes from import |
|
""" |
|
global EXTRA_USER_ATTRIBUTES_STORE |
|
|
|
with open(EXTRA_USER_ATTRIBUTES_STORE, encoding="utf-8") as extraAttributesFile: |
|
csvReader = csv.reader(extraAttributesFile, delimiter=';') |
|
|
|
extraUserAttributes = {} |
|
for line in csvReader: |
|
extraUserAttributes[line[0]] = { |
|
"email": line[1] |
|
} |
|
|
|
return True, extraUserAttributes |
|
|
|
# Ldap user stuff |
|
|
|
def setFirstPasswordOfUser(username): |
|
print(" * Processing first password ... ", end="") |
|
rc, ldapUser = ldapHelper.searchOne("sAMAccountName={}".format(user["username"])) |
|
if not rc: |
|
print("-> Error reading user from LDAP!") |
|
return False |
|
|
|
desiredFirstPassword = ldapUser["sophomorixBirthdate"] + "Muster!" |
|
|
|
if desiredFirstPassword == ldapUser["sophomorixFirstPassword"]: |
|
print("-> nothing to do") |
|
return True |
|
|
|
rc = ldapHelper.setUserPassword(ldapUser["distinguishedName"], desiredFirstPassword, isFirstPassword=True) |
|
|
|
print("-> OK" if rc else "-> Error writing to LDAP") |
|
return rc |
|
|
|
def setFirstEmailOfUser(user): |
|
username = user["username"] |
|
print(" * Processing first email ... ", end="") |
|
rc, ldapUser = ldapHelper.searchOne(f"sAMAccountName={username}") |
|
if not rc: |
|
print("-> Error reading user from LDAP!") |
|
return False |
|
|
|
if "sophomorixCustom1" in ldapUser: |
|
print("-> nothing to do") |
|
return True |
|
|
|
if "email" in user: |
|
newMail = user["email"] |
|
else: |
|
print("-> No mail found in file!") |
|
newMail = f"{username}@linuxmuster.lan" |
|
return True |
|
|
|
rc = ldapHelper.setAttribute(ldapUser["distinguishedName"], "sophomorixCustom1", newMail) |
|
|
|
print("-> OK" if rc else "-> Error writing to LDAP") |
|
return rc |
|
|
|
# end - Functions |
|
|
|
if not ldapHelper.bindAsAdmin(): |
|
sys.exit(1) |
|
|
|
rc, usersAddedInLastImport = getUsersAddedInLastImport() |
|
if not rc: |
|
sys.exit(1) |
|
|
|
for user in usersAddedInLastImport: |
|
if user["class"] not in ["mlm", "extern"]: |
|
continue |
|
|
|
print("* Processing user {}".format(user["username"])) |
|
#setFirstPasswordOfUser(user["username"]) |
|
setFirstEmailOfUser(user) |