This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useradd.rb
executable file
·168 lines (125 loc) · 3.59 KB
/
useradd.rb
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
#!/usr/bin/ruby
require 'active_ldap'
require 'optparse'
require 'config/user'
require 'config/group'
require 'config/connect'
##
# Attr
attr = Hash.new
#attr["uid"] = false
#attr["gid_number"] = false # -g, --gid
#attr["uid_number"] = false # -u, --uid
#attr["groups"] = false # -G, --groups
#attr["home_directory"] = false # -d, --home
#attr["login_shell"] = false # -s, --shell
#attr["given_name"] = false # -f, --first
#attr["sn"] = false # -l, --last
#attr["cn"] = false # automatically created
modify = false # -m, --modify
##
# OptionParser Object
opts = OptionParser.new do |opts|
opts.banner = "Usage: useradd.rb [attr] username"
opts.separator ""
opts.separator "Specific attr:"
opts.on("-g", "--gid GID",
"GID Number, defaults to 100") do |gid|
attr["gid_number"] = gid
end
opts.on("-u", "--uid UID",
"UID Number, defaults to highest existing uid+1") do |uid|
attr["uid_number"] = uid
end
opts.on("-G", "--groups GROUP1,GROUP2,...",
"Add user to GROUP1, GROUP2, ...") do |groups|
attr["groups"] = groups.split(',')
end
opts.on("-d", "--home HOME",
"Set home folder, defaults to /home/NAME") do |home|
attr["home_directory"] = home
end
opts.on("-s", "--shell SHELL",
"Set shell of user, defaults to /bin/bash") do |shell|
attr["login_shell"] = shell
end
opts.on("-f", "--first FIRST",
"Set first name") do |first|
attr["given_name"] = first
end
opts.on("-l", "--last SURNAME",
"Set surname") do |last|
attr["sn"] = last
end
opts.on("-m", "--modify",
"Modify existing user") do |mod|
modify = true
end
opts.separator ""
opts.separator "Common attr:"
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
opts.parse!(ARGV)
attr["uid"] = ARGV[0] || raise( "Username missing." )
##
# Checks for name and uid
# create default home if not given
if modify
User.exists?(attr["uid"]) || raise("User #{attr['uid']} does not exist.")
user = User.find(attr["uid"])
else
##
# Default values following here
if User.exists?(attr["uid"])
$stderr.puts("User #{attr['uid']} already exists.")
exit 1
end
attr["gid_number"] = 100 unless attr["gid_number"]
# Get highest existing uid + 1, start at 999 (+1)
if !attr["uid_number"]
attr["uid_number"] = 999
User.find(:all, :attribute => 'uidNumber').collect { |user| user.uid_Number>attr["uid_number"] ? attr["uid_number"]=user.uid_Number : false }
attr["uid_number"]+=1
end
attr["home_directory"] = "/home/#{attr['uid']}" unless attr["home_directory"]
attr["login_shell"] = "/bin/bash" unless attr["login_shell"]
user = User.new(attr["uid"])
end
##
# Things that are always needed
# Add failure notice here if group can not be found
if attr["groups"]
tmp = Array.new
attr["groups"].each { |grp| tmp << Group.find(grp) }
attr["groups"] = tmp
end
# Very unRubyish. Make this a lot prettyer for release!
if attr["given_name"] || attr["sn"]
attr["given_name"] = user.given_name unless attr["given_name"]
attr["sn"] = user.sn unless attr["sn"]
( attr["given_name"] && attr["sn"] ) ? delimiter = " " : delimiter = ""
attr["cn"] = attr["given_name"] + delimiter + attr["sn"]
end
##
# Create user
def mod_user(user, attributes)
# I am sure this could be done better!
attributes.each do |key, val|
if key != "groups"
user[key] = val
else
user.groups = val
end
end
return user
end
mod_user(user,attr)
#puts user.to_ldif
unless user.save
puts "User creation failed"
puts user.errors.full_messages
exit 1
end