new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / init / adduser.sh
1 #!/bin/sh
2
3 set -e
4 set -u
5
6 if ! [ $# -eq 5 ]; then
7     echo "Usage: ${0} USERNAME_DEFAULT USERNAME_MAIL USERNAME_REMOTE GROUPNAME DIRECTORY.";
8     exit 1;
9 fi
10
11 echo "Wait for success..."
12
13 USER=$1
14 USER_MAIL=$2
15 USER_REM=$3
16 GROUP=$4
17 DIR=$5
18
19 UNAME=$(uname);
20
21 # Thanks Chuck L. for the mac addusers
22 if [ "$UNAME" = "Darwin" ]; then
23     if ! id -u "${USER}" > /dev/null 2>&1; then
24
25         # Creating for <= 10.4
26         if /usr/bin/sw_vers 2>/dev/null| grep "ProductVersion" | grep -E "10.2.|10.3|10.4" > /dev/null 2>&1; then
27             chmod +x ./init/darwin-addusers.pl
28             ./init/darwin-addusers.pl
29         else
30             chmod +x ./init/osx105-addusers.sh
31             ./init/osx105-addusers.sh
32         fi
33     fi
34
35 else
36     if [ "$UNAME" = "FreeBSD" -o "$UNAME" = "DragonFly" ]; then
37         GROUPADD="/usr/sbin/pw groupadd"
38         USERADD="/usr/sbin/pw useradd"
39         OSMYSHELL="/sbin/nologin"
40     elif [ "$UNAME" = "SunOS" ]; then
41         GROUPADD="/usr/sbin/groupadd"
42         USERADD="/usr/sbin/useradd"
43         OSMYSHELL="/bin/false"
44     elif [ "$UNAME" = "AIX" ]; then
45         GROUPADD="/usr/sbin/mkgroup"
46         USERADD="/usr/sbin/useradd"
47         OSMYSHELL="/bin/false"
48     elif [ "$UNAME" = "OpenBSD" ]; then
49         GROUPADD="/usr/sbin/groupadd"
50         USERADD="/usr/sbin/useradd"
51         OSMYSHELL="/sbin/nologin"
52     else
53         # Alpine linux has adduser/addgroup
54         if [ -e "/etc/alpine-release" ]; then
55             GROUPADD="/usr/sbin/addgroup"
56             USERADD="/usr/sbin/adduser"
57             OSMYSHELL="/sbin/nologin"
58         else
59             # All current linux distributions should support system accounts for
60             # users/groups. If not, leave the GROUPADD/USERADD as it was before
61             # this change
62             sys_acct_chk () {
63                 $1 --help 2>&1 | grep -e " *-r.*system account" >/dev/null 2>&1 && echo "$1 -r" || echo "$1"
64             }
65             GROUPADD=$(sys_acct_chk "/usr/sbin/groupadd -f")
66             USERADD=$(sys_acct_chk "/usr/sbin/useradd")
67             OSMYSHELL="/sbin/nologin"
68         fi
69     fi
70
71     if [ -x /usr/bin/getent ]; then
72         if [ `getent group "${GROUP}" | wc -l` -lt 1 ]; then
73             ${GROUPADD} "${GROUP}"
74         fi
75     elif ! grep "^${GROUP}" /etc/group > /dev/null 2>&1; then
76         ${GROUPADD} "${GROUP}"
77     fi
78
79     if [ "${OSMYSHELL}" = "/sbin/nologin" ]; then
80         # We first check if /sbin/nologin is present. If it is not,
81         # we look for /bin/false. If none of them is present, we
82         # just stick with nologin (no need to fail the install for that).
83         if [ ! -f ${OSMYSHELL} ]; then
84             if [ -f /bin/false ]; then
85                 OSMYSHELL="/bin/false"
86             fi
87         fi
88     fi
89
90     for U in ${USER} ${USER_MAIL} ${USER_REM}; do
91         if [ -x /usr/bin/getent ]; then 
92             if [ `getent passwd ${U} | wc -l` -lt 1 ]; then
93                     if [ "$UNAME" = "OpenBSD" ] || [ "$UNAME" = "SunOS" ]; then
94                         ${USERADD} -d "${DIR}" -s ${OSMYSHELL} -g "${GROUP}" "${U}"
95                     elif [ -e "/etc/alpine-release" ]; then
96                         ${USERADD} -G ${GROUP} -s ${OSMYSHELL} -h ${DIR} -S ${U}
97                     else
98                         ${USERADD} "${U}" -d "${DIR}" -s ${OSMYSHELL} -g "${GROUP}"
99                     fi
100             fi
101         elif [ ! `grep "^${U}" /etc/passwd > /dev/null 2>&1` ]; then
102                 if [ "$UNAME" = "OpenBSD" ] || [ "$UNAME" = "SunOS" ]; then
103                     ${USERADD} -d "${DIR}" -s ${OSMYSHELL} -g "${GROUP}" "${U}"
104                 elif [ -e "/etc/alpine-release" ]; then
105                     ${USERADD} -G ${GROUP} -s ${OSMYSHELL} -h ${DIR} -S ${U}
106                 else
107                     ${USERADD} "${U}" -d "${DIR}" -s ${OSMYSHELL} -g "${GROUP}"
108                 fi
109         fi
110     done
111 fi
112
113 echo "success";
114 exit 0;