xref: /minix3/crypto/external/bsd/heimdal/dist/admin/get.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: get.c,v 1.1.1.2 2014/04/24 12:45:26 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "ktutil_locl.h"
37ebfedea0SLionel Sambuc 
38*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc static void*
open_kadmin_connection(char * principal,const char * realm,char * admin_server,int server_port)41ebfedea0SLionel Sambuc open_kadmin_connection(char *principal,
42ebfedea0SLionel Sambuc 		       const char *realm,
43ebfedea0SLionel Sambuc 		       char *admin_server,
44ebfedea0SLionel Sambuc 		       int server_port)
45ebfedea0SLionel Sambuc {
46ebfedea0SLionel Sambuc     static kadm5_config_params conf;
47ebfedea0SLionel Sambuc     krb5_error_code ret;
48ebfedea0SLionel Sambuc     void *kadm_handle;
49ebfedea0SLionel Sambuc     memset(&conf, 0, sizeof(conf));
50ebfedea0SLionel Sambuc 
51ebfedea0SLionel Sambuc     if(realm) {
52ebfedea0SLionel Sambuc 	conf.realm = strdup(realm);
53ebfedea0SLionel Sambuc 	if (conf.realm == NULL) {
54ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, 0, "malloc: out of memory");
55ebfedea0SLionel Sambuc 	    return NULL;
56ebfedea0SLionel Sambuc 	}
57ebfedea0SLionel Sambuc 	conf.mask |= KADM5_CONFIG_REALM;
58ebfedea0SLionel Sambuc     }
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc     if (admin_server) {
61ebfedea0SLionel Sambuc 	conf.admin_server = admin_server;
62ebfedea0SLionel Sambuc 	conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
63ebfedea0SLionel Sambuc     }
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc     if (server_port) {
66ebfedea0SLionel Sambuc 	conf.kadmind_port = htons(server_port);
67ebfedea0SLionel Sambuc 	conf.mask |= KADM5_CONFIG_KADMIND_PORT;
68ebfedea0SLionel Sambuc     }
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc     /* should get realm from each principal, instead of doing
71ebfedea0SLionel Sambuc        everything with the same (local) realm */
72ebfedea0SLionel Sambuc 
73ebfedea0SLionel Sambuc     ret = kadm5_init_with_password_ctx(context,
74ebfedea0SLionel Sambuc 				       principal,
75ebfedea0SLionel Sambuc 				       NULL,
76ebfedea0SLionel Sambuc 				       KADM5_ADMIN_SERVICE,
77ebfedea0SLionel Sambuc 				       &conf, 0, 0,
78ebfedea0SLionel Sambuc 				       &kadm_handle);
79ebfedea0SLionel Sambuc     free(conf.realm);
80ebfedea0SLionel Sambuc     if(ret) {
81ebfedea0SLionel Sambuc 	krb5_warn(context, ret, "kadm5_init_with_password");
82ebfedea0SLionel Sambuc 	return NULL;
83ebfedea0SLionel Sambuc     }
84ebfedea0SLionel Sambuc     return kadm_handle;
85ebfedea0SLionel Sambuc }
86ebfedea0SLionel Sambuc 
87ebfedea0SLionel Sambuc int
kt_get(struct get_options * opt,int argc,char ** argv)88ebfedea0SLionel Sambuc kt_get(struct get_options *opt, int argc, char **argv)
89ebfedea0SLionel Sambuc {
90ebfedea0SLionel Sambuc     krb5_error_code ret = 0;
91ebfedea0SLionel Sambuc     krb5_keytab keytab;
92ebfedea0SLionel Sambuc     void *kadm_handle = NULL;
93ebfedea0SLionel Sambuc     krb5_enctype *etypes = NULL;
94ebfedea0SLionel Sambuc     size_t netypes = 0;
95*0a6a1f1dSLionel Sambuc     size_t i;
96*0a6a1f1dSLionel Sambuc     int a, j;
97ebfedea0SLionel Sambuc     unsigned int failed = 0;
98ebfedea0SLionel Sambuc 
99ebfedea0SLionel Sambuc     if((keytab = ktutil_open_keytab()) == NULL)
100ebfedea0SLionel Sambuc 	return 1;
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc     if(opt->realm_string)
103ebfedea0SLionel Sambuc 	krb5_set_default_realm(context, opt->realm_string);
104ebfedea0SLionel Sambuc 
105ebfedea0SLionel Sambuc     if (opt->enctypes_strings.num_strings != 0) {
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc 	etypes = malloc (opt->enctypes_strings.num_strings * sizeof(*etypes));
108ebfedea0SLionel Sambuc 	if (etypes == NULL) {
109ebfedea0SLionel Sambuc 	    krb5_warnx(context, "malloc failed");
110ebfedea0SLionel Sambuc 	    goto out;
111ebfedea0SLionel Sambuc 	}
112ebfedea0SLionel Sambuc 	netypes = opt->enctypes_strings.num_strings;
113ebfedea0SLionel Sambuc 	for(i = 0; i < netypes; i++) {
114ebfedea0SLionel Sambuc 	    ret = krb5_string_to_enctype(context,
115ebfedea0SLionel Sambuc 					 opt->enctypes_strings.strings[i],
116ebfedea0SLionel Sambuc 					 &etypes[i]);
117ebfedea0SLionel Sambuc 	    if(ret) {
118ebfedea0SLionel Sambuc 		krb5_warnx(context, "unrecognized enctype: %s",
119ebfedea0SLionel Sambuc 			   opt->enctypes_strings.strings[i]);
120ebfedea0SLionel Sambuc 		goto out;
121ebfedea0SLionel Sambuc 	    }
122ebfedea0SLionel Sambuc 	}
123ebfedea0SLionel Sambuc     }
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc 
126*0a6a1f1dSLionel Sambuc     for(a = 0; a < argc; a++){
127ebfedea0SLionel Sambuc 	krb5_principal princ_ent;
128ebfedea0SLionel Sambuc 	kadm5_principal_ent_rec princ;
129ebfedea0SLionel Sambuc 	int mask = 0;
130ebfedea0SLionel Sambuc 	krb5_keyblock *keys;
131ebfedea0SLionel Sambuc 	int n_keys;
132ebfedea0SLionel Sambuc 	int created = 0;
133ebfedea0SLionel Sambuc 	krb5_keytab_entry entry;
134ebfedea0SLionel Sambuc 
135*0a6a1f1dSLionel Sambuc 	ret = krb5_parse_name(context, argv[a], &princ_ent);
136ebfedea0SLionel Sambuc 	if (ret) {
137*0a6a1f1dSLionel Sambuc 	    krb5_warn(context, ret, "can't parse principal %s", argv[a]);
138ebfedea0SLionel Sambuc 	    failed++;
139ebfedea0SLionel Sambuc 	    continue;
140ebfedea0SLionel Sambuc 	}
141ebfedea0SLionel Sambuc 	memset(&princ, 0, sizeof(princ));
142ebfedea0SLionel Sambuc 	princ.principal = princ_ent;
143ebfedea0SLionel Sambuc 	mask |= KADM5_PRINCIPAL;
144ebfedea0SLionel Sambuc 	princ.attributes |= KRB5_KDB_DISALLOW_ALL_TIX;
145ebfedea0SLionel Sambuc 	mask |= KADM5_ATTRIBUTES;
146ebfedea0SLionel Sambuc 	princ.princ_expire_time = 0;
147ebfedea0SLionel Sambuc 	mask |= KADM5_PRINC_EXPIRE_TIME;
148ebfedea0SLionel Sambuc 
149ebfedea0SLionel Sambuc 	if(kadm_handle == NULL) {
150ebfedea0SLionel Sambuc 	    const char *r;
151ebfedea0SLionel Sambuc 	    if(opt->realm_string != NULL)
152ebfedea0SLionel Sambuc 		r = opt->realm_string;
153ebfedea0SLionel Sambuc 	    else
154ebfedea0SLionel Sambuc 		r = krb5_principal_get_realm(context, princ_ent);
155ebfedea0SLionel Sambuc 	    kadm_handle = open_kadmin_connection(opt->principal_string,
156ebfedea0SLionel Sambuc 						 r,
157ebfedea0SLionel Sambuc 						 opt->admin_server_string,
158ebfedea0SLionel Sambuc 						 opt->server_port_integer);
159ebfedea0SLionel Sambuc 	    if(kadm_handle == NULL)
160ebfedea0SLionel Sambuc 		break;
161ebfedea0SLionel Sambuc 	}
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc 	ret = kadm5_create_principal(kadm_handle, &princ, mask, "x");
164ebfedea0SLionel Sambuc 	if(ret == 0)
165ebfedea0SLionel Sambuc 	    created = 1;
166ebfedea0SLionel Sambuc 	else if(ret != KADM5_DUP) {
167*0a6a1f1dSLionel Sambuc 	    krb5_warn(context, ret, "kadm5_create_principal(%s)", argv[a]);
168ebfedea0SLionel Sambuc 	    krb5_free_principal(context, princ_ent);
169ebfedea0SLionel Sambuc 	    failed++;
170ebfedea0SLionel Sambuc 	    continue;
171ebfedea0SLionel Sambuc 	}
172ebfedea0SLionel Sambuc 	ret = kadm5_randkey_principal(kadm_handle, princ_ent, &keys, &n_keys);
173ebfedea0SLionel Sambuc 	if (ret) {
174*0a6a1f1dSLionel Sambuc 	    krb5_warn(context, ret, "kadm5_randkey_principal(%s)", argv[a]);
175ebfedea0SLionel Sambuc 	    krb5_free_principal(context, princ_ent);
176ebfedea0SLionel Sambuc 	    failed++;
177ebfedea0SLionel Sambuc 	    continue;
178ebfedea0SLionel Sambuc 	}
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc 	ret = kadm5_get_principal(kadm_handle, princ_ent, &princ,
181ebfedea0SLionel Sambuc 			      KADM5_PRINCIPAL | KADM5_KVNO | KADM5_ATTRIBUTES);
182ebfedea0SLionel Sambuc 	if (ret) {
183*0a6a1f1dSLionel Sambuc 	    krb5_warn(context, ret, "kadm5_get_principal(%s)", argv[a]);
184ebfedea0SLionel Sambuc 	    for (j = 0; j < n_keys; j++)
185ebfedea0SLionel Sambuc 		krb5_free_keyblock_contents(context, &keys[j]);
186ebfedea0SLionel Sambuc 	    krb5_free_principal(context, princ_ent);
187ebfedea0SLionel Sambuc 	    failed++;
188ebfedea0SLionel Sambuc 	    continue;
189ebfedea0SLionel Sambuc 	}
190ebfedea0SLionel Sambuc 	if(!created && (princ.attributes & KRB5_KDB_DISALLOW_ALL_TIX))
191*0a6a1f1dSLionel Sambuc 	    krb5_warnx(context, "%s: disallow-all-tix flag set - clearing", argv[a]);
192ebfedea0SLionel Sambuc 	princ.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
193ebfedea0SLionel Sambuc 	mask = KADM5_ATTRIBUTES;
194ebfedea0SLionel Sambuc 	if(created) {
195ebfedea0SLionel Sambuc 	    princ.kvno = 1;
196ebfedea0SLionel Sambuc 	    mask |= KADM5_KVNO;
197ebfedea0SLionel Sambuc 	}
198ebfedea0SLionel Sambuc 	ret = kadm5_modify_principal(kadm_handle, &princ, mask);
199ebfedea0SLionel Sambuc 	if (ret) {
200*0a6a1f1dSLionel Sambuc 	    krb5_warn(context, ret, "kadm5_modify_principal(%s)", argv[a]);
201ebfedea0SLionel Sambuc 	    for (j = 0; j < n_keys; j++)
202ebfedea0SLionel Sambuc 		krb5_free_keyblock_contents(context, &keys[j]);
203ebfedea0SLionel Sambuc 	    krb5_free_principal(context, princ_ent);
204ebfedea0SLionel Sambuc 	    failed++;
205ebfedea0SLionel Sambuc 	    continue;
206ebfedea0SLionel Sambuc 	}
207ebfedea0SLionel Sambuc 	for(j = 0; j < n_keys; j++) {
208ebfedea0SLionel Sambuc 	    int do_add = TRUE;
209ebfedea0SLionel Sambuc 
210ebfedea0SLionel Sambuc 	    if (netypes) {
211*0a6a1f1dSLionel Sambuc 		size_t k;
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc 		do_add = FALSE;
214ebfedea0SLionel Sambuc 		for (k = 0; k < netypes; ++k)
215ebfedea0SLionel Sambuc 		    if (keys[j].keytype == etypes[k]) {
216ebfedea0SLionel Sambuc 			do_add = TRUE;
217ebfedea0SLionel Sambuc 			break;
218ebfedea0SLionel Sambuc 		    }
219ebfedea0SLionel Sambuc 	    }
220ebfedea0SLionel Sambuc 	    if (do_add) {
221ebfedea0SLionel Sambuc 		entry.principal = princ_ent;
222ebfedea0SLionel Sambuc 		entry.vno = princ.kvno;
223ebfedea0SLionel Sambuc 		entry.keyblock = keys[j];
224ebfedea0SLionel Sambuc 		entry.timestamp = time (NULL);
225ebfedea0SLionel Sambuc 		ret = krb5_kt_add_entry(context, keytab, &entry);
226ebfedea0SLionel Sambuc 		if (ret)
227ebfedea0SLionel Sambuc 		    krb5_warn(context, ret, "krb5_kt_add_entry");
228ebfedea0SLionel Sambuc 	    }
229ebfedea0SLionel Sambuc 	    krb5_free_keyblock_contents(context, &keys[j]);
230ebfedea0SLionel Sambuc 	}
231ebfedea0SLionel Sambuc 
232ebfedea0SLionel Sambuc 	kadm5_free_principal_ent(kadm_handle, &princ);
233ebfedea0SLionel Sambuc 	krb5_free_principal(context, princ_ent);
234ebfedea0SLionel Sambuc     }
235ebfedea0SLionel Sambuc  out:
236ebfedea0SLionel Sambuc     free(etypes);
237ebfedea0SLionel Sambuc     if (kadm_handle)
238ebfedea0SLionel Sambuc 	kadm5_destroy(kadm_handle);
239ebfedea0SLionel Sambuc     krb5_kt_close(context, keytab);
240ebfedea0SLionel Sambuc     return ret != 0 || failed > 0;
241ebfedea0SLionel Sambuc }
242