c025005e5d208e21ebf959f6a820262adabdb62c
[infodrom/musiikki-web.git] / install-system
1 #! /bin/bash
2
3 ESC=$'\e'
4 col_red="${ESC}[1;31m"
5 col_green_bold="${ESC}[1;32m"
6 col_green="${ESC}[32m"
7 col_yellow="${ESC}[1;33m"
8 #col_cyan="${ESC}[1;36m"
9 col_reset="${ESC}[0m"
10
11 opt_destdir=/srv/musiikki
12 opt_adminpwfile=${opt_destdir}/htpasswd
13 opt_verbose=1
14 opt_uid=
15 opt_gid=
16 opt_apt=
17 opt_hostname=
18 opt_essid=
19 opt_wpakey=
20 opt_subnet=
21
22 # default name of Raspian
23 default_hostname=raspberrypi
24
25 echo_ok()
26 {
27     echo "${col_green}$*${col_reset}"
28 }
29
30 echo_ok_bold()
31 {
32     echo "${col_green_bold}$*${col_reset}"
33 }
34
35 echo_error()
36 {
37     echo "${col_red}$*${col_reset}"
38 }
39
40 echo_verbose()
41 {
42     test $opt_verbose -eq 1 || return
43
44     echo -n "$*"
45 }
46
47 echo_newline_verbose()
48 {
49     test $opt_verbose -eq 1 || return
50
51     echo ""
52 }
53
54 # ------------------------------------------------------------
55
56 assert_system_uidgid()
57 {
58     test -z "$opt_uid" || return
59
60     if [ -d ${opt_destdir} ]
61     then
62         opt_uid=$(stat --format=%u ${opt_destdir})
63         opt_gid=$(stat --format=%g ${opt_destdir})
64     else
65         echo -n "${col_yellow}Set file owner to${col_reset}: "
66         read opt_uid
67
68         echo -n "${col_yellow}Set file group to${col_reset}: "
69         read opt_gid
70     fi
71 }
72
73 assert_system_hostname()
74 {
75     test -z "$opt_hostname" || return
76
77     echo -n "${col_yellow}Set hostname to${col_reset}: "
78     read opt_hostname
79 }
80
81 assert_system_essid()
82 {
83     test -z "$opt_essid" || return
84
85     if [ -f /etc/musiikki.conf ]
86     then
87         opt_essid=$(grep '^title=' /etc/musiikki.conf)
88         opt_essid=${opt_essid#title=}
89     else
90         if [ -f /etc/hostapd/hostapd.conf ]
91         then
92             opt_essid=$(grep '^ssid=' /etc/hostapd/hostapd.conf)
93             opt_essid=${opt_essid#ssid=}
94         else
95             echo -n "${col_yellow}Set Wireless ESSID to${col_reset}: "
96             read opt_essid
97         fi
98     fi
99 }
100
101 assert_system_wpakey()
102 {
103     test -z "$opt_wpakey" || return
104
105     if [ -f /etc/hostapd/hostapd.conf ]
106     then
107         opt_wpakey=$(grep '^wpa_passphrase=' /etc/hostapd/hostapd.conf)
108         opt_wpakey=${opt_wpakey#wpa_passphrase=}
109     else
110         echo -n "${col_yellow}Set Wireless WPA key to${col_reset}: "
111         read opt_wpakey
112     fi
113 }
114
115 assert_system_subnet()
116 {
117     test -z "$opt_subnet" || return
118
119
120     if grep -q '^server\.bind' /etc/lighttpd/lighttpd.conf
121     then
122         opt_subnet=$(grep '^server\.bind\s' /etc/lighttpd/lighttpd.conf)
123         opt_subnet=${opt_subnet#*\"}
124         opt_subnet=${opt_subnet%.1\"*}
125     else
126         echo -n "${col_yellow}Set subnet (3 octets only) to${col_reset}: "
127         read opt_subnet
128     fi
129 }
130
131 # ------------------------------------------------------------
132
133 welcome()
134 {
135     echo
136     echo_ok_bold "Installing Infodrom MediaServer"
137     echo
138 }
139
140 assert_root()
141 {
142     if [ "$(/usr/bin/whoami)" != "root" ]
143     then
144         echo_error "The installation needs to be done as root"
145         return 1
146     fi
147
148     return 0
149 }
150
151 assert_directory()
152 {
153     if [ ! -x install-system ]
154     then
155         echo_error "Current directory lacks executable install-system"
156         return 1
157     fi
158
159     if [ ! -d bin ]
160     then
161         echo_error "Current directory lacks bin directory"
162         return 1
163     fi
164
165     if [ ! -d config-templates ]
166     then
167         echo_error "Current directory lacks config-templates directory"
168         return 1
169     fi
170
171     if [ "$(pwd)" != "${opt_destdir}" -a -x /usr/bin/rsync ]
172     then
173         assert_system_uidgid
174
175         # Relocate ourself
176         rsync -arH ./ ${opt_destdir}/
177         chown -R ${opt_uid}.${opt_gid} ${opt_destdir}
178     fi
179
180     return 0
181 }
182
183 assert_hostname()
184 {
185     if [ "$(cat /etc/hostname)" = "$default_hostname" ]
186     then
187         echo_verbose "Default hostname found"; echo_newline_verbose
188         assert_system_hostname
189         echo $opt_hostname > /etc/hostname
190         printf "192.168.1.1\t%s\n" "$opt_hostname" >> /etc/hosts
191     fi
192 }
193
194 assert_programs()
195 {
196
197     echo_verbose "Looking for programs: "
198
199     echo_verbose "sed... "
200     if [ ! -x /bin/sed ]
201     then
202         echo_newline_verbose
203         echo_error "Program sed missing"
204         return 1
205     fi
206
207     echo_verbose "dpkg... "
208     if [ ! -x /usr/bin/dpkg ]
209     then
210         echo_newline_verbose
211         echo_error "Program dpkg missing"
212         return 1
213     fi
214
215     echo_verbose "apt-get... "
216     if [ -x /usr/bin/apt-get ]
217     then
218         opt_apt=/usr/bin/apt-get
219     else
220         echo_verbose "apt... "
221         if [ -x /usr/bin/apt ]
222         then
223             opt_apt=/usr/bin/apt
224         else
225             echo_verbose "aptitude... "
226             if [ -x /usr/bin/aptitude ]
227             then
228                 opt_apt=/usr/bin/aptitude
229             else
230                 echo_newline_verbose
231                 echo_error "High-level package manager missing (apt-get/apt/aptitude)"
232                 return 1
233             fi
234         fi
235     fi
236
237     echo_newline_verbose
238
239     return 0
240 }
241
242 assert_no_recommends()
243 {
244     if ! grep -q 'APT::Install-Recommends False;' /etc/apt/apt.conf.d/*
245     then
246         echo_ok "Configure APT not to install recommended packages"
247         echo 'APT::Install-Recommends False;' > /etc/apt/apt.conf.d/40recommends
248     fi
249 }
250
251 install_packages()
252 {
253     if [ -z "$opt_apt" ]
254     then
255         echo_error "No high-level package manager available"
256         return 1
257     fi
258
259     set -e
260     $opt_apt update
261     $opt_apt install -y $*
262     set +e
263
264     return 0
265 }
266
267 remove_packages()
268 {
269     if [ -z "$opt_apt" ]
270     then
271         echo_error "No high-level package manager available"
272         return 1
273     fi
274
275     set -e
276     $opt_apt purge -y $*
277     set +e
278
279     return 0
280 }
281
282 package_exists()
283 {
284     local pkg=$1
285
286     if [ -f "/var/lib/dpkg/info/${pkg}.list" ]
287     then
288         return 0
289     else
290         return 1
291     fi
292 }
293
294 assert_packages()
295 {
296     missing=
297
298     echo_verbose "Looking for installed packages: "
299
300     for pkg in sudo super rsync hostapd isc-dhcp-server minidlna lighttpd samba \
301                     php7.0-cgi php7.0-json php7.0-sqlite3
302     do
303         echo_verbose "${pkg}... "
304         if ! package_exists ${pkg}
305         then
306             missing="$missing $pkg"
307         fi
308     done
309
310     echo_newline_verbose
311
312     if [ -n "$missing" ]
313     then
314         echo_ok "Installing$missing"
315         install_packages $missing || return 1
316         echo_ok "Successfully installed$missing"
317     fi
318
319     toomuch=
320
321     echo_verbose "Looking for installed packages: "
322
323     for pkg in triggerhappy avahi-daemon
324     do
325         echo_verbose "${pkg}... "
326         if package_exists ${pkg}
327         then
328             toomuch="$toomuch $pkg"
329         fi
330     done
331
332     echo_newline_verbose
333
334     if [ -n "$toomuch" ]
335     then
336         echo_ok "Removing$toomuch"
337         remove_packages $toomuch || return 1
338         echo_ok "Successfully removed$toomuch"
339     fi
340 }
341
342 assert_musiikki()
343 {
344     if [ ! -f /etc/musiikki.conf ]
345     then
346         assert_system_essid
347
348         echo_ok "Installing main configuration"
349         sed "s/{essid}/${opt_essid}/g;s!{basedir}!${opt_destdir}!g;s!{htpasswd}!${opt_adminpwfile}!g" config-templates/musiikki.conf.template > /root/.template
350         install -o root -g root -m 644 /root/.template /etc/musiikki.conf
351         rm -f /root/.template
352     fi
353 }
354
355 assert_hostapd()
356 {
357     if [ ! -f /etc/hostapd/hostapd.conf ]
358     then
359         assert_system_essid
360         assert_system_wpakey
361
362         echo_ok "Create hostapd configuration"
363         sed "s/{essid}/${opt_essid}/g;s/{wpakey}/${opt_wpakey}/g" config-templates/hostapd.conf.template > /root/.template
364         install -o root -g root -m 644 /root/.template /etc/hostapd/hostapd.conf
365         rm -f /root/.template
366     fi
367
368     if grep -q '#DAEMON_CONF=""' /etc/default/hostapd
369     then
370         echo_ok "Activate hostapd"
371         sed -i 's!#DAEMON_CONF=""!DAEMON_CONF="/etc/hostapd/hostapd.conf"!' /etc/default/hostapd
372     fi
373 }
374
375 assert_network()
376 {
377     if ! grep -q '^source /etc/network/interfaces.d/wlan1.conf' /etc/network/interfaces
378     then
379         echo >> /etc/network/interfaces
380         echo 'source /etc/network/interfaces.d/wlan1.conf' >> /etc/network/interfaces
381         echo >> /etc/network/interfaces
382     fi
383
384     sed -i 's!#net.ipv4.ip_forward=1!net.ipv4.ip_forward=1!' /etc/sysctl.conf
385     if ! grep -q '^net.ipv4.ip_forward=1' /etc/sysctl.conf
386     then
387         echo >> /etc/sysctl.conf
388         echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
389     fi
390
391     # iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
392     # iptables -A FORWARD -i wlan0 -o wlan1 -m state --state RELATED,ESTABLISHED -j ACCEPT
393     # iptables -A FORWARD -i wlan1 -o wlan0 -j ACCEPT
394     # iptables-save > /srv/musiikki/iptables_nat.conf
395 }
396
397 assert_dhcpd()
398 {
399     assert_system_subnet
400
401     if ! grep -q "^subnet ${opt_subnet}.0 " /etc/dhcp/dhcpd.conf
402     then
403         echo_ok "Add DHCP configuration"
404         sed "s/{subnet}/${opt_subnet}/g" config-templates/dhcpd.conf.template >> /etc/dhcp/dhcpd.conf
405     fi
406
407     if grep -q 'INTERFACESv4=""' /etc/default/isc-dhcp-server
408     then
409         echo_ok "Add WLAN interface to DCHP config"
410         sed -i 's/INTERFACESv4=".*"/INTERFACESv4="wlan0"/' /etc/default/isc-dhcp-server
411     fi
412 }
413
414 assert_super()
415 {
416     echo_verbose "Asserting permissions "
417     for prg in $(cd bin && echo *)
418     do
419         test $prg != musiikki-rescan || continue
420
421         echo_verbose "${prg}... "
422         if ! grep -q "^${prg}" /etc/super.tab
423         then
424             printf "%s\t%s/bin/%s www-data u+g=root password=n\n" ${prg} ${opt_destdir} ${prg} >> /etc/super.tab
425         fi
426     done
427
428     echo_newline_verbose
429 }
430
431 assert_minidlna()
432 {
433     assert_system_essid
434     assert_system_subnet
435
436     if ! grep -q "^friendly_name=${opt_essid}" /etc/minidlna.conf
437     then
438         sed -i "s!#friendly_name=!friendly_name=${opt_essid}!;s!^media_dir=.*!!" /etc/minidlna.conf
439         sed -i "s!#listening_ip=!listening_ip=${opt_subnet}.1!" /etc/minidlna.conf
440         sed -i "s!#db_dir=.*!db_dir=/media/music/cache!" /etc/minidlna.conf
441
442         echo "" >>  /etc/minidlna.conf
443         echo "model_name=${opt_essid}" >>  /etc/minidlna.conf
444         echo "media_dir=A,/media/music/music" >>  /etc/minidlna.conf
445         echo "media_dir=P,/media/music/pictures" >>  /etc/minidlna.conf
446         echo "media_dir=V,/media/music/video" >>  /etc/minidlna.conf
447     fi
448
449     test -d /media/music/cache || install -d -m 755 /media/music/cache
450     chown -R minidlna:minidlna /media/music/cache
451
452     test -d /media/music/music/playlists || install -d -m 755 /media/music/music/playlists
453     chown -R www-data:www-data /media/music/music/playlists
454 }
455
456 assert_lighttpd()
457 {
458     echo_verbose "Asserting lighttpd modules "
459
460
461     echo_verbose "05-auth... "
462     if [ ! -f /etc/lighttpd/conf-enabled/05-auth.conf ]
463     then
464        assert_system_essid
465
466        cat <<EOT > /etc/lighttpd/conf-enabled/05-auth.conf
467 # /usr/share/doc/lighttpd/authentication.txt.gz
468
469 server.modules                += ( "mod_auth" )
470
471 auth.backend                 = "plain"
472 auth.backend.plain.userfile  = "${opt_adminpwfile}"
473 # auth.backend.plain.groupfile = "lighttpd.group"
474
475 auth.require                   = ( "/admin/" =>
476                                    (
477                                      "method"  => "basic",
478                                      "realm"   => "${opt_essid}",
479                                      "require" => "user=admin",
480                                    )
481                                  )
482 EOT
483     fi
484
485     for name in 10-fastcgi 15-fastcgi-php
486     do
487         echo_verbose "${name}... "
488         if [ ! -f /etc/lighttpd/conf-enabled/${name}.conf ]
489         then
490             ln -s ../conf-available/${name}.conf /etc/lighttpd/conf-enabled/${name}.conf
491         fi
492     done
493
494     echo_newline_verbose
495
496     if [ ! -f ${opt_adminpwfile} ]
497     then
498         echo -n "${col_yellow}Set web admin password to${col_reset}: "
499         read admin_pw
500
501         echo "admin:${admin_pw}" > ${opt_adminpwfile}
502     fi
503
504     chown www-data.www-data ${opt_adminpwfile}
505
506     if ! grep -q '^server.tag' /etc/lighttpd/lighttpd.conf
507     then
508         assert_system_essid
509         assert_system_subnet
510
511         sed -i "s!^server.document-root\\s*=\\s*\".*\"!server.document-root        = \"${opt_destdir}/html\"!" /etc/lighttpd/lighttpd.conf
512         echo "server.tag                  = \"${opt_essid}\"" >> /etc/lighttpd/lighttpd.conf
513         echo "server.bind                 = \"${opt_subnet}.1\"" >> /etc/lighttpd/lighttpd.conf
514     fi
515 }
516
517 assert_samba()
518 {
519     if ! grep -q '^\[musicread\]$' /etc/samba/smb.conf
520     then
521         echo_ok "Add shares to Samba"
522         sed "s/{essid}/${opt_essid}/g" config-templates/smb.conf.template >> /etc/samba/smb.conf
523     fi
524
525     if ! grep -q '^musicread:' /etc/passwd
526     then
527         assert_system_essid
528
529         echo_ok "Create read/only Samba account"
530         adduser --quiet --disabled-password --system --group --no-create-home --home /var/lib/misc --gecos "${opt_essid} read/only account" musicread
531     fi
532
533     if ! grep -q '^musicwrite:' /etc/passwd
534     then
535         assert_system_essid
536
537         echo_ok "Create read/write Samba account"
538         adduser --quiet --disabled-password --system --group --no-create-home --home /var/lib/misc --gecos "${opt_essid} read/write account" musicwrite
539     fi
540 }
541
542 assert_cron()
543 {
544     if [ ! -f /etc/cron.d/musiikki ]
545     then
546         echo_ok "Install crontab"
547         cp config-templates/cron.d.template /etc/cron.d/musiikki
548     fi
549
550     if [ ! -d /var/local/musiikki ]
551     then
552         install -d -o minidlna -g minidlna -m 755 /var/local/musiikki
553     fi
554
555     if [ ! -d /var/local/musiikki/rescan ]
556     then
557         install -d -o minidlna -g minidlna -m 777 /var/local/musiikki/rescan
558     fi
559 }
560
561
562 welcome
563
564 assert_root || exit 2
565 assert_directory || exit 2
566 assert_hostname || exit 2
567 assert_programs || exit 2
568 assert_no_recommends || exit 2
569 assert_packages || exit 2
570
571 assert_musiikki
572 assert_hostapd
573 assert_network
574 assert_dhcpd
575 assert_super
576 assert_minidlna
577 assert_lighttpd
578 assert_samba
579 assert_cron