-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathaddauser.py
executable file
·234 lines (204 loc) · 6.76 KB
/
addauser.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
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
232
233
234
#!/usr/bin/env python
# Copyright 2018 Jose Delarosa
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Script to manually add users
import sys, os, subprocess, pwd, grp, getpass, time, shutil, re
from time import strftime
# colors
red="\033[91m"
green="\033[92m"
bold="\033[1m"
end="\033[0m"
ll = 80 # line length to use
# Users
min_uid = 1000
min_gid = 1000
pasfile = "/etc/passwd"
shafile = "/etc/shadow"
grpfile = "/etc/group"
def die(msg):
print >>sys.stderr, msg
os._exit(1)
def checkuser(name):
try: return pwd.getpwnam(name)
except KeyError: return None
def checkgroup(group):
try: return grp.getgrnam(group)
except KeyError: return None
def getfirst():
fn = ""
try:
while len(fn) == 0: fn = raw_input('First Name: ')
except KeyboardInterrupt: die("Interrupt detected, exiting.")
return fn
def getlast():
ln = ""
try:
while len(ln) == 0: ln = raw_input('Last Name: ')
except KeyboardInterrupt: die("Interrupt detected, exiting.")
return ln
def getusername(fn, ln):
user = ""
# Build username from first letter in 'fn' and up to 7 letters in 'ln'
# Is this the best way to do it?
fn1 = fn[0:1].lower() # get first character
ln = ln.replace(" ", "") # remove empty spaces, if any
ln7 = ln[0:7].lower() # get first 7 characters
user_rec = fn1 + ln7
try:
while len(user) == 0:
user = raw_input('Username [' + green + '%s' % user_rec + end + ']: ')
if len(user) == 0: user = user_rec
if checkuser(user):
print red + "Username %s already exists!" % user + end
user = ""
except KeyboardInterrupt: die("Interrupt detected, exiting.")
return user
def getgroup(user):
group = ""
group_rec = user
try:
while len(group) == 0:
group = raw_input('Group [' + green + '%s' % group_rec + end + ']: ')
if len(group) == 0: group = group_rec
if checkgroup(group):
print red + "Group %s already exists!" % group + end
group = ""
except KeyboardInterrupt: die("Interrupt detected, exiting.")
return group
def gethomedir(user):
homedir = ""
homedir_rec = "/home/" + user
try:
while len(homedir) == 0:
homedir = raw_input('Home Dir [' + green+ '%s' % homedir_rec+end+']: ')
if len(homedir) == 0: homedir = homedir_rec
if os.path.exists(homedir):
print red + "Directory %s already exists!" % homedir + end
homedir = ""
except KeyboardInterrupt: die("Interrupt detected, exiting.")
return homedir
def getpasswd():
passwd = ""
passwdvfy = ""
try:
while True:
while len(passwd) == 0: passwd = getpass.getpass()
while len(passwdvfy) == 0:
passwdvfy = getpass.getpass(prompt='Verify Password: ')
if not passwd == passwdvfy:
print red + "Password does not match!" + end
passwd = ""
passwdvfy = ""
else: break
except KeyboardInterrupt: die("Interrupt detected, exiting.")
return passwd
def getuid(user):
myuid = min_uid
uid_list = []
fh = open(pasfile, 'r') # Read file
ll = fh.readlines()
fh.close()
# read all lines in file, extract and sort
for line in ll:
uid = line.split(':')[2] # Same as cut -d":" -f3
uid_list.append(int(uid)) # Create array with uids (convert to int first)
# Look for next available UID
for uid in sorted(uid_list):
if uid >= min_uid:
if uid == myuid: myuid = myuid + 1
else: break
return myuid
def getgid(group):
mygid = min_gid
gid_list = []
fh = open(grpfile, 'r') # Read file
ll = fh.readlines()
fh.close()
# read all lines in file, extract and sort
for line in ll:
gid = line.split(':')[2] # Same as cut -d":" -f3
gid_list.append(int(gid)) # Create array with gids (convert to int first)
# Look for next available GID
for gid in sorted(gid_list):
if gid >= min_gid:
if gid == mygid: mygid = mygid + 1
else: break
return mygid
def creategroup(group):
# Get next available gid
gid = getgid(group)
s = "groupadd -g %s %s 1> /dev/null 2>&1" % (gid, group)
result = subprocess.call(s, shell=True)
# Failure?
if not result == 0: print red + "Error in creating group %s" % group + end
else: print green + "Created group %s" % group + end
return result
def createuser(fn, ln, user, group, homedir):
# Create backups
ts = strftime("%Y.%m.%d %H:%M:%S")
shutil.copy2(pasfile, pasfile + "." + ts)
shutil.copy2(shafile, shafile + "." + ts)
uid = getuid(user)
s = ' '.join(["useradd -c \"%s %s\" -m -d %s " % (fn, ln, homedir),
"-u %s -g %s %s 1> /dev/null 2>&1" % (uid, group, user)])
result = subprocess.call(s, shell=True)
# Failure?
if not result == 0:
print red + "Error in creating user %s" % user + end
os.remove(pasfile + "." + ts)
os.remove(shafile + "." + ts)
else: print green + "Created user %s" % user + end
return result
def setuserpass(user, passwd):
# Set user password
s = "echo %s | passwd --stdin %s 1> /dev/null 2>&1" % (passwd, user)
result = subprocess.call(s, shell=True)
if not result == 0: print red + "Error in setting password" + end
else: print green + "Set password" + end
return result
def main():
# are we root
if not os.geteuid() == 0:
print "You must be root to run this script."
sys.exit(1)
print "Enter user information:\n"
fn = getfirst()
ln = getlast()
user = getusername(fn, ln)
passwd = getpasswd()
group = getgroup(user)
homedir = gethomedir(user)
print " First name : %s" % fn
print " Last name : %s" % ln
print " User : %s" % user
print " Password : ********"
print " Group : %s" % group
print " Home Dir : %s" % homedir
print " c) continue"
print " q) quit"
while True:
choice = raw_input('Select [c] : ')
if choice == "q":
print "Nothing done"
sys.exit(0)
else: break
if not creategroup(group) == 0: sys.exit(1)
if not createuser(fn, ln, user, group, homedir) == 0:
subprocess.call('groupdel %s 1> /dev/null 2>&1' % group, shell=True)
sys.exit(1) # remove group created
setuserpass(user, passwd)
if __name__ == "__main__":
main()
# 2015.09.22 08:25:29 - JD