5 # Configure exim for GForge
6 # Roland Mas, debian-sf (GForge for Debian)
10 if [ $(id -u) != 0 ] ; then
11 echo "You must be root to run this, please enter passwd"
24 echo "Usage: $0 {configure-files|configure|purge|purge-files|dump|restore}"
28 # Tell PostgreSQL to let us use the database
29 db_passwd=$(perl -e'require "/etc/gforge/local.pl"; print "$sys_dbpasswd\n";')
30 ip_address=$(perl -e'require "/etc/gforge/local.pl"; print "$sys_dbhost\n";')
31 pattern=$(basename $0).XXXXXX
32 pg_version=$(dpkg -s postgresql | awk '/^Version: / { print $2 }')
33 if dpkg --compare-versions $pg_version lt 7.3 ; then
34 # PostgreSQL configuration for versions prior to 7.3
35 echo "Configuring for PostgreSQL 7.2"
36 cp -a /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.gforge-new
37 if grep -q "^host.*gforge_passwd$" /etc/postgresql/pg_hba.conf.gforge-new ; then
38 perl -pi -e "s/^host.*gforge_passwd$/host gforge $ip_address 255.255.255.255 password gforge_passwd/" /etc/postgresql/pg_hba.conf.gforge-new
40 cur=$(mktemp /tmp/$pattern)
41 echo "### Next line inserted by GForge install" > $cur
42 echo "host gforge $ip_address 255.255.255.255 password gforge_passwd" >> $cur
43 cat /etc/postgresql/pg_hba.conf.gforge-new >> $cur
44 cat $cur > /etc/postgresql/pg_hba.conf.gforge-new
47 su -s /bin/sh postgres -c "touch /var/lib/postgres/data/gforge_passwd"
48 su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/pg_passwd /var/lib/postgres/data/gforge_passwd > /dev/null" <<-EOF
54 # PostgreSQL configuration for versions from 7.3 on
55 echo "Configuring for PostgreSQL 7.3"
56 cp -a /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.gforge-new
57 if grep -q "^host.*gforge_passwd$" /etc/postgresql/pg_hba.conf.gforge-new ; then
58 perl -pi -e "s/^host.*gforge_passwd$/host gforge gforge $ip_address 255.255.255.255 password/" /etc/postgresql/pg_hba.conf.gforge-new
59 elif grep -q "^host gforge gforge.*password$" /etc/postgresql/pg_hba.conf.gforge-new ; then
60 perl -pi -e "s/^host gforge gforge.*password$/host gforge gforge $ip_address 255.255.255.255 password/" /etc/postgresql/pg_hba.conf.gforge-new
62 cur=$(mktemp /tmp/$pattern)
63 echo "### Next line inserted by GForge install" > $cur
64 echo "host gforge gforge $ip_address 255.255.255.255 password" >> $cur
65 cat /etc/postgresql/pg_hba.conf.gforge-new >> $cur
66 cat $cur > /etc/postgresql/pg_hba.conf.gforge-new
69 # Set the password for the user
70 db_passwd=$(perl -e'require "/etc/gforge/local.pl"; print "$sys_dbpasswd\n";')
71 su -s /bin/sh postgres -c "/usr/bin/psql template1" &> /dev/null <<-EOF
72 update pg_shadow set passwd='$db_passwd' where usename='gforge' ;
74 # Remove old password file
75 if [ -e /var/lib/postgres/data/gforge_passwd ] ; then
76 rm -f /var/lib/postgres/data/gforge_passwd
82 # Create the appropriate database user
83 pattern=$(basename $0).XXXXXX
84 tmp1=$(mktemp /tmp/$pattern)
85 tmp2=$(mktemp /tmp/$pattern)
86 if su -s /bin/sh postgres -c "createuser --no-createdb --no-adduser gforge" 1> $tmp1 2> $tmp2 \
87 && [ "$(head -1 $tmp1)" = 'CREATE USER' ] \
88 || grep -q '^ERROR: CREATE USER: user name "gforge" already exists$' $tmp2 ; then
89 # Creation OK or user already existing -- no problem here
93 echo "Cannot create PostgreSQL user... This shouldn't have happened."
94 echo "Maybe a problem in your PostgreSQL configuration?"
95 echo "Please report a bug to the Debian bug tracking system"
96 echo "Please include the following output:"
97 echo "createuser's STDOUT:"
99 echo "createuser's STDERR:"
105 # Create the appropriate database
106 tmp1=$(mktemp /tmp/$pattern)
107 tmp2=$(mktemp /tmp/$pattern)
108 if su -s /bin/sh postgres -c "createdb gforge" 1> $tmp1 2> $tmp2 \
109 && [ "$(head -1 $tmp1)" = 'CREATE DATABASE' ] \
110 || grep -q '^ERROR: CREATE DATABASE: database "gforge" already exists$' $tmp2 ; then
111 # Creation OK or database already existing -- no problem here
115 echo "Cannot create PostgreSQL database... This shouldn't have happened."
116 echo "Maybe a problem in your PostgreSQL configuration?"
117 echo "Please report a bug to the Debian bug tracking system"
118 echo "Please include the following output:"
119 echo "createdb's STDOUT:"
121 echo "createdb's STDERR:"
127 pattern=$(basename $0).XXXXXX
128 tmp1=$(mktemp /tmp/$pattern)
129 tmp2=$(mktemp /tmp/$pattern)
130 if su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/enable_lang plpgsql gforge" 1> $tmp1 2> $tmp2 \
131 || grep -q '^plpgsql added to gforge$' $tmp1 \
132 || grep -q '^plpgsql is already enabled in gforge$' $tmp1 ; then
133 # Creation OK or user already existing -- no problem here
137 echo "Cannot enable the PLPGSQL language in the database... This shouldn't have happened."
138 echo "Maybe a problem in your PostgreSQL configuration?"
139 echo "Please report a bug to the Debian bug tracking system"
140 echo "Please include the following output:"
141 echo "enable_lang's STDOUT:"
143 echo "enable_lang's STDERR:"
149 # Install/upgrade the database contents (tables and data)
150 kill -HUP $(head -1 /var/lib/postgres/data/postmaster.pid)
151 /usr/lib/gforge/bin/db-upgrade.pl 2>&1 | grep -v ^NOTICE:
153 if [ $p != 0 ] ; then
159 cp -a /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.gforge-new
160 if grep -q "### Next line inserted by GForge install" /etc/postgresql/pg_hba.conf.gforge-new
162 perl -pi -e "s/### Next line inserted by GForge install\n//" /etc/postgresql/pg_hba.conf.gforge-new
163 perl -pi -e "s/^host gforge gforge.*password\n//" /etc/postgresql/pg_hba.conf.gforge-new
164 perl -pi -e "s/^host.*gforge_passwd\n//" /etc/postgresql/pg_hba.conf.gforge-new
168 su -s /bin/sh postgres -c "dropdb gforge" > /dev/null 2>&1 || true
169 su -s /bin/sh postgres -c "dropuser gforge" > /dev/null 2>&1 || true
170 rm -f /var/lib/postgres/data/gforge_passwd
171 kill -HUP $(head -1 /var/lib/postgres/data/postmaster.pid)
174 if [ "x$2" != "x" ] ;then
177 DUMPFILE=/var/lib/gforge/dumps/db_dump
179 if [ "x$3" != "x" ] ;then
184 echo "Dumping $DB database in $DUMPFILE"
185 su -s /bin/sh $DB -c /usr/lib/postgresql/bin/pg_dump $DB > $DUMPFILE
188 pattern=$(basename $0).XXXXXX
189 newpg=$(mktemp /tmp/$pattern)
190 echo "### Next line inserted by GForge restore" > $newpg
191 echo "local all trust" >> $newpg
192 #echo "host all 127.0.0.1 255.255.255.255 trust" >> $newpg
193 cat /etc/postgresql/pg_hba.conf >> $newpg
194 mv $newpg /etc/postgresql/pg_hba.conf
195 chmod 644 /etc/postgresql/pg_hba.conf
196 /etc/init.d/postgresql restart
197 if [ "x$2" != "x" ] ;then
200 RESTFILE=/var/lib/gforge/dumps/db_dump
202 echo "Restoring $RESTFILE"
203 su -s /bin/sh postgres -c "dropdb gforge" || true
204 su -s /bin/sh postgres -c "createdb gforge" || true
205 su -s /bin/sh postgres -c "/usr/lib/postgresql/bin/psql -f $RESTFILE gforge"
206 perl -pi -e "s/### Next line inserted by GForge restore\n//" /etc/postgresql/pg_hba.conf
207 perl -pi -e "s/local all trust\n//" /etc/postgresql/pg_hba.conf
208 #perl -pi -e "s/host all 127.0.0.1 255.255.255.255 trust\n//" /etc/postgresql/pg_hba.conf
209 /etc/init.d/postgresql restart