aboutsummaryrefslogtreecommitdiffstats
path: root/init-sh/grpuser
blob: fb2ad25fd29afac8b9c8372647408f4c9fd63c7e (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
#!/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 "	--add [ groupname ] [ username ] ---> Add an user to a group."
	echo "	--del [ groupname ] [ username ] ---> Delete an user from a group."
}

ModifyFile() {
    mv /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}
	
    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`
}

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 1
    fi
    
    return 0
}

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 0
    return 1
}

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

	return 1;
}

Add() {
    IsGroupExisting;
    if [[ $? == 0 ]]; then
	echo "Sorry, group \"${group_name}\" does not exist."
	echo "Please create it using the \"groupadd\" command."
	exit 1
    fi

	IsUserExisting;
    if [[ $? == 1 ]]; then
    echo "Sorry, user \"${user_name}\" does not exist."
    exit 1
    fi

    IsUserAlreadyInGroup;
    if [[ $? == 1 ]]; then
	echo "Sorry, user \"${user_name}\" is already in group \"${group_name}\"."
	exit 1
    fi

    AppendUserToGroup;
    ModifyFile;

    exit 0
}

Del() {
    IsGroupExisting;
    if [[ $? == 0 ]]; then
	echo "Sorry, group \"${group_name}\" does not exist."
	exit 1
    fi

    IsUserAlreadyInGroup;
    if [[ $? == 0 ]]; then
	echo "Sorry, user \"${user_name}\" is not in group \"${group_name}\"."
	exit 1
    fi

    RemoveUserFromGroup;
    ModifyFile;
    
    exit 0
}

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

if [[ $# == 3 ]]; then
	case $1 in
		"--add")
			Perm;
			Add;
		        exit 0
			;;
		"--del")
			Perm;
			Del;
			exit 0
			;;
		esac
			Usage;
			exit 0
else
    Usage;
fi