summaryrefslogtreecommitdiff
path: root/sysutils/user_darwin/files/groupadd.sh
blob: ab9dcc6fb26c4a282e7ea3371b5ce1361d82df58 (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
#!/bin/sh

PATH=/bin:/usr/bin:$PATH

while [ $# -gt 1 ]; do
    case $1 in
	-g) gid="$2" ;;
	*) echo "groupadd: Unrecognized option $1" 1>&2; exit 1; ;;
    esac
    shift; shift
done

getnextgid()
{
    # See the comments in useradd for more details.

    used_gids=`nireport . /groups gid 2>/dev/null ||			\
      dscl . -readall /groups PrimaryGroupID  | grep '^PrimaryGroupID:' | \
      cut -d' ' -f2`
    low_gid=300

    maybe_gid=$low_gid
    while true; do
        if echo $used_gids | grep -q $maybe_gid; then
            maybe_gid=`expr $maybe_gid + 1`
        else
            echo $maybe_gid
            return 0
        fi
    done
}

group="$1"
if [ -z "$group" ]; then
    echo "groupadd: Must specify group name" 1>&2
    exit 1
fi

if nireport . /groups/$group gid 2>/dev/null ||				\
   dscl . -read /groups/$group gid >/dev/null 2>&1; then
    echo "groupadd: Group '$group' already exists" 1>&2
    exit 1
fi

if [ -n "$gid" ]; then
    if nireport . /groups/gid=$gid gid 2>/dev/null ||			\
      dscl . -search /groups PrimaryGroupID $gid 2>/dev/null |		\
      grep PrimaryGroupID >/dev/null 2>&1 ; then
	echo "groupadd: GID $gid already exists" 1>&2
	exit 1
    fi
else
    gid=`getnextgid`
fi

if [ -x /usr/bin/niload ] || which niload | grep -v -q '^no '; then
    echo "${group}:*:${gid}:" | niload group .
else
    dscl . -create /groups/$group RecordName $group
    dscl . -create /groups/$group RecordType dsRecTypeNative:groups
    dscl . -create /groups/$group PrimaryGroupID $gid
fi
if ! nireport . /groups/$group gid 2>/dev/null &&			\
   ! dscl . -search /groups PrimaryGroupID $gid 2>/dev/null |		\
   grep PrimaryGroupID >/dev/null 2>&1 ; then
    echo "groupadd: Could not create group $gid: $group" 1>&2
    exit 1
fi