various fixes
[distro-setup] / conflink
1 #!/bin/bash
2
3 source /a/bin/errhandle/err
4 err-cleanup() {
5 echo 1 >~/.local/conflink
6 }
7
8
9 usage() {
10 cat <<EOF
11 Usage: ${0##*/} [OPTIONS]
12 Link or otherwise install configuration files.
13
14 -f For fast. Dont use lnf, use ln -sf. Good for updating existing files.
15 EOF
16 exit $1
17 }
18
19
20 m() {
21 echo "$*"
22 "$@"
23 }
24 s() { sudo "$@"; }
25
26 lnf() { /a/exe/lnf "$@"; }
27
28 fast=false
29 if [[ $1 == -f ]]; then # f for fast
30 fast=true
31 shift
32 elif
33 [[ $1 ]]; then
34 echo "error: unrecognized arguments" >&2
35 exit 0
36 fi
37
38 if $fast; then
39 lnf() { ln -sf "$@"; }
40 fi
41
42 shopt -s nullglob
43 shopt -s extglob
44 shopt -s dotglob
45
46 # If we make a link back to the root, we stop going deeper into subdir_files.
47 # This makes it so we can do subdir directories. eg
48 # /p/c/subdir_files/.config/gajim -> ../../gagim
49 #
50 # Also note, under filesystem/, symlinks are expanded.
51
52 subdir-link-r() {
53 local root="$1"
54 local targets=()
55 if [[ $2 ]]; then
56 targets=( "$2"/!(.git|..|.|.#*) )
57 else
58 for f in "$1"/!(.git|..|.|.#*); do
59 if [[ -d $f ]]; then targets+=("$f"); fi
60 done
61 fi
62 local below
63 below="$( readlink -f "$root/..")"
64 for path in "${targets[@]}"; do
65 local fullpath
66 fullpath="$(readlink -f "$path")"
67 if [[ -f $path || $(dirname "$fullpath") == "$below" ]]; then
68 m lnf -T "$path" "$HOME/${path#$root/}"
69 elif [[ -d "$path" ]]; then
70 subdir-link-r "$root" "$path"
71 fi
72 done
73 }
74
75
76 common-file-setup() {
77 local dir fs x f reload_systemd
78 local -a reload_services
79 local -a restart_services
80 reload_systemd=false
81 for dir in "$@"; do
82 fs=$dir/filesystem
83 if [[ -e $fs && $user =~ ^iank?$ ]]; then
84 cmd=( s rsync -aiSAX --chown=root:root --chmod=g-s
85 --exclude=/etc/dovecot/users
86 --exclude='/etc/exim4/passwd*'
87 --exclude='/etc/exim4/*.pem'
88 $fs/ / )
89 echo "${cmd[@]@Q}"
90 while read -r line; do
91 file="${line:12}"
92 case $file in
93 etc/prometheus/rules/iank.yml)
94 case $HOSTNAME in
95 kd) m s systemctl reload prometheus ;;
96 esac
97 ;;
98 etc/systemd/system/*)
99 reload_systemd=true
100 ;;
101 etc/dnsmasq.d/*)
102 restart_services+=(dnsmasq)
103 ;;
104 etc/systemd/resolved.conf.d/*)
105 restart_services+=(systemd-resolved)
106 ;;
107 esac
108 # Previously did this with tar, but it doesn't
109 # update directory permissions.
110 #
111 # S = do spare files efficiently
112 # A = preserve acls
113 # X = preserve extended attributes
114 # i = itemize
115 done < <("${cmd[@]}")
116 fi
117
118 if [[ -e $dir/subdir_files ]]; then
119 m subdir-link-r $dir/subdir_files
120 fi
121 local x=( $dir/!(binds|subdir_files|filesystem|machine_specific|..|.|.#*) )
122 (( ${#x[@]} >= 1 )) || continue
123 m lnf ${x[@]} ~
124 done
125 if $reload_systemd; then
126 m s systemctl daemon-reload
127 fi
128 for service in ${restart_services[@]}; do
129 if systemctl is-active $service >/dev/null; then
130 m s systemctl restart $service
131 fi
132 done
133 }
134
135 user=$(id -un)
136 all_dirs=({/a/bin/ds,/p/c}{,/machine_specific/$HOSTNAME})
137 # note, we assume a group of hosts does not have the
138 # same name as a single host, which is no problem on our scale.
139 for x in /p/c/machine_specific/*.hosts /a/bin/ds/machine_specific/*.hosts; do
140 if grep -qxF $HOSTNAME $x; then all_dirs+=( ${x%.hosts} ); fi
141 done
142
143 # old files 2022-03
144 for t in systemstatus epanicclean btrfsmaintstop dynamicipupdate; do
145 f=/etc/systemd/system/$t.timer
146 if [[ -e $f ]]; then
147 s systemctl stop $t.timer
148 s systemctl disable $t.timer
149 s rm -fv $f
150 reload_systemd=true
151 fi
152 done
153
154 c_dirs=(/a/c{,/machine_specific/$HOSTNAME})
155 case $user in
156 iank)
157 /a/bin/ds/install-my-scripts
158 files=(/p/c/machine_specific/*/filesystem/etc/ssh/*_key
159 /p/c/machine_specific/*/filesystem/etc/openvpn/client/*.key
160 /p/c/filesystem/etc/openvpn/client/*.key
161 /p/c/filesystem/etc/openvpn/easy-rsa/keys/*.key
162 )
163 if [[ -e ${files[0]} ]]; then
164 chmod 600 ${files[@]}
165 fi
166 # p needs to go first so .ssh link is created, then config link inside it
167 m common-file-setup ${all_dirs[@]}
168
169 #### begin special extra stuff ####
170 install -d -m700 ~/gpg-agent-socket
171
172 f=/var/lib/bind
173 if [[ -e $f ]]; then
174 # reset to the original permissions.
175 m s chgrp -R bind $f
176 m s chmod g+w $f
177 fi
178 sudo bash -c 'shopt -s nullglob; for f in /etc/bind/*.key /etc/bind/*.private /etc/bind/key.*; do chgrp bind $f; done'
179 if [[ -e /etc/caldav-htpasswd ]] && getent group www-data &>/dev/null; then
180 s chgrp www-data /etc/caldav-htpasswd
181 fi
182 if [[ -e /var/lib/znc ]] && getent group znc; then
183 s chown -R znc:znc /var/lib/znc
184 fi
185 for f in /etc/prometheus-{,export-}htpasswd; do
186 if [[ -e $f ]]; then
187 s chmod 640 $f
188 if getent passwd www-data; then
189 s chown root:www-data $f
190 fi
191 fi
192 done
193 f=/etc/prometheus-pass
194 if [[ -e $f ]]; then
195 # note: this is duplicative of the file's own permissions
196 s chmod 640 $f /etc/prometheus-pass
197 if getent passwd prometheus; then
198 s chown root:prometheus $f
199 fi
200 fi
201
202
203 ##### end special extra stuff #####
204
205 m sudo -H -u user2 "${BASH_SOURCE[0]}"
206
207 f=/a/bin/distro-setup/system-status
208 if [[ -x $f ]]; then
209 $f _
210 fi
211 mkdir -p ~/.local
212 echo 0 >~/.local/conflink
213
214 ;;
215 user2)
216 m common-file-setup ${c_dirs[@]}
217 ;;
218 *)
219 echo "$0: error: unexpected user"; exit 1
220 ;;
221 esac