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