Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3280cba

Browse files
committed
Make initdb safe against using
a) mismatching backend program, by checking --version output b) mismatching bki files, by putting a version-identifying comment atop those files.
1 parent c443231 commit 3280cba

File tree

3 files changed

+75
-39
lines changed

3 files changed

+75
-39
lines changed

src/backend/catalog/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Makefile for catalog
44
#
5-
# $Header: /cvsroot/pgsql/src/backend/catalog/Makefile,v 1.31 2000/10/24 01:38:23 tgl Exp $
5+
# $Header: /cvsroot/pgsql/src/backend/catalog/Makefile,v 1.32 2000/10/28 22:14:14 petere Exp $
66
#
77
#-------------------------------------------------------------------------
88

@@ -39,11 +39,11 @@ pg_includes := $(sort -I$(top_srcdir)/src/include -I$(top_builddir)/src/include)
3939

4040
global.bki global.description: genbki.sh $(GLOBAL_BKI_SRCS) $(top_srcdir)/src/include/catalog/indexing.h \
4141
$(top_srcdir)/src/include/postgres_ext.h $(top_builddir)/src/include/config.h
42-
CPP='$(CPP)' AWK='$(AWK)' $(SHELL) $< $(BKIOPTS) -o global $(pg_includes) $(GLOBAL_BKI_SRCS)
42+
CPP='$(CPP)' AWK='$(AWK)' $(SHELL) $< $(BKIOPTS) -o global $(pg_includes) $(GLOBAL_BKI_SRCS) --set-version=$(VERSION)
4343

4444
template1.bki template1.description: genbki.sh $(TEMPLATE1_BKI_SRCS) \
4545
$(top_srcdir)/src/include/postgres_ext.h $(top_builddir)/src/include/config.h
46-
CPP='$(CPP)' AWK='$(AWK)' $(SHELL) $< $(BKIOPTS) -o template1 $(pg_includes) $(TEMPLATE1_BKI_SRCS)
46+
CPP='$(CPP)' AWK='$(AWK)' $(SHELL) $< $(BKIOPTS) -o template1 $(pg_includes) $(TEMPLATE1_BKI_SRCS) --set-version=$(VERSION)
4747

4848
.PHONY: install-bki
4949
install-bki: $(BKIFILES) installdirs

src/backend/catalog/genbki.sh

+21-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
#
1212
# IDENTIFICATION
13-
# $Header: /cvsroot/pgsql/src/backend/catalog/Attic/genbki.sh,v 1.17 2000/10/20 21:03:42 petere Exp $
13+
# $Header: /cvsroot/pgsql/src/backend/catalog/Attic/genbki.sh,v 1.18 2000/10/28 22:14:14 petere Exp $
1414
#
1515
# NOTES
1616
# non-essential whitespace is removed from the generated file.
@@ -28,6 +28,7 @@ BKIOPTS=
2828
INCLUDE_DIRS=
2929
OUTPUT_PREFIX=
3030
INFILES=
31+
major_version=
3132

3233
#
3334
# Process command line switches.
@@ -54,15 +55,20 @@ do
5455
-o*)
5556
OUTPUT_PREFIX=`echo $1 | sed -e 's/^-o//'`
5657
;;
58+
--set-version=*)
59+
arg=`expr x"$1" : x"--set-version=\(.*\)"`
60+
major_version=`expr x"$arg" : x'\([0-9][0-9]*\.[0-9][0-9]*\)'`
61+
;;
5762
--help)
5863
echo "$CMDNAME generates system catalog bootstrapping files."
5964
echo
6065
echo "Usage:"
61-
echo " $CMDNAME [ -D define [...] ] [ -I dir ] [ -o prefix ]"
66+
echo " $CMDNAME [ -D define [...] ] [ -I dir ] --set-version=VERSION -o prefix files..."
6267
echo
6368
echo "Options:"
6469
echo " -I path to postgres_ext.h and config.h files"
6570
echo " -o prefix of output files"
71+
echo " --set-version PostgreSQL version number for initdb cross-check"
6672
echo
6773
echo "The environment variables CPP and AWK determine which C"
6874
echo "preprocessor and Awk program to use. The defaults are"
@@ -97,6 +103,10 @@ if [ x"$INCLUDE_DIRS" = x"" ] ; then
97103
exit 1
98104
fi
99105

106+
if [ x"$major_version" = x"" ] ; then
107+
echo "$CMDNAME: invalid or no version number specified" 1>&2
108+
exit 1
109+
fi
100110

101111
if [ x"$TMPDIR" = x"" ] ; then
102112
TMPDIR=/tmp
@@ -105,12 +115,7 @@ fi
105115

106116
TMPFILE="$TMPDIR/genbkitmp.c"
107117

108-
trap "rm -f $TMPFILE" 0 1 2 3 15
109-
110-
111-
# clear output files
112-
> ${OUTPUT_PREFIX}.bki
113-
> ${OUTPUT_PREFIX}.description
118+
trap "rm -f $TMPFILE ${OUTPUT_PREFIX}.bki.$$ ${OUTPUT_PREFIX}.description.$$" 0 1 2 3 15
114119

115120

116121
# Get NAMEDATALEN from postgres_ext.h
@@ -136,6 +141,8 @@ done
136141
INDEXMAXKEYS2=`expr $INDEXMAXKEYS '*' 2` || exit
137142
INDEXMAXKEYS4=`expr $INDEXMAXKEYS '*' 4` || exit
138143

144+
touch ${OUTPUT_PREFIX}.description.$$
145+
139146
# ----------------
140147
# strip comments and trash from .h before we generate
141148
# the .bki file...
@@ -360,11 +367,15 @@ END {
360367
reln_open = 0;
361368
}
362369
}
363-
' "descriptionfile=${OUTPUT_PREFIX}.description" > $TMPFILE || exit
370+
' "descriptionfile=${OUTPUT_PREFIX}.description.$$" > $TMPFILE || exit
371+
372+
echo "# PostgreSQL $major_version" >${OUTPUT_PREFIX}.bki.$$
364373

365374
$CPP $BKIOPTS $TMPFILE | \
366375
sed -e '/^[ ]*$/d' \
367-
-e 's/[ ][ ]*/ /g' > ${OUTPUT_PREFIX}.bki || exit
376+
-e 's/[ ][ ]*/ /g' >>${OUTPUT_PREFIX}.bki.$$ || exit
368377

378+
mv ${OUTPUT_PREFIX}.bki.$$ ${OUTPUT_PREFIX}.bki || exit
379+
mv ${OUTPUT_PREFIX}.description.$$ ${OUTPUT_PREFIX}.description || exit
369380

370381
exit 0

src/bin/initdb/initdb.sh

+51-26
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#! /bin/sh
22
#-------------------------------------------------------------------------
33
#
4-
# initdb creates (initializes) a Postgres database cluster (site,
4+
# initdb creates (initializes) a PostgreSQL database cluster (site,
55
# instance, installation, whatever). A database cluster is a
6-
# collection of Postgres databases all managed by the same postmaster.
6+
# collection of PostgreSQL databases all managed by the same postmaster.
77
#
88
# To create the database cluster, we create the directory that contains
99
# all its data, create the files that hold the global tables, create
1010
# a few other control files for it, and create one database: the
1111
# template database.
1212
#
13-
# The template database is an ordinary Postgres database. Its data
14-
# never changes, though. It exists to make it easy for Postgres to
13+
# The template database is an ordinary PostgreSQL database. Its data
14+
# never changes, though. It exists to make it easy for PostgreSQL to
1515
# create other databases -- it just copies.
1616
#
1717
# Optionally, we can skip creating the complete database cluster and
@@ -23,7 +23,7 @@
2323
#
2424
# Copyright (c) 1994, Regents of the University of California
2525
#
26-
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.106 2000/10/22 17:55:45 pjw Exp $
26+
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.107 2000/10/28 22:14:14 petere Exp $
2727
#
2828
#-------------------------------------------------------------------------
2929

@@ -85,7 +85,7 @@ fi
8585
if echo "$0" | grep '/' > /dev/null 2>&1
8686
then
8787
# explicit dir name given
88-
PGPATH=`echo $0 | sed 's,/[^/]*$,,'` # (dirname command is not portable)
88+
self_path=`echo $0 | sed 's,/[^/]*$,,'` # (dirname command is not portable)
8989
else
9090
# look for it in PATH ('which' command is not portable)
9191
for dir in `echo "$PATH" | sed 's/:/ /g'`
@@ -94,36 +94,52 @@ else
9494
[ -z "$dir" ] && dir='.'
9595
if [ -f "$dir/$CMDNAME" ]
9696
then
97-
PGPATH="$dir"
97+
self_path="$dir"
9898
break
9999
fi
100100
done
101101
fi
102102

103-
if [ x"$PGPATH" = x"" ] ; then
104-
PGPATH=$bindir
103+
104+
# Check for right version of backend. First we check for an
105+
# executable in the same directory is this initdb script (presuming
106+
# the above code worked). Then we fall back to the hard-wired bindir.
107+
# We do it in this order because during upgrades users might move
108+
# their trees to backup places, so $bindir might be inaccurate.
109+
110+
if [ x"$self_path" != x"" ] \
111+
&& [ -x "$self_path/postgres" ] \
112+
&& [ x"`$self_path/postgres --version 2>/dev/null`" == x"postgres (PostgreSQL) $VERSION" ]
113+
then
114+
PGPATH=$self_path
115+
elif [ -x "$bindir/postgres" ]; then
116+
if [ x"`$bindir/postgres --version 2>/dev/null`" == x"postgres (PostgreSQL) $VERSION" ]
117+
then
118+
PGPATH=$bindir
119+
else
120+
echo "The program '$bindir/postgres' needed by $CMDNAME does not belong to"
121+
echo "PostgreSQL version $VERSION. Check your installation."
122+
exit 1
123+
fi
124+
else
125+
echo "The program 'postgres' is needed by $CMDNAME but was not found in"
126+
echo "the directory '$bindir'. Check your installation."
127+
exit 1
105128
fi
106129

107-
# Check if needed programs actually exist in path
108-
for prog in postgres pg_id
109-
do
110-
if [ ! -x "$PGPATH/$prog" ]
111-
then
112-
echo "The program \`$prog' needed by $CMDNAME could not be found. It was"
113-
echo "expected at:"
114-
echo " $PGPATH/$prog"
115-
echo "If this is not the correct directory, please start $CMDNAME"
116-
echo "with a full search path. Otherwise make sure that the program"
117-
echo "was installed successfully."
118-
exit 1
119-
fi
120-
done
130+
131+
# Now we can assume that 'pg_id' belongs to the same version as the
132+
# verified 'postgres' in the same directory.
133+
if [ ! -x "$PGPATH/pg_id" ]; then
134+
echo "The program 'pg_id' is needed by $CMDNAME but was not found in"
135+
echo "the directory '$PGPATH'. Check your installation."
136+
exit 1
137+
fi
121138

122139

123-
# Gotta wait for pg_id existence check above
124140
EffectiveUser=`$PGPATH/pg_id -n -u`
125141
if [ -z "$EffectiveUser" ]; then
126-
echo "Could not determine current user name. You are really hosed."
142+
echo "$CMDNAME: could not determine current user name"
127143
exit 1
128144
fi
129145

@@ -275,7 +291,7 @@ fi
275291

276292
if [ "$MULTIBYTE" ]
277293
then
278-
MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE 2> /dev/null`
294+
MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE`
279295
if [ "$?" -ne 0 ]
280296
then
281297
echo "$CMDNAME: pg_encoding failed"
@@ -354,6 +370,15 @@ do
354370
fi
355371
done
356372

373+
for file in "$TEMPLATE1_BKI" "$GLOBAL_BKI"; do
374+
if [ x"`sed 1q $file`" != x"# PostgreSQL $short_version" ]; then
375+
echo "The input file '$file' needed by $CMDNAME does not"
376+
echo "belong to PostgreSQL $VERSION. Check your installation or specify the"
377+
echo "correct path using the -L option."
378+
exit 1
379+
fi
380+
done
381+
357382

358383
trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15
359384

0 commit comments

Comments
 (0)