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.
9 # authors: Amalu Obinna <amaluobinna@aol.com>
10 # Brady Miller <brady@sparmy.com>
12 # Debian package post installation script steps:
13 # 1) Collect setting from package configuration file
14 # 2) Install or Upgrade
16 # a) Collect the MySQL root password (if possible)
17 # b) Ensure OpenEMR MySQL database and user do not exist. (if applicable)
18 # c) Configure OpenEMR (if applicable)
22 # a) Modify new OpenEMR version configuration files
23 # b) Upgrade MySQL database(s)
24 # c) Upgrade Access Controls
25 # d) Copy over old configuration files
26 # (Copy to files with .OLD extension to allow manual comparisons by user)
27 # 3) Modify permissions for writable directories
28 # 4) Secure the php installation/upgrading scripts (if applicable)
29 # 5) Modify the package configuration file
30 # 6) Display instructions on starting openemr
32 # summary of how this script can be called:
33 # * <postinst> `configure' <most-recently-configured-version>
34 # * <old-postinst> `abort-upgrade' <new version>
35 # * <conflictor's-postinst> `abort-remove' `in-favour' <package>
37 # * <postinst> `abort-remove'
38 # * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
39 # <failed-install-package> <version> `removing'
40 # <conflicting-package> <version>
41 # for details, see http://www.debian.org/doc/debian-policy/ or
42 # the debian-policy package
44 # Source debconf library.
45 .
/usr
/share
/debconf
/confmodule
51 LOGDIR
=/var
/log
/openemr
53 CONFIGDIR
=/etc
/openemr
54 CONFIG
=$CONFIGDIR/openemr.conf
55 TMPDIR
=/tmp
/openemr-tmp
58 SITEDIR
=$OPENEMR/sites
59 #hardcoded mysql user and database for install (not pertinent for upgrading)
60 # upgrading can use whatever is found in openemr/library/sqlconf.php
62 INSTALL_DATABASE
=openemr
64 INST
=$OPENEMR/contrib
/util
/installScripts
/InstallerAuto.php
65 INSTTEMP
=$OPENEMR/contrib
/util
/installScripts
/InstallerAutoTemp.php
67 PHP
=/etc
/php
5/apache
2/php.ini
68 APACHE
=/etc
/apache
2/httpd.conf
73 #Standardized echo function to send to only log file
74 # requires one parameter (string)
76 echo "`date`: $1" >> $LOG
79 #Standardized exit functions to be used
80 # requires one parameter (string with reason for exiting)
82 echo "`date`: $1" >> $LOG
83 echo "`date`: EXITING.........." >> $LOG
87 #function to check mysql for selected databases
88 # 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
101 if [ "`mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^
$2$
"`" == "$2" ]; then
108 #function to collect variables from config files
109 # 1st param is variable name, 2nd param is filename
111 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
114 #function to insert variables into config files
115 # 1st param is variable name, 2nd param is variable, 3rd param is filename
117 sed -i 's@^[ ]*'"$1"'[ =].*$@'"$1"' = '"$2"'@' "$3"
120 #function to prompt for input
121 # 1st param is name, 2nd param is priority, 3rd param is where result gets sent back in
125 db_fset
"$1" seen false
126 db_input
"$2" "$1" || true
129 local input_value
="$RET"
131 db_fset
"$1" seen false
133 eval $__result="'$input_value'"
136 #collect scripting information from config file
137 PROCESS
=$
(collect_var process
$CONFIG)
138 PLAN
=$
(collect_var plan
$CONFIG)
140 #Don't allow re-configuration
141 if [ "$PROCESS" == "complete" ] ; then
142 unable_exit
"OpenEMR has already been configured."
143 elif [ "$PROCESS" == "pending" ] ; then
144 #continue with configuration
145 log_only
"Configuring package..."
147 unable_exit
"Error reading process variable in configuration file."
150 if [ "$PLAN" == "upgrade" ] ; then
151 #continue with upgrade
152 OLD_VERSION
=$
(collect_var previous_version
$CONFIG)
153 log_only
"Continuing Upgrade from ($OLD_VERSION)"
155 #go to openemr directory
158 #To support the multisite module, go through each site
159 for dir
in $
(find $SITEDIR/* -maxdepth 0 -type d
); do
161 SITENAME
=$
(basename "$dir")
162 log_only
"Configuring Site ($SITENAME)"
164 #collect more information from config file
165 SQLLOCATION
=$
(collect_var
${SITENAME}_sqllocation
$CONFIG)
166 SQLUSER
=$
(collect_var
${SITENAME}_sqluser
$CONFIG)
167 SQLPASSWORD
=$
(collect_var
${SITENAME}_sqlpassword
$CONFIG)
168 SQLDATABASE
=$
(collect_var
${SITENAME}_sqldatabase
$CONFIG)
169 SQLUTFFLAG
=$
(collect_var
${SITENAME}_sqlutfflag
$CONFIG)
171 #configure database configuration file
172 insert_var
"\$host" "\'$SQLLOCATION\';" $SITEDIR/$SITENAME/sqlconf.php
173 insert_var
"\$login" "\'$SQLUSER\';" $SITEDIR/$SITENAME/sqlconf.php
174 insert_var
"\$pass" "\'$SQLPASSWORD\';" $SITEDIR/$SITENAME/sqlconf.php
175 insert_var
"\$dbase" "\'$SQLDATABASE\';" $SITEDIR/$SITENAME/sqlconf.php
176 insert_var
"\$disable_utf8_flag" "$SQLUTFFLAG;" $SITEDIR/$SITENAME/sqlconf.php
177 sed -i "s/^[ ]*\$config[ =].*0/\$config = 1/" $SITEDIR/$SITENAME/sqlconf.php
179 #upgrade the sql database
180 CONC_VERSION
=$
(echo $OLD_VERSION | cut
-d \
- -f 1)
181 echo "<?php \$_GET['site'] = '$SITENAME'; ?>" > $OPENEMR/TEMPsql_upgrade.php
182 cat $OPENEMR/sql_upgrade.php
>> $OPENEMR/TEMPsql_upgrade.php
183 sed -i "/input type='submit'/d" $OPENEMR/TEMPsql_upgrade.php
184 sed -i "s/!empty(\$_POST\['form_submit'\])/empty(\$_POST\['form_submit'\])/" $OPENEMR/TEMPsql_upgrade.php
185 sed -i "s/^[ ]*\$form_old_version[ =].*$/\$form_old_version = \"$CONC_VERSION\";/" $OPENEMR/TEMPsql_upgrade.php
186 php
-f $OPENEMR/TEMPsql_upgrade.php
>> $LOG 2>&1
187 rm -f $OPENEMR/TEMPsql_upgrade.php
189 #copy the old config file into new with the OLD at end to allow manual configuration of old
191 if [ -d "$TMPDIR/openemr_web_$OLD_VERSION/sites/$SITENAME" ]; then
192 cp -f "$TMPDIR/openemr_web_$OLD_VERSION/sites/$SITENAME/config.php" "$SITEDIR/$SITENAME/config.php.OLD"
194 #need to move from old location, so sitename will always be default in this case
195 cp -f $TMPDIR/openemr_web_
$OLD_VERSION/includes
/config.php
$SITEDIR/default
/config.php.OLD
199 log_only
"Upgraded OpenEMR site ($SITENAME) with sql database ($SQLDATABASE) and sql user ($SQLUSER)."
203 # if site-specific directories are in the old locations, move them.
204 if [ -d $OPENEMR/documents
]; then
205 if [ "$(ls $OPENEMR/documents)" ]; then
206 mv -f $OPENEMR/documents
/* $SITEDIR/default
/documents
/
208 rm -rf $OPENEMR/documents
210 if [ -d $OPENEMR/era
]; then
211 if [ "$(ls $OPENEMR/era)" ]; then
212 mv -f $OPENEMR/era
/* $SITEDIR/default
/era
/
216 if [ -d $OPENEMR/edi
]; then
217 if [ "$(ls $OPENEMR/edi)" ]; then
218 mv -f $OPENEMR/edi
/* $SITEDIR/default
/edi
/
222 if [ -d $OPENEMR/custom
/letter_templates
]; then
223 if [ "$(ls $OPENEMR/custom/letter_templates)" ]; then
224 mv -f $OPENEMR/custom
/letter_templates
/* $SITEDIR/default
/letter_templates
/
226 rm -rf $OPENEMR/custom
/letter_templates
230 chown
-Rf root
:root
$OPENEMR
231 chmod 600 $OPENEMR/acl_setup.php
232 chmod 600 $OPENEMR/acl_upgrade.php
233 chmod 600 $OPENEMR/sl_convert.php
234 chmod 600 $OPENEMR/setup.php
235 chmod 600 $OPENEMR/sql_upgrade.php
236 chmod 600 $OPENEMR/ippf_upgrade.php
237 chmod 600 $OPENEMR/gacl
/setup.php
239 #set writable directories (that are not within sites directory)
240 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/library
/freeb
241 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/cache
242 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/compiled
243 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/gacl
/admin
/templates_c
245 #set writable directories (that are within sites directory)
246 # (go through each site)
247 for dir
in $
(find $SITEDIR/* -maxdepth 0 -type d
); do
249 SITENAME
=$
(basename "$dir")
250 #set the writable directories
251 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/documents
252 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/edi
253 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/era
254 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/letter_templates
257 #update config file, change process to complete and remove others
258 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
259 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
260 sed -i "/^[ ]*previous_version[ =].*$/d" $CONFIG
261 sed -i "/^[ ]*.*sqllocation[ =].*$/d" $CONFIG
262 sed -i "/^[ ]*.*sqluser[ =].*$/d" $CONFIG
263 sed -i "/^[ ]*.*sqlpassword[ =].*$/d" $CONFIG
264 sed -i "/^[ ]*.*sqldatabase[ =].*$/d" $CONFIG
265 sed -i "/^[ ]*.*sqlutfflag[ =].*$/d" $CONFIG
268 prompt_input openemr
/success_upgrade critical ret_result
269 log_only
"OpenEMR upgrade is complete."
270 log_only
"Recommend setting optional configuration settings in:"
271 log_only
"$SITEDIR/<sitename>/config.php"
272 log_only
"(We have renamed your old configuration files to *.OLD)"
273 log_only
"(We recommend you delete the *.OLD files when done)"
274 log_only
"We have placed backup of your old OpenEMR in $TMPDIR"
275 log_only
"(We recommend you copy this somewhere protected since it"
276 log_only
"contains confidential patient information)"
280 elif [ "$PLAN" == "install" ] ; then
281 #continue with installation
282 log_only
"Installing OpenEMR"
284 unable_exit
"Error reading plan variable in configuration file."
287 #collect the mysql root password (if applicable)
289 if check_mysql
"$MPASS" "mysql"; then
290 log_only
"Passed the mysql check loop"
292 #the blank initial mysql password didn't work, so prompt for password
293 # (will give 3 chances to provide correct password)
296 prompt_input openemr
/mysql_p_install_
${COUNTDOWN} critical ret_result
298 if check_mysql
"$MPASS" "mysql"; then
299 #the mysql root password works, so can exit loop
300 log_only
"Passed the mysql check loop"
303 #the mysql root password did not work
304 if [ "$COUNTDOWN" -ge "3" ]; then
305 prompt_input openemr
/no_configure_mysql_root high ret_result
306 log_only
"Will install OpenEMR, however will not configure OpenEMR. (unable to provide root password)"
314 #decide whether to configure OpenEMR after it is installed
316 if check_mysql
"$MPASS" "mysql"; then
317 #before auto configuration, ensure the openemr user and database do not exist
318 # Check for openemr database in mysql, if exist then will not configure
319 if check_mysql
"$MPASS" "$INSTALL_DATABASE"; then
320 prompt_input openemr
/no_configure_mysql_database high ret_result
321 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL database already exists)"
322 configure_flag
=false
;
324 # Check for OpenEMR user in mysql.user, if exist then will not configure
325 USER
=$
(mysql
-s -u root
-h localhost
--password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
326 if [ "$USER" == "$INSTALL_USER" ]; then
327 prompt_input openemr
/no_configure_mysql_user high ret_result
328 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL user already exists)"
329 configure_flag
=false
;
332 #the mysql root password didn't work, so do not configure OpenEMR
333 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (root password did not work)"
334 configure_flag
=false
;
337 #go to openemr directory
341 chown
-Rf root
:root
$OPENEMR
343 #INSTALL/CONFIGURE OPENEMR
345 if $configure_flag; then
346 log_only
"Installing/Configuring OpenEMR..."
348 log_only
"Installing OpenEMR ..."
351 # Set file and directory permissions (note use default site directory for new install)
352 chmod 666 $SITEDIR/default
/sqlconf.php
353 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/documents
354 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/edi
355 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/era
356 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/library
/freeb
357 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/letter_templates
358 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/cache
359 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/compiled
360 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/gacl
/admin
/templates_c
362 if $configure_flag; then
363 # Create a random password for the openemr mysql user
364 password
=$
(makepasswd
--char=12)
366 # openemr installation VARIABLES
367 if [ "$MPASS" == "" ] ; then
368 rootpass
="rootpass=BLANK" #MySQL server root password
370 rootpass
="rootpass=$MPASS" #MySQL server root password
372 login
="login=$INSTALL_USER" #username to MySQL openemr database
373 pass
="pass=$password" #password to MySQL openemr database
374 dbname
="dbname=$INSTALL_DATABASE" #MySQL openemr database name
379 sed -e 's@^exit;@ @' <$INST >$INSTTEMP
380 php
-f $INSTTEMP $rootpass $login $pass $dbname >> $LOG 2>&1
383 #remove global permission to all setup scripts
384 chmod 600 $OPENEMR/acl_setup.php
385 chmod 600 $OPENEMR/acl_upgrade.php
386 chmod 600 $OPENEMR/sl_convert.php
387 chmod 600 $OPENEMR/setup.php
388 chmod 600 $OPENEMR/sql_upgrade.php
389 chmod 600 $OPENEMR/ippf_upgrade.php
390 chmod 600 $OPENEMR/gacl
/setup.php
392 log_only
"Done configuring OpenEMR"
395 #This section configures Apache for OpenEMR
396 log_only
"Configuring Apache for OpenEMR"
398 #Check to ensure the apache configuration files exists
399 if [ -f $APACHE ]; then
401 # First, backup the httpd.conf file before modifying
402 cp -f $APACHE $APACHE.BAK
404 # Second, append information to secure selected directories in OpenEMR
405 echo "#This is the start of the Apache configuration for OpenEMR." >> $APACHE
406 echo "#Below will secure directories with patient information." >> $APACHE
407 echo "<Directory \"$SITEDIR/*/documents\">" >> $APACHE
408 echo " order deny,allow" >> $APACHE
409 echo " Deny from all" >> $APACHE
410 echo "</Directory>" >> $APACHE
411 echo "<Directory \"$SITEDIR/*/edi\">" >> $APACHE
412 echo " order deny,allow" >> $APACHE
413 echo " Deny from all" >> $APACHE
414 echo "</Directory>" >> $APACHE
415 echo "<Directory \"$SITEDIR/*/era\">" >> $APACHE
416 echo " order deny,allow" >> $APACHE
417 echo " Deny from all" >> $APACHE
418 echo "</Directory>" >> $APACHE
419 echo "#This is the end of the Apache configuration for OpenEMR." >> $APACHE
421 #let user know the plan
422 prompt_input openemr
/apache_configure high ret_result
423 log_only
"Added entries to apache configuration to secure directories with patient information."
424 log_only
"Placed backup of your original apache configuration file to $APACHE.BAK"
427 #can't find apache config file, so just echo instructions
428 log_only
"We recommend placing below lines into your apache configuration file:"
429 log_only
"#This is the start of the Apache configuration for OpenEMR."
430 log_only
"#Below will secure directories with patient information."
431 log_only
"<Directory \"$SITEDIR/*/documents\">"
432 log_only
" order deny,allow"
433 log_only
" Deny from all"
434 log_only
"</Directory>"
435 log_only
"<Directory \"$SITEDIR/*/edi\">"
436 log_only
" order deny,allow"
437 log_only
" Deny from all"
438 log_only
"</Directory>"
439 log_only
"<Directory \"$SITEDIR/*/era\">"
440 log_only
" order deny,allow"
441 log_only
" Deny from all"
442 log_only
"</Directory>"
443 log_only
"#This is the end of the Apache configuration for OpenEMR."
446 log_only
"Done configuring Apache"
448 #This Section edits the php.ini file to accomodate the proper functioning of OpenEMR using php
449 log_only
"Configuring PHP for OpenEMR"
451 #check to ensure the php configuration file exists
453 # First, collect php variables
455 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
457 TAG_TEXT
="short_open_tag"
458 TAG
=$
(collect_php
"$TAG_TEXT")
459 EXEC_TEXT
="max_execution_time"
460 EXEC
=$
(collect_php
"$EXEC_TEXT")
461 INPUT_TEXT
="max_input_time"
462 INPUT
=$
(collect_php
"$INPUT_TEXT")
463 MEM_TEXT
="memory_limit"
464 MEM
=$
(collect_php
"$MEM_TEXT")
465 DISP_TEXT
="display_errors"
466 DISP
=$
(collect_php
"$DISP_TEXT")
467 LOGG_TEXT
="log_errors"
468 LOGG
=$
(collect_php
"$LOGG_TEXT")
469 GLOB_TEXT
="register_globals"
470 GLOB
=$
(collect_php
"$GLOB_TEXT")
471 POST_TEXT
="post_max_size"
472 POST
=$
(collect_php
"$POST_TEXT")
473 UPLOAD_TEXT
="file_uploads"
474 UPLOAD
=$
(collect_php
"$UPLOAD_TEXT")
475 FILESIZE_TEXT
="upload_max_filesize"
476 FILESIZE
=$
(collect_php
"$FILESIZE_TEXT")
478 # Second, backup the php.ini file before modifying
481 # Third, edit the required entries
482 # Do this in a for loop.
483 # First iteration will discover the recommended changes
484 # Second iteration will make the changes (if user request this)
487 if [ "$3" -eq "1" ]; then
488 # make rec to php.ini
489 if [ "$FLAG_ON" -eq "0" ]; then
490 log_only
"We changed the following setting(s) in your php configuration file at $PHP :"
495 sed -i "s/^[ ]*$1[ =].*$/$1 = $2/" $PHP
496 log_only
"Successfully set $1 = $2"
499 for i
in `seq 1 2`; do
500 if [ "$TAG" != "On" ]; then
501 process_php
"$TAG_TEXT" "On" $i
503 if [ "$EXEC" -lt "60" ]; then
504 process_php
"$EXEC_TEXT" "60" $i
506 if [ "$INPUT" -lt "90" ]; then
507 process_php
"$INPUT_TEXT" "90" $i
509 if [ "$MEM" -lt "128" ]; then
510 process_php
"$MEM_TEXT" "128M" $i
512 if [ "$DISP" != "Off" ]; then
513 process_php
"$DISP_TEXT" "Off" $i
515 if [ "$LOGG" != "On" ]; then
516 process_php
"$LOGG_TEXT" "On" $i
518 if [ "$GLOB" != "Off" ]; then
519 process_php
"$GLOB_TEXT" "Off" $i
521 if [ "$POST" -lt "30" ]; then
522 process_php
"$POST_TEXT" "30M" $i
524 if [ "$UPLOAD" != "On" ]; then
525 process_php
"$UPLOAD_TEXT" "On" $i
527 if [ "$FILESIZE" -lt "30" ]; then
528 process_php
"$FILESIZE_TEXT" "30M" $i
530 if [ "$FLAG_ON" -eq "0" ]; then
531 log_only
"Your PHP configuration is perfect for OpenEMR."
534 if [ "$i" -eq "1" ]; then
535 prompt_input openemr
/php_configure high ret_result
538 if [ "$i" -eq "1" ]; then
539 log_only
"(We have placed a backup of your php configuration at $PHP.BAK)"
543 #can't find php config file, so just echo instructions
544 log_only
"We recommend ensuring you have below settings in your php configuration file:"
545 log_only
"short_open_tag = On"
546 log_only
"max_execution_time = 60"
547 log_only
"max_input_time = 90"
548 log_only
"memory_limit = 128M"
549 log_only
"display_errors = Off"
550 log_only
"log_errors = On"
551 log_only
"register_globals = Off"
552 log_only
"post_max_size = 30M"
553 log_only
"file_uploads = On"
554 log_only
"upload_max_filesize = 30M"
557 log_only
"Done configuring PHP"
559 log_only
"Restarting Apache service"
560 invoke-rc.d apache2 restart
>> $LOG 2>&1
562 if $configure_flag; then
563 prompt_input openemr
/success_install_config high ret_result
564 log_only
"You can now use OpenEMR by browsing to:"
565 log_only
"http://localhost/openemr"
566 log_only
"user is 'admin' and password is 'pass'"
567 log_only
"See the openemr man page for further instructions:"
568 log_only
"type 'man openemr' at command line"
570 prompt_input openemr
/success_install high ret_result
571 log_only
"You can now configure OpenEMR by browsing to:"
572 log_only
"http://localhost/openemr"
573 log_only
"See the openemr man page for further instructions:"
574 log_only
"type 'man openemr' at command line"
577 #update config file, change process to complete and remove plan and pass
578 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
579 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
586 abort-upgrade|abort-remove|abort-deconfigure
)
588 echo "postinst asked to do $1"
592 echo "postinst called with unknown argument \`$1'" >&2