-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisam
executable file
·1562 lines (1272 loc) · 49.9 KB
/
lisam
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/sh
#==========================================================#
# Linux Service Account Manager (LiSAM) #
# v2.0 #
#==========================================================#
# #
# Users created by this script will: #
# - Have user systemd service functions #
# (with `loginctl enable-linger` #
# and `export XDG_RUNTIME_DIR`) #
# - Have limited system resources, configured by #
# systemd resource control (cgroups) #
# - (Optional) Get open ports for the account #
# (with `firewalld`) #
# - Ignored if `firewalld` is not installed #
# #
#==========================================================#
# Author: Kim Hwiwon < kim.hwiwon@outlook.com > #
#==========================================================#
#
# usage: lisam <MODE> [ARGS]... [<OPT> <OPT_VAL>]...
#
#
# <MODE>
#
# - add: create a new LiSAM service account
# $ lisam add <account-name> [OPTIONS]...
# [OPTIONS]
# -u, --uid < # > : Linux account UID
# -c, --cpu-weight < 1-10000 | idle > : Systemd CPUWeight
# -C, --cpu-quota < #% > : Systemd CPUQuota
# -m, --memory-max < #[K|M|G|T] | 0%-100% > : Systemd MemoryMax
# -M, --memory-high < #[K|M|G|T] | 0%-100% > : Systemd MemoryHigh
# -s, --swap-max < #[K|M|G|T] | 0%-100% > : Systemd MemorySwapMax
# -z, --zswap-max < #[K|M|G|T] | 0%-100% > : Systemd MemoryZSwapMax
# -t, --tasks-max < # > : Systemd TasksMax
# -i, --io-weight < 1-10000 > : Systemd IOWeight
# -d, --dev-allowed < ${DEV_PATH} > : Systemd DeviceAllow
# * Pass multiple options to set more than one device path
# -p, --sock-allowed < 1-65535 > : Systemd SockBindAllow
# * Pass multiple options to set more than one
#
# - mod: modify a LiSAM service account
# $ lisam mod <account-name> [OPTIONS]...
# [OPTIONS]
# -c, --cpu-weight < 1-10000 | idle > : Systemd CPUWeight
# -C, --cpu-quota < #% > : Systemd CPUQuota
# -m, --memory-max < #[K|M|G|T] | 0%-100% > : Systemd MemoryMax
# -M, --memory-high < #[K|M|G|T] | 0%-100% > : Systemd MemoryHigh
# -s, --swap-max < #[K|M|G|T] | 0%-100% > : Systemd MemorySwapMax
# -z, --zswap-max < #[K|M|G|T] | 0%-100% > : Systemd MemoryZSwapMax
# -t, --tasks-max < # > : Systemd TasksMax
# -i, --io-weight < 1-10000 > : Systemd IOWeight
# -d, --dev-allowed < ${DEV_PATH} > : Systemd DeviceAllow
# * Pass multiple options to set more than one device path
# -p, --sock-allowed < 1-65535 > : Systemd SockBindAllow
# * Pass multiple options to set more than one
#
# - ls: list all LiSAM service accounts
# $ lisam ls
#
# - detail: print details of an LiSAM service account
# $ lisam detail <account-name>
#
# - rm: remove an LiSAM service account
# $ lisam rm <account-name> [OPTIONS]...
# [OPTIONS]
# -f, --force : Force remove without prompt
#
# - usage: print usage message
# $ lisam usage
#
# - help: print help message
# $ lisam help
#
#
# [OPT_VAL] Special Patterns
#
# - <X> : Required wrapper
# - [X] : Optional wrapper (X can be omitted)
# - # : Arbitrary digits (positive integer)
# - ${X} : Arbitrary string (X is a description of the string)
# - A|B : A or B
# - A-B : Value range from A to B (inclusive)
#
#==========================================================#
# environments #
#==========================================================#
########## envs ##########
# configurable
UID_LOWER_LIMIT_INC="${UID_LOWER_LIMIT_INC:-30000}"
UID_UPPER_LIMIT_EXC="${UID_UPPER_LIMIT_EXC:-40000}"
LISAM_COLORED="${LISAM_COLORED:-}"
TERM_WIDTH="${TERM_WIDTH:-60}"
LIST_SPACES="${LIST_SPACES:-3}"
# fixed
BIN_BASENAME="$(basename "${0}")"
ACC_HOME_BASEDIR="/home"
LISAM_FILE_PREFIX="LiSAM" # no dash ('-')
LISAM_ACC_COMMENT_PREFIX="[LiSAM]" # no space (' ')
USER_SLICE_PREFIX="user-"
USER_SLICE_POSTFIX=".slice"
SYSTEMD_RC_DIR="/etc/systemd/system.control"
SYSTEMD_USER_SLICE_PREFIX="${SYSTEMD_RC_DIR}/user-"
SYSTEMD_USER_SLICE_POSTFIX=".slice.d"
SYSTEMD_USER_SERVICE_PREFIX="${SYSTEMD_RC_DIR}/user@"
SYSTEMD_USER_SERVICE_POSTFIX=".service.d"
# etc
CGROUP_USER_SLICE_ROOT="/sys/fs/cgroup/user.slice"
#==========================================================#
# systemd resource control vars #
#==========================================================#
# cache content for get_rc() func
unset USER_SLICE_RC_CONTENT
########## systemd RC accountings ##########
# shellcheck disable=SC2034
USER_CPU__LABEL="CPUAccounting"
# shellcheck disable=SC2034
USER_MEM__LABEL="MemoryAccounting"
# shellcheck disable=SC2034
USER_TASKS__LABEL="TasksAccounting"
# shellcheck disable=SC2034
USER_IO__LABEL="IOAccounting"
# shellcheck disable=SC2034
USER_NETWORK__LABEL="IPAccounting"
########## systemd resource control consts keys ##########
# shellcheck disable=SC2034
USER_SOCK_DENY__LABEL="SocketBindDeny"
# shellcheck disable=SC2034
USER_DEV_POLICY__LABEL="DevicePolicy"
# shellcheck disable=SC2034
USER_OOM_SWAP__LABEL="ManagedOOMSwap"
# shellcheck disable=SC2034
USER_OOM_MEM_PRESSURE__LABEL="ManagedOOMMemoryPressure"
# default values
# shellcheck disable=SC2034
USER_SOCK_DENY__DEFAULT="any"
# shellcheck disable=SC2034
USER_DEV_POLICY__DEFAULT="closed"
# shellcheck disable=SC2034
USER_OOM_SWAP__DEFAULT="kill"
# shellcheck disable=SC2034
USER_OOM_MEM_PRESSURE__DEFAULT="kill"
########## systemd resource control configurable keys & vars ##########
# shellcheck disable=SC2034
USER_CPU_WEIGHT__LABEL="CPUWeight"
# shellcheck disable=SC2034
USER_CPU_WEIGHT__CNT=1
unset USER_CPU_WEIGHT
# shellcheck disable=SC2034
USER_CPU_QUOTA__LABEL="CPUQuota"
# shellcheck disable=SC2034
USER_CPU_QUOTA__CNT=1
unset USER_CPU_QUOTA
# shellcheck disable=SC2034
USER_MEM_MAX__LABEL="MemoryMax"
# shellcheck disable=SC2034
USER_MEM_MAX__CNT=1
unset USER_MEM_MAX
# shellcheck disable=SC2034
USER_MEM_HIGH__LABEL="MemoryHigh"
# shellcheck disable=SC2034
USER_MEM_HIGH__CNT=1
unset USER_MEM_HIGH
# shellcheck disable=SC2034
USER_SWAP_MAX__LABEL="MemorySwapMax"
# shellcheck disable=SC2034
USER_SWAP_MAX__CNT=1
unset USER_SWAP_MAX
# shellcheck disable=SC2034
USER_ZSWAP_MAX__LABEL="MemoryZSwapMax"
# shellcheck disable=SC2034
USER_ZSWAP_MAX__CNT=1
# shellcheck disable=SC2034
USER_ZSWAP_MAX__OPTIONAL=1
unset USER_ZSWAP_MAX
# shellcheck disable=SC2034
USER_TASKS_MAX__LABEL="TasksMax"
# shellcheck disable=SC2034
USER_TASKS_MAX__CNT=1
unset USER_TASKS_MAX
# shellcheck disable=SC2034
USER_IO_WEIGHT__LABEL="IOWeight"
# shellcheck disable=SC2034
USER_IO_WEIGHT__CNT=1
unset USER_IO_WEIGHT
# shellcheck disable=SC2034
USER_DEV_ALLOW__LABEL="DeviceAllow"
# shellcheck disable=SC2034
USER_DEV_ALLOW__CNT=
unset USER_DEV_ALLOW
# shellcheck disable=SC2034
USER_SOCK_ALLOW__LABEL="SocketBindAllow"
# shellcheck disable=SC2034
USER_SOCK_ALLOW__CNT=
unset USER_SOCK_ALLOW
# shellcheck disable=SC2034
USER_CPU_WEIGHT__CURFILE="cpu.weight"
# shellcheck disable=SC2034
USER_CPU_QUOTA__CURFILE="cpu.max"
# shellcheck disable=SC2034
USER_MEM_MAX__CURFILE="memory.max"
# shellcheck disable=SC2034
USER_MEM_HIGH__CURFILE="memory.high"
# shellcheck disable=SC2034
USER_SWAP_MAX__CURFILE="memory.swap.max"
# shellcheck disable=SC2034
USER_ZSWAP_MAX__CURFILE="memory.zswap.max"
# shellcheck disable=SC2034
USER_TASKS_MAX__CURFILE="pids.max"
# shellcheck disable=SC2034
USER_IO_WEIGHT__CURFILE="io.weight io.bfq.weight"
# default values
# shellcheck disable=SC2034
USER_CPU_WEIGHT__DEFAULT="20"
# shellcheck disable=SC2034
USER_CPU_QUOTA__DEFAULT=""
# shellcheck disable=SC2034
USER_MEM_MAX__DEFAULT="50%"
# shellcheck disable=SC2034
USER_MEM_HIGH__DEFAULT=""
# shellcheck disable=SC2034
USER_SWAP_MAX__DEFAULT="0"
# shellcheck disable=SC2034
USER_ZSWAP_MAX__DEFAULT="0"
# shellcheck disable=SC2034
USER_TASKS_MAX__DEFAULT="1000"
# shellcheck disable=SC2034
USER_IO_WEIGHT__DEFAULT="20"
# shellcheck disable=SC2034
USER_DEV_ALLOW__DEFAULT=""
# shellcheck disable=SC2034
USER_SOCK_ALLOW__DEFAULT=""
# default command
# shellcheck disable=SC2016,SC2034
USER_MEM_HIGH__DEFAULT_CMD='
__USER_MEM_HIGH__DEFAULT_CMD__mem_max="$(get_var_default "USER_MEM_MAX")" || exit 1;
__USER_MEM_HIGH__DEFAULT_CMD__mem_max_n="${__USER_MEM_HIGH__DEFAULT_CMD__mem_max%%[^[:digit:]]*}" || exit 1;
printf "%s%s" "$((__USER_MEM_HIGH__DEFAULT_CMD__mem_max_n * 9 / 10))" "${__USER_MEM_HIGH__DEFAULT_CMD__mem_max#${__USER_MEM_HIGH__DEFAULT_CMD__mem_max_n}}" || exit 1;'
########## var list for dynamic var read ##########
# all rc configurable key list
SYSTEMD_RC_LIST="USER_CPU_WEIGHT USER_CPU_QUOTA \
USER_MEM_MAX USER_MEM_HIGH USER_SWAP_MAX USER_ZSWAP_MAX \
USER_TASKS_MAX USER_IO_WEIGHT USER_DEV_ALLOW USER_SOCK_ALLOW"
# all rc accounting list
SYSTEMD_RC_ACCOUNTING_LIST="USER_CPU USER_MEM USER_TASKS \
USER_IO USER_NETWORK"
# all rc const key list
SYSTEMD_RC_CONST_LIST="USER_SOCK_DENY USER_DEV_POLICY \
USER_OOM_SWAP USER_OOM_MEM_PRESSURE"
#==========================================================#
# main routine #
#==========================================================#
# main function - choose mode
# local prefix: M
# args: full argument list of the current command
main() {
set_colors
# script mode
_M__MODE="${1}"
if [ "$#" -gt 0 ]; then shift 2>/dev/null; fi
case "${_M__MODE}" in
"add") mode_add "${@}" ;;
"mod") mode_mod "${@}" ;;
"ls") mode_ls "${@}" ;;
"detail") mode_detail "${@}" ;;
"rm") mode_rm "${@}" ;;
"usage") printf "%s\n" "$(print_usage)" ;;
"help") printf "%s\n" "$(print_help)" ;;
"") abort_usage "Missing command verb" ;;
*) abort_usage "Invalid command verb: '${_M__MODE}'" ;;
esac
}
# [add] mode
# local prefix: MA
# args: <acc-name> [args]...
mode_add() {
# parse all opts and args
parse_opts "${BIN_BASENAME}" "u:c:C:m:M:s:z:t:i:d:p:" "uid:,cpu-weight:,cpu-quota:,memory-max:,memory-high:,swap-max:,zswap-max:,tasks-max:,io-weight:,dev-allowed:,sock-allowed:" "${@}" \
&& eval set -- "${PARSE_OPTS__args}" || return 1
# parsed opts
# shellcheck disable=SC2034
USER_UID="${PARSE_OPTS__opt__uid:-${PARSE_OPTS__opt_u}}"
# shellcheck disable=SC2034
USER_CPU_WEIGHT="${PARSE_OPTS__opt__cpu_weight:-${PARSE_OPTS__opt_c}}"
# shellcheck disable=SC2034
USER_CPU_QUOTA="${PARSE_OPTS__opt__cpu_quota:-${PARSE_OPTS__opt_C}}"
# shellcheck disable=SC2034
USER_MEM_MAX="${PARSE_OPTS__opt__memory_max:-${PARSE_OPTS__opt_m}}"
# shellcheck disable=SC2034
USER_MEM_HIGH="${PARSE_OPTS__opt__memory_high:-${PARSE_OPTS__opt_M}}"
# shellcheck disable=SC2034
USER_SWAP_MAX="${PARSE_OPTS__opt__swap_max:-${PARSE_OPTS__opt_s}}"
# shellcheck disable=SC2034
USER_ZSWAP_MAX="${PARSE_OPTS__opt__zswap_max:-${PARSE_OPTS__opt_z}}"
# shellcheck disable=SC2034
USER_TASKS_MAX="${PARSE_OPTS__opt__tasks_max:-${PARSE_OPTS__opt_t}}"
# shellcheck disable=SC2034
USER_IO_WEIGHT="${PARSE_OPTS__opt__io_weight:-${PARSE_OPTS__opt_i}}"
# shellcheck disable=SC2034
USER_DEV_ALLOW="${PARSE_OPTS__opt__dev_allowed:+${PARSE_OPTS__opt__dev_allowed}
}${PARSE_OPTS__opt_d:-}"
# shellcheck disable=SC2034
USER_SOCK_ALLOW="${PARSE_OPTS__opt__sock_allowed:+${PARSE_OPTS__opt__sock_allowed}
}${PARSE_OPTS__opt_p:-}"
# linux account name
_MA__acc_name="${1}"
if [ -z "${_MA__acc_name}" ]; \
then abort_usage "Missing linux user account name"; fi
if getent passwd "${_MA__acc_name}" >/dev/null 2>/dev/null; \
then abort "Linux user '${_MA__acc_name}' already exists"; fi
if getent group "${_MA__acc_name}" >/dev/null 2>/dev/null; \
then abort "Linux user group '${_MA__acc_name}' already exists"; fi
_MA__acc_home="${ACC_HOME_BASEDIR}/.${LISAM_FILE_PREFIX}.${_MA__acc_name}"
if [ -e "${_MA__acc_home}" ]; \
then abort "Home directory '${_MA__acc_home}' already exists"; fi
_MA__acc_id="$(search_empty_user_id "${USER_UID}" "$((USER_UID + 1))")" \
|| exit 1
_MA__firewalld_svc_name="${LISAM_FILE_PREFIX}-${_MA__acc_name}"
shift 2>/dev/null
test_root_permission
new_svc_acc "${_MA__acc_name}" "${_MA__acc_home}" "${_MA__acc_id}" \
|| {
rm_svc_acc "${_MA__acc_name}" >/dev/null
rm_acc_rc "${_MA__acc_id}" >/dev/null
abort "Failed to create a new service account"
}
# add all data
refresh_acc_rc "${_MA__acc_id}" || {
rm_svc_acc "${_MA__acc_name}" >/dev/null
rm_acc_rc "${_MA__acc_id}" >/dev/null
abort "Failed to create new resource control configs"
}
sudo loginctl enable-linger "${_MA__acc_name}" \
|| {
rm_svc_acc "${_MA__acc_name}" >/dev/null
rm_acc_rc "${_MA__acc_id}" >/dev/null
abort "Failed to enable linger"
}
if command -v firewall-cmd >/dev/null
then
new_firewall_svc "${_MA__firewalld_svc_name}" \
|| {
rm_firewall_svc "${_MA__firewalld_svc_name}" >/dev/null
rm_svc_acc "${_MA__acc_name}" >/dev/null
rm_acc_rc "${_MA__acc_id}" >/dev/null
abort "Failed to create a new firewalld service"
}
fi
# print info
mode_detail "${_MA__acc_name}"
}
# [mod] mode
# local prefix: MM
# args: <acc-name> [args]...
mode_mod() {
# parse all opts and args
parse_opts "${BIN_BASENAME}" "c:C:m:M:s:z:t:i:d:p:" "cpu-weight:,cpu-quota:,memory-max:,memory-high:,swap-max:,zswap-max:,tasks-max:,io-weight:,dev-allowed:,sock-allowed:" "${@}" \
&& eval set -- "${PARSE_OPTS__args}" || return 1
# parsed opts
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__cpu_weight && ! if_var_set PARSE_OPTS__opt_c \
|| USER_CPU_WEIGHT="${PARSE_OPTS__opt__cpu_weight:-${PARSE_OPTS__opt_c}}"
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__cpu_quota && ! if_var_set PARSE_OPTS__opt_C \
|| USER_CPU_QUOTA="${PARSE_OPTS__opt__cpu_quota:-${PARSE_OPTS__opt_C}}"
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__memory_max && ! if_var_set PARSE_OPTS__opt_m \
|| USER_MEM_MAX="${PARSE_OPTS__opt__memory_max:-${PARSE_OPTS__opt_m}}"
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__memory_high && ! if_var_set PARSE_OPTS__opt_M \
|| USER_MEM_HIGH="${PARSE_OPTS__opt__memory_high:-${PARSE_OPTS__opt_M}}"
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__swap_max && ! if_var_set PARSE_OPTS__opt_s \
|| USER_SWAP_MAX="${PARSE_OPTS__opt__swap_max:-${PARSE_OPTS__opt_s}}"
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__zswap_max && ! if_var_set PARSE_OPTS__opt_z \
|| USER_ZSWAP_MAX="${PARSE_OPTS__opt__zswap_max:-${PARSE_OPTS__opt_z}}"
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__tasks_max && ! if_var_set PARSE_OPTS__opt_t \
|| USER_TASKS_MAX="${PARSE_OPTS__opt__tasks_max:-${PARSE_OPTS__opt_t}}"
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__io_weight && ! if_var_set PARSE_OPTS__opt_i \
|| USER_IO_WEIGHT="${PARSE_OPTS__opt__io_weight:-${PARSE_OPTS__opt_i}}"
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__dev_allowed && ! if_var_set PARSE_OPTS__opt_d \
|| USER_DEV_ALLOW="${PARSE_OPTS__opt__dev_allowed:+${PARSE_OPTS__opt__dev_allowed}
}${PARSE_OPTS__opt_d:-}"
# shellcheck disable=SC2034
! if_var_set PARSE_OPTS__opt__sock_allowed && ! if_var_set PARSE_OPTS__opt_p \
|| USER_SOCK_ALLOW="${PARSE_OPTS__opt__sock_allowed:+${PARSE_OPTS__opt__sock_allowed}
}${PARSE_OPTS__opt_p:-}"
# linux account name
_MM__acc_name="${1}"
if [ -z "${_MM__acc_name}" ]; \
then abort_usage "Missing linux user account name"; fi
if ! _MM__acc_passwd_ent="$(getent passwd "${_MM__acc_name}")" \
>/dev/null 2>/dev/null; \
then abort "No linux user '${_MM__acc_name}' found"; fi
if ! printf "%s" "${_MM__acc_passwd_ent}" \
| cut -d: -f5 \
| grep -F "${LISAM_ACC_COMMENT_PREFIX}" \
>/dev/null 2>/dev/null
then abort "Linux user '${_MM__acc_name}' is not an LiSAM account"; fi
_MM__acc_uid="$(printf "%s" "${_MM__acc_passwd_ent}" | cut -d: -f3)"
_MM__firewalld_svc_name="${LISAM_FILE_PREFIX}-${_MM__acc_name}"
shift 2>/dev/null
test_root_permission
# add all data
refresh_acc_rc "${_MM__acc_uid}" "reset" || {
abort "Failed to refresh resource control configs"
}
if command -v firewall-cmd >/dev/null && if_var_set USER_SOCK_ALLOW
then
rm_firewall_svc "${_MM__firewalld_svc_name}" >/dev/null
new_firewall_svc "${_MM__firewalld_svc_name}" \
|| {
rm_firewall_svc "${_MM__firewalld_svc_name}" >/dev/null
abort "Failed to create a new firewalld service"
}
fi
# print info
mode_detail "${_MM__acc_name}"
}
# [ls] mode
# local prefix: ML
# no arg
mode_ls() {
# parse all opts and args
parse_opts "${BIN_BASENAME}" "" "" "${@}" && eval set -- "${PARSE_OPTS__args}" || return 1
_ML__acc_passwd_ent="$(getent passwd)"
_ML__sed_args="$(printf "%s" "${_ML__acc_passwd_ent}" | cut -d: -f5 \
| cut -d' ' -f1 \
| grep -Fxn "${LISAM_ACC_COMMENT_PREFIX}" | cut -d: -f1 \
| xargs -r printf "-e %sp ")"
unset _ML__acc_list
# shellcheck disable=SC2086
[ -n "${_ML__sed_args}" ] \
&& _ML__acc_list="$(printf "%s" "${_ML__acc_passwd_ent}" \
| sed -n ${_ML__sed_args} 2>/dev/null \
| cut -d: -f1)"
if terminal_output
then
_ML__acc_name_width="$(( LIST_SPACES + $(printf "%s\n" "${_ML__acc_list}" | wc -L) ))"
_ML__acc_per_line="$(( TERM_WIDTH / _ML__acc_name_width ))"
[ "${_ML__acc_per_line}" -eq 0 ] && _ML__acc_per_line=1
_ML__acc_list="$(printf "%s" "${_ML__acc_list}" \
| xargs -I{} -r \
printf "%-*s" "${_ML__acc_name_width}" "{}" \
| sed -e 's|\([^ ][^ ]*\)\( *\)|'"${C_G_BG}"'\1'"${C_RESET}"'\2|g' \
-e 's|\(\([^ ][^ ]* *\)\{1,'"${_ML__acc_per_line}"'\}\)|\1\n|g')"
# print non-existence message if stdout is terminal
_ML__acc_list="${_ML__acc_list:-${C_ERR_FG} * No LiSAM service account exists * ${C_RESET}}"
fi
printf "%s" "${_ML__acc_list:+${_ML__acc_list}
}"
}
# [detail] mode
# local prefix: MD
# args: <acc-name>
mode_detail() {
# parse all opts and args
parse_opts "${BIN_BASENAME}" "" "" "${@}" && eval set -- "${PARSE_OPTS__args}" || return 1
_MD__acc_name="${1}"
if [ -z "${_MD__acc_name}" ]; \
then abort_usage "Missing linux user account name"; fi
if ! _MD__acc_passwd_ent="$(getent passwd "${_MD__acc_name}")" \
>/dev/null 2>/dev/null; \
then abort "No linux user '${_MD__acc_name}' found"; fi
if ! printf "%s" "${_MD__acc_passwd_ent}" | cut -d: -f5 \
| cut -d' ' -f1 \
| grep -Fx "${LISAM_ACC_COMMENT_PREFIX}" >/dev/null 2>/dev/null
then abort "Linux user '${_MD__acc_name}' is not an LiSAM account"; fi
_MD__acc_uid="$(printf "%s" "${_MD__acc_passwd_ent}" | cut -d: -f3)"
_MD__acc_gid="$(printf "%s" "${_MD__acc_passwd_ent}" | cut -d: -f4)"
_MD__acc_home="$(printf "%s" "${_MD__acc_passwd_ent}" | cut -d: -f6)"
# get all configurable rc configs
unset _MD__rc_detail_str
cache_rc "${_MD__acc_uid}"
for _MD__k_varname in ${SYSTEMD_RC_LIST}
do
_MD__cnt="$(get_var_cnt "${_MD__k_varname}")"
_MD__k="$(get_var_label "${_MD__k_varname}")"
_MD__v="$(get_rc "${_MD__acc_uid}" "${_MD__k}" "${_MD__cnt}")"
# get current cgroup v2 value
# - if curfile var is set but value is not read, unset var
# - if curfile var is not set, leave var empty (but set)
_MD__v_current="$(get_var_actual "${_MD__acc_uid}" \
"${_MD__k_varname}")" \
&& [ -z "${_MD__v_current}" ] && unset _MD__v_current
if [ "${_MD__cnt}" -le 1 ]
then
_MD__rc_detail_str="$(printf "%s\n %-20s %s%-15s%s %s%-15s%s" \
"${_MD__rc_detail_str}" \
"${_MD__k}" \
"${_MD__v:+${C_G_FG}}" \
"${_MD__v:-(not-set)}" \
"${C_RESET}" \
"${_MD__v_current:+${C_G_FG}}" \
"${_MD__v_current-(N/A)}" \
"${C_RESET}" \
)"
else
terminal_output && _MD__v_delim=" " || _MD__v_delim="$(printf '\t')"
_MD__v_fmt="$(IFS='
'
for _MD__v_tmp in ${_MD__v}
do
printf "%s%c" "${C_G_BG}${_MD__v_tmp}${C_RESET}" "${_MD__v_delim}"
done
)"
_MD__rc_detail_str="$(printf "%s\n %-20s %s%s" \
"${_MD__rc_detail_str}" \
"${_MD__k}" \
"${_MD__v_fmt:-(not-set)}" \
"${C_RESET}" \
)"
fi
done
printf "
Username %s%s%s
UID:GID %s%s%s:%s%s%s
Home %s%s%s
Resource-Limits ('%s')
%-20s %-15s %s %s
\n" \
"${C_G_FG}" "${_MD__acc_name}" "${C_RESET}" \
"${C_G_FG}" "${_MD__acc_uid}" "${C_RESET}" \
"${C_G_FG}" "${_MD__acc_gid}" "${C_RESET}" \
"${C_G_FG}" "${_MD__acc_home}" "${C_RESET}" \
"${USER_SLICE_PREFIX}${_MD__acc_uid}${USER_SLICE_POSTFIX}" \
"[ Key ]" "[ Value ]" "[ Cgroup Status ]" \
"${_MD__rc_detail_str}"
}
# [rm] mode
# local prefix: MR
# args: <acc-name> [-f]
mode_rm() {
# parse all opts and args
parse_opts "${BIN_BASENAME}" "f" "force" "${@}" && eval set -- "${PARSE_OPTS__args}" || return 1
# linux account name
_MR__acc_name="${1}"
if [ -z "${_MR__acc_name}" ]; \
then abort_usage "Missing linux user account name"; fi
if ! _MR__acc_passwd_ent="$(getent passwd "${_MR__acc_name}")" \
>/dev/null 2>/dev/null; \
then abort "No linux user '${_MR__acc_name}' found"; fi
if ! printf "%s" "${_MR__acc_passwd_ent}" \
| cut -d: -f5 \
| grep -F "${LISAM_ACC_COMMENT_PREFIX}" \
>/dev/null 2>/dev/null
then abort "Linux user '${_MR__acc_name}' is not an LiSAM account"; fi
_MR__acc_uid="$(printf "%s" "${_MR__acc_passwd_ent}" | cut -d: -f3)"
_MR__firewalld_svc_name="${LISAM_FILE_PREFIX}-${_MR__acc_name}"
test_root_permission
# prompt once before remove if interactive shell
if [ -t 0 ] && { [ -z "${PARSE_OPTS__opt_f}" ] && [ -z "${PARSE_OPTS__opt__force}" ] ; }
then
_MR__prompt="value_to_start_loop"
until [ "${_MR__prompt}" = "y" ] || [ "${_MR__prompt}" = "Y" ] \
|| [ "${_MR__prompt}" = "n" ] || [ "${_MR__prompt}" = "N" ] \
|| [ -z "${_MR__prompt}" ]; do
read_normal " ${C_ERR_BG}[!!! WARNING !!!]${C_RESET} This process will ${C_ERR_FG}REMOVE ALL DATA${C_RESET} of the user '${C_G_FG}${_MR__acc_name}${C_RESET}'.
Proceed? [${C_ERR_FG}y${C_RESET}/N]" _MR__prompt || return 1
done
if [ "${_MR__prompt}" != "y" ] && [ "${_MR__prompt}" != "Y" ]; \
then exit 1; fi
fi
# remove all data
rm_svc_acc "${_MR__acc_name}" \
|| abort "Failed to remove the existing account"
rm_acc_rc "${_MR__acc_uid}"
if command -v firewall-cmd >/dev/null \
&& sudo firewall-cmd --get-services \
| grep "${_MR__firewalld_svc_name}" >/dev/null 2>/dev/null \
&& sudo firewall-cmd --info-service="${_MR__firewalld_svc_name}" \
>/dev/null 2>/dev/null
then
rm_firewall_svc "${_MR__firewalld_svc_name}" \
|| abort "Failed to remove the existing firewalld service"
fi
}
#==========================================================#
# unit business logic #
#==========================================================#
# create a new linux service account
# local prefix: NSA
# args: <account-name> <account-home> <account-id>
new_svc_acc() {
_NSA__name="${1:?name}"
_NSA__home="${2:?home}"
_NSA__id="${3:?id}"
printf " - Creating a new service account '%s' (UID/GID %s)...
(with home directory '%s')\n" \
"${_NSA__name}" \
"${_NSA__id}" \
"${_NSA__home}"
# create & init
sudo useradd \
-mU \
-d "${_NSA__home}" \
-u "${_NSA__id}" \
-c "${LISAM_ACC_COMMENT_PREFIX} ${_NSA__name}" \
-s "${SHELL:-/bin/sh}" \
"${_NSA__name}" || return 1
sudo loginctl enable-linger "${_NSA__name}" || return 1
printf "%s\n%s\n" \
"# for systemd command ('systemctl --user')" \
"export XDG_RUNTIME_DIR=\"/run/user/\$(id -u)\"" \
| sudo -u "${_NSA__name}" \
tee "${_NSA__home}/.profile.lisam" >/dev/null \
|| return 1
printf "\n\n. \"\$HOME/.profile.lisam\"\n" \
| sudo -u "${_NSA__name}" \
tee -a "${_NSA__home}/.profile" \
| sudo -u "${_NSA__name}" \
tee -a "${_NSA__home}/.bashrc" >/dev/null \
|| return 1
if sudo -u "${_NSA__name}" \
[ -e "${_NSA__home}/.bash_profile" ];
then
printf "\n\n. \"\$HOME/.profile.lisam\"\n" \
| sudo -u "${_NSA__name}" \
tee -a "${_NSA__home}/.bash_profile" \
>/dev/null || return 1
fi
if sudo -u "${_NSA__name}" \
[ -e "${_NSA__home}/.zshrc" ];
then
printf "\n\n. \"\$HOME/.profile.lisam\"\n" \
| sudo -u "${_NSA__name}" \
tee -a "${_NSA__home}/.zshrc" \
>/dev/null || return 1
fi
}
# remove an existing linux service account created by this script
# local prefix: RSA
# args: <account-name>
rm_svc_acc() {
_RSA__name="${1:?name}"
printf " - Removing an existing service account '%s'...
(with its home directory)\n" \
"${_RSA__name}"
sudo loginctl disable-linger "${_RSA__name}" || return 1
sudo killall -w -u "${_RSA__name}"
if command -v sestatus >/dev/null
then sudo userdel -frZ "${_RSA__name}" || return 1
else sudo userdel -fr "${_RSA__name}" || return 1
fi
sudo groupdel -f "${_RSA__name}" 2>/dev/null || true
}
# create all systemd resource control configs
# local prefix: RAR
# args: <uid> [is-reset]
refresh_acc_rc() {
_RAR__uid="${1:?uid}"
_RAR__reset="${2}"
# set all configurable rc configs
for _RAR__k in ${SYSTEMD_RC_LIST}
do
if if_var_set "${_RAR__k}"
then
_RAR__v="$(get_var_default "${_RAR__k}")" || return 1
if [ -n "${_RAR__v}" ]
then
# reset first if reset-flag is set, to prevent append on multi-value properties
if [ -n "${_RAR__reset}" ]
then
set_rc "${_RAR__uid}" \
"$(eval "printf \"%s\" \"\${${_RAR__k}__LABEL}\"")" >/dev/null
fi
# set value
set_rc "${_RAR__uid}" \
"$(eval "printf \"%s\" \"\${${_RAR__k}__LABEL}\"")" \
"${_RAR__v}" \
|| return 1
fi
fi
done
# set all accounting rc configs (nothing on stdout)
for _RAR__accounting in ${SYSTEMD_RC_ACCOUNTING_LIST}
do
set_rc "${_RAR__uid}" \
"$(eval "printf \"%s\" \"\${${_RAR__accounting}__LABEL}\"")" \
"true" \
>/dev/null \
|| return 1
done
# set all non-configurable rc configs (nothing on stdout)
for _RAR__nonconf in ${SYSTEMD_RC_CONST_LIST}
do
_RAR__nonconf_v="$(get_var_default "${_RAR__nonconf}")" || return 1
set_rc "${_RAR__uid}" \
"$(eval "printf \"%s\" \"\${${_RAR__nonconf}__LABEL}\"")" \
"${_RAR__nonconf_v}" \
>/dev/null \
|| return 1
done
# reload
sudo systemctl daemon-reload
sudo systemctl restart "${USER_SLICE_PREFIX}${_RAR__uid}${USER_SLICE_POSTFIX}"
}
# remove all systemd resource control configs
# local prefix: RAR0
# args: <uid>
rm_acc_rc() {
_RAR0__uid="${1:?uid}"
# set all configurable rc configs
_RAR0__return=0
for _RAR0__k in ${SYSTEMD_RC_LIST}
do
set_rc "${_RAR0__uid}" \
"$(eval "printf \"%s\" \"\${${_RAR0__k}__LABEL}\"")" 2>/dev/null \
|| _RAR0__return=1
done
sudo rm -rf \
"${SYSTEMD_USER_SLICE_PREFIX}${_RAR0__uid}${SYSTEMD_USER_SLICE_POSTFIX}" \
"${SYSTEMD_USER_SERVICE_PREFIX}${_RAR0__uid}${SYSTEMD_USER_SERVICE_POSTFIX}"
# reload
sudo systemctl daemon-reload
sudo systemctl restart "${USER_SLICE_PREFIX}${_RAR0__uid}${USER_SLICE_POSTFIX}"
return "${_RAR0__return}"
}
# create a new firewall rule for this account
# local prefix: NFS
# args: <service-name>
new_firewall_svc() {
# args
_NFS__name="${1:?name}"
shift 2>/dev/null
_NFS__ports="$(get_var_default USER_SOCK_ALLOW)" || return 1
[ -z "${_NFS__ports}" ] && return 0
printf " - Creating a new firewalld rule '%s'...
(with ports: %s)\n" \
"${_NFS__name}" "$(printf '%s' "${_NFS__ports}" | tr '\n' ' ')"
# add firewall service and apply
sudo firewall-cmd --permanent \
--new-service="${_NFS__name}" \
>/dev/null || return 1
sudo firewall-cmd --permanent \
--service="${_NFS__name}" \
--set-short="LiSAM ports config" \
>/dev/null || return 1
sudo firewall-cmd --permanent \
--service="${_NFS__name}" \
--set-description="LiSAM ports config for ${_NFS__name}" \
>/dev/null || return 1
for _NFS__port in ${_NFS__ports}
do
sudo firewall-cmd --permanent \
--service="${_NFS__name}" \
--add-port="${_NFS__port}"/tcp >/dev/null || return 1
sudo firewall-cmd --permanent \
--service="${_NFS__name}" \
--add-port="${_NFS__port}"/udp >/dev/null || return 1
done
# enable & reload
sudo firewall-cmd --permanent \
--add-service="${_NFS__name}" \
>/dev/null || return 1
sudo firewall-cmd --reload >/dev/null || return 1
}
# remove an existing firewall rule for this account
# local prefix: RFS
# args: <service-name> ...
rm_firewall_svc() {
_RFS__name="${1:?name}"
printf " - Removing an existing firewalld rule '%s'...\n" \
"${_RFS__name}"
# enable & reload
sudo firewall-cmd --permanent \
--delete-service="${_RFS__name}" >/dev/null || return 1
sudo rm -f "/etc/firewalld/services/${_RFS__name}.xml.old" \
>/dev/null 2>/dev/null
sudo firewall-cmd --reload >/dev/null || return 1
}
#==========================================================#
# application logic #
#==========================================================#
# check if variable is set
# local prefix: IVS
# args: <varname>
if_var_set() {
_IVS__varname="${1:?varname}"
eval "[ ! -z \${${_IVS__varname}+set} ]"
}
# check if root permission, and ask password if root
# no arg
test_root_permission() {
if ! sudo true
then
abort "Root permission check failed!"
fi
}
# parse options
# local prefix: PO
# usage e.g.: parse_opts "(binname)" "(shorts)" "(longs)" "${@}" && eval set -- "${PARSE_OPTS__args}" || return 1
# args: <bin-name> <short-opt-list> <long-opt-list> [args]...
parse_opts() {
# shellcheck disable=SC2046
unset $(set | grep "^PARSE_OPTS__.*$" | cut -d= -f1) # unset all vars
_PO__bin_name="${1:?bin-name}"
_PO__short_list="${2?short-opt-list}"
_PO__long_list="${3?long-opt-list}"
shift 3 || return 1
# parse valid short options
_PO__short_list_base="$(printf "%s" "${_PO__short_list}" \
| sed -n 's/\([^:]::\|[^:]:\|[^:]\)/\1\n/gp')"
_PO__short_list_flags="$(printf "%s" "${_PO__short_list_base}" \
| sed -n 's/^\([^:]\)$/-\1/p')"
_PO__short_list_args="$(printf "%s" "${_PO__short_list_base}" \
| sed -n 's/^\([^:]\):\?:$/-\1/p')"
# parse valid long options
_PO__long_list_base="$(printf "%s" "${_PO__long_list}" \
| tr ',' '\n')"
_PO__long_list_flags="$(printf "%s" "${_PO__long_list_base}" \
| sed -n 's/^\([^:][^:]*\)$/--\1/p')"
_PO__long_list_args="$(printf "%s" "${_PO__long_list_base}" \
| sed -n 's/^\([^:][^:]*\):\?:$/--\1/p')"
_PO__evalargs="$(getopt -n "${_PO__bin_name}" -o "${_PO__short_list}" \
${_PO__long_list+--longoptions="${_PO__long_list}"} \
-- "${@}")" \