1/*
2 * Copyright (c) 2013 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2013 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36
37#include "kuser_locl.h"
38#include <config.h>
39#include "kcc-commands.h"
40
41static const struct {
42    char *name;
43    int type;
44} types[] = {
45    { "kdc", KRB5_KRBHST_KDC },
46    { "kadmin", KRB5_KRBHST_ADMIN },
47    { "changepw", KRB5_KRBHST_CHANGEPW },
48    { "krb524", KRB5_KRBHST_KRB524 },
49    { "kkdcp", KRB5_KRBHST_KKDCP }
50};
51
52int
53kdc(struct kdc_options *opt, int argc, char **argv)
54{
55    krb5_krbhst_handle handle;
56    int type = KRB5_KRBHST_KDC;
57    char host[MAXHOSTNAMELEN];
58    krb5_error_code ret;
59    int first_realm = 1;
60    int i;
61
62    if (argc == 0) {
63	printf("give at least on realm\n");
64	return 1;
65    }
66
67    if (opt->type_string) {
68
69	for (i = 0; i < sizeof(types)/sizeof(types[0]); i++) {
70	    if (strcasecmp(types[i].name, opt->type_string) == 0)
71		type = types[i].type;
72	    if (i == sizeof(types)/sizeof(types[0])) {
73		printf("unknown type: %s\n", opt->type_string);
74		return 1;
75	    }
76	}
77    }
78
79    if (opt->json_flag)
80	printf("{");
81
82    for (i = 0; i < argc; i++) {
83	const char *realm = argv[i];
84
85	ret = krb5_krbhst_init(kcc_context, realm, type, &handle);
86	if (ret) {
87	    krb5_warn(kcc_context, ret, "krb5_krbhst_init");
88	    return 1;
89	}
90
91	if (opt->json_flag) {
92	    int first = 1;
93	    printf("%s\n\t%s = [ ", first_realm ? "" : ",", realm);
94	    first_realm = 0;
95
96	    while(krb5_krbhst_next_as_string(kcc_context, handle, host, sizeof(host)) == 0) {
97		printf("%s\n\t\t\"%s\"", first ? "" : ",", host);
98		first = 0;
99	    }
100
101	    printf("\n\t]");
102	} else {
103	    printf("[realms]\n");
104	    printf("\t%s = {\n", realm);
105
106	    while(krb5_krbhst_next_as_string(kcc_context, handle, host, sizeof(host)) == 0)
107		printf("\t\tkdc = %s\n", host);
108
109	    printf("\t}\n");
110	}
111
112	krb5_krbhst_free(kcc_context, handle);
113    }
114
115    if (opt->json_flag)
116	printf("\n}\n");
117
118    return 0;
119}
120