challenger

OAuth 2.0-based authentication service that validates user can receive messages at a certain address
Log | Files | Refs | Submodules | README | LICENSE

challenger-dbconfig (3858B)


      1 #!/bin/bash
      2 # This file is part of GNU TALER.
      3 # Copyright (C) 2023 Taler Systems SA
      4 #
      5 # TALER is free software; you can redistribute it and/or modify it under the
      6 # terms of the GNU Lesser General Public License as published by the Free Software
      7 # Foundation; either version 2.1, or (at your option) any later version.
      8 #
      9 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU Lesser General Public License along with
     14 # TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 #
     16 # @author Christian Grothoff
     17 #
     18 #
     19 # Error checking on
     20 set -eu
     21 
     22 RESET_DB=0
     23 SKIP_DBINIT=0
     24 DBUSER="challenger-httpd"
     25 CFGFILE="/etc/challenger/challenger.conf"
     26 
     27 
     28 
     29 # Parse command-line options
     30 while getopts 'c:hrsu:' OPTION; do
     31   case "$OPTION" in
     32   c)
     33     CFGFILE="$OPTARG"
     34     ;;
     35   h)
     36     echo 'Supported options:'
     37     echo "  -c FILENAME  -- write configuration to FILENAME (default: $CFGFILE)"
     38     echo "  -h           -- print this help text"
     39     echo "  -r           -- reset database (dangerous)"
     40     echo "  -s           -- skip database initialization"
     41     echo "  -u USER      -- challenger-httpd to be run by USER (default: $DBUSER)"
     42     exit 0
     43     ;;
     44   r)
     45     RESET_DB="1"
     46     ;;
     47   s)
     48     SKIP_DBINIT="1"
     49     ;;
     50   u)
     51     DBUSER="$OPTARG"
     52     ;;
     53   ?)
     54     echo "Unrecognized command line option '$OPTION'" 1>&2
     55     exit 1
     56     ;;
     57   esac
     58 done
     59 
     60 if ! id postgres >/dev/null;
     61 then
     62   echo "Could not find 'postgres' user. Please install Postgresql first"
     63   exit 1
     64 fi
     65 
     66 if [ "$(id -u)" -ne 0 ];
     67 then
     68   echo "This script must be run as root"
     69   exit 1
     70 fi
     71 
     72 if [ 0 = "$SKIP_DBINIT" ];
     73 then
     74     if ! challenger-dbinit -v 2>/dev/null;
     75     then
     76 	echo "Required 'challenger-dbinit' not found. Please fix your installation."
     77 	exit 1
     78     fi
     79     DBINIT=$(which challenger-dbinit)
     80 fi
     81 
     82 if ! id "$DBUSER" >/dev/null;
     83 then
     84     echo "Could not find '$DBUSER' user. Please create it up first"
     85     exit 1
     86 fi
     87 
     88 echo "Setting up database user $DBUSER." 1>&2
     89 
     90 if ! sudo -i -u postgres createuser "$DBUSER" 2>/dev/null;
     91 then
     92     echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2
     93 fi
     94 
     95 DBPATH=$(challenger-config \
     96   -c "$CFGFILE" \
     97   -s challengerdb-postgres \
     98   -o CONFIG)
     99 
    100 if ! echo "$DBPATH" | grep "postgres://" >/dev/null;
    101 then
    102     echo "Invalid database configuration value '$DBPATH'." 1>&2
    103     exit 1
    104 fi
    105 
    106 # Drop the query string ('?host=/var/run/postgresql') *first*: it may contain
    107 # slashes, and the greedy 'postgres:\/\/.*\/' would then match up to the last
    108 # slash of the socket path and yield 'postgresql' as the database name --
    109 # which is what '-r' would proceed to drop.  Only the authority part (which
    110 # never contains a slash) is stripped from the front.
    111 DBNAME=$(echo "$DBPATH" |
    112   sed \
    113     -e "s/?.*//" \
    114     -e "s/^postgres:\/\/[^\/]*\///")
    115 
    116 if sudo -i -u postgres psql "$DBNAME" </dev/null 2>/dev/null;
    117 then
    118     if [ 1 = "$RESET_DB" ];
    119     then
    120 	echo "Deleting existing database '$DBNAME'." 1>&2
    121 	if ! sudo -i -u postgres dropdb "$DBNAME";
    122 	then
    123 	    echo "Failed to delete existing database '$DBNAME'"
    124 	    exit 1
    125 	fi
    126 	DO_CREATE=1
    127     else
    128 	echo "Database '$DBNAME' already exists, continuing anyway."
    129 	DO_CREATE=0
    130     fi
    131 else
    132     DO_CREATE=1
    133 fi
    134 
    135 if [ 1 = "$DO_CREATE" ];
    136 then
    137     echo "Creating database '$DBNAME'." 1>&2
    138 
    139     if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME";
    140     then
    141 	echo "Failed to create database '$DBNAME'"
    142 	exit 1
    143     fi
    144 fi
    145 
    146 if [ 0 = "$SKIP_DBINIT" ];
    147 then
    148     echo "Initializing database '$DBNAME'." 1>&2
    149     if ! sudo -u "$DBUSER" "$DBINIT" -c "$CFGFILE";
    150     then
    151 	echo "Failed to initialize database schema"
    152 	exit 1
    153     fi
    154 fi
    155 
    156 echo "Database configuration finished." 1>&2
    157 
    158 exit 0