minor fixups
[grimoire-planeshift.git] / libaccount
blob1c3261b29acefc095b1152389b09682e8f8ce565
1 #!/bin/bash
2 #---------------------------------------------------------------------
3 ##
4 ## @Synopsis Functions for dealing with account and group generation etc.
5 ## @Copyright Copyright (C) 2004 The Source Mage Team <http://www.sourcemage.org>
6 ## A library to help deal with managing accounts, particularly creating
7 ## new accounts.
8 ##
9 #---------------------------------------------------------------------
11 ACCOUNT_LIST=$GRIMOIRE/accounts
12 GROUP_LIST=$GRIMOIRE/groups
14 #---------------------------------------------------------------------
15 ## Tests account and group list if there are no duplicate ID's. In
16 ## case there are, exits since this is serious problem
17 #---------------------------------------------------------------------
18 function sanity_checks() {
19 local COUNT1=$( cut -d : -f2 "$ACCOUNT_LIST" | wc -l )
20 local COUNT2=$( cut -d : -f2 "$ACCOUNT_LIST" | sort -u | wc -l )
21 if [ $COUNT1 != $COUNT2 ]; then
22 message "${PROBLEM_COLOR}Fatal error, $ACCOUNT_LIST contains duplicate UID's${DEFAULT_COLOR}"
23 exit 1
26 COUNT1=$( cut -d : -f2 "$GROUP_LIST" | wc -l )
27 COUNT2=$( cut -d : -f2 "$GROUP_LIST" | sort -u | wc -l )
28 if [ $COUNT1 != $COUNT2 ]; then
29 message "${PROBLEM_COLOR}Fatal error, $GROUP_LIST contains duplicate GID's${DEFAULT_COLOR}"
30 exit 1
34 #---------------------------------------------------------------------
35 ## @param system account
36 ## @param [home directory] - default used when not defined or empty
37 ## @param [shell] - default used when not defined or empty
38 ## @param [secondary gids] - comma separated list with
39 ## no intervening whitespace
41 ## Creates account (if account has been defined).
42 ## @return 0 if success (or account already exists).
43 ## @return 1 if failed (or account has not been defined).
45 #---------------------------------------------------------------------
46 function create_account() {
48 # sanity_checks
50 local HOME_DIR=/var/run/$1
51 if [ -n "$2" ]; then
52 HOME_DIR="$2"
55 local USER_SHELL=/bin/false
56 if [ -n "$3" ]; then
57 USER_SHELL="$3"
60 local SECONDARY_GIDS=''
61 if [ -n "$4" ]; then
62 SECONDARY_GIDS="-G $4"
65 if ! exists_account "$1" ; then
66 debug "libgrimoire" "create_account() - $1 not defined!"
67 return 0 # should return 0 to gracefully continue casting.
70 local ACCOUNT_UID=`get_uid_for_account $1`
71 local PRIMARY_GID=`get_primary_gid_for_account $1`
72 local PRIMARY_GNAME=`get_group_name $PRIMARY_GID`
74 debug "libgrimoire" "create_account() - $1, UID=$ACCOUNT_UID, GID=$PRIMARY_GID:$PRIMARY_GNAME, HOME=$HOME_DIR, SHELL=$USER_SHELL, SECONDARY_GIDS=$4"
76 groupadd -g $PRIMARY_GID -f $PRIMARY_GNAME
78 # check for adding user problems and try to notify user.
79 useradd -u $ACCOUNT_UID -g $PRIMARY_GNAME $SECONDARY_GIDS -d "$HOME_DIR" -s "$USER_SHELL" $1
80 local USERADD_RETURN=$?
81 debug "libgrimoire.create_account()" "useradd return code was: $USERADD_RETURN"
83 if [ $USERADD_RETURN == 0 ] ; then
84 message "${MESSAGE_COLOR}The user id $ACCOUNT_UID created!${DEFAULT_COLOR}"
85 elif [ $USERADD_RETURN == 4 ] ; then
86 message "${MESSAGE_COLOR}The user id $ACCOUNT_UID already exists, so continuing...${DEFAULT_COLOR}"
87 return 0
88 elif [ $USERADD_RETURN == 9 ] ; then
89 message "${MESSAGE_COLOR}The user name $1 already exists, so continuing...${DEFAULT_COLOR}"
90 return 0
91 else
92 debug "libgrimoire.create_account()" "useradd fails with strange code : $USERADD_RETURN"
93 message "${MESSAGE_COLOR}Something went wrong with adding the user $1 with uid $ACCOUNT_UID"
94 message "and gid $PRIMARY_GNAME so going to stop here...${DEFAULT_COLOR}"
95 return 1
99 #---------------------------------------------------------------------
100 ## @param system group
102 ## Creates group (if group has been defined).
103 ## @return 0 if success (or group already exists).
104 ## @return 1 if failed (or group has not been defined).
106 #---------------------------------------------------------------------
107 function create_group() {
109 # sanity_checks
111 if ! exists_group "$1" ; then
112 debug "libgrimoire" "create_group() - $1 not defined!"
113 return 0 # should return 0 to gracefully continue casting.
116 local GROUP_GID=`get_gid_for_group $1`
118 debug "libgrimoire" "create_group() - $1, GID=$GROUP_UID "
120 groupadd -g $GROUP_GID $1 > /dev/null 2>&1
122 local GROUPADD_RETURN=$?
123 debug "libgrimoire.create_group()" "groupadd return code was: $GROUPADD_RETURN"
125 if [ $GROUPADD_RETURN == 0 ] ; then
126 message "${MESSAGE_COLOR}The group name $1 with id $GROUP_GID has been created!${DEFAULT_COLOR}"
127 elif [ $GROUPADD_RETURN == 4 ] ; then
128 message "${MESSAGE_COLOR}The group id $GROUP_GID already exists, stopping here...${DEFAULT_COLOR}"
129 return 1
130 elif [ $GROUPADD_RETURN == 9 ] ; then
131 message "${MESSAGE_COLOR}The group name $1 already exists, so continuing...${DEFAULT_COLOR}"
132 return 0
133 else
134 debug "libgrimoire.create_group()" "groupadd fails with strange code : $GROUPADD_RETURN"
135 message "${MESSAGE_COLOR}Something went wrong with adding the group $1 with gid $GROUP_GID"
136 message "so going to stop here...${DEFAULT_COLOR}"
137 return 1
142 #---------------------------------------------------------------------
143 ## @param gid
145 ## @Stdout group name
146 ## returns that name assigned to a group id.
148 #---------------------------------------------------------------------
149 function get_group_name() {
151 if grep -q ":$1:" $GROUP_LIST; then
152 grep ":$1:" $GROUP_LIST | cut -d : -f1
158 #---------------------------------------------------------------------
159 ## @param system account
161 ## @Stdout group ids
162 ## Return list of group id's assigned to account name.
163 ## All except primary
165 #---------------------------------------------------------------------
166 function get_gids_for_account() {
168 if grep -q "^$1:" $ACCOUNT_LIST; then
169 ENTRY=`grep "^1:" $ACCOUNT_LIST`
171 while [[ `echo $ENTRY | cut -d : -f$i` ]] ; do
172 NEW=`echo $ENTRY | cut -d : -f$i`
173 GROUPS="$GROUPS $NEW"
174 let i++
175 done
177 return $GROUPS
181 #---------------------------------------------------------------------
182 ## @param system account
183 ## @return 0 if exists
184 ## @return 1 if not
186 #---------------------------------------------------------------------
187 function exists_account() {
189 grep -q "^$1:" $ACCOUNT_LIST
194 #---------------------------------------------------------------------
195 ## @param named system account
196 ## @Stdout UID
197 ## Outputs the UID for the named system account
199 #---------------------------------------------------------------------
200 function get_uid_for_account() {
202 if grep -q "$1:" $ACCOUNT_LIST; then
203 grep "^$1:" $ACCOUNT_LIST | cut -d : -f2
208 #---------------------------------------------------------------------
209 ## @param named system group
210 ## @Stdout GID
211 ## Outputs the GID for the named system group
213 #---------------------------------------------------------------------
214 function get_gid_for_group() {
216 if grep -q "$1:" $GROUP_LIST; then
217 grep "^$1:" $GROUP_LIST | cut -d : -f2
222 #---------------------------------------------------------------------
223 ## @param named system account
224 ## @Stdout GID
226 ## Outputs the GID for the named system account
228 #---------------------------------------------------------------------
229 function get_primary_gid_for_account() {
231 if grep -q "$1:" $ACCOUNT_LIST; then
232 grep "^$1:" $ACCOUNT_LIST | cut -d : -f3
237 #---------------------------------------------------------------------
238 ## @param system group
239 ## @return 0 if exists
240 ## @return 1 if not
242 #---------------------------------------------------------------------
243 function exists_group() {
245 grep -q "^$1:" $GROUP_LIST
249 #---------------------------------------------------------------------
250 ## @License
252 ## This software is free software; you can redistribute it and/or modify
253 ## it under the terms of the GNU General Public License as published by
254 ## the Free Software Foundation; either version 2 of the License, or
255 ## (at your option) any later version.
257 ## This software is distributed in the hope that it will be useful,
258 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
259 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
260 ## GNU General Public License for more details.
262 ## You should have received a copy of the GNU General Public License
263 ## along with this software; if not, write to the Free Software
264 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
266 #---------------------------------------------------------------------