bug fix for ubuntu/debian 4.0 package
[openemr.git] / contrib / util / ubuntu_package_scripts / production / postinst
blob5365e96a9d0b84d8786ad51b1bca92ee5e9f5b71
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
212 chmod 600 $OPENEMR/admin.php
214 #set writable directories
215 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/documents
216 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/edi
217 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/era
218 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/library/freeb
219 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/letter_templates
220 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache
221 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled
222 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/gacl/admin/templates_c
224 #update config file, change process to complete and remove others
225 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
226 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
227 sed -i "/^[ ]*pass[ =].*$/d" $CONFIG
228 sed -i "/^[ ]*previous_version[ =].*$/d" $CONFIG
229 sed -i "/^[ ]*sqllocation[ =].*$/d" $CONFIG
230 sed -i "/^[ ]*sqluser[ =].*$/d" $CONFIG
231 sed -i "/^[ ]*sqlpassword[ =].*$/d" $CONFIG
232 sed -i "/^[ ]*sqldatabase[ =].*$/d" $CONFIG
233 sed -i "/^[ ]*sqlutfflag[ =].*$/d" $CONFIG
235 #done upgrading
236 echo ""
237 echo "-----------------------------------------------------"
238 echo ""
239 output_both "OpenEMR upgrade is complete."
240 echo ""
241 output_both "Recommend setting optional configuration settings in:"
242 output_both "$SITEDIR/config.php"
243 output_both "(We have renamed your old configuration files to *.OLD)"
244 output_both "(We recommend you delete the *.OLD files when done)"
245 echo ""
246 output_both "We have placed backup of your old OpenEMR in $TMPDIR"
247 output_both "(We recommend you copy this somewhere protected since it"
248 output_both "contains confidential patient information)"
249 echo ""
250 echo "-----------------------------------------------------"
252 sleep 5
253 exit 0
255 elif [ "$PLAN" == "install" ] ; then
256 #continue with installation
257 log_only "Installing OpenEMR"
258 else
259 unable_exit "Error reading plan variable in configuration file."
262 ## BEGIN MYSQL ROOT PASSWORD GRAB
263 if [ "`check_mysql "$MPASS" "mysql"`" != "mysql" ]; then
264 #the initial mysql password didn't work, so ask for password
265 COUNTDOWN=1
266 while true; do
267 echo ""
268 echo -n "Please enter your MySQL root password:"
269 read MPASS
270 echo ""
271 if [ "`check_mysql "$MPASS" "mysql"`" == "mysql" ]; then
272 #the mysql root password works, so can exit loop
273 break
274 else
275 #the mysql root password did not work
276 if [ "$COUNTDOWN" -ge "5" ]; then
277 output_both "5 attempts to enter your mysql root password have failed"
278 output_both "Recommend repeating OpenEMR installation when you know your mysql root password"
279 unable_exit "Giving up on OpenEMR package installation."
281 echo "The entered MySQL root password did not work."
282 echo "$COUNTDOWN of 5 total attempts."
283 echo "PLEASE TRY AGAIN..."
285 let "COUNTDOWN += 1"
286 done
288 ## END MYSQL ROOT PASSWORD GRAB
290 #now ensure the openemr user and database do not exist, if so then exit
291 # Check for openemr database in mysql, if exist then exit
292 if [ "`check_mysql "$MPASS" "$INSTALL_DATABASE"`" == "$INSTALL_DATABASE" ]; then
293 unable_exit "MySQL '$INSTALL_DATABASE' database already exists"
295 # Check for OpenEMR user in mysql.user, if exist then exit
296 USER=$(mysql -s -u root -h localhost --password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
297 if [ "$USER" == "$INSTALL_USER" ]; then
298 unable_exit "MySQl user '$INSTALL_USER' already exists"
301 #go to openemr directory
302 cd $OPENEMR
304 #secure openemr
305 chown -Rf root:root $OPENEMR
307 #INSTALL AND CONFIGURE OPENEMR
308 output_both "Configuring OpenEMR"
310 # Create a random password for the openemr mysql user
311 password=$(makepasswd --char=12)
313 # openemr installation VARIABLES
314 if [ "$MPASS" == "" ] ; then
315 rootpass="rootpass=BLANK" #MySQL server root password
316 else
317 rootpass="rootpass=$MPASS" #MySQL server root password
319 login="login=$INSTALL_USER" #username to MySQL openemr database
320 pass="pass=$password" #password to MySQL openemr database
321 dbname="dbname=$INSTALL_DATABASE" #MySQL openemr database name
323 # Set file and directory permissions
324 chmod 666 $SITEDIR/sqlconf.php
325 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/documents
326 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/edi
327 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/era
328 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/library/freeb
329 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/letter_templates
330 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache
331 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled
332 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/gacl/admin/templates_c
334 # Run Auto Installer
336 sed -e 's@^exit;@ @' <$INST >$INSTTEMP
337 php -f $INSTTEMP $rootpass $login $pass $dbname >> $LOG
338 rm -f $INSTTEMP
340 #remove global permission to all setup scripts
341 chmod 600 $OPENEMR/acl_setup.php
342 chmod 600 $OPENEMR/acl_upgrade.php
343 chmod 600 $OPENEMR/sl_convert.php
344 chmod 600 $OPENEMR/setup.php
345 chmod 600 $OPENEMR/sql_upgrade.php
346 chmod 600 $OPENEMR/ippf_upgrade.php
347 chmod 600 $OPENEMR/gacl/setup.php
349 log_only "Done configuring OpenEMR"
351 #This section configures Apache for OpenEMR
352 output_both "Configuring Apache for OpenEMR"
354 #Check to ensure the apache configuration files exists
355 if [ -f $APACHE ]; then
357 # First, backup the httpd.conf file before modifying
358 cp -f $APACHE $APACHE.BAK
360 # Second, append information to secure selected directories in OpenEMR
361 echo "#This is the start of the Apache configuration for OpenEMR." >> $APACHE
362 echo "#Below will secure directories with patient information." >> $APACHE
363 echo "<Directory \"$SITEDIR/documents\">" >> $APACHE
364 echo " order deny,allow" >> $APACHE
365 echo " Deny from all" >> $APACHE
366 echo "</Directory>" >> $APACHE
367 echo "<Directory \"$SITEDIR/edi\">" >> $APACHE
368 echo " order deny,allow" >> $APACHE
369 echo " Deny from all" >> $APACHE
370 echo "</Directory>" >> $APACHE
371 echo "<Directory \"$SITEDIR/era\">" >> $APACHE
372 echo " order deny,allow" >> $APACHE
373 echo " Deny from all" >> $APACHE
374 echo "</Directory>" >> $APACHE
375 echo "#This is the end of the Apache configuration for OpenEMR." >> $APACHE
377 #let user know the plan
378 output_both "Added entries to apache configuration to secure directories with patient information."
379 output_both "Placed backup of your original apache configuration file to $APACHE.BAK"
381 else
382 #can't find apache config file, so just echo instructions
383 echo ""
384 output_both "We recommend placing below lines into your apache configuration file:"
385 output_both "#This is the start of the Apache configuration for OpenEMR."
386 output_both "#Below will secure directories with patient information."
387 output_both "<Directory \"$SITEDIR/documents\">"
388 output_both " order deny,allow"
389 output_both " Deny from all"
390 output_both "</Directory>"
391 output_both "<Directory \"$SITEDIR/edi\">"
392 output_both " order deny,allow"
393 output_both " Deny from all"
394 output_both "</Directory>"
395 output_both "<Directory \"$SITEDIR/era\">"
396 output_both " order deny,allow"
397 output_both " Deny from all"
398 output_both "</Directory>"
399 output_both "#This is the end of the Apache configuration for OpenEMR."
400 echo ""
403 log_only "Done configuring Apache"
405 #This Section edits the php.ini file to accomodate the proper functioning of OpenEMR using php
406 output_both "Configuring PHP for OpenEMR"
408 #check to ensure the php configuration file exists
409 if [ -f $PHP ]; then
410 # First, collect php variables
411 collect_php () {
412 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
414 TAG_TEXT="short_open_tag"
415 TAG=$(collect_php "$TAG_TEXT")
416 EXEC_TEXT="max_execution_time"
417 EXEC=$(collect_php "$EXEC_TEXT")
418 INPUT_TEXT="max_input_time"
419 INPUT=$(collect_php "$INPUT_TEXT")
420 MEM_TEXT="memory_limit"
421 MEM=$(collect_php "$MEM_TEXT")
422 DISP_TEXT="display_errors"
423 DISP=$(collect_php "$DISP_TEXT")
424 LOGG_TEXT="log_errors"
425 LOGG=$(collect_php "$LOGG_TEXT")
426 GLOB_TEXT="register_globals"
427 GLOB=$(collect_php "$GLOB_TEXT")
428 POST_TEXT="post_max_size"
429 POST=$(collect_php "$POST_TEXT")
430 MAGIC_TEXT="magic_quotes_gpc"
431 MAGIC=$(collect_php "$MAGIC_TEXT")
432 UPLOAD_TEXT="file_uploads"
433 UPLOAD=$(collect_php "$UPLOAD_TEXT")
434 FILESIZE_TEXT="upload_max_filesize"
435 FILESIZE=$(collect_php "$FILESIZE_TEXT")
437 # Second, backup the php.ini file before modifying
438 cp $PHP $PHP.BAK
440 # Third, edit the required entries
441 # Do this in a for loop.
442 # First iteration will discover the recommended changes
443 # Second iteration will make the changes (if user request this)
444 FLAG_ON=0
445 process_php () {
446 if [ "$3" -eq "1" ]; then
447 # make rec to php.ini
448 if [ "$FLAG_ON" -eq "0" ]; then
449 output_both "We changed the following setting(s) in your php configuration file at $PHP :"
451 FLAG_ON=1
452 else
453 # modify php.ini
454 sed -i "s/^[ ]*$1[ =].*$/$1 = $2/" $PHP
455 output_both "Successfully set $1 = $2"
458 for i in `seq 1 2`; do
459 if [ "$TAG" != "On" ]; then
460 process_php "$TAG_TEXT" "On" $i
462 if [ "$EXEC" -lt "60" ]; then
463 process_php "$EXEC_TEXT" "60" $i
465 if [ "$INPUT" -lt "90" ]; then
466 process_php "$INPUT_TEXT" "90" $i
468 if [ "$MEM" -lt "128" ]; then
469 process_php "$MEM_TEXT" "128M" $i
471 if [ "$DISP" != "Off" ]; then
472 process_php "$DISP_TEXT" "Off" $i
474 if [ "$LOGG" != "On" ]; then
475 process_php "$LOGG_TEXT" "On" $i
477 if [ "$GLOB" != "Off" ]; then
478 process_php "$GLOB_TEXT" "Off" $i
480 if [ "$POST" -lt "30" ]; then
481 process_php "$POST_TEXT" "30M" $i
483 if [ "$MAGIC" != "On" ]; then
484 process_php "$MAGIC_TEXT" "On" $i
486 if [ "$UPLOAD" != "On" ]; then
487 process_php "$UPLOAD_TEXT" "On" $i
489 if [ "$FILESIZE" -lt "30" ]; then
490 process_php "$FILESIZE_TEXT" "30M" $i
492 if [ "$FLAG_ON" -eq "0" ]; then
493 output_both "Your PHP configuration is perfect for OpenEMR."
494 break
496 if [ "$i" -eq "1" ]; then
497 output_both "(We have placed a backup of your php configuration at $PHP.BAK)"
499 done
500 else
501 #can't find php config file, so just echo instructions
502 echo ""
503 output_both "We recommend ensuring you have below settings in your php configuration file:"
504 output_both "short_open_tag = On"
505 output_both "max_execution_time = 60"
506 output_both "max_input_time = 90"
507 output_both "memory_limit = 128M"
508 output_both "display_errors = Off"
509 output_both "log_errors = On"
510 output_both "register_globals = Off"
511 output_both "post_max_size = 30M"
512 output_both "magic_quotes_gpc = On"
513 output_both "file_uploads = On"
514 output_both "upload_max_filesize = 30M"
515 echo ""
518 log_only "Done configuring PHP"
520 output_both "Restarting Apache service"
521 invoke-rc.d apache2 restart >> $LOG
523 echo "--------------------------------------------------"
524 echo ""
525 output_both "You can now use OpenEMR by browsing to:"
526 output_both "http://localhost/openemr"
527 output_both "user is 'admin' and password is 'pass'"
528 echo ""
529 output_both "See the openemr man page for further instructions:"
530 output_both "type 'man openemr' at command line"
531 echo ""
532 echo "--------------------------------------------------"
534 #update config file, change process to complete and remove plan and pass
535 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
536 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
537 sed -i "/^[ ]*pass[ =].*$/d" $CONFIG
539 sleep 5
540 exit 0
542 abort-upgrade|abort-remove|abort-deconfigure)
544 echo "postinst asked to do $1"
545 exit 0
548 echo "postinst called with unknown argument \`$1'" >&2
549 exit 1
551 esac
553 sleep 5
554 exit 0