xref: /freebsd/crypto/krb5/src/tests/hrealm.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/hrealm.c - Test harness for host-realm interfaces */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2012 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert  * This program is intended to be run from a python script as:
35*7f2fe78bSCy Schubert  *
36*7f2fe78bSCy Schubert  *     hrealm -h|-f|-d [hostname]
37*7f2fe78bSCy Schubert  *
38*7f2fe78bSCy Schubert  * Calls krb5_get_host_realm, krb5_get_fallback_host_realm, or
39*7f2fe78bSCy Schubert  * krb5_default_realm depending on the option given.  For the first two
40*7f2fe78bSCy Schubert  * choices, hostname or NULL is passed as the argument.  The results are
41*7f2fe78bSCy Schubert  * displayed one per line.
42*7f2fe78bSCy Schubert  */
43*7f2fe78bSCy Schubert 
44*7f2fe78bSCy Schubert #include "k5-int.h"
45*7f2fe78bSCy Schubert 
46*7f2fe78bSCy Schubert static krb5_context ctx;
47*7f2fe78bSCy Schubert 
48*7f2fe78bSCy Schubert static void
check(krb5_error_code code)49*7f2fe78bSCy Schubert check(krb5_error_code code)
50*7f2fe78bSCy Schubert {
51*7f2fe78bSCy Schubert     const char *errmsg;
52*7f2fe78bSCy Schubert 
53*7f2fe78bSCy Schubert     if (code) {
54*7f2fe78bSCy Schubert         errmsg = krb5_get_error_message(ctx, code);
55*7f2fe78bSCy Schubert         fprintf(stderr, "%s\n", errmsg);
56*7f2fe78bSCy Schubert         krb5_free_error_message(ctx, errmsg);
57*7f2fe78bSCy Schubert         exit(1);
58*7f2fe78bSCy Schubert     }
59*7f2fe78bSCy Schubert }
60*7f2fe78bSCy Schubert 
61*7f2fe78bSCy Schubert static void
display(char ** realms)62*7f2fe78bSCy Schubert display(char **realms)
63*7f2fe78bSCy Schubert {
64*7f2fe78bSCy Schubert     while (realms != NULL && *realms != NULL)
65*7f2fe78bSCy Schubert         printf("%s\n", *realms++);
66*7f2fe78bSCy Schubert }
67*7f2fe78bSCy Schubert 
68*7f2fe78bSCy Schubert int
main(int argc,char ** argv)69*7f2fe78bSCy Schubert main(int argc, char **argv)
70*7f2fe78bSCy Schubert {
71*7f2fe78bSCy Schubert     krb5_data d;
72*7f2fe78bSCy Schubert     char **realms, *realm;
73*7f2fe78bSCy Schubert 
74*7f2fe78bSCy Schubert     check(krb5_init_context(&ctx));
75*7f2fe78bSCy Schubert 
76*7f2fe78bSCy Schubert     /* Parse arguments. */
77*7f2fe78bSCy Schubert     if (argc < 2 || argc > 3)
78*7f2fe78bSCy Schubert         abort();
79*7f2fe78bSCy Schubert 
80*7f2fe78bSCy Schubert     if (strcmp(argv[1], "-d") == 0) {
81*7f2fe78bSCy Schubert         check(krb5_get_default_realm(ctx, &realm));
82*7f2fe78bSCy Schubert         printf("%s\n", realm);
83*7f2fe78bSCy Schubert         krb5_free_default_realm(ctx, realm);
84*7f2fe78bSCy Schubert     } else if (strcmp(argv[1], "-h") == 0) {
85*7f2fe78bSCy Schubert         check(krb5_get_host_realm(ctx, argv[2], &realms));
86*7f2fe78bSCy Schubert         display(realms);
87*7f2fe78bSCy Schubert         krb5_free_host_realm(ctx, realms);
88*7f2fe78bSCy Schubert     } else if (strcmp(argv[1], "-f") == 0) {
89*7f2fe78bSCy Schubert         assert(argc == 3);
90*7f2fe78bSCy Schubert         d = string2data(argv[2]);
91*7f2fe78bSCy Schubert         check(krb5_get_fallback_host_realm(ctx, &d, &realms));
92*7f2fe78bSCy Schubert         display(realms);
93*7f2fe78bSCy Schubert         krb5_free_host_realm(ctx, realms);
94*7f2fe78bSCy Schubert     } else {
95*7f2fe78bSCy Schubert         abort();
96*7f2fe78bSCy Schubert     }
97*7f2fe78bSCy Schubert     krb5_free_context(ctx);
98*7f2fe78bSCy Schubert     return 0;
99*7f2fe78bSCy Schubert }
100