-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportusr.py
More file actions
137 lines (126 loc) · 4.62 KB
/
importusr.py
File metadata and controls
137 lines (126 loc) · 4.62 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
# Import users on linux from exported files
import grp
import os
import subprocess
import sys
import shutil
from datetime import datetime
# Must be run as root or sudo.
if not os.geteuid() == 0:
sys.exit('This script must be run as root (or sudo)!')
# Backup existing files
print 'Backing up existing files... ',
try:
backup_date = datetime.now().strftime('%Y%m%d%H%M%S')
str_passwd = '/etc/passwd'
str_group = '/etc/group'
str_shadow = '/etc/shadow'
shutil.copy2(str_passwd, str_passwd + '.' + backup_date + '.bak' )
shutil.copy2(str_group, str_group + '.' + backup_date + '.bak' )
shutil.copy2(str_shadow, str_shadow + '.' + backup_date + '.bak' )
print '[ OK ]'
except Exception, e:
print '[ ERROR ]'
sys.exit(1)
# Check if the user already exists in the /etc/passwd file
def pwd_check(user):
if user in pwd_slst:
flag = 1
return flag
else:
flag = 0
return flag
# Replace the blank password with the hashed password from the source server
def upd_pwd(user, shdpw):
replace_str = user + ':!'
new_str = user + ':' + shdpw
# Read the shadow password fie
with open('/etc/shadow', 'r') as fshd_origup:
fshdfile = fshd_origup.read()
# Replace the old blank password
fshdfile = fshdfile.replace(replace_str, new_str)
# Write the shadow file back out
with open('/etc/shadow', 'w') as fshd_origup:
fshd_origup.write(fshdfile)
# Load existing passwd file into memory
spwdfile_lst = []
spwd_slst = []
with open('/etc/passwd', 'r') as fpwd_orig:
for line in fpwd_orig:
line = line.strip()
spwdfile_lst.append(line)
for line in spwdfile_lst:
spwd_lst = line.split(':')
spwd_slst.append(spwd_lst)
# Check for and create groups with more than one member.
# This has to be done ahead of time of the useradd will fail.
igrpfile_lst = []
with open('group_mig.txt', 'r') as fgrp_imp:
for line in fgrp_imp:
line = line.strip()
line = line.split(':')
igrpfile_lst.append(line)
for group in igrpfile_lst:
if group[3]:
group_name = group[0]
group_memcount = group[3]
group_memcount = str(len(group_memcount.split(',')))
print '[ ' + group_name + ' ]' + ' has ' + group_memcount + ' members.'
# Read local server's existing groups
try:
gr_data = grp.getgrnam(group[0])
except KeyError:
print "[ {} ] Group does not exist. Creating...".format(group[0])
command = ['groupadd', str(group[0])]
process = subprocess.Popen(command, stdout=subprocess.PIPE)
output, error = process.communicate()
# Open exported passwd file and read the users
with open('passwd_mig.txt', 'r') as fpwd:
for cnt, line in enumerate(fpwd):
pwd_lst = line.split(':')
# Open the exported shadow file - Need the hashed password
with open('shadow_mig.txt', 'r') as fshd:
flag = 0
index = 0
for sline in fshd:
index += 1
if str(pwd_lst[0]) in sline:
flag = 1
break
if flag == 0:
print "[ ERROR ]: Password for user {}, not found.".format(pwd_lst[0])
exit(1)
else:
shd_lst = sline.split(':')
# print "Password for user {}, is: {}.".format(pwd_lst[0], shd_lst[1])
# Open the exported group file - Need the users groups
with open('group_mig.txt', 'r') as fgrp:
flag = 0
index = 0
for gline in fgrp:
index += 1
grp_lst = gline.split(':')
if str(pwd_lst[0]) in grp_lst[0]:
flag = 1
break
if flag == 0:
print "[ ERROR ]: Group for user {}, not found.".format(pwd_lst[0])
exit(1)
else:
shd_lst = sline.split(':')
# print "Group for user {}, is: {}.".format(pwd_lst[0], grp_lst[2])
# Make sure user doesn't already exist
if not pwd_lst[0] in spwd_slst:
print "Creating user... {}".format(pwd_lst[0])
command = ['useradd',
'-d', pwd_lst[5],
'-u', pwd_lst[2],
'-s', pwd_lst[6],
'-G', pwd_lst[7].rstrip(),
'-m',
pwd_lst[0]]
process = subprocess.Popen(command, stdout=subprocess.PIPE)
output, error = process.communicate()
upd_pwd(pwd_lst[0], shd_lst[1])
else:
print "User exists... Skipping."