Bug 23090: Add some special characters
[koha.git] / debian / scripts / koha-create
1 #!/bin/bash
2 #
3 # koha-create -- Create a new Koha instance.
4 # Copyright 2010  Catalyst IT, Ltd
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 # Read configuration variable file if it is present
20 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
21
22 set -e
23
24 # include helper functions
25 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
26     . "/usr/share/koha/bin/koha-functions.sh"
27 else
28     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
29     exit 1
30 fi
31
32 usage()
33 {
34     local scriptname=$0
35     cat <<EOF
36
37 Creates new Koha instances.
38
39 Usage:
40
41   $scriptname [DB usage mode] [options] instancename
42
43 DB usage mode:
44   --create-db               Create a new database on localhost. (default).
45   --request-db              Creates a instancename-db-request.txt file where
46                             you adjust your DB settings and re-run with --populate-db.
47   --populate-db             Finish the installation you started with --request-db after
48                             you adjusted the instancename-db-request.txt file.
49   --use-db                  Use this option if you already created and populated your DB.
50
51 Options:
52   --marcflavor flavor       Set the MARC flavor. Valid values are marc21 (default),
53                             normarc and unimarc.
54   --zebralang lang          Choose the primary language for Zebra indexing. Valid
55                             values are cs, en (default), es, fr, gr, nb, ru and uk.
56   --memcached-servers str   Set a comma-separated list of host:port memcached servers.
57   --memcached-prefix str    Set the desired prefix for the instance memcached namespace.
58   --enable-sru              Enable the Z39.50/SRU server (default: disabled).
59   --sru-port                Specifiy a TCP port number for the Z39.50/SRU server
60                             to listen on. (default: 7090).
61   --defaultsql some.sql     Specify a default SQL file to be loaded on the DB.
62   --configfile cfg_file     Specify an alternate config file for reading default values.
63   --passwdfile passwd       Specify an alternate passwd file.
64   --dbhost host             Enforce the use of the specified DB server
65   --database dbname         Enforce the use of the specified DB name (64 char limit)
66   --adminuser n             Explicit the admin user ID in the DB. Relevant in
67                             conjunction with --defaultsql and --populate-db.
68   --template-cache-dir      Set a user defined template_cache_dir. It defaults to
69                             /var/cache/koha/<instance>/templates
70   --timezone time/zone      Specify a timezone. e.g. America/Argentina
71   --upload-path dir         Set a user defined upload_path. It defaults to
72                             /var/lib/koha/<instance>/uploads
73   --tmp-path dir            Set a user defined tmp_path. It defaults to
74                             /var/lib/koha/<instance>/tmp
75   --letsencrypt             Set up a https-only site with letsencrypt certificates
76   --help,-h                 Show this help.
77
78 Note: the instance name cannot be longer that 11 chars.
79
80 EOF
81 }
82
83 # UPPER CASE VARIABLES - from configfile or default value
84 # lower case variables - generated within this script
85 generate_config_file() {
86     touch "$2"
87     chown "root:$username" "$2"
88     chmod 0640 "$2"
89     sed -e "s/__KOHA_CONF_DIR__/\/etc\/koha\/sites\/$name/g" \
90         -e "s/__KOHASITE__/$name/g" \
91         -e "s/__OPACPORT__/$OPACPORT/g" \
92         -e "s/__INTRAPORT__/$INTRAPORT/g" \
93         -e "s/__OPACSERVER__/$opacdomain/g" \
94         -e "s/__INTRASERVER__/$intradomain/g" \
95         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
96         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
97         -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
98         -e "s/__SRU_BIBLIOS_PORT__/$SRU_SERVER_PORT/g" \
99         -e "s/__START_SRU_PUBLICSERVER__/$START_SRU_PUBLICSERVER/g" \
100         -e "s/__END_SRU_PUBLICSERVER__/$END_SRU_PUBLICSERVER/g" \
101         -e "s/__API_SECRET__/$API_SECRET/g" \
102         -e "s/__DB_NAME__/$mysqldb/g" \
103         -e "s/__DB_HOST__/$mysqlhost/g" \
104         -e "s/__DB_USER__/$mysqluser/g" \
105         -e "s/__DB_PASS__/$mysqlpwd/g" \
106         -e "s/__UNIXUSER__/$username/g" \
107         -e "s/__UNIXGROUP__/$username/g" \
108         -e "s#__TEMPLATE_CACHE_DIR__#$TEMPLATE_CACHE_DIR#g" \
109         -e "s#__TIMEZONE__#$TIMEZONE#g" \
110         -e "s#__UPLOAD_PATH__#$UPLOAD_PATH#g" \
111         -e "s#__TMP_PATH__#$TMP_PATH#g" \
112         -e "s/__LOG_DIR__/\/var\/log\/koha\/$name/g" \
113         -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
114         -e "s/__MEMCACHED_NAMESPACE__/$MEMCACHED_NAMESPACE/g" \
115         -e "s/__MEMCACHED_SERVERS__/$MEMCACHED_SERVERS/g" \
116         "/etc/koha/$1" > "$2"
117
118 }
119
120 getmysqlhost() {
121     if [ ! -f /etc/mysql/debian.cnf ]
122     then
123         echo localhost
124         return
125     fi
126     awk '
127         BEGIN { FS="=" }
128         $1 ~/\[/ { inclient=0 }
129         $1 ~/\[client\]/ { inclient=1; next }
130         inclient==1 && $1 ~/host/ { gsub(/ /, "", $2); print $2 }' \
131         /etc/mysql/koha-common.cnf
132 }
133
134 getinstancemysqlpassword() {
135     xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
136 }
137
138 getinstancemysqluser() {
139     xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
140 }
141
142 getinstancemysqldatabase() {
143     xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
144 }
145
146 check_apache_config()
147 {
148
149     # Check that mpm_itk is installed and enabled
150     if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_itk'; then
151         # Check Apache version
152         APACHE_DISABLE_MPM_MSG=""
153         if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
154             # mpm_event or mpm_worker need to be disabled first. mpm_itk depends
155             # on mpm_prefork, which is enabled if needed. See
156             # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=734865
157             if /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_event'; then
158                 APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_event ;"
159             elif /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_worker'; then
160                 APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_worker ;"
161             # else mpm_prefork: a2enmod mpm_itk works
162             fi
163         # else Apache 2.2: a2enmod mpm_itk works
164         fi
165
166         cat 1>&2  <<EOM
167
168 Koha requires mpm_itk to be enabled within Apache in order to run.
169 Typically this can be enabled with:
170
171    $APACHE_DISABLE_MPM_MSG sudo a2enmod mpm_itk
172 EOM
173
174         die
175     fi
176
177     # Check that mod_rewrite is installed and enabled.
178     if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'rewrite_module'; then
179         cat 1>&2  <<EOM
180
181 Koha requires mod_rewrite to be enabled within Apache in order to run.
182 Typically this can be enabled with:
183
184     sudo a2enmod rewrite
185 EOM
186         die
187     fi
188
189     # Check that the CGI module is installed and enabled
190     # (Apache 2.4 may not have it by default.)
191     if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'cgi_module'; then
192         cat 1>&2 << EOM
193 Koha requires mod_cgi to be enabled within Apache in order to run.
194 Typically this can be enabled with:
195
196     sudo a2enmod cgi
197 EOM
198         die
199     fi
200
201     # Check that mod_ssl is installed and enabled.
202     if [ "$CLO_LETSENCRYPT" = "yes" ]; then
203         if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'ssl_module'; then
204             cat 1>&2  <<EOM
205
206 Koha requires mod_ssl to be enabled within Apache in order to run with --letsencrypt.
207 Typically this can be enabled with:
208
209     sudo a2enmod ssl
210 EOM
211             die
212         fi
213     fi
214
215 }
216
217 set_memcached()
218 {
219     local instance="$1"
220
221     if [ "$CLO_MEMCACHED_SERVERS" != "" ]; then
222         MEMCACHED_SERVERS=$CLO_MEMCACHED_SERVERS
223     else
224         if [ "$MEMCACHED_SERVERS" = "" ]; then
225             MEMCACHED_SERVERS=$DEFAULT_MEMCACHED_SERVERS
226         # else: was set by the koha-sites.conf file
227         fi
228     fi
229
230     if [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
231         MEMCACHED_NAMESPACE="$CLO_MEMCACHED_PREFIX$instance"
232     else
233         if [ "$MEMCACHED_PREFIX" != "" ]; then
234             MEMCACHED_NAMESPACE="$MEMCACHED_PREFIX$instance"
235         else
236             MEMCACHED_NAMESPACE="$DEFAULT_MEMCACHED_PREFIX$instance"
237         fi
238     fi
239
240 }
241
242 set_upload_path()
243 {
244     local instance="$1"
245
246     if [ "$CLO_UPLOAD_PATH" != "" ]; then
247         UPLOAD_PATH=$CLO_UPLOAD_PATH
248     else
249         UPLOAD_PATH="$INSTANCE_PATH_BASE/$instance/$UPLOAD_DIR"
250     fi
251 }
252
253 set_tmp_path()
254 {
255     local instance="$1"
256
257     if [ "$CLO_TMP_PATH" != "" ]; then
258         TMP_PATH=$CLO_TMP_PATH
259     else
260         TMP_PATH="$INSTANCE_PATH_BASE/$instance/$TMP_DIR"
261     fi
262 }
263
264 enable_sru_server()
265 {
266     # remove the commenting symbols
267     START_SRU_PUBLICSERVER=""
268     END_SRU_PUBLICSERVER=""
269     if [ "$SRU_SERVER_PORT" = "" ]; then
270         # --sru-port not passed, use the default
271         SRU_SERVER_PORT=$DEFAULT_SRU_SERVER_PORT
272     fi
273 }
274
275 check_letsencrypt()
276 {
277     if [ $(dpkg-query -W -f='${Status}' letsencrypt 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
278         set +e
279         apt-cache show letsencrypt &>/dev/null
280         local aptcacheshow=$?
281         set -e
282         if [ $aptcacheshow -eq 0 ]; then
283                 read -r -p "The letsencrypt package is not installed. Do it now?  [y/N] " response
284                 if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
285                     local debrelease="$(lsb_release -c -s)"
286                     if [ $debrelease = "jessie" ]; then
287                         apt-get install -y -t jessie-backports letsencrypt
288                     else
289                         apt-get install -y letsencrypt
290                     fi
291                 else
292                     die "You have to install letsencrypt to use the --letsencrypt parameter."
293                 fi
294         else
295             echo "No installation candidate available for package letsencrypt."
296             if [[ -f /usr/bin/letsencrypt ]]; then
297                 read -r -p "If you have a symlink from /usr/bin/letsencrypt to letsencrypt-auto, it should work. [y/N] " response
298                 if [[ ! $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
299                     die "You have to install letsencrypt to use the --letsencrypt parameter."
300                 fi
301             else
302                 die "You can create a symlink from /usr/bin/letsencrypt to letsencrypt-auto."
303             fi
304         fi
305     fi
306 }
307
308 letsencrypt_instance()
309 {
310     # Get letsencrypt certificates
311     letsencrypt --agree-tos --renew-by-default --webroot certonly \
312         -w /usr/share/koha/opac/htdocs/ -d $opacdomain -w /usr/share/koha/intranet/htdocs/ -d $intradomain
313     # enable all ssl settings (apache won't start with these before certs are present)
314     sed -i "s:^\s*#\(\s*SSL.*\)$:\1:" "/etc/apache2/sites-available/$name.conf"
315     # change port from 80 to 443. (apache won't start if it is 443 without certs present)
316     sed -i "s:^\s*\(<VirtualHost \*\:\)80> #https$:\1443>:" "/etc/apache2/sites-available/$name.conf"
317     # enable redirect from http to https on port 80
318     sed -i "s:^\s*#\(.*\)#nohttps$:\1:" "/etc/apache2/sites-available/$name.conf"
319     # make koha-list --letsencrypt aware of this instance # could be done by checking apache conf instead
320     echo -e "opacdomain=\"$opacdomain\"\nintradomain=\"$intradomain\"" > /var/lib/koha/$name/letsencrypt.enabled
321     # restart apache with working certs
322     service apache2 restart
323 }
324
325 # Set defaults and read config file, if it exists.
326 DOMAIN=""
327 OPACPORT="80"
328 OPACPREFIX=""
329 OPACSUFFIX=""
330 INTRAPORT="8080"
331 INTRAPREFIX=""
332 INTRASUFFIX=""
333 DEFAULTSQL=""
334 ZEBRA_MARC_FORMAT="marc21"
335 ZEBRA_LANGUAGE="en"
336 ADMINUSER="1"
337 PASSWDFILE="/etc/koha/passwd"
338
339 # memcached variables
340 USE_MEMCACHED="yes"
341 MEMCACHED_SERVERS=""
342 MEMCACHED_PREFIX=""
343 # hardcoded memcached defaults
344 DEFAULT_MEMCACHED_SERVERS="127.0.0.1:11211"
345 DEFAULT_MEMCACHED_PREFIX="koha_"
346 # hardcoded instance base path
347 INSTANCE_PATH_BASE="/var/lib/koha"
348 UPLOAD_DIR="uploads"
349 UPLOAD_PATH=""
350 # timezone defaults to empty
351 TIMEZONE=""
352 # hardcoded upload_tmp_path
353 TMP_DIR="tmp"
354 TMP_PATH=""
355 # cache base dir
356 CACHE_DIR_BASE="/var/cache/koha"
357 # Generate a randomizaed API secret
358 API_SECRET="$(pwgen -s 64 1)"
359 # SRU server variables
360 ENABLE_SRU="no"
361 SRU_SERVER_PORT=""
362 # hardcoded default SRU server port
363 DEFAULT_SRU_SERVER_PORT="7090"
364 START_SRU_PUBLICSERVER="<!--"
365 END_SRU_PUBLICSERVER="-->"
366
367 APACHE_CONFIGFILE=""
368
369 if [ -e /etc/koha/koha-sites.conf ]
370 then
371     . /etc/koha/koha-sites.conf
372 fi
373
374 [ $# -ge 1 ] && [ $# -le 16 ] || ( usage ; die "Error: wrong parameters" )
375
376 TEMP=`getopt -o chrpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,enable-sru,sru-port:,help,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,dbhost:,database:,adminuser:,memcached-servers:,memcached-prefix:,template-cache-dir:,timezone:,upload-path:,tmp-path:,letsencrypt, \
377      -n "$0" -- "$@"`
378
379 # Note the quotes around `$TEMP': they are essential!
380 eval set -- "$TEMP"
381
382 # Temporary variables for the command line options
383 CLO_ZEBRA_MARC_FORMAT=""
384 CLO_ZEBRA_LANGUAGE=""
385 CLO_DEFAULTSQL=""
386 CLO_ADMINUSER=""
387 CLO_MEMCACHED_SERVERS=""
388 CLO_MEMCACHED_PREFIX=""
389 CLO_UPLOAD_PATH=""
390 CLO_TMP_PATH=""
391 CLO_LETSENCRYPT=""
392 CLO_TEMPLATE_CACHE_DIR=""
393 CLO_TIMEZONE=""
394
395 while true ; do
396     case "$1" in
397         -c|--create-db)
398             op=create ; shift ;;
399         -r|--request-db)
400             op=request ; shift ;;
401         -p|--populate-db)
402             op=populate ; shift ;;
403         -u|--use-db)
404             op=use ; shift ;;
405         --memcached-servers)
406             CLO_MEMCACHED_SERVERS="$2" ; shift 2 ;;
407         --memcached-prefix)
408             CLO_MEMCACHED_PREFIX="$2" ; shift 2;;
409         -m|--marcflavor)
410             CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
411         -l|--zebralang)
412             CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
413         -d|--defaultsql)
414             CLO_DEFAULTSQL="$2" ; shift 2 ;;
415         -f|--configfile)
416             configfile="$2" ; shift 2 ;;
417         -s|--passwdfile)
418             CLO_PASSWDFILE="$2" ; shift 2 ;;
419         -b|--database)
420             CLO_DATABASE="$2" ; shift 2 ;;
421         --dbhost)
422             CLO_DBHOST="$2" ; shift 2 ;;
423         -a|--adminuser)
424             CLO_ADMINUSER="$2" ; shift 2 ;;
425         --enable-sru)
426             ENABLE_SRU="yes" ; shift ;;
427         --sru-port)
428             SRU_SERVER_PORT="$2" ; shift 2 ;;
429         --template-cache-dir)
430             CLO_TEMPLATE_CACHE_DIR="$2" ; shift 2 ;;
431         --timezone)
432             CLO_TIMEZONE="$2" ; shift 2 ;;
433         --upload-path)
434             CLO_UPLOAD_PATH="$2" ; shift 2 ;;
435         --tmp-path)
436             CLO_TMP_PATH="$2" ; shift 2 ;;
437         --letsencrypt)
438             CLO_LETSENCRYPT="yes" ; shift ;;
439         -h|--help)
440             usage ; exit 0 ;;
441         --)
442             shift ; break ;;
443         *)
444             die "Internal error processing command line arguments" ;;
445     esac
446 done
447
448 # Load the configfile given on the command line
449 if [ "$configfile" != "" ]
450 then
451     if [ -e "$configfile" ]
452     then
453         . "$configfile"
454     else
455         die "$configfile does not exist.";
456     fi
457 fi
458
459 # Make sure options from the command line get the highest precedence
460 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
461 then
462     ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
463 fi
464 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
465 then
466     ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
467 fi
468 if [ "$CLO_DEFAULTSQL" != "" ]
469 then
470     DEFAULTSQL="$CLO_DEFAULTSQL"
471 fi
472 if [ "$CLO_ADMINUSER" != "" ]
473 then
474     ADMINUSER="$CLO_ADMINUSER"
475 fi
476 if [ "$CLO_PASSWDFILE" != "" ]
477 then
478     PASSWDFILE="$CLO_PASSWDFILE"
479 fi
480
481 if [ "$CLO_TIMEZONE" != "" ]; then
482     TIMEZONE=$CLO_TIMEZONE
483 fi
484
485 if [ "$ENABLE_SRU" != "no" ]; then
486     enable_sru_server
487 fi
488
489 [ $# -ge 1 ] || ( usage ; die "Missing instance name..." )
490
491 name="$1"
492
493 set_upload_path $name
494 set_tmp_path $name
495
496 if [ "$op" = use ] && [ "$CLO_DATABASE" = "" ] &&
497    ( [ ! -f "$PASSWDFILE" ] || [ ! `cat $PASSWDFILE | grep "^$name:"` ] )
498 then
499     cat <<NO_DB
500 --use-db must have a database name. It can be specified in a readable
501 password file ($PASSWDFILE). Using --passwdfile overrides the default
502 /usr/koha/passwd file. Each line of a passwd file should be in the format of:
503     instance:username:password:dbname:dbhost
504 A database name can also be specified using '--database dbname'.
505 NO_DB
506     die;
507 fi
508
509 if [ "$USE_MEMCACHED" = "no" ]; then
510     MEMCACHED_SERVERS=""
511     MEMCACHED_NAMESPACE=""
512     MEMCACHED_PREFIX=""
513 else
514     set_memcached $name
515 fi
516
517 # Set template cache dir
518 if [ "$CLO_TEMPLATE_CACHE_DIR" != "" ]; then
519     TEMPLATE_CACHE_DIR="$CLO_TEMPLATE_CACHE_DIR"
520 else
521     TEMPLATE_CACHE_DIR="$CACHE_DIR_BASE/$name/templates"
522 fi
523
524 # Are we root? If not, the mod_rewrite check will fail and be confusing, so
525 # we look into this first.
526 if [[ $UID -ne 0 ]]
527 then
528     die "This script must be run with root privileges."
529 fi
530
531 # Check everything is ok with Apache, die otherwise
532 check_apache_config
533
534 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
535 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
536
537 # Check everything is ok with letsencrypt, die otherwise
538 if [ "$CLO_LETSENCRYPT" = "yes" ]; then
539     check_letsencrypt
540 fi
541
542 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
543 then
544     passwdline=`cat $PASSWDFILE | grep "^$name:"`
545     mysqluser=`echo $passwdline | cut -d ":" -f 2`
546     mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
547     mysqldb=`echo $passwdline | cut -d ":" -f 4`
548     mysqlhost=`echo $passwdline | cut -d ":" -f 5`
549 fi
550
551 # The order of precedence for MySQL database name is:
552 # default < passwd file < command line
553 if [ "$mysqldb" = "" ]
554 then
555     mysqldb="koha_$name"
556 fi
557
558 if [ "$CLO_DATABASE" != "" ]
559 then
560     mysqldb="$CLO_DATABASE"
561 fi
562
563 if [ "$mysqluser" = "" ]
564 then
565     mysqluser="koha_$name"
566 fi
567
568 if [ "$CLO_DBHOST" != "" ]
569 then
570     mysqlhost="$CLO_DBHOST"
571 fi
572
573 if [ "$mysqlhost" = "" ]
574 then
575     mysqlhost="$(getmysqlhost)"
576 fi
577
578 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
579 then
580     if [ "$mysqlpwd" = "" ]
581     then
582         # over-size, so when problematic chars are removed,
583         # still likely 16 characters left.
584         mysqlpwd="$(pwgen -s -y 32 1)"
585         mysqlpwd="$(echo $mysqlpwd | tr -d :\'\\\<\>\/ | cut -c1-16)"
586     fi
587 else
588     mysqlpwd="$(getinstancemysqlpassword $name)"
589 fi
590
591
592 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
593 then
594     # Create new user and group.
595     username="$name-koha"
596     if getent passwd "$username" > /dev/null
597     then
598         die "User $username already exists."
599     fi
600     if getent group "$username" > /dev/null
601     then
602         die "Group $username already exists."
603     fi
604     adduser --no-create-home --disabled-login \
605         --gecos "Koha instance $username" \
606         --home "/var/lib/koha/$name" \
607         --quiet "$username"
608
609     # Create the site-specific directories.
610     koha-create-dirs "$name"
611
612     # Generate Zebra database password.
613     zebrapwd="$(pwgen -s 16 1)"
614     # Future enhancement: make this configurable for when your db is on
615     # another server.
616     mysql_hostname="localhost"
617     # Set up MySQL database for this instance.
618     if [ "$op" = create ]
619     then
620         if [ ! -e /etc/mysql/debian.cnf ]; then
621             MYSQL_OPTIONS="-u root"
622             echo "WARNING: The koha-common.cnf file is a dead soft link!"
623         else
624             MYSQL_OPTIONS="--defaults-extra-file=/etc/mysql/koha-common.cnf"
625         fi
626         mysql $MYSQL_OPTIONS <<eof
627 CREATE DATABASE \`$mysqldb\`;
628 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
629 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`@'$mysql_hostname';
630 FLUSH PRIVILEGES;
631 eof
632     fi #`
633
634     if [ "$CLO_LETSENCRYPT" = "yes" ]; then
635         APACHE_CONFIGFILE="apache-site-https.conf.in"
636     else
637         APACHE_CONFIGFILE="apache-site.conf.in"
638     fi
639     # Generate and install Apache site-available file and log dir.
640     generate_config_file $APACHE_CONFIGFILE \
641         "/etc/apache2/sites-available/$name.conf"
642     mkdir "/var/log/koha/$name"
643     chown "$username:$username" "/var/log/koha/$name"
644
645
646     # Generate and install main Koha config file.
647     generate_config_file koha-conf-site.xml.in \
648         "/etc/koha/sites/$name/koha-conf.xml"
649
650     # Generate and install the log4perl config file.
651     generate_config_file log4perl-site.conf.in \
652         "/etc/koha/sites/$name/log4perl.conf"
653
654     # Generate and install Zebra config files.
655     generate_config_file zebra-biblios-dom-site.cfg.in \
656         "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
657     generate_config_file zebra-authorities-dom-site.cfg.in \
658         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
659     generate_config_file zebra.passwd.in \
660         "/etc/koha/sites/$name/zebra.passwd"
661
662     # Create a GPG-encrypted file for requesting a DB to be set up.
663     if [ "$op" = request ]
664     then
665         touch "$name-db-request.txt"
666         chmod 0600 "$name-db-request.txt"
667         cat > "$name-db-request.txt" << eof
668 Please create a MySQL database and user on $mysqlhost as follows:
669
670 database name: $mysqldb
671 database user: $mysqluser
672      password: $mysqlpwd
673
674 Thank you.
675 eof
676
677         echo "See $name-db-request.txt for database creation request."
678         echo "Please forward it to the right person, and then run"
679         echo "$0 --populate-db $name"
680         echo "Thanks."
681     fi
682 fi
683
684
685 if [ "$op" = create ] || [ "$op" = populate ]
686 then
687     # Re-fetch the passwords from the config we've generated, allows it
688     # to be different from what we set, in case the user had to change
689     # something.
690     mysqluser=$(getinstancemysqluser $name)
691     mysqldb=$(getinstancemysqldatabase $name)
692     # Use the default database content if that exists.
693     if [ -e "$DEFAULTSQL" ]
694     then
695         # Populate the database with default content.
696         zcat -f "$DEFAULTSQL" |
697         sed "s/__KOHASITE__/koha_$name/g" |
698         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
699
700
701         # Change the default user's password.
702         staffpass="$(pwgen 12 1)"
703         staffdigest=$(echo -n "$staffpass" |
704                       perl -e '
705                             use Digest::MD5 qw(md5_base64); 
706                             while (<>) { print md5_base64($_), "\n"; }')
707         mysql --host="$mysqlhost" --user="$mysqluser" \
708 --password="$mysqlpwd" <<eof
709 USE \`$mysqldb\`;
710 UPDATE borrowers 
711 SET password = '$staffdigest' 
712 WHERE borrowernumber = $ADMINUSER;
713 eof
714         #`
715         echo "staff user password is '$staffpass' but keep that secret"
716
717         # Upgrade the database schema, just in case the dump was from an 
718         # old version.
719         koha-upgrade-schema "$name"
720     else
721         echo "Koha instance is empty, no staff user created."
722     fi
723 fi
724
725
726 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
727 then
728     # Reconfigure Apache.
729     if ! {
730         a2ensite "$name" > /dev/null 2>&1 ||
731             a2ensite "${name}.conf" > /dev/null 2>&1
732     }; then
733         echo "Warning: problem enabling $name in Apache" >&2
734     fi
735     service apache2 restart
736
737     # Start Zebra.
738     koha-zebra --start "$name"
739
740     if [ "$USE_INDEXER_DAEMON" = "yes" ]; then
741         # Start Indexer daemon
742         koha-indexer --start "$name"
743     fi
744
745     if [ "$CLO_LETSENCRYPT" = "yes" ]; then
746         # Get letsencrypt certificates
747         letsencrypt_instance
748     fi
749 fi
750
751
752 if [ "$op" = request ]
753 then
754     koha-disable "$name"
755 fi
756
757 echo <<eoh
758
759 Email for this instance is disabled. When you're ready to enable it, use:
760 koha-email-enable $name
761 eoh