forked from lunar-linux/lunar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseradd.lunar
114 lines (97 loc) · 3.5 KB
/
useradd.lunar
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
#!/bin/bash
# #
# This code is written for Lunar Linux, see #
# http://lunar-linux.org #
# #
############################################################
# #
# $FUNCTIONS/uid.lunar #
# includes add_priv_user add_priv_group #
# 20030306 #
# #
############################################################
# #
# Copyrighted Auke Kok 2006 under GPLv2 #
# #
############################################################
# function: add_priv_group
# usage : add_priv_group groupname [addgroupopts [addgroupopts]...]
# info : adds groupname and passes addgroupopts to 'addgroup'
function add_priv_group() {
local GROUPNAME
debug_msg "function add_priv_group ($@)"
if [ -n "$INSTALLWATCHFILE" ] ; then
devoke_installwatch
fi
GROUPNAME=$1
if [ -z "$GROUPNAME" ] ; then
message "${PROBLEM_COLOR}!add_priv_user: no groupname specified${DEFAULT_COLOR}"
return 1
fi
if grep -q "^$GROUPNAME:" /etc/group ; then
verbose_msg "group \"$GROUPNAME\" already exists, not creating"
else
# add the group:
for (( N=0 ; N<100 ; N++)) ; do
if [ -z "$(cat /etc/group | cut -d: -f3 | grep "^$N$" )" ] ; then
break
fi
done
if [ "$N" == "100" ] ; then
message "${PROBLEM_COLOR}!add_priv_user: no more group id's left under gid=100, bailing out!${DEFAULT_COLOR}"
return 1
fi
verbose_msg "creating group \"$GROUPNAME\" with id=\"$N\""
groupadd -g $N $GROUPNAME
fi
if [ -n "$INSTALLWATCHFILE" ] ; then
invoke_installwatch
fi
}
# function: add_priv_user
# usage : add_priv_user username:groupname [adduseropts [adduseropts]...]
# info : adds username:groupname and passes adduseropts to 'adduser'
function add_priv_user() {
local USERNAME GROUPNAME
debug_msg "function add_priv_user ($@)"
if [ -n "$INSTALLWATCHFILE" ] ; then
devoke_installwatch
fi
USERNAME=$(echo $1 | cut -d: -f1)
GROUPNAME=$(echo $1 | cut -d: -f2)
if [ -z "$USERNAME" ] ; then
message "${PROBLEM_COLOR}!add_priv_user: no username specified${DEFAULT_COLOR}"
return 1
fi
if [ -z "$GROUPNAME" ] ; then
message "${PROBLEM_COLOR}!add_priv_user: no groupname specified${DEFAULT_COLOR}"
return 1
fi
if id $USERNAME &> /dev/null ; then
verbose_msg "user \"$USERNAME\" already exists, not creating or modifying"
else
if grep -q "^$GROUPNAME:" /etc/group ; then
verbose_msg "group \"$GROUPNAME\" already exists, not creating"
else
if ! add_priv_group $GROUPNAME ; then
return 1
fi
fi
# add the user:
for (( N=0 ; N<100 ; N++)) ; do
if [ -z "$(cat /etc/passwd | cut -d: -f3 | grep "^$N$" )" ] ; then
break
fi
done
if [ "$N" == "100" ] ; then
message "${PROBLEM_COLOR}!add_priv_user: no more user id's left under uid=100, bailing out!${DEFAULT_COLOR}"
return 1
fi
shift
verbose_msg "creating user \"$USERNAME\" (opts=\"-u $N -g $GROUPNAME $@\")"
useradd -u $N -g $GROUPNAME $USERNAME $@
fi
if [ -n "$INSTALLWATCHFILE" ] ; then
invoke_installwatch
fi
}