-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
232 lines (197 loc) · 8.19 KB
/
main.py
File metadata and controls
232 lines (197 loc) · 8.19 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import ldap
import ldap.modlist
import getpass
from passlib.hash import ldap_salted_sha1 as ssha
## LDAP database global settings
## =============================
ldapURI = # e.g. : "ldap://example.org"
adminDN = # e.g. : "cn=Administrator,dc=example,dc=org"
peopleBaseDN = # e.g. : "ou=people,dc=example,dc=org"
groupBaseDN = # e.g. : "ou=group,dc=example,dc=org"
defaultUsersGID = # e.g. : "100"
defaultUserPassword = # e.g. : "usersCreatedWithThisPassword"
## Search and List user or group
## =============================
def searchUserGroup(userGroupName,choice):
searchScope = ldap.SCOPE_SUBTREE
searchFilter = "cn="+ userGroupName
if choice == "1":
searchAttributes = ["uidNumber", "gidNumber"]
return connect.search_s(peopleBaseDN, searchScope, searchFilter, searchAttributes)
connect.unbind_s()
if choice == "6":
while 1:
listmember = input("List group members ? (Y/N) .......... : ")
if listmember in ["y", "Y", "n", "N"]:
break
if listmember in ["y", "Y"]:
searchAttributes = ["member"]
else:
searchAttributes = ["gidNumber"]
return connect.search_s(groupBaseDN, searchScope, searchFilter, searchAttributes)
connect.unbind_s()
## Create user or group
## ====================
def createUserGroup(userGroupName,choice):
if choice == "2":
userFirstName = input("User first name ..................... : ")
userLastName = input("User last name ...................... : ")
while 1:
userUID = input("User UID ............................ : ")
if len(userUID) != 0:
break
if len(userLastName) == 0:
userLastName = userGroupName
createAttributes = {
"objectClass": ["top".encode("utf-8"), "posixAccount".encode("utf-8"), "inetOrgPerson".encode("utf-8")],
"uid": [userGroupName.encode("utf-8")],
"givenName": [userFirstName.encode("utf-8")], # First name
"sn": [userLastName.encode("utf-8")], # Last name
"uidNumber": [userUID.encode("utf-8")],
"homeDirectory": [str("/home/"+ userGroupName).encode("utf-8")],
"loginShell": ["/bin/bash".encode("utf-8")],
"gidNumber": [defaultUsersGID.encode("utf-8")],
"description": ["Domain user".encode("utf-8")],
"userPassword": [str(ssha.hash(defaultUserPassword)).encode("utf-8")],
}
connect.add_s("cn="+ userGroupName +","+ peopleBaseDN,ldap.modlist.addModlist(createAttributes))
connect.unbind_s()
if choice == "4":
groupDescription = input("Group description ................... : ")
while 1:
groupIDNumber = input("Group GID ........................... : ")
if len(groupIDNumber) != 0:
break
createAttributes = {
"objectClass": ["top".encode("utf-8"), "posixGroup".encode("utf-8"), "groupOfNames".encode("utf-8")],
"gidNumber": [groupIDNumber.encode("utf-8")],
"description": ["Domain user".encode("utf-8")],
}
connect.add_s("cn="+ userGroupName +","+ groupBaseDN,ldap.modlist.addModlist(createAttributes))
connect.unbind_s()
## Add/delete group member
## =======================
def addDeleteMember():
userName = input("User name ........................... : ")
groupName = input("Group name .......................... : ")
userDN = str("cn="+ userName +","+ peopleBaseDN).encode("utf-8")
if len(searchUserGroup(userName,"1")) == 0:
print(userName +" doesn't exist")
else:
try:
while 1:
addDelete = input("Add member (a), Delete member (d) ... : ")
if addDelete in ["a", "A", "d", "D"]:
break
if addDelete in ["a", "A"]:
connect.modify_s("cn="+ groupName +","+ groupBaseDN,[(ldap.MOD_ADD, "member", [userDN])])
else:
connect.modify_s("cn="+ groupName +","+ groupBaseDN,[(ldap.MOD_DELETE, "member", [userDN])])
except ldap.NO_SUCH_OBJECT :
print(groupName +" doesn't exist")
except ldap.NO_SUCH_ATTRIBUTE :
print(userName +" isn't member of "+ groupName)
except ldap.TYPE_OR_VALUE_EXISTS :
print(userName +" is already member of "+ groupName)
connect.unbind_s()
## Update user's passsword
## =======================
def userPasswordUpdate(userGroupName):
while 1:
passwd1 = getpass.getpass("New password ........................ : ")
passwd2 = getpass.getpass("Confirm new password ................ : ")
if passwd1 == passwd2:
break
else:
print("\n"+"Error ! - typing doesn't match."+"\n")
oldPassword = {"userPassword": ["*"]}
newPassword = {"userPassword": [str(ssha.hash(passwd1)).encode("utf-8")]}
modifyAttributes = ldap.modlist.modifyModlist(oldPassword, newPassword)
connect.modify_s("cn="+ userGroupName +","+ peopleBaseDN, modifyAttributes)
connect.unbind_s()
## Delete a user or a group
## ========================
def deleteUserOrGroup(userGroupName):
while 1:
deleteUserGroup = input("Delete a user (u), a group (g) ...... : ")
if deleteUserGroup in ["u", "U", "g", "G"]:
break
if deleteUserGroup in ["u", "U"]:
connect.delete_s("cn="+ userGroupName +","+ peopleBaseDN)
else:
connect.delete_s("cn="+ groupBaseDN +","+ peopleBaseDN)
connect.unbind_s()
## Launch operation
## ================
def exec():
print()
print("Search for a user ................... = 1")
print("Create user ......................... = 2")
print("Update user password ................ = 3")
print("Create group ........................ = 4")
print("Add/Delete group members ............ = 5")
print("List groups or a group's members .... = 6")
print("Delete a user or a group ............ = 7")
print("Quit ................................ = 8")
print()
choice = input("Choose an operation ................. : ")
print()
if choice in ["1", "2", "3", "4", "6", "7"]:
userGroupName = input("User or group unique ID ............. : ")
# Search for user and list group members
if choice in ["1", "6"]:
if len(searchUserGroup(userGroupName,choice)) == 0:
print("User or group doesn't exist")
exec()
else:
print(searchUserGroup(userGroupName,choice))
exec()
# Create user or group
elif choice in ["2", "4"]:
if len(searchUserGroup(userGroupName,"1")) >= 1:
print("User or group already exist")
exec()
else:
createUserGroup(userGroupName,choice)
exec()
# Update user password
elif choice == "3":
if len(searchUserGroup(userGroupName,"1")) == 0:
print("User doesn't exist")
exec()
else:
userPasswordUpdate(userGroupName)
exec()
# Add/Delete group members
elif choice == "5":
addDeleteMember()
exec()
# Delete a user or a groug
elif choice == "7":
deleteUserOrGroup(userGroupName)
exec()
# Script ending
elif choice == "8":
print("Ending the script..."+"\n")
else:
print(choice +" is not a valid operation")
exec()
## LDAP Connect
## ============
connect = ldap.initialize(ldapURI)
connect.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
connect.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
connect.set_option(ldap.OPT_X_TLS_NEWCTX, 0)
while 1:
print()
adminPWD = getpass.getpass("LDAP administrator password ......... : ")
try:
connect.simple_bind_s(adminDN, adminPWD)
except ldap.INVALID_CREDENTIALS:
print("\n"+"Incorrect login or password")
except ldap.SERVER_DOWN:
print("\n"+"Unable to join server at " + ldapURI +"\n")
break
else:
exec()
break