-
Notifications
You must be signed in to change notification settings - Fork 14
/
self-destruct.sh
executable file
·193 lines (148 loc) · 4.25 KB
/
self-destruct.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
#!/usr/bin/env bash
SELF_DESTRUCT_VERSION="0.93"
is_valid_timestamp() {
PATTERN='^[0-9]\+\s\(Minutes\?\|Hours\?\|Days\?\|Weeks\?\|Years\?\)$'
echo $* | grep -o $PATTERN > /dev/null
return $?
}
convert_tag_to_timestamp() {
if [[ $1 =~ ^([0-9]+)\ ([A-Za-z]+)$ ]]; then
DT=$(echo ${BASH_REMATCH[1]} | xargs)
DF=$(echo ${BASH_REMATCH[2]} | xargs | tr '[:upper:]' '[:lower:]')
case ${DF} in
second|seconds)
DF='S'
;;
minute|minutes)
DF='M'
;;
hour|hours)
DF='H'
;;
day|days)
DF='d'
;;
week|weeks)
DF='w'
;;
month|months)
DF='m'
;;
year|years)
DF='y'
;;
*)
echo "DF (Date Format) wrong!"
exit 1
esac
fi
echo $(($(date -j -v "+${DT}${DF}" "+%s") - $(date -j "+%s")))
}
secure_delete_files_for_tag_after_seconds () {
if [ -z "$1" ]; then
echo "-Parameter 1 required: Tag"
return 0
fi;
if [ -z "$2" ]; then
echo "-Parameter 2 required: Seconds"
return 0
elif [[ ! $2 =~ ^-?[0-9]+$ ]]; then
echo "-Parameter 2 must be an integer!"
return 0
fi;
current_stamp=$(date +%s)
mdfind "tag:$1" | while read f; do
file_modified=$(stat -f%Y "$f")
file_diff=$((current_stamp-file_modified))
if [ $file_diff -ge $2 ]; then
if [ -f "$f" -o -L "$f" ]; then
rm -f "$f"
elif [ -d "$f" ]; then
rm -f "$f"
fi
fi;
done
}
load_configuration () {
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
script_install_path="/usr/local/bin/self-destruct"
plist_name="com.github.tdlm.mac-os-self-destruct"
plist_path="$script_dir/$plist_name.plist"
plist_install_path="$HOME/Library/LaunchAgents/$plist_name.plist"
is_plist_loaded=$(launchctl list | grep -c "$plist_name")
}
install () {
load_configuration
echo "Copying self to $script_install_path"
cp -f $0 $script_install_path
chmod +x $script_install_path
local user=`whoami`
if [ $is_plist_loaded -gt 0 ]; then
echo "Plist found. Unloading before install..."
launchctl unload $plist_install_path
else
echo "Plist not found. Loading fresh..."
fi
echo "Generating plist for user and installing: $plist_install_path"
cat $plist_path | sed -e "s#{SCRIPT_INSTALL_PATH}#'$script_install_path'#g" > $plist_install_path
launchctl load -w -F $plist_install_path
echo "Installation complete!"
}
uninstall () {
load_configuration
local is_plist_loaded=$(launchctl list | grep -c "$plist_name")
if [ -f $script_install_path ]; then
echo "Removing script..."
rm -f $script_install_path
fi
if [ $is_plist_loaded -gt 0 ]; then
echo "Unloading plist"
launchctl unload $plist_install_path
fi
if [ -f $plist_install_path ]; then
echo "Removing $plist_install_path..."
rm -f $plist_install_path
fi
echo "Uninstall complete!"
}
show_usage() {
cat <<EOF
Self Destruct v${SELF_DESTRUCT_VERSION} for macOS
Securely destroys files or directories on a delay based on their macOS "Tag"
Available Tags: 1 Minute, 1 Hour, 1 Day, 1 Week, 1 Month, 1 Year.
Multiplications of these tags are also supported (10 Minutes, 3 Hours, etc.).
For example, if a file is tagged with "1 Week" then it will be deleted at
the time exactly one week from the last time the file was modified.
RUNNING MANUALLY
$0 --run
HELP
$0 --help
INSTALLATION FROM REPOSITORY
git clone [email protected]:tdlm/mac-os-self-destruct.git
./mac-os-self-destruct/self-destruct.sh --install
UNINSTALL
$0 --uninstall
Licensed under GNU GPL v2.0 by Scott Weaver <http://scottmw.com>
EOF
exit 2
}
if [ "$1" = "-r" -o "$1" = "--run" ]; then
TAGS=$(mdfind -0 "(kMDItemUserTags == '*')" | \
xargs -0 mdls -name kMDItemUserTags | grep '^ ' | cut -c5- | cut -d , -f 1 | sort -u)
while read -r TAG; do
# Remove quotes
TAG=$(echo -n $TAG | tr -d '"')
if is_valid_timestamp $TAG; then
echo "Convert tag: $TAG"
secure_delete_files_for_tag_after_seconds "$TAG" $(convert_tag_to_timestamp "$TAG")
else
echo "Invalid tag: $TAG"
fi
done <<< "$TAGS"
elif [ "$1" = "-i" -o "$1" = "--install" ]; then
install
elif [ "$1" = "-u" -o "$1" = "--uninstall" ]; then
uninstall
else
show_usage
fi