drsuapi.idl: fix source_dsa spelling
[samba4-gss.git] / third_party / heimdal / kadmin / check.c
blob7a4350ada72ece85befe11d44e8f6e3b9afb53be
1 /*
2 * Copyright (c) 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Check database for strange configurations on default principals
38 #include "kadmin_locl.h"
39 #include "kadmin-commands.h"
41 static int
42 get_check_entry(const char *name, kadm5_principal_ent_rec *ent)
44 krb5_error_code ret;
45 krb5_principal principal;
47 ret = krb5_parse_name(context, name, &principal);
48 if (ret) {
49 krb5_warn(context, ret, "krb5_unparse_name: %s", name);
50 return 1;
53 memset(ent, 0, sizeof(*ent));
54 ret = kadm5_get_principal(kadm_handle, principal, ent, KADM5_ATTRIBUTES);
55 krb5_free_principal(context, principal);
56 if(ret)
57 return 1;
59 return 0;
63 static int
64 do_check_entry(krb5_principal principal, void *data)
66 krb5_error_code ret;
67 kadm5_principal_ent_rec princ;
68 char *name;
69 int i;
71 ret = krb5_unparse_name(context, principal, &name);
72 if (ret)
73 return 1;
75 memset (&princ, 0, sizeof(princ));
76 ret = kadm5_get_principal(data, principal, &princ,
77 KADM5_PRINCIPAL | KADM5_KEY_DATA);
78 if(ret) {
79 krb5_warn(context, ret, "Failed to get principal: %s", name);
80 free(name);
81 return 0;
84 for (i = 0; i < princ.n_key_data; i++) {
85 size_t keysize;
86 ret = krb5_enctype_keysize(context,
87 princ.key_data[i].key_data_type[0],
88 &keysize);
89 if (ret == 0 && keysize != (size_t)princ.key_data[i].key_data_length[0]) {
90 krb5_warnx(context,
91 "Principal %s enctype %d, wrong length: %d\n",
92 name, princ.key_data[i].key_data_type[0],
93 princ.key_data[i].key_data_length[0]);
97 free(name);
98 kadm5_free_principal_ent(data, &princ);
100 return 0;
104 check(void *opt, int argc, char **argv)
106 kadm5_principal_ent_rec ent;
107 krb5_error_code ret;
108 char *realm = NULL, *p, *p2;
109 void *inner_kadm_handle = NULL;
110 int found;
112 if (argc == 0) {
113 ret = krb5_get_default_realm(context, &realm);
114 if (ret) {
115 krb5_warn(context, ret, "krb5_get_default_realm");
116 goto fail;
118 } else {
119 realm = strdup(argv[0]);
120 if (realm == NULL) {
121 krb5_warnx(context, "malloc");
122 goto fail;
127 * Check krbtgt/REALM@REALM
129 * For now, just check existance
132 if (asprintf(&p, "%s/%s@%s", KRB5_TGS_NAME, realm, realm) == -1) {
133 krb5_warn(context, errno, "asprintf");
134 goto fail;
137 ret = get_check_entry(p, &ent);
138 if (ret) {
139 fprintf(stderr,
140 "%s does not exist, are you sure %s is a realm in your database?\n",
141 p, realm);
142 free(p);
143 goto fail;
145 free(p);
147 kadm5_free_principal_ent(kadm_handle, &ent);
150 * Check kadmin/admin@REALM
153 if (asprintf(&p, "kadmin/admin@%s", realm) == -1) {
154 krb5_warn(context, errno, "asprintf");
155 goto fail;
158 ret = get_check_entry(p, &ent);
159 if (ret) {
160 fprintf(stderr,
161 "%s does not exist, there is no way to do remote administration.\n",
163 free(p);
164 goto fail;
166 free(p);
168 kadm5_free_principal_ent(kadm_handle, &ent);
171 * Check kadmin/changepw@REALM
174 if (asprintf(&p, "kadmin/changepw@%s", realm) == -1) {
175 krb5_warn(context, errno, "asprintf");
176 goto fail;
179 ret = get_check_entry(p, &ent);
180 if (ret) {
181 fprintf(stderr,
182 "%s does not exist, there is no way to do change password.\n",
184 free(p);
185 goto fail;
187 free(p);
189 kadm5_free_principal_ent(kadm_handle, &ent);
192 * Check default@REALM
194 * Check that disallow-all-tix is set on the default principal
195 * (or that the entry does not exist)
198 if (asprintf(&p, "default@%s", realm) == -1) {
199 krb5_warn(context, errno, "asprintf");
200 goto fail;
203 ret = get_check_entry(p, &ent);
204 if (ret == 0) {
205 if ((ent.attributes & KRB5_KDB_DISALLOW_ALL_TIX) == 0) {
206 fprintf(stderr, "default template entry is not disabled\n");
207 ret = EINVAL;
209 kadm5_free_principal_ent(kadm_handle, &ent);
211 } else {
212 ret = 0;
215 free(p);
217 if (ret)
218 goto fail;
221 * Check for duplicate afs keys
224 p2 = strdup(realm);
225 if (p2 == NULL) {
226 krb5_warn(context, errno, "malloc");
227 goto fail;
229 strlwr(p2);
231 if (asprintf(&p, "afs/%s@%s", p2, realm) == -1) {
232 krb5_warn(context, errno, "asprintf");
233 free(p2);
234 goto fail;
236 free(p2);
238 ret = get_check_entry(p, &ent);
239 free(p);
240 if (ret == 0) {
241 kadm5_free_principal_ent(kadm_handle, &ent);
242 found = 1;
243 } else
244 found = 0;
246 if (asprintf(&p, "afs@%s", realm) == -1) {
247 krb5_warn(context, errno, "asprintf");
248 goto fail;
251 ret = get_check_entry(p, &ent);
252 free(p);
253 if (ret == 0) {
254 kadm5_free_principal_ent(kadm_handle, &ent);
255 if (found) {
256 krb5_warnx(context, "afs@REALM and afs/cellname@REALM both exists");
257 goto fail;
261 ret = kadm5_dup_context(kadm_handle, &inner_kadm_handle);
262 if (ret == 0)
263 ret = foreach_principal("*", do_check_entry, "check", inner_kadm_handle);
264 if (inner_kadm_handle)
265 kadm5_destroy(inner_kadm_handle);
266 if (ret) {
267 krb5_warn(context, ret, "Could not iterate principals in realm");
268 goto fail;
271 free(realm);
272 return 0;
273 fail:
274 free(realm);
275 return 1;