-
Notifications
You must be signed in to change notification settings - Fork 1
/
lock-fstree.sh
381 lines (289 loc) · 16.2 KB
/
lock-fstree.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
#!/bin/bash
# -------------------------------------------------------------------------------
# Name: lock-fstree
# Description: Locks down a file system subtree with the right set of permissions
# Author: Carlos Veira Lorenzo - cveira [at] thinkinbig.org
# Version: 3.0b
# Date: 2016/10/28
# -------------------------------------------------------------------------------
# Usage: lock-fstree.sh <ConfigProfileName> [-confirm] [-?|-h|--help]
# -------------------------------------------------------------------------------
# Dependencies: cat, ls, head, rm, wc, tr, touch, grep, awk, sed, date, find,
# chmod, chown
# lock-fstree-<ConfigProfileName>.conf
# lock-fstree-<ConfigProfileName>-exceptions.conf
# -------------------------------------------------------------------------------
InstallDir=${BaseDir}/scripts
LogsDir=${BaseDir}/logs
TmpDir=${BaseDir}/tmp
CLI_Confirm="-confirm"
STATUS_OK=0
STATUS_ON=1
STATUS_OFF=0
OriginalDir="$PWD"
ExitCode=0
# Default values for CLI parameters
ConfigurationProfile="$1"
Confirm=$STATUS_OFF
GetHelp=$STATUS_OFF
# Initial state for configuration parameters
TargetPath="TargetPath"
ExceptionsFile="ExceptionsFile"
LockOwner="LockOwner"
LockFileACL="LockFileACL"
LockDirACL="LockDirACL"
UnLockOwner="UnLockOwner"
UnLockFileACL="UnLockFileACL"
UnLockDirACL="UnLockDirACL"
ExceptionOwner="ExceptionOwner"
ExceptionLockFileACL="ExceptionLockFileACL"
ExceptionLockDirACL="ExceptionLockDirACL"
FullControlLockFileACL="FullControlLockFileACL"
FullControlLockDirACL="FullControlLockDirACL"
TreeMode="tree"
FolderMode="folder"
FileMode="file"
FullControlRule="FullControl"
# Establish a SessionId and the StartTime
CurrentDate=$(date +%Y%m%d-%H%M%S)
CurrentSequenceId=$(ls -1AB $LogsDir/*$CurrentDate* 2> /dev/null | wc -l)
CurrentSessionId="$CurrentDate-$CurrentSequenceId"
StartTime="$(date '+%Y/%m/%d %H:%M:%S')"
# Load support functions and variables
. $InstallDir/libcore.sh
# Evaluate and Process CLI parameters
if [ -z $(echo "$1" | grep '^-') ] ; then
if [ ! -f $InstallDir/"lock-fstree-$ConfigurationProfile.conf" ] ; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): ERROR: Can't find a Configuration Profile named $ConfigurationProfile"
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): lock-fstree.sh <ConfigProfileName> [-confirm] [-?|-h|--help]"
exit 1
fi
shift 1
else
GetHelp=$STATUS_ON
fi
case "$(echo $1 | tr ":" "\n" | head -1)" in
"-?"|"-h"|"--help") GetHelp=$STATUS_ON ;;
"$CLI_Confirm") Confirm=$STATUS_ON ;;
esac
# Display a quick help and exit
if [ $GetHelp -eq $STATUS_ON ] ; then
echo
echo " lock-fstree.sh <ConfigProfileName> [-confirm] [-?|-h|--help]"
echo
exit 0
fi
echo
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): -------------------------------------------------------------------------------------------"
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Lock-FsTree v3.0b "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Carlos Veira Lorenzo - [http://thinkinbig.org] "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): -------------------------------------------------------------------------------------------"
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): This software come with ABSOLUTELY NO WARRANTY. This is free "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): software under GPL 2.0 license terms and conditions. "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): -------------------------------------------------------------------------------------------"
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Loading configuration ..."
for ConfigurationItem in $( cat "$InstallDir/lock-fstree-$ConfigurationProfile.conf" | grep -v "#" | grep . ); do
PropertyName=`echo $ConfigurationItem | awk -F "=" '{ print $1 }'`
PropertyValue=`echo $ConfigurationItem | awk -F "=" '{ print $2 }'`
if [ $PropertyName == $TargetPath ] ; then TargetPath="$PropertyValue" ; fi
if [ $PropertyName == $ExceptionsFile ] ; then ExceptionsFile="$PropertyValue" ; fi
if [ $PropertyName == $LockOwner ] ; then LockOwner="$PropertyValue" ; fi
if [ $PropertyName == $LockFileACL ] ; then LockFileACL="$PropertyValue" ; fi
if [ $PropertyName == $LockDirACL ] ; then LockDirACL="$PropertyValue" ; fi
if [ $PropertyName == $UnLockOwner ] ; then UnLockOwner="$PropertyValue" ; fi
if [ $PropertyName == $UnLockFileACL ] ; then UnLockFileACL="$PropertyValue" ; fi
if [ $PropertyName == $UnLockDirACL ] ; then UnLockDirACL="$PropertyValue" ; fi
if [ $PropertyName == $ExceptionOwner ] ; then ExceptionOwner="$PropertyValue" ; fi
if [ $PropertyName == $ExceptionLockFileACL ] ; then ExceptionLockFileACL="$PropertyValue" ; fi
if [ $PropertyName == $ExceptionLockDirACL ] ; then ExceptionLockDirACL="$PropertyValue" ; fi
if [ $PropertyName == $FullControlLockFileACL ] ; then FullControlLockFileACL="$PropertyValue" ; fi
if [ $PropertyName == $FullControlLockDirACL ] ; then FullControlLockDirACL="$PropertyValue" ; fi
done
if [ -f "$TmpDir/.LockFsTree-$ConfigurationProfile.lock" ] ; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Aborting operation."
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): A previous instance is still running."
exit 0
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Acquiring locks ..."
touch "$TmpDir/.LockFsTree-$ConfigurationProfile.lock"
touch "$TmpDir/.UnLockFsTree-$ConfigurationProfile.lock"
fi
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Execution parameters: "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): User Confirmation: $(ConvertTo-String $Confirm) "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Lock Owner: $LockOwner "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Lock File ACLs: $LockFileACL "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Lock Directory ACLs: $LockDirACL "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): UnLock Owner: $UnLockOwner "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): UnLock File ACLs: $UnLockFileACL "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): UnLock Directory ACLs: $UnLockDirACL "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Owner for Exceptions: $ExceptionOwner "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): File ACLs for Exceptions: $ExceptionLockFileACL "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Directory ACLs for Exceptions: $ExceptionLockDirACL "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Full Control File ACLs: $FullControlLockFileACL "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Full Control Directory: $FullControlLockDirACL "
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): -------------------------------------------------------------------------------------------"
if [ $Confirm -eq $STATUS_OFF ]; then
read -p "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Do you want to proceed (y/n) ? " UserConfirmation
if [ "$UserConfirmation" != "y" ] ; then
if [ -f "$TmpDir/.LockFsTree-$ConfigurationProfile.lock" ] ; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Releasing locks ..."
rm -f "$TmpDir/.LockFsTree-$ConfigurationProfile.lock"
rm -f "$TmpDir/.UnLockFsTree-$ConfigurationProfile.lock"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Could not release lock: lock not found."
fi
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Started: $StartTime"
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Finished: $(date '+%Y/%m/%d %H:%M:%S')"
exit 0
fi
fi
cd "$TargetPath"
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting object onwership ..."
chown -Rv "$LockOwner" "$TargetPath" | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting file ACLs ..."
find "$TargetPath" -depth -type f -print -exec chmod $LockFileACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting folder ACLs ..."
find "$TargetPath" -depth -type d -print -exec chmod $LockDirACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting up exceptions ..."
for Exception in $( cat $InstallDir/$ExceptionsFile | grep -v "#" | grep . ); do
ExecutionMode=`echo $Exception | awk -F ":" '{ print $1 }'`
SecurityRule=`echo $Exception | awk -F ":" '{ print $2 }'`
TargetObject=`echo $Exception | awk -F ":" '{ print $3 }'`
case "$ExecutionMode" in
$TreeMode)
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting object onwership ..."
chown -Rv "$ExceptionOwner" "$TargetObject" | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
if [ "$SecurityRule" == $FullControlRule ] ; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting folder ACLs ..."
# find "$TargetObject" -depth -type f -print -exec chmod $FullControlLockFileACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
find "$TargetObject" -depth -type d -print -exec chmod $FullControlLockDirACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting file ACLs ..."
find "$TargetObject" -depth -type f -print -exec chmod $ExceptionLockFileACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting folder ACLs ..."
find "$TargetObject" -depth -type d -print -exec chmod $ExceptionLockDirACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
fi
;;
$FolderMode)
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting object onwership ..."
chown -Rv "$ExceptionOwner" "$TargetObject" | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
if [ "$SecurityRule" == $FullControlRule ] ; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting folder ACLs ..."
# find "$TargetObject" -type f -print -exec chmod $FullControlLockFileACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
find "$TargetObject" -type d -print -exec chmod $FullControlLockDirACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting file ACLs ..."
find "$TargetObject" -type f -print -exec chmod $ExceptionLockFileACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting folder ACLs ..."
find "$TargetObject" -type d -print -exec chmod $ExceptionLockDirACL '{}' \; | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
fi
;;
$FileMode)
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting object onwership ..."
chown -v "$ExceptionOwner" "$TargetObject" | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
if [ "$SecurityRule" == $FullControlRule ] ; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting file ACL ..."
chmod $FullControlLockFileACL "$TargetObject" | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): + Setting file ACL ..."
chmod $ExceptionLockFileACL "$TargetObject" | sed "s@^@[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): @g"
if [ $? -eq 0 ]; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): > Operation completed with errors"
ExitCode=$(($ExitCode+1))
fi
fi
;;
esac
done
cd "$OriginalDir"
if [ -f "$TmpDir/.LockFsTree-$ConfigurationProfile.lock" ] ; then
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Releasing locks ..."
rm -f "$TmpDir/.LockFsTree-$ConfigurationProfile.lock"
rm -f "$TmpDir/.UnLockFsTree-$ConfigurationProfile.lock"
else
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Could not release lock: lock not found."
fi
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Total errors captured: $ExitCode"
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Started: $StartTime"
echo "[Lock-FsTree] $(date '+%Y/%m/%d %H:%M:%S'): Finished: $(date '+%Y/%m/%d %H:%M:%S')"
exit $ExitCode