-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
armbian-firstlogin
executable file
·417 lines (338 loc) · 14.4 KB
/
armbian-firstlogin
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/bash
#
# Copyright (c) Authors: http://www.armbian.com/authors
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
. /etc/armbian-release
check_abort()
{
echo -e "\nDisabling user account creation procedure\n"
rm -f /root/.not_logged_in_yet
if [[ ${USER_SHELL} == zsh ]]; then
printf "\nYou selected \e[0;91mZSH\x1B[0m as your default shell. If you want to use it right away, please logout and login! \n\n"
fi
trap - INT
exit 0
}
read_password()
{
unset password
prompt="$1 password: "
while IFS= read -p "$prompt" -r -s -n 1 char
do
if [[ $char == $'\0' ]]
then
break
fi
prompt='*'
password+="$char"
done
}
set_shell()
{
optionsAudits=($(cat /etc/shells | grep "zsh\|/bash" | sed 's/\/bin\///g' | sed 's/\/usr//g' | uniq))
USER_SHELL="bash"
if [[ "${#optionsAudits[@]}" -gt 1 ]]; then
while :
do
i=1
echo -e "\nChoose default system command shell:\n"
for o in "${optionsAudits[@]}"; do
echo "$i) $o"
let i++
done
read -n1 -s reply
case $reply in
"1"|"${optionsAudits[0]}") USER_SHELL="${optionsAudits[0]}"; break;;
"2"|"${optionsAudits[1]}") USER_SHELL="${optionsAudits[1]}"; break;;
*) USER_SHELL="${optionsAudits[0]}"; break;;
esac
done
fi
SHELL_PATH=$(grep /$USER_SHELL$ /etc/shells | tail -1)
chsh -s $(grep -iF "/$USER_SHELL" /etc/shells | tail -1)
echo -e "\nShell: \x1B[92m${USER_SHELL^^}\x1B[0m"
# change shell for future users
sed -i "s|^SHELL=.*|SHELL=${SHELL_PATH}|" /etc/default/useradd
sed -i "s|^DSHELL=.*|DSHELL=${SHELL_PATH}|" /etc/adduser.conf
}
set_timezone_and_locales()
{
# Grab this machine's public IP address
PUBLIC_IP=`curl --max-time 5 -s https://ipinfo.io/ip`
if [ $? -eq 0 ]; then
# Call the geolocation API and capture the output
RES=$(
curl --max-time 5 -s http://ipwhois.app/json/${PUBLIC_IP} | \
jq '.timezone, .country, .country_code' | \
while read -r TIMEZONE; do
read -r COUNTRY
echo "${TIMEZONE},${COUNTRY},${COUNTRYCODE}" | tr --delete \"
done
)
TZDATA=$(echo ${RES} | cut -d"," -f1)
STATE=$(echo ${RES} | cut -d"," -f2)
CCODE=$(echo ${RES} | cut -d"," -f3 | xargs)
LOCALES=$(grep territory /usr/share/i18n/locales/* | grep _"$CCODE" | cut -d ":" -f 1 | cut -d "/" -f 6 | \
xargs -I{} grep {} /usr/share/i18n/SUPPORTED | grep "\.UTF-8" | cut -d " " -f 1)
# UTF8 is not present everywhere so check again in case it returns empty value
[[ -z "$LOCALES" ]] && LOCALES=$(grep territory /usr/share/i18n/locales/* | grep _"$CCODE" | cut -d ":" -f 1 | cut -d "/" -f 6 | \
xargs -I{} grep {} /usr/share/i18n/SUPPORTED | cut -d " " -f 1)
echo -e "Detected timezone: \x1B[92m$TZDATA\x1B[0m"
echo ""
read -n1 -s -r -p "Set user language based on your location? [Y/n] " response
echo ""
# change it only if we have a match and if we agree
if [[ -n "$LOCALES" && "${response}" =~ ^(Y|y|"")$ ]]; then
options=(`echo ${LOCALES}`);
# when having more locales, prompt for choosing one
if [[ "${#options[@]}" -gt 1 ]]; then
options+=("Skip generating locales")
echo -e "\nAt your location, more locales are possible:\n"
PS3='Please enter your choice:'
select opt in "${options[@]}"
do
if [[ " ${options[@]} " =~ " ${opt} " ]]; then
LOCALES=${opt}
break
fi
done
fi
if [[ "${LOCALES}" != *Skip* ]]; then
# reconfigure tzdata
timedatectl set-timezone "${TZDATA}"
dpkg-reconfigure --frontend=noninteractive tzdata > /dev/null 2>&1
# generate locales
echo ""
sed -i 's/# '"${LOCALES}"'/'"${LOCALES}"'/' /etc/locale.gen
echo -e "Generating locales: \x1B[92m${LOCALES}\x1B[0m"
locale-gen $LOCALES > /dev/null 2>&1
# setting detected locales only for user
echo "export LC_ALL=$LOCALES" >> /home/$RealUserName/.bashrc
echo "export LANG=$LOCALES" >> /home/$RealUserName/.bashrc
echo "export LANGUAGE=$LOCALES" >> /home/$RealUserName/.bashrc
echo "export LC_ALL=$LOCALES" >> /home/$RealUserName/.xsessionrc
echo "export LANG=$LOCALES" >> /home/$RealUserName/.xsessionrc
echo "export LANGUAGE=$LOCALES" >> /home/$RealUserName/.xsessionrc
fi
fi
fi
}
add_profile_sync_settings()
{
/usr/bin/psd >/dev/null 2>&1
config_file="${HOME}/.config/psd/psd.conf"
if [ -f "${config_file}" ]; then
# test for overlayfs
sed -i 's/#USE_OVERLAYFS=.*/USE_OVERLAYFS="yes"/' "${config_file}"
case $(/usr/bin/psd p 2>/dev/null | grep Overlayfs) in
*active*)
echo -e "\nConfigured profile sync daemon with overlayfs."
;;
*)
echo -e "\nConfigured profile sync daemon."
sed -i 's/USE_OVERLAYFS="yes"/#USE_OVERLAYFS="no"/' "${config_file}"
;;
esac
fi
systemctl --user enable psd.service >/dev/null 2>&1
systemctl --user start psd.service >/dev/null 2>&1
}
add_user()
{
read -t 0 temp
while [ -f "/root/.not_logged_in_yet" ]; do
echo -e "\nPlease provide a username (eg. your forename): \c"
read -e username
RealUserName="$(echo "$username" | tr '[:upper:]' '[:lower:]' | tr -d -c '[:alnum:]')"
[ -z "$RealUserName" ] && return
if ! id "$RealUserName" >/dev/null 2>&1; then break; else echo -e "Username \e[0;31m$RealUserName\x1B[0m already exists on the system."; fi
done
while [ -f "/root/.not_logged_in_yet" ]; do
read_password "Create"
first_input=$password
echo ""
read_password "Repeat"
second_input=$password
echo ""
if [[ $first_input == $second_input ]]; then
result="$(cracklib-check <<<"$password")"
okay="$(awk -F': ' '{ print $2}' <<<"$result")"
if [[ "$okay" == "OK" ]]; then
echo -e ""
read -e -p "Please provide your real name: " -i "${RealUserName^}" RealName
adduser --quiet --disabled-password --home /home/"$RealUserName" --gecos "$RealName" "$RealUserName"
(echo $first_input;echo $second_input;) | passwd "$RealUserName" >/dev/null 2>&1
for additionalgroup in sudo netdev audio video disk tty users games dialout plugdev input bluetooth systemd-journal ssh; do
usermod -aG ${additionalgroup} ${RealUserName} 2>/dev/null
done
# fix for gksu in Xenial
touch /home/$RealUserName/.Xauthority
chown $RealUserName:$RealUserName /home/$RealUserName/.Xauthority
RealName="$(awk -F":" "/^${RealUserName}:/ {print \$5}" </etc/passwd | cut -d',' -f1)"
[ -z "$RealName" ] && RealName=$RealUserName
echo -e "\nDear \e[0;92m${RealName}\x1B[0m, your account \e[0;92m${RealUserName}\x1B[0m has been created and is sudo enabled."
echo -e "Please use this account for your daily work from now on.\n"
rm -f /root/.not_logged_in_yet
# set up profile sync daemon on desktop systems
which psd >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "${RealUserName} ALL=(ALL) NOPASSWD: /usr/bin/psd-overlay-helper" >> /etc/sudoers
touch /home/${RealUserName}/.activate_psd
chown $RealUserName:$RealUserName /home/${RealUserName}/.activate_psd
fi
break
else
echo -e "Rejected - \e[0;31m$okay.\x1B[0m Try again."
fi
elif [[ -n $password ]]; then
echo -e "Rejected - \e[0;31mpasswords do not match.\x1B[0m Try again."
fi
done
}
if [[ -f /root/.not_logged_in_yet && -n $(tty) ]]; then
# disable autologin
rm -f /etc/systemd/system/getty@.service.d/override.conf
rm -f /etc/systemd/system/serial-getty@.service.d/override.conf
systemctl daemon-reload
# detect display manager
desktop_lightdm=$(dpkg-query -W -f='${db:Status-Abbrev}\n' lightdm 2>/dev/null)
desktop_gdm3=$(dpkg-query -W -f='${db:Status-Abbrev}\n' gdm3 2>/dev/null)
if [ "$IMAGE_TYPE" != "nightly" ]; then
if [ "$BRANCH" == "dev" ]; then
echo -e "\nYou are using an Armbian preview build !!!"
echo -e "\nThis image is provided \e[0;31mAS IS\x1B[0m with \e[0;31mNO WARRANTY\x1B[0m and \e[0;31mNO END USER SUPPORT!\x1B[0m.\n"
elif [ "$DISTRIBUTION_STATUS" != "supported" ]; then
echo -e "\nYou are using an Armbian with unsupported ($DISTRIBUTION_CODENAME) userspace !!!"
echo -e "\nThis image is provided \e[0;31mAS IS\x1B[0m with \e[0;31mNO WARRANTY\x1B[0m and \e[0;31mNO END USER SUPPORT!\x1B[0m.\n"
fi
else
echo -e "\nYou are using an Armbian nightly build meant only for developers to provide"
echo -e "constructive feedback to improve build system, OS settings or user experience."
echo -e "If this does not apply to you, \e[0;31mSTOP NOW!\x1B[0m. Especially don't use this image for"
echo -e "daily work since things might not work as expected or at all and may break"
echo -e "anytime with next update. \e[0;31mYOU HAVE BEEN WARNED!\x1B[0m"
echo -e "\nThis image is provided \e[0;31mAS IS\x1B[0m with \e[0;31mNO WARRANTY\x1B[0m and \e[0;31mNO END USER SUPPORT!\x1B[0m.\n"
fi
echo -e "New to Armbian? Documentation: \e[1m\e[39mhttps://docs.armbian.com\x1B[0m Support: \e[1m\e[39mhttps://forum.armbian.com\x1B[0m\n"
trap '' 2
while [ -f "/root/.not_logged_in_yet" ]; do
read_password "New root"
# only allow one login. Once you enter root password, kill others.
loginfrom=$(who am i | awk '{print $2}')
who -la | grep root | grep -v "$loginfrom" | awk '{print $7}' | xargs --no-run-if-empty kill -9
first_input=$password
echo ""
read_password "Repeat"
second_input=$password
echo ""
if [[ $first_input == $second_input ]]; then
result="$(cracklib-check <<<"$password")"
okay="$(awk -F': ' '{ print $2}' <<<"$result")"
if [[ "$okay" == "OK" ]]; then
(echo $first_input;echo $second_input;) | passwd root >/dev/null 2>&1
break
else
echo -e "Rejected - \e[0;31m$okay.\x1B[0m Try again."
fi
elif [[ -n $password ]]; then
echo -e "Rejected - \e[0;31mpasswords do not match.\x1B[0m Try again."
fi
done
trap - INT TERM EXIT
# ask user to select shell
trap '' 2
set_shell
trap - INT TERM EXIT
trap check_abort INT
while [ -f "/root/.not_logged_in_yet" ]; do
echo -e "\nCreating a new user account. Press <Ctrl-C> to abort"
[ -n "$desktop_lightdm" ] && echo -e "\n\e[0;31mDesktop environment will not be enabled if you abort the new user creation\x1B[0m"
add_user
done
trap - INT TERM EXIT
# ask user to select automated locales or not
trap '' 2
set_timezone_and_locales
trap - INT TERM EXIT
if [[ ${USER_SHELL} == zsh ]]; then
printf "\nYou selected \e[0;91mZSH\x1B[0m as your default shell. If you want to use it right away, please logout and login! \n\n"
fi
# check whether desktop environment has to be considered
if [ -n "$desktop_lightdm" ] && [ -n "$RealName" ] ; then
# 1st run goes without login
mkdir -p /etc/lightdm/lightdm.conf.d
cat <<-EOF > /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
[Seat:*]
autologin-user=$RealUserName
autologin-user-timeout=0
user-session=xfce
EOF
# select gnome session (has to be first or it breaks budgie/cinnamon desktop autologin and user-session)
[[ -x $(which gnome-session) ]] && sed -i "s/user-session.*/user-session=ubuntu/" /etc/lightdm/lightdm.conf.d/11-armbian.conf
[[ -x $(which gnome-session) ]] && sed -i "s/user-session.*/user-session=ubuntu/" /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
# select budgie session
[[ -x $(which budgie-desktop) ]] && sed -i "s/user-session.*/user-session=budgie-desktop/" /etc/lightdm/lightdm.conf.d/11-armbian.conf
[[ -x $(which budgie-desktop) ]] && sed -i "s/user-session.*/user-session=budgie-desktop/" /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
# select deepin session
[[ -x $(which deepin-wm) ]] && sed -i "s/user-session.*/user-session=deepin/" /etc/lightdm/lightdm.conf.d/11-armbian.conf
[[ -x $(which deepin-wm) ]] && sed -i "s/user-session.*/user-session=deepin/" /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
# select cinnamon session
[[ -x $(which cinnamon) ]] && sed -i "s/user-session.*/user-session=cinnamon/" /etc/lightdm/lightdm.conf.d/11-armbian.conf
[[ -x $(which cinnamon) ]] && sed -i "s/user-session.*/user-session=cinnamon/" /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
# select mate session
[[ -x $(which mate-wm) ]] && sed -i "s/user-session.*/user-session=mate/" /etc/lightdm/lightdm.conf.d/11-armbian.conf
[[ -x $(which mate-wm) ]] && sed -i "s/user-session.*/user-session=mate/" /etc/lightdm/lightdm.conf.d/22-armbian-autologin.conf
ln -sf /lib/systemd/system/lightdm.service /etc/systemd/system/display-manager.service
if [[ -f /var/run/resize2fs-reboot ]]; then
# Let the user reboot now otherwise start desktop environment
printf "\n\n\e[0;91mWarning: a reboot is needed to finish resizing the filesystem \x1B[0m \n"
printf "\e[0;91mPlease reboot the system now \x1B[0m \n\n"
elif [ -z "$ConfigureDisplay" ] || [ "$ConfigureDisplay" = "n" ] || [ "$ConfigureDisplay" = "N" ]; then
echo -e "\n\e[1m\e[39mNow starting desktop environment...\x1B[0m\n"
sleep 1
service lightdm start 2>/dev/null
if [ -f /root/.desktop_autologin ]; then
rm /root/.desktop_autologin
else
systemctl -q enable armbian-disable-autologin.timer
systemctl start armbian-disable-autologin.timer
fi
# logout if logged at console
[[ -n $(who -la | grep root | grep tty1) ]] && exit 1
fi
elif [ -n "$desktop_gdm3" ] && [ -n "$RealName" ] ; then
# 1st run goes without login
mkdir -p /etc/gdm3
cat <<-EOF > /etc/gdm3/custom.conf
[daemon]
AutomaticLoginEnable = true
AutomaticLogin = $RealUserName
EOF
ln -sf /lib/systemd/system/gdm3.service /etc/systemd/system/display-manager.service
if [[ -f /var/run/resize2fs-reboot ]]; then
# Let the user reboot now otherwise start desktop environment
printf "\n\n\e[0;91mWarning: a reboot is needed to finish resizing the filesystem \x1B[0m \n"
printf "\e[0;91mPlease reboot the system now \x1B[0m \n\n"
elif [ -z "$ConfigureDisplay" ] || [ "$ConfigureDisplay" = "n" ] || [ "$ConfigureDisplay" = "N" ]; then
echo -e "\n\e[1m\e[39mNow starting desktop environment...\x1B[0m\n"
sleep 1
service gdm3 start 2>/dev/null
if [ -f /root/.desktop_autologin ]; then
rm /root/.desktop_autologin
else
(sleep 20; sed -i "s/AutomaticLoginEnable.*/AutomaticLoginEnable = false/" /etc/gdm3/custom.conf) &
fi
# logout if logged at console
[[ -n $(who -la | grep root | grep tty1) ]] && exit 1
fi
else
# Display reboot recommendation if necessary
if [[ -f /var/run/resize2fs-reboot ]]; then
printf "\n\n\e[0;91mWarning: a reboot is needed to finish resizing the filesystem \x1B[0m \n"
printf "\e[0;91mPlease reboot the system now \x1B[0m \n\n"
fi
fi
fi