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