-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaccountServices.py
162 lines (143 loc) · 6.28 KB
/
accountServices.py
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
import logging
import random
import ldap
import kadmin
import socket
from papercut import PaperCut
class Manager:
def __init__(self, config):
self.loungeACL = config["ACL"]
self.config = config
self.words = config["WORDS"]
self.logger = logging.getLogger("AcctServices")
self.kadmin = kadmin.KAdmin(config["krb5"]["aprinc"],
config["krb5"]["atab"])
self.mailDomain = config["SETTINGS"]["mailDomain"]
self.gidNumber = config["SETTINGS"]["userGID"]
self.fileServerAddress = config["SETTINGS"]["fileServerAddress"]
self.fileServerPort = config["SETTINGS"]["fileServerPort"]
self.pc = PaperCut(config["papercut"]["url"], config["papercut"]["secret"])
def uidExists(self, username):
conn = self.connectLDAP()
result = conn.search_s("ou=people,dc=collegiumv,dc=org",
ldap.SCOPE_SUBTREE,
"(uid={0})".format(username), attrlist=["uid"])
conn.unbind()
self.logger.debug("Account %s exists? %s", username, bool(len(result)))
return bool(len(result))
def netIDExists(self, netID):
conn = self.connectLDAP()
result = conn.search_s("ou=people,dc=collegiumv,dc=org",
ldap.SCOPE_SUBTREE,
"(netID={0})".format(netID),
attrlist=["netID"])
conn.unbind()
self.logger.debug("Account %s exists? %s", netID, bool(len(result)))
return bool(len(result))
def uidFromNetID(self, netID):
conn = self.connectLDAP()
result = conn.search_s("ou=people,dc=collegiumv,dc=org",
ldap.SCOPE_SUBTREE,
"(netID={0})".format(netID), attrlist=["uid"])
conn.unbind()
return result[0][1]['uid'][0]
def provision(self, netID, username, password):
success = False
fname = self.loungeACL[netID][0]
lname = self.loungeACL[netID][1]
ObjectClass = ["inetOrgPerson", "posixAccount", "cvPerson"]
userDN = "uid="+username+",ou=people,dc=collegiumv,dc=org"
# create the ldap values
ldapAttrs = list()
ldapAttrs.append(("objectClass", ObjectClass))
ldapAttrs.append(("cn", [fname + ' ' + lname]))
ldapAttrs.append(("sn", [lname]))
ldapAttrs.append(("ou", ["cv"]))
ldapAttrs.append(("displayName", [fname + ' ' + lname]))
ldapAttrs.append(("givenName", [fname]))
ldapAttrs.append(("netID", [str(netID)]))
ldapAttrs.append(("mail", [str(netID+'@' + self.mailDomain)]))
ldapAttrs.append(("o", ["Collegium V"]))
ldapAttrs.append(("uid", [str(username)]))
ldapAttrs.append(("uidNumber", [str(self.nextUID())]))
ldapAttrs.append(("userPassword", ["{{SASL}}{0}@COLLEGIUMV.ORG"
.format(username)]))
ldapAttrs.append(("gidNumber", [str(self.gidNumber)]))
ldapAttrs.append(("homeDirectory", [str("/home/" + username)]))
ldapAttrs.append(("loginShell", ["/bin/bash"]))
ldapAttrs.append(("desktopEnvironment", ["cinnamon"]))
conn = self.connectLDAP()
try:
conn.add_s(userDN, ldapAttrs)
if self.kadmin.createPrinc(username, password):
self.logger.info("Successfully provisioned account %s for %s",
username, netID)
else:
self.logger.error("Kerberos Error on account %s", username)
fileSock = socket.socket()
try:
fileSock.connect((self.fileServerAddress, self.fileServerPort))
except socket.error, e:
self.logger.error("FileServer socket error: %s", e[1])
finally:
fileSock.close()
success = True
except ldap.LDAPError as e:
self.logger.error("An ldap error has occured, %s", e)
finally:
conn.unbind()
self.pc.performUserAndGroupSync()
return success
def connectLDAP(self):
try:
conn = ldap.initialize(self.config["LDAP"]["ldapAddr"])
bindDN = self.config["LDAP"]["bindDN"]
bindPW = self.config["LDAP"]["bindPW"]
try:
conn.bind_s(bindDN, bindPW)
except ldap.INVALID_CREDENTIALS:
self.logger.severe("Invalid LDAP credentials")
except ldap.LDAPError as e:
self.logger.error("LDAP Error: %s", e)
except:
self.logger.error("An unidentified error occured in LDAP")
return conn
def chPassword(self, username, password):
return self.kadmin.chPassword(username, password)
def mkPassword(self):
self.logger.debug("Made a password")
password = ""
for i in range(0, 4):
password += random.choice(self.words).capitalize()
return password
def nextUID(self):
conn = self.connectLDAP()
num = self.getLastuidNumber(sock=conn)
conn.unbind()
return num+1
def getLastuidNumber(self, sock=None):
# this function courtesy of justanull
lastIndex = 0
highestUid = 0
while True:
searchID = sock.search("ou=people,dc=collegiumv,dc=org",
ldap.SCOPE_SUBTREE,
"(uidNumber>={0})".format(lastIndex),
attrlist=["uidNumber"])
while True:
try:
tempResults = list()
tempResults.append(sock.result(searchID, all=0, timeout=2))
if len(tempResults[0][1]) == 0:
# oh god why
return highestUid
if tempResults[-1][1] == list():
tempResults = tempResults[:-1]
for entry in tempResults:
for uidEntry in entry[1][0][1].values():
uid = int(uidEntry[0])
lastIndex = uid
if uid > highestUid:
highestUid = uid
except (ldap.TIMEOUT, ldap.SIZELIMIT_EXCEEDED):
break