/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/hrealm.c - Test harness for host-realm interfaces */2/*3* Copyright (C) 2012 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/*33* This program is intended to be run from a python script as:34*35* hrealm -h|-f|-d [hostname]36*37* Calls krb5_get_host_realm, krb5_get_fallback_host_realm, or38* krb5_default_realm depending on the option given. For the first two39* choices, hostname or NULL is passed as the argument. The results are40* displayed one per line.41*/4243#include "k5-int.h"4445static krb5_context ctx;4647static void48check(krb5_error_code code)49{50const char *errmsg;5152if (code) {53errmsg = krb5_get_error_message(ctx, code);54fprintf(stderr, "%s\n", errmsg);55krb5_free_error_message(ctx, errmsg);56exit(1);57}58}5960static void61display(char **realms)62{63while (realms != NULL && *realms != NULL)64printf("%s\n", *realms++);65}6667int68main(int argc, char **argv)69{70krb5_data d;71char **realms, *realm;7273check(krb5_init_context(&ctx));7475/* Parse arguments. */76if (argc < 2 || argc > 3)77abort();7879if (strcmp(argv[1], "-d") == 0) {80check(krb5_get_default_realm(ctx, &realm));81printf("%s\n", realm);82krb5_free_default_realm(ctx, realm);83} else if (strcmp(argv[1], "-h") == 0) {84check(krb5_get_host_realm(ctx, argv[2], &realms));85display(realms);86krb5_free_host_realm(ctx, realms);87} else if (strcmp(argv[1], "-f") == 0) {88assert(argc == 3);89d = string2data(argv[2]);90check(krb5_get_fallback_host_realm(ctx, &d, &realms));91display(realms);92krb5_free_host_realm(ctx, realms);93} else {94abort();95}96krb5_free_context(ctx);97return 0;98}99100101