-
Notifications
You must be signed in to change notification settings - Fork 1
/
01_group.sh
executable file
·202 lines (174 loc) · 5.79 KB
/
01_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
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
#!/usr/bin/env bash
# ----------------------------------------------------------- #
# Copyright (C) 2008 Red Hat, Inc. #
# Written by Michel Samia <[email protected]> #
# Adapted for SCE by Martin Preisler <[email protected]> #
# #
# group.sh #
# more info in group.dsc #
# ----------------------------------------------------------- #
#constants
passwd=/etc/passwd
group=/etc/group
shadow=/etc/shadow
group_shadow=/etc/gshadow
if [[ $UID -ne '0' ]]
then
echo "You have to be logged as root to run this test!"
exit ${XCCDF_RESULT_ERROR}
fi
RET=$XCCDF_RESULT_PASS
# gets a value of a constant defined in a c/c++ header file by #define
# usage example:
# getValueFromH '/usr/include/bits/utmp.h' 'UT_NAMESIZE'
# echo $ReturnVal
function getValueFromH {
if ! [[ -r "$1" ]]; then
report 'WARNING' 1234 "Can't read a constant $2, header file $1 not found"
return 0
else
line="$(egrep "^#define $2..*" $1)"
if [[ -n "$line" ]]; then
local -i retval=$(echo "$line" | cut -f2)
return $retval
else
report 'WARNING' 1234 "Can't read a constant $2 from file $1, definition of the constant not found in this file"
return 0
fi
fi
}
# function isValidName
# tests a string whether it is a valid group/user name
# 1 - true
# 0 - false
function isValidName {
# first we need to set LC_ALL to C to get ranges working case-sensitively
oldLC_ALL=${LC_ALL}
LC_ALL="C"
# this constant contains a regex which recognizes, if the string is valid name of user or group
allowedNamesRegex='^[a-zA-Z0-9_.][a-zA-Z0-9_.-]*[a-zA-Z0-9_.$-]?$'
echo $1 | egrep -q "$allowedNamesRegex"
returnValue=$[ 1 - $? ]
LC_ALL=${oldLC_ALL}
return $returnValue
}
UID_MIN_VALUE="`egrep '^UID_MIN' /etc/login.defs | awk '{ print $2 }'`"
UID_MAX_VALUE="`egrep '^UID_MAX' /etc/login.defs | awk '{ print $2 }'`"
GID_MIN_VALUE="`egrep '^GID_MIN' /etc/login.defs | awk '{ print $2 }'`"
GID_MAX_VALUE="`egrep '^GID_MAX' /etc/login.defs | awk '{ print $2 }'`"
i=0
while read line
do
i=$[i+1]
##### empty line #####
if [[ "$line" == "" ]]
then
echo "$shadow: Line $i is empty"
echo "Please delete this line."
[ "$RET" == $XCCDF_RESULT_FAIL ] || RET=$XCCDF_RESULT_INFORMATIONAL
continue
fi
##### number of fields #####
nf=`echo "$line" | awk -F: '{printf NF}'`
if [ "$nf" -ne "4" ]
then
echo $line
echo "$shadow: Line $i has wrong number of fields"
echo "Please see 'man group' and correct this line."
RET=$XCCDF_RESULT_FAIL
continue
fi
# now we can parse these fields, we know that all fields exist
groupname="`echo $line | awk -F: '{print $1}'`"
pass="`echo $line | awk -F: '{print $2}'`"
gid="`echo $line | awk -F: '{print $3}'`"
users="`echo $line | awk -F: '{print $4}'`"
##### line has an empty groupname field #####
if [[ "$groupname" == "" ]]
then
echo "$group: Line $i: missing groupname!"
echo "Check this line, fill in first item (groupname), or delete whole line."
RET=$XCCDF_RESULT_FAIL
fi
##### disallowed characters #####
isValidName $groupname
if [ $? -ne 1 ]
then
echo "$shadow: Line $i: Group name $groupname contains disallowed characters."
echo "Check this line and rename group name to contain lowercase letters only. Don't forget to update the $group_shadow file!"
RET=$XCCDF_RESULT_FAIL
fi
##### too long groupname #####
getValueFromH '/usr/include/bits/utmp.h' 'UT_NAMESIZE'
MaxLength=$?
length=`echo "$groupname" | wc -m`
if [ $length -gt $MaxLength ]
then
echo "$group: Line $i: User $groupname has too long group name."
echo "Check this line in $group and rename group name to be shorter than $MaxLength characters"
RET=$XCCDF_RESULT_FAIL
fi
##### password not shadowed #####
if [[ "$pass" != "x" ]] && [[ "$pass" != "" ]]
then
echo "$group: Line $i: Group $groupname has a password but it is not shadowed!"
echo "Please use grpconv utility for moving group passwords from $group to $group_shadow"
RET=$XCCDF_RESULT_FAIL
fi
##### is GID a number? #####
if [[ "`echo $gid | grep -e '^-\?[0-9]\+$'`" != "$gid" ]]
then
echo "$group: Line $i: Group ID of the group $groupname is not a valid number"
echo "Please correct the group ID."
RET=$XCCDF_RESULT_FAIL
else
##### is UID and GID in the range? #####
if [[ "$groupname" != "nfsnobody" ]]
then
if (( $gid < 0 || $gid > $GID_MAX_VALUE )); then
echo "$group: Line $i: Group $groupname has GID out of range"
echo "Change GID of this group to be in the range <0, $GID_MAX_VALUE>"
RET=$XCCDF_RESULT_FAIL
fi
fi
fi
done<<EOF
`cat $group`
EOF
##### two groups with the same group name #####
while read groupname
do
if [[ "$groupname" != "" ]]
then
lines="`grep -n -e "^$groupname:" $group | awk -F: '{ print $1 }'| tr '\n' ','`"
lines="${lines%','}" # delete last coma
echo "Duplicate group name '$groupname' (lines $lines)"
echo "Please change groupnames on these lines to be different or delete duplicate records"
RET=$XCCDF_RESULT_FAIL
fi
done<<EOF
`awk -F: '{ if ($1 != "") print $1 }' $group | sort | uniq -d`
EOF
##### two groups with the same GID #####
while read groupID
do
if [[ "$groupID" != "" ]]
then
lines="`grep -n -e "^.*:.*:$groupID:.*$" $group | awk -F: '{ print $1 }'| tr '\n' ','`"
lines="${lines%','}" # delete last coma
echo "Duplicate group IDs '$groupID' (lines $lines)"
echo "Please change IDs on these lines to be different or delete duplicate records"
RET=$XCCDF_RESULT_FAIL
fi
done<<EOF
`awk -F: '{ if ($3 != "") print $3 }' $group | sort | uniq -d`
EOF
##### has root GID 0? #####
root_gid=`grep '^root:' $group |cut -d: -f3`
if [[ $root_gid != "0" ]]
then
echo "User root has GID $root_gid, but should have 0"
echo "Change root's GID to 0"
RET=$XCCDF_RESULT_FAIL
fi
exit $RET