aboutsummaryrefslogtreecommitdiffstats
path: root/init-sh/grpuser.sh
blob: d2f2dafe9640c63c143e8578238359442cb6a552 (plain)
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
#!/bin/sh

#
# Writen by Vandoorselaere Yoann <yoann@mandrakesoft.com>
# Thanks to Francis Galiegue.
#

file="group"
group_line=""
new_group_line=""
group_name=$2
user_name=$3

Usage() {
	echo "Usage :"
	echo "  --clean      ---> Remove all group change."
	echo "  --refresh    ---> Read group name in /etc/security/msec/group.conf"
	echo "                    and add each user in /etc/security/msec/user.conf"  
	echo "                    in these groups ( if security level is <= 2 )" 
}

ModifyFile() {
    cp /etc/${file} /tmp/${file}.old

    head -$((group_line_number - 1)) /tmp/${file}.old > /etc/${file}
    echo "${new_group_line}" >> /etc/${file}
    tail +$((group_line_number + 1)) /tmp/${file}.old >> /etc/${file}
	
    new_group_line=""

    rm -f /tmp/${file}.old
}

RemoveUserFromGroup() {
    new_group_line=${group}`echo ${group_users} | 
	sed -e s/,${user_name}$//g -e s/${user_name},//g -e s/${user_name}$//g`
	
    echo ${new_group_line}
}

AppendUserToGroup() {
    if [[ -z ${group_users} ]]; then
	new_group_line=${group_line}${user_name}
    else
	new_group_line=${group_line}",${user_name}"
    fi
}

IsUserAlreadyInGroup() {
    if echo ${group_users} | grep -qw "${user_name}"; then
	return 0
    fi
    
    return 1
}

IsGroupExisting() {
    group_line=""
    group_line_number=""
    
    # We get some group infos as well, will be used later
    tmp=`grep -n "^${group_name}:" /etc/${file} | tr -d " "`
    
    group_line_number=`echo ${tmp} | awk -F: '{print $1}'`
    group=`echo ${tmp} | awk -F: '{print $2":"$3":"$4":"}'`
    group_users=`echo ${tmp} | awk -F: '{print $5}'`
    group_line=`echo ${tmp} | awk -F: '{print $2":"$3":"$4":"$5}'`

    [ -z "${tmp}" ] && return 1
    
    return 0
}

IsUserExisting() {
	grep -qn "^${user_name}:" /etc/passwd
	if [[ $? == 0 ]]; then
		return 0;
	fi

	return 1;
}

RefreshAdd() {
    if [[ ${SECURE_LEVEL} > 2 ]]; then
	echo "You are in a secure level > 2, in this level you need to add group user by yourself."
	echo "Use the command : usermod -G group_name user_name"
	exit 1;
    fi

    cat /etc/security/msec/group.conf | while read group_name; do
	IsGroupExisting;
	if [[ $? != 0 ]]; then
	    echo "Group \"${group_name}\" doesn't exist. skiping it."
	else
	    cat /etc/security/msec/user.conf | while read user_name; do
		IsUserExisting; 
		if [[ $? != 0 ]]; then
		    # user doesn't exist
		    echo "Can't add user \"${user_name}\" to group \"${group_name}\" user doesn't exist. skiping."
		    IsUserAlreadyInGroup;
		    if [[ $? == 0 ]]; then
			echo "User doesn't exist but is in a group... delete user from this group."
			RemoveUserFromGroup;
			ModifyFile;
		    fi
		else
		    echo "Adding user \"${user_name}\" to group \"${group_name}\"."
		    #AppendUserToGroup;
		    #ModifyFile;
		    usermod -G ${group_name} ${user_name}
		fi
	    done
	fi
    done
}

RefreshDel() {
    cat /etc/security/msec/group.conf | while read group_name; do
	IsGroupExisting;
	if [[ $? != 0 ]]; then
	    echo "Group \"${group_name}\" doesn't exist. skiping it."
        else
	    cat /etc/security/msec/user.conf | while read user_name; do
		IsGroupExisting; # We need some variable at each turn.
		IsUserAlreadyInGroup;
		if [[ $? == 0 ]]; then
		    echo "Removing \"${user_name}\" from group \"${group_name}\"."
		    RemoveUserFromGroup;
		    ModifyFile;
		fi
	    done
	fi
    done
}



Perm() {
	if [[ ${UID} != 0 ]]; then
	    echo "You need root access to use this tool."
	    echo "And this script shouldn't be used by users."
	    exit 1
	fi

	if [[ ! -w /etc/${file} ]]; then
	    echo "You're not allowed to write to /etc/group..." 
	    exit 1
	fi

	if [[ ! -f /etc/security/msec/group.conf ]]; then
	    echo "/etc/security/msec/group.conf doesn't exist..."
	    exit 1
	fi
       
	if [[ ! -f /etc/security/msec/user.conf ]]; then
	    echo "/etc/security/msec/user.conf doesn't exist..."
	    exit 1
	fi
}

if [[ $# == 1 ]]; then
	case $1 in
		"--refresh")
			Perm;
			RefreshAdd;
			exit 0
			;;
		"--clean")
			Perm;
			RefreshDel;
			exit 0
			;;
		esac
			Usage;
			exit 0
else
    Usage;
fi