| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | |
| 3 | # |
| 4 | # Copyright (C) 2013 Red Hat, Inc. |
| 5 | # SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 | |
| 7 | set -euf |
| 8 | |
| 9 | cd $(dirnamescip-shell system . . dirname. $0) |
| 10 | |
| 11 | warningscip-shell shell shell-project 0.0.0 warning().() |
| 12 | { |
| 13 | echoscip-shell system . . echo. "mock-kdc: $*" >&2 |
| 14 | } |
| 15 | |
| 16 | check_requirescip-shell shell shell-project 0.0.0 check_require().() |
| 17 | { |
| 18 | for n in "$@"; do |
| 19 | if ! whichscip-shell system . . which. $n 2> /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 20 | warningscip-shell shell shell-project 0.0.0 warning(). "$n not present" |
| 21 | exit 18 |
| 22 | fi |
| 23 | done |
| 24 | } |
| 25 | |
| 26 | check_requirescip-shell shell shell-project 0.0.0 check_require(). kadmin.local krb5kdc kdb5_util |
| 27 | |
| 28 | dir=$(mktempscip-shell system . . mktemp. -d) |
| 29 | |
| 30 | # Generate the KDC configuration file |
| 31 | sedscip-shell system . . sed. -e "s,[@]dir[@],$dir,g" mock-krb5.conf.in > $dir/krb5.conf |
| 32 | sedscip-shell system . . sed. -e "s,[@]dir[@],$dir,g" mock-kdc.conf.in > $dir/kdc.conf |
| 33 | KRB5_KDC_PROFILE=$dir/kdc.conf |
| 34 | export KRB5_KDC_PROFILE |
| 35 | KRB5_CONFIG=$dir/krb5.conf |
| 36 | export KRB5_CONFIG |
| 37 | |
| 38 | echoscip-shell system . . echo. "KRB5_CONFIG=$dir/krb5.conf" |
| 39 | echoscip-shell system . . echo. "KRB5_KTNAME=$dir/localhost.keytab" |
| 40 | |
| 41 | touchscip-shell system . . touch. $dir/kadmind.log |
| 42 | |
| 43 | # Generate the KDC database, undocumented -W argument: no strong random |
| 44 | kdb5_util create -r COCKPIT.MOCK -s -W -P foobar > /dev/nullscip-shell filesystem . . `/dev/null`. |
| 45 | |
| 46 | # Generate the ACL file |
| 47 | echoscip-shell system . . echo. "*/[email protected] *" > $dir/kadm5.acl |
| 48 | |
| 49 | # Add the users |
| 50 | kadmin="kadmin.local -r COCKPIT.MOCK" |
| 51 | $kadmin -q "addprinc -pw foobar -clearpolicy root/admin"# > /dev/nullscip-shell filesystem . . `/dev/null`. |
| 52 | $kadmin -q "addprinc -pw marmalade -clearpolicy $USER" > /dev/nullscip-shell filesystem . . `/dev/null`. |
| 53 | |
| 54 | unique_localhostsscip-shell shell shell-project 0.0.0 unique_localhosts().() |
| 55 | { |
| 56 | grepscip-shell system . . grep. '\blocalhost\b' /etc/hostsscip-shell filesystem . . `/etc/hosts`. | while read addr names; do |
| 57 | for name in $names; do |
| 58 | echoscip-shell system . . echo. $name |
| 59 | done |
| 60 | done | sortscip-shell system . . sort. | uniqscip-shell system . . uniq. |
| 61 | } |
| 62 | |
| 63 | # Add the hosts |
| 64 | unique_localhostsscip-shell shell shell-project 0.0.0 unique_localhosts(). | while read name; do |
| 65 | $kadmin -q "addprinc -randkey -clearpolicy host/$name@COCKPIT.MOCK" > /dev/nullscip-shell filesystem . . `/dev/null`. |
| 66 | $kadmin -q "ktadd -q -keytab $dir/localhost.keytab host/$name@COCKPIT.MOCK" > /dev/nullscip-shell filesystem . . `/dev/null`. |
| 67 | done |
| 68 | |
| 69 | # Run krb5kdc |
| 70 | LC_ALL=C krb5kdc -n -r COCKPIT.MOCK 2>&1 |
| 71 | |
| 72 | rmscip-shell system . . rm. -rf $dir/ |