-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpacefile.sh
More file actions
1221 lines (1051 loc) · 27.8 KB
/
Spacefile.sh
File metadata and controls
1221 lines (1051 loc) · 27.8 KB
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
#
# Copyright 2016-2017 Blockie AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#================================
# OS_DEP_INSTALL
#
# Check for dependencies
#
#================================
OS_DEP_INSTALL()
{
SPACE_DEP="PRINT"
PRINT "No particular dependencies." "ok"
}
#================
# OS_ID
#
# Get the OS identification and package manager.
#
# Expects:
# out_ostype
# out_ospkgmgr
# out_oshome - Users home dir.
# out_oscwd - Current CWD.
#
#================
OS_ID()
{
SPACE_DEP="OS_COMMAND"
out_ostype="gnu"
out_oshome="/home"
out_oscwd="$(pwd)"
out_ospkgmgr=
out_osinit="sysvinit"
if OS_COMMAND "systemctl" >/dev/null; then
out_osinit="systemd"
fi
if [ "${out_oscwd}" = "/" ]; then
# We'll conform the root directory to not end with slash,
# since other directories do not end with slash.
out_oscwd="/."
fi
[ -f "/etc/debian_version" ] && out_ospkgmgr="apt"
[ -f "/etc/arch-release" ] && out_ospkgmgr="pacman"
[ -d "/etc/yum" ] && out_ospkgmgr="yum"
[ -f "/etc/redhat-release" ] && out_ospkgmgr="yum"
[ -f "/etc/alpine-release" ] && { out_ospkgmgr="apk"; out_ostype="busybox"; }
# Some releases are more tricky.
if [ "${out_ospkgmgr}" = "" ]; then
if OS_COMMAND "apt-get" >/dev/null; then
out_ospkgmgr="apt"
elif OS_COMMAND "pacman" >/dev/null; then
out_ospkgmgr="pacman"
elif OS_COMMAND "yum" >/dev/null; then
out_ospkgmgr="yum"
elif OS_COMMAND "apk" >/dev/null; then
out_ospkgmgr="apk"
# We assume Alpine Linux runs on BusyBox.
# We could have a more fine grained check,
# but we would have to execute 'ls' a couple of times to do that.
out_ostype="busybox"
elif OS_COMMAND "brew" >/dev/null; then
out_ospkgmgr="brew"
out_ostype="darwin"
out_oshome="/Users"
out_osinit="launchd"
elif OS_COMMAND "pkg" >/dev/null; then
out_ospkgmgr="pkg"
out_ostype="FreeBSD"
out_osinit="rc"
fi
fi
return 0
}
# Disable warning about local keyword
# shellcheck disable=SC2039
#=============
# OS_INFO
#
# Show some information about the current OS.
#
#=============
OS_INFO()
{
SPACE_DEP="OS_ID PRINT"
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
PRINT "OS type: ${out_ostype}."
PRINT "OS init system: ${out_osinit}."
PRINT "OS package manager: ${out_ospkgmgr}."
PRINT "OS home directory: ${out_oshome}."
PRINT "Current directory: ${out_oscwd}."
}
# Disable warning about local keyword
# shellcheck disable=SC2039
#=============
# OS_IS_INSTALLED
#
# Check if program is installed.
# If not installed then install its package, if provided.
#
# Parameters:
# $1: program name
# $2: package name
#
# Returns:
# 0: program is available
# 0: program is not available and failed to install it
#
#=============
OS_IS_INSTALLED()
{
SPACE_SIGNATURE="program:1 [pkg]"
SPACE_DEP="OS_INSTALL_PKG _OS_PROGRAM_TRANSLATE PRINT OS_COMMAND"
local program="${1}"
shift
local pkg="${1-}"
shift $(( $# > 0 ? 1 : 0 ))
local program2="${program}"
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
_OS_PROGRAM_TRANSLATE
PRINT "Check if ${program} (originally ${program2}) is installed." "debug"
if OS_COMMAND "${program}" >/dev/null; then
PRINT "Available: ${program} is installed." "debug"
return 0
else
if [ "${pkg}" = "" ]; then
PRINT "Missing: ${program} is not installed."
return 1
fi
PRINT "Missing: ${program} is not installed. Attempting installation..." "info"
OS_INSTALL_PKG "${pkg}"
return $?
fi
}
# Disable warning about local keyword
# shellcheck disable=SC2039
#================
# _OS_PKG_TRANSLATE
#
# Translates Debian style package names into
# the current OS package manager naming.
#
# A translation could be translated into one or many
# package names.
#
# Expects:
# ${pkg}: package(s) name(s) to adjust
#
#================
_OS_PKG_TRANSLATE()
{
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
local pkg2="${pkg}"
local p=""
pkg=""
for p in ${pkg2}; do
# This function should be further added to
# to handle more packages.
if [ "${out_ospkgmgr}" = "apk" ]; then
if [ "${p}" = "openssh-server" ]; then
p="openssh"
elif [ "${p}" = "openssh-client" ]; then
p="openssh"
elif [ "${p}" = "libyaml-dev" ]; then
p="yaml-dev"
elif [ "${p}" = "libreadline-dev" ]; then
p="readline-dev"
elif [ "${p}" = "libncurses-dev" ]; then
p="ncurses-dev"
fi
elif [ "${out_ospkgmgr}" = "yum" ]; then
if [ "${p}" = "coreutils" ]; then
p="xtra-utils"
#elif [ "${p}" = "lua5.1" ]; then
#p="lua"
elif [ "${p}" = "openssh-client" ]; then
p="openssh-clients"
elif [ "${p}" = "libyaml-dev" ]; then
p="libyaml-devel"
elif [ "${p}" = "libc-dev" ]; then
p="glibc-devel glibc-headers"
#elif [ "${p}" = "lua5.1-dev" ]; then
#p="lua-devel"
elif [ "${p}" = "libreadline-dev" ]; then
p="readline-devel"
elif [ "${p}" = "libncurses-dev" ]; then
p="ncurses-devel"
fi
elif [ "${out_ospkgmgr}" = "pacman" ]; then
if [ "${p}" = "lua5.1" ]; then
p="lua51"
elif [ "${p}" = "lua5.2" ]; then
p="lua52"
elif [ "${p}" = "lua5.3" ]; then
p="lua" # NOTE: This is dependant on pacman version since one day "lua" will mean "lua5.4", which is not good.
elif [ "${p}" = "openssh-server" ]; then
p="openssh"
elif [ "${p}" = "openssh-client" ]; then
p="openssh"
elif [ "${p}" = "libyaml-dev" ]; then
p="libyaml"
elif [ "${p}" = "libc-dev" ]; then
p="linux-api-headers"
elif [ "${p}" = "lua5.1-dev" ]; then
p="lua51"
elif [ "${p}" = "libreadline-dev" ]; then
p="readline"
elif [ "${p}" = "libncurses-dev" ]; then
p="ncurses"
fi
fi
if [ -z "${p}" ]; then
continue
fi
pkg="${pkg:+$pkg }${p}"
done
}
# Disable warning about local keyword
# shellcheck disable=SC2039
#================
# _OS_PROGRAM_TRANSLATE
#
# Translates Debian style program names into
# the current OS distributions program naming.
#
# Expects:
# ${program}: program name to translate
#
#================
_OS_PROGRAM_TRANSLATE()
{
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
# This function should be further added to
# to handle more programs.
if [ "${out_ospkgmgr}" = "apk" ]; then
:
elif [ "${out_ospkgmgr}" = "yum" ]; then
if [ "${program}" = "lua5.1" ]; then
program="lua"
fi
elif [ "${out_ospkgmgr}" = "pacman" ]; then
:
fi
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
# Disable warning about local keyword
# shellcheck disable=SC2039
# Disable warning about ${pkg}: word splitting is intended
# shellcheck disable=SC2086
#================
# OS_INSTALL_PKG
#
# Install one or more packages.
#
# Give the Debian style name of packages
# and the function will attempt to translate
# it to the current package managers name for it.
#
# Parameters:
# $1: package(s) name(s)
#
# Returns:
# 0: success
# 1: failed to install package either due to unknown package manager or package name
#
#================
OS_INSTALL_PKG()
{
SPACE_SIGNATURE="pkg:1"
SPACE_DEP="OS_ID _OS_PKG_TRANSLATE OS_UPDATE PRINT"
local pkg="${1}"
shift
PRINT "Install pkg(s) (untranslated): ${pkg}." "debug"
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
_OS_PKG_TRANSLATE
if [ "${pkg}" = "" ]; then
PRINT "Package has no target for pkg mgr: ${out_ospkgmgr}."
return 0
fi
PRINT "Install package(s) using ${out_ospkgmgr}: ${pkg}." "info"
if [ "${out_ospkgmgr}" = "apt" ]; then
apt-get -y install ${pkg}
if [ "$?" -eq 100 ]; then
OS_UPDATE
apt-get -y install ${pkg}
fi
elif [ "${out_ospkgmgr}" = "pacman" ]; then
pacman -Syu --noconfirm ${pkg}
if [ "$?" -gt 0 ]; then
OS_UPDATE
pacman -Syu --noconfirm ${pkg}
fi
elif [ "${out_ospkgmgr}" = "yum" ]; then
yum -y install ${pkg}
elif [ "${out_ospkgmgr}" = "apk" ]; then
apk add ${pkg}
if [ "$?" -gt 0 ]; then
OS_UPDATE
apk add ${pkg}
fi
elif [ "${out_ospkgmgr}" = "brew" ]; then
brew install ${pkg}
if [ "$?" -gt 0 ]; then
OS_UPDATE
brew install ${pkg}
fi
elif [ "${out_ospkgmgr}" = "pkg" ]; then
pkg install ${pkg}
if [ "$?" -gt 0 ]; then
OS_UPDATE
pkg install ${pkg}
fi
else
PRINT "Could not determine what package manager is being used in the OS." "error"
return 1
fi
if [ "$?" -gt 0 ]; then
PRINT "Could not install ${pkg}." "error"
return 1
fi
PRINT "Package(s): ${pkg}, installed successfully!" "ok"
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
# Disable warning about local keyword
# shellcheck disable=SC2039
#=======================
# OS_UPDATE
#
# Update the system package lists.
#
#=======================
OS_UPDATE()
{
SPACE_DEP="OS_ID PRINT"
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
PRINT "Update package lists." "info"
if [ "${out_ospkgmgr}" = "apt" ]; then
apt-get update -y
elif [ "${out_ospkgmgr}" = "pacman" ]; then
pacman --noconfirm -Sy
elif [ "${out_ospkgmgr}" = "yum" ]; then
:
elif [ "${out_ospkgmgr}" = "apk" ]; then
apk update
elif [ "${out_ospkgmgr}" = "brew" ]; then
brew update
elif [ "${out_ospkgmgr}" = "pkg" ]; then
pkg update
else
PRINT "Could not determine what package manager is being used in the OS." "error"
return 1
fi
if [ "$?" -gt 0 ]; then
PRINT "Could not update package lists." "error"
return 1
fi
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
# Disable warning about local keyword
# shellcheck disable=SC2039
#=======================
# OS_UPGRADE
#
# Upgrade the system.
#
# Returns:
# Non-zero on error.
#
#=======================
OS_UPGRADE()
{
SPACE_DEP="OS_ID OS_UPDATE PRINT"
OS_UPDATE
if [ "$?" -gt 0 ]; then
return 1
fi
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
PRINT "Upgrade OS..." "info"
if [ "${out_ospkgmgr}" = "apt" ]; then
apt-get -y upgrade
elif [ "${out_ospkgmgr}" = "pacman" ]; then
pacman --noconfirm -Syu
elif [ "${out_ospkgmgr}" = "yum" ]; then
yum update -y
elif [ "${out_ospkgmgr}" = "apk" ]; then
apk upgrade
elif [ "${out_ospkgmgr}" = "brew" ]; then
brew upgrade
elif [ "${out_ospkgmgr}" = "pkg" ]; then
pkg upgrade
else
PRINT "Could not determine what package manager is being used in the OS." "error"
return 1
fi
if [ "$?" -gt 0 ]; then
PRINT "Could not upgrade OS." "error"
return 1
fi
}
# Disable warning about local keyword
# shellcheck disable=SC2039
#=======================
# OS_SERVICE
#
# Control a service.
#
# Parameters:
# $1: service name
# $2: action
#
# Returns:
# 0: success
# 1: failure
#
#=======================
OS_SERVICE()
{
SPACE_SIGNATURE="service:1 action:1"
SPACE_DEP="PRINT OS_ID"
local service="${1}"
shift
local action="${1}"
shift
PRINT "Service ${service}, ${action}." "info"
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
if [ "${out_osinit}" = "systemd" ]; then
systemctl "${action}" "${service}"
elif [ "${out_osinit}" = "sysvinit" ]; then
"/etc/init.d/${service}" "${action}"
elif [ "${out_osinit}" = "launchd" ]; then
launchctl "${action}" "${service}"
elif [ "${out_osinit}" = "rc" ]; then
"/etc/rc.d/${service}" "${action}"
else
PRINT "Could not determine what init service is being used in the OS." "error"
return 1
fi
}
# Disable warning about local keyword
# shellcheck disable=SC2039
#=======================
# OS_REBOOT
#
# Reboot the system.
#
#=======================
OS_REBOOT()
{
SPACE_DEP="PRINT OS_ID"
PRINT "Reboot system now." "info"
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
if [ "${out_osinit}" = "systemd" ]; then
systemctl reboot
elif [ "${out_osinit}" = "sysvinit" ] \
|| [ "${out_osinit}" = "launchd" ] \
|| [ "${out_osinit}" = "rc" ]; then
reboot now
else
PRINT "Could not determine what init service is being used in the OS." "error"
return 1
fi
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
#=======================
# OS_USER_EXIST
#
# Check if a user does exist.
#
# Parameters:
# $1: the name of the user to lookup.
#
# Returns:
# 0: users does exist
# 1: failure. User does NOT exist
#
#=======================
OS_USER_EXIST()
{
SPACE_SIGNATURE="targetuser:1"
SPACE_DEP="PRINT"
# shellcheck disable=SC2039
local targetuser="${1}"
shift
PRINT "Check if user ${targetuser} exists." "debug"
id -u "${targetuser}" >/dev/null 2>&1
if [ "$?" -eq 0 ]; then
PRINT "User does exist." "debug"
return 0
else
PRINT "User does not exist." "debug"
return 1
fi
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
#=======================
# OS_GROUP_EXIST
#
# Check if a group does exist.
#
# Parameters:
# $1: the name of the group to lookup.
#
# Returns:
# 0: group does exist
# 1: failure. Group does NOT exist
#
#=======================
OS_GROUP_EXIST()
{
SPACE_SIGNATURE="group:1"
SPACE_DEP="PRINT FILE_GREP"
# shellcheck disable=SC2039
local group="${1}"
shift
PRINT "Check if group ${group} exists." "debug"
FILE_GREP "^${group}:.*\$" "/etc/group" "1" "ge" >/dev/null
if [ "$?" -eq 0 ]; then
PRINT "Group does exist." "debug"
return 0
else
PRINT "Group does not exist." "debug"
return 1
fi
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
# Disable warning about local keyword
# shellcheck disable=SC2039
#=======================
# OS_CREATE_USER
#
# Create passwordless user and install ssh key for it.
#
# Parameters:
# $1: The name of the user to create.
# $2: Path of the pub key file to install for the user. The contents will be passed over stdin.
#
# Returns:
# 0: success
# 1: failure
#
#=======================
OS_CREATE_USER()
{
SPACE_SIGNATURE="targetuser sshpubkeyfile"
SPACE_REDIR="<${2}"
SPACE_DEP="_OS_CREATE_USER"
local targetuser="${1}"
shift
_OS_CREATE_USER "${targetuser}"
}
# Helper function to OS_CREATE_USER
_OS_CREATE_USER()
{
SPACE_SIGNATURE="targetuser"
SPACE_DEP="PRINT FILE_CHMOD FILE_MKDIRP FILE_ROW_PERSIST FILE_CHOWNR OS_ID OS_ADD_USER OS_USER_EXIST"
local targetuser="${1}"
shift
PRINT "Create ${targetuser}." "debug"
# shellcheck disable=2034
local out_ostype=''
local out_ospkgmgr=''
local out_oshome=''
local out_oscwd=''
local out_osinit=''
OS_ID
# Read pub key from stdin
local keycontents="$(cat)"
local home="${out_oshome}/${targetuser}"
if ! OS_USER_EXIST "${targetuser}"; then
OS_ADD_USER "${targetuser}" "${home}"
fi &&
FILE_CHMOD "700" "${home}" &&
FILE_MKDIRP "${home}/.ssh" &&
FILE_CHMOD "700" "${home}/.ssh" &&
FILE_ROW_PERSIST "${keycontents}" "${home}/.ssh/authorized_keys" &&
FILE_CHMOD "600" "${home}/.ssh/authorized_keys" &&
FILE_CHOWNR "${targetuser}:${targetuser}" "${home}/.ssh"
if [ "$?" -gt 0 ]; then
PRINT "Could not create user: ${targetuser}." "error"
return 1
fi
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
# Disable warning about local keyword
# shellcheck disable=SC2039
#=======================
# OS_ADD_USER
#
# Add a user, with a home directory.
#
# Parameters:
# $1: user name
# $2: home path directory
# $3: shell path
#
# Returns:
# 0: success
# 1: failed to call useradd/adduser
#
#=======================
OS_ADD_USER()
{
SPACE_SIGNATURE="user:1 home:1 [shell]"
SPACE_DEP="OS_COMMAND"
local targetuser="${1}"
shift
local home="${1}"
shift
local shpath="${1-}"
shift $(( $# > 0 ? 1 : 0 ))
if [ "${shpath}" = "" ]; then
shpath="$(OS_COMMAND bash)"
if [ "$?" -gt 0 ]; then
shpath="$(OS_COMMAND sh)"
fi
fi
if OS_COMMAND useradd >/dev/null; then
useradd -m -d "${home}" -s "${shpath}" -U "${targetuser}"
elif OS_COMMAND adduser >/dev/null; then
adduser -D -h "${home}" -s "${shpath}" "${targetuser}"
else
PRINT "No useradd/adduser installed." "error"
return 1
fi
}
#=======================
# OS_ADD_GROUP
#
# Add a group to the system
#
# Parameters:
# $1: group name
#
# Returns:
# 0: success
# 1: failed to call groupadd
#
#=======================
OS_ADD_GROUP()
{
SPACE_SIGNATURE="group"
SPACE_DEP="OS_COMMAND"
local group="${1}"
shift
if OS_COMMAND groupadd >/dev/null; then
groupadd "${group}"
else
PRINT "No groupadd installed." "error"
return 1
fi
}
# Disable warning about local keyword
# shellcheck disable=SC2039
#===========
# OS_COMMAND
#
# Parameters:
# $1: command to look for
#
# Return:
# Same as 'command'
#
#===========
OS_COMMAND()
{
SPACE_SIGNATURE="command:1"
local cmd="${1}"
shift
command -v ${cmd} >/dev/null
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
# Disable warning about local keyword
# shellcheck disable=SC2039
#=======================
# OS_MKSUDO_USER
#
# Make a user passwordless SUDO.
#
# Parameters:
# $1: The name of the user to make into sudo user.
#
# Returns:
# 0: success
# 1: failure
#
#=======================
OS_MKSUDO_USER()
{
SPACE_SIGNATURE="targetuser:1"
SPACE_DEP="PRINT FILE_ROW_PERSIST OS_USER_ADD_GROUP OS_GROUP_EXIST"
local targetuser="${1}"
shift
PRINT "mksudo ${targetuser}." "debug"
OS_GROUP_EXIST "sudo"
if [ "$?" -eq 0 ]; then
PRINT "Add user to sudo group." "debug"
OS_USER_ADD_GROUP "${targetuser}" "sudo"
fi
FILE_ROW_PERSIST "${targetuser} ALL=(ALL:ALL) NOPASSWD: ALL" "/etc/sudoers"
if [ "$?" -gt 0 ]; then
PRINT "Could not mksudo user: ${targetuser}." "error"
return 1
fi
}
# Disable warning about local keyword
# shellcheck disable=SC2039
#=======================
# OS_USER_ADD_GROUP
#
# Add a given user to a group
#
# Parameters:
# $1: user name
# $2: group name
#
# Returns:
# Non-zero on error.
#
#=======================
OS_USER_ADD_GROUP()
{
SPACE_SIGNATURE="targetuser:1 group:1"
SPACE_DEP="OS_COMMAND PRINT"
local targetuser="${1}"
shift
local group="${1}"
shift
if OS_COMMAND usermod >/dev/null; then
PRINT "User usermod -Ag to add the user: '${targetuser}' to the group: '${group}'" "debug"
usermod -aG "${group}" "${targetuser}"
elif OS_COMMAND addgroup >/dev/null; then
addgroup "${targetuser}" "${group}"
else
PRINT "Nor 'usermod' nor 'addgroup' found, can't add user to group." "error"
return 1
fi
}
#=======================
# OS_MOTD
#
# Copy a motd file into the system.
# Message of the day is the greeting text that shows when you login.
#
# Parameters;
# $1: The path of the motd file to upload.
#
#=======================
OS_MOTD()
{
SPACE_SIGNATURE="motdfile:1"
# shellcheck disable=2034
SPACE_REDIR="<${1}"
SPACE_DEP="FILE_PIPE_WRITE PRINT"
# shellcheck disable=SC2039
local motdfile="${1}"
shift
PRINT "Replace /etc/motd with ${motdfile}." "debug"
FILE_PIPE_WRITE "/etc/motd"
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
#=======================
# OS_DISABLE_ROOT
#
# Disable root from logging in both via ssh and physically.
#
# Returns:
# 0: success
# 1: failure
#
#=======================
OS_DISABLE_ROOT()
{
SPACE_DEP="PRINT FILE_SED FILE_ROW_PERSIST OS_SERVICE"
PRINT "Inactivating root account." "debug"
#if [ "$(id -u)" -eq 0 ]; then
#PRINT "Do not run this as root." "error"
#return 1
#fi
passwd -d root &&
FILE_SED "s/^PermitRootLogin.*$/PermitRootLogin no/g" "/etc/ssh/sshd_config" &&
FILE_ROW_PERSIST "PermitRootLogin no" "/etc/ssh/sshd_config" &&
OS_SERVICE "sshd" "reload"
if [ "$?" -gt 0 ]; then
PRINT "Could not inactivate root." "error"
return 1
fi
}
# Disable warning about indirectly checking status code
# shellcheck disable=SC2181
# Disable warning about local keyword
# shellcheck disable=SC2039
#=======================
# OS_DISABLE_USER
#
# Disable a user from logging in both via ssh and physically.