-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain-join
207 lines (179 loc) · 4.59 KB
/
domain-join
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
#
# Join to domain for Enterprise Linux 7 & 8
#
# Read domain to be joined from $(hostname)
# Operating system name and version is read from /etc/os-release
#
# Software installs necessary packages
#
# @author Arsi Atomi <[email protected]>
# @license GNU LGPL 2.1 <https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt>
function Help {
echo "Usage: domain-join [OPTIONS]"
echo
echo "Description: Join machine to Active Directory domain"
echo
echo "OPTIONS:"
echo " -u, --user User with join priviledges to domain"
echo " --os-name Operating system (defaults to /etc/os-release)"
echo " --os-version Operating system major version (defaults to /etc/os-release)"
echo " --computer-ou OU for Computer"
echo " --login-groups Login groups"
echo " --admin-groups Admin groups (sudoers)"
echo " -v Verbose"
echo " -q Quiet"
echo
}
LEFTOVERS=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--help)
Help
exit
;;
--os-name)
OSNAME=$2
shift
shift
;;
--os-version)
OSVERSION=$2
shift
shift
;;
--computer-ou)
COMPUTEROU=$2
shift
shift
;;
--login-groups)
LOGINGROUPS=$2
shift
shift
;;
--admin-groups)
ADMINGROUPS=$2
shift
shift
;;
-u|--user)
USERNAME=$2
shift
shift
;;
-v)
VERBOSE="-v"
shift
;;
-q)
QUIET="-q"
shift
;;
*)
LEFTOVERS+=("$1")
shift
;;
esac
done
set -- "${LEFTOVERS[@]}"
if [ -n "$1" ]
then
DOMAIN=$1
fi
source /etc/os-release
if [ "$(whoami)" != "root" ]
then
echo "You need to be root."
exit 1
fi
if [ -z "${USERNAME}" ]
then
read -p "Username: " USERNAME
fi
if [ -z "${LOGINGROUPS}" ]
then
read -p "Login groups: " LOGINGROUPS
fi
if [ -z "${ADMINGROUPS}" ]
then
read -p "Admin groups (sudoers): " ADMINGROUPS
fi
USERSTR="--user=${USERNAME}"
if [ -z "${OSNAME}" ]
then
if [ -n "${NAME}" ]
then
OSNAME="${NAME}"
OSNAMESTR="--os-name=\"${OSNAME}\""
if [ -z "${OSVERSION}" ]
then
IFS='.' read majorver minorver <<< "${VERSION_ID}"
OSVERSION=${majorver}
fi
OSVERSIONSTR="--os-version=${OSVERSION}"
fi
fi
if [ -n "${COMPUTEROU}" ]
then
COMPUTEROUSTR="--computer-ou=\"${COMPUTEROU}\ "
fi
if [ -z "${DOMAIN}" ]
then
IFS='.' read x y z <<< "$(hostname)"
DOMAIN=${y}.${z}
fi
if [ -z "${QUIET}" ]
then
echo "Checking and installing missing packages ..."
fi
yum -y install realmd samba-common oddjob oddjob-mkhomedir sssd adcli samba-common-tools > /dev/null || {
echo "Unable to install packages."
exit 1
}
realm discover ${DOMAIN} > /dev/null || {
exit 1
} && {
realm join ${VERBOSE} ${USERSTR} ${OSVERSIONSTR} ${COMPUTEROUSTR} ${DOMAIN}
}
CONFDIR="/etc/sssd"
CONFDFPATH=${CONFDIR}/"conf.d/$(echo ${DOMAIN} | tr . -).conf"
SUDOERSDFPATH="/etc/sudoers.d/$(echo ${DOMAIN} | tr . -)"
if [ -z "${QUIET}" ]
then
echo "Modifying ${CONFDIR}/sssd.conf ..."
fi
mv ${CONFDIR}/sssd.conf ${CONFDIR}/sssd.conf.orig
cat ${CONFDIR}/sssd.conf.orig | \
sed -e 's#access_provider = ad#access_provider = simple#' \
> ${CONFDIR}/sssd.conf
chmod 600 ${CONFDIR}/sssd.conf
if [ -z "${QUIET}" ]
then
echo "Creating ${CONFDFPATH} ..."
fi
cat <<EOF > ${CONFDFPATH}
[domain/${DOMAIN}]
krb5_store_password_if_offline = False
use_fully_qualified_names = True
access_provider = simple
simple_allow_groups = ${LOGINGROUPS}
EOF
chmod 600 ${CONFDFPATH}
if [ -z "${QUIET}" ]
then
echo "Creating ${SUDOERSDFPATH} ..."
fi
IFS=","
for ADMINGROUP in ${ADMINGROUPS}
do
ADMINGROUP_T=$(echo ${ADMINGROUP} | xargs)
cat <<EOF >> ${SUDOERSDFPATH}
%$(echo ${ADMINGROUP_T} | sed 's# #\\ #g')@${DOMAIN} ALL=(ALL) ALL
EOF
done
visudo -cf ${SUDOERSDFPATH}
chmod 440 ${SUDOERSDFPATH}
systemctl restart sssd