forked from makubo/linux-ad-join
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin-ad-sssd.sh
executable file
·2706 lines (1956 loc) · 59.3 KB
/
join-ad-sssd.sh
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# Joins Debian machine to the Active Directory by using sssd and realmd.
#
# This script configures the environment and joins the machine to the Active Directory domain.
# To join the domain, used sssd (System Security Services Daemon) and realmd.
#
# realmd discovers information about the domain or realm automatically and
# does not require complicated configuration in order to join a domain or realm.
# realmd configures the basic settings of the sssd to do the actual network authentication and
# user account lookups.
#
# The script configures several subsystems during execution.
#
# - Configures the local DNS cache using dnsmasq. Available DNS servers are automatically detected.
# - Configures the local DNS resolver and checks that DNS settings are correct.
# - Configures the NTP client and forces synchronization of the system time.
# Available NTP servers are automatically detected.
# - Configures Kerberos. Available KDC servers are automatically detected.
# - Configures realmd and joins the machine to the domain using the first available LDAP server.
# The available LDAP servers are detected automatically.
# - Configures sssd, fine tuning after realmd.
# - Configures PAM, enables mkhomedir module.
# - Configures access to the server (login) using domain groups.
# - Configures administrator rights on the server (sudo) using the domain groups.
#
# Bonus:
#
# - Configures SSH and enables GSSAPI for passwordless login.
# - Configures autocomplete in bash, enables autocomplete for a interactive root sessions.
#
# Copyright (C) 2017 Stepan Kokhanovskiy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Script configuration section
readonly PROGNAME="$(basename "${0}")"
readonly LOCK_ENABLED=1
readonly LOCK_FILE="/tmp/${PROGNAME}.lock"
readonly LOCK_FD=931
readonly LOG_NAME="${PROGNAME%.*}"
readonly LOG_ENABLED=0
readonly LOG_FILE="/var/log/${LOG_NAME}/${LOG_NAME}.log"
readonly SYSLOG_ENABLED=0
readonly SYSLOG_PRIORITY="user.notice"
# Default Site Configuration
readonly DEFAULT_SERVER_LIST="time.campus.qut.edu.au"
readonly DEFAULT_DOMAIN_NAME="qut.edu.au"
readonly COMPUTER_AD_OU="OU=Linux,OU=SEF,OU=Specialised,OU=Workstations"
readonly SITE_REQUIRED_PACKAGES="libpangox-1.0-0"
# Global constants
readonly APTGET_ASSUME_YES=1
readonly BACKUP_DIR="${HOME}/.${PROGNAME}"
readonly OS_RELEASE_FILE="/etc/os-release"
readonly PACKAGE_INSTALLED_STRING="ok installed"
readonly DNS_LDAP_SRV_FORMAT="_ldap._tcp.%s"
readonly DIG_TRIES=1
readonly DIG_TIMEOUT=3
readonly DNSMASQ_CONFIG_FILE="/etc/dnsmasq.d/dnscache.conf"
readonly DNSMASQ_RESOLV_FILE="/etc/resolv.dnsmasq"
readonly DNSMASQ_SYSTEM_RESOLV_FILE="/etc/resolv.conf"
readonly DNSMASQ_HOSTS_FILE="/etc/hosts"
readonly DNSMASQ_SERVICE_NAME="dnsmasq"
readonly NTP_CONFIG_FILE="/etc/ntp.conf"
readonly NTP_CONFIG_BACKUP="${BACKUP_DIR}/$(basename "${NTP_CONFIG_FILE}")"
readonly NTP_SERVICE_NAME="ntp"
readonly NTP_TEMP_LOG="/tmp/ntpd.log"
readonly NTP_SERVER_NAME="time.campus.qut.edu.au"
readonly PORT_CONNECT_TIMEOUT=5
readonly PORT_TEST_COMMAND="cat </dev/null >/dev/tcp/%s/%s"
readonly LDAP_PORT=389
readonly KERBEROS_PORT=88
readonly KERBEROS_CONFIG_FILE="/etc/krb5.conf"
readonly KERBEROS_TICKET_LIFETIME="10h"
readonly KERBEROS_RENEW_LIFETIME="7d"
readonly KERBEROS_CLOCKSKEW=300
readonly USER_NAME_PROMPT="Enter the domain user name or leave empty to exit.
Username: "
readonly LOGIN_GROUP_PROMPT="Enter the name of the domain group that will be permitted to login or leave empty to continue.
Group to login: "
readonly SUDO_GROUP_PROMPT="Enter the name of the domain group that will be permitted to sudo or leave empty to continue.
Group to sudo: "
readonly REALMD_CONFIG_FILE="/etc/realmd.conf"
readonly REALMD_PRINCIPAL="host/%s@%s"
readonly SSSD_CONFIG_FILE="/etc/sssd/sssd.conf"
readonly SSSD_DB_DIR="/var/lib/sss/db"
readonly SSSD_CACHE_DIR="/var/lib/sss/mc"
readonly SSSD_SERVICE_NAME="sssd"
readonly SSSD_DISCOVERY_SERVER="_srv_"
readonly SSSD_DEBUG_LEVEL=0
readonly SSSD_AD_SERVER_DISCOVERY=1
readonly SSSD_CACHE_CREDENTIALS=1
readonly SSSD_KRB5_AUTH_TIMEOUT=60
readonly SSSD_LDAP_OPT_TIMEOUT=${SSSD_KRB5_AUTH_TIMEOUT}
readonly SSSD_PAM_ID_TIMEOUT=${SSSD_KRB5_AUTH_TIMEOUT}
readonly SSSD_IGNORE_GROUP_MEMBERS=1
readonly SSSD_USE_FQDN_NAMES=0
readonly SSSD_PAM_PWD_EXP_DAYS=14
readonly PAM_SESSIONS_CONFIG_FILE="/etc/pam.d/common-session"
readonly PAM_MKHOMEDIR_CONFIG_FILE="/usr/share/pam-configs/mkhomedir"
readonly SUDO_CONFIG_DIR="/etc/sudoers.d"
readonly SUDO_SERVICE_NAME="sudo"
readonly SSHD_CONFIG_FILE="/etc/ssh/sshd_config"
readonly SSHD_TMP_CONFIG_FILE="/tmp/sshd_config"
readonly SSHD_SERVICE_NAME="ssh"
readonly BASH_SYSTEM_STARTUP_FILE="/etc/bash.bashrc"
readonly BASH_TMP_SYSTEM_STARTUP_FILE="/tmp/bash.bashrc"
readonly BASH_COMPLETION_FROM_PATTERN="# enable bash completion in interactive shells\r#if ! shopt -oq posix; then\r# if \[ -f \/usr\/share\/bash-completion\/bash_completion \]; then\r# \. \/usr\/share\/bash-completion\/bash_completion\r# elif \[ -f \/etc\/bash_completion \]; then\r# \. \/etc\/bash_completion\r# fi\r#fi\r"
readonly BASH_COMPLETION_TO_PATTERN="# enable bash completion in interactive shells\rif ! shopt -oq posix; then\r if \[ -f \/usr\/share\/bash-completion\/bash_completion \]; then\r \. \/usr\/share\/bash-completion\/bash_completion\r elif \[ -f \/etc\/bash_completion \]; then\r \. \/etc\/bash_completion\r fi\rfi\r"
# Paths to binaries
readonly LOGGER_PATH="/usr/bin/logger"
readonly APTGET_PATH="/usr/bin/apt-get"
readonly SERVICE_PATH="/usr/sbin/service"
readonly DIRNAME_PATH="/usr/bin/dirname"
readonly GETENT_PATH="/usr/bin/getent"
readonly DIG_PATH="/usr/bin/dig"
readonly NTPDATE_PATH="/usr/sbin/ntpdate"
readonly NTPD_PATH="/usr/sbin/ntpd"
readonly REALM_PATH="/usr/sbin/realm"
readonly TIMEOUT_PATH="/usr/bin/timeout"
readonly TR_PATH="/usr/bin/tr"
readonly KINIT_PATH="/usr/bin/kinit"
readonly KLIST_PATH="/usr/bin/klist"
readonly KDESTROY_PATH="/usr/bin/kdestroy"
readonly DNSMASQ_PATH="/usr/sbin/dnsmasq"
readonly PAM_AUTH_UPDATE_PATH="/usr/sbin/pam-auth-update"
readonly HEAD_PATH="/usr/bin/head"
readonly WC_PATH="/usr/bin/wc"
readonly AWK_PATH="/usr/bin/awk"
# Error messages
readonly E_ANOTHER_INSTANCE_IS_RUNNING="Possibly an another instance of the ${PROGNAME} script is currently running."
readonly E_CAN_NOT_CREATE_LOCK_FILE="Can not create lock file: '${LOCK_FILE}'. ${E_ANOTHER_INSTANCE_IS_RUNNING}"
readonly E_CAN_NOT_LOCK_FILE="Can not lock file: '${LOCK_FILE}'. ${E_ANOTHER_INSTANCE_IS_RUNNING}"
readonly E_CAN_NOT_CREATE_LOG_FILE="Can not create log file: ${LOG_NAME}."
readonly E_CAN_NOT_WRITE_LOG_FILE="Can not write to log file: ${LOG_NAME}."
readonly E_LOG_FILE_IS_NOT_SPECIFIED="Log enabled (LOG_ENABLED = ${LOG_ENABLED}) but a file name for log (LOG_FILE) is not specified. Check the script configuration section."
readonly E_LOGGER_FAILED="Can not write message to the syslog."
readonly E_ARGS_INVALID="Try '${PROGNAME} -h' for help."
readonly E_APTGET_UPDATE="apt-get update failed."
readonly E_APTGET_INSTALL="apt-get install failed."
readonly E_ROOT_REQUIRED="This script must be run as root."
readonly E_DOMAIN_NAME_NOT_FOUND="The DNS domain name not found. Try to specify the domain name using -d parameter or see 'man dnsdomainname' for details."
readonly E_DOMAIN_NAME_NOT_RESOLVED="Check the DNS settings specified at the /etc/resolv.conf."
readonly E_LDAP_SRV_NOT_RESOLVED="Can not resolve the LDAP SRV record '%s'. Check that the DNS servers are configured properly."
readonly E_DOMAIN_CONTROLLER_NOT_FOUND="Can not found domain controllers for the DNS domain name '%s'."
readonly E_NTP_SERVER_UNAVAILABLE="NTP server '%s' is unavailable."
readonly E_NTP_SERVER_NO_AVAILABLE="There are no NTP servers available."
readonly E_NTP_SYNC_FAILED="Time synchronization failed."
readonly E_PORT_UNAVAILABLE="Port '%s:%s' is unavailable."
readonly E_LDAP_SERVER_NO_AVAILABLE="There are no LDAP servers available."
readonly E_DIG_EMPTY_RESPONSE="Can not resolve the DNS record '%s', type %s. Empty response from DNS server."
readonly E_REALM_ALREADY_JOINED="Already joined to the domain '%s'."
readonly E_DNS_SERVER_UNAVAILABLE="DNS server %s is unavailable."
readonly E_GROUP_NOT_FOUND="Can not found group '%s'."
readonly E_HOST_ADDRESS_NOT_FOUND="Can not determine the IP address of the host."
readonly E_USER_NAME_NOT_SPECIFIED="Domain user name is not specified."
readonly E_KERBEROS_SERVER_NO_AVAILABLE="There are no kerberos servers available."
# Global flags
IS_APTGET_UPDATE_COMPLETED=0
DEBUG_ENABLED=1
# Creates empty file
# Arguments:
# 1: Path to file to create
# Returns:
# 0: success
# 1: failure
create_file()
{
local path="${1}"
[[ -z "${path}" ]] && return 0
install -D "/dev/null" "${path}" || return 1
return 0
}
# Checks that flag value is true: not empty or zero.
# Arguments:
# 1: value to check
# Returns:
# 0: value is true
# 1: value is false
is_true()
{
local value="${1}"
if [[ -n "${value}" ]] && [[ "${value}" != "0" ]] ; then
return 0
else
return 1
fi
}
# Prints message to the error output and to the log file
# Arguments:
# 1: message to write
# Returns:
# 0: success
# 1: failure
log()
{
local msg="${@}"
local timestamp="$(date --rfc-3339=seconds)"
echo "${msg}" 1>&2
if is_true "${LOG_ENABLED}" ; then
# Exit if log file is not specified
if [[ -z "${LOG_FILE}" ]] ; then
echo "${E_LOG_FILE_IS_NOT_SPECIFIED}" 1>&2
return 1
fi
# Create log file if it is not exists
if ! [[ -f "${LOG_FILE}" ]] ; then
if ! create_file "${LOG_FILE}" ; then
echo "${E_CAN_NOT_CREATE_LOG_FILE}" 1>&2
return 1
fi
fi
if ! echo "[${timestamp}]: ${msg}" >> "${LOG_FILE}" ; then
echo "${E_CAN_NOT_WRITE_LOG_FILE}" 1>&2
return 1
fi
fi
if is_true "${SYSLOG_ENABLED}" && test_app "${LOGGER_PATH}" ; then
if ! "${LOGGER_PATH}" -t "${LOG_NAME}" -p "${SYSLOG_PRIORITY}" "${msg}" ; then
echo "${E_LOGGER_FAILED}" 1>&2
fi
fi
return 0
}
# Prints debug log message
# Arguments:
# 1: message to write
# Returns:
# 0: success
# 1: failure
debug() {
local msg="${@}"
if is_true "${DEBUG_ENABLED}" ; then
log "DEBUG: ${msg}" || return 1
fi
return 0
}
# Locks file to prevent multiple instances of the script from running at the same time
# Arguments:
# None
# Returns:
# 0: success
# 1: failure
lock()
{
if is_true "${LOCK_ENABLED}" ; then
# Create lock file
if ! eval "exec ${LOCK_FD}>${LOCK_FILE}" ; then
log "${E_CAN_NOT_CREATE_LOCK_FILE}"
return 1
fi
# Acquier the lock
if ! flock -n "${LOCK_FD}" ; then
log "${E_CAN_NOT_LOCK_FILE}"
return 1
fi
fi
return 0
}
# Stop script execution with error message
# Arguments:
# 1: error message
# Returns:
# None
eexit()
{
local msg="${@}"
log "${msg}"
exit 1
}
# Print text with the specified new line at the end
# Arguments:
# 1: text to print
# 2: new line to print
# Returns:
# 0: success
# 1: failure
add_line()
{
local text="${1}"
local new_line="${2}"
[[ -z "${text}" ]] || echo "${text}"
[[ -z "${new_line}" ]] || echo "${new_line}"
return 0
}
# Print joined lines by using the specified delimiter
# Arguments:
# 1: lines of the text
# 2: delimiter, empty by default
# Returns:
# 0: success
# 1: failure
join_lines()
{
local text="${1}"
local delimiter="${2}"
local line=""
local first_line_flag=1
[[ -z "${text}" ]] && return 0
while read line ; do
if is_true "${first_line_flag}" ; then
first_line_flag=0
else
printf "%s" "${delimiter}"
fi
printf "%s" "${line}"
done <<< "${text}"
return 0
}
# Prints only the first line from text
# Arguments:
# 1: lines of the text
# Returns:
# 0: success
# 1: failure
first_line()
{
local text="${1}"
[[ -z "${text}" ]] && return 0
"${HEAD_PATH}" --lines=1 <<< "${text}" || return 1
return 0
}
# Prints number of lines in the text
# Arguments:
# 1: lines of the text
# Returns:
# 0: success
# 1: failure
print_lines_count()
{
local text="${1}"
"${WC_PATH}" --lines <<< "${text}" || return 1
return 0
}
# Prints text with replaced char
# Arguments:
# 1: source text
# 2: char to replace
# 3: replacing char
# Returns:
# 0: success
# 1: failure
replace_chars()
{
local text="${1}"
local from_char="${2}"
local to_char="${3}"
[[ -z "${text}" ]] && return 0
echo "${text}" | "${TR_PATH}" "${from_char}" "${to_char}" || return 1
return 0
}
# Prints text with replaced substring
# Arguments:
# 1: source text
# 2: substring to replace
# 3: replacing substring
# Returns:
# 0: success
# 1: failure
replace_string()
{
local text="${1}"
local from_string="${2}"
local to_string="${3}"
[[ -z "${text}" ]] && return 0
echo "${text}" | sed "s/${from_string}/${to_string}/g" || return 1
return 0
}
# Prints text uppercase
# Arguments:
# 1: text to uppercase
# Returns:
# 0: success
# 1: failure
print_uppercase()
{
local text="${1}"
replace_chars "${text}" '[:lower:]' '[:upper:]' || return 1
return 0
}
# Prints text lowercase
# Arguments:
# 1: text to lowercase
# Returns:
# 0: success
# 1: failure
print_lowercase()
{
local text="${1}"
replace_chars "${text}" '[:upper:]' '[:lower:]' || return 1
return 0
}
# Checks that scipt running as root
# Arguments:
# None
# Returns:
# 0: running as root
# 1: running as non-root user
test_root()
{
if [[ "${EUID}" != "0" ]] ; then
log "${E_ROOT_REQUIRED}"
return 1
fi
return 0
}
# Checks that application binaries is exists
# Arguments:
# @: paths or names of the application binaries
# Returns:
# 0: all application exists
# 1: one or more applications does not exists
test_app()
{
local app_list="${@}"
local app=""
[[ -z "${app_list}" ]] && return 0
for app in ${app_list} ; do
debug "Check application: '${app}'."
if ! command -v "${app}" &>/dev/null ; then
log "Application '${app}' does not exists."
return 1
fi
done
return 0
}
# Checks that specified packages are installed
# Arguments:
# @: names of the packages to check
# Returns:
# 0: all packages are installed
# 1: one or more packages are not installed
test_package()
{
local package_list="${@}"
local package=""
local status_string=""
[[ -z "${package_list}" ]] && return 0
for package in ${package_list} ; do
debug "Check package installed: '${package}'."
status_string="$(dpkg-query --show --showformat='${status}\n' "${package}")" 2>/dev/null || return 1
if ! echo "${status_string}" | grep "${PACKAGE_INSTALLED_STRING}" &>/dev/null ; then
debug "Package '${package}' does not installed."
return 1
fi
done
return 0
}
# Starts 'apt-get update' once
# Arguments:
# None
# Returns:
# 0: apt-get update (already) finished successfully
# 1: apt-get update failed
start_aptget_update()
{
if ! is_true "${IS_APTGET_UPDATE_COMPLETED}" ; then
debug "Start 'apt-get update' once."
if ! "${APTGET_PATH}" "update" ; then
log "${E_APTGET_UPDATE}"
return 1
fi
IS_APTGET_UPDATE_COMPLETED=1
debug "'apt-get update' finished successfully."
fi
return 0
}
# Starts 'apt-get install' with specified package list
# Arguments:
# @: names of the packages to install
# Returns:
# 0: success
# 1: failure
start_aptget_install()
{
local package_list="${@}"
local is_error=0
[[ -z "${package_list}" ]] && return 0
if [[ -z "${APTGET_ASSUME_YES}" ]] || [[ "${APTGET_ASSUME_YES}" == "0" ]] ; then
debug "Start 'apt-get install ${package_list}'."
"${APTGET_PATH}" "install" ${package_list} || is_error=1
else
debug "Start 'apt-get -y install ${package_list}'."
"${APTGET_PATH}" --assume-yes "install" ${package_list} || is_error=1
fi
if [[ "${is_error}" != 0 ]] ; then
log "${E_APTGET_INSTALL}"
return 1
fi
debug "Installed successfully: ${package_list}."
return 0
}
# Installs packages
# Arguments:
# @: names of the packages to install
# Returns:
# 0: success
# 1: failure
install_package()
{
local pkg="${@}"
[[ -z "${pkg}" ]] && return 0
debug "Install package: ${pkg}."
start_aptget_update || return 1
start_aptget_install "${pkg}" || return 1
return 0
}
# Check that the service is running
# Arguments:
# 1: service name
# Returns:
# 0: service is running
# 1: service not running
test_service()
{
local service_name="${1}"
[[ -z "${service_name}" ]] && return 0
debug "Test state of service '${service_name}'."
if ! "${SERVICE_PATH}" "${service_name}" status &>/dev/null ; then
debug "Service is stopped."
return 1
fi
debug "Service is running."
return 0
}
# Stops the service
# Arguments:
# 1: service name
# Returns:
# 0: stopped successfully
# 1: failure
stop_service()
{
local service_name="${1}"
[[ -z "${service_name}" ]] && return 0
if test_service "${service_name}" ; then
debug "Stop service '${service_name}'."
"${SERVICE_PATH}" "${service_name}" stop || return 1
debug "Stopped successfully."
fi
return 0
}
# Starts the service
# Arguments:
# 1: service name
# Returns:
# 0: started successfully
# 1: failure
start_service()
{
local service_name="${1}"
[[ -z "${service_name}" ]] && return 0
debug "Start service '${service_name}'."
"${SERVICE_PATH}" "${service_name}" restart || return 1
debug "Started successfully."
return 0
}
# Creates directories from file path
# Arguments:
# 1: path to file
# Returns:
# 0: success
# 1: failure
make_path_for_file()
{
local file_path="${1}"
local dir_path=""
[[ -z "${file_path}" ]] && return 0
dir_path="$("${DIRNAME_PATH}" "${file_path}")" || return 1
if [[ ! -d "${dir_path}" ]] ; then
debug "Create directory '${dir_path}'."
mkdir -p "${dir_path}" || return 1
fi
return 0
}
# Copies file to the backup directory
# Arguments:
# 1: path to file
# Returns:
# 0: success
# 1: failure
backup_file()
{
local src_file="${1}"
local dst_file="${BACKUP_DIR}/$(basename "${src_file}")"
[[ -z "${src_file}" ]] && return 0
[[ -f "${src_file}" ]] || return 0
debug "Backup file '${src_file}' to '${dst_file}'."
make_path_for_file "${dst_file}" || return 1
cp --backup=numbered "${src_file}" "${dst_file}" || return 1
return 0
}
# Writes a text data to the file and makes backup of file before that
# Arguments:
# 1: text to write
# 2: path to file
# Returns:
# 0: success
# 1: failure
write_to_file()
{
local text="${1}"
local dst_file="${2}"
[[ -z "${text}" ]] && return 0
[[ -z "${dst_file}" ]] && return 0
local lines_count="$(print_lines_count "${text}")"
backup_file "${dst_file}" || return 1
make_path_for_file "${dst_file}" || return 1
debug "Write data to the file '${dst_file}'."
echo "${text}" > "${dst_file}"
debug "Wrote ${lines_count} lines successfully."
return 0
}
# Prints name of the domain
# Arguments:
# None
# Returns:
# 0: success
# 1: failure
print_domain_name()
{
local domain_name=""
domain_name="$(dnsdomainname)" || return 1
if [[ -z "${domain_name}" ]] ; then
log "${E_DOMAIN_NAME_NOT_FOUND}"
return 1
fi
debug "Found domain '${domain_name}'."
echo "${domain_name}"
return 0
}
# Prints name of the current host as FQDN
# Arguments:
# 1: domain name
# Returns:
# 0: success
# 1: failure
print_host_fqdn()
{
local domain_name="${1}"
local host_name=""
[[ -z "${domain_name}" ]] && return 0
host_name="$(hostname --fqdn)" || return 1
if [[ ! "${host_name}" == *.${domain_name} ]] ; then
host_name="${host_name}.${domain_name}"
fi
debug "Found host FQDN '${host_name}'."
echo "${host_name}"
return 0
}
# Installs dnsutils package
# Arguments:
# None
# Returns:
# 0: success
# 1: failure
install_dnsutils()
{
test_package "dnsutils" || install_package "dnsutils" || return 1
test_app "${DIG_PATH}" || return 1
return 0
}
# Resolves DNS name to ip address
# Arguments:
# 1: name to resolve
# 2: DNS record type, A by default
# 3: DNS server that will be used for resolve, empty by default.
# system nameservers from /etc/resolv.conf will be used if empty.
# Returns:
# 0: success
# 1: failure
lookup_hostname()
{
local name="${1}"
local type="${2:-A}"
local server="${3}"
local output=""
[[ -z "${name}" ]] && return 0
if [[ -z "${server}" ]] ; then
if [[ "${type}" == "A" ]] ; then
if test_app "${DIG_PATH}" &>/dev/null ; then
debug "Lookup DNS record '${name}'."
output="$("${DIG_PATH}" "${name}" "${type}" +short +search +tries=${DIG_TRIES} +time=${DIG_TIMEOUT})" || return 1
else
debug "Lookup DNS record '${name}'."
output="$("${GETENT_PATH}" hosts "${name}" | "${AWK_PATH}" '{ print $1 }')"
fi
else
debug "Lookup DNS record '${name}', type ${type}."
output="$("${DIG_PATH}" "${name}" "${type}" +short +search +tries=${DIG_TRIES} +time=${DIG_TIMEOUT})" || return 1
fi
else
debug "Lookup DNS record '${name}', type ${type} by the server '$server'."
output="$("${DIG_PATH}" "@${server}" "${name}" "${type}" +short +search +tries=${DIG_TRIES} +time=${DIG_TIMEOUT})" || return 1
fi
if [[ -z "${output}" ]] ; then
log "$(printf "${E_DIG_EMPTY_RESPONSE}" "${name}" "${type}")"
return 1
fi
debug "Resolved successfully."
echo "${output}"
return 0
}
# Resolves ip address to DNS name
# Arguments:
# 1: ip address to resolve
# Returns:
# 0: success
# 1: failure
lookup_address()
{
local address="${1}"
local dig_output=""