pg_passwd 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!@l_bash@
  2. ##
  3. ## pg_passwd -- PostgreSQL Database Password Changing Utility
  4. ## Copyright (c) 2007 OpenPKG Foundation e.V. <http://openpkg.net/>
  5. ## Copyright (c) 2007 Ralf S. Engelschall <http://engelschall.com/>
  6. ##
  7. ## Permission to use, copy, modify, and distribute this software for
  8. ## any purpose with or without fee is hereby granted, provided that
  9. ## the above copyright notice and this permission notice appear in all
  10. ## copies.
  11. ##
  12. ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  13. ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
  16. ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  19. ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. ## SUCH DAMAGE.
  24. ##
  25. # determine system username
  26. system_username="`(id -un) 2>/dev/null`"
  27. if [ ".$system_username" = . ]; then
  28. str="`(id) 2>/dev/null`"
  29. if [ ".`echo $str | grep '^uid[ ]*=[ ]*[0-9]*('`" != . ]; then
  30. system_username=`echo $str | sed -e 's/^uid[ ]*=[ ]*[0-9]*(//' -e 's/).*$//'`
  31. fi
  32. if [ ".$system_username" = . ]; then
  33. system_username="$LOGNAME"
  34. if [ ".$system_username" = . ]; then
  35. system_username="$USER"
  36. if [ ".$system_username" = . ]; then
  37. system_username="`(whoami) 2>/dev/null | awk '{ printf("%s", $1); }'`"
  38. if [ ".$system_username" = . ]; then
  39. system_username="`(who am i) 2>/dev/null | awk '{ printf("%s", $1); }'`"
  40. fi
  41. fi
  42. fi
  43. fi
  44. fi
  45. # determine database superuser username, password and database
  46. superuser_username=""
  47. superuser_password=""
  48. superuser_database=""
  49. superuser_config_file="@l_prefix@/var/postgresql/db/pg_superuser.conf"
  50. if [ -r $superuser_config_file ]; then
  51. # read information
  52. eval `. $superuser_config_file; \
  53. echo superuser_database=\"$superuser_database\"; \
  54. echo superuser_username=\"$superuser_username\"; \
  55. echo superuser_password=\"$superuser_password\"`
  56. else
  57. # guess information
  58. superuser_username="postgresql"
  59. superuser_database="template1"
  60. fi
  61. # determine requested username, database and hostname
  62. username="$1"
  63. database="$2"
  64. hostname="$3"
  65. if [ ".$username" = . ]; then
  66. if [ ".$system_username" = ".root" -o ".$system_username" = ".@l_rusr@" ]; then
  67. username="$superuser_username"
  68. else
  69. username="$system_username"
  70. fi
  71. fi
  72. if [ ".$database" = . ]; then
  73. if [ ".$username" = ".$superuser_username" ]; then
  74. database="$superuser_database"
  75. else
  76. database="$username"
  77. fi
  78. fi
  79. if [ ".$hostname" = . ]; then
  80. hostname="localhost"
  81. fi
  82. # make sure that the PostgreSQL super-user password
  83. # can be kept in sync with the external storage
  84. if [ ".$username" = ".$superuser_username" -a \
  85. ".$database" = ".$superuser_database" ]; then
  86. if [ ".$system_username" != ".root" -a ".$system_username" != ".@l_rusr@" ]; then
  87. echo "$0:ERROR: super-user account password can be changed by \"root\" and \"@l_rusr@\" only" 2>&1
  88. exit 1
  89. fi
  90. if [ -h $superuser_config_file ]; then
  91. echo "$0:ERROR: superuser config \"$superuser_config_file\": invalid (symbolic link)" 2>&1
  92. exit 1
  93. fi
  94. if [ ! -f $superuser_config_file ]; then
  95. echo "$0:WARNING: superuser config \"$superuser_config_file\": not existing" 2>&1
  96. exit 1
  97. elif [ ! -w $superuser_password_file ]; then
  98. echo "$0:ERROR: superuser config \"$superuser_config_file\": permission denied (not writeable)" 2>&1
  99. exit 1
  100. fi
  101. fi
  102. # request old and new password
  103. password_old=""
  104. password_new=""
  105. password_new_verify=""
  106. if [ ".$username" = ".$superuser_username" -a \
  107. ".$database" = ".$superuser_database" ]; then
  108. password_old="$superuser_password"
  109. fi
  110. while [ ".$password_old" = . ]; do
  111. read -s -p "$username:$database:$hostname OLD password: " password_old
  112. echo ""
  113. done
  114. while [ ".$password_new" = . ]; do
  115. read -s -p "$username:$database:$hostname NEW password: " password_new
  116. echo ""
  117. done
  118. while [ ".$password_new_verify" = . ]; do
  119. read -s -p "$username:$database:$hostname NEW password (retype to verify): " password_new_verify
  120. echo ""
  121. done
  122. if [ ".$password_new" != ".$password_new_verify" ]; then
  123. echo "$0:ERROR: mismatch on NEW password" 1>&2
  124. exit 1
  125. fi
  126. # change the password
  127. echo "ALTER ROLE $username WITH PASSWORD '$password_new'" | \
  128. PGPASSWORD="$password_old" @l_prefix@/bin/psql \
  129. -q -U $username -d $database -h $hostname -f- || exit $?
  130. # update superuser configuration
  131. if [ ".$username" = ".$superuser_username" -a \
  132. ".$database" = ".$superuser_database" ]; then
  133. ( umask 077
  134. sed -e "s;.*\(superuser_password=\"\).*\(\"\).*;\1$password_new\2;" \
  135. <$superuser_config_file >$superuser_config_file.new || exit $?
  136. cp $superuser_config_file.new $superuser_config_file || exit $?
  137. rm -f $superuser_config_file.new || exit $?
  138. exit 0
  139. ) || {
  140. echo "$0:ERROR: \"$superuser_config_file\": failed to update content" 1>&2
  141. rm -f $superuser_config_file.new || true
  142. exit $?
  143. }
  144. ( superuser_database_old="$superuser_database"
  145. superuser_username_old="$superuser_username"
  146. superuser_password_old="$superuser_password"
  147. . $superuser_config_file
  148. [ ".$superuser_database" != ".$superuser_database_old" ] && exit 1
  149. [ ".$superuser_username" != ".$superuser_username_old" ] && exit 1
  150. [ ".$superuser_password" = ".$superuser_password_old" ] && exit 1
  151. [ ".$superuser_password" != ".$password_new" ] && exit 1
  152. exit 0
  153. ) || {
  154. echo "$0:ERROR: \"$superuser_config_file\": unexpected updated content" 1>&2
  155. exit $?
  156. }
  157. ( if [ ".$system_username" = ".root" ]; then
  158. chown @l_rusr@:@l_rgrp@ $superuser_config_file || exit $?
  159. fi
  160. chmod 600 $superuser_config_file || exit $?
  161. exit 0
  162. ) || {
  163. echo "$0:ERROR: \"$superuser_config_file\": failed to fixate attributes" 1>&2
  164. exit $?
  165. }
  166. fi