-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk4group
executable file
·88 lines (71 loc) · 1.44 KB
/
mk4group
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
#!/bin/bash
#
# mk4group
# recursively makes dirs group-owned, setgid, and copies user mode to group
#
# args:
# - system group to chgrp given as $1
# - all remaining args: trees to be recursively converted
#
# scott@smemsh.net
# http://smemsh.net/src/utilsh/
# http://spdx.org/licenses/GPL-2.0
#
##############################################################################
bomb ()
{
echo "$@" >&2
exit 1
}
###
dirtree_setgid_on_dirs ()
{
local dir=$1
find "$dir" -type d -print0 \
| xargs -r0 chmod g+s
}
dirtree_change_group ()
{
local grp=$1
local dir="$2"
chgrp -R $grp "$dir"
}
dirtree_user2group_permcopy ()
{
local dir="$1"
local permbit
for permbit in 4 2 1
do
find "$dir" \
-not -type l \
-perm -0${permbit}00 \
-print0 \
| xargs -r0 chmod +00${permbit}0
done
}
##############################################################################
main ()
{
local group=$1; shift
local gid dir path
gid=$(getent group $group | awk -F : '{print $(NF-1)}')
if ! [[ $gid ]]; then
bomb "group $group does not look up"; fi
for ((i = 1; i <= $#; i++))
do
dir="${!i}"
path=$(readlink -e "$dir")
test -d "$path" ||
bomb "path '$path' dne"
test -w "$path" ||
bomb "path '$path' not writable"
dirtree_change_group $gid "$path" ||
bomb "chrgrp failed"
dirtree_setgid_on_dirs "$path" ||
bomb "setgid failed"
dirtree_user2group_permcopy "$path" ||
bomb "perms copy failed"
done
}
###
main "$@"