-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpm-user_group.sh
executable file
·145 lines (122 loc) · 2.45 KB
/
rpm-user_group.sh
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
#!/bin/sh
[ -f /etc/sysconfig/rpm ] && . /etc/sysconfig/rpm
[ -z "$RPM_SCRIPTVERBOSITY" ] && RPM_SCRIPTVERBOSITY=5
# aborts program abnormally
die() {
local rc=${2:-1}
if [ "$1" ]; then
echo >&2 "ERROR: $1"
else
echo >&2 "ERROR"
fi
exit $rc
}
bannercmd() {
if [ "$BANNERCMD" = cat ]; then
echo cat
else
if [ "$RPM_SCRIPTVERBOSITY" -lt 2 ]; then
echo "$BANNERCMD -M $1"
else
echo "$BANNERCMD -s -M $1"
fi
fi
}
testrm() {
local mode=$1
local name=$2
[ "$RPM_USERDEL" != yes ] || [ ! -x /bin/rpm ] && return 1
[ -z "$name" ] && return 2
rpm -q --whatprovides "$mode($name)" >/dev/null 2>&1
# no package Provides it (strange)
[ $? -ne 0 ] && return 0
# only current package Provides it
[ $(rpm -q --whatprovides "$mode($name)" | wc -l) -lt 2 ] && return 0
return 1
}
groupremove() {
local name="$1"
local gid=$(getgid "$name" 2>/dev/null)
echo "Removing group $name" | $(bannercmd "groupdel-$name")
/usr/sbin/groupdel $name || :
# flush nscd cache
[ ! -x /usr/sbin/nscd ] || /usr/sbin/nscd -i group
}
userremove() {
local uid=$(id -un "$name" 2>/dev/null)
echo "Removing user $name" | $(bannercmd "userdel-$name")
/usr/sbin/userdel $name || :
# flush nscd cache
[ ! -x /usr/sbin/nscd ] || /usr/sbin/nscd -i passwd
}
remove() {
local mode=$1
local name=$2
if ! testrm $mode $name; then
return
fi
${mode}remove $name
}
addtogroup() {
local user="$1"
local group="$2"
local uid=$(id -un "$user" 2>/dev/null)
local gid=$(getgid "$group" 2>/dev/null)
if [ -z "$gid" ]; then
if [ "$quiet" ]; then
return
else
die "group $group does not exist"
fi
fi
if [ -z "$uid" ]; then
if [ "$quiet" ]; then
return
else
die "user $user does not exist"
fi
fi
groups=$(id -n -G $user)
if [[ " $groups " != *\ $group\ * ]]; then
echo "Adding user $user to group $group" | $(bannercmd "${MODE}mod-$user")
for grp in $groups $group; do
new="$new${new:+,}$grp"
done
usermod -G "$new" $user
fi
}
if [ -x /usr/bin/banner.sh ]; then
BANNERCMD="/usr/bin/banner.sh "
BANNERPARA="-s -M user-group.error"
else
BANNERCMD="cat"
BANNERPARA=""
fi
if [ "$1" = user -o "$1" = group ]; then
MODE=$1
shift
else
die "Invalid usage"
fi
# quiet mode cames from $ENV
quiet=$quiet
case "$1" in
testrm)
testrm $MODE $2
exit $?
;;
del)
remove $MODE $2
exit $?
;;
addtogroup)
if [ "$MODE" = "user" ]; then
if [ -z "$2" -o -z "$3" ]; then
die "Invalid usage"
fi
addtogroup $2 $3
fi
;;
*)
die "Invalid usage" 2
esac