-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaotikumAddUser
executable file
·92 lines (64 loc) · 1.87 KB
/
chaotikumAddUser
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
#!/usr/bin/env python3
import getpass
import sys
from lib import util
c = util.connect()
if c == None:
print('Authentication failed.')
sys.exit(1)
while True:
username = input('\nUsername: ').lower()
if util.user_exists(c, username):
print('User \'%s\' already exists.' % username)
else:
break
givenName = input('Given name: ')
surname = input('Surname: ')
while True:
memberString = input('Member [y/n]: ')
if memberString in ['y', 'n']:
member = memberString == 'y'
break
userdn = 'uid=%s,ou=users,ou=%s,%s' % (username, ['external', 'internal'][member], util.BASE)
homedir = '/home/%s' % username
print()
while True:
userpass = getpass.getpass('Password: ')
userpass2 = getpass.getpass('Confirm password: ')
if userpass == userpass2:
break
print('Password mismatch.\n')
uid = 2000
gid = 2000
if member:
mail = '%[email protected]' % username
else:
mail = input('\nMail address: ')
while True:
if not c.search(search_base=util.BASE, search_filter='(&(objectClass=posixAccount)(uidNumber=%i))' % uid):
break
uid = uid+1
attributes = {
'uid': username,
'cn': '%s %s' % (givenName, surname),
'givenName': givenName,
'sn': surname,
'loginShell': '/bin/bash',
'homeDirectory': homedir,
'uidNumber': '%i' % uid,
'gidNumber': '%i' % gid,
'mail': mail,
'shadowMin': '0',
'shadowMax': '99999',
'shadowWarning': '7',
}
if not c.add(userdn, ['top', 'person', 'organizationalPerson', 'inetOrgPerson', 'posixAccount', 'shadowAccount'], attributes=attributes):
print('Unable to add user.')
sys.exit(1)
if not util.change_password(c, userdn, userpass):
print('Unable to set password.')
sys.exit(1)
if member:
if not util.add_group(c, username, 'members'):
print('Unable to add \'members\' group.')
sys.exit(1)