-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomesync
executable file
·337 lines (286 loc) · 9.58 KB
/
homesync
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
#!/bin/bash
checkMountpoint() {
if mountpoint -q "$1"; then
true
else
false
fi
}
checkDependencies() {
dependencies=("inotifywait" "ldapsearch" "unison")
for dep in "${dependencies[@]}"; do
if [ ! "$(command -v "$dep")" ]; then
fatal "[ERROR] Command $dep not installed."
fi
done
}
checkLock() {
exec {LOCK}>/var/lock/homesync-"$USER".lock
flock -x -n ${LOCK} || fatal "[INFO] Homesync already running for user $USER."
}
debug() {
if [ "$(getConfig global DEBUG)" == "true" ]; then
logfile=$(getConfig global LOG_FILE | sed "s|~|$HOME|")
if [ ! -f "$logfile" ]; then
touch "$logfile"
fi
exec 3>&1 1>"$logfile" 2>&1
trap '{ set +x; } 2>/dev/null; echo -n "[$(date "+%d/%m/%Y %R")] "; set -x' DEBUG
fi
}
fatal() {
echo "$@" >&2
kill -10 $proc
}
getConfig() {
val=$(awk '/^\[.*\]$/{obj=$0}/=/{print obj $0}' "$conf" | grep '^\['"$1"'\]'"$2"' = ' | sed -r 's/^([^=]+)= //')
if [ -z "$val" ]; then
if [ "$1" != "global" ]; then
getConfig global "$2"
else
fatal "[ERROR] Configuration $2 not found in $conf."
fi
else
echo "$val"
fi
}
getMachine() {
if [ "$ldapBind" != "false" ]; then
ldapUser=$(ldapsearch -x -H "$ldapURL" -LLL -D "$ldapBind" -w "$ldapBindPW" -b "$ldapDN" cn="$USER")
else
ldapUser=$(ldapsearch -x -H "$ldapURL" -LLL -b "$ldapDN" cn="$USER")
fi
if [ "$?" -eq 0 ]; then
echo "$ldapUser" | grep "$ldapMachineAttribute" | tr -d " " | cut -d ":" -f 2
else
fatal "[ERROR] LDAP bind failed."
fi
}
getName() {
getent passwd "$USER" | cut -d ":" -f 5 | cut -d " " -f 1
}
getShell() {
getent passwd "$USER" | cut -d ":" -f 7
}
setupHome() {
userShell=$(getShell)
homesyncStart="homesync -a"
homesyncStop="homesync -s && homesync -k"
storageRemoteBaseSize=$(du -s "$storageRemoteBase" 2> /dev/null | cut -f 1 | awk '1; END { if (NR == 0) printf "0"; }')
if ! (( "$storageRemoteBaseSize" >=0 && "$storageRemoteBaseSize" <= 4 )); then
if [ ! -d "$storageRemote" ] || [ -z "$(ls -A "$storageRemote")" ]; then
for dir in "${storageDirsSync[@]}"; do
mkdir -p "$storageRemote"/"$dir"
done
chmod -R 750 "$storageRemote"
fi
fi
if [ "${storageDirsNoSync[0]}" != "false" ]; then
if [ ! -d "$storageLocal" ] || [ -z "$(ls -A "$storageLocal")" ]; then
for dir in "${storageDirsNoSync[@]}"; do
mkdir -p "$storageLocal"/"$dir"
done
chmod -R 750 "$storageLocal"
fi
fi
if [ ! -d "$homeLocal" ] || [ -z "$(ls -A "$homeLocal")" ]; then
for dir in "${homeDirs[@]}"; do
mkdir -p "$homeLocal"/"$dir"
done
for file in "${homeFiles[@]}"; do
touch "$homeLocal"/"$file"
done
if [ "$userShell" == "/bin/bash" ]; then
touch "$homeLocal"/.bash_history
elif [ "$userShell" == "/bin/zsh" ]; then
touch "$homeLocal"/.zsh_history
fi
chmod -R 700 "$homeLocal"
fi
if [ ! -d "$HOME" ] || [ -z "$(ls -A "$HOME")" ]; then
if [ "$userShell" == "/bin/bash" ]; then
ln -sf "$homeLocal"/.bash_history "$HOME"/.bash_history
cp /etc/skel/.{bash_logout,bashrc} "$HOME"
echo "$homesyncStop" >> "$HOME"/.bash_logout
elif [ "$userShell" == "/bin/zsh" ]; then
ln -sf "$homeLocal"/.zsh_history "$HOME"/.zsh_history
echo "HISTFILE=/home/ldap/$USER/.zsh_history" > "$HOME"/.zshrc
echo "$homesyncStart" >> "$HOME"/.zprofile
echo "$homesyncStop" >> "$HOME"/.zlogout
fi
cp /etc/skel/.profile "$HOME"
echo "$homesyncStart" >> "$HOME"/.profile
for dir in "${homeDirs[@]}"; do
ln -snf "$homeLocal"/"$dir" "$HOME"/"$dir"
done
for file in "${homeFiles[@]}"; do
ln -sf "$homeLocal"/"$file" "$HOME"/"$file"
if [ "$file" == ".viminfo" ]; then
cat <<-EOF > "$HOME"/.vimrc
source \$VIMRUNTIME/defaults.vim
set viminfo+=n$homeLocal/$file
EOF
fi
done
fi
}
symlinksStorage() {
if [ "$1" == "true" ]; then
origin="$storageRemote"
else
origin="$storageLocal"
fi
for dir in "${storageDirsSync[@]}"; do
ln -snf "$origin"/"$dir" "$HOME"/"$dir"
done
if [ "${storageDirsNoSync[0]}" != "false" ]; then
for dir in "${storageDirsNoSync[@]}"; do
ln -snf "$storageLocal"/"$dir" "$HOME"/"$dir"
done
fi
ln -snf "$origin" "$HOME"/"$storageLocalName"
}
syncHandler() {
if checkMountpoint "$storageRemoteBase"; then
if [ "$HOSTNAME" == "$(getMachine)" ]; then
syncRemote &
fi
fi
}
syncHome() {
mount "$bindPath"
syncMain "$HOME" "$bindPath""$HOME"
umount "$bindPath"
}
syncMain() {
if [ ! -d "$2" ]; then
cp -rp "$1" "$2"
elif [ -z "$(ls -A "$2")" ]; then
cp -rp "$1"/* "$2"
else
if [ "$1" == "$HOME" ]; then
unison "${unisonArgs[@]}" "$1" "$2"
else
unison "${unisonArgs[@]}" "${storageDirsSync[@]/#/-path=}" "$1" "$2"
fi
fi
}
syncRemote() {
syncMain "$storageRemote" "$storageLocal"
{ inotifywait -r -m "$storageRemote" -e create -e delete -e modify -e move -e attrib --format "%w|%e|%f" |
while IFS='|' read -r dir action file; do
syncMain "$storageRemote" "$storageLocal"
done }
}
usage() {
cat <<-EOF
Usage: homesync -arg
-a Online: Sync roaming home with local home, and network share with local storage
Offline: Change symlinks to local storage
-d Setup local home directory structure
-h Show this help message and exit
-k Kill all homesync processes started by the current user session
-l Setup home directory symlinks according to mount status
-r Only sync network share with local storage
-s Only sync roaming home with the underlying directory
EOF
}
if [ $# -gt 0 ] && [[ $1 == -* ]]; then
proc=$$
conf="/etc/homesync.conf"
trap 'exit 1' SIGUSR1
if [ -f $conf ]; then
ldapURL=$(getConfig global LDAP_URL)
ldapDN=$(getConfig global LDAP_DN)
ldapBind=$(getConfig global LDAP_BIND)
ldapBindPW=$(getConfig global LDAP_BIND_PW)
ldapMachineAttribute=$(getConfig global LDAP_MACHINE_ATTRIBUTE)
bindPath=$(getConfig global BIND_PATH)
storageRemote=$(getConfig global STORAGE_REMOTE)/$(getName)
storageRemoteBase=$(cut -d "/" -f -2 <<< "$storageRemote")
storageLocal=$(getConfig global STORAGE_LOCAL)
storageLocalName=$(getConfig global STORAGE_LOCAL_NAME)
homeLocal=$(getConfig global HOME_LOCAL)/$USER
read -r -a unisonArgs <<< "$(getConfig global UNISON_ARGS)"
read -r -a storageDirsSync <<< "$(getConfig "$USER" STORAGE_DIRS_SYNC)"
read -r -a storageDirsNoSync <<< "$(getConfig "$USER" STORAGE_DIRS_NOSYNC)"
read -r -a homeDirs <<< "$(getConfig global HOME_DIRS)"
read -r -a homeFiles <<< "$(getConfig global HOME_FILES)"
export UNISON=$homeLocal/.unison
while getopts 'adhklrs' arg; do
case $arg in
a)
checkLock
checkDependencies
debug
if checkMountpoint "$HOME"; then
setupHome
syncHome
symlinksStorage true
syncHandler
elif [[ ! $(stat -f -L -c %T "$storageRemoteBase" 2> /dev/null) =~ ^(autofs|nfs|smb|^$) ]]; then
setupHome
syncHome
symlinksStorage true
else
symlinksStorage false
fi
exit
;;
d)
debug
if checkMountpoint "$HOME"; then
setupHome
symlinksStorage true
fi
;;
h)
usage
exit
;;
k)
pkill -P "$(pgrep -t "$(tty | cut -d "/" -f 3-)" -o homesync)"
exit
;;
l)
debug
if checkMountpoint "$HOME"; then
symlinksStorage true
else
symlinksStorage false
fi
exit
;;
r)
checkLock
checkDependencies
debug
if checkMountpoint "$HOME"; then
syncHandler
fi
exit
;;
s)
checkDependencies
debug
if checkMountpoint "$HOME"; then
syncHome
elif [[ ! $(stat -f -L -c %T "$storageRemoteBase" 2> /dev/null) =~ ^(autofs|nfs|smb|^$) ]]; then
syncHome
fi
exit
;;
*)
usage
exit 1
;;
esac
done
else
fatal "[ERROR] Configuration file not found."
fi
else
echo "$0: missing argument"
usage
exit 1
fi