xref: /freebsd/contrib/pam-krb5/tests/data/generate-krb5-conf (revision bf6873c5786e333d679a7838d28812febf479a8a)
1#!/bin/sh
2
3# Generate a krb5.conf file in the current directory for testing purposes.
4# Takes one command-line argument: the default realm to use.  Strips out the
5# entire [appdefaults] section to avoid picking up any local configuration and
6# sets the default realm as indicated.
7#
8# The canonical version of this file is maintained in the rra-c-util package,
9# which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
10#
11# Written by Russ Allbery <eagle@eyrie.org>
12# Copyright 2016, 2020 Russ Allbery <eagle@eyrie.org>
13# Copyright 2006-2008, 2010-2011
14#     The Board of Trustees of the Leland Stanford Junior University
15#
16# Permission is hereby granted, free of charge, to any person obtaining a copy
17# of this software and associated documentation files (the "Software"), to
18# deal in the Software without restriction, including without limitation the
19# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
20# sell copies of the Software, and to permit persons to whom the Software is
21# furnished to do so, subject to the following conditions:
22#
23# The above copyright notice and this permission notice shall be included in
24# all copies or substantial portions of the Software.
25#
26# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
29# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32# IN THE SOFTWARE.
33#
34# SPDX-License-Identifier: MIT
35
36set -e
37
38# Load the test library.
39. "$C_TAP_SOURCE/tap/libtap.sh"
40cd "$C_TAP_BUILD"
41
42# If there is no default realm specified on the command line, we leave the
43# realm information alone.
44realm="$1"
45
46# Locate the krb5.conf file to use as a base.  Prefer the one in the test
47# configuration area, if it exists.
48krb5conf=`test_file_path config/krb5.conf`
49if [ -z "$krb5conf" ] ; then
50    for p in /etc/krb5.conf /usr/local/etc/krb5.conf ; do
51        if [ -r "$p" ] ; then
52            krb5conf="$p"
53            break
54        fi
55    done
56fi
57if [ -z "$krb5conf" ] ; then
58    echo 'no krb5.conf found, see test instructions' >&2
59    exit 1
60fi
61
62# We found a krb5.conf file.  Generate our munged one.
63mkdir -p tmp
64awk '
65    BEGIN                             { skip = 0 }
66    /^ *\[appdefaults\]/              { skip = 1 }
67    !/^ *\[appdefaults\]/ && / *\[/   { skip = 0 }
68
69    { if (skip == 0) print }
70' "$krb5conf" > tmp/krb5.conf.tmp
71if [ -n "$realm" ] ; then
72    pattern='^[ 	]*default_realm.*='
73    if grep "$pattern" tmp/krb5.conf.tmp >/dev/null 2>/dev/null; then
74        sed -e "s/\\(default_realm.*=\\) .*/\\1 $realm/" \
75            tmp/krb5.conf.tmp >tmp/krb5.conf
76    else
77        (
78            cat tmp/krb5.conf.tmp
79            echo "[libdefaults]"
80            echo "    default_realm = $realm"
81        ) >tmp/krb5.conf
82    fi
83    rm tmp/krb5.conf.tmp
84else
85    mv tmp/krb5.conf.tmp tmp/krb5.conf
86fi
87