bug fixes in planned production ubuntu package (still need to test)
[openemr.git] / contrib / util / ubuntu_package_scripts / production / postinst
blobd5d2cba096b9a3ee40fc50bc5fcf73808eccb6f2
1 #!/bin/bash
3 #This program is free software; you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation; either version 2 of the License, or
6 #(at your option) any later version.
8 # authors: Amalu Obinna <amaluobinna@aol.com>
9 # Brady Miller <brady@sparmy.com>
11 # date: 10/05/10
13 # Debian package post installation script steps:
14 # 1) Collect setting from package configuration file
15 # 2) Install or Upgrade
16 # -Install
17 # a) Ensure OpenEMR MySQL database and user do not exist.
18 # b) If MySQL is already installed:
19 # -Collect the MySQL root password
20 # -ensure openemr mysql database/user does not exist
21 # c) Configure OpenEMR
22 # d) Configure Apache
23 # e) Configure PHP
24 # -Upgrade
25 # a) Modify new OpenEMR version configuration files
26 # b) Upgrade MySQL database
27 # c) Upgrade Access Controls
28 # d) Copy over old configuration files
29 # (Copy to files with .OLD extension to allow manual comparisons by user)
30 # e) Update PHP settings with new recommendations (not needed yet)
31 # f) Modify permissions for writable directories
32 # g) Secure the php installation/upgrading scripts
33 # 3) Modify the package configuration file
34 # 4) Echo instructions on starting openemr
36 # summary of how this script can be called:
37 # * <postinst> `configure' <most-recently-configured-version>
38 # * <old-postinst> `abort-upgrade' <new version>
39 # * <conflictor's-postinst> `abort-remove' `in-favour' <package>
40 # <new-version>
41 # * <postinst> `abort-remove'
42 # * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
43 # <failed-install-package> <version> `removing'
44 # <conflicting-package> <version>
45 # for details, see http://www.debian.org/doc/debian-policy/ or
46 # the debian-policy package
48 case "$1" in
49 configure)
51 #constants and paths
52 LOGDIR=/var/log/openemr
53 LOG=$LOGDIR/install
54 CONFIGDIR=/etc/openemr
55 CONFIG=$CONFIGDIR/openemr.conf
56 TMPDIR=/tmp/openemr-tmp
57 WEB=/var/www
58 OPENEMR=$WEB/openemr
59 SITEDIR=$OPENEMR/sites/default
60 #hardcoded mysql user and database for install (not pertinent for upgrading)
61 # upgrading can use whatever is found in openemr/library/sqlconf.php
62 INSTALL_USER=openemr
63 INSTALL_DATABASE=openemr
64 #auto install scripts
65 INST=$OPENEMR/contrib/util/installScripts/InstallerAuto.php
66 INSTTEMP=$OPENEMR/contrib/util/installScripts/InstallerAutoTemp.php
67 #php and apache files
68 PHP=/etc/php5/apache2/php.ini
69 APACHE=/etc/apache2/httpd.conf
70 #web user and group
71 WEB_GROUP=www-data
72 WEB_USER=www-data
74 #Standardized echo function to send to both echo and to log file
75 # requires one parameter (string)
76 output_both () {
77 echo $1
78 echo "`date`: $1" >> $LOG
81 #Standardized echo function to send to only log file
82 # requires one parameter (string)
83 log_only () {
84 echo "`date`: $1" >> $LOG
87 #Standardized exit functions to be used
88 # requires one parameter (string with reason for exiting)
89 unable_exit () {
90 echo $1
91 echo "`date`: $1" >> $LOG
92 echo "EXITING.........."
93 echo "`date`: EXITING.........." >> $LOG
94 sleep 5
95 exit 1
98 #function to check mysql for selected databases
99 # 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
100 check_mysql () {
101 if [ -n "$3" ]; then
102 HOST=$3
103 else
104 HOST=localhost
106 if [ -n "$4" ]; then
107 USE=$4
108 else
109 USE=root
111 echo `mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^$2$"`
114 #function to collect variables from config files
115 # 1st param is variable name, 2nd param is filename
116 collect_var () {
117 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
120 #function to insert variables into config files
121 # 1st param is variable name, 2nd param is variable, 3rd param is filename
122 insert_var () {
123 sed -i 's@^[ ]*'"$1"'[ =].*$@'"$1"' = '"$2"'@' "$3"
126 #collect scripting information from config file
127 PROCESS=$(collect_var process $CONFIG)
128 PLAN=$(collect_var plan $CONFIG)
129 MPASS=$(collect_var pass $CONFIG)
131 #Don't allow re-configuration
132 if [ "$PROCESS" == "complete" ] ; then
133 unable_exit "OpenEMR has already been configured."
134 elif [ "$PROCESS" == "pending" ] ; then
135 #continue with configuration
136 log_only "Configuring package..."
137 else
138 unable_exit "Error reading process variable in configuration file."
141 if [ "$PLAN" == "upgrade" ] ; then
142 #continue with upgrade
144 #collect more information from config file
145 OLD_VERSION=$(collect_var previous_version $CONFIG)
146 SQLLOCATION=$(collect_var sqllocation $CONFIG)
147 SQLUSER=$(collect_var sqluser $CONFIG)
148 SQLPASSWORD=$(collect_var sqlpassword $CONFIG)
149 SQLDATABASE=$(collect_var sqldatabase $CONFIG)
150 SQLUTFFLAG=$(collect_var sqlutfflag $CONFIG)
152 #configure openemr/sites/default/sqlconf.php
153 insert_var "\$host" "\'$SQLLOCATION\';" $SITEDIR/sqlconf.php
154 insert_var "\$login" "\'$SQLUSER\';" $SITEDIR/sqlconf.php
155 insert_var "\$pass" "\'$SQLPASSWORD\';" $SITEDIR/sqlconf.php
156 insert_var "\$dbase" "\'$SQLDATABASE\';" $SITEDIR/sqlconf.php
157 insert_var "\$disable_utf8_flag" "$SQLUTFFLAG;" $SITEDIR/sqlconf.php
158 sed -i "s/^[ ]*\$config[ =].*0/\$config = 1/" $SITEDIR/sqlconf.php
160 #before run scripts, go to openemr directory
161 cd $OPENEMR
163 #upgrade the sql database
164 CONC_VERSION=$(echo $OLD_VERSION | cut -d \- -f 1)
165 cp -f $OPENEMR/sql_upgrade.php $OPENEMR/TEMPsql_upgrade.php
166 sed -i "/input type='submit'/d" $OPENEMR/TEMPsql_upgrade.php
167 sed -i "s/!empty(\$_POST\['form_submit'\])/empty(\$_POST\['form_submit'\])/" $OPENEMR/TEMPsql_upgrade.php
168 sed -i "s/^[ ]*\$form_old_version[ =].*$/\$form_old_version = \"$CONC_VERSION\";/" $OPENEMR/TEMPsql_upgrade.php
169 php -f $OPENEMR/TEMPsql_upgrade.php >> $LOG
170 rm $OPENEMR/TEMPsql_upgrade.php
172 #upgrade the gacl controls
173 php -f $OPENEMR/acl_upgrade.php >> $LOG
175 #copy the old config file into new with the OLD at end to allow manual configuration of old
176 # optional settings.
177 if [ -d $TMPDIR/openemr_web_$OLD_VERSION/sites/default ]; then
178 cp -f $TMPDIR/openemr_web_$OLD_VERSION/sites/default/config.php $SITEDIR/config.php.OLD
179 else
180 cp -f $TMPDIR/openemr_web_$OLD_VERSION/includes/config.php $SITEDIR/config.php.OLD
183 # if site-specific directories are in the old locations, move them.
184 if [ -d $OPENEMR/documents ]; then
185 mv -f $OPENEMR/documents/* $SITEDIR/documents/
186 rm -rf $OPENEMR/documents
188 if [ -d $OPENEMR/era ]; then
189 mv -f $OPENEMR/era/* $SITEDIR/era/
190 rm -rf $OPENEMR/era
192 if [ -d $OPENEMR/edi ]; then
193 mv -f $OPENEMR/edi/* $SITEDIR/edi/
194 rm -rf $OPENEMR/edi
196 if [ -d $OPENEMR/custom/letter_templates ]; then
197 mv -f $OPENEMR/custom/letter_templates/* $SITEDIR/letter_templates/
198 rm -rf $OPENEMR/custom/letter_templates
201 #upgrade php settings if change or have new recs in future (none yet)
203 #secure openemr
204 chown -Rf root:root $OPENEMR
205 chmod 600 $OPENEMR/acl_setup.php
206 chmod 600 $OPENEMR/acl_upgrade.php
207 chmod 600 $OPENEMR/sl_convert.php
208 chmod 600 $OPENEMR/setup.php
209 chmod 600 $OPENEMR/sql_upgrade.php
210 chmod 600 $OPENEMR/ippf_upgrade.php
211 chmod 600 $OPENEMR/gacl/setup.php
213 #set writable directories
214 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/documents
215 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/edi
216 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/era
217 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/library/freeb
218 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/letter_templates
219 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache
220 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled
221 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/gacl/admin/templates_c
223 #update config file, change process to complete and remove others
224 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
225 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
226 sed -i "/^[ ]*pass[ =].*$/d" $CONFIG
227 sed -i "/^[ ]*previous_version[ =].*$/d" $CONFIG
228 sed -i "/^[ ]*sqllocation[ =].*$/d" $CONFIG
229 sed -i "/^[ ]*sqluser[ =].*$/d" $CONFIG
230 sed -i "/^[ ]*sqlpassword[ =].*$/d" $CONFIG
231 sed -i "/^[ ]*sqldatabase[ =].*$/d" $CONFIG
232 sed -i "/^[ ]*sqlutfflag[ =].*$/d" $CONFIG
234 #done upgrading
235 echo ""
236 echo "-----------------------------------------------------"
237 echo ""
238 output_both "OpenEMR upgrade is complete."
239 echo ""
240 output_both "Recommend setting optional configuration settings in:"
241 output_both "$SITEDIR/config.php"
242 output_both "(We have renamed your old configuration files to *.OLD)"
243 output_both "(We recommend you delete the *.OLD files when done)"
244 echo ""
245 output_both "We have placed backup of your old OpenEMR in $TMPDIR"
246 output_both "(We recommend you copy this somewhere protected since it"
247 output_both "contains confidential patient information)"
248 echo ""
249 echo "-----------------------------------------------------"
251 sleep 5
252 exit 0
254 elif [ "$PLAN" == "install" ] ; then
255 #continue with installation
256 log_only "Installing OpenEMR"
257 else
258 unable_exit "Error reading plan variable in configuration file."
261 ## BEGIN MYSQL ROOT PASSWORD GRAB
262 if [ "`check_mysql "$MPASS" "mysql"`" != "mysql" ]; then
263 #the initial mysql password didn't work, so ask for password
264 COUNTDOWN=1
265 while true; do
266 echo ""
267 echo -n "Please enter your MySQL root password:"
268 read MPASS
269 echo ""
270 if [ "`check_mysql "$MPASS" "mysql"`" == "mysql" ]; then
271 #the mysql root password works, so can exit loop
272 break
273 else
274 #the mysql root password did not work
275 if [ "$COUNTDOWN" -ge "5" ]; then
276 output_both "5 attempts to enter your mysql root password have failed"
277 output_both "Recommend repeating OpenEMR installation when you know your mysql root password"
278 unable_exit "Giving up on OpenEMR package installation."
280 echo "The entered MySQL root password did not work."
281 echo "$COUNTDOWN of 5 total attempts."
282 echo "PLEASE TRY AGAIN..."
284 let "COUNTDOWN += 1"
285 done
287 ## END MYSQL ROOT PASSWORD GRAB
289 #now ensure the openemr user and database do not exist, if so then exit
290 # Check for openemr database in mysql, if exist then exit
291 if [ "`check_mysql "$MPASS" "$INSTALL_DATABASE"`" == "$INSTALL_DATABASE" ]; then
292 unable_exit "MySQL '$INSTALL_DATABASE' database already exists"
294 # Check for OpenEMR user in mysql.user, if exist then exit
295 USER=$(mysql -s -u root -h localhost --password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
296 if [ "$USER" == "$INSTALL_USER" ]; then
297 unable_exit "MySQl user '$INSTALL_USER' already exists"
300 #go to openemr directory
301 cd $OPENEMR
303 #secure openemr
304 chown -Rf root:root $OPENEMR
306 #INSTALL AND CONFIGURE OPENEMR
307 output_both "Configuring OpenEMR"
309 # Create a random password for the openemr mysql user
310 password=$(makepasswd --char=12)
312 # openemr installation VARIABLES
313 if [ "$MPASS" == "" ] ; then
314 rootpass="rootpass=BLANK" #MySQL server root password
315 else
316 rootpass="rootpass=$MPASS" #MySQL server root password
318 login="login=$INSTALL_USER" #username to MySQL openemr database
319 pass="pass=$password" #password to MySQL openemr database
320 dbname="dbname=$INSTALL_DATABASE" #MySQL openemr database name
322 # Set file and directory permissions
323 chmod 666 $SITEDIR/sqlconf.php
324 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/documents
325 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/edi
326 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/era
327 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/library/freeb
328 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/letter_templates
329 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache
330 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled
331 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/gacl/admin/templates_c
333 # Run Auto Installer
335 sed -e 's@^exit;@ @' <$INST >$INSTTEMP
336 php -f $INSTTEMP $rootpass $login $pass $dbname >> $LOG
337 rm -f $INSTTEMP
339 #remove global permission to all setup scripts
340 chmod 600 $OPENEMR/acl_setup.php
341 chmod 600 $OPENEMR/acl_upgrade.php
342 chmod 600 $OPENEMR/sl_convert.php
343 chmod 600 $OPENEMR/setup.php
344 chmod 600 $OPENEMR/sql_upgrade.php
345 chmod 600 $OPENEMR/ippf_upgrade.php
346 chmod 600 $OPENEMR/gacl/setup.php
348 log_only "Done configuring OpenEMR"
350 #This section configures Apache for OpenEMR
351 output_both "Configuring Apache for OpenEMR"
353 #Check to ensure the apache configuration files exists
354 if [ -f $APACHE ]; then
356 # First, backup the httpd.conf file before modifying
357 cp -f $APACHE $APACHE.BAK
359 # Second, append information to secure selected directories in OpenEMR
360 echo "#This is the start of the Apache configuration for OpenEMR." >> $APACHE
361 echo "#Below will secure directories with patient information." >> $APACHE
362 echo "<Directory \"$SITEDIR/documents\">" >> $APACHE
363 echo " order deny,allow" >> $APACHE
364 echo " Deny from all" >> $APACHE
365 echo "</Directory>" >> $APACHE
366 echo "<Directory \"$SITEDIR/edi\">" >> $APACHE
367 echo " order deny,allow" >> $APACHE
368 echo " Deny from all" >> $APACHE
369 echo "</Directory>" >> $APACHE
370 echo "<Directory \"$SITEDIR/era\">" >> $APACHE
371 echo " order deny,allow" >> $APACHE
372 echo " Deny from all" >> $APACHE
373 echo "</Directory>" >> $APACHE
374 echo "#This is the end of the Apache configuration for OpenEMR." >> $APACHE
376 #let user know the plan
377 output_both "Added entries to apache configuration to secure directories with patient information."
378 output_both "Placed backup of your original apache configuration file to $APACHE.BAK"
380 else
381 #can't find apache config file, so just echo instructions
382 echo ""
383 output_both "We recommend placing below lines into your apache configuration file:"
384 output_both "#This is the start of the Apache configuration for OpenEMR."
385 output_both "#Below will secure directories with patient information."
386 output_both "<Directory \"$SITEDIR/documents\">"
387 output_both " order deny,allow"
388 output_both " Deny from all"
389 output_both "</Directory>"
390 output_both "<Directory \"$SITEDIR/edi\">"
391 output_both " order deny,allow"
392 output_both " Deny from all"
393 output_both "</Directory>"
394 output_both "<Directory \"$SITEDIR/era\">"
395 output_both " order deny,allow"
396 output_both " Deny from all"
397 output_both "</Directory>"
398 output_both "#This is the end of the Apache configuration for OpenEMR."
399 echo ""
402 log_only "Done configuring Apache"
404 #This Section edits the php.ini file to accomodate the proper functioning of OpenEMR using php
405 output_both "Configuring PHP for OpenEMR"
407 #check to ensure the php configuration file exists
408 if [ -f $PHP ]; then
409 # First, collect php variables
410 collect_php () {
411 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
413 TAG_TEXT="short_open_tag"
414 TAG=$(collect_php "$TAG_TEXT")
415 EXEC_TEXT="max_execution_time"
416 EXEC=$(collect_php "$EXEC_TEXT")
417 INPUT_TEXT="max_input_time"
418 INPUT=$(collect_php "$INPUT_TEXT")
419 MEM_TEXT="memory_limit"
420 MEM=$(collect_php "$MEM_TEXT")
421 DISP_TEXT="display_errors"
422 DISP=$(collect_php "$DISP_TEXT")
423 LOGG_TEXT="log_errors"
424 LOGG=$(collect_php "$LOGG_TEXT")
425 GLOB_TEXT="register_globals"
426 GLOB=$(collect_php "$GLOB_TEXT")
427 POST_TEXT="post_max_size"
428 POST=$(collect_php "$POST_TEXT")
429 MAGIC_TEXT="magic_quotes_gpc"
430 MAGIC=$(collect_php "$MAGIC_TEXT")
431 UPLOAD_TEXT="file_uploads"
432 UPLOAD=$(collect_php "$UPLOAD_TEXT")
433 FILESIZE_TEXT="upload_max_filesize"
434 FILESIZE=$(collect_php "$FILESIZE_TEXT")
436 # Second, backup the php.ini file before modifying
437 cp $PHP $PHP.BAK
439 # Third, edit the required entries
440 # Do this in a for loop.
441 # First iteration will discover the recommended changes
442 # Second iteration will make the changes (if user request this)
443 FLAG_ON=0
444 process_php () {
445 if [ "$3" -eq "1" ]; then
446 # make rec to php.ini
447 if [ "$FLAG_ON" -eq "0" ]; then
448 output_both "We changed the following setting(s) in your php configuration file at $PHP :"
450 FLAG_ON=1
451 else
452 # modify php.ini
453 sed -i "s/^[ ]*$1[ =].*$/$1 = $2/" $PHP
454 output_both "Successfully set $1 = $2"
457 for i in `seq 1 2`; do
458 if [ "$TAG" != "On" ]; then
459 process_php "$TAG_TEXT" "On" $i
461 if [ "$EXEC" -lt "60" ]; then
462 process_php "$EXEC_TEXT" "60" $i
464 if [ "$INPUT" -lt "90" ]; then
465 process_php "$INPUT_TEXT" "90" $i
467 if [ "$MEM" -lt "128" ]; then
468 process_php "$MEM_TEXT" "128M" $i
470 if [ "$DISP" != "Off" ]; then
471 process_php "$DISP_TEXT" "Off" $i
473 if [ "$LOGG" != "On" ]; then
474 process_php "$LOGG_TEXT" "On" $i
476 if [ "$GLOB" != "Off" ]; then
477 process_php "$GLOB_TEXT" "Off" $i
479 if [ "$POST" -lt "30" ]; then
480 process_php "$POST_TEXT" "30M" $i
482 if [ "$MAGIC" != "On" ]; then
483 process_php "$MAGIC_TEXT" "On" $i
485 if [ "$UPLOAD" != "On" ]; then
486 process_php "$UPLOAD_TEXT" "On" $i
488 if [ "$FILESIZE" -lt "30" ]; then
489 process_php "$FILESIZE_TEXT" "30M" $i
491 if [ "$FLAG_ON" -eq "0" ]; then
492 output_both "Your PHP configuration is perfect for OpenEMR."
493 break
495 if [ "$i" -eq "1" ]; then
496 output_both "(We have placed a backup of your php configuration at $PHP.BAK)"
498 done
499 else
500 #can't find php config file, so just echo instructions
501 echo ""
502 output_both "We recommend ensuring you have below settings in your php configuration file:"
503 output_both "short_open_tag = On"
504 output_both "max_execution_time = 60"
505 output_both "max_input_time = 90"
506 output_both "memory_limit = 128M"
507 output_both "display_errors = Off"
508 output_both "log_errors = On"
509 output_both "register_globals = Off"
510 output_both "post_max_size = 30M"
511 output_both "magic_quotes_gpc = On"
512 output_both "file_uploads = On"
513 output_both "upload_max_filesize = 30M"
514 echo ""
517 log_only "Done configuring PHP"
519 output_both "Restarting Apache service"
520 invoke-rc.d apache2 restart >> $LOG
522 echo "--------------------------------------------------"
523 echo ""
524 output_both "You can now use OpenEMR by browsing to:"
525 output_both "http://localhost/openemr"
526 output_both "user is 'admin' and password is 'pass'"
527 echo ""
528 output_both "See the openemr man page for further instructions:"
529 output_both "type 'man openemr' at command line"
530 echo ""
531 echo "--------------------------------------------------"
533 #update config file, change process to complete and remove plan and pass
534 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
535 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
536 sed -i "/^[ ]*pass[ =].*$/d" $CONFIG
538 sleep 5
539 exit 0
541 abort-upgrade|abort-remove|abort-deconfigure)
543 echo "postinst asked to do $1"
544 exit 0
547 echo "postinst called with unknown argument \`$1'" >&2
548 exit 1
550 esac
552 sleep 5
553 exit 0