Commit 3280cba2 authored by Peter Eisentraut's avatar Peter Eisentraut

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.
parent c4432317
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# #
# Makefile for catalog # Makefile for catalog
# #
# $Header: /cvsroot/pgsql/src/backend/catalog/Makefile,v 1.31 2000/10/24 01:38:23 tgl Exp $ # $Header: /cvsroot/pgsql/src/backend/catalog/Makefile,v 1.32 2000/10/28 22:14:14 petere Exp $
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
...@@ -39,11 +39,11 @@ pg_includes := $(sort -I$(top_srcdir)/src/include -I$(top_builddir)/src/include) ...@@ -39,11 +39,11 @@ pg_includes := $(sort -I$(top_srcdir)/src/include -I$(top_builddir)/src/include)
global.bki global.description: genbki.sh $(GLOBAL_BKI_SRCS) $(top_srcdir)/src/include/catalog/indexing.h \ global.bki global.description: genbki.sh $(GLOBAL_BKI_SRCS) $(top_srcdir)/src/include/catalog/indexing.h \
$(top_srcdir)/src/include/postgres_ext.h $(top_builddir)/src/include/config.h $(top_srcdir)/src/include/postgres_ext.h $(top_builddir)/src/include/config.h
CPP='$(CPP)' AWK='$(AWK)' $(SHELL) $< $(BKIOPTS) -o global $(pg_includes) $(GLOBAL_BKI_SRCS) CPP='$(CPP)' AWK='$(AWK)' $(SHELL) $< $(BKIOPTS) -o global $(pg_includes) $(GLOBAL_BKI_SRCS) --set-version=$(VERSION)
template1.bki template1.description: genbki.sh $(TEMPLATE1_BKI_SRCS) \ template1.bki template1.description: genbki.sh $(TEMPLATE1_BKI_SRCS) \
$(top_srcdir)/src/include/postgres_ext.h $(top_builddir)/src/include/config.h $(top_srcdir)/src/include/postgres_ext.h $(top_builddir)/src/include/config.h
CPP='$(CPP)' AWK='$(AWK)' $(SHELL) $< $(BKIOPTS) -o template1 $(pg_includes) $(TEMPLATE1_BKI_SRCS) CPP='$(CPP)' AWK='$(AWK)' $(SHELL) $< $(BKIOPTS) -o template1 $(pg_includes) $(TEMPLATE1_BKI_SRCS) --set-version=$(VERSION)
.PHONY: install-bki .PHONY: install-bki
install-bki: $(BKIFILES) installdirs install-bki: $(BKIFILES) installdirs
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# #
# IDENTIFICATION # IDENTIFICATION
# $Header: /cvsroot/pgsql/src/backend/catalog/Attic/genbki.sh,v 1.17 2000/10/20 21:03:42 petere Exp $ # $Header: /cvsroot/pgsql/src/backend/catalog/Attic/genbki.sh,v 1.18 2000/10/28 22:14:14 petere Exp $
# #
# NOTES # NOTES
# non-essential whitespace is removed from the generated file. # non-essential whitespace is removed from the generated file.
...@@ -28,6 +28,7 @@ BKIOPTS= ...@@ -28,6 +28,7 @@ BKIOPTS=
INCLUDE_DIRS= INCLUDE_DIRS=
OUTPUT_PREFIX= OUTPUT_PREFIX=
INFILES= INFILES=
major_version=
# #
# Process command line switches. # Process command line switches.
...@@ -54,15 +55,20 @@ do ...@@ -54,15 +55,20 @@ do
-o*) -o*)
OUTPUT_PREFIX=`echo $1 | sed -e 's/^-o//'` OUTPUT_PREFIX=`echo $1 | sed -e 's/^-o//'`
;; ;;
--set-version=*)
arg=`expr x"$1" : x"--set-version=\(.*\)"`
major_version=`expr x"$arg" : x'\([0-9][0-9]*\.[0-9][0-9]*\)'`
;;
--help) --help)
echo "$CMDNAME generates system catalog bootstrapping files." echo "$CMDNAME generates system catalog bootstrapping files."
echo echo
echo "Usage:" echo "Usage:"
echo " $CMDNAME [ -D define [...] ] [ -I dir ] [ -o prefix ]" echo " $CMDNAME [ -D define [...] ] [ -I dir ] --set-version=VERSION -o prefix files..."
echo echo
echo "Options:" echo "Options:"
echo " -I path to postgres_ext.h and config.h files" echo " -I path to postgres_ext.h and config.h files"
echo " -o prefix of output files" echo " -o prefix of output files"
echo " --set-version PostgreSQL version number for initdb cross-check"
echo echo
echo "The environment variables CPP and AWK determine which C" echo "The environment variables CPP and AWK determine which C"
echo "preprocessor and Awk program to use. The defaults are" echo "preprocessor and Awk program to use. The defaults are"
...@@ -97,6 +103,10 @@ if [ x"$INCLUDE_DIRS" = x"" ] ; then ...@@ -97,6 +103,10 @@ if [ x"$INCLUDE_DIRS" = x"" ] ; then
exit 1 exit 1
fi fi
if [ x"$major_version" = x"" ] ; then
echo "$CMDNAME: invalid or no version number specified" 1>&2
exit 1
fi
if [ x"$TMPDIR" = x"" ] ; then if [ x"$TMPDIR" = x"" ] ; then
TMPDIR=/tmp TMPDIR=/tmp
...@@ -105,12 +115,7 @@ fi ...@@ -105,12 +115,7 @@ fi
TMPFILE="$TMPDIR/genbkitmp.c" TMPFILE="$TMPDIR/genbkitmp.c"
trap "rm -f $TMPFILE" 0 1 2 3 15 trap "rm -f $TMPFILE ${OUTPUT_PREFIX}.bki.$$ ${OUTPUT_PREFIX}.description.$$" 0 1 2 3 15
# clear output files
> ${OUTPUT_PREFIX}.bki
> ${OUTPUT_PREFIX}.description
# Get NAMEDATALEN from postgres_ext.h # Get NAMEDATALEN from postgres_ext.h
...@@ -136,6 +141,8 @@ done ...@@ -136,6 +141,8 @@ done
INDEXMAXKEYS2=`expr $INDEXMAXKEYS '*' 2` || exit INDEXMAXKEYS2=`expr $INDEXMAXKEYS '*' 2` || exit
INDEXMAXKEYS4=`expr $INDEXMAXKEYS '*' 4` || exit INDEXMAXKEYS4=`expr $INDEXMAXKEYS '*' 4` || exit
touch ${OUTPUT_PREFIX}.description.$$
# ---------------- # ----------------
# strip comments and trash from .h before we generate # strip comments and trash from .h before we generate
# the .bki file... # the .bki file...
...@@ -360,11 +367,15 @@ END { ...@@ -360,11 +367,15 @@ END {
reln_open = 0; reln_open = 0;
} }
} }
' "descriptionfile=${OUTPUT_PREFIX}.description" > $TMPFILE || exit ' "descriptionfile=${OUTPUT_PREFIX}.description.$$" > $TMPFILE || exit
echo "# PostgreSQL $major_version" >${OUTPUT_PREFIX}.bki.$$
$CPP $BKIOPTS $TMPFILE | \ $CPP $BKIOPTS $TMPFILE | \
sed -e '/^[ ]*$/d' \ sed -e '/^[ ]*$/d' \
-e 's/[ ][ ]*/ /g' > ${OUTPUT_PREFIX}.bki || exit -e 's/[ ][ ]*/ /g' >>${OUTPUT_PREFIX}.bki.$$ || exit
mv ${OUTPUT_PREFIX}.bki.$$ ${OUTPUT_PREFIX}.bki || exit
mv ${OUTPUT_PREFIX}.description.$$ ${OUTPUT_PREFIX}.description || exit
exit 0 exit 0
#! /bin/sh #! /bin/sh
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# initdb creates (initializes) a Postgres database cluster (site, # initdb creates (initializes) a PostgreSQL database cluster (site,
# instance, installation, whatever). A database cluster is a # instance, installation, whatever). A database cluster is a
# collection of Postgres databases all managed by the same postmaster. # collection of PostgreSQL databases all managed by the same postmaster.
# #
# To create the database cluster, we create the directory that contains # To create the database cluster, we create the directory that contains
# all its data, create the files that hold the global tables, create # all its data, create the files that hold the global tables, create
# a few other control files for it, and create one database: the # a few other control files for it, and create one database: the
# template database. # template database.
# #
# The template database is an ordinary Postgres database. Its data # The template database is an ordinary PostgreSQL database. Its data
# never changes, though. It exists to make it easy for Postgres to # never changes, though. It exists to make it easy for PostgreSQL to
# create other databases -- it just copies. # create other databases -- it just copies.
# #
# Optionally, we can skip creating the complete database cluster and # Optionally, we can skip creating the complete database cluster and
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
# #
# Copyright (c) 1994, Regents of the University of California # Copyright (c) 1994, Regents of the University of California
# #
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.106 2000/10/22 17:55:45 pjw Exp $ # $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.107 2000/10/28 22:14:14 petere Exp $
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
...@@ -85,7 +85,7 @@ fi ...@@ -85,7 +85,7 @@ fi
if echo "$0" | grep '/' > /dev/null 2>&1 if echo "$0" | grep '/' > /dev/null 2>&1
then then
# explicit dir name given # explicit dir name given
PGPATH=`echo $0 | sed 's,/[^/]*$,,'` # (dirname command is not portable) self_path=`echo $0 | sed 's,/[^/]*$,,'` # (dirname command is not portable)
else else
# look for it in PATH ('which' command is not portable) # look for it in PATH ('which' command is not portable)
for dir in `echo "$PATH" | sed 's/:/ /g'` for dir in `echo "$PATH" | sed 's/:/ /g'`
...@@ -94,36 +94,52 @@ else ...@@ -94,36 +94,52 @@ else
[ -z "$dir" ] && dir='.' [ -z "$dir" ] && dir='.'
if [ -f "$dir/$CMDNAME" ] if [ -f "$dir/$CMDNAME" ]
then then
PGPATH="$dir" self_path="$dir"
break break
fi fi
done done
fi fi
if [ x"$PGPATH" = x"" ] ; then
PGPATH=$bindir # Check for right version of backend. First we check for an
# executable in the same directory is this initdb script (presuming
# the above code worked). Then we fall back to the hard-wired bindir.
# We do it in this order because during upgrades users might move
# their trees to backup places, so $bindir might be inaccurate.
if [ x"$self_path" != x"" ] \
&& [ -x "$self_path/postgres" ] \
&& [ x"`$self_path/postgres --version 2>/dev/null`" == x"postgres (PostgreSQL) $VERSION" ]
then
PGPATH=$self_path
elif [ -x "$bindir/postgres" ]; then
if [ x"`$bindir/postgres --version 2>/dev/null`" == x"postgres (PostgreSQL) $VERSION" ]
then
PGPATH=$bindir
else
echo "The program '$bindir/postgres' needed by $CMDNAME does not belong to"
echo "PostgreSQL version $VERSION. Check your installation."
exit 1
fi
else
echo "The program 'postgres' is needed by $CMDNAME but was not found in"
echo "the directory '$bindir'. Check your installation."
exit 1
fi fi
# Check if needed programs actually exist in path
for prog in postgres pg_id # Now we can assume that 'pg_id' belongs to the same version as the
do # verified 'postgres' in the same directory.
if [ ! -x "$PGPATH/$prog" ] if [ ! -x "$PGPATH/pg_id" ]; then
then echo "The program 'pg_id' is needed by $CMDNAME but was not found in"
echo "The program \`$prog' needed by $CMDNAME could not be found. It was" echo "the directory '$PGPATH'. Check your installation."
echo "expected at:" exit 1
echo " $PGPATH/$prog" fi
echo "If this is not the correct directory, please start $CMDNAME"
echo "with a full search path. Otherwise make sure that the program"
echo "was installed successfully."
exit 1
fi
done
# Gotta wait for pg_id existence check above
EffectiveUser=`$PGPATH/pg_id -n -u` EffectiveUser=`$PGPATH/pg_id -n -u`
if [ -z "$EffectiveUser" ]; then if [ -z "$EffectiveUser" ]; then
echo "Could not determine current user name. You are really hosed." echo "$CMDNAME: could not determine current user name"
exit 1 exit 1
fi fi
...@@ -275,7 +291,7 @@ fi ...@@ -275,7 +291,7 @@ fi
if [ "$MULTIBYTE" ] if [ "$MULTIBYTE" ]
then then
MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE 2> /dev/null` MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE`
if [ "$?" -ne 0 ] if [ "$?" -ne 0 ]
then then
echo "$CMDNAME: pg_encoding failed" echo "$CMDNAME: pg_encoding failed"
...@@ -354,6 +370,15 @@ do ...@@ -354,6 +370,15 @@ do
fi fi
done done
for file in "$TEMPLATE1_BKI" "$GLOBAL_BKI"; do
if [ x"`sed 1q $file`" != x"# PostgreSQL $short_version" ]; then
echo "The input file '$file' needed by $CMDNAME does not"
echo "belong to PostgreSQL $VERSION. Check your installation or specify the"
echo "correct path using the -L option."
exit 1
fi
done
trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15 trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment