This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontaineradm
executable file
·323 lines (271 loc) · 6.89 KB
/
containeradm
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/bash
# Under Apache 2.0 License see LICENSE file.
#
# Copyright IBM 2021,2022
# SPDX-License-Identifier: Apache2.0
#
# Authors:
# - Thomas Weinzettl <thomasw@ae.ibm.com>
#
#===============================================================================
# We need to set DEBUG to 0 if it is not set in order to get the debug level
# right.
[ -z $DEBUG ] && DEBUG=0 || [ $DEBUG -ge 1 ] && echo "Container: Debug level=${DEBUG}"
# This shell script is helps to administer the container users as well as the
# ssh keys of the user and the host.
#
source /bin/ssh-functions.sh
######
# This function will print all possible users with their uid:gid settings
# the container is aware of in `id` format.
#
list_users() {
users=`ls /home/`
for user in $users ; do
if [ -d /home/${user} ]; then
# the `id` command helps us to print out the groups and ids
id ${user}
fi
done
}
######
# This function adds a ssh-key to a users authorized_keys file. It is split in two
# for security reasons. The acual add is executed with as the user whos key is to
# be added.
#
# We only parse the parameters, the format is "username:sshkey"
#
add_key(){
IFS=":" read -r user key <<< "$@"
[ $DEBUG -ge 1 ] && echo "Container: DEBUG > add_key for user=${user} with key=${key:0:24}..."
#
# We call here `sudo` as the user to add the key, to avoid any permission bit, or
# ownership issues.
sudo -E -u $user ssh-key.sh add $key
}
######
# This function lists the keys installed for one user
# $1 .. is the user name.
#
list_keys(){
user=$1
echo "Container: list keys for ${user}"
#
# To avoid permission or ownership issues we call the list script as the user
sudo -E -u ${user} ssh-key.sh list
}
######
# This functions adds a new user to the container. It requires a username as parameter.
# Optionally uid and gid can be specified.
#
add_user() {
IFS=":" read -r user uid gid <<<$@
[ -z "$uid" ] && uid_cmd="" || uid_cmd="-u ${uid}"
if [ -z "$gid" ]; then
gid_cmd=""
else
gid_cmd="-g ${gid}"
groupadd -g $gid $user
fi
# -m needed to ensure we have the home-dir
useradd -m $uid_cmd $gid_cmd $user
echo "User $user was added."
}
######
# This functions adds a new group to the data. It requires a group name as
# parameter. Optionally a gid can be specified.
#
add_group() {
IFS=":" read -r group gid <<<$@
[ -z "$gid" ] && gid_cmd="" || gid_cmd="-g ${gid}"
groupadd $gid_cmd $group
echo "Group $group was added."
}
######
# remove a user from a group
#
remove_from_group() {
IFS=" " read -r user group force <<< "$@"
[ $DEBUG -ge 1 ] && echo "Container: DEBUG > add_to_group user=${user},group=${group}"
[ "$user" == "$group" ] && {
echo "Error: cannot remove primary group"
return
}
if [ -f /home/${user}/.groups.d/${group} ]; then
rm /home/${user}/.groups.d/${group}
gpasswd -d $user $group
fi
}
######
# This function checks if a group exists
#
group_is_existing(){
IFS=" " read -r group trash <<<$@
result=0
[ -z "$group" ] || {
getent group $group >/dev/null && result=1
}
return $result
}
######
# This function adds a user to a group. It stores the group associations in
# $HOME/.groups.d/${groupname} by owning it by the user.
#
add_to_group() {
IFS=" " read -r user group <<< "$@"
[ $DEBUG -ge 1 ] && echo "Container: DEBUG > add_to_group user=${user},group=${group}"
# We need to have an .groups.d directoy inside $HOME
if [ ! -d /home/${user}/.groups.d ]; then
sudo -u $user mkdir -p /home/${user}/.groups.d
sudo -u $user chmod 700 /home/${user}/.groups.d
fi
group_is_existing $group
isexisting=$?
[ $isexisting == 1 ] && {
gpasswd -a ${user} ${group}
sudo -u ${user} -g ${group} touch /home/${user}/.groups.d/${group}
sudo -u ${user} -g ${group} chmod 400 /home/${user}/.groups.d/${group}
} || {
echo "Group $group does not exist."
}
}
######
# This function removes users
#
del_user(){
IFS=" " read -r user <<<$@
userdel ${user}
if [ -d /home/$user ]; then
rm -Rf /home/${user}
fi
echo "User $user was deleted."
}
######
# This function regenerates the hostkeys.
#
hostkey_refresh(){
rm -r /etc/ssh/ssh_host_*_key /etc/ssh/ssh_host_*_key.pub
ssh-keygen -A
store
}
#####
# Print usage info
#
usage(){
echo "containeradm ... usage"
echo ""
echo "# containeradm [object] [command] [parameter]"
echo ""
echo "Objects:"
echo " - user ....... to work with users."
echo " - group ...... to work with groups."
echo " - key ........ to work with public ssh keys."
echo " - hostkey .... to work with host keys."
echo " - showconfig . to show the current sshd_config"
}
#####
# Print Usage info for user subcommand
usage_user(){
echo ""
echo "Commands:"
echo " - list ..... to list users"
echo " no parameters"
echo " - add ...... to add a user"
echo " parameters: USERNAME[:[UID]:[GID]])"
echo " - del ...... to delete a user"
echo " parameters: USERNAME"
echo " - addgrp ... add to group"
echo " parameters: USERNAME GROUPNAME"
echo " - rmgrp .... remove from a group"
echo " parameters: USERNAME GROUPNAME"
}
######
# Print usage for key subcommand
#
usage_key(){
echo ""
echo "Commands:"
echo " - list ..... to list a users key"
echo " parameters: USERNAME"
echo " - add ...... to add a key to a user"
echo " parameters: USERNAME:PUBLIC-SSH-KEY"
}
######
# Print usage for hostkey subcommand
usage_hostkey() {
echo ""
echo "Commands:"
echo " - refresh .. generates new host keys"
}
######
# Print usage for adding grouops
usage_group() {
echo ""
echo "Commands:"
echo " - add .... GROUPNAME[:GID] to add a group"
}
################################################################################
# main
# Lets get the arguments and load them into our variables
read -r object command parameters <<<$@
#TODO: switch to case (replacing if)
case "$object" in
"user")
case "$command" in
"list")
list_users
;;
"add")
add_user ${parameters}
;;
"del")
del_user ${parameters}
;;
"addgrp")
add_to_group ${parameters}
;;
"rmgrp")
remove_from_group ${parameters}
;;
*|"help")
usage
usage_user
;;
esac
;;
"group")
case "$command" in
"add")
add_group ${parameters}
;;
*|"help")
usage
usage_group
;;
esac
;;
"key")
if [[ -z "$command" || "$command" == "help" ]]; then
usage
usage_key
elif [ "$command" == "list" ]; then
list_keys $parameters
elif [ "$command" == "add" ]; then
add_key $parameters
fi
;;
"hostkey")
if [[ -z "$command" || "$command" == "help" ]]; then
usage
usage_hostkey
elif [ "$command" == "refresh" ]; then
hostkey_refresh
fi
;;
"showconfig")
cat /etc/sshd/config
;;
*|"help")
usage
;;
esac